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 snd_kcontrol *eld_ctl;
90 struct hdmi_pcm *pcm; /* pointer to spec->pcm_rec[n] dynamically*/
91 int pcm_idx; /* which pcm is attached. -1 means no pcm is attached */
93 bool setup; /* the stream has been set up by prepare callback */
94 int channels; /* current number of channels */
96 bool chmap_set; /* channel-map override by ALSA API? */
97 unsigned char chmap[8]; /* ALSA API channel-map */
98 #ifdef CONFIG_SND_PROC_FS
99 struct snd_info_entry *proc_entry;
103 struct cea_channel_speaker_allocation;
105 /* operations used by generic code that can be overridden by patches */
107 int (*pin_get_eld)(struct hda_codec *codec, hda_nid_t pin_nid,
108 unsigned char *buf, int *eld_size);
110 /* get and set channel assigned to each HDMI ASP (audio sample packet) slot */
111 int (*pin_get_slot_channel)(struct hda_codec *codec, hda_nid_t pin_nid,
113 int (*pin_set_slot_channel)(struct hda_codec *codec, hda_nid_t pin_nid,
114 int asp_slot, int channel);
116 void (*pin_setup_infoframe)(struct hda_codec *codec, hda_nid_t pin_nid,
117 int ca, int active_channels, int conn_type);
119 /* enable/disable HBR (HD passthrough) */
120 int (*pin_hbr_setup)(struct hda_codec *codec, hda_nid_t pin_nid, bool hbr);
122 int (*setup_stream)(struct hda_codec *codec, hda_nid_t cvt_nid,
123 hda_nid_t pin_nid, u32 stream_tag, int format);
125 /* Helpers for producing the channel map TLVs. These can be overridden
126 * for devices that have non-standard mapping requirements. */
127 int (*chmap_cea_alloc_validate_get_type)(struct cea_channel_speaker_allocation *cap,
129 void (*cea_alloc_to_tlv_chmap)(struct cea_channel_speaker_allocation *cap,
130 unsigned int *chmap, int channels);
132 /* check that the user-given chmap is supported */
133 int (*chmap_validate)(int ca, int channels, unsigned char *chmap);
138 struct snd_jack *jack;
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 pin_idx = kcontrol->private_value;
479 per_pin = get_pin(spec, pin_idx);
480 eld = &per_pin->sink_eld;
482 mutex_lock(&per_pin->lock);
483 uinfo->count = eld->eld_valid ? eld->eld_size : 0;
484 mutex_unlock(&per_pin->lock);
489 static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
490 struct snd_ctl_elem_value *ucontrol)
492 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
493 struct hdmi_spec *spec = codec->spec;
494 struct hdmi_spec_per_pin *per_pin;
495 struct hdmi_eld *eld;
498 pin_idx = kcontrol->private_value;
499 per_pin = get_pin(spec, pin_idx);
500 eld = &per_pin->sink_eld;
502 mutex_lock(&per_pin->lock);
503 if (eld->eld_size > ARRAY_SIZE(ucontrol->value.bytes.data) ||
504 eld->eld_size > ELD_MAX_SIZE) {
505 mutex_unlock(&per_pin->lock);
510 memset(ucontrol->value.bytes.data, 0,
511 ARRAY_SIZE(ucontrol->value.bytes.data));
513 memcpy(ucontrol->value.bytes.data, eld->eld_buffer,
515 mutex_unlock(&per_pin->lock);
520 static struct snd_kcontrol_new eld_bytes_ctl = {
521 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
522 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
524 .info = hdmi_eld_ctl_info,
525 .get = hdmi_eld_ctl_get,
528 static int hdmi_create_eld_ctl(struct hda_codec *codec, int pin_idx,
531 struct snd_kcontrol *kctl;
532 struct hdmi_spec *spec = codec->spec;
535 kctl = snd_ctl_new1(&eld_bytes_ctl, codec);
538 kctl->private_value = pin_idx;
539 kctl->id.device = device;
541 err = snd_hda_ctl_add(codec, get_pin(spec, pin_idx)->pin_nid, kctl);
545 get_pin(spec, pin_idx)->eld_ctl = kctl;
550 static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
551 int *packet_index, int *byte_index)
555 val = snd_hda_codec_read(codec, pin_nid, 0,
556 AC_VERB_GET_HDMI_DIP_INDEX, 0);
558 *packet_index = val >> 5;
559 *byte_index = val & 0x1f;
563 static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
564 int packet_index, int byte_index)
568 val = (packet_index << 5) | (byte_index & 0x1f);
570 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
573 static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t pin_nid,
576 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
579 static void hdmi_init_pin(struct hda_codec *codec, hda_nid_t pin_nid)
581 struct hdmi_spec *spec = codec->spec;
585 if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
586 snd_hda_codec_write(codec, pin_nid, 0,
587 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
589 if (spec->dyn_pin_out)
590 /* Disable pin out until stream is active */
593 /* Enable pin out: some machines with GM965 gets broken output
594 * when the pin is disabled or changed while using with HDMI
598 snd_hda_codec_write(codec, pin_nid, 0,
599 AC_VERB_SET_PIN_WIDGET_CONTROL, pin_out);
602 static int hdmi_get_channel_count(struct hda_codec *codec, hda_nid_t cvt_nid)
604 return 1 + snd_hda_codec_read(codec, cvt_nid, 0,
605 AC_VERB_GET_CVT_CHAN_COUNT, 0);
608 static void hdmi_set_channel_count(struct hda_codec *codec,
609 hda_nid_t cvt_nid, int chs)
611 if (chs != hdmi_get_channel_count(codec, cvt_nid))
612 snd_hda_codec_write(codec, cvt_nid, 0,
613 AC_VERB_SET_CVT_CHAN_COUNT, chs - 1);
620 #ifdef CONFIG_SND_PROC_FS
621 static void print_eld_info(struct snd_info_entry *entry,
622 struct snd_info_buffer *buffer)
624 struct hdmi_spec_per_pin *per_pin = entry->private_data;
626 mutex_lock(&per_pin->lock);
627 snd_hdmi_print_eld_info(&per_pin->sink_eld, buffer);
628 mutex_unlock(&per_pin->lock);
631 static void write_eld_info(struct snd_info_entry *entry,
632 struct snd_info_buffer *buffer)
634 struct hdmi_spec_per_pin *per_pin = entry->private_data;
636 mutex_lock(&per_pin->lock);
637 snd_hdmi_write_eld_info(&per_pin->sink_eld, buffer);
638 mutex_unlock(&per_pin->lock);
641 static int eld_proc_new(struct hdmi_spec_per_pin *per_pin, int index)
644 struct hda_codec *codec = per_pin->codec;
645 struct snd_info_entry *entry;
648 snprintf(name, sizeof(name), "eld#%d.%d", codec->addr, index);
649 err = snd_card_proc_new(codec->card, name, &entry);
653 snd_info_set_text_ops(entry, per_pin, print_eld_info);
654 entry->c.text.write = write_eld_info;
655 entry->mode |= S_IWUSR;
656 per_pin->proc_entry = entry;
661 static void eld_proc_free(struct hdmi_spec_per_pin *per_pin)
663 if (!per_pin->codec->bus->shutdown) {
664 snd_info_free_entry(per_pin->proc_entry);
665 per_pin->proc_entry = NULL;
669 static inline int eld_proc_new(struct hdmi_spec_per_pin *per_pin,
674 static inline void eld_proc_free(struct hdmi_spec_per_pin *per_pin)
680 * Channel mapping routines
684 * Compute derived values in channel_allocations[].
686 static void init_channel_allocations(void)
689 struct cea_channel_speaker_allocation *p;
691 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
692 p = channel_allocations + i;
695 for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
696 if (p->speakers[j]) {
698 p->spk_mask |= p->speakers[j];
703 static int get_channel_allocation_order(int ca)
707 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
708 if (channel_allocations[i].ca_index == ca)
715 * The transformation takes two steps:
717 * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
718 * spk_mask => (channel_allocations[]) => ai->CA
720 * TODO: it could select the wrong CA from multiple candidates.
722 static int hdmi_channel_allocation(struct hda_codec *codec,
723 struct hdmi_eld *eld, int channels)
728 char buf[SND_PRINT_CHANNEL_ALLOCATION_ADVISED_BUFSIZE];
731 * CA defaults to 0 for basic stereo audio
737 * expand ELD's speaker allocation mask
739 * ELD tells the speaker mask in a compact(paired) form,
740 * expand ELD's notions to match the ones used by Audio InfoFrame.
742 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
743 if (eld->info.spk_alloc & (1 << i))
744 spk_mask |= eld_speaker_allocation_bits[i];
747 /* search for the first working match in the CA table */
748 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
749 if (channels == channel_allocations[i].channels &&
750 (spk_mask & channel_allocations[i].spk_mask) ==
751 channel_allocations[i].spk_mask) {
752 ca = channel_allocations[i].ca_index;
758 /* if there was no match, select the regular ALSA channel
759 * allocation with the matching number of channels */
760 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
761 if (channels == channel_allocations[i].channels) {
762 ca = channel_allocations[i].ca_index;
768 snd_print_channel_allocation(eld->info.spk_alloc, buf, sizeof(buf));
769 codec_dbg(codec, "HDMI: select CA 0x%x for %d-channel allocation: %s\n",
775 static void hdmi_debug_channel_mapping(struct hda_codec *codec,
778 #ifdef CONFIG_SND_DEBUG_VERBOSE
779 struct hdmi_spec *spec = codec->spec;
783 for (i = 0; i < 8; i++) {
784 channel = spec->ops.pin_get_slot_channel(codec, pin_nid, i);
785 codec_dbg(codec, "HDMI: ASP channel %d => slot %d\n",
791 static void hdmi_std_setup_channel_mapping(struct hda_codec *codec,
796 struct hdmi_spec *spec = codec->spec;
797 struct cea_channel_speaker_allocation *ch_alloc;
801 int non_pcm_mapping[8];
803 order = get_channel_allocation_order(ca);
804 ch_alloc = &channel_allocations[order];
806 if (hdmi_channel_mapping[ca][1] == 0) {
808 /* fill actual channel mappings in ALSA channel (i) order */
809 for (i = 0; i < ch_alloc->channels; i++) {
810 while (!ch_alloc->speakers[7 - hdmi_slot] && !WARN_ON(hdmi_slot >= 8))
811 hdmi_slot++; /* skip zero slots */
813 hdmi_channel_mapping[ca][i] = (i << 4) | hdmi_slot++;
815 /* fill the rest of the slots with ALSA channel 0xf */
816 for (hdmi_slot = 0; hdmi_slot < 8; hdmi_slot++)
817 if (!ch_alloc->speakers[7 - hdmi_slot])
818 hdmi_channel_mapping[ca][i++] = (0xf << 4) | hdmi_slot;
822 for (i = 0; i < ch_alloc->channels; i++)
823 non_pcm_mapping[i] = (i << 4) | i;
825 non_pcm_mapping[i] = (0xf << 4) | i;
828 for (i = 0; i < 8; i++) {
829 int slotsetup = non_pcm ? non_pcm_mapping[i] : hdmi_channel_mapping[ca][i];
830 int hdmi_slot = slotsetup & 0x0f;
831 int channel = (slotsetup & 0xf0) >> 4;
832 err = spec->ops.pin_set_slot_channel(codec, pin_nid, hdmi_slot, channel);
834 codec_dbg(codec, "HDMI: channel mapping failed\n");
840 struct channel_map_table {
841 unsigned char map; /* ALSA API channel map position */
842 int spk_mask; /* speaker position bit mask */
845 static struct channel_map_table map_tables[] = {
846 { SNDRV_CHMAP_FL, FL },
847 { SNDRV_CHMAP_FR, FR },
848 { SNDRV_CHMAP_RL, RL },
849 { SNDRV_CHMAP_RR, RR },
850 { SNDRV_CHMAP_LFE, LFE },
851 { SNDRV_CHMAP_FC, FC },
852 { SNDRV_CHMAP_RLC, RLC },
853 { SNDRV_CHMAP_RRC, RRC },
854 { SNDRV_CHMAP_RC, RC },
855 { SNDRV_CHMAP_FLC, FLC },
856 { SNDRV_CHMAP_FRC, FRC },
857 { SNDRV_CHMAP_TFL, FLH },
858 { SNDRV_CHMAP_TFR, FRH },
859 { SNDRV_CHMAP_FLW, FLW },
860 { SNDRV_CHMAP_FRW, FRW },
861 { SNDRV_CHMAP_TC, TC },
862 { SNDRV_CHMAP_TFC, FCH },
866 /* from ALSA API channel position to speaker bit mask */
867 static int to_spk_mask(unsigned char c)
869 struct channel_map_table *t = map_tables;
870 for (; t->map; t++) {
877 /* from ALSA API channel position to CEA slot */
878 static int to_cea_slot(int ordered_ca, unsigned char pos)
880 int mask = to_spk_mask(pos);
884 for (i = 0; i < 8; i++) {
885 if (channel_allocations[ordered_ca].speakers[7 - i] == mask)
893 /* from speaker bit mask to ALSA API channel position */
894 static int spk_to_chmap(int spk)
896 struct channel_map_table *t = map_tables;
897 for (; t->map; t++) {
898 if (t->spk_mask == spk)
904 /* from CEA slot to ALSA API channel position */
905 static int from_cea_slot(int ordered_ca, unsigned char slot)
907 int mask = channel_allocations[ordered_ca].speakers[7 - slot];
909 return spk_to_chmap(mask);
912 /* get the CA index corresponding to the given ALSA API channel map */
913 static int hdmi_manual_channel_allocation(int chs, unsigned char *map)
915 int i, spks = 0, spk_mask = 0;
917 for (i = 0; i < chs; i++) {
918 int mask = to_spk_mask(map[i]);
925 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
926 if ((chs == channel_allocations[i].channels ||
927 spks == channel_allocations[i].channels) &&
928 (spk_mask & channel_allocations[i].spk_mask) ==
929 channel_allocations[i].spk_mask)
930 return channel_allocations[i].ca_index;
935 /* set up the channel slots for the given ALSA API channel map */
936 static int hdmi_manual_setup_channel_mapping(struct hda_codec *codec,
938 int chs, unsigned char *map,
941 struct hdmi_spec *spec = codec->spec;
942 int ordered_ca = get_channel_allocation_order(ca);
943 int alsa_pos, hdmi_slot;
944 int assignments[8] = {[0 ... 7] = 0xf};
946 for (alsa_pos = 0; alsa_pos < chs; alsa_pos++) {
948 hdmi_slot = to_cea_slot(ordered_ca, map[alsa_pos]);
951 continue; /* unassigned channel */
953 assignments[hdmi_slot] = alsa_pos;
956 for (hdmi_slot = 0; hdmi_slot < 8; hdmi_slot++) {
959 err = spec->ops.pin_set_slot_channel(codec, pin_nid, hdmi_slot,
960 assignments[hdmi_slot]);
967 /* store ALSA API channel map from the current default map */
968 static void hdmi_setup_fake_chmap(unsigned char *map, int ca)
971 int ordered_ca = get_channel_allocation_order(ca);
972 for (i = 0; i < 8; i++) {
973 if (i < channel_allocations[ordered_ca].channels)
974 map[i] = from_cea_slot(ordered_ca, hdmi_channel_mapping[ca][i] & 0x0f);
980 static void hdmi_setup_channel_mapping(struct hda_codec *codec,
981 hda_nid_t pin_nid, bool non_pcm, int ca,
982 int channels, unsigned char *map,
985 if (!non_pcm && chmap_set) {
986 hdmi_manual_setup_channel_mapping(codec, pin_nid,
989 hdmi_std_setup_channel_mapping(codec, pin_nid, non_pcm, ca);
990 hdmi_setup_fake_chmap(map, ca);
993 hdmi_debug_channel_mapping(codec, pin_nid);
996 static int hdmi_pin_set_slot_channel(struct hda_codec *codec, hda_nid_t pin_nid,
997 int asp_slot, int channel)
999 return snd_hda_codec_write(codec, pin_nid, 0,
1000 AC_VERB_SET_HDMI_CHAN_SLOT,
1001 (channel << 4) | asp_slot);
1004 static int hdmi_pin_get_slot_channel(struct hda_codec *codec, hda_nid_t pin_nid,
1007 return (snd_hda_codec_read(codec, pin_nid, 0,
1008 AC_VERB_GET_HDMI_CHAN_SLOT,
1009 asp_slot) & 0xf0) >> 4;
1013 * Audio InfoFrame routines
1017 * Enable Audio InfoFrame Transmission
1019 static void hdmi_start_infoframe_trans(struct hda_codec *codec,
1022 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
1023 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
1028 * Disable Audio InfoFrame Transmission
1030 static void hdmi_stop_infoframe_trans(struct hda_codec *codec,
1033 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
1034 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
1035 AC_DIPXMIT_DISABLE);
1038 static void hdmi_debug_dip_size(struct hda_codec *codec, hda_nid_t pin_nid)
1040 #ifdef CONFIG_SND_DEBUG_VERBOSE
1044 size = snd_hdmi_get_eld_size(codec, pin_nid);
1045 codec_dbg(codec, "HDMI: ELD buf size is %d\n", size);
1047 for (i = 0; i < 8; i++) {
1048 size = snd_hda_codec_read(codec, pin_nid, 0,
1049 AC_VERB_GET_HDMI_DIP_SIZE, i);
1050 codec_dbg(codec, "HDMI: DIP GP[%d] buf size is %d\n", i, size);
1055 static void hdmi_clear_dip_buffers(struct hda_codec *codec, hda_nid_t pin_nid)
1061 for (i = 0; i < 8; i++) {
1062 size = snd_hda_codec_read(codec, pin_nid, 0,
1063 AC_VERB_GET_HDMI_DIP_SIZE, i);
1067 hdmi_set_dip_index(codec, pin_nid, i, 0x0);
1068 for (j = 1; j < 1000; j++) {
1069 hdmi_write_dip_byte(codec, pin_nid, 0x0);
1070 hdmi_get_dip_index(codec, pin_nid, &pi, &bi);
1072 codec_dbg(codec, "dip index %d: %d != %d\n",
1074 if (bi == 0) /* byte index wrapped around */
1078 "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
1084 static void hdmi_checksum_audio_infoframe(struct hdmi_audio_infoframe *hdmi_ai)
1086 u8 *bytes = (u8 *)hdmi_ai;
1090 hdmi_ai->checksum = 0;
1092 for (i = 0; i < sizeof(*hdmi_ai); i++)
1095 hdmi_ai->checksum = -sum;
1098 static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
1104 hdmi_debug_dip_size(codec, pin_nid);
1105 hdmi_clear_dip_buffers(codec, pin_nid); /* be paranoid */
1107 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
1108 for (i = 0; i < size; i++)
1109 hdmi_write_dip_byte(codec, pin_nid, dip[i]);
1112 static bool hdmi_infoframe_uptodate(struct hda_codec *codec, hda_nid_t pin_nid,
1118 if (snd_hda_codec_read(codec, pin_nid, 0, AC_VERB_GET_HDMI_DIP_XMIT, 0)
1122 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
1123 for (i = 0; i < size; i++) {
1124 val = snd_hda_codec_read(codec, pin_nid, 0,
1125 AC_VERB_GET_HDMI_DIP_DATA, 0);
1133 static void hdmi_pin_setup_infoframe(struct hda_codec *codec,
1135 int ca, int active_channels,
1138 union audio_infoframe ai;
1140 memset(&ai, 0, sizeof(ai));
1141 if (conn_type == 0) { /* HDMI */
1142 struct hdmi_audio_infoframe *hdmi_ai = &ai.hdmi;
1144 hdmi_ai->type = 0x84;
1145 hdmi_ai->ver = 0x01;
1146 hdmi_ai->len = 0x0a;
1147 hdmi_ai->CC02_CT47 = active_channels - 1;
1149 hdmi_checksum_audio_infoframe(hdmi_ai);
1150 } else if (conn_type == 1) { /* DisplayPort */
1151 struct dp_audio_infoframe *dp_ai = &ai.dp;
1155 dp_ai->ver = 0x11 << 2;
1156 dp_ai->CC02_CT47 = active_channels - 1;
1159 codec_dbg(codec, "HDMI: unknown connection type at pin %d\n",
1165 * sizeof(ai) is used instead of sizeof(*hdmi_ai) or
1166 * sizeof(*dp_ai) to avoid partial match/update problems when
1167 * the user switches between HDMI/DP monitors.
1169 if (!hdmi_infoframe_uptodate(codec, pin_nid, ai.bytes,
1172 "hdmi_pin_setup_infoframe: pin=%d channels=%d ca=0x%02x\n",
1174 active_channels, ca);
1175 hdmi_stop_infoframe_trans(codec, pin_nid);
1176 hdmi_fill_audio_infoframe(codec, pin_nid,
1177 ai.bytes, sizeof(ai));
1178 hdmi_start_infoframe_trans(codec, pin_nid);
1182 static void hdmi_setup_audio_infoframe(struct hda_codec *codec,
1183 struct hdmi_spec_per_pin *per_pin,
1186 struct hdmi_spec *spec = codec->spec;
1187 hda_nid_t pin_nid = per_pin->pin_nid;
1188 int channels = per_pin->channels;
1189 int active_channels;
1190 struct hdmi_eld *eld;
1196 if (is_haswell_plus(codec))
1197 snd_hda_codec_write(codec, pin_nid, 0,
1198 AC_VERB_SET_AMP_GAIN_MUTE,
1201 eld = &per_pin->sink_eld;
1203 if (!non_pcm && per_pin->chmap_set)
1204 ca = hdmi_manual_channel_allocation(channels, per_pin->chmap);
1206 ca = hdmi_channel_allocation(codec, eld, channels);
1210 ordered_ca = get_channel_allocation_order(ca);
1211 active_channels = channel_allocations[ordered_ca].channels;
1213 hdmi_set_channel_count(codec, per_pin->cvt_nid, active_channels);
1216 * always configure channel mapping, it may have been changed by the
1217 * user in the meantime
1219 hdmi_setup_channel_mapping(codec, pin_nid, non_pcm, ca,
1220 channels, per_pin->chmap,
1221 per_pin->chmap_set);
1223 spec->ops.pin_setup_infoframe(codec, pin_nid, ca, active_channels,
1224 eld->info.conn_type);
1226 per_pin->non_pcm = non_pcm;
1230 * Unsolicited events
1233 static bool hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll);
1235 static void check_presence_and_report(struct hda_codec *codec, hda_nid_t nid)
1237 struct hdmi_spec *spec = codec->spec;
1238 int pin_idx = pin_nid_to_pin_index(codec, nid);
1242 if (hdmi_present_sense(get_pin(spec, pin_idx), 1))
1243 snd_hda_jack_report_sync(codec);
1246 static void jack_callback(struct hda_codec *codec,
1247 struct hda_jack_callback *jack)
1249 check_presence_and_report(codec, jack->nid);
1252 static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
1254 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
1255 struct hda_jack_tbl *jack;
1256 int dev_entry = (res & AC_UNSOL_RES_DE) >> AC_UNSOL_RES_DE_SHIFT;
1258 jack = snd_hda_jack_tbl_get_from_tag(codec, tag);
1261 jack->jack_dirty = 1;
1264 "HDMI hot plug event: Codec=%d Pin=%d Device=%d Inactive=%d Presence_Detect=%d ELD_Valid=%d\n",
1265 codec->addr, jack->nid, dev_entry, !!(res & AC_UNSOL_RES_IA),
1266 !!(res & AC_UNSOL_RES_PD), !!(res & AC_UNSOL_RES_ELDV));
1268 check_presence_and_report(codec, jack->nid);
1271 static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
1273 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
1274 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
1275 int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
1276 int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
1279 "HDMI CP event: CODEC=%d TAG=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
1294 static void hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
1296 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
1297 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
1299 if (!snd_hda_jack_tbl_get_from_tag(codec, tag)) {
1300 codec_dbg(codec, "Unexpected HDMI event tag 0x%x\n", tag);
1305 hdmi_intrinsic_event(codec, res);
1307 hdmi_non_intrinsic_event(codec, res);
1310 static void haswell_verify_D0(struct hda_codec *codec,
1311 hda_nid_t cvt_nid, hda_nid_t nid)
1315 /* For Haswell, the converter 1/2 may keep in D3 state after bootup,
1316 * thus pins could only choose converter 0 for use. Make sure the
1317 * converters are in correct power state */
1318 if (!snd_hda_check_power_state(codec, cvt_nid, AC_PWRST_D0))
1319 snd_hda_codec_write(codec, cvt_nid, 0, AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
1321 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0)) {
1322 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
1325 pwr = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
1326 pwr = (pwr & AC_PWRST_ACTUAL) >> AC_PWRST_ACTUAL_SHIFT;
1327 codec_dbg(codec, "Haswell HDMI audio: Power for pin 0x%x is now D%d\n", nid, pwr);
1335 /* HBR should be Non-PCM, 8 channels */
1336 #define is_hbr_format(format) \
1337 ((format & AC_FMT_TYPE_NON_PCM) && (format & AC_FMT_CHAN_MASK) == 7)
1339 static int hdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
1342 int pinctl, new_pinctl;
1344 if (snd_hda_query_pin_caps(codec, pin_nid) & AC_PINCAP_HBR) {
1345 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
1346 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1349 return hbr ? -EINVAL : 0;
1351 new_pinctl = pinctl & ~AC_PINCTL_EPT;
1353 new_pinctl |= AC_PINCTL_EPT_HBR;
1355 new_pinctl |= AC_PINCTL_EPT_NATIVE;
1358 "hdmi_pin_hbr_setup: NID=0x%x, %spinctl=0x%x\n",
1360 pinctl == new_pinctl ? "" : "new-",
1363 if (pinctl != new_pinctl)
1364 snd_hda_codec_write(codec, pin_nid, 0,
1365 AC_VERB_SET_PIN_WIDGET_CONTROL,
1373 static int hdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
1374 hda_nid_t pin_nid, u32 stream_tag, int format)
1376 struct hdmi_spec *spec = codec->spec;
1379 if (is_haswell_plus(codec))
1380 haswell_verify_D0(codec, cvt_nid, pin_nid);
1382 err = spec->ops.pin_hbr_setup(codec, pin_nid, is_hbr_format(format));
1385 codec_dbg(codec, "hdmi_setup_stream: HBR is not supported\n");
1389 snd_hda_codec_setup_stream(codec, cvt_nid, stream_tag, 0, format);
1393 /* Try to find an available converter
1394 * If pin_idx is less then zero, just try to find an available converter.
1395 * Otherwise, try to find an available converter and get the cvt mux index
1398 static int hdmi_choose_cvt(struct hda_codec *codec,
1399 int pin_idx, int *cvt_id, int *mux_id)
1401 struct hdmi_spec *spec = codec->spec;
1402 struct hdmi_spec_per_pin *per_pin;
1403 struct hdmi_spec_per_cvt *per_cvt = NULL;
1404 int cvt_idx, mux_idx = 0;
1406 /* pin_idx < 0 means no pin will be bound to the converter */
1410 per_pin = get_pin(spec, pin_idx);
1412 /* Dynamically assign converter to stream */
1413 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
1414 per_cvt = get_cvt(spec, cvt_idx);
1416 /* Must not already be assigned */
1417 if (per_cvt->assigned)
1419 if (per_pin == NULL)
1421 /* Must be in pin's mux's list of converters */
1422 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
1423 if (per_pin->mux_nids[mux_idx] == per_cvt->cvt_nid)
1425 /* Not in mux list */
1426 if (mux_idx == per_pin->num_mux_nids)
1431 /* No free converters */
1432 if (cvt_idx == spec->num_cvts)
1435 if (per_pin != NULL)
1436 per_pin->mux_idx = mux_idx;
1446 /* Assure the pin select the right convetor */
1447 static void intel_verify_pin_cvt_connect(struct hda_codec *codec,
1448 struct hdmi_spec_per_pin *per_pin)
1450 hda_nid_t pin_nid = per_pin->pin_nid;
1453 mux_idx = per_pin->mux_idx;
1454 curr = snd_hda_codec_read(codec, pin_nid, 0,
1455 AC_VERB_GET_CONNECT_SEL, 0);
1456 if (curr != mux_idx)
1457 snd_hda_codec_write_cache(codec, pin_nid, 0,
1458 AC_VERB_SET_CONNECT_SEL,
1462 /* get the mux index for the converter of the pins
1463 * converter's mux index is the same for all pins on Intel platform
1465 static int intel_cvt_id_to_mux_idx(struct hdmi_spec *spec,
1470 for (i = 0; i < spec->num_cvts; i++)
1471 if (spec->cvt_nids[i] == cvt_nid)
1476 /* Intel HDMI workaround to fix audio routing issue:
1477 * For some Intel display codecs, pins share the same connection list.
1478 * So a conveter can be selected by multiple pins and playback on any of these
1479 * pins will generate sound on the external display, because audio flows from
1480 * the same converter to the display pipeline. Also muting one pin may make
1481 * other pins have no sound output.
1482 * So this function assures that an assigned converter for a pin is not selected
1483 * by any other pins.
1485 static void intel_not_share_assigned_cvt(struct hda_codec *codec,
1486 hda_nid_t pin_nid, int mux_idx)
1488 struct hdmi_spec *spec = codec->spec;
1491 struct hdmi_spec_per_cvt *per_cvt;
1493 /* configure all pins, including "no physical connection" ones */
1494 for_each_hda_codec_node(nid, codec) {
1495 unsigned int wid_caps = get_wcaps(codec, nid);
1496 unsigned int wid_type = get_wcaps_type(wid_caps);
1498 if (wid_type != AC_WID_PIN)
1504 curr = snd_hda_codec_read(codec, nid, 0,
1505 AC_VERB_GET_CONNECT_SEL, 0);
1506 if (curr != mux_idx)
1509 /* choose an unassigned converter. The conveters in the
1510 * connection list are in the same order as in the codec.
1512 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
1513 per_cvt = get_cvt(spec, cvt_idx);
1514 if (!per_cvt->assigned) {
1516 "choose cvt %d for pin nid %d\n",
1518 snd_hda_codec_write_cache(codec, nid, 0,
1519 AC_VERB_SET_CONNECT_SEL,
1527 /* A wrapper of intel_not_share_asigned_cvt() */
1528 static void intel_not_share_assigned_cvt_nid(struct hda_codec *codec,
1529 hda_nid_t pin_nid, hda_nid_t cvt_nid)
1532 struct hdmi_spec *spec = codec->spec;
1534 if (!is_haswell_plus(codec) && !is_valleyview_plus(codec))
1537 /* On Intel platform, the mapping of converter nid to
1538 * mux index of the pins are always the same.
1539 * The pin nid may be 0, this means all pins will not
1540 * share the converter.
1542 mux_idx = intel_cvt_id_to_mux_idx(spec, cvt_nid);
1544 intel_not_share_assigned_cvt(codec, pin_nid, mux_idx);
1547 /* called in hdmi_pcm_open when no pin is assigned to the PCM
1548 * in dyn_pcm_assign mode.
1550 static int hdmi_pcm_open_no_pin(struct hda_pcm_stream *hinfo,
1551 struct hda_codec *codec,
1552 struct snd_pcm_substream *substream)
1554 struct hdmi_spec *spec = codec->spec;
1555 struct snd_pcm_runtime *runtime = substream->runtime;
1556 int cvt_idx, pcm_idx;
1557 struct hdmi_spec_per_cvt *per_cvt = NULL;
1560 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1564 err = hdmi_choose_cvt(codec, -1, &cvt_idx, NULL);
1568 per_cvt = get_cvt(spec, cvt_idx);
1569 per_cvt->assigned = 1;
1570 hinfo->nid = per_cvt->cvt_nid;
1572 intel_not_share_assigned_cvt_nid(codec, 0, per_cvt->cvt_nid);
1574 set_bit(pcm_idx, &spec->pcm_in_use);
1575 /* todo: setup spdif ctls assign */
1577 /* Initially set the converter's capabilities */
1578 hinfo->channels_min = per_cvt->channels_min;
1579 hinfo->channels_max = per_cvt->channels_max;
1580 hinfo->rates = per_cvt->rates;
1581 hinfo->formats = per_cvt->formats;
1582 hinfo->maxbps = per_cvt->maxbps;
1584 /* Store the updated parameters */
1585 runtime->hw.channels_min = hinfo->channels_min;
1586 runtime->hw.channels_max = hinfo->channels_max;
1587 runtime->hw.formats = hinfo->formats;
1588 runtime->hw.rates = hinfo->rates;
1590 snd_pcm_hw_constraint_step(substream->runtime, 0,
1591 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
1598 static int hdmi_pcm_open(struct hda_pcm_stream *hinfo,
1599 struct hda_codec *codec,
1600 struct snd_pcm_substream *substream)
1602 struct hdmi_spec *spec = codec->spec;
1603 struct snd_pcm_runtime *runtime = substream->runtime;
1604 int pin_idx, cvt_idx, pcm_idx, mux_idx = 0;
1605 struct hdmi_spec_per_pin *per_pin;
1606 struct hdmi_eld *eld;
1607 struct hdmi_spec_per_cvt *per_cvt = NULL;
1610 /* Validate hinfo */
1611 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1615 mutex_lock(&spec->pcm_lock);
1616 pin_idx = hinfo_to_pin_index(codec, hinfo);
1617 if (!spec->dyn_pcm_assign) {
1618 if (snd_BUG_ON(pin_idx < 0)) {
1619 mutex_unlock(&spec->pcm_lock);
1623 /* no pin is assigned to the PCM
1624 * PA need pcm open successfully when probe
1627 err = hdmi_pcm_open_no_pin(hinfo, codec, substream);
1628 mutex_unlock(&spec->pcm_lock);
1633 err = hdmi_choose_cvt(codec, pin_idx, &cvt_idx, &mux_idx);
1635 mutex_unlock(&spec->pcm_lock);
1639 per_cvt = get_cvt(spec, cvt_idx);
1640 /* Claim converter */
1641 per_cvt->assigned = 1;
1643 set_bit(pcm_idx, &spec->pcm_in_use);
1644 per_pin = get_pin(spec, pin_idx);
1645 per_pin->cvt_nid = per_cvt->cvt_nid;
1646 hinfo->nid = per_cvt->cvt_nid;
1648 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
1649 AC_VERB_SET_CONNECT_SEL,
1652 /* configure unused pins to choose other converters */
1653 if (is_haswell_plus(codec) || is_valleyview_plus(codec))
1654 intel_not_share_assigned_cvt(codec, per_pin->pin_nid, mux_idx);
1656 snd_hda_spdif_ctls_assign(codec, pcm_idx, per_cvt->cvt_nid);
1658 /* Initially set the converter's capabilities */
1659 hinfo->channels_min = per_cvt->channels_min;
1660 hinfo->channels_max = per_cvt->channels_max;
1661 hinfo->rates = per_cvt->rates;
1662 hinfo->formats = per_cvt->formats;
1663 hinfo->maxbps = per_cvt->maxbps;
1665 eld = &per_pin->sink_eld;
1666 /* Restrict capabilities by ELD if this isn't disabled */
1667 if (!static_hdmi_pcm && eld->eld_valid) {
1668 snd_hdmi_eld_update_pcm_info(&eld->info, hinfo);
1669 if (hinfo->channels_min > hinfo->channels_max ||
1670 !hinfo->rates || !hinfo->formats) {
1671 per_cvt->assigned = 0;
1673 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
1674 mutex_unlock(&spec->pcm_lock);
1679 mutex_unlock(&spec->pcm_lock);
1680 /* Store the updated parameters */
1681 runtime->hw.channels_min = hinfo->channels_min;
1682 runtime->hw.channels_max = hinfo->channels_max;
1683 runtime->hw.formats = hinfo->formats;
1684 runtime->hw.rates = hinfo->rates;
1686 snd_pcm_hw_constraint_step(substream->runtime, 0,
1687 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
1692 * HDA/HDMI auto parsing
1694 static int hdmi_read_pin_conn(struct hda_codec *codec, int pin_idx)
1696 struct hdmi_spec *spec = codec->spec;
1697 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1698 hda_nid_t pin_nid = per_pin->pin_nid;
1700 if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
1702 "HDMI: pin %d wcaps %#x does not support connection list\n",
1703 pin_nid, get_wcaps(codec, pin_nid));
1707 per_pin->num_mux_nids = snd_hda_get_connections(codec, pin_nid,
1709 HDA_MAX_CONNECTIONS);
1714 static int hdmi_find_pcm_slot(struct hdmi_spec *spec,
1715 struct hdmi_spec_per_pin *per_pin)
1719 /* try the prefer PCM */
1720 if (!test_bit(per_pin->pin_nid_idx, &spec->pcm_bitmap))
1721 return per_pin->pin_nid_idx;
1723 /* have a second try; check the "reserved area" over num_pins */
1724 for (i = spec->num_pins; i < spec->pcm_used; i++) {
1725 if (!test_bit(i, &spec->pcm_bitmap))
1729 /* the last try; check the empty slots in pins */
1730 for (i = 0; i < spec->num_pins; i++) {
1731 if (!test_bit(i, &spec->pcm_bitmap))
1737 static void hdmi_attach_hda_pcm(struct hdmi_spec *spec,
1738 struct hdmi_spec_per_pin *per_pin)
1742 /* pcm already be attached to the pin */
1745 idx = hdmi_find_pcm_slot(spec, per_pin);
1748 per_pin->pcm_idx = idx;
1749 per_pin->pcm = get_hdmi_pcm(spec, idx);
1750 set_bit(idx, &spec->pcm_bitmap);
1753 static void hdmi_detach_hda_pcm(struct hdmi_spec *spec,
1754 struct hdmi_spec_per_pin *per_pin)
1758 /* pcm already be detached from the pin */
1761 idx = per_pin->pcm_idx;
1762 per_pin->pcm_idx = -1;
1763 per_pin->pcm = NULL;
1764 if (idx >= 0 && idx < spec->pcm_used)
1765 clear_bit(idx, &spec->pcm_bitmap);
1768 static int hdmi_get_pin_cvt_mux(struct hdmi_spec *spec,
1769 struct hdmi_spec_per_pin *per_pin, hda_nid_t cvt_nid)
1773 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
1774 if (per_pin->mux_nids[mux_idx] == cvt_nid)
1779 static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid);
1781 static void hdmi_pcm_setup_pin(struct hdmi_spec *spec,
1782 struct hdmi_spec_per_pin *per_pin)
1784 struct hda_codec *codec = per_pin->codec;
1785 struct hda_pcm *pcm;
1786 struct hda_pcm_stream *hinfo;
1787 struct snd_pcm_substream *substream;
1791 if (per_pin->pcm_idx >= 0 && per_pin->pcm_idx < spec->pcm_used)
1792 pcm = get_pcm_rec(spec, per_pin->pcm_idx);
1795 if (!test_bit(per_pin->pcm_idx, &spec->pcm_in_use))
1798 /* hdmi audio only uses playback and one substream */
1799 hinfo = pcm->stream;
1800 substream = pcm->pcm->streams[0].substream;
1802 per_pin->cvt_nid = hinfo->nid;
1804 mux_idx = hdmi_get_pin_cvt_mux(spec, per_pin, hinfo->nid);
1805 if (mux_idx < per_pin->num_mux_nids)
1806 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
1807 AC_VERB_SET_CONNECT_SEL,
1809 snd_hda_spdif_ctls_assign(codec, per_pin->pcm_idx, hinfo->nid);
1811 non_pcm = check_non_pcm_per_cvt(codec, hinfo->nid);
1812 if (substream->runtime)
1813 per_pin->channels = substream->runtime->channels;
1814 per_pin->setup = true;
1815 per_pin->mux_idx = mux_idx;
1817 hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
1820 static void hdmi_pcm_reset_pin(struct hdmi_spec *spec,
1821 struct hdmi_spec_per_pin *per_pin)
1823 if (per_pin->pcm_idx >= 0 && per_pin->pcm_idx < spec->pcm_used)
1824 snd_hda_spdif_ctls_unassign(per_pin->codec, per_pin->pcm_idx);
1826 per_pin->chmap_set = false;
1827 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
1829 per_pin->setup = false;
1830 per_pin->channels = 0;
1833 /* update per_pin ELD from the given new ELD;
1834 * setup info frame and notification accordingly
1836 static void update_eld(struct hda_codec *codec,
1837 struct hdmi_spec_per_pin *per_pin,
1838 struct hdmi_eld *eld)
1840 struct hdmi_eld *pin_eld = &per_pin->sink_eld;
1841 struct hdmi_spec *spec = codec->spec;
1842 bool old_eld_valid = pin_eld->eld_valid;
1845 if (spec->dyn_pcm_assign) {
1846 if (eld->eld_valid) {
1847 hdmi_attach_hda_pcm(spec, per_pin);
1848 hdmi_pcm_setup_pin(spec, per_pin);
1850 hdmi_pcm_reset_pin(spec, per_pin);
1851 hdmi_detach_hda_pcm(spec, per_pin);
1856 snd_hdmi_show_eld(codec, &eld->info);
1858 eld_changed = (pin_eld->eld_valid != eld->eld_valid);
1859 if (eld->eld_valid && pin_eld->eld_valid)
1860 if (pin_eld->eld_size != eld->eld_size ||
1861 memcmp(pin_eld->eld_buffer, eld->eld_buffer,
1862 eld->eld_size) != 0)
1865 pin_eld->eld_valid = eld->eld_valid;
1866 pin_eld->eld_size = eld->eld_size;
1868 memcpy(pin_eld->eld_buffer, eld->eld_buffer, eld->eld_size);
1869 pin_eld->info = eld->info;
1872 * Re-setup pin and infoframe. This is needed e.g. when
1873 * - sink is first plugged-in
1874 * - transcoder can change during stream playback on Haswell
1875 * and this can make HW reset converter selection on a pin.
1877 if (eld->eld_valid && !old_eld_valid && per_pin->setup) {
1878 if (is_haswell_plus(codec) || is_valleyview_plus(codec)) {
1879 intel_verify_pin_cvt_connect(codec, per_pin);
1880 intel_not_share_assigned_cvt(codec, per_pin->pin_nid,
1884 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
1888 snd_ctl_notify(codec->card,
1889 SNDRV_CTL_EVENT_MASK_VALUE |
1890 SNDRV_CTL_EVENT_MASK_INFO,
1891 &per_pin->eld_ctl->id);
1894 /* update ELD and jack state via HD-audio verbs */
1895 static bool hdmi_present_sense_via_verbs(struct hdmi_spec_per_pin *per_pin,
1898 struct hda_jack_tbl *jack;
1899 struct hda_codec *codec = per_pin->codec;
1900 struct hdmi_spec *spec = codec->spec;
1901 struct hdmi_eld *eld = &spec->temp_eld;
1902 struct hdmi_eld *pin_eld = &per_pin->sink_eld;
1903 hda_nid_t pin_nid = per_pin->pin_nid;
1905 * Always execute a GetPinSense verb here, even when called from
1906 * hdmi_intrinsic_event; for some NVIDIA HW, the unsolicited
1907 * response's PD bit is not the real PD value, but indicates that
1908 * the real PD value changed. An older version of the HD-audio
1909 * specification worked this way. Hence, we just ignore the data in
1910 * the unsolicited response to avoid custom WARs.
1914 bool do_repoll = false;
1916 snd_hda_power_up_pm(codec);
1917 present = snd_hda_pin_sense(codec, pin_nid);
1919 mutex_lock(&per_pin->lock);
1920 pin_eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
1921 if (pin_eld->monitor_present)
1922 eld->eld_valid = !!(present & AC_PINSENSE_ELDV);
1924 eld->eld_valid = false;
1927 "HDMI status: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
1928 codec->addr, pin_nid, pin_eld->monitor_present, eld->eld_valid);
1930 if (eld->eld_valid) {
1931 if (spec->ops.pin_get_eld(codec, pin_nid, eld->eld_buffer,
1932 &eld->eld_size) < 0)
1933 eld->eld_valid = false;
1935 if (snd_hdmi_parse_eld(codec, &eld->info, eld->eld_buffer,
1937 eld->eld_valid = false;
1939 if (!eld->eld_valid && repoll)
1944 schedule_delayed_work(&per_pin->work, msecs_to_jiffies(300));
1946 update_eld(codec, per_pin, eld);
1948 ret = !repoll || !pin_eld->monitor_present || pin_eld->eld_valid;
1950 jack = snd_hda_jack_tbl_get(codec, pin_nid);
1952 jack->block_report = !ret;
1954 mutex_unlock(&per_pin->lock);
1955 snd_hda_power_down_pm(codec);
1959 /* update ELD and jack state via audio component */
1960 static void sync_eld_via_acomp(struct hda_codec *codec,
1961 struct hdmi_spec_per_pin *per_pin)
1963 struct hdmi_spec *spec = codec->spec;
1964 struct hdmi_eld *eld = &spec->temp_eld;
1965 struct snd_jack *jack = NULL;
1968 mutex_lock(&per_pin->lock);
1969 size = snd_hdac_acomp_get_eld(&codec->bus->core, per_pin->pin_nid,
1970 &eld->monitor_present, eld->eld_buffer,
1975 size = min(size, ELD_MAX_SIZE);
1976 if (snd_hdmi_parse_eld(codec, &eld->info,
1977 eld->eld_buffer, size) < 0)
1982 eld->eld_valid = true;
1983 eld->eld_size = size;
1985 eld->eld_valid = false;
1989 /* pcm_idx >=0 before update_eld() means it is in monitor
1990 * disconnected event. Jack must be fetched before update_eld()
1992 if (per_pin->pcm_idx >= 0)
1993 jack = spec->pcm_rec[per_pin->pcm_idx].jack;
1994 update_eld(codec, per_pin, eld);
1995 if (jack == NULL && per_pin->pcm_idx >= 0)
1996 jack = spec->pcm_rec[per_pin->pcm_idx].jack;
1999 snd_jack_report(jack,
2000 eld->monitor_present ? SND_JACK_AVOUT : 0);
2002 mutex_unlock(&per_pin->lock);
2005 static bool hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll)
2007 struct hda_codec *codec = per_pin->codec;
2008 struct hdmi_spec *spec = codec->spec;
2011 mutex_lock(&spec->pcm_lock);
2012 if (codec_has_acomp(codec)) {
2013 sync_eld_via_acomp(codec, per_pin);
2014 ret = false; /* don't call snd_hda_jack_report_sync() */
2016 ret = hdmi_present_sense_via_verbs(per_pin, repoll);
2018 mutex_unlock(&spec->pcm_lock);
2023 static void hdmi_repoll_eld(struct work_struct *work)
2025 struct hdmi_spec_per_pin *per_pin =
2026 container_of(to_delayed_work(work), struct hdmi_spec_per_pin, work);
2028 if (per_pin->repoll_count++ > 6)
2029 per_pin->repoll_count = 0;
2031 if (hdmi_present_sense(per_pin, per_pin->repoll_count))
2032 snd_hda_jack_report_sync(per_pin->codec);
2035 static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
2038 static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
2040 struct hdmi_spec *spec = codec->spec;
2041 unsigned int caps, config;
2043 struct hdmi_spec_per_pin *per_pin;
2046 caps = snd_hda_query_pin_caps(codec, pin_nid);
2047 if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP)))
2050 config = snd_hda_codec_get_pincfg(codec, pin_nid);
2051 if (get_defcfg_connect(config) == AC_JACK_PORT_NONE)
2054 if (is_haswell_plus(codec))
2055 intel_haswell_fixup_connect_list(codec, pin_nid);
2057 pin_idx = spec->num_pins;
2058 per_pin = snd_array_new(&spec->pins);
2062 per_pin->pin_nid = pin_nid;
2063 per_pin->non_pcm = false;
2064 if (spec->dyn_pcm_assign)
2065 per_pin->pcm_idx = -1;
2067 per_pin->pcm = get_hdmi_pcm(spec, pin_idx);
2068 per_pin->pcm_idx = pin_idx;
2070 per_pin->pin_nid_idx = pin_idx;
2072 err = hdmi_read_pin_conn(codec, pin_idx);
2081 static int hdmi_add_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
2083 struct hdmi_spec *spec = codec->spec;
2084 struct hdmi_spec_per_cvt *per_cvt;
2088 chans = get_wcaps(codec, cvt_nid);
2089 chans = get_wcaps_channels(chans);
2091 per_cvt = snd_array_new(&spec->cvts);
2095 per_cvt->cvt_nid = cvt_nid;
2096 per_cvt->channels_min = 2;
2098 per_cvt->channels_max = chans;
2099 if (chans > spec->channels_max)
2100 spec->channels_max = chans;
2103 err = snd_hda_query_supported_pcm(codec, cvt_nid,
2110 if (spec->num_cvts < ARRAY_SIZE(spec->cvt_nids))
2111 spec->cvt_nids[spec->num_cvts] = cvt_nid;
2117 static int hdmi_parse_codec(struct hda_codec *codec)
2122 nodes = snd_hda_get_sub_nodes(codec, codec->core.afg, &nid);
2123 if (!nid || nodes < 0) {
2124 codec_warn(codec, "HDMI: failed to get afg sub nodes\n");
2128 for (i = 0; i < nodes; i++, nid++) {
2132 caps = get_wcaps(codec, nid);
2133 type = get_wcaps_type(caps);
2135 if (!(caps & AC_WCAP_DIGITAL))
2139 case AC_WID_AUD_OUT:
2140 hdmi_add_cvt(codec, nid);
2143 hdmi_add_pin(codec, nid);
2153 static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
2155 struct hda_spdif_out *spdif;
2158 mutex_lock(&codec->spdif_mutex);
2159 spdif = snd_hda_spdif_out_of_nid(codec, cvt_nid);
2160 non_pcm = !!(spdif->status & IEC958_AES0_NONAUDIO);
2161 mutex_unlock(&codec->spdif_mutex);
2169 static int generic_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2170 struct hda_codec *codec,
2171 unsigned int stream_tag,
2172 unsigned int format,
2173 struct snd_pcm_substream *substream)
2175 hda_nid_t cvt_nid = hinfo->nid;
2176 struct hdmi_spec *spec = codec->spec;
2178 struct hdmi_spec_per_pin *per_pin;
2180 struct snd_pcm_runtime *runtime = substream->runtime;
2185 mutex_lock(&spec->pcm_lock);
2186 pin_idx = hinfo_to_pin_index(codec, hinfo);
2187 if (spec->dyn_pcm_assign && pin_idx < 0) {
2188 /* when dyn_pcm_assign and pcm is not bound to a pin
2189 * skip pin setup and return 0 to make audio playback
2192 intel_not_share_assigned_cvt_nid(codec, 0, cvt_nid);
2193 snd_hda_codec_setup_stream(codec, cvt_nid,
2194 stream_tag, 0, format);
2195 mutex_unlock(&spec->pcm_lock);
2199 if (snd_BUG_ON(pin_idx < 0)) {
2200 mutex_unlock(&spec->pcm_lock);
2203 per_pin = get_pin(spec, pin_idx);
2204 pin_nid = per_pin->pin_nid;
2205 if (is_haswell_plus(codec) || is_valleyview_plus(codec)) {
2206 /* Verify pin:cvt selections to avoid silent audio after S3.
2207 * After S3, the audio driver restores pin:cvt selections
2208 * but this can happen before gfx is ready and such selection
2209 * is overlooked by HW. Thus multiple pins can share a same
2210 * default convertor and mute control will affect each other,
2211 * which can cause a resumed audio playback become silent
2214 intel_verify_pin_cvt_connect(codec, per_pin);
2215 intel_not_share_assigned_cvt(codec, pin_nid, per_pin->mux_idx);
2218 /* Call sync_audio_rate to set the N/CTS/M manually if necessary */
2219 /* Todo: add DP1.2 MST audio support later */
2220 snd_hdac_sync_audio_rate(&codec->bus->core, pin_nid, runtime->rate);
2222 non_pcm = check_non_pcm_per_cvt(codec, cvt_nid);
2223 mutex_lock(&per_pin->lock);
2224 per_pin->channels = substream->runtime->channels;
2225 per_pin->setup = true;
2227 hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
2228 mutex_unlock(&per_pin->lock);
2229 if (spec->dyn_pin_out) {
2230 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
2231 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2232 snd_hda_codec_write(codec, pin_nid, 0,
2233 AC_VERB_SET_PIN_WIDGET_CONTROL,
2237 err = spec->ops.setup_stream(codec, cvt_nid, pin_nid,
2238 stream_tag, format);
2239 mutex_unlock(&spec->pcm_lock);
2243 static int generic_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
2244 struct hda_codec *codec,
2245 struct snd_pcm_substream *substream)
2247 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
2251 static int hdmi_pcm_close(struct hda_pcm_stream *hinfo,
2252 struct hda_codec *codec,
2253 struct snd_pcm_substream *substream)
2255 struct hdmi_spec *spec = codec->spec;
2256 int cvt_idx, pin_idx, pcm_idx;
2257 struct hdmi_spec_per_cvt *per_cvt;
2258 struct hdmi_spec_per_pin *per_pin;
2262 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
2263 if (snd_BUG_ON(pcm_idx < 0))
2265 cvt_idx = cvt_nid_to_cvt_index(codec, hinfo->nid);
2266 if (snd_BUG_ON(cvt_idx < 0))
2268 per_cvt = get_cvt(spec, cvt_idx);
2270 snd_BUG_ON(!per_cvt->assigned);
2271 per_cvt->assigned = 0;
2274 mutex_lock(&spec->pcm_lock);
2275 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
2276 clear_bit(pcm_idx, &spec->pcm_in_use);
2277 pin_idx = hinfo_to_pin_index(codec, hinfo);
2278 if (spec->dyn_pcm_assign && pin_idx < 0) {
2279 mutex_unlock(&spec->pcm_lock);
2283 if (snd_BUG_ON(pin_idx < 0)) {
2284 mutex_unlock(&spec->pcm_lock);
2287 per_pin = get_pin(spec, pin_idx);
2289 if (spec->dyn_pin_out) {
2290 pinctl = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
2291 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2292 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
2293 AC_VERB_SET_PIN_WIDGET_CONTROL,
2297 mutex_lock(&per_pin->lock);
2298 per_pin->chmap_set = false;
2299 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
2301 per_pin->setup = false;
2302 per_pin->channels = 0;
2303 mutex_unlock(&per_pin->lock);
2304 mutex_unlock(&spec->pcm_lock);
2310 static const struct hda_pcm_ops generic_ops = {
2311 .open = hdmi_pcm_open,
2312 .close = hdmi_pcm_close,
2313 .prepare = generic_hdmi_playback_pcm_prepare,
2314 .cleanup = generic_hdmi_playback_pcm_cleanup,
2318 * ALSA API channel-map control callbacks
2320 static int hdmi_chmap_ctl_info(struct snd_kcontrol *kcontrol,
2321 struct snd_ctl_elem_info *uinfo)
2323 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2324 struct hda_codec *codec = info->private_data;
2325 struct hdmi_spec *spec = codec->spec;
2326 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2327 uinfo->count = spec->channels_max;
2328 uinfo->value.integer.min = 0;
2329 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
2333 static int hdmi_chmap_cea_alloc_validate_get_type(struct cea_channel_speaker_allocation *cap,
2336 /* If the speaker allocation matches the channel count, it is OK.*/
2337 if (cap->channels != channels)
2340 /* all channels are remappable freely */
2341 return SNDRV_CTL_TLVT_CHMAP_VAR;
2344 static void hdmi_cea_alloc_to_tlv_chmap(struct cea_channel_speaker_allocation *cap,
2345 unsigned int *chmap, int channels)
2350 for (c = 7; c >= 0; c--) {
2351 int spk = cap->speakers[c];
2355 chmap[count++] = spk_to_chmap(spk);
2358 WARN_ON(count != channels);
2361 static int hdmi_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2362 unsigned int size, unsigned int __user *tlv)
2364 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2365 struct hda_codec *codec = info->private_data;
2366 struct hdmi_spec *spec = codec->spec;
2367 unsigned int __user *dst;
2372 if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
2376 for (chs = 2; chs <= spec->channels_max; chs++) {
2378 struct cea_channel_speaker_allocation *cap;
2379 cap = channel_allocations;
2380 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++, cap++) {
2381 int chs_bytes = chs * 4;
2382 int type = spec->ops.chmap_cea_alloc_validate_get_type(cap, chs);
2383 unsigned int tlv_chmap[8];
2389 if (put_user(type, dst) ||
2390 put_user(chs_bytes, dst + 1))
2395 if (size < chs_bytes)
2399 spec->ops.cea_alloc_to_tlv_chmap(cap, tlv_chmap, chs);
2400 if (copy_to_user(dst, tlv_chmap, chs_bytes))
2405 if (put_user(count, tlv + 1))
2410 static int hdmi_chmap_ctl_get(struct snd_kcontrol *kcontrol,
2411 struct snd_ctl_elem_value *ucontrol)
2413 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2414 struct hda_codec *codec = info->private_data;
2415 struct hdmi_spec *spec = codec->spec;
2416 int pcm_idx = kcontrol->private_value;
2417 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
2421 for (i = 0; i < spec->channels_max; i++)
2422 ucontrol->value.integer.value[i] = 0;
2426 for (i = 0; i < ARRAY_SIZE(per_pin->chmap); i++)
2427 ucontrol->value.integer.value[i] = per_pin->chmap[i];
2431 static int hdmi_chmap_ctl_put(struct snd_kcontrol *kcontrol,
2432 struct snd_ctl_elem_value *ucontrol)
2434 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2435 struct hda_codec *codec = info->private_data;
2436 struct hdmi_spec *spec = codec->spec;
2437 int pcm_idx = kcontrol->private_value;
2438 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx);
2439 unsigned int ctl_idx;
2440 struct snd_pcm_substream *substream;
2441 unsigned char chmap[8];
2442 int i, err, ca, prepared = 0;
2444 /* No monitor is connected in dyn_pcm_assign.
2445 * It's invalid to setup the chmap
2450 ctl_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2451 substream = snd_pcm_chmap_substream(info, ctl_idx);
2452 if (!substream || !substream->runtime)
2453 return 0; /* just for avoiding error from alsactl restore */
2454 switch (substream->runtime->status->state) {
2455 case SNDRV_PCM_STATE_OPEN:
2456 case SNDRV_PCM_STATE_SETUP:
2458 case SNDRV_PCM_STATE_PREPARED:
2464 memset(chmap, 0, sizeof(chmap));
2465 for (i = 0; i < ARRAY_SIZE(chmap); i++)
2466 chmap[i] = ucontrol->value.integer.value[i];
2467 if (!memcmp(chmap, per_pin->chmap, sizeof(chmap)))
2469 ca = hdmi_manual_channel_allocation(ARRAY_SIZE(chmap), chmap);
2472 if (spec->ops.chmap_validate) {
2473 err = spec->ops.chmap_validate(ca, ARRAY_SIZE(chmap), chmap);
2477 mutex_lock(&per_pin->lock);
2478 per_pin->chmap_set = true;
2479 memcpy(per_pin->chmap, chmap, sizeof(chmap));
2481 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
2482 mutex_unlock(&per_pin->lock);
2487 static int generic_hdmi_build_pcms(struct hda_codec *codec)
2489 struct hdmi_spec *spec = codec->spec;
2492 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2493 struct hda_pcm *info;
2494 struct hda_pcm_stream *pstr;
2496 info = snd_hda_codec_pcm_new(codec, "HDMI %d", pin_idx);
2500 spec->pcm_rec[pin_idx].pcm = info;
2502 info->pcm_type = HDA_PCM_TYPE_HDMI;
2503 info->own_chmap = true;
2505 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
2506 pstr->substreams = 1;
2507 pstr->ops = generic_ops;
2508 /* other pstr fields are set in open */
2514 static void free_hdmi_jack_priv(struct snd_jack *jack)
2516 struct hdmi_pcm *pcm = jack->private_data;
2521 static int add_hdmi_jack_kctl(struct hda_codec *codec,
2522 struct hdmi_spec *spec,
2526 struct snd_jack *jack;
2529 err = snd_jack_new(codec->card, name, SND_JACK_AVOUT, &jack,
2534 spec->pcm_rec[pcm_idx].jack = jack;
2535 jack->private_data = &spec->pcm_rec[pcm_idx];
2536 jack->private_free = free_hdmi_jack_priv;
2540 static int generic_hdmi_build_jack(struct hda_codec *codec, int pcm_idx)
2542 char hdmi_str[32] = "HDMI/DP";
2543 struct hdmi_spec *spec = codec->spec;
2544 struct hdmi_spec_per_pin *per_pin;
2545 struct hda_jack_tbl *jack;
2546 int pcmdev = get_pcm_rec(spec, pcm_idx)->device;
2551 sprintf(hdmi_str + strlen(hdmi_str), ",pcm=%d", pcmdev);
2553 if (spec->dyn_pcm_assign)
2554 return add_hdmi_jack_kctl(codec, spec, pcm_idx, hdmi_str);
2556 /* for !dyn_pcm_assign, we still use hda_jack for compatibility */
2557 /* if !dyn_pcm_assign, it must be non-MST mode.
2558 * This means pcms and pins are statically mapped.
2559 * And pcm_idx is pin_idx.
2561 per_pin = get_pin(spec, pcm_idx);
2562 phantom_jack = !is_jack_detectable(codec, per_pin->pin_nid);
2564 strncat(hdmi_str, " Phantom",
2565 sizeof(hdmi_str) - strlen(hdmi_str) - 1);
2566 ret = snd_hda_jack_add_kctl(codec, per_pin->pin_nid, hdmi_str,
2570 jack = snd_hda_jack_tbl_get(codec, per_pin->pin_nid);
2573 /* assign jack->jack to pcm_rec[].jack to
2574 * align with dyn_pcm_assign mode
2576 spec->pcm_rec[pcm_idx].jack = jack->jack;
2580 static int generic_hdmi_build_controls(struct hda_codec *codec)
2582 struct hdmi_spec *spec = codec->spec;
2584 int pin_idx, pcm_idx;
2587 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
2588 err = generic_hdmi_build_jack(codec, pcm_idx);
2592 /* create the spdif for each pcm
2593 * pin will be bound when monitor is connected
2595 if (spec->dyn_pcm_assign)
2596 err = snd_hda_create_dig_out_ctls(codec,
2597 0, spec->cvt_nids[0],
2600 struct hdmi_spec_per_pin *per_pin =
2601 get_pin(spec, pcm_idx);
2602 err = snd_hda_create_dig_out_ctls(codec,
2604 per_pin->mux_nids[0],
2609 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
2612 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2613 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2615 /* add control for ELD Bytes */
2616 err = hdmi_create_eld_ctl(codec, pin_idx,
2617 get_pcm_rec(spec, pin_idx)->device);
2622 hdmi_present_sense(per_pin, 0);
2625 /* add channel maps */
2626 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
2627 struct hda_pcm *pcm;
2628 struct snd_pcm_chmap *chmap;
2629 struct snd_kcontrol *kctl;
2632 pcm = get_pcm_rec(spec, pcm_idx);
2633 if (!pcm || !pcm->pcm)
2635 err = snd_pcm_add_chmap_ctls(pcm->pcm,
2636 SNDRV_PCM_STREAM_PLAYBACK,
2637 NULL, 0, pcm_idx, &chmap);
2640 /* override handlers */
2641 chmap->private_data = codec;
2643 for (i = 0; i < kctl->count; i++)
2644 kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_WRITE;
2645 kctl->info = hdmi_chmap_ctl_info;
2646 kctl->get = hdmi_chmap_ctl_get;
2647 kctl->put = hdmi_chmap_ctl_put;
2648 kctl->tlv.c = hdmi_chmap_ctl_tlv;
2654 static int generic_hdmi_init_per_pins(struct hda_codec *codec)
2656 struct hdmi_spec *spec = codec->spec;
2659 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2660 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2662 per_pin->codec = codec;
2663 mutex_init(&per_pin->lock);
2664 INIT_DELAYED_WORK(&per_pin->work, hdmi_repoll_eld);
2665 eld_proc_new(per_pin, pin_idx);
2670 static int generic_hdmi_init(struct hda_codec *codec)
2672 struct hdmi_spec *spec = codec->spec;
2675 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2676 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2677 hda_nid_t pin_nid = per_pin->pin_nid;
2679 hdmi_init_pin(codec, pin_nid);
2680 if (!codec_has_acomp(codec))
2681 snd_hda_jack_detect_enable_callback(codec, pin_nid,
2682 codec->jackpoll_interval > 0 ?
2683 jack_callback : NULL);
2688 static void hdmi_array_init(struct hdmi_spec *spec, int nums)
2690 snd_array_init(&spec->pins, sizeof(struct hdmi_spec_per_pin), nums);
2691 snd_array_init(&spec->cvts, sizeof(struct hdmi_spec_per_cvt), nums);
2694 static void hdmi_array_free(struct hdmi_spec *spec)
2696 snd_array_free(&spec->pins);
2697 snd_array_free(&spec->cvts);
2700 static void generic_hdmi_free(struct hda_codec *codec)
2702 struct hdmi_spec *spec = codec->spec;
2703 int pin_idx, pcm_idx;
2705 if (codec_has_acomp(codec))
2706 snd_hdac_i915_register_notifier(NULL);
2708 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2709 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2710 cancel_delayed_work_sync(&per_pin->work);
2711 eld_proc_free(per_pin);
2714 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
2715 if (spec->pcm_rec[pcm_idx].jack == NULL)
2717 if (spec->dyn_pcm_assign)
2718 snd_device_free(codec->card,
2719 spec->pcm_rec[pcm_idx].jack);
2721 spec->pcm_rec[pcm_idx].jack = NULL;
2724 if (spec->i915_bound)
2725 snd_hdac_i915_exit(&codec->bus->core);
2726 hdmi_array_free(spec);
2731 static int generic_hdmi_resume(struct hda_codec *codec)
2733 struct hdmi_spec *spec = codec->spec;
2736 codec->patch_ops.init(codec);
2737 regcache_sync(codec->core.regmap);
2739 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2740 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2741 hdmi_present_sense(per_pin, 1);
2747 static const struct hda_codec_ops generic_hdmi_patch_ops = {
2748 .init = generic_hdmi_init,
2749 .free = generic_hdmi_free,
2750 .build_pcms = generic_hdmi_build_pcms,
2751 .build_controls = generic_hdmi_build_controls,
2752 .unsol_event = hdmi_unsol_event,
2754 .resume = generic_hdmi_resume,
2758 static const struct hdmi_ops generic_standard_hdmi_ops = {
2759 .pin_get_eld = snd_hdmi_get_eld,
2760 .pin_get_slot_channel = hdmi_pin_get_slot_channel,
2761 .pin_set_slot_channel = hdmi_pin_set_slot_channel,
2762 .pin_setup_infoframe = hdmi_pin_setup_infoframe,
2763 .pin_hbr_setup = hdmi_pin_hbr_setup,
2764 .setup_stream = hdmi_setup_stream,
2765 .chmap_cea_alloc_validate_get_type = hdmi_chmap_cea_alloc_validate_get_type,
2766 .cea_alloc_to_tlv_chmap = hdmi_cea_alloc_to_tlv_chmap,
2770 static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
2773 struct hdmi_spec *spec = codec->spec;
2777 nconns = snd_hda_get_connections(codec, nid, conns, ARRAY_SIZE(conns));
2778 if (nconns == spec->num_cvts &&
2779 !memcmp(conns, spec->cvt_nids, spec->num_cvts * sizeof(hda_nid_t)))
2782 /* override pins connection list */
2783 codec_dbg(codec, "hdmi: haswell: override pin connection 0x%x\n", nid);
2784 snd_hda_override_conn_list(codec, nid, spec->num_cvts, spec->cvt_nids);
2787 #define INTEL_VENDOR_NID 0x08
2788 #define INTEL_GET_VENDOR_VERB 0xf81
2789 #define INTEL_SET_VENDOR_VERB 0x781
2790 #define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
2791 #define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
2793 static void intel_haswell_enable_all_pins(struct hda_codec *codec,
2796 unsigned int vendor_param;
2798 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
2799 INTEL_GET_VENDOR_VERB, 0);
2800 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
2803 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
2804 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
2805 INTEL_SET_VENDOR_VERB, vendor_param);
2806 if (vendor_param == -1)
2810 snd_hda_codec_update_widgets(codec);
2813 static void intel_haswell_fixup_enable_dp12(struct hda_codec *codec)
2815 unsigned int vendor_param;
2817 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
2818 INTEL_GET_VENDOR_VERB, 0);
2819 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
2822 /* enable DP1.2 mode */
2823 vendor_param |= INTEL_EN_DP12;
2824 snd_hdac_regmap_add_vendor_verb(&codec->core, INTEL_SET_VENDOR_VERB);
2825 snd_hda_codec_write_cache(codec, INTEL_VENDOR_NID, 0,
2826 INTEL_SET_VENDOR_VERB, vendor_param);
2829 /* Haswell needs to re-issue the vendor-specific verbs before turning to D0.
2830 * Otherwise you may get severe h/w communication errors.
2832 static void haswell_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2833 unsigned int power_state)
2835 if (power_state == AC_PWRST_D0) {
2836 intel_haswell_enable_all_pins(codec, false);
2837 intel_haswell_fixup_enable_dp12(codec);
2840 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE, power_state);
2841 snd_hda_codec_set_power_to_all(codec, fg, power_state);
2844 static void intel_pin_eld_notify(void *audio_ptr, int port)
2846 struct hda_codec *codec = audio_ptr;
2847 int pin_nid = port + 0x04;
2849 /* skip notification during system suspend (but not in runtime PM);
2850 * the state will be updated at resume
2852 if (snd_power_get_state(codec->card) != SNDRV_CTL_POWER_D0)
2854 /* ditto during suspend/resume process itself */
2855 if (atomic_read(&(codec)->core.in_pm))
2858 check_presence_and_report(codec, pin_nid);
2861 static int patch_generic_hdmi(struct hda_codec *codec)
2863 struct hdmi_spec *spec;
2865 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2869 spec->ops = generic_standard_hdmi_ops;
2870 mutex_init(&spec->pcm_lock);
2872 hdmi_array_init(spec, 4);
2874 /* Try to bind with i915 for any Intel codecs (if not done yet) */
2875 if (!codec_has_acomp(codec) &&
2876 (codec->core.vendor_id >> 16) == 0x8086)
2877 if (!snd_hdac_i915_init(&codec->bus->core))
2878 spec->i915_bound = true;
2880 if (is_haswell_plus(codec)) {
2881 intel_haswell_enable_all_pins(codec, true);
2882 intel_haswell_fixup_enable_dp12(codec);
2885 /* For Valleyview/Cherryview, only the display codec is in the display
2886 * power well and can use link_power ops to request/release the power.
2887 * For Haswell/Broadwell, the controller is also in the power well and
2888 * can cover the codec power request, and so need not set this flag.
2889 * For previous platforms, there is no such power well feature.
2891 if (is_valleyview_plus(codec) || is_skylake(codec) ||
2893 codec->core.link_power_control = 1;
2895 if (codec_has_acomp(codec)) {
2896 codec->depop_delay = 0;
2897 spec->i915_audio_ops.audio_ptr = codec;
2898 spec->i915_audio_ops.pin_eld_notify = intel_pin_eld_notify;
2899 snd_hdac_i915_register_notifier(&spec->i915_audio_ops);
2902 if (hdmi_parse_codec(codec) < 0) {
2903 if (spec->i915_bound)
2904 snd_hdac_i915_exit(&codec->bus->core);
2909 codec->patch_ops = generic_hdmi_patch_ops;
2910 if (is_haswell_plus(codec)) {
2911 codec->patch_ops.set_power_state = haswell_set_power_state;
2912 codec->dp_mst = true;
2915 /* Enable runtime pm for HDMI audio codec of HSW/BDW/SKL/BYT/BSW */
2916 if (is_haswell_plus(codec) || is_valleyview_plus(codec))
2917 codec->auto_runtime_pm = 1;
2919 generic_hdmi_init_per_pins(codec);
2921 init_channel_allocations();
2923 WARN_ON(spec->dyn_pcm_assign && !codec_has_acomp(codec));
2928 * Shared non-generic implementations
2931 static int simple_playback_build_pcms(struct hda_codec *codec)
2933 struct hdmi_spec *spec = codec->spec;
2934 struct hda_pcm *info;
2936 struct hda_pcm_stream *pstr;
2937 struct hdmi_spec_per_cvt *per_cvt;
2939 per_cvt = get_cvt(spec, 0);
2940 chans = get_wcaps(codec, per_cvt->cvt_nid);
2941 chans = get_wcaps_channels(chans);
2943 info = snd_hda_codec_pcm_new(codec, "HDMI 0");
2946 spec->pcm_rec[0].pcm = info;
2947 info->pcm_type = HDA_PCM_TYPE_HDMI;
2948 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
2949 *pstr = spec->pcm_playback;
2950 pstr->nid = per_cvt->cvt_nid;
2951 if (pstr->channels_max <= 2 && chans && chans <= 16)
2952 pstr->channels_max = chans;
2957 /* unsolicited event for jack sensing */
2958 static void simple_hdmi_unsol_event(struct hda_codec *codec,
2961 snd_hda_jack_set_dirty_all(codec);
2962 snd_hda_jack_report_sync(codec);
2965 /* generic_hdmi_build_jack can be used for simple_hdmi, too,
2966 * as long as spec->pins[] is set correctly
2968 #define simple_hdmi_build_jack generic_hdmi_build_jack
2970 static int simple_playback_build_controls(struct hda_codec *codec)
2972 struct hdmi_spec *spec = codec->spec;
2973 struct hdmi_spec_per_cvt *per_cvt;
2976 per_cvt = get_cvt(spec, 0);
2977 err = snd_hda_create_dig_out_ctls(codec, per_cvt->cvt_nid,
2982 return simple_hdmi_build_jack(codec, 0);
2985 static int simple_playback_init(struct hda_codec *codec)
2987 struct hdmi_spec *spec = codec->spec;
2988 struct hdmi_spec_per_pin *per_pin = get_pin(spec, 0);
2989 hda_nid_t pin = per_pin->pin_nid;
2991 snd_hda_codec_write(codec, pin, 0,
2992 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
2993 /* some codecs require to unmute the pin */
2994 if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
2995 snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
2997 snd_hda_jack_detect_enable(codec, pin);
3001 static void simple_playback_free(struct hda_codec *codec)
3003 struct hdmi_spec *spec = codec->spec;
3005 hdmi_array_free(spec);
3010 * Nvidia specific implementations
3013 #define Nv_VERB_SET_Channel_Allocation 0xF79
3014 #define Nv_VERB_SET_Info_Frame_Checksum 0xF7A
3015 #define Nv_VERB_SET_Audio_Protection_On 0xF98
3016 #define Nv_VERB_SET_Audio_Protection_Off 0xF99
3018 #define nvhdmi_master_con_nid_7x 0x04
3019 #define nvhdmi_master_pin_nid_7x 0x05
3021 static const hda_nid_t nvhdmi_con_nids_7x[4] = {
3022 /*front, rear, clfe, rear_surr */
3026 static const struct hda_verb nvhdmi_basic_init_7x_2ch[] = {
3027 /* set audio protect on */
3028 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
3029 /* enable digital output on pin widget */
3030 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3034 static const struct hda_verb nvhdmi_basic_init_7x_8ch[] = {
3035 /* set audio protect on */
3036 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
3037 /* enable digital output on pin widget */
3038 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3039 { 0x7, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3040 { 0x9, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3041 { 0xb, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3042 { 0xd, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3046 #ifdef LIMITED_RATE_FMT_SUPPORT
3047 /* support only the safe format and rate */
3048 #define SUPPORTED_RATES SNDRV_PCM_RATE_48000
3049 #define SUPPORTED_MAXBPS 16
3050 #define SUPPORTED_FORMATS SNDRV_PCM_FMTBIT_S16_LE
3052 /* support all rates and formats */
3053 #define SUPPORTED_RATES \
3054 (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
3055 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
3056 SNDRV_PCM_RATE_192000)
3057 #define SUPPORTED_MAXBPS 24
3058 #define SUPPORTED_FORMATS \
3059 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
3062 static int nvhdmi_7x_init_2ch(struct hda_codec *codec)
3064 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_2ch);
3068 static int nvhdmi_7x_init_8ch(struct hda_codec *codec)
3070 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_8ch);
3074 static unsigned int channels_2_6_8[] = {
3078 static unsigned int channels_2_8[] = {
3082 static struct snd_pcm_hw_constraint_list hw_constraints_2_6_8_channels = {
3083 .count = ARRAY_SIZE(channels_2_6_8),
3084 .list = channels_2_6_8,
3088 static struct snd_pcm_hw_constraint_list hw_constraints_2_8_channels = {
3089 .count = ARRAY_SIZE(channels_2_8),
3090 .list = channels_2_8,
3094 static int simple_playback_pcm_open(struct hda_pcm_stream *hinfo,
3095 struct hda_codec *codec,
3096 struct snd_pcm_substream *substream)
3098 struct hdmi_spec *spec = codec->spec;
3099 struct snd_pcm_hw_constraint_list *hw_constraints_channels = NULL;
3101 switch (codec->preset->vendor_id) {
3106 hw_constraints_channels = &hw_constraints_2_8_channels;
3109 hw_constraints_channels = &hw_constraints_2_6_8_channels;
3115 if (hw_constraints_channels != NULL) {
3116 snd_pcm_hw_constraint_list(substream->runtime, 0,
3117 SNDRV_PCM_HW_PARAM_CHANNELS,
3118 hw_constraints_channels);
3120 snd_pcm_hw_constraint_step(substream->runtime, 0,
3121 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
3124 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3127 static int simple_playback_pcm_close(struct hda_pcm_stream *hinfo,
3128 struct hda_codec *codec,
3129 struct snd_pcm_substream *substream)
3131 struct hdmi_spec *spec = codec->spec;
3132 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3135 static int simple_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3136 struct hda_codec *codec,
3137 unsigned int stream_tag,
3138 unsigned int format,
3139 struct snd_pcm_substream *substream)
3141 struct hdmi_spec *spec = codec->spec;
3142 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3143 stream_tag, format, substream);
3146 static const struct hda_pcm_stream simple_pcm_playback = {
3151 .open = simple_playback_pcm_open,
3152 .close = simple_playback_pcm_close,
3153 .prepare = simple_playback_pcm_prepare
3157 static const struct hda_codec_ops simple_hdmi_patch_ops = {
3158 .build_controls = simple_playback_build_controls,
3159 .build_pcms = simple_playback_build_pcms,
3160 .init = simple_playback_init,
3161 .free = simple_playback_free,
3162 .unsol_event = simple_hdmi_unsol_event,
3165 static int patch_simple_hdmi(struct hda_codec *codec,
3166 hda_nid_t cvt_nid, hda_nid_t pin_nid)
3168 struct hdmi_spec *spec;
3169 struct hdmi_spec_per_cvt *per_cvt;
3170 struct hdmi_spec_per_pin *per_pin;
3172 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
3177 hdmi_array_init(spec, 1);
3179 spec->multiout.num_dacs = 0; /* no analog */
3180 spec->multiout.max_channels = 2;
3181 spec->multiout.dig_out_nid = cvt_nid;
3184 per_pin = snd_array_new(&spec->pins);
3185 per_cvt = snd_array_new(&spec->cvts);
3186 if (!per_pin || !per_cvt) {
3187 simple_playback_free(codec);
3190 per_cvt->cvt_nid = cvt_nid;
3191 per_pin->pin_nid = pin_nid;
3192 spec->pcm_playback = simple_pcm_playback;
3194 codec->patch_ops = simple_hdmi_patch_ops;
3199 static void nvhdmi_8ch_7x_set_info_frame_parameters(struct hda_codec *codec,
3202 unsigned int chanmask;
3203 int chan = channels ? (channels - 1) : 1;
3222 /* Set the audio infoframe channel allocation and checksum fields. The
3223 * channel count is computed implicitly by the hardware. */
3224 snd_hda_codec_write(codec, 0x1, 0,
3225 Nv_VERB_SET_Channel_Allocation, chanmask);
3227 snd_hda_codec_write(codec, 0x1, 0,
3228 Nv_VERB_SET_Info_Frame_Checksum,
3229 (0x71 - chan - chanmask));
3232 static int nvhdmi_8ch_7x_pcm_close(struct hda_pcm_stream *hinfo,
3233 struct hda_codec *codec,
3234 struct snd_pcm_substream *substream)
3236 struct hdmi_spec *spec = codec->spec;
3239 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x,
3240 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
3241 for (i = 0; i < 4; i++) {
3242 /* set the stream id */
3243 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
3244 AC_VERB_SET_CHANNEL_STREAMID, 0);
3245 /* set the stream format */
3246 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
3247 AC_VERB_SET_STREAM_FORMAT, 0);
3250 /* The audio hardware sends a channel count of 0x7 (8ch) when all the
3251 * streams are disabled. */
3252 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
3254 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3257 static int nvhdmi_8ch_7x_pcm_prepare(struct hda_pcm_stream *hinfo,
3258 struct hda_codec *codec,
3259 unsigned int stream_tag,
3260 unsigned int format,
3261 struct snd_pcm_substream *substream)
3264 unsigned int dataDCC2, channel_id;
3266 struct hdmi_spec *spec = codec->spec;
3267 struct hda_spdif_out *spdif;
3268 struct hdmi_spec_per_cvt *per_cvt;
3270 mutex_lock(&codec->spdif_mutex);
3271 per_cvt = get_cvt(spec, 0);
3272 spdif = snd_hda_spdif_out_of_nid(codec, per_cvt->cvt_nid);
3274 chs = substream->runtime->channels;
3278 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
3279 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
3280 snd_hda_codec_write(codec,
3281 nvhdmi_master_con_nid_7x,
3283 AC_VERB_SET_DIGI_CONVERT_1,
3284 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
3286 /* set the stream id */
3287 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
3288 AC_VERB_SET_CHANNEL_STREAMID, (stream_tag << 4) | 0x0);
3290 /* set the stream format */
3291 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
3292 AC_VERB_SET_STREAM_FORMAT, format);
3294 /* turn on again (if needed) */
3295 /* enable and set the channel status audio/data flag */
3296 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE)) {
3297 snd_hda_codec_write(codec,
3298 nvhdmi_master_con_nid_7x,
3300 AC_VERB_SET_DIGI_CONVERT_1,
3301 spdif->ctls & 0xff);
3302 snd_hda_codec_write(codec,
3303 nvhdmi_master_con_nid_7x,
3305 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
3308 for (i = 0; i < 4; i++) {
3314 /* turn off SPDIF once;
3315 *otherwise the IEC958 bits won't be updated
3317 if (codec->spdif_status_reset &&
3318 (spdif->ctls & AC_DIG1_ENABLE))
3319 snd_hda_codec_write(codec,
3320 nvhdmi_con_nids_7x[i],
3322 AC_VERB_SET_DIGI_CONVERT_1,
3323 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
3324 /* set the stream id */
3325 snd_hda_codec_write(codec,
3326 nvhdmi_con_nids_7x[i],
3328 AC_VERB_SET_CHANNEL_STREAMID,
3329 (stream_tag << 4) | channel_id);
3330 /* set the stream format */
3331 snd_hda_codec_write(codec,
3332 nvhdmi_con_nids_7x[i],
3334 AC_VERB_SET_STREAM_FORMAT,
3336 /* turn on again (if needed) */
3337 /* enable and set the channel status audio/data flag */
3338 if (codec->spdif_status_reset &&
3339 (spdif->ctls & AC_DIG1_ENABLE)) {
3340 snd_hda_codec_write(codec,
3341 nvhdmi_con_nids_7x[i],
3343 AC_VERB_SET_DIGI_CONVERT_1,
3344 spdif->ctls & 0xff);
3345 snd_hda_codec_write(codec,
3346 nvhdmi_con_nids_7x[i],
3348 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
3352 nvhdmi_8ch_7x_set_info_frame_parameters(codec, chs);
3354 mutex_unlock(&codec->spdif_mutex);
3358 static const struct hda_pcm_stream nvhdmi_pcm_playback_8ch_7x = {
3362 .nid = nvhdmi_master_con_nid_7x,
3363 .rates = SUPPORTED_RATES,
3364 .maxbps = SUPPORTED_MAXBPS,
3365 .formats = SUPPORTED_FORMATS,
3367 .open = simple_playback_pcm_open,
3368 .close = nvhdmi_8ch_7x_pcm_close,
3369 .prepare = nvhdmi_8ch_7x_pcm_prepare
3373 static int patch_nvhdmi_2ch(struct hda_codec *codec)
3375 struct hdmi_spec *spec;
3376 int err = patch_simple_hdmi(codec, nvhdmi_master_con_nid_7x,
3377 nvhdmi_master_pin_nid_7x);
3381 codec->patch_ops.init = nvhdmi_7x_init_2ch;
3382 /* override the PCM rates, etc, as the codec doesn't give full list */
3384 spec->pcm_playback.rates = SUPPORTED_RATES;
3385 spec->pcm_playback.maxbps = SUPPORTED_MAXBPS;
3386 spec->pcm_playback.formats = SUPPORTED_FORMATS;
3390 static int nvhdmi_7x_8ch_build_pcms(struct hda_codec *codec)
3392 struct hdmi_spec *spec = codec->spec;
3393 int err = simple_playback_build_pcms(codec);
3395 struct hda_pcm *info = get_pcm_rec(spec, 0);
3396 info->own_chmap = true;
3401 static int nvhdmi_7x_8ch_build_controls(struct hda_codec *codec)
3403 struct hdmi_spec *spec = codec->spec;
3404 struct hda_pcm *info;
3405 struct snd_pcm_chmap *chmap;
3408 err = simple_playback_build_controls(codec);
3412 /* add channel maps */
3413 info = get_pcm_rec(spec, 0);
3414 err = snd_pcm_add_chmap_ctls(info->pcm,
3415 SNDRV_PCM_STREAM_PLAYBACK,
3416 snd_pcm_alt_chmaps, 8, 0, &chmap);
3419 switch (codec->preset->vendor_id) {
3424 chmap->channel_mask = (1U << 2) | (1U << 8);
3427 chmap->channel_mask = (1U << 2) | (1U << 6) | (1U << 8);
3432 static int patch_nvhdmi_8ch_7x(struct hda_codec *codec)
3434 struct hdmi_spec *spec;
3435 int err = patch_nvhdmi_2ch(codec);
3439 spec->multiout.max_channels = 8;
3440 spec->pcm_playback = nvhdmi_pcm_playback_8ch_7x;
3441 codec->patch_ops.init = nvhdmi_7x_init_8ch;
3442 codec->patch_ops.build_pcms = nvhdmi_7x_8ch_build_pcms;
3443 codec->patch_ops.build_controls = nvhdmi_7x_8ch_build_controls;
3445 /* Initialize the audio infoframe channel mask and checksum to something
3447 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
3453 * NVIDIA codecs ignore ASP mapping for 2ch - confirmed on:
3457 static int nvhdmi_chmap_cea_alloc_validate_get_type(struct cea_channel_speaker_allocation *cap,
3460 if (cap->ca_index == 0x00 && channels == 2)
3461 return SNDRV_CTL_TLVT_CHMAP_FIXED;
3463 return hdmi_chmap_cea_alloc_validate_get_type(cap, channels);
3466 static int nvhdmi_chmap_validate(int ca, int chs, unsigned char *map)
3468 if (ca == 0x00 && (map[0] != SNDRV_CHMAP_FL || map[1] != SNDRV_CHMAP_FR))
3474 static int patch_nvhdmi(struct hda_codec *codec)
3476 struct hdmi_spec *spec;
3479 err = patch_generic_hdmi(codec);
3484 spec->dyn_pin_out = true;
3486 spec->ops.chmap_cea_alloc_validate_get_type =
3487 nvhdmi_chmap_cea_alloc_validate_get_type;
3488 spec->ops.chmap_validate = nvhdmi_chmap_validate;
3494 * The HDA codec on NVIDIA Tegra contains two scratch registers that are
3495 * accessed using vendor-defined verbs. These registers can be used for
3496 * interoperability between the HDA and HDMI drivers.
3499 /* Audio Function Group node */
3500 #define NVIDIA_AFG_NID 0x01
3503 * The SCRATCH0 register is used to notify the HDMI codec of changes in audio
3504 * format. On Tegra, bit 31 is used as a trigger that causes an interrupt to
3505 * be raised in the HDMI codec. The remainder of the bits is arbitrary. This
3506 * implementation stores the HDA format (see AC_FMT_*) in bits [15:0] and an
3507 * additional bit (at position 30) to signal the validity of the format.
3509 * | 31 | 30 | 29 16 | 15 0 |
3510 * +---------+-------+--------+--------+
3511 * | TRIGGER | VALID | UNUSED | FORMAT |
3512 * +-----------------------------------|
3514 * Note that for the trigger bit to take effect it needs to change value
3515 * (i.e. it needs to be toggled).
3517 #define NVIDIA_GET_SCRATCH0 0xfa6
3518 #define NVIDIA_SET_SCRATCH0_BYTE0 0xfa7
3519 #define NVIDIA_SET_SCRATCH0_BYTE1 0xfa8
3520 #define NVIDIA_SET_SCRATCH0_BYTE2 0xfa9
3521 #define NVIDIA_SET_SCRATCH0_BYTE3 0xfaa
3522 #define NVIDIA_SCRATCH_TRIGGER (1 << 7)
3523 #define NVIDIA_SCRATCH_VALID (1 << 6)
3525 #define NVIDIA_GET_SCRATCH1 0xfab
3526 #define NVIDIA_SET_SCRATCH1_BYTE0 0xfac
3527 #define NVIDIA_SET_SCRATCH1_BYTE1 0xfad
3528 #define NVIDIA_SET_SCRATCH1_BYTE2 0xfae
3529 #define NVIDIA_SET_SCRATCH1_BYTE3 0xfaf
3532 * The format parameter is the HDA audio format (see AC_FMT_*). If set to 0,
3533 * the format is invalidated so that the HDMI codec can be disabled.
3535 static void tegra_hdmi_set_format(struct hda_codec *codec, unsigned int format)
3539 /* bits [31:30] contain the trigger and valid bits */
3540 value = snd_hda_codec_read(codec, NVIDIA_AFG_NID, 0,
3541 NVIDIA_GET_SCRATCH0, 0);
3542 value = (value >> 24) & 0xff;
3544 /* bits [15:0] are used to store the HDA format */
3545 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3546 NVIDIA_SET_SCRATCH0_BYTE0,
3547 (format >> 0) & 0xff);
3548 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3549 NVIDIA_SET_SCRATCH0_BYTE1,
3550 (format >> 8) & 0xff);
3552 /* bits [16:24] are unused */
3553 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3554 NVIDIA_SET_SCRATCH0_BYTE2, 0);
3557 * Bit 30 signals that the data is valid and hence that HDMI audio can
3561 value &= ~NVIDIA_SCRATCH_VALID;
3563 value |= NVIDIA_SCRATCH_VALID;
3566 * Whenever the trigger bit is toggled, an interrupt is raised in the
3567 * HDMI codec. The HDMI driver will use that as trigger to update its
3570 value ^= NVIDIA_SCRATCH_TRIGGER;
3572 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3573 NVIDIA_SET_SCRATCH0_BYTE3, value);
3576 static int tegra_hdmi_pcm_prepare(struct hda_pcm_stream *hinfo,
3577 struct hda_codec *codec,
3578 unsigned int stream_tag,
3579 unsigned int format,
3580 struct snd_pcm_substream *substream)
3584 err = generic_hdmi_playback_pcm_prepare(hinfo, codec, stream_tag,
3589 /* notify the HDMI codec of the format change */
3590 tegra_hdmi_set_format(codec, format);
3595 static int tegra_hdmi_pcm_cleanup(struct hda_pcm_stream *hinfo,
3596 struct hda_codec *codec,
3597 struct snd_pcm_substream *substream)
3599 /* invalidate the format in the HDMI codec */
3600 tegra_hdmi_set_format(codec, 0);
3602 return generic_hdmi_playback_pcm_cleanup(hinfo, codec, substream);
3605 static struct hda_pcm *hda_find_pcm_by_type(struct hda_codec *codec, int type)
3607 struct hdmi_spec *spec = codec->spec;
3610 for (i = 0; i < spec->num_pins; i++) {
3611 struct hda_pcm *pcm = get_pcm_rec(spec, i);
3613 if (pcm->pcm_type == type)
3620 static int tegra_hdmi_build_pcms(struct hda_codec *codec)
3622 struct hda_pcm_stream *stream;
3623 struct hda_pcm *pcm;
3626 err = generic_hdmi_build_pcms(codec);
3630 pcm = hda_find_pcm_by_type(codec, HDA_PCM_TYPE_HDMI);
3635 * Override ->prepare() and ->cleanup() operations to notify the HDMI
3636 * codec about format changes.
3638 stream = &pcm->stream[SNDRV_PCM_STREAM_PLAYBACK];
3639 stream->ops.prepare = tegra_hdmi_pcm_prepare;
3640 stream->ops.cleanup = tegra_hdmi_pcm_cleanup;
3645 static int patch_tegra_hdmi(struct hda_codec *codec)
3649 err = patch_generic_hdmi(codec);
3653 codec->patch_ops.build_pcms = tegra_hdmi_build_pcms;
3659 * ATI/AMD-specific implementations
3662 #define is_amdhdmi_rev3_or_later(codec) \
3663 ((codec)->core.vendor_id == 0x1002aa01 && \
3664 ((codec)->core.revision_id & 0xff00) >= 0x0300)
3665 #define has_amd_full_remap_support(codec) is_amdhdmi_rev3_or_later(codec)
3667 /* ATI/AMD specific HDA pin verbs, see the AMD HDA Verbs specification */
3668 #define ATI_VERB_SET_CHANNEL_ALLOCATION 0x771
3669 #define ATI_VERB_SET_DOWNMIX_INFO 0x772
3670 #define ATI_VERB_SET_MULTICHANNEL_01 0x777
3671 #define ATI_VERB_SET_MULTICHANNEL_23 0x778
3672 #define ATI_VERB_SET_MULTICHANNEL_45 0x779
3673 #define ATI_VERB_SET_MULTICHANNEL_67 0x77a
3674 #define ATI_VERB_SET_HBR_CONTROL 0x77c
3675 #define ATI_VERB_SET_MULTICHANNEL_1 0x785
3676 #define ATI_VERB_SET_MULTICHANNEL_3 0x786
3677 #define ATI_VERB_SET_MULTICHANNEL_5 0x787
3678 #define ATI_VERB_SET_MULTICHANNEL_7 0x788
3679 #define ATI_VERB_SET_MULTICHANNEL_MODE 0x789
3680 #define ATI_VERB_GET_CHANNEL_ALLOCATION 0xf71
3681 #define ATI_VERB_GET_DOWNMIX_INFO 0xf72
3682 #define ATI_VERB_GET_MULTICHANNEL_01 0xf77
3683 #define ATI_VERB_GET_MULTICHANNEL_23 0xf78
3684 #define ATI_VERB_GET_MULTICHANNEL_45 0xf79
3685 #define ATI_VERB_GET_MULTICHANNEL_67 0xf7a
3686 #define ATI_VERB_GET_HBR_CONTROL 0xf7c
3687 #define ATI_VERB_GET_MULTICHANNEL_1 0xf85
3688 #define ATI_VERB_GET_MULTICHANNEL_3 0xf86
3689 #define ATI_VERB_GET_MULTICHANNEL_5 0xf87
3690 #define ATI_VERB_GET_MULTICHANNEL_7 0xf88
3691 #define ATI_VERB_GET_MULTICHANNEL_MODE 0xf89
3693 /* AMD specific HDA cvt verbs */
3694 #define ATI_VERB_SET_RAMP_RATE 0x770
3695 #define ATI_VERB_GET_RAMP_RATE 0xf70
3697 #define ATI_OUT_ENABLE 0x1
3699 #define ATI_MULTICHANNEL_MODE_PAIRED 0
3700 #define ATI_MULTICHANNEL_MODE_SINGLE 1
3702 #define ATI_HBR_CAPABLE 0x01
3703 #define ATI_HBR_ENABLE 0x10
3705 static int atihdmi_pin_get_eld(struct hda_codec *codec, hda_nid_t nid,
3706 unsigned char *buf, int *eld_size)
3708 /* call hda_eld.c ATI/AMD-specific function */
3709 return snd_hdmi_get_eld_ati(codec, nid, buf, eld_size,
3710 is_amdhdmi_rev3_or_later(codec));
3713 static void atihdmi_pin_setup_infoframe(struct hda_codec *codec, hda_nid_t pin_nid, int ca,
3714 int active_channels, int conn_type)
3716 snd_hda_codec_write(codec, pin_nid, 0, ATI_VERB_SET_CHANNEL_ALLOCATION, ca);
3719 static int atihdmi_paired_swap_fc_lfe(int pos)
3722 * ATI/AMD have automatic FC/LFE swap built-in
3723 * when in pairwise mapping mode.
3727 /* see channel_allocations[].speakers[] */
3736 static int atihdmi_paired_chmap_validate(int ca, int chs, unsigned char *map)
3738 struct cea_channel_speaker_allocation *cap;
3741 /* check that only channel pairs need to be remapped on old pre-rev3 ATI/AMD */
3743 cap = &channel_allocations[get_channel_allocation_order(ca)];
3744 for (i = 0; i < chs; ++i) {
3745 int mask = to_spk_mask(map[i]);
3747 bool companion_ok = false;
3752 for (j = 0 + i % 2; j < 8; j += 2) {
3753 int chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j);
3754 if (cap->speakers[chan_idx] == mask) {
3755 /* channel is in a supported position */
3758 if (i % 2 == 0 && i + 1 < chs) {
3759 /* even channel, check the odd companion */
3760 int comp_chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j + 1);
3761 int comp_mask_req = to_spk_mask(map[i+1]);
3762 int comp_mask_act = cap->speakers[comp_chan_idx];
3764 if (comp_mask_req == comp_mask_act)
3765 companion_ok = true;
3777 i++; /* companion channel already checked */
3783 static int atihdmi_pin_set_slot_channel(struct hda_codec *codec, hda_nid_t pin_nid,
3784 int hdmi_slot, int stream_channel)
3787 int ati_channel_setup = 0;
3792 if (!has_amd_full_remap_support(codec)) {
3793 hdmi_slot = atihdmi_paired_swap_fc_lfe(hdmi_slot);
3795 /* In case this is an odd slot but without stream channel, do not
3796 * disable the slot since the corresponding even slot could have a
3797 * channel. In case neither have a channel, the slot pair will be
3798 * disabled when this function is called for the even slot. */
3799 if (hdmi_slot % 2 != 0 && stream_channel == 0xf)
3802 hdmi_slot -= hdmi_slot % 2;
3804 if (stream_channel != 0xf)
3805 stream_channel -= stream_channel % 2;
3808 verb = ATI_VERB_SET_MULTICHANNEL_01 + hdmi_slot/2 + (hdmi_slot % 2) * 0x00e;
3810 /* ati_channel_setup format: [7..4] = stream_channel_id, [1] = mute, [0] = enable */
3812 if (stream_channel != 0xf)
3813 ati_channel_setup = (stream_channel << 4) | ATI_OUT_ENABLE;
3815 return snd_hda_codec_write(codec, pin_nid, 0, verb, ati_channel_setup);
3818 static int atihdmi_pin_get_slot_channel(struct hda_codec *codec, hda_nid_t pin_nid,
3821 bool was_odd = false;
3822 int ati_asp_slot = asp_slot;
3824 int ati_channel_setup;
3829 if (!has_amd_full_remap_support(codec)) {
3830 ati_asp_slot = atihdmi_paired_swap_fc_lfe(asp_slot);
3831 if (ati_asp_slot % 2 != 0) {
3837 verb = ATI_VERB_GET_MULTICHANNEL_01 + ati_asp_slot/2 + (ati_asp_slot % 2) * 0x00e;
3839 ati_channel_setup = snd_hda_codec_read(codec, pin_nid, 0, verb, 0);
3841 if (!(ati_channel_setup & ATI_OUT_ENABLE))
3844 return ((ati_channel_setup & 0xf0) >> 4) + !!was_odd;
3847 static int atihdmi_paired_chmap_cea_alloc_validate_get_type(struct cea_channel_speaker_allocation *cap,
3853 * Pre-rev3 ATI/AMD codecs operate in a paired channel mode, so
3854 * we need to take that into account (a single channel may take 2
3855 * channel slots if we need to carry a silent channel next to it).
3856 * On Rev3+ AMD codecs this function is not used.
3860 /* We only produce even-numbered channel count TLVs */
3861 if ((channels % 2) != 0)
3864 for (c = 0; c < 7; c += 2) {
3865 if (cap->speakers[c] || cap->speakers[c+1])
3869 if (chanpairs * 2 != channels)
3872 return SNDRV_CTL_TLVT_CHMAP_PAIRED;
3875 static void atihdmi_paired_cea_alloc_to_tlv_chmap(struct cea_channel_speaker_allocation *cap,
3876 unsigned int *chmap, int channels)
3878 /* produce paired maps for pre-rev3 ATI/AMD codecs */
3882 for (c = 7; c >= 0; c--) {
3883 int chan = 7 - atihdmi_paired_swap_fc_lfe(7 - c);
3884 int spk = cap->speakers[chan];
3886 /* add N/A channel if the companion channel is occupied */
3887 if (cap->speakers[chan + (chan % 2 ? -1 : 1)])
3888 chmap[count++] = SNDRV_CHMAP_NA;
3893 chmap[count++] = spk_to_chmap(spk);
3896 WARN_ON(count != channels);
3899 static int atihdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
3902 int hbr_ctl, hbr_ctl_new;
3904 hbr_ctl = snd_hda_codec_read(codec, pin_nid, 0, ATI_VERB_GET_HBR_CONTROL, 0);
3905 if (hbr_ctl >= 0 && (hbr_ctl & ATI_HBR_CAPABLE)) {
3907 hbr_ctl_new = hbr_ctl | ATI_HBR_ENABLE;
3909 hbr_ctl_new = hbr_ctl & ~ATI_HBR_ENABLE;
3912 "atihdmi_pin_hbr_setup: NID=0x%x, %shbr-ctl=0x%x\n",
3914 hbr_ctl == hbr_ctl_new ? "" : "new-",
3917 if (hbr_ctl != hbr_ctl_new)
3918 snd_hda_codec_write(codec, pin_nid, 0,
3919 ATI_VERB_SET_HBR_CONTROL,
3928 static int atihdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
3929 hda_nid_t pin_nid, u32 stream_tag, int format)
3932 if (is_amdhdmi_rev3_or_later(codec)) {
3933 int ramp_rate = 180; /* default as per AMD spec */
3934 /* disable ramp-up/down for non-pcm as per AMD spec */
3935 if (format & AC_FMT_TYPE_NON_PCM)
3938 snd_hda_codec_write(codec, cvt_nid, 0, ATI_VERB_SET_RAMP_RATE, ramp_rate);
3941 return hdmi_setup_stream(codec, cvt_nid, pin_nid, stream_tag, format);
3945 static int atihdmi_init(struct hda_codec *codec)
3947 struct hdmi_spec *spec = codec->spec;
3950 err = generic_hdmi_init(codec);
3955 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
3956 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
3958 /* make sure downmix information in infoframe is zero */
3959 snd_hda_codec_write(codec, per_pin->pin_nid, 0, ATI_VERB_SET_DOWNMIX_INFO, 0);
3961 /* enable channel-wise remap mode if supported */
3962 if (has_amd_full_remap_support(codec))
3963 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
3964 ATI_VERB_SET_MULTICHANNEL_MODE,
3965 ATI_MULTICHANNEL_MODE_SINGLE);
3971 static int patch_atihdmi(struct hda_codec *codec)
3973 struct hdmi_spec *spec;
3974 struct hdmi_spec_per_cvt *per_cvt;
3977 err = patch_generic_hdmi(codec);
3982 codec->patch_ops.init = atihdmi_init;
3986 spec->ops.pin_get_eld = atihdmi_pin_get_eld;
3987 spec->ops.pin_get_slot_channel = atihdmi_pin_get_slot_channel;
3988 spec->ops.pin_set_slot_channel = atihdmi_pin_set_slot_channel;
3989 spec->ops.pin_setup_infoframe = atihdmi_pin_setup_infoframe;
3990 spec->ops.pin_hbr_setup = atihdmi_pin_hbr_setup;
3991 spec->ops.setup_stream = atihdmi_setup_stream;
3993 if (!has_amd_full_remap_support(codec)) {
3994 /* override to ATI/AMD-specific versions with pairwise mapping */
3995 spec->ops.chmap_cea_alloc_validate_get_type =
3996 atihdmi_paired_chmap_cea_alloc_validate_get_type;
3997 spec->ops.cea_alloc_to_tlv_chmap = atihdmi_paired_cea_alloc_to_tlv_chmap;
3998 spec->ops.chmap_validate = atihdmi_paired_chmap_validate;
4001 /* ATI/AMD converters do not advertise all of their capabilities */
4002 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
4003 per_cvt = get_cvt(spec, cvt_idx);
4004 per_cvt->channels_max = max(per_cvt->channels_max, 8u);
4005 per_cvt->rates |= SUPPORTED_RATES;
4006 per_cvt->formats |= SUPPORTED_FORMATS;
4007 per_cvt->maxbps = max(per_cvt->maxbps, 24u);
4010 spec->channels_max = max(spec->channels_max, 8u);
4015 /* VIA HDMI Implementation */
4016 #define VIAHDMI_CVT_NID 0x02 /* audio converter1 */
4017 #define VIAHDMI_PIN_NID 0x03 /* HDMI output pin1 */
4019 static int patch_via_hdmi(struct hda_codec *codec)
4021 return patch_simple_hdmi(codec, VIAHDMI_CVT_NID, VIAHDMI_PIN_NID);
4027 static const struct hda_device_id snd_hda_id_hdmi[] = {
4028 HDA_CODEC_ENTRY(0x1002793c, "RS600 HDMI", patch_atihdmi),
4029 HDA_CODEC_ENTRY(0x10027919, "RS600 HDMI", patch_atihdmi),
4030 HDA_CODEC_ENTRY(0x1002791a, "RS690/780 HDMI", patch_atihdmi),
4031 HDA_CODEC_ENTRY(0x1002aa01, "R6xx HDMI", patch_atihdmi),
4032 HDA_CODEC_ENTRY(0x10951390, "SiI1390 HDMI", patch_generic_hdmi),
4033 HDA_CODEC_ENTRY(0x10951392, "SiI1392 HDMI", patch_generic_hdmi),
4034 HDA_CODEC_ENTRY(0x17e80047, "Chrontel HDMI", patch_generic_hdmi),
4035 HDA_CODEC_ENTRY(0x10de0002, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
4036 HDA_CODEC_ENTRY(0x10de0003, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
4037 HDA_CODEC_ENTRY(0x10de0005, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
4038 HDA_CODEC_ENTRY(0x10de0006, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
4039 HDA_CODEC_ENTRY(0x10de0007, "MCP79/7A HDMI", patch_nvhdmi_8ch_7x),
4040 HDA_CODEC_ENTRY(0x10de000a, "GPU 0a HDMI/DP", patch_nvhdmi),
4041 HDA_CODEC_ENTRY(0x10de000b, "GPU 0b HDMI/DP", patch_nvhdmi),
4042 HDA_CODEC_ENTRY(0x10de000c, "MCP89 HDMI", patch_nvhdmi),
4043 HDA_CODEC_ENTRY(0x10de000d, "GPU 0d HDMI/DP", patch_nvhdmi),
4044 HDA_CODEC_ENTRY(0x10de0010, "GPU 10 HDMI/DP", patch_nvhdmi),
4045 HDA_CODEC_ENTRY(0x10de0011, "GPU 11 HDMI/DP", patch_nvhdmi),
4046 HDA_CODEC_ENTRY(0x10de0012, "GPU 12 HDMI/DP", patch_nvhdmi),
4047 HDA_CODEC_ENTRY(0x10de0013, "GPU 13 HDMI/DP", patch_nvhdmi),
4048 HDA_CODEC_ENTRY(0x10de0014, "GPU 14 HDMI/DP", patch_nvhdmi),
4049 HDA_CODEC_ENTRY(0x10de0015, "GPU 15 HDMI/DP", patch_nvhdmi),
4050 HDA_CODEC_ENTRY(0x10de0016, "GPU 16 HDMI/DP", patch_nvhdmi),
4051 /* 17 is known to be absent */
4052 HDA_CODEC_ENTRY(0x10de0018, "GPU 18 HDMI/DP", patch_nvhdmi),
4053 HDA_CODEC_ENTRY(0x10de0019, "GPU 19 HDMI/DP", patch_nvhdmi),
4054 HDA_CODEC_ENTRY(0x10de001a, "GPU 1a HDMI/DP", patch_nvhdmi),
4055 HDA_CODEC_ENTRY(0x10de001b, "GPU 1b HDMI/DP", patch_nvhdmi),
4056 HDA_CODEC_ENTRY(0x10de001c, "GPU 1c HDMI/DP", patch_nvhdmi),
4057 HDA_CODEC_ENTRY(0x10de0020, "Tegra30 HDMI", patch_tegra_hdmi),
4058 HDA_CODEC_ENTRY(0x10de0022, "Tegra114 HDMI", patch_tegra_hdmi),
4059 HDA_CODEC_ENTRY(0x10de0028, "Tegra124 HDMI", patch_tegra_hdmi),
4060 HDA_CODEC_ENTRY(0x10de0029, "Tegra210 HDMI/DP", patch_tegra_hdmi),
4061 HDA_CODEC_ENTRY(0x10de0040, "GPU 40 HDMI/DP", patch_nvhdmi),
4062 HDA_CODEC_ENTRY(0x10de0041, "GPU 41 HDMI/DP", patch_nvhdmi),
4063 HDA_CODEC_ENTRY(0x10de0042, "GPU 42 HDMI/DP", patch_nvhdmi),
4064 HDA_CODEC_ENTRY(0x10de0043, "GPU 43 HDMI/DP", patch_nvhdmi),
4065 HDA_CODEC_ENTRY(0x10de0044, "GPU 44 HDMI/DP", patch_nvhdmi),
4066 HDA_CODEC_ENTRY(0x10de0051, "GPU 51 HDMI/DP", patch_nvhdmi),
4067 HDA_CODEC_ENTRY(0x10de0060, "GPU 60 HDMI/DP", patch_nvhdmi),
4068 HDA_CODEC_ENTRY(0x10de0067, "MCP67 HDMI", patch_nvhdmi_2ch),
4069 HDA_CODEC_ENTRY(0x10de0070, "GPU 70 HDMI/DP", patch_nvhdmi),
4070 HDA_CODEC_ENTRY(0x10de0071, "GPU 71 HDMI/DP", patch_nvhdmi),
4071 HDA_CODEC_ENTRY(0x10de0072, "GPU 72 HDMI/DP", patch_nvhdmi),
4072 HDA_CODEC_ENTRY(0x10de007d, "GPU 7d HDMI/DP", patch_nvhdmi),
4073 HDA_CODEC_ENTRY(0x10de0083, "GPU 83 HDMI/DP", patch_nvhdmi),
4074 HDA_CODEC_ENTRY(0x10de8001, "MCP73 HDMI", patch_nvhdmi_2ch),
4075 HDA_CODEC_ENTRY(0x11069f80, "VX900 HDMI/DP", patch_via_hdmi),
4076 HDA_CODEC_ENTRY(0x11069f81, "VX900 HDMI/DP", patch_via_hdmi),
4077 HDA_CODEC_ENTRY(0x11069f84, "VX11 HDMI/DP", patch_generic_hdmi),
4078 HDA_CODEC_ENTRY(0x11069f85, "VX11 HDMI/DP", patch_generic_hdmi),
4079 HDA_CODEC_ENTRY(0x80860054, "IbexPeak HDMI", patch_generic_hdmi),
4080 HDA_CODEC_ENTRY(0x80862801, "Bearlake HDMI", patch_generic_hdmi),
4081 HDA_CODEC_ENTRY(0x80862802, "Cantiga HDMI", patch_generic_hdmi),
4082 HDA_CODEC_ENTRY(0x80862803, "Eaglelake HDMI", patch_generic_hdmi),
4083 HDA_CODEC_ENTRY(0x80862804, "IbexPeak HDMI", patch_generic_hdmi),
4084 HDA_CODEC_ENTRY(0x80862805, "CougarPoint HDMI", patch_generic_hdmi),
4085 HDA_CODEC_ENTRY(0x80862806, "PantherPoint HDMI", patch_generic_hdmi),
4086 HDA_CODEC_ENTRY(0x80862807, "Haswell HDMI", patch_generic_hdmi),
4087 HDA_CODEC_ENTRY(0x80862808, "Broadwell HDMI", patch_generic_hdmi),
4088 HDA_CODEC_ENTRY(0x80862809, "Skylake HDMI", patch_generic_hdmi),
4089 HDA_CODEC_ENTRY(0x8086280a, "Broxton HDMI", patch_generic_hdmi),
4090 HDA_CODEC_ENTRY(0x8086280b, "Kabylake HDMI", patch_generic_hdmi),
4091 HDA_CODEC_ENTRY(0x80862880, "CedarTrail HDMI", patch_generic_hdmi),
4092 HDA_CODEC_ENTRY(0x80862882, "Valleyview2 HDMI", patch_generic_hdmi),
4093 HDA_CODEC_ENTRY(0x80862883, "Braswell HDMI", patch_generic_hdmi),
4094 HDA_CODEC_ENTRY(0x808629fb, "Crestline HDMI", patch_generic_hdmi),
4095 /* special ID for generic HDMI */
4096 HDA_CODEC_ENTRY(HDA_CODEC_ID_GENERIC_HDMI, "Generic HDMI", patch_generic_hdmi),
4099 MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_hdmi);
4101 MODULE_LICENSE("GPL");
4102 MODULE_DESCRIPTION("HDMI HD-audio codec");
4103 MODULE_ALIAS("snd-hda-codec-intelhdmi");
4104 MODULE_ALIAS("snd-hda-codec-nvhdmi");
4105 MODULE_ALIAS("snd-hda-codec-atihdmi");
4107 static struct hda_codec_driver hdmi_driver = {
4108 .id = snd_hda_id_hdmi,
4111 module_hda_codec_driver(hdmi_driver);