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 <sound/core.h>
37 #include <sound/jack.h>
38 #include <sound/asoundef.h>
39 #include <sound/tlv.h>
40 #include <sound/hdaudio.h>
41 #include <sound/hda_i915.h>
42 #include "hda_codec.h"
43 #include "hda_local.h"
46 static bool static_hdmi_pcm;
47 module_param(static_hdmi_pcm, bool, 0644);
48 MODULE_PARM_DESC(static_hdmi_pcm, "Don't restrict PCM parameters per ELD info");
50 #define is_haswell(codec) ((codec)->core.vendor_id == 0x80862807)
51 #define is_broadwell(codec) ((codec)->core.vendor_id == 0x80862808)
52 #define is_skylake(codec) ((codec)->core.vendor_id == 0x80862809)
53 #define is_broxton(codec) ((codec)->core.vendor_id == 0x8086280a)
54 #define is_kabylake(codec) ((codec)->core.vendor_id == 0x8086280b)
55 #define is_haswell_plus(codec) (is_haswell(codec) || is_broadwell(codec) \
56 || is_skylake(codec) || is_broxton(codec) \
57 || is_kabylake(codec))
59 #define is_valleyview(codec) ((codec)->core.vendor_id == 0x80862882)
60 #define is_cherryview(codec) ((codec)->core.vendor_id == 0x80862883)
61 #define is_valleyview_plus(codec) (is_valleyview(codec) || is_cherryview(codec))
63 struct hdmi_spec_per_cvt {
66 unsigned int channels_min;
67 unsigned int channels_max;
73 /* max. connections to a widget */
74 #define HDA_MAX_CONNECTIONS 32
76 struct hdmi_spec_per_pin {
78 /* pin idx, different device entries on the same pin use the same idx */
81 hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
85 struct hda_codec *codec;
86 struct hdmi_eld sink_eld;
88 struct delayed_work work;
89 struct hdmi_pcm *pcm; /* pointer to spec->pcm_rec[n] dynamically*/
90 int pcm_idx; /* which pcm is attached. -1 means no pcm is attached */
92 bool setup; /* the stream has been set up by prepare callback */
93 int channels; /* current number of channels */
95 bool chmap_set; /* channel-map override by ALSA API? */
96 unsigned char chmap[8]; /* ALSA API channel-map */
97 #ifdef CONFIG_SND_PROC_FS
98 struct snd_info_entry *proc_entry;
102 struct cea_channel_speaker_allocation;
104 /* operations used by generic code that can be overridden by patches */
106 int (*pin_get_eld)(struct hda_codec *codec, hda_nid_t pin_nid,
107 unsigned char *buf, int *eld_size);
109 /* get and set channel assigned to each HDMI ASP (audio sample packet) slot */
110 int (*pin_get_slot_channel)(struct hda_codec *codec, hda_nid_t pin_nid,
112 int (*pin_set_slot_channel)(struct hda_codec *codec, hda_nid_t pin_nid,
113 int asp_slot, int channel);
115 void (*pin_setup_infoframe)(struct hda_codec *codec, hda_nid_t pin_nid,
116 int ca, int active_channels, int conn_type);
118 /* enable/disable HBR (HD passthrough) */
119 int (*pin_hbr_setup)(struct hda_codec *codec, hda_nid_t pin_nid, bool hbr);
121 int (*setup_stream)(struct hda_codec *codec, hda_nid_t cvt_nid,
122 hda_nid_t pin_nid, u32 stream_tag, int format);
124 /* Helpers for producing the channel map TLVs. These can be overridden
125 * for devices that have non-standard mapping requirements. */
126 int (*chmap_cea_alloc_validate_get_type)(struct cea_channel_speaker_allocation *cap,
128 void (*cea_alloc_to_tlv_chmap)(struct cea_channel_speaker_allocation *cap,
129 unsigned int *chmap, int channels);
131 /* check that the user-given chmap is supported */
132 int (*chmap_validate)(int ca, int channels, unsigned char *chmap);
137 struct snd_jack *jack;
138 struct snd_kcontrol *eld_ctl;
143 struct snd_array cvts; /* struct hdmi_spec_per_cvt */
144 hda_nid_t cvt_nids[4]; /* only for haswell fix */
147 struct snd_array pins; /* struct hdmi_spec_per_pin */
148 struct hdmi_pcm pcm_rec[16];
149 struct mutex pcm_lock;
150 /* pcm_bitmap means which pcms have been assigned to pins*/
151 unsigned long pcm_bitmap;
152 int pcm_used; /* counter of pcm_rec[] */
153 /* bitmap shows whether the pcm is opened in user space
154 * bit 0 means the first playback PCM (PCM3);
155 * bit 1 means the second playback PCM, and so on.
157 unsigned long pcm_in_use;
158 unsigned int channels_max; /* max over all cvts */
160 struct hdmi_eld temp_eld;
166 * Non-generic VIA/NVIDIA specific
168 struct hda_multi_out multiout;
169 struct hda_pcm_stream pcm_playback;
171 /* i915/powerwell (Haswell+/Valleyview+) specific */
172 struct i915_audio_component_audio_ops i915_audio_ops;
173 bool i915_bound; /* was i915 bound in this driver? */
176 #ifdef CONFIG_SND_HDA_I915
177 #define codec_has_acomp(codec) \
178 ((codec)->bus->core.audio_component != NULL)
180 #define codec_has_acomp(codec) false
183 struct hdmi_audio_infoframe {
190 u8 CC02_CT47; /* CC in bits 0:2, CT in 4:7 */
194 u8 LFEPBL01_LSV36_DM_INH7;
197 struct dp_audio_infoframe {
200 u8 ver; /* 0x11 << 2 */
202 u8 CC02_CT47; /* match with HDMI infoframe from this on */
206 u8 LFEPBL01_LSV36_DM_INH7;
209 union audio_infoframe {
210 struct hdmi_audio_infoframe hdmi;
211 struct dp_audio_infoframe dp;
216 * CEA speaker placement:
219 * FLW FL FLC FC FRC FR FRW
226 * The Left/Right Surround channel _notions_ LS/RS in SMPTE 320M corresponds to
227 * CEA RL/RR; The SMPTE channel _assignment_ C/LFE is swapped to CEA LFE/FC.
229 enum cea_speaker_placement {
230 FL = (1 << 0), /* Front Left */
231 FC = (1 << 1), /* Front Center */
232 FR = (1 << 2), /* Front Right */
233 FLC = (1 << 3), /* Front Left Center */
234 FRC = (1 << 4), /* Front Right Center */
235 RL = (1 << 5), /* Rear Left */
236 RC = (1 << 6), /* Rear Center */
237 RR = (1 << 7), /* Rear Right */
238 RLC = (1 << 8), /* Rear Left Center */
239 RRC = (1 << 9), /* Rear Right Center */
240 LFE = (1 << 10), /* Low Frequency Effect */
241 FLW = (1 << 11), /* Front Left Wide */
242 FRW = (1 << 12), /* Front Right Wide */
243 FLH = (1 << 13), /* Front Left High */
244 FCH = (1 << 14), /* Front Center High */
245 FRH = (1 << 15), /* Front Right High */
246 TC = (1 << 16), /* Top Center */
250 * ELD SA bits in the CEA Speaker Allocation data block
252 static int eld_speaker_allocation_bits[] = {
260 /* the following are not defined in ELD yet */
267 struct cea_channel_speaker_allocation {
271 /* derived values, just for convenience */
279 * surround40 surround41 surround50 surround51 surround71
280 * ch0 front left = = = =
281 * ch1 front right = = = =
282 * ch2 rear left = = = =
283 * ch3 rear right = = = =
284 * ch4 LFE center center center
289 * surround71 = {FL, FR, RLC, RRC, FC, LFE, RL, RR}
291 static int hdmi_channel_mapping[0x32][8] = {
293 [0x00] = { 0x00, 0x11, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
295 [0x01] = { 0x00, 0x11, 0x22, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
297 [0x02] = { 0x00, 0x11, 0x23, 0xf2, 0xf4, 0xf5, 0xf6, 0xf7 },
299 [0x08] = { 0x00, 0x11, 0x24, 0x35, 0xf3, 0xf2, 0xf6, 0xf7 },
301 [0x03] = { 0x00, 0x11, 0x23, 0x32, 0x44, 0xf5, 0xf6, 0xf7 },
303 [0x09] = { 0x00, 0x11, 0x24, 0x35, 0x42, 0xf3, 0xf6, 0xf7 },
305 [0x0a] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0xf2, 0xf6, 0xf7 },
307 [0x0b] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0x52, 0xf6, 0xf7 },
309 [0x13] = { 0x00, 0x11, 0x26, 0x37, 0x43, 0x52, 0x64, 0x75 },
313 * This is an ordered list!
315 * The preceding ones have better chances to be selected by
316 * hdmi_channel_allocation().
318 static struct cea_channel_speaker_allocation channel_allocations[] = {
319 /* channel: 7 6 5 4 3 2 1 0 */
320 { .ca_index = 0x00, .speakers = { 0, 0, 0, 0, 0, 0, FR, FL } },
322 { .ca_index = 0x01, .speakers = { 0, 0, 0, 0, 0, LFE, FR, FL } },
324 { .ca_index = 0x02, .speakers = { 0, 0, 0, 0, FC, 0, FR, FL } },
326 { .ca_index = 0x08, .speakers = { 0, 0, RR, RL, 0, 0, FR, FL } },
328 { .ca_index = 0x09, .speakers = { 0, 0, RR, RL, 0, LFE, FR, FL } },
330 { .ca_index = 0x0a, .speakers = { 0, 0, RR, RL, FC, 0, FR, FL } },
332 { .ca_index = 0x0b, .speakers = { 0, 0, RR, RL, FC, LFE, FR, FL } },
334 { .ca_index = 0x0f, .speakers = { 0, RC, RR, RL, FC, LFE, FR, FL } },
336 { .ca_index = 0x13, .speakers = { RRC, RLC, RR, RL, FC, LFE, FR, FL } },
338 { .ca_index = 0x03, .speakers = { 0, 0, 0, 0, FC, LFE, FR, FL } },
339 { .ca_index = 0x04, .speakers = { 0, 0, 0, RC, 0, 0, FR, FL } },
340 { .ca_index = 0x05, .speakers = { 0, 0, 0, RC, 0, LFE, FR, FL } },
341 { .ca_index = 0x06, .speakers = { 0, 0, 0, RC, FC, 0, FR, FL } },
342 { .ca_index = 0x07, .speakers = { 0, 0, 0, RC, FC, LFE, FR, FL } },
343 { .ca_index = 0x0c, .speakers = { 0, RC, RR, RL, 0, 0, FR, FL } },
344 { .ca_index = 0x0d, .speakers = { 0, RC, RR, RL, 0, LFE, FR, FL } },
345 { .ca_index = 0x0e, .speakers = { 0, RC, RR, RL, FC, 0, FR, FL } },
346 { .ca_index = 0x10, .speakers = { RRC, RLC, RR, RL, 0, 0, FR, FL } },
347 { .ca_index = 0x11, .speakers = { RRC, RLC, RR, RL, 0, LFE, FR, FL } },
348 { .ca_index = 0x12, .speakers = { RRC, RLC, RR, RL, FC, 0, FR, FL } },
349 { .ca_index = 0x14, .speakers = { FRC, FLC, 0, 0, 0, 0, FR, FL } },
350 { .ca_index = 0x15, .speakers = { FRC, FLC, 0, 0, 0, LFE, FR, FL } },
351 { .ca_index = 0x16, .speakers = { FRC, FLC, 0, 0, FC, 0, FR, FL } },
352 { .ca_index = 0x17, .speakers = { FRC, FLC, 0, 0, FC, LFE, FR, FL } },
353 { .ca_index = 0x18, .speakers = { FRC, FLC, 0, RC, 0, 0, FR, FL } },
354 { .ca_index = 0x19, .speakers = { FRC, FLC, 0, RC, 0, LFE, FR, FL } },
355 { .ca_index = 0x1a, .speakers = { FRC, FLC, 0, RC, FC, 0, FR, FL } },
356 { .ca_index = 0x1b, .speakers = { FRC, FLC, 0, RC, FC, LFE, FR, FL } },
357 { .ca_index = 0x1c, .speakers = { FRC, FLC, RR, RL, 0, 0, FR, FL } },
358 { .ca_index = 0x1d, .speakers = { FRC, FLC, RR, RL, 0, LFE, FR, FL } },
359 { .ca_index = 0x1e, .speakers = { FRC, FLC, RR, RL, FC, 0, FR, FL } },
360 { .ca_index = 0x1f, .speakers = { FRC, FLC, RR, RL, FC, LFE, FR, FL } },
361 { .ca_index = 0x20, .speakers = { 0, FCH, RR, RL, FC, 0, FR, FL } },
362 { .ca_index = 0x21, .speakers = { 0, FCH, RR, RL, FC, LFE, FR, FL } },
363 { .ca_index = 0x22, .speakers = { TC, 0, RR, RL, FC, 0, FR, FL } },
364 { .ca_index = 0x23, .speakers = { TC, 0, RR, RL, FC, LFE, FR, FL } },
365 { .ca_index = 0x24, .speakers = { FRH, FLH, RR, RL, 0, 0, FR, FL } },
366 { .ca_index = 0x25, .speakers = { FRH, FLH, RR, RL, 0, LFE, FR, FL } },
367 { .ca_index = 0x26, .speakers = { FRW, FLW, RR, RL, 0, 0, FR, FL } },
368 { .ca_index = 0x27, .speakers = { FRW, FLW, RR, RL, 0, LFE, FR, FL } },
369 { .ca_index = 0x28, .speakers = { TC, RC, RR, RL, FC, 0, FR, FL } },
370 { .ca_index = 0x29, .speakers = { TC, RC, RR, RL, FC, LFE, FR, FL } },
371 { .ca_index = 0x2a, .speakers = { FCH, RC, RR, RL, FC, 0, FR, FL } },
372 { .ca_index = 0x2b, .speakers = { FCH, RC, RR, RL, FC, LFE, FR, FL } },
373 { .ca_index = 0x2c, .speakers = { TC, FCH, RR, RL, FC, 0, FR, FL } },
374 { .ca_index = 0x2d, .speakers = { TC, FCH, RR, RL, FC, LFE, FR, FL } },
375 { .ca_index = 0x2e, .speakers = { FRH, FLH, RR, RL, FC, 0, FR, FL } },
376 { .ca_index = 0x2f, .speakers = { FRH, FLH, RR, RL, FC, LFE, FR, FL } },
377 { .ca_index = 0x30, .speakers = { FRW, FLW, RR, RL, FC, 0, FR, FL } },
378 { .ca_index = 0x31, .speakers = { FRW, FLW, RR, RL, FC, LFE, FR, FL } },
386 #define get_pin(spec, idx) \
387 ((struct hdmi_spec_per_pin *)snd_array_elem(&spec->pins, idx))
388 #define get_cvt(spec, idx) \
389 ((struct hdmi_spec_per_cvt *)snd_array_elem(&spec->cvts, idx))
390 /* obtain hdmi_pcm object assigned to idx */
391 #define get_hdmi_pcm(spec, idx) (&(spec)->pcm_rec[idx])
392 /* obtain hda_pcm object assigned to idx */
393 #define get_pcm_rec(spec, idx) (get_hdmi_pcm(spec, idx)->pcm)
395 static int pin_nid_to_pin_index(struct hda_codec *codec, hda_nid_t pin_nid)
397 struct hdmi_spec *spec = codec->spec;
400 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++)
401 if (get_pin(spec, pin_idx)->pin_nid == pin_nid)
404 codec_warn(codec, "HDMI: pin nid %d not registered\n", pin_nid);
408 static int hinfo_to_pcm_index(struct hda_codec *codec,
409 struct hda_pcm_stream *hinfo)
411 struct hdmi_spec *spec = codec->spec;
414 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++)
415 if (get_pcm_rec(spec, pcm_idx)->stream == hinfo)
418 codec_warn(codec, "HDMI: hinfo %p not registered\n", hinfo);
422 static int hinfo_to_pin_index(struct hda_codec *codec,
423 struct hda_pcm_stream *hinfo)
425 struct hdmi_spec *spec = codec->spec;
426 struct hdmi_spec_per_pin *per_pin;
429 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
430 per_pin = get_pin(spec, pin_idx);
432 per_pin->pcm->pcm->stream == hinfo)
436 codec_dbg(codec, "HDMI: hinfo %p not registered\n", hinfo);
440 static struct hdmi_spec_per_pin *pcm_idx_to_pin(struct hdmi_spec *spec,
444 struct hdmi_spec_per_pin *per_pin;
446 for (i = 0; i < spec->num_pins; i++) {
447 per_pin = get_pin(spec, i);
448 if (per_pin->pcm_idx == pcm_idx)
454 static int cvt_nid_to_cvt_index(struct hda_codec *codec, hda_nid_t cvt_nid)
456 struct hdmi_spec *spec = codec->spec;
459 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++)
460 if (get_cvt(spec, cvt_idx)->cvt_nid == cvt_nid)
463 codec_warn(codec, "HDMI: cvt nid %d not registered\n", cvt_nid);
467 static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
468 struct snd_ctl_elem_info *uinfo)
470 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
471 struct hdmi_spec *spec = codec->spec;
472 struct hdmi_spec_per_pin *per_pin;
473 struct hdmi_eld *eld;
476 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
478 pcm_idx = kcontrol->private_value;
479 mutex_lock(&spec->pcm_lock);
480 per_pin = pcm_idx_to_pin(spec, pcm_idx);
482 /* no pin is bound to the pcm */
484 mutex_unlock(&spec->pcm_lock);
487 eld = &per_pin->sink_eld;
488 uinfo->count = eld->eld_valid ? eld->eld_size : 0;
489 mutex_unlock(&spec->pcm_lock);
494 static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
495 struct snd_ctl_elem_value *ucontrol)
497 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
498 struct hdmi_spec *spec = codec->spec;
499 struct hdmi_spec_per_pin *per_pin;
500 struct hdmi_eld *eld;
503 pcm_idx = kcontrol->private_value;
504 mutex_lock(&spec->pcm_lock);
505 per_pin = pcm_idx_to_pin(spec, pcm_idx);
507 /* no pin is bound to the pcm */
508 memset(ucontrol->value.bytes.data, 0,
509 ARRAY_SIZE(ucontrol->value.bytes.data));
510 mutex_unlock(&spec->pcm_lock);
513 eld = &per_pin->sink_eld;
515 if (eld->eld_size > ARRAY_SIZE(ucontrol->value.bytes.data) ||
516 eld->eld_size > ELD_MAX_SIZE) {
517 mutex_unlock(&spec->pcm_lock);
522 memset(ucontrol->value.bytes.data, 0,
523 ARRAY_SIZE(ucontrol->value.bytes.data));
525 memcpy(ucontrol->value.bytes.data, eld->eld_buffer,
527 mutex_unlock(&spec->pcm_lock);
532 static struct snd_kcontrol_new eld_bytes_ctl = {
533 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
534 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
536 .info = hdmi_eld_ctl_info,
537 .get = hdmi_eld_ctl_get,
540 static int hdmi_create_eld_ctl(struct hda_codec *codec, int pcm_idx,
543 struct snd_kcontrol *kctl;
544 struct hdmi_spec *spec = codec->spec;
547 kctl = snd_ctl_new1(&eld_bytes_ctl, codec);
550 kctl->private_value = pcm_idx;
551 kctl->id.device = device;
553 /* no pin nid is associated with the kctl now
554 * tbd: associate pin nid to eld ctl later
556 err = snd_hda_ctl_add(codec, 0, kctl);
560 get_hdmi_pcm(spec, pcm_idx)->eld_ctl = kctl;
565 static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
566 int *packet_index, int *byte_index)
570 val = snd_hda_codec_read(codec, pin_nid, 0,
571 AC_VERB_GET_HDMI_DIP_INDEX, 0);
573 *packet_index = val >> 5;
574 *byte_index = val & 0x1f;
578 static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
579 int packet_index, int byte_index)
583 val = (packet_index << 5) | (byte_index & 0x1f);
585 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
588 static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t pin_nid,
591 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
594 static void hdmi_init_pin(struct hda_codec *codec, hda_nid_t pin_nid)
596 struct hdmi_spec *spec = codec->spec;
600 if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
601 snd_hda_codec_write(codec, pin_nid, 0,
602 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
604 if (spec->dyn_pin_out)
605 /* Disable pin out until stream is active */
608 /* Enable pin out: some machines with GM965 gets broken output
609 * when the pin is disabled or changed while using with HDMI
613 snd_hda_codec_write(codec, pin_nid, 0,
614 AC_VERB_SET_PIN_WIDGET_CONTROL, pin_out);
617 static int hdmi_get_channel_count(struct hda_codec *codec, hda_nid_t cvt_nid)
619 return 1 + snd_hda_codec_read(codec, cvt_nid, 0,
620 AC_VERB_GET_CVT_CHAN_COUNT, 0);
623 static void hdmi_set_channel_count(struct hda_codec *codec,
624 hda_nid_t cvt_nid, int chs)
626 if (chs != hdmi_get_channel_count(codec, cvt_nid))
627 snd_hda_codec_write(codec, cvt_nid, 0,
628 AC_VERB_SET_CVT_CHAN_COUNT, chs - 1);
635 #ifdef CONFIG_SND_PROC_FS
636 static void print_eld_info(struct snd_info_entry *entry,
637 struct snd_info_buffer *buffer)
639 struct hdmi_spec_per_pin *per_pin = entry->private_data;
641 mutex_lock(&per_pin->lock);
642 snd_hdmi_print_eld_info(&per_pin->sink_eld, buffer);
643 mutex_unlock(&per_pin->lock);
646 static void write_eld_info(struct snd_info_entry *entry,
647 struct snd_info_buffer *buffer)
649 struct hdmi_spec_per_pin *per_pin = entry->private_data;
651 mutex_lock(&per_pin->lock);
652 snd_hdmi_write_eld_info(&per_pin->sink_eld, buffer);
653 mutex_unlock(&per_pin->lock);
656 static int eld_proc_new(struct hdmi_spec_per_pin *per_pin, int index)
659 struct hda_codec *codec = per_pin->codec;
660 struct snd_info_entry *entry;
663 snprintf(name, sizeof(name), "eld#%d.%d", codec->addr, index);
664 err = snd_card_proc_new(codec->card, name, &entry);
668 snd_info_set_text_ops(entry, per_pin, print_eld_info);
669 entry->c.text.write = write_eld_info;
670 entry->mode |= S_IWUSR;
671 per_pin->proc_entry = entry;
676 static void eld_proc_free(struct hdmi_spec_per_pin *per_pin)
678 if (!per_pin->codec->bus->shutdown) {
679 snd_info_free_entry(per_pin->proc_entry);
680 per_pin->proc_entry = NULL;
684 static inline int eld_proc_new(struct hdmi_spec_per_pin *per_pin,
689 static inline void eld_proc_free(struct hdmi_spec_per_pin *per_pin)
695 * Channel mapping routines
699 * Compute derived values in channel_allocations[].
701 static void init_channel_allocations(void)
704 struct cea_channel_speaker_allocation *p;
706 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
707 p = channel_allocations + i;
710 for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
711 if (p->speakers[j]) {
713 p->spk_mask |= p->speakers[j];
718 static int get_channel_allocation_order(int ca)
722 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
723 if (channel_allocations[i].ca_index == ca)
730 * The transformation takes two steps:
732 * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
733 * spk_mask => (channel_allocations[]) => ai->CA
735 * TODO: it could select the wrong CA from multiple candidates.
737 static int hdmi_channel_allocation(struct hda_codec *codec,
738 struct hdmi_eld *eld, int channels)
743 char buf[SND_PRINT_CHANNEL_ALLOCATION_ADVISED_BUFSIZE];
746 * CA defaults to 0 for basic stereo audio
752 * expand ELD's speaker allocation mask
754 * ELD tells the speaker mask in a compact(paired) form,
755 * expand ELD's notions to match the ones used by Audio InfoFrame.
757 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
758 if (eld->info.spk_alloc & (1 << i))
759 spk_mask |= eld_speaker_allocation_bits[i];
762 /* search for the first working match in the CA table */
763 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
764 if (channels == channel_allocations[i].channels &&
765 (spk_mask & channel_allocations[i].spk_mask) ==
766 channel_allocations[i].spk_mask) {
767 ca = channel_allocations[i].ca_index;
773 /* if there was no match, select the regular ALSA channel
774 * allocation with the matching number of channels */
775 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
776 if (channels == channel_allocations[i].channels) {
777 ca = channel_allocations[i].ca_index;
783 snd_print_channel_allocation(eld->info.spk_alloc, buf, sizeof(buf));
784 codec_dbg(codec, "HDMI: select CA 0x%x for %d-channel allocation: %s\n",
790 static void hdmi_debug_channel_mapping(struct hda_codec *codec,
793 #ifdef CONFIG_SND_DEBUG_VERBOSE
794 struct hdmi_spec *spec = codec->spec;
798 for (i = 0; i < 8; i++) {
799 channel = spec->ops.pin_get_slot_channel(codec, pin_nid, i);
800 codec_dbg(codec, "HDMI: ASP channel %d => slot %d\n",
806 static void hdmi_std_setup_channel_mapping(struct hda_codec *codec,
811 struct hdmi_spec *spec = codec->spec;
812 struct cea_channel_speaker_allocation *ch_alloc;
816 int non_pcm_mapping[8];
818 order = get_channel_allocation_order(ca);
819 ch_alloc = &channel_allocations[order];
821 if (hdmi_channel_mapping[ca][1] == 0) {
823 /* fill actual channel mappings in ALSA channel (i) order */
824 for (i = 0; i < ch_alloc->channels; i++) {
825 while (!ch_alloc->speakers[7 - hdmi_slot] && !WARN_ON(hdmi_slot >= 8))
826 hdmi_slot++; /* skip zero slots */
828 hdmi_channel_mapping[ca][i] = (i << 4) | hdmi_slot++;
830 /* fill the rest of the slots with ALSA channel 0xf */
831 for (hdmi_slot = 0; hdmi_slot < 8; hdmi_slot++)
832 if (!ch_alloc->speakers[7 - hdmi_slot])
833 hdmi_channel_mapping[ca][i++] = (0xf << 4) | hdmi_slot;
837 for (i = 0; i < ch_alloc->channels; i++)
838 non_pcm_mapping[i] = (i << 4) | i;
840 non_pcm_mapping[i] = (0xf << 4) | i;
843 for (i = 0; i < 8; i++) {
844 int slotsetup = non_pcm ? non_pcm_mapping[i] : hdmi_channel_mapping[ca][i];
845 int hdmi_slot = slotsetup & 0x0f;
846 int channel = (slotsetup & 0xf0) >> 4;
847 err = spec->ops.pin_set_slot_channel(codec, pin_nid, hdmi_slot, channel);
849 codec_dbg(codec, "HDMI: channel mapping failed\n");
855 struct channel_map_table {
856 unsigned char map; /* ALSA API channel map position */
857 int spk_mask; /* speaker position bit mask */
860 static struct channel_map_table map_tables[] = {
861 { SNDRV_CHMAP_FL, FL },
862 { SNDRV_CHMAP_FR, FR },
863 { SNDRV_CHMAP_RL, RL },
864 { SNDRV_CHMAP_RR, RR },
865 { SNDRV_CHMAP_LFE, LFE },
866 { SNDRV_CHMAP_FC, FC },
867 { SNDRV_CHMAP_RLC, RLC },
868 { SNDRV_CHMAP_RRC, RRC },
869 { SNDRV_CHMAP_RC, RC },
870 { SNDRV_CHMAP_FLC, FLC },
871 { SNDRV_CHMAP_FRC, FRC },
872 { SNDRV_CHMAP_TFL, FLH },
873 { SNDRV_CHMAP_TFR, FRH },
874 { SNDRV_CHMAP_FLW, FLW },
875 { SNDRV_CHMAP_FRW, FRW },
876 { SNDRV_CHMAP_TC, TC },
877 { SNDRV_CHMAP_TFC, FCH },
881 /* from ALSA API channel position to speaker bit mask */
882 static int to_spk_mask(unsigned char c)
884 struct channel_map_table *t = map_tables;
885 for (; t->map; t++) {
892 /* from ALSA API channel position to CEA slot */
893 static int to_cea_slot(int ordered_ca, unsigned char pos)
895 int mask = to_spk_mask(pos);
899 for (i = 0; i < 8; i++) {
900 if (channel_allocations[ordered_ca].speakers[7 - i] == mask)
908 /* from speaker bit mask to ALSA API channel position */
909 static int spk_to_chmap(int spk)
911 struct channel_map_table *t = map_tables;
912 for (; t->map; t++) {
913 if (t->spk_mask == spk)
919 /* from CEA slot to ALSA API channel position */
920 static int from_cea_slot(int ordered_ca, unsigned char slot)
922 int mask = channel_allocations[ordered_ca].speakers[7 - slot];
924 return spk_to_chmap(mask);
927 /* get the CA index corresponding to the given ALSA API channel map */
928 static int hdmi_manual_channel_allocation(int chs, unsigned char *map)
930 int i, spks = 0, spk_mask = 0;
932 for (i = 0; i < chs; i++) {
933 int mask = to_spk_mask(map[i]);
940 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
941 if ((chs == channel_allocations[i].channels ||
942 spks == channel_allocations[i].channels) &&
943 (spk_mask & channel_allocations[i].spk_mask) ==
944 channel_allocations[i].spk_mask)
945 return channel_allocations[i].ca_index;
950 /* set up the channel slots for the given ALSA API channel map */
951 static int hdmi_manual_setup_channel_mapping(struct hda_codec *codec,
953 int chs, unsigned char *map,
956 struct hdmi_spec *spec = codec->spec;
957 int ordered_ca = get_channel_allocation_order(ca);
958 int alsa_pos, hdmi_slot;
959 int assignments[8] = {[0 ... 7] = 0xf};
961 for (alsa_pos = 0; alsa_pos < chs; alsa_pos++) {
963 hdmi_slot = to_cea_slot(ordered_ca, map[alsa_pos]);
966 continue; /* unassigned channel */
968 assignments[hdmi_slot] = alsa_pos;
971 for (hdmi_slot = 0; hdmi_slot < 8; hdmi_slot++) {
974 err = spec->ops.pin_set_slot_channel(codec, pin_nid, hdmi_slot,
975 assignments[hdmi_slot]);
982 /* store ALSA API channel map from the current default map */
983 static void hdmi_setup_fake_chmap(unsigned char *map, int ca)
986 int ordered_ca = get_channel_allocation_order(ca);
987 for (i = 0; i < 8; i++) {
988 if (i < channel_allocations[ordered_ca].channels)
989 map[i] = from_cea_slot(ordered_ca, hdmi_channel_mapping[ca][i] & 0x0f);
995 static void hdmi_setup_channel_mapping(struct hda_codec *codec,
996 hda_nid_t pin_nid, bool non_pcm, int ca,
997 int channels, unsigned char *map,
1000 if (!non_pcm && chmap_set) {
1001 hdmi_manual_setup_channel_mapping(codec, pin_nid,
1004 hdmi_std_setup_channel_mapping(codec, pin_nid, non_pcm, ca);
1005 hdmi_setup_fake_chmap(map, ca);
1008 hdmi_debug_channel_mapping(codec, pin_nid);
1011 static int hdmi_pin_set_slot_channel(struct hda_codec *codec, hda_nid_t pin_nid,
1012 int asp_slot, int channel)
1014 return snd_hda_codec_write(codec, pin_nid, 0,
1015 AC_VERB_SET_HDMI_CHAN_SLOT,
1016 (channel << 4) | asp_slot);
1019 static int hdmi_pin_get_slot_channel(struct hda_codec *codec, hda_nid_t pin_nid,
1022 return (snd_hda_codec_read(codec, pin_nid, 0,
1023 AC_VERB_GET_HDMI_CHAN_SLOT,
1024 asp_slot) & 0xf0) >> 4;
1028 * Audio InfoFrame routines
1032 * Enable Audio InfoFrame Transmission
1034 static void hdmi_start_infoframe_trans(struct hda_codec *codec,
1037 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
1038 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
1043 * Disable Audio InfoFrame Transmission
1045 static void hdmi_stop_infoframe_trans(struct hda_codec *codec,
1048 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
1049 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
1050 AC_DIPXMIT_DISABLE);
1053 static void hdmi_debug_dip_size(struct hda_codec *codec, hda_nid_t pin_nid)
1055 #ifdef CONFIG_SND_DEBUG_VERBOSE
1059 size = snd_hdmi_get_eld_size(codec, pin_nid);
1060 codec_dbg(codec, "HDMI: ELD buf size is %d\n", size);
1062 for (i = 0; i < 8; i++) {
1063 size = snd_hda_codec_read(codec, pin_nid, 0,
1064 AC_VERB_GET_HDMI_DIP_SIZE, i);
1065 codec_dbg(codec, "HDMI: DIP GP[%d] buf size is %d\n", i, size);
1070 static void hdmi_clear_dip_buffers(struct hda_codec *codec, hda_nid_t pin_nid)
1076 for (i = 0; i < 8; i++) {
1077 size = snd_hda_codec_read(codec, pin_nid, 0,
1078 AC_VERB_GET_HDMI_DIP_SIZE, i);
1082 hdmi_set_dip_index(codec, pin_nid, i, 0x0);
1083 for (j = 1; j < 1000; j++) {
1084 hdmi_write_dip_byte(codec, pin_nid, 0x0);
1085 hdmi_get_dip_index(codec, pin_nid, &pi, &bi);
1087 codec_dbg(codec, "dip index %d: %d != %d\n",
1089 if (bi == 0) /* byte index wrapped around */
1093 "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
1099 static void hdmi_checksum_audio_infoframe(struct hdmi_audio_infoframe *hdmi_ai)
1101 u8 *bytes = (u8 *)hdmi_ai;
1105 hdmi_ai->checksum = 0;
1107 for (i = 0; i < sizeof(*hdmi_ai); i++)
1110 hdmi_ai->checksum = -sum;
1113 static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
1119 hdmi_debug_dip_size(codec, pin_nid);
1120 hdmi_clear_dip_buffers(codec, pin_nid); /* be paranoid */
1122 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
1123 for (i = 0; i < size; i++)
1124 hdmi_write_dip_byte(codec, pin_nid, dip[i]);
1127 static bool hdmi_infoframe_uptodate(struct hda_codec *codec, hda_nid_t pin_nid,
1133 if (snd_hda_codec_read(codec, pin_nid, 0, AC_VERB_GET_HDMI_DIP_XMIT, 0)
1137 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
1138 for (i = 0; i < size; i++) {
1139 val = snd_hda_codec_read(codec, pin_nid, 0,
1140 AC_VERB_GET_HDMI_DIP_DATA, 0);
1148 static void hdmi_pin_setup_infoframe(struct hda_codec *codec,
1150 int ca, int active_channels,
1153 union audio_infoframe ai;
1155 memset(&ai, 0, sizeof(ai));
1156 if (conn_type == 0) { /* HDMI */
1157 struct hdmi_audio_infoframe *hdmi_ai = &ai.hdmi;
1159 hdmi_ai->type = 0x84;
1160 hdmi_ai->ver = 0x01;
1161 hdmi_ai->len = 0x0a;
1162 hdmi_ai->CC02_CT47 = active_channels - 1;
1164 hdmi_checksum_audio_infoframe(hdmi_ai);
1165 } else if (conn_type == 1) { /* DisplayPort */
1166 struct dp_audio_infoframe *dp_ai = &ai.dp;
1170 dp_ai->ver = 0x11 << 2;
1171 dp_ai->CC02_CT47 = active_channels - 1;
1174 codec_dbg(codec, "HDMI: unknown connection type at pin %d\n",
1180 * sizeof(ai) is used instead of sizeof(*hdmi_ai) or
1181 * sizeof(*dp_ai) to avoid partial match/update problems when
1182 * the user switches between HDMI/DP monitors.
1184 if (!hdmi_infoframe_uptodate(codec, pin_nid, ai.bytes,
1187 "hdmi_pin_setup_infoframe: pin=%d channels=%d ca=0x%02x\n",
1189 active_channels, ca);
1190 hdmi_stop_infoframe_trans(codec, pin_nid);
1191 hdmi_fill_audio_infoframe(codec, pin_nid,
1192 ai.bytes, sizeof(ai));
1193 hdmi_start_infoframe_trans(codec, pin_nid);
1197 static void hdmi_setup_audio_infoframe(struct hda_codec *codec,
1198 struct hdmi_spec_per_pin *per_pin,
1201 struct hdmi_spec *spec = codec->spec;
1202 hda_nid_t pin_nid = per_pin->pin_nid;
1203 int channels = per_pin->channels;
1204 int active_channels;
1205 struct hdmi_eld *eld;
1211 if (is_haswell_plus(codec))
1212 snd_hda_codec_write(codec, pin_nid, 0,
1213 AC_VERB_SET_AMP_GAIN_MUTE,
1216 eld = &per_pin->sink_eld;
1218 if (!non_pcm && per_pin->chmap_set)
1219 ca = hdmi_manual_channel_allocation(channels, per_pin->chmap);
1221 ca = hdmi_channel_allocation(codec, eld, channels);
1225 ordered_ca = get_channel_allocation_order(ca);
1226 active_channels = channel_allocations[ordered_ca].channels;
1228 hdmi_set_channel_count(codec, per_pin->cvt_nid, active_channels);
1231 * always configure channel mapping, it may have been changed by the
1232 * user in the meantime
1234 hdmi_setup_channel_mapping(codec, pin_nid, non_pcm, ca,
1235 channels, per_pin->chmap,
1236 per_pin->chmap_set);
1238 spec->ops.pin_setup_infoframe(codec, pin_nid, ca, active_channels,
1239 eld->info.conn_type);
1241 per_pin->non_pcm = non_pcm;
1245 * Unsolicited events
1248 static bool hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll);
1250 static void check_presence_and_report(struct hda_codec *codec, hda_nid_t nid)
1252 struct hdmi_spec *spec = codec->spec;
1253 int pin_idx = pin_nid_to_pin_index(codec, nid);
1257 if (hdmi_present_sense(get_pin(spec, pin_idx), 1))
1258 snd_hda_jack_report_sync(codec);
1261 static void jack_callback(struct hda_codec *codec,
1262 struct hda_jack_callback *jack)
1264 check_presence_and_report(codec, jack->nid);
1267 static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
1269 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
1270 struct hda_jack_tbl *jack;
1271 int dev_entry = (res & AC_UNSOL_RES_DE) >> AC_UNSOL_RES_DE_SHIFT;
1273 jack = snd_hda_jack_tbl_get_from_tag(codec, tag);
1276 jack->jack_dirty = 1;
1279 "HDMI hot plug event: Codec=%d Pin=%d Device=%d Inactive=%d Presence_Detect=%d ELD_Valid=%d\n",
1280 codec->addr, jack->nid, dev_entry, !!(res & AC_UNSOL_RES_IA),
1281 !!(res & AC_UNSOL_RES_PD), !!(res & AC_UNSOL_RES_ELDV));
1283 check_presence_and_report(codec, jack->nid);
1286 static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
1288 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
1289 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
1290 int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
1291 int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
1294 "HDMI CP event: CODEC=%d TAG=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
1309 static void hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
1311 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
1312 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
1314 if (!snd_hda_jack_tbl_get_from_tag(codec, tag)) {
1315 codec_dbg(codec, "Unexpected HDMI event tag 0x%x\n", tag);
1320 hdmi_intrinsic_event(codec, res);
1322 hdmi_non_intrinsic_event(codec, res);
1325 static void haswell_verify_D0(struct hda_codec *codec,
1326 hda_nid_t cvt_nid, hda_nid_t nid)
1330 /* For Haswell, the converter 1/2 may keep in D3 state after bootup,
1331 * thus pins could only choose converter 0 for use. Make sure the
1332 * converters are in correct power state */
1333 if (!snd_hda_check_power_state(codec, cvt_nid, AC_PWRST_D0))
1334 snd_hda_codec_write(codec, cvt_nid, 0, AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
1336 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0)) {
1337 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
1340 pwr = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
1341 pwr = (pwr & AC_PWRST_ACTUAL) >> AC_PWRST_ACTUAL_SHIFT;
1342 codec_dbg(codec, "Haswell HDMI audio: Power for pin 0x%x is now D%d\n", nid, pwr);
1350 /* HBR should be Non-PCM, 8 channels */
1351 #define is_hbr_format(format) \
1352 ((format & AC_FMT_TYPE_NON_PCM) && (format & AC_FMT_CHAN_MASK) == 7)
1354 static int hdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
1357 int pinctl, new_pinctl;
1359 if (snd_hda_query_pin_caps(codec, pin_nid) & AC_PINCAP_HBR) {
1360 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
1361 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1364 return hbr ? -EINVAL : 0;
1366 new_pinctl = pinctl & ~AC_PINCTL_EPT;
1368 new_pinctl |= AC_PINCTL_EPT_HBR;
1370 new_pinctl |= AC_PINCTL_EPT_NATIVE;
1373 "hdmi_pin_hbr_setup: NID=0x%x, %spinctl=0x%x\n",
1375 pinctl == new_pinctl ? "" : "new-",
1378 if (pinctl != new_pinctl)
1379 snd_hda_codec_write(codec, pin_nid, 0,
1380 AC_VERB_SET_PIN_WIDGET_CONTROL,
1388 static int hdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
1389 hda_nid_t pin_nid, u32 stream_tag, int format)
1391 struct hdmi_spec *spec = codec->spec;
1394 if (is_haswell_plus(codec))
1395 haswell_verify_D0(codec, cvt_nid, pin_nid);
1397 err = spec->ops.pin_hbr_setup(codec, pin_nid, is_hbr_format(format));
1400 codec_dbg(codec, "hdmi_setup_stream: HBR is not supported\n");
1404 snd_hda_codec_setup_stream(codec, cvt_nid, stream_tag, 0, format);
1408 /* Try to find an available converter
1409 * If pin_idx is less then zero, just try to find an available converter.
1410 * Otherwise, try to find an available converter and get the cvt mux index
1413 static int hdmi_choose_cvt(struct hda_codec *codec,
1414 int pin_idx, int *cvt_id, int *mux_id)
1416 struct hdmi_spec *spec = codec->spec;
1417 struct hdmi_spec_per_pin *per_pin;
1418 struct hdmi_spec_per_cvt *per_cvt = NULL;
1419 int cvt_idx, mux_idx = 0;
1421 /* pin_idx < 0 means no pin will be bound to the converter */
1425 per_pin = get_pin(spec, pin_idx);
1427 /* Dynamically assign converter to stream */
1428 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
1429 per_cvt = get_cvt(spec, cvt_idx);
1431 /* Must not already be assigned */
1432 if (per_cvt->assigned)
1434 if (per_pin == NULL)
1436 /* Must be in pin's mux's list of converters */
1437 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
1438 if (per_pin->mux_nids[mux_idx] == per_cvt->cvt_nid)
1440 /* Not in mux list */
1441 if (mux_idx == per_pin->num_mux_nids)
1446 /* No free converters */
1447 if (cvt_idx == spec->num_cvts)
1450 if (per_pin != NULL)
1451 per_pin->mux_idx = mux_idx;
1461 /* Assure the pin select the right convetor */
1462 static void intel_verify_pin_cvt_connect(struct hda_codec *codec,
1463 struct hdmi_spec_per_pin *per_pin)
1465 hda_nid_t pin_nid = per_pin->pin_nid;
1468 mux_idx = per_pin->mux_idx;
1469 curr = snd_hda_codec_read(codec, pin_nid, 0,
1470 AC_VERB_GET_CONNECT_SEL, 0);
1471 if (curr != mux_idx)
1472 snd_hda_codec_write_cache(codec, pin_nid, 0,
1473 AC_VERB_SET_CONNECT_SEL,
1477 /* get the mux index for the converter of the pins
1478 * converter's mux index is the same for all pins on Intel platform
1480 static int intel_cvt_id_to_mux_idx(struct hdmi_spec *spec,
1485 for (i = 0; i < spec->num_cvts; i++)
1486 if (spec->cvt_nids[i] == cvt_nid)
1491 /* Intel HDMI workaround to fix audio routing issue:
1492 * For some Intel display codecs, pins share the same connection list.
1493 * So a conveter can be selected by multiple pins and playback on any of these
1494 * pins will generate sound on the external display, because audio flows from
1495 * the same converter to the display pipeline. Also muting one pin may make
1496 * other pins have no sound output.
1497 * So this function assures that an assigned converter for a pin is not selected
1498 * by any other pins.
1500 static void intel_not_share_assigned_cvt(struct hda_codec *codec,
1501 hda_nid_t pin_nid, int mux_idx)
1503 struct hdmi_spec *spec = codec->spec;
1506 struct hdmi_spec_per_cvt *per_cvt;
1508 /* configure all pins, including "no physical connection" ones */
1509 for_each_hda_codec_node(nid, codec) {
1510 unsigned int wid_caps = get_wcaps(codec, nid);
1511 unsigned int wid_type = get_wcaps_type(wid_caps);
1513 if (wid_type != AC_WID_PIN)
1519 curr = snd_hda_codec_read(codec, nid, 0,
1520 AC_VERB_GET_CONNECT_SEL, 0);
1521 if (curr != mux_idx)
1524 /* choose an unassigned converter. The conveters in the
1525 * connection list are in the same order as in the codec.
1527 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
1528 per_cvt = get_cvt(spec, cvt_idx);
1529 if (!per_cvt->assigned) {
1531 "choose cvt %d for pin nid %d\n",
1533 snd_hda_codec_write_cache(codec, nid, 0,
1534 AC_VERB_SET_CONNECT_SEL,
1542 /* A wrapper of intel_not_share_asigned_cvt() */
1543 static void intel_not_share_assigned_cvt_nid(struct hda_codec *codec,
1544 hda_nid_t pin_nid, hda_nid_t cvt_nid)
1547 struct hdmi_spec *spec = codec->spec;
1549 if (!is_haswell_plus(codec) && !is_valleyview_plus(codec))
1552 /* On Intel platform, the mapping of converter nid to
1553 * mux index of the pins are always the same.
1554 * The pin nid may be 0, this means all pins will not
1555 * share the converter.
1557 mux_idx = intel_cvt_id_to_mux_idx(spec, cvt_nid);
1559 intel_not_share_assigned_cvt(codec, pin_nid, mux_idx);
1562 /* called in hdmi_pcm_open when no pin is assigned to the PCM
1563 * in dyn_pcm_assign mode.
1565 static int hdmi_pcm_open_no_pin(struct hda_pcm_stream *hinfo,
1566 struct hda_codec *codec,
1567 struct snd_pcm_substream *substream)
1569 struct hdmi_spec *spec = codec->spec;
1570 struct snd_pcm_runtime *runtime = substream->runtime;
1571 int cvt_idx, pcm_idx;
1572 struct hdmi_spec_per_cvt *per_cvt = NULL;
1575 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1579 err = hdmi_choose_cvt(codec, -1, &cvt_idx, NULL);
1583 per_cvt = get_cvt(spec, cvt_idx);
1584 per_cvt->assigned = 1;
1585 hinfo->nid = per_cvt->cvt_nid;
1587 intel_not_share_assigned_cvt_nid(codec, 0, per_cvt->cvt_nid);
1589 set_bit(pcm_idx, &spec->pcm_in_use);
1590 /* todo: setup spdif ctls assign */
1592 /* Initially set the converter's capabilities */
1593 hinfo->channels_min = per_cvt->channels_min;
1594 hinfo->channels_max = per_cvt->channels_max;
1595 hinfo->rates = per_cvt->rates;
1596 hinfo->formats = per_cvt->formats;
1597 hinfo->maxbps = per_cvt->maxbps;
1599 /* Store the updated parameters */
1600 runtime->hw.channels_min = hinfo->channels_min;
1601 runtime->hw.channels_max = hinfo->channels_max;
1602 runtime->hw.formats = hinfo->formats;
1603 runtime->hw.rates = hinfo->rates;
1605 snd_pcm_hw_constraint_step(substream->runtime, 0,
1606 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
1613 static int hdmi_pcm_open(struct hda_pcm_stream *hinfo,
1614 struct hda_codec *codec,
1615 struct snd_pcm_substream *substream)
1617 struct hdmi_spec *spec = codec->spec;
1618 struct snd_pcm_runtime *runtime = substream->runtime;
1619 int pin_idx, cvt_idx, pcm_idx, mux_idx = 0;
1620 struct hdmi_spec_per_pin *per_pin;
1621 struct hdmi_eld *eld;
1622 struct hdmi_spec_per_cvt *per_cvt = NULL;
1625 /* Validate hinfo */
1626 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1630 mutex_lock(&spec->pcm_lock);
1631 pin_idx = hinfo_to_pin_index(codec, hinfo);
1632 if (!spec->dyn_pcm_assign) {
1633 if (snd_BUG_ON(pin_idx < 0)) {
1634 mutex_unlock(&spec->pcm_lock);
1638 /* no pin is assigned to the PCM
1639 * PA need pcm open successfully when probe
1642 err = hdmi_pcm_open_no_pin(hinfo, codec, substream);
1643 mutex_unlock(&spec->pcm_lock);
1648 err = hdmi_choose_cvt(codec, pin_idx, &cvt_idx, &mux_idx);
1650 mutex_unlock(&spec->pcm_lock);
1654 per_cvt = get_cvt(spec, cvt_idx);
1655 /* Claim converter */
1656 per_cvt->assigned = 1;
1658 set_bit(pcm_idx, &spec->pcm_in_use);
1659 per_pin = get_pin(spec, pin_idx);
1660 per_pin->cvt_nid = per_cvt->cvt_nid;
1661 hinfo->nid = per_cvt->cvt_nid;
1663 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
1664 AC_VERB_SET_CONNECT_SEL,
1667 /* configure unused pins to choose other converters */
1668 if (is_haswell_plus(codec) || is_valleyview_plus(codec))
1669 intel_not_share_assigned_cvt(codec, per_pin->pin_nid, mux_idx);
1671 snd_hda_spdif_ctls_assign(codec, pcm_idx, per_cvt->cvt_nid);
1673 /* Initially set the converter's capabilities */
1674 hinfo->channels_min = per_cvt->channels_min;
1675 hinfo->channels_max = per_cvt->channels_max;
1676 hinfo->rates = per_cvt->rates;
1677 hinfo->formats = per_cvt->formats;
1678 hinfo->maxbps = per_cvt->maxbps;
1680 eld = &per_pin->sink_eld;
1681 /* Restrict capabilities by ELD if this isn't disabled */
1682 if (!static_hdmi_pcm && eld->eld_valid) {
1683 snd_hdmi_eld_update_pcm_info(&eld->info, hinfo);
1684 if (hinfo->channels_min > hinfo->channels_max ||
1685 !hinfo->rates || !hinfo->formats) {
1686 per_cvt->assigned = 0;
1688 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
1689 mutex_unlock(&spec->pcm_lock);
1694 mutex_unlock(&spec->pcm_lock);
1695 /* Store the updated parameters */
1696 runtime->hw.channels_min = hinfo->channels_min;
1697 runtime->hw.channels_max = hinfo->channels_max;
1698 runtime->hw.formats = hinfo->formats;
1699 runtime->hw.rates = hinfo->rates;
1701 snd_pcm_hw_constraint_step(substream->runtime, 0,
1702 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
1707 * HDA/HDMI auto parsing
1709 static int hdmi_read_pin_conn(struct hda_codec *codec, int pin_idx)
1711 struct hdmi_spec *spec = codec->spec;
1712 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1713 hda_nid_t pin_nid = per_pin->pin_nid;
1715 if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
1717 "HDMI: pin %d wcaps %#x does not support connection list\n",
1718 pin_nid, get_wcaps(codec, pin_nid));
1722 per_pin->num_mux_nids = snd_hda_get_connections(codec, pin_nid,
1724 HDA_MAX_CONNECTIONS);
1729 static int hdmi_find_pcm_slot(struct hdmi_spec *spec,
1730 struct hdmi_spec_per_pin *per_pin)
1734 /* try the prefer PCM */
1735 if (!test_bit(per_pin->pin_nid_idx, &spec->pcm_bitmap))
1736 return per_pin->pin_nid_idx;
1738 /* have a second try; check the "reserved area" over num_pins */
1739 for (i = spec->num_pins; i < spec->pcm_used; i++) {
1740 if (!test_bit(i, &spec->pcm_bitmap))
1744 /* the last try; check the empty slots in pins */
1745 for (i = 0; i < spec->num_pins; i++) {
1746 if (!test_bit(i, &spec->pcm_bitmap))
1752 static void hdmi_attach_hda_pcm(struct hdmi_spec *spec,
1753 struct hdmi_spec_per_pin *per_pin)
1757 /* pcm already be attached to the pin */
1760 idx = hdmi_find_pcm_slot(spec, per_pin);
1763 per_pin->pcm_idx = idx;
1764 per_pin->pcm = get_hdmi_pcm(spec, idx);
1765 set_bit(idx, &spec->pcm_bitmap);
1768 static void hdmi_detach_hda_pcm(struct hdmi_spec *spec,
1769 struct hdmi_spec_per_pin *per_pin)
1773 /* pcm already be detached from the pin */
1776 idx = per_pin->pcm_idx;
1777 per_pin->pcm_idx = -1;
1778 per_pin->pcm = NULL;
1779 if (idx >= 0 && idx < spec->pcm_used)
1780 clear_bit(idx, &spec->pcm_bitmap);
1783 static int hdmi_get_pin_cvt_mux(struct hdmi_spec *spec,
1784 struct hdmi_spec_per_pin *per_pin, hda_nid_t cvt_nid)
1788 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
1789 if (per_pin->mux_nids[mux_idx] == cvt_nid)
1794 static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid);
1796 static void hdmi_pcm_setup_pin(struct hdmi_spec *spec,
1797 struct hdmi_spec_per_pin *per_pin)
1799 struct hda_codec *codec = per_pin->codec;
1800 struct hda_pcm *pcm;
1801 struct hda_pcm_stream *hinfo;
1802 struct snd_pcm_substream *substream;
1806 if (per_pin->pcm_idx >= 0 && per_pin->pcm_idx < spec->pcm_used)
1807 pcm = get_pcm_rec(spec, per_pin->pcm_idx);
1810 if (!test_bit(per_pin->pcm_idx, &spec->pcm_in_use))
1813 /* hdmi audio only uses playback and one substream */
1814 hinfo = pcm->stream;
1815 substream = pcm->pcm->streams[0].substream;
1817 per_pin->cvt_nid = hinfo->nid;
1819 mux_idx = hdmi_get_pin_cvt_mux(spec, per_pin, hinfo->nid);
1820 if (mux_idx < per_pin->num_mux_nids)
1821 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
1822 AC_VERB_SET_CONNECT_SEL,
1824 snd_hda_spdif_ctls_assign(codec, per_pin->pcm_idx, hinfo->nid);
1826 non_pcm = check_non_pcm_per_cvt(codec, hinfo->nid);
1827 if (substream->runtime)
1828 per_pin->channels = substream->runtime->channels;
1829 per_pin->setup = true;
1830 per_pin->mux_idx = mux_idx;
1832 hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
1835 static void hdmi_pcm_reset_pin(struct hdmi_spec *spec,
1836 struct hdmi_spec_per_pin *per_pin)
1838 if (per_pin->pcm_idx >= 0 && per_pin->pcm_idx < spec->pcm_used)
1839 snd_hda_spdif_ctls_unassign(per_pin->codec, per_pin->pcm_idx);
1841 per_pin->chmap_set = false;
1842 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
1844 per_pin->setup = false;
1845 per_pin->channels = 0;
1848 /* update per_pin ELD from the given new ELD;
1849 * setup info frame and notification accordingly
1851 static void update_eld(struct hda_codec *codec,
1852 struct hdmi_spec_per_pin *per_pin,
1853 struct hdmi_eld *eld)
1855 struct hdmi_eld *pin_eld = &per_pin->sink_eld;
1856 struct hdmi_spec *spec = codec->spec;
1857 bool old_eld_valid = pin_eld->eld_valid;
1861 /* for monitor disconnection, save pcm_idx firstly */
1862 pcm_idx = per_pin->pcm_idx;
1863 if (spec->dyn_pcm_assign) {
1864 if (eld->eld_valid) {
1865 hdmi_attach_hda_pcm(spec, per_pin);
1866 hdmi_pcm_setup_pin(spec, per_pin);
1868 hdmi_pcm_reset_pin(spec, per_pin);
1869 hdmi_detach_hda_pcm(spec, per_pin);
1872 /* if pcm_idx == -1, it means this is in monitor connection event
1873 * we can get the correct pcm_idx now.
1876 pcm_idx = per_pin->pcm_idx;
1879 snd_hdmi_show_eld(codec, &eld->info);
1881 eld_changed = (pin_eld->eld_valid != eld->eld_valid);
1882 if (eld->eld_valid && pin_eld->eld_valid)
1883 if (pin_eld->eld_size != eld->eld_size ||
1884 memcmp(pin_eld->eld_buffer, eld->eld_buffer,
1885 eld->eld_size) != 0)
1888 pin_eld->eld_valid = eld->eld_valid;
1889 pin_eld->eld_size = eld->eld_size;
1891 memcpy(pin_eld->eld_buffer, eld->eld_buffer, eld->eld_size);
1892 pin_eld->info = eld->info;
1895 * Re-setup pin and infoframe. This is needed e.g. when
1896 * - sink is first plugged-in
1897 * - transcoder can change during stream playback on Haswell
1898 * and this can make HW reset converter selection on a pin.
1900 if (eld->eld_valid && !old_eld_valid && per_pin->setup) {
1901 if (is_haswell_plus(codec) || is_valleyview_plus(codec)) {
1902 intel_verify_pin_cvt_connect(codec, per_pin);
1903 intel_not_share_assigned_cvt(codec, per_pin->pin_nid,
1907 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
1910 if (eld_changed && pcm_idx >= 0)
1911 snd_ctl_notify(codec->card,
1912 SNDRV_CTL_EVENT_MASK_VALUE |
1913 SNDRV_CTL_EVENT_MASK_INFO,
1914 &get_hdmi_pcm(spec, pcm_idx)->eld_ctl->id);
1917 /* update ELD and jack state via HD-audio verbs */
1918 static bool hdmi_present_sense_via_verbs(struct hdmi_spec_per_pin *per_pin,
1921 struct hda_jack_tbl *jack;
1922 struct hda_codec *codec = per_pin->codec;
1923 struct hdmi_spec *spec = codec->spec;
1924 struct hdmi_eld *eld = &spec->temp_eld;
1925 struct hdmi_eld *pin_eld = &per_pin->sink_eld;
1926 hda_nid_t pin_nid = per_pin->pin_nid;
1928 * Always execute a GetPinSense verb here, even when called from
1929 * hdmi_intrinsic_event; for some NVIDIA HW, the unsolicited
1930 * response's PD bit is not the real PD value, but indicates that
1931 * the real PD value changed. An older version of the HD-audio
1932 * specification worked this way. Hence, we just ignore the data in
1933 * the unsolicited response to avoid custom WARs.
1937 bool do_repoll = false;
1939 snd_hda_power_up_pm(codec);
1940 present = snd_hda_pin_sense(codec, pin_nid);
1942 mutex_lock(&per_pin->lock);
1943 pin_eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
1944 if (pin_eld->monitor_present)
1945 eld->eld_valid = !!(present & AC_PINSENSE_ELDV);
1947 eld->eld_valid = false;
1950 "HDMI status: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
1951 codec->addr, pin_nid, pin_eld->monitor_present, eld->eld_valid);
1953 if (eld->eld_valid) {
1954 if (spec->ops.pin_get_eld(codec, pin_nid, eld->eld_buffer,
1955 &eld->eld_size) < 0)
1956 eld->eld_valid = false;
1958 if (snd_hdmi_parse_eld(codec, &eld->info, eld->eld_buffer,
1960 eld->eld_valid = false;
1962 if (!eld->eld_valid && repoll)
1967 schedule_delayed_work(&per_pin->work, msecs_to_jiffies(300));
1969 update_eld(codec, per_pin, eld);
1971 ret = !repoll || !pin_eld->monitor_present || pin_eld->eld_valid;
1973 jack = snd_hda_jack_tbl_get(codec, pin_nid);
1975 jack->block_report = !ret;
1977 mutex_unlock(&per_pin->lock);
1978 snd_hda_power_down_pm(codec);
1982 static struct snd_jack *pin_idx_to_jack(struct hda_codec *codec,
1983 struct hdmi_spec_per_pin *per_pin)
1985 struct hdmi_spec *spec = codec->spec;
1986 struct snd_jack *jack = NULL;
1987 struct hda_jack_tbl *jack_tbl;
1989 /* if !dyn_pcm_assign, get jack from hda_jack_tbl
1990 * in !dyn_pcm_assign case, spec->pcm_rec[].jack is not
1991 * NULL even after snd_hda_jack_tbl_clear() is called to
1992 * free snd_jack. This may cause access invalid memory
1993 * when calling snd_jack_report
1995 if (per_pin->pcm_idx >= 0 && spec->dyn_pcm_assign)
1996 jack = spec->pcm_rec[per_pin->pcm_idx].jack;
1997 else if (!spec->dyn_pcm_assign) {
1998 jack_tbl = snd_hda_jack_tbl_get(codec, per_pin->pin_nid);
2000 jack = jack_tbl->jack;
2005 /* update ELD and jack state via audio component */
2006 static void sync_eld_via_acomp(struct hda_codec *codec,
2007 struct hdmi_spec_per_pin *per_pin)
2009 struct hdmi_spec *spec = codec->spec;
2010 struct hdmi_eld *eld = &spec->temp_eld;
2011 struct snd_jack *jack = NULL;
2014 mutex_lock(&per_pin->lock);
2015 size = snd_hdac_acomp_get_eld(&codec->bus->core, per_pin->pin_nid,
2016 &eld->monitor_present, eld->eld_buffer,
2021 size = min(size, ELD_MAX_SIZE);
2022 if (snd_hdmi_parse_eld(codec, &eld->info,
2023 eld->eld_buffer, size) < 0)
2028 eld->eld_valid = true;
2029 eld->eld_size = size;
2031 eld->eld_valid = false;
2035 /* pcm_idx >=0 before update_eld() means it is in monitor
2036 * disconnected event. Jack must be fetched before update_eld()
2038 jack = pin_idx_to_jack(codec, per_pin);
2039 update_eld(codec, per_pin, eld);
2041 jack = pin_idx_to_jack(codec, per_pin);
2044 snd_jack_report(jack,
2045 eld->monitor_present ? SND_JACK_AVOUT : 0);
2047 mutex_unlock(&per_pin->lock);
2050 static bool hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll)
2052 struct hda_codec *codec = per_pin->codec;
2053 struct hdmi_spec *spec = codec->spec;
2056 mutex_lock(&spec->pcm_lock);
2057 if (codec_has_acomp(codec)) {
2058 sync_eld_via_acomp(codec, per_pin);
2059 ret = false; /* don't call snd_hda_jack_report_sync() */
2061 ret = hdmi_present_sense_via_verbs(per_pin, repoll);
2063 mutex_unlock(&spec->pcm_lock);
2068 static void hdmi_repoll_eld(struct work_struct *work)
2070 struct hdmi_spec_per_pin *per_pin =
2071 container_of(to_delayed_work(work), struct hdmi_spec_per_pin, work);
2073 if (per_pin->repoll_count++ > 6)
2074 per_pin->repoll_count = 0;
2076 if (hdmi_present_sense(per_pin, per_pin->repoll_count))
2077 snd_hda_jack_report_sync(per_pin->codec);
2080 static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
2083 static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
2085 struct hdmi_spec *spec = codec->spec;
2086 unsigned int caps, config;
2088 struct hdmi_spec_per_pin *per_pin;
2091 caps = snd_hda_query_pin_caps(codec, pin_nid);
2092 if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP)))
2095 config = snd_hda_codec_get_pincfg(codec, pin_nid);
2096 if (get_defcfg_connect(config) == AC_JACK_PORT_NONE)
2099 if (is_haswell_plus(codec))
2100 intel_haswell_fixup_connect_list(codec, pin_nid);
2102 pin_idx = spec->num_pins;
2103 per_pin = snd_array_new(&spec->pins);
2107 per_pin->pin_nid = pin_nid;
2108 per_pin->non_pcm = false;
2109 if (spec->dyn_pcm_assign)
2110 per_pin->pcm_idx = -1;
2112 per_pin->pcm = get_hdmi_pcm(spec, pin_idx);
2113 per_pin->pcm_idx = pin_idx;
2115 per_pin->pin_nid_idx = pin_idx;
2117 err = hdmi_read_pin_conn(codec, pin_idx);
2126 static int hdmi_add_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
2128 struct hdmi_spec *spec = codec->spec;
2129 struct hdmi_spec_per_cvt *per_cvt;
2133 chans = get_wcaps(codec, cvt_nid);
2134 chans = get_wcaps_channels(chans);
2136 per_cvt = snd_array_new(&spec->cvts);
2140 per_cvt->cvt_nid = cvt_nid;
2141 per_cvt->channels_min = 2;
2143 per_cvt->channels_max = chans;
2144 if (chans > spec->channels_max)
2145 spec->channels_max = chans;
2148 err = snd_hda_query_supported_pcm(codec, cvt_nid,
2155 if (spec->num_cvts < ARRAY_SIZE(spec->cvt_nids))
2156 spec->cvt_nids[spec->num_cvts] = cvt_nid;
2162 static int hdmi_parse_codec(struct hda_codec *codec)
2167 nodes = snd_hda_get_sub_nodes(codec, codec->core.afg, &nid);
2168 if (!nid || nodes < 0) {
2169 codec_warn(codec, "HDMI: failed to get afg sub nodes\n");
2173 for (i = 0; i < nodes; i++, nid++) {
2177 caps = get_wcaps(codec, nid);
2178 type = get_wcaps_type(caps);
2180 if (!(caps & AC_WCAP_DIGITAL))
2184 case AC_WID_AUD_OUT:
2185 hdmi_add_cvt(codec, nid);
2188 hdmi_add_pin(codec, nid);
2198 static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
2200 struct hda_spdif_out *spdif;
2203 mutex_lock(&codec->spdif_mutex);
2204 spdif = snd_hda_spdif_out_of_nid(codec, cvt_nid);
2205 non_pcm = !!(spdif->status & IEC958_AES0_NONAUDIO);
2206 mutex_unlock(&codec->spdif_mutex);
2214 static int generic_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2215 struct hda_codec *codec,
2216 unsigned int stream_tag,
2217 unsigned int format,
2218 struct snd_pcm_substream *substream)
2220 hda_nid_t cvt_nid = hinfo->nid;
2221 struct hdmi_spec *spec = codec->spec;
2223 struct hdmi_spec_per_pin *per_pin;
2225 struct snd_pcm_runtime *runtime = substream->runtime;
2230 mutex_lock(&spec->pcm_lock);
2231 pin_idx = hinfo_to_pin_index(codec, hinfo);
2232 if (spec->dyn_pcm_assign && pin_idx < 0) {
2233 /* when dyn_pcm_assign and pcm is not bound to a pin
2234 * skip pin setup and return 0 to make audio playback
2237 intel_not_share_assigned_cvt_nid(codec, 0, cvt_nid);
2238 snd_hda_codec_setup_stream(codec, cvt_nid,
2239 stream_tag, 0, format);
2240 mutex_unlock(&spec->pcm_lock);
2244 if (snd_BUG_ON(pin_idx < 0)) {
2245 mutex_unlock(&spec->pcm_lock);
2248 per_pin = get_pin(spec, pin_idx);
2249 pin_nid = per_pin->pin_nid;
2250 if (is_haswell_plus(codec) || is_valleyview_plus(codec)) {
2251 /* Verify pin:cvt selections to avoid silent audio after S3.
2252 * After S3, the audio driver restores pin:cvt selections
2253 * but this can happen before gfx is ready and such selection
2254 * is overlooked by HW. Thus multiple pins can share a same
2255 * default convertor and mute control will affect each other,
2256 * which can cause a resumed audio playback become silent
2259 intel_verify_pin_cvt_connect(codec, per_pin);
2260 intel_not_share_assigned_cvt(codec, pin_nid, per_pin->mux_idx);
2263 /* Call sync_audio_rate to set the N/CTS/M manually if necessary */
2264 /* Todo: add DP1.2 MST audio support later */
2265 snd_hdac_sync_audio_rate(&codec->bus->core, pin_nid, runtime->rate);
2267 non_pcm = check_non_pcm_per_cvt(codec, cvt_nid);
2268 mutex_lock(&per_pin->lock);
2269 per_pin->channels = substream->runtime->channels;
2270 per_pin->setup = true;
2272 hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
2273 mutex_unlock(&per_pin->lock);
2274 if (spec->dyn_pin_out) {
2275 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
2276 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2277 snd_hda_codec_write(codec, pin_nid, 0,
2278 AC_VERB_SET_PIN_WIDGET_CONTROL,
2282 err = spec->ops.setup_stream(codec, cvt_nid, pin_nid,
2283 stream_tag, format);
2284 mutex_unlock(&spec->pcm_lock);
2288 static int generic_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
2289 struct hda_codec *codec,
2290 struct snd_pcm_substream *substream)
2292 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
2296 static int hdmi_pcm_close(struct hda_pcm_stream *hinfo,
2297 struct hda_codec *codec,
2298 struct snd_pcm_substream *substream)
2300 struct hdmi_spec *spec = codec->spec;
2301 int cvt_idx, pin_idx, pcm_idx;
2302 struct hdmi_spec_per_cvt *per_cvt;
2303 struct hdmi_spec_per_pin *per_pin;
2307 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
2308 if (snd_BUG_ON(pcm_idx < 0))
2310 cvt_idx = cvt_nid_to_cvt_index(codec, hinfo->nid);
2311 if (snd_BUG_ON(cvt_idx < 0))
2313 per_cvt = get_cvt(spec, cvt_idx);
2315 snd_BUG_ON(!per_cvt->assigned);
2316 per_cvt->assigned = 0;
2319 mutex_lock(&spec->pcm_lock);
2320 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
2321 clear_bit(pcm_idx, &spec->pcm_in_use);
2322 pin_idx = hinfo_to_pin_index(codec, hinfo);
2323 if (spec->dyn_pcm_assign && pin_idx < 0) {
2324 mutex_unlock(&spec->pcm_lock);
2328 if (snd_BUG_ON(pin_idx < 0)) {
2329 mutex_unlock(&spec->pcm_lock);
2332 per_pin = get_pin(spec, pin_idx);
2334 if (spec->dyn_pin_out) {
2335 pinctl = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
2336 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2337 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
2338 AC_VERB_SET_PIN_WIDGET_CONTROL,
2342 mutex_lock(&per_pin->lock);
2343 per_pin->chmap_set = false;
2344 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
2346 per_pin->setup = false;
2347 per_pin->channels = 0;
2348 mutex_unlock(&per_pin->lock);
2349 mutex_unlock(&spec->pcm_lock);
2355 static const struct hda_pcm_ops generic_ops = {
2356 .open = hdmi_pcm_open,
2357 .close = hdmi_pcm_close,
2358 .prepare = generic_hdmi_playback_pcm_prepare,
2359 .cleanup = generic_hdmi_playback_pcm_cleanup,
2363 * ALSA API channel-map control callbacks
2365 static int hdmi_chmap_ctl_info(struct snd_kcontrol *kcontrol,
2366 struct snd_ctl_elem_info *uinfo)
2368 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2369 struct hda_codec *codec = info->private_data;
2370 struct hdmi_spec *spec = codec->spec;
2371 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2372 uinfo->count = spec->channels_max;
2373 uinfo->value.integer.min = 0;
2374 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
2378 static int hdmi_chmap_cea_alloc_validate_get_type(struct cea_channel_speaker_allocation *cap,
2381 /* If the speaker allocation matches the channel count, it is OK.*/
2382 if (cap->channels != channels)
2385 /* all channels are remappable freely */
2386 return SNDRV_CTL_TLVT_CHMAP_VAR;
2389 static void hdmi_cea_alloc_to_tlv_chmap(struct cea_channel_speaker_allocation *cap,
2390 unsigned int *chmap, int channels)
2395 for (c = 7; c >= 0; c--) {
2396 int spk = cap->speakers[c];
2400 chmap[count++] = spk_to_chmap(spk);
2403 WARN_ON(count != channels);
2406 static int hdmi_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2407 unsigned int size, unsigned int __user *tlv)
2409 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2410 struct hda_codec *codec = info->private_data;
2411 struct hdmi_spec *spec = codec->spec;
2412 unsigned int __user *dst;
2417 if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
2421 for (chs = 2; chs <= spec->channels_max; chs++) {
2423 struct cea_channel_speaker_allocation *cap;
2424 cap = channel_allocations;
2425 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++, cap++) {
2426 int chs_bytes = chs * 4;
2427 int type = spec->ops.chmap_cea_alloc_validate_get_type(cap, chs);
2428 unsigned int tlv_chmap[8];
2434 if (put_user(type, dst) ||
2435 put_user(chs_bytes, dst + 1))
2440 if (size < chs_bytes)
2444 spec->ops.cea_alloc_to_tlv_chmap(cap, tlv_chmap, chs);
2445 if (copy_to_user(dst, tlv_chmap, chs_bytes))
2450 if (put_user(count, tlv + 1))
2455 static int hdmi_chmap_ctl_get(struct snd_kcontrol *kcontrol,
2456 struct snd_ctl_elem_value *ucontrol)
2458 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2459 struct hda_codec *codec = info->private_data;
2460 struct hdmi_spec *spec = codec->spec;
2461 int pcm_idx = kcontrol->private_value;
2462 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
2466 for (i = 0; i < spec->channels_max; i++)
2467 ucontrol->value.integer.value[i] = 0;
2471 for (i = 0; i < ARRAY_SIZE(per_pin->chmap); i++)
2472 ucontrol->value.integer.value[i] = per_pin->chmap[i];
2476 static int hdmi_chmap_ctl_put(struct snd_kcontrol *kcontrol,
2477 struct snd_ctl_elem_value *ucontrol)
2479 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2480 struct hda_codec *codec = info->private_data;
2481 struct hdmi_spec *spec = codec->spec;
2482 int pcm_idx = kcontrol->private_value;
2483 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
2484 unsigned int ctl_idx;
2485 struct snd_pcm_substream *substream;
2486 unsigned char chmap[8];
2487 int i, err, ca, prepared = 0;
2489 /* No monitor is connected in dyn_pcm_assign.
2490 * It's invalid to setup the chmap
2495 ctl_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2496 substream = snd_pcm_chmap_substream(info, ctl_idx);
2497 if (!substream || !substream->runtime)
2498 return 0; /* just for avoiding error from alsactl restore */
2499 switch (substream->runtime->status->state) {
2500 case SNDRV_PCM_STATE_OPEN:
2501 case SNDRV_PCM_STATE_SETUP:
2503 case SNDRV_PCM_STATE_PREPARED:
2509 memset(chmap, 0, sizeof(chmap));
2510 for (i = 0; i < ARRAY_SIZE(chmap); i++)
2511 chmap[i] = ucontrol->value.integer.value[i];
2512 if (!memcmp(chmap, per_pin->chmap, sizeof(chmap)))
2514 ca = hdmi_manual_channel_allocation(ARRAY_SIZE(chmap), chmap);
2517 if (spec->ops.chmap_validate) {
2518 err = spec->ops.chmap_validate(ca, ARRAY_SIZE(chmap), chmap);
2522 mutex_lock(&per_pin->lock);
2523 per_pin->chmap_set = true;
2524 memcpy(per_pin->chmap, chmap, sizeof(chmap));
2526 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
2527 mutex_unlock(&per_pin->lock);
2532 static int generic_hdmi_build_pcms(struct hda_codec *codec)
2534 struct hdmi_spec *spec = codec->spec;
2537 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2538 struct hda_pcm *info;
2539 struct hda_pcm_stream *pstr;
2541 info = snd_hda_codec_pcm_new(codec, "HDMI %d", pin_idx);
2545 spec->pcm_rec[pin_idx].pcm = info;
2547 info->pcm_type = HDA_PCM_TYPE_HDMI;
2548 info->own_chmap = true;
2550 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
2551 pstr->substreams = 1;
2552 pstr->ops = generic_ops;
2553 /* other pstr fields are set in open */
2559 static void free_hdmi_jack_priv(struct snd_jack *jack)
2561 struct hdmi_pcm *pcm = jack->private_data;
2566 static int add_hdmi_jack_kctl(struct hda_codec *codec,
2567 struct hdmi_spec *spec,
2571 struct snd_jack *jack;
2574 err = snd_jack_new(codec->card, name, SND_JACK_AVOUT, &jack,
2579 spec->pcm_rec[pcm_idx].jack = jack;
2580 jack->private_data = &spec->pcm_rec[pcm_idx];
2581 jack->private_free = free_hdmi_jack_priv;
2585 static int generic_hdmi_build_jack(struct hda_codec *codec, int pcm_idx)
2587 char hdmi_str[32] = "HDMI/DP";
2588 struct hdmi_spec *spec = codec->spec;
2589 struct hdmi_spec_per_pin *per_pin;
2590 struct hda_jack_tbl *jack;
2591 int pcmdev = get_pcm_rec(spec, pcm_idx)->device;
2596 sprintf(hdmi_str + strlen(hdmi_str), ",pcm=%d", pcmdev);
2598 if (spec->dyn_pcm_assign)
2599 return add_hdmi_jack_kctl(codec, spec, pcm_idx, hdmi_str);
2601 /* for !dyn_pcm_assign, we still use hda_jack for compatibility */
2602 /* if !dyn_pcm_assign, it must be non-MST mode.
2603 * This means pcms and pins are statically mapped.
2604 * And pcm_idx is pin_idx.
2606 per_pin = get_pin(spec, pcm_idx);
2607 phantom_jack = !is_jack_detectable(codec, per_pin->pin_nid);
2609 strncat(hdmi_str, " Phantom",
2610 sizeof(hdmi_str) - strlen(hdmi_str) - 1);
2611 ret = snd_hda_jack_add_kctl(codec, per_pin->pin_nid, hdmi_str,
2615 jack = snd_hda_jack_tbl_get(codec, per_pin->pin_nid);
2618 /* assign jack->jack to pcm_rec[].jack to
2619 * align with dyn_pcm_assign mode
2621 spec->pcm_rec[pcm_idx].jack = jack->jack;
2625 static int generic_hdmi_build_controls(struct hda_codec *codec)
2627 struct hdmi_spec *spec = codec->spec;
2629 int pin_idx, pcm_idx;
2632 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
2633 err = generic_hdmi_build_jack(codec, pcm_idx);
2637 /* create the spdif for each pcm
2638 * pin will be bound when monitor is connected
2640 if (spec->dyn_pcm_assign)
2641 err = snd_hda_create_dig_out_ctls(codec,
2642 0, spec->cvt_nids[0],
2645 struct hdmi_spec_per_pin *per_pin =
2646 get_pin(spec, pcm_idx);
2647 err = snd_hda_create_dig_out_ctls(codec,
2649 per_pin->mux_nids[0],
2654 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
2656 /* add control for ELD Bytes */
2657 err = hdmi_create_eld_ctl(codec, pcm_idx,
2658 get_pcm_rec(spec, pcm_idx)->device);
2663 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2664 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2666 hdmi_present_sense(per_pin, 0);
2669 /* add channel maps */
2670 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
2671 struct hda_pcm *pcm;
2672 struct snd_pcm_chmap *chmap;
2673 struct snd_kcontrol *kctl;
2676 pcm = get_pcm_rec(spec, pcm_idx);
2677 if (!pcm || !pcm->pcm)
2679 err = snd_pcm_add_chmap_ctls(pcm->pcm,
2680 SNDRV_PCM_STREAM_PLAYBACK,
2681 NULL, 0, pcm_idx, &chmap);
2684 /* override handlers */
2685 chmap->private_data = codec;
2687 for (i = 0; i < kctl->count; i++)
2688 kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_WRITE;
2689 kctl->info = hdmi_chmap_ctl_info;
2690 kctl->get = hdmi_chmap_ctl_get;
2691 kctl->put = hdmi_chmap_ctl_put;
2692 kctl->tlv.c = hdmi_chmap_ctl_tlv;
2698 static int generic_hdmi_init_per_pins(struct hda_codec *codec)
2700 struct hdmi_spec *spec = codec->spec;
2703 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2704 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2706 per_pin->codec = codec;
2707 mutex_init(&per_pin->lock);
2708 INIT_DELAYED_WORK(&per_pin->work, hdmi_repoll_eld);
2709 eld_proc_new(per_pin, pin_idx);
2714 static int generic_hdmi_init(struct hda_codec *codec)
2716 struct hdmi_spec *spec = codec->spec;
2719 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2720 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2721 hda_nid_t pin_nid = per_pin->pin_nid;
2723 hdmi_init_pin(codec, pin_nid);
2724 if (!codec_has_acomp(codec))
2725 snd_hda_jack_detect_enable_callback(codec, pin_nid,
2726 codec->jackpoll_interval > 0 ?
2727 jack_callback : NULL);
2732 static void hdmi_array_init(struct hdmi_spec *spec, int nums)
2734 snd_array_init(&spec->pins, sizeof(struct hdmi_spec_per_pin), nums);
2735 snd_array_init(&spec->cvts, sizeof(struct hdmi_spec_per_cvt), nums);
2738 static void hdmi_array_free(struct hdmi_spec *spec)
2740 snd_array_free(&spec->pins);
2741 snd_array_free(&spec->cvts);
2744 static void generic_hdmi_free(struct hda_codec *codec)
2746 struct hdmi_spec *spec = codec->spec;
2747 int pin_idx, pcm_idx;
2749 if (codec_has_acomp(codec))
2750 snd_hdac_i915_register_notifier(NULL);
2752 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2753 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2754 cancel_delayed_work_sync(&per_pin->work);
2755 eld_proc_free(per_pin);
2758 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
2759 if (spec->pcm_rec[pcm_idx].jack == NULL)
2761 if (spec->dyn_pcm_assign)
2762 snd_device_free(codec->card,
2763 spec->pcm_rec[pcm_idx].jack);
2765 spec->pcm_rec[pcm_idx].jack = NULL;
2768 if (spec->i915_bound)
2769 snd_hdac_i915_exit(&codec->bus->core);
2770 hdmi_array_free(spec);
2775 static int generic_hdmi_resume(struct hda_codec *codec)
2777 struct hdmi_spec *spec = codec->spec;
2780 codec->patch_ops.init(codec);
2781 regcache_sync(codec->core.regmap);
2783 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2784 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2785 hdmi_present_sense(per_pin, 1);
2791 static const struct hda_codec_ops generic_hdmi_patch_ops = {
2792 .init = generic_hdmi_init,
2793 .free = generic_hdmi_free,
2794 .build_pcms = generic_hdmi_build_pcms,
2795 .build_controls = generic_hdmi_build_controls,
2796 .unsol_event = hdmi_unsol_event,
2798 .resume = generic_hdmi_resume,
2802 static const struct hdmi_ops generic_standard_hdmi_ops = {
2803 .pin_get_eld = snd_hdmi_get_eld,
2804 .pin_get_slot_channel = hdmi_pin_get_slot_channel,
2805 .pin_set_slot_channel = hdmi_pin_set_slot_channel,
2806 .pin_setup_infoframe = hdmi_pin_setup_infoframe,
2807 .pin_hbr_setup = hdmi_pin_hbr_setup,
2808 .setup_stream = hdmi_setup_stream,
2809 .chmap_cea_alloc_validate_get_type = hdmi_chmap_cea_alloc_validate_get_type,
2810 .cea_alloc_to_tlv_chmap = hdmi_cea_alloc_to_tlv_chmap,
2814 static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
2817 struct hdmi_spec *spec = codec->spec;
2821 nconns = snd_hda_get_connections(codec, nid, conns, ARRAY_SIZE(conns));
2822 if (nconns == spec->num_cvts &&
2823 !memcmp(conns, spec->cvt_nids, spec->num_cvts * sizeof(hda_nid_t)))
2826 /* override pins connection list */
2827 codec_dbg(codec, "hdmi: haswell: override pin connection 0x%x\n", nid);
2828 snd_hda_override_conn_list(codec, nid, spec->num_cvts, spec->cvt_nids);
2831 #define INTEL_VENDOR_NID 0x08
2832 #define INTEL_GET_VENDOR_VERB 0xf81
2833 #define INTEL_SET_VENDOR_VERB 0x781
2834 #define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
2835 #define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
2837 static void intel_haswell_enable_all_pins(struct hda_codec *codec,
2840 unsigned int vendor_param;
2842 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
2843 INTEL_GET_VENDOR_VERB, 0);
2844 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
2847 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
2848 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
2849 INTEL_SET_VENDOR_VERB, vendor_param);
2850 if (vendor_param == -1)
2854 snd_hda_codec_update_widgets(codec);
2857 static void intel_haswell_fixup_enable_dp12(struct hda_codec *codec)
2859 unsigned int vendor_param;
2861 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
2862 INTEL_GET_VENDOR_VERB, 0);
2863 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
2866 /* enable DP1.2 mode */
2867 vendor_param |= INTEL_EN_DP12;
2868 snd_hdac_regmap_add_vendor_verb(&codec->core, INTEL_SET_VENDOR_VERB);
2869 snd_hda_codec_write_cache(codec, INTEL_VENDOR_NID, 0,
2870 INTEL_SET_VENDOR_VERB, vendor_param);
2873 /* Haswell needs to re-issue the vendor-specific verbs before turning to D0.
2874 * Otherwise you may get severe h/w communication errors.
2876 static void haswell_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2877 unsigned int power_state)
2879 if (power_state == AC_PWRST_D0) {
2880 intel_haswell_enable_all_pins(codec, false);
2881 intel_haswell_fixup_enable_dp12(codec);
2884 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE, power_state);
2885 snd_hda_codec_set_power_to_all(codec, fg, power_state);
2888 static void intel_pin_eld_notify(void *audio_ptr, int port)
2890 struct hda_codec *codec = audio_ptr;
2891 int pin_nid = port + 0x04;
2893 /* skip notification during system suspend (but not in runtime PM);
2894 * the state will be updated at resume
2896 if (snd_power_get_state(codec->card) != SNDRV_CTL_POWER_D0)
2898 /* ditto during suspend/resume process itself */
2899 if (atomic_read(&(codec)->core.in_pm))
2902 check_presence_and_report(codec, pin_nid);
2905 static int patch_generic_hdmi(struct hda_codec *codec)
2907 struct hdmi_spec *spec;
2909 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2913 spec->ops = generic_standard_hdmi_ops;
2914 mutex_init(&spec->pcm_lock);
2916 hdmi_array_init(spec, 4);
2918 /* Try to bind with i915 for any Intel codecs (if not done yet) */
2919 if (!codec_has_acomp(codec) &&
2920 (codec->core.vendor_id >> 16) == 0x8086)
2921 if (!snd_hdac_i915_init(&codec->bus->core))
2922 spec->i915_bound = true;
2924 if (is_haswell_plus(codec)) {
2925 intel_haswell_enable_all_pins(codec, true);
2926 intel_haswell_fixup_enable_dp12(codec);
2929 /* For Valleyview/Cherryview, only the display codec is in the display
2930 * power well and can use link_power ops to request/release the power.
2931 * For Haswell/Broadwell, the controller is also in the power well and
2932 * can cover the codec power request, and so need not set this flag.
2933 * For previous platforms, there is no such power well feature.
2935 if (is_valleyview_plus(codec) || is_skylake(codec) ||
2937 codec->core.link_power_control = 1;
2939 if (hdmi_parse_codec(codec) < 0) {
2940 if (spec->i915_bound)
2941 snd_hdac_i915_exit(&codec->bus->core);
2946 codec->patch_ops = generic_hdmi_patch_ops;
2947 if (is_haswell_plus(codec)) {
2948 codec->patch_ops.set_power_state = haswell_set_power_state;
2949 codec->dp_mst = true;
2952 /* Enable runtime pm for HDMI audio codec of HSW/BDW/SKL/BYT/BSW */
2953 if (is_haswell_plus(codec) || is_valleyview_plus(codec))
2954 codec->auto_runtime_pm = 1;
2956 generic_hdmi_init_per_pins(codec);
2958 init_channel_allocations();
2960 if (codec_has_acomp(codec)) {
2961 codec->depop_delay = 0;
2962 spec->i915_audio_ops.audio_ptr = codec;
2963 /* intel_audio_codec_enable() or intel_audio_codec_disable()
2964 * will call pin_eld_notify with using audio_ptr pointer
2965 * We need make sure audio_ptr is really setup
2968 spec->i915_audio_ops.pin_eld_notify = intel_pin_eld_notify;
2969 snd_hdac_i915_register_notifier(&spec->i915_audio_ops);
2972 WARN_ON(spec->dyn_pcm_assign && !codec_has_acomp(codec));
2977 * Shared non-generic implementations
2980 static int simple_playback_build_pcms(struct hda_codec *codec)
2982 struct hdmi_spec *spec = codec->spec;
2983 struct hda_pcm *info;
2985 struct hda_pcm_stream *pstr;
2986 struct hdmi_spec_per_cvt *per_cvt;
2988 per_cvt = get_cvt(spec, 0);
2989 chans = get_wcaps(codec, per_cvt->cvt_nid);
2990 chans = get_wcaps_channels(chans);
2992 info = snd_hda_codec_pcm_new(codec, "HDMI 0");
2995 spec->pcm_rec[0].pcm = info;
2996 info->pcm_type = HDA_PCM_TYPE_HDMI;
2997 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
2998 *pstr = spec->pcm_playback;
2999 pstr->nid = per_cvt->cvt_nid;
3000 if (pstr->channels_max <= 2 && chans && chans <= 16)
3001 pstr->channels_max = chans;
3006 /* unsolicited event for jack sensing */
3007 static void simple_hdmi_unsol_event(struct hda_codec *codec,
3010 snd_hda_jack_set_dirty_all(codec);
3011 snd_hda_jack_report_sync(codec);
3014 /* generic_hdmi_build_jack can be used for simple_hdmi, too,
3015 * as long as spec->pins[] is set correctly
3017 #define simple_hdmi_build_jack generic_hdmi_build_jack
3019 static int simple_playback_build_controls(struct hda_codec *codec)
3021 struct hdmi_spec *spec = codec->spec;
3022 struct hdmi_spec_per_cvt *per_cvt;
3025 per_cvt = get_cvt(spec, 0);
3026 err = snd_hda_create_dig_out_ctls(codec, per_cvt->cvt_nid,
3031 return simple_hdmi_build_jack(codec, 0);
3034 static int simple_playback_init(struct hda_codec *codec)
3036 struct hdmi_spec *spec = codec->spec;
3037 struct hdmi_spec_per_pin *per_pin = get_pin(spec, 0);
3038 hda_nid_t pin = per_pin->pin_nid;
3040 snd_hda_codec_write(codec, pin, 0,
3041 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
3042 /* some codecs require to unmute the pin */
3043 if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
3044 snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
3046 snd_hda_jack_detect_enable(codec, pin);
3050 static void simple_playback_free(struct hda_codec *codec)
3052 struct hdmi_spec *spec = codec->spec;
3054 hdmi_array_free(spec);
3059 * Nvidia specific implementations
3062 #define Nv_VERB_SET_Channel_Allocation 0xF79
3063 #define Nv_VERB_SET_Info_Frame_Checksum 0xF7A
3064 #define Nv_VERB_SET_Audio_Protection_On 0xF98
3065 #define Nv_VERB_SET_Audio_Protection_Off 0xF99
3067 #define nvhdmi_master_con_nid_7x 0x04
3068 #define nvhdmi_master_pin_nid_7x 0x05
3070 static const hda_nid_t nvhdmi_con_nids_7x[4] = {
3071 /*front, rear, clfe, rear_surr */
3075 static const struct hda_verb nvhdmi_basic_init_7x_2ch[] = {
3076 /* set audio protect on */
3077 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
3078 /* enable digital output on pin widget */
3079 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3083 static const struct hda_verb nvhdmi_basic_init_7x_8ch[] = {
3084 /* set audio protect on */
3085 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
3086 /* enable digital output on pin widget */
3087 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3088 { 0x7, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3089 { 0x9, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3090 { 0xb, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3091 { 0xd, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3095 #ifdef LIMITED_RATE_FMT_SUPPORT
3096 /* support only the safe format and rate */
3097 #define SUPPORTED_RATES SNDRV_PCM_RATE_48000
3098 #define SUPPORTED_MAXBPS 16
3099 #define SUPPORTED_FORMATS SNDRV_PCM_FMTBIT_S16_LE
3101 /* support all rates and formats */
3102 #define SUPPORTED_RATES \
3103 (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
3104 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
3105 SNDRV_PCM_RATE_192000)
3106 #define SUPPORTED_MAXBPS 24
3107 #define SUPPORTED_FORMATS \
3108 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
3111 static int nvhdmi_7x_init_2ch(struct hda_codec *codec)
3113 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_2ch);
3117 static int nvhdmi_7x_init_8ch(struct hda_codec *codec)
3119 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_8ch);
3123 static unsigned int channels_2_6_8[] = {
3127 static unsigned int channels_2_8[] = {
3131 static struct snd_pcm_hw_constraint_list hw_constraints_2_6_8_channels = {
3132 .count = ARRAY_SIZE(channels_2_6_8),
3133 .list = channels_2_6_8,
3137 static struct snd_pcm_hw_constraint_list hw_constraints_2_8_channels = {
3138 .count = ARRAY_SIZE(channels_2_8),
3139 .list = channels_2_8,
3143 static int simple_playback_pcm_open(struct hda_pcm_stream *hinfo,
3144 struct hda_codec *codec,
3145 struct snd_pcm_substream *substream)
3147 struct hdmi_spec *spec = codec->spec;
3148 struct snd_pcm_hw_constraint_list *hw_constraints_channels = NULL;
3150 switch (codec->preset->vendor_id) {
3155 hw_constraints_channels = &hw_constraints_2_8_channels;
3158 hw_constraints_channels = &hw_constraints_2_6_8_channels;
3164 if (hw_constraints_channels != NULL) {
3165 snd_pcm_hw_constraint_list(substream->runtime, 0,
3166 SNDRV_PCM_HW_PARAM_CHANNELS,
3167 hw_constraints_channels);
3169 snd_pcm_hw_constraint_step(substream->runtime, 0,
3170 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
3173 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3176 static int simple_playback_pcm_close(struct hda_pcm_stream *hinfo,
3177 struct hda_codec *codec,
3178 struct snd_pcm_substream *substream)
3180 struct hdmi_spec *spec = codec->spec;
3181 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3184 static int simple_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3185 struct hda_codec *codec,
3186 unsigned int stream_tag,
3187 unsigned int format,
3188 struct snd_pcm_substream *substream)
3190 struct hdmi_spec *spec = codec->spec;
3191 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3192 stream_tag, format, substream);
3195 static const struct hda_pcm_stream simple_pcm_playback = {
3200 .open = simple_playback_pcm_open,
3201 .close = simple_playback_pcm_close,
3202 .prepare = simple_playback_pcm_prepare
3206 static const struct hda_codec_ops simple_hdmi_patch_ops = {
3207 .build_controls = simple_playback_build_controls,
3208 .build_pcms = simple_playback_build_pcms,
3209 .init = simple_playback_init,
3210 .free = simple_playback_free,
3211 .unsol_event = simple_hdmi_unsol_event,
3214 static int patch_simple_hdmi(struct hda_codec *codec,
3215 hda_nid_t cvt_nid, hda_nid_t pin_nid)
3217 struct hdmi_spec *spec;
3218 struct hdmi_spec_per_cvt *per_cvt;
3219 struct hdmi_spec_per_pin *per_pin;
3221 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
3226 hdmi_array_init(spec, 1);
3228 spec->multiout.num_dacs = 0; /* no analog */
3229 spec->multiout.max_channels = 2;
3230 spec->multiout.dig_out_nid = cvt_nid;
3233 per_pin = snd_array_new(&spec->pins);
3234 per_cvt = snd_array_new(&spec->cvts);
3235 if (!per_pin || !per_cvt) {
3236 simple_playback_free(codec);
3239 per_cvt->cvt_nid = cvt_nid;
3240 per_pin->pin_nid = pin_nid;
3241 spec->pcm_playback = simple_pcm_playback;
3243 codec->patch_ops = simple_hdmi_patch_ops;
3248 static void nvhdmi_8ch_7x_set_info_frame_parameters(struct hda_codec *codec,
3251 unsigned int chanmask;
3252 int chan = channels ? (channels - 1) : 1;
3271 /* Set the audio infoframe channel allocation and checksum fields. The
3272 * channel count is computed implicitly by the hardware. */
3273 snd_hda_codec_write(codec, 0x1, 0,
3274 Nv_VERB_SET_Channel_Allocation, chanmask);
3276 snd_hda_codec_write(codec, 0x1, 0,
3277 Nv_VERB_SET_Info_Frame_Checksum,
3278 (0x71 - chan - chanmask));
3281 static int nvhdmi_8ch_7x_pcm_close(struct hda_pcm_stream *hinfo,
3282 struct hda_codec *codec,
3283 struct snd_pcm_substream *substream)
3285 struct hdmi_spec *spec = codec->spec;
3288 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x,
3289 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
3290 for (i = 0; i < 4; i++) {
3291 /* set the stream id */
3292 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
3293 AC_VERB_SET_CHANNEL_STREAMID, 0);
3294 /* set the stream format */
3295 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
3296 AC_VERB_SET_STREAM_FORMAT, 0);
3299 /* The audio hardware sends a channel count of 0x7 (8ch) when all the
3300 * streams are disabled. */
3301 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
3303 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3306 static int nvhdmi_8ch_7x_pcm_prepare(struct hda_pcm_stream *hinfo,
3307 struct hda_codec *codec,
3308 unsigned int stream_tag,
3309 unsigned int format,
3310 struct snd_pcm_substream *substream)
3313 unsigned int dataDCC2, channel_id;
3315 struct hdmi_spec *spec = codec->spec;
3316 struct hda_spdif_out *spdif;
3317 struct hdmi_spec_per_cvt *per_cvt;
3319 mutex_lock(&codec->spdif_mutex);
3320 per_cvt = get_cvt(spec, 0);
3321 spdif = snd_hda_spdif_out_of_nid(codec, per_cvt->cvt_nid);
3323 chs = substream->runtime->channels;
3327 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
3328 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
3329 snd_hda_codec_write(codec,
3330 nvhdmi_master_con_nid_7x,
3332 AC_VERB_SET_DIGI_CONVERT_1,
3333 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
3335 /* set the stream id */
3336 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
3337 AC_VERB_SET_CHANNEL_STREAMID, (stream_tag << 4) | 0x0);
3339 /* set the stream format */
3340 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
3341 AC_VERB_SET_STREAM_FORMAT, format);
3343 /* turn on again (if needed) */
3344 /* enable and set the channel status audio/data flag */
3345 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE)) {
3346 snd_hda_codec_write(codec,
3347 nvhdmi_master_con_nid_7x,
3349 AC_VERB_SET_DIGI_CONVERT_1,
3350 spdif->ctls & 0xff);
3351 snd_hda_codec_write(codec,
3352 nvhdmi_master_con_nid_7x,
3354 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
3357 for (i = 0; i < 4; i++) {
3363 /* turn off SPDIF once;
3364 *otherwise the IEC958 bits won't be updated
3366 if (codec->spdif_status_reset &&
3367 (spdif->ctls & AC_DIG1_ENABLE))
3368 snd_hda_codec_write(codec,
3369 nvhdmi_con_nids_7x[i],
3371 AC_VERB_SET_DIGI_CONVERT_1,
3372 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
3373 /* set the stream id */
3374 snd_hda_codec_write(codec,
3375 nvhdmi_con_nids_7x[i],
3377 AC_VERB_SET_CHANNEL_STREAMID,
3378 (stream_tag << 4) | channel_id);
3379 /* set the stream format */
3380 snd_hda_codec_write(codec,
3381 nvhdmi_con_nids_7x[i],
3383 AC_VERB_SET_STREAM_FORMAT,
3385 /* turn on again (if needed) */
3386 /* enable and set the channel status audio/data flag */
3387 if (codec->spdif_status_reset &&
3388 (spdif->ctls & AC_DIG1_ENABLE)) {
3389 snd_hda_codec_write(codec,
3390 nvhdmi_con_nids_7x[i],
3392 AC_VERB_SET_DIGI_CONVERT_1,
3393 spdif->ctls & 0xff);
3394 snd_hda_codec_write(codec,
3395 nvhdmi_con_nids_7x[i],
3397 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
3401 nvhdmi_8ch_7x_set_info_frame_parameters(codec, chs);
3403 mutex_unlock(&codec->spdif_mutex);
3407 static const struct hda_pcm_stream nvhdmi_pcm_playback_8ch_7x = {
3411 .nid = nvhdmi_master_con_nid_7x,
3412 .rates = SUPPORTED_RATES,
3413 .maxbps = SUPPORTED_MAXBPS,
3414 .formats = SUPPORTED_FORMATS,
3416 .open = simple_playback_pcm_open,
3417 .close = nvhdmi_8ch_7x_pcm_close,
3418 .prepare = nvhdmi_8ch_7x_pcm_prepare
3422 static int patch_nvhdmi_2ch(struct hda_codec *codec)
3424 struct hdmi_spec *spec;
3425 int err = patch_simple_hdmi(codec, nvhdmi_master_con_nid_7x,
3426 nvhdmi_master_pin_nid_7x);
3430 codec->patch_ops.init = nvhdmi_7x_init_2ch;
3431 /* override the PCM rates, etc, as the codec doesn't give full list */
3433 spec->pcm_playback.rates = SUPPORTED_RATES;
3434 spec->pcm_playback.maxbps = SUPPORTED_MAXBPS;
3435 spec->pcm_playback.formats = SUPPORTED_FORMATS;
3439 static int nvhdmi_7x_8ch_build_pcms(struct hda_codec *codec)
3441 struct hdmi_spec *spec = codec->spec;
3442 int err = simple_playback_build_pcms(codec);
3444 struct hda_pcm *info = get_pcm_rec(spec, 0);
3445 info->own_chmap = true;
3450 static int nvhdmi_7x_8ch_build_controls(struct hda_codec *codec)
3452 struct hdmi_spec *spec = codec->spec;
3453 struct hda_pcm *info;
3454 struct snd_pcm_chmap *chmap;
3457 err = simple_playback_build_controls(codec);
3461 /* add channel maps */
3462 info = get_pcm_rec(spec, 0);
3463 err = snd_pcm_add_chmap_ctls(info->pcm,
3464 SNDRV_PCM_STREAM_PLAYBACK,
3465 snd_pcm_alt_chmaps, 8, 0, &chmap);
3468 switch (codec->preset->vendor_id) {
3473 chmap->channel_mask = (1U << 2) | (1U << 8);
3476 chmap->channel_mask = (1U << 2) | (1U << 6) | (1U << 8);
3481 static int patch_nvhdmi_8ch_7x(struct hda_codec *codec)
3483 struct hdmi_spec *spec;
3484 int err = patch_nvhdmi_2ch(codec);
3488 spec->multiout.max_channels = 8;
3489 spec->pcm_playback = nvhdmi_pcm_playback_8ch_7x;
3490 codec->patch_ops.init = nvhdmi_7x_init_8ch;
3491 codec->patch_ops.build_pcms = nvhdmi_7x_8ch_build_pcms;
3492 codec->patch_ops.build_controls = nvhdmi_7x_8ch_build_controls;
3494 /* Initialize the audio infoframe channel mask and checksum to something
3496 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
3502 * NVIDIA codecs ignore ASP mapping for 2ch - confirmed on:
3506 static int nvhdmi_chmap_cea_alloc_validate_get_type(struct cea_channel_speaker_allocation *cap,
3509 if (cap->ca_index == 0x00 && channels == 2)
3510 return SNDRV_CTL_TLVT_CHMAP_FIXED;
3512 return hdmi_chmap_cea_alloc_validate_get_type(cap, channels);
3515 static int nvhdmi_chmap_validate(int ca, int chs, unsigned char *map)
3517 if (ca == 0x00 && (map[0] != SNDRV_CHMAP_FL || map[1] != SNDRV_CHMAP_FR))
3523 static int patch_nvhdmi(struct hda_codec *codec)
3525 struct hdmi_spec *spec;
3528 err = patch_generic_hdmi(codec);
3533 spec->dyn_pin_out = true;
3535 spec->ops.chmap_cea_alloc_validate_get_type =
3536 nvhdmi_chmap_cea_alloc_validate_get_type;
3537 spec->ops.chmap_validate = nvhdmi_chmap_validate;
3543 * The HDA codec on NVIDIA Tegra contains two scratch registers that are
3544 * accessed using vendor-defined verbs. These registers can be used for
3545 * interoperability between the HDA and HDMI drivers.
3548 /* Audio Function Group node */
3549 #define NVIDIA_AFG_NID 0x01
3552 * The SCRATCH0 register is used to notify the HDMI codec of changes in audio
3553 * format. On Tegra, bit 31 is used as a trigger that causes an interrupt to
3554 * be raised in the HDMI codec. The remainder of the bits is arbitrary. This
3555 * implementation stores the HDA format (see AC_FMT_*) in bits [15:0] and an
3556 * additional bit (at position 30) to signal the validity of the format.
3558 * | 31 | 30 | 29 16 | 15 0 |
3559 * +---------+-------+--------+--------+
3560 * | TRIGGER | VALID | UNUSED | FORMAT |
3561 * +-----------------------------------|
3563 * Note that for the trigger bit to take effect it needs to change value
3564 * (i.e. it needs to be toggled).
3566 #define NVIDIA_GET_SCRATCH0 0xfa6
3567 #define NVIDIA_SET_SCRATCH0_BYTE0 0xfa7
3568 #define NVIDIA_SET_SCRATCH0_BYTE1 0xfa8
3569 #define NVIDIA_SET_SCRATCH0_BYTE2 0xfa9
3570 #define NVIDIA_SET_SCRATCH0_BYTE3 0xfaa
3571 #define NVIDIA_SCRATCH_TRIGGER (1 << 7)
3572 #define NVIDIA_SCRATCH_VALID (1 << 6)
3574 #define NVIDIA_GET_SCRATCH1 0xfab
3575 #define NVIDIA_SET_SCRATCH1_BYTE0 0xfac
3576 #define NVIDIA_SET_SCRATCH1_BYTE1 0xfad
3577 #define NVIDIA_SET_SCRATCH1_BYTE2 0xfae
3578 #define NVIDIA_SET_SCRATCH1_BYTE3 0xfaf
3581 * The format parameter is the HDA audio format (see AC_FMT_*). If set to 0,
3582 * the format is invalidated so that the HDMI codec can be disabled.
3584 static void tegra_hdmi_set_format(struct hda_codec *codec, unsigned int format)
3588 /* bits [31:30] contain the trigger and valid bits */
3589 value = snd_hda_codec_read(codec, NVIDIA_AFG_NID, 0,
3590 NVIDIA_GET_SCRATCH0, 0);
3591 value = (value >> 24) & 0xff;
3593 /* bits [15:0] are used to store the HDA format */
3594 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3595 NVIDIA_SET_SCRATCH0_BYTE0,
3596 (format >> 0) & 0xff);
3597 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3598 NVIDIA_SET_SCRATCH0_BYTE1,
3599 (format >> 8) & 0xff);
3601 /* bits [16:24] are unused */
3602 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3603 NVIDIA_SET_SCRATCH0_BYTE2, 0);
3606 * Bit 30 signals that the data is valid and hence that HDMI audio can
3610 value &= ~NVIDIA_SCRATCH_VALID;
3612 value |= NVIDIA_SCRATCH_VALID;
3615 * Whenever the trigger bit is toggled, an interrupt is raised in the
3616 * HDMI codec. The HDMI driver will use that as trigger to update its
3619 value ^= NVIDIA_SCRATCH_TRIGGER;
3621 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3622 NVIDIA_SET_SCRATCH0_BYTE3, value);
3625 static int tegra_hdmi_pcm_prepare(struct hda_pcm_stream *hinfo,
3626 struct hda_codec *codec,
3627 unsigned int stream_tag,
3628 unsigned int format,
3629 struct snd_pcm_substream *substream)
3633 err = generic_hdmi_playback_pcm_prepare(hinfo, codec, stream_tag,
3638 /* notify the HDMI codec of the format change */
3639 tegra_hdmi_set_format(codec, format);
3644 static int tegra_hdmi_pcm_cleanup(struct hda_pcm_stream *hinfo,
3645 struct hda_codec *codec,
3646 struct snd_pcm_substream *substream)
3648 /* invalidate the format in the HDMI codec */
3649 tegra_hdmi_set_format(codec, 0);
3651 return generic_hdmi_playback_pcm_cleanup(hinfo, codec, substream);
3654 static struct hda_pcm *hda_find_pcm_by_type(struct hda_codec *codec, int type)
3656 struct hdmi_spec *spec = codec->spec;
3659 for (i = 0; i < spec->num_pins; i++) {
3660 struct hda_pcm *pcm = get_pcm_rec(spec, i);
3662 if (pcm->pcm_type == type)
3669 static int tegra_hdmi_build_pcms(struct hda_codec *codec)
3671 struct hda_pcm_stream *stream;
3672 struct hda_pcm *pcm;
3675 err = generic_hdmi_build_pcms(codec);
3679 pcm = hda_find_pcm_by_type(codec, HDA_PCM_TYPE_HDMI);
3684 * Override ->prepare() and ->cleanup() operations to notify the HDMI
3685 * codec about format changes.
3687 stream = &pcm->stream[SNDRV_PCM_STREAM_PLAYBACK];
3688 stream->ops.prepare = tegra_hdmi_pcm_prepare;
3689 stream->ops.cleanup = tegra_hdmi_pcm_cleanup;
3694 static int patch_tegra_hdmi(struct hda_codec *codec)
3698 err = patch_generic_hdmi(codec);
3702 codec->patch_ops.build_pcms = tegra_hdmi_build_pcms;
3708 * ATI/AMD-specific implementations
3711 #define is_amdhdmi_rev3_or_later(codec) \
3712 ((codec)->core.vendor_id == 0x1002aa01 && \
3713 ((codec)->core.revision_id & 0xff00) >= 0x0300)
3714 #define has_amd_full_remap_support(codec) is_amdhdmi_rev3_or_later(codec)
3716 /* ATI/AMD specific HDA pin verbs, see the AMD HDA Verbs specification */
3717 #define ATI_VERB_SET_CHANNEL_ALLOCATION 0x771
3718 #define ATI_VERB_SET_DOWNMIX_INFO 0x772
3719 #define ATI_VERB_SET_MULTICHANNEL_01 0x777
3720 #define ATI_VERB_SET_MULTICHANNEL_23 0x778
3721 #define ATI_VERB_SET_MULTICHANNEL_45 0x779
3722 #define ATI_VERB_SET_MULTICHANNEL_67 0x77a
3723 #define ATI_VERB_SET_HBR_CONTROL 0x77c
3724 #define ATI_VERB_SET_MULTICHANNEL_1 0x785
3725 #define ATI_VERB_SET_MULTICHANNEL_3 0x786
3726 #define ATI_VERB_SET_MULTICHANNEL_5 0x787
3727 #define ATI_VERB_SET_MULTICHANNEL_7 0x788
3728 #define ATI_VERB_SET_MULTICHANNEL_MODE 0x789
3729 #define ATI_VERB_GET_CHANNEL_ALLOCATION 0xf71
3730 #define ATI_VERB_GET_DOWNMIX_INFO 0xf72
3731 #define ATI_VERB_GET_MULTICHANNEL_01 0xf77
3732 #define ATI_VERB_GET_MULTICHANNEL_23 0xf78
3733 #define ATI_VERB_GET_MULTICHANNEL_45 0xf79
3734 #define ATI_VERB_GET_MULTICHANNEL_67 0xf7a
3735 #define ATI_VERB_GET_HBR_CONTROL 0xf7c
3736 #define ATI_VERB_GET_MULTICHANNEL_1 0xf85
3737 #define ATI_VERB_GET_MULTICHANNEL_3 0xf86
3738 #define ATI_VERB_GET_MULTICHANNEL_5 0xf87
3739 #define ATI_VERB_GET_MULTICHANNEL_7 0xf88
3740 #define ATI_VERB_GET_MULTICHANNEL_MODE 0xf89
3742 /* AMD specific HDA cvt verbs */
3743 #define ATI_VERB_SET_RAMP_RATE 0x770
3744 #define ATI_VERB_GET_RAMP_RATE 0xf70
3746 #define ATI_OUT_ENABLE 0x1
3748 #define ATI_MULTICHANNEL_MODE_PAIRED 0
3749 #define ATI_MULTICHANNEL_MODE_SINGLE 1
3751 #define ATI_HBR_CAPABLE 0x01
3752 #define ATI_HBR_ENABLE 0x10
3754 static int atihdmi_pin_get_eld(struct hda_codec *codec, hda_nid_t nid,
3755 unsigned char *buf, int *eld_size)
3757 /* call hda_eld.c ATI/AMD-specific function */
3758 return snd_hdmi_get_eld_ati(codec, nid, buf, eld_size,
3759 is_amdhdmi_rev3_or_later(codec));
3762 static void atihdmi_pin_setup_infoframe(struct hda_codec *codec, hda_nid_t pin_nid, int ca,
3763 int active_channels, int conn_type)
3765 snd_hda_codec_write(codec, pin_nid, 0, ATI_VERB_SET_CHANNEL_ALLOCATION, ca);
3768 static int atihdmi_paired_swap_fc_lfe(int pos)
3771 * ATI/AMD have automatic FC/LFE swap built-in
3772 * when in pairwise mapping mode.
3776 /* see channel_allocations[].speakers[] */
3785 static int atihdmi_paired_chmap_validate(int ca, int chs, unsigned char *map)
3787 struct cea_channel_speaker_allocation *cap;
3790 /* check that only channel pairs need to be remapped on old pre-rev3 ATI/AMD */
3792 cap = &channel_allocations[get_channel_allocation_order(ca)];
3793 for (i = 0; i < chs; ++i) {
3794 int mask = to_spk_mask(map[i]);
3796 bool companion_ok = false;
3801 for (j = 0 + i % 2; j < 8; j += 2) {
3802 int chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j);
3803 if (cap->speakers[chan_idx] == mask) {
3804 /* channel is in a supported position */
3807 if (i % 2 == 0 && i + 1 < chs) {
3808 /* even channel, check the odd companion */
3809 int comp_chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j + 1);
3810 int comp_mask_req = to_spk_mask(map[i+1]);
3811 int comp_mask_act = cap->speakers[comp_chan_idx];
3813 if (comp_mask_req == comp_mask_act)
3814 companion_ok = true;
3826 i++; /* companion channel already checked */
3832 static int atihdmi_pin_set_slot_channel(struct hda_codec *codec, hda_nid_t pin_nid,
3833 int hdmi_slot, int stream_channel)
3836 int ati_channel_setup = 0;
3841 if (!has_amd_full_remap_support(codec)) {
3842 hdmi_slot = atihdmi_paired_swap_fc_lfe(hdmi_slot);
3844 /* In case this is an odd slot but without stream channel, do not
3845 * disable the slot since the corresponding even slot could have a
3846 * channel. In case neither have a channel, the slot pair will be
3847 * disabled when this function is called for the even slot. */
3848 if (hdmi_slot % 2 != 0 && stream_channel == 0xf)
3851 hdmi_slot -= hdmi_slot % 2;
3853 if (stream_channel != 0xf)
3854 stream_channel -= stream_channel % 2;
3857 verb = ATI_VERB_SET_MULTICHANNEL_01 + hdmi_slot/2 + (hdmi_slot % 2) * 0x00e;
3859 /* ati_channel_setup format: [7..4] = stream_channel_id, [1] = mute, [0] = enable */
3861 if (stream_channel != 0xf)
3862 ati_channel_setup = (stream_channel << 4) | ATI_OUT_ENABLE;
3864 return snd_hda_codec_write(codec, pin_nid, 0, verb, ati_channel_setup);
3867 static int atihdmi_pin_get_slot_channel(struct hda_codec *codec, hda_nid_t pin_nid,
3870 bool was_odd = false;
3871 int ati_asp_slot = asp_slot;
3873 int ati_channel_setup;
3878 if (!has_amd_full_remap_support(codec)) {
3879 ati_asp_slot = atihdmi_paired_swap_fc_lfe(asp_slot);
3880 if (ati_asp_slot % 2 != 0) {
3886 verb = ATI_VERB_GET_MULTICHANNEL_01 + ati_asp_slot/2 + (ati_asp_slot % 2) * 0x00e;
3888 ati_channel_setup = snd_hda_codec_read(codec, pin_nid, 0, verb, 0);
3890 if (!(ati_channel_setup & ATI_OUT_ENABLE))
3893 return ((ati_channel_setup & 0xf0) >> 4) + !!was_odd;
3896 static int atihdmi_paired_chmap_cea_alloc_validate_get_type(struct cea_channel_speaker_allocation *cap,
3902 * Pre-rev3 ATI/AMD codecs operate in a paired channel mode, so
3903 * we need to take that into account (a single channel may take 2
3904 * channel slots if we need to carry a silent channel next to it).
3905 * On Rev3+ AMD codecs this function is not used.
3909 /* We only produce even-numbered channel count TLVs */
3910 if ((channels % 2) != 0)
3913 for (c = 0; c < 7; c += 2) {
3914 if (cap->speakers[c] || cap->speakers[c+1])
3918 if (chanpairs * 2 != channels)
3921 return SNDRV_CTL_TLVT_CHMAP_PAIRED;
3924 static void atihdmi_paired_cea_alloc_to_tlv_chmap(struct cea_channel_speaker_allocation *cap,
3925 unsigned int *chmap, int channels)
3927 /* produce paired maps for pre-rev3 ATI/AMD codecs */
3931 for (c = 7; c >= 0; c--) {
3932 int chan = 7 - atihdmi_paired_swap_fc_lfe(7 - c);
3933 int spk = cap->speakers[chan];
3935 /* add N/A channel if the companion channel is occupied */
3936 if (cap->speakers[chan + (chan % 2 ? -1 : 1)])
3937 chmap[count++] = SNDRV_CHMAP_NA;
3942 chmap[count++] = spk_to_chmap(spk);
3945 WARN_ON(count != channels);
3948 static int atihdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
3951 int hbr_ctl, hbr_ctl_new;
3953 hbr_ctl = snd_hda_codec_read(codec, pin_nid, 0, ATI_VERB_GET_HBR_CONTROL, 0);
3954 if (hbr_ctl >= 0 && (hbr_ctl & ATI_HBR_CAPABLE)) {
3956 hbr_ctl_new = hbr_ctl | ATI_HBR_ENABLE;
3958 hbr_ctl_new = hbr_ctl & ~ATI_HBR_ENABLE;
3961 "atihdmi_pin_hbr_setup: NID=0x%x, %shbr-ctl=0x%x\n",
3963 hbr_ctl == hbr_ctl_new ? "" : "new-",
3966 if (hbr_ctl != hbr_ctl_new)
3967 snd_hda_codec_write(codec, pin_nid, 0,
3968 ATI_VERB_SET_HBR_CONTROL,
3977 static int atihdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
3978 hda_nid_t pin_nid, u32 stream_tag, int format)
3981 if (is_amdhdmi_rev3_or_later(codec)) {
3982 int ramp_rate = 180; /* default as per AMD spec */
3983 /* disable ramp-up/down for non-pcm as per AMD spec */
3984 if (format & AC_FMT_TYPE_NON_PCM)
3987 snd_hda_codec_write(codec, cvt_nid, 0, ATI_VERB_SET_RAMP_RATE, ramp_rate);
3990 return hdmi_setup_stream(codec, cvt_nid, pin_nid, stream_tag, format);
3994 static int atihdmi_init(struct hda_codec *codec)
3996 struct hdmi_spec *spec = codec->spec;
3999 err = generic_hdmi_init(codec);
4004 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
4005 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
4007 /* make sure downmix information in infoframe is zero */
4008 snd_hda_codec_write(codec, per_pin->pin_nid, 0, ATI_VERB_SET_DOWNMIX_INFO, 0);
4010 /* enable channel-wise remap mode if supported */
4011 if (has_amd_full_remap_support(codec))
4012 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
4013 ATI_VERB_SET_MULTICHANNEL_MODE,
4014 ATI_MULTICHANNEL_MODE_SINGLE);
4020 static int patch_atihdmi(struct hda_codec *codec)
4022 struct hdmi_spec *spec;
4023 struct hdmi_spec_per_cvt *per_cvt;
4026 err = patch_generic_hdmi(codec);
4031 codec->patch_ops.init = atihdmi_init;
4035 spec->ops.pin_get_eld = atihdmi_pin_get_eld;
4036 spec->ops.pin_get_slot_channel = atihdmi_pin_get_slot_channel;
4037 spec->ops.pin_set_slot_channel = atihdmi_pin_set_slot_channel;
4038 spec->ops.pin_setup_infoframe = atihdmi_pin_setup_infoframe;
4039 spec->ops.pin_hbr_setup = atihdmi_pin_hbr_setup;
4040 spec->ops.setup_stream = atihdmi_setup_stream;
4042 if (!has_amd_full_remap_support(codec)) {
4043 /* override to ATI/AMD-specific versions with pairwise mapping */
4044 spec->ops.chmap_cea_alloc_validate_get_type =
4045 atihdmi_paired_chmap_cea_alloc_validate_get_type;
4046 spec->ops.cea_alloc_to_tlv_chmap = atihdmi_paired_cea_alloc_to_tlv_chmap;
4047 spec->ops.chmap_validate = atihdmi_paired_chmap_validate;
4050 /* ATI/AMD converters do not advertise all of their capabilities */
4051 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
4052 per_cvt = get_cvt(spec, cvt_idx);
4053 per_cvt->channels_max = max(per_cvt->channels_max, 8u);
4054 per_cvt->rates |= SUPPORTED_RATES;
4055 per_cvt->formats |= SUPPORTED_FORMATS;
4056 per_cvt->maxbps = max(per_cvt->maxbps, 24u);
4059 spec->channels_max = max(spec->channels_max, 8u);
4064 /* VIA HDMI Implementation */
4065 #define VIAHDMI_CVT_NID 0x02 /* audio converter1 */
4066 #define VIAHDMI_PIN_NID 0x03 /* HDMI output pin1 */
4068 static int patch_via_hdmi(struct hda_codec *codec)
4070 return patch_simple_hdmi(codec, VIAHDMI_CVT_NID, VIAHDMI_PIN_NID);
4076 static const struct hda_device_id snd_hda_id_hdmi[] = {
4077 HDA_CODEC_ENTRY(0x1002793c, "RS600 HDMI", patch_atihdmi),
4078 HDA_CODEC_ENTRY(0x10027919, "RS600 HDMI", patch_atihdmi),
4079 HDA_CODEC_ENTRY(0x1002791a, "RS690/780 HDMI", patch_atihdmi),
4080 HDA_CODEC_ENTRY(0x1002aa01, "R6xx HDMI", patch_atihdmi),
4081 HDA_CODEC_ENTRY(0x10951390, "SiI1390 HDMI", patch_generic_hdmi),
4082 HDA_CODEC_ENTRY(0x10951392, "SiI1392 HDMI", patch_generic_hdmi),
4083 HDA_CODEC_ENTRY(0x17e80047, "Chrontel HDMI", patch_generic_hdmi),
4084 HDA_CODEC_ENTRY(0x10de0002, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
4085 HDA_CODEC_ENTRY(0x10de0003, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
4086 HDA_CODEC_ENTRY(0x10de0005, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
4087 HDA_CODEC_ENTRY(0x10de0006, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
4088 HDA_CODEC_ENTRY(0x10de0007, "MCP79/7A HDMI", patch_nvhdmi_8ch_7x),
4089 HDA_CODEC_ENTRY(0x10de000a, "GPU 0a HDMI/DP", patch_nvhdmi),
4090 HDA_CODEC_ENTRY(0x10de000b, "GPU 0b HDMI/DP", patch_nvhdmi),
4091 HDA_CODEC_ENTRY(0x10de000c, "MCP89 HDMI", patch_nvhdmi),
4092 HDA_CODEC_ENTRY(0x10de000d, "GPU 0d HDMI/DP", patch_nvhdmi),
4093 HDA_CODEC_ENTRY(0x10de0010, "GPU 10 HDMI/DP", patch_nvhdmi),
4094 HDA_CODEC_ENTRY(0x10de0011, "GPU 11 HDMI/DP", patch_nvhdmi),
4095 HDA_CODEC_ENTRY(0x10de0012, "GPU 12 HDMI/DP", patch_nvhdmi),
4096 HDA_CODEC_ENTRY(0x10de0013, "GPU 13 HDMI/DP", patch_nvhdmi),
4097 HDA_CODEC_ENTRY(0x10de0014, "GPU 14 HDMI/DP", patch_nvhdmi),
4098 HDA_CODEC_ENTRY(0x10de0015, "GPU 15 HDMI/DP", patch_nvhdmi),
4099 HDA_CODEC_ENTRY(0x10de0016, "GPU 16 HDMI/DP", patch_nvhdmi),
4100 /* 17 is known to be absent */
4101 HDA_CODEC_ENTRY(0x10de0018, "GPU 18 HDMI/DP", patch_nvhdmi),
4102 HDA_CODEC_ENTRY(0x10de0019, "GPU 19 HDMI/DP", patch_nvhdmi),
4103 HDA_CODEC_ENTRY(0x10de001a, "GPU 1a HDMI/DP", patch_nvhdmi),
4104 HDA_CODEC_ENTRY(0x10de001b, "GPU 1b HDMI/DP", patch_nvhdmi),
4105 HDA_CODEC_ENTRY(0x10de001c, "GPU 1c HDMI/DP", patch_nvhdmi),
4106 HDA_CODEC_ENTRY(0x10de0020, "Tegra30 HDMI", patch_tegra_hdmi),
4107 HDA_CODEC_ENTRY(0x10de0022, "Tegra114 HDMI", patch_tegra_hdmi),
4108 HDA_CODEC_ENTRY(0x10de0028, "Tegra124 HDMI", patch_tegra_hdmi),
4109 HDA_CODEC_ENTRY(0x10de0029, "Tegra210 HDMI/DP", patch_tegra_hdmi),
4110 HDA_CODEC_ENTRY(0x10de0040, "GPU 40 HDMI/DP", patch_nvhdmi),
4111 HDA_CODEC_ENTRY(0x10de0041, "GPU 41 HDMI/DP", patch_nvhdmi),
4112 HDA_CODEC_ENTRY(0x10de0042, "GPU 42 HDMI/DP", patch_nvhdmi),
4113 HDA_CODEC_ENTRY(0x10de0043, "GPU 43 HDMI/DP", patch_nvhdmi),
4114 HDA_CODEC_ENTRY(0x10de0044, "GPU 44 HDMI/DP", patch_nvhdmi),
4115 HDA_CODEC_ENTRY(0x10de0051, "GPU 51 HDMI/DP", patch_nvhdmi),
4116 HDA_CODEC_ENTRY(0x10de0060, "GPU 60 HDMI/DP", patch_nvhdmi),
4117 HDA_CODEC_ENTRY(0x10de0067, "MCP67 HDMI", patch_nvhdmi_2ch),
4118 HDA_CODEC_ENTRY(0x10de0070, "GPU 70 HDMI/DP", patch_nvhdmi),
4119 HDA_CODEC_ENTRY(0x10de0071, "GPU 71 HDMI/DP", patch_nvhdmi),
4120 HDA_CODEC_ENTRY(0x10de0072, "GPU 72 HDMI/DP", patch_nvhdmi),
4121 HDA_CODEC_ENTRY(0x10de007d, "GPU 7d HDMI/DP", patch_nvhdmi),
4122 HDA_CODEC_ENTRY(0x10de0083, "GPU 83 HDMI/DP", patch_nvhdmi),
4123 HDA_CODEC_ENTRY(0x10de8001, "MCP73 HDMI", patch_nvhdmi_2ch),
4124 HDA_CODEC_ENTRY(0x11069f80, "VX900 HDMI/DP", patch_via_hdmi),
4125 HDA_CODEC_ENTRY(0x11069f81, "VX900 HDMI/DP", patch_via_hdmi),
4126 HDA_CODEC_ENTRY(0x11069f84, "VX11 HDMI/DP", patch_generic_hdmi),
4127 HDA_CODEC_ENTRY(0x11069f85, "VX11 HDMI/DP", patch_generic_hdmi),
4128 HDA_CODEC_ENTRY(0x80860054, "IbexPeak HDMI", patch_generic_hdmi),
4129 HDA_CODEC_ENTRY(0x80862801, "Bearlake HDMI", patch_generic_hdmi),
4130 HDA_CODEC_ENTRY(0x80862802, "Cantiga HDMI", patch_generic_hdmi),
4131 HDA_CODEC_ENTRY(0x80862803, "Eaglelake HDMI", patch_generic_hdmi),
4132 HDA_CODEC_ENTRY(0x80862804, "IbexPeak HDMI", patch_generic_hdmi),
4133 HDA_CODEC_ENTRY(0x80862805, "CougarPoint HDMI", patch_generic_hdmi),
4134 HDA_CODEC_ENTRY(0x80862806, "PantherPoint HDMI", patch_generic_hdmi),
4135 HDA_CODEC_ENTRY(0x80862807, "Haswell HDMI", patch_generic_hdmi),
4136 HDA_CODEC_ENTRY(0x80862808, "Broadwell HDMI", patch_generic_hdmi),
4137 HDA_CODEC_ENTRY(0x80862809, "Skylake HDMI", patch_generic_hdmi),
4138 HDA_CODEC_ENTRY(0x8086280a, "Broxton HDMI", patch_generic_hdmi),
4139 HDA_CODEC_ENTRY(0x8086280b, "Kabylake HDMI", patch_generic_hdmi),
4140 HDA_CODEC_ENTRY(0x80862880, "CedarTrail HDMI", patch_generic_hdmi),
4141 HDA_CODEC_ENTRY(0x80862882, "Valleyview2 HDMI", patch_generic_hdmi),
4142 HDA_CODEC_ENTRY(0x80862883, "Braswell HDMI", patch_generic_hdmi),
4143 HDA_CODEC_ENTRY(0x808629fb, "Crestline HDMI", patch_generic_hdmi),
4144 /* special ID for generic HDMI */
4145 HDA_CODEC_ENTRY(HDA_CODEC_ID_GENERIC_HDMI, "Generic HDMI", patch_generic_hdmi),
4148 MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_hdmi);
4150 MODULE_LICENSE("GPL");
4151 MODULE_DESCRIPTION("HDMI HD-audio codec");
4152 MODULE_ALIAS("snd-hda-codec-intelhdmi");
4153 MODULE_ALIAS("snd-hda-codec-nvhdmi");
4154 MODULE_ALIAS("snd-hda-codec-atihdmi");
4156 static struct hda_codec_driver hdmi_driver = {
4157 .id = snd_hda_id_hdmi,
4160 module_hda_codec_driver(hdmi_driver);