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>
11 * Wu Fengguang <wfg@linux.intel.com>
14 * Wu Fengguang <wfg@linux.intel.com>
16 * This program is free software; you can redistribute it and/or modify it
17 * under the terms of the GNU General Public License as published by the Free
18 * Software Foundation; either version 2 of the License, or (at your option)
21 * This program is distributed in the hope that it will be useful, but
22 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
23 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software Foundation,
28 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31 #include <linux/init.h>
32 #include <linux/delay.h>
33 #include <linux/slab.h>
34 #include <linux/module.h>
35 #include <sound/core.h>
36 #include <sound/jack.h>
37 #include <sound/asoundef.h>
38 #include <sound/tlv.h>
39 #include "hda_codec.h"
40 #include "hda_local.h"
43 static bool static_hdmi_pcm;
44 module_param(static_hdmi_pcm, bool, 0644);
45 MODULE_PARM_DESC(static_hdmi_pcm, "Don't restrict PCM parameters per ELD info");
47 struct hdmi_spec_per_cvt {
50 unsigned int channels_min;
51 unsigned int channels_max;
57 /* max. connections to a widget */
58 #define HDA_MAX_CONNECTIONS 32
60 struct hdmi_spec_per_pin {
63 hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
65 struct hda_codec *codec;
66 struct hdmi_eld sink_eld;
67 struct delayed_work work;
68 struct snd_kcontrol *eld_ctl;
70 bool setup; /* the stream has been set up by prepare callback */
71 int channels; /* current number of channels */
73 bool chmap_set; /* channel-map override by ALSA API? */
74 unsigned char chmap[8]; /* ALSA API channel-map */
75 char pcm_name[8]; /* filled in build_pcm callbacks */
80 struct snd_array cvts; /* struct hdmi_spec_per_cvt */
81 hda_nid_t cvt_nids[4]; /* only for haswell fix */
84 struct snd_array pins; /* struct hdmi_spec_per_pin */
85 struct snd_array pcm_rec; /* struct hda_pcm */
86 unsigned int channels_max; /* max over all cvts */
88 struct hdmi_eld temp_eld;
90 * Non-generic ATI/NVIDIA specific
92 struct hda_multi_out multiout;
93 struct hda_pcm_stream pcm_playback;
97 struct hdmi_audio_infoframe {
104 u8 CC02_CT47; /* CC in bits 0:2, CT in 4:7 */
108 u8 LFEPBL01_LSV36_DM_INH7;
111 struct dp_audio_infoframe {
114 u8 ver; /* 0x11 << 2 */
116 u8 CC02_CT47; /* match with HDMI infoframe from this on */
120 u8 LFEPBL01_LSV36_DM_INH7;
123 union audio_infoframe {
124 struct hdmi_audio_infoframe hdmi;
125 struct dp_audio_infoframe dp;
130 * CEA speaker placement:
133 * FLW FL FLC FC FRC FR FRW
140 * The Left/Right Surround channel _notions_ LS/RS in SMPTE 320M corresponds to
141 * CEA RL/RR; The SMPTE channel _assignment_ C/LFE is swapped to CEA LFE/FC.
143 enum cea_speaker_placement {
144 FL = (1 << 0), /* Front Left */
145 FC = (1 << 1), /* Front Center */
146 FR = (1 << 2), /* Front Right */
147 FLC = (1 << 3), /* Front Left Center */
148 FRC = (1 << 4), /* Front Right Center */
149 RL = (1 << 5), /* Rear Left */
150 RC = (1 << 6), /* Rear Center */
151 RR = (1 << 7), /* Rear Right */
152 RLC = (1 << 8), /* Rear Left Center */
153 RRC = (1 << 9), /* Rear Right Center */
154 LFE = (1 << 10), /* Low Frequency Effect */
155 FLW = (1 << 11), /* Front Left Wide */
156 FRW = (1 << 12), /* Front Right Wide */
157 FLH = (1 << 13), /* Front Left High */
158 FCH = (1 << 14), /* Front Center High */
159 FRH = (1 << 15), /* Front Right High */
160 TC = (1 << 16), /* Top Center */
164 * ELD SA bits in the CEA Speaker Allocation data block
166 static int eld_speaker_allocation_bits[] = {
174 /* the following are not defined in ELD yet */
181 struct cea_channel_speaker_allocation {
185 /* derived values, just for convenience */
193 * surround40 surround41 surround50 surround51 surround71
194 * ch0 front left = = = =
195 * ch1 front right = = = =
196 * ch2 rear left = = = =
197 * ch3 rear right = = = =
198 * ch4 LFE center center center
203 * surround71 = {FL, FR, RLC, RRC, FC, LFE, RL, RR}
205 static int hdmi_channel_mapping[0x32][8] = {
207 [0x00] = { 0x00, 0x11, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
209 [0x01] = { 0x00, 0x11, 0x22, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
211 [0x02] = { 0x00, 0x11, 0x23, 0xf2, 0xf4, 0xf5, 0xf6, 0xf7 },
213 [0x08] = { 0x00, 0x11, 0x24, 0x35, 0xf3, 0xf2, 0xf6, 0xf7 },
215 [0x03] = { 0x00, 0x11, 0x23, 0x32, 0x44, 0xf5, 0xf6, 0xf7 },
217 [0x09] = { 0x00, 0x11, 0x24, 0x35, 0x42, 0xf3, 0xf6, 0xf7 },
219 [0x0a] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0xf2, 0xf6, 0xf7 },
221 [0x0b] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0x52, 0xf6, 0xf7 },
223 [0x13] = { 0x00, 0x11, 0x26, 0x37, 0x43, 0x52, 0x64, 0x75 },
227 * This is an ordered list!
229 * The preceding ones have better chances to be selected by
230 * hdmi_channel_allocation().
232 static struct cea_channel_speaker_allocation channel_allocations[] = {
233 /* channel: 7 6 5 4 3 2 1 0 */
234 { .ca_index = 0x00, .speakers = { 0, 0, 0, 0, 0, 0, FR, FL } },
236 { .ca_index = 0x01, .speakers = { 0, 0, 0, 0, 0, LFE, FR, FL } },
238 { .ca_index = 0x02, .speakers = { 0, 0, 0, 0, FC, 0, FR, FL } },
240 { .ca_index = 0x08, .speakers = { 0, 0, RR, RL, 0, 0, FR, FL } },
242 { .ca_index = 0x09, .speakers = { 0, 0, RR, RL, 0, LFE, FR, FL } },
244 { .ca_index = 0x0a, .speakers = { 0, 0, RR, RL, FC, 0, FR, FL } },
246 { .ca_index = 0x0b, .speakers = { 0, 0, RR, RL, FC, LFE, FR, FL } },
248 { .ca_index = 0x0f, .speakers = { 0, RC, RR, RL, FC, LFE, FR, FL } },
250 { .ca_index = 0x13, .speakers = { RRC, RLC, RR, RL, FC, LFE, FR, FL } },
252 { .ca_index = 0x03, .speakers = { 0, 0, 0, 0, FC, LFE, FR, FL } },
253 { .ca_index = 0x04, .speakers = { 0, 0, 0, RC, 0, 0, FR, FL } },
254 { .ca_index = 0x05, .speakers = { 0, 0, 0, RC, 0, LFE, FR, FL } },
255 { .ca_index = 0x06, .speakers = { 0, 0, 0, RC, FC, 0, FR, FL } },
256 { .ca_index = 0x07, .speakers = { 0, 0, 0, RC, FC, LFE, FR, FL } },
257 { .ca_index = 0x0c, .speakers = { 0, RC, RR, RL, 0, 0, FR, FL } },
258 { .ca_index = 0x0d, .speakers = { 0, RC, RR, RL, 0, LFE, FR, FL } },
259 { .ca_index = 0x0e, .speakers = { 0, RC, RR, RL, FC, 0, FR, FL } },
260 { .ca_index = 0x10, .speakers = { RRC, RLC, RR, RL, 0, 0, FR, FL } },
261 { .ca_index = 0x11, .speakers = { RRC, RLC, RR, RL, 0, LFE, FR, FL } },
262 { .ca_index = 0x12, .speakers = { RRC, RLC, RR, RL, FC, 0, FR, FL } },
263 { .ca_index = 0x14, .speakers = { FRC, FLC, 0, 0, 0, 0, FR, FL } },
264 { .ca_index = 0x15, .speakers = { FRC, FLC, 0, 0, 0, LFE, FR, FL } },
265 { .ca_index = 0x16, .speakers = { FRC, FLC, 0, 0, FC, 0, FR, FL } },
266 { .ca_index = 0x17, .speakers = { FRC, FLC, 0, 0, FC, LFE, FR, FL } },
267 { .ca_index = 0x18, .speakers = { FRC, FLC, 0, RC, 0, 0, FR, FL } },
268 { .ca_index = 0x19, .speakers = { FRC, FLC, 0, RC, 0, LFE, FR, FL } },
269 { .ca_index = 0x1a, .speakers = { FRC, FLC, 0, RC, FC, 0, FR, FL } },
270 { .ca_index = 0x1b, .speakers = { FRC, FLC, 0, RC, FC, LFE, FR, FL } },
271 { .ca_index = 0x1c, .speakers = { FRC, FLC, RR, RL, 0, 0, FR, FL } },
272 { .ca_index = 0x1d, .speakers = { FRC, FLC, RR, RL, 0, LFE, FR, FL } },
273 { .ca_index = 0x1e, .speakers = { FRC, FLC, RR, RL, FC, 0, FR, FL } },
274 { .ca_index = 0x1f, .speakers = { FRC, FLC, RR, RL, FC, LFE, FR, FL } },
275 { .ca_index = 0x20, .speakers = { 0, FCH, RR, RL, FC, 0, FR, FL } },
276 { .ca_index = 0x21, .speakers = { 0, FCH, RR, RL, FC, LFE, FR, FL } },
277 { .ca_index = 0x22, .speakers = { TC, 0, RR, RL, FC, 0, FR, FL } },
278 { .ca_index = 0x23, .speakers = { TC, 0, RR, RL, FC, LFE, FR, FL } },
279 { .ca_index = 0x24, .speakers = { FRH, FLH, RR, RL, 0, 0, FR, FL } },
280 { .ca_index = 0x25, .speakers = { FRH, FLH, RR, RL, 0, LFE, FR, FL } },
281 { .ca_index = 0x26, .speakers = { FRW, FLW, RR, RL, 0, 0, FR, FL } },
282 { .ca_index = 0x27, .speakers = { FRW, FLW, RR, RL, 0, LFE, FR, FL } },
283 { .ca_index = 0x28, .speakers = { TC, RC, RR, RL, FC, 0, FR, FL } },
284 { .ca_index = 0x29, .speakers = { TC, RC, RR, RL, FC, LFE, FR, FL } },
285 { .ca_index = 0x2a, .speakers = { FCH, RC, RR, RL, FC, 0, FR, FL } },
286 { .ca_index = 0x2b, .speakers = { FCH, RC, RR, RL, FC, LFE, FR, FL } },
287 { .ca_index = 0x2c, .speakers = { TC, FCH, RR, RL, FC, 0, FR, FL } },
288 { .ca_index = 0x2d, .speakers = { TC, FCH, RR, RL, FC, LFE, FR, FL } },
289 { .ca_index = 0x2e, .speakers = { FRH, FLH, RR, RL, FC, 0, FR, FL } },
290 { .ca_index = 0x2f, .speakers = { FRH, FLH, RR, RL, FC, LFE, FR, FL } },
291 { .ca_index = 0x30, .speakers = { FRW, FLW, RR, RL, FC, 0, FR, FL } },
292 { .ca_index = 0x31, .speakers = { FRW, FLW, RR, RL, FC, LFE, FR, FL } },
300 #define get_pin(spec, idx) \
301 ((struct hdmi_spec_per_pin *)snd_array_elem(&spec->pins, idx))
302 #define get_cvt(spec, idx) \
303 ((struct hdmi_spec_per_cvt *)snd_array_elem(&spec->cvts, idx))
304 #define get_pcm_rec(spec, idx) \
305 ((struct hda_pcm *)snd_array_elem(&spec->pcm_rec, idx))
307 static int pin_nid_to_pin_index(struct hdmi_spec *spec, hda_nid_t pin_nid)
311 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++)
312 if (get_pin(spec, pin_idx)->pin_nid == pin_nid)
315 snd_printk(KERN_WARNING "HDMI: pin nid %d not registered\n", pin_nid);
319 static int hinfo_to_pin_index(struct hdmi_spec *spec,
320 struct hda_pcm_stream *hinfo)
324 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++)
325 if (get_pcm_rec(spec, pin_idx)->stream == hinfo)
328 snd_printk(KERN_WARNING "HDMI: hinfo %p not registered\n", hinfo);
332 static int cvt_nid_to_cvt_index(struct hdmi_spec *spec, hda_nid_t cvt_nid)
336 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++)
337 if (get_cvt(spec, cvt_idx)->cvt_nid == cvt_nid)
340 snd_printk(KERN_WARNING "HDMI: cvt nid %d not registered\n", cvt_nid);
344 static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
345 struct snd_ctl_elem_info *uinfo)
347 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
348 struct hdmi_spec *spec = codec->spec;
349 struct hdmi_eld *eld;
352 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
354 pin_idx = kcontrol->private_value;
355 eld = &get_pin(spec, pin_idx)->sink_eld;
357 mutex_lock(&eld->lock);
358 uinfo->count = eld->eld_valid ? eld->eld_size : 0;
359 mutex_unlock(&eld->lock);
364 static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
365 struct snd_ctl_elem_value *ucontrol)
367 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
368 struct hdmi_spec *spec = codec->spec;
369 struct hdmi_eld *eld;
372 pin_idx = kcontrol->private_value;
373 eld = &get_pin(spec, pin_idx)->sink_eld;
375 mutex_lock(&eld->lock);
376 if (eld->eld_size > ARRAY_SIZE(ucontrol->value.bytes.data)) {
377 mutex_unlock(&eld->lock);
382 memset(ucontrol->value.bytes.data, 0,
383 ARRAY_SIZE(ucontrol->value.bytes.data));
385 memcpy(ucontrol->value.bytes.data, eld->eld_buffer,
387 mutex_unlock(&eld->lock);
392 static struct snd_kcontrol_new eld_bytes_ctl = {
393 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
394 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
396 .info = hdmi_eld_ctl_info,
397 .get = hdmi_eld_ctl_get,
400 static int hdmi_create_eld_ctl(struct hda_codec *codec, int pin_idx,
403 struct snd_kcontrol *kctl;
404 struct hdmi_spec *spec = codec->spec;
407 kctl = snd_ctl_new1(&eld_bytes_ctl, codec);
410 kctl->private_value = pin_idx;
411 kctl->id.device = device;
413 err = snd_hda_ctl_add(codec, get_pin(spec, pin_idx)->pin_nid, kctl);
417 get_pin(spec, pin_idx)->eld_ctl = kctl;
422 static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
423 int *packet_index, int *byte_index)
427 val = snd_hda_codec_read(codec, pin_nid, 0,
428 AC_VERB_GET_HDMI_DIP_INDEX, 0);
430 *packet_index = val >> 5;
431 *byte_index = val & 0x1f;
435 static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
436 int packet_index, int byte_index)
440 val = (packet_index << 5) | (byte_index & 0x1f);
442 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
445 static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t pin_nid,
448 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
451 static void hdmi_init_pin(struct hda_codec *codec, hda_nid_t pin_nid)
454 if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
455 snd_hda_codec_write(codec, pin_nid, 0,
456 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
457 /* Enable pin out: some machines with GM965 gets broken output when
458 * the pin is disabled or changed while using with HDMI
460 snd_hda_codec_write(codec, pin_nid, 0,
461 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
464 static int hdmi_get_channel_count(struct hda_codec *codec, hda_nid_t cvt_nid)
466 return 1 + snd_hda_codec_read(codec, cvt_nid, 0,
467 AC_VERB_GET_CVT_CHAN_COUNT, 0);
470 static void hdmi_set_channel_count(struct hda_codec *codec,
471 hda_nid_t cvt_nid, int chs)
473 if (chs != hdmi_get_channel_count(codec, cvt_nid))
474 snd_hda_codec_write(codec, cvt_nid, 0,
475 AC_VERB_SET_CVT_CHAN_COUNT, chs - 1);
480 * Channel mapping routines
484 * Compute derived values in channel_allocations[].
486 static void init_channel_allocations(void)
489 struct cea_channel_speaker_allocation *p;
491 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
492 p = channel_allocations + i;
495 for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
496 if (p->speakers[j]) {
498 p->spk_mask |= p->speakers[j];
503 static int get_channel_allocation_order(int ca)
507 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
508 if (channel_allocations[i].ca_index == ca)
515 * The transformation takes two steps:
517 * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
518 * spk_mask => (channel_allocations[]) => ai->CA
520 * TODO: it could select the wrong CA from multiple candidates.
522 static int hdmi_channel_allocation(struct hdmi_eld *eld, int channels)
527 char buf[SND_PRINT_CHANNEL_ALLOCATION_ADVISED_BUFSIZE];
530 * CA defaults to 0 for basic stereo audio
536 * expand ELD's speaker allocation mask
538 * ELD tells the speaker mask in a compact(paired) form,
539 * expand ELD's notions to match the ones used by Audio InfoFrame.
541 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
542 if (eld->info.spk_alloc & (1 << i))
543 spk_mask |= eld_speaker_allocation_bits[i];
546 /* search for the first working match in the CA table */
547 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
548 if (channels == channel_allocations[i].channels &&
549 (spk_mask & channel_allocations[i].spk_mask) ==
550 channel_allocations[i].spk_mask) {
551 ca = channel_allocations[i].ca_index;
557 /* if there was no match, select the regular ALSA channel
558 * allocation with the matching number of channels */
559 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
560 if (channels == channel_allocations[i].channels) {
561 ca = channel_allocations[i].ca_index;
567 snd_print_channel_allocation(eld->info.spk_alloc, buf, sizeof(buf));
568 snd_printdd("HDMI: select CA 0x%x for %d-channel allocation: %s\n",
574 static void hdmi_debug_channel_mapping(struct hda_codec *codec,
577 #ifdef CONFIG_SND_DEBUG_VERBOSE
581 for (i = 0; i < 8; i++) {
582 slot = snd_hda_codec_read(codec, pin_nid, 0,
583 AC_VERB_GET_HDMI_CHAN_SLOT, i);
584 printk(KERN_DEBUG "HDMI: ASP channel %d => slot %d\n",
585 slot >> 4, slot & 0xf);
591 static void hdmi_std_setup_channel_mapping(struct hda_codec *codec,
599 int non_pcm_mapping[8];
601 order = get_channel_allocation_order(ca);
603 if (hdmi_channel_mapping[ca][1] == 0) {
604 for (i = 0; i < channel_allocations[order].channels; i++)
605 hdmi_channel_mapping[ca][i] = i | (i << 4);
607 hdmi_channel_mapping[ca][i] = 0xf | (i << 4);
611 for (i = 0; i < channel_allocations[order].channels; i++)
612 non_pcm_mapping[i] = i | (i << 4);
614 non_pcm_mapping[i] = 0xf | (i << 4);
617 for (i = 0; i < 8; i++) {
618 err = snd_hda_codec_write(codec, pin_nid, 0,
619 AC_VERB_SET_HDMI_CHAN_SLOT,
620 non_pcm ? non_pcm_mapping[i] : hdmi_channel_mapping[ca][i]);
622 snd_printdd(KERN_NOTICE
623 "HDMI: channel mapping failed\n");
628 hdmi_debug_channel_mapping(codec, pin_nid);
631 struct channel_map_table {
632 unsigned char map; /* ALSA API channel map position */
633 unsigned char cea_slot; /* CEA slot value */
634 int spk_mask; /* speaker position bit mask */
637 static struct channel_map_table map_tables[] = {
638 { SNDRV_CHMAP_FL, 0x00, FL },
639 { SNDRV_CHMAP_FR, 0x01, FR },
640 { SNDRV_CHMAP_RL, 0x04, RL },
641 { SNDRV_CHMAP_RR, 0x05, RR },
642 { SNDRV_CHMAP_LFE, 0x02, LFE },
643 { SNDRV_CHMAP_FC, 0x03, FC },
644 { SNDRV_CHMAP_RLC, 0x06, RLC },
645 { SNDRV_CHMAP_RRC, 0x07, RRC },
649 /* from ALSA API channel position to speaker bit mask */
650 static int to_spk_mask(unsigned char c)
652 struct channel_map_table *t = map_tables;
653 for (; t->map; t++) {
660 /* from ALSA API channel position to CEA slot */
661 static int to_cea_slot(unsigned char c)
663 struct channel_map_table *t = map_tables;
664 for (; t->map; t++) {
671 /* from CEA slot to ALSA API channel position */
672 static int from_cea_slot(unsigned char c)
674 struct channel_map_table *t = map_tables;
675 for (; t->map; t++) {
676 if (t->cea_slot == c)
682 /* from speaker bit mask to ALSA API channel position */
683 static int spk_to_chmap(int spk)
685 struct channel_map_table *t = map_tables;
686 for (; t->map; t++) {
687 if (t->spk_mask == spk)
693 /* get the CA index corresponding to the given ALSA API channel map */
694 static int hdmi_manual_channel_allocation(int chs, unsigned char *map)
696 int i, spks = 0, spk_mask = 0;
698 for (i = 0; i < chs; i++) {
699 int mask = to_spk_mask(map[i]);
706 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
707 if ((chs == channel_allocations[i].channels ||
708 spks == channel_allocations[i].channels) &&
709 (spk_mask & channel_allocations[i].spk_mask) ==
710 channel_allocations[i].spk_mask)
711 return channel_allocations[i].ca_index;
716 /* set up the channel slots for the given ALSA API channel map */
717 static int hdmi_manual_setup_channel_mapping(struct hda_codec *codec,
719 int chs, unsigned char *map)
722 for (i = 0; i < 8; i++) {
725 val = to_cea_slot(map[i]);
729 err = snd_hda_codec_write(codec, pin_nid, 0,
730 AC_VERB_SET_HDMI_CHAN_SLOT, val);
737 /* store ALSA API channel map from the current default map */
738 static void hdmi_setup_fake_chmap(unsigned char *map, int ca)
741 for (i = 0; i < 8; i++) {
742 if (i < channel_allocations[ca].channels)
743 map[i] = from_cea_slot((hdmi_channel_mapping[ca][i] >> 4) & 0x0f);
749 static void hdmi_setup_channel_mapping(struct hda_codec *codec,
750 hda_nid_t pin_nid, bool non_pcm, int ca,
751 int channels, unsigned char *map,
754 if (!non_pcm && chmap_set) {
755 hdmi_manual_setup_channel_mapping(codec, pin_nid,
758 hdmi_std_setup_channel_mapping(codec, pin_nid, non_pcm, ca);
759 hdmi_setup_fake_chmap(map, ca);
764 * Audio InfoFrame routines
768 * Enable Audio InfoFrame Transmission
770 static void hdmi_start_infoframe_trans(struct hda_codec *codec,
773 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
774 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
779 * Disable Audio InfoFrame Transmission
781 static void hdmi_stop_infoframe_trans(struct hda_codec *codec,
784 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
785 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
789 static void hdmi_debug_dip_size(struct hda_codec *codec, hda_nid_t pin_nid)
791 #ifdef CONFIG_SND_DEBUG_VERBOSE
795 size = snd_hdmi_get_eld_size(codec, pin_nid);
796 printk(KERN_DEBUG "HDMI: ELD buf size is %d\n", size);
798 for (i = 0; i < 8; i++) {
799 size = snd_hda_codec_read(codec, pin_nid, 0,
800 AC_VERB_GET_HDMI_DIP_SIZE, i);
801 printk(KERN_DEBUG "HDMI: DIP GP[%d] buf size is %d\n", i, size);
806 static void hdmi_clear_dip_buffers(struct hda_codec *codec, hda_nid_t pin_nid)
812 for (i = 0; i < 8; i++) {
813 size = snd_hda_codec_read(codec, pin_nid, 0,
814 AC_VERB_GET_HDMI_DIP_SIZE, i);
818 hdmi_set_dip_index(codec, pin_nid, i, 0x0);
819 for (j = 1; j < 1000; j++) {
820 hdmi_write_dip_byte(codec, pin_nid, 0x0);
821 hdmi_get_dip_index(codec, pin_nid, &pi, &bi);
823 snd_printd(KERN_INFO "dip index %d: %d != %d\n",
825 if (bi == 0) /* byte index wrapped around */
829 "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
835 static void hdmi_checksum_audio_infoframe(struct hdmi_audio_infoframe *hdmi_ai)
837 u8 *bytes = (u8 *)hdmi_ai;
841 hdmi_ai->checksum = 0;
843 for (i = 0; i < sizeof(*hdmi_ai); i++)
846 hdmi_ai->checksum = -sum;
849 static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
855 hdmi_debug_dip_size(codec, pin_nid);
856 hdmi_clear_dip_buffers(codec, pin_nid); /* be paranoid */
858 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
859 for (i = 0; i < size; i++)
860 hdmi_write_dip_byte(codec, pin_nid, dip[i]);
863 static bool hdmi_infoframe_uptodate(struct hda_codec *codec, hda_nid_t pin_nid,
869 if (snd_hda_codec_read(codec, pin_nid, 0, AC_VERB_GET_HDMI_DIP_XMIT, 0)
873 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
874 for (i = 0; i < size; i++) {
875 val = snd_hda_codec_read(codec, pin_nid, 0,
876 AC_VERB_GET_HDMI_DIP_DATA, 0);
884 static void hdmi_setup_audio_infoframe(struct hda_codec *codec,
885 struct hdmi_spec_per_pin *per_pin,
888 hda_nid_t pin_nid = per_pin->pin_nid;
889 int channels = per_pin->channels;
890 struct hdmi_eld *eld;
892 union audio_infoframe ai;
897 eld = &per_pin->sink_eld;
898 if (!eld->monitor_present)
901 if (!non_pcm && per_pin->chmap_set)
902 ca = hdmi_manual_channel_allocation(channels, per_pin->chmap);
904 ca = hdmi_channel_allocation(eld, channels);
908 memset(&ai, 0, sizeof(ai));
909 if (eld->info.conn_type == 0) { /* HDMI */
910 struct hdmi_audio_infoframe *hdmi_ai = &ai.hdmi;
912 hdmi_ai->type = 0x84;
915 hdmi_ai->CC02_CT47 = channels - 1;
917 hdmi_checksum_audio_infoframe(hdmi_ai);
918 } else if (eld->info.conn_type == 1) { /* DisplayPort */
919 struct dp_audio_infoframe *dp_ai = &ai.dp;
923 dp_ai->ver = 0x11 << 2;
924 dp_ai->CC02_CT47 = channels - 1;
927 snd_printd("HDMI: unknown connection type at pin %d\n",
933 * always configure channel mapping, it may have been changed by the
934 * user in the meantime
936 hdmi_setup_channel_mapping(codec, pin_nid, non_pcm, ca,
937 channels, per_pin->chmap,
941 * sizeof(ai) is used instead of sizeof(*hdmi_ai) or
942 * sizeof(*dp_ai) to avoid partial match/update problems when
943 * the user switches between HDMI/DP monitors.
945 if (!hdmi_infoframe_uptodate(codec, pin_nid, ai.bytes,
947 snd_printdd("hdmi_setup_audio_infoframe: "
948 "pin=%d channels=%d\n",
951 hdmi_stop_infoframe_trans(codec, pin_nid);
952 hdmi_fill_audio_infoframe(codec, pin_nid,
953 ai.bytes, sizeof(ai));
954 hdmi_start_infoframe_trans(codec, pin_nid);
957 per_pin->non_pcm = non_pcm;
965 static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll);
967 static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
969 struct hdmi_spec *spec = codec->spec;
970 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
973 struct hda_jack_tbl *jack;
975 jack = snd_hda_jack_tbl_get_from_tag(codec, tag);
979 jack->jack_dirty = 1;
981 _snd_printd(SND_PR_VERBOSE,
982 "HDMI hot plug event: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
983 codec->addr, pin_nid,
984 !!(res & AC_UNSOL_RES_PD), !!(res & AC_UNSOL_RES_ELDV));
986 pin_idx = pin_nid_to_pin_index(spec, pin_nid);
990 hdmi_present_sense(get_pin(spec, pin_idx), 1);
991 snd_hda_jack_report_sync(codec);
994 static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
996 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
997 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
998 int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
999 int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
1002 "HDMI CP event: CODEC=%d TAG=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
1017 static void hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
1019 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
1020 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
1022 if (!snd_hda_jack_tbl_get_from_tag(codec, tag)) {
1023 snd_printd(KERN_INFO "Unexpected HDMI event tag 0x%x\n", tag);
1028 hdmi_intrinsic_event(codec, res);
1030 hdmi_non_intrinsic_event(codec, res);
1033 static void haswell_verify_pin_D0(struct hda_codec *codec, hda_nid_t nid)
1035 int pwr, lamp, ramp;
1037 pwr = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
1038 pwr = (pwr & AC_PWRST_ACTUAL) >> AC_PWRST_ACTUAL_SHIFT;
1039 if (pwr != AC_PWRST_D0) {
1040 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
1043 pwr = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
1044 pwr = (pwr & AC_PWRST_ACTUAL) >> AC_PWRST_ACTUAL_SHIFT;
1045 snd_printd("Haswell HDMI audio: Power for pin 0x%x is now D%d\n", nid, pwr);
1048 lamp = snd_hda_codec_read(codec, nid, 0,
1049 AC_VERB_GET_AMP_GAIN_MUTE,
1050 AC_AMP_GET_LEFT | AC_AMP_GET_OUTPUT);
1051 ramp = snd_hda_codec_read(codec, nid, 0,
1052 AC_VERB_GET_AMP_GAIN_MUTE,
1053 AC_AMP_GET_RIGHT | AC_AMP_GET_OUTPUT);
1055 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
1056 AC_AMP_SET_RIGHT | AC_AMP_SET_OUTPUT | lamp);
1058 lamp = snd_hda_codec_read(codec, nid, 0,
1059 AC_VERB_GET_AMP_GAIN_MUTE,
1060 AC_AMP_GET_LEFT | AC_AMP_GET_OUTPUT);
1061 ramp = snd_hda_codec_read(codec, nid, 0,
1062 AC_VERB_GET_AMP_GAIN_MUTE,
1063 AC_AMP_GET_RIGHT | AC_AMP_GET_OUTPUT);
1064 snd_printd("Haswell HDMI audio: Mute after set on pin 0x%x: [0x%x 0x%x]\n", nid, lamp, ramp);
1072 /* HBR should be Non-PCM, 8 channels */
1073 #define is_hbr_format(format) \
1074 ((format & AC_FMT_TYPE_NON_PCM) && (format & AC_FMT_CHAN_MASK) == 7)
1076 static int hdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
1077 hda_nid_t pin_nid, u32 stream_tag, int format)
1082 if (codec->vendor_id == 0x80862807)
1083 haswell_verify_pin_D0(codec, pin_nid);
1085 if (snd_hda_query_pin_caps(codec, pin_nid) & AC_PINCAP_HBR) {
1086 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
1087 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1089 new_pinctl = pinctl & ~AC_PINCTL_EPT;
1090 if (is_hbr_format(format))
1091 new_pinctl |= AC_PINCTL_EPT_HBR;
1093 new_pinctl |= AC_PINCTL_EPT_NATIVE;
1095 snd_printdd("hdmi_setup_stream: "
1096 "NID=0x%x, %spinctl=0x%x\n",
1098 pinctl == new_pinctl ? "" : "new-",
1101 if (pinctl != new_pinctl)
1102 snd_hda_codec_write(codec, pin_nid, 0,
1103 AC_VERB_SET_PIN_WIDGET_CONTROL,
1107 if (is_hbr_format(format) && !new_pinctl) {
1108 snd_printdd("hdmi_setup_stream: HBR is not supported\n");
1112 snd_hda_codec_setup_stream(codec, cvt_nid, stream_tag, 0, format);
1119 static int hdmi_pcm_open(struct hda_pcm_stream *hinfo,
1120 struct hda_codec *codec,
1121 struct snd_pcm_substream *substream)
1123 struct hdmi_spec *spec = codec->spec;
1124 struct snd_pcm_runtime *runtime = substream->runtime;
1125 int pin_idx, cvt_idx, mux_idx = 0;
1126 struct hdmi_spec_per_pin *per_pin;
1127 struct hdmi_eld *eld;
1128 struct hdmi_spec_per_cvt *per_cvt = NULL;
1130 /* Validate hinfo */
1131 pin_idx = hinfo_to_pin_index(spec, hinfo);
1132 if (snd_BUG_ON(pin_idx < 0))
1134 per_pin = get_pin(spec, pin_idx);
1135 eld = &per_pin->sink_eld;
1137 /* Dynamically assign converter to stream */
1138 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
1139 per_cvt = get_cvt(spec, cvt_idx);
1141 /* Must not already be assigned */
1142 if (per_cvt->assigned)
1144 /* Must be in pin's mux's list of converters */
1145 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
1146 if (per_pin->mux_nids[mux_idx] == per_cvt->cvt_nid)
1148 /* Not in mux list */
1149 if (mux_idx == per_pin->num_mux_nids)
1153 /* No free converters */
1154 if (cvt_idx == spec->num_cvts)
1157 /* Claim converter */
1158 per_cvt->assigned = 1;
1159 hinfo->nid = per_cvt->cvt_nid;
1161 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
1162 AC_VERB_SET_CONNECT_SEL,
1164 snd_hda_spdif_ctls_assign(codec, pin_idx, per_cvt->cvt_nid);
1166 /* Initially set the converter's capabilities */
1167 hinfo->channels_min = per_cvt->channels_min;
1168 hinfo->channels_max = per_cvt->channels_max;
1169 hinfo->rates = per_cvt->rates;
1170 hinfo->formats = per_cvt->formats;
1171 hinfo->maxbps = per_cvt->maxbps;
1173 /* Restrict capabilities by ELD if this isn't disabled */
1174 if (!static_hdmi_pcm && eld->eld_valid) {
1175 snd_hdmi_eld_update_pcm_info(&eld->info, hinfo);
1176 if (hinfo->channels_min > hinfo->channels_max ||
1177 !hinfo->rates || !hinfo->formats) {
1178 per_cvt->assigned = 0;
1180 snd_hda_spdif_ctls_unassign(codec, pin_idx);
1185 /* Store the updated parameters */
1186 runtime->hw.channels_min = hinfo->channels_min;
1187 runtime->hw.channels_max = hinfo->channels_max;
1188 runtime->hw.formats = hinfo->formats;
1189 runtime->hw.rates = hinfo->rates;
1191 snd_pcm_hw_constraint_step(substream->runtime, 0,
1192 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
1197 * HDA/HDMI auto parsing
1199 static int hdmi_read_pin_conn(struct hda_codec *codec, int pin_idx)
1201 struct hdmi_spec *spec = codec->spec;
1202 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1203 hda_nid_t pin_nid = per_pin->pin_nid;
1205 if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
1206 snd_printk(KERN_WARNING
1207 "HDMI: pin %d wcaps %#x "
1208 "does not support connection list\n",
1209 pin_nid, get_wcaps(codec, pin_nid));
1213 per_pin->num_mux_nids = snd_hda_get_connections(codec, pin_nid,
1215 HDA_MAX_CONNECTIONS);
1220 static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll)
1222 struct hda_codec *codec = per_pin->codec;
1223 struct hdmi_spec *spec = codec->spec;
1224 struct hdmi_eld *eld = &spec->temp_eld;
1225 struct hdmi_eld *pin_eld = &per_pin->sink_eld;
1226 hda_nid_t pin_nid = per_pin->pin_nid;
1228 * Always execute a GetPinSense verb here, even when called from
1229 * hdmi_intrinsic_event; for some NVIDIA HW, the unsolicited
1230 * response's PD bit is not the real PD value, but indicates that
1231 * the real PD value changed. An older version of the HD-audio
1232 * specification worked this way. Hence, we just ignore the data in
1233 * the unsolicited response to avoid custom WARs.
1235 int present = snd_hda_pin_sense(codec, pin_nid);
1236 bool update_eld = false;
1237 bool eld_changed = false;
1239 pin_eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
1240 if (pin_eld->monitor_present)
1241 eld->eld_valid = !!(present & AC_PINSENSE_ELDV);
1243 eld->eld_valid = false;
1245 _snd_printd(SND_PR_VERBOSE,
1246 "HDMI status: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
1247 codec->addr, pin_nid, pin_eld->monitor_present, eld->eld_valid);
1249 if (eld->eld_valid) {
1250 if (snd_hdmi_get_eld(codec, pin_nid, eld->eld_buffer,
1251 &eld->eld_size) < 0)
1252 eld->eld_valid = false;
1254 memset(&eld->info, 0, sizeof(struct parsed_hdmi_eld));
1255 if (snd_hdmi_parse_eld(&eld->info, eld->eld_buffer,
1257 eld->eld_valid = false;
1260 if (eld->eld_valid) {
1261 snd_hdmi_show_eld(&eld->info);
1265 queue_delayed_work(codec->bus->workq,
1267 msecs_to_jiffies(300));
1272 mutex_lock(&pin_eld->lock);
1273 if (pin_eld->eld_valid && !eld->eld_valid) {
1278 bool old_eld_valid = pin_eld->eld_valid;
1279 pin_eld->eld_valid = eld->eld_valid;
1280 eld_changed = pin_eld->eld_size != eld->eld_size ||
1281 memcmp(pin_eld->eld_buffer, eld->eld_buffer,
1282 eld->eld_size) != 0;
1284 memcpy(pin_eld->eld_buffer, eld->eld_buffer,
1286 pin_eld->eld_size = eld->eld_size;
1287 pin_eld->info = eld->info;
1289 /* Haswell-specific workaround: re-setup when the transcoder is
1290 * changed during the stream playback
1292 if (codec->vendor_id == 0x80862807 &&
1293 eld->eld_valid && !old_eld_valid && per_pin->setup) {
1294 snd_hda_codec_write(codec, pin_nid, 0,
1295 AC_VERB_SET_AMP_GAIN_MUTE,
1297 hdmi_setup_audio_infoframe(codec, per_pin,
1301 mutex_unlock(&pin_eld->lock);
1304 snd_ctl_notify(codec->bus->card,
1305 SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
1306 &per_pin->eld_ctl->id);
1309 static void hdmi_repoll_eld(struct work_struct *work)
1311 struct hdmi_spec_per_pin *per_pin =
1312 container_of(to_delayed_work(work), struct hdmi_spec_per_pin, work);
1314 if (per_pin->repoll_count++ > 6)
1315 per_pin->repoll_count = 0;
1317 hdmi_present_sense(per_pin, per_pin->repoll_count);
1320 static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
1323 static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
1325 struct hdmi_spec *spec = codec->spec;
1326 unsigned int caps, config;
1328 struct hdmi_spec_per_pin *per_pin;
1331 caps = snd_hda_query_pin_caps(codec, pin_nid);
1332 if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP)))
1335 config = snd_hda_codec_get_pincfg(codec, pin_nid);
1336 if (get_defcfg_connect(config) == AC_JACK_PORT_NONE)
1339 if (codec->vendor_id == 0x80862807)
1340 intel_haswell_fixup_connect_list(codec, pin_nid);
1342 pin_idx = spec->num_pins;
1343 per_pin = snd_array_new(&spec->pins);
1347 per_pin->pin_nid = pin_nid;
1348 per_pin->non_pcm = false;
1350 err = hdmi_read_pin_conn(codec, pin_idx);
1359 static int hdmi_add_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
1361 struct hdmi_spec *spec = codec->spec;
1362 struct hdmi_spec_per_cvt *per_cvt;
1366 chans = get_wcaps(codec, cvt_nid);
1367 chans = get_wcaps_channels(chans);
1369 per_cvt = snd_array_new(&spec->cvts);
1373 per_cvt->cvt_nid = cvt_nid;
1374 per_cvt->channels_min = 2;
1376 per_cvt->channels_max = chans;
1377 if (chans > spec->channels_max)
1378 spec->channels_max = chans;
1381 err = snd_hda_query_supported_pcm(codec, cvt_nid,
1388 if (spec->num_cvts < ARRAY_SIZE(spec->cvt_nids))
1389 spec->cvt_nids[spec->num_cvts] = cvt_nid;
1395 static int hdmi_parse_codec(struct hda_codec *codec)
1400 nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid);
1401 if (!nid || nodes < 0) {
1402 snd_printk(KERN_WARNING "HDMI: failed to get afg sub nodes\n");
1406 for (i = 0; i < nodes; i++, nid++) {
1410 caps = get_wcaps(codec, nid);
1411 type = get_wcaps_type(caps);
1413 if (!(caps & AC_WCAP_DIGITAL))
1417 case AC_WID_AUD_OUT:
1418 hdmi_add_cvt(codec, nid);
1421 hdmi_add_pin(codec, nid);
1427 /* We're seeing some problems with unsolicited hot plug events on
1428 * PantherPoint after S3, if this is not enabled */
1429 if (codec->vendor_id == 0x80862806)
1430 codec->bus->power_keep_link_on = 1;
1432 * G45/IbexPeak don't support EPSS: the unsolicited pin hot plug event
1433 * can be lost and presence sense verb will become inaccurate if the
1434 * HDA link is powered off at hot plug or hw initialization time.
1436 else if (!(snd_hda_param_read(codec, codec->afg, AC_PAR_POWER_STATE) &
1438 codec->bus->power_keep_link_on = 1;
1446 static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
1448 struct hda_spdif_out *spdif;
1451 mutex_lock(&codec->spdif_mutex);
1452 spdif = snd_hda_spdif_out_of_nid(codec, cvt_nid);
1453 non_pcm = !!(spdif->status & IEC958_AES0_NONAUDIO);
1454 mutex_unlock(&codec->spdif_mutex);
1463 static int generic_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
1464 struct hda_codec *codec,
1465 unsigned int stream_tag,
1466 unsigned int format,
1467 struct snd_pcm_substream *substream)
1469 hda_nid_t cvt_nid = hinfo->nid;
1470 struct hdmi_spec *spec = codec->spec;
1471 int pin_idx = hinfo_to_pin_index(spec, hinfo);
1472 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1473 hda_nid_t pin_nid = per_pin->pin_nid;
1476 non_pcm = check_non_pcm_per_cvt(codec, cvt_nid);
1477 per_pin->channels = substream->runtime->channels;
1478 per_pin->setup = true;
1480 hdmi_set_channel_count(codec, cvt_nid, substream->runtime->channels);
1482 hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
1484 return hdmi_setup_stream(codec, cvt_nid, pin_nid, stream_tag, format);
1487 static int generic_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
1488 struct hda_codec *codec,
1489 struct snd_pcm_substream *substream)
1491 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
1495 static int hdmi_pcm_close(struct hda_pcm_stream *hinfo,
1496 struct hda_codec *codec,
1497 struct snd_pcm_substream *substream)
1499 struct hdmi_spec *spec = codec->spec;
1500 int cvt_idx, pin_idx;
1501 struct hdmi_spec_per_cvt *per_cvt;
1502 struct hdmi_spec_per_pin *per_pin;
1505 cvt_idx = cvt_nid_to_cvt_index(spec, hinfo->nid);
1506 if (snd_BUG_ON(cvt_idx < 0))
1508 per_cvt = get_cvt(spec, cvt_idx);
1510 snd_BUG_ON(!per_cvt->assigned);
1511 per_cvt->assigned = 0;
1514 pin_idx = hinfo_to_pin_index(spec, hinfo);
1515 if (snd_BUG_ON(pin_idx < 0))
1517 per_pin = get_pin(spec, pin_idx);
1519 snd_hda_spdif_ctls_unassign(codec, pin_idx);
1520 per_pin->chmap_set = false;
1521 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
1523 per_pin->setup = false;
1524 per_pin->channels = 0;
1530 static const struct hda_pcm_ops generic_ops = {
1531 .open = hdmi_pcm_open,
1532 .close = hdmi_pcm_close,
1533 .prepare = generic_hdmi_playback_pcm_prepare,
1534 .cleanup = generic_hdmi_playback_pcm_cleanup,
1538 * ALSA API channel-map control callbacks
1540 static int hdmi_chmap_ctl_info(struct snd_kcontrol *kcontrol,
1541 struct snd_ctl_elem_info *uinfo)
1543 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1544 struct hda_codec *codec = info->private_data;
1545 struct hdmi_spec *spec = codec->spec;
1546 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1547 uinfo->count = spec->channels_max;
1548 uinfo->value.integer.min = 0;
1549 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
1553 static int hdmi_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1554 unsigned int size, unsigned int __user *tlv)
1556 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1557 struct hda_codec *codec = info->private_data;
1558 struct hdmi_spec *spec = codec->spec;
1559 const unsigned int valid_mask =
1560 FL | FR | RL | RR | LFE | FC | RLC | RRC;
1561 unsigned int __user *dst;
1566 if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
1570 for (chs = 2; chs <= spec->channels_max; chs++) {
1572 struct cea_channel_speaker_allocation *cap;
1573 cap = channel_allocations;
1574 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++, cap++) {
1575 int chs_bytes = chs * 4;
1576 if (cap->channels != chs)
1578 if (cap->spk_mask & ~valid_mask)
1582 if (put_user(SNDRV_CTL_TLVT_CHMAP_VAR, dst) ||
1583 put_user(chs_bytes, dst + 1))
1588 if (size < chs_bytes)
1592 for (c = 7; c >= 0; c--) {
1593 int spk = cap->speakers[c];
1596 if (put_user(spk_to_chmap(spk), dst))
1602 if (put_user(count, tlv + 1))
1607 static int hdmi_chmap_ctl_get(struct snd_kcontrol *kcontrol,
1608 struct snd_ctl_elem_value *ucontrol)
1610 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1611 struct hda_codec *codec = info->private_data;
1612 struct hdmi_spec *spec = codec->spec;
1613 int pin_idx = kcontrol->private_value;
1614 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1617 for (i = 0; i < ARRAY_SIZE(per_pin->chmap); i++)
1618 ucontrol->value.integer.value[i] = per_pin->chmap[i];
1622 static int hdmi_chmap_ctl_put(struct snd_kcontrol *kcontrol,
1623 struct snd_ctl_elem_value *ucontrol)
1625 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1626 struct hda_codec *codec = info->private_data;
1627 struct hdmi_spec *spec = codec->spec;
1628 int pin_idx = kcontrol->private_value;
1629 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1630 unsigned int ctl_idx;
1631 struct snd_pcm_substream *substream;
1632 unsigned char chmap[8];
1633 int i, ca, prepared = 0;
1635 ctl_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1636 substream = snd_pcm_chmap_substream(info, ctl_idx);
1637 if (!substream || !substream->runtime)
1638 return 0; /* just for avoiding error from alsactl restore */
1639 switch (substream->runtime->status->state) {
1640 case SNDRV_PCM_STATE_OPEN:
1641 case SNDRV_PCM_STATE_SETUP:
1643 case SNDRV_PCM_STATE_PREPARED:
1649 memset(chmap, 0, sizeof(chmap));
1650 for (i = 0; i < ARRAY_SIZE(chmap); i++)
1651 chmap[i] = ucontrol->value.integer.value[i];
1652 if (!memcmp(chmap, per_pin->chmap, sizeof(chmap)))
1654 ca = hdmi_manual_channel_allocation(ARRAY_SIZE(chmap), chmap);
1657 per_pin->chmap_set = true;
1658 memcpy(per_pin->chmap, chmap, sizeof(chmap));
1660 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
1665 static int generic_hdmi_build_pcms(struct hda_codec *codec)
1667 struct hdmi_spec *spec = codec->spec;
1670 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1671 struct hda_pcm *info;
1672 struct hda_pcm_stream *pstr;
1673 struct hdmi_spec_per_pin *per_pin;
1675 per_pin = get_pin(spec, pin_idx);
1676 sprintf(per_pin->pcm_name, "HDMI %d", pin_idx);
1677 info = snd_array_new(&spec->pcm_rec);
1680 info->name = per_pin->pcm_name;
1681 info->pcm_type = HDA_PCM_TYPE_HDMI;
1682 info->own_chmap = true;
1684 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
1685 pstr->substreams = 1;
1686 pstr->ops = generic_ops;
1687 /* other pstr fields are set in open */
1690 codec->num_pcms = spec->num_pins;
1691 codec->pcm_info = spec->pcm_rec.list;
1696 static int generic_hdmi_build_jack(struct hda_codec *codec, int pin_idx)
1698 char hdmi_str[32] = "HDMI/DP";
1699 struct hdmi_spec *spec = codec->spec;
1700 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1701 int pcmdev = get_pcm_rec(spec, pin_idx)->device;
1704 sprintf(hdmi_str + strlen(hdmi_str), ",pcm=%d", pcmdev);
1705 if (!is_jack_detectable(codec, per_pin->pin_nid))
1706 strncat(hdmi_str, " Phantom",
1707 sizeof(hdmi_str) - strlen(hdmi_str) - 1);
1709 return snd_hda_jack_add_kctl(codec, per_pin->pin_nid, hdmi_str, 0);
1712 static int generic_hdmi_build_controls(struct hda_codec *codec)
1714 struct hdmi_spec *spec = codec->spec;
1718 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1719 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1721 err = generic_hdmi_build_jack(codec, pin_idx);
1725 err = snd_hda_create_dig_out_ctls(codec,
1727 per_pin->mux_nids[0],
1731 snd_hda_spdif_ctls_unassign(codec, pin_idx);
1733 /* add control for ELD Bytes */
1734 err = hdmi_create_eld_ctl(codec, pin_idx,
1735 get_pcm_rec(spec, pin_idx)->device);
1740 hdmi_present_sense(per_pin, 0);
1743 /* add channel maps */
1744 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1745 struct snd_pcm_chmap *chmap;
1746 struct snd_kcontrol *kctl;
1749 if (!codec->pcm_info[pin_idx].pcm)
1751 err = snd_pcm_add_chmap_ctls(codec->pcm_info[pin_idx].pcm,
1752 SNDRV_PCM_STREAM_PLAYBACK,
1753 NULL, 0, pin_idx, &chmap);
1756 /* override handlers */
1757 chmap->private_data = codec;
1759 for (i = 0; i < kctl->count; i++)
1760 kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_WRITE;
1761 kctl->info = hdmi_chmap_ctl_info;
1762 kctl->get = hdmi_chmap_ctl_get;
1763 kctl->put = hdmi_chmap_ctl_put;
1764 kctl->tlv.c = hdmi_chmap_ctl_tlv;
1770 static int generic_hdmi_init_per_pins(struct hda_codec *codec)
1772 struct hdmi_spec *spec = codec->spec;
1775 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1776 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1777 struct hdmi_eld *eld = &per_pin->sink_eld;
1779 per_pin->codec = codec;
1780 mutex_init(&eld->lock);
1781 INIT_DELAYED_WORK(&per_pin->work, hdmi_repoll_eld);
1782 snd_hda_eld_proc_new(codec, eld, pin_idx);
1787 static int generic_hdmi_init(struct hda_codec *codec)
1789 struct hdmi_spec *spec = codec->spec;
1792 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1793 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1794 hda_nid_t pin_nid = per_pin->pin_nid;
1796 hdmi_init_pin(codec, pin_nid);
1797 snd_hda_jack_detect_enable(codec, pin_nid, pin_nid);
1802 static void hdmi_array_init(struct hdmi_spec *spec, int nums)
1804 snd_array_init(&spec->pins, sizeof(struct hdmi_spec_per_pin), nums);
1805 snd_array_init(&spec->cvts, sizeof(struct hdmi_spec_per_cvt), nums);
1806 snd_array_init(&spec->pcm_rec, sizeof(struct hda_pcm), nums);
1809 static void hdmi_array_free(struct hdmi_spec *spec)
1811 snd_array_free(&spec->pins);
1812 snd_array_free(&spec->cvts);
1813 snd_array_free(&spec->pcm_rec);
1816 static void generic_hdmi_free(struct hda_codec *codec)
1818 struct hdmi_spec *spec = codec->spec;
1821 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1822 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1823 struct hdmi_eld *eld = &per_pin->sink_eld;
1825 cancel_delayed_work(&per_pin->work);
1826 snd_hda_eld_proc_free(codec, eld);
1829 flush_workqueue(codec->bus->workq);
1830 hdmi_array_free(spec);
1834 static const struct hda_codec_ops generic_hdmi_patch_ops = {
1835 .init = generic_hdmi_init,
1836 .free = generic_hdmi_free,
1837 .build_pcms = generic_hdmi_build_pcms,
1838 .build_controls = generic_hdmi_build_controls,
1839 .unsol_event = hdmi_unsol_event,
1843 static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
1846 struct hdmi_spec *spec = codec->spec;
1850 nconns = snd_hda_get_connections(codec, nid, conns, ARRAY_SIZE(conns));
1851 if (nconns == spec->num_cvts &&
1852 !memcmp(conns, spec->cvt_nids, spec->num_cvts * sizeof(hda_nid_t)))
1855 /* override pins connection list */
1856 snd_printdd("hdmi: haswell: override pin connection 0x%x\n", nid);
1857 nconns = max(spec->num_cvts, 4);
1858 snd_hda_override_conn_list(codec, nid, spec->num_cvts, spec->cvt_nids);
1861 #define INTEL_VENDOR_NID 0x08
1862 #define INTEL_GET_VENDOR_VERB 0xf81
1863 #define INTEL_SET_VENDOR_VERB 0x781
1864 #define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
1865 #define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
1867 static void intel_haswell_enable_all_pins(struct hda_codec *codec,
1870 unsigned int vendor_param;
1872 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
1873 INTEL_GET_VENDOR_VERB, 0);
1874 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1877 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1878 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
1879 INTEL_SET_VENDOR_VERB, vendor_param);
1880 if (vendor_param == -1)
1884 snd_hda_codec_update_widgets(codec);
1887 static void intel_haswell_fixup_enable_dp12(struct hda_codec *codec)
1889 unsigned int vendor_param;
1891 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
1892 INTEL_GET_VENDOR_VERB, 0);
1893 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1896 /* enable DP1.2 mode */
1897 vendor_param |= INTEL_EN_DP12;
1898 snd_hda_codec_write_cache(codec, INTEL_VENDOR_NID, 0,
1899 INTEL_SET_VENDOR_VERB, vendor_param);
1902 /* Haswell needs to re-issue the vendor-specific verbs before turning to D0.
1903 * Otherwise you may get severe h/w communication errors.
1905 static void haswell_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1906 unsigned int power_state)
1908 if (power_state == AC_PWRST_D0) {
1909 intel_haswell_enable_all_pins(codec, false);
1910 intel_haswell_fixup_enable_dp12(codec);
1913 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE, power_state);
1914 snd_hda_codec_set_power_to_all(codec, fg, power_state);
1917 static int patch_generic_hdmi(struct hda_codec *codec)
1919 struct hdmi_spec *spec;
1921 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1926 hdmi_array_init(spec, 4);
1928 if (codec->vendor_id == 0x80862807) {
1929 intel_haswell_enable_all_pins(codec, true);
1930 intel_haswell_fixup_enable_dp12(codec);
1933 if (hdmi_parse_codec(codec) < 0) {
1938 codec->patch_ops = generic_hdmi_patch_ops;
1939 if (codec->vendor_id == 0x80862807)
1940 codec->patch_ops.set_power_state = haswell_set_power_state;
1942 generic_hdmi_init_per_pins(codec);
1944 init_channel_allocations();
1950 * Shared non-generic implementations
1953 static int simple_playback_build_pcms(struct hda_codec *codec)
1955 struct hdmi_spec *spec = codec->spec;
1956 struct hda_pcm *info;
1958 struct hda_pcm_stream *pstr;
1959 struct hdmi_spec_per_cvt *per_cvt;
1961 per_cvt = get_cvt(spec, 0);
1962 chans = get_wcaps(codec, per_cvt->cvt_nid);
1963 chans = get_wcaps_channels(chans);
1965 info = snd_array_new(&spec->pcm_rec);
1968 info->name = get_pin(spec, 0)->pcm_name;
1969 sprintf(info->name, "HDMI 0");
1970 info->pcm_type = HDA_PCM_TYPE_HDMI;
1971 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
1972 *pstr = spec->pcm_playback;
1973 pstr->nid = per_cvt->cvt_nid;
1974 if (pstr->channels_max <= 2 && chans && chans <= 16)
1975 pstr->channels_max = chans;
1977 codec->num_pcms = 1;
1978 codec->pcm_info = info;
1983 /* unsolicited event for jack sensing */
1984 static void simple_hdmi_unsol_event(struct hda_codec *codec,
1987 snd_hda_jack_set_dirty_all(codec);
1988 snd_hda_jack_report_sync(codec);
1991 /* generic_hdmi_build_jack can be used for simple_hdmi, too,
1992 * as long as spec->pins[] is set correctly
1994 #define simple_hdmi_build_jack generic_hdmi_build_jack
1996 static int simple_playback_build_controls(struct hda_codec *codec)
1998 struct hdmi_spec *spec = codec->spec;
1999 struct hdmi_spec_per_cvt *per_cvt;
2002 per_cvt = get_cvt(spec, 0);
2003 err = snd_hda_create_spdif_out_ctls(codec, per_cvt->cvt_nid,
2007 return simple_hdmi_build_jack(codec, 0);
2010 static int simple_playback_init(struct hda_codec *codec)
2012 struct hdmi_spec *spec = codec->spec;
2013 struct hdmi_spec_per_pin *per_pin = get_pin(spec, 0);
2014 hda_nid_t pin = per_pin->pin_nid;
2016 snd_hda_codec_write(codec, pin, 0,
2017 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
2018 /* some codecs require to unmute the pin */
2019 if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
2020 snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
2022 snd_hda_jack_detect_enable(codec, pin, pin);
2026 static void simple_playback_free(struct hda_codec *codec)
2028 struct hdmi_spec *spec = codec->spec;
2030 hdmi_array_free(spec);
2035 * Nvidia specific implementations
2038 #define Nv_VERB_SET_Channel_Allocation 0xF79
2039 #define Nv_VERB_SET_Info_Frame_Checksum 0xF7A
2040 #define Nv_VERB_SET_Audio_Protection_On 0xF98
2041 #define Nv_VERB_SET_Audio_Protection_Off 0xF99
2043 #define nvhdmi_master_con_nid_7x 0x04
2044 #define nvhdmi_master_pin_nid_7x 0x05
2046 static const hda_nid_t nvhdmi_con_nids_7x[4] = {
2047 /*front, rear, clfe, rear_surr */
2051 static const struct hda_verb nvhdmi_basic_init_7x_2ch[] = {
2052 /* set audio protect on */
2053 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
2054 /* enable digital output on pin widget */
2055 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2059 static const struct hda_verb nvhdmi_basic_init_7x_8ch[] = {
2060 /* set audio protect on */
2061 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
2062 /* enable digital output on pin widget */
2063 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2064 { 0x7, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2065 { 0x9, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2066 { 0xb, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2067 { 0xd, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2071 #ifdef LIMITED_RATE_FMT_SUPPORT
2072 /* support only the safe format and rate */
2073 #define SUPPORTED_RATES SNDRV_PCM_RATE_48000
2074 #define SUPPORTED_MAXBPS 16
2075 #define SUPPORTED_FORMATS SNDRV_PCM_FMTBIT_S16_LE
2077 /* support all rates and formats */
2078 #define SUPPORTED_RATES \
2079 (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
2080 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
2081 SNDRV_PCM_RATE_192000)
2082 #define SUPPORTED_MAXBPS 24
2083 #define SUPPORTED_FORMATS \
2084 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
2087 static int nvhdmi_7x_init_2ch(struct hda_codec *codec)
2089 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_2ch);
2093 static int nvhdmi_7x_init_8ch(struct hda_codec *codec)
2095 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_8ch);
2099 static unsigned int channels_2_6_8[] = {
2103 static unsigned int channels_2_8[] = {
2107 static struct snd_pcm_hw_constraint_list hw_constraints_2_6_8_channels = {
2108 .count = ARRAY_SIZE(channels_2_6_8),
2109 .list = channels_2_6_8,
2113 static struct snd_pcm_hw_constraint_list hw_constraints_2_8_channels = {
2114 .count = ARRAY_SIZE(channels_2_8),
2115 .list = channels_2_8,
2119 static int simple_playback_pcm_open(struct hda_pcm_stream *hinfo,
2120 struct hda_codec *codec,
2121 struct snd_pcm_substream *substream)
2123 struct hdmi_spec *spec = codec->spec;
2124 struct snd_pcm_hw_constraint_list *hw_constraints_channels = NULL;
2126 switch (codec->preset->id) {
2131 hw_constraints_channels = &hw_constraints_2_8_channels;
2134 hw_constraints_channels = &hw_constraints_2_6_8_channels;
2140 if (hw_constraints_channels != NULL) {
2141 snd_pcm_hw_constraint_list(substream->runtime, 0,
2142 SNDRV_PCM_HW_PARAM_CHANNELS,
2143 hw_constraints_channels);
2145 snd_pcm_hw_constraint_step(substream->runtime, 0,
2146 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
2149 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
2152 static int simple_playback_pcm_close(struct hda_pcm_stream *hinfo,
2153 struct hda_codec *codec,
2154 struct snd_pcm_substream *substream)
2156 struct hdmi_spec *spec = codec->spec;
2157 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
2160 static int simple_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2161 struct hda_codec *codec,
2162 unsigned int stream_tag,
2163 unsigned int format,
2164 struct snd_pcm_substream *substream)
2166 struct hdmi_spec *spec = codec->spec;
2167 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
2168 stream_tag, format, substream);
2171 static const struct hda_pcm_stream simple_pcm_playback = {
2176 .open = simple_playback_pcm_open,
2177 .close = simple_playback_pcm_close,
2178 .prepare = simple_playback_pcm_prepare
2182 static const struct hda_codec_ops simple_hdmi_patch_ops = {
2183 .build_controls = simple_playback_build_controls,
2184 .build_pcms = simple_playback_build_pcms,
2185 .init = simple_playback_init,
2186 .free = simple_playback_free,
2187 .unsol_event = simple_hdmi_unsol_event,
2190 static int patch_simple_hdmi(struct hda_codec *codec,
2191 hda_nid_t cvt_nid, hda_nid_t pin_nid)
2193 struct hdmi_spec *spec;
2194 struct hdmi_spec_per_cvt *per_cvt;
2195 struct hdmi_spec_per_pin *per_pin;
2197 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2202 hdmi_array_init(spec, 1);
2204 spec->multiout.num_dacs = 0; /* no analog */
2205 spec->multiout.max_channels = 2;
2206 spec->multiout.dig_out_nid = cvt_nid;
2209 per_pin = snd_array_new(&spec->pins);
2210 per_cvt = snd_array_new(&spec->cvts);
2211 if (!per_pin || !per_cvt) {
2212 simple_playback_free(codec);
2215 per_cvt->cvt_nid = cvt_nid;
2216 per_pin->pin_nid = pin_nid;
2217 spec->pcm_playback = simple_pcm_playback;
2219 codec->patch_ops = simple_hdmi_patch_ops;
2224 static void nvhdmi_8ch_7x_set_info_frame_parameters(struct hda_codec *codec,
2227 unsigned int chanmask;
2228 int chan = channels ? (channels - 1) : 1;
2247 /* Set the audio infoframe channel allocation and checksum fields. The
2248 * channel count is computed implicitly by the hardware. */
2249 snd_hda_codec_write(codec, 0x1, 0,
2250 Nv_VERB_SET_Channel_Allocation, chanmask);
2252 snd_hda_codec_write(codec, 0x1, 0,
2253 Nv_VERB_SET_Info_Frame_Checksum,
2254 (0x71 - chan - chanmask));
2257 static int nvhdmi_8ch_7x_pcm_close(struct hda_pcm_stream *hinfo,
2258 struct hda_codec *codec,
2259 struct snd_pcm_substream *substream)
2261 struct hdmi_spec *spec = codec->spec;
2264 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x,
2265 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
2266 for (i = 0; i < 4; i++) {
2267 /* set the stream id */
2268 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
2269 AC_VERB_SET_CHANNEL_STREAMID, 0);
2270 /* set the stream format */
2271 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
2272 AC_VERB_SET_STREAM_FORMAT, 0);
2275 /* The audio hardware sends a channel count of 0x7 (8ch) when all the
2276 * streams are disabled. */
2277 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
2279 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
2282 static int nvhdmi_8ch_7x_pcm_prepare(struct hda_pcm_stream *hinfo,
2283 struct hda_codec *codec,
2284 unsigned int stream_tag,
2285 unsigned int format,
2286 struct snd_pcm_substream *substream)
2289 unsigned int dataDCC2, channel_id;
2291 struct hdmi_spec *spec = codec->spec;
2292 struct hda_spdif_out *spdif;
2293 struct hdmi_spec_per_cvt *per_cvt;
2295 mutex_lock(&codec->spdif_mutex);
2296 per_cvt = get_cvt(spec, 0);
2297 spdif = snd_hda_spdif_out_of_nid(codec, per_cvt->cvt_nid);
2299 chs = substream->runtime->channels;
2303 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
2304 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
2305 snd_hda_codec_write(codec,
2306 nvhdmi_master_con_nid_7x,
2308 AC_VERB_SET_DIGI_CONVERT_1,
2309 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
2311 /* set the stream id */
2312 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
2313 AC_VERB_SET_CHANNEL_STREAMID, (stream_tag << 4) | 0x0);
2315 /* set the stream format */
2316 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
2317 AC_VERB_SET_STREAM_FORMAT, format);
2319 /* turn on again (if needed) */
2320 /* enable and set the channel status audio/data flag */
2321 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE)) {
2322 snd_hda_codec_write(codec,
2323 nvhdmi_master_con_nid_7x,
2325 AC_VERB_SET_DIGI_CONVERT_1,
2326 spdif->ctls & 0xff);
2327 snd_hda_codec_write(codec,
2328 nvhdmi_master_con_nid_7x,
2330 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
2333 for (i = 0; i < 4; i++) {
2339 /* turn off SPDIF once;
2340 *otherwise the IEC958 bits won't be updated
2342 if (codec->spdif_status_reset &&
2343 (spdif->ctls & AC_DIG1_ENABLE))
2344 snd_hda_codec_write(codec,
2345 nvhdmi_con_nids_7x[i],
2347 AC_VERB_SET_DIGI_CONVERT_1,
2348 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
2349 /* set the stream id */
2350 snd_hda_codec_write(codec,
2351 nvhdmi_con_nids_7x[i],
2353 AC_VERB_SET_CHANNEL_STREAMID,
2354 (stream_tag << 4) | channel_id);
2355 /* set the stream format */
2356 snd_hda_codec_write(codec,
2357 nvhdmi_con_nids_7x[i],
2359 AC_VERB_SET_STREAM_FORMAT,
2361 /* turn on again (if needed) */
2362 /* enable and set the channel status audio/data flag */
2363 if (codec->spdif_status_reset &&
2364 (spdif->ctls & AC_DIG1_ENABLE)) {
2365 snd_hda_codec_write(codec,
2366 nvhdmi_con_nids_7x[i],
2368 AC_VERB_SET_DIGI_CONVERT_1,
2369 spdif->ctls & 0xff);
2370 snd_hda_codec_write(codec,
2371 nvhdmi_con_nids_7x[i],
2373 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
2377 nvhdmi_8ch_7x_set_info_frame_parameters(codec, chs);
2379 mutex_unlock(&codec->spdif_mutex);
2383 static const struct hda_pcm_stream nvhdmi_pcm_playback_8ch_7x = {
2387 .nid = nvhdmi_master_con_nid_7x,
2388 .rates = SUPPORTED_RATES,
2389 .maxbps = SUPPORTED_MAXBPS,
2390 .formats = SUPPORTED_FORMATS,
2392 .open = simple_playback_pcm_open,
2393 .close = nvhdmi_8ch_7x_pcm_close,
2394 .prepare = nvhdmi_8ch_7x_pcm_prepare
2398 static int patch_nvhdmi_2ch(struct hda_codec *codec)
2400 struct hdmi_spec *spec;
2401 int err = patch_simple_hdmi(codec, nvhdmi_master_con_nid_7x,
2402 nvhdmi_master_pin_nid_7x);
2406 codec->patch_ops.init = nvhdmi_7x_init_2ch;
2407 /* override the PCM rates, etc, as the codec doesn't give full list */
2409 spec->pcm_playback.rates = SUPPORTED_RATES;
2410 spec->pcm_playback.maxbps = SUPPORTED_MAXBPS;
2411 spec->pcm_playback.formats = SUPPORTED_FORMATS;
2415 static int nvhdmi_7x_8ch_build_pcms(struct hda_codec *codec)
2417 struct hdmi_spec *spec = codec->spec;
2418 int err = simple_playback_build_pcms(codec);
2420 struct hda_pcm *info = get_pcm_rec(spec, 0);
2421 info->own_chmap = true;
2426 static int nvhdmi_7x_8ch_build_controls(struct hda_codec *codec)
2428 struct hdmi_spec *spec = codec->spec;
2429 struct hda_pcm *info;
2430 struct snd_pcm_chmap *chmap;
2433 err = simple_playback_build_controls(codec);
2437 /* add channel maps */
2438 info = get_pcm_rec(spec, 0);
2439 err = snd_pcm_add_chmap_ctls(info->pcm,
2440 SNDRV_PCM_STREAM_PLAYBACK,
2441 snd_pcm_alt_chmaps, 8, 0, &chmap);
2444 switch (codec->preset->id) {
2449 chmap->channel_mask = (1U << 2) | (1U << 8);
2452 chmap->channel_mask = (1U << 2) | (1U << 6) | (1U << 8);
2457 static int patch_nvhdmi_8ch_7x(struct hda_codec *codec)
2459 struct hdmi_spec *spec;
2460 int err = patch_nvhdmi_2ch(codec);
2464 spec->multiout.max_channels = 8;
2465 spec->pcm_playback = nvhdmi_pcm_playback_8ch_7x;
2466 codec->patch_ops.init = nvhdmi_7x_init_8ch;
2467 codec->patch_ops.build_pcms = nvhdmi_7x_8ch_build_pcms;
2468 codec->patch_ops.build_controls = nvhdmi_7x_8ch_build_controls;
2470 /* Initialize the audio infoframe channel mask and checksum to something
2472 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
2478 * ATI-specific implementations
2480 * FIXME: we may omit the whole this and use the generic code once after
2481 * it's confirmed to work.
2484 #define ATIHDMI_CVT_NID 0x02 /* audio converter */
2485 #define ATIHDMI_PIN_NID 0x03 /* HDMI output pin */
2487 static int atihdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2488 struct hda_codec *codec,
2489 unsigned int stream_tag,
2490 unsigned int format,
2491 struct snd_pcm_substream *substream)
2493 struct hdmi_spec *spec = codec->spec;
2494 struct hdmi_spec_per_cvt *per_cvt = get_cvt(spec, 0);
2495 int chans = substream->runtime->channels;
2498 err = simple_playback_pcm_prepare(hinfo, codec, stream_tag, format,
2502 snd_hda_codec_write(codec, per_cvt->cvt_nid, 0,
2503 AC_VERB_SET_CVT_CHAN_COUNT, chans - 1);
2505 for (i = 0; i < chans; i++) {
2506 snd_hda_codec_write(codec, per_cvt->cvt_nid, 0,
2507 AC_VERB_SET_HDMI_CHAN_SLOT,
2513 static int patch_atihdmi(struct hda_codec *codec)
2515 struct hdmi_spec *spec;
2516 int err = patch_simple_hdmi(codec, ATIHDMI_CVT_NID, ATIHDMI_PIN_NID);
2520 spec->pcm_playback.ops.prepare = atihdmi_playback_pcm_prepare;
2524 /* VIA HDMI Implementation */
2525 #define VIAHDMI_CVT_NID 0x02 /* audio converter1 */
2526 #define VIAHDMI_PIN_NID 0x03 /* HDMI output pin1 */
2528 static int patch_via_hdmi(struct hda_codec *codec)
2530 return patch_simple_hdmi(codec, VIAHDMI_CVT_NID, VIAHDMI_PIN_NID);
2536 static const struct hda_codec_preset snd_hda_preset_hdmi[] = {
2537 { .id = 0x1002793c, .name = "RS600 HDMI", .patch = patch_atihdmi },
2538 { .id = 0x10027919, .name = "RS600 HDMI", .patch = patch_atihdmi },
2539 { .id = 0x1002791a, .name = "RS690/780 HDMI", .patch = patch_atihdmi },
2540 { .id = 0x1002aa01, .name = "R6xx HDMI", .patch = patch_generic_hdmi },
2541 { .id = 0x10951390, .name = "SiI1390 HDMI", .patch = patch_generic_hdmi },
2542 { .id = 0x10951392, .name = "SiI1392 HDMI", .patch = patch_generic_hdmi },
2543 { .id = 0x17e80047, .name = "Chrontel HDMI", .patch = patch_generic_hdmi },
2544 { .id = 0x10de0002, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2545 { .id = 0x10de0003, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2546 { .id = 0x10de0005, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2547 { .id = 0x10de0006, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2548 { .id = 0x10de0007, .name = "MCP79/7A HDMI", .patch = patch_nvhdmi_8ch_7x },
2549 { .id = 0x10de000a, .name = "GPU 0a HDMI/DP", .patch = patch_generic_hdmi },
2550 { .id = 0x10de000b, .name = "GPU 0b HDMI/DP", .patch = patch_generic_hdmi },
2551 { .id = 0x10de000c, .name = "MCP89 HDMI", .patch = patch_generic_hdmi },
2552 { .id = 0x10de000d, .name = "GPU 0d HDMI/DP", .patch = patch_generic_hdmi },
2553 { .id = 0x10de0010, .name = "GPU 10 HDMI/DP", .patch = patch_generic_hdmi },
2554 { .id = 0x10de0011, .name = "GPU 11 HDMI/DP", .patch = patch_generic_hdmi },
2555 { .id = 0x10de0012, .name = "GPU 12 HDMI/DP", .patch = patch_generic_hdmi },
2556 { .id = 0x10de0013, .name = "GPU 13 HDMI/DP", .patch = patch_generic_hdmi },
2557 { .id = 0x10de0014, .name = "GPU 14 HDMI/DP", .patch = patch_generic_hdmi },
2558 { .id = 0x10de0015, .name = "GPU 15 HDMI/DP", .patch = patch_generic_hdmi },
2559 { .id = 0x10de0016, .name = "GPU 16 HDMI/DP", .patch = patch_generic_hdmi },
2560 /* 17 is known to be absent */
2561 { .id = 0x10de0018, .name = "GPU 18 HDMI/DP", .patch = patch_generic_hdmi },
2562 { .id = 0x10de0019, .name = "GPU 19 HDMI/DP", .patch = patch_generic_hdmi },
2563 { .id = 0x10de001a, .name = "GPU 1a HDMI/DP", .patch = patch_generic_hdmi },
2564 { .id = 0x10de001b, .name = "GPU 1b HDMI/DP", .patch = patch_generic_hdmi },
2565 { .id = 0x10de001c, .name = "GPU 1c HDMI/DP", .patch = patch_generic_hdmi },
2566 { .id = 0x10de0040, .name = "GPU 40 HDMI/DP", .patch = patch_generic_hdmi },
2567 { .id = 0x10de0041, .name = "GPU 41 HDMI/DP", .patch = patch_generic_hdmi },
2568 { .id = 0x10de0042, .name = "GPU 42 HDMI/DP", .patch = patch_generic_hdmi },
2569 { .id = 0x10de0043, .name = "GPU 43 HDMI/DP", .patch = patch_generic_hdmi },
2570 { .id = 0x10de0044, .name = "GPU 44 HDMI/DP", .patch = patch_generic_hdmi },
2571 { .id = 0x10de0051, .name = "GPU 51 HDMI/DP", .patch = patch_generic_hdmi },
2572 { .id = 0x10de0060, .name = "GPU 60 HDMI/DP", .patch = patch_generic_hdmi },
2573 { .id = 0x10de0067, .name = "MCP67 HDMI", .patch = patch_nvhdmi_2ch },
2574 { .id = 0x10de8001, .name = "MCP73 HDMI", .patch = patch_nvhdmi_2ch },
2575 { .id = 0x11069f80, .name = "VX900 HDMI/DP", .patch = patch_via_hdmi },
2576 { .id = 0x11069f81, .name = "VX900 HDMI/DP", .patch = patch_via_hdmi },
2577 { .id = 0x11069f84, .name = "VX11 HDMI/DP", .patch = patch_generic_hdmi },
2578 { .id = 0x11069f85, .name = "VX11 HDMI/DP", .patch = patch_generic_hdmi },
2579 { .id = 0x80860054, .name = "IbexPeak HDMI", .patch = patch_generic_hdmi },
2580 { .id = 0x80862801, .name = "Bearlake HDMI", .patch = patch_generic_hdmi },
2581 { .id = 0x80862802, .name = "Cantiga HDMI", .patch = patch_generic_hdmi },
2582 { .id = 0x80862803, .name = "Eaglelake HDMI", .patch = patch_generic_hdmi },
2583 { .id = 0x80862804, .name = "IbexPeak HDMI", .patch = patch_generic_hdmi },
2584 { .id = 0x80862805, .name = "CougarPoint HDMI", .patch = patch_generic_hdmi },
2585 { .id = 0x80862806, .name = "PantherPoint HDMI", .patch = patch_generic_hdmi },
2586 { .id = 0x80862807, .name = "Haswell HDMI", .patch = patch_generic_hdmi },
2587 { .id = 0x80862880, .name = "CedarTrail HDMI", .patch = patch_generic_hdmi },
2588 { .id = 0x808629fb, .name = "Crestline HDMI", .patch = patch_generic_hdmi },
2592 MODULE_ALIAS("snd-hda-codec-id:1002793c");
2593 MODULE_ALIAS("snd-hda-codec-id:10027919");
2594 MODULE_ALIAS("snd-hda-codec-id:1002791a");
2595 MODULE_ALIAS("snd-hda-codec-id:1002aa01");
2596 MODULE_ALIAS("snd-hda-codec-id:10951390");
2597 MODULE_ALIAS("snd-hda-codec-id:10951392");
2598 MODULE_ALIAS("snd-hda-codec-id:10de0002");
2599 MODULE_ALIAS("snd-hda-codec-id:10de0003");
2600 MODULE_ALIAS("snd-hda-codec-id:10de0005");
2601 MODULE_ALIAS("snd-hda-codec-id:10de0006");
2602 MODULE_ALIAS("snd-hda-codec-id:10de0007");
2603 MODULE_ALIAS("snd-hda-codec-id:10de000a");
2604 MODULE_ALIAS("snd-hda-codec-id:10de000b");
2605 MODULE_ALIAS("snd-hda-codec-id:10de000c");
2606 MODULE_ALIAS("snd-hda-codec-id:10de000d");
2607 MODULE_ALIAS("snd-hda-codec-id:10de0010");
2608 MODULE_ALIAS("snd-hda-codec-id:10de0011");
2609 MODULE_ALIAS("snd-hda-codec-id:10de0012");
2610 MODULE_ALIAS("snd-hda-codec-id:10de0013");
2611 MODULE_ALIAS("snd-hda-codec-id:10de0014");
2612 MODULE_ALIAS("snd-hda-codec-id:10de0015");
2613 MODULE_ALIAS("snd-hda-codec-id:10de0016");
2614 MODULE_ALIAS("snd-hda-codec-id:10de0018");
2615 MODULE_ALIAS("snd-hda-codec-id:10de0019");
2616 MODULE_ALIAS("snd-hda-codec-id:10de001a");
2617 MODULE_ALIAS("snd-hda-codec-id:10de001b");
2618 MODULE_ALIAS("snd-hda-codec-id:10de001c");
2619 MODULE_ALIAS("snd-hda-codec-id:10de0040");
2620 MODULE_ALIAS("snd-hda-codec-id:10de0041");
2621 MODULE_ALIAS("snd-hda-codec-id:10de0042");
2622 MODULE_ALIAS("snd-hda-codec-id:10de0043");
2623 MODULE_ALIAS("snd-hda-codec-id:10de0044");
2624 MODULE_ALIAS("snd-hda-codec-id:10de0051");
2625 MODULE_ALIAS("snd-hda-codec-id:10de0060");
2626 MODULE_ALIAS("snd-hda-codec-id:10de0067");
2627 MODULE_ALIAS("snd-hda-codec-id:10de8001");
2628 MODULE_ALIAS("snd-hda-codec-id:11069f80");
2629 MODULE_ALIAS("snd-hda-codec-id:11069f81");
2630 MODULE_ALIAS("snd-hda-codec-id:11069f84");
2631 MODULE_ALIAS("snd-hda-codec-id:11069f85");
2632 MODULE_ALIAS("snd-hda-codec-id:17e80047");
2633 MODULE_ALIAS("snd-hda-codec-id:80860054");
2634 MODULE_ALIAS("snd-hda-codec-id:80862801");
2635 MODULE_ALIAS("snd-hda-codec-id:80862802");
2636 MODULE_ALIAS("snd-hda-codec-id:80862803");
2637 MODULE_ALIAS("snd-hda-codec-id:80862804");
2638 MODULE_ALIAS("snd-hda-codec-id:80862805");
2639 MODULE_ALIAS("snd-hda-codec-id:80862806");
2640 MODULE_ALIAS("snd-hda-codec-id:80862807");
2641 MODULE_ALIAS("snd-hda-codec-id:80862880");
2642 MODULE_ALIAS("snd-hda-codec-id:808629fb");
2644 MODULE_LICENSE("GPL");
2645 MODULE_DESCRIPTION("HDMI HD-audio codec");
2646 MODULE_ALIAS("snd-hda-codec-intelhdmi");
2647 MODULE_ALIAS("snd-hda-codec-nvhdmi");
2648 MODULE_ALIAS("snd-hda-codec-atihdmi");
2650 static struct hda_codec_preset_list intel_list = {
2651 .preset = snd_hda_preset_hdmi,
2652 .owner = THIS_MODULE,
2655 static int __init patch_hdmi_init(void)
2657 return snd_hda_add_codec_preset(&intel_list);
2660 static void __exit patch_hdmi_exit(void)
2662 snd_hda_delete_codec_preset(&intel_list);
2665 module_init(patch_hdmi_init)
2666 module_exit(patch_hdmi_exit)