3 * patch_hdmi.c - routines for HDMI/DisplayPort codecs
5 * Copyright(c) 2008-2010 Intel Corporation. All rights reserved.
6 * Copyright (c) 2006 ATI Technologies Inc.
7 * Copyright (c) 2008 NVIDIA Corp. All rights reserved.
8 * Copyright (c) 2008 Wei Ni <wni@nvidia.com>
9 * Copyright (c) 2013 Anssi Hannula <anssi.hannula@iki.fi>
12 * Wu Fengguang <wfg@linux.intel.com>
15 * Wu Fengguang <wfg@linux.intel.com>
17 * This program is free software; you can redistribute it and/or modify it
18 * under the terms of the GNU General Public License as published by the Free
19 * Software Foundation; either version 2 of the License, or (at your option)
22 * This program is distributed in the hope that it will be useful, but
23 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software Foundation,
29 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32 #include <linux/init.h>
33 #include <linux/delay.h>
34 #include <linux/slab.h>
35 #include <linux/module.h>
36 #include <sound/core.h>
37 #include <sound/jack.h>
38 #include <sound/asoundef.h>
39 #include <sound/tlv.h>
40 #include <sound/hdaudio.h>
41 #include <sound/hda_i915.h>
42 #include "hda_codec.h"
43 #include "hda_local.h"
46 static bool static_hdmi_pcm;
47 module_param(static_hdmi_pcm, bool, 0644);
48 MODULE_PARM_DESC(static_hdmi_pcm, "Don't restrict PCM parameters per ELD info");
50 #define is_haswell(codec) ((codec)->core.vendor_id == 0x80862807)
51 #define is_broadwell(codec) ((codec)->core.vendor_id == 0x80862808)
52 #define is_skylake(codec) ((codec)->core.vendor_id == 0x80862809)
53 #define is_broxton(codec) ((codec)->core.vendor_id == 0x8086280a)
54 #define is_kabylake(codec) ((codec)->core.vendor_id == 0x8086280b)
55 #define is_haswell_plus(codec) (is_haswell(codec) || is_broadwell(codec) \
56 || is_skylake(codec) || is_broxton(codec) \
57 || is_kabylake(codec))
59 #define is_valleyview(codec) ((codec)->core.vendor_id == 0x80862882)
60 #define is_cherryview(codec) ((codec)->core.vendor_id == 0x80862883)
61 #define is_valleyview_plus(codec) (is_valleyview(codec) || is_cherryview(codec))
63 struct hdmi_spec_per_cvt {
66 unsigned int channels_min;
67 unsigned int channels_max;
73 /* max. connections to a widget */
74 #define HDA_MAX_CONNECTIONS 32
76 struct hdmi_spec_per_pin {
78 /* pin idx, different device entries on the same pin use the same idx */
81 hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
85 struct hda_codec *codec;
86 struct hdmi_eld sink_eld;
88 struct delayed_work work;
89 struct snd_kcontrol *eld_ctl;
90 struct snd_jack *acomp_jack; /* jack via audio component */
91 struct hdmi_pcm *pcm; /* pointer to spec->pcm_rec[n] dynamically*/
92 int pcm_idx; /* which pcm is attached. -1 means no pcm is attached */
94 bool setup; /* the stream has been set up by prepare callback */
95 int channels; /* current number of channels */
97 bool chmap_set; /* channel-map override by ALSA API? */
98 unsigned char chmap[8]; /* ALSA API channel-map */
99 #ifdef CONFIG_SND_PROC_FS
100 struct snd_info_entry *proc_entry;
104 struct cea_channel_speaker_allocation;
106 /* operations used by generic code that can be overridden by patches */
108 int (*pin_get_eld)(struct hda_codec *codec, hda_nid_t pin_nid,
109 unsigned char *buf, int *eld_size);
111 /* get and set channel assigned to each HDMI ASP (audio sample packet) slot */
112 int (*pin_get_slot_channel)(struct hda_codec *codec, hda_nid_t pin_nid,
114 int (*pin_set_slot_channel)(struct hda_codec *codec, hda_nid_t pin_nid,
115 int asp_slot, int channel);
117 void (*pin_setup_infoframe)(struct hda_codec *codec, hda_nid_t pin_nid,
118 int ca, int active_channels, int conn_type);
120 /* enable/disable HBR (HD passthrough) */
121 int (*pin_hbr_setup)(struct hda_codec *codec, hda_nid_t pin_nid, bool hbr);
123 int (*setup_stream)(struct hda_codec *codec, hda_nid_t cvt_nid,
124 hda_nid_t pin_nid, u32 stream_tag, int format);
126 /* Helpers for producing the channel map TLVs. These can be overridden
127 * for devices that have non-standard mapping requirements. */
128 int (*chmap_cea_alloc_validate_get_type)(struct cea_channel_speaker_allocation *cap,
130 void (*cea_alloc_to_tlv_chmap)(struct cea_channel_speaker_allocation *cap,
131 unsigned int *chmap, int channels);
133 /* check that the user-given chmap is supported */
134 int (*chmap_validate)(int ca, int channels, unsigned char *chmap);
139 struct snd_jack *jack;
144 struct snd_array cvts; /* struct hdmi_spec_per_cvt */
145 hda_nid_t cvt_nids[4]; /* only for haswell fix */
148 struct snd_array pins; /* struct hdmi_spec_per_pin */
149 struct hdmi_pcm pcm_rec[16];
150 struct mutex pcm_lock;
151 /* pcm_bitmap means which pcms have been assigned to pins*/
152 unsigned long pcm_bitmap;
153 int pcm_used; /* counter of pcm_rec[] */
154 /* bitmap shows whether the pcm is opened in user space
155 * bit 0 means the first playback PCM (PCM3);
156 * bit 1 means the second playback PCM, and so on.
158 unsigned long pcm_in_use;
159 unsigned int channels_max; /* max over all cvts */
161 struct hdmi_eld temp_eld;
167 * Non-generic VIA/NVIDIA specific
169 struct hda_multi_out multiout;
170 struct hda_pcm_stream pcm_playback;
172 /* i915/powerwell (Haswell+/Valleyview+) specific */
173 struct i915_audio_component_audio_ops i915_audio_ops;
174 bool i915_bound; /* was i915 bound in this driver? */
177 #ifdef CONFIG_SND_HDA_I915
178 #define codec_has_acomp(codec) \
179 ((codec)->bus->core.audio_component != NULL)
181 #define codec_has_acomp(codec) false
184 struct hdmi_audio_infoframe {
191 u8 CC02_CT47; /* CC in bits 0:2, CT in 4:7 */
195 u8 LFEPBL01_LSV36_DM_INH7;
198 struct dp_audio_infoframe {
201 u8 ver; /* 0x11 << 2 */
203 u8 CC02_CT47; /* match with HDMI infoframe from this on */
207 u8 LFEPBL01_LSV36_DM_INH7;
210 union audio_infoframe {
211 struct hdmi_audio_infoframe hdmi;
212 struct dp_audio_infoframe dp;
217 * CEA speaker placement:
220 * FLW FL FLC FC FRC FR FRW
227 * The Left/Right Surround channel _notions_ LS/RS in SMPTE 320M corresponds to
228 * CEA RL/RR; The SMPTE channel _assignment_ C/LFE is swapped to CEA LFE/FC.
230 enum cea_speaker_placement {
231 FL = (1 << 0), /* Front Left */
232 FC = (1 << 1), /* Front Center */
233 FR = (1 << 2), /* Front Right */
234 FLC = (1 << 3), /* Front Left Center */
235 FRC = (1 << 4), /* Front Right Center */
236 RL = (1 << 5), /* Rear Left */
237 RC = (1 << 6), /* Rear Center */
238 RR = (1 << 7), /* Rear Right */
239 RLC = (1 << 8), /* Rear Left Center */
240 RRC = (1 << 9), /* Rear Right Center */
241 LFE = (1 << 10), /* Low Frequency Effect */
242 FLW = (1 << 11), /* Front Left Wide */
243 FRW = (1 << 12), /* Front Right Wide */
244 FLH = (1 << 13), /* Front Left High */
245 FCH = (1 << 14), /* Front Center High */
246 FRH = (1 << 15), /* Front Right High */
247 TC = (1 << 16), /* Top Center */
251 * ELD SA bits in the CEA Speaker Allocation data block
253 static int eld_speaker_allocation_bits[] = {
261 /* the following are not defined in ELD yet */
268 struct cea_channel_speaker_allocation {
272 /* derived values, just for convenience */
280 * surround40 surround41 surround50 surround51 surround71
281 * ch0 front left = = = =
282 * ch1 front right = = = =
283 * ch2 rear left = = = =
284 * ch3 rear right = = = =
285 * ch4 LFE center center center
290 * surround71 = {FL, FR, RLC, RRC, FC, LFE, RL, RR}
292 static int hdmi_channel_mapping[0x32][8] = {
294 [0x00] = { 0x00, 0x11, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
296 [0x01] = { 0x00, 0x11, 0x22, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
298 [0x02] = { 0x00, 0x11, 0x23, 0xf2, 0xf4, 0xf5, 0xf6, 0xf7 },
300 [0x08] = { 0x00, 0x11, 0x24, 0x35, 0xf3, 0xf2, 0xf6, 0xf7 },
302 [0x03] = { 0x00, 0x11, 0x23, 0x32, 0x44, 0xf5, 0xf6, 0xf7 },
304 [0x09] = { 0x00, 0x11, 0x24, 0x35, 0x42, 0xf3, 0xf6, 0xf7 },
306 [0x0a] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0xf2, 0xf6, 0xf7 },
308 [0x0b] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0x52, 0xf6, 0xf7 },
310 [0x13] = { 0x00, 0x11, 0x26, 0x37, 0x43, 0x52, 0x64, 0x75 },
314 * This is an ordered list!
316 * The preceding ones have better chances to be selected by
317 * hdmi_channel_allocation().
319 static struct cea_channel_speaker_allocation channel_allocations[] = {
320 /* channel: 7 6 5 4 3 2 1 0 */
321 { .ca_index = 0x00, .speakers = { 0, 0, 0, 0, 0, 0, FR, FL } },
323 { .ca_index = 0x01, .speakers = { 0, 0, 0, 0, 0, LFE, FR, FL } },
325 { .ca_index = 0x02, .speakers = { 0, 0, 0, 0, FC, 0, FR, FL } },
327 { .ca_index = 0x08, .speakers = { 0, 0, RR, RL, 0, 0, FR, FL } },
329 { .ca_index = 0x09, .speakers = { 0, 0, RR, RL, 0, LFE, FR, FL } },
331 { .ca_index = 0x0a, .speakers = { 0, 0, RR, RL, FC, 0, FR, FL } },
333 { .ca_index = 0x0b, .speakers = { 0, 0, RR, RL, FC, LFE, FR, FL } },
335 { .ca_index = 0x0f, .speakers = { 0, RC, RR, RL, FC, LFE, FR, FL } },
337 { .ca_index = 0x13, .speakers = { RRC, RLC, RR, RL, FC, LFE, FR, FL } },
339 { .ca_index = 0x03, .speakers = { 0, 0, 0, 0, FC, LFE, FR, FL } },
340 { .ca_index = 0x04, .speakers = { 0, 0, 0, RC, 0, 0, FR, FL } },
341 { .ca_index = 0x05, .speakers = { 0, 0, 0, RC, 0, LFE, FR, FL } },
342 { .ca_index = 0x06, .speakers = { 0, 0, 0, RC, FC, 0, FR, FL } },
343 { .ca_index = 0x07, .speakers = { 0, 0, 0, RC, FC, LFE, FR, FL } },
344 { .ca_index = 0x0c, .speakers = { 0, RC, RR, RL, 0, 0, FR, FL } },
345 { .ca_index = 0x0d, .speakers = { 0, RC, RR, RL, 0, LFE, FR, FL } },
346 { .ca_index = 0x0e, .speakers = { 0, RC, RR, RL, FC, 0, FR, FL } },
347 { .ca_index = 0x10, .speakers = { RRC, RLC, RR, RL, 0, 0, FR, FL } },
348 { .ca_index = 0x11, .speakers = { RRC, RLC, RR, RL, 0, LFE, FR, FL } },
349 { .ca_index = 0x12, .speakers = { RRC, RLC, RR, RL, FC, 0, FR, FL } },
350 { .ca_index = 0x14, .speakers = { FRC, FLC, 0, 0, 0, 0, FR, FL } },
351 { .ca_index = 0x15, .speakers = { FRC, FLC, 0, 0, 0, LFE, FR, FL } },
352 { .ca_index = 0x16, .speakers = { FRC, FLC, 0, 0, FC, 0, FR, FL } },
353 { .ca_index = 0x17, .speakers = { FRC, FLC, 0, 0, FC, LFE, FR, FL } },
354 { .ca_index = 0x18, .speakers = { FRC, FLC, 0, RC, 0, 0, FR, FL } },
355 { .ca_index = 0x19, .speakers = { FRC, FLC, 0, RC, 0, LFE, FR, FL } },
356 { .ca_index = 0x1a, .speakers = { FRC, FLC, 0, RC, FC, 0, FR, FL } },
357 { .ca_index = 0x1b, .speakers = { FRC, FLC, 0, RC, FC, LFE, FR, FL } },
358 { .ca_index = 0x1c, .speakers = { FRC, FLC, RR, RL, 0, 0, FR, FL } },
359 { .ca_index = 0x1d, .speakers = { FRC, FLC, RR, RL, 0, LFE, FR, FL } },
360 { .ca_index = 0x1e, .speakers = { FRC, FLC, RR, RL, FC, 0, FR, FL } },
361 { .ca_index = 0x1f, .speakers = { FRC, FLC, RR, RL, FC, LFE, FR, FL } },
362 { .ca_index = 0x20, .speakers = { 0, FCH, RR, RL, FC, 0, FR, FL } },
363 { .ca_index = 0x21, .speakers = { 0, FCH, RR, RL, FC, LFE, FR, FL } },
364 { .ca_index = 0x22, .speakers = { TC, 0, RR, RL, FC, 0, FR, FL } },
365 { .ca_index = 0x23, .speakers = { TC, 0, RR, RL, FC, LFE, FR, FL } },
366 { .ca_index = 0x24, .speakers = { FRH, FLH, RR, RL, 0, 0, FR, FL } },
367 { .ca_index = 0x25, .speakers = { FRH, FLH, RR, RL, 0, LFE, FR, FL } },
368 { .ca_index = 0x26, .speakers = { FRW, FLW, RR, RL, 0, 0, FR, FL } },
369 { .ca_index = 0x27, .speakers = { FRW, FLW, RR, RL, 0, LFE, FR, FL } },
370 { .ca_index = 0x28, .speakers = { TC, RC, RR, RL, FC, 0, FR, FL } },
371 { .ca_index = 0x29, .speakers = { TC, RC, RR, RL, FC, LFE, FR, FL } },
372 { .ca_index = 0x2a, .speakers = { FCH, RC, RR, RL, FC, 0, FR, FL } },
373 { .ca_index = 0x2b, .speakers = { FCH, RC, RR, RL, FC, LFE, FR, FL } },
374 { .ca_index = 0x2c, .speakers = { TC, FCH, RR, RL, FC, 0, FR, FL } },
375 { .ca_index = 0x2d, .speakers = { TC, FCH, RR, RL, FC, LFE, FR, FL } },
376 { .ca_index = 0x2e, .speakers = { FRH, FLH, RR, RL, FC, 0, FR, FL } },
377 { .ca_index = 0x2f, .speakers = { FRH, FLH, RR, RL, FC, LFE, FR, FL } },
378 { .ca_index = 0x30, .speakers = { FRW, FLW, RR, RL, FC, 0, FR, FL } },
379 { .ca_index = 0x31, .speakers = { FRW, FLW, RR, RL, FC, LFE, FR, FL } },
387 #define get_pin(spec, idx) \
388 ((struct hdmi_spec_per_pin *)snd_array_elem(&spec->pins, idx))
389 #define get_cvt(spec, idx) \
390 ((struct hdmi_spec_per_cvt *)snd_array_elem(&spec->cvts, idx))
391 /* obtain hdmi_pcm object assigned to idx */
392 #define get_hdmi_pcm(spec, idx) (&(spec)->pcm_rec[idx])
393 /* obtain hda_pcm object assigned to idx */
394 #define get_pcm_rec(spec, idx) (get_hdmi_pcm(spec, idx)->pcm)
396 static int pin_nid_to_pin_index(struct hda_codec *codec, hda_nid_t pin_nid)
398 struct hdmi_spec *spec = codec->spec;
401 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++)
402 if (get_pin(spec, pin_idx)->pin_nid == pin_nid)
405 codec_warn(codec, "HDMI: pin nid %d not registered\n", pin_nid);
409 static int hinfo_to_pcm_index(struct hda_codec *codec,
410 struct hda_pcm_stream *hinfo)
412 struct hdmi_spec *spec = codec->spec;
415 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++)
416 if (get_pcm_rec(spec, pcm_idx)->stream == hinfo)
419 codec_warn(codec, "HDMI: hinfo %p not registered\n", hinfo);
423 static int hinfo_to_pin_index(struct hda_codec *codec,
424 struct hda_pcm_stream *hinfo)
426 struct hdmi_spec *spec = codec->spec;
427 struct hdmi_spec_per_pin *per_pin;
430 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
431 per_pin = get_pin(spec, pin_idx);
433 per_pin->pcm->pcm->stream == hinfo)
437 codec_dbg(codec, "HDMI: hinfo %p not registered\n", hinfo);
441 static int cvt_nid_to_cvt_index(struct hda_codec *codec, hda_nid_t cvt_nid)
443 struct hdmi_spec *spec = codec->spec;
446 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++)
447 if (get_cvt(spec, cvt_idx)->cvt_nid == cvt_nid)
450 codec_warn(codec, "HDMI: cvt nid %d not registered\n", cvt_nid);
454 static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
455 struct snd_ctl_elem_info *uinfo)
457 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
458 struct hdmi_spec *spec = codec->spec;
459 struct hdmi_spec_per_pin *per_pin;
460 struct hdmi_eld *eld;
463 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
465 pin_idx = kcontrol->private_value;
466 per_pin = get_pin(spec, pin_idx);
467 eld = &per_pin->sink_eld;
469 mutex_lock(&per_pin->lock);
470 uinfo->count = eld->eld_valid ? eld->eld_size : 0;
471 mutex_unlock(&per_pin->lock);
476 static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
477 struct snd_ctl_elem_value *ucontrol)
479 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
480 struct hdmi_spec *spec = codec->spec;
481 struct hdmi_spec_per_pin *per_pin;
482 struct hdmi_eld *eld;
485 pin_idx = kcontrol->private_value;
486 per_pin = get_pin(spec, pin_idx);
487 eld = &per_pin->sink_eld;
489 mutex_lock(&per_pin->lock);
490 if (eld->eld_size > ARRAY_SIZE(ucontrol->value.bytes.data)) {
491 mutex_unlock(&per_pin->lock);
496 memset(ucontrol->value.bytes.data, 0,
497 ARRAY_SIZE(ucontrol->value.bytes.data));
499 memcpy(ucontrol->value.bytes.data, eld->eld_buffer,
501 mutex_unlock(&per_pin->lock);
506 static struct snd_kcontrol_new eld_bytes_ctl = {
507 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
508 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
510 .info = hdmi_eld_ctl_info,
511 .get = hdmi_eld_ctl_get,
514 static int hdmi_create_eld_ctl(struct hda_codec *codec, int pin_idx,
517 struct snd_kcontrol *kctl;
518 struct hdmi_spec *spec = codec->spec;
521 kctl = snd_ctl_new1(&eld_bytes_ctl, codec);
524 kctl->private_value = pin_idx;
525 kctl->id.device = device;
527 err = snd_hda_ctl_add(codec, get_pin(spec, pin_idx)->pin_nid, kctl);
531 get_pin(spec, pin_idx)->eld_ctl = kctl;
536 static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
537 int *packet_index, int *byte_index)
541 val = snd_hda_codec_read(codec, pin_nid, 0,
542 AC_VERB_GET_HDMI_DIP_INDEX, 0);
544 *packet_index = val >> 5;
545 *byte_index = val & 0x1f;
549 static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
550 int packet_index, int byte_index)
554 val = (packet_index << 5) | (byte_index & 0x1f);
556 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
559 static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t pin_nid,
562 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
565 static void hdmi_init_pin(struct hda_codec *codec, hda_nid_t pin_nid)
567 struct hdmi_spec *spec = codec->spec;
571 if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
572 snd_hda_codec_write(codec, pin_nid, 0,
573 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
575 if (spec->dyn_pin_out)
576 /* Disable pin out until stream is active */
579 /* Enable pin out: some machines with GM965 gets broken output
580 * when the pin is disabled or changed while using with HDMI
584 snd_hda_codec_write(codec, pin_nid, 0,
585 AC_VERB_SET_PIN_WIDGET_CONTROL, pin_out);
588 static int hdmi_get_channel_count(struct hda_codec *codec, hda_nid_t cvt_nid)
590 return 1 + snd_hda_codec_read(codec, cvt_nid, 0,
591 AC_VERB_GET_CVT_CHAN_COUNT, 0);
594 static void hdmi_set_channel_count(struct hda_codec *codec,
595 hda_nid_t cvt_nid, int chs)
597 if (chs != hdmi_get_channel_count(codec, cvt_nid))
598 snd_hda_codec_write(codec, cvt_nid, 0,
599 AC_VERB_SET_CVT_CHAN_COUNT, chs - 1);
606 #ifdef CONFIG_SND_PROC_FS
607 static void print_eld_info(struct snd_info_entry *entry,
608 struct snd_info_buffer *buffer)
610 struct hdmi_spec_per_pin *per_pin = entry->private_data;
612 mutex_lock(&per_pin->lock);
613 snd_hdmi_print_eld_info(&per_pin->sink_eld, buffer);
614 mutex_unlock(&per_pin->lock);
617 static void write_eld_info(struct snd_info_entry *entry,
618 struct snd_info_buffer *buffer)
620 struct hdmi_spec_per_pin *per_pin = entry->private_data;
622 mutex_lock(&per_pin->lock);
623 snd_hdmi_write_eld_info(&per_pin->sink_eld, buffer);
624 mutex_unlock(&per_pin->lock);
627 static int eld_proc_new(struct hdmi_spec_per_pin *per_pin, int index)
630 struct hda_codec *codec = per_pin->codec;
631 struct snd_info_entry *entry;
634 snprintf(name, sizeof(name), "eld#%d.%d", codec->addr, index);
635 err = snd_card_proc_new(codec->card, name, &entry);
639 snd_info_set_text_ops(entry, per_pin, print_eld_info);
640 entry->c.text.write = write_eld_info;
641 entry->mode |= S_IWUSR;
642 per_pin->proc_entry = entry;
647 static void eld_proc_free(struct hdmi_spec_per_pin *per_pin)
649 if (!per_pin->codec->bus->shutdown) {
650 snd_info_free_entry(per_pin->proc_entry);
651 per_pin->proc_entry = NULL;
655 static inline int eld_proc_new(struct hdmi_spec_per_pin *per_pin,
660 static inline void eld_proc_free(struct hdmi_spec_per_pin *per_pin)
666 * Channel mapping routines
670 * Compute derived values in channel_allocations[].
672 static void init_channel_allocations(void)
675 struct cea_channel_speaker_allocation *p;
677 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
678 p = channel_allocations + i;
681 for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
682 if (p->speakers[j]) {
684 p->spk_mask |= p->speakers[j];
689 static int get_channel_allocation_order(int ca)
693 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
694 if (channel_allocations[i].ca_index == ca)
701 * The transformation takes two steps:
703 * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
704 * spk_mask => (channel_allocations[]) => ai->CA
706 * TODO: it could select the wrong CA from multiple candidates.
708 static int hdmi_channel_allocation(struct hda_codec *codec,
709 struct hdmi_eld *eld, int channels)
714 char buf[SND_PRINT_CHANNEL_ALLOCATION_ADVISED_BUFSIZE];
717 * CA defaults to 0 for basic stereo audio
723 * expand ELD's speaker allocation mask
725 * ELD tells the speaker mask in a compact(paired) form,
726 * expand ELD's notions to match the ones used by Audio InfoFrame.
728 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
729 if (eld->info.spk_alloc & (1 << i))
730 spk_mask |= eld_speaker_allocation_bits[i];
733 /* search for the first working match in the CA table */
734 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
735 if (channels == channel_allocations[i].channels &&
736 (spk_mask & channel_allocations[i].spk_mask) ==
737 channel_allocations[i].spk_mask) {
738 ca = channel_allocations[i].ca_index;
744 /* if there was no match, select the regular ALSA channel
745 * allocation with the matching number of channels */
746 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
747 if (channels == channel_allocations[i].channels) {
748 ca = channel_allocations[i].ca_index;
754 snd_print_channel_allocation(eld->info.spk_alloc, buf, sizeof(buf));
755 codec_dbg(codec, "HDMI: select CA 0x%x for %d-channel allocation: %s\n",
761 static void hdmi_debug_channel_mapping(struct hda_codec *codec,
764 #ifdef CONFIG_SND_DEBUG_VERBOSE
765 struct hdmi_spec *spec = codec->spec;
769 for (i = 0; i < 8; i++) {
770 channel = spec->ops.pin_get_slot_channel(codec, pin_nid, i);
771 codec_dbg(codec, "HDMI: ASP channel %d => slot %d\n",
777 static void hdmi_std_setup_channel_mapping(struct hda_codec *codec,
782 struct hdmi_spec *spec = codec->spec;
783 struct cea_channel_speaker_allocation *ch_alloc;
787 int non_pcm_mapping[8];
789 order = get_channel_allocation_order(ca);
790 ch_alloc = &channel_allocations[order];
792 if (hdmi_channel_mapping[ca][1] == 0) {
794 /* fill actual channel mappings in ALSA channel (i) order */
795 for (i = 0; i < ch_alloc->channels; i++) {
796 while (!ch_alloc->speakers[7 - hdmi_slot] && !WARN_ON(hdmi_slot >= 8))
797 hdmi_slot++; /* skip zero slots */
799 hdmi_channel_mapping[ca][i] = (i << 4) | hdmi_slot++;
801 /* fill the rest of the slots with ALSA channel 0xf */
802 for (hdmi_slot = 0; hdmi_slot < 8; hdmi_slot++)
803 if (!ch_alloc->speakers[7 - hdmi_slot])
804 hdmi_channel_mapping[ca][i++] = (0xf << 4) | hdmi_slot;
808 for (i = 0; i < ch_alloc->channels; i++)
809 non_pcm_mapping[i] = (i << 4) | i;
811 non_pcm_mapping[i] = (0xf << 4) | i;
814 for (i = 0; i < 8; i++) {
815 int slotsetup = non_pcm ? non_pcm_mapping[i] : hdmi_channel_mapping[ca][i];
816 int hdmi_slot = slotsetup & 0x0f;
817 int channel = (slotsetup & 0xf0) >> 4;
818 err = spec->ops.pin_set_slot_channel(codec, pin_nid, hdmi_slot, channel);
820 codec_dbg(codec, "HDMI: channel mapping failed\n");
826 struct channel_map_table {
827 unsigned char map; /* ALSA API channel map position */
828 int spk_mask; /* speaker position bit mask */
831 static struct channel_map_table map_tables[] = {
832 { SNDRV_CHMAP_FL, FL },
833 { SNDRV_CHMAP_FR, FR },
834 { SNDRV_CHMAP_RL, RL },
835 { SNDRV_CHMAP_RR, RR },
836 { SNDRV_CHMAP_LFE, LFE },
837 { SNDRV_CHMAP_FC, FC },
838 { SNDRV_CHMAP_RLC, RLC },
839 { SNDRV_CHMAP_RRC, RRC },
840 { SNDRV_CHMAP_RC, RC },
841 { SNDRV_CHMAP_FLC, FLC },
842 { SNDRV_CHMAP_FRC, FRC },
843 { SNDRV_CHMAP_TFL, FLH },
844 { SNDRV_CHMAP_TFR, FRH },
845 { SNDRV_CHMAP_FLW, FLW },
846 { SNDRV_CHMAP_FRW, FRW },
847 { SNDRV_CHMAP_TC, TC },
848 { SNDRV_CHMAP_TFC, FCH },
852 /* from ALSA API channel position to speaker bit mask */
853 static int to_spk_mask(unsigned char c)
855 struct channel_map_table *t = map_tables;
856 for (; t->map; t++) {
863 /* from ALSA API channel position to CEA slot */
864 static int to_cea_slot(int ordered_ca, unsigned char pos)
866 int mask = to_spk_mask(pos);
870 for (i = 0; i < 8; i++) {
871 if (channel_allocations[ordered_ca].speakers[7 - i] == mask)
879 /* from speaker bit mask to ALSA API channel position */
880 static int spk_to_chmap(int spk)
882 struct channel_map_table *t = map_tables;
883 for (; t->map; t++) {
884 if (t->spk_mask == spk)
890 /* from CEA slot to ALSA API channel position */
891 static int from_cea_slot(int ordered_ca, unsigned char slot)
893 int mask = channel_allocations[ordered_ca].speakers[7 - slot];
895 return spk_to_chmap(mask);
898 /* get the CA index corresponding to the given ALSA API channel map */
899 static int hdmi_manual_channel_allocation(int chs, unsigned char *map)
901 int i, spks = 0, spk_mask = 0;
903 for (i = 0; i < chs; i++) {
904 int mask = to_spk_mask(map[i]);
911 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
912 if ((chs == channel_allocations[i].channels ||
913 spks == channel_allocations[i].channels) &&
914 (spk_mask & channel_allocations[i].spk_mask) ==
915 channel_allocations[i].spk_mask)
916 return channel_allocations[i].ca_index;
921 /* set up the channel slots for the given ALSA API channel map */
922 static int hdmi_manual_setup_channel_mapping(struct hda_codec *codec,
924 int chs, unsigned char *map,
927 struct hdmi_spec *spec = codec->spec;
928 int ordered_ca = get_channel_allocation_order(ca);
929 int alsa_pos, hdmi_slot;
930 int assignments[8] = {[0 ... 7] = 0xf};
932 for (alsa_pos = 0; alsa_pos < chs; alsa_pos++) {
934 hdmi_slot = to_cea_slot(ordered_ca, map[alsa_pos]);
937 continue; /* unassigned channel */
939 assignments[hdmi_slot] = alsa_pos;
942 for (hdmi_slot = 0; hdmi_slot < 8; hdmi_slot++) {
945 err = spec->ops.pin_set_slot_channel(codec, pin_nid, hdmi_slot,
946 assignments[hdmi_slot]);
953 /* store ALSA API channel map from the current default map */
954 static void hdmi_setup_fake_chmap(unsigned char *map, int ca)
957 int ordered_ca = get_channel_allocation_order(ca);
958 for (i = 0; i < 8; i++) {
959 if (i < channel_allocations[ordered_ca].channels)
960 map[i] = from_cea_slot(ordered_ca, hdmi_channel_mapping[ca][i] & 0x0f);
966 static void hdmi_setup_channel_mapping(struct hda_codec *codec,
967 hda_nid_t pin_nid, bool non_pcm, int ca,
968 int channels, unsigned char *map,
971 if (!non_pcm && chmap_set) {
972 hdmi_manual_setup_channel_mapping(codec, pin_nid,
975 hdmi_std_setup_channel_mapping(codec, pin_nid, non_pcm, ca);
976 hdmi_setup_fake_chmap(map, ca);
979 hdmi_debug_channel_mapping(codec, pin_nid);
982 static int hdmi_pin_set_slot_channel(struct hda_codec *codec, hda_nid_t pin_nid,
983 int asp_slot, int channel)
985 return snd_hda_codec_write(codec, pin_nid, 0,
986 AC_VERB_SET_HDMI_CHAN_SLOT,
987 (channel << 4) | asp_slot);
990 static int hdmi_pin_get_slot_channel(struct hda_codec *codec, hda_nid_t pin_nid,
993 return (snd_hda_codec_read(codec, pin_nid, 0,
994 AC_VERB_GET_HDMI_CHAN_SLOT,
995 asp_slot) & 0xf0) >> 4;
999 * Audio InfoFrame routines
1003 * Enable Audio InfoFrame Transmission
1005 static void hdmi_start_infoframe_trans(struct hda_codec *codec,
1008 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
1009 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
1014 * Disable Audio InfoFrame Transmission
1016 static void hdmi_stop_infoframe_trans(struct hda_codec *codec,
1019 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
1020 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
1021 AC_DIPXMIT_DISABLE);
1024 static void hdmi_debug_dip_size(struct hda_codec *codec, hda_nid_t pin_nid)
1026 #ifdef CONFIG_SND_DEBUG_VERBOSE
1030 size = snd_hdmi_get_eld_size(codec, pin_nid);
1031 codec_dbg(codec, "HDMI: ELD buf size is %d\n", size);
1033 for (i = 0; i < 8; i++) {
1034 size = snd_hda_codec_read(codec, pin_nid, 0,
1035 AC_VERB_GET_HDMI_DIP_SIZE, i);
1036 codec_dbg(codec, "HDMI: DIP GP[%d] buf size is %d\n", i, size);
1041 static void hdmi_clear_dip_buffers(struct hda_codec *codec, hda_nid_t pin_nid)
1047 for (i = 0; i < 8; i++) {
1048 size = snd_hda_codec_read(codec, pin_nid, 0,
1049 AC_VERB_GET_HDMI_DIP_SIZE, i);
1053 hdmi_set_dip_index(codec, pin_nid, i, 0x0);
1054 for (j = 1; j < 1000; j++) {
1055 hdmi_write_dip_byte(codec, pin_nid, 0x0);
1056 hdmi_get_dip_index(codec, pin_nid, &pi, &bi);
1058 codec_dbg(codec, "dip index %d: %d != %d\n",
1060 if (bi == 0) /* byte index wrapped around */
1064 "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
1070 static void hdmi_checksum_audio_infoframe(struct hdmi_audio_infoframe *hdmi_ai)
1072 u8 *bytes = (u8 *)hdmi_ai;
1076 hdmi_ai->checksum = 0;
1078 for (i = 0; i < sizeof(*hdmi_ai); i++)
1081 hdmi_ai->checksum = -sum;
1084 static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
1090 hdmi_debug_dip_size(codec, pin_nid);
1091 hdmi_clear_dip_buffers(codec, pin_nid); /* be paranoid */
1093 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
1094 for (i = 0; i < size; i++)
1095 hdmi_write_dip_byte(codec, pin_nid, dip[i]);
1098 static bool hdmi_infoframe_uptodate(struct hda_codec *codec, hda_nid_t pin_nid,
1104 if (snd_hda_codec_read(codec, pin_nid, 0, AC_VERB_GET_HDMI_DIP_XMIT, 0)
1108 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
1109 for (i = 0; i < size; i++) {
1110 val = snd_hda_codec_read(codec, pin_nid, 0,
1111 AC_VERB_GET_HDMI_DIP_DATA, 0);
1119 static void hdmi_pin_setup_infoframe(struct hda_codec *codec,
1121 int ca, int active_channels,
1124 union audio_infoframe ai;
1126 memset(&ai, 0, sizeof(ai));
1127 if (conn_type == 0) { /* HDMI */
1128 struct hdmi_audio_infoframe *hdmi_ai = &ai.hdmi;
1130 hdmi_ai->type = 0x84;
1131 hdmi_ai->ver = 0x01;
1132 hdmi_ai->len = 0x0a;
1133 hdmi_ai->CC02_CT47 = active_channels - 1;
1135 hdmi_checksum_audio_infoframe(hdmi_ai);
1136 } else if (conn_type == 1) { /* DisplayPort */
1137 struct dp_audio_infoframe *dp_ai = &ai.dp;
1141 dp_ai->ver = 0x11 << 2;
1142 dp_ai->CC02_CT47 = active_channels - 1;
1145 codec_dbg(codec, "HDMI: unknown connection type at pin %d\n",
1151 * sizeof(ai) is used instead of sizeof(*hdmi_ai) or
1152 * sizeof(*dp_ai) to avoid partial match/update problems when
1153 * the user switches between HDMI/DP monitors.
1155 if (!hdmi_infoframe_uptodate(codec, pin_nid, ai.bytes,
1158 "hdmi_pin_setup_infoframe: pin=%d channels=%d ca=0x%02x\n",
1160 active_channels, ca);
1161 hdmi_stop_infoframe_trans(codec, pin_nid);
1162 hdmi_fill_audio_infoframe(codec, pin_nid,
1163 ai.bytes, sizeof(ai));
1164 hdmi_start_infoframe_trans(codec, pin_nid);
1168 static void hdmi_setup_audio_infoframe(struct hda_codec *codec,
1169 struct hdmi_spec_per_pin *per_pin,
1172 struct hdmi_spec *spec = codec->spec;
1173 hda_nid_t pin_nid = per_pin->pin_nid;
1174 int channels = per_pin->channels;
1175 int active_channels;
1176 struct hdmi_eld *eld;
1182 if (is_haswell_plus(codec))
1183 snd_hda_codec_write(codec, pin_nid, 0,
1184 AC_VERB_SET_AMP_GAIN_MUTE,
1187 eld = &per_pin->sink_eld;
1189 if (!non_pcm && per_pin->chmap_set)
1190 ca = hdmi_manual_channel_allocation(channels, per_pin->chmap);
1192 ca = hdmi_channel_allocation(codec, eld, channels);
1196 ordered_ca = get_channel_allocation_order(ca);
1197 active_channels = channel_allocations[ordered_ca].channels;
1199 hdmi_set_channel_count(codec, per_pin->cvt_nid, active_channels);
1202 * always configure channel mapping, it may have been changed by the
1203 * user in the meantime
1205 hdmi_setup_channel_mapping(codec, pin_nid, non_pcm, ca,
1206 channels, per_pin->chmap,
1207 per_pin->chmap_set);
1209 spec->ops.pin_setup_infoframe(codec, pin_nid, ca, active_channels,
1210 eld->info.conn_type);
1212 per_pin->non_pcm = non_pcm;
1216 * Unsolicited events
1219 static bool hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll);
1221 static void check_presence_and_report(struct hda_codec *codec, hda_nid_t nid)
1223 struct hdmi_spec *spec = codec->spec;
1224 int pin_idx = pin_nid_to_pin_index(codec, nid);
1228 if (hdmi_present_sense(get_pin(spec, pin_idx), 1))
1229 snd_hda_jack_report_sync(codec);
1232 static void jack_callback(struct hda_codec *codec,
1233 struct hda_jack_callback *jack)
1235 check_presence_and_report(codec, jack->tbl->nid);
1238 static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
1240 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
1241 struct hda_jack_tbl *jack;
1242 int dev_entry = (res & AC_UNSOL_RES_DE) >> AC_UNSOL_RES_DE_SHIFT;
1244 jack = snd_hda_jack_tbl_get_from_tag(codec, tag);
1247 jack->jack_dirty = 1;
1250 "HDMI hot plug event: Codec=%d Pin=%d Device=%d Inactive=%d Presence_Detect=%d ELD_Valid=%d\n",
1251 codec->addr, jack->nid, dev_entry, !!(res & AC_UNSOL_RES_IA),
1252 !!(res & AC_UNSOL_RES_PD), !!(res & AC_UNSOL_RES_ELDV));
1254 check_presence_and_report(codec, jack->nid);
1257 static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
1259 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
1260 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
1261 int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
1262 int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
1265 "HDMI CP event: CODEC=%d TAG=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
1280 static void hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
1282 int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
1283 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
1285 if (!snd_hda_jack_tbl_get_from_tag(codec, tag)) {
1286 codec_dbg(codec, "Unexpected HDMI event tag 0x%x\n", tag);
1291 hdmi_intrinsic_event(codec, res);
1293 hdmi_non_intrinsic_event(codec, res);
1296 static void haswell_verify_D0(struct hda_codec *codec,
1297 hda_nid_t cvt_nid, hda_nid_t nid)
1301 /* For Haswell, the converter 1/2 may keep in D3 state after bootup,
1302 * thus pins could only choose converter 0 for use. Make sure the
1303 * converters are in correct power state */
1304 if (!snd_hda_check_power_state(codec, cvt_nid, AC_PWRST_D0))
1305 snd_hda_codec_write(codec, cvt_nid, 0, AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
1307 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0)) {
1308 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
1311 pwr = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
1312 pwr = (pwr & AC_PWRST_ACTUAL) >> AC_PWRST_ACTUAL_SHIFT;
1313 codec_dbg(codec, "Haswell HDMI audio: Power for pin 0x%x is now D%d\n", nid, pwr);
1321 /* HBR should be Non-PCM, 8 channels */
1322 #define is_hbr_format(format) \
1323 ((format & AC_FMT_TYPE_NON_PCM) && (format & AC_FMT_CHAN_MASK) == 7)
1325 static int hdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
1328 int pinctl, new_pinctl;
1330 if (snd_hda_query_pin_caps(codec, pin_nid) & AC_PINCAP_HBR) {
1331 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
1332 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1335 return hbr ? -EINVAL : 0;
1337 new_pinctl = pinctl & ~AC_PINCTL_EPT;
1339 new_pinctl |= AC_PINCTL_EPT_HBR;
1341 new_pinctl |= AC_PINCTL_EPT_NATIVE;
1344 "hdmi_pin_hbr_setup: NID=0x%x, %spinctl=0x%x\n",
1346 pinctl == new_pinctl ? "" : "new-",
1349 if (pinctl != new_pinctl)
1350 snd_hda_codec_write(codec, pin_nid, 0,
1351 AC_VERB_SET_PIN_WIDGET_CONTROL,
1359 static int hdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
1360 hda_nid_t pin_nid, u32 stream_tag, int format)
1362 struct hdmi_spec *spec = codec->spec;
1365 if (is_haswell_plus(codec))
1366 haswell_verify_D0(codec, cvt_nid, pin_nid);
1368 err = spec->ops.pin_hbr_setup(codec, pin_nid, is_hbr_format(format));
1371 codec_dbg(codec, "hdmi_setup_stream: HBR is not supported\n");
1375 snd_hda_codec_setup_stream(codec, cvt_nid, stream_tag, 0, format);
1379 /* Try to find an available converter
1380 * If pin_idx is less then zero, just try to find an available converter.
1381 * Otherwise, try to find an available converter and get the cvt mux index
1384 static int hdmi_choose_cvt(struct hda_codec *codec,
1385 int pin_idx, int *cvt_id, int *mux_id)
1387 struct hdmi_spec *spec = codec->spec;
1388 struct hdmi_spec_per_pin *per_pin;
1389 struct hdmi_spec_per_cvt *per_cvt = NULL;
1390 int cvt_idx, mux_idx = 0;
1392 /* pin_idx < 0 means no pin will be bound to the converter */
1396 per_pin = get_pin(spec, pin_idx);
1398 /* Dynamically assign converter to stream */
1399 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
1400 per_cvt = get_cvt(spec, cvt_idx);
1402 /* Must not already be assigned */
1403 if (per_cvt->assigned)
1405 if (per_pin == NULL)
1407 /* Must be in pin's mux's list of converters */
1408 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
1409 if (per_pin->mux_nids[mux_idx] == per_cvt->cvt_nid)
1411 /* Not in mux list */
1412 if (mux_idx == per_pin->num_mux_nids)
1417 /* No free converters */
1418 if (cvt_idx == spec->num_cvts)
1421 if (per_pin != NULL)
1422 per_pin->mux_idx = mux_idx;
1432 /* Assure the pin select the right convetor */
1433 static void intel_verify_pin_cvt_connect(struct hda_codec *codec,
1434 struct hdmi_spec_per_pin *per_pin)
1436 hda_nid_t pin_nid = per_pin->pin_nid;
1439 mux_idx = per_pin->mux_idx;
1440 curr = snd_hda_codec_read(codec, pin_nid, 0,
1441 AC_VERB_GET_CONNECT_SEL, 0);
1442 if (curr != mux_idx)
1443 snd_hda_codec_write_cache(codec, pin_nid, 0,
1444 AC_VERB_SET_CONNECT_SEL,
1448 /* get the mux index for the converter of the pins
1449 * converter's mux index is the same for all pins on Intel platform
1451 static int intel_cvt_id_to_mux_idx(struct hdmi_spec *spec,
1456 for (i = 0; i < spec->num_cvts; i++)
1457 if (spec->cvt_nids[i] == cvt_nid)
1462 /* Intel HDMI workaround to fix audio routing issue:
1463 * For some Intel display codecs, pins share the same connection list.
1464 * So a conveter can be selected by multiple pins and playback on any of these
1465 * pins will generate sound on the external display, because audio flows from
1466 * the same converter to the display pipeline. Also muting one pin may make
1467 * other pins have no sound output.
1468 * So this function assures that an assigned converter for a pin is not selected
1469 * by any other pins.
1471 static void intel_not_share_assigned_cvt(struct hda_codec *codec,
1472 hda_nid_t pin_nid, int mux_idx)
1474 struct hdmi_spec *spec = codec->spec;
1477 struct hdmi_spec_per_cvt *per_cvt;
1479 /* configure all pins, including "no physical connection" ones */
1480 for_each_hda_codec_node(nid, codec) {
1481 unsigned int wid_caps = get_wcaps(codec, nid);
1482 unsigned int wid_type = get_wcaps_type(wid_caps);
1484 if (wid_type != AC_WID_PIN)
1490 curr = snd_hda_codec_read(codec, nid, 0,
1491 AC_VERB_GET_CONNECT_SEL, 0);
1492 if (curr != mux_idx)
1495 /* choose an unassigned converter. The conveters in the
1496 * connection list are in the same order as in the codec.
1498 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
1499 per_cvt = get_cvt(spec, cvt_idx);
1500 if (!per_cvt->assigned) {
1502 "choose cvt %d for pin nid %d\n",
1504 snd_hda_codec_write_cache(codec, nid, 0,
1505 AC_VERB_SET_CONNECT_SEL,
1513 /* A wrapper of intel_not_share_asigned_cvt() */
1514 static void intel_not_share_assigned_cvt_nid(struct hda_codec *codec,
1515 hda_nid_t pin_nid, hda_nid_t cvt_nid)
1518 struct hdmi_spec *spec = codec->spec;
1520 if (!is_haswell_plus(codec) && !is_valleyview_plus(codec))
1523 /* On Intel platform, the mapping of converter nid to
1524 * mux index of the pins are always the same.
1525 * The pin nid may be 0, this means all pins will not
1526 * share the converter.
1528 mux_idx = intel_cvt_id_to_mux_idx(spec, cvt_nid);
1530 intel_not_share_assigned_cvt(codec, pin_nid, mux_idx);
1533 /* called in hdmi_pcm_open when no pin is assigned to the PCM
1534 * in dyn_pcm_assign mode.
1536 static int hdmi_pcm_open_no_pin(struct hda_pcm_stream *hinfo,
1537 struct hda_codec *codec,
1538 struct snd_pcm_substream *substream)
1540 struct hdmi_spec *spec = codec->spec;
1541 struct snd_pcm_runtime *runtime = substream->runtime;
1542 int cvt_idx, pcm_idx;
1543 struct hdmi_spec_per_cvt *per_cvt = NULL;
1546 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1550 err = hdmi_choose_cvt(codec, -1, &cvt_idx, NULL);
1554 per_cvt = get_cvt(spec, cvt_idx);
1555 per_cvt->assigned = 1;
1556 hinfo->nid = per_cvt->cvt_nid;
1558 intel_not_share_assigned_cvt_nid(codec, 0, per_cvt->cvt_nid);
1560 set_bit(pcm_idx, &spec->pcm_in_use);
1561 /* todo: setup spdif ctls assign */
1563 /* Initially set the converter's capabilities */
1564 hinfo->channels_min = per_cvt->channels_min;
1565 hinfo->channels_max = per_cvt->channels_max;
1566 hinfo->rates = per_cvt->rates;
1567 hinfo->formats = per_cvt->formats;
1568 hinfo->maxbps = per_cvt->maxbps;
1570 /* Store the updated parameters */
1571 runtime->hw.channels_min = hinfo->channels_min;
1572 runtime->hw.channels_max = hinfo->channels_max;
1573 runtime->hw.formats = hinfo->formats;
1574 runtime->hw.rates = hinfo->rates;
1576 snd_pcm_hw_constraint_step(substream->runtime, 0,
1577 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
1584 static int hdmi_pcm_open(struct hda_pcm_stream *hinfo,
1585 struct hda_codec *codec,
1586 struct snd_pcm_substream *substream)
1588 struct hdmi_spec *spec = codec->spec;
1589 struct snd_pcm_runtime *runtime = substream->runtime;
1590 int pin_idx, cvt_idx, pcm_idx, mux_idx = 0;
1591 struct hdmi_spec_per_pin *per_pin;
1592 struct hdmi_eld *eld;
1593 struct hdmi_spec_per_cvt *per_cvt = NULL;
1596 /* Validate hinfo */
1597 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
1601 mutex_lock(&spec->pcm_lock);
1602 pin_idx = hinfo_to_pin_index(codec, hinfo);
1603 if (!spec->dyn_pcm_assign) {
1604 if (snd_BUG_ON(pin_idx < 0)) {
1605 mutex_unlock(&spec->pcm_lock);
1609 /* no pin is assigned to the PCM
1610 * PA need pcm open successfully when probe
1613 err = hdmi_pcm_open_no_pin(hinfo, codec, substream);
1614 mutex_unlock(&spec->pcm_lock);
1619 err = hdmi_choose_cvt(codec, pin_idx, &cvt_idx, &mux_idx);
1621 mutex_unlock(&spec->pcm_lock);
1625 per_cvt = get_cvt(spec, cvt_idx);
1626 /* Claim converter */
1627 per_cvt->assigned = 1;
1629 set_bit(pcm_idx, &spec->pcm_in_use);
1630 per_pin = get_pin(spec, pin_idx);
1631 per_pin->cvt_nid = per_cvt->cvt_nid;
1632 hinfo->nid = per_cvt->cvt_nid;
1634 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
1635 AC_VERB_SET_CONNECT_SEL,
1638 /* configure unused pins to choose other converters */
1639 if (is_haswell_plus(codec) || is_valleyview_plus(codec))
1640 intel_not_share_assigned_cvt(codec, per_pin->pin_nid, mux_idx);
1642 snd_hda_spdif_ctls_assign(codec, pcm_idx, per_cvt->cvt_nid);
1644 /* Initially set the converter's capabilities */
1645 hinfo->channels_min = per_cvt->channels_min;
1646 hinfo->channels_max = per_cvt->channels_max;
1647 hinfo->rates = per_cvt->rates;
1648 hinfo->formats = per_cvt->formats;
1649 hinfo->maxbps = per_cvt->maxbps;
1651 eld = &per_pin->sink_eld;
1652 /* Restrict capabilities by ELD if this isn't disabled */
1653 if (!static_hdmi_pcm && eld->eld_valid) {
1654 snd_hdmi_eld_update_pcm_info(&eld->info, hinfo);
1655 if (hinfo->channels_min > hinfo->channels_max ||
1656 !hinfo->rates || !hinfo->formats) {
1657 per_cvt->assigned = 0;
1659 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
1660 mutex_unlock(&spec->pcm_lock);
1665 mutex_unlock(&spec->pcm_lock);
1666 /* Store the updated parameters */
1667 runtime->hw.channels_min = hinfo->channels_min;
1668 runtime->hw.channels_max = hinfo->channels_max;
1669 runtime->hw.formats = hinfo->formats;
1670 runtime->hw.rates = hinfo->rates;
1672 snd_pcm_hw_constraint_step(substream->runtime, 0,
1673 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
1678 * HDA/HDMI auto parsing
1680 static int hdmi_read_pin_conn(struct hda_codec *codec, int pin_idx)
1682 struct hdmi_spec *spec = codec->spec;
1683 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
1684 hda_nid_t pin_nid = per_pin->pin_nid;
1686 if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
1688 "HDMI: pin %d wcaps %#x does not support connection list\n",
1689 pin_nid, get_wcaps(codec, pin_nid));
1693 per_pin->num_mux_nids = snd_hda_get_connections(codec, pin_nid,
1695 HDA_MAX_CONNECTIONS);
1700 static int hdmi_find_pcm_slot(struct hdmi_spec *spec,
1701 struct hdmi_spec_per_pin *per_pin)
1705 /* try the prefer PCM */
1706 if (!test_bit(per_pin->pin_nid_idx, &spec->pcm_bitmap))
1707 return per_pin->pin_nid_idx;
1709 /* have a second try; check the "reserved area" over num_pins */
1710 for (i = spec->num_pins; i < spec->pcm_used; i++) {
1711 if (!test_bit(i, &spec->pcm_bitmap))
1715 /* the last try; check the empty slots in pins */
1716 for (i = 0; i < spec->num_pins; i++) {
1717 if (!test_bit(i, &spec->pcm_bitmap))
1723 static void hdmi_attach_hda_pcm(struct hdmi_spec *spec,
1724 struct hdmi_spec_per_pin *per_pin)
1728 /* pcm already be attached to the pin */
1731 idx = hdmi_find_pcm_slot(spec, per_pin);
1734 per_pin->pcm_idx = idx;
1735 per_pin->pcm = get_hdmi_pcm(spec, idx);
1736 set_bit(idx, &spec->pcm_bitmap);
1739 static void hdmi_detach_hda_pcm(struct hdmi_spec *spec,
1740 struct hdmi_spec_per_pin *per_pin)
1744 /* pcm already be detached from the pin */
1747 idx = per_pin->pcm_idx;
1748 per_pin->pcm_idx = -1;
1749 per_pin->pcm = NULL;
1750 if (idx >= 0 && idx < spec->pcm_used)
1751 clear_bit(idx, &spec->pcm_bitmap);
1754 static int hdmi_get_pin_cvt_mux(struct hdmi_spec *spec,
1755 struct hdmi_spec_per_pin *per_pin, hda_nid_t cvt_nid)
1759 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
1760 if (per_pin->mux_nids[mux_idx] == cvt_nid)
1765 static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid);
1767 static void hdmi_pcm_setup_pin(struct hdmi_spec *spec,
1768 struct hdmi_spec_per_pin *per_pin)
1770 struct hda_codec *codec = per_pin->codec;
1771 struct hda_pcm *pcm;
1772 struct hda_pcm_stream *hinfo;
1773 struct snd_pcm_substream *substream;
1777 if (per_pin->pcm_idx >= 0 && per_pin->pcm_idx < spec->pcm_used)
1778 pcm = get_pcm_rec(spec, per_pin->pcm_idx);
1781 if (!test_bit(per_pin->pcm_idx, &spec->pcm_in_use))
1784 /* hdmi audio only uses playback and one substream */
1785 hinfo = pcm->stream;
1786 substream = pcm->pcm->streams[0].substream;
1788 per_pin->cvt_nid = hinfo->nid;
1790 mux_idx = hdmi_get_pin_cvt_mux(spec, per_pin, hinfo->nid);
1791 if (mux_idx < per_pin->num_mux_nids)
1792 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
1793 AC_VERB_SET_CONNECT_SEL,
1795 snd_hda_spdif_ctls_assign(codec, per_pin->pcm_idx, hinfo->nid);
1797 non_pcm = check_non_pcm_per_cvt(codec, hinfo->nid);
1798 if (substream->runtime)
1799 per_pin->channels = substream->runtime->channels;
1800 per_pin->setup = true;
1801 per_pin->mux_idx = mux_idx;
1803 hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
1806 static void hdmi_pcm_reset_pin(struct hdmi_spec *spec,
1807 struct hdmi_spec_per_pin *per_pin)
1809 if (per_pin->pcm_idx >= 0 && per_pin->pcm_idx < spec->pcm_used)
1810 snd_hda_spdif_ctls_unassign(per_pin->codec, per_pin->pcm_idx);
1812 per_pin->chmap_set = false;
1813 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
1815 per_pin->setup = false;
1816 per_pin->channels = 0;
1819 /* update per_pin ELD from the given new ELD;
1820 * setup info frame and notification accordingly
1822 static void update_eld(struct hda_codec *codec,
1823 struct hdmi_spec_per_pin *per_pin,
1824 struct hdmi_eld *eld)
1826 struct hdmi_eld *pin_eld = &per_pin->sink_eld;
1827 struct hdmi_spec *spec = codec->spec;
1828 bool old_eld_valid = pin_eld->eld_valid;
1831 if (spec->dyn_pcm_assign) {
1832 if (eld->eld_valid) {
1833 hdmi_attach_hda_pcm(spec, per_pin);
1834 hdmi_pcm_setup_pin(spec, per_pin);
1836 hdmi_pcm_reset_pin(spec, per_pin);
1837 hdmi_detach_hda_pcm(spec, per_pin);
1842 snd_hdmi_show_eld(codec, &eld->info);
1844 eld_changed = (pin_eld->eld_valid != eld->eld_valid);
1845 if (eld->eld_valid && pin_eld->eld_valid)
1846 if (pin_eld->eld_size != eld->eld_size ||
1847 memcmp(pin_eld->eld_buffer, eld->eld_buffer,
1848 eld->eld_size) != 0)
1851 pin_eld->eld_valid = eld->eld_valid;
1852 pin_eld->eld_size = eld->eld_size;
1854 memcpy(pin_eld->eld_buffer, eld->eld_buffer, eld->eld_size);
1855 pin_eld->info = eld->info;
1858 * Re-setup pin and infoframe. This is needed e.g. when
1859 * - sink is first plugged-in
1860 * - transcoder can change during stream playback on Haswell
1861 * and this can make HW reset converter selection on a pin.
1863 if (eld->eld_valid && !old_eld_valid && per_pin->setup) {
1864 if (is_haswell_plus(codec) || is_valleyview_plus(codec)) {
1865 intel_verify_pin_cvt_connect(codec, per_pin);
1866 intel_not_share_assigned_cvt(codec, per_pin->pin_nid,
1870 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
1874 snd_ctl_notify(codec->card,
1875 SNDRV_CTL_EVENT_MASK_VALUE |
1876 SNDRV_CTL_EVENT_MASK_INFO,
1877 &per_pin->eld_ctl->id);
1880 /* update ELD and jack state via HD-audio verbs */
1881 static bool hdmi_present_sense_via_verbs(struct hdmi_spec_per_pin *per_pin,
1884 struct hda_jack_tbl *jack;
1885 struct hda_codec *codec = per_pin->codec;
1886 struct hdmi_spec *spec = codec->spec;
1887 struct hdmi_eld *eld = &spec->temp_eld;
1888 struct hdmi_eld *pin_eld = &per_pin->sink_eld;
1889 hda_nid_t pin_nid = per_pin->pin_nid;
1891 * Always execute a GetPinSense verb here, even when called from
1892 * hdmi_intrinsic_event; for some NVIDIA HW, the unsolicited
1893 * response's PD bit is not the real PD value, but indicates that
1894 * the real PD value changed. An older version of the HD-audio
1895 * specification worked this way. Hence, we just ignore the data in
1896 * the unsolicited response to avoid custom WARs.
1900 bool do_repoll = false;
1902 snd_hda_power_up_pm(codec);
1903 present = snd_hda_pin_sense(codec, pin_nid);
1905 mutex_lock(&per_pin->lock);
1906 pin_eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
1907 if (pin_eld->monitor_present)
1908 eld->eld_valid = !!(present & AC_PINSENSE_ELDV);
1910 eld->eld_valid = false;
1913 "HDMI status: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
1914 codec->addr, pin_nid, pin_eld->monitor_present, eld->eld_valid);
1916 if (eld->eld_valid) {
1917 if (spec->ops.pin_get_eld(codec, pin_nid, eld->eld_buffer,
1918 &eld->eld_size) < 0)
1919 eld->eld_valid = false;
1921 if (snd_hdmi_parse_eld(codec, &eld->info, eld->eld_buffer,
1923 eld->eld_valid = false;
1925 if (!eld->eld_valid && repoll)
1930 schedule_delayed_work(&per_pin->work, msecs_to_jiffies(300));
1932 update_eld(codec, per_pin, eld);
1934 ret = !repoll || !pin_eld->monitor_present || pin_eld->eld_valid;
1936 jack = snd_hda_jack_tbl_get(codec, pin_nid);
1938 jack->block_report = !ret;
1940 mutex_unlock(&per_pin->lock);
1941 snd_hda_power_down_pm(codec);
1945 /* update ELD and jack state via audio component */
1946 static void sync_eld_via_acomp(struct hda_codec *codec,
1947 struct hdmi_spec_per_pin *per_pin)
1949 struct hdmi_spec *spec = codec->spec;
1950 struct hdmi_eld *eld = &spec->temp_eld;
1953 mutex_lock(&per_pin->lock);
1954 size = snd_hdac_acomp_get_eld(&codec->bus->core, per_pin->pin_nid,
1955 &eld->monitor_present, eld->eld_buffer,
1960 size = min(size, ELD_MAX_SIZE);
1961 if (snd_hdmi_parse_eld(codec, &eld->info,
1962 eld->eld_buffer, size) < 0)
1967 eld->eld_valid = true;
1968 eld->eld_size = size;
1970 eld->eld_valid = false;
1974 update_eld(codec, per_pin, eld);
1975 snd_jack_report(per_pin->acomp_jack,
1976 eld->monitor_present ? SND_JACK_AVOUT : 0);
1978 mutex_unlock(&per_pin->lock);
1981 static bool hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll)
1983 struct hda_codec *codec = per_pin->codec;
1984 struct hdmi_spec *spec = codec->spec;
1987 mutex_lock(&spec->pcm_lock);
1988 if (codec_has_acomp(codec)) {
1989 sync_eld_via_acomp(codec, per_pin);
1990 ret = false; /* don't call snd_hda_jack_report_sync() */
1992 ret = hdmi_present_sense_via_verbs(per_pin, repoll);
1994 mutex_unlock(&spec->pcm_lock);
1999 static void hdmi_repoll_eld(struct work_struct *work)
2001 struct hdmi_spec_per_pin *per_pin =
2002 container_of(to_delayed_work(work), struct hdmi_spec_per_pin, work);
2004 if (per_pin->repoll_count++ > 6)
2005 per_pin->repoll_count = 0;
2007 if (hdmi_present_sense(per_pin, per_pin->repoll_count))
2008 snd_hda_jack_report_sync(per_pin->codec);
2011 static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
2014 static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
2016 struct hdmi_spec *spec = codec->spec;
2017 unsigned int caps, config;
2019 struct hdmi_spec_per_pin *per_pin;
2022 caps = snd_hda_query_pin_caps(codec, pin_nid);
2023 if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP)))
2026 config = snd_hda_codec_get_pincfg(codec, pin_nid);
2027 if (get_defcfg_connect(config) == AC_JACK_PORT_NONE)
2030 if (is_haswell_plus(codec))
2031 intel_haswell_fixup_connect_list(codec, pin_nid);
2033 pin_idx = spec->num_pins;
2034 per_pin = snd_array_new(&spec->pins);
2038 per_pin->pin_nid = pin_nid;
2039 per_pin->non_pcm = false;
2040 if (spec->dyn_pcm_assign)
2041 per_pin->pcm_idx = -1;
2043 per_pin->pcm = get_hdmi_pcm(spec, pin_idx);
2044 per_pin->pcm_idx = pin_idx;
2046 per_pin->pin_nid_idx = pin_idx;
2048 err = hdmi_read_pin_conn(codec, pin_idx);
2057 static int hdmi_add_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
2059 struct hdmi_spec *spec = codec->spec;
2060 struct hdmi_spec_per_cvt *per_cvt;
2064 chans = get_wcaps(codec, cvt_nid);
2065 chans = get_wcaps_channels(chans);
2067 per_cvt = snd_array_new(&spec->cvts);
2071 per_cvt->cvt_nid = cvt_nid;
2072 per_cvt->channels_min = 2;
2074 per_cvt->channels_max = chans;
2075 if (chans > spec->channels_max)
2076 spec->channels_max = chans;
2079 err = snd_hda_query_supported_pcm(codec, cvt_nid,
2086 if (spec->num_cvts < ARRAY_SIZE(spec->cvt_nids))
2087 spec->cvt_nids[spec->num_cvts] = cvt_nid;
2093 static int hdmi_parse_codec(struct hda_codec *codec)
2098 nodes = snd_hda_get_sub_nodes(codec, codec->core.afg, &nid);
2099 if (!nid || nodes < 0) {
2100 codec_warn(codec, "HDMI: failed to get afg sub nodes\n");
2104 for (i = 0; i < nodes; i++, nid++) {
2108 caps = get_wcaps(codec, nid);
2109 type = get_wcaps_type(caps);
2111 if (!(caps & AC_WCAP_DIGITAL))
2115 case AC_WID_AUD_OUT:
2116 hdmi_add_cvt(codec, nid);
2119 hdmi_add_pin(codec, nid);
2129 static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
2131 struct hda_spdif_out *spdif;
2134 mutex_lock(&codec->spdif_mutex);
2135 spdif = snd_hda_spdif_out_of_nid(codec, cvt_nid);
2136 non_pcm = !!(spdif->status & IEC958_AES0_NONAUDIO);
2137 mutex_unlock(&codec->spdif_mutex);
2145 static int generic_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
2146 struct hda_codec *codec,
2147 unsigned int stream_tag,
2148 unsigned int format,
2149 struct snd_pcm_substream *substream)
2151 hda_nid_t cvt_nid = hinfo->nid;
2152 struct hdmi_spec *spec = codec->spec;
2154 struct hdmi_spec_per_pin *per_pin;
2156 struct snd_pcm_runtime *runtime = substream->runtime;
2161 mutex_lock(&spec->pcm_lock);
2162 pin_idx = hinfo_to_pin_index(codec, hinfo);
2163 if (spec->dyn_pcm_assign && pin_idx < 0) {
2164 /* when dyn_pcm_assign and pcm is not bound to a pin
2165 * skip pin setup and return 0 to make audio playback
2168 intel_not_share_assigned_cvt_nid(codec, 0, cvt_nid);
2169 snd_hda_codec_setup_stream(codec, cvt_nid,
2170 stream_tag, 0, format);
2171 mutex_unlock(&spec->pcm_lock);
2175 if (snd_BUG_ON(pin_idx < 0)) {
2176 mutex_unlock(&spec->pcm_lock);
2179 per_pin = get_pin(spec, pin_idx);
2180 pin_nid = per_pin->pin_nid;
2181 if (is_haswell_plus(codec) || is_valleyview_plus(codec)) {
2182 /* Verify pin:cvt selections to avoid silent audio after S3.
2183 * After S3, the audio driver restores pin:cvt selections
2184 * but this can happen before gfx is ready and such selection
2185 * is overlooked by HW. Thus multiple pins can share a same
2186 * default convertor and mute control will affect each other,
2187 * which can cause a resumed audio playback become silent
2190 intel_verify_pin_cvt_connect(codec, per_pin);
2191 intel_not_share_assigned_cvt(codec, pin_nid, per_pin->mux_idx);
2194 /* Call sync_audio_rate to set the N/CTS/M manually if necessary */
2195 /* Todo: add DP1.2 MST audio support later */
2196 snd_hdac_sync_audio_rate(&codec->bus->core, pin_nid, runtime->rate);
2198 non_pcm = check_non_pcm_per_cvt(codec, cvt_nid);
2199 mutex_lock(&per_pin->lock);
2200 per_pin->channels = substream->runtime->channels;
2201 per_pin->setup = true;
2203 hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
2204 mutex_unlock(&per_pin->lock);
2205 if (spec->dyn_pin_out) {
2206 pinctl = snd_hda_codec_read(codec, pin_nid, 0,
2207 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2208 snd_hda_codec_write(codec, pin_nid, 0,
2209 AC_VERB_SET_PIN_WIDGET_CONTROL,
2213 err = spec->ops.setup_stream(codec, cvt_nid, pin_nid,
2214 stream_tag, format);
2215 mutex_unlock(&spec->pcm_lock);
2219 static int generic_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
2220 struct hda_codec *codec,
2221 struct snd_pcm_substream *substream)
2223 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
2227 static int hdmi_pcm_close(struct hda_pcm_stream *hinfo,
2228 struct hda_codec *codec,
2229 struct snd_pcm_substream *substream)
2231 struct hdmi_spec *spec = codec->spec;
2232 int cvt_idx, pin_idx, pcm_idx;
2233 struct hdmi_spec_per_cvt *per_cvt;
2234 struct hdmi_spec_per_pin *per_pin;
2238 pcm_idx = hinfo_to_pcm_index(codec, hinfo);
2239 if (snd_BUG_ON(pcm_idx < 0))
2241 cvt_idx = cvt_nid_to_cvt_index(codec, hinfo->nid);
2242 if (snd_BUG_ON(cvt_idx < 0))
2244 per_cvt = get_cvt(spec, cvt_idx);
2246 snd_BUG_ON(!per_cvt->assigned);
2247 per_cvt->assigned = 0;
2250 mutex_lock(&spec->pcm_lock);
2251 clear_bit(pcm_idx, &spec->pcm_in_use);
2252 pin_idx = hinfo_to_pin_index(codec, hinfo);
2253 if (spec->dyn_pcm_assign && pin_idx < 0) {
2254 mutex_unlock(&spec->pcm_lock);
2258 if (snd_BUG_ON(pin_idx < 0)) {
2259 mutex_unlock(&spec->pcm_lock);
2262 per_pin = get_pin(spec, pin_idx);
2264 if (spec->dyn_pin_out) {
2265 pinctl = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
2266 AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
2267 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
2268 AC_VERB_SET_PIN_WIDGET_CONTROL,
2272 snd_hda_spdif_ctls_unassign(codec, pcm_idx);
2274 mutex_lock(&per_pin->lock);
2275 per_pin->chmap_set = false;
2276 memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
2278 per_pin->setup = false;
2279 per_pin->channels = 0;
2280 mutex_unlock(&per_pin->lock);
2281 mutex_unlock(&spec->pcm_lock);
2287 static const struct hda_pcm_ops generic_ops = {
2288 .open = hdmi_pcm_open,
2289 .close = hdmi_pcm_close,
2290 .prepare = generic_hdmi_playback_pcm_prepare,
2291 .cleanup = generic_hdmi_playback_pcm_cleanup,
2295 * ALSA API channel-map control callbacks
2297 static int hdmi_chmap_ctl_info(struct snd_kcontrol *kcontrol,
2298 struct snd_ctl_elem_info *uinfo)
2300 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2301 struct hda_codec *codec = info->private_data;
2302 struct hdmi_spec *spec = codec->spec;
2303 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2304 uinfo->count = spec->channels_max;
2305 uinfo->value.integer.min = 0;
2306 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
2310 static int hdmi_chmap_cea_alloc_validate_get_type(struct cea_channel_speaker_allocation *cap,
2313 /* If the speaker allocation matches the channel count, it is OK.*/
2314 if (cap->channels != channels)
2317 /* all channels are remappable freely */
2318 return SNDRV_CTL_TLVT_CHMAP_VAR;
2321 static void hdmi_cea_alloc_to_tlv_chmap(struct cea_channel_speaker_allocation *cap,
2322 unsigned int *chmap, int channels)
2327 for (c = 7; c >= 0; c--) {
2328 int spk = cap->speakers[c];
2332 chmap[count++] = spk_to_chmap(spk);
2335 WARN_ON(count != channels);
2338 static int hdmi_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2339 unsigned int size, unsigned int __user *tlv)
2341 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2342 struct hda_codec *codec = info->private_data;
2343 struct hdmi_spec *spec = codec->spec;
2344 unsigned int __user *dst;
2349 if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
2353 for (chs = 2; chs <= spec->channels_max; chs++) {
2355 struct cea_channel_speaker_allocation *cap;
2356 cap = channel_allocations;
2357 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++, cap++) {
2358 int chs_bytes = chs * 4;
2359 int type = spec->ops.chmap_cea_alloc_validate_get_type(cap, chs);
2360 unsigned int tlv_chmap[8];
2366 if (put_user(type, dst) ||
2367 put_user(chs_bytes, dst + 1))
2372 if (size < chs_bytes)
2376 spec->ops.cea_alloc_to_tlv_chmap(cap, tlv_chmap, chs);
2377 if (copy_to_user(dst, tlv_chmap, chs_bytes))
2382 if (put_user(count, tlv + 1))
2387 static int hdmi_chmap_ctl_get(struct snd_kcontrol *kcontrol,
2388 struct snd_ctl_elem_value *ucontrol)
2390 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2391 struct hda_codec *codec = info->private_data;
2392 struct hdmi_spec *spec = codec->spec;
2393 int pin_idx = kcontrol->private_value;
2394 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2397 for (i = 0; i < ARRAY_SIZE(per_pin->chmap); i++)
2398 ucontrol->value.integer.value[i] = per_pin->chmap[i];
2402 static int hdmi_chmap_ctl_put(struct snd_kcontrol *kcontrol,
2403 struct snd_ctl_elem_value *ucontrol)
2405 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
2406 struct hda_codec *codec = info->private_data;
2407 struct hdmi_spec *spec = codec->spec;
2408 int pin_idx = kcontrol->private_value;
2409 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2410 unsigned int ctl_idx;
2411 struct snd_pcm_substream *substream;
2412 unsigned char chmap[8];
2413 int i, err, ca, prepared = 0;
2415 ctl_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2416 substream = snd_pcm_chmap_substream(info, ctl_idx);
2417 if (!substream || !substream->runtime)
2418 return 0; /* just for avoiding error from alsactl restore */
2419 switch (substream->runtime->status->state) {
2420 case SNDRV_PCM_STATE_OPEN:
2421 case SNDRV_PCM_STATE_SETUP:
2423 case SNDRV_PCM_STATE_PREPARED:
2429 memset(chmap, 0, sizeof(chmap));
2430 for (i = 0; i < ARRAY_SIZE(chmap); i++)
2431 chmap[i] = ucontrol->value.integer.value[i];
2432 if (!memcmp(chmap, per_pin->chmap, sizeof(chmap)))
2434 ca = hdmi_manual_channel_allocation(ARRAY_SIZE(chmap), chmap);
2437 if (spec->ops.chmap_validate) {
2438 err = spec->ops.chmap_validate(ca, ARRAY_SIZE(chmap), chmap);
2442 mutex_lock(&per_pin->lock);
2443 per_pin->chmap_set = true;
2444 memcpy(per_pin->chmap, chmap, sizeof(chmap));
2446 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
2447 mutex_unlock(&per_pin->lock);
2452 static int generic_hdmi_build_pcms(struct hda_codec *codec)
2454 struct hdmi_spec *spec = codec->spec;
2457 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2458 struct hda_pcm *info;
2459 struct hda_pcm_stream *pstr;
2461 info = snd_hda_codec_pcm_new(codec, "HDMI %d", pin_idx);
2465 spec->pcm_rec[pin_idx].pcm = info;
2467 info->pcm_type = HDA_PCM_TYPE_HDMI;
2468 info->own_chmap = true;
2470 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
2471 pstr->substreams = 1;
2472 pstr->ops = generic_ops;
2473 /* other pstr fields are set in open */
2479 static void free_acomp_jack_priv(struct snd_jack *jack)
2481 struct hdmi_spec_per_pin *per_pin = jack->private_data;
2483 per_pin->acomp_jack = NULL;
2486 static int add_acomp_jack_kctl(struct hda_codec *codec,
2487 struct hdmi_spec_per_pin *per_pin,
2490 struct snd_jack *jack;
2493 err = snd_jack_new(codec->card, name, SND_JACK_AVOUT, &jack,
2497 per_pin->acomp_jack = jack;
2498 jack->private_data = per_pin;
2499 jack->private_free = free_acomp_jack_priv;
2503 static int generic_hdmi_build_jack(struct hda_codec *codec, int pin_idx)
2505 char hdmi_str[32] = "HDMI/DP";
2506 struct hdmi_spec *spec = codec->spec;
2507 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2508 int pcmdev = get_pcm_rec(spec, pin_idx)->device;
2512 sprintf(hdmi_str + strlen(hdmi_str), ",pcm=%d", pcmdev);
2513 if (codec_has_acomp(codec))
2514 return add_acomp_jack_kctl(codec, per_pin, hdmi_str);
2515 phantom_jack = !is_jack_detectable(codec, per_pin->pin_nid);
2517 strncat(hdmi_str, " Phantom",
2518 sizeof(hdmi_str) - strlen(hdmi_str) - 1);
2520 return snd_hda_jack_add_kctl(codec, per_pin->pin_nid, hdmi_str,
2524 static int generic_hdmi_build_controls(struct hda_codec *codec)
2526 struct hdmi_spec *spec = codec->spec;
2530 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2531 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2533 err = generic_hdmi_build_jack(codec, pin_idx);
2537 err = snd_hda_create_dig_out_ctls(codec,
2539 per_pin->mux_nids[0],
2543 /* pin number is the same with pcm number so far */
2544 snd_hda_spdif_ctls_unassign(codec, pin_idx);
2546 /* add control for ELD Bytes */
2547 err = hdmi_create_eld_ctl(codec, pin_idx,
2548 get_pcm_rec(spec, pin_idx)->device);
2553 hdmi_present_sense(per_pin, 0);
2556 /* add channel maps */
2557 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2558 struct hda_pcm *pcm;
2559 struct snd_pcm_chmap *chmap;
2560 struct snd_kcontrol *kctl;
2563 pcm = get_pcm_rec(spec, pin_idx);
2564 if (!pcm || !pcm->pcm)
2566 err = snd_pcm_add_chmap_ctls(pcm->pcm,
2567 SNDRV_PCM_STREAM_PLAYBACK,
2568 NULL, 0, pin_idx, &chmap);
2571 /* override handlers */
2572 chmap->private_data = codec;
2574 for (i = 0; i < kctl->count; i++)
2575 kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_WRITE;
2576 kctl->info = hdmi_chmap_ctl_info;
2577 kctl->get = hdmi_chmap_ctl_get;
2578 kctl->put = hdmi_chmap_ctl_put;
2579 kctl->tlv.c = hdmi_chmap_ctl_tlv;
2585 static int generic_hdmi_init_per_pins(struct hda_codec *codec)
2587 struct hdmi_spec *spec = codec->spec;
2590 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2591 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2593 per_pin->codec = codec;
2594 mutex_init(&per_pin->lock);
2595 INIT_DELAYED_WORK(&per_pin->work, hdmi_repoll_eld);
2596 eld_proc_new(per_pin, pin_idx);
2601 static int generic_hdmi_init(struct hda_codec *codec)
2603 struct hdmi_spec *spec = codec->spec;
2606 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2607 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2608 hda_nid_t pin_nid = per_pin->pin_nid;
2610 hdmi_init_pin(codec, pin_nid);
2611 if (!codec_has_acomp(codec))
2612 snd_hda_jack_detect_enable_callback(codec, pin_nid,
2613 codec->jackpoll_interval > 0 ?
2614 jack_callback : NULL);
2619 static void hdmi_array_init(struct hdmi_spec *spec, int nums)
2621 snd_array_init(&spec->pins, sizeof(struct hdmi_spec_per_pin), nums);
2622 snd_array_init(&spec->cvts, sizeof(struct hdmi_spec_per_cvt), nums);
2625 static void hdmi_array_free(struct hdmi_spec *spec)
2627 snd_array_free(&spec->pins);
2628 snd_array_free(&spec->cvts);
2631 static void generic_hdmi_free(struct hda_codec *codec)
2633 struct hdmi_spec *spec = codec->spec;
2636 if (codec_has_acomp(codec))
2637 snd_hdac_i915_register_notifier(NULL);
2639 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2640 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2642 cancel_delayed_work_sync(&per_pin->work);
2643 eld_proc_free(per_pin);
2644 if (per_pin->acomp_jack)
2645 snd_device_free(codec->card, per_pin->acomp_jack);
2648 if (spec->i915_bound)
2649 snd_hdac_i915_exit(&codec->bus->core);
2650 hdmi_array_free(spec);
2655 static int generic_hdmi_resume(struct hda_codec *codec)
2657 struct hdmi_spec *spec = codec->spec;
2660 codec->patch_ops.init(codec);
2661 regcache_sync(codec->core.regmap);
2663 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
2664 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
2665 hdmi_present_sense(per_pin, 1);
2671 static const struct hda_codec_ops generic_hdmi_patch_ops = {
2672 .init = generic_hdmi_init,
2673 .free = generic_hdmi_free,
2674 .build_pcms = generic_hdmi_build_pcms,
2675 .build_controls = generic_hdmi_build_controls,
2676 .unsol_event = hdmi_unsol_event,
2678 .resume = generic_hdmi_resume,
2682 static const struct hdmi_ops generic_standard_hdmi_ops = {
2683 .pin_get_eld = snd_hdmi_get_eld,
2684 .pin_get_slot_channel = hdmi_pin_get_slot_channel,
2685 .pin_set_slot_channel = hdmi_pin_set_slot_channel,
2686 .pin_setup_infoframe = hdmi_pin_setup_infoframe,
2687 .pin_hbr_setup = hdmi_pin_hbr_setup,
2688 .setup_stream = hdmi_setup_stream,
2689 .chmap_cea_alloc_validate_get_type = hdmi_chmap_cea_alloc_validate_get_type,
2690 .cea_alloc_to_tlv_chmap = hdmi_cea_alloc_to_tlv_chmap,
2694 static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
2697 struct hdmi_spec *spec = codec->spec;
2701 nconns = snd_hda_get_connections(codec, nid, conns, ARRAY_SIZE(conns));
2702 if (nconns == spec->num_cvts &&
2703 !memcmp(conns, spec->cvt_nids, spec->num_cvts * sizeof(hda_nid_t)))
2706 /* override pins connection list */
2707 codec_dbg(codec, "hdmi: haswell: override pin connection 0x%x\n", nid);
2708 snd_hda_override_conn_list(codec, nid, spec->num_cvts, spec->cvt_nids);
2711 #define INTEL_VENDOR_NID 0x08
2712 #define INTEL_GET_VENDOR_VERB 0xf81
2713 #define INTEL_SET_VENDOR_VERB 0x781
2714 #define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
2715 #define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
2717 static void intel_haswell_enable_all_pins(struct hda_codec *codec,
2720 unsigned int vendor_param;
2722 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
2723 INTEL_GET_VENDOR_VERB, 0);
2724 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
2727 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
2728 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
2729 INTEL_SET_VENDOR_VERB, vendor_param);
2730 if (vendor_param == -1)
2734 snd_hda_codec_update_widgets(codec);
2737 static void intel_haswell_fixup_enable_dp12(struct hda_codec *codec)
2739 unsigned int vendor_param;
2741 vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
2742 INTEL_GET_VENDOR_VERB, 0);
2743 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
2746 /* enable DP1.2 mode */
2747 vendor_param |= INTEL_EN_DP12;
2748 snd_hdac_regmap_add_vendor_verb(&codec->core, INTEL_SET_VENDOR_VERB);
2749 snd_hda_codec_write_cache(codec, INTEL_VENDOR_NID, 0,
2750 INTEL_SET_VENDOR_VERB, vendor_param);
2753 /* Haswell needs to re-issue the vendor-specific verbs before turning to D0.
2754 * Otherwise you may get severe h/w communication errors.
2756 static void haswell_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2757 unsigned int power_state)
2759 if (power_state == AC_PWRST_D0) {
2760 intel_haswell_enable_all_pins(codec, false);
2761 intel_haswell_fixup_enable_dp12(codec);
2764 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE, power_state);
2765 snd_hda_codec_set_power_to_all(codec, fg, power_state);
2768 static void intel_pin_eld_notify(void *audio_ptr, int port)
2770 struct hda_codec *codec = audio_ptr;
2771 int pin_nid = port + 0x04;
2773 /* skip notification during system suspend (but not in runtime PM);
2774 * the state will be updated at resume
2776 if (snd_power_get_state(codec->card) != SNDRV_CTL_POWER_D0)
2778 /* ditto during suspend/resume process itself */
2779 if (atomic_read(&(codec)->core.in_pm))
2782 check_presence_and_report(codec, pin_nid);
2785 static int patch_generic_hdmi(struct hda_codec *codec)
2787 struct hdmi_spec *spec;
2789 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2793 spec->ops = generic_standard_hdmi_ops;
2794 mutex_init(&spec->pcm_lock);
2796 hdmi_array_init(spec, 4);
2798 /* Try to bind with i915 for any Intel codecs (if not done yet) */
2799 if (!codec_has_acomp(codec) &&
2800 (codec->core.vendor_id >> 16) == 0x8086)
2801 if (!snd_hdac_i915_init(&codec->bus->core))
2802 spec->i915_bound = true;
2804 if (is_haswell_plus(codec)) {
2805 intel_haswell_enable_all_pins(codec, true);
2806 intel_haswell_fixup_enable_dp12(codec);
2809 /* For Valleyview/Cherryview, only the display codec is in the display
2810 * power well and can use link_power ops to request/release the power.
2811 * For Haswell/Broadwell, the controller is also in the power well and
2812 * can cover the codec power request, and so need not set this flag.
2813 * For previous platforms, there is no such power well feature.
2815 if (is_valleyview_plus(codec) || is_skylake(codec) ||
2817 codec->core.link_power_control = 1;
2819 if (codec_has_acomp(codec)) {
2820 codec->depop_delay = 0;
2821 spec->i915_audio_ops.audio_ptr = codec;
2822 spec->i915_audio_ops.pin_eld_notify = intel_pin_eld_notify;
2823 snd_hdac_i915_register_notifier(&spec->i915_audio_ops);
2826 if (hdmi_parse_codec(codec) < 0) {
2827 if (spec->i915_bound)
2828 snd_hdac_i915_exit(&codec->bus->core);
2833 codec->patch_ops = generic_hdmi_patch_ops;
2834 if (is_haswell_plus(codec)) {
2835 codec->patch_ops.set_power_state = haswell_set_power_state;
2836 codec->dp_mst = true;
2839 /* Enable runtime pm for HDMI audio codec of HSW/BDW/SKL/BYT/BSW */
2840 if (is_haswell_plus(codec) || is_valleyview_plus(codec))
2841 codec->auto_runtime_pm = 1;
2843 generic_hdmi_init_per_pins(codec);
2845 init_channel_allocations();
2851 * Shared non-generic implementations
2854 static int simple_playback_build_pcms(struct hda_codec *codec)
2856 struct hdmi_spec *spec = codec->spec;
2857 struct hda_pcm *info;
2859 struct hda_pcm_stream *pstr;
2860 struct hdmi_spec_per_cvt *per_cvt;
2862 per_cvt = get_cvt(spec, 0);
2863 chans = get_wcaps(codec, per_cvt->cvt_nid);
2864 chans = get_wcaps_channels(chans);
2866 info = snd_hda_codec_pcm_new(codec, "HDMI 0");
2869 spec->pcm_rec[0].pcm = info;
2870 info->pcm_type = HDA_PCM_TYPE_HDMI;
2871 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
2872 *pstr = spec->pcm_playback;
2873 pstr->nid = per_cvt->cvt_nid;
2874 if (pstr->channels_max <= 2 && chans && chans <= 16)
2875 pstr->channels_max = chans;
2880 /* unsolicited event for jack sensing */
2881 static void simple_hdmi_unsol_event(struct hda_codec *codec,
2884 snd_hda_jack_set_dirty_all(codec);
2885 snd_hda_jack_report_sync(codec);
2888 /* generic_hdmi_build_jack can be used for simple_hdmi, too,
2889 * as long as spec->pins[] is set correctly
2891 #define simple_hdmi_build_jack generic_hdmi_build_jack
2893 static int simple_playback_build_controls(struct hda_codec *codec)
2895 struct hdmi_spec *spec = codec->spec;
2896 struct hdmi_spec_per_cvt *per_cvt;
2899 per_cvt = get_cvt(spec, 0);
2900 err = snd_hda_create_dig_out_ctls(codec, per_cvt->cvt_nid,
2905 return simple_hdmi_build_jack(codec, 0);
2908 static int simple_playback_init(struct hda_codec *codec)
2910 struct hdmi_spec *spec = codec->spec;
2911 struct hdmi_spec_per_pin *per_pin = get_pin(spec, 0);
2912 hda_nid_t pin = per_pin->pin_nid;
2914 snd_hda_codec_write(codec, pin, 0,
2915 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
2916 /* some codecs require to unmute the pin */
2917 if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
2918 snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
2920 snd_hda_jack_detect_enable(codec, pin);
2924 static void simple_playback_free(struct hda_codec *codec)
2926 struct hdmi_spec *spec = codec->spec;
2928 hdmi_array_free(spec);
2933 * Nvidia specific implementations
2936 #define Nv_VERB_SET_Channel_Allocation 0xF79
2937 #define Nv_VERB_SET_Info_Frame_Checksum 0xF7A
2938 #define Nv_VERB_SET_Audio_Protection_On 0xF98
2939 #define Nv_VERB_SET_Audio_Protection_Off 0xF99
2941 #define nvhdmi_master_con_nid_7x 0x04
2942 #define nvhdmi_master_pin_nid_7x 0x05
2944 static const hda_nid_t nvhdmi_con_nids_7x[4] = {
2945 /*front, rear, clfe, rear_surr */
2949 static const struct hda_verb nvhdmi_basic_init_7x_2ch[] = {
2950 /* set audio protect on */
2951 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
2952 /* enable digital output on pin widget */
2953 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2957 static const struct hda_verb nvhdmi_basic_init_7x_8ch[] = {
2958 /* set audio protect on */
2959 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
2960 /* enable digital output on pin widget */
2961 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2962 { 0x7, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2963 { 0x9, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2964 { 0xb, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2965 { 0xd, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
2969 #ifdef LIMITED_RATE_FMT_SUPPORT
2970 /* support only the safe format and rate */
2971 #define SUPPORTED_RATES SNDRV_PCM_RATE_48000
2972 #define SUPPORTED_MAXBPS 16
2973 #define SUPPORTED_FORMATS SNDRV_PCM_FMTBIT_S16_LE
2975 /* support all rates and formats */
2976 #define SUPPORTED_RATES \
2977 (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
2978 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
2979 SNDRV_PCM_RATE_192000)
2980 #define SUPPORTED_MAXBPS 24
2981 #define SUPPORTED_FORMATS \
2982 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
2985 static int nvhdmi_7x_init_2ch(struct hda_codec *codec)
2987 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_2ch);
2991 static int nvhdmi_7x_init_8ch(struct hda_codec *codec)
2993 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_8ch);
2997 static unsigned int channels_2_6_8[] = {
3001 static unsigned int channels_2_8[] = {
3005 static struct snd_pcm_hw_constraint_list hw_constraints_2_6_8_channels = {
3006 .count = ARRAY_SIZE(channels_2_6_8),
3007 .list = channels_2_6_8,
3011 static struct snd_pcm_hw_constraint_list hw_constraints_2_8_channels = {
3012 .count = ARRAY_SIZE(channels_2_8),
3013 .list = channels_2_8,
3017 static int simple_playback_pcm_open(struct hda_pcm_stream *hinfo,
3018 struct hda_codec *codec,
3019 struct snd_pcm_substream *substream)
3021 struct hdmi_spec *spec = codec->spec;
3022 struct snd_pcm_hw_constraint_list *hw_constraints_channels = NULL;
3024 switch (codec->preset->vendor_id) {
3029 hw_constraints_channels = &hw_constraints_2_8_channels;
3032 hw_constraints_channels = &hw_constraints_2_6_8_channels;
3038 if (hw_constraints_channels != NULL) {
3039 snd_pcm_hw_constraint_list(substream->runtime, 0,
3040 SNDRV_PCM_HW_PARAM_CHANNELS,
3041 hw_constraints_channels);
3043 snd_pcm_hw_constraint_step(substream->runtime, 0,
3044 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
3047 return snd_hda_multi_out_dig_open(codec, &spec->multiout);
3050 static int simple_playback_pcm_close(struct hda_pcm_stream *hinfo,
3051 struct hda_codec *codec,
3052 struct snd_pcm_substream *substream)
3054 struct hdmi_spec *spec = codec->spec;
3055 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3058 static int simple_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
3059 struct hda_codec *codec,
3060 unsigned int stream_tag,
3061 unsigned int format,
3062 struct snd_pcm_substream *substream)
3064 struct hdmi_spec *spec = codec->spec;
3065 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
3066 stream_tag, format, substream);
3069 static const struct hda_pcm_stream simple_pcm_playback = {
3074 .open = simple_playback_pcm_open,
3075 .close = simple_playback_pcm_close,
3076 .prepare = simple_playback_pcm_prepare
3080 static const struct hda_codec_ops simple_hdmi_patch_ops = {
3081 .build_controls = simple_playback_build_controls,
3082 .build_pcms = simple_playback_build_pcms,
3083 .init = simple_playback_init,
3084 .free = simple_playback_free,
3085 .unsol_event = simple_hdmi_unsol_event,
3088 static int patch_simple_hdmi(struct hda_codec *codec,
3089 hda_nid_t cvt_nid, hda_nid_t pin_nid)
3091 struct hdmi_spec *spec;
3092 struct hdmi_spec_per_cvt *per_cvt;
3093 struct hdmi_spec_per_pin *per_pin;
3095 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
3100 hdmi_array_init(spec, 1);
3102 spec->multiout.num_dacs = 0; /* no analog */
3103 spec->multiout.max_channels = 2;
3104 spec->multiout.dig_out_nid = cvt_nid;
3107 per_pin = snd_array_new(&spec->pins);
3108 per_cvt = snd_array_new(&spec->cvts);
3109 if (!per_pin || !per_cvt) {
3110 simple_playback_free(codec);
3113 per_cvt->cvt_nid = cvt_nid;
3114 per_pin->pin_nid = pin_nid;
3115 spec->pcm_playback = simple_pcm_playback;
3117 codec->patch_ops = simple_hdmi_patch_ops;
3122 static void nvhdmi_8ch_7x_set_info_frame_parameters(struct hda_codec *codec,
3125 unsigned int chanmask;
3126 int chan = channels ? (channels - 1) : 1;
3145 /* Set the audio infoframe channel allocation and checksum fields. The
3146 * channel count is computed implicitly by the hardware. */
3147 snd_hda_codec_write(codec, 0x1, 0,
3148 Nv_VERB_SET_Channel_Allocation, chanmask);
3150 snd_hda_codec_write(codec, 0x1, 0,
3151 Nv_VERB_SET_Info_Frame_Checksum,
3152 (0x71 - chan - chanmask));
3155 static int nvhdmi_8ch_7x_pcm_close(struct hda_pcm_stream *hinfo,
3156 struct hda_codec *codec,
3157 struct snd_pcm_substream *substream)
3159 struct hdmi_spec *spec = codec->spec;
3162 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x,
3163 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
3164 for (i = 0; i < 4; i++) {
3165 /* set the stream id */
3166 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
3167 AC_VERB_SET_CHANNEL_STREAMID, 0);
3168 /* set the stream format */
3169 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
3170 AC_VERB_SET_STREAM_FORMAT, 0);
3173 /* The audio hardware sends a channel count of 0x7 (8ch) when all the
3174 * streams are disabled. */
3175 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
3177 return snd_hda_multi_out_dig_close(codec, &spec->multiout);
3180 static int nvhdmi_8ch_7x_pcm_prepare(struct hda_pcm_stream *hinfo,
3181 struct hda_codec *codec,
3182 unsigned int stream_tag,
3183 unsigned int format,
3184 struct snd_pcm_substream *substream)
3187 unsigned int dataDCC2, channel_id;
3189 struct hdmi_spec *spec = codec->spec;
3190 struct hda_spdif_out *spdif;
3191 struct hdmi_spec_per_cvt *per_cvt;
3193 mutex_lock(&codec->spdif_mutex);
3194 per_cvt = get_cvt(spec, 0);
3195 spdif = snd_hda_spdif_out_of_nid(codec, per_cvt->cvt_nid);
3197 chs = substream->runtime->channels;
3201 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
3202 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
3203 snd_hda_codec_write(codec,
3204 nvhdmi_master_con_nid_7x,
3206 AC_VERB_SET_DIGI_CONVERT_1,
3207 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
3209 /* set the stream id */
3210 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
3211 AC_VERB_SET_CHANNEL_STREAMID, (stream_tag << 4) | 0x0);
3213 /* set the stream format */
3214 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
3215 AC_VERB_SET_STREAM_FORMAT, format);
3217 /* turn on again (if needed) */
3218 /* enable and set the channel status audio/data flag */
3219 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE)) {
3220 snd_hda_codec_write(codec,
3221 nvhdmi_master_con_nid_7x,
3223 AC_VERB_SET_DIGI_CONVERT_1,
3224 spdif->ctls & 0xff);
3225 snd_hda_codec_write(codec,
3226 nvhdmi_master_con_nid_7x,
3228 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
3231 for (i = 0; i < 4; i++) {
3237 /* turn off SPDIF once;
3238 *otherwise the IEC958 bits won't be updated
3240 if (codec->spdif_status_reset &&
3241 (spdif->ctls & AC_DIG1_ENABLE))
3242 snd_hda_codec_write(codec,
3243 nvhdmi_con_nids_7x[i],
3245 AC_VERB_SET_DIGI_CONVERT_1,
3246 spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
3247 /* set the stream id */
3248 snd_hda_codec_write(codec,
3249 nvhdmi_con_nids_7x[i],
3251 AC_VERB_SET_CHANNEL_STREAMID,
3252 (stream_tag << 4) | channel_id);
3253 /* set the stream format */
3254 snd_hda_codec_write(codec,
3255 nvhdmi_con_nids_7x[i],
3257 AC_VERB_SET_STREAM_FORMAT,
3259 /* turn on again (if needed) */
3260 /* enable and set the channel status audio/data flag */
3261 if (codec->spdif_status_reset &&
3262 (spdif->ctls & AC_DIG1_ENABLE)) {
3263 snd_hda_codec_write(codec,
3264 nvhdmi_con_nids_7x[i],
3266 AC_VERB_SET_DIGI_CONVERT_1,
3267 spdif->ctls & 0xff);
3268 snd_hda_codec_write(codec,
3269 nvhdmi_con_nids_7x[i],
3271 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
3275 nvhdmi_8ch_7x_set_info_frame_parameters(codec, chs);
3277 mutex_unlock(&codec->spdif_mutex);
3281 static const struct hda_pcm_stream nvhdmi_pcm_playback_8ch_7x = {
3285 .nid = nvhdmi_master_con_nid_7x,
3286 .rates = SUPPORTED_RATES,
3287 .maxbps = SUPPORTED_MAXBPS,
3288 .formats = SUPPORTED_FORMATS,
3290 .open = simple_playback_pcm_open,
3291 .close = nvhdmi_8ch_7x_pcm_close,
3292 .prepare = nvhdmi_8ch_7x_pcm_prepare
3296 static int patch_nvhdmi_2ch(struct hda_codec *codec)
3298 struct hdmi_spec *spec;
3299 int err = patch_simple_hdmi(codec, nvhdmi_master_con_nid_7x,
3300 nvhdmi_master_pin_nid_7x);
3304 codec->patch_ops.init = nvhdmi_7x_init_2ch;
3305 /* override the PCM rates, etc, as the codec doesn't give full list */
3307 spec->pcm_playback.rates = SUPPORTED_RATES;
3308 spec->pcm_playback.maxbps = SUPPORTED_MAXBPS;
3309 spec->pcm_playback.formats = SUPPORTED_FORMATS;
3313 static int nvhdmi_7x_8ch_build_pcms(struct hda_codec *codec)
3315 struct hdmi_spec *spec = codec->spec;
3316 int err = simple_playback_build_pcms(codec);
3318 struct hda_pcm *info = get_pcm_rec(spec, 0);
3319 info->own_chmap = true;
3324 static int nvhdmi_7x_8ch_build_controls(struct hda_codec *codec)
3326 struct hdmi_spec *spec = codec->spec;
3327 struct hda_pcm *info;
3328 struct snd_pcm_chmap *chmap;
3331 err = simple_playback_build_controls(codec);
3335 /* add channel maps */
3336 info = get_pcm_rec(spec, 0);
3337 err = snd_pcm_add_chmap_ctls(info->pcm,
3338 SNDRV_PCM_STREAM_PLAYBACK,
3339 snd_pcm_alt_chmaps, 8, 0, &chmap);
3342 switch (codec->preset->vendor_id) {
3347 chmap->channel_mask = (1U << 2) | (1U << 8);
3350 chmap->channel_mask = (1U << 2) | (1U << 6) | (1U << 8);
3355 static int patch_nvhdmi_8ch_7x(struct hda_codec *codec)
3357 struct hdmi_spec *spec;
3358 int err = patch_nvhdmi_2ch(codec);
3362 spec->multiout.max_channels = 8;
3363 spec->pcm_playback = nvhdmi_pcm_playback_8ch_7x;
3364 codec->patch_ops.init = nvhdmi_7x_init_8ch;
3365 codec->patch_ops.build_pcms = nvhdmi_7x_8ch_build_pcms;
3366 codec->patch_ops.build_controls = nvhdmi_7x_8ch_build_controls;
3368 /* Initialize the audio infoframe channel mask and checksum to something
3370 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
3376 * NVIDIA codecs ignore ASP mapping for 2ch - confirmed on:
3380 static int nvhdmi_chmap_cea_alloc_validate_get_type(struct cea_channel_speaker_allocation *cap,
3383 if (cap->ca_index == 0x00 && channels == 2)
3384 return SNDRV_CTL_TLVT_CHMAP_FIXED;
3386 return hdmi_chmap_cea_alloc_validate_get_type(cap, channels);
3389 static int nvhdmi_chmap_validate(int ca, int chs, unsigned char *map)
3391 if (ca == 0x00 && (map[0] != SNDRV_CHMAP_FL || map[1] != SNDRV_CHMAP_FR))
3397 static int patch_nvhdmi(struct hda_codec *codec)
3399 struct hdmi_spec *spec;
3402 err = patch_generic_hdmi(codec);
3407 spec->dyn_pin_out = true;
3409 spec->ops.chmap_cea_alloc_validate_get_type =
3410 nvhdmi_chmap_cea_alloc_validate_get_type;
3411 spec->ops.chmap_validate = nvhdmi_chmap_validate;
3417 * The HDA codec on NVIDIA Tegra contains two scratch registers that are
3418 * accessed using vendor-defined verbs. These registers can be used for
3419 * interoperability between the HDA and HDMI drivers.
3422 /* Audio Function Group node */
3423 #define NVIDIA_AFG_NID 0x01
3426 * The SCRATCH0 register is used to notify the HDMI codec of changes in audio
3427 * format. On Tegra, bit 31 is used as a trigger that causes an interrupt to
3428 * be raised in the HDMI codec. The remainder of the bits is arbitrary. This
3429 * implementation stores the HDA format (see AC_FMT_*) in bits [15:0] and an
3430 * additional bit (at position 30) to signal the validity of the format.
3432 * | 31 | 30 | 29 16 | 15 0 |
3433 * +---------+-------+--------+--------+
3434 * | TRIGGER | VALID | UNUSED | FORMAT |
3435 * +-----------------------------------|
3437 * Note that for the trigger bit to take effect it needs to change value
3438 * (i.e. it needs to be toggled).
3440 #define NVIDIA_GET_SCRATCH0 0xfa6
3441 #define NVIDIA_SET_SCRATCH0_BYTE0 0xfa7
3442 #define NVIDIA_SET_SCRATCH0_BYTE1 0xfa8
3443 #define NVIDIA_SET_SCRATCH0_BYTE2 0xfa9
3444 #define NVIDIA_SET_SCRATCH0_BYTE3 0xfaa
3445 #define NVIDIA_SCRATCH_TRIGGER (1 << 7)
3446 #define NVIDIA_SCRATCH_VALID (1 << 6)
3448 #define NVIDIA_GET_SCRATCH1 0xfab
3449 #define NVIDIA_SET_SCRATCH1_BYTE0 0xfac
3450 #define NVIDIA_SET_SCRATCH1_BYTE1 0xfad
3451 #define NVIDIA_SET_SCRATCH1_BYTE2 0xfae
3452 #define NVIDIA_SET_SCRATCH1_BYTE3 0xfaf
3455 * The format parameter is the HDA audio format (see AC_FMT_*). If set to 0,
3456 * the format is invalidated so that the HDMI codec can be disabled.
3458 static void tegra_hdmi_set_format(struct hda_codec *codec, unsigned int format)
3462 /* bits [31:30] contain the trigger and valid bits */
3463 value = snd_hda_codec_read(codec, NVIDIA_AFG_NID, 0,
3464 NVIDIA_GET_SCRATCH0, 0);
3465 value = (value >> 24) & 0xff;
3467 /* bits [15:0] are used to store the HDA format */
3468 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3469 NVIDIA_SET_SCRATCH0_BYTE0,
3470 (format >> 0) & 0xff);
3471 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3472 NVIDIA_SET_SCRATCH0_BYTE1,
3473 (format >> 8) & 0xff);
3475 /* bits [16:24] are unused */
3476 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3477 NVIDIA_SET_SCRATCH0_BYTE2, 0);
3480 * Bit 30 signals that the data is valid and hence that HDMI audio can
3484 value &= ~NVIDIA_SCRATCH_VALID;
3486 value |= NVIDIA_SCRATCH_VALID;
3489 * Whenever the trigger bit is toggled, an interrupt is raised in the
3490 * HDMI codec. The HDMI driver will use that as trigger to update its
3493 value ^= NVIDIA_SCRATCH_TRIGGER;
3495 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0,
3496 NVIDIA_SET_SCRATCH0_BYTE3, value);
3499 static int tegra_hdmi_pcm_prepare(struct hda_pcm_stream *hinfo,
3500 struct hda_codec *codec,
3501 unsigned int stream_tag,
3502 unsigned int format,
3503 struct snd_pcm_substream *substream)
3507 err = generic_hdmi_playback_pcm_prepare(hinfo, codec, stream_tag,
3512 /* notify the HDMI codec of the format change */
3513 tegra_hdmi_set_format(codec, format);
3518 static int tegra_hdmi_pcm_cleanup(struct hda_pcm_stream *hinfo,
3519 struct hda_codec *codec,
3520 struct snd_pcm_substream *substream)
3522 /* invalidate the format in the HDMI codec */
3523 tegra_hdmi_set_format(codec, 0);
3525 return generic_hdmi_playback_pcm_cleanup(hinfo, codec, substream);
3528 static struct hda_pcm *hda_find_pcm_by_type(struct hda_codec *codec, int type)
3530 struct hdmi_spec *spec = codec->spec;
3533 for (i = 0; i < spec->num_pins; i++) {
3534 struct hda_pcm *pcm = get_pcm_rec(spec, i);
3536 if (pcm->pcm_type == type)
3543 static int tegra_hdmi_build_pcms(struct hda_codec *codec)
3545 struct hda_pcm_stream *stream;
3546 struct hda_pcm *pcm;
3549 err = generic_hdmi_build_pcms(codec);
3553 pcm = hda_find_pcm_by_type(codec, HDA_PCM_TYPE_HDMI);
3558 * Override ->prepare() and ->cleanup() operations to notify the HDMI
3559 * codec about format changes.
3561 stream = &pcm->stream[SNDRV_PCM_STREAM_PLAYBACK];
3562 stream->ops.prepare = tegra_hdmi_pcm_prepare;
3563 stream->ops.cleanup = tegra_hdmi_pcm_cleanup;
3568 static int patch_tegra_hdmi(struct hda_codec *codec)
3572 err = patch_generic_hdmi(codec);
3576 codec->patch_ops.build_pcms = tegra_hdmi_build_pcms;
3582 * ATI/AMD-specific implementations
3585 #define is_amdhdmi_rev3_or_later(codec) \
3586 ((codec)->core.vendor_id == 0x1002aa01 && \
3587 ((codec)->core.revision_id & 0xff00) >= 0x0300)
3588 #define has_amd_full_remap_support(codec) is_amdhdmi_rev3_or_later(codec)
3590 /* ATI/AMD specific HDA pin verbs, see the AMD HDA Verbs specification */
3591 #define ATI_VERB_SET_CHANNEL_ALLOCATION 0x771
3592 #define ATI_VERB_SET_DOWNMIX_INFO 0x772
3593 #define ATI_VERB_SET_MULTICHANNEL_01 0x777
3594 #define ATI_VERB_SET_MULTICHANNEL_23 0x778
3595 #define ATI_VERB_SET_MULTICHANNEL_45 0x779
3596 #define ATI_VERB_SET_MULTICHANNEL_67 0x77a
3597 #define ATI_VERB_SET_HBR_CONTROL 0x77c
3598 #define ATI_VERB_SET_MULTICHANNEL_1 0x785
3599 #define ATI_VERB_SET_MULTICHANNEL_3 0x786
3600 #define ATI_VERB_SET_MULTICHANNEL_5 0x787
3601 #define ATI_VERB_SET_MULTICHANNEL_7 0x788
3602 #define ATI_VERB_SET_MULTICHANNEL_MODE 0x789
3603 #define ATI_VERB_GET_CHANNEL_ALLOCATION 0xf71
3604 #define ATI_VERB_GET_DOWNMIX_INFO 0xf72
3605 #define ATI_VERB_GET_MULTICHANNEL_01 0xf77
3606 #define ATI_VERB_GET_MULTICHANNEL_23 0xf78
3607 #define ATI_VERB_GET_MULTICHANNEL_45 0xf79
3608 #define ATI_VERB_GET_MULTICHANNEL_67 0xf7a
3609 #define ATI_VERB_GET_HBR_CONTROL 0xf7c
3610 #define ATI_VERB_GET_MULTICHANNEL_1 0xf85
3611 #define ATI_VERB_GET_MULTICHANNEL_3 0xf86
3612 #define ATI_VERB_GET_MULTICHANNEL_5 0xf87
3613 #define ATI_VERB_GET_MULTICHANNEL_7 0xf88
3614 #define ATI_VERB_GET_MULTICHANNEL_MODE 0xf89
3616 /* AMD specific HDA cvt verbs */
3617 #define ATI_VERB_SET_RAMP_RATE 0x770
3618 #define ATI_VERB_GET_RAMP_RATE 0xf70
3620 #define ATI_OUT_ENABLE 0x1
3622 #define ATI_MULTICHANNEL_MODE_PAIRED 0
3623 #define ATI_MULTICHANNEL_MODE_SINGLE 1
3625 #define ATI_HBR_CAPABLE 0x01
3626 #define ATI_HBR_ENABLE 0x10
3628 static int atihdmi_pin_get_eld(struct hda_codec *codec, hda_nid_t nid,
3629 unsigned char *buf, int *eld_size)
3631 /* call hda_eld.c ATI/AMD-specific function */
3632 return snd_hdmi_get_eld_ati(codec, nid, buf, eld_size,
3633 is_amdhdmi_rev3_or_later(codec));
3636 static void atihdmi_pin_setup_infoframe(struct hda_codec *codec, hda_nid_t pin_nid, int ca,
3637 int active_channels, int conn_type)
3639 snd_hda_codec_write(codec, pin_nid, 0, ATI_VERB_SET_CHANNEL_ALLOCATION, ca);
3642 static int atihdmi_paired_swap_fc_lfe(int pos)
3645 * ATI/AMD have automatic FC/LFE swap built-in
3646 * when in pairwise mapping mode.
3650 /* see channel_allocations[].speakers[] */
3659 static int atihdmi_paired_chmap_validate(int ca, int chs, unsigned char *map)
3661 struct cea_channel_speaker_allocation *cap;
3664 /* check that only channel pairs need to be remapped on old pre-rev3 ATI/AMD */
3666 cap = &channel_allocations[get_channel_allocation_order(ca)];
3667 for (i = 0; i < chs; ++i) {
3668 int mask = to_spk_mask(map[i]);
3670 bool companion_ok = false;
3675 for (j = 0 + i % 2; j < 8; j += 2) {
3676 int chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j);
3677 if (cap->speakers[chan_idx] == mask) {
3678 /* channel is in a supported position */
3681 if (i % 2 == 0 && i + 1 < chs) {
3682 /* even channel, check the odd companion */
3683 int comp_chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j + 1);
3684 int comp_mask_req = to_spk_mask(map[i+1]);
3685 int comp_mask_act = cap->speakers[comp_chan_idx];
3687 if (comp_mask_req == comp_mask_act)
3688 companion_ok = true;
3700 i++; /* companion channel already checked */
3706 static int atihdmi_pin_set_slot_channel(struct hda_codec *codec, hda_nid_t pin_nid,
3707 int hdmi_slot, int stream_channel)
3710 int ati_channel_setup = 0;
3715 if (!has_amd_full_remap_support(codec)) {
3716 hdmi_slot = atihdmi_paired_swap_fc_lfe(hdmi_slot);
3718 /* In case this is an odd slot but without stream channel, do not
3719 * disable the slot since the corresponding even slot could have a
3720 * channel. In case neither have a channel, the slot pair will be
3721 * disabled when this function is called for the even slot. */
3722 if (hdmi_slot % 2 != 0 && stream_channel == 0xf)
3725 hdmi_slot -= hdmi_slot % 2;
3727 if (stream_channel != 0xf)
3728 stream_channel -= stream_channel % 2;
3731 verb = ATI_VERB_SET_MULTICHANNEL_01 + hdmi_slot/2 + (hdmi_slot % 2) * 0x00e;
3733 /* ati_channel_setup format: [7..4] = stream_channel_id, [1] = mute, [0] = enable */
3735 if (stream_channel != 0xf)
3736 ati_channel_setup = (stream_channel << 4) | ATI_OUT_ENABLE;
3738 return snd_hda_codec_write(codec, pin_nid, 0, verb, ati_channel_setup);
3741 static int atihdmi_pin_get_slot_channel(struct hda_codec *codec, hda_nid_t pin_nid,
3744 bool was_odd = false;
3745 int ati_asp_slot = asp_slot;
3747 int ati_channel_setup;
3752 if (!has_amd_full_remap_support(codec)) {
3753 ati_asp_slot = atihdmi_paired_swap_fc_lfe(asp_slot);
3754 if (ati_asp_slot % 2 != 0) {
3760 verb = ATI_VERB_GET_MULTICHANNEL_01 + ati_asp_slot/2 + (ati_asp_slot % 2) * 0x00e;
3762 ati_channel_setup = snd_hda_codec_read(codec, pin_nid, 0, verb, 0);
3764 if (!(ati_channel_setup & ATI_OUT_ENABLE))
3767 return ((ati_channel_setup & 0xf0) >> 4) + !!was_odd;
3770 static int atihdmi_paired_chmap_cea_alloc_validate_get_type(struct cea_channel_speaker_allocation *cap,
3776 * Pre-rev3 ATI/AMD codecs operate in a paired channel mode, so
3777 * we need to take that into account (a single channel may take 2
3778 * channel slots if we need to carry a silent channel next to it).
3779 * On Rev3+ AMD codecs this function is not used.
3783 /* We only produce even-numbered channel count TLVs */
3784 if ((channels % 2) != 0)
3787 for (c = 0; c < 7; c += 2) {
3788 if (cap->speakers[c] || cap->speakers[c+1])
3792 if (chanpairs * 2 != channels)
3795 return SNDRV_CTL_TLVT_CHMAP_PAIRED;
3798 static void atihdmi_paired_cea_alloc_to_tlv_chmap(struct cea_channel_speaker_allocation *cap,
3799 unsigned int *chmap, int channels)
3801 /* produce paired maps for pre-rev3 ATI/AMD codecs */
3805 for (c = 7; c >= 0; c--) {
3806 int chan = 7 - atihdmi_paired_swap_fc_lfe(7 - c);
3807 int spk = cap->speakers[chan];
3809 /* add N/A channel if the companion channel is occupied */
3810 if (cap->speakers[chan + (chan % 2 ? -1 : 1)])
3811 chmap[count++] = SNDRV_CHMAP_NA;
3816 chmap[count++] = spk_to_chmap(spk);
3819 WARN_ON(count != channels);
3822 static int atihdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
3825 int hbr_ctl, hbr_ctl_new;
3827 hbr_ctl = snd_hda_codec_read(codec, pin_nid, 0, ATI_VERB_GET_HBR_CONTROL, 0);
3828 if (hbr_ctl >= 0 && (hbr_ctl & ATI_HBR_CAPABLE)) {
3830 hbr_ctl_new = hbr_ctl | ATI_HBR_ENABLE;
3832 hbr_ctl_new = hbr_ctl & ~ATI_HBR_ENABLE;
3835 "atihdmi_pin_hbr_setup: NID=0x%x, %shbr-ctl=0x%x\n",
3837 hbr_ctl == hbr_ctl_new ? "" : "new-",
3840 if (hbr_ctl != hbr_ctl_new)
3841 snd_hda_codec_write(codec, pin_nid, 0,
3842 ATI_VERB_SET_HBR_CONTROL,
3851 static int atihdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
3852 hda_nid_t pin_nid, u32 stream_tag, int format)
3855 if (is_amdhdmi_rev3_or_later(codec)) {
3856 int ramp_rate = 180; /* default as per AMD spec */
3857 /* disable ramp-up/down for non-pcm as per AMD spec */
3858 if (format & AC_FMT_TYPE_NON_PCM)
3861 snd_hda_codec_write(codec, cvt_nid, 0, ATI_VERB_SET_RAMP_RATE, ramp_rate);
3864 return hdmi_setup_stream(codec, cvt_nid, pin_nid, stream_tag, format);
3868 static int atihdmi_init(struct hda_codec *codec)
3870 struct hdmi_spec *spec = codec->spec;
3873 err = generic_hdmi_init(codec);
3878 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
3879 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
3881 /* make sure downmix information in infoframe is zero */
3882 snd_hda_codec_write(codec, per_pin->pin_nid, 0, ATI_VERB_SET_DOWNMIX_INFO, 0);
3884 /* enable channel-wise remap mode if supported */
3885 if (has_amd_full_remap_support(codec))
3886 snd_hda_codec_write(codec, per_pin->pin_nid, 0,
3887 ATI_VERB_SET_MULTICHANNEL_MODE,
3888 ATI_MULTICHANNEL_MODE_SINGLE);
3894 static int patch_atihdmi(struct hda_codec *codec)
3896 struct hdmi_spec *spec;
3897 struct hdmi_spec_per_cvt *per_cvt;
3900 err = patch_generic_hdmi(codec);
3905 codec->patch_ops.init = atihdmi_init;
3909 spec->ops.pin_get_eld = atihdmi_pin_get_eld;
3910 spec->ops.pin_get_slot_channel = atihdmi_pin_get_slot_channel;
3911 spec->ops.pin_set_slot_channel = atihdmi_pin_set_slot_channel;
3912 spec->ops.pin_setup_infoframe = atihdmi_pin_setup_infoframe;
3913 spec->ops.pin_hbr_setup = atihdmi_pin_hbr_setup;
3914 spec->ops.setup_stream = atihdmi_setup_stream;
3916 if (!has_amd_full_remap_support(codec)) {
3917 /* override to ATI/AMD-specific versions with pairwise mapping */
3918 spec->ops.chmap_cea_alloc_validate_get_type =
3919 atihdmi_paired_chmap_cea_alloc_validate_get_type;
3920 spec->ops.cea_alloc_to_tlv_chmap = atihdmi_paired_cea_alloc_to_tlv_chmap;
3921 spec->ops.chmap_validate = atihdmi_paired_chmap_validate;
3924 /* ATI/AMD converters do not advertise all of their capabilities */
3925 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
3926 per_cvt = get_cvt(spec, cvt_idx);
3927 per_cvt->channels_max = max(per_cvt->channels_max, 8u);
3928 per_cvt->rates |= SUPPORTED_RATES;
3929 per_cvt->formats |= SUPPORTED_FORMATS;
3930 per_cvt->maxbps = max(per_cvt->maxbps, 24u);
3933 spec->channels_max = max(spec->channels_max, 8u);
3938 /* VIA HDMI Implementation */
3939 #define VIAHDMI_CVT_NID 0x02 /* audio converter1 */
3940 #define VIAHDMI_PIN_NID 0x03 /* HDMI output pin1 */
3942 static int patch_via_hdmi(struct hda_codec *codec)
3944 return patch_simple_hdmi(codec, VIAHDMI_CVT_NID, VIAHDMI_PIN_NID);
3950 static const struct hda_device_id snd_hda_id_hdmi[] = {
3951 HDA_CODEC_ENTRY(0x1002793c, "RS600 HDMI", patch_atihdmi),
3952 HDA_CODEC_ENTRY(0x10027919, "RS600 HDMI", patch_atihdmi),
3953 HDA_CODEC_ENTRY(0x1002791a, "RS690/780 HDMI", patch_atihdmi),
3954 HDA_CODEC_ENTRY(0x1002aa01, "R6xx HDMI", patch_atihdmi),
3955 HDA_CODEC_ENTRY(0x10951390, "SiI1390 HDMI", patch_generic_hdmi),
3956 HDA_CODEC_ENTRY(0x10951392, "SiI1392 HDMI", patch_generic_hdmi),
3957 HDA_CODEC_ENTRY(0x17e80047, "Chrontel HDMI", patch_generic_hdmi),
3958 HDA_CODEC_ENTRY(0x10de0002, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
3959 HDA_CODEC_ENTRY(0x10de0003, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
3960 HDA_CODEC_ENTRY(0x10de0005, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
3961 HDA_CODEC_ENTRY(0x10de0006, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x),
3962 HDA_CODEC_ENTRY(0x10de0007, "MCP79/7A HDMI", patch_nvhdmi_8ch_7x),
3963 HDA_CODEC_ENTRY(0x10de000a, "GPU 0a HDMI/DP", patch_nvhdmi),
3964 HDA_CODEC_ENTRY(0x10de000b, "GPU 0b HDMI/DP", patch_nvhdmi),
3965 HDA_CODEC_ENTRY(0x10de000c, "MCP89 HDMI", patch_nvhdmi),
3966 HDA_CODEC_ENTRY(0x10de000d, "GPU 0d HDMI/DP", patch_nvhdmi),
3967 HDA_CODEC_ENTRY(0x10de0010, "GPU 10 HDMI/DP", patch_nvhdmi),
3968 HDA_CODEC_ENTRY(0x10de0011, "GPU 11 HDMI/DP", patch_nvhdmi),
3969 HDA_CODEC_ENTRY(0x10de0012, "GPU 12 HDMI/DP", patch_nvhdmi),
3970 HDA_CODEC_ENTRY(0x10de0013, "GPU 13 HDMI/DP", patch_nvhdmi),
3971 HDA_CODEC_ENTRY(0x10de0014, "GPU 14 HDMI/DP", patch_nvhdmi),
3972 HDA_CODEC_ENTRY(0x10de0015, "GPU 15 HDMI/DP", patch_nvhdmi),
3973 HDA_CODEC_ENTRY(0x10de0016, "GPU 16 HDMI/DP", patch_nvhdmi),
3974 /* 17 is known to be absent */
3975 HDA_CODEC_ENTRY(0x10de0018, "GPU 18 HDMI/DP", patch_nvhdmi),
3976 HDA_CODEC_ENTRY(0x10de0019, "GPU 19 HDMI/DP", patch_nvhdmi),
3977 HDA_CODEC_ENTRY(0x10de001a, "GPU 1a HDMI/DP", patch_nvhdmi),
3978 HDA_CODEC_ENTRY(0x10de001b, "GPU 1b HDMI/DP", patch_nvhdmi),
3979 HDA_CODEC_ENTRY(0x10de001c, "GPU 1c HDMI/DP", patch_nvhdmi),
3980 HDA_CODEC_ENTRY(0x10de0020, "Tegra30 HDMI", patch_tegra_hdmi),
3981 HDA_CODEC_ENTRY(0x10de0022, "Tegra114 HDMI", patch_tegra_hdmi),
3982 HDA_CODEC_ENTRY(0x10de0028, "Tegra124 HDMI", patch_tegra_hdmi),
3983 HDA_CODEC_ENTRY(0x10de0029, "Tegra210 HDMI/DP", patch_tegra_hdmi),
3984 HDA_CODEC_ENTRY(0x10de0040, "GPU 40 HDMI/DP", patch_nvhdmi),
3985 HDA_CODEC_ENTRY(0x10de0041, "GPU 41 HDMI/DP", patch_nvhdmi),
3986 HDA_CODEC_ENTRY(0x10de0042, "GPU 42 HDMI/DP", patch_nvhdmi),
3987 HDA_CODEC_ENTRY(0x10de0043, "GPU 43 HDMI/DP", patch_nvhdmi),
3988 HDA_CODEC_ENTRY(0x10de0044, "GPU 44 HDMI/DP", patch_nvhdmi),
3989 HDA_CODEC_ENTRY(0x10de0051, "GPU 51 HDMI/DP", patch_nvhdmi),
3990 HDA_CODEC_ENTRY(0x10de0060, "GPU 60 HDMI/DP", patch_nvhdmi),
3991 HDA_CODEC_ENTRY(0x10de0067, "MCP67 HDMI", patch_nvhdmi_2ch),
3992 HDA_CODEC_ENTRY(0x10de0070, "GPU 70 HDMI/DP", patch_nvhdmi),
3993 HDA_CODEC_ENTRY(0x10de0071, "GPU 71 HDMI/DP", patch_nvhdmi),
3994 HDA_CODEC_ENTRY(0x10de0072, "GPU 72 HDMI/DP", patch_nvhdmi),
3995 HDA_CODEC_ENTRY(0x10de007d, "GPU 7d HDMI/DP", patch_nvhdmi),
3996 HDA_CODEC_ENTRY(0x10de8001, "MCP73 HDMI", patch_nvhdmi_2ch),
3997 HDA_CODEC_ENTRY(0x11069f80, "VX900 HDMI/DP", patch_via_hdmi),
3998 HDA_CODEC_ENTRY(0x11069f81, "VX900 HDMI/DP", patch_via_hdmi),
3999 HDA_CODEC_ENTRY(0x11069f84, "VX11 HDMI/DP", patch_generic_hdmi),
4000 HDA_CODEC_ENTRY(0x11069f85, "VX11 HDMI/DP", patch_generic_hdmi),
4001 HDA_CODEC_ENTRY(0x80860054, "IbexPeak HDMI", patch_generic_hdmi),
4002 HDA_CODEC_ENTRY(0x80862801, "Bearlake HDMI", patch_generic_hdmi),
4003 HDA_CODEC_ENTRY(0x80862802, "Cantiga HDMI", patch_generic_hdmi),
4004 HDA_CODEC_ENTRY(0x80862803, "Eaglelake HDMI", patch_generic_hdmi),
4005 HDA_CODEC_ENTRY(0x80862804, "IbexPeak HDMI", patch_generic_hdmi),
4006 HDA_CODEC_ENTRY(0x80862805, "CougarPoint HDMI", patch_generic_hdmi),
4007 HDA_CODEC_ENTRY(0x80862806, "PantherPoint HDMI", patch_generic_hdmi),
4008 HDA_CODEC_ENTRY(0x80862807, "Haswell HDMI", patch_generic_hdmi),
4009 HDA_CODEC_ENTRY(0x80862808, "Broadwell HDMI", patch_generic_hdmi),
4010 HDA_CODEC_ENTRY(0x80862809, "Skylake HDMI", patch_generic_hdmi),
4011 HDA_CODEC_ENTRY(0x8086280a, "Broxton HDMI", patch_generic_hdmi),
4012 HDA_CODEC_ENTRY(0x8086280b, "Kabylake HDMI", patch_generic_hdmi),
4013 HDA_CODEC_ENTRY(0x80862880, "CedarTrail HDMI", patch_generic_hdmi),
4014 HDA_CODEC_ENTRY(0x80862882, "Valleyview2 HDMI", patch_generic_hdmi),
4015 HDA_CODEC_ENTRY(0x80862883, "Braswell HDMI", patch_generic_hdmi),
4016 HDA_CODEC_ENTRY(0x808629fb, "Crestline HDMI", patch_generic_hdmi),
4017 /* special ID for generic HDMI */
4018 HDA_CODEC_ENTRY(HDA_CODEC_ID_GENERIC_HDMI, "Generic HDMI", patch_generic_hdmi),
4021 MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_hdmi);
4023 MODULE_LICENSE("GPL");
4024 MODULE_DESCRIPTION("HDMI HD-audio codec");
4025 MODULE_ALIAS("snd-hda-codec-intelhdmi");
4026 MODULE_ALIAS("snd-hda-codec-nvhdmi");
4027 MODULE_ALIAS("snd-hda-codec-atihdmi");
4029 static struct hda_codec_driver hdmi_driver = {
4030 .id = snd_hda_id_hdmi,
4033 module_hda_codec_driver(hdmi_driver);