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 int cvt_nid_to_cvt_index(struct hda_codec *codec, hda_nid_t cvt_nid)
442 struct hdmi_spec *spec = codec->spec;
445 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++)
446 if (get_cvt(spec, cvt_idx)->cvt_nid == cvt_nid)
449 codec_warn(codec, "HDMI: cvt nid %d not registered\n", cvt_nid);
453 static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
454 struct snd_ctl_elem_info *uinfo)
456 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
457 struct hdmi_spec *spec = codec->spec;
458 struct hdmi_spec_per_pin *per_pin;
459 struct hdmi_eld *eld;
462 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
464 pin_idx = kcontrol->private_value;
465 per_pin = get_pin(spec, pin_idx);
466 eld = &per_pin->sink_eld;
468 mutex_lock(&per_pin->lock);
469 uinfo->count = eld->eld_valid ? eld->eld_size : 0;
470 mutex_unlock(&per_pin->lock);
475 static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
476 struct snd_ctl_elem_value *ucontrol)
478 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
479 struct hdmi_spec *spec = codec->spec;
480 struct hdmi_spec_per_pin *per_pin;
481 struct hdmi_eld *eld;
484 pin_idx = kcontrol->private_value;
485 per_pin = get_pin(spec, pin_idx);
486 eld = &per_pin->sink_eld;
488 mutex_lock(&per_pin->lock);
489 if (eld->eld_size > ARRAY_SIZE(ucontrol->value.bytes.data)) {
490 mutex_unlock(&per_pin->lock);
495 memset(ucontrol->value.bytes.data, 0,
496 ARRAY_SIZE(ucontrol->value.bytes.data));
498 memcpy(ucontrol->value.bytes.data, eld->eld_buffer,
500 mutex_unlock(&per_pin->lock);
505 static struct snd_kcontrol_new eld_bytes_ctl = {
506 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
507 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
509 .info = hdmi_eld_ctl_info,
510 .get = hdmi_eld_ctl_get,
513 static int hdmi_create_eld_ctl(struct hda_codec *codec, int pin_idx,
516 struct snd_kcontrol *kctl;
517 struct hdmi_spec *spec = codec->spec;
520 kctl = snd_ctl_new1(&eld_bytes_ctl, codec);
523 kctl->private_value = pin_idx;
524 kctl->id.device = device;
526 err = snd_hda_ctl_add(codec, get_pin(spec, pin_idx)->pin_nid, kctl);
530 get_pin(spec, pin_idx)->eld_ctl = kctl;
535 static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
536 int *packet_index, int *byte_index)
540 val = snd_hda_codec_read(codec, pin_nid, 0,
541 AC_VERB_GET_HDMI_DIP_INDEX, 0);
543 *packet_index = val >> 5;
544 *byte_index = val & 0x1f;
548 static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
549 int packet_index, int byte_index)
553 val = (packet_index << 5) | (byte_index & 0x1f);
555 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
558 static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t pin_nid,
561 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
564 static void hdmi_init_pin(struct hda_codec *codec, hda_nid_t pin_nid)
566 struct hdmi_spec *spec = codec->spec;
570 if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
571 snd_hda_codec_write(codec, pin_nid, 0,
572 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
574 if (spec->dyn_pin_out)
575 /* Disable pin out until stream is active */
578 /* Enable pin out: some machines with GM965 gets broken output
579 * when the pin is disabled or changed while using with HDMI
583 snd_hda_codec_write(codec, pin_nid, 0,
584 AC_VERB_SET_PIN_WIDGET_CONTROL, pin_out);
587 static int hdmi_get_channel_count(struct hda_codec *codec, hda_nid_t cvt_nid)
589 return 1 + snd_hda_codec_read(codec, cvt_nid, 0,
590 AC_VERB_GET_CVT_CHAN_COUNT, 0);
593 static void hdmi_set_channel_count(struct hda_codec *codec,
594 hda_nid_t cvt_nid, int chs)
596 if (chs != hdmi_get_channel_count(codec, cvt_nid))
597 snd_hda_codec_write(codec, cvt_nid, 0,
598 AC_VERB_SET_CVT_CHAN_COUNT, chs - 1);
605 #ifdef CONFIG_SND_PROC_FS
606 static void print_eld_info(struct snd_info_entry *entry,
607 struct snd_info_buffer *buffer)
609 struct hdmi_spec_per_pin *per_pin = entry->private_data;
611 mutex_lock(&per_pin->lock);
612 snd_hdmi_print_eld_info(&per_pin->sink_eld, buffer);
613 mutex_unlock(&per_pin->lock);
616 static void write_eld_info(struct snd_info_entry *entry,
617 struct snd_info_buffer *buffer)
619 struct hdmi_spec_per_pin *per_pin = entry->private_data;
621 mutex_lock(&per_pin->lock);
622 snd_hdmi_write_eld_info(&per_pin->sink_eld, buffer);
623 mutex_unlock(&per_pin->lock);
626 static int eld_proc_new(struct hdmi_spec_per_pin *per_pin, int index)
629 struct hda_codec *codec = per_pin->codec;
630 struct snd_info_entry *entry;
633 snprintf(name, sizeof(name), "eld#%d.%d", codec->addr, index);
634 err = snd_card_proc_new(codec->card, name, &entry);
638 snd_info_set_text_ops(entry, per_pin, print_eld_info);
639 entry->c.text.write = write_eld_info;
640 entry->mode |= S_IWUSR;
641 per_pin->proc_entry = entry;
646 static void eld_proc_free(struct hdmi_spec_per_pin *per_pin)
648 if (!per_pin->codec->bus->shutdown) {
649 snd_info_free_entry(per_pin->proc_entry);
650 per_pin->proc_entry = NULL;
654 static inline int eld_proc_new(struct hdmi_spec_per_pin *per_pin,
659 static inline void eld_proc_free(struct hdmi_spec_per_pin *per_pin)
665 * Channel mapping routines
669 * Compute derived values in channel_allocations[].
671 static void init_channel_allocations(void)
674 struct cea_channel_speaker_allocation *p;
676 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
677 p = channel_allocations + i;
680 for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
681 if (p->speakers[j]) {
683 p->spk_mask |= p->speakers[j];
688 static int get_channel_allocation_order(int ca)
692 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
693 if (channel_allocations[i].ca_index == ca)
700 * The transformation takes two steps:
702 * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
703 * spk_mask => (channel_allocations[]) => ai->CA
705 * TODO: it could select the wrong CA from multiple candidates.
707 static int hdmi_channel_allocation(struct hda_codec *codec,
708 struct hdmi_eld *eld, int channels)
713 char buf[SND_PRINT_CHANNEL_ALLOCATION_ADVISED_BUFSIZE];
716 * CA defaults to 0 for basic stereo audio
722 * expand ELD's speaker allocation mask
724 * ELD tells the speaker mask in a compact(paired) form,
725 * expand ELD's notions to match the ones used by Audio InfoFrame.
727 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
728 if (eld->info.spk_alloc & (1 << i))
729 spk_mask |= eld_speaker_allocation_bits[i];
732 /* search for the first working match in the CA table */
733 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
734 if (channels == channel_allocations[i].channels &&
735 (spk_mask & channel_allocations[i].spk_mask) ==
736 channel_allocations[i].spk_mask) {
737 ca = channel_allocations[i].ca_index;
743 /* if there was no match, select the regular ALSA channel
744 * allocation with the matching number of channels */
745 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
746 if (channels == channel_allocations[i].channels) {
747 ca = channel_allocations[i].ca_index;
753 snd_print_channel_allocation(eld->info.spk_alloc, buf, sizeof(buf));
754 codec_dbg(codec, "HDMI: select CA 0x%x for %d-channel allocation: %s\n",
760 static void hdmi_debug_channel_mapping(struct hda_codec *codec,
763 #ifdef CONFIG_SND_DEBUG_VERBOSE
764 struct hdmi_spec *spec = codec->spec;
768 for (i = 0; i < 8; i++) {
769 channel = spec->ops.pin_get_slot_channel(codec, pin_nid, i);
770 codec_dbg(codec, "HDMI: ASP channel %d => slot %d\n",
776 static void hdmi_std_setup_channel_mapping(struct hda_codec *codec,
781 struct hdmi_spec *spec = codec->spec;
782 struct cea_channel_speaker_allocation *ch_alloc;
786 int non_pcm_mapping[8];
788 order = get_channel_allocation_order(ca);
789 ch_alloc = &channel_allocations[order];
791 if (hdmi_channel_mapping[ca][1] == 0) {
793 /* fill actual channel mappings in ALSA channel (i) order */
794 for (i = 0; i < ch_alloc->channels; i++) {
795 while (!ch_alloc->speakers[7 - hdmi_slot] && !WARN_ON(hdmi_slot >= 8))
796 hdmi_slot++; /* skip zero slots */
798 hdmi_channel_mapping[ca][i] = (i << 4) | hdmi_slot++;
800 /* fill the rest of the slots with ALSA channel 0xf */
801 for (hdmi_slot = 0; hdmi_slot < 8; hdmi_slot++)
802 if (!ch_alloc->speakers[7 - hdmi_slot])
803 hdmi_channel_mapping[ca][i++] = (0xf << 4) | hdmi_slot;
807 for (i = 0; i < ch_alloc->channels; i++)
808 non_pcm_mapping[i] = (i << 4) | i;
810 non_pcm_mapping[i] = (0xf << 4) | i;
813 for (i = 0; i < 8; i++) {
814 int slotsetup = non_pcm ? non_pcm_mapping[i] : hdmi_channel_mapping[ca][i];
815 int hdmi_slot = slotsetup & 0x0f;
816 int channel = (slotsetup & 0xf0) >> 4;
817 err = spec->ops.pin_set_slot_channel(codec, pin_nid, hdmi_slot, channel);
819 codec_dbg(codec, "HDMI: channel mapping failed\n");
825 struct channel_map_table {
826 unsigned char map; /* ALSA API channel map position */
827 int spk_mask; /* speaker position bit mask */
830 static struct channel_map_table map_tables[] = {
831 { SNDRV_CHMAP_FL, FL },
832 { SNDRV_CHMAP_FR, FR },
833 { SNDRV_CHMAP_RL, RL },
834 { SNDRV_CHMAP_RR, RR },
835 { SNDRV_CHMAP_LFE, LFE },
836 { SNDRV_CHMAP_FC, FC },
837 { SNDRV_CHMAP_RLC, RLC },
838 { SNDRV_CHMAP_RRC, RRC },
839 { SNDRV_CHMAP_RC, RC },
840 { SNDRV_CHMAP_FLC, FLC },
841 { SNDRV_CHMAP_FRC, FRC },
842 { SNDRV_CHMAP_TFL, FLH },
843 { SNDRV_CHMAP_TFR, FRH },
844 { SNDRV_CHMAP_FLW, FLW },
845 { SNDRV_CHMAP_FRW, FRW },
846 { SNDRV_CHMAP_TC, TC },
847 { SNDRV_CHMAP_TFC, FCH },
851 /* from ALSA API channel position to speaker bit mask */
852 static int to_spk_mask(unsigned char c)
854 struct channel_map_table *t = map_tables;
855 for (; t->map; t++) {
862 /* from ALSA API channel position to CEA slot */
863 static int to_cea_slot(int ordered_ca, unsigned char pos)
865 int mask = to_spk_mask(pos);
869 for (i = 0; i < 8; i++) {
870 if (channel_allocations[ordered_ca].speakers[7 - i] == mask)
878 /* from speaker bit mask to ALSA API channel position */
879 static int spk_to_chmap(int spk)
881 struct channel_map_table *t = map_tables;
882 for (; t->map; t++) {
883 if (t->spk_mask == spk)
889 /* from CEA slot to ALSA API channel position */
890 static int from_cea_slot(int ordered_ca, unsigned char slot)
892 int mask = channel_allocations[ordered_ca].speakers[7 - slot];
894 return spk_to_chmap(mask);
897 /* get the CA index corresponding to the given ALSA API channel map */
898 static int hdmi_manual_channel_allocation(int chs, unsigned char *map)
900 int i, spks = 0, spk_mask = 0;
902 for (i = 0; i < chs; i++) {
903 int mask = to_spk_mask(map[i]);
910 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
911 if ((chs == channel_allocations[i].channels ||
912 spks == channel_allocations[i].channels) &&
913 (spk_mask & channel_allocations[i].spk_mask) ==
914 channel_allocations[i].spk_mask)
915 return channel_allocations[i].ca_index;
920 /* set up the channel slots for the given ALSA API channel map */
921 static int hdmi_manual_setup_channel_mapping(struct hda_codec *codec,
923 int chs, unsigned char *map,
926 struct hdmi_spec *spec = codec->spec;
927 int ordered_ca = get_channel_allocation_order(ca);
928 int alsa_pos, hdmi_slot;
929 int assignments[8] = {[0 ... 7] = 0xf};
931 for (alsa_pos = 0; alsa_pos < chs; alsa_pos++) {
933 hdmi_slot = to_cea_slot(ordered_ca, map[alsa_pos]);
936 continue; /* unassigned channel */
938 assignments[hdmi_slot] = alsa_pos;
941 for (hdmi_slot = 0; hdmi_slot < 8; hdmi_slot++) {
944 err = spec->ops.pin_set_slot_channel(codec, pin_nid, hdmi_slot,
945 assignments[hdmi_slot]);
952 /* store ALSA API channel map from the current default map */
953 static void hdmi_setup_fake_chmap(unsigned char *map, int ca)
956 int ordered_ca = get_channel_allocation_order(ca);
957 for (i = 0; i < 8; i++) {
958 if (i < channel_allocations[ordered_ca].channels)
959 map[i] = from_cea_slot(ordered_ca, hdmi_channel_mapping[ca][i] & 0x0f);
965 static void hdmi_setup_channel_mapping(struct hda_codec *codec,
966 hda_nid_t pin_nid, bool non_pcm, int ca,
967 int channels, unsigned char *map,
970 if (!non_pcm && chmap_set) {
971 hdmi_manual_setup_channel_mapping(codec, pin_nid,
974 hdmi_std_setup_channel_mapping(codec, pin_nid, non_pcm, ca);
975 hdmi_setup_fake_chmap(map, ca);
978 hdmi_debug_channel_mapping(codec, pin_nid);
981 static int hdmi_pin_set_slot_channel(struct hda_codec *codec, hda_nid_t pin_nid,
982 int asp_slot, int channel)
984 return snd_hda_codec_write(codec, pin_nid, 0,
985 AC_VERB_SET_HDMI_CHAN_SLOT,
986 (channel << 4) | asp_slot);
989 static int hdmi_pin_get_slot_channel(struct hda_codec *codec, hda_nid_t pin_nid,
992 return (snd_hda_codec_read(codec, pin_nid, 0,
993 AC_VERB_GET_HDMI_CHAN_SLOT,
994 asp_slot) & 0xf0) >> 4;
998 * Audio InfoFrame routines
1002 * Enable Audio InfoFrame Transmission
1004 static void hdmi_start_infoframe_trans(struct hda_codec *codec,
1007 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
1008 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
1013 * Disable Audio InfoFrame Transmission
1015 static void hdmi_stop_infoframe_trans(struct hda_codec *codec,
1018 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
1019 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
1020 AC_DIPXMIT_DISABLE);
1023 static void hdmi_debug_dip_size(struct hda_codec *codec, hda_nid_t pin_nid)
1025 #ifdef CONFIG_SND_DEBUG_VERBOSE
1029 size = snd_hdmi_get_eld_size(codec, pin_nid);
1030 codec_dbg(codec, "HDMI: ELD buf size is %d\n", size);
1032 for (i = 0; i < 8; i++) {
1033 size = snd_hda_codec_read(codec, pin_nid, 0,
1034 AC_VERB_GET_HDMI_DIP_SIZE, i);
1035 codec_dbg(codec, "HDMI: DIP GP[%d] buf size is %d\n", i, size);
1040 static void hdmi_clear_dip_buffers(struct hda_codec *codec, hda_nid_t pin_nid)
1046 for (i = 0; i < 8; i++) {
1047 size = snd_hda_codec_read(codec, pin_nid, 0,
1048 AC_VERB_GET_HDMI_DIP_SIZE, i);
1052 hdmi_set_dip_index(codec, pin_nid, i, 0x0);
1053 for (j = 1; j < 1000; j++) {
1054 hdmi_write_dip_byte(codec, pin_nid, 0x0);
1055 hdmi_get_dip_index(codec, pin_nid, &pi, &bi);
1057 codec_dbg(codec, "dip index %d: %d != %d\n",
1059 if (bi == 0) /* byte index wrapped around */
1063 "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
1069 static void hdmi_checksum_audio_infoframe(struct hdmi_audio_infoframe *hdmi_ai)
1071 u8 *bytes = (u8 *)hdmi_ai;
1075 hdmi_ai->checksum = 0;
1077 for (i = 0; i < sizeof(*hdmi_ai); i++)
1080 hdmi_ai->checksum = -sum;
1083 static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
1089 hdmi_debug_dip_size(codec, pin_nid);
1090 hdmi_clear_dip_buffers(codec, pin_nid); /* be paranoid */
1092 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
1093 for (i = 0; i < size; i++)
1094 hdmi_write_dip_byte(codec, pin_nid, dip[i]);
1097 static bool hdmi_infoframe_uptodate(struct hda_codec *codec, hda_nid_t pin_nid,
1103 if (snd_hda_codec_read(codec, pin_nid, 0, AC_VERB_GET_HDMI_DIP_XMIT, 0)
1107 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
1108 for (i = 0; i < size; i++) {
1109 val = snd_hda_codec_read(codec, pin_nid, 0,
1110 AC_VERB_GET_HDMI_DIP_DATA, 0);
1118 static void hdmi_pin_setup_infoframe(struct hda_codec *codec,
1120 int ca, int active_channels,
1123 union audio_infoframe ai;
1125 memset(&ai, 0, sizeof(ai));
1126 if (conn_type == 0) { /* HDMI */
1127 struct hdmi_audio_infoframe *hdmi_ai = &ai.hdmi;
1129 hdmi_ai->type = 0x84;
1130 hdmi_ai->ver = 0x01;
1131 hdmi_ai->len = 0x0a;
1132 hdmi_ai->CC02_CT47 = active_channels - 1;
1134 hdmi_checksum_audio_infoframe(hdmi_ai);
1135 } else if (conn_type == 1) { /* DisplayPort */
1136 struct dp_audio_infoframe *dp_ai = &ai.dp;
1140 dp_ai->ver = 0x11 << 2;
1141 dp_ai->CC02_CT47 = active_channels - 1;
1144 codec_dbg(codec, "HDMI: unknown connection type at pin %d\n",
1150 * sizeof(ai) is used instead of sizeof(*hdmi_ai) or
1151 * sizeof(*dp_ai) to avoid partial match/update problems when
1152 * the user switches between HDMI/DP monitors.
1154 if (!hdmi_infoframe_uptodate(codec, pin_nid, ai.bytes,
1157 "hdmi_pin_setup_infoframe: pin=%d channels=%d ca=0x%02x\n",
1159 active_channels, ca);
1160 hdmi_stop_infoframe_trans(codec, pin_nid);
1161 hdmi_fill_audio_infoframe(codec, pin_nid,
1162 ai.bytes, sizeof(ai));
1163 hdmi_start_infoframe_trans(codec, pin_nid);
1167 static void hdmi_setup_audio_infoframe(struct hda_codec *codec,
1168 struct hdmi_spec_per_pin *per_pin,
1171 struct hdmi_spec *spec = codec->spec;
1172 hda_nid_t pin_nid = per_pin->pin_nid;
1173 int channels = per_pin->channels;
1174 int active_channels;
1175 struct hdmi_eld *eld;
1181 if (is_haswell_plus(codec))
1182 snd_hda_codec_write(codec, pin_nid, 0,
1183 AC_VERB_SET_AMP_GAIN_MUTE,
1186 eld = &per_pin->sink_eld;
1188 if (!non_pcm && per_pin->chmap_set)
1189 ca = hdmi_manual_channel_allocation(channels, per_pin->chmap);
1191 ca = hdmi_channel_allocation(codec, eld, channels);
1195 ordered_ca = get_channel_allocation_order(ca);
1196 active_channels = channel_allocations[ordered_ca].channels;
1198 hdmi_set_channel_count(codec, per_pin->cvt_nid, active_channels);
1201 * always configure channel mapping, it may have been changed by the
1202 * user in the meantime
1204 hdmi_setup_channel_mapping(codec, pin_nid, non_pcm, ca,
1205 channels, per_pin->chmap,
1206 per_pin->chmap_set);
1208 spec->ops.pin_setup_infoframe(codec, pin_nid, ca, active_channels,
1209 eld->info.conn_type);
1211 per_pin->non_pcm = non_pcm;
1215 * Unsolicited events
1218 static bool hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll);
1220 static void check_presence_and_report(struct hda_codec *codec, hda_nid_t nid)
1222 struct hdmi_spec *spec = codec->spec;
1223 int pin_idx = pin_nid_to_pin_index(codec, nid);
1227 if (hdmi_present_sense(get_pin(spec, pin_idx), 1))
1228 snd_hda_jack_report_sync(codec);
1231 static void jack_callback(struct hda_codec *codec,
1232 struct hda_jack_callback *jack)
1234 check_presence_and_report(codec, jack->tbl->nid);
1237 static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
1239 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
1240 struct hda_jack_tbl *jack;
1241 int dev_entry = (res & AC_UNSOL_RES_DE) >> AC_UNSOL_RES_DE_SHIFT;
1243 jack = snd_hda_jack_tbl_get_from_tag(codec, tag);
1246 jack->jack_dirty = 1;
1249 "HDMI hot plug event: Codec=%d Pin=%d Device=%d Inactive=%d Presence_Detect=%d ELD_Valid=%d\n",
1250 codec->addr, jack->nid, dev_entry, !!(res & AC_UNSOL_RES_IA),
1251 !!(res & AC_UNSOL_RES_PD), !!(res & AC_UNSOL_RES_ELDV));
1253 check_presence_and_report(codec, jack->nid);
1256 static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
1258 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
1259 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
1260 int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
1261 int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
1264 "HDMI CP event: CODEC=%d TAG=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
1279 static void hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
1281 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
1282 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
1284 if (!snd_hda_jack_tbl_get_from_tag(codec, tag)) {
1285 codec_dbg(codec, "Unexpected HDMI event tag 0x%x\n", tag);
1290 hdmi_intrinsic_event(codec, res);
1292 hdmi_non_intrinsic_event(codec, res);
1295 static void haswell_verify_D0(struct hda_codec *codec,
1296 hda_nid_t cvt_nid, hda_nid_t nid)
1300 /* For Haswell, the converter 1/2 may keep in D3 state after bootup,
1301 * thus pins could only choose converter 0 for use. Make sure the
1302 * converters are in correct power state */
1303 if (!snd_hda_check_power_state(codec, cvt_nid, AC_PWRST_D0))
1304 snd_hda_codec_write(codec, cvt_nid, 0, AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
1306 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0)) {
1307 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
1310 pwr = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
1311 pwr = (pwr & AC_PWRST_ACTUAL) >> AC_PWRST_ACTUAL_SHIFT;
1312 codec_dbg(codec, "Haswell HDMI audio: Power for pin 0x%x is now D%d\n", nid, pwr);
1320 /* HBR should be Non-PCM, 8 channels */
1321 #define is_hbr_format(format) \
1322 ((format & AC_FMT_TYPE_NON_PCM) && (format & AC_FMT_CHAN_MASK) == 7)
1324 static int hdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
1327 int pinctl, new_pinctl;
1329 if (snd_hda_query_pin_caps(codec, pin_nid) & AC_PINCAP_HBR) {
1330 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
1331 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1334 return hbr ? -EINVAL : 0;
1336 new_pinctl = pinctl & ~AC_PINCTL_EPT;
1338 new_pinctl |= AC_PINCTL_EPT_HBR;
1340 new_pinctl |= AC_PINCTL_EPT_NATIVE;
1343 "hdmi_pin_hbr_setup: NID=0x%x, %spinctl=0x%x\n",
1345 pinctl == new_pinctl ? "" : "new-",
1348 if (pinctl != new_pinctl)
1349 snd_hda_codec_write(codec, pin_nid, 0,
1350 AC_VERB_SET_PIN_WIDGET_CONTROL,
1358 static int hdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
1359 hda_nid_t pin_nid, u32 stream_tag, int format)
1361 struct hdmi_spec *spec = codec->spec;
1364 if (is_haswell_plus(codec))
1365 haswell_verify_D0(codec, cvt_nid, pin_nid);
1367 err = spec->ops.pin_hbr_setup(codec, pin_nid, is_hbr_format(format));
1370 codec_dbg(codec, "hdmi_setup_stream: HBR is not supported\n");
1374 snd_hda_codec_setup_stream(codec, cvt_nid, stream_tag, 0, format);
1378 /* Try to find an available converter
1379 * If pin_idx is less then zero, just try to find an available converter.
1380 * Otherwise, try to find an available converter and get the cvt mux index
1383 static int hdmi_choose_cvt(struct hda_codec *codec,
1384 int pin_idx, int *cvt_id, int *mux_id)
1386 struct hdmi_spec *spec = codec->spec;
1387 struct hdmi_spec_per_pin *per_pin;
1388 struct hdmi_spec_per_cvt *per_cvt = NULL;
1389 int cvt_idx, mux_idx = 0;
1391 /* pin_idx < 0 means no pin will be bound to the converter */
1395 per_pin = get_pin(spec, pin_idx);
1397 /* Dynamically assign converter to stream */
1398 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
1399 per_cvt = get_cvt(spec, cvt_idx);
1401 /* Must not already be assigned */
1402 if (per_cvt->assigned)
1404 if (per_pin == NULL)
1406 /* Must be in pin's mux's list of converters */
1407 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
1408 if (per_pin->mux_nids[mux_idx] == per_cvt->cvt_nid)
1410 /* Not in mux list */
1411 if (mux_idx == per_pin->num_mux_nids)
1416 /* No free converters */
1417 if (cvt_idx == spec->num_cvts)
1420 if (per_pin != NULL)
1421 per_pin->mux_idx = mux_idx;
1431 /* Assure the pin select the right convetor */
1432 static void intel_verify_pin_cvt_connect(struct hda_codec *codec,
1433 struct hdmi_spec_per_pin *per_pin)
1435 hda_nid_t pin_nid = per_pin->pin_nid;
1438 mux_idx = per_pin->mux_idx;
1439 curr = snd_hda_codec_read(codec, pin_nid, 0,
1440 AC_VERB_GET_CONNECT_SEL, 0);
1441 if (curr != mux_idx)
1442 snd_hda_codec_write_cache(codec, pin_nid, 0,
1443 AC_VERB_SET_CONNECT_SEL,
1447 /* get the mux index for the converter of the pins
1448 * converter's mux index is the same for all pins on Intel platform
1450 static int intel_cvt_id_to_mux_idx(struct hdmi_spec *spec,
1455 for (i = 0; i < spec->num_cvts; i++)
1456 if (spec->cvt_nids[i] == cvt_nid)
1461 /* Intel HDMI workaround to fix audio routing issue:
1462 * For some Intel display codecs, pins share the same connection list.
1463 * So a conveter can be selected by multiple pins and playback on any of these
1464 * pins will generate sound on the external display, because audio flows from
1465 * the same converter to the display pipeline. Also muting one pin may make
1466 * other pins have no sound output.
1467 * So this function assures that an assigned converter for a pin is not selected
1468 * by any other pins.
1470 static void intel_not_share_assigned_cvt(struct hda_codec *codec,
1471 hda_nid_t pin_nid, int mux_idx)
1473 struct hdmi_spec *spec = codec->spec;
1476 struct hdmi_spec_per_cvt *per_cvt;
1478 /* configure all pins, including "no physical connection" ones */
1479 for_each_hda_codec_node(nid, codec) {
1480 unsigned int wid_caps = get_wcaps(codec, nid);
1481 unsigned int wid_type = get_wcaps_type(wid_caps);
1483 if (wid_type != AC_WID_PIN)
1489 curr = snd_hda_codec_read(codec, nid, 0,
1490 AC_VERB_GET_CONNECT_SEL, 0);
1491 if (curr != mux_idx)
1494 /* choose an unassigned converter. The conveters in the
1495 * connection list are in the same order as in the codec.
1497 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
1498 per_cvt = get_cvt(spec, cvt_idx);
1499 if (!per_cvt->assigned) {
1501 "choose cvt %d for pin nid %d\n",
1503 snd_hda_codec_write_cache(codec, nid, 0,
1504 AC_VERB_SET_CONNECT_SEL,
1512 /* A wrapper of intel_not_share_asigned_cvt() */
1513 static void intel_not_share_assigned_cvt_nid(struct hda_codec *codec,
1514 hda_nid_t pin_nid, hda_nid_t cvt_nid)
1517 struct hdmi_spec *spec = codec->spec;
1519 if (!is_haswell_plus(codec) && !is_valleyview_plus(codec))
1522 /* On Intel platform, the mapping of converter nid to
1523 * mux index of the pins are always the same.
1524 * The pin nid may be 0, this means all pins will not
1525 * share the converter.
1527 mux_idx = intel_cvt_id_to_mux_idx(spec, cvt_nid);
1529 intel_not_share_assigned_cvt(codec, pin_nid, mux_idx);
1532 /* called in hdmi_pcm_open when no pin is assigned to the PCM
1533 * in dyn_pcm_assign mode.
1535 static int hdmi_pcm_open_no_pin(struct hda_pcm_stream *hinfo,
1536 struct hda_codec *codec,
1537 struct snd_pcm_substream *substream)
1539 struct hdmi_spec *spec = codec->spec;
1540 struct snd_pcm_runtime *runtime = substream->runtime;
1541 int cvt_idx, pcm_idx;
1542 struct hdmi_spec_per_cvt *per_cvt = NULL;
1545 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1549 err = hdmi_choose_cvt(codec, -1, &cvt_idx, NULL);
1553 per_cvt = get_cvt(spec, cvt_idx);
1554 per_cvt->assigned = 1;
1555 hinfo->nid = per_cvt->cvt_nid;
1557 intel_not_share_assigned_cvt_nid(codec, 0, per_cvt->cvt_nid);
1559 set_bit(pcm_idx, &spec->pcm_in_use);
1560 /* todo: setup spdif ctls assign */
1562 /* Initially set the converter's capabilities */
1563 hinfo->channels_min = per_cvt->channels_min;
1564 hinfo->channels_max = per_cvt->channels_max;
1565 hinfo->rates = per_cvt->rates;
1566 hinfo->formats = per_cvt->formats;
1567 hinfo->maxbps = per_cvt->maxbps;
1569 /* Store the updated parameters */
1570 runtime->hw.channels_min = hinfo->channels_min;
1571 runtime->hw.channels_max = hinfo->channels_max;
1572 runtime->hw.formats = hinfo->formats;
1573 runtime->hw.rates = hinfo->rates;
1575 snd_pcm_hw_constraint_step(substream->runtime, 0,
1576 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
1583 static int hdmi_pcm_open(struct hda_pcm_stream *hinfo,
1584 struct hda_codec *codec,
1585 struct snd_pcm_substream *substream)
1587 struct hdmi_spec *spec = codec->spec;
1588 struct snd_pcm_runtime *runtime = substream->runtime;
1589 int pin_idx, cvt_idx, pcm_idx, mux_idx = 0;
1590 struct hdmi_spec_per_pin *per_pin;
1591 struct hdmi_eld *eld;
1592 struct hdmi_spec_per_cvt *per_cvt = NULL;
1595 /* Validate hinfo */
1596 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1600 mutex_lock(&spec->pcm_lock);
1601 pin_idx = hinfo_to_pin_index(codec, hinfo);
1602 if (!spec->dyn_pcm_assign) {
1603 if (snd_BUG_ON(pin_idx < 0)) {
1604 mutex_unlock(&spec->pcm_lock);
1608 /* no pin is assigned to the PCM
1609 * PA need pcm open successfully when probe
1612 err = hdmi_pcm_open_no_pin(hinfo, codec, substream);
1613 mutex_unlock(&spec->pcm_lock);
1618 err = hdmi_choose_cvt(codec, pin_idx, &cvt_idx, &mux_idx);
1620 mutex_unlock(&spec->pcm_lock);
1624 per_cvt = get_cvt(spec, cvt_idx);
1625 /* Claim converter */
1626 per_cvt->assigned = 1;
1628 set_bit(pcm_idx, &spec->pcm_in_use);
1629 per_pin = get_pin(spec, pin_idx);
1630 per_pin->cvt_nid = per_cvt->cvt_nid;
1631 hinfo->nid = per_cvt->cvt_nid;
1633 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
1634 AC_VERB_SET_CONNECT_SEL,
1637 /* configure unused pins to choose other converters */
1638 if (is_haswell_plus(codec) || is_valleyview_plus(codec))
1639 intel_not_share_assigned_cvt(codec, per_pin->pin_nid, mux_idx);
1641 snd_hda_spdif_ctls_assign(codec, pcm_idx, per_cvt->cvt_nid);
1643 /* Initially set the converter's capabilities */
1644 hinfo->channels_min = per_cvt->channels_min;
1645 hinfo->channels_max = per_cvt->channels_max;
1646 hinfo->rates = per_cvt->rates;
1647 hinfo->formats = per_cvt->formats;
1648 hinfo->maxbps = per_cvt->maxbps;
1650 eld = &per_pin->sink_eld;
1651 /* Restrict capabilities by ELD if this isn't disabled */
1652 if (!static_hdmi_pcm && eld->eld_valid) {
1653 snd_hdmi_eld_update_pcm_info(&eld->info, hinfo);
1654 if (hinfo->channels_min > hinfo->channels_max ||
1655 !hinfo->rates || !hinfo->formats) {
1656 per_cvt->assigned = 0;
1658 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
1659 mutex_unlock(&spec->pcm_lock);
1664 mutex_unlock(&spec->pcm_lock);
1665 /* Store the updated parameters */
1666 runtime->hw.channels_min = hinfo->channels_min;
1667 runtime->hw.channels_max = hinfo->channels_max;
1668 runtime->hw.formats = hinfo->formats;
1669 runtime->hw.rates = hinfo->rates;
1671 snd_pcm_hw_constraint_step(substream->runtime, 0,
1672 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
1677 * HDA/HDMI auto parsing
1679 static int hdmi_read_pin_conn(struct hda_codec *codec, int pin_idx)
1681 struct hdmi_spec *spec = codec->spec;
1682 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1683 hda_nid_t pin_nid = per_pin->pin_nid;
1685 if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
1687 "HDMI: pin %d wcaps %#x does not support connection list\n",
1688 pin_nid, get_wcaps(codec, pin_nid));
1692 per_pin->num_mux_nids = snd_hda_get_connections(codec, pin_nid,
1694 HDA_MAX_CONNECTIONS);
1699 static int hdmi_find_pcm_slot(struct hdmi_spec *spec,
1700 struct hdmi_spec_per_pin *per_pin)
1704 /* try the prefer PCM */
1705 if (!test_bit(per_pin->pin_nid_idx, &spec->pcm_bitmap))
1706 return per_pin->pin_nid_idx;
1708 /* have a second try; check the "reserved area" over num_pins */
1709 for (i = spec->num_pins; i < spec->pcm_used; i++) {
1710 if (!test_bit(i, &spec->pcm_bitmap))
1714 /* the last try; check the empty slots in pins */
1715 for (i = 0; i < spec->num_pins; i++) {
1716 if (!test_bit(i, &spec->pcm_bitmap))
1722 static void hdmi_attach_hda_pcm(struct hdmi_spec *spec,
1723 struct hdmi_spec_per_pin *per_pin)
1727 /* pcm already be attached to the pin */
1730 idx = hdmi_find_pcm_slot(spec, per_pin);
1733 per_pin->pcm_idx = idx;
1734 per_pin->pcm = get_hdmi_pcm(spec, idx);
1735 set_bit(idx, &spec->pcm_bitmap);
1738 static void hdmi_detach_hda_pcm(struct hdmi_spec *spec,
1739 struct hdmi_spec_per_pin *per_pin)
1743 /* pcm already be detached from the pin */
1746 idx = per_pin->pcm_idx;
1747 per_pin->pcm_idx = -1;
1748 per_pin->pcm = NULL;
1749 if (idx >= 0 && idx < spec->pcm_used)
1750 clear_bit(idx, &spec->pcm_bitmap);
1753 static int hdmi_get_pin_cvt_mux(struct hdmi_spec *spec,
1754 struct hdmi_spec_per_pin *per_pin, hda_nid_t cvt_nid)
1758 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
1759 if (per_pin->mux_nids[mux_idx] == cvt_nid)
1764 static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid);
1766 static void hdmi_pcm_setup_pin(struct hdmi_spec *spec,
1767 struct hdmi_spec_per_pin *per_pin)
1769 struct hda_codec *codec = per_pin->codec;
1770 struct hda_pcm *pcm;
1771 struct hda_pcm_stream *hinfo;
1772 struct snd_pcm_substream *substream;
1776 if (per_pin->pcm_idx >= 0 && per_pin->pcm_idx < spec->pcm_used)
1777 pcm = get_pcm_rec(spec, per_pin->pcm_idx);
1780 if (!test_bit(per_pin->pcm_idx, &spec->pcm_in_use))
1783 /* hdmi audio only uses playback and one substream */
1784 hinfo = pcm->stream;
1785 substream = pcm->pcm->streams[0].substream;
1787 per_pin->cvt_nid = hinfo->nid;
1789 mux_idx = hdmi_get_pin_cvt_mux(spec, per_pin, hinfo->nid);
1790 if (mux_idx < per_pin->num_mux_nids)
1791 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
1792 AC_VERB_SET_CONNECT_SEL,
1794 snd_hda_spdif_ctls_assign(codec, per_pin->pcm_idx, hinfo->nid);
1796 non_pcm = check_non_pcm_per_cvt(codec, hinfo->nid);
1797 if (substream->runtime)
1798 per_pin->channels = substream->runtime->channels;
1799 per_pin->setup = true;
1800 per_pin->mux_idx = mux_idx;
1802 hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
1805 static void hdmi_pcm_reset_pin(struct hdmi_spec *spec,
1806 struct hdmi_spec_per_pin *per_pin)
1808 if (per_pin->pcm_idx >= 0 && per_pin->pcm_idx < spec->pcm_used)
1809 snd_hda_spdif_ctls_unassign(per_pin->codec, per_pin->pcm_idx);
1811 per_pin->chmap_set = false;
1812 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
1814 per_pin->setup = false;
1815 per_pin->channels = 0;
1818 /* update per_pin ELD from the given new ELD;
1819 * setup info frame and notification accordingly
1821 static void update_eld(struct hda_codec *codec,
1822 struct hdmi_spec_per_pin *per_pin,
1823 struct hdmi_eld *eld)
1825 struct hdmi_eld *pin_eld = &per_pin->sink_eld;
1826 struct hdmi_spec *spec = codec->spec;
1827 bool old_eld_valid = pin_eld->eld_valid;
1830 if (spec->dyn_pcm_assign) {
1831 if (eld->eld_valid) {
1832 hdmi_attach_hda_pcm(spec, per_pin);
1833 hdmi_pcm_setup_pin(spec, per_pin);
1835 hdmi_pcm_reset_pin(spec, per_pin);
1836 hdmi_detach_hda_pcm(spec, per_pin);
1841 snd_hdmi_show_eld(codec, &eld->info);
1843 eld_changed = (pin_eld->eld_valid != eld->eld_valid);
1844 if (eld->eld_valid && pin_eld->eld_valid)
1845 if (pin_eld->eld_size != eld->eld_size ||
1846 memcmp(pin_eld->eld_buffer, eld->eld_buffer,
1847 eld->eld_size) != 0)
1850 pin_eld->eld_valid = eld->eld_valid;
1851 pin_eld->eld_size = eld->eld_size;
1853 memcpy(pin_eld->eld_buffer, eld->eld_buffer, eld->eld_size);
1854 pin_eld->info = eld->info;
1857 * Re-setup pin and infoframe. This is needed e.g. when
1858 * - sink is first plugged-in
1859 * - transcoder can change during stream playback on Haswell
1860 * and this can make HW reset converter selection on a pin.
1862 if (eld->eld_valid && !old_eld_valid && per_pin->setup) {
1863 if (is_haswell_plus(codec) || is_valleyview_plus(codec)) {
1864 intel_verify_pin_cvt_connect(codec, per_pin);
1865 intel_not_share_assigned_cvt(codec, per_pin->pin_nid,
1869 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
1873 snd_ctl_notify(codec->card,
1874 SNDRV_CTL_EVENT_MASK_VALUE |
1875 SNDRV_CTL_EVENT_MASK_INFO,
1876 &per_pin->eld_ctl->id);
1879 /* update ELD and jack state via HD-audio verbs */
1880 static bool hdmi_present_sense_via_verbs(struct hdmi_spec_per_pin *per_pin,
1883 struct hda_jack_tbl *jack;
1884 struct hda_codec *codec = per_pin->codec;
1885 struct hdmi_spec *spec = codec->spec;
1886 struct hdmi_eld *eld = &spec->temp_eld;
1887 struct hdmi_eld *pin_eld = &per_pin->sink_eld;
1888 hda_nid_t pin_nid = per_pin->pin_nid;
1890 * Always execute a GetPinSense verb here, even when called from
1891 * hdmi_intrinsic_event; for some NVIDIA HW, the unsolicited
1892 * response's PD bit is not the real PD value, but indicates that
1893 * the real PD value changed. An older version of the HD-audio
1894 * specification worked this way. Hence, we just ignore the data in
1895 * the unsolicited response to avoid custom WARs.
1899 bool do_repoll = false;
1901 snd_hda_power_up_pm(codec);
1902 present = snd_hda_pin_sense(codec, pin_nid);
1904 mutex_lock(&per_pin->lock);
1905 pin_eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
1906 if (pin_eld->monitor_present)
1907 eld->eld_valid = !!(present & AC_PINSENSE_ELDV);
1909 eld->eld_valid = false;
1912 "HDMI status: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
1913 codec->addr, pin_nid, pin_eld->monitor_present, eld->eld_valid);
1915 if (eld->eld_valid) {
1916 if (spec->ops.pin_get_eld(codec, pin_nid, eld->eld_buffer,
1917 &eld->eld_size) < 0)
1918 eld->eld_valid = false;
1920 if (snd_hdmi_parse_eld(codec, &eld->info, eld->eld_buffer,
1922 eld->eld_valid = false;
1924 if (!eld->eld_valid && repoll)
1929 schedule_delayed_work(&per_pin->work, msecs_to_jiffies(300));
1931 update_eld(codec, per_pin, eld);
1933 ret = !repoll || !pin_eld->monitor_present || pin_eld->eld_valid;
1935 jack = snd_hda_jack_tbl_get(codec, pin_nid);
1937 jack->block_report = !ret;
1939 mutex_unlock(&per_pin->lock);
1940 snd_hda_power_down_pm(codec);
1944 /* update ELD and jack state via audio component */
1945 static void sync_eld_via_acomp(struct hda_codec *codec,
1946 struct hdmi_spec_per_pin *per_pin)
1948 struct hdmi_spec *spec = codec->spec;
1949 struct hdmi_eld *eld = &spec->temp_eld;
1950 struct snd_jack *jack = NULL;
1953 mutex_lock(&per_pin->lock);
1954 size = snd_hdac_acomp_get_eld(&codec->bus->core, per_pin->pin_nid,
1955 &eld->monitor_present, eld->eld_buffer,
1960 size = min(size, ELD_MAX_SIZE);
1961 if (snd_hdmi_parse_eld(codec, &eld->info,
1962 eld->eld_buffer, size) < 0)
1967 eld->eld_valid = true;
1968 eld->eld_size = size;
1970 eld->eld_valid = false;
1974 /* pcm_idx >=0 before update_eld() means it is in monitor
1975 * disconnected event. Jack must be fetched before update_eld()
1977 if (per_pin->pcm_idx >= 0)
1978 jack = spec->pcm_rec[per_pin->pcm_idx].jack;
1979 update_eld(codec, per_pin, eld);
1980 if (jack == NULL && per_pin->pcm_idx >= 0)
1981 jack = spec->pcm_rec[per_pin->pcm_idx].jack;
1984 snd_jack_report(jack,
1985 eld->monitor_present ? SND_JACK_AVOUT : 0);
1987 mutex_unlock(&per_pin->lock);
1990 static bool hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll)
1992 struct hda_codec *codec = per_pin->codec;
1993 struct hdmi_spec *spec = codec->spec;
1996 mutex_lock(&spec->pcm_lock);
1997 if (codec_has_acomp(codec)) {
1998 sync_eld_via_acomp(codec, per_pin);
1999 ret = false; /* don't call snd_hda_jack_report_sync() */
2001 ret = hdmi_present_sense_via_verbs(per_pin, repoll);
2003 mutex_unlock(&spec->pcm_lock);
2008 static void hdmi_repoll_eld(struct work_struct *work)
2010 struct hdmi_spec_per_pin *per_pin =
2011 container_of(to_delayed_work(work), struct hdmi_spec_per_pin, work);
2013 if (per_pin->repoll_count++ > 6)
2014 per_pin->repoll_count = 0;
2016 if (hdmi_present_sense(per_pin, per_pin->repoll_count))
2017 snd_hda_jack_report_sync(per_pin->codec);
2020 static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
2023 static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
2025 struct hdmi_spec *spec = codec->spec;
2026 unsigned int caps, config;
2028 struct hdmi_spec_per_pin *per_pin;
2031 caps = snd_hda_query_pin_caps(codec, pin_nid);
2032 if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP)))
2035 config = snd_hda_codec_get_pincfg(codec, pin_nid);
2036 if (get_defcfg_connect(config) == AC_JACK_PORT_NONE)
2039 if (is_haswell_plus(codec))
2040 intel_haswell_fixup_connect_list(codec, pin_nid);
2042 pin_idx = spec->num_pins;
2043 per_pin = snd_array_new(&spec->pins);
2047 per_pin->pin_nid = pin_nid;
2048 per_pin->non_pcm = false;
2049 if (spec->dyn_pcm_assign)
2050 per_pin->pcm_idx = -1;
2052 per_pin->pcm = get_hdmi_pcm(spec, pin_idx);
2053 per_pin->pcm_idx = pin_idx;
2055 per_pin->pin_nid_idx = pin_idx;
2057 err = hdmi_read_pin_conn(codec, pin_idx);
2066 static int hdmi_add_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
2068 struct hdmi_spec *spec = codec->spec;
2069 struct hdmi_spec_per_cvt *per_cvt;
2073 chans = get_wcaps(codec, cvt_nid);
2074 chans = get_wcaps_channels(chans);
2076 per_cvt = snd_array_new(&spec->cvts);
2080 per_cvt->cvt_nid = cvt_nid;
2081 per_cvt->channels_min = 2;
2083 per_cvt->channels_max = chans;
2084 if (chans > spec->channels_max)
2085 spec->channels_max = chans;
2088 err = snd_hda_query_supported_pcm(codec, cvt_nid,
2095 if (spec->num_cvts < ARRAY_SIZE(spec->cvt_nids))
2096 spec->cvt_nids[spec->num_cvts] = cvt_nid;
2102 static int hdmi_parse_codec(struct hda_codec *codec)
2107 nodes = snd_hda_get_sub_nodes(codec, codec->core.afg, &nid);
2108 if (!nid || nodes < 0) {
2109 codec_warn(codec, "HDMI: failed to get afg sub nodes\n");
2113 for (i = 0; i < nodes; i++, nid++) {
2117 caps = get_wcaps(codec, nid);
2118 type = get_wcaps_type(caps);
2120 if (!(caps & AC_WCAP_DIGITAL))
2124 case AC_WID_AUD_OUT:
2125 hdmi_add_cvt(codec, nid);
2128 hdmi_add_pin(codec, nid);
2138 static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
2140 struct hda_spdif_out *spdif;
2143 mutex_lock(&codec->spdif_mutex);
2144 spdif = snd_hda_spdif_out_of_nid(codec, cvt_nid);
2145 non_pcm = !!(spdif->status & IEC958_AES0_NONAUDIO);
2146 mutex_unlock(&codec->spdif_mutex);
2154 static int generic_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2155 struct hda_codec *codec,
2156 unsigned int stream_tag,
2157 unsigned int format,
2158 struct snd_pcm_substream *substream)
2160 hda_nid_t cvt_nid = hinfo->nid;
2161 struct hdmi_spec *spec = codec->spec;
2163 struct hdmi_spec_per_pin *per_pin;
2165 struct snd_pcm_runtime *runtime = substream->runtime;
2170 mutex_lock(&spec->pcm_lock);
2171 pin_idx = hinfo_to_pin_index(codec, hinfo);
2172 if (spec->dyn_pcm_assign && pin_idx < 0) {
2173 /* when dyn_pcm_assign and pcm is not bound to a pin
2174 * skip pin setup and return 0 to make audio playback
2177 intel_not_share_assigned_cvt_nid(codec, 0, cvt_nid);
2178 snd_hda_codec_setup_stream(codec, cvt_nid,
2179 stream_tag, 0, format);
2180 mutex_unlock(&spec->pcm_lock);
2184 if (snd_BUG_ON(pin_idx < 0)) {
2185 mutex_unlock(&spec->pcm_lock);
2188 per_pin = get_pin(spec, pin_idx);
2189 pin_nid = per_pin->pin_nid;
2190 if (is_haswell_plus(codec) || is_valleyview_plus(codec)) {
2191 /* Verify pin:cvt selections to avoid silent audio after S3.
2192 * After S3, the audio driver restores pin:cvt selections
2193 * but this can happen before gfx is ready and such selection
2194 * is overlooked by HW. Thus multiple pins can share a same
2195 * default convertor and mute control will affect each other,
2196 * which can cause a resumed audio playback become silent
2199 intel_verify_pin_cvt_connect(codec, per_pin);
2200 intel_not_share_assigned_cvt(codec, pin_nid, per_pin->mux_idx);
2203 /* Call sync_audio_rate to set the N/CTS/M manually if necessary */
2204 /* Todo: add DP1.2 MST audio support later */
2205 snd_hdac_sync_audio_rate(&codec->bus->core, pin_nid, runtime->rate);
2207 non_pcm = check_non_pcm_per_cvt(codec, cvt_nid);
2208 mutex_lock(&per_pin->lock);
2209 per_pin->channels = substream->runtime->channels;
2210 per_pin->setup = true;
2212 hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
2213 mutex_unlock(&per_pin->lock);
2214 if (spec->dyn_pin_out) {
2215 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
2216 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2217 snd_hda_codec_write(codec, pin_nid, 0,
2218 AC_VERB_SET_PIN_WIDGET_CONTROL,
2222 err = spec->ops.setup_stream(codec, cvt_nid, pin_nid,
2223 stream_tag, format);
2224 mutex_unlock(&spec->pcm_lock);
2228 static int generic_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
2229 struct hda_codec *codec,
2230 struct snd_pcm_substream *substream)
2232 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
2236 static int hdmi_pcm_close(struct hda_pcm_stream *hinfo,
2237 struct hda_codec *codec,
2238 struct snd_pcm_substream *substream)
2240 struct hdmi_spec *spec = codec->spec;
2241 int cvt_idx, pin_idx, pcm_idx;
2242 struct hdmi_spec_per_cvt *per_cvt;
2243 struct hdmi_spec_per_pin *per_pin;
2247 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
2248 if (snd_BUG_ON(pcm_idx < 0))
2250 cvt_idx = cvt_nid_to_cvt_index(codec, hinfo->nid);
2251 if (snd_BUG_ON(cvt_idx < 0))
2253 per_cvt = get_cvt(spec, cvt_idx);
2255 snd_BUG_ON(!per_cvt->assigned);
2256 per_cvt->assigned = 0;
2259 mutex_lock(&spec->pcm_lock);
2260 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
2261 clear_bit(pcm_idx, &spec->pcm_in_use);
2262 pin_idx = hinfo_to_pin_index(codec, hinfo);
2263 if (spec->dyn_pcm_assign && pin_idx < 0) {
2264 mutex_unlock(&spec->pcm_lock);
2268 if (snd_BUG_ON(pin_idx < 0)) {
2269 mutex_unlock(&spec->pcm_lock);
2272 per_pin = get_pin(spec, pin_idx);
2274 if (spec->dyn_pin_out) {
2275 pinctl = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
2276 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2277 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
2278 AC_VERB_SET_PIN_WIDGET_CONTROL,
2282 mutex_lock(&per_pin->lock);
2283 per_pin->chmap_set = false;
2284 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
2286 per_pin->setup = false;
2287 per_pin->channels = 0;
2288 mutex_unlock(&per_pin->lock);
2289 mutex_unlock(&spec->pcm_lock);
2295 static const struct hda_pcm_ops generic_ops = {
2296 .open = hdmi_pcm_open,
2297 .close = hdmi_pcm_close,
2298 .prepare = generic_hdmi_playback_pcm_prepare,
2299 .cleanup = generic_hdmi_playback_pcm_cleanup,
2303 * ALSA API channel-map control callbacks
2305 static int hdmi_chmap_ctl_info(struct snd_kcontrol *kcontrol,
2306 struct snd_ctl_elem_info *uinfo)
2308 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2309 struct hda_codec *codec = info->private_data;
2310 struct hdmi_spec *spec = codec->spec;
2311 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2312 uinfo->count = spec->channels_max;
2313 uinfo->value.integer.min = 0;
2314 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
2318 static int hdmi_chmap_cea_alloc_validate_get_type(struct cea_channel_speaker_allocation *cap,
2321 /* If the speaker allocation matches the channel count, it is OK.*/
2322 if (cap->channels != channels)
2325 /* all channels are remappable freely */
2326 return SNDRV_CTL_TLVT_CHMAP_VAR;
2329 static void hdmi_cea_alloc_to_tlv_chmap(struct cea_channel_speaker_allocation *cap,
2330 unsigned int *chmap, int channels)
2335 for (c = 7; c >= 0; c--) {
2336 int spk = cap->speakers[c];
2340 chmap[count++] = spk_to_chmap(spk);
2343 WARN_ON(count != channels);
2346 static int hdmi_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2347 unsigned int size, unsigned int __user *tlv)
2349 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2350 struct hda_codec *codec = info->private_data;
2351 struct hdmi_spec *spec = codec->spec;
2352 unsigned int __user *dst;
2357 if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
2361 for (chs = 2; chs <= spec->channels_max; chs++) {
2363 struct cea_channel_speaker_allocation *cap;
2364 cap = channel_allocations;
2365 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++, cap++) {
2366 int chs_bytes = chs * 4;
2367 int type = spec->ops.chmap_cea_alloc_validate_get_type(cap, chs);
2368 unsigned int tlv_chmap[8];
2374 if (put_user(type, dst) ||
2375 put_user(chs_bytes, dst + 1))
2380 if (size < chs_bytes)
2384 spec->ops.cea_alloc_to_tlv_chmap(cap, tlv_chmap, chs);
2385 if (copy_to_user(dst, tlv_chmap, chs_bytes))
2390 if (put_user(count, tlv + 1))
2395 static int hdmi_chmap_ctl_get(struct snd_kcontrol *kcontrol,
2396 struct snd_ctl_elem_value *ucontrol)
2398 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2399 struct hda_codec *codec = info->private_data;
2400 struct hdmi_spec *spec = codec->spec;
2401 int pin_idx = kcontrol->private_value;
2402 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2405 for (i = 0; i < ARRAY_SIZE(per_pin->chmap); i++)
2406 ucontrol->value.integer.value[i] = per_pin->chmap[i];
2410 static int hdmi_chmap_ctl_put(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 pin_idx = kcontrol->private_value;
2417 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2418 unsigned int ctl_idx;
2419 struct snd_pcm_substream *substream;
2420 unsigned char chmap[8];
2421 int i, err, ca, prepared = 0;
2423 ctl_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2424 substream = snd_pcm_chmap_substream(info, ctl_idx);
2425 if (!substream || !substream->runtime)
2426 return 0; /* just for avoiding error from alsactl restore */
2427 switch (substream->runtime->status->state) {
2428 case SNDRV_PCM_STATE_OPEN:
2429 case SNDRV_PCM_STATE_SETUP:
2431 case SNDRV_PCM_STATE_PREPARED:
2437 memset(chmap, 0, sizeof(chmap));
2438 for (i = 0; i < ARRAY_SIZE(chmap); i++)
2439 chmap[i] = ucontrol->value.integer.value[i];
2440 if (!memcmp(chmap, per_pin->chmap, sizeof(chmap)))
2442 ca = hdmi_manual_channel_allocation(ARRAY_SIZE(chmap), chmap);
2445 if (spec->ops.chmap_validate) {
2446 err = spec->ops.chmap_validate(ca, ARRAY_SIZE(chmap), chmap);
2450 mutex_lock(&per_pin->lock);
2451 per_pin->chmap_set = true;
2452 memcpy(per_pin->chmap, chmap, sizeof(chmap));
2454 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
2455 mutex_unlock(&per_pin->lock);
2460 static int generic_hdmi_build_pcms(struct hda_codec *codec)
2462 struct hdmi_spec *spec = codec->spec;
2465 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2466 struct hda_pcm *info;
2467 struct hda_pcm_stream *pstr;
2469 info = snd_hda_codec_pcm_new(codec, "HDMI %d", pin_idx);
2473 spec->pcm_rec[pin_idx].pcm = info;
2475 info->pcm_type = HDA_PCM_TYPE_HDMI;
2476 info->own_chmap = true;
2478 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
2479 pstr->substreams = 1;
2480 pstr->ops = generic_ops;
2481 /* other pstr fields are set in open */
2487 static void free_hdmi_jack_priv(struct snd_jack *jack)
2489 struct hdmi_pcm *pcm = jack->private_data;
2494 static int add_hdmi_jack_kctl(struct hda_codec *codec,
2495 struct hdmi_spec *spec,
2499 struct snd_jack *jack;
2502 err = snd_jack_new(codec->card, name, SND_JACK_AVOUT, &jack,
2507 spec->pcm_rec[pcm_idx].jack = jack;
2508 jack->private_data = &spec->pcm_rec[pcm_idx];
2509 jack->private_free = free_hdmi_jack_priv;
2513 static int generic_hdmi_build_jack(struct hda_codec *codec, int pcm_idx)
2515 char hdmi_str[32] = "HDMI/DP";
2516 struct hdmi_spec *spec = codec->spec;
2517 struct hdmi_spec_per_pin *per_pin;
2518 struct hda_jack_tbl *jack;
2519 int pcmdev = get_pcm_rec(spec, pcm_idx)->device;
2524 sprintf(hdmi_str + strlen(hdmi_str), ",pcm=%d", pcmdev);
2526 if (spec->dyn_pcm_assign)
2527 return add_hdmi_jack_kctl(codec, spec, pcm_idx, hdmi_str);
2529 /* for !dyn_pcm_assign, we still use hda_jack for compatibility */
2530 /* if !dyn_pcm_assign, it must be non-MST mode.
2531 * This means pcms and pins are statically mapped.
2532 * And pcm_idx is pin_idx.
2534 per_pin = get_pin(spec, pcm_idx);
2535 phantom_jack = !is_jack_detectable(codec, per_pin->pin_nid);
2537 strncat(hdmi_str, " Phantom",
2538 sizeof(hdmi_str) - strlen(hdmi_str) - 1);
2539 ret = snd_hda_jack_add_kctl(codec, per_pin->pin_nid, hdmi_str,
2543 jack = snd_hda_jack_tbl_get(codec, per_pin->pin_nid);
2546 /* assign jack->jack to pcm_rec[].jack to
2547 * align with dyn_pcm_assign mode
2549 spec->pcm_rec[pcm_idx].jack = jack->jack;
2553 static int generic_hdmi_build_controls(struct hda_codec *codec)
2555 struct hdmi_spec *spec = codec->spec;
2557 int pin_idx, pcm_idx;
2560 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
2561 err = generic_hdmi_build_jack(codec, pcm_idx);
2565 /* create the spdif for each pcm
2566 * pin will be bound when monitor is connected
2568 if (spec->dyn_pcm_assign)
2569 err = snd_hda_create_dig_out_ctls(codec,
2570 0, spec->cvt_nids[0],
2573 struct hdmi_spec_per_pin *per_pin =
2574 get_pin(spec, pcm_idx);
2575 err = snd_hda_create_dig_out_ctls(codec,
2577 per_pin->mux_nids[0],
2582 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
2585 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2586 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2588 /* add control for ELD Bytes */
2589 err = hdmi_create_eld_ctl(codec, pin_idx,
2590 get_pcm_rec(spec, pin_idx)->device);
2595 hdmi_present_sense(per_pin, 0);
2598 /* add channel maps */
2599 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2600 struct hda_pcm *pcm;
2601 struct snd_pcm_chmap *chmap;
2602 struct snd_kcontrol *kctl;
2605 pcm = get_pcm_rec(spec, pin_idx);
2606 if (!pcm || !pcm->pcm)
2608 err = snd_pcm_add_chmap_ctls(pcm->pcm,
2609 SNDRV_PCM_STREAM_PLAYBACK,
2610 NULL, 0, pin_idx, &chmap);
2613 /* override handlers */
2614 chmap->private_data = codec;
2616 for (i = 0; i < kctl->count; i++)
2617 kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_WRITE;
2618 kctl->info = hdmi_chmap_ctl_info;
2619 kctl->get = hdmi_chmap_ctl_get;
2620 kctl->put = hdmi_chmap_ctl_put;
2621 kctl->tlv.c = hdmi_chmap_ctl_tlv;
2627 static int generic_hdmi_init_per_pins(struct hda_codec *codec)
2629 struct hdmi_spec *spec = codec->spec;
2632 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2633 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2635 per_pin->codec = codec;
2636 mutex_init(&per_pin->lock);
2637 INIT_DELAYED_WORK(&per_pin->work, hdmi_repoll_eld);
2638 eld_proc_new(per_pin, pin_idx);
2643 static int generic_hdmi_init(struct hda_codec *codec)
2645 struct hdmi_spec *spec = codec->spec;
2648 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2649 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2650 hda_nid_t pin_nid = per_pin->pin_nid;
2652 hdmi_init_pin(codec, pin_nid);
2653 if (!codec_has_acomp(codec))
2654 snd_hda_jack_detect_enable_callback(codec, pin_nid,
2655 codec->jackpoll_interval > 0 ?
2656 jack_callback : NULL);
2661 static void hdmi_array_init(struct hdmi_spec *spec, int nums)
2663 snd_array_init(&spec->pins, sizeof(struct hdmi_spec_per_pin), nums);
2664 snd_array_init(&spec->cvts, sizeof(struct hdmi_spec_per_cvt), nums);
2667 static void hdmi_array_free(struct hdmi_spec *spec)
2669 snd_array_free(&spec->pins);
2670 snd_array_free(&spec->cvts);
2673 static void generic_hdmi_free(struct hda_codec *codec)
2675 struct hdmi_spec *spec = codec->spec;
2676 int pin_idx, pcm_idx;
2678 if (codec_has_acomp(codec))
2679 snd_hdac_i915_register_notifier(NULL);
2681 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2682 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2683 cancel_delayed_work_sync(&per_pin->work);
2684 eld_proc_free(per_pin);
2687 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) {
2688 if (spec->pcm_rec[pcm_idx].jack == NULL)
2690 if (spec->dyn_pcm_assign)
2691 snd_device_free(codec->card,
2692 spec->pcm_rec[pcm_idx].jack);
2694 spec->pcm_rec[pcm_idx].jack = NULL;
2697 if (spec->i915_bound)
2698 snd_hdac_i915_exit(&codec->bus->core);
2699 hdmi_array_free(spec);
2704 static int generic_hdmi_resume(struct hda_codec *codec)
2706 struct hdmi_spec *spec = codec->spec;
2709 codec->patch_ops.init(codec);
2710 regcache_sync(codec->core.regmap);
2712 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2713 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2714 hdmi_present_sense(per_pin, 1);
2720 static const struct hda_codec_ops generic_hdmi_patch_ops = {
2721 .init = generic_hdmi_init,
2722 .free = generic_hdmi_free,
2723 .build_pcms = generic_hdmi_build_pcms,
2724 .build_controls = generic_hdmi_build_controls,
2725 .unsol_event = hdmi_unsol_event,
2727 .resume = generic_hdmi_resume,
2731 static const struct hdmi_ops generic_standard_hdmi_ops = {
2732 .pin_get_eld = snd_hdmi_get_eld,
2733 .pin_get_slot_channel = hdmi_pin_get_slot_channel,
2734 .pin_set_slot_channel = hdmi_pin_set_slot_channel,
2735 .pin_setup_infoframe = hdmi_pin_setup_infoframe,
2736 .pin_hbr_setup = hdmi_pin_hbr_setup,
2737 .setup_stream = hdmi_setup_stream,
2738 .chmap_cea_alloc_validate_get_type = hdmi_chmap_cea_alloc_validate_get_type,
2739 .cea_alloc_to_tlv_chmap = hdmi_cea_alloc_to_tlv_chmap,
2743 static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
2746 struct hdmi_spec *spec = codec->spec;
2750 nconns = snd_hda_get_connections(codec, nid, conns, ARRAY_SIZE(conns));
2751 if (nconns == spec->num_cvts &&
2752 !memcmp(conns, spec->cvt_nids, spec->num_cvts * sizeof(hda_nid_t)))
2755 /* override pins connection list */
2756 codec_dbg(codec, "hdmi: haswell: override pin connection 0x%x\n", nid);
2757 snd_hda_override_conn_list(codec, nid, spec->num_cvts, spec->cvt_nids);
2760 #define INTEL_VENDOR_NID 0x08
2761 #define INTEL_GET_VENDOR_VERB 0xf81
2762 #define INTEL_SET_VENDOR_VERB 0x781
2763 #define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
2764 #define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
2766 static void intel_haswell_enable_all_pins(struct hda_codec *codec,
2769 unsigned int vendor_param;
2771 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
2772 INTEL_GET_VENDOR_VERB, 0);
2773 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
2776 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
2777 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
2778 INTEL_SET_VENDOR_VERB, vendor_param);
2779 if (vendor_param == -1)
2783 snd_hda_codec_update_widgets(codec);
2786 static void intel_haswell_fixup_enable_dp12(struct hda_codec *codec)
2788 unsigned int vendor_param;
2790 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
2791 INTEL_GET_VENDOR_VERB, 0);
2792 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
2795 /* enable DP1.2 mode */
2796 vendor_param |= INTEL_EN_DP12;
2797 snd_hdac_regmap_add_vendor_verb(&codec->core, INTEL_SET_VENDOR_VERB);
2798 snd_hda_codec_write_cache(codec, INTEL_VENDOR_NID, 0,
2799 INTEL_SET_VENDOR_VERB, vendor_param);
2802 /* Haswell needs to re-issue the vendor-specific verbs before turning to D0.
2803 * Otherwise you may get severe h/w communication errors.
2805 static void haswell_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2806 unsigned int power_state)
2808 if (power_state == AC_PWRST_D0) {
2809 intel_haswell_enable_all_pins(codec, false);
2810 intel_haswell_fixup_enable_dp12(codec);
2813 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE, power_state);
2814 snd_hda_codec_set_power_to_all(codec, fg, power_state);
2817 static void intel_pin_eld_notify(void *audio_ptr, int port)
2819 struct hda_codec *codec = audio_ptr;
2820 int pin_nid = port + 0x04;
2822 /* skip notification during system suspend (but not in runtime PM);
2823 * the state will be updated at resume
2825 if (snd_power_get_state(codec->card) != SNDRV_CTL_POWER_D0)
2827 /* ditto during suspend/resume process itself */
2828 if (atomic_read(&(codec)->core.in_pm))
2831 check_presence_and_report(codec, pin_nid);
2834 static int patch_generic_hdmi(struct hda_codec *codec)
2836 struct hdmi_spec *spec;
2838 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2842 spec->ops = generic_standard_hdmi_ops;
2843 mutex_init(&spec->pcm_lock);
2845 hdmi_array_init(spec, 4);
2847 /* Try to bind with i915 for any Intel codecs (if not done yet) */
2848 if (!codec_has_acomp(codec) &&
2849 (codec->core.vendor_id >> 16) == 0x8086)
2850 if (!snd_hdac_i915_init(&codec->bus->core))
2851 spec->i915_bound = true;
2853 if (is_haswell_plus(codec)) {
2854 intel_haswell_enable_all_pins(codec, true);
2855 intel_haswell_fixup_enable_dp12(codec);
2858 /* For Valleyview/Cherryview, only the display codec is in the display
2859 * power well and can use link_power ops to request/release the power.
2860 * For Haswell/Broadwell, the controller is also in the power well and
2861 * can cover the codec power request, and so need not set this flag.
2862 * For previous platforms, there is no such power well feature.
2864 if (is_valleyview_plus(codec) || is_skylake(codec) ||
2866 codec->core.link_power_control = 1;
2868 if (codec_has_acomp(codec)) {
2869 codec->depop_delay = 0;
2870 spec->i915_audio_ops.audio_ptr = codec;
2871 spec->i915_audio_ops.pin_eld_notify = intel_pin_eld_notify;
2872 snd_hdac_i915_register_notifier(&spec->i915_audio_ops);
2875 if (hdmi_parse_codec(codec) < 0) {
2876 if (spec->i915_bound)
2877 snd_hdac_i915_exit(&codec->bus->core);
2882 codec->patch_ops = generic_hdmi_patch_ops;
2883 if (is_haswell_plus(codec)) {
2884 codec->patch_ops.set_power_state = haswell_set_power_state;
2885 codec->dp_mst = true;
2888 /* Enable runtime pm for HDMI audio codec of HSW/BDW/SKL/BYT/BSW */
2889 if (is_haswell_plus(codec) || is_valleyview_plus(codec))
2890 codec->auto_runtime_pm = 1;
2892 generic_hdmi_init_per_pins(codec);
2894 init_channel_allocations();
2896 WARN_ON(spec->dyn_pcm_assign && !codec_has_acomp(codec));
2901 * Shared non-generic implementations
2904 static int simple_playback_build_pcms(struct hda_codec *codec)
2906 struct hdmi_spec *spec = codec->spec;
2907 struct hda_pcm *info;
2909 struct hda_pcm_stream *pstr;
2910 struct hdmi_spec_per_cvt *per_cvt;
2912 per_cvt = get_cvt(spec, 0);
2913 chans = get_wcaps(codec, per_cvt->cvt_nid);
2914 chans = get_wcaps_channels(chans);
2916 info = snd_hda_codec_pcm_new(codec, "HDMI 0");
2919 spec->pcm_rec[0].pcm = info;
2920 info->pcm_type = HDA_PCM_TYPE_HDMI;
2921 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
2922 *pstr = spec->pcm_playback;
2923 pstr->nid = per_cvt->cvt_nid;
2924 if (pstr->channels_max <= 2 && chans && chans <= 16)
2925 pstr->channels_max = chans;
2930 /* unsolicited event for jack sensing */
2931 static void simple_hdmi_unsol_event(struct hda_codec *codec,
2934 snd_hda_jack_set_dirty_all(codec);
2935 snd_hda_jack_report_sync(codec);
2938 /* generic_hdmi_build_jack can be used for simple_hdmi, too,
2939 * as long as spec->pins[] is set correctly
2941 #define simple_hdmi_build_jack generic_hdmi_build_jack
2943 static int simple_playback_build_controls(struct hda_codec *codec)
2945 struct hdmi_spec *spec = codec->spec;
2946 struct hdmi_spec_per_cvt *per_cvt;
2949 per_cvt = get_cvt(spec, 0);
2950 err = snd_hda_create_dig_out_ctls(codec, per_cvt->cvt_nid,
2955 return simple_hdmi_build_jack(codec, 0);
2958 static int simple_playback_init(struct hda_codec *codec)
2960 struct hdmi_spec *spec = codec->spec;
2961 struct hdmi_spec_per_pin *per_pin = get_pin(spec, 0);
2962 hda_nid_t pin = per_pin->pin_nid;
2964 snd_hda_codec_write(codec, pin, 0,
2965 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
2966 /* some codecs require to unmute the pin */
2967 if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
2968 snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
2970 snd_hda_jack_detect_enable(codec, pin);
2974 static void simple_playback_free(struct hda_codec *codec)
2976 struct hdmi_spec *spec = codec->spec;
2978 hdmi_array_free(spec);
2983 * Nvidia specific implementations
2986 #define Nv_VERB_SET_Channel_Allocation 0xF79
2987 #define Nv_VERB_SET_Info_Frame_Checksum 0xF7A
2988 #define Nv_VERB_SET_Audio_Protection_On 0xF98
2989 #define Nv_VERB_SET_Audio_Protection_Off 0xF99
2991 #define nvhdmi_master_con_nid_7x 0x04
2992 #define nvhdmi_master_pin_nid_7x 0x05
2994 static const hda_nid_t nvhdmi_con_nids_7x[4] = {
2995 /*front, rear, clfe, rear_surr */
2999 static const struct hda_verb nvhdmi_basic_init_7x_2ch[] = {
3000 /* set audio protect on */
3001 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
3002 /* enable digital output on pin widget */
3003 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3007 static const struct hda_verb nvhdmi_basic_init_7x_8ch[] = {
3008 /* set audio protect on */
3009 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
3010 /* enable digital output on pin widget */
3011 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3012 { 0x7, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3013 { 0x9, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3014 { 0xb, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3015 { 0xd, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
3019 #ifdef LIMITED_RATE_FMT_SUPPORT
3020 /* support only the safe format and rate */
3021 #define SUPPORTED_RATES SNDRV_PCM_RATE_48000
3022 #define SUPPORTED_MAXBPS 16
3023 #define SUPPORTED_FORMATS SNDRV_PCM_FMTBIT_S16_LE
3025 /* support all rates and formats */
3026 #define SUPPORTED_RATES \
3027 (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
3028 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
3029 SNDRV_PCM_RATE_192000)
3030 #define SUPPORTED_MAXBPS 24
3031 #define SUPPORTED_FORMATS \
3032 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
3035 static int nvhdmi_7x_init_2ch(struct hda_codec *codec)
3037 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_2ch);
3041 static int nvhdmi_7x_init_8ch(struct hda_codec *codec)
3043 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_8ch);
3047 static unsigned int channels_2_6_8[] = {
3051 static unsigned int channels_2_8[] = {
3055 static struct snd_pcm_hw_constraint_list hw_constraints_2_6_8_channels = {
3056 .count = ARRAY_SIZE(channels_2_6_8),
3057 .list = channels_2_6_8,
3061 static struct snd_pcm_hw_constraint_list hw_constraints_2_8_channels = {
3062 .count = ARRAY_SIZE(channels_2_8),
3063 .list = channels_2_8,
3067 static int simple_playback_pcm_open(struct hda_pcm_stream *hinfo,
3068 struct hda_codec *codec,
3069 struct snd_pcm_substream *substream)
3071 struct hdmi_spec *spec = codec->spec;
3072 struct snd_pcm_hw_constraint_list *hw_constraints_channels = NULL;
3074 switch (codec->preset->vendor_id) {
3079 hw_constraints_channels = &hw_constraints_2_8_channels;
3082 hw_constraints_channels = &hw_constraints_2_6_8_channels;
3088 if (hw_constraints_channels != NULL) {
3089 snd_pcm_hw_constraint_list(substream->runtime, 0,
3090 SNDRV_PCM_HW_PARAM_CHANNELS,
3091 hw_constraints_channels);
3093 snd_pcm_hw_constraint_step(substream->runtime, 0,
3094 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
3097 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3100 static int simple_playback_pcm_close(struct hda_pcm_stream *hinfo,
3101 struct hda_codec *codec,
3102 struct snd_pcm_substream *substream)
3104 struct hdmi_spec *spec = codec->spec;
3105 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3108 static int simple_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3109 struct hda_codec *codec,
3110 unsigned int stream_tag,
3111 unsigned int format,
3112 struct snd_pcm_substream *substream)
3114 struct hdmi_spec *spec = codec->spec;
3115 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3116 stream_tag, format, substream);
3119 static const struct hda_pcm_stream simple_pcm_playback = {
3124 .open = simple_playback_pcm_open,
3125 .close = simple_playback_pcm_close,
3126 .prepare = simple_playback_pcm_prepare
3130 static const struct hda_codec_ops simple_hdmi_patch_ops = {
3131 .build_controls = simple_playback_build_controls,
3132 .build_pcms = simple_playback_build_pcms,
3133 .init = simple_playback_init,
3134 .free = simple_playback_free,
3135 .unsol_event = simple_hdmi_unsol_event,
3138 static int patch_simple_hdmi(struct hda_codec *codec,
3139 hda_nid_t cvt_nid, hda_nid_t pin_nid)
3141 struct hdmi_spec *spec;
3142 struct hdmi_spec_per_cvt *per_cvt;
3143 struct hdmi_spec_per_pin *per_pin;
3145 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
3150 hdmi_array_init(spec, 1);
3152 spec->multiout.num_dacs = 0; /* no analog */
3153 spec->multiout.max_channels = 2;
3154 spec->multiout.dig_out_nid = cvt_nid;
3157 per_pin = snd_array_new(&spec->pins);
3158 per_cvt = snd_array_new(&spec->cvts);
3159 if (!per_pin || !per_cvt) {
3160 simple_playback_free(codec);
3163 per_cvt->cvt_nid = cvt_nid;
3164 per_pin->pin_nid = pin_nid;
3165 spec->pcm_playback = simple_pcm_playback;
3167 codec->patch_ops = simple_hdmi_patch_ops;
3172 static void nvhdmi_8ch_7x_set_info_frame_parameters(struct hda_codec *codec,
3175 unsigned int chanmask;
3176 int chan = channels ? (channels - 1) : 1;
3195 /* Set the audio infoframe channel allocation and checksum fields. The
3196 * channel count is computed implicitly by the hardware. */
3197 snd_hda_codec_write(codec, 0x1, 0,
3198 Nv_VERB_SET_Channel_Allocation, chanmask);
3200 snd_hda_codec_write(codec, 0x1, 0,
3201 Nv_VERB_SET_Info_Frame_Checksum,
3202 (0x71 - chan - chanmask));
3205 static int nvhdmi_8ch_7x_pcm_close(struct hda_pcm_stream *hinfo,
3206 struct hda_codec *codec,
3207 struct snd_pcm_substream *substream)
3209 struct hdmi_spec *spec = codec->spec;
3212 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x,
3213 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
3214 for (i = 0; i < 4; i++) {
3215 /* set the stream id */
3216 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
3217 AC_VERB_SET_CHANNEL_STREAMID, 0);
3218 /* set the stream format */
3219 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
3220 AC_VERB_SET_STREAM_FORMAT, 0);
3223 /* The audio hardware sends a channel count of 0x7 (8ch) when all the
3224 * streams are disabled. */
3225 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
3227 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3230 static int nvhdmi_8ch_7x_pcm_prepare(struct hda_pcm_stream *hinfo,
3231 struct hda_codec *codec,
3232 unsigned int stream_tag,
3233 unsigned int format,
3234 struct snd_pcm_substream *substream)
3237 unsigned int dataDCC2, channel_id;
3239 struct hdmi_spec *spec = codec->spec;
3240 struct hda_spdif_out *spdif;
3241 struct hdmi_spec_per_cvt *per_cvt;
3243 mutex_lock(&codec->spdif_mutex);
3244 per_cvt = get_cvt(spec, 0);
3245 spdif = snd_hda_spdif_out_of_nid(codec, per_cvt->cvt_nid);
3247 chs = substream->runtime->channels;
3251 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
3252 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
3253 snd_hda_codec_write(codec,
3254 nvhdmi_master_con_nid_7x,
3256 AC_VERB_SET_DIGI_CONVERT_1,
3257 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
3259 /* set the stream id */
3260 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
3261 AC_VERB_SET_CHANNEL_STREAMID, (stream_tag << 4) | 0x0);
3263 /* set the stream format */
3264 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
3265 AC_VERB_SET_STREAM_FORMAT, format);
3267 /* turn on again (if needed) */
3268 /* enable and set the channel status audio/data flag */
3269 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE)) {
3270 snd_hda_codec_write(codec,
3271 nvhdmi_master_con_nid_7x,
3273 AC_VERB_SET_DIGI_CONVERT_1,
3274 spdif->ctls & 0xff);
3275 snd_hda_codec_write(codec,
3276 nvhdmi_master_con_nid_7x,
3278 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
3281 for (i = 0; i < 4; i++) {
3287 /* turn off SPDIF once;
3288 *otherwise the IEC958 bits won't be updated
3290 if (codec->spdif_status_reset &&
3291 (spdif->ctls & AC_DIG1_ENABLE))
3292 snd_hda_codec_write(codec,
3293 nvhdmi_con_nids_7x[i],
3295 AC_VERB_SET_DIGI_CONVERT_1,
3296 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
3297 /* set the stream id */
3298 snd_hda_codec_write(codec,
3299 nvhdmi_con_nids_7x[i],
3301 AC_VERB_SET_CHANNEL_STREAMID,
3302 (stream_tag << 4) | channel_id);
3303 /* set the stream format */
3304 snd_hda_codec_write(codec,
3305 nvhdmi_con_nids_7x[i],
3307 AC_VERB_SET_STREAM_FORMAT,
3309 /* turn on again (if needed) */
3310 /* enable and set the channel status audio/data flag */
3311 if (codec->spdif_status_reset &&
3312 (spdif->ctls & AC_DIG1_ENABLE)) {
3313 snd_hda_codec_write(codec,
3314 nvhdmi_con_nids_7x[i],
3316 AC_VERB_SET_DIGI_CONVERT_1,
3317 spdif->ctls & 0xff);
3318 snd_hda_codec_write(codec,
3319 nvhdmi_con_nids_7x[i],
3321 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
3325 nvhdmi_8ch_7x_set_info_frame_parameters(codec, chs);
3327 mutex_unlock(&codec->spdif_mutex);
3331 static const struct hda_pcm_stream nvhdmi_pcm_playback_8ch_7x = {
3335 .nid = nvhdmi_master_con_nid_7x,
3336 .rates = SUPPORTED_RATES,
3337 .maxbps = SUPPORTED_MAXBPS,
3338 .formats = SUPPORTED_FORMATS,
3340 .open = simple_playback_pcm_open,
3341 .close = nvhdmi_8ch_7x_pcm_close,
3342 .prepare = nvhdmi_8ch_7x_pcm_prepare
3346 static int patch_nvhdmi_2ch(struct hda_codec *codec)
3348 struct hdmi_spec *spec;
3349 int err = patch_simple_hdmi(codec, nvhdmi_master_con_nid_7x,
3350 nvhdmi_master_pin_nid_7x);
3354 codec->patch_ops.init = nvhdmi_7x_init_2ch;
3355 /* override the PCM rates, etc, as the codec doesn't give full list */
3357 spec->pcm_playback.rates = SUPPORTED_RATES;
3358 spec->pcm_playback.maxbps = SUPPORTED_MAXBPS;
3359 spec->pcm_playback.formats = SUPPORTED_FORMATS;
3363 static int nvhdmi_7x_8ch_build_pcms(struct hda_codec *codec)
3365 struct hdmi_spec *spec = codec->spec;
3366 int err = simple_playback_build_pcms(codec);
3368 struct hda_pcm *info = get_pcm_rec(spec, 0);
3369 info->own_chmap = true;
3374 static int nvhdmi_7x_8ch_build_controls(struct hda_codec *codec)
3376 struct hdmi_spec *spec = codec->spec;
3377 struct hda_pcm *info;
3378 struct snd_pcm_chmap *chmap;
3381 err = simple_playback_build_controls(codec);
3385 /* add channel maps */
3386 info = get_pcm_rec(spec, 0);
3387 err = snd_pcm_add_chmap_ctls(info->pcm,
3388 SNDRV_PCM_STREAM_PLAYBACK,
3389 snd_pcm_alt_chmaps, 8, 0, &chmap);
3392 switch (codec->preset->vendor_id) {
3397 chmap->channel_mask = (1U << 2) | (1U << 8);
3400 chmap->channel_mask = (1U << 2) | (1U << 6) | (1U << 8);
3405 static int patch_nvhdmi_8ch_7x(struct hda_codec *codec)
3407 struct hdmi_spec *spec;
3408 int err = patch_nvhdmi_2ch(codec);
3412 spec->multiout.max_channels = 8;
3413 spec->pcm_playback = nvhdmi_pcm_playback_8ch_7x;
3414 codec->patch_ops.init = nvhdmi_7x_init_8ch;
3415 codec->patch_ops.build_pcms = nvhdmi_7x_8ch_build_pcms;
3416 codec->patch_ops.build_controls = nvhdmi_7x_8ch_build_controls;
3418 /* Initialize the audio infoframe channel mask and checksum to something
3420 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
3426 * NVIDIA codecs ignore ASP mapping for 2ch - confirmed on:
3430 static int nvhdmi_chmap_cea_alloc_validate_get_type(struct cea_channel_speaker_allocation *cap,
3433 if (cap->ca_index == 0x00 && channels == 2)
3434 return SNDRV_CTL_TLVT_CHMAP_FIXED;
3436 return hdmi_chmap_cea_alloc_validate_get_type(cap, channels);
3439 static int nvhdmi_chmap_validate(int ca, int chs, unsigned char *map)
3441 if (ca == 0x00 && (map[0] != SNDRV_CHMAP_FL || map[1] != SNDRV_CHMAP_FR))
3447 static int patch_nvhdmi(struct hda_codec *codec)
3449 struct hdmi_spec *spec;
3452 err = patch_generic_hdmi(codec);
3457 spec->dyn_pin_out = true;
3459 spec->ops.chmap_cea_alloc_validate_get_type =
3460 nvhdmi_chmap_cea_alloc_validate_get_type;
3461 spec->ops.chmap_validate = nvhdmi_chmap_validate;
3467 * The HDA codec on NVIDIA Tegra contains two scratch registers that are
3468 * accessed using vendor-defined verbs. These registers can be used for
3469 * interoperability between the HDA and HDMI drivers.
3472 /* Audio Function Group node */
3473 #define NVIDIA_AFG_NID 0x01
3476 * The SCRATCH0 register is used to notify the HDMI codec of changes in audio
3477 * format. On Tegra, bit 31 is used as a trigger that causes an interrupt to
3478 * be raised in the HDMI codec. The remainder of the bits is arbitrary. This
3479 * implementation stores the HDA format (see AC_FMT_*) in bits [15:0] and an
3480 * additional bit (at position 30) to signal the validity of the format.
3482 * | 31 | 30 | 29 16 | 15 0 |
3483 * +---------+-------+--------+--------+
3484 * | TRIGGER | VALID | UNUSED | FORMAT |
3485 * +-----------------------------------|
3487 * Note that for the trigger bit to take effect it needs to change value
3488 * (i.e. it needs to be toggled).
3490 #define NVIDIA_GET_SCRATCH0 0xfa6
3491 #define NVIDIA_SET_SCRATCH0_BYTE0 0xfa7
3492 #define NVIDIA_SET_SCRATCH0_BYTE1 0xfa8
3493 #define NVIDIA_SET_SCRATCH0_BYTE2 0xfa9
3494 #define NVIDIA_SET_SCRATCH0_BYTE3 0xfaa
3495 #define NVIDIA_SCRATCH_TRIGGER (1 << 7)
3496 #define NVIDIA_SCRATCH_VALID (1 << 6)
3498 #define NVIDIA_GET_SCRATCH1 0xfab
3499 #define NVIDIA_SET_SCRATCH1_BYTE0 0xfac
3500 #define NVIDIA_SET_SCRATCH1_BYTE1 0xfad
3501 #define NVIDIA_SET_SCRATCH1_BYTE2 0xfae
3502 #define NVIDIA_SET_SCRATCH1_BYTE3 0xfaf
3505 * The format parameter is the HDA audio format (see AC_FMT_*). If set to 0,
3506 * the format is invalidated so that the HDMI codec can be disabled.
3508 static void tegra_hdmi_set_format(struct hda_codec *codec, unsigned int format)
3512 /* bits [31:30] contain the trigger and valid bits */
3513 value = snd_hda_codec_read(codec, NVIDIA_AFG_NID, 0,
3514 NVIDIA_GET_SCRATCH0, 0);
3515 value = (value >> 24) & 0xff;
3517 /* bits [15:0] are used to store the HDA format */
3518 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3519 NVIDIA_SET_SCRATCH0_BYTE0,
3520 (format >> 0) & 0xff);
3521 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3522 NVIDIA_SET_SCRATCH0_BYTE1,
3523 (format >> 8) & 0xff);
3525 /* bits [16:24] are unused */
3526 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3527 NVIDIA_SET_SCRATCH0_BYTE2, 0);
3530 * Bit 30 signals that the data is valid and hence that HDMI audio can
3534 value &= ~NVIDIA_SCRATCH_VALID;
3536 value |= NVIDIA_SCRATCH_VALID;
3539 * Whenever the trigger bit is toggled, an interrupt is raised in the
3540 * HDMI codec. The HDMI driver will use that as trigger to update its
3543 value ^= NVIDIA_SCRATCH_TRIGGER;
3545 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3546 NVIDIA_SET_SCRATCH0_BYTE3, value);
3549 static int tegra_hdmi_pcm_prepare(struct hda_pcm_stream *hinfo,
3550 struct hda_codec *codec,
3551 unsigned int stream_tag,
3552 unsigned int format,
3553 struct snd_pcm_substream *substream)
3557 err = generic_hdmi_playback_pcm_prepare(hinfo, codec, stream_tag,
3562 /* notify the HDMI codec of the format change */
3563 tegra_hdmi_set_format(codec, format);
3568 static int tegra_hdmi_pcm_cleanup(struct hda_pcm_stream *hinfo,
3569 struct hda_codec *codec,
3570 struct snd_pcm_substream *substream)
3572 /* invalidate the format in the HDMI codec */
3573 tegra_hdmi_set_format(codec, 0);
3575 return generic_hdmi_playback_pcm_cleanup(hinfo, codec, substream);
3578 static struct hda_pcm *hda_find_pcm_by_type(struct hda_codec *codec, int type)
3580 struct hdmi_spec *spec = codec->spec;
3583 for (i = 0; i < spec->num_pins; i++) {
3584 struct hda_pcm *pcm = get_pcm_rec(spec, i);
3586 if (pcm->pcm_type == type)
3593 static int tegra_hdmi_build_pcms(struct hda_codec *codec)
3595 struct hda_pcm_stream *stream;
3596 struct hda_pcm *pcm;
3599 err = generic_hdmi_build_pcms(codec);
3603 pcm = hda_find_pcm_by_type(codec, HDA_PCM_TYPE_HDMI);
3608 * Override ->prepare() and ->cleanup() operations to notify the HDMI
3609 * codec about format changes.
3611 stream = &pcm->stream[SNDRV_PCM_STREAM_PLAYBACK];
3612 stream->ops.prepare = tegra_hdmi_pcm_prepare;
3613 stream->ops.cleanup = tegra_hdmi_pcm_cleanup;
3618 static int patch_tegra_hdmi(struct hda_codec *codec)
3622 err = patch_generic_hdmi(codec);
3626 codec->patch_ops.build_pcms = tegra_hdmi_build_pcms;
3632 * ATI/AMD-specific implementations
3635 #define is_amdhdmi_rev3_or_later(codec) \
3636 ((codec)->core.vendor_id == 0x1002aa01 && \
3637 ((codec)->core.revision_id & 0xff00) >= 0x0300)
3638 #define has_amd_full_remap_support(codec) is_amdhdmi_rev3_or_later(codec)
3640 /* ATI/AMD specific HDA pin verbs, see the AMD HDA Verbs specification */
3641 #define ATI_VERB_SET_CHANNEL_ALLOCATION 0x771
3642 #define ATI_VERB_SET_DOWNMIX_INFO 0x772
3643 #define ATI_VERB_SET_MULTICHANNEL_01 0x777
3644 #define ATI_VERB_SET_MULTICHANNEL_23 0x778
3645 #define ATI_VERB_SET_MULTICHANNEL_45 0x779
3646 #define ATI_VERB_SET_MULTICHANNEL_67 0x77a
3647 #define ATI_VERB_SET_HBR_CONTROL 0x77c
3648 #define ATI_VERB_SET_MULTICHANNEL_1 0x785
3649 #define ATI_VERB_SET_MULTICHANNEL_3 0x786
3650 #define ATI_VERB_SET_MULTICHANNEL_5 0x787
3651 #define ATI_VERB_SET_MULTICHANNEL_7 0x788
3652 #define ATI_VERB_SET_MULTICHANNEL_MODE 0x789
3653 #define ATI_VERB_GET_CHANNEL_ALLOCATION 0xf71
3654 #define ATI_VERB_GET_DOWNMIX_INFO 0xf72
3655 #define ATI_VERB_GET_MULTICHANNEL_01 0xf77
3656 #define ATI_VERB_GET_MULTICHANNEL_23 0xf78
3657 #define ATI_VERB_GET_MULTICHANNEL_45 0xf79
3658 #define ATI_VERB_GET_MULTICHANNEL_67 0xf7a
3659 #define ATI_VERB_GET_HBR_CONTROL 0xf7c
3660 #define ATI_VERB_GET_MULTICHANNEL_1 0xf85
3661 #define ATI_VERB_GET_MULTICHANNEL_3 0xf86
3662 #define ATI_VERB_GET_MULTICHANNEL_5 0xf87
3663 #define ATI_VERB_GET_MULTICHANNEL_7 0xf88
3664 #define ATI_VERB_GET_MULTICHANNEL_MODE 0xf89
3666 /* AMD specific HDA cvt verbs */
3667 #define ATI_VERB_SET_RAMP_RATE 0x770
3668 #define ATI_VERB_GET_RAMP_RATE 0xf70
3670 #define ATI_OUT_ENABLE 0x1
3672 #define ATI_MULTICHANNEL_MODE_PAIRED 0
3673 #define ATI_MULTICHANNEL_MODE_SINGLE 1
3675 #define ATI_HBR_CAPABLE 0x01
3676 #define ATI_HBR_ENABLE 0x10
3678 static int atihdmi_pin_get_eld(struct hda_codec *codec, hda_nid_t nid,
3679 unsigned char *buf, int *eld_size)
3681 /* call hda_eld.c ATI/AMD-specific function */
3682 return snd_hdmi_get_eld_ati(codec, nid, buf, eld_size,
3683 is_amdhdmi_rev3_or_later(codec));
3686 static void atihdmi_pin_setup_infoframe(struct hda_codec *codec, hda_nid_t pin_nid, int ca,
3687 int active_channels, int conn_type)
3689 snd_hda_codec_write(codec, pin_nid, 0, ATI_VERB_SET_CHANNEL_ALLOCATION, ca);
3692 static int atihdmi_paired_swap_fc_lfe(int pos)
3695 * ATI/AMD have automatic FC/LFE swap built-in
3696 * when in pairwise mapping mode.
3700 /* see channel_allocations[].speakers[] */
3709 static int atihdmi_paired_chmap_validate(int ca, int chs, unsigned char *map)
3711 struct cea_channel_speaker_allocation *cap;
3714 /* check that only channel pairs need to be remapped on old pre-rev3 ATI/AMD */
3716 cap = &channel_allocations[get_channel_allocation_order(ca)];
3717 for (i = 0; i < chs; ++i) {
3718 int mask = to_spk_mask(map[i]);
3720 bool companion_ok = false;
3725 for (j = 0 + i % 2; j < 8; j += 2) {
3726 int chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j);
3727 if (cap->speakers[chan_idx] == mask) {
3728 /* channel is in a supported position */
3731 if (i % 2 == 0 && i + 1 < chs) {
3732 /* even channel, check the odd companion */
3733 int comp_chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j + 1);
3734 int comp_mask_req = to_spk_mask(map[i+1]);
3735 int comp_mask_act = cap->speakers[comp_chan_idx];
3737 if (comp_mask_req == comp_mask_act)
3738 companion_ok = true;
3750 i++; /* companion channel already checked */
3756 static int atihdmi_pin_set_slot_channel(struct hda_codec *codec, hda_nid_t pin_nid,
3757 int hdmi_slot, int stream_channel)
3760 int ati_channel_setup = 0;
3765 if (!has_amd_full_remap_support(codec)) {
3766 hdmi_slot = atihdmi_paired_swap_fc_lfe(hdmi_slot);
3768 /* In case this is an odd slot but without stream channel, do not
3769 * disable the slot since the corresponding even slot could have a
3770 * channel. In case neither have a channel, the slot pair will be
3771 * disabled when this function is called for the even slot. */
3772 if (hdmi_slot % 2 != 0 && stream_channel == 0xf)
3775 hdmi_slot -= hdmi_slot % 2;
3777 if (stream_channel != 0xf)
3778 stream_channel -= stream_channel % 2;
3781 verb = ATI_VERB_SET_MULTICHANNEL_01 + hdmi_slot/2 + (hdmi_slot % 2) * 0x00e;
3783 /* ati_channel_setup format: [7..4] = stream_channel_id, [1] = mute, [0] = enable */
3785 if (stream_channel != 0xf)
3786 ati_channel_setup = (stream_channel << 4) | ATI_OUT_ENABLE;
3788 return snd_hda_codec_write(codec, pin_nid, 0, verb, ati_channel_setup);
3791 static int atihdmi_pin_get_slot_channel(struct hda_codec *codec, hda_nid_t pin_nid,
3794 bool was_odd = false;
3795 int ati_asp_slot = asp_slot;
3797 int ati_channel_setup;
3802 if (!has_amd_full_remap_support(codec)) {
3803 ati_asp_slot = atihdmi_paired_swap_fc_lfe(asp_slot);
3804 if (ati_asp_slot % 2 != 0) {
3810 verb = ATI_VERB_GET_MULTICHANNEL_01 + ati_asp_slot/2 + (ati_asp_slot % 2) * 0x00e;
3812 ati_channel_setup = snd_hda_codec_read(codec, pin_nid, 0, verb, 0);
3814 if (!(ati_channel_setup & ATI_OUT_ENABLE))
3817 return ((ati_channel_setup & 0xf0) >> 4) + !!was_odd;
3820 static int atihdmi_paired_chmap_cea_alloc_validate_get_type(struct cea_channel_speaker_allocation *cap,
3826 * Pre-rev3 ATI/AMD codecs operate in a paired channel mode, so
3827 * we need to take that into account (a single channel may take 2
3828 * channel slots if we need to carry a silent channel next to it).
3829 * On Rev3+ AMD codecs this function is not used.
3833 /* We only produce even-numbered channel count TLVs */
3834 if ((channels % 2) != 0)
3837 for (c = 0; c < 7; c += 2) {
3838 if (cap->speakers[c] || cap->speakers[c+1])
3842 if (chanpairs * 2 != channels)
3845 return SNDRV_CTL_TLVT_CHMAP_PAIRED;
3848 static void atihdmi_paired_cea_alloc_to_tlv_chmap(struct cea_channel_speaker_allocation *cap,
3849 unsigned int *chmap, int channels)
3851 /* produce paired maps for pre-rev3 ATI/AMD codecs */
3855 for (c = 7; c >= 0; c--) {
3856 int chan = 7 - atihdmi_paired_swap_fc_lfe(7 - c);
3857 int spk = cap->speakers[chan];
3859 /* add N/A channel if the companion channel is occupied */
3860 if (cap->speakers[chan + (chan % 2 ? -1 : 1)])
3861 chmap[count++] = SNDRV_CHMAP_NA;
3866 chmap[count++] = spk_to_chmap(spk);
3869 WARN_ON(count != channels);
3872 static int atihdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
3875 int hbr_ctl, hbr_ctl_new;
3877 hbr_ctl = snd_hda_codec_read(codec, pin_nid, 0, ATI_VERB_GET_HBR_CONTROL, 0);
3878 if (hbr_ctl >= 0 && (hbr_ctl & ATI_HBR_CAPABLE)) {
3880 hbr_ctl_new = hbr_ctl | ATI_HBR_ENABLE;
3882 hbr_ctl_new = hbr_ctl & ~ATI_HBR_ENABLE;
3885 "atihdmi_pin_hbr_setup: NID=0x%x, %shbr-ctl=0x%x\n",
3887 hbr_ctl == hbr_ctl_new ? "" : "new-",
3890 if (hbr_ctl != hbr_ctl_new)
3891 snd_hda_codec_write(codec, pin_nid, 0,
3892 ATI_VERB_SET_HBR_CONTROL,
3901 static int atihdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
3902 hda_nid_t pin_nid, u32 stream_tag, int format)
3905 if (is_amdhdmi_rev3_or_later(codec)) {
3906 int ramp_rate = 180; /* default as per AMD spec */
3907 /* disable ramp-up/down for non-pcm as per AMD spec */
3908 if (format & AC_FMT_TYPE_NON_PCM)
3911 snd_hda_codec_write(codec, cvt_nid, 0, ATI_VERB_SET_RAMP_RATE, ramp_rate);
3914 return hdmi_setup_stream(codec, cvt_nid, pin_nid, stream_tag, format);
3918 static int atihdmi_init(struct hda_codec *codec)
3920 struct hdmi_spec *spec = codec->spec;
3923 err = generic_hdmi_init(codec);
3928 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
3929 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
3931 /* make sure downmix information in infoframe is zero */
3932 snd_hda_codec_write(codec, per_pin->pin_nid, 0, ATI_VERB_SET_DOWNMIX_INFO, 0);
3934 /* enable channel-wise remap mode if supported */
3935 if (has_amd_full_remap_support(codec))
3936 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
3937 ATI_VERB_SET_MULTICHANNEL_MODE,
3938 ATI_MULTICHANNEL_MODE_SINGLE);
3944 static int patch_atihdmi(struct hda_codec *codec)
3946 struct hdmi_spec *spec;
3947 struct hdmi_spec_per_cvt *per_cvt;
3950 err = patch_generic_hdmi(codec);
3955 codec->patch_ops.init = atihdmi_init;
3959 spec->ops.pin_get_eld = atihdmi_pin_get_eld;
3960 spec->ops.pin_get_slot_channel = atihdmi_pin_get_slot_channel;
3961 spec->ops.pin_set_slot_channel = atihdmi_pin_set_slot_channel;
3962 spec->ops.pin_setup_infoframe = atihdmi_pin_setup_infoframe;
3963 spec->ops.pin_hbr_setup = atihdmi_pin_hbr_setup;
3964 spec->ops.setup_stream = atihdmi_setup_stream;
3966 if (!has_amd_full_remap_support(codec)) {
3967 /* override to ATI/AMD-specific versions with pairwise mapping */
3968 spec->ops.chmap_cea_alloc_validate_get_type =
3969 atihdmi_paired_chmap_cea_alloc_validate_get_type;
3970 spec->ops.cea_alloc_to_tlv_chmap = atihdmi_paired_cea_alloc_to_tlv_chmap;
3971 spec->ops.chmap_validate = atihdmi_paired_chmap_validate;
3974 /* ATI/AMD converters do not advertise all of their capabilities */
3975 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
3976 per_cvt = get_cvt(spec, cvt_idx);
3977 per_cvt->channels_max = max(per_cvt->channels_max, 8u);
3978 per_cvt->rates |= SUPPORTED_RATES;
3979 per_cvt->formats |= SUPPORTED_FORMATS;
3980 per_cvt->maxbps = max(per_cvt->maxbps, 24u);
3983 spec->channels_max = max(spec->channels_max, 8u);
3988 /* VIA HDMI Implementation */
3989 #define VIAHDMI_CVT_NID 0x02 /* audio converter1 */
3990 #define VIAHDMI_PIN_NID 0x03 /* HDMI output pin1 */
3992 static int patch_via_hdmi(struct hda_codec *codec)
3994 return patch_simple_hdmi(codec, VIAHDMI_CVT_NID, VIAHDMI_PIN_NID);
4000 static const struct hda_device_id snd_hda_id_hdmi[] = {
4001 HDA_CODEC_ENTRY(0x1002793c, "RS600 HDMI", patch_atihdmi),
4002 HDA_CODEC_ENTRY(0x10027919, "RS600 HDMI", patch_atihdmi),
4003 HDA_CODEC_ENTRY(0x1002791a, "RS690/780 HDMI", patch_atihdmi),
4004 HDA_CODEC_ENTRY(0x1002aa01, "R6xx HDMI", patch_atihdmi),
4005 HDA_CODEC_ENTRY(0x10951390, "SiI1390 HDMI", patch_generic_hdmi),
4006 HDA_CODEC_ENTRY(0x10951392, "SiI1392 HDMI", patch_generic_hdmi),
4007 HDA_CODEC_ENTRY(0x17e80047, "Chrontel HDMI", patch_generic_hdmi),
4008 HDA_CODEC_ENTRY(0x10de0002, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
4009 HDA_CODEC_ENTRY(0x10de0003, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
4010 HDA_CODEC_ENTRY(0x10de0005, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
4011 HDA_CODEC_ENTRY(0x10de0006, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
4012 HDA_CODEC_ENTRY(0x10de0007, "MCP79/7A HDMI", patch_nvhdmi_8ch_7x),
4013 HDA_CODEC_ENTRY(0x10de000a, "GPU 0a HDMI/DP", patch_nvhdmi),
4014 HDA_CODEC_ENTRY(0x10de000b, "GPU 0b HDMI/DP", patch_nvhdmi),
4015 HDA_CODEC_ENTRY(0x10de000c, "MCP89 HDMI", patch_nvhdmi),
4016 HDA_CODEC_ENTRY(0x10de000d, "GPU 0d HDMI/DP", patch_nvhdmi),
4017 HDA_CODEC_ENTRY(0x10de0010, "GPU 10 HDMI/DP", patch_nvhdmi),
4018 HDA_CODEC_ENTRY(0x10de0011, "GPU 11 HDMI/DP", patch_nvhdmi),
4019 HDA_CODEC_ENTRY(0x10de0012, "GPU 12 HDMI/DP", patch_nvhdmi),
4020 HDA_CODEC_ENTRY(0x10de0013, "GPU 13 HDMI/DP", patch_nvhdmi),
4021 HDA_CODEC_ENTRY(0x10de0014, "GPU 14 HDMI/DP", patch_nvhdmi),
4022 HDA_CODEC_ENTRY(0x10de0015, "GPU 15 HDMI/DP", patch_nvhdmi),
4023 HDA_CODEC_ENTRY(0x10de0016, "GPU 16 HDMI/DP", patch_nvhdmi),
4024 /* 17 is known to be absent */
4025 HDA_CODEC_ENTRY(0x10de0018, "GPU 18 HDMI/DP", patch_nvhdmi),
4026 HDA_CODEC_ENTRY(0x10de0019, "GPU 19 HDMI/DP", patch_nvhdmi),
4027 HDA_CODEC_ENTRY(0x10de001a, "GPU 1a HDMI/DP", patch_nvhdmi),
4028 HDA_CODEC_ENTRY(0x10de001b, "GPU 1b HDMI/DP", patch_nvhdmi),
4029 HDA_CODEC_ENTRY(0x10de001c, "GPU 1c HDMI/DP", patch_nvhdmi),
4030 HDA_CODEC_ENTRY(0x10de0020, "Tegra30 HDMI", patch_tegra_hdmi),
4031 HDA_CODEC_ENTRY(0x10de0022, "Tegra114 HDMI", patch_tegra_hdmi),
4032 HDA_CODEC_ENTRY(0x10de0028, "Tegra124 HDMI", patch_tegra_hdmi),
4033 HDA_CODEC_ENTRY(0x10de0029, "Tegra210 HDMI/DP", patch_tegra_hdmi),
4034 HDA_CODEC_ENTRY(0x10de0040, "GPU 40 HDMI/DP", patch_nvhdmi),
4035 HDA_CODEC_ENTRY(0x10de0041, "GPU 41 HDMI/DP", patch_nvhdmi),
4036 HDA_CODEC_ENTRY(0x10de0042, "GPU 42 HDMI/DP", patch_nvhdmi),
4037 HDA_CODEC_ENTRY(0x10de0043, "GPU 43 HDMI/DP", patch_nvhdmi),
4038 HDA_CODEC_ENTRY(0x10de0044, "GPU 44 HDMI/DP", patch_nvhdmi),
4039 HDA_CODEC_ENTRY(0x10de0051, "GPU 51 HDMI/DP", patch_nvhdmi),
4040 HDA_CODEC_ENTRY(0x10de0060, "GPU 60 HDMI/DP", patch_nvhdmi),
4041 HDA_CODEC_ENTRY(0x10de0067, "MCP67 HDMI", patch_nvhdmi_2ch),
4042 HDA_CODEC_ENTRY(0x10de0070, "GPU 70 HDMI/DP", patch_nvhdmi),
4043 HDA_CODEC_ENTRY(0x10de0071, "GPU 71 HDMI/DP", patch_nvhdmi),
4044 HDA_CODEC_ENTRY(0x10de0072, "GPU 72 HDMI/DP", patch_nvhdmi),
4045 HDA_CODEC_ENTRY(0x10de007d, "GPU 7d HDMI/DP", patch_nvhdmi),
4046 HDA_CODEC_ENTRY(0x10de8001, "MCP73 HDMI", patch_nvhdmi_2ch),
4047 HDA_CODEC_ENTRY(0x11069f80, "VX900 HDMI/DP", patch_via_hdmi),
4048 HDA_CODEC_ENTRY(0x11069f81, "VX900 HDMI/DP", patch_via_hdmi),
4049 HDA_CODEC_ENTRY(0x11069f84, "VX11 HDMI/DP", patch_generic_hdmi),
4050 HDA_CODEC_ENTRY(0x11069f85, "VX11 HDMI/DP", patch_generic_hdmi),
4051 HDA_CODEC_ENTRY(0x80860054, "IbexPeak HDMI", patch_generic_hdmi),
4052 HDA_CODEC_ENTRY(0x80862801, "Bearlake HDMI", patch_generic_hdmi),
4053 HDA_CODEC_ENTRY(0x80862802, "Cantiga HDMI", patch_generic_hdmi),
4054 HDA_CODEC_ENTRY(0x80862803, "Eaglelake HDMI", patch_generic_hdmi),
4055 HDA_CODEC_ENTRY(0x80862804, "IbexPeak HDMI", patch_generic_hdmi),
4056 HDA_CODEC_ENTRY(0x80862805, "CougarPoint HDMI", patch_generic_hdmi),
4057 HDA_CODEC_ENTRY(0x80862806, "PantherPoint HDMI", patch_generic_hdmi),
4058 HDA_CODEC_ENTRY(0x80862807, "Haswell HDMI", patch_generic_hdmi),
4059 HDA_CODEC_ENTRY(0x80862808, "Broadwell HDMI", patch_generic_hdmi),
4060 HDA_CODEC_ENTRY(0x80862809, "Skylake HDMI", patch_generic_hdmi),
4061 HDA_CODEC_ENTRY(0x8086280a, "Broxton HDMI", patch_generic_hdmi),
4062 HDA_CODEC_ENTRY(0x8086280b, "Kabylake HDMI", patch_generic_hdmi),
4063 HDA_CODEC_ENTRY(0x80862880, "CedarTrail HDMI", patch_generic_hdmi),
4064 HDA_CODEC_ENTRY(0x80862882, "Valleyview2 HDMI", patch_generic_hdmi),
4065 HDA_CODEC_ENTRY(0x80862883, "Braswell HDMI", patch_generic_hdmi),
4066 HDA_CODEC_ENTRY(0x808629fb, "Crestline HDMI", patch_generic_hdmi),
4067 /* special ID for generic HDMI */
4068 HDA_CODEC_ENTRY(HDA_CODEC_ID_GENERIC_HDMI, "Generic HDMI", patch_generic_hdmi),
4071 MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_hdmi);
4073 MODULE_LICENSE("GPL");
4074 MODULE_DESCRIPTION("HDMI HD-audio codec");
4075 MODULE_ALIAS("snd-hda-codec-intelhdmi");
4076 MODULE_ALIAS("snd-hda-codec-nvhdmi");
4077 MODULE_ALIAS("snd-hda-codec-atihdmi");
4079 static struct hda_codec_driver hdmi_driver = {
4080 .id = snd_hda_id_hdmi,
4083 module_hda_codec_driver(hdmi_driver);