1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * 4 * patch_hdmi.c - routines for HDMI/DisplayPort codecs 5 * 6 * Copyright(c) 2008-2010 Intel Corporation. All rights reserved. 7 * Copyright (c) 2006 ATI Technologies Inc. 8 * Copyright (c) 2008 NVIDIA Corp. All rights reserved. 9 * Copyright (c) 2008 Wei Ni <wni@nvidia.com> 10 * Copyright (c) 2013 Anssi Hannula <anssi.hannula@iki.fi> 11 * 12 * Authors: 13 * Wu Fengguang <wfg@linux.intel.com> 14 * 15 * Maintained by: 16 * Wu Fengguang <wfg@linux.intel.com> 17 */ 18 19 #include <linux/init.h> 20 #include <linux/delay.h> 21 #include <linux/pci.h> 22 #include <linux/slab.h> 23 #include <linux/module.h> 24 #include <linux/pm_runtime.h> 25 #include <sound/core.h> 26 #include <sound/jack.h> 27 #include <sound/asoundef.h> 28 #include <sound/tlv.h> 29 #include <sound/hdaudio.h> 30 #include <sound/hda_i915.h> 31 #include <sound/hda_chmap.h> 32 #include <sound/hda_codec.h> 33 #include "hda_local.h" 34 #include "hda_jack.h" 35 #include "hda_controller.h" 36 37 static bool static_hdmi_pcm; 38 module_param(static_hdmi_pcm, bool, 0644); 39 MODULE_PARM_DESC(static_hdmi_pcm, "Don't restrict PCM parameters per ELD info"); 40 41 static bool enable_acomp = true; 42 module_param(enable_acomp, bool, 0444); 43 MODULE_PARM_DESC(enable_acomp, "Enable audio component binding (default=yes)"); 44 45 static bool enable_silent_stream = 46 IS_ENABLED(CONFIG_SND_HDA_INTEL_HDMI_SILENT_STREAM); 47 module_param(enable_silent_stream, bool, 0644); 48 MODULE_PARM_DESC(enable_silent_stream, "Enable Silent Stream for HDMI devices"); 49 50 struct hdmi_spec_per_cvt { 51 hda_nid_t cvt_nid; 52 int assigned; 53 unsigned int channels_min; 54 unsigned int channels_max; 55 u32 rates; 56 u64 formats; 57 unsigned int maxbps; 58 }; 59 60 /* max. connections to a widget */ 61 #define HDA_MAX_CONNECTIONS 32 62 63 struct hdmi_spec_per_pin { 64 hda_nid_t pin_nid; 65 int dev_id; 66 /* pin idx, different device entries on the same pin use the same idx */ 67 int pin_nid_idx; 68 int num_mux_nids; 69 hda_nid_t mux_nids[HDA_MAX_CONNECTIONS]; 70 int mux_idx; 71 hda_nid_t cvt_nid; 72 73 struct hda_codec *codec; 74 struct hdmi_eld sink_eld; 75 struct mutex lock; 76 struct delayed_work work; 77 struct hdmi_pcm *pcm; /* pointer to spec->pcm_rec[n] dynamically*/ 78 int pcm_idx; /* which pcm is attached. -1 means no pcm is attached */ 79 int repoll_count; 80 bool setup; /* the stream has been set up by prepare callback */ 81 bool silent_stream; 82 int channels; /* current number of channels */ 83 bool non_pcm; 84 bool chmap_set; /* channel-map override by ALSA API? */ 85 unsigned char chmap[8]; /* ALSA API channel-map */ 86 #ifdef CONFIG_SND_PROC_FS 87 struct snd_info_entry *proc_entry; 88 #endif 89 }; 90 91 /* operations used by generic code that can be overridden by patches */ 92 struct hdmi_ops { 93 int (*pin_get_eld)(struct hda_codec *codec, hda_nid_t pin_nid, 94 int dev_id, unsigned char *buf, int *eld_size); 95 96 void (*pin_setup_infoframe)(struct hda_codec *codec, hda_nid_t pin_nid, 97 int dev_id, 98 int ca, int active_channels, int conn_type); 99 100 /* enable/disable HBR (HD passthrough) */ 101 int (*pin_hbr_setup)(struct hda_codec *codec, hda_nid_t pin_nid, 102 int dev_id, bool hbr); 103 104 int (*setup_stream)(struct hda_codec *codec, hda_nid_t cvt_nid, 105 hda_nid_t pin_nid, int dev_id, u32 stream_tag, 106 int format); 107 108 void (*pin_cvt_fixup)(struct hda_codec *codec, 109 struct hdmi_spec_per_pin *per_pin, 110 hda_nid_t cvt_nid); 111 }; 112 113 struct hdmi_pcm { 114 struct hda_pcm *pcm; 115 struct snd_jack *jack; 116 struct snd_kcontrol *eld_ctl; 117 }; 118 119 struct hdmi_spec { 120 struct hda_codec *codec; 121 int num_cvts; 122 struct snd_array cvts; /* struct hdmi_spec_per_cvt */ 123 hda_nid_t cvt_nids[4]; /* only for haswell fix */ 124 125 /* 126 * num_pins is the number of virtual pins 127 * for example, there are 3 pins, and each pin 128 * has 4 device entries, then the num_pins is 12 129 */ 130 int num_pins; 131 /* 132 * num_nids is the number of real pins 133 * In the above example, num_nids is 3 134 */ 135 int num_nids; 136 /* 137 * dev_num is the number of device entries 138 * on each pin. 139 * In the above example, dev_num is 4 140 */ 141 int dev_num; 142 struct snd_array pins; /* struct hdmi_spec_per_pin */ 143 struct hdmi_pcm pcm_rec[16]; 144 struct mutex pcm_lock; 145 struct mutex bind_lock; /* for audio component binding */ 146 /* pcm_bitmap means which pcms have been assigned to pins*/ 147 unsigned long pcm_bitmap; 148 int pcm_used; /* counter of pcm_rec[] */ 149 /* bitmap shows whether the pcm is opened in user space 150 * bit 0 means the first playback PCM (PCM3); 151 * bit 1 means the second playback PCM, and so on. 152 */ 153 unsigned long pcm_in_use; 154 155 struct hdmi_eld temp_eld; 156 struct hdmi_ops ops; 157 158 bool dyn_pin_out; 159 bool dyn_pcm_assign; 160 bool intel_hsw_fixup; /* apply Intel platform-specific fixups */ 161 /* 162 * Non-generic VIA/NVIDIA specific 163 */ 164 struct hda_multi_out multiout; 165 struct hda_pcm_stream pcm_playback; 166 167 bool use_acomp_notifier; /* use eld_notify callback for hotplug */ 168 bool acomp_registered; /* audio component registered in this driver */ 169 bool force_connect; /* force connectivity */ 170 struct drm_audio_component_audio_ops drm_audio_ops; 171 int (*port2pin)(struct hda_codec *, int); /* reverse port/pin mapping */ 172 173 struct hdac_chmap chmap; 174 hda_nid_t vendor_nid; 175 const int *port_map; 176 int port_num; 177 bool send_silent_stream; /* Flag to enable silent stream feature */ 178 }; 179 180 #ifdef CONFIG_SND_HDA_COMPONENT 181 static inline bool codec_has_acomp(struct hda_codec *codec) 182 { 183 struct hdmi_spec *spec = codec->spec; 184 return spec->use_acomp_notifier; 185 } 186 #else 187 #define codec_has_acomp(codec) false 188 #endif 189 190 struct hdmi_audio_infoframe { 191 u8 type; /* 0x84 */ 192 u8 ver; /* 0x01 */ 193 u8 len; /* 0x0a */ 194 195 u8 checksum; 196 197 u8 CC02_CT47; /* CC in bits 0:2, CT in 4:7 */ 198 u8 SS01_SF24; 199 u8 CXT04; 200 u8 CA; 201 u8 LFEPBL01_LSV36_DM_INH7; 202 }; 203 204 struct dp_audio_infoframe { 205 u8 type; /* 0x84 */ 206 u8 len; /* 0x1b */ 207 u8 ver; /* 0x11 << 2 */ 208 209 u8 CC02_CT47; /* match with HDMI infoframe from this on */ 210 u8 SS01_SF24; 211 u8 CXT04; 212 u8 CA; 213 u8 LFEPBL01_LSV36_DM_INH7; 214 }; 215 216 union audio_infoframe { 217 struct hdmi_audio_infoframe hdmi; 218 struct dp_audio_infoframe dp; 219 u8 bytes[0]; 220 }; 221 222 /* 223 * HDMI routines 224 */ 225 226 #define get_pin(spec, idx) \ 227 ((struct hdmi_spec_per_pin *)snd_array_elem(&spec->pins, idx)) 228 #define get_cvt(spec, idx) \ 229 ((struct hdmi_spec_per_cvt *)snd_array_elem(&spec->cvts, idx)) 230 /* obtain hdmi_pcm object assigned to idx */ 231 #define get_hdmi_pcm(spec, idx) (&(spec)->pcm_rec[idx]) 232 /* obtain hda_pcm object assigned to idx */ 233 #define get_pcm_rec(spec, idx) (get_hdmi_pcm(spec, idx)->pcm) 234 235 static int pin_id_to_pin_index(struct hda_codec *codec, 236 hda_nid_t pin_nid, int dev_id) 237 { 238 struct hdmi_spec *spec = codec->spec; 239 int pin_idx; 240 struct hdmi_spec_per_pin *per_pin; 241 242 /* 243 * (dev_id == -1) means it is NON-MST pin 244 * return the first virtual pin on this port 245 */ 246 if (dev_id == -1) 247 dev_id = 0; 248 249 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) { 250 per_pin = get_pin(spec, pin_idx); 251 if ((per_pin->pin_nid == pin_nid) && 252 (per_pin->dev_id == dev_id)) 253 return pin_idx; 254 } 255 256 codec_warn(codec, "HDMI: pin NID 0x%x not registered\n", pin_nid); 257 return -EINVAL; 258 } 259 260 static int hinfo_to_pcm_index(struct hda_codec *codec, 261 struct hda_pcm_stream *hinfo) 262 { 263 struct hdmi_spec *spec = codec->spec; 264 int pcm_idx; 265 266 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) 267 if (get_pcm_rec(spec, pcm_idx)->stream == hinfo) 268 return pcm_idx; 269 270 codec_warn(codec, "HDMI: hinfo %p not tied to a PCM\n", hinfo); 271 return -EINVAL; 272 } 273 274 static int hinfo_to_pin_index(struct hda_codec *codec, 275 struct hda_pcm_stream *hinfo) 276 { 277 struct hdmi_spec *spec = codec->spec; 278 struct hdmi_spec_per_pin *per_pin; 279 int pin_idx; 280 281 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) { 282 per_pin = get_pin(spec, pin_idx); 283 if (per_pin->pcm && 284 per_pin->pcm->pcm->stream == hinfo) 285 return pin_idx; 286 } 287 288 codec_dbg(codec, "HDMI: hinfo %p (pcm %d) not registered\n", hinfo, 289 hinfo_to_pcm_index(codec, hinfo)); 290 return -EINVAL; 291 } 292 293 static struct hdmi_spec_per_pin *pcm_idx_to_pin(struct hdmi_spec *spec, 294 int pcm_idx) 295 { 296 int i; 297 struct hdmi_spec_per_pin *per_pin; 298 299 for (i = 0; i < spec->num_pins; i++) { 300 per_pin = get_pin(spec, i); 301 if (per_pin->pcm_idx == pcm_idx) 302 return per_pin; 303 } 304 return NULL; 305 } 306 307 static int cvt_nid_to_cvt_index(struct hda_codec *codec, hda_nid_t cvt_nid) 308 { 309 struct hdmi_spec *spec = codec->spec; 310 int cvt_idx; 311 312 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) 313 if (get_cvt(spec, cvt_idx)->cvt_nid == cvt_nid) 314 return cvt_idx; 315 316 codec_warn(codec, "HDMI: cvt NID 0x%x not registered\n", cvt_nid); 317 return -EINVAL; 318 } 319 320 static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol, 321 struct snd_ctl_elem_info *uinfo) 322 { 323 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 324 struct hdmi_spec *spec = codec->spec; 325 struct hdmi_spec_per_pin *per_pin; 326 struct hdmi_eld *eld; 327 int pcm_idx; 328 329 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES; 330 331 pcm_idx = kcontrol->private_value; 332 mutex_lock(&spec->pcm_lock); 333 per_pin = pcm_idx_to_pin(spec, pcm_idx); 334 if (!per_pin) { 335 /* no pin is bound to the pcm */ 336 uinfo->count = 0; 337 goto unlock; 338 } 339 eld = &per_pin->sink_eld; 340 uinfo->count = eld->eld_valid ? eld->eld_size : 0; 341 342 unlock: 343 mutex_unlock(&spec->pcm_lock); 344 return 0; 345 } 346 347 static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol, 348 struct snd_ctl_elem_value *ucontrol) 349 { 350 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 351 struct hdmi_spec *spec = codec->spec; 352 struct hdmi_spec_per_pin *per_pin; 353 struct hdmi_eld *eld; 354 int pcm_idx; 355 int err = 0; 356 357 pcm_idx = kcontrol->private_value; 358 mutex_lock(&spec->pcm_lock); 359 per_pin = pcm_idx_to_pin(spec, pcm_idx); 360 if (!per_pin) { 361 /* no pin is bound to the pcm */ 362 memset(ucontrol->value.bytes.data, 0, 363 ARRAY_SIZE(ucontrol->value.bytes.data)); 364 goto unlock; 365 } 366 367 eld = &per_pin->sink_eld; 368 if (eld->eld_size > ARRAY_SIZE(ucontrol->value.bytes.data) || 369 eld->eld_size > ELD_MAX_SIZE) { 370 snd_BUG(); 371 err = -EINVAL; 372 goto unlock; 373 } 374 375 memset(ucontrol->value.bytes.data, 0, 376 ARRAY_SIZE(ucontrol->value.bytes.data)); 377 if (eld->eld_valid) 378 memcpy(ucontrol->value.bytes.data, eld->eld_buffer, 379 eld->eld_size); 380 381 unlock: 382 mutex_unlock(&spec->pcm_lock); 383 return err; 384 } 385 386 static const struct snd_kcontrol_new eld_bytes_ctl = { 387 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE | 388 SNDRV_CTL_ELEM_ACCESS_SKIP_CHECK, 389 .iface = SNDRV_CTL_ELEM_IFACE_PCM, 390 .name = "ELD", 391 .info = hdmi_eld_ctl_info, 392 .get = hdmi_eld_ctl_get, 393 }; 394 395 static int hdmi_create_eld_ctl(struct hda_codec *codec, int pcm_idx, 396 int device) 397 { 398 struct snd_kcontrol *kctl; 399 struct hdmi_spec *spec = codec->spec; 400 int err; 401 402 kctl = snd_ctl_new1(&eld_bytes_ctl, codec); 403 if (!kctl) 404 return -ENOMEM; 405 kctl->private_value = pcm_idx; 406 kctl->id.device = device; 407 408 /* no pin nid is associated with the kctl now 409 * tbd: associate pin nid to eld ctl later 410 */ 411 err = snd_hda_ctl_add(codec, 0, kctl); 412 if (err < 0) 413 return err; 414 415 get_hdmi_pcm(spec, pcm_idx)->eld_ctl = kctl; 416 return 0; 417 } 418 419 #ifdef BE_PARANOID 420 static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t pin_nid, 421 int *packet_index, int *byte_index) 422 { 423 int val; 424 425 val = snd_hda_codec_read(codec, pin_nid, 0, 426 AC_VERB_GET_HDMI_DIP_INDEX, 0); 427 428 *packet_index = val >> 5; 429 *byte_index = val & 0x1f; 430 } 431 #endif 432 433 static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t pin_nid, 434 int packet_index, int byte_index) 435 { 436 int val; 437 438 val = (packet_index << 5) | (byte_index & 0x1f); 439 440 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val); 441 } 442 443 static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t pin_nid, 444 unsigned char val) 445 { 446 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val); 447 } 448 449 static void hdmi_init_pin(struct hda_codec *codec, hda_nid_t pin_nid) 450 { 451 struct hdmi_spec *spec = codec->spec; 452 int pin_out; 453 454 /* Unmute */ 455 if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP) 456 snd_hda_codec_write(codec, pin_nid, 0, 457 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE); 458 459 if (spec->dyn_pin_out) 460 /* Disable pin out until stream is active */ 461 pin_out = 0; 462 else 463 /* Enable pin out: some machines with GM965 gets broken output 464 * when the pin is disabled or changed while using with HDMI 465 */ 466 pin_out = PIN_OUT; 467 468 snd_hda_codec_write(codec, pin_nid, 0, 469 AC_VERB_SET_PIN_WIDGET_CONTROL, pin_out); 470 } 471 472 /* 473 * ELD proc files 474 */ 475 476 #ifdef CONFIG_SND_PROC_FS 477 static void print_eld_info(struct snd_info_entry *entry, 478 struct snd_info_buffer *buffer) 479 { 480 struct hdmi_spec_per_pin *per_pin = entry->private_data; 481 482 mutex_lock(&per_pin->lock); 483 snd_hdmi_print_eld_info(&per_pin->sink_eld, buffer); 484 mutex_unlock(&per_pin->lock); 485 } 486 487 static void write_eld_info(struct snd_info_entry *entry, 488 struct snd_info_buffer *buffer) 489 { 490 struct hdmi_spec_per_pin *per_pin = entry->private_data; 491 492 mutex_lock(&per_pin->lock); 493 snd_hdmi_write_eld_info(&per_pin->sink_eld, buffer); 494 mutex_unlock(&per_pin->lock); 495 } 496 497 static int eld_proc_new(struct hdmi_spec_per_pin *per_pin, int index) 498 { 499 char name[32]; 500 struct hda_codec *codec = per_pin->codec; 501 struct snd_info_entry *entry; 502 int err; 503 504 snprintf(name, sizeof(name), "eld#%d.%d", codec->addr, index); 505 err = snd_card_proc_new(codec->card, name, &entry); 506 if (err < 0) 507 return err; 508 509 snd_info_set_text_ops(entry, per_pin, print_eld_info); 510 entry->c.text.write = write_eld_info; 511 entry->mode |= 0200; 512 per_pin->proc_entry = entry; 513 514 return 0; 515 } 516 517 static void eld_proc_free(struct hdmi_spec_per_pin *per_pin) 518 { 519 if (!per_pin->codec->bus->shutdown) { 520 snd_info_free_entry(per_pin->proc_entry); 521 per_pin->proc_entry = NULL; 522 } 523 } 524 #else 525 static inline int eld_proc_new(struct hdmi_spec_per_pin *per_pin, 526 int index) 527 { 528 return 0; 529 } 530 static inline void eld_proc_free(struct hdmi_spec_per_pin *per_pin) 531 { 532 } 533 #endif 534 535 /* 536 * Audio InfoFrame routines 537 */ 538 539 /* 540 * Enable Audio InfoFrame Transmission 541 */ 542 static void hdmi_start_infoframe_trans(struct hda_codec *codec, 543 hda_nid_t pin_nid) 544 { 545 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0); 546 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT, 547 AC_DIPXMIT_BEST); 548 } 549 550 /* 551 * Disable Audio InfoFrame Transmission 552 */ 553 static void hdmi_stop_infoframe_trans(struct hda_codec *codec, 554 hda_nid_t pin_nid) 555 { 556 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0); 557 snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT, 558 AC_DIPXMIT_DISABLE); 559 } 560 561 static void hdmi_debug_dip_size(struct hda_codec *codec, hda_nid_t pin_nid) 562 { 563 #ifdef CONFIG_SND_DEBUG_VERBOSE 564 int i; 565 int size; 566 567 size = snd_hdmi_get_eld_size(codec, pin_nid); 568 codec_dbg(codec, "HDMI: ELD buf size is %d\n", size); 569 570 for (i = 0; i < 8; i++) { 571 size = snd_hda_codec_read(codec, pin_nid, 0, 572 AC_VERB_GET_HDMI_DIP_SIZE, i); 573 codec_dbg(codec, "HDMI: DIP GP[%d] buf size is %d\n", i, size); 574 } 575 #endif 576 } 577 578 static void hdmi_clear_dip_buffers(struct hda_codec *codec, hda_nid_t pin_nid) 579 { 580 #ifdef BE_PARANOID 581 int i, j; 582 int size; 583 int pi, bi; 584 for (i = 0; i < 8; i++) { 585 size = snd_hda_codec_read(codec, pin_nid, 0, 586 AC_VERB_GET_HDMI_DIP_SIZE, i); 587 if (size == 0) 588 continue; 589 590 hdmi_set_dip_index(codec, pin_nid, i, 0x0); 591 for (j = 1; j < 1000; j++) { 592 hdmi_write_dip_byte(codec, pin_nid, 0x0); 593 hdmi_get_dip_index(codec, pin_nid, &pi, &bi); 594 if (pi != i) 595 codec_dbg(codec, "dip index %d: %d != %d\n", 596 bi, pi, i); 597 if (bi == 0) /* byte index wrapped around */ 598 break; 599 } 600 codec_dbg(codec, 601 "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n", 602 i, size, j); 603 } 604 #endif 605 } 606 607 static void hdmi_checksum_audio_infoframe(struct hdmi_audio_infoframe *hdmi_ai) 608 { 609 u8 *bytes = (u8 *)hdmi_ai; 610 u8 sum = 0; 611 int i; 612 613 hdmi_ai->checksum = 0; 614 615 for (i = 0; i < sizeof(*hdmi_ai); i++) 616 sum += bytes[i]; 617 618 hdmi_ai->checksum = -sum; 619 } 620 621 static void hdmi_fill_audio_infoframe(struct hda_codec *codec, 622 hda_nid_t pin_nid, 623 u8 *dip, int size) 624 { 625 int i; 626 627 hdmi_debug_dip_size(codec, pin_nid); 628 hdmi_clear_dip_buffers(codec, pin_nid); /* be paranoid */ 629 630 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0); 631 for (i = 0; i < size; i++) 632 hdmi_write_dip_byte(codec, pin_nid, dip[i]); 633 } 634 635 static bool hdmi_infoframe_uptodate(struct hda_codec *codec, hda_nid_t pin_nid, 636 u8 *dip, int size) 637 { 638 u8 val; 639 int i; 640 641 hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0); 642 if (snd_hda_codec_read(codec, pin_nid, 0, AC_VERB_GET_HDMI_DIP_XMIT, 0) 643 != AC_DIPXMIT_BEST) 644 return false; 645 646 for (i = 0; i < size; i++) { 647 val = snd_hda_codec_read(codec, pin_nid, 0, 648 AC_VERB_GET_HDMI_DIP_DATA, 0); 649 if (val != dip[i]) 650 return false; 651 } 652 653 return true; 654 } 655 656 static int hdmi_pin_get_eld(struct hda_codec *codec, hda_nid_t nid, 657 int dev_id, unsigned char *buf, int *eld_size) 658 { 659 snd_hda_set_dev_select(codec, nid, dev_id); 660 661 return snd_hdmi_get_eld(codec, nid, buf, eld_size); 662 } 663 664 static void hdmi_pin_setup_infoframe(struct hda_codec *codec, 665 hda_nid_t pin_nid, int dev_id, 666 int ca, int active_channels, 667 int conn_type) 668 { 669 union audio_infoframe ai; 670 671 memset(&ai, 0, sizeof(ai)); 672 if (conn_type == 0) { /* HDMI */ 673 struct hdmi_audio_infoframe *hdmi_ai = &ai.hdmi; 674 675 hdmi_ai->type = 0x84; 676 hdmi_ai->ver = 0x01; 677 hdmi_ai->len = 0x0a; 678 hdmi_ai->CC02_CT47 = active_channels - 1; 679 hdmi_ai->CA = ca; 680 hdmi_checksum_audio_infoframe(hdmi_ai); 681 } else if (conn_type == 1) { /* DisplayPort */ 682 struct dp_audio_infoframe *dp_ai = &ai.dp; 683 684 dp_ai->type = 0x84; 685 dp_ai->len = 0x1b; 686 dp_ai->ver = 0x11 << 2; 687 dp_ai->CC02_CT47 = active_channels - 1; 688 dp_ai->CA = ca; 689 } else { 690 codec_dbg(codec, "HDMI: unknown connection type at pin NID 0x%x\n", pin_nid); 691 return; 692 } 693 694 snd_hda_set_dev_select(codec, pin_nid, dev_id); 695 696 /* 697 * sizeof(ai) is used instead of sizeof(*hdmi_ai) or 698 * sizeof(*dp_ai) to avoid partial match/update problems when 699 * the user switches between HDMI/DP monitors. 700 */ 701 if (!hdmi_infoframe_uptodate(codec, pin_nid, ai.bytes, 702 sizeof(ai))) { 703 codec_dbg(codec, "%s: pin NID=0x%x channels=%d ca=0x%02x\n", 704 __func__, pin_nid, active_channels, ca); 705 hdmi_stop_infoframe_trans(codec, pin_nid); 706 hdmi_fill_audio_infoframe(codec, pin_nid, 707 ai.bytes, sizeof(ai)); 708 hdmi_start_infoframe_trans(codec, pin_nid); 709 } 710 } 711 712 static void hdmi_setup_audio_infoframe(struct hda_codec *codec, 713 struct hdmi_spec_per_pin *per_pin, 714 bool non_pcm) 715 { 716 struct hdmi_spec *spec = codec->spec; 717 struct hdac_chmap *chmap = &spec->chmap; 718 hda_nid_t pin_nid = per_pin->pin_nid; 719 int dev_id = per_pin->dev_id; 720 int channels = per_pin->channels; 721 int active_channels; 722 struct hdmi_eld *eld; 723 int ca; 724 725 if (!channels) 726 return; 727 728 snd_hda_set_dev_select(codec, pin_nid, dev_id); 729 730 /* some HW (e.g. HSW+) needs reprogramming the amp at each time */ 731 if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP) 732 snd_hda_codec_write(codec, pin_nid, 0, 733 AC_VERB_SET_AMP_GAIN_MUTE, 734 AMP_OUT_UNMUTE); 735 736 eld = &per_pin->sink_eld; 737 738 ca = snd_hdac_channel_allocation(&codec->core, 739 eld->info.spk_alloc, channels, 740 per_pin->chmap_set, non_pcm, per_pin->chmap); 741 742 active_channels = snd_hdac_get_active_channels(ca); 743 744 chmap->ops.set_channel_count(&codec->core, per_pin->cvt_nid, 745 active_channels); 746 747 /* 748 * always configure channel mapping, it may have been changed by the 749 * user in the meantime 750 */ 751 snd_hdac_setup_channel_mapping(&spec->chmap, 752 pin_nid, non_pcm, ca, channels, 753 per_pin->chmap, per_pin->chmap_set); 754 755 spec->ops.pin_setup_infoframe(codec, pin_nid, dev_id, 756 ca, active_channels, eld->info.conn_type); 757 758 per_pin->non_pcm = non_pcm; 759 } 760 761 /* 762 * Unsolicited events 763 */ 764 765 static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll); 766 767 static void check_presence_and_report(struct hda_codec *codec, hda_nid_t nid, 768 int dev_id) 769 { 770 struct hdmi_spec *spec = codec->spec; 771 int pin_idx = pin_id_to_pin_index(codec, nid, dev_id); 772 773 if (pin_idx < 0) 774 return; 775 mutex_lock(&spec->pcm_lock); 776 hdmi_present_sense(get_pin(spec, pin_idx), 1); 777 mutex_unlock(&spec->pcm_lock); 778 } 779 780 static void jack_callback(struct hda_codec *codec, 781 struct hda_jack_callback *jack) 782 { 783 /* stop polling when notification is enabled */ 784 if (codec_has_acomp(codec)) 785 return; 786 787 check_presence_and_report(codec, jack->nid, jack->dev_id); 788 } 789 790 static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res, 791 struct hda_jack_tbl *jack) 792 { 793 jack->jack_dirty = 1; 794 795 codec_dbg(codec, 796 "HDMI hot plug event: Codec=%d NID=0x%x Device=%d Inactive=%d Presence_Detect=%d ELD_Valid=%d\n", 797 codec->addr, jack->nid, jack->dev_id, !!(res & AC_UNSOL_RES_IA), 798 !!(res & AC_UNSOL_RES_PD), !!(res & AC_UNSOL_RES_ELDV)); 799 800 check_presence_and_report(codec, jack->nid, jack->dev_id); 801 } 802 803 static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res) 804 { 805 int tag = res >> AC_UNSOL_RES_TAG_SHIFT; 806 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT; 807 int cp_state = !!(res & AC_UNSOL_RES_CP_STATE); 808 int cp_ready = !!(res & AC_UNSOL_RES_CP_READY); 809 810 codec_info(codec, 811 "HDMI CP event: CODEC=%d TAG=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n", 812 codec->addr, 813 tag, 814 subtag, 815 cp_state, 816 cp_ready); 817 818 /* TODO */ 819 if (cp_state) { 820 ; 821 } 822 if (cp_ready) { 823 ; 824 } 825 } 826 827 828 static void hdmi_unsol_event(struct hda_codec *codec, unsigned int res) 829 { 830 int tag = res >> AC_UNSOL_RES_TAG_SHIFT; 831 int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT; 832 struct hda_jack_tbl *jack; 833 834 if (codec_has_acomp(codec)) 835 return; 836 837 if (codec->dp_mst) { 838 int dev_entry = 839 (res & AC_UNSOL_RES_DE) >> AC_UNSOL_RES_DE_SHIFT; 840 841 jack = snd_hda_jack_tbl_get_from_tag(codec, tag, dev_entry); 842 } else { 843 jack = snd_hda_jack_tbl_get_from_tag(codec, tag, 0); 844 } 845 846 if (!jack) { 847 codec_dbg(codec, "Unexpected HDMI event tag 0x%x\n", tag); 848 return; 849 } 850 851 if (subtag == 0) 852 hdmi_intrinsic_event(codec, res, jack); 853 else 854 hdmi_non_intrinsic_event(codec, res); 855 } 856 857 static void haswell_verify_D0(struct hda_codec *codec, 858 hda_nid_t cvt_nid, hda_nid_t nid) 859 { 860 int pwr; 861 862 /* For Haswell, the converter 1/2 may keep in D3 state after bootup, 863 * thus pins could only choose converter 0 for use. Make sure the 864 * converters are in correct power state */ 865 if (!snd_hda_check_power_state(codec, cvt_nid, AC_PWRST_D0)) 866 snd_hda_codec_write(codec, cvt_nid, 0, AC_VERB_SET_POWER_STATE, AC_PWRST_D0); 867 868 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0)) { 869 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE, 870 AC_PWRST_D0); 871 msleep(40); 872 pwr = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0); 873 pwr = (pwr & AC_PWRST_ACTUAL) >> AC_PWRST_ACTUAL_SHIFT; 874 codec_dbg(codec, "Haswell HDMI audio: Power for NID 0x%x is now D%d\n", nid, pwr); 875 } 876 } 877 878 /* 879 * Callbacks 880 */ 881 882 /* HBR should be Non-PCM, 8 channels */ 883 #define is_hbr_format(format) \ 884 ((format & AC_FMT_TYPE_NON_PCM) && (format & AC_FMT_CHAN_MASK) == 7) 885 886 static int hdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid, 887 int dev_id, bool hbr) 888 { 889 int pinctl, new_pinctl; 890 891 if (snd_hda_query_pin_caps(codec, pin_nid) & AC_PINCAP_HBR) { 892 snd_hda_set_dev_select(codec, pin_nid, dev_id); 893 pinctl = snd_hda_codec_read(codec, pin_nid, 0, 894 AC_VERB_GET_PIN_WIDGET_CONTROL, 0); 895 896 if (pinctl < 0) 897 return hbr ? -EINVAL : 0; 898 899 new_pinctl = pinctl & ~AC_PINCTL_EPT; 900 if (hbr) 901 new_pinctl |= AC_PINCTL_EPT_HBR; 902 else 903 new_pinctl |= AC_PINCTL_EPT_NATIVE; 904 905 codec_dbg(codec, 906 "hdmi_pin_hbr_setup: NID=0x%x, %spinctl=0x%x\n", 907 pin_nid, 908 pinctl == new_pinctl ? "" : "new-", 909 new_pinctl); 910 911 if (pinctl != new_pinctl) 912 snd_hda_codec_write(codec, pin_nid, 0, 913 AC_VERB_SET_PIN_WIDGET_CONTROL, 914 new_pinctl); 915 } else if (hbr) 916 return -EINVAL; 917 918 return 0; 919 } 920 921 static int hdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid, 922 hda_nid_t pin_nid, int dev_id, 923 u32 stream_tag, int format) 924 { 925 struct hdmi_spec *spec = codec->spec; 926 unsigned int param; 927 int err; 928 929 err = spec->ops.pin_hbr_setup(codec, pin_nid, dev_id, 930 is_hbr_format(format)); 931 932 if (err) { 933 codec_dbg(codec, "hdmi_setup_stream: HBR is not supported\n"); 934 return err; 935 } 936 937 if (spec->intel_hsw_fixup) { 938 939 /* 940 * on recent platforms IEC Coding Type is required for HBR 941 * support, read current Digital Converter settings and set 942 * ICT bitfield if needed. 943 */ 944 param = snd_hda_codec_read(codec, cvt_nid, 0, 945 AC_VERB_GET_DIGI_CONVERT_1, 0); 946 947 param = (param >> 16) & ~(AC_DIG3_ICT); 948 949 /* on recent platforms ICT mode is required for HBR support */ 950 if (is_hbr_format(format)) 951 param |= 0x1; 952 953 snd_hda_codec_write(codec, cvt_nid, 0, 954 AC_VERB_SET_DIGI_CONVERT_3, param); 955 } 956 957 snd_hda_codec_setup_stream(codec, cvt_nid, stream_tag, 0, format); 958 return 0; 959 } 960 961 /* Try to find an available converter 962 * If pin_idx is less then zero, just try to find an available converter. 963 * Otherwise, try to find an available converter and get the cvt mux index 964 * of the pin. 965 */ 966 static int hdmi_choose_cvt(struct hda_codec *codec, 967 int pin_idx, int *cvt_id) 968 { 969 struct hdmi_spec *spec = codec->spec; 970 struct hdmi_spec_per_pin *per_pin; 971 struct hdmi_spec_per_cvt *per_cvt = NULL; 972 int cvt_idx, mux_idx = 0; 973 974 /* pin_idx < 0 means no pin will be bound to the converter */ 975 if (pin_idx < 0) 976 per_pin = NULL; 977 else 978 per_pin = get_pin(spec, pin_idx); 979 980 if (per_pin && per_pin->silent_stream) { 981 cvt_idx = cvt_nid_to_cvt_index(codec, per_pin->cvt_nid); 982 if (cvt_id) 983 *cvt_id = cvt_idx; 984 return 0; 985 } 986 987 /* Dynamically assign converter to stream */ 988 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) { 989 per_cvt = get_cvt(spec, cvt_idx); 990 991 /* Must not already be assigned */ 992 if (per_cvt->assigned) 993 continue; 994 if (per_pin == NULL) 995 break; 996 /* Must be in pin's mux's list of converters */ 997 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++) 998 if (per_pin->mux_nids[mux_idx] == per_cvt->cvt_nid) 999 break; 1000 /* Not in mux list */ 1001 if (mux_idx == per_pin->num_mux_nids) 1002 continue; 1003 break; 1004 } 1005 1006 /* No free converters */ 1007 if (cvt_idx == spec->num_cvts) 1008 return -EBUSY; 1009 1010 if (per_pin != NULL) 1011 per_pin->mux_idx = mux_idx; 1012 1013 if (cvt_id) 1014 *cvt_id = cvt_idx; 1015 1016 return 0; 1017 } 1018 1019 /* Assure the pin select the right convetor */ 1020 static void intel_verify_pin_cvt_connect(struct hda_codec *codec, 1021 struct hdmi_spec_per_pin *per_pin) 1022 { 1023 hda_nid_t pin_nid = per_pin->pin_nid; 1024 int mux_idx, curr; 1025 1026 mux_idx = per_pin->mux_idx; 1027 curr = snd_hda_codec_read(codec, pin_nid, 0, 1028 AC_VERB_GET_CONNECT_SEL, 0); 1029 if (curr != mux_idx) 1030 snd_hda_codec_write_cache(codec, pin_nid, 0, 1031 AC_VERB_SET_CONNECT_SEL, 1032 mux_idx); 1033 } 1034 1035 /* get the mux index for the converter of the pins 1036 * converter's mux index is the same for all pins on Intel platform 1037 */ 1038 static int intel_cvt_id_to_mux_idx(struct hdmi_spec *spec, 1039 hda_nid_t cvt_nid) 1040 { 1041 int i; 1042 1043 for (i = 0; i < spec->num_cvts; i++) 1044 if (spec->cvt_nids[i] == cvt_nid) 1045 return i; 1046 return -EINVAL; 1047 } 1048 1049 /* Intel HDMI workaround to fix audio routing issue: 1050 * For some Intel display codecs, pins share the same connection list. 1051 * So a conveter can be selected by multiple pins and playback on any of these 1052 * pins will generate sound on the external display, because audio flows from 1053 * the same converter to the display pipeline. Also muting one pin may make 1054 * other pins have no sound output. 1055 * So this function assures that an assigned converter for a pin is not selected 1056 * by any other pins. 1057 */ 1058 static void intel_not_share_assigned_cvt(struct hda_codec *codec, 1059 hda_nid_t pin_nid, 1060 int dev_id, int mux_idx) 1061 { 1062 struct hdmi_spec *spec = codec->spec; 1063 hda_nid_t nid; 1064 int cvt_idx, curr; 1065 struct hdmi_spec_per_cvt *per_cvt; 1066 struct hdmi_spec_per_pin *per_pin; 1067 int pin_idx; 1068 1069 /* configure the pins connections */ 1070 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) { 1071 int dev_id_saved; 1072 int dev_num; 1073 1074 per_pin = get_pin(spec, pin_idx); 1075 /* 1076 * pin not connected to monitor 1077 * no need to operate on it 1078 */ 1079 if (!per_pin->pcm) 1080 continue; 1081 1082 if ((per_pin->pin_nid == pin_nid) && 1083 (per_pin->dev_id == dev_id)) 1084 continue; 1085 1086 /* 1087 * if per_pin->dev_id >= dev_num, 1088 * snd_hda_get_dev_select() will fail, 1089 * and the following operation is unpredictable. 1090 * So skip this situation. 1091 */ 1092 dev_num = snd_hda_get_num_devices(codec, per_pin->pin_nid) + 1; 1093 if (per_pin->dev_id >= dev_num) 1094 continue; 1095 1096 nid = per_pin->pin_nid; 1097 1098 /* 1099 * Calling this function should not impact 1100 * on the device entry selection 1101 * So let's save the dev id for each pin, 1102 * and restore it when return 1103 */ 1104 dev_id_saved = snd_hda_get_dev_select(codec, nid); 1105 snd_hda_set_dev_select(codec, nid, per_pin->dev_id); 1106 curr = snd_hda_codec_read(codec, nid, 0, 1107 AC_VERB_GET_CONNECT_SEL, 0); 1108 if (curr != mux_idx) { 1109 snd_hda_set_dev_select(codec, nid, dev_id_saved); 1110 continue; 1111 } 1112 1113 1114 /* choose an unassigned converter. The conveters in the 1115 * connection list are in the same order as in the codec. 1116 */ 1117 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) { 1118 per_cvt = get_cvt(spec, cvt_idx); 1119 if (!per_cvt->assigned) { 1120 codec_dbg(codec, 1121 "choose cvt %d for pin NID 0x%x\n", 1122 cvt_idx, nid); 1123 snd_hda_codec_write_cache(codec, nid, 0, 1124 AC_VERB_SET_CONNECT_SEL, 1125 cvt_idx); 1126 break; 1127 } 1128 } 1129 snd_hda_set_dev_select(codec, nid, dev_id_saved); 1130 } 1131 } 1132 1133 /* A wrapper of intel_not_share_asigned_cvt() */ 1134 static void intel_not_share_assigned_cvt_nid(struct hda_codec *codec, 1135 hda_nid_t pin_nid, int dev_id, hda_nid_t cvt_nid) 1136 { 1137 int mux_idx; 1138 struct hdmi_spec *spec = codec->spec; 1139 1140 /* On Intel platform, the mapping of converter nid to 1141 * mux index of the pins are always the same. 1142 * The pin nid may be 0, this means all pins will not 1143 * share the converter. 1144 */ 1145 mux_idx = intel_cvt_id_to_mux_idx(spec, cvt_nid); 1146 if (mux_idx >= 0) 1147 intel_not_share_assigned_cvt(codec, pin_nid, dev_id, mux_idx); 1148 } 1149 1150 /* skeleton caller of pin_cvt_fixup ops */ 1151 static void pin_cvt_fixup(struct hda_codec *codec, 1152 struct hdmi_spec_per_pin *per_pin, 1153 hda_nid_t cvt_nid) 1154 { 1155 struct hdmi_spec *spec = codec->spec; 1156 1157 if (spec->ops.pin_cvt_fixup) 1158 spec->ops.pin_cvt_fixup(codec, per_pin, cvt_nid); 1159 } 1160 1161 /* called in hdmi_pcm_open when no pin is assigned to the PCM 1162 * in dyn_pcm_assign mode. 1163 */ 1164 static int hdmi_pcm_open_no_pin(struct hda_pcm_stream *hinfo, 1165 struct hda_codec *codec, 1166 struct snd_pcm_substream *substream) 1167 { 1168 struct hdmi_spec *spec = codec->spec; 1169 struct snd_pcm_runtime *runtime = substream->runtime; 1170 int cvt_idx, pcm_idx; 1171 struct hdmi_spec_per_cvt *per_cvt = NULL; 1172 int err; 1173 1174 pcm_idx = hinfo_to_pcm_index(codec, hinfo); 1175 if (pcm_idx < 0) 1176 return -EINVAL; 1177 1178 err = hdmi_choose_cvt(codec, -1, &cvt_idx); 1179 if (err) 1180 return err; 1181 1182 per_cvt = get_cvt(spec, cvt_idx); 1183 per_cvt->assigned = 1; 1184 hinfo->nid = per_cvt->cvt_nid; 1185 1186 pin_cvt_fixup(codec, NULL, per_cvt->cvt_nid); 1187 1188 set_bit(pcm_idx, &spec->pcm_in_use); 1189 /* todo: setup spdif ctls assign */ 1190 1191 /* Initially set the converter's capabilities */ 1192 hinfo->channels_min = per_cvt->channels_min; 1193 hinfo->channels_max = per_cvt->channels_max; 1194 hinfo->rates = per_cvt->rates; 1195 hinfo->formats = per_cvt->formats; 1196 hinfo->maxbps = per_cvt->maxbps; 1197 1198 /* Store the updated parameters */ 1199 runtime->hw.channels_min = hinfo->channels_min; 1200 runtime->hw.channels_max = hinfo->channels_max; 1201 runtime->hw.formats = hinfo->formats; 1202 runtime->hw.rates = hinfo->rates; 1203 1204 snd_pcm_hw_constraint_step(substream->runtime, 0, 1205 SNDRV_PCM_HW_PARAM_CHANNELS, 2); 1206 return 0; 1207 } 1208 1209 /* 1210 * HDA PCM callbacks 1211 */ 1212 static int hdmi_pcm_open(struct hda_pcm_stream *hinfo, 1213 struct hda_codec *codec, 1214 struct snd_pcm_substream *substream) 1215 { 1216 struct hdmi_spec *spec = codec->spec; 1217 struct snd_pcm_runtime *runtime = substream->runtime; 1218 int pin_idx, cvt_idx, pcm_idx; 1219 struct hdmi_spec_per_pin *per_pin; 1220 struct hdmi_eld *eld; 1221 struct hdmi_spec_per_cvt *per_cvt = NULL; 1222 int err; 1223 1224 /* Validate hinfo */ 1225 pcm_idx = hinfo_to_pcm_index(codec, hinfo); 1226 if (pcm_idx < 0) 1227 return -EINVAL; 1228 1229 mutex_lock(&spec->pcm_lock); 1230 pin_idx = hinfo_to_pin_index(codec, hinfo); 1231 if (!spec->dyn_pcm_assign) { 1232 if (snd_BUG_ON(pin_idx < 0)) { 1233 err = -EINVAL; 1234 goto unlock; 1235 } 1236 } else { 1237 /* no pin is assigned to the PCM 1238 * PA need pcm open successfully when probe 1239 */ 1240 if (pin_idx < 0) { 1241 err = hdmi_pcm_open_no_pin(hinfo, codec, substream); 1242 goto unlock; 1243 } 1244 } 1245 1246 err = hdmi_choose_cvt(codec, pin_idx, &cvt_idx); 1247 if (err < 0) 1248 goto unlock; 1249 1250 per_cvt = get_cvt(spec, cvt_idx); 1251 /* Claim converter */ 1252 per_cvt->assigned = 1; 1253 1254 set_bit(pcm_idx, &spec->pcm_in_use); 1255 per_pin = get_pin(spec, pin_idx); 1256 per_pin->cvt_nid = per_cvt->cvt_nid; 1257 hinfo->nid = per_cvt->cvt_nid; 1258 1259 /* flip stripe flag for the assigned stream if supported */ 1260 if (get_wcaps(codec, per_cvt->cvt_nid) & AC_WCAP_STRIPE) 1261 azx_stream(get_azx_dev(substream))->stripe = 1; 1262 1263 snd_hda_set_dev_select(codec, per_pin->pin_nid, per_pin->dev_id); 1264 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0, 1265 AC_VERB_SET_CONNECT_SEL, 1266 per_pin->mux_idx); 1267 1268 /* configure unused pins to choose other converters */ 1269 pin_cvt_fixup(codec, per_pin, 0); 1270 1271 snd_hda_spdif_ctls_assign(codec, pcm_idx, per_cvt->cvt_nid); 1272 1273 /* Initially set the converter's capabilities */ 1274 hinfo->channels_min = per_cvt->channels_min; 1275 hinfo->channels_max = per_cvt->channels_max; 1276 hinfo->rates = per_cvt->rates; 1277 hinfo->formats = per_cvt->formats; 1278 hinfo->maxbps = per_cvt->maxbps; 1279 1280 eld = &per_pin->sink_eld; 1281 /* Restrict capabilities by ELD if this isn't disabled */ 1282 if (!static_hdmi_pcm && eld->eld_valid) { 1283 snd_hdmi_eld_update_pcm_info(&eld->info, hinfo); 1284 if (hinfo->channels_min > hinfo->channels_max || 1285 !hinfo->rates || !hinfo->formats) { 1286 per_cvt->assigned = 0; 1287 hinfo->nid = 0; 1288 snd_hda_spdif_ctls_unassign(codec, pcm_idx); 1289 err = -ENODEV; 1290 goto unlock; 1291 } 1292 } 1293 1294 /* Store the updated parameters */ 1295 runtime->hw.channels_min = hinfo->channels_min; 1296 runtime->hw.channels_max = hinfo->channels_max; 1297 runtime->hw.formats = hinfo->formats; 1298 runtime->hw.rates = hinfo->rates; 1299 1300 snd_pcm_hw_constraint_step(substream->runtime, 0, 1301 SNDRV_PCM_HW_PARAM_CHANNELS, 2); 1302 unlock: 1303 mutex_unlock(&spec->pcm_lock); 1304 return err; 1305 } 1306 1307 /* 1308 * HDA/HDMI auto parsing 1309 */ 1310 static int hdmi_read_pin_conn(struct hda_codec *codec, int pin_idx) 1311 { 1312 struct hdmi_spec *spec = codec->spec; 1313 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx); 1314 hda_nid_t pin_nid = per_pin->pin_nid; 1315 int dev_id = per_pin->dev_id; 1316 int conns; 1317 1318 if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) { 1319 codec_warn(codec, 1320 "HDMI: pin NID 0x%x wcaps %#x does not support connection list\n", 1321 pin_nid, get_wcaps(codec, pin_nid)); 1322 return -EINVAL; 1323 } 1324 1325 snd_hda_set_dev_select(codec, pin_nid, dev_id); 1326 1327 if (spec->intel_hsw_fixup) { 1328 conns = spec->num_cvts; 1329 memcpy(per_pin->mux_nids, spec->cvt_nids, 1330 sizeof(hda_nid_t) * conns); 1331 } else { 1332 conns = snd_hda_get_raw_connections(codec, pin_nid, 1333 per_pin->mux_nids, 1334 HDA_MAX_CONNECTIONS); 1335 } 1336 1337 /* all the device entries on the same pin have the same conn list */ 1338 per_pin->num_mux_nids = conns; 1339 1340 return 0; 1341 } 1342 1343 static int hdmi_find_pcm_slot(struct hdmi_spec *spec, 1344 struct hdmi_spec_per_pin *per_pin) 1345 { 1346 int i; 1347 1348 /* 1349 * generic_hdmi_build_pcms() may allocate extra PCMs on some 1350 * platforms (with maximum of 'num_nids + dev_num - 1') 1351 * 1352 * The per_pin of pin_nid_idx=n and dev_id=m prefers to get pcm-n 1353 * if m==0. This guarantees that dynamic pcm assignments are compatible 1354 * with the legacy static per_pin-pcm assignment that existed in the 1355 * days before DP-MST. 1356 * 1357 * Intel DP-MST prefers this legacy behavior for compatibility, too. 1358 * 1359 * per_pin of m!=0 prefers to get pcm=(num_nids + (m - 1)). 1360 */ 1361 1362 if (per_pin->dev_id == 0 || spec->intel_hsw_fixup) { 1363 if (!test_bit(per_pin->pin_nid_idx, &spec->pcm_bitmap)) 1364 return per_pin->pin_nid_idx; 1365 } else { 1366 i = spec->num_nids + (per_pin->dev_id - 1); 1367 if (i < spec->pcm_used && !(test_bit(i, &spec->pcm_bitmap))) 1368 return i; 1369 } 1370 1371 /* have a second try; check the area over num_nids */ 1372 for (i = spec->num_nids; i < spec->pcm_used; i++) { 1373 if (!test_bit(i, &spec->pcm_bitmap)) 1374 return i; 1375 } 1376 1377 /* the last try; check the empty slots in pins */ 1378 for (i = 0; i < spec->num_nids; i++) { 1379 if (!test_bit(i, &spec->pcm_bitmap)) 1380 return i; 1381 } 1382 return -EBUSY; 1383 } 1384 1385 static void hdmi_attach_hda_pcm(struct hdmi_spec *spec, 1386 struct hdmi_spec_per_pin *per_pin) 1387 { 1388 int idx; 1389 1390 /* pcm already be attached to the pin */ 1391 if (per_pin->pcm) 1392 return; 1393 idx = hdmi_find_pcm_slot(spec, per_pin); 1394 if (idx == -EBUSY) 1395 return; 1396 per_pin->pcm_idx = idx; 1397 per_pin->pcm = get_hdmi_pcm(spec, idx); 1398 set_bit(idx, &spec->pcm_bitmap); 1399 } 1400 1401 static void hdmi_detach_hda_pcm(struct hdmi_spec *spec, 1402 struct hdmi_spec_per_pin *per_pin) 1403 { 1404 int idx; 1405 1406 /* pcm already be detached from the pin */ 1407 if (!per_pin->pcm) 1408 return; 1409 idx = per_pin->pcm_idx; 1410 per_pin->pcm_idx = -1; 1411 per_pin->pcm = NULL; 1412 if (idx >= 0 && idx < spec->pcm_used) 1413 clear_bit(idx, &spec->pcm_bitmap); 1414 } 1415 1416 static int hdmi_get_pin_cvt_mux(struct hdmi_spec *spec, 1417 struct hdmi_spec_per_pin *per_pin, hda_nid_t cvt_nid) 1418 { 1419 int mux_idx; 1420 1421 for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++) 1422 if (per_pin->mux_nids[mux_idx] == cvt_nid) 1423 break; 1424 return mux_idx; 1425 } 1426 1427 static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid); 1428 1429 static void hdmi_pcm_setup_pin(struct hdmi_spec *spec, 1430 struct hdmi_spec_per_pin *per_pin) 1431 { 1432 struct hda_codec *codec = per_pin->codec; 1433 struct hda_pcm *pcm; 1434 struct hda_pcm_stream *hinfo; 1435 struct snd_pcm_substream *substream; 1436 int mux_idx; 1437 bool non_pcm; 1438 1439 if (per_pin->pcm_idx >= 0 && per_pin->pcm_idx < spec->pcm_used) 1440 pcm = get_pcm_rec(spec, per_pin->pcm_idx); 1441 else 1442 return; 1443 if (!pcm->pcm) 1444 return; 1445 if (!test_bit(per_pin->pcm_idx, &spec->pcm_in_use)) 1446 return; 1447 1448 /* hdmi audio only uses playback and one substream */ 1449 hinfo = pcm->stream; 1450 substream = pcm->pcm->streams[0].substream; 1451 1452 per_pin->cvt_nid = hinfo->nid; 1453 1454 mux_idx = hdmi_get_pin_cvt_mux(spec, per_pin, hinfo->nid); 1455 if (mux_idx < per_pin->num_mux_nids) { 1456 snd_hda_set_dev_select(codec, per_pin->pin_nid, 1457 per_pin->dev_id); 1458 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0, 1459 AC_VERB_SET_CONNECT_SEL, 1460 mux_idx); 1461 } 1462 snd_hda_spdif_ctls_assign(codec, per_pin->pcm_idx, hinfo->nid); 1463 1464 non_pcm = check_non_pcm_per_cvt(codec, hinfo->nid); 1465 if (substream->runtime) 1466 per_pin->channels = substream->runtime->channels; 1467 per_pin->setup = true; 1468 per_pin->mux_idx = mux_idx; 1469 1470 hdmi_setup_audio_infoframe(codec, per_pin, non_pcm); 1471 } 1472 1473 static void hdmi_pcm_reset_pin(struct hdmi_spec *spec, 1474 struct hdmi_spec_per_pin *per_pin) 1475 { 1476 if (per_pin->pcm_idx >= 0 && per_pin->pcm_idx < spec->pcm_used) 1477 snd_hda_spdif_ctls_unassign(per_pin->codec, per_pin->pcm_idx); 1478 1479 per_pin->chmap_set = false; 1480 memset(per_pin->chmap, 0, sizeof(per_pin->chmap)); 1481 1482 per_pin->setup = false; 1483 per_pin->channels = 0; 1484 } 1485 1486 static struct snd_jack *pin_idx_to_pcm_jack(struct hda_codec *codec, 1487 struct hdmi_spec_per_pin *per_pin) 1488 { 1489 struct hdmi_spec *spec = codec->spec; 1490 1491 if (per_pin->pcm_idx >= 0) 1492 return spec->pcm_rec[per_pin->pcm_idx].jack; 1493 else 1494 return NULL; 1495 } 1496 1497 /* update per_pin ELD from the given new ELD; 1498 * setup info frame and notification accordingly 1499 * also notify ELD kctl and report jack status changes 1500 */ 1501 static void update_eld(struct hda_codec *codec, 1502 struct hdmi_spec_per_pin *per_pin, 1503 struct hdmi_eld *eld, 1504 int repoll) 1505 { 1506 struct hdmi_eld *pin_eld = &per_pin->sink_eld; 1507 struct hdmi_spec *spec = codec->spec; 1508 struct snd_jack *pcm_jack; 1509 bool old_eld_valid = pin_eld->eld_valid; 1510 bool eld_changed; 1511 int pcm_idx; 1512 1513 if (eld->eld_valid) { 1514 if (eld->eld_size <= 0 || 1515 snd_hdmi_parse_eld(codec, &eld->info, eld->eld_buffer, 1516 eld->eld_size) < 0) { 1517 eld->eld_valid = false; 1518 if (repoll) { 1519 schedule_delayed_work(&per_pin->work, 1520 msecs_to_jiffies(300)); 1521 return; 1522 } 1523 } 1524 } 1525 1526 if (!eld->eld_valid || eld->eld_size <= 0) { 1527 eld->eld_valid = false; 1528 eld->eld_size = 0; 1529 } 1530 1531 /* for monitor disconnection, save pcm_idx firstly */ 1532 pcm_idx = per_pin->pcm_idx; 1533 1534 /* 1535 * pcm_idx >=0 before update_eld() means it is in monitor 1536 * disconnected event. Jack must be fetched before update_eld(). 1537 */ 1538 pcm_jack = pin_idx_to_pcm_jack(codec, per_pin); 1539 1540 if (spec->dyn_pcm_assign) { 1541 if (eld->eld_valid) { 1542 hdmi_attach_hda_pcm(spec, per_pin); 1543 hdmi_pcm_setup_pin(spec, per_pin); 1544 } else { 1545 hdmi_pcm_reset_pin(spec, per_pin); 1546 hdmi_detach_hda_pcm(spec, per_pin); 1547 } 1548 } 1549 /* if pcm_idx == -1, it means this is in monitor connection event 1550 * we can get the correct pcm_idx now. 1551 */ 1552 if (pcm_idx == -1) 1553 pcm_idx = per_pin->pcm_idx; 1554 if (!pcm_jack) 1555 pcm_jack = pin_idx_to_pcm_jack(codec, per_pin); 1556 1557 if (eld->eld_valid) 1558 snd_hdmi_show_eld(codec, &eld->info); 1559 1560 eld_changed = (pin_eld->eld_valid != eld->eld_valid); 1561 eld_changed |= (pin_eld->monitor_present != eld->monitor_present); 1562 if (!eld_changed && eld->eld_valid && pin_eld->eld_valid) 1563 if (pin_eld->eld_size != eld->eld_size || 1564 memcmp(pin_eld->eld_buffer, eld->eld_buffer, 1565 eld->eld_size) != 0) 1566 eld_changed = true; 1567 1568 if (eld_changed) { 1569 pin_eld->monitor_present = eld->monitor_present; 1570 pin_eld->eld_valid = eld->eld_valid; 1571 pin_eld->eld_size = eld->eld_size; 1572 if (eld->eld_valid) 1573 memcpy(pin_eld->eld_buffer, eld->eld_buffer, 1574 eld->eld_size); 1575 pin_eld->info = eld->info; 1576 } 1577 1578 /* 1579 * Re-setup pin and infoframe. This is needed e.g. when 1580 * - sink is first plugged-in 1581 * - transcoder can change during stream playback on Haswell 1582 * and this can make HW reset converter selection on a pin. 1583 */ 1584 if (eld->eld_valid && !old_eld_valid && per_pin->setup) { 1585 pin_cvt_fixup(codec, per_pin, 0); 1586 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm); 1587 } 1588 1589 if (eld_changed && pcm_idx >= 0) 1590 snd_ctl_notify(codec->card, 1591 SNDRV_CTL_EVENT_MASK_VALUE | 1592 SNDRV_CTL_EVENT_MASK_INFO, 1593 &get_hdmi_pcm(spec, pcm_idx)->eld_ctl->id); 1594 1595 if (eld_changed && pcm_jack) 1596 snd_jack_report(pcm_jack, 1597 (eld->monitor_present && eld->eld_valid) ? 1598 SND_JACK_AVOUT : 0); 1599 } 1600 1601 /* update ELD and jack state via HD-audio verbs */ 1602 static void hdmi_present_sense_via_verbs(struct hdmi_spec_per_pin *per_pin, 1603 int repoll) 1604 { 1605 struct hda_codec *codec = per_pin->codec; 1606 struct hdmi_spec *spec = codec->spec; 1607 struct hdmi_eld *eld = &spec->temp_eld; 1608 hda_nid_t pin_nid = per_pin->pin_nid; 1609 int dev_id = per_pin->dev_id; 1610 /* 1611 * Always execute a GetPinSense verb here, even when called from 1612 * hdmi_intrinsic_event; for some NVIDIA HW, the unsolicited 1613 * response's PD bit is not the real PD value, but indicates that 1614 * the real PD value changed. An older version of the HD-audio 1615 * specification worked this way. Hence, we just ignore the data in 1616 * the unsolicited response to avoid custom WARs. 1617 */ 1618 int present; 1619 int ret; 1620 1621 ret = snd_hda_power_up_pm(codec); 1622 if (ret < 0 && pm_runtime_suspended(hda_codec_dev(codec))) 1623 goto out; 1624 1625 present = snd_hda_jack_pin_sense(codec, pin_nid, dev_id); 1626 1627 mutex_lock(&per_pin->lock); 1628 eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE); 1629 if (eld->monitor_present) 1630 eld->eld_valid = !!(present & AC_PINSENSE_ELDV); 1631 else 1632 eld->eld_valid = false; 1633 1634 codec_dbg(codec, 1635 "HDMI status: Codec=%d NID=0x%x Presence_Detect=%d ELD_Valid=%d\n", 1636 codec->addr, pin_nid, eld->monitor_present, eld->eld_valid); 1637 1638 if (eld->eld_valid) { 1639 if (spec->ops.pin_get_eld(codec, pin_nid, dev_id, 1640 eld->eld_buffer, &eld->eld_size) < 0) 1641 eld->eld_valid = false; 1642 } 1643 1644 update_eld(codec, per_pin, eld, repoll); 1645 mutex_unlock(&per_pin->lock); 1646 out: 1647 snd_hda_power_down_pm(codec); 1648 } 1649 1650 #define I915_SILENT_RATE 48000 1651 #define I915_SILENT_CHANNELS 2 1652 #define I915_SILENT_FORMAT SNDRV_PCM_FORMAT_S16_LE 1653 #define I915_SILENT_FORMAT_BITS 16 1654 #define I915_SILENT_FMT_MASK 0xf 1655 1656 static void silent_stream_enable(struct hda_codec *codec, 1657 struct hdmi_spec_per_pin *per_pin) 1658 { 1659 struct hdmi_spec *spec = codec->spec; 1660 struct hdmi_spec_per_cvt *per_cvt; 1661 int cvt_idx, pin_idx, err; 1662 unsigned int format; 1663 1664 mutex_lock(&per_pin->lock); 1665 1666 if (per_pin->setup) { 1667 codec_dbg(codec, "hdmi: PCM already open, no silent stream\n"); 1668 goto unlock_out; 1669 } 1670 1671 pin_idx = pin_id_to_pin_index(codec, per_pin->pin_nid, per_pin->dev_id); 1672 err = hdmi_choose_cvt(codec, pin_idx, &cvt_idx); 1673 if (err) { 1674 codec_err(codec, "hdmi: no free converter to enable silent mode\n"); 1675 goto unlock_out; 1676 } 1677 1678 per_cvt = get_cvt(spec, cvt_idx); 1679 per_cvt->assigned = 1; 1680 per_pin->cvt_nid = per_cvt->cvt_nid; 1681 per_pin->silent_stream = true; 1682 1683 codec_dbg(codec, "hdmi: enabling silent stream pin-NID=0x%x cvt-NID=0x%x\n", 1684 per_pin->pin_nid, per_cvt->cvt_nid); 1685 1686 snd_hda_set_dev_select(codec, per_pin->pin_nid, per_pin->dev_id); 1687 snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0, 1688 AC_VERB_SET_CONNECT_SEL, 1689 per_pin->mux_idx); 1690 1691 /* configure unused pins to choose other converters */ 1692 pin_cvt_fixup(codec, per_pin, 0); 1693 1694 snd_hdac_sync_audio_rate(&codec->core, per_pin->pin_nid, 1695 per_pin->dev_id, I915_SILENT_RATE); 1696 1697 /* trigger silent stream generation in hw */ 1698 format = snd_hdac_calc_stream_format(I915_SILENT_RATE, I915_SILENT_CHANNELS, 1699 I915_SILENT_FORMAT, I915_SILENT_FORMAT_BITS, 0); 1700 snd_hda_codec_setup_stream(codec, per_pin->cvt_nid, 1701 I915_SILENT_FMT_MASK, I915_SILENT_FMT_MASK, format); 1702 usleep_range(100, 200); 1703 snd_hda_codec_setup_stream(codec, per_pin->cvt_nid, I915_SILENT_FMT_MASK, 0, format); 1704 1705 per_pin->channels = I915_SILENT_CHANNELS; 1706 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm); 1707 1708 unlock_out: 1709 mutex_unlock(&per_pin->lock); 1710 } 1711 1712 static void silent_stream_disable(struct hda_codec *codec, 1713 struct hdmi_spec_per_pin *per_pin) 1714 { 1715 struct hdmi_spec *spec = codec->spec; 1716 struct hdmi_spec_per_cvt *per_cvt; 1717 int cvt_idx; 1718 1719 mutex_lock(&per_pin->lock); 1720 if (!per_pin->silent_stream) 1721 goto unlock_out; 1722 1723 codec_dbg(codec, "HDMI: disable silent stream on pin-NID=0x%x cvt-NID=0x%x\n", 1724 per_pin->pin_nid, per_pin->cvt_nid); 1725 1726 cvt_idx = cvt_nid_to_cvt_index(codec, per_pin->cvt_nid); 1727 if (cvt_idx >= 0 && cvt_idx < spec->num_cvts) { 1728 per_cvt = get_cvt(spec, cvt_idx); 1729 per_cvt->assigned = 0; 1730 } 1731 1732 per_pin->cvt_nid = 0; 1733 per_pin->silent_stream = false; 1734 1735 unlock_out: 1736 mutex_unlock(&per_pin->lock); 1737 } 1738 1739 /* update ELD and jack state via audio component */ 1740 static void sync_eld_via_acomp(struct hda_codec *codec, 1741 struct hdmi_spec_per_pin *per_pin) 1742 { 1743 struct hdmi_spec *spec = codec->spec; 1744 struct hdmi_eld *eld = &spec->temp_eld; 1745 bool monitor_prev, monitor_next; 1746 1747 mutex_lock(&per_pin->lock); 1748 eld->monitor_present = false; 1749 monitor_prev = per_pin->sink_eld.monitor_present; 1750 eld->eld_size = snd_hdac_acomp_get_eld(&codec->core, per_pin->pin_nid, 1751 per_pin->dev_id, &eld->monitor_present, 1752 eld->eld_buffer, ELD_MAX_SIZE); 1753 eld->eld_valid = (eld->eld_size > 0); 1754 update_eld(codec, per_pin, eld, 0); 1755 monitor_next = per_pin->sink_eld.monitor_present; 1756 mutex_unlock(&per_pin->lock); 1757 1758 /* 1759 * Power-up will call hdmi_present_sense, so the PM calls 1760 * have to be done without mutex held. 1761 */ 1762 1763 if (spec->send_silent_stream) { 1764 int pm_ret; 1765 1766 if (!monitor_prev && monitor_next) { 1767 pm_ret = snd_hda_power_up_pm(codec); 1768 if (pm_ret < 0) 1769 codec_err(codec, 1770 "Monitor plugged-in, Failed to power up codec ret=[%d]\n", 1771 pm_ret); 1772 silent_stream_enable(codec, per_pin); 1773 } else if (monitor_prev && !monitor_next) { 1774 silent_stream_disable(codec, per_pin); 1775 pm_ret = snd_hda_power_down_pm(codec); 1776 if (pm_ret < 0) 1777 codec_err(codec, 1778 "Monitor plugged-out, Failed to power down codec ret=[%d]\n", 1779 pm_ret); 1780 } 1781 } 1782 } 1783 1784 static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll) 1785 { 1786 struct hda_codec *codec = per_pin->codec; 1787 1788 if (!codec_has_acomp(codec)) 1789 hdmi_present_sense_via_verbs(per_pin, repoll); 1790 else 1791 sync_eld_via_acomp(codec, per_pin); 1792 } 1793 1794 static void hdmi_repoll_eld(struct work_struct *work) 1795 { 1796 struct hdmi_spec_per_pin *per_pin = 1797 container_of(to_delayed_work(work), struct hdmi_spec_per_pin, work); 1798 struct hda_codec *codec = per_pin->codec; 1799 struct hdmi_spec *spec = codec->spec; 1800 struct hda_jack_tbl *jack; 1801 1802 jack = snd_hda_jack_tbl_get_mst(codec, per_pin->pin_nid, 1803 per_pin->dev_id); 1804 if (jack) 1805 jack->jack_dirty = 1; 1806 1807 if (per_pin->repoll_count++ > 6) 1808 per_pin->repoll_count = 0; 1809 1810 mutex_lock(&spec->pcm_lock); 1811 hdmi_present_sense(per_pin, per_pin->repoll_count); 1812 mutex_unlock(&spec->pcm_lock); 1813 } 1814 1815 static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid) 1816 { 1817 struct hdmi_spec *spec = codec->spec; 1818 unsigned int caps, config; 1819 int pin_idx; 1820 struct hdmi_spec_per_pin *per_pin; 1821 int err; 1822 int dev_num, i; 1823 1824 caps = snd_hda_query_pin_caps(codec, pin_nid); 1825 if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP))) 1826 return 0; 1827 1828 /* 1829 * For DP MST audio, Configuration Default is the same for 1830 * all device entries on the same pin 1831 */ 1832 config = snd_hda_codec_get_pincfg(codec, pin_nid); 1833 if (get_defcfg_connect(config) == AC_JACK_PORT_NONE && 1834 !spec->force_connect) 1835 return 0; 1836 1837 /* 1838 * To simplify the implementation, malloc all 1839 * the virtual pins in the initialization statically 1840 */ 1841 if (spec->intel_hsw_fixup) { 1842 /* 1843 * On Intel platforms, device entries number is 1844 * changed dynamically. If there is a DP MST 1845 * hub connected, the device entries number is 3. 1846 * Otherwise, it is 1. 1847 * Here we manually set dev_num to 3, so that 1848 * we can initialize all the device entries when 1849 * bootup statically. 1850 */ 1851 dev_num = 3; 1852 spec->dev_num = 3; 1853 } else if (spec->dyn_pcm_assign && codec->dp_mst) { 1854 dev_num = snd_hda_get_num_devices(codec, pin_nid) + 1; 1855 /* 1856 * spec->dev_num is the maxinum number of device entries 1857 * among all the pins 1858 */ 1859 spec->dev_num = (spec->dev_num > dev_num) ? 1860 spec->dev_num : dev_num; 1861 } else { 1862 /* 1863 * If the platform doesn't support DP MST, 1864 * manually set dev_num to 1. This means 1865 * the pin has only one device entry. 1866 */ 1867 dev_num = 1; 1868 spec->dev_num = 1; 1869 } 1870 1871 for (i = 0; i < dev_num; i++) { 1872 pin_idx = spec->num_pins; 1873 per_pin = snd_array_new(&spec->pins); 1874 1875 if (!per_pin) 1876 return -ENOMEM; 1877 1878 if (spec->dyn_pcm_assign) { 1879 per_pin->pcm = NULL; 1880 per_pin->pcm_idx = -1; 1881 } else { 1882 per_pin->pcm = get_hdmi_pcm(spec, pin_idx); 1883 per_pin->pcm_idx = pin_idx; 1884 } 1885 per_pin->pin_nid = pin_nid; 1886 per_pin->pin_nid_idx = spec->num_nids; 1887 per_pin->dev_id = i; 1888 per_pin->non_pcm = false; 1889 snd_hda_set_dev_select(codec, pin_nid, i); 1890 err = hdmi_read_pin_conn(codec, pin_idx); 1891 if (err < 0) 1892 return err; 1893 spec->num_pins++; 1894 } 1895 spec->num_nids++; 1896 1897 return 0; 1898 } 1899 1900 static int hdmi_add_cvt(struct hda_codec *codec, hda_nid_t cvt_nid) 1901 { 1902 struct hdmi_spec *spec = codec->spec; 1903 struct hdmi_spec_per_cvt *per_cvt; 1904 unsigned int chans; 1905 int err; 1906 1907 chans = get_wcaps(codec, cvt_nid); 1908 chans = get_wcaps_channels(chans); 1909 1910 per_cvt = snd_array_new(&spec->cvts); 1911 if (!per_cvt) 1912 return -ENOMEM; 1913 1914 per_cvt->cvt_nid = cvt_nid; 1915 per_cvt->channels_min = 2; 1916 if (chans <= 16) { 1917 per_cvt->channels_max = chans; 1918 if (chans > spec->chmap.channels_max) 1919 spec->chmap.channels_max = chans; 1920 } 1921 1922 err = snd_hda_query_supported_pcm(codec, cvt_nid, 1923 &per_cvt->rates, 1924 &per_cvt->formats, 1925 &per_cvt->maxbps); 1926 if (err < 0) 1927 return err; 1928 1929 if (spec->num_cvts < ARRAY_SIZE(spec->cvt_nids)) 1930 spec->cvt_nids[spec->num_cvts] = cvt_nid; 1931 spec->num_cvts++; 1932 1933 return 0; 1934 } 1935 1936 static const struct snd_pci_quirk force_connect_list[] = { 1937 SND_PCI_QUIRK(0x103c, 0x870f, "HP", 1), 1938 SND_PCI_QUIRK(0x103c, 0x871a, "HP", 1), 1939 {} 1940 }; 1941 1942 static int hdmi_parse_codec(struct hda_codec *codec) 1943 { 1944 struct hdmi_spec *spec = codec->spec; 1945 hda_nid_t start_nid; 1946 unsigned int caps; 1947 int i, nodes; 1948 const struct snd_pci_quirk *q; 1949 1950 nodes = snd_hda_get_sub_nodes(codec, codec->core.afg, &start_nid); 1951 if (!start_nid || nodes < 0) { 1952 codec_warn(codec, "HDMI: failed to get afg sub nodes\n"); 1953 return -EINVAL; 1954 } 1955 1956 q = snd_pci_quirk_lookup(codec->bus->pci, force_connect_list); 1957 1958 if (q && q->value) 1959 spec->force_connect = true; 1960 1961 /* 1962 * hdmi_add_pin() assumes total amount of converters to 1963 * be known, so first discover all converters 1964 */ 1965 for (i = 0; i < nodes; i++) { 1966 hda_nid_t nid = start_nid + i; 1967 1968 caps = get_wcaps(codec, nid); 1969 1970 if (!(caps & AC_WCAP_DIGITAL)) 1971 continue; 1972 1973 if (get_wcaps_type(caps) == AC_WID_AUD_OUT) 1974 hdmi_add_cvt(codec, nid); 1975 } 1976 1977 /* discover audio pins */ 1978 for (i = 0; i < nodes; i++) { 1979 hda_nid_t nid = start_nid + i; 1980 1981 caps = get_wcaps(codec, nid); 1982 1983 if (!(caps & AC_WCAP_DIGITAL)) 1984 continue; 1985 1986 if (get_wcaps_type(caps) == AC_WID_PIN) 1987 hdmi_add_pin(codec, nid); 1988 } 1989 1990 return 0; 1991 } 1992 1993 /* 1994 */ 1995 static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid) 1996 { 1997 struct hda_spdif_out *spdif; 1998 bool non_pcm; 1999 2000 mutex_lock(&codec->spdif_mutex); 2001 spdif = snd_hda_spdif_out_of_nid(codec, cvt_nid); 2002 /* Add sanity check to pass klockwork check. 2003 * This should never happen. 2004 */ 2005 if (WARN_ON(spdif == NULL)) { 2006 mutex_unlock(&codec->spdif_mutex); 2007 return true; 2008 } 2009 non_pcm = !!(spdif->status & IEC958_AES0_NONAUDIO); 2010 mutex_unlock(&codec->spdif_mutex); 2011 return non_pcm; 2012 } 2013 2014 /* 2015 * HDMI callbacks 2016 */ 2017 2018 static int generic_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo, 2019 struct hda_codec *codec, 2020 unsigned int stream_tag, 2021 unsigned int format, 2022 struct snd_pcm_substream *substream) 2023 { 2024 hda_nid_t cvt_nid = hinfo->nid; 2025 struct hdmi_spec *spec = codec->spec; 2026 int pin_idx; 2027 struct hdmi_spec_per_pin *per_pin; 2028 struct snd_pcm_runtime *runtime = substream->runtime; 2029 bool non_pcm; 2030 int pinctl, stripe; 2031 int err = 0; 2032 2033 mutex_lock(&spec->pcm_lock); 2034 pin_idx = hinfo_to_pin_index(codec, hinfo); 2035 if (spec->dyn_pcm_assign && pin_idx < 0) { 2036 /* when dyn_pcm_assign and pcm is not bound to a pin 2037 * skip pin setup and return 0 to make audio playback 2038 * be ongoing 2039 */ 2040 pin_cvt_fixup(codec, NULL, cvt_nid); 2041 snd_hda_codec_setup_stream(codec, cvt_nid, 2042 stream_tag, 0, format); 2043 goto unlock; 2044 } 2045 2046 if (snd_BUG_ON(pin_idx < 0)) { 2047 err = -EINVAL; 2048 goto unlock; 2049 } 2050 per_pin = get_pin(spec, pin_idx); 2051 2052 /* Verify pin:cvt selections to avoid silent audio after S3. 2053 * After S3, the audio driver restores pin:cvt selections 2054 * but this can happen before gfx is ready and such selection 2055 * is overlooked by HW. Thus multiple pins can share a same 2056 * default convertor and mute control will affect each other, 2057 * which can cause a resumed audio playback become silent 2058 * after S3. 2059 */ 2060 pin_cvt_fixup(codec, per_pin, 0); 2061 2062 /* Call sync_audio_rate to set the N/CTS/M manually if necessary */ 2063 /* Todo: add DP1.2 MST audio support later */ 2064 if (codec_has_acomp(codec)) 2065 snd_hdac_sync_audio_rate(&codec->core, per_pin->pin_nid, 2066 per_pin->dev_id, runtime->rate); 2067 2068 non_pcm = check_non_pcm_per_cvt(codec, cvt_nid); 2069 mutex_lock(&per_pin->lock); 2070 per_pin->channels = substream->runtime->channels; 2071 per_pin->setup = true; 2072 2073 if (get_wcaps(codec, cvt_nid) & AC_WCAP_STRIPE) { 2074 stripe = snd_hdac_get_stream_stripe_ctl(&codec->bus->core, 2075 substream); 2076 snd_hda_codec_write(codec, cvt_nid, 0, 2077 AC_VERB_SET_STRIPE_CONTROL, 2078 stripe); 2079 } 2080 2081 hdmi_setup_audio_infoframe(codec, per_pin, non_pcm); 2082 mutex_unlock(&per_pin->lock); 2083 if (spec->dyn_pin_out) { 2084 snd_hda_set_dev_select(codec, per_pin->pin_nid, 2085 per_pin->dev_id); 2086 pinctl = snd_hda_codec_read(codec, per_pin->pin_nid, 0, 2087 AC_VERB_GET_PIN_WIDGET_CONTROL, 0); 2088 snd_hda_codec_write(codec, per_pin->pin_nid, 0, 2089 AC_VERB_SET_PIN_WIDGET_CONTROL, 2090 pinctl | PIN_OUT); 2091 } 2092 2093 /* snd_hda_set_dev_select() has been called before */ 2094 err = spec->ops.setup_stream(codec, cvt_nid, per_pin->pin_nid, 2095 per_pin->dev_id, stream_tag, format); 2096 unlock: 2097 mutex_unlock(&spec->pcm_lock); 2098 return err; 2099 } 2100 2101 static int generic_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo, 2102 struct hda_codec *codec, 2103 struct snd_pcm_substream *substream) 2104 { 2105 snd_hda_codec_cleanup_stream(codec, hinfo->nid); 2106 return 0; 2107 } 2108 2109 static int hdmi_pcm_close(struct hda_pcm_stream *hinfo, 2110 struct hda_codec *codec, 2111 struct snd_pcm_substream *substream) 2112 { 2113 struct hdmi_spec *spec = codec->spec; 2114 int cvt_idx, pin_idx, pcm_idx; 2115 struct hdmi_spec_per_cvt *per_cvt; 2116 struct hdmi_spec_per_pin *per_pin; 2117 int pinctl; 2118 int err = 0; 2119 2120 mutex_lock(&spec->pcm_lock); 2121 if (hinfo->nid) { 2122 pcm_idx = hinfo_to_pcm_index(codec, hinfo); 2123 if (snd_BUG_ON(pcm_idx < 0)) { 2124 err = -EINVAL; 2125 goto unlock; 2126 } 2127 cvt_idx = cvt_nid_to_cvt_index(codec, hinfo->nid); 2128 if (snd_BUG_ON(cvt_idx < 0)) { 2129 err = -EINVAL; 2130 goto unlock; 2131 } 2132 per_cvt = get_cvt(spec, cvt_idx); 2133 per_cvt->assigned = 0; 2134 hinfo->nid = 0; 2135 2136 azx_stream(get_azx_dev(substream))->stripe = 0; 2137 2138 snd_hda_spdif_ctls_unassign(codec, pcm_idx); 2139 clear_bit(pcm_idx, &spec->pcm_in_use); 2140 pin_idx = hinfo_to_pin_index(codec, hinfo); 2141 if (spec->dyn_pcm_assign && pin_idx < 0) 2142 goto unlock; 2143 2144 if (snd_BUG_ON(pin_idx < 0)) { 2145 err = -EINVAL; 2146 goto unlock; 2147 } 2148 per_pin = get_pin(spec, pin_idx); 2149 2150 if (spec->dyn_pin_out) { 2151 snd_hda_set_dev_select(codec, per_pin->pin_nid, 2152 per_pin->dev_id); 2153 pinctl = snd_hda_codec_read(codec, per_pin->pin_nid, 0, 2154 AC_VERB_GET_PIN_WIDGET_CONTROL, 0); 2155 snd_hda_codec_write(codec, per_pin->pin_nid, 0, 2156 AC_VERB_SET_PIN_WIDGET_CONTROL, 2157 pinctl & ~PIN_OUT); 2158 } 2159 2160 mutex_lock(&per_pin->lock); 2161 per_pin->chmap_set = false; 2162 memset(per_pin->chmap, 0, sizeof(per_pin->chmap)); 2163 2164 per_pin->setup = false; 2165 per_pin->channels = 0; 2166 mutex_unlock(&per_pin->lock); 2167 } 2168 2169 unlock: 2170 mutex_unlock(&spec->pcm_lock); 2171 2172 return err; 2173 } 2174 2175 static const struct hda_pcm_ops generic_ops = { 2176 .open = hdmi_pcm_open, 2177 .close = hdmi_pcm_close, 2178 .prepare = generic_hdmi_playback_pcm_prepare, 2179 .cleanup = generic_hdmi_playback_pcm_cleanup, 2180 }; 2181 2182 static int hdmi_get_spk_alloc(struct hdac_device *hdac, int pcm_idx) 2183 { 2184 struct hda_codec *codec = hdac_to_hda_codec(hdac); 2185 struct hdmi_spec *spec = codec->spec; 2186 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx); 2187 2188 if (!per_pin) 2189 return 0; 2190 2191 return per_pin->sink_eld.info.spk_alloc; 2192 } 2193 2194 static void hdmi_get_chmap(struct hdac_device *hdac, int pcm_idx, 2195 unsigned char *chmap) 2196 { 2197 struct hda_codec *codec = hdac_to_hda_codec(hdac); 2198 struct hdmi_spec *spec = codec->spec; 2199 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx); 2200 2201 /* chmap is already set to 0 in caller */ 2202 if (!per_pin) 2203 return; 2204 2205 memcpy(chmap, per_pin->chmap, ARRAY_SIZE(per_pin->chmap)); 2206 } 2207 2208 static void hdmi_set_chmap(struct hdac_device *hdac, int pcm_idx, 2209 unsigned char *chmap, int prepared) 2210 { 2211 struct hda_codec *codec = hdac_to_hda_codec(hdac); 2212 struct hdmi_spec *spec = codec->spec; 2213 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx); 2214 2215 if (!per_pin) 2216 return; 2217 mutex_lock(&per_pin->lock); 2218 per_pin->chmap_set = true; 2219 memcpy(per_pin->chmap, chmap, ARRAY_SIZE(per_pin->chmap)); 2220 if (prepared) 2221 hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm); 2222 mutex_unlock(&per_pin->lock); 2223 } 2224 2225 static bool is_hdmi_pcm_attached(struct hdac_device *hdac, int pcm_idx) 2226 { 2227 struct hda_codec *codec = hdac_to_hda_codec(hdac); 2228 struct hdmi_spec *spec = codec->spec; 2229 struct hdmi_spec_per_pin *per_pin = pcm_idx_to_pin(spec, pcm_idx); 2230 2231 return per_pin ? true:false; 2232 } 2233 2234 static int generic_hdmi_build_pcms(struct hda_codec *codec) 2235 { 2236 struct hdmi_spec *spec = codec->spec; 2237 int idx, pcm_num; 2238 2239 /* 2240 * for non-mst mode, pcm number is the same as before 2241 * for DP MST mode without extra PCM, pcm number is same 2242 * for DP MST mode with extra PCMs, pcm number is 2243 * (nid number + dev_num - 1) 2244 * dev_num is the device entry number in a pin 2245 */ 2246 2247 if (codec->mst_no_extra_pcms) 2248 pcm_num = spec->num_nids; 2249 else 2250 pcm_num = spec->num_nids + spec->dev_num - 1; 2251 2252 codec_dbg(codec, "hdmi: pcm_num set to %d\n", pcm_num); 2253 2254 for (idx = 0; idx < pcm_num; idx++) { 2255 struct hda_pcm *info; 2256 struct hda_pcm_stream *pstr; 2257 2258 info = snd_hda_codec_pcm_new(codec, "HDMI %d", idx); 2259 if (!info) 2260 return -ENOMEM; 2261 2262 spec->pcm_rec[idx].pcm = info; 2263 spec->pcm_used++; 2264 info->pcm_type = HDA_PCM_TYPE_HDMI; 2265 info->own_chmap = true; 2266 2267 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK]; 2268 pstr->substreams = 1; 2269 pstr->ops = generic_ops; 2270 /* pcm number is less than 16 */ 2271 if (spec->pcm_used >= 16) 2272 break; 2273 /* other pstr fields are set in open */ 2274 } 2275 2276 return 0; 2277 } 2278 2279 static void free_hdmi_jack_priv(struct snd_jack *jack) 2280 { 2281 struct hdmi_pcm *pcm = jack->private_data; 2282 2283 pcm->jack = NULL; 2284 } 2285 2286 static int generic_hdmi_build_jack(struct hda_codec *codec, int pcm_idx) 2287 { 2288 char hdmi_str[32] = "HDMI/DP"; 2289 struct hdmi_spec *spec = codec->spec; 2290 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pcm_idx); 2291 struct snd_jack *jack; 2292 int pcmdev = get_pcm_rec(spec, pcm_idx)->device; 2293 int err; 2294 2295 if (pcmdev > 0) 2296 sprintf(hdmi_str + strlen(hdmi_str), ",pcm=%d", pcmdev); 2297 if (!spec->dyn_pcm_assign && 2298 !is_jack_detectable(codec, per_pin->pin_nid)) 2299 strncat(hdmi_str, " Phantom", 2300 sizeof(hdmi_str) - strlen(hdmi_str) - 1); 2301 2302 err = snd_jack_new(codec->card, hdmi_str, SND_JACK_AVOUT, &jack, 2303 true, false); 2304 if (err < 0) 2305 return err; 2306 2307 spec->pcm_rec[pcm_idx].jack = jack; 2308 jack->private_data = &spec->pcm_rec[pcm_idx]; 2309 jack->private_free = free_hdmi_jack_priv; 2310 return 0; 2311 } 2312 2313 static int generic_hdmi_build_controls(struct hda_codec *codec) 2314 { 2315 struct hdmi_spec *spec = codec->spec; 2316 int dev, err; 2317 int pin_idx, pcm_idx; 2318 2319 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) { 2320 if (!get_pcm_rec(spec, pcm_idx)->pcm) { 2321 /* no PCM: mark this for skipping permanently */ 2322 set_bit(pcm_idx, &spec->pcm_bitmap); 2323 continue; 2324 } 2325 2326 err = generic_hdmi_build_jack(codec, pcm_idx); 2327 if (err < 0) 2328 return err; 2329 2330 /* create the spdif for each pcm 2331 * pin will be bound when monitor is connected 2332 */ 2333 if (spec->dyn_pcm_assign) 2334 err = snd_hda_create_dig_out_ctls(codec, 2335 0, spec->cvt_nids[0], 2336 HDA_PCM_TYPE_HDMI); 2337 else { 2338 struct hdmi_spec_per_pin *per_pin = 2339 get_pin(spec, pcm_idx); 2340 err = snd_hda_create_dig_out_ctls(codec, 2341 per_pin->pin_nid, 2342 per_pin->mux_nids[0], 2343 HDA_PCM_TYPE_HDMI); 2344 } 2345 if (err < 0) 2346 return err; 2347 snd_hda_spdif_ctls_unassign(codec, pcm_idx); 2348 2349 dev = get_pcm_rec(spec, pcm_idx)->device; 2350 if (dev != SNDRV_PCM_INVALID_DEVICE) { 2351 /* add control for ELD Bytes */ 2352 err = hdmi_create_eld_ctl(codec, pcm_idx, dev); 2353 if (err < 0) 2354 return err; 2355 } 2356 } 2357 2358 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) { 2359 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx); 2360 struct hdmi_eld *pin_eld = &per_pin->sink_eld; 2361 2362 pin_eld->eld_valid = false; 2363 hdmi_present_sense(per_pin, 0); 2364 } 2365 2366 /* add channel maps */ 2367 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) { 2368 struct hda_pcm *pcm; 2369 2370 pcm = get_pcm_rec(spec, pcm_idx); 2371 if (!pcm || !pcm->pcm) 2372 break; 2373 err = snd_hdac_add_chmap_ctls(pcm->pcm, pcm_idx, &spec->chmap); 2374 if (err < 0) 2375 return err; 2376 } 2377 2378 return 0; 2379 } 2380 2381 static int generic_hdmi_init_per_pins(struct hda_codec *codec) 2382 { 2383 struct hdmi_spec *spec = codec->spec; 2384 int pin_idx; 2385 2386 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) { 2387 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx); 2388 2389 per_pin->codec = codec; 2390 mutex_init(&per_pin->lock); 2391 INIT_DELAYED_WORK(&per_pin->work, hdmi_repoll_eld); 2392 eld_proc_new(per_pin, pin_idx); 2393 } 2394 return 0; 2395 } 2396 2397 static int generic_hdmi_init(struct hda_codec *codec) 2398 { 2399 struct hdmi_spec *spec = codec->spec; 2400 int pin_idx; 2401 2402 mutex_lock(&spec->bind_lock); 2403 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) { 2404 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx); 2405 hda_nid_t pin_nid = per_pin->pin_nid; 2406 int dev_id = per_pin->dev_id; 2407 2408 snd_hda_set_dev_select(codec, pin_nid, dev_id); 2409 hdmi_init_pin(codec, pin_nid); 2410 if (codec_has_acomp(codec)) 2411 continue; 2412 snd_hda_jack_detect_enable_callback_mst(codec, pin_nid, dev_id, 2413 jack_callback); 2414 } 2415 mutex_unlock(&spec->bind_lock); 2416 return 0; 2417 } 2418 2419 static void hdmi_array_init(struct hdmi_spec *spec, int nums) 2420 { 2421 snd_array_init(&spec->pins, sizeof(struct hdmi_spec_per_pin), nums); 2422 snd_array_init(&spec->cvts, sizeof(struct hdmi_spec_per_cvt), nums); 2423 } 2424 2425 static void hdmi_array_free(struct hdmi_spec *spec) 2426 { 2427 snd_array_free(&spec->pins); 2428 snd_array_free(&spec->cvts); 2429 } 2430 2431 static void generic_spec_free(struct hda_codec *codec) 2432 { 2433 struct hdmi_spec *spec = codec->spec; 2434 2435 if (spec) { 2436 hdmi_array_free(spec); 2437 kfree(spec); 2438 codec->spec = NULL; 2439 } 2440 codec->dp_mst = false; 2441 } 2442 2443 static void generic_hdmi_free(struct hda_codec *codec) 2444 { 2445 struct hdmi_spec *spec = codec->spec; 2446 int pin_idx, pcm_idx; 2447 2448 if (spec->acomp_registered) { 2449 snd_hdac_acomp_exit(&codec->bus->core); 2450 } else if (codec_has_acomp(codec)) { 2451 snd_hdac_acomp_register_notifier(&codec->bus->core, NULL); 2452 } 2453 codec->relaxed_resume = 0; 2454 2455 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) { 2456 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx); 2457 cancel_delayed_work_sync(&per_pin->work); 2458 eld_proc_free(per_pin); 2459 } 2460 2461 for (pcm_idx = 0; pcm_idx < spec->pcm_used; pcm_idx++) { 2462 if (spec->pcm_rec[pcm_idx].jack == NULL) 2463 continue; 2464 if (spec->dyn_pcm_assign) 2465 snd_device_free(codec->card, 2466 spec->pcm_rec[pcm_idx].jack); 2467 else 2468 spec->pcm_rec[pcm_idx].jack = NULL; 2469 } 2470 2471 generic_spec_free(codec); 2472 } 2473 2474 #ifdef CONFIG_PM 2475 static int generic_hdmi_resume(struct hda_codec *codec) 2476 { 2477 struct hdmi_spec *spec = codec->spec; 2478 int pin_idx; 2479 2480 codec->patch_ops.init(codec); 2481 snd_hda_regmap_sync(codec); 2482 2483 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) { 2484 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx); 2485 hdmi_present_sense(per_pin, 1); 2486 } 2487 return 0; 2488 } 2489 #endif 2490 2491 static const struct hda_codec_ops generic_hdmi_patch_ops = { 2492 .init = generic_hdmi_init, 2493 .free = generic_hdmi_free, 2494 .build_pcms = generic_hdmi_build_pcms, 2495 .build_controls = generic_hdmi_build_controls, 2496 .unsol_event = hdmi_unsol_event, 2497 #ifdef CONFIG_PM 2498 .resume = generic_hdmi_resume, 2499 #endif 2500 }; 2501 2502 static const struct hdmi_ops generic_standard_hdmi_ops = { 2503 .pin_get_eld = hdmi_pin_get_eld, 2504 .pin_setup_infoframe = hdmi_pin_setup_infoframe, 2505 .pin_hbr_setup = hdmi_pin_hbr_setup, 2506 .setup_stream = hdmi_setup_stream, 2507 }; 2508 2509 /* allocate codec->spec and assign/initialize generic parser ops */ 2510 static int alloc_generic_hdmi(struct hda_codec *codec) 2511 { 2512 struct hdmi_spec *spec; 2513 2514 spec = kzalloc(sizeof(*spec), GFP_KERNEL); 2515 if (!spec) 2516 return -ENOMEM; 2517 2518 spec->codec = codec; 2519 spec->ops = generic_standard_hdmi_ops; 2520 spec->dev_num = 1; /* initialize to 1 */ 2521 mutex_init(&spec->pcm_lock); 2522 mutex_init(&spec->bind_lock); 2523 snd_hdac_register_chmap_ops(&codec->core, &spec->chmap); 2524 2525 spec->chmap.ops.get_chmap = hdmi_get_chmap; 2526 spec->chmap.ops.set_chmap = hdmi_set_chmap; 2527 spec->chmap.ops.is_pcm_attached = is_hdmi_pcm_attached; 2528 spec->chmap.ops.get_spk_alloc = hdmi_get_spk_alloc; 2529 2530 codec->spec = spec; 2531 hdmi_array_init(spec, 4); 2532 2533 codec->patch_ops = generic_hdmi_patch_ops; 2534 2535 return 0; 2536 } 2537 2538 /* generic HDMI parser */ 2539 static int patch_generic_hdmi(struct hda_codec *codec) 2540 { 2541 int err; 2542 2543 err = alloc_generic_hdmi(codec); 2544 if (err < 0) 2545 return err; 2546 2547 err = hdmi_parse_codec(codec); 2548 if (err < 0) { 2549 generic_spec_free(codec); 2550 return err; 2551 } 2552 2553 generic_hdmi_init_per_pins(codec); 2554 return 0; 2555 } 2556 2557 /* 2558 * generic audio component binding 2559 */ 2560 2561 /* turn on / off the unsol event jack detection dynamically */ 2562 static void reprogram_jack_detect(struct hda_codec *codec, hda_nid_t nid, 2563 int dev_id, bool use_acomp) 2564 { 2565 struct hda_jack_tbl *tbl; 2566 2567 tbl = snd_hda_jack_tbl_get_mst(codec, nid, dev_id); 2568 if (tbl) { 2569 /* clear unsol even if component notifier is used, or re-enable 2570 * if notifier is cleared 2571 */ 2572 unsigned int val = use_acomp ? 0 : (AC_USRSP_EN | tbl->tag); 2573 snd_hda_codec_write_cache(codec, nid, 0, 2574 AC_VERB_SET_UNSOLICITED_ENABLE, val); 2575 } 2576 } 2577 2578 /* set up / clear component notifier dynamically */ 2579 static void generic_acomp_notifier_set(struct drm_audio_component *acomp, 2580 bool use_acomp) 2581 { 2582 struct hdmi_spec *spec; 2583 int i; 2584 2585 spec = container_of(acomp->audio_ops, struct hdmi_spec, drm_audio_ops); 2586 mutex_lock(&spec->bind_lock); 2587 spec->use_acomp_notifier = use_acomp; 2588 spec->codec->relaxed_resume = use_acomp; 2589 spec->codec->bus->keep_power = 0; 2590 /* reprogram each jack detection logic depending on the notifier */ 2591 for (i = 0; i < spec->num_pins; i++) 2592 reprogram_jack_detect(spec->codec, 2593 get_pin(spec, i)->pin_nid, 2594 get_pin(spec, i)->dev_id, 2595 use_acomp); 2596 mutex_unlock(&spec->bind_lock); 2597 } 2598 2599 /* enable / disable the notifier via master bind / unbind */ 2600 static int generic_acomp_master_bind(struct device *dev, 2601 struct drm_audio_component *acomp) 2602 { 2603 generic_acomp_notifier_set(acomp, true); 2604 return 0; 2605 } 2606 2607 static void generic_acomp_master_unbind(struct device *dev, 2608 struct drm_audio_component *acomp) 2609 { 2610 generic_acomp_notifier_set(acomp, false); 2611 } 2612 2613 /* check whether both HD-audio and DRM PCI devices belong to the same bus */ 2614 static int match_bound_vga(struct device *dev, int subtype, void *data) 2615 { 2616 struct hdac_bus *bus = data; 2617 struct pci_dev *pci, *master; 2618 2619 if (!dev_is_pci(dev) || !dev_is_pci(bus->dev)) 2620 return 0; 2621 master = to_pci_dev(bus->dev); 2622 pci = to_pci_dev(dev); 2623 return master->bus == pci->bus; 2624 } 2625 2626 /* audio component notifier for AMD/Nvidia HDMI codecs */ 2627 static void generic_acomp_pin_eld_notify(void *audio_ptr, int port, int dev_id) 2628 { 2629 struct hda_codec *codec = audio_ptr; 2630 struct hdmi_spec *spec = codec->spec; 2631 hda_nid_t pin_nid = spec->port2pin(codec, port); 2632 2633 if (!pin_nid) 2634 return; 2635 if (get_wcaps_type(get_wcaps(codec, pin_nid)) != AC_WID_PIN) 2636 return; 2637 /* skip notification during system suspend (but not in runtime PM); 2638 * the state will be updated at resume 2639 */ 2640 if (snd_power_get_state(codec->card) != SNDRV_CTL_POWER_D0) 2641 return; 2642 /* ditto during suspend/resume process itself */ 2643 if (snd_hdac_is_in_pm(&codec->core)) 2644 return; 2645 2646 check_presence_and_report(codec, pin_nid, dev_id); 2647 } 2648 2649 /* set up the private drm_audio_ops from the template */ 2650 static void setup_drm_audio_ops(struct hda_codec *codec, 2651 const struct drm_audio_component_audio_ops *ops) 2652 { 2653 struct hdmi_spec *spec = codec->spec; 2654 2655 spec->drm_audio_ops.audio_ptr = codec; 2656 /* intel_audio_codec_enable() or intel_audio_codec_disable() 2657 * will call pin_eld_notify with using audio_ptr pointer 2658 * We need make sure audio_ptr is really setup 2659 */ 2660 wmb(); 2661 spec->drm_audio_ops.pin2port = ops->pin2port; 2662 spec->drm_audio_ops.pin_eld_notify = ops->pin_eld_notify; 2663 spec->drm_audio_ops.master_bind = ops->master_bind; 2664 spec->drm_audio_ops.master_unbind = ops->master_unbind; 2665 } 2666 2667 /* initialize the generic HDMI audio component */ 2668 static void generic_acomp_init(struct hda_codec *codec, 2669 const struct drm_audio_component_audio_ops *ops, 2670 int (*port2pin)(struct hda_codec *, int)) 2671 { 2672 struct hdmi_spec *spec = codec->spec; 2673 2674 if (!enable_acomp) { 2675 codec_info(codec, "audio component disabled by module option\n"); 2676 return; 2677 } 2678 2679 spec->port2pin = port2pin; 2680 setup_drm_audio_ops(codec, ops); 2681 if (!snd_hdac_acomp_init(&codec->bus->core, &spec->drm_audio_ops, 2682 match_bound_vga, 0)) { 2683 spec->acomp_registered = true; 2684 } 2685 } 2686 2687 /* 2688 * Intel codec parsers and helpers 2689 */ 2690 2691 #define INTEL_GET_VENDOR_VERB 0xf81 2692 #define INTEL_SET_VENDOR_VERB 0x781 2693 #define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */ 2694 #define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */ 2695 2696 static void intel_haswell_enable_all_pins(struct hda_codec *codec, 2697 bool update_tree) 2698 { 2699 unsigned int vendor_param; 2700 struct hdmi_spec *spec = codec->spec; 2701 2702 vendor_param = snd_hda_codec_read(codec, spec->vendor_nid, 0, 2703 INTEL_GET_VENDOR_VERB, 0); 2704 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS) 2705 return; 2706 2707 vendor_param |= INTEL_EN_ALL_PIN_CVTS; 2708 vendor_param = snd_hda_codec_read(codec, spec->vendor_nid, 0, 2709 INTEL_SET_VENDOR_VERB, vendor_param); 2710 if (vendor_param == -1) 2711 return; 2712 2713 if (update_tree) 2714 snd_hda_codec_update_widgets(codec); 2715 } 2716 2717 static void intel_haswell_fixup_enable_dp12(struct hda_codec *codec) 2718 { 2719 unsigned int vendor_param; 2720 struct hdmi_spec *spec = codec->spec; 2721 2722 vendor_param = snd_hda_codec_read(codec, spec->vendor_nid, 0, 2723 INTEL_GET_VENDOR_VERB, 0); 2724 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12) 2725 return; 2726 2727 /* enable DP1.2 mode */ 2728 vendor_param |= INTEL_EN_DP12; 2729 snd_hdac_regmap_add_vendor_verb(&codec->core, INTEL_SET_VENDOR_VERB); 2730 snd_hda_codec_write_cache(codec, spec->vendor_nid, 0, 2731 INTEL_SET_VENDOR_VERB, vendor_param); 2732 } 2733 2734 /* Haswell needs to re-issue the vendor-specific verbs before turning to D0. 2735 * Otherwise you may get severe h/w communication errors. 2736 */ 2737 static void haswell_set_power_state(struct hda_codec *codec, hda_nid_t fg, 2738 unsigned int power_state) 2739 { 2740 if (power_state == AC_PWRST_D0) { 2741 intel_haswell_enable_all_pins(codec, false); 2742 intel_haswell_fixup_enable_dp12(codec); 2743 } 2744 2745 snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE, power_state); 2746 snd_hda_codec_set_power_to_all(codec, fg, power_state); 2747 } 2748 2749 /* There is a fixed mapping between audio pin node and display port. 2750 * on SNB, IVY, HSW, BSW, SKL, BXT, KBL: 2751 * Pin Widget 5 - PORT B (port = 1 in i915 driver) 2752 * Pin Widget 6 - PORT C (port = 2 in i915 driver) 2753 * Pin Widget 7 - PORT D (port = 3 in i915 driver) 2754 * 2755 * on VLV, ILK: 2756 * Pin Widget 4 - PORT B (port = 1 in i915 driver) 2757 * Pin Widget 5 - PORT C (port = 2 in i915 driver) 2758 * Pin Widget 6 - PORT D (port = 3 in i915 driver) 2759 */ 2760 static int intel_base_nid(struct hda_codec *codec) 2761 { 2762 switch (codec->core.vendor_id) { 2763 case 0x80860054: /* ILK */ 2764 case 0x80862804: /* ILK */ 2765 case 0x80862882: /* VLV */ 2766 return 4; 2767 default: 2768 return 5; 2769 } 2770 } 2771 2772 static int intel_pin2port(void *audio_ptr, int pin_nid) 2773 { 2774 struct hda_codec *codec = audio_ptr; 2775 struct hdmi_spec *spec = codec->spec; 2776 int base_nid, i; 2777 2778 if (!spec->port_num) { 2779 base_nid = intel_base_nid(codec); 2780 if (WARN_ON(pin_nid < base_nid || pin_nid >= base_nid + 3)) 2781 return -1; 2782 return pin_nid - base_nid + 1; 2783 } 2784 2785 /* 2786 * looking for the pin number in the mapping table and return 2787 * the index which indicate the port number 2788 */ 2789 for (i = 0; i < spec->port_num; i++) { 2790 if (pin_nid == spec->port_map[i]) 2791 return i; 2792 } 2793 2794 codec_info(codec, "Can't find the HDMI/DP port for pin NID 0x%x\n", pin_nid); 2795 return -1; 2796 } 2797 2798 static int intel_port2pin(struct hda_codec *codec, int port) 2799 { 2800 struct hdmi_spec *spec = codec->spec; 2801 2802 if (!spec->port_num) { 2803 /* we assume only from port-B to port-D */ 2804 if (port < 1 || port > 3) 2805 return 0; 2806 return port + intel_base_nid(codec) - 1; 2807 } 2808 2809 if (port < 0 || port >= spec->port_num) 2810 return 0; 2811 return spec->port_map[port]; 2812 } 2813 2814 static void intel_pin_eld_notify(void *audio_ptr, int port, int pipe) 2815 { 2816 struct hda_codec *codec = audio_ptr; 2817 int pin_nid; 2818 int dev_id = pipe; 2819 2820 pin_nid = intel_port2pin(codec, port); 2821 if (!pin_nid) 2822 return; 2823 /* skip notification during system suspend (but not in runtime PM); 2824 * the state will be updated at resume 2825 */ 2826 if (snd_power_get_state(codec->card) != SNDRV_CTL_POWER_D0) 2827 return; 2828 /* ditto during suspend/resume process itself */ 2829 if (snd_hdac_is_in_pm(&codec->core)) 2830 return; 2831 2832 snd_hdac_i915_set_bclk(&codec->bus->core); 2833 check_presence_and_report(codec, pin_nid, dev_id); 2834 } 2835 2836 static const struct drm_audio_component_audio_ops intel_audio_ops = { 2837 .pin2port = intel_pin2port, 2838 .pin_eld_notify = intel_pin_eld_notify, 2839 }; 2840 2841 /* register i915 component pin_eld_notify callback */ 2842 static void register_i915_notifier(struct hda_codec *codec) 2843 { 2844 struct hdmi_spec *spec = codec->spec; 2845 2846 spec->use_acomp_notifier = true; 2847 spec->port2pin = intel_port2pin; 2848 setup_drm_audio_ops(codec, &intel_audio_ops); 2849 snd_hdac_acomp_register_notifier(&codec->bus->core, 2850 &spec->drm_audio_ops); 2851 /* no need for forcible resume for jack check thanks to notifier */ 2852 codec->relaxed_resume = 1; 2853 } 2854 2855 /* setup_stream ops override for HSW+ */ 2856 static int i915_hsw_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid, 2857 hda_nid_t pin_nid, int dev_id, u32 stream_tag, 2858 int format) 2859 { 2860 haswell_verify_D0(codec, cvt_nid, pin_nid); 2861 return hdmi_setup_stream(codec, cvt_nid, pin_nid, dev_id, 2862 stream_tag, format); 2863 } 2864 2865 /* pin_cvt_fixup ops override for HSW+ and VLV+ */ 2866 static void i915_pin_cvt_fixup(struct hda_codec *codec, 2867 struct hdmi_spec_per_pin *per_pin, 2868 hda_nid_t cvt_nid) 2869 { 2870 if (per_pin) { 2871 haswell_verify_D0(codec, per_pin->cvt_nid, per_pin->pin_nid); 2872 snd_hda_set_dev_select(codec, per_pin->pin_nid, 2873 per_pin->dev_id); 2874 intel_verify_pin_cvt_connect(codec, per_pin); 2875 intel_not_share_assigned_cvt(codec, per_pin->pin_nid, 2876 per_pin->dev_id, per_pin->mux_idx); 2877 } else { 2878 intel_not_share_assigned_cvt_nid(codec, 0, 0, cvt_nid); 2879 } 2880 } 2881 2882 /* precondition and allocation for Intel codecs */ 2883 static int alloc_intel_hdmi(struct hda_codec *codec) 2884 { 2885 int err; 2886 2887 /* requires i915 binding */ 2888 if (!codec->bus->core.audio_component) { 2889 codec_info(codec, "No i915 binding for Intel HDMI/DP codec\n"); 2890 /* set probe_id here to prevent generic fallback binding */ 2891 codec->probe_id = HDA_CODEC_ID_SKIP_PROBE; 2892 return -ENODEV; 2893 } 2894 2895 err = alloc_generic_hdmi(codec); 2896 if (err < 0) 2897 return err; 2898 /* no need to handle unsol events */ 2899 codec->patch_ops.unsol_event = NULL; 2900 return 0; 2901 } 2902 2903 /* parse and post-process for Intel codecs */ 2904 static int parse_intel_hdmi(struct hda_codec *codec) 2905 { 2906 int err, retries = 3; 2907 2908 do { 2909 err = hdmi_parse_codec(codec); 2910 } while (err < 0 && retries--); 2911 2912 if (err < 0) { 2913 generic_spec_free(codec); 2914 return err; 2915 } 2916 2917 generic_hdmi_init_per_pins(codec); 2918 register_i915_notifier(codec); 2919 return 0; 2920 } 2921 2922 /* Intel Haswell and onwards; audio component with eld notifier */ 2923 static int intel_hsw_common_init(struct hda_codec *codec, hda_nid_t vendor_nid, 2924 const int *port_map, int port_num) 2925 { 2926 struct hdmi_spec *spec; 2927 int err; 2928 2929 err = alloc_intel_hdmi(codec); 2930 if (err < 0) 2931 return err; 2932 spec = codec->spec; 2933 codec->dp_mst = true; 2934 spec->dyn_pcm_assign = true; 2935 spec->vendor_nid = vendor_nid; 2936 spec->port_map = port_map; 2937 spec->port_num = port_num; 2938 spec->intel_hsw_fixup = true; 2939 2940 intel_haswell_enable_all_pins(codec, true); 2941 intel_haswell_fixup_enable_dp12(codec); 2942 2943 codec->display_power_control = 1; 2944 2945 codec->patch_ops.set_power_state = haswell_set_power_state; 2946 codec->depop_delay = 0; 2947 codec->auto_runtime_pm = 1; 2948 2949 spec->ops.setup_stream = i915_hsw_setup_stream; 2950 spec->ops.pin_cvt_fixup = i915_pin_cvt_fixup; 2951 2952 /* 2953 * Enable silent stream feature, if it is enabled via 2954 * module param or Kconfig option 2955 */ 2956 if (enable_silent_stream) 2957 spec->send_silent_stream = true; 2958 2959 return parse_intel_hdmi(codec); 2960 } 2961 2962 static int patch_i915_hsw_hdmi(struct hda_codec *codec) 2963 { 2964 return intel_hsw_common_init(codec, 0x08, NULL, 0); 2965 } 2966 2967 static int patch_i915_glk_hdmi(struct hda_codec *codec) 2968 { 2969 return intel_hsw_common_init(codec, 0x0b, NULL, 0); 2970 } 2971 2972 static int patch_i915_icl_hdmi(struct hda_codec *codec) 2973 { 2974 /* 2975 * pin to port mapping table where the value indicate the pin number and 2976 * the index indicate the port number. 2977 */ 2978 static const int map[] = {0x0, 0x4, 0x6, 0x8, 0xa, 0xb}; 2979 2980 return intel_hsw_common_init(codec, 0x02, map, ARRAY_SIZE(map)); 2981 } 2982 2983 static int patch_i915_tgl_hdmi(struct hda_codec *codec) 2984 { 2985 /* 2986 * pin to port mapping table where the value indicate the pin number and 2987 * the index indicate the port number. 2988 */ 2989 static const int map[] = {0x4, 0x6, 0x8, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf}; 2990 2991 return intel_hsw_common_init(codec, 0x02, map, ARRAY_SIZE(map)); 2992 } 2993 2994 /* Intel Baytrail and Braswell; with eld notifier */ 2995 static int patch_i915_byt_hdmi(struct hda_codec *codec) 2996 { 2997 struct hdmi_spec *spec; 2998 int err; 2999 3000 err = alloc_intel_hdmi(codec); 3001 if (err < 0) 3002 return err; 3003 spec = codec->spec; 3004 3005 /* For Valleyview/Cherryview, only the display codec is in the display 3006 * power well and can use link_power ops to request/release the power. 3007 */ 3008 codec->display_power_control = 1; 3009 3010 codec->depop_delay = 0; 3011 codec->auto_runtime_pm = 1; 3012 3013 spec->ops.pin_cvt_fixup = i915_pin_cvt_fixup; 3014 3015 return parse_intel_hdmi(codec); 3016 } 3017 3018 /* Intel IronLake, SandyBridge and IvyBridge; with eld notifier */ 3019 static int patch_i915_cpt_hdmi(struct hda_codec *codec) 3020 { 3021 int err; 3022 3023 err = alloc_intel_hdmi(codec); 3024 if (err < 0) 3025 return err; 3026 return parse_intel_hdmi(codec); 3027 } 3028 3029 /* 3030 * Shared non-generic implementations 3031 */ 3032 3033 static int simple_playback_build_pcms(struct hda_codec *codec) 3034 { 3035 struct hdmi_spec *spec = codec->spec; 3036 struct hda_pcm *info; 3037 unsigned int chans; 3038 struct hda_pcm_stream *pstr; 3039 struct hdmi_spec_per_cvt *per_cvt; 3040 3041 per_cvt = get_cvt(spec, 0); 3042 chans = get_wcaps(codec, per_cvt->cvt_nid); 3043 chans = get_wcaps_channels(chans); 3044 3045 info = snd_hda_codec_pcm_new(codec, "HDMI 0"); 3046 if (!info) 3047 return -ENOMEM; 3048 spec->pcm_rec[0].pcm = info; 3049 info->pcm_type = HDA_PCM_TYPE_HDMI; 3050 pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK]; 3051 *pstr = spec->pcm_playback; 3052 pstr->nid = per_cvt->cvt_nid; 3053 if (pstr->channels_max <= 2 && chans && chans <= 16) 3054 pstr->channels_max = chans; 3055 3056 return 0; 3057 } 3058 3059 /* unsolicited event for jack sensing */ 3060 static void simple_hdmi_unsol_event(struct hda_codec *codec, 3061 unsigned int res) 3062 { 3063 snd_hda_jack_set_dirty_all(codec); 3064 snd_hda_jack_report_sync(codec); 3065 } 3066 3067 /* generic_hdmi_build_jack can be used for simple_hdmi, too, 3068 * as long as spec->pins[] is set correctly 3069 */ 3070 #define simple_hdmi_build_jack generic_hdmi_build_jack 3071 3072 static int simple_playback_build_controls(struct hda_codec *codec) 3073 { 3074 struct hdmi_spec *spec = codec->spec; 3075 struct hdmi_spec_per_cvt *per_cvt; 3076 int err; 3077 3078 per_cvt = get_cvt(spec, 0); 3079 err = snd_hda_create_dig_out_ctls(codec, per_cvt->cvt_nid, 3080 per_cvt->cvt_nid, 3081 HDA_PCM_TYPE_HDMI); 3082 if (err < 0) 3083 return err; 3084 return simple_hdmi_build_jack(codec, 0); 3085 } 3086 3087 static int simple_playback_init(struct hda_codec *codec) 3088 { 3089 struct hdmi_spec *spec = codec->spec; 3090 struct hdmi_spec_per_pin *per_pin = get_pin(spec, 0); 3091 hda_nid_t pin = per_pin->pin_nid; 3092 3093 snd_hda_codec_write(codec, pin, 0, 3094 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT); 3095 /* some codecs require to unmute the pin */ 3096 if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP) 3097 snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE, 3098 AMP_OUT_UNMUTE); 3099 snd_hda_jack_detect_enable(codec, pin, per_pin->dev_id); 3100 return 0; 3101 } 3102 3103 static void simple_playback_free(struct hda_codec *codec) 3104 { 3105 struct hdmi_spec *spec = codec->spec; 3106 3107 hdmi_array_free(spec); 3108 kfree(spec); 3109 } 3110 3111 /* 3112 * Nvidia specific implementations 3113 */ 3114 3115 #define Nv_VERB_SET_Channel_Allocation 0xF79 3116 #define Nv_VERB_SET_Info_Frame_Checksum 0xF7A 3117 #define Nv_VERB_SET_Audio_Protection_On 0xF98 3118 #define Nv_VERB_SET_Audio_Protection_Off 0xF99 3119 3120 #define nvhdmi_master_con_nid_7x 0x04 3121 #define nvhdmi_master_pin_nid_7x 0x05 3122 3123 static const hda_nid_t nvhdmi_con_nids_7x[4] = { 3124 /*front, rear, clfe, rear_surr */ 3125 0x6, 0x8, 0xa, 0xc, 3126 }; 3127 3128 static const struct hda_verb nvhdmi_basic_init_7x_2ch[] = { 3129 /* set audio protect on */ 3130 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1}, 3131 /* enable digital output on pin widget */ 3132 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 }, 3133 {} /* terminator */ 3134 }; 3135 3136 static const struct hda_verb nvhdmi_basic_init_7x_8ch[] = { 3137 /* set audio protect on */ 3138 { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1}, 3139 /* enable digital output on pin widget */ 3140 { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 }, 3141 { 0x7, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 }, 3142 { 0x9, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 }, 3143 { 0xb, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 }, 3144 { 0xd, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 }, 3145 {} /* terminator */ 3146 }; 3147 3148 #ifdef LIMITED_RATE_FMT_SUPPORT 3149 /* support only the safe format and rate */ 3150 #define SUPPORTED_RATES SNDRV_PCM_RATE_48000 3151 #define SUPPORTED_MAXBPS 16 3152 #define SUPPORTED_FORMATS SNDRV_PCM_FMTBIT_S16_LE 3153 #else 3154 /* support all rates and formats */ 3155 #define SUPPORTED_RATES \ 3156 (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\ 3157 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\ 3158 SNDRV_PCM_RATE_192000) 3159 #define SUPPORTED_MAXBPS 24 3160 #define SUPPORTED_FORMATS \ 3161 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE) 3162 #endif 3163 3164 static int nvhdmi_7x_init_2ch(struct hda_codec *codec) 3165 { 3166 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_2ch); 3167 return 0; 3168 } 3169 3170 static int nvhdmi_7x_init_8ch(struct hda_codec *codec) 3171 { 3172 snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_8ch); 3173 return 0; 3174 } 3175 3176 static const unsigned int channels_2_6_8[] = { 3177 2, 6, 8 3178 }; 3179 3180 static const unsigned int channels_2_8[] = { 3181 2, 8 3182 }; 3183 3184 static const struct snd_pcm_hw_constraint_list hw_constraints_2_6_8_channels = { 3185 .count = ARRAY_SIZE(channels_2_6_8), 3186 .list = channels_2_6_8, 3187 .mask = 0, 3188 }; 3189 3190 static const struct snd_pcm_hw_constraint_list hw_constraints_2_8_channels = { 3191 .count = ARRAY_SIZE(channels_2_8), 3192 .list = channels_2_8, 3193 .mask = 0, 3194 }; 3195 3196 static int simple_playback_pcm_open(struct hda_pcm_stream *hinfo, 3197 struct hda_codec *codec, 3198 struct snd_pcm_substream *substream) 3199 { 3200 struct hdmi_spec *spec = codec->spec; 3201 const struct snd_pcm_hw_constraint_list *hw_constraints_channels = NULL; 3202 3203 switch (codec->preset->vendor_id) { 3204 case 0x10de0002: 3205 case 0x10de0003: 3206 case 0x10de0005: 3207 case 0x10de0006: 3208 hw_constraints_channels = &hw_constraints_2_8_channels; 3209 break; 3210 case 0x10de0007: 3211 hw_constraints_channels = &hw_constraints_2_6_8_channels; 3212 break; 3213 default: 3214 break; 3215 } 3216 3217 if (hw_constraints_channels != NULL) { 3218 snd_pcm_hw_constraint_list(substream->runtime, 0, 3219 SNDRV_PCM_HW_PARAM_CHANNELS, 3220 hw_constraints_channels); 3221 } else { 3222 snd_pcm_hw_constraint_step(substream->runtime, 0, 3223 SNDRV_PCM_HW_PARAM_CHANNELS, 2); 3224 } 3225 3226 return snd_hda_multi_out_dig_open(codec, &spec->multiout); 3227 } 3228 3229 static int simple_playback_pcm_close(struct hda_pcm_stream *hinfo, 3230 struct hda_codec *codec, 3231 struct snd_pcm_substream *substream) 3232 { 3233 struct hdmi_spec *spec = codec->spec; 3234 return snd_hda_multi_out_dig_close(codec, &spec->multiout); 3235 } 3236 3237 static int simple_playback_pcm_prepare(struct hda_pcm_stream *hinfo, 3238 struct hda_codec *codec, 3239 unsigned int stream_tag, 3240 unsigned int format, 3241 struct snd_pcm_substream *substream) 3242 { 3243 struct hdmi_spec *spec = codec->spec; 3244 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout, 3245 stream_tag, format, substream); 3246 } 3247 3248 static const struct hda_pcm_stream simple_pcm_playback = { 3249 .substreams = 1, 3250 .channels_min = 2, 3251 .channels_max = 2, 3252 .ops = { 3253 .open = simple_playback_pcm_open, 3254 .close = simple_playback_pcm_close, 3255 .prepare = simple_playback_pcm_prepare 3256 }, 3257 }; 3258 3259 static const struct hda_codec_ops simple_hdmi_patch_ops = { 3260 .build_controls = simple_playback_build_controls, 3261 .build_pcms = simple_playback_build_pcms, 3262 .init = simple_playback_init, 3263 .free = simple_playback_free, 3264 .unsol_event = simple_hdmi_unsol_event, 3265 }; 3266 3267 static int patch_simple_hdmi(struct hda_codec *codec, 3268 hda_nid_t cvt_nid, hda_nid_t pin_nid) 3269 { 3270 struct hdmi_spec *spec; 3271 struct hdmi_spec_per_cvt *per_cvt; 3272 struct hdmi_spec_per_pin *per_pin; 3273 3274 spec = kzalloc(sizeof(*spec), GFP_KERNEL); 3275 if (!spec) 3276 return -ENOMEM; 3277 3278 spec->codec = codec; 3279 codec->spec = spec; 3280 hdmi_array_init(spec, 1); 3281 3282 spec->multiout.num_dacs = 0; /* no analog */ 3283 spec->multiout.max_channels = 2; 3284 spec->multiout.dig_out_nid = cvt_nid; 3285 spec->num_cvts = 1; 3286 spec->num_pins = 1; 3287 per_pin = snd_array_new(&spec->pins); 3288 per_cvt = snd_array_new(&spec->cvts); 3289 if (!per_pin || !per_cvt) { 3290 simple_playback_free(codec); 3291 return -ENOMEM; 3292 } 3293 per_cvt->cvt_nid = cvt_nid; 3294 per_pin->pin_nid = pin_nid; 3295 spec->pcm_playback = simple_pcm_playback; 3296 3297 codec->patch_ops = simple_hdmi_patch_ops; 3298 3299 return 0; 3300 } 3301 3302 static void nvhdmi_8ch_7x_set_info_frame_parameters(struct hda_codec *codec, 3303 int channels) 3304 { 3305 unsigned int chanmask; 3306 int chan = channels ? (channels - 1) : 1; 3307 3308 switch (channels) { 3309 default: 3310 case 0: 3311 case 2: 3312 chanmask = 0x00; 3313 break; 3314 case 4: 3315 chanmask = 0x08; 3316 break; 3317 case 6: 3318 chanmask = 0x0b; 3319 break; 3320 case 8: 3321 chanmask = 0x13; 3322 break; 3323 } 3324 3325 /* Set the audio infoframe channel allocation and checksum fields. The 3326 * channel count is computed implicitly by the hardware. */ 3327 snd_hda_codec_write(codec, 0x1, 0, 3328 Nv_VERB_SET_Channel_Allocation, chanmask); 3329 3330 snd_hda_codec_write(codec, 0x1, 0, 3331 Nv_VERB_SET_Info_Frame_Checksum, 3332 (0x71 - chan - chanmask)); 3333 } 3334 3335 static int nvhdmi_8ch_7x_pcm_close(struct hda_pcm_stream *hinfo, 3336 struct hda_codec *codec, 3337 struct snd_pcm_substream *substream) 3338 { 3339 struct hdmi_spec *spec = codec->spec; 3340 int i; 3341 3342 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 3343 0, AC_VERB_SET_CHANNEL_STREAMID, 0); 3344 for (i = 0; i < 4; i++) { 3345 /* set the stream id */ 3346 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0, 3347 AC_VERB_SET_CHANNEL_STREAMID, 0); 3348 /* set the stream format */ 3349 snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0, 3350 AC_VERB_SET_STREAM_FORMAT, 0); 3351 } 3352 3353 /* The audio hardware sends a channel count of 0x7 (8ch) when all the 3354 * streams are disabled. */ 3355 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8); 3356 3357 return snd_hda_multi_out_dig_close(codec, &spec->multiout); 3358 } 3359 3360 static int nvhdmi_8ch_7x_pcm_prepare(struct hda_pcm_stream *hinfo, 3361 struct hda_codec *codec, 3362 unsigned int stream_tag, 3363 unsigned int format, 3364 struct snd_pcm_substream *substream) 3365 { 3366 int chs; 3367 unsigned int dataDCC2, channel_id; 3368 int i; 3369 struct hdmi_spec *spec = codec->spec; 3370 struct hda_spdif_out *spdif; 3371 struct hdmi_spec_per_cvt *per_cvt; 3372 3373 mutex_lock(&codec->spdif_mutex); 3374 per_cvt = get_cvt(spec, 0); 3375 spdif = snd_hda_spdif_out_of_nid(codec, per_cvt->cvt_nid); 3376 3377 chs = substream->runtime->channels; 3378 3379 dataDCC2 = 0x2; 3380 3381 /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */ 3382 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE)) 3383 snd_hda_codec_write(codec, 3384 nvhdmi_master_con_nid_7x, 3385 0, 3386 AC_VERB_SET_DIGI_CONVERT_1, 3387 spdif->ctls & ~AC_DIG1_ENABLE & 0xff); 3388 3389 /* set the stream id */ 3390 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0, 3391 AC_VERB_SET_CHANNEL_STREAMID, (stream_tag << 4) | 0x0); 3392 3393 /* set the stream format */ 3394 snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0, 3395 AC_VERB_SET_STREAM_FORMAT, format); 3396 3397 /* turn on again (if needed) */ 3398 /* enable and set the channel status audio/data flag */ 3399 if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE)) { 3400 snd_hda_codec_write(codec, 3401 nvhdmi_master_con_nid_7x, 3402 0, 3403 AC_VERB_SET_DIGI_CONVERT_1, 3404 spdif->ctls & 0xff); 3405 snd_hda_codec_write(codec, 3406 nvhdmi_master_con_nid_7x, 3407 0, 3408 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2); 3409 } 3410 3411 for (i = 0; i < 4; i++) { 3412 if (chs == 2) 3413 channel_id = 0; 3414 else 3415 channel_id = i * 2; 3416 3417 /* turn off SPDIF once; 3418 *otherwise the IEC958 bits won't be updated 3419 */ 3420 if (codec->spdif_status_reset && 3421 (spdif->ctls & AC_DIG1_ENABLE)) 3422 snd_hda_codec_write(codec, 3423 nvhdmi_con_nids_7x[i], 3424 0, 3425 AC_VERB_SET_DIGI_CONVERT_1, 3426 spdif->ctls & ~AC_DIG1_ENABLE & 0xff); 3427 /* set the stream id */ 3428 snd_hda_codec_write(codec, 3429 nvhdmi_con_nids_7x[i], 3430 0, 3431 AC_VERB_SET_CHANNEL_STREAMID, 3432 (stream_tag << 4) | channel_id); 3433 /* set the stream format */ 3434 snd_hda_codec_write(codec, 3435 nvhdmi_con_nids_7x[i], 3436 0, 3437 AC_VERB_SET_STREAM_FORMAT, 3438 format); 3439 /* turn on again (if needed) */ 3440 /* enable and set the channel status audio/data flag */ 3441 if (codec->spdif_status_reset && 3442 (spdif->ctls & AC_DIG1_ENABLE)) { 3443 snd_hda_codec_write(codec, 3444 nvhdmi_con_nids_7x[i], 3445 0, 3446 AC_VERB_SET_DIGI_CONVERT_1, 3447 spdif->ctls & 0xff); 3448 snd_hda_codec_write(codec, 3449 nvhdmi_con_nids_7x[i], 3450 0, 3451 AC_VERB_SET_DIGI_CONVERT_2, dataDCC2); 3452 } 3453 } 3454 3455 nvhdmi_8ch_7x_set_info_frame_parameters(codec, chs); 3456 3457 mutex_unlock(&codec->spdif_mutex); 3458 return 0; 3459 } 3460 3461 static const struct hda_pcm_stream nvhdmi_pcm_playback_8ch_7x = { 3462 .substreams = 1, 3463 .channels_min = 2, 3464 .channels_max = 8, 3465 .nid = nvhdmi_master_con_nid_7x, 3466 .rates = SUPPORTED_RATES, 3467 .maxbps = SUPPORTED_MAXBPS, 3468 .formats = SUPPORTED_FORMATS, 3469 .ops = { 3470 .open = simple_playback_pcm_open, 3471 .close = nvhdmi_8ch_7x_pcm_close, 3472 .prepare = nvhdmi_8ch_7x_pcm_prepare 3473 }, 3474 }; 3475 3476 static int patch_nvhdmi_2ch(struct hda_codec *codec) 3477 { 3478 struct hdmi_spec *spec; 3479 int err = patch_simple_hdmi(codec, nvhdmi_master_con_nid_7x, 3480 nvhdmi_master_pin_nid_7x); 3481 if (err < 0) 3482 return err; 3483 3484 codec->patch_ops.init = nvhdmi_7x_init_2ch; 3485 /* override the PCM rates, etc, as the codec doesn't give full list */ 3486 spec = codec->spec; 3487 spec->pcm_playback.rates = SUPPORTED_RATES; 3488 spec->pcm_playback.maxbps = SUPPORTED_MAXBPS; 3489 spec->pcm_playback.formats = SUPPORTED_FORMATS; 3490 return 0; 3491 } 3492 3493 static int nvhdmi_7x_8ch_build_pcms(struct hda_codec *codec) 3494 { 3495 struct hdmi_spec *spec = codec->spec; 3496 int err = simple_playback_build_pcms(codec); 3497 if (!err) { 3498 struct hda_pcm *info = get_pcm_rec(spec, 0); 3499 info->own_chmap = true; 3500 } 3501 return err; 3502 } 3503 3504 static int nvhdmi_7x_8ch_build_controls(struct hda_codec *codec) 3505 { 3506 struct hdmi_spec *spec = codec->spec; 3507 struct hda_pcm *info; 3508 struct snd_pcm_chmap *chmap; 3509 int err; 3510 3511 err = simple_playback_build_controls(codec); 3512 if (err < 0) 3513 return err; 3514 3515 /* add channel maps */ 3516 info = get_pcm_rec(spec, 0); 3517 err = snd_pcm_add_chmap_ctls(info->pcm, 3518 SNDRV_PCM_STREAM_PLAYBACK, 3519 snd_pcm_alt_chmaps, 8, 0, &chmap); 3520 if (err < 0) 3521 return err; 3522 switch (codec->preset->vendor_id) { 3523 case 0x10de0002: 3524 case 0x10de0003: 3525 case 0x10de0005: 3526 case 0x10de0006: 3527 chmap->channel_mask = (1U << 2) | (1U << 8); 3528 break; 3529 case 0x10de0007: 3530 chmap->channel_mask = (1U << 2) | (1U << 6) | (1U << 8); 3531 } 3532 return 0; 3533 } 3534 3535 static int patch_nvhdmi_8ch_7x(struct hda_codec *codec) 3536 { 3537 struct hdmi_spec *spec; 3538 int err = patch_nvhdmi_2ch(codec); 3539 if (err < 0) 3540 return err; 3541 spec = codec->spec; 3542 spec->multiout.max_channels = 8; 3543 spec->pcm_playback = nvhdmi_pcm_playback_8ch_7x; 3544 codec->patch_ops.init = nvhdmi_7x_init_8ch; 3545 codec->patch_ops.build_pcms = nvhdmi_7x_8ch_build_pcms; 3546 codec->patch_ops.build_controls = nvhdmi_7x_8ch_build_controls; 3547 3548 /* Initialize the audio infoframe channel mask and checksum to something 3549 * valid */ 3550 nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8); 3551 3552 return 0; 3553 } 3554 3555 /* 3556 * NVIDIA codecs ignore ASP mapping for 2ch - confirmed on: 3557 * - 0x10de0015 3558 * - 0x10de0040 3559 */ 3560 static int nvhdmi_chmap_cea_alloc_validate_get_type(struct hdac_chmap *chmap, 3561 struct hdac_cea_channel_speaker_allocation *cap, int channels) 3562 { 3563 if (cap->ca_index == 0x00 && channels == 2) 3564 return SNDRV_CTL_TLVT_CHMAP_FIXED; 3565 3566 /* If the speaker allocation matches the channel count, it is OK. */ 3567 if (cap->channels != channels) 3568 return -1; 3569 3570 /* all channels are remappable freely */ 3571 return SNDRV_CTL_TLVT_CHMAP_VAR; 3572 } 3573 3574 static int nvhdmi_chmap_validate(struct hdac_chmap *chmap, 3575 int ca, int chs, unsigned char *map) 3576 { 3577 if (ca == 0x00 && (map[0] != SNDRV_CHMAP_FL || map[1] != SNDRV_CHMAP_FR)) 3578 return -EINVAL; 3579 3580 return 0; 3581 } 3582 3583 /* map from pin NID to port; port is 0-based */ 3584 /* for Nvidia: assume widget NID starting from 4, with step 1 (4, 5, 6, ...) */ 3585 static int nvhdmi_pin2port(void *audio_ptr, int pin_nid) 3586 { 3587 return pin_nid - 4; 3588 } 3589 3590 /* reverse-map from port to pin NID: see above */ 3591 static int nvhdmi_port2pin(struct hda_codec *codec, int port) 3592 { 3593 return port + 4; 3594 } 3595 3596 static const struct drm_audio_component_audio_ops nvhdmi_audio_ops = { 3597 .pin2port = nvhdmi_pin2port, 3598 .pin_eld_notify = generic_acomp_pin_eld_notify, 3599 .master_bind = generic_acomp_master_bind, 3600 .master_unbind = generic_acomp_master_unbind, 3601 }; 3602 3603 static int patch_nvhdmi(struct hda_codec *codec) 3604 { 3605 struct hdmi_spec *spec; 3606 int err; 3607 3608 err = alloc_generic_hdmi(codec); 3609 if (err < 0) 3610 return err; 3611 codec->dp_mst = true; 3612 3613 spec = codec->spec; 3614 spec->dyn_pcm_assign = true; 3615 3616 err = hdmi_parse_codec(codec); 3617 if (err < 0) { 3618 generic_spec_free(codec); 3619 return err; 3620 } 3621 3622 generic_hdmi_init_per_pins(codec); 3623 3624 spec->dyn_pin_out = true; 3625 3626 spec->chmap.ops.chmap_cea_alloc_validate_get_type = 3627 nvhdmi_chmap_cea_alloc_validate_get_type; 3628 spec->chmap.ops.chmap_validate = nvhdmi_chmap_validate; 3629 3630 codec->link_down_at_suspend = 1; 3631 3632 generic_acomp_init(codec, &nvhdmi_audio_ops, nvhdmi_port2pin); 3633 3634 return 0; 3635 } 3636 3637 static int patch_nvhdmi_legacy(struct hda_codec *codec) 3638 { 3639 struct hdmi_spec *spec; 3640 int err; 3641 3642 err = patch_generic_hdmi(codec); 3643 if (err) 3644 return err; 3645 3646 spec = codec->spec; 3647 spec->dyn_pin_out = true; 3648 3649 spec->chmap.ops.chmap_cea_alloc_validate_get_type = 3650 nvhdmi_chmap_cea_alloc_validate_get_type; 3651 spec->chmap.ops.chmap_validate = nvhdmi_chmap_validate; 3652 3653 codec->link_down_at_suspend = 1; 3654 3655 return 0; 3656 } 3657 3658 /* 3659 * The HDA codec on NVIDIA Tegra contains two scratch registers that are 3660 * accessed using vendor-defined verbs. These registers can be used for 3661 * interoperability between the HDA and HDMI drivers. 3662 */ 3663 3664 /* Audio Function Group node */ 3665 #define NVIDIA_AFG_NID 0x01 3666 3667 /* 3668 * The SCRATCH0 register is used to notify the HDMI codec of changes in audio 3669 * format. On Tegra, bit 31 is used as a trigger that causes an interrupt to 3670 * be raised in the HDMI codec. The remainder of the bits is arbitrary. This 3671 * implementation stores the HDA format (see AC_FMT_*) in bits [15:0] and an 3672 * additional bit (at position 30) to signal the validity of the format. 3673 * 3674 * | 31 | 30 | 29 16 | 15 0 | 3675 * +---------+-------+--------+--------+ 3676 * | TRIGGER | VALID | UNUSED | FORMAT | 3677 * +-----------------------------------| 3678 * 3679 * Note that for the trigger bit to take effect it needs to change value 3680 * (i.e. it needs to be toggled). 3681 */ 3682 #define NVIDIA_GET_SCRATCH0 0xfa6 3683 #define NVIDIA_SET_SCRATCH0_BYTE0 0xfa7 3684 #define NVIDIA_SET_SCRATCH0_BYTE1 0xfa8 3685 #define NVIDIA_SET_SCRATCH0_BYTE2 0xfa9 3686 #define NVIDIA_SET_SCRATCH0_BYTE3 0xfaa 3687 #define NVIDIA_SCRATCH_TRIGGER (1 << 7) 3688 #define NVIDIA_SCRATCH_VALID (1 << 6) 3689 3690 #define NVIDIA_GET_SCRATCH1 0xfab 3691 #define NVIDIA_SET_SCRATCH1_BYTE0 0xfac 3692 #define NVIDIA_SET_SCRATCH1_BYTE1 0xfad 3693 #define NVIDIA_SET_SCRATCH1_BYTE2 0xfae 3694 #define NVIDIA_SET_SCRATCH1_BYTE3 0xfaf 3695 3696 /* 3697 * The format parameter is the HDA audio format (see AC_FMT_*). If set to 0, 3698 * the format is invalidated so that the HDMI codec can be disabled. 3699 */ 3700 static void tegra_hdmi_set_format(struct hda_codec *codec, unsigned int format) 3701 { 3702 unsigned int value; 3703 3704 /* bits [31:30] contain the trigger and valid bits */ 3705 value = snd_hda_codec_read(codec, NVIDIA_AFG_NID, 0, 3706 NVIDIA_GET_SCRATCH0, 0); 3707 value = (value >> 24) & 0xff; 3708 3709 /* bits [15:0] are used to store the HDA format */ 3710 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0, 3711 NVIDIA_SET_SCRATCH0_BYTE0, 3712 (format >> 0) & 0xff); 3713 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0, 3714 NVIDIA_SET_SCRATCH0_BYTE1, 3715 (format >> 8) & 0xff); 3716 3717 /* bits [16:24] are unused */ 3718 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0, 3719 NVIDIA_SET_SCRATCH0_BYTE2, 0); 3720 3721 /* 3722 * Bit 30 signals that the data is valid and hence that HDMI audio can 3723 * be enabled. 3724 */ 3725 if (format == 0) 3726 value &= ~NVIDIA_SCRATCH_VALID; 3727 else 3728 value |= NVIDIA_SCRATCH_VALID; 3729 3730 /* 3731 * Whenever the trigger bit is toggled, an interrupt is raised in the 3732 * HDMI codec. The HDMI driver will use that as trigger to update its 3733 * configuration. 3734 */ 3735 value ^= NVIDIA_SCRATCH_TRIGGER; 3736 3737 snd_hda_codec_write(codec, NVIDIA_AFG_NID, 0, 3738 NVIDIA_SET_SCRATCH0_BYTE3, value); 3739 } 3740 3741 static int tegra_hdmi_pcm_prepare(struct hda_pcm_stream *hinfo, 3742 struct hda_codec *codec, 3743 unsigned int stream_tag, 3744 unsigned int format, 3745 struct snd_pcm_substream *substream) 3746 { 3747 int err; 3748 3749 err = generic_hdmi_playback_pcm_prepare(hinfo, codec, stream_tag, 3750 format, substream); 3751 if (err < 0) 3752 return err; 3753 3754 /* notify the HDMI codec of the format change */ 3755 tegra_hdmi_set_format(codec, format); 3756 3757 return 0; 3758 } 3759 3760 static int tegra_hdmi_pcm_cleanup(struct hda_pcm_stream *hinfo, 3761 struct hda_codec *codec, 3762 struct snd_pcm_substream *substream) 3763 { 3764 /* invalidate the format in the HDMI codec */ 3765 tegra_hdmi_set_format(codec, 0); 3766 3767 return generic_hdmi_playback_pcm_cleanup(hinfo, codec, substream); 3768 } 3769 3770 static struct hda_pcm *hda_find_pcm_by_type(struct hda_codec *codec, int type) 3771 { 3772 struct hdmi_spec *spec = codec->spec; 3773 unsigned int i; 3774 3775 for (i = 0; i < spec->num_pins; i++) { 3776 struct hda_pcm *pcm = get_pcm_rec(spec, i); 3777 3778 if (pcm->pcm_type == type) 3779 return pcm; 3780 } 3781 3782 return NULL; 3783 } 3784 3785 static int tegra_hdmi_build_pcms(struct hda_codec *codec) 3786 { 3787 struct hda_pcm_stream *stream; 3788 struct hda_pcm *pcm; 3789 int err; 3790 3791 err = generic_hdmi_build_pcms(codec); 3792 if (err < 0) 3793 return err; 3794 3795 pcm = hda_find_pcm_by_type(codec, HDA_PCM_TYPE_HDMI); 3796 if (!pcm) 3797 return -ENODEV; 3798 3799 /* 3800 * Override ->prepare() and ->cleanup() operations to notify the HDMI 3801 * codec about format changes. 3802 */ 3803 stream = &pcm->stream[SNDRV_PCM_STREAM_PLAYBACK]; 3804 stream->ops.prepare = tegra_hdmi_pcm_prepare; 3805 stream->ops.cleanup = tegra_hdmi_pcm_cleanup; 3806 3807 return 0; 3808 } 3809 3810 static int patch_tegra_hdmi(struct hda_codec *codec) 3811 { 3812 struct hdmi_spec *spec; 3813 int err; 3814 3815 err = patch_generic_hdmi(codec); 3816 if (err) 3817 return err; 3818 3819 codec->patch_ops.build_pcms = tegra_hdmi_build_pcms; 3820 spec = codec->spec; 3821 spec->chmap.ops.chmap_cea_alloc_validate_get_type = 3822 nvhdmi_chmap_cea_alloc_validate_get_type; 3823 spec->chmap.ops.chmap_validate = nvhdmi_chmap_validate; 3824 3825 return 0; 3826 } 3827 3828 /* 3829 * ATI/AMD-specific implementations 3830 */ 3831 3832 #define is_amdhdmi_rev3_or_later(codec) \ 3833 ((codec)->core.vendor_id == 0x1002aa01 && \ 3834 ((codec)->core.revision_id & 0xff00) >= 0x0300) 3835 #define has_amd_full_remap_support(codec) is_amdhdmi_rev3_or_later(codec) 3836 3837 /* ATI/AMD specific HDA pin verbs, see the AMD HDA Verbs specification */ 3838 #define ATI_VERB_SET_CHANNEL_ALLOCATION 0x771 3839 #define ATI_VERB_SET_DOWNMIX_INFO 0x772 3840 #define ATI_VERB_SET_MULTICHANNEL_01 0x777 3841 #define ATI_VERB_SET_MULTICHANNEL_23 0x778 3842 #define ATI_VERB_SET_MULTICHANNEL_45 0x779 3843 #define ATI_VERB_SET_MULTICHANNEL_67 0x77a 3844 #define ATI_VERB_SET_HBR_CONTROL 0x77c 3845 #define ATI_VERB_SET_MULTICHANNEL_1 0x785 3846 #define ATI_VERB_SET_MULTICHANNEL_3 0x786 3847 #define ATI_VERB_SET_MULTICHANNEL_5 0x787 3848 #define ATI_VERB_SET_MULTICHANNEL_7 0x788 3849 #define ATI_VERB_SET_MULTICHANNEL_MODE 0x789 3850 #define ATI_VERB_GET_CHANNEL_ALLOCATION 0xf71 3851 #define ATI_VERB_GET_DOWNMIX_INFO 0xf72 3852 #define ATI_VERB_GET_MULTICHANNEL_01 0xf77 3853 #define ATI_VERB_GET_MULTICHANNEL_23 0xf78 3854 #define ATI_VERB_GET_MULTICHANNEL_45 0xf79 3855 #define ATI_VERB_GET_MULTICHANNEL_67 0xf7a 3856 #define ATI_VERB_GET_HBR_CONTROL 0xf7c 3857 #define ATI_VERB_GET_MULTICHANNEL_1 0xf85 3858 #define ATI_VERB_GET_MULTICHANNEL_3 0xf86 3859 #define ATI_VERB_GET_MULTICHANNEL_5 0xf87 3860 #define ATI_VERB_GET_MULTICHANNEL_7 0xf88 3861 #define ATI_VERB_GET_MULTICHANNEL_MODE 0xf89 3862 3863 /* AMD specific HDA cvt verbs */ 3864 #define ATI_VERB_SET_RAMP_RATE 0x770 3865 #define ATI_VERB_GET_RAMP_RATE 0xf70 3866 3867 #define ATI_OUT_ENABLE 0x1 3868 3869 #define ATI_MULTICHANNEL_MODE_PAIRED 0 3870 #define ATI_MULTICHANNEL_MODE_SINGLE 1 3871 3872 #define ATI_HBR_CAPABLE 0x01 3873 #define ATI_HBR_ENABLE 0x10 3874 3875 static int atihdmi_pin_get_eld(struct hda_codec *codec, hda_nid_t nid, 3876 int dev_id, unsigned char *buf, int *eld_size) 3877 { 3878 WARN_ON(dev_id != 0); 3879 /* call hda_eld.c ATI/AMD-specific function */ 3880 return snd_hdmi_get_eld_ati(codec, nid, buf, eld_size, 3881 is_amdhdmi_rev3_or_later(codec)); 3882 } 3883 3884 static void atihdmi_pin_setup_infoframe(struct hda_codec *codec, 3885 hda_nid_t pin_nid, int dev_id, int ca, 3886 int active_channels, int conn_type) 3887 { 3888 WARN_ON(dev_id != 0); 3889 snd_hda_codec_write(codec, pin_nid, 0, ATI_VERB_SET_CHANNEL_ALLOCATION, ca); 3890 } 3891 3892 static int atihdmi_paired_swap_fc_lfe(int pos) 3893 { 3894 /* 3895 * ATI/AMD have automatic FC/LFE swap built-in 3896 * when in pairwise mapping mode. 3897 */ 3898 3899 switch (pos) { 3900 /* see channel_allocations[].speakers[] */ 3901 case 2: return 3; 3902 case 3: return 2; 3903 default: break; 3904 } 3905 3906 return pos; 3907 } 3908 3909 static int atihdmi_paired_chmap_validate(struct hdac_chmap *chmap, 3910 int ca, int chs, unsigned char *map) 3911 { 3912 struct hdac_cea_channel_speaker_allocation *cap; 3913 int i, j; 3914 3915 /* check that only channel pairs need to be remapped on old pre-rev3 ATI/AMD */ 3916 3917 cap = snd_hdac_get_ch_alloc_from_ca(ca); 3918 for (i = 0; i < chs; ++i) { 3919 int mask = snd_hdac_chmap_to_spk_mask(map[i]); 3920 bool ok = false; 3921 bool companion_ok = false; 3922 3923 if (!mask) 3924 continue; 3925 3926 for (j = 0 + i % 2; j < 8; j += 2) { 3927 int chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j); 3928 if (cap->speakers[chan_idx] == mask) { 3929 /* channel is in a supported position */ 3930 ok = true; 3931 3932 if (i % 2 == 0 && i + 1 < chs) { 3933 /* even channel, check the odd companion */ 3934 int comp_chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j + 1); 3935 int comp_mask_req = snd_hdac_chmap_to_spk_mask(map[i+1]); 3936 int comp_mask_act = cap->speakers[comp_chan_idx]; 3937 3938 if (comp_mask_req == comp_mask_act) 3939 companion_ok = true; 3940 else 3941 return -EINVAL; 3942 } 3943 break; 3944 } 3945 } 3946 3947 if (!ok) 3948 return -EINVAL; 3949 3950 if (companion_ok) 3951 i++; /* companion channel already checked */ 3952 } 3953 3954 return 0; 3955 } 3956 3957 static int atihdmi_pin_set_slot_channel(struct hdac_device *hdac, 3958 hda_nid_t pin_nid, int hdmi_slot, int stream_channel) 3959 { 3960 struct hda_codec *codec = hdac_to_hda_codec(hdac); 3961 int verb; 3962 int ati_channel_setup = 0; 3963 3964 if (hdmi_slot > 7) 3965 return -EINVAL; 3966 3967 if (!has_amd_full_remap_support(codec)) { 3968 hdmi_slot = atihdmi_paired_swap_fc_lfe(hdmi_slot); 3969 3970 /* In case this is an odd slot but without stream channel, do not 3971 * disable the slot since the corresponding even slot could have a 3972 * channel. In case neither have a channel, the slot pair will be 3973 * disabled when this function is called for the even slot. */ 3974 if (hdmi_slot % 2 != 0 && stream_channel == 0xf) 3975 return 0; 3976 3977 hdmi_slot -= hdmi_slot % 2; 3978 3979 if (stream_channel != 0xf) 3980 stream_channel -= stream_channel % 2; 3981 } 3982 3983 verb = ATI_VERB_SET_MULTICHANNEL_01 + hdmi_slot/2 + (hdmi_slot % 2) * 0x00e; 3984 3985 /* ati_channel_setup format: [7..4] = stream_channel_id, [1] = mute, [0] = enable */ 3986 3987 if (stream_channel != 0xf) 3988 ati_channel_setup = (stream_channel << 4) | ATI_OUT_ENABLE; 3989 3990 return snd_hda_codec_write(codec, pin_nid, 0, verb, ati_channel_setup); 3991 } 3992 3993 static int atihdmi_pin_get_slot_channel(struct hdac_device *hdac, 3994 hda_nid_t pin_nid, int asp_slot) 3995 { 3996 struct hda_codec *codec = hdac_to_hda_codec(hdac); 3997 bool was_odd = false; 3998 int ati_asp_slot = asp_slot; 3999 int verb; 4000 int ati_channel_setup; 4001 4002 if (asp_slot > 7) 4003 return -EINVAL; 4004 4005 if (!has_amd_full_remap_support(codec)) { 4006 ati_asp_slot = atihdmi_paired_swap_fc_lfe(asp_slot); 4007 if (ati_asp_slot % 2 != 0) { 4008 ati_asp_slot -= 1; 4009 was_odd = true; 4010 } 4011 } 4012 4013 verb = ATI_VERB_GET_MULTICHANNEL_01 + ati_asp_slot/2 + (ati_asp_slot % 2) * 0x00e; 4014 4015 ati_channel_setup = snd_hda_codec_read(codec, pin_nid, 0, verb, 0); 4016 4017 if (!(ati_channel_setup & ATI_OUT_ENABLE)) 4018 return 0xf; 4019 4020 return ((ati_channel_setup & 0xf0) >> 4) + !!was_odd; 4021 } 4022 4023 static int atihdmi_paired_chmap_cea_alloc_validate_get_type( 4024 struct hdac_chmap *chmap, 4025 struct hdac_cea_channel_speaker_allocation *cap, 4026 int channels) 4027 { 4028 int c; 4029 4030 /* 4031 * Pre-rev3 ATI/AMD codecs operate in a paired channel mode, so 4032 * we need to take that into account (a single channel may take 2 4033 * channel slots if we need to carry a silent channel next to it). 4034 * On Rev3+ AMD codecs this function is not used. 4035 */ 4036 int chanpairs = 0; 4037 4038 /* We only produce even-numbered channel count TLVs */ 4039 if ((channels % 2) != 0) 4040 return -1; 4041 4042 for (c = 0; c < 7; c += 2) { 4043 if (cap->speakers[c] || cap->speakers[c+1]) 4044 chanpairs++; 4045 } 4046 4047 if (chanpairs * 2 != channels) 4048 return -1; 4049 4050 return SNDRV_CTL_TLVT_CHMAP_PAIRED; 4051 } 4052 4053 static void atihdmi_paired_cea_alloc_to_tlv_chmap(struct hdac_chmap *hchmap, 4054 struct hdac_cea_channel_speaker_allocation *cap, 4055 unsigned int *chmap, int channels) 4056 { 4057 /* produce paired maps for pre-rev3 ATI/AMD codecs */ 4058 int count = 0; 4059 int c; 4060 4061 for (c = 7; c >= 0; c--) { 4062 int chan = 7 - atihdmi_paired_swap_fc_lfe(7 - c); 4063 int spk = cap->speakers[chan]; 4064 if (!spk) { 4065 /* add N/A channel if the companion channel is occupied */ 4066 if (cap->speakers[chan + (chan % 2 ? -1 : 1)]) 4067 chmap[count++] = SNDRV_CHMAP_NA; 4068 4069 continue; 4070 } 4071 4072 chmap[count++] = snd_hdac_spk_to_chmap(spk); 4073 } 4074 4075 WARN_ON(count != channels); 4076 } 4077 4078 static int atihdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid, 4079 int dev_id, bool hbr) 4080 { 4081 int hbr_ctl, hbr_ctl_new; 4082 4083 WARN_ON(dev_id != 0); 4084 4085 hbr_ctl = snd_hda_codec_read(codec, pin_nid, 0, ATI_VERB_GET_HBR_CONTROL, 0); 4086 if (hbr_ctl >= 0 && (hbr_ctl & ATI_HBR_CAPABLE)) { 4087 if (hbr) 4088 hbr_ctl_new = hbr_ctl | ATI_HBR_ENABLE; 4089 else 4090 hbr_ctl_new = hbr_ctl & ~ATI_HBR_ENABLE; 4091 4092 codec_dbg(codec, 4093 "atihdmi_pin_hbr_setup: NID=0x%x, %shbr-ctl=0x%x\n", 4094 pin_nid, 4095 hbr_ctl == hbr_ctl_new ? "" : "new-", 4096 hbr_ctl_new); 4097 4098 if (hbr_ctl != hbr_ctl_new) 4099 snd_hda_codec_write(codec, pin_nid, 0, 4100 ATI_VERB_SET_HBR_CONTROL, 4101 hbr_ctl_new); 4102 4103 } else if (hbr) 4104 return -EINVAL; 4105 4106 return 0; 4107 } 4108 4109 static int atihdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid, 4110 hda_nid_t pin_nid, int dev_id, 4111 u32 stream_tag, int format) 4112 { 4113 if (is_amdhdmi_rev3_or_later(codec)) { 4114 int ramp_rate = 180; /* default as per AMD spec */ 4115 /* disable ramp-up/down for non-pcm as per AMD spec */ 4116 if (format & AC_FMT_TYPE_NON_PCM) 4117 ramp_rate = 0; 4118 4119 snd_hda_codec_write(codec, cvt_nid, 0, ATI_VERB_SET_RAMP_RATE, ramp_rate); 4120 } 4121 4122 return hdmi_setup_stream(codec, cvt_nid, pin_nid, dev_id, 4123 stream_tag, format); 4124 } 4125 4126 4127 static int atihdmi_init(struct hda_codec *codec) 4128 { 4129 struct hdmi_spec *spec = codec->spec; 4130 int pin_idx, err; 4131 4132 err = generic_hdmi_init(codec); 4133 4134 if (err) 4135 return err; 4136 4137 for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) { 4138 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx); 4139 4140 /* make sure downmix information in infoframe is zero */ 4141 snd_hda_codec_write(codec, per_pin->pin_nid, 0, ATI_VERB_SET_DOWNMIX_INFO, 0); 4142 4143 /* enable channel-wise remap mode if supported */ 4144 if (has_amd_full_remap_support(codec)) 4145 snd_hda_codec_write(codec, per_pin->pin_nid, 0, 4146 ATI_VERB_SET_MULTICHANNEL_MODE, 4147 ATI_MULTICHANNEL_MODE_SINGLE); 4148 } 4149 codec->auto_runtime_pm = 1; 4150 4151 return 0; 4152 } 4153 4154 /* map from pin NID to port; port is 0-based */ 4155 /* for AMD: assume widget NID starting from 3, with step 2 (3, 5, 7, ...) */ 4156 static int atihdmi_pin2port(void *audio_ptr, int pin_nid) 4157 { 4158 return pin_nid / 2 - 1; 4159 } 4160 4161 /* reverse-map from port to pin NID: see above */ 4162 static int atihdmi_port2pin(struct hda_codec *codec, int port) 4163 { 4164 return port * 2 + 3; 4165 } 4166 4167 static const struct drm_audio_component_audio_ops atihdmi_audio_ops = { 4168 .pin2port = atihdmi_pin2port, 4169 .pin_eld_notify = generic_acomp_pin_eld_notify, 4170 .master_bind = generic_acomp_master_bind, 4171 .master_unbind = generic_acomp_master_unbind, 4172 }; 4173 4174 static int patch_atihdmi(struct hda_codec *codec) 4175 { 4176 struct hdmi_spec *spec; 4177 struct hdmi_spec_per_cvt *per_cvt; 4178 int err, cvt_idx; 4179 4180 err = patch_generic_hdmi(codec); 4181 4182 if (err) 4183 return err; 4184 4185 codec->patch_ops.init = atihdmi_init; 4186 4187 spec = codec->spec; 4188 4189 spec->ops.pin_get_eld = atihdmi_pin_get_eld; 4190 spec->ops.pin_setup_infoframe = atihdmi_pin_setup_infoframe; 4191 spec->ops.pin_hbr_setup = atihdmi_pin_hbr_setup; 4192 spec->ops.setup_stream = atihdmi_setup_stream; 4193 4194 spec->chmap.ops.pin_get_slot_channel = atihdmi_pin_get_slot_channel; 4195 spec->chmap.ops.pin_set_slot_channel = atihdmi_pin_set_slot_channel; 4196 4197 if (!has_amd_full_remap_support(codec)) { 4198 /* override to ATI/AMD-specific versions with pairwise mapping */ 4199 spec->chmap.ops.chmap_cea_alloc_validate_get_type = 4200 atihdmi_paired_chmap_cea_alloc_validate_get_type; 4201 spec->chmap.ops.cea_alloc_to_tlv_chmap = 4202 atihdmi_paired_cea_alloc_to_tlv_chmap; 4203 spec->chmap.ops.chmap_validate = atihdmi_paired_chmap_validate; 4204 } 4205 4206 /* ATI/AMD converters do not advertise all of their capabilities */ 4207 for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) { 4208 per_cvt = get_cvt(spec, cvt_idx); 4209 per_cvt->channels_max = max(per_cvt->channels_max, 8u); 4210 per_cvt->rates |= SUPPORTED_RATES; 4211 per_cvt->formats |= SUPPORTED_FORMATS; 4212 per_cvt->maxbps = max(per_cvt->maxbps, 24u); 4213 } 4214 4215 spec->chmap.channels_max = max(spec->chmap.channels_max, 8u); 4216 4217 /* AMD GPUs have neither EPSS nor CLKSTOP bits, hence preventing 4218 * the link-down as is. Tell the core to allow it. 4219 */ 4220 codec->link_down_at_suspend = 1; 4221 4222 generic_acomp_init(codec, &atihdmi_audio_ops, atihdmi_port2pin); 4223 4224 return 0; 4225 } 4226 4227 /* VIA HDMI Implementation */ 4228 #define VIAHDMI_CVT_NID 0x02 /* audio converter1 */ 4229 #define VIAHDMI_PIN_NID 0x03 /* HDMI output pin1 */ 4230 4231 static int patch_via_hdmi(struct hda_codec *codec) 4232 { 4233 return patch_simple_hdmi(codec, VIAHDMI_CVT_NID, VIAHDMI_PIN_NID); 4234 } 4235 4236 /* 4237 * patch entries 4238 */ 4239 static const struct hda_device_id snd_hda_id_hdmi[] = { 4240 HDA_CODEC_ENTRY(0x1002793c, "RS600 HDMI", patch_atihdmi), 4241 HDA_CODEC_ENTRY(0x10027919, "RS600 HDMI", patch_atihdmi), 4242 HDA_CODEC_ENTRY(0x1002791a, "RS690/780 HDMI", patch_atihdmi), 4243 HDA_CODEC_ENTRY(0x1002aa01, "R6xx HDMI", patch_atihdmi), 4244 HDA_CODEC_ENTRY(0x10951390, "SiI1390 HDMI", patch_generic_hdmi), 4245 HDA_CODEC_ENTRY(0x10951392, "SiI1392 HDMI", patch_generic_hdmi), 4246 HDA_CODEC_ENTRY(0x17e80047, "Chrontel HDMI", patch_generic_hdmi), 4247 HDA_CODEC_ENTRY(0x10de0001, "MCP73 HDMI", patch_nvhdmi_2ch), 4248 HDA_CODEC_ENTRY(0x10de0002, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x), 4249 HDA_CODEC_ENTRY(0x10de0003, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x), 4250 HDA_CODEC_ENTRY(0x10de0004, "GPU 04 HDMI", patch_nvhdmi_8ch_7x), 4251 HDA_CODEC_ENTRY(0x10de0005, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x), 4252 HDA_CODEC_ENTRY(0x10de0006, "MCP77/78 HDMI", patch_nvhdmi_8ch_7x), 4253 HDA_CODEC_ENTRY(0x10de0007, "MCP79/7A HDMI", patch_nvhdmi_8ch_7x), 4254 HDA_CODEC_ENTRY(0x10de0008, "GPU 08 HDMI/DP", patch_nvhdmi_legacy), 4255 HDA_CODEC_ENTRY(0x10de0009, "GPU 09 HDMI/DP", patch_nvhdmi_legacy), 4256 HDA_CODEC_ENTRY(0x10de000a, "GPU 0a HDMI/DP", patch_nvhdmi_legacy), 4257 HDA_CODEC_ENTRY(0x10de000b, "GPU 0b HDMI/DP", patch_nvhdmi_legacy), 4258 HDA_CODEC_ENTRY(0x10de000c, "MCP89 HDMI", patch_nvhdmi_legacy), 4259 HDA_CODEC_ENTRY(0x10de000d, "GPU 0d HDMI/DP", patch_nvhdmi_legacy), 4260 HDA_CODEC_ENTRY(0x10de0010, "GPU 10 HDMI/DP", patch_nvhdmi_legacy), 4261 HDA_CODEC_ENTRY(0x10de0011, "GPU 11 HDMI/DP", patch_nvhdmi_legacy), 4262 HDA_CODEC_ENTRY(0x10de0012, "GPU 12 HDMI/DP", patch_nvhdmi_legacy), 4263 HDA_CODEC_ENTRY(0x10de0013, "GPU 13 HDMI/DP", patch_nvhdmi_legacy), 4264 HDA_CODEC_ENTRY(0x10de0014, "GPU 14 HDMI/DP", patch_nvhdmi_legacy), 4265 HDA_CODEC_ENTRY(0x10de0015, "GPU 15 HDMI/DP", patch_nvhdmi_legacy), 4266 HDA_CODEC_ENTRY(0x10de0016, "GPU 16 HDMI/DP", patch_nvhdmi_legacy), 4267 /* 17 is known to be absent */ 4268 HDA_CODEC_ENTRY(0x10de0018, "GPU 18 HDMI/DP", patch_nvhdmi_legacy), 4269 HDA_CODEC_ENTRY(0x10de0019, "GPU 19 HDMI/DP", patch_nvhdmi_legacy), 4270 HDA_CODEC_ENTRY(0x10de001a, "GPU 1a HDMI/DP", patch_nvhdmi_legacy), 4271 HDA_CODEC_ENTRY(0x10de001b, "GPU 1b HDMI/DP", patch_nvhdmi_legacy), 4272 HDA_CODEC_ENTRY(0x10de001c, "GPU 1c HDMI/DP", patch_nvhdmi_legacy), 4273 HDA_CODEC_ENTRY(0x10de0020, "Tegra30 HDMI", patch_tegra_hdmi), 4274 HDA_CODEC_ENTRY(0x10de0022, "Tegra114 HDMI", patch_tegra_hdmi), 4275 HDA_CODEC_ENTRY(0x10de0028, "Tegra124 HDMI", patch_tegra_hdmi), 4276 HDA_CODEC_ENTRY(0x10de0029, "Tegra210 HDMI/DP", patch_tegra_hdmi), 4277 HDA_CODEC_ENTRY(0x10de002d, "Tegra186 HDMI/DP0", patch_tegra_hdmi), 4278 HDA_CODEC_ENTRY(0x10de002e, "Tegra186 HDMI/DP1", patch_tegra_hdmi), 4279 HDA_CODEC_ENTRY(0x10de002f, "Tegra194 HDMI/DP2", patch_tegra_hdmi), 4280 HDA_CODEC_ENTRY(0x10de0030, "Tegra194 HDMI/DP3", patch_tegra_hdmi), 4281 HDA_CODEC_ENTRY(0x10de0040, "GPU 40 HDMI/DP", patch_nvhdmi), 4282 HDA_CODEC_ENTRY(0x10de0041, "GPU 41 HDMI/DP", patch_nvhdmi), 4283 HDA_CODEC_ENTRY(0x10de0042, "GPU 42 HDMI/DP", patch_nvhdmi), 4284 HDA_CODEC_ENTRY(0x10de0043, "GPU 43 HDMI/DP", patch_nvhdmi), 4285 HDA_CODEC_ENTRY(0x10de0044, "GPU 44 HDMI/DP", patch_nvhdmi), 4286 HDA_CODEC_ENTRY(0x10de0045, "GPU 45 HDMI/DP", patch_nvhdmi), 4287 HDA_CODEC_ENTRY(0x10de0050, "GPU 50 HDMI/DP", patch_nvhdmi), 4288 HDA_CODEC_ENTRY(0x10de0051, "GPU 51 HDMI/DP", patch_nvhdmi), 4289 HDA_CODEC_ENTRY(0x10de0052, "GPU 52 HDMI/DP", patch_nvhdmi), 4290 HDA_CODEC_ENTRY(0x10de0060, "GPU 60 HDMI/DP", patch_nvhdmi), 4291 HDA_CODEC_ENTRY(0x10de0061, "GPU 61 HDMI/DP", patch_nvhdmi), 4292 HDA_CODEC_ENTRY(0x10de0062, "GPU 62 HDMI/DP", patch_nvhdmi), 4293 HDA_CODEC_ENTRY(0x10de0067, "MCP67 HDMI", patch_nvhdmi_2ch), 4294 HDA_CODEC_ENTRY(0x10de0070, "GPU 70 HDMI/DP", patch_nvhdmi), 4295 HDA_CODEC_ENTRY(0x10de0071, "GPU 71 HDMI/DP", patch_nvhdmi), 4296 HDA_CODEC_ENTRY(0x10de0072, "GPU 72 HDMI/DP", patch_nvhdmi), 4297 HDA_CODEC_ENTRY(0x10de0073, "GPU 73 HDMI/DP", patch_nvhdmi), 4298 HDA_CODEC_ENTRY(0x10de0074, "GPU 74 HDMI/DP", patch_nvhdmi), 4299 HDA_CODEC_ENTRY(0x10de0076, "GPU 76 HDMI/DP", patch_nvhdmi), 4300 HDA_CODEC_ENTRY(0x10de007b, "GPU 7b HDMI/DP", patch_nvhdmi), 4301 HDA_CODEC_ENTRY(0x10de007c, "GPU 7c HDMI/DP", patch_nvhdmi), 4302 HDA_CODEC_ENTRY(0x10de007d, "GPU 7d HDMI/DP", patch_nvhdmi), 4303 HDA_CODEC_ENTRY(0x10de007e, "GPU 7e HDMI/DP", patch_nvhdmi), 4304 HDA_CODEC_ENTRY(0x10de0080, "GPU 80 HDMI/DP", patch_nvhdmi), 4305 HDA_CODEC_ENTRY(0x10de0081, "GPU 81 HDMI/DP", patch_nvhdmi), 4306 HDA_CODEC_ENTRY(0x10de0082, "GPU 82 HDMI/DP", patch_nvhdmi), 4307 HDA_CODEC_ENTRY(0x10de0083, "GPU 83 HDMI/DP", patch_nvhdmi), 4308 HDA_CODEC_ENTRY(0x10de0084, "GPU 84 HDMI/DP", patch_nvhdmi), 4309 HDA_CODEC_ENTRY(0x10de0090, "GPU 90 HDMI/DP", patch_nvhdmi), 4310 HDA_CODEC_ENTRY(0x10de0091, "GPU 91 HDMI/DP", patch_nvhdmi), 4311 HDA_CODEC_ENTRY(0x10de0092, "GPU 92 HDMI/DP", patch_nvhdmi), 4312 HDA_CODEC_ENTRY(0x10de0093, "GPU 93 HDMI/DP", patch_nvhdmi), 4313 HDA_CODEC_ENTRY(0x10de0094, "GPU 94 HDMI/DP", patch_nvhdmi), 4314 HDA_CODEC_ENTRY(0x10de0095, "GPU 95 HDMI/DP", patch_nvhdmi), 4315 HDA_CODEC_ENTRY(0x10de0097, "GPU 97 HDMI/DP", patch_nvhdmi), 4316 HDA_CODEC_ENTRY(0x10de0098, "GPU 98 HDMI/DP", patch_nvhdmi), 4317 HDA_CODEC_ENTRY(0x10de0099, "GPU 99 HDMI/DP", patch_nvhdmi), 4318 HDA_CODEC_ENTRY(0x10de009a, "GPU 9a HDMI/DP", patch_nvhdmi), 4319 HDA_CODEC_ENTRY(0x10de009d, "GPU 9d HDMI/DP", patch_nvhdmi), 4320 HDA_CODEC_ENTRY(0x10de009e, "GPU 9e HDMI/DP", patch_nvhdmi), 4321 HDA_CODEC_ENTRY(0x10de009f, "GPU 9f HDMI/DP", patch_nvhdmi), 4322 HDA_CODEC_ENTRY(0x10de00a0, "GPU a0 HDMI/DP", patch_nvhdmi), 4323 HDA_CODEC_ENTRY(0x10de8001, "MCP73 HDMI", patch_nvhdmi_2ch), 4324 HDA_CODEC_ENTRY(0x10de8067, "MCP67/68 HDMI", patch_nvhdmi_2ch), 4325 HDA_CODEC_ENTRY(0x11069f80, "VX900 HDMI/DP", patch_via_hdmi), 4326 HDA_CODEC_ENTRY(0x11069f81, "VX900 HDMI/DP", patch_via_hdmi), 4327 HDA_CODEC_ENTRY(0x11069f84, "VX11 HDMI/DP", patch_generic_hdmi), 4328 HDA_CODEC_ENTRY(0x11069f85, "VX11 HDMI/DP", patch_generic_hdmi), 4329 HDA_CODEC_ENTRY(0x80860054, "IbexPeak HDMI", patch_i915_cpt_hdmi), 4330 HDA_CODEC_ENTRY(0x80862800, "Geminilake HDMI", patch_i915_glk_hdmi), 4331 HDA_CODEC_ENTRY(0x80862801, "Bearlake HDMI", patch_generic_hdmi), 4332 HDA_CODEC_ENTRY(0x80862802, "Cantiga HDMI", patch_generic_hdmi), 4333 HDA_CODEC_ENTRY(0x80862803, "Eaglelake HDMI", patch_generic_hdmi), 4334 HDA_CODEC_ENTRY(0x80862804, "IbexPeak HDMI", patch_i915_cpt_hdmi), 4335 HDA_CODEC_ENTRY(0x80862805, "CougarPoint HDMI", patch_i915_cpt_hdmi), 4336 HDA_CODEC_ENTRY(0x80862806, "PantherPoint HDMI", patch_i915_cpt_hdmi), 4337 HDA_CODEC_ENTRY(0x80862807, "Haswell HDMI", patch_i915_hsw_hdmi), 4338 HDA_CODEC_ENTRY(0x80862808, "Broadwell HDMI", patch_i915_hsw_hdmi), 4339 HDA_CODEC_ENTRY(0x80862809, "Skylake HDMI", patch_i915_hsw_hdmi), 4340 HDA_CODEC_ENTRY(0x8086280a, "Broxton HDMI", patch_i915_hsw_hdmi), 4341 HDA_CODEC_ENTRY(0x8086280b, "Kabylake HDMI", patch_i915_hsw_hdmi), 4342 HDA_CODEC_ENTRY(0x8086280c, "Cannonlake HDMI", patch_i915_glk_hdmi), 4343 HDA_CODEC_ENTRY(0x8086280d, "Geminilake HDMI", patch_i915_glk_hdmi), 4344 HDA_CODEC_ENTRY(0x8086280f, "Icelake HDMI", patch_i915_icl_hdmi), 4345 HDA_CODEC_ENTRY(0x80862812, "Tigerlake HDMI", patch_i915_tgl_hdmi), 4346 HDA_CODEC_ENTRY(0x80862814, "DG1 HDMI", patch_i915_tgl_hdmi), 4347 HDA_CODEC_ENTRY(0x80862815, "Alderlake HDMI", patch_i915_tgl_hdmi), 4348 HDA_CODEC_ENTRY(0x8086281c, "Alderlake-P HDMI", patch_i915_tgl_hdmi), 4349 HDA_CODEC_ENTRY(0x80862816, "Rocketlake HDMI", patch_i915_tgl_hdmi), 4350 HDA_CODEC_ENTRY(0x8086281a, "Jasperlake HDMI", patch_i915_icl_hdmi), 4351 HDA_CODEC_ENTRY(0x8086281b, "Elkhartlake HDMI", patch_i915_icl_hdmi), 4352 HDA_CODEC_ENTRY(0x80862880, "CedarTrail HDMI", patch_generic_hdmi), 4353 HDA_CODEC_ENTRY(0x80862882, "Valleyview2 HDMI", patch_i915_byt_hdmi), 4354 HDA_CODEC_ENTRY(0x80862883, "Braswell HDMI", patch_i915_byt_hdmi), 4355 HDA_CODEC_ENTRY(0x808629fb, "Crestline HDMI", patch_generic_hdmi), 4356 /* special ID for generic HDMI */ 4357 HDA_CODEC_ENTRY(HDA_CODEC_ID_GENERIC_HDMI, "Generic HDMI", patch_generic_hdmi), 4358 {} /* terminator */ 4359 }; 4360 MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_hdmi); 4361 4362 MODULE_LICENSE("GPL"); 4363 MODULE_DESCRIPTION("HDMI HD-audio codec"); 4364 MODULE_ALIAS("snd-hda-codec-intelhdmi"); 4365 MODULE_ALIAS("snd-hda-codec-nvhdmi"); 4366 MODULE_ALIAS("snd-hda-codec-atihdmi"); 4367 4368 static struct hda_codec_driver hdmi_driver = { 4369 .id = snd_hda_id_hdmi, 4370 }; 4371 4372 module_hda_codec_driver(hdmi_driver); 4373