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;
93 * Non-generic ATI/NVIDIA specific
95 struct hda_multi_out multiout;
96 struct hda_pcm_stream pcm_playback;
100 struct hdmi_audio_infoframe {
107 u8 CC02_CT47; /* CC in bits 0:2, CT in 4:7 */
111 u8 LFEPBL01_LSV36_DM_INH7;
114 struct dp_audio_infoframe {
117 u8 ver; /* 0x11 << 2 */
119 u8 CC02_CT47; /* match with HDMI infoframe from this on */
123 u8 LFEPBL01_LSV36_DM_INH7;
126 union audio_infoframe {
127 struct hdmi_audio_infoframe hdmi;
128 struct dp_audio_infoframe dp;
133 * CEA speaker placement:
136 * FLW FL FLC FC FRC FR FRW
143 * The Left/Right Surround channel _notions_ LS/RS in SMPTE 320M corresponds to
144 * CEA RL/RR; The SMPTE channel _assignment_ C/LFE is swapped to CEA LFE/FC.
146 enum cea_speaker_placement {
147 FL = (1 << 0), /* Front Left */
148 FC = (1 << 1), /* Front Center */
149 FR = (1 << 2), /* Front Right */
150 FLC = (1 << 3), /* Front Left Center */
151 FRC = (1 << 4), /* Front Right Center */
152 RL = (1 << 5), /* Rear Left */
153 RC = (1 << 6), /* Rear Center */
154 RR = (1 << 7), /* Rear Right */
155 RLC = (1 << 8), /* Rear Left Center */
156 RRC = (1 << 9), /* Rear Right Center */
157 LFE = (1 << 10), /* Low Frequency Effect */
158 FLW = (1 << 11), /* Front Left Wide */
159 FRW = (1 << 12), /* Front Right Wide */
160 FLH = (1 << 13), /* Front Left High */
161 FCH = (1 << 14), /* Front Center High */
162 FRH = (1 << 15), /* Front Right High */
163 TC = (1 << 16), /* Top Center */
167 * ELD SA bits in the CEA Speaker Allocation data block
169 static int eld_speaker_allocation_bits[] = {
177 /* the following are not defined in ELD yet */
184 struct cea_channel_speaker_allocation {
188 /* derived values, just for convenience */
196 * surround40 surround41 surround50 surround51 surround71
197 * ch0 front left = = = =
198 * ch1 front right = = = =
199 * ch2 rear left = = = =
200 * ch3 rear right = = = =
201 * ch4 LFE center center center
206 * surround71 = {FL, FR, RLC, RRC, FC, LFE, RL, RR}
208 static int hdmi_channel_mapping[0x32][8] = {
210 [0x00] = { 0x00, 0x11, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
212 [0x01] = { 0x00, 0x11, 0x22, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
214 [0x02] = { 0x00, 0x11, 0x23, 0xf2, 0xf4, 0xf5, 0xf6, 0xf7 },
216 [0x08] = { 0x00, 0x11, 0x24, 0x35, 0xf3, 0xf2, 0xf6, 0xf7 },
218 [0x03] = { 0x00, 0x11, 0x23, 0x32, 0x44, 0xf5, 0xf6, 0xf7 },
220 [0x09] = { 0x00, 0x11, 0x24, 0x35, 0x42, 0xf3, 0xf6, 0xf7 },
222 [0x0a] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0xf2, 0xf6, 0xf7 },
224 [0x0b] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0x52, 0xf6, 0xf7 },
226 [0x13] = { 0x00, 0x11, 0x26, 0x37, 0x43, 0x52, 0x64, 0x75 },
230 * This is an ordered list!
232 * The preceding ones have better chances to be selected by
233 * hdmi_channel_allocation().
235 static struct cea_channel_speaker_allocation channel_allocations[] = {
236 /* channel: 7 6 5 4 3 2 1 0 */
237 { .ca_index = 0x00, .speakers = { 0, 0, 0, 0, 0, 0, FR, FL } },
239 { .ca_index = 0x01, .speakers = { 0, 0, 0, 0, 0, LFE, FR, FL } },
241 { .ca_index = 0x02, .speakers = { 0, 0, 0, 0, FC, 0, FR, FL } },
243 { .ca_index = 0x08, .speakers = { 0, 0, RR, RL, 0, 0, FR, FL } },
245 { .ca_index = 0x09, .speakers = { 0, 0, RR, RL, 0, LFE, FR, FL } },
247 { .ca_index = 0x0a, .speakers = { 0, 0, RR, RL, FC, 0, FR, FL } },
249 { .ca_index = 0x0b, .speakers = { 0, 0, RR, RL, FC, LFE, FR, FL } },
251 { .ca_index = 0x0f, .speakers = { 0, RC, RR, RL, FC, LFE, FR, FL } },
253 { .ca_index = 0x13, .speakers = { RRC, RLC, RR, RL, FC, LFE, FR, FL } },
255 { .ca_index = 0x03, .speakers = { 0, 0, 0, 0, FC, LFE, FR, FL } },
256 { .ca_index = 0x04, .speakers = { 0, 0, 0, RC, 0, 0, FR, FL } },
257 { .ca_index = 0x05, .speakers = { 0, 0, 0, RC, 0, LFE, FR, FL } },
258 { .ca_index = 0x06, .speakers = { 0, 0, 0, RC, FC, 0, FR, FL } },
259 { .ca_index = 0x07, .speakers = { 0, 0, 0, RC, FC, LFE, FR, FL } },
260 { .ca_index = 0x0c, .speakers = { 0, RC, RR, RL, 0, 0, FR, FL } },
261 { .ca_index = 0x0d, .speakers = { 0, RC, RR, RL, 0, LFE, FR, FL } },
262 { .ca_index = 0x0e, .speakers = { 0, RC, RR, RL, FC, 0, FR, FL } },
263 { .ca_index = 0x10, .speakers = { RRC, RLC, RR, RL, 0, 0, FR, FL } },
264 { .ca_index = 0x11, .speakers = { RRC, RLC, RR, RL, 0, LFE, FR, FL } },
265 { .ca_index = 0x12, .speakers = { RRC, RLC, RR, RL, FC, 0, FR, FL } },
266 { .ca_index = 0x14, .speakers = { FRC, FLC, 0, 0, 0, 0, FR, FL } },
267 { .ca_index = 0x15, .speakers = { FRC, FLC, 0, 0, 0, LFE, FR, FL } },
268 { .ca_index = 0x16, .speakers = { FRC, FLC, 0, 0, FC, 0, FR, FL } },
269 { .ca_index = 0x17, .speakers = { FRC, FLC, 0, 0, FC, LFE, FR, FL } },
270 { .ca_index = 0x18, .speakers = { FRC, FLC, 0, RC, 0, 0, FR, FL } },
271 { .ca_index = 0x19, .speakers = { FRC, FLC, 0, RC, 0, LFE, FR, FL } },
272 { .ca_index = 0x1a, .speakers = { FRC, FLC, 0, RC, FC, 0, FR, FL } },
273 { .ca_index = 0x1b, .speakers = { FRC, FLC, 0, RC, FC, LFE, FR, FL } },
274 { .ca_index = 0x1c, .speakers = { FRC, FLC, RR, RL, 0, 0, FR, FL } },
275 { .ca_index = 0x1d, .speakers = { FRC, FLC, RR, RL, 0, LFE, FR, FL } },
276 { .ca_index = 0x1e, .speakers = { FRC, FLC, RR, RL, FC, 0, FR, FL } },
277 { .ca_index = 0x1f, .speakers = { FRC, FLC, RR, RL, FC, LFE, FR, FL } },
278 { .ca_index = 0x20, .speakers = { 0, FCH, RR, RL, FC, 0, FR, FL } },
279 { .ca_index = 0x21, .speakers = { 0, FCH, RR, RL, FC, LFE, FR, FL } },
280 { .ca_index = 0x22, .speakers = { TC, 0, RR, RL, FC, 0, FR, FL } },
281 { .ca_index = 0x23, .speakers = { TC, 0, RR, RL, FC, LFE, FR, FL } },
282 { .ca_index = 0x24, .speakers = { FRH, FLH, RR, RL, 0, 0, FR, FL } },
283 { .ca_index = 0x25, .speakers = { FRH, FLH, RR, RL, 0, LFE, FR, FL } },
284 { .ca_index = 0x26, .speakers = { FRW, FLW, RR, RL, 0, 0, FR, FL } },
285 { .ca_index = 0x27, .speakers = { FRW, FLW, RR, RL, 0, LFE, FR, FL } },
286 { .ca_index = 0x28, .speakers = { TC, RC, RR, RL, FC, 0, FR, FL } },
287 { .ca_index = 0x29, .speakers = { TC, RC, RR, RL, FC, LFE, FR, FL } },
288 { .ca_index = 0x2a, .speakers = { FCH, RC, RR, RL, FC, 0, FR, FL } },
289 { .ca_index = 0x2b, .speakers = { FCH, RC, RR, RL, FC, LFE, FR, FL } },
290 { .ca_index = 0x2c, .speakers = { TC, FCH, RR, RL, FC, 0, FR, FL } },
291 { .ca_index = 0x2d, .speakers = { TC, FCH, RR, RL, FC, LFE, FR, FL } },
292 { .ca_index = 0x2e, .speakers = { FRH, FLH, RR, RL, FC, 0, FR, FL } },
293 { .ca_index = 0x2f, .speakers = { FRH, FLH, RR, RL, FC, LFE, FR, FL } },
294 { .ca_index = 0x30, .speakers = { FRW, FLW, RR, RL, FC, 0, FR, FL } },
295 { .ca_index = 0x31, .speakers = { FRW, FLW, RR, RL, FC, LFE, FR, FL } },
303 #define get_pin(spec, idx) \
304 ((struct hdmi_spec_per_pin *)snd_array_elem(&spec->pins, idx))
305 #define get_cvt(spec, idx) \
306 ((struct hdmi_spec_per_cvt *)snd_array_elem(&spec->cvts, idx))
307 #define get_pcm_rec(spec, idx) \
308 ((struct hda_pcm *)snd_array_elem(&spec->pcm_rec, idx))
310 static int pin_nid_to_pin_index(struct hdmi_spec *spec, hda_nid_t pin_nid)
314 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++)
315 if (get_pin(spec, pin_idx)->pin_nid == pin_nid)
318 snd_printk(KERN_WARNING "HDMI: pin nid %d not registered\n", pin_nid);
322 static int hinfo_to_pin_index(struct hdmi_spec *spec,
323 struct hda_pcm_stream *hinfo)
327 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++)
328 if (get_pcm_rec(spec, pin_idx)->stream == hinfo)
331 snd_printk(KERN_WARNING "HDMI: hinfo %p not registered\n", hinfo);
335 static int cvt_nid_to_cvt_index(struct hdmi_spec *spec, hda_nid_t cvt_nid)
339 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++)
340 if (get_cvt(spec, cvt_idx)->cvt_nid == cvt_nid)
343 snd_printk(KERN_WARNING "HDMI: cvt nid %d not registered\n", cvt_nid);
347 static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
348 struct snd_ctl_elem_info *uinfo)
350 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
351 struct hdmi_spec *spec = codec->spec;
352 struct hdmi_eld *eld;
355 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
357 pin_idx = kcontrol->private_value;
358 eld = &get_pin(spec, pin_idx)->sink_eld;
360 mutex_lock(&eld->lock);
361 uinfo->count = eld->eld_valid ? eld->eld_size : 0;
362 mutex_unlock(&eld->lock);
367 static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
368 struct snd_ctl_elem_value *ucontrol)
370 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
371 struct hdmi_spec *spec = codec->spec;
372 struct hdmi_eld *eld;
375 pin_idx = kcontrol->private_value;
376 eld = &get_pin(spec, pin_idx)->sink_eld;
378 mutex_lock(&eld->lock);
379 if (eld->eld_size > ARRAY_SIZE(ucontrol->value.bytes.data)) {
380 mutex_unlock(&eld->lock);
385 memset(ucontrol->value.bytes.data, 0,
386 ARRAY_SIZE(ucontrol->value.bytes.data));
388 memcpy(ucontrol->value.bytes.data, eld->eld_buffer,
390 mutex_unlock(&eld->lock);
395 static struct snd_kcontrol_new eld_bytes_ctl = {
396 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
397 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
399 .info = hdmi_eld_ctl_info,
400 .get = hdmi_eld_ctl_get,
403 static int hdmi_create_eld_ctl(struct hda_codec *codec, int pin_idx,
406 struct snd_kcontrol *kctl;
407 struct hdmi_spec *spec = codec->spec;
410 kctl = snd_ctl_new1(&eld_bytes_ctl, codec);
413 kctl->private_value = pin_idx;
414 kctl->id.device = device;
416 err = snd_hda_ctl_add(codec, get_pin(spec, pin_idx)->pin_nid, kctl);
420 get_pin(spec, pin_idx)->eld_ctl = kctl;
425 static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
426 int *packet_index, int *byte_index)
430 val = snd_hda_codec_read(codec, pin_nid, 0,
431 AC_VERB_GET_HDMI_DIP_INDEX, 0);
433 *packet_index = val >> 5;
434 *byte_index = val & 0x1f;
438 static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
439 int packet_index, int byte_index)
443 val = (packet_index << 5) | (byte_index & 0x1f);
445 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
448 static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t pin_nid,
451 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
454 static void hdmi_init_pin(struct hda_codec *codec, hda_nid_t pin_nid)
456 struct hdmi_spec *spec = codec->spec;
460 if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
461 snd_hda_codec_write(codec, pin_nid, 0,
462 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
464 if (spec->dyn_pin_out)
465 /* Disable pin out until stream is active */
468 /* Enable pin out: some machines with GM965 gets broken output
469 * when the pin is disabled or changed while using with HDMI
473 snd_hda_codec_write(codec, pin_nid, 0,
474 AC_VERB_SET_PIN_WIDGET_CONTROL, pin_out);
477 static int hdmi_get_channel_count(struct hda_codec *codec, hda_nid_t cvt_nid)
479 return 1 + snd_hda_codec_read(codec, cvt_nid, 0,
480 AC_VERB_GET_CVT_CHAN_COUNT, 0);
483 static void hdmi_set_channel_count(struct hda_codec *codec,
484 hda_nid_t cvt_nid, int chs)
486 if (chs != hdmi_get_channel_count(codec, cvt_nid))
487 snd_hda_codec_write(codec, cvt_nid, 0,
488 AC_VERB_SET_CVT_CHAN_COUNT, chs - 1);
493 * Channel mapping routines
497 * Compute derived values in channel_allocations[].
499 static void init_channel_allocations(void)
502 struct cea_channel_speaker_allocation *p;
504 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
505 p = channel_allocations + i;
508 for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
509 if (p->speakers[j]) {
511 p->spk_mask |= p->speakers[j];
516 static int get_channel_allocation_order(int ca)
520 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
521 if (channel_allocations[i].ca_index == ca)
528 * The transformation takes two steps:
530 * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
531 * spk_mask => (channel_allocations[]) => ai->CA
533 * TODO: it could select the wrong CA from multiple candidates.
535 static int hdmi_channel_allocation(struct hdmi_eld *eld, int channels)
540 char buf[SND_PRINT_CHANNEL_ALLOCATION_ADVISED_BUFSIZE];
543 * CA defaults to 0 for basic stereo audio
549 * expand ELD's speaker allocation mask
551 * ELD tells the speaker mask in a compact(paired) form,
552 * expand ELD's notions to match the ones used by Audio InfoFrame.
554 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
555 if (eld->info.spk_alloc & (1 << i))
556 spk_mask |= eld_speaker_allocation_bits[i];
559 /* search for the first working match in the CA table */
560 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
561 if (channels == channel_allocations[i].channels &&
562 (spk_mask & channel_allocations[i].spk_mask) ==
563 channel_allocations[i].spk_mask) {
564 ca = channel_allocations[i].ca_index;
570 /* if there was no match, select the regular ALSA channel
571 * allocation with the matching number of channels */
572 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
573 if (channels == channel_allocations[i].channels) {
574 ca = channel_allocations[i].ca_index;
580 snd_print_channel_allocation(eld->info.spk_alloc, buf, sizeof(buf));
581 snd_printdd("HDMI: select CA 0x%x for %d-channel allocation: %s\n",
587 static void hdmi_debug_channel_mapping(struct hda_codec *codec,
590 #ifdef CONFIG_SND_DEBUG_VERBOSE
594 for (i = 0; i < 8; i++) {
595 slot = snd_hda_codec_read(codec, pin_nid, 0,
596 AC_VERB_GET_HDMI_CHAN_SLOT, i);
597 printk(KERN_DEBUG "HDMI: ASP channel %d => slot %d\n",
598 slot >> 4, slot & 0xf);
604 static void hdmi_std_setup_channel_mapping(struct hda_codec *codec,
612 int non_pcm_mapping[8];
614 order = get_channel_allocation_order(ca);
616 if (hdmi_channel_mapping[ca][1] == 0) {
617 for (i = 0; i < channel_allocations[order].channels; i++)
618 hdmi_channel_mapping[ca][i] = i | (i << 4);
620 hdmi_channel_mapping[ca][i] = 0xf | (i << 4);
624 for (i = 0; i < channel_allocations[order].channels; i++)
625 non_pcm_mapping[i] = i | (i << 4);
627 non_pcm_mapping[i] = 0xf | (i << 4);
630 for (i = 0; i < 8; i++) {
631 err = snd_hda_codec_write(codec, pin_nid, 0,
632 AC_VERB_SET_HDMI_CHAN_SLOT,
633 non_pcm ? non_pcm_mapping[i] : hdmi_channel_mapping[ca][i]);
635 snd_printdd(KERN_NOTICE
636 "HDMI: channel mapping failed\n");
641 hdmi_debug_channel_mapping(codec, pin_nid);
644 struct channel_map_table {
645 unsigned char map; /* ALSA API channel map position */
646 unsigned char cea_slot; /* CEA slot value */
647 int spk_mask; /* speaker position bit mask */
650 static struct channel_map_table map_tables[] = {
651 { SNDRV_CHMAP_FL, 0x00, FL },
652 { SNDRV_CHMAP_FR, 0x01, FR },
653 { SNDRV_CHMAP_RL, 0x04, RL },
654 { SNDRV_CHMAP_RR, 0x05, RR },
655 { SNDRV_CHMAP_LFE, 0x02, LFE },
656 { SNDRV_CHMAP_FC, 0x03, FC },
657 { SNDRV_CHMAP_RLC, 0x06, RLC },
658 { SNDRV_CHMAP_RRC, 0x07, RRC },
662 /* from ALSA API channel position to speaker bit mask */
663 static int to_spk_mask(unsigned char c)
665 struct channel_map_table *t = map_tables;
666 for (; t->map; t++) {
673 /* from ALSA API channel position to CEA slot */
674 static int to_cea_slot(unsigned char c)
676 struct channel_map_table *t = map_tables;
677 for (; t->map; t++) {
684 /* from CEA slot to ALSA API channel position */
685 static int from_cea_slot(unsigned char c)
687 struct channel_map_table *t = map_tables;
688 for (; t->map; t++) {
689 if (t->cea_slot == c)
695 /* from speaker bit mask to ALSA API channel position */
696 static int spk_to_chmap(int spk)
698 struct channel_map_table *t = map_tables;
699 for (; t->map; t++) {
700 if (t->spk_mask == spk)
706 /* get the CA index corresponding to the given ALSA API channel map */
707 static int hdmi_manual_channel_allocation(int chs, unsigned char *map)
709 int i, spks = 0, spk_mask = 0;
711 for (i = 0; i < chs; i++) {
712 int mask = to_spk_mask(map[i]);
719 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
720 if ((chs == channel_allocations[i].channels ||
721 spks == channel_allocations[i].channels) &&
722 (spk_mask & channel_allocations[i].spk_mask) ==
723 channel_allocations[i].spk_mask)
724 return channel_allocations[i].ca_index;
729 /* set up the channel slots for the given ALSA API channel map */
730 static int hdmi_manual_setup_channel_mapping(struct hda_codec *codec,
732 int chs, unsigned char *map)
735 for (i = 0; i < 8; i++) {
738 val = to_cea_slot(map[i]);
742 err = snd_hda_codec_write(codec, pin_nid, 0,
743 AC_VERB_SET_HDMI_CHAN_SLOT, val);
750 /* store ALSA API channel map from the current default map */
751 static void hdmi_setup_fake_chmap(unsigned char *map, int ca)
754 int ordered_ca = get_channel_allocation_order(ca);
755 for (i = 0; i < 8; i++) {
756 if (i < channel_allocations[ordered_ca].channels)
757 map[i] = from_cea_slot(hdmi_channel_mapping[ca][i] & 0x0f);
763 static void hdmi_setup_channel_mapping(struct hda_codec *codec,
764 hda_nid_t pin_nid, bool non_pcm, int ca,
765 int channels, unsigned char *map,
768 if (!non_pcm && chmap_set) {
769 hdmi_manual_setup_channel_mapping(codec, pin_nid,
772 hdmi_std_setup_channel_mapping(codec, pin_nid, non_pcm, ca);
773 hdmi_setup_fake_chmap(map, ca);
778 * Audio InfoFrame routines
782 * Enable Audio InfoFrame Transmission
784 static void hdmi_start_infoframe_trans(struct hda_codec *codec,
787 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
788 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
793 * Disable Audio InfoFrame Transmission
795 static void hdmi_stop_infoframe_trans(struct hda_codec *codec,
798 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
799 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
803 static void hdmi_debug_dip_size(struct hda_codec *codec, hda_nid_t pin_nid)
805 #ifdef CONFIG_SND_DEBUG_VERBOSE
809 size = snd_hdmi_get_eld_size(codec, pin_nid);
810 printk(KERN_DEBUG "HDMI: ELD buf size is %d\n", size);
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);
815 printk(KERN_DEBUG "HDMI: DIP GP[%d] buf size is %d\n", i, size);
820 static void hdmi_clear_dip_buffers(struct hda_codec *codec, hda_nid_t pin_nid)
826 for (i = 0; i < 8; i++) {
827 size = snd_hda_codec_read(codec, pin_nid, 0,
828 AC_VERB_GET_HDMI_DIP_SIZE, i);
832 hdmi_set_dip_index(codec, pin_nid, i, 0x0);
833 for (j = 1; j < 1000; j++) {
834 hdmi_write_dip_byte(codec, pin_nid, 0x0);
835 hdmi_get_dip_index(codec, pin_nid, &pi, &bi);
837 snd_printd(KERN_INFO "dip index %d: %d != %d\n",
839 if (bi == 0) /* byte index wrapped around */
843 "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
849 static void hdmi_checksum_audio_infoframe(struct hdmi_audio_infoframe *hdmi_ai)
851 u8 *bytes = (u8 *)hdmi_ai;
855 hdmi_ai->checksum = 0;
857 for (i = 0; i < sizeof(*hdmi_ai); i++)
860 hdmi_ai->checksum = -sum;
863 static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
869 hdmi_debug_dip_size(codec, pin_nid);
870 hdmi_clear_dip_buffers(codec, pin_nid); /* be paranoid */
872 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
873 for (i = 0; i < size; i++)
874 hdmi_write_dip_byte(codec, pin_nid, dip[i]);
877 static bool hdmi_infoframe_uptodate(struct hda_codec *codec, hda_nid_t pin_nid,
883 if (snd_hda_codec_read(codec, pin_nid, 0, AC_VERB_GET_HDMI_DIP_XMIT, 0)
887 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
888 for (i = 0; i < size; i++) {
889 val = snd_hda_codec_read(codec, pin_nid, 0,
890 AC_VERB_GET_HDMI_DIP_DATA, 0);
898 static void hdmi_setup_audio_infoframe(struct hda_codec *codec,
899 struct hdmi_spec_per_pin *per_pin,
902 hda_nid_t pin_nid = per_pin->pin_nid;
903 int channels = per_pin->channels;
904 struct hdmi_eld *eld;
906 union audio_infoframe ai;
911 eld = &per_pin->sink_eld;
912 if (!eld->monitor_present)
915 if (!non_pcm && per_pin->chmap_set)
916 ca = hdmi_manual_channel_allocation(channels, per_pin->chmap);
918 ca = hdmi_channel_allocation(eld, channels);
922 memset(&ai, 0, sizeof(ai));
923 if (eld->info.conn_type == 0) { /* HDMI */
924 struct hdmi_audio_infoframe *hdmi_ai = &ai.hdmi;
926 hdmi_ai->type = 0x84;
929 hdmi_ai->CC02_CT47 = channels - 1;
931 hdmi_checksum_audio_infoframe(hdmi_ai);
932 } else if (eld->info.conn_type == 1) { /* DisplayPort */
933 struct dp_audio_infoframe *dp_ai = &ai.dp;
937 dp_ai->ver = 0x11 << 2;
938 dp_ai->CC02_CT47 = channels - 1;
941 snd_printd("HDMI: unknown connection type at pin %d\n",
947 * always configure channel mapping, it may have been changed by the
948 * user in the meantime
950 hdmi_setup_channel_mapping(codec, pin_nid, non_pcm, ca,
951 channels, per_pin->chmap,
955 * sizeof(ai) is used instead of sizeof(*hdmi_ai) or
956 * sizeof(*dp_ai) to avoid partial match/update problems when
957 * the user switches between HDMI/DP monitors.
959 if (!hdmi_infoframe_uptodate(codec, pin_nid, ai.bytes,
961 snd_printdd("hdmi_setup_audio_infoframe: "
962 "pin=%d channels=%d\n",
965 hdmi_stop_infoframe_trans(codec, pin_nid);
966 hdmi_fill_audio_infoframe(codec, pin_nid,
967 ai.bytes, sizeof(ai));
968 hdmi_start_infoframe_trans(codec, pin_nid);
971 per_pin->non_pcm = non_pcm;
979 static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll);
981 static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
983 struct hdmi_spec *spec = codec->spec;
984 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
987 struct hda_jack_tbl *jack;
989 jack = snd_hda_jack_tbl_get_from_tag(codec, tag);
993 jack->jack_dirty = 1;
995 _snd_printd(SND_PR_VERBOSE,
996 "HDMI hot plug event: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
997 codec->addr, pin_nid,
998 !!(res & AC_UNSOL_RES_PD), !!(res & AC_UNSOL_RES_ELDV));
1000 pin_idx = pin_nid_to_pin_index(spec, pin_nid);
1004 hdmi_present_sense(get_pin(spec, pin_idx), 1);
1005 snd_hda_jack_report_sync(codec);
1008 static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
1010 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
1011 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
1012 int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
1013 int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
1016 "HDMI CP event: CODEC=%d TAG=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
1031 static void hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
1033 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
1034 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
1036 if (!snd_hda_jack_tbl_get_from_tag(codec, tag)) {
1037 snd_printd(KERN_INFO "Unexpected HDMI event tag 0x%x\n", tag);
1042 hdmi_intrinsic_event(codec, res);
1044 hdmi_non_intrinsic_event(codec, res);
1047 static void haswell_verify_pin_D0(struct hda_codec *codec, hda_nid_t nid)
1049 int pwr, lamp, ramp;
1051 pwr = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
1052 pwr = (pwr & AC_PWRST_ACTUAL) >> AC_PWRST_ACTUAL_SHIFT;
1053 if (pwr != AC_PWRST_D0) {
1054 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
1057 pwr = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
1058 pwr = (pwr & AC_PWRST_ACTUAL) >> AC_PWRST_ACTUAL_SHIFT;
1059 snd_printd("Haswell HDMI audio: Power for pin 0x%x is now D%d\n", nid, pwr);
1062 lamp = snd_hda_codec_read(codec, nid, 0,
1063 AC_VERB_GET_AMP_GAIN_MUTE,
1064 AC_AMP_GET_LEFT | AC_AMP_GET_OUTPUT);
1065 ramp = snd_hda_codec_read(codec, nid, 0,
1066 AC_VERB_GET_AMP_GAIN_MUTE,
1067 AC_AMP_GET_RIGHT | AC_AMP_GET_OUTPUT);
1069 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
1070 AC_AMP_SET_RIGHT | AC_AMP_SET_OUTPUT | lamp);
1072 lamp = snd_hda_codec_read(codec, nid, 0,
1073 AC_VERB_GET_AMP_GAIN_MUTE,
1074 AC_AMP_GET_LEFT | AC_AMP_GET_OUTPUT);
1075 ramp = snd_hda_codec_read(codec, nid, 0,
1076 AC_VERB_GET_AMP_GAIN_MUTE,
1077 AC_AMP_GET_RIGHT | AC_AMP_GET_OUTPUT);
1078 snd_printd("Haswell HDMI audio: Mute after set on pin 0x%x: [0x%x 0x%x]\n", nid, lamp, ramp);
1086 /* HBR should be Non-PCM, 8 channels */
1087 #define is_hbr_format(format) \
1088 ((format & AC_FMT_TYPE_NON_PCM) && (format & AC_FMT_CHAN_MASK) == 7)
1090 static int hdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
1091 hda_nid_t pin_nid, u32 stream_tag, int format)
1096 if (codec->vendor_id == 0x80862807)
1097 haswell_verify_pin_D0(codec, pin_nid);
1099 if (snd_hda_query_pin_caps(codec, pin_nid) & AC_PINCAP_HBR) {
1100 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
1101 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1103 new_pinctl = pinctl & ~AC_PINCTL_EPT;
1104 if (is_hbr_format(format))
1105 new_pinctl |= AC_PINCTL_EPT_HBR;
1107 new_pinctl |= AC_PINCTL_EPT_NATIVE;
1109 snd_printdd("hdmi_setup_stream: "
1110 "NID=0x%x, %spinctl=0x%x\n",
1112 pinctl == new_pinctl ? "" : "new-",
1115 if (pinctl != new_pinctl)
1116 snd_hda_codec_write(codec, pin_nid, 0,
1117 AC_VERB_SET_PIN_WIDGET_CONTROL,
1121 if (is_hbr_format(format) && !new_pinctl) {
1122 snd_printdd("hdmi_setup_stream: HBR is not supported\n");
1126 snd_hda_codec_setup_stream(codec, cvt_nid, stream_tag, 0, format);
1133 static int hdmi_pcm_open(struct hda_pcm_stream *hinfo,
1134 struct hda_codec *codec,
1135 struct snd_pcm_substream *substream)
1137 struct hdmi_spec *spec = codec->spec;
1138 struct snd_pcm_runtime *runtime = substream->runtime;
1139 int pin_idx, cvt_idx, mux_idx = 0;
1140 struct hdmi_spec_per_pin *per_pin;
1141 struct hdmi_eld *eld;
1142 struct hdmi_spec_per_cvt *per_cvt = NULL;
1144 /* Validate hinfo */
1145 pin_idx = hinfo_to_pin_index(spec, hinfo);
1146 if (snd_BUG_ON(pin_idx < 0))
1148 per_pin = get_pin(spec, pin_idx);
1149 eld = &per_pin->sink_eld;
1151 /* Dynamically assign converter to stream */
1152 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
1153 per_cvt = get_cvt(spec, cvt_idx);
1155 /* Must not already be assigned */
1156 if (per_cvt->assigned)
1158 /* Must be in pin's mux's list of converters */
1159 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
1160 if (per_pin->mux_nids[mux_idx] == per_cvt->cvt_nid)
1162 /* Not in mux list */
1163 if (mux_idx == per_pin->num_mux_nids)
1167 /* No free converters */
1168 if (cvt_idx == spec->num_cvts)
1171 /* Claim converter */
1172 per_cvt->assigned = 1;
1173 hinfo->nid = per_cvt->cvt_nid;
1175 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
1176 AC_VERB_SET_CONNECT_SEL,
1178 snd_hda_spdif_ctls_assign(codec, pin_idx, per_cvt->cvt_nid);
1180 /* Initially set the converter's capabilities */
1181 hinfo->channels_min = per_cvt->channels_min;
1182 hinfo->channels_max = per_cvt->channels_max;
1183 hinfo->rates = per_cvt->rates;
1184 hinfo->formats = per_cvt->formats;
1185 hinfo->maxbps = per_cvt->maxbps;
1187 /* Restrict capabilities by ELD if this isn't disabled */
1188 if (!static_hdmi_pcm && eld->eld_valid) {
1189 snd_hdmi_eld_update_pcm_info(&eld->info, hinfo);
1190 if (hinfo->channels_min > hinfo->channels_max ||
1191 !hinfo->rates || !hinfo->formats) {
1192 per_cvt->assigned = 0;
1194 snd_hda_spdif_ctls_unassign(codec, pin_idx);
1199 /* Store the updated parameters */
1200 runtime->hw.channels_min = hinfo->channels_min;
1201 runtime->hw.channels_max = hinfo->channels_max;
1202 runtime->hw.formats = hinfo->formats;
1203 runtime->hw.rates = hinfo->rates;
1205 snd_pcm_hw_constraint_step(substream->runtime, 0,
1206 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
1211 * HDA/HDMI auto parsing
1213 static int hdmi_read_pin_conn(struct hda_codec *codec, int pin_idx)
1215 struct hdmi_spec *spec = codec->spec;
1216 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1217 hda_nid_t pin_nid = per_pin->pin_nid;
1219 if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
1220 snd_printk(KERN_WARNING
1221 "HDMI: pin %d wcaps %#x "
1222 "does not support connection list\n",
1223 pin_nid, get_wcaps(codec, pin_nid));
1227 per_pin->num_mux_nids = snd_hda_get_connections(codec, pin_nid,
1229 HDA_MAX_CONNECTIONS);
1234 static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll)
1236 struct hda_codec *codec = per_pin->codec;
1237 struct hdmi_spec *spec = codec->spec;
1238 struct hdmi_eld *eld = &spec->temp_eld;
1239 struct hdmi_eld *pin_eld = &per_pin->sink_eld;
1240 hda_nid_t pin_nid = per_pin->pin_nid;
1242 * Always execute a GetPinSense verb here, even when called from
1243 * hdmi_intrinsic_event; for some NVIDIA HW, the unsolicited
1244 * response's PD bit is not the real PD value, but indicates that
1245 * the real PD value changed. An older version of the HD-audio
1246 * specification worked this way. Hence, we just ignore the data in
1247 * the unsolicited response to avoid custom WARs.
1249 int present = snd_hda_pin_sense(codec, pin_nid);
1250 bool update_eld = false;
1251 bool eld_changed = false;
1253 pin_eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
1254 if (pin_eld->monitor_present)
1255 eld->eld_valid = !!(present & AC_PINSENSE_ELDV);
1257 eld->eld_valid = false;
1259 _snd_printd(SND_PR_VERBOSE,
1260 "HDMI status: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
1261 codec->addr, pin_nid, pin_eld->monitor_present, eld->eld_valid);
1263 if (eld->eld_valid) {
1264 if (snd_hdmi_get_eld(codec, pin_nid, eld->eld_buffer,
1265 &eld->eld_size) < 0)
1266 eld->eld_valid = false;
1268 memset(&eld->info, 0, sizeof(struct parsed_hdmi_eld));
1269 if (snd_hdmi_parse_eld(&eld->info, eld->eld_buffer,
1271 eld->eld_valid = false;
1274 if (eld->eld_valid) {
1275 snd_hdmi_show_eld(&eld->info);
1279 queue_delayed_work(codec->bus->workq,
1281 msecs_to_jiffies(300));
1286 mutex_lock(&pin_eld->lock);
1287 if (pin_eld->eld_valid && !eld->eld_valid) {
1292 bool old_eld_valid = pin_eld->eld_valid;
1293 pin_eld->eld_valid = eld->eld_valid;
1294 eld_changed = pin_eld->eld_size != eld->eld_size ||
1295 memcmp(pin_eld->eld_buffer, eld->eld_buffer,
1296 eld->eld_size) != 0;
1298 memcpy(pin_eld->eld_buffer, eld->eld_buffer,
1300 pin_eld->eld_size = eld->eld_size;
1301 pin_eld->info = eld->info;
1303 /* Haswell-specific workaround: re-setup when the transcoder is
1304 * changed during the stream playback
1306 if (codec->vendor_id == 0x80862807 &&
1307 eld->eld_valid && !old_eld_valid && per_pin->setup) {
1308 snd_hda_codec_write(codec, pin_nid, 0,
1309 AC_VERB_SET_AMP_GAIN_MUTE,
1311 hdmi_setup_audio_infoframe(codec, per_pin,
1315 mutex_unlock(&pin_eld->lock);
1318 snd_ctl_notify(codec->bus->card,
1319 SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
1320 &per_pin->eld_ctl->id);
1323 static void hdmi_repoll_eld(struct work_struct *work)
1325 struct hdmi_spec_per_pin *per_pin =
1326 container_of(to_delayed_work(work), struct hdmi_spec_per_pin, work);
1328 if (per_pin->repoll_count++ > 6)
1329 per_pin->repoll_count = 0;
1331 hdmi_present_sense(per_pin, per_pin->repoll_count);
1334 static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
1337 static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
1339 struct hdmi_spec *spec = codec->spec;
1340 unsigned int caps, config;
1342 struct hdmi_spec_per_pin *per_pin;
1345 caps = snd_hda_query_pin_caps(codec, pin_nid);
1346 if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP)))
1349 config = snd_hda_codec_get_pincfg(codec, pin_nid);
1350 if (get_defcfg_connect(config) == AC_JACK_PORT_NONE)
1353 if (codec->vendor_id == 0x80862807)
1354 intel_haswell_fixup_connect_list(codec, pin_nid);
1356 pin_idx = spec->num_pins;
1357 per_pin = snd_array_new(&spec->pins);
1361 per_pin->pin_nid = pin_nid;
1362 per_pin->non_pcm = false;
1364 err = hdmi_read_pin_conn(codec, pin_idx);
1373 static int hdmi_add_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
1375 struct hdmi_spec *spec = codec->spec;
1376 struct hdmi_spec_per_cvt *per_cvt;
1380 chans = get_wcaps(codec, cvt_nid);
1381 chans = get_wcaps_channels(chans);
1383 per_cvt = snd_array_new(&spec->cvts);
1387 per_cvt->cvt_nid = cvt_nid;
1388 per_cvt->channels_min = 2;
1390 per_cvt->channels_max = chans;
1391 if (chans > spec->channels_max)
1392 spec->channels_max = chans;
1395 err = snd_hda_query_supported_pcm(codec, cvt_nid,
1402 if (spec->num_cvts < ARRAY_SIZE(spec->cvt_nids))
1403 spec->cvt_nids[spec->num_cvts] = cvt_nid;
1409 static int hdmi_parse_codec(struct hda_codec *codec)
1414 nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid);
1415 if (!nid || nodes < 0) {
1416 snd_printk(KERN_WARNING "HDMI: failed to get afg sub nodes\n");
1420 for (i = 0; i < nodes; i++, nid++) {
1424 caps = get_wcaps(codec, nid);
1425 type = get_wcaps_type(caps);
1427 if (!(caps & AC_WCAP_DIGITAL))
1431 case AC_WID_AUD_OUT:
1432 hdmi_add_cvt(codec, nid);
1435 hdmi_add_pin(codec, nid);
1441 /* We're seeing some problems with unsolicited hot plug events on
1442 * PantherPoint after S3, if this is not enabled */
1443 if (codec->vendor_id == 0x80862806)
1444 codec->bus->power_keep_link_on = 1;
1446 * G45/IbexPeak don't support EPSS: the unsolicited pin hot plug event
1447 * can be lost and presence sense verb will become inaccurate if the
1448 * HDA link is powered off at hot plug or hw initialization time.
1450 else if (!(snd_hda_param_read(codec, codec->afg, AC_PAR_POWER_STATE) &
1452 codec->bus->power_keep_link_on = 1;
1460 static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
1462 struct hda_spdif_out *spdif;
1465 mutex_lock(&codec->spdif_mutex);
1466 spdif = snd_hda_spdif_out_of_nid(codec, cvt_nid);
1467 non_pcm = !!(spdif->status & IEC958_AES0_NONAUDIO);
1468 mutex_unlock(&codec->spdif_mutex);
1477 static int generic_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
1478 struct hda_codec *codec,
1479 unsigned int stream_tag,
1480 unsigned int format,
1481 struct snd_pcm_substream *substream)
1483 hda_nid_t cvt_nid = hinfo->nid;
1484 struct hdmi_spec *spec = codec->spec;
1485 int pin_idx = hinfo_to_pin_index(spec, hinfo);
1486 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1487 hda_nid_t pin_nid = per_pin->pin_nid;
1491 non_pcm = check_non_pcm_per_cvt(codec, cvt_nid);
1492 per_pin->channels = substream->runtime->channels;
1493 per_pin->setup = true;
1495 hdmi_set_channel_count(codec, cvt_nid, substream->runtime->channels);
1497 hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
1499 if (spec->dyn_pin_out) {
1500 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
1501 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1502 snd_hda_codec_write(codec, pin_nid, 0,
1503 AC_VERB_SET_PIN_WIDGET_CONTROL,
1507 return hdmi_setup_stream(codec, cvt_nid, pin_nid, stream_tag, format);
1510 static int generic_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
1511 struct hda_codec *codec,
1512 struct snd_pcm_substream *substream)
1514 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
1518 static int hdmi_pcm_close(struct hda_pcm_stream *hinfo,
1519 struct hda_codec *codec,
1520 struct snd_pcm_substream *substream)
1522 struct hdmi_spec *spec = codec->spec;
1523 int cvt_idx, pin_idx;
1524 struct hdmi_spec_per_cvt *per_cvt;
1525 struct hdmi_spec_per_pin *per_pin;
1529 cvt_idx = cvt_nid_to_cvt_index(spec, hinfo->nid);
1530 if (snd_BUG_ON(cvt_idx < 0))
1532 per_cvt = get_cvt(spec, cvt_idx);
1534 snd_BUG_ON(!per_cvt->assigned);
1535 per_cvt->assigned = 0;
1538 pin_idx = hinfo_to_pin_index(spec, hinfo);
1539 if (snd_BUG_ON(pin_idx < 0))
1541 per_pin = get_pin(spec, pin_idx);
1543 if (spec->dyn_pin_out) {
1544 pinctl = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
1545 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1546 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
1547 AC_VERB_SET_PIN_WIDGET_CONTROL,
1551 snd_hda_spdif_ctls_unassign(codec, pin_idx);
1552 per_pin->chmap_set = false;
1553 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
1555 per_pin->setup = false;
1556 per_pin->channels = 0;
1562 static const struct hda_pcm_ops generic_ops = {
1563 .open = hdmi_pcm_open,
1564 .close = hdmi_pcm_close,
1565 .prepare = generic_hdmi_playback_pcm_prepare,
1566 .cleanup = generic_hdmi_playback_pcm_cleanup,
1570 * ALSA API channel-map control callbacks
1572 static int hdmi_chmap_ctl_info(struct snd_kcontrol *kcontrol,
1573 struct snd_ctl_elem_info *uinfo)
1575 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1576 struct hda_codec *codec = info->private_data;
1577 struct hdmi_spec *spec = codec->spec;
1578 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1579 uinfo->count = spec->channels_max;
1580 uinfo->value.integer.min = 0;
1581 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
1585 static int hdmi_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1586 unsigned int size, unsigned int __user *tlv)
1588 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1589 struct hda_codec *codec = info->private_data;
1590 struct hdmi_spec *spec = codec->spec;
1591 const unsigned int valid_mask =
1592 FL | FR | RL | RR | LFE | FC | RLC | RRC;
1593 unsigned int __user *dst;
1598 if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
1602 for (chs = 2; chs <= spec->channels_max; chs++) {
1604 struct cea_channel_speaker_allocation *cap;
1605 cap = channel_allocations;
1606 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++, cap++) {
1607 int chs_bytes = chs * 4;
1608 if (cap->channels != chs)
1610 if (cap->spk_mask & ~valid_mask)
1614 if (put_user(SNDRV_CTL_TLVT_CHMAP_VAR, dst) ||
1615 put_user(chs_bytes, dst + 1))
1620 if (size < chs_bytes)
1624 for (c = 7; c >= 0; c--) {
1625 int spk = cap->speakers[c];
1628 if (put_user(spk_to_chmap(spk), dst))
1634 if (put_user(count, tlv + 1))
1639 static int hdmi_chmap_ctl_get(struct snd_kcontrol *kcontrol,
1640 struct snd_ctl_elem_value *ucontrol)
1642 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1643 struct hda_codec *codec = info->private_data;
1644 struct hdmi_spec *spec = codec->spec;
1645 int pin_idx = kcontrol->private_value;
1646 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1649 for (i = 0; i < ARRAY_SIZE(per_pin->chmap); i++)
1650 ucontrol->value.integer.value[i] = per_pin->chmap[i];
1654 static int hdmi_chmap_ctl_put(struct snd_kcontrol *kcontrol,
1655 struct snd_ctl_elem_value *ucontrol)
1657 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
1658 struct hda_codec *codec = info->private_data;
1659 struct hdmi_spec *spec = codec->spec;
1660 int pin_idx = kcontrol->private_value;
1661 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1662 unsigned int ctl_idx;
1663 struct snd_pcm_substream *substream;
1664 unsigned char chmap[8];
1665 int i, ca, prepared = 0;
1667 ctl_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1668 substream = snd_pcm_chmap_substream(info, ctl_idx);
1669 if (!substream || !substream->runtime)
1670 return 0; /* just for avoiding error from alsactl restore */
1671 switch (substream->runtime->status->state) {
1672 case SNDRV_PCM_STATE_OPEN:
1673 case SNDRV_PCM_STATE_SETUP:
1675 case SNDRV_PCM_STATE_PREPARED:
1681 memset(chmap, 0, sizeof(chmap));
1682 for (i = 0; i < ARRAY_SIZE(chmap); i++)
1683 chmap[i] = ucontrol->value.integer.value[i];
1684 if (!memcmp(chmap, per_pin->chmap, sizeof(chmap)))
1686 ca = hdmi_manual_channel_allocation(ARRAY_SIZE(chmap), chmap);
1689 per_pin->chmap_set = true;
1690 memcpy(per_pin->chmap, chmap, sizeof(chmap));
1692 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
1697 static int generic_hdmi_build_pcms(struct hda_codec *codec)
1699 struct hdmi_spec *spec = codec->spec;
1702 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1703 struct hda_pcm *info;
1704 struct hda_pcm_stream *pstr;
1705 struct hdmi_spec_per_pin *per_pin;
1707 per_pin = get_pin(spec, pin_idx);
1708 sprintf(per_pin->pcm_name, "HDMI %d", pin_idx);
1709 info = snd_array_new(&spec->pcm_rec);
1712 info->name = per_pin->pcm_name;
1713 info->pcm_type = HDA_PCM_TYPE_HDMI;
1714 info->own_chmap = true;
1716 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
1717 pstr->substreams = 1;
1718 pstr->ops = generic_ops;
1719 /* other pstr fields are set in open */
1722 codec->num_pcms = spec->num_pins;
1723 codec->pcm_info = spec->pcm_rec.list;
1728 static int generic_hdmi_build_jack(struct hda_codec *codec, int pin_idx)
1730 char hdmi_str[32] = "HDMI/DP";
1731 struct hdmi_spec *spec = codec->spec;
1732 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1733 int pcmdev = get_pcm_rec(spec, pin_idx)->device;
1736 sprintf(hdmi_str + strlen(hdmi_str), ",pcm=%d", pcmdev);
1737 if (!is_jack_detectable(codec, per_pin->pin_nid))
1738 strncat(hdmi_str, " Phantom",
1739 sizeof(hdmi_str) - strlen(hdmi_str) - 1);
1741 return snd_hda_jack_add_kctl(codec, per_pin->pin_nid, hdmi_str, 0);
1744 static int generic_hdmi_build_controls(struct hda_codec *codec)
1746 struct hdmi_spec *spec = codec->spec;
1750 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1751 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1753 err = generic_hdmi_build_jack(codec, pin_idx);
1757 err = snd_hda_create_dig_out_ctls(codec,
1759 per_pin->mux_nids[0],
1763 snd_hda_spdif_ctls_unassign(codec, pin_idx);
1765 /* add control for ELD Bytes */
1766 err = hdmi_create_eld_ctl(codec, pin_idx,
1767 get_pcm_rec(spec, pin_idx)->device);
1772 hdmi_present_sense(per_pin, 0);
1775 /* add channel maps */
1776 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1777 struct snd_pcm_chmap *chmap;
1778 struct snd_kcontrol *kctl;
1781 if (!codec->pcm_info[pin_idx].pcm)
1783 err = snd_pcm_add_chmap_ctls(codec->pcm_info[pin_idx].pcm,
1784 SNDRV_PCM_STREAM_PLAYBACK,
1785 NULL, 0, pin_idx, &chmap);
1788 /* override handlers */
1789 chmap->private_data = codec;
1791 for (i = 0; i < kctl->count; i++)
1792 kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_WRITE;
1793 kctl->info = hdmi_chmap_ctl_info;
1794 kctl->get = hdmi_chmap_ctl_get;
1795 kctl->put = hdmi_chmap_ctl_put;
1796 kctl->tlv.c = hdmi_chmap_ctl_tlv;
1802 static int generic_hdmi_init_per_pins(struct hda_codec *codec)
1804 struct hdmi_spec *spec = codec->spec;
1807 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1808 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1809 struct hdmi_eld *eld = &per_pin->sink_eld;
1811 per_pin->codec = codec;
1812 mutex_init(&eld->lock);
1813 INIT_DELAYED_WORK(&per_pin->work, hdmi_repoll_eld);
1814 snd_hda_eld_proc_new(codec, eld, pin_idx);
1819 static int generic_hdmi_init(struct hda_codec *codec)
1821 struct hdmi_spec *spec = codec->spec;
1824 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1825 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1826 hda_nid_t pin_nid = per_pin->pin_nid;
1828 hdmi_init_pin(codec, pin_nid);
1829 snd_hda_jack_detect_enable(codec, pin_nid, pin_nid);
1834 static void hdmi_array_init(struct hdmi_spec *spec, int nums)
1836 snd_array_init(&spec->pins, sizeof(struct hdmi_spec_per_pin), nums);
1837 snd_array_init(&spec->cvts, sizeof(struct hdmi_spec_per_cvt), nums);
1838 snd_array_init(&spec->pcm_rec, sizeof(struct hda_pcm), nums);
1841 static void hdmi_array_free(struct hdmi_spec *spec)
1843 snd_array_free(&spec->pins);
1844 snd_array_free(&spec->cvts);
1845 snd_array_free(&spec->pcm_rec);
1848 static void generic_hdmi_free(struct hda_codec *codec)
1850 struct hdmi_spec *spec = codec->spec;
1853 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
1854 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1855 struct hdmi_eld *eld = &per_pin->sink_eld;
1857 cancel_delayed_work(&per_pin->work);
1858 snd_hda_eld_proc_free(codec, eld);
1861 flush_workqueue(codec->bus->workq);
1862 hdmi_array_free(spec);
1866 static const struct hda_codec_ops generic_hdmi_patch_ops = {
1867 .init = generic_hdmi_init,
1868 .free = generic_hdmi_free,
1869 .build_pcms = generic_hdmi_build_pcms,
1870 .build_controls = generic_hdmi_build_controls,
1871 .unsol_event = hdmi_unsol_event,
1875 static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
1878 struct hdmi_spec *spec = codec->spec;
1882 nconns = snd_hda_get_connections(codec, nid, conns, ARRAY_SIZE(conns));
1883 if (nconns == spec->num_cvts &&
1884 !memcmp(conns, spec->cvt_nids, spec->num_cvts * sizeof(hda_nid_t)))
1887 /* override pins connection list */
1888 snd_printdd("hdmi: haswell: override pin connection 0x%x\n", nid);
1889 nconns = max(spec->num_cvts, 4);
1890 snd_hda_override_conn_list(codec, nid, spec->num_cvts, spec->cvt_nids);
1893 #define INTEL_VENDOR_NID 0x08
1894 #define INTEL_GET_VENDOR_VERB 0xf81
1895 #define INTEL_SET_VENDOR_VERB 0x781
1896 #define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
1897 #define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
1899 static void intel_haswell_enable_all_pins(struct hda_codec *codec,
1902 unsigned int vendor_param;
1904 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
1905 INTEL_GET_VENDOR_VERB, 0);
1906 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1909 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1910 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
1911 INTEL_SET_VENDOR_VERB, vendor_param);
1912 if (vendor_param == -1)
1916 snd_hda_codec_update_widgets(codec);
1919 static void intel_haswell_fixup_enable_dp12(struct hda_codec *codec)
1921 unsigned int vendor_param;
1923 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
1924 INTEL_GET_VENDOR_VERB, 0);
1925 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1928 /* enable DP1.2 mode */
1929 vendor_param |= INTEL_EN_DP12;
1930 snd_hda_codec_write_cache(codec, INTEL_VENDOR_NID, 0,
1931 INTEL_SET_VENDOR_VERB, vendor_param);
1934 /* Haswell needs to re-issue the vendor-specific verbs before turning to D0.
1935 * Otherwise you may get severe h/w communication errors.
1937 static void haswell_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1938 unsigned int power_state)
1940 if (power_state == AC_PWRST_D0) {
1941 intel_haswell_enable_all_pins(codec, false);
1942 intel_haswell_fixup_enable_dp12(codec);
1945 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE, power_state);
1946 snd_hda_codec_set_power_to_all(codec, fg, power_state);
1949 static int patch_generic_hdmi(struct hda_codec *codec)
1951 struct hdmi_spec *spec;
1953 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1958 hdmi_array_init(spec, 4);
1960 if (codec->vendor_id == 0x80862807) {
1961 intel_haswell_enable_all_pins(codec, true);
1962 intel_haswell_fixup_enable_dp12(codec);
1965 if (hdmi_parse_codec(codec) < 0) {
1970 codec->patch_ops = generic_hdmi_patch_ops;
1971 if (codec->vendor_id == 0x80862807)
1972 codec->patch_ops.set_power_state = haswell_set_power_state;
1974 generic_hdmi_init_per_pins(codec);
1976 init_channel_allocations();
1982 * Shared non-generic implementations
1985 static int simple_playback_build_pcms(struct hda_codec *codec)
1987 struct hdmi_spec *spec = codec->spec;
1988 struct hda_pcm *info;
1990 struct hda_pcm_stream *pstr;
1991 struct hdmi_spec_per_cvt *per_cvt;
1993 per_cvt = get_cvt(spec, 0);
1994 chans = get_wcaps(codec, per_cvt->cvt_nid);
1995 chans = get_wcaps_channels(chans);
1997 info = snd_array_new(&spec->pcm_rec);
2000 info->name = get_pin(spec, 0)->pcm_name;
2001 sprintf(info->name, "HDMI 0");
2002 info->pcm_type = HDA_PCM_TYPE_HDMI;
2003 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
2004 *pstr = spec->pcm_playback;
2005 pstr->nid = per_cvt->cvt_nid;
2006 if (pstr->channels_max <= 2 && chans && chans <= 16)
2007 pstr->channels_max = chans;
2009 codec->num_pcms = 1;
2010 codec->pcm_info = info;
2015 /* unsolicited event for jack sensing */
2016 static void simple_hdmi_unsol_event(struct hda_codec *codec,
2019 snd_hda_jack_set_dirty_all(codec);
2020 snd_hda_jack_report_sync(codec);
2023 /* generic_hdmi_build_jack can be used for simple_hdmi, too,
2024 * as long as spec->pins[] is set correctly
2026 #define simple_hdmi_build_jack generic_hdmi_build_jack
2028 static int simple_playback_build_controls(struct hda_codec *codec)
2030 struct hdmi_spec *spec = codec->spec;
2031 struct hdmi_spec_per_cvt *per_cvt;
2034 per_cvt = get_cvt(spec, 0);
2035 err = snd_hda_create_dig_out_ctls(codec, per_cvt->cvt_nid,
2040 return simple_hdmi_build_jack(codec, 0);
2043 static int simple_playback_init(struct hda_codec *codec)
2045 struct hdmi_spec *spec = codec->spec;
2046 struct hdmi_spec_per_pin *per_pin = get_pin(spec, 0);
2047 hda_nid_t pin = per_pin->pin_nid;
2049 snd_hda_codec_write(codec, pin, 0,
2050 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
2051 /* some codecs require to unmute the pin */
2052 if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
2053 snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
2055 snd_hda_jack_detect_enable(codec, pin, pin);
2059 static void simple_playback_free(struct hda_codec *codec)
2061 struct hdmi_spec *spec = codec->spec;
2063 hdmi_array_free(spec);
2068 * Nvidia specific implementations
2071 #define Nv_VERB_SET_Channel_Allocation 0xF79
2072 #define Nv_VERB_SET_Info_Frame_Checksum 0xF7A
2073 #define Nv_VERB_SET_Audio_Protection_On 0xF98
2074 #define Nv_VERB_SET_Audio_Protection_Off 0xF99
2076 #define nvhdmi_master_con_nid_7x 0x04
2077 #define nvhdmi_master_pin_nid_7x 0x05
2079 static const hda_nid_t nvhdmi_con_nids_7x[4] = {
2080 /*front, rear, clfe, rear_surr */
2084 static const struct hda_verb nvhdmi_basic_init_7x_2ch[] = {
2085 /* set audio protect on */
2086 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
2087 /* enable digital output on pin widget */
2088 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2092 static const struct hda_verb nvhdmi_basic_init_7x_8ch[] = {
2093 /* set audio protect on */
2094 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
2095 /* enable digital output on pin widget */
2096 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2097 { 0x7, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2098 { 0x9, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2099 { 0xb, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2100 { 0xd, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2104 #ifdef LIMITED_RATE_FMT_SUPPORT
2105 /* support only the safe format and rate */
2106 #define SUPPORTED_RATES SNDRV_PCM_RATE_48000
2107 #define SUPPORTED_MAXBPS 16
2108 #define SUPPORTED_FORMATS SNDRV_PCM_FMTBIT_S16_LE
2110 /* support all rates and formats */
2111 #define SUPPORTED_RATES \
2112 (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
2113 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
2114 SNDRV_PCM_RATE_192000)
2115 #define SUPPORTED_MAXBPS 24
2116 #define SUPPORTED_FORMATS \
2117 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
2120 static int nvhdmi_7x_init_2ch(struct hda_codec *codec)
2122 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_2ch);
2126 static int nvhdmi_7x_init_8ch(struct hda_codec *codec)
2128 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_8ch);
2132 static unsigned int channels_2_6_8[] = {
2136 static unsigned int channels_2_8[] = {
2140 static struct snd_pcm_hw_constraint_list hw_constraints_2_6_8_channels = {
2141 .count = ARRAY_SIZE(channels_2_6_8),
2142 .list = channels_2_6_8,
2146 static struct snd_pcm_hw_constraint_list hw_constraints_2_8_channels = {
2147 .count = ARRAY_SIZE(channels_2_8),
2148 .list = channels_2_8,
2152 static int simple_playback_pcm_open(struct hda_pcm_stream *hinfo,
2153 struct hda_codec *codec,
2154 struct snd_pcm_substream *substream)
2156 struct hdmi_spec *spec = codec->spec;
2157 struct snd_pcm_hw_constraint_list *hw_constraints_channels = NULL;
2159 switch (codec->preset->id) {
2164 hw_constraints_channels = &hw_constraints_2_8_channels;
2167 hw_constraints_channels = &hw_constraints_2_6_8_channels;
2173 if (hw_constraints_channels != NULL) {
2174 snd_pcm_hw_constraint_list(substream->runtime, 0,
2175 SNDRV_PCM_HW_PARAM_CHANNELS,
2176 hw_constraints_channels);
2178 snd_pcm_hw_constraint_step(substream->runtime, 0,
2179 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
2182 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
2185 static int simple_playback_pcm_close(struct hda_pcm_stream *hinfo,
2186 struct hda_codec *codec,
2187 struct snd_pcm_substream *substream)
2189 struct hdmi_spec *spec = codec->spec;
2190 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
2193 static int simple_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2194 struct hda_codec *codec,
2195 unsigned int stream_tag,
2196 unsigned int format,
2197 struct snd_pcm_substream *substream)
2199 struct hdmi_spec *spec = codec->spec;
2200 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
2201 stream_tag, format, substream);
2204 static const struct hda_pcm_stream simple_pcm_playback = {
2209 .open = simple_playback_pcm_open,
2210 .close = simple_playback_pcm_close,
2211 .prepare = simple_playback_pcm_prepare
2215 static const struct hda_codec_ops simple_hdmi_patch_ops = {
2216 .build_controls = simple_playback_build_controls,
2217 .build_pcms = simple_playback_build_pcms,
2218 .init = simple_playback_init,
2219 .free = simple_playback_free,
2220 .unsol_event = simple_hdmi_unsol_event,
2223 static int patch_simple_hdmi(struct hda_codec *codec,
2224 hda_nid_t cvt_nid, hda_nid_t pin_nid)
2226 struct hdmi_spec *spec;
2227 struct hdmi_spec_per_cvt *per_cvt;
2228 struct hdmi_spec_per_pin *per_pin;
2230 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2235 hdmi_array_init(spec, 1);
2237 spec->multiout.num_dacs = 0; /* no analog */
2238 spec->multiout.max_channels = 2;
2239 spec->multiout.dig_out_nid = cvt_nid;
2242 per_pin = snd_array_new(&spec->pins);
2243 per_cvt = snd_array_new(&spec->cvts);
2244 if (!per_pin || !per_cvt) {
2245 simple_playback_free(codec);
2248 per_cvt->cvt_nid = cvt_nid;
2249 per_pin->pin_nid = pin_nid;
2250 spec->pcm_playback = simple_pcm_playback;
2252 codec->patch_ops = simple_hdmi_patch_ops;
2257 static void nvhdmi_8ch_7x_set_info_frame_parameters(struct hda_codec *codec,
2260 unsigned int chanmask;
2261 int chan = channels ? (channels - 1) : 1;
2280 /* Set the audio infoframe channel allocation and checksum fields. The
2281 * channel count is computed implicitly by the hardware. */
2282 snd_hda_codec_write(codec, 0x1, 0,
2283 Nv_VERB_SET_Channel_Allocation, chanmask);
2285 snd_hda_codec_write(codec, 0x1, 0,
2286 Nv_VERB_SET_Info_Frame_Checksum,
2287 (0x71 - chan - chanmask));
2290 static int nvhdmi_8ch_7x_pcm_close(struct hda_pcm_stream *hinfo,
2291 struct hda_codec *codec,
2292 struct snd_pcm_substream *substream)
2294 struct hdmi_spec *spec = codec->spec;
2297 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x,
2298 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
2299 for (i = 0; i < 4; i++) {
2300 /* set the stream id */
2301 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
2302 AC_VERB_SET_CHANNEL_STREAMID, 0);
2303 /* set the stream format */
2304 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
2305 AC_VERB_SET_STREAM_FORMAT, 0);
2308 /* The audio hardware sends a channel count of 0x7 (8ch) when all the
2309 * streams are disabled. */
2310 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
2312 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
2315 static int nvhdmi_8ch_7x_pcm_prepare(struct hda_pcm_stream *hinfo,
2316 struct hda_codec *codec,
2317 unsigned int stream_tag,
2318 unsigned int format,
2319 struct snd_pcm_substream *substream)
2322 unsigned int dataDCC2, channel_id;
2324 struct hdmi_spec *spec = codec->spec;
2325 struct hda_spdif_out *spdif;
2326 struct hdmi_spec_per_cvt *per_cvt;
2328 mutex_lock(&codec->spdif_mutex);
2329 per_cvt = get_cvt(spec, 0);
2330 spdif = snd_hda_spdif_out_of_nid(codec, per_cvt->cvt_nid);
2332 chs = substream->runtime->channels;
2336 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
2337 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
2338 snd_hda_codec_write(codec,
2339 nvhdmi_master_con_nid_7x,
2341 AC_VERB_SET_DIGI_CONVERT_1,
2342 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
2344 /* set the stream id */
2345 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
2346 AC_VERB_SET_CHANNEL_STREAMID, (stream_tag << 4) | 0x0);
2348 /* set the stream format */
2349 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
2350 AC_VERB_SET_STREAM_FORMAT, format);
2352 /* turn on again (if needed) */
2353 /* enable and set the channel status audio/data flag */
2354 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE)) {
2355 snd_hda_codec_write(codec,
2356 nvhdmi_master_con_nid_7x,
2358 AC_VERB_SET_DIGI_CONVERT_1,
2359 spdif->ctls & 0xff);
2360 snd_hda_codec_write(codec,
2361 nvhdmi_master_con_nid_7x,
2363 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
2366 for (i = 0; i < 4; i++) {
2372 /* turn off SPDIF once;
2373 *otherwise the IEC958 bits won't be updated
2375 if (codec->spdif_status_reset &&
2376 (spdif->ctls & AC_DIG1_ENABLE))
2377 snd_hda_codec_write(codec,
2378 nvhdmi_con_nids_7x[i],
2380 AC_VERB_SET_DIGI_CONVERT_1,
2381 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
2382 /* set the stream id */
2383 snd_hda_codec_write(codec,
2384 nvhdmi_con_nids_7x[i],
2386 AC_VERB_SET_CHANNEL_STREAMID,
2387 (stream_tag << 4) | channel_id);
2388 /* set the stream format */
2389 snd_hda_codec_write(codec,
2390 nvhdmi_con_nids_7x[i],
2392 AC_VERB_SET_STREAM_FORMAT,
2394 /* turn on again (if needed) */
2395 /* enable and set the channel status audio/data flag */
2396 if (codec->spdif_status_reset &&
2397 (spdif->ctls & AC_DIG1_ENABLE)) {
2398 snd_hda_codec_write(codec,
2399 nvhdmi_con_nids_7x[i],
2401 AC_VERB_SET_DIGI_CONVERT_1,
2402 spdif->ctls & 0xff);
2403 snd_hda_codec_write(codec,
2404 nvhdmi_con_nids_7x[i],
2406 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
2410 nvhdmi_8ch_7x_set_info_frame_parameters(codec, chs);
2412 mutex_unlock(&codec->spdif_mutex);
2416 static const struct hda_pcm_stream nvhdmi_pcm_playback_8ch_7x = {
2420 .nid = nvhdmi_master_con_nid_7x,
2421 .rates = SUPPORTED_RATES,
2422 .maxbps = SUPPORTED_MAXBPS,
2423 .formats = SUPPORTED_FORMATS,
2425 .open = simple_playback_pcm_open,
2426 .close = nvhdmi_8ch_7x_pcm_close,
2427 .prepare = nvhdmi_8ch_7x_pcm_prepare
2431 static int patch_nvhdmi_2ch(struct hda_codec *codec)
2433 struct hdmi_spec *spec;
2434 int err = patch_simple_hdmi(codec, nvhdmi_master_con_nid_7x,
2435 nvhdmi_master_pin_nid_7x);
2439 codec->patch_ops.init = nvhdmi_7x_init_2ch;
2440 /* override the PCM rates, etc, as the codec doesn't give full list */
2442 spec->pcm_playback.rates = SUPPORTED_RATES;
2443 spec->pcm_playback.maxbps = SUPPORTED_MAXBPS;
2444 spec->pcm_playback.formats = SUPPORTED_FORMATS;
2448 static int nvhdmi_7x_8ch_build_pcms(struct hda_codec *codec)
2450 struct hdmi_spec *spec = codec->spec;
2451 int err = simple_playback_build_pcms(codec);
2453 struct hda_pcm *info = get_pcm_rec(spec, 0);
2454 info->own_chmap = true;
2459 static int nvhdmi_7x_8ch_build_controls(struct hda_codec *codec)
2461 struct hdmi_spec *spec = codec->spec;
2462 struct hda_pcm *info;
2463 struct snd_pcm_chmap *chmap;
2466 err = simple_playback_build_controls(codec);
2470 /* add channel maps */
2471 info = get_pcm_rec(spec, 0);
2472 err = snd_pcm_add_chmap_ctls(info->pcm,
2473 SNDRV_PCM_STREAM_PLAYBACK,
2474 snd_pcm_alt_chmaps, 8, 0, &chmap);
2477 switch (codec->preset->id) {
2482 chmap->channel_mask = (1U << 2) | (1U << 8);
2485 chmap->channel_mask = (1U << 2) | (1U << 6) | (1U << 8);
2490 static int patch_nvhdmi_8ch_7x(struct hda_codec *codec)
2492 struct hdmi_spec *spec;
2493 int err = patch_nvhdmi_2ch(codec);
2497 spec->multiout.max_channels = 8;
2498 spec->pcm_playback = nvhdmi_pcm_playback_8ch_7x;
2499 codec->patch_ops.init = nvhdmi_7x_init_8ch;
2500 codec->patch_ops.build_pcms = nvhdmi_7x_8ch_build_pcms;
2501 codec->patch_ops.build_controls = nvhdmi_7x_8ch_build_controls;
2503 /* Initialize the audio infoframe channel mask and checksum to something
2505 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
2510 static int patch_nvhdmi(struct hda_codec *codec)
2512 struct hdmi_spec *spec;
2515 err = patch_generic_hdmi(codec);
2520 spec->dyn_pin_out = true;
2526 * ATI-specific implementations
2528 * FIXME: we may omit the whole this and use the generic code once after
2529 * it's confirmed to work.
2532 #define ATIHDMI_CVT_NID 0x02 /* audio converter */
2533 #define ATIHDMI_PIN_NID 0x03 /* HDMI output pin */
2535 static int atihdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2536 struct hda_codec *codec,
2537 unsigned int stream_tag,
2538 unsigned int format,
2539 struct snd_pcm_substream *substream)
2541 struct hdmi_spec *spec = codec->spec;
2542 struct hdmi_spec_per_cvt *per_cvt = get_cvt(spec, 0);
2543 int chans = substream->runtime->channels;
2546 err = simple_playback_pcm_prepare(hinfo, codec, stream_tag, format,
2550 snd_hda_codec_write(codec, per_cvt->cvt_nid, 0,
2551 AC_VERB_SET_CVT_CHAN_COUNT, chans - 1);
2553 for (i = 0; i < chans; i++) {
2554 snd_hda_codec_write(codec, per_cvt->cvt_nid, 0,
2555 AC_VERB_SET_HDMI_CHAN_SLOT,
2561 static int patch_atihdmi(struct hda_codec *codec)
2563 struct hdmi_spec *spec;
2564 int err = patch_simple_hdmi(codec, ATIHDMI_CVT_NID, ATIHDMI_PIN_NID);
2568 spec->pcm_playback.ops.prepare = atihdmi_playback_pcm_prepare;
2572 /* VIA HDMI Implementation */
2573 #define VIAHDMI_CVT_NID 0x02 /* audio converter1 */
2574 #define VIAHDMI_PIN_NID 0x03 /* HDMI output pin1 */
2576 static int patch_via_hdmi(struct hda_codec *codec)
2578 return patch_simple_hdmi(codec, VIAHDMI_CVT_NID, VIAHDMI_PIN_NID);
2584 static const struct hda_codec_preset snd_hda_preset_hdmi[] = {
2585 { .id = 0x1002793c, .name = "RS600 HDMI", .patch = patch_atihdmi },
2586 { .id = 0x10027919, .name = "RS600 HDMI", .patch = patch_atihdmi },
2587 { .id = 0x1002791a, .name = "RS690/780 HDMI", .patch = patch_atihdmi },
2588 { .id = 0x1002aa01, .name = "R6xx HDMI", .patch = patch_generic_hdmi },
2589 { .id = 0x10951390, .name = "SiI1390 HDMI", .patch = patch_generic_hdmi },
2590 { .id = 0x10951392, .name = "SiI1392 HDMI", .patch = patch_generic_hdmi },
2591 { .id = 0x17e80047, .name = "Chrontel HDMI", .patch = patch_generic_hdmi },
2592 { .id = 0x10de0002, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2593 { .id = 0x10de0003, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2594 { .id = 0x10de0005, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2595 { .id = 0x10de0006, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
2596 { .id = 0x10de0007, .name = "MCP79/7A HDMI", .patch = patch_nvhdmi_8ch_7x },
2597 { .id = 0x10de000a, .name = "GPU 0a HDMI/DP", .patch = patch_nvhdmi },
2598 { .id = 0x10de000b, .name = "GPU 0b HDMI/DP", .patch = patch_nvhdmi },
2599 { .id = 0x10de000c, .name = "MCP89 HDMI", .patch = patch_nvhdmi },
2600 { .id = 0x10de000d, .name = "GPU 0d HDMI/DP", .patch = patch_nvhdmi },
2601 { .id = 0x10de0010, .name = "GPU 10 HDMI/DP", .patch = patch_nvhdmi },
2602 { .id = 0x10de0011, .name = "GPU 11 HDMI/DP", .patch = patch_nvhdmi },
2603 { .id = 0x10de0012, .name = "GPU 12 HDMI/DP", .patch = patch_nvhdmi },
2604 { .id = 0x10de0013, .name = "GPU 13 HDMI/DP", .patch = patch_nvhdmi },
2605 { .id = 0x10de0014, .name = "GPU 14 HDMI/DP", .patch = patch_nvhdmi },
2606 { .id = 0x10de0015, .name = "GPU 15 HDMI/DP", .patch = patch_nvhdmi },
2607 { .id = 0x10de0016, .name = "GPU 16 HDMI/DP", .patch = patch_nvhdmi },
2608 /* 17 is known to be absent */
2609 { .id = 0x10de0018, .name = "GPU 18 HDMI/DP", .patch = patch_nvhdmi },
2610 { .id = 0x10de0019, .name = "GPU 19 HDMI/DP", .patch = patch_nvhdmi },
2611 { .id = 0x10de001a, .name = "GPU 1a HDMI/DP", .patch = patch_nvhdmi },
2612 { .id = 0x10de001b, .name = "GPU 1b HDMI/DP", .patch = patch_nvhdmi },
2613 { .id = 0x10de001c, .name = "GPU 1c HDMI/DP", .patch = patch_nvhdmi },
2614 { .id = 0x10de0040, .name = "GPU 40 HDMI/DP", .patch = patch_nvhdmi },
2615 { .id = 0x10de0041, .name = "GPU 41 HDMI/DP", .patch = patch_nvhdmi },
2616 { .id = 0x10de0042, .name = "GPU 42 HDMI/DP", .patch = patch_nvhdmi },
2617 { .id = 0x10de0043, .name = "GPU 43 HDMI/DP", .patch = patch_nvhdmi },
2618 { .id = 0x10de0044, .name = "GPU 44 HDMI/DP", .patch = patch_nvhdmi },
2619 { .id = 0x10de0051, .name = "GPU 51 HDMI/DP", .patch = patch_nvhdmi },
2620 { .id = 0x10de0060, .name = "GPU 60 HDMI/DP", .patch = patch_nvhdmi },
2621 { .id = 0x10de0067, .name = "MCP67 HDMI", .patch = patch_nvhdmi_2ch },
2622 { .id = 0x10de8001, .name = "MCP73 HDMI", .patch = patch_nvhdmi_2ch },
2623 { .id = 0x11069f80, .name = "VX900 HDMI/DP", .patch = patch_via_hdmi },
2624 { .id = 0x11069f81, .name = "VX900 HDMI/DP", .patch = patch_via_hdmi },
2625 { .id = 0x11069f84, .name = "VX11 HDMI/DP", .patch = patch_generic_hdmi },
2626 { .id = 0x11069f85, .name = "VX11 HDMI/DP", .patch = patch_generic_hdmi },
2627 { .id = 0x80860054, .name = "IbexPeak HDMI", .patch = patch_generic_hdmi },
2628 { .id = 0x80862801, .name = "Bearlake HDMI", .patch = patch_generic_hdmi },
2629 { .id = 0x80862802, .name = "Cantiga HDMI", .patch = patch_generic_hdmi },
2630 { .id = 0x80862803, .name = "Eaglelake HDMI", .patch = patch_generic_hdmi },
2631 { .id = 0x80862804, .name = "IbexPeak HDMI", .patch = patch_generic_hdmi },
2632 { .id = 0x80862805, .name = "CougarPoint HDMI", .patch = patch_generic_hdmi },
2633 { .id = 0x80862806, .name = "PantherPoint HDMI", .patch = patch_generic_hdmi },
2634 { .id = 0x80862807, .name = "Haswell HDMI", .patch = patch_generic_hdmi },
2635 { .id = 0x80862880, .name = "CedarTrail HDMI", .patch = patch_generic_hdmi },
2636 { .id = 0x808629fb, .name = "Crestline HDMI", .patch = patch_generic_hdmi },
2640 MODULE_ALIAS("snd-hda-codec-id:1002793c");
2641 MODULE_ALIAS("snd-hda-codec-id:10027919");
2642 MODULE_ALIAS("snd-hda-codec-id:1002791a");
2643 MODULE_ALIAS("snd-hda-codec-id:1002aa01");
2644 MODULE_ALIAS("snd-hda-codec-id:10951390");
2645 MODULE_ALIAS("snd-hda-codec-id:10951392");
2646 MODULE_ALIAS("snd-hda-codec-id:10de0002");
2647 MODULE_ALIAS("snd-hda-codec-id:10de0003");
2648 MODULE_ALIAS("snd-hda-codec-id:10de0005");
2649 MODULE_ALIAS("snd-hda-codec-id:10de0006");
2650 MODULE_ALIAS("snd-hda-codec-id:10de0007");
2651 MODULE_ALIAS("snd-hda-codec-id:10de000a");
2652 MODULE_ALIAS("snd-hda-codec-id:10de000b");
2653 MODULE_ALIAS("snd-hda-codec-id:10de000c");
2654 MODULE_ALIAS("snd-hda-codec-id:10de000d");
2655 MODULE_ALIAS("snd-hda-codec-id:10de0010");
2656 MODULE_ALIAS("snd-hda-codec-id:10de0011");
2657 MODULE_ALIAS("snd-hda-codec-id:10de0012");
2658 MODULE_ALIAS("snd-hda-codec-id:10de0013");
2659 MODULE_ALIAS("snd-hda-codec-id:10de0014");
2660 MODULE_ALIAS("snd-hda-codec-id:10de0015");
2661 MODULE_ALIAS("snd-hda-codec-id:10de0016");
2662 MODULE_ALIAS("snd-hda-codec-id:10de0018");
2663 MODULE_ALIAS("snd-hda-codec-id:10de0019");
2664 MODULE_ALIAS("snd-hda-codec-id:10de001a");
2665 MODULE_ALIAS("snd-hda-codec-id:10de001b");
2666 MODULE_ALIAS("snd-hda-codec-id:10de001c");
2667 MODULE_ALIAS("snd-hda-codec-id:10de0040");
2668 MODULE_ALIAS("snd-hda-codec-id:10de0041");
2669 MODULE_ALIAS("snd-hda-codec-id:10de0042");
2670 MODULE_ALIAS("snd-hda-codec-id:10de0043");
2671 MODULE_ALIAS("snd-hda-codec-id:10de0044");
2672 MODULE_ALIAS("snd-hda-codec-id:10de0051");
2673 MODULE_ALIAS("snd-hda-codec-id:10de0060");
2674 MODULE_ALIAS("snd-hda-codec-id:10de0067");
2675 MODULE_ALIAS("snd-hda-codec-id:10de8001");
2676 MODULE_ALIAS("snd-hda-codec-id:11069f80");
2677 MODULE_ALIAS("snd-hda-codec-id:11069f81");
2678 MODULE_ALIAS("snd-hda-codec-id:11069f84");
2679 MODULE_ALIAS("snd-hda-codec-id:11069f85");
2680 MODULE_ALIAS("snd-hda-codec-id:17e80047");
2681 MODULE_ALIAS("snd-hda-codec-id:80860054");
2682 MODULE_ALIAS("snd-hda-codec-id:80862801");
2683 MODULE_ALIAS("snd-hda-codec-id:80862802");
2684 MODULE_ALIAS("snd-hda-codec-id:80862803");
2685 MODULE_ALIAS("snd-hda-codec-id:80862804");
2686 MODULE_ALIAS("snd-hda-codec-id:80862805");
2687 MODULE_ALIAS("snd-hda-codec-id:80862806");
2688 MODULE_ALIAS("snd-hda-codec-id:80862807");
2689 MODULE_ALIAS("snd-hda-codec-id:80862880");
2690 MODULE_ALIAS("snd-hda-codec-id:808629fb");
2692 MODULE_LICENSE("GPL");
2693 MODULE_DESCRIPTION("HDMI HD-audio codec");
2694 MODULE_ALIAS("snd-hda-codec-intelhdmi");
2695 MODULE_ALIAS("snd-hda-codec-nvhdmi");
2696 MODULE_ALIAS("snd-hda-codec-atihdmi");
2698 static struct hda_codec_preset_list intel_list = {
2699 .preset = snd_hda_preset_hdmi,
2700 .owner = THIS_MODULE,
2703 static int __init patch_hdmi_init(void)
2705 return snd_hda_add_codec_preset(&intel_list);
2708 static void __exit patch_hdmi_exit(void)
2710 snd_hda_delete_codec_preset(&intel_list);
2713 module_init(patch_hdmi_init)
2714 module_exit(patch_hdmi_exit)