1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * HD audio codec driver for AD1882, AD1884, AD1981HD, AD1983, AD1984, 4 * AD1986A, AD1988 5 * 6 * Copyright (c) 2005-2007 Takashi Iwai <tiwai@suse.de> 7 */ 8 9 #include <linux/init.h> 10 #include <linux/slab.h> 11 #include <linux/module.h> 12 13 #include <sound/core.h> 14 #include <sound/hda_codec.h> 15 #include "hda_local.h" 16 #include "hda_auto_parser.h" 17 #include "hda_beep.h" 18 #include "hda_jack.h" 19 #include "generic.h" 20 21 enum { 22 MODEL_AD1882, 23 MODEL_AD1884, 24 MODEL_AD1981, 25 MODEL_AD1983, 26 MODEL_AD1986A, 27 MODEL_AD1988, 28 }; 29 30 struct ad198x_spec { 31 struct hda_gen_spec gen; 32 int model; 33 34 /* for auto parser */ 35 int smux_paths[4]; 36 unsigned int cur_smux; 37 hda_nid_t eapd_nid; 38 39 unsigned int beep_amp; /* beep amp value, set via set_beep_amp() */ 40 int num_smux_conns; 41 42 unsigned int gpio_data; 43 }; 44 45 46 #ifdef CONFIG_SND_HDA_INPUT_BEEP 47 /* additional beep mixers; the actual parameters are overwritten at build */ 48 static const struct snd_kcontrol_new ad_beep_mixer[] = { 49 HDA_CODEC_VOLUME("Beep Playback Volume", 0, 0, HDA_OUTPUT), 50 HDA_CODEC_MUTE_BEEP("Beep Playback Switch", 0, 0, HDA_OUTPUT), 51 { } /* end */ 52 }; 53 54 #define set_beep_amp(spec, nid, idx, dir) \ 55 ((spec)->beep_amp = HDA_COMPOSE_AMP_VAL(nid, 1, idx, dir)) /* mono */ 56 #else 57 #define set_beep_amp(spec, nid, idx, dir) /* NOP */ 58 #endif 59 60 #ifdef CONFIG_SND_HDA_INPUT_BEEP 61 static int create_beep_ctls(struct hda_codec *codec) 62 { 63 struct ad198x_spec *spec = codec->spec; 64 const struct snd_kcontrol_new *knew; 65 66 if (!spec->beep_amp) 67 return 0; 68 69 for (knew = ad_beep_mixer ; knew->name; knew++) { 70 int err; 71 struct snd_kcontrol *kctl; 72 kctl = snd_ctl_new1(knew, codec); 73 if (!kctl) 74 return -ENOMEM; 75 kctl->private_value = spec->beep_amp; 76 err = snd_hda_ctl_add(codec, 0, kctl); 77 if (err < 0) 78 return err; 79 } 80 return 0; 81 } 82 #else 83 #define create_beep_ctls(codec) 0 84 #endif 85 86 static void ad198x_power_eapd_write(struct hda_codec *codec, hda_nid_t front, 87 hda_nid_t hp) 88 { 89 if (snd_hda_query_pin_caps(codec, front) & AC_PINCAP_EAPD) 90 snd_hda_codec_write(codec, front, 0, AC_VERB_SET_EAPD_BTLENABLE, 91 !codec->inv_eapd ? 0x00 : 0x02); 92 if (snd_hda_query_pin_caps(codec, hp) & AC_PINCAP_EAPD) 93 snd_hda_codec_write(codec, hp, 0, AC_VERB_SET_EAPD_BTLENABLE, 94 !codec->inv_eapd ? 0x00 : 0x02); 95 } 96 97 static void ad198x_power_eapd(struct hda_codec *codec) 98 { 99 /* We currently only handle front, HP */ 100 switch (codec->core.vendor_id) { 101 case 0x11d41882: 102 case 0x11d4882a: 103 case 0x11d41884: 104 case 0x11d41984: 105 case 0x11d41883: 106 case 0x11d4184a: 107 case 0x11d4194a: 108 case 0x11d4194b: 109 case 0x11d41988: 110 case 0x11d4198b: 111 case 0x11d4989a: 112 case 0x11d4989b: 113 ad198x_power_eapd_write(codec, 0x12, 0x11); 114 break; 115 case 0x11d41981: 116 case 0x11d41983: 117 ad198x_power_eapd_write(codec, 0x05, 0x06); 118 break; 119 case 0x11d41986: 120 ad198x_power_eapd_write(codec, 0x1b, 0x1a); 121 break; 122 } 123 } 124 125 static int ad_codec_suspend(struct hda_codec *codec) 126 { 127 snd_hda_shutup_pins(codec); 128 ad198x_power_eapd(codec); 129 return 0; 130 } 131 132 /* follow EAPD via vmaster hook */ 133 static void ad_vmaster_eapd_hook(void *private_data, int enabled) 134 { 135 struct hda_codec *codec = private_data; 136 struct ad198x_spec *spec = codec->spec; 137 138 if (!spec->eapd_nid) 139 return; 140 if (codec->inv_eapd) 141 enabled = !enabled; 142 snd_hda_codec_write_cache(codec, spec->eapd_nid, 0, 143 AC_VERB_SET_EAPD_BTLENABLE, 144 enabled ? 0x02 : 0x00); 145 } 146 147 /* 148 * Automatic parse of I/O pins from the BIOS configuration 149 */ 150 151 static int ad_codec_build_controls(struct hda_codec *codec) 152 { 153 int err; 154 155 err = snd_hda_gen_build_controls(codec); 156 if (err < 0) 157 return err; 158 err = create_beep_ctls(codec); 159 if (err < 0) 160 return err; 161 return 0; 162 } 163 164 static int ad198x_parse_auto_config(struct hda_codec *codec, bool indep_hp) 165 { 166 struct ad198x_spec *spec = codec->spec; 167 struct auto_pin_cfg *cfg = &spec->gen.autocfg; 168 int err; 169 170 codec->spdif_status_reset = 1; 171 codec->no_trigger_sense = 1; 172 codec->no_sticky_stream = 1; 173 174 spec->gen.indep_hp = indep_hp; 175 if (!spec->gen.add_stereo_mix_input) 176 spec->gen.add_stereo_mix_input = HDA_HINT_STEREO_MIX_AUTO; 177 178 err = snd_hda_parse_pin_defcfg(codec, cfg, NULL, 0); 179 if (err < 0) 180 return err; 181 err = snd_hda_gen_parse_auto_config(codec, cfg); 182 if (err < 0) 183 return err; 184 185 return 0; 186 } 187 188 /* 189 * AD1986A specific 190 */ 191 192 static int alloc_ad_spec(struct hda_codec *codec) 193 { 194 struct ad198x_spec *spec; 195 196 spec = kzalloc_obj(*spec); 197 if (!spec) 198 return -ENOMEM; 199 codec->spec = spec; 200 snd_hda_gen_spec_init(&spec->gen); 201 return 0; 202 } 203 204 /* 205 * AD1986A fixup codes 206 */ 207 208 /* Lenovo N100 seems to report the reversed bit for HP jack-sensing */ 209 static void ad_fixup_inv_jack_detect(struct hda_codec *codec, 210 const struct hda_fixup *fix, int action) 211 { 212 struct ad198x_spec *spec = codec->spec; 213 214 if (action == HDA_FIXUP_ACT_PRE_PROBE) { 215 codec->inv_jack_detect = 1; 216 spec->gen.keep_eapd_on = 1; 217 spec->gen.vmaster_mute.hook = ad_vmaster_eapd_hook; 218 spec->eapd_nid = 0x1b; 219 } 220 } 221 222 /* Toshiba Satellite L40 implements EAPD in a standard way unlike others */ 223 static void ad1986a_fixup_eapd(struct hda_codec *codec, 224 const struct hda_fixup *fix, int action) 225 { 226 struct ad198x_spec *spec = codec->spec; 227 228 if (action == HDA_FIXUP_ACT_PRE_PROBE) { 229 codec->inv_eapd = 0; 230 spec->gen.keep_eapd_on = 1; 231 spec->eapd_nid = 0x1b; 232 } 233 } 234 235 /* enable stereo-mix input for avoiding regression on KDE (bko#88251) */ 236 static void ad1986a_fixup_eapd_mix_in(struct hda_codec *codec, 237 const struct hda_fixup *fix, int action) 238 { 239 struct ad198x_spec *spec = codec->spec; 240 241 if (action == HDA_FIXUP_ACT_PRE_PROBE) { 242 ad1986a_fixup_eapd(codec, fix, action); 243 spec->gen.add_stereo_mix_input = HDA_HINT_STEREO_MIX_ENABLE; 244 } 245 } 246 247 enum { 248 AD1986A_FIXUP_INV_JACK_DETECT, 249 AD1986A_FIXUP_ULTRA, 250 AD1986A_FIXUP_SAMSUNG, 251 AD1986A_FIXUP_3STACK, 252 AD1986A_FIXUP_LAPTOP, 253 AD1986A_FIXUP_LAPTOP_IMIC, 254 AD1986A_FIXUP_EAPD, 255 AD1986A_FIXUP_EAPD_MIX_IN, 256 AD1986A_FIXUP_EASYNOTE, 257 }; 258 259 static const struct hda_fixup ad1986a_fixups[] = { 260 [AD1986A_FIXUP_INV_JACK_DETECT] = { 261 .type = HDA_FIXUP_FUNC, 262 .v.func = ad_fixup_inv_jack_detect, 263 }, 264 [AD1986A_FIXUP_ULTRA] = { 265 .type = HDA_FIXUP_PINS, 266 .v.pins = (const struct hda_pintbl[]) { 267 { 0x1b, 0x90170110 }, /* speaker */ 268 { 0x1d, 0x90a7013e }, /* int mic */ 269 {} 270 }, 271 }, 272 [AD1986A_FIXUP_SAMSUNG] = { 273 .type = HDA_FIXUP_PINS, 274 .v.pins = (const struct hda_pintbl[]) { 275 { 0x1b, 0x90170110 }, /* speaker */ 276 { 0x1d, 0x90a7013e }, /* int mic */ 277 { 0x20, 0x411111f0 }, /* N/A */ 278 { 0x24, 0x411111f0 }, /* N/A */ 279 {} 280 }, 281 }, 282 [AD1986A_FIXUP_3STACK] = { 283 .type = HDA_FIXUP_PINS, 284 .v.pins = (const struct hda_pintbl[]) { 285 { 0x1a, 0x02214021 }, /* headphone */ 286 { 0x1b, 0x01014011 }, /* front */ 287 { 0x1c, 0x01813030 }, /* line-in */ 288 { 0x1d, 0x01a19020 }, /* rear mic */ 289 { 0x1e, 0x411111f0 }, /* N/A */ 290 { 0x1f, 0x02a190f0 }, /* mic */ 291 { 0x20, 0x411111f0 }, /* N/A */ 292 {} 293 }, 294 }, 295 [AD1986A_FIXUP_LAPTOP] = { 296 .type = HDA_FIXUP_PINS, 297 .v.pins = (const struct hda_pintbl[]) { 298 { 0x1a, 0x02214021 }, /* headphone */ 299 { 0x1b, 0x90170110 }, /* speaker */ 300 { 0x1c, 0x411111f0 }, /* N/A */ 301 { 0x1d, 0x411111f0 }, /* N/A */ 302 { 0x1e, 0x411111f0 }, /* N/A */ 303 { 0x1f, 0x02a191f0 }, /* mic */ 304 { 0x20, 0x411111f0 }, /* N/A */ 305 {} 306 }, 307 }, 308 [AD1986A_FIXUP_LAPTOP_IMIC] = { 309 .type = HDA_FIXUP_PINS, 310 .v.pins = (const struct hda_pintbl[]) { 311 { 0x1d, 0x90a7013e }, /* int mic */ 312 {} 313 }, 314 .chained_before = 1, 315 .chain_id = AD1986A_FIXUP_LAPTOP, 316 }, 317 [AD1986A_FIXUP_EAPD] = { 318 .type = HDA_FIXUP_FUNC, 319 .v.func = ad1986a_fixup_eapd, 320 }, 321 [AD1986A_FIXUP_EAPD_MIX_IN] = { 322 .type = HDA_FIXUP_FUNC, 323 .v.func = ad1986a_fixup_eapd_mix_in, 324 }, 325 [AD1986A_FIXUP_EASYNOTE] = { 326 .type = HDA_FIXUP_PINS, 327 .v.pins = (const struct hda_pintbl[]) { 328 { 0x1a, 0x0421402f }, /* headphone */ 329 { 0x1b, 0x90170110 }, /* speaker */ 330 { 0x1c, 0x411111f0 }, /* N/A */ 331 { 0x1d, 0x90a70130 }, /* int mic */ 332 { 0x1e, 0x411111f0 }, /* N/A */ 333 { 0x1f, 0x04a19040 }, /* mic */ 334 { 0x20, 0x411111f0 }, /* N/A */ 335 { 0x21, 0x411111f0 }, /* N/A */ 336 { 0x22, 0x411111f0 }, /* N/A */ 337 { 0x23, 0x411111f0 }, /* N/A */ 338 { 0x24, 0x411111f0 }, /* N/A */ 339 { 0x25, 0x411111f0 }, /* N/A */ 340 {} 341 }, 342 .chained = true, 343 .chain_id = AD1986A_FIXUP_EAPD_MIX_IN, 344 }, 345 }; 346 347 static const struct hda_quirk ad1986a_fixup_tbl[] = { 348 SND_PCI_QUIRK(0x103c, 0x30af, "HP B2800", AD1986A_FIXUP_LAPTOP_IMIC), 349 SND_PCI_QUIRK(0x1043, 0x1153, "ASUS M9V", AD1986A_FIXUP_LAPTOP_IMIC), 350 SND_PCI_QUIRK(0x1043, 0x1443, "ASUS Z99He", AD1986A_FIXUP_EAPD), 351 SND_PCI_QUIRK(0x1043, 0x1447, "ASUS A8JN", AD1986A_FIXUP_EAPD), 352 SND_PCI_QUIRK_MASK(0x1043, 0xff00, 0x8100, "ASUS P5", AD1986A_FIXUP_3STACK), 353 SND_PCI_QUIRK_MASK(0x1043, 0xff00, 0x8200, "ASUS M2", AD1986A_FIXUP_3STACK), 354 SND_PCI_QUIRK(0x10de, 0xcb84, "ASUS A8N-VM", AD1986A_FIXUP_3STACK), 355 SND_PCI_QUIRK(0x1179, 0xff40, "Toshiba Satellite L40", AD1986A_FIXUP_EAPD), 356 SND_PCI_QUIRK(0x144d, 0xc01e, "FSC V2060", AD1986A_FIXUP_LAPTOP), 357 SND_PCI_QUIRK_MASK(0x144d, 0xff00, 0xc000, "Samsung", AD1986A_FIXUP_SAMSUNG), 358 SND_PCI_QUIRK(0x144d, 0xc027, "Samsung Q1", AD1986A_FIXUP_ULTRA), 359 SND_PCI_QUIRK(0x1631, 0xc022, "PackardBell EasyNote MX65", AD1986A_FIXUP_EASYNOTE), 360 SND_PCI_QUIRK(0x17aa, 0x2066, "Lenovo N100", AD1986A_FIXUP_INV_JACK_DETECT), 361 SND_PCI_QUIRK(0x17aa, 0x1011, "Lenovo M55", AD1986A_FIXUP_3STACK), 362 SND_PCI_QUIRK(0x17aa, 0x1017, "Lenovo A60", AD1986A_FIXUP_3STACK), 363 {} 364 }; 365 366 static const struct hda_model_fixup ad1986a_fixup_models[] = { 367 { .id = AD1986A_FIXUP_3STACK, .name = "3stack" }, 368 { .id = AD1986A_FIXUP_LAPTOP, .name = "laptop" }, 369 { .id = AD1986A_FIXUP_LAPTOP_IMIC, .name = "laptop-imic" }, 370 { .id = AD1986A_FIXUP_LAPTOP_IMIC, .name = "laptop-eapd" }, /* alias */ 371 { .id = AD1986A_FIXUP_EAPD, .name = "eapd" }, 372 {} 373 }; 374 375 /* 376 */ 377 static int ad1986a_probe(struct hda_codec *codec) 378 { 379 int err; 380 struct ad198x_spec *spec = codec->spec; 381 static const hda_nid_t preferred_pairs[] = { 382 0x1a, 0x03, 383 0x1b, 0x03, 384 0x1c, 0x04, 385 0x1d, 0x05, 386 0x1e, 0x03, 387 0 388 }; 389 390 /* AD1986A has the inverted EAPD implementation */ 391 codec->inv_eapd = 1; 392 393 spec->gen.mixer_nid = 0x07; 394 spec->gen.beep_nid = 0x19; 395 set_beep_amp(spec, 0x18, 0, HDA_OUTPUT); 396 397 /* AD1986A has a hardware problem that it can't share a stream 398 * with multiple output pins. The copy of front to surrounds 399 * causes noisy or silent outputs at a certain timing, e.g. 400 * changing the volume. 401 * So, let's disable the shared stream. 402 */ 403 spec->gen.multiout.no_share_stream = 1; 404 /* give fixed DAC/pin pairs */ 405 spec->gen.preferred_dacs = preferred_pairs; 406 407 /* AD1986A can't manage the dynamic pin on/off smoothly */ 408 spec->gen.auto_mute_via_amp = 1; 409 410 snd_hda_pick_fixup(codec, ad1986a_fixup_models, ad1986a_fixup_tbl, 411 ad1986a_fixups); 412 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE); 413 414 err = ad198x_parse_auto_config(codec, false); 415 if (err < 0) 416 return err; 417 418 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE); 419 420 return 0; 421 } 422 423 424 /* 425 * AD1983 specific 426 */ 427 428 /* 429 * SPDIF mux control for AD1983 auto-parser 430 */ 431 static int ad1983_auto_smux_enum_info(struct snd_kcontrol *kcontrol, 432 struct snd_ctl_elem_info *uinfo) 433 { 434 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 435 struct ad198x_spec *spec = codec->spec; 436 static const char * const texts2[] = { "PCM", "ADC" }; 437 static const char * const texts3[] = { "PCM", "ADC1", "ADC2" }; 438 int num_conns = spec->num_smux_conns; 439 440 if (num_conns == 2) 441 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, texts2); 442 else if (num_conns == 3) 443 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3); 444 else 445 return -EINVAL; 446 } 447 448 static int ad1983_auto_smux_enum_get(struct snd_kcontrol *kcontrol, 449 struct snd_ctl_elem_value *ucontrol) 450 { 451 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 452 struct ad198x_spec *spec = codec->spec; 453 454 ucontrol->value.enumerated.item[0] = spec->cur_smux; 455 return 0; 456 } 457 458 static int ad1983_auto_smux_enum_put(struct snd_kcontrol *kcontrol, 459 struct snd_ctl_elem_value *ucontrol) 460 { 461 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 462 struct ad198x_spec *spec = codec->spec; 463 unsigned int val = ucontrol->value.enumerated.item[0]; 464 hda_nid_t dig_out = spec->gen.multiout.dig_out_nid; 465 int num_conns = spec->num_smux_conns; 466 467 if (val >= num_conns) 468 return -EINVAL; 469 if (spec->cur_smux == val) 470 return 0; 471 spec->cur_smux = val; 472 snd_hda_codec_write_cache(codec, dig_out, 0, 473 AC_VERB_SET_CONNECT_SEL, val); 474 return 1; 475 } 476 477 static const struct snd_kcontrol_new ad1983_auto_smux_mixer = { 478 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 479 .name = "IEC958 Playback Source", 480 .info = ad1983_auto_smux_enum_info, 481 .get = ad1983_auto_smux_enum_get, 482 .put = ad1983_auto_smux_enum_put, 483 }; 484 485 static int ad1983_add_spdif_mux_ctl(struct hda_codec *codec) 486 { 487 struct ad198x_spec *spec = codec->spec; 488 hda_nid_t dig_out = spec->gen.multiout.dig_out_nid; 489 int num_conns; 490 491 if (!dig_out) 492 return 0; 493 num_conns = snd_hda_get_num_conns(codec, dig_out); 494 if (num_conns != 2 && num_conns != 3) 495 return 0; 496 spec->num_smux_conns = num_conns; 497 if (!snd_hda_gen_add_kctl(&spec->gen, NULL, &ad1983_auto_smux_mixer)) 498 return -ENOMEM; 499 return 0; 500 } 501 502 static int ad1983_probe(struct hda_codec *codec) 503 { 504 static const hda_nid_t conn_0c[] = { 0x08 }; 505 static const hda_nid_t conn_0d[] = { 0x09 }; 506 struct ad198x_spec *spec = codec->spec; 507 int err; 508 509 spec->gen.mixer_nid = 0x0e; 510 spec->gen.beep_nid = 0x10; 511 set_beep_amp(spec, 0x10, 0, HDA_OUTPUT); 512 513 /* limit the loopback routes not to confuse the parser */ 514 snd_hda_override_conn_list(codec, 0x0c, ARRAY_SIZE(conn_0c), conn_0c); 515 snd_hda_override_conn_list(codec, 0x0d, ARRAY_SIZE(conn_0d), conn_0d); 516 517 err = ad198x_parse_auto_config(codec, false); 518 if (err < 0) 519 return err; 520 err = ad1983_add_spdif_mux_ctl(codec); 521 if (err < 0) 522 return err; 523 return 0; 524 } 525 526 527 /* 528 * AD1981 HD specific 529 */ 530 531 static void ad1981_fixup_hp_eapd(struct hda_codec *codec, 532 const struct hda_fixup *fix, int action) 533 { 534 struct ad198x_spec *spec = codec->spec; 535 536 if (action == HDA_FIXUP_ACT_PRE_PROBE) { 537 spec->gen.vmaster_mute.hook = ad_vmaster_eapd_hook; 538 spec->eapd_nid = 0x05; 539 } 540 } 541 542 /* set the upper-limit for mixer amp to 0dB for avoiding the possible 543 * damage by overloading 544 */ 545 static void ad1981_fixup_amp_override(struct hda_codec *codec, 546 const struct hda_fixup *fix, int action) 547 { 548 if (action == HDA_FIXUP_ACT_PRE_PROBE) 549 snd_hda_override_amp_caps(codec, 0x11, HDA_INPUT, 550 (0x17 << AC_AMPCAP_OFFSET_SHIFT) | 551 (0x17 << AC_AMPCAP_NUM_STEPS_SHIFT) | 552 (0x05 << AC_AMPCAP_STEP_SIZE_SHIFT) | 553 (1 << AC_AMPCAP_MUTE_SHIFT)); 554 } 555 556 enum { 557 AD1981_FIXUP_AMP_OVERRIDE, 558 AD1981_FIXUP_HP_EAPD, 559 }; 560 561 static const struct hda_fixup ad1981_fixups[] = { 562 [AD1981_FIXUP_AMP_OVERRIDE] = { 563 .type = HDA_FIXUP_FUNC, 564 .v.func = ad1981_fixup_amp_override, 565 }, 566 [AD1981_FIXUP_HP_EAPD] = { 567 .type = HDA_FIXUP_FUNC, 568 .v.func = ad1981_fixup_hp_eapd, 569 .chained = true, 570 .chain_id = AD1981_FIXUP_AMP_OVERRIDE, 571 }, 572 }; 573 574 static const struct hda_quirk ad1981_fixup_tbl[] = { 575 SND_PCI_QUIRK_VENDOR(0x1014, "Lenovo", AD1981_FIXUP_AMP_OVERRIDE), 576 SND_PCI_QUIRK_VENDOR(0x103c, "HP", AD1981_FIXUP_HP_EAPD), 577 SND_PCI_QUIRK_VENDOR(0x17aa, "Lenovo", AD1981_FIXUP_AMP_OVERRIDE), 578 /* HP nx6320 (reversed SSID, H/W bug) */ 579 SND_PCI_QUIRK(0x30b0, 0x103c, "HP nx6320", AD1981_FIXUP_HP_EAPD), 580 {} 581 }; 582 583 static int ad1981_probe(struct hda_codec *codec) 584 { 585 struct ad198x_spec *spec = codec->spec; 586 int err; 587 588 spec->gen.mixer_nid = 0x0e; 589 spec->gen.beep_nid = 0x10; 590 set_beep_amp(spec, 0x0d, 0, HDA_OUTPUT); 591 592 snd_hda_pick_fixup(codec, NULL, ad1981_fixup_tbl, ad1981_fixups); 593 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE); 594 595 err = ad198x_parse_auto_config(codec, false); 596 if (err < 0) 597 return err; 598 err = ad1983_add_spdif_mux_ctl(codec); 599 if (err < 0) 600 return err; 601 602 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE); 603 604 return 0; 605 } 606 607 608 /* 609 * AD1988 610 * 611 * Output pins and routes 612 * 613 * Pin Mix Sel DAC (*) 614 * port-A 0x11 (mute/hp) <- 0x22 <- 0x37 <- 03/04/06 615 * port-B 0x14 (mute/hp) <- 0x2b <- 0x30 <- 03/04/06 616 * port-C 0x15 (mute) <- 0x2c <- 0x31 <- 05/0a 617 * port-D 0x12 (mute/hp) <- 0x29 <- 04 618 * port-E 0x17 (mute/hp) <- 0x26 <- 0x32 <- 05/0a 619 * port-F 0x16 (mute) <- 0x2a <- 06 620 * port-G 0x24 (mute) <- 0x27 <- 05 621 * port-H 0x25 (mute) <- 0x28 <- 0a 622 * mono 0x13 (mute/amp)<- 0x1e <- 0x36 <- 03/04/06 623 * 624 * DAC0 = 03h, DAC1 = 04h, DAC2 = 05h, DAC3 = 06h, DAC4 = 0ah 625 * (*) DAC2/3/4 are swapped to DAC3/4/2 on AD198A rev.2 due to a h/w bug. 626 * 627 * Input pins and routes 628 * 629 * pin boost mix input # / adc input # 630 * port-A 0x11 -> 0x38 -> mix 2, ADC 0 631 * port-B 0x14 -> 0x39 -> mix 0, ADC 1 632 * port-C 0x15 -> 0x3a -> 33:0 - mix 1, ADC 2 633 * port-D 0x12 -> 0x3d -> mix 3, ADC 8 634 * port-E 0x17 -> 0x3c -> 34:0 - mix 4, ADC 4 635 * port-F 0x16 -> 0x3b -> mix 5, ADC 3 636 * port-G 0x24 -> N/A -> 33:1 - mix 1, 34:1 - mix 4, ADC 6 637 * port-H 0x25 -> N/A -> 33:2 - mix 1, 34:2 - mix 4, ADC 7 638 * 639 * 640 * DAC assignment 641 * 6stack - front/surr/CLFE/side/opt DACs - 04/06/05/0a/03 642 * 3stack - front/surr/CLFE/opt DACs - 04/05/0a/03 643 * 644 * Inputs of Analog Mix (0x20) 645 * 0:Port-B (front mic) 646 * 1:Port-C/G/H (line-in) 647 * 2:Port-A 648 * 3:Port-D (line-in/2) 649 * 4:Port-E/G/H (mic-in) 650 * 5:Port-F (mic2-in) 651 * 6:CD 652 * 7:Beep 653 * 654 * ADC selection 655 * 0:Port-A 656 * 1:Port-B (front mic-in) 657 * 2:Port-C (line-in) 658 * 3:Port-F (mic2-in) 659 * 4:Port-E (mic-in) 660 * 5:CD 661 * 6:Port-G 662 * 7:Port-H 663 * 8:Port-D (line-in/2) 664 * 9:Mix 665 * 666 * Proposed pin assignments by the datasheet 667 * 668 * 6-stack 669 * Port-A front headphone 670 * B front mic-in 671 * C rear line-in 672 * D rear front-out 673 * E rear mic-in 674 * F rear surround 675 * G rear CLFE 676 * H rear side 677 * 678 * 3-stack 679 * Port-A front headphone 680 * B front mic 681 * C rear line-in/surround 682 * D rear front-out 683 * E rear mic-in/CLFE 684 * 685 * laptop 686 * Port-A headphone 687 * B mic-in 688 * C docking station 689 * D internal speaker (with EAPD) 690 * E/F quad mic array 691 */ 692 693 static int ad1988_auto_smux_enum_info(struct snd_kcontrol *kcontrol, 694 struct snd_ctl_elem_info *uinfo) 695 { 696 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 697 struct ad198x_spec *spec = codec->spec; 698 static const char * const texts[] = { 699 "PCM", "ADC1", "ADC2", "ADC3", 700 }; 701 int num_conns = spec->num_smux_conns; 702 703 if (num_conns > 4) 704 num_conns = 4; 705 return snd_hda_enum_helper_info(kcontrol, uinfo, num_conns, texts); 706 } 707 708 static int ad1988_auto_smux_enum_get(struct snd_kcontrol *kcontrol, 709 struct snd_ctl_elem_value *ucontrol) 710 { 711 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 712 struct ad198x_spec *spec = codec->spec; 713 714 ucontrol->value.enumerated.item[0] = spec->cur_smux; 715 return 0; 716 } 717 718 static int ad1988_auto_smux_enum_put(struct snd_kcontrol *kcontrol, 719 struct snd_ctl_elem_value *ucontrol) 720 { 721 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 722 struct ad198x_spec *spec = codec->spec; 723 unsigned int val = ucontrol->value.enumerated.item[0]; 724 struct nid_path *path; 725 int num_conns = spec->num_smux_conns; 726 727 if (val >= num_conns) 728 return -EINVAL; 729 if (spec->cur_smux == val) 730 return 0; 731 732 guard(mutex)(&codec->control_mutex); 733 path = snd_hda_get_path_from_idx(codec, 734 spec->smux_paths[spec->cur_smux]); 735 if (path) 736 snd_hda_activate_path(codec, path, false, true); 737 path = snd_hda_get_path_from_idx(codec, spec->smux_paths[val]); 738 if (path) 739 snd_hda_activate_path(codec, path, true, true); 740 spec->cur_smux = val; 741 return 1; 742 } 743 744 static const struct snd_kcontrol_new ad1988_auto_smux_mixer = { 745 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 746 .name = "IEC958 Playback Source", 747 .info = ad1988_auto_smux_enum_info, 748 .get = ad1988_auto_smux_enum_get, 749 .put = ad1988_auto_smux_enum_put, 750 }; 751 752 static int ad_codec_init(struct hda_codec *codec) 753 { 754 struct ad198x_spec *spec = codec->spec; 755 int i, err; 756 757 err = snd_hda_gen_init(codec); 758 if (err < 0) 759 return err; 760 if (spec->model != MODEL_AD1988) 761 return 0; 762 if (!spec->gen.autocfg.dig_outs) 763 return 0; 764 765 for (i = 0; i < 4; i++) { 766 struct nid_path *path; 767 path = snd_hda_get_path_from_idx(codec, spec->smux_paths[i]); 768 if (path) 769 snd_hda_activate_path(codec, path, path->active, false); 770 } 771 772 return 0; 773 } 774 775 static int ad1988_add_spdif_mux_ctl(struct hda_codec *codec) 776 { 777 struct ad198x_spec *spec = codec->spec; 778 int i, num_conns; 779 /* we create four static faked paths, since AD codecs have odd 780 * widget connections regarding the SPDIF out source 781 */ 782 static const struct nid_path fake_paths[4] = { 783 { 784 .depth = 3, 785 .path = { 0x02, 0x1d, 0x1b }, 786 .idx = { 0, 0, 0 }, 787 .multi = { 0, 0, 0 }, 788 }, 789 { 790 .depth = 4, 791 .path = { 0x08, 0x0b, 0x1d, 0x1b }, 792 .idx = { 0, 0, 1, 0 }, 793 .multi = { 0, 1, 0, 0 }, 794 }, 795 { 796 .depth = 4, 797 .path = { 0x09, 0x0b, 0x1d, 0x1b }, 798 .idx = { 0, 1, 1, 0 }, 799 .multi = { 0, 1, 0, 0 }, 800 }, 801 { 802 .depth = 4, 803 .path = { 0x0f, 0x0b, 0x1d, 0x1b }, 804 .idx = { 0, 2, 1, 0 }, 805 .multi = { 0, 1, 0, 0 }, 806 }, 807 }; 808 809 /* SPDIF source mux appears to be present only on AD1988A */ 810 if (!spec->gen.autocfg.dig_outs || 811 get_wcaps_type(get_wcaps(codec, 0x1d)) != AC_WID_AUD_MIX) 812 return 0; 813 814 num_conns = snd_hda_get_num_conns(codec, 0x0b) + 1; 815 if (num_conns != 3 && num_conns != 4) 816 return 0; 817 spec->num_smux_conns = num_conns; 818 819 for (i = 0; i < num_conns; i++) { 820 struct nid_path *path = snd_array_new(&spec->gen.paths); 821 if (!path) 822 return -ENOMEM; 823 *path = fake_paths[i]; 824 if (!i) 825 path->active = 1; 826 spec->smux_paths[i] = snd_hda_get_path_idx(codec, path); 827 } 828 829 if (!snd_hda_gen_add_kctl(&spec->gen, NULL, &ad1988_auto_smux_mixer)) 830 return -ENOMEM; 831 832 return 0; 833 } 834 835 /* 836 */ 837 838 enum { 839 AD1988_FIXUP_6STACK_DIG, 840 }; 841 842 static const struct hda_fixup ad1988_fixups[] = { 843 [AD1988_FIXUP_6STACK_DIG] = { 844 .type = HDA_FIXUP_PINS, 845 .v.pins = (const struct hda_pintbl[]) { 846 { 0x11, 0x02214130 }, /* front-hp */ 847 { 0x12, 0x01014010 }, /* line-out */ 848 { 0x14, 0x02a19122 }, /* front-mic */ 849 { 0x15, 0x01813021 }, /* line-in */ 850 { 0x16, 0x01011012 }, /* line-out */ 851 { 0x17, 0x01a19020 }, /* mic */ 852 { 0x1b, 0x0145f1f0 }, /* SPDIF */ 853 { 0x24, 0x01016011 }, /* line-out */ 854 { 0x25, 0x01012013 }, /* line-out */ 855 { } 856 } 857 }, 858 }; 859 860 static const struct hda_model_fixup ad1988_fixup_models[] = { 861 { .id = AD1988_FIXUP_6STACK_DIG, .name = "6stack-dig" }, 862 {} 863 }; 864 865 static int ad1988_probe(struct hda_codec *codec) 866 { 867 struct ad198x_spec *spec = codec->spec; 868 int err; 869 870 spec->gen.mixer_nid = 0x20; 871 spec->gen.mixer_merge_nid = 0x21; 872 spec->gen.beep_nid = 0x10; 873 set_beep_amp(spec, 0x10, 0, HDA_OUTPUT); 874 875 snd_hda_pick_fixup(codec, ad1988_fixup_models, NULL, ad1988_fixups); 876 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE); 877 878 err = ad198x_parse_auto_config(codec, true); 879 if (err < 0) 880 return err; 881 err = ad1988_add_spdif_mux_ctl(codec); 882 if (err < 0) 883 return err; 884 885 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE); 886 887 return 0; 888 } 889 890 891 /* 892 * AD1884 / AD1984 893 * 894 * port-B - front line/mic-in 895 * port-E - aux in/out 896 * port-F - aux in/out 897 * port-C - rear line/mic-in 898 * port-D - rear line/hp-out 899 * port-A - front line/hp-out 900 * 901 * AD1984 = AD1884 + two digital mic-ins 902 * 903 * AD1883 / AD1884A / AD1984A / AD1984B 904 * 905 * port-B (0x14) - front mic-in 906 * port-E (0x1c) - rear mic-in 907 * port-F (0x16) - CD / ext out 908 * port-C (0x15) - rear line-in 909 * port-D (0x12) - rear line-out 910 * port-A (0x11) - front hp-out 911 * 912 * AD1984A = AD1884A + digital-mic 913 * AD1883 = equivalent with AD1984A 914 * AD1984B = AD1984A + extra SPDIF-out 915 */ 916 917 /* set the upper-limit for mixer amp to 0dB for avoiding the possible 918 * damage by overloading 919 */ 920 static void ad1884_fixup_amp_override(struct hda_codec *codec, 921 const struct hda_fixup *fix, int action) 922 { 923 if (action == HDA_FIXUP_ACT_PRE_PROBE) 924 snd_hda_override_amp_caps(codec, 0x20, HDA_INPUT, 925 (0x17 << AC_AMPCAP_OFFSET_SHIFT) | 926 (0x17 << AC_AMPCAP_NUM_STEPS_SHIFT) | 927 (0x05 << AC_AMPCAP_STEP_SIZE_SHIFT) | 928 (1 << AC_AMPCAP_MUTE_SHIFT)); 929 } 930 931 /* toggle GPIO1 according to the mute state */ 932 static void ad1884_vmaster_hp_gpio_hook(void *private_data, int enabled) 933 { 934 struct hda_codec *codec = private_data; 935 struct ad198x_spec *spec = codec->spec; 936 937 if (spec->eapd_nid) 938 ad_vmaster_eapd_hook(private_data, enabled); 939 spec->gpio_data = enabled ? 0x00 : 0x02; 940 snd_hda_codec_write(codec, 0x01, 0, 941 AC_VERB_SET_GPIO_DATA, spec->gpio_data); 942 } 943 944 static void ad1884_fixup_hp_eapd(struct hda_codec *codec, 945 const struct hda_fixup *fix, int action) 946 { 947 struct ad198x_spec *spec = codec->spec; 948 949 switch (action) { 950 case HDA_FIXUP_ACT_PRE_PROBE: 951 spec->gen.vmaster_mute.hook = ad1884_vmaster_hp_gpio_hook; 952 spec->gen.own_eapd_ctl = 1; 953 spec->gpio_data = 0x02; 954 break; 955 case HDA_FIXUP_ACT_PROBE: 956 if (spec->gen.autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT) 957 spec->eapd_nid = spec->gen.autocfg.line_out_pins[0]; 958 else 959 spec->eapd_nid = spec->gen.autocfg.speaker_pins[0]; 960 break; 961 case HDA_FIXUP_ACT_INIT: 962 snd_hda_codec_set_gpio(codec, 0x02, 0x02, spec->gpio_data, 0); 963 break; 964 } 965 } 966 967 static void ad1884_fixup_thinkpad(struct hda_codec *codec, 968 const struct hda_fixup *fix, int action) 969 { 970 struct ad198x_spec *spec = codec->spec; 971 972 if (action == HDA_FIXUP_ACT_PRE_PROBE) { 973 spec->gen.keep_eapd_on = 1; 974 spec->gen.vmaster_mute.hook = ad_vmaster_eapd_hook; 975 spec->eapd_nid = 0x12; 976 /* Analog PC Beeper - allow firmware/ACPI beeps */ 977 spec->beep_amp = HDA_COMPOSE_AMP_VAL(0x20, 3, 3, HDA_INPUT); 978 spec->gen.beep_nid = 0; /* no digital beep */ 979 } 980 } 981 982 /* set magic COEFs for dmic */ 983 static const struct hda_verb ad1884_dmic_init_verbs[] = { 984 {0x01, AC_VERB_SET_COEF_INDEX, 0x13f7}, 985 {0x01, AC_VERB_SET_PROC_COEF, 0x08}, 986 {} 987 }; 988 989 enum { 990 AD1884_FIXUP_AMP_OVERRIDE, 991 AD1884_FIXUP_HP_EAPD, 992 AD1884_FIXUP_DMIC_COEF, 993 AD1884_FIXUP_THINKPAD, 994 AD1884_FIXUP_HP_TOUCHSMART, 995 }; 996 997 static const struct hda_fixup ad1884_fixups[] = { 998 [AD1884_FIXUP_AMP_OVERRIDE] = { 999 .type = HDA_FIXUP_FUNC, 1000 .v.func = ad1884_fixup_amp_override, 1001 }, 1002 [AD1884_FIXUP_HP_EAPD] = { 1003 .type = HDA_FIXUP_FUNC, 1004 .v.func = ad1884_fixup_hp_eapd, 1005 .chained = true, 1006 .chain_id = AD1884_FIXUP_AMP_OVERRIDE, 1007 }, 1008 [AD1884_FIXUP_DMIC_COEF] = { 1009 .type = HDA_FIXUP_VERBS, 1010 .v.verbs = ad1884_dmic_init_verbs, 1011 }, 1012 [AD1884_FIXUP_THINKPAD] = { 1013 .type = HDA_FIXUP_FUNC, 1014 .v.func = ad1884_fixup_thinkpad, 1015 .chained = true, 1016 .chain_id = AD1884_FIXUP_DMIC_COEF, 1017 }, 1018 [AD1884_FIXUP_HP_TOUCHSMART] = { 1019 .type = HDA_FIXUP_VERBS, 1020 .v.verbs = ad1884_dmic_init_verbs, 1021 .chained = true, 1022 .chain_id = AD1884_FIXUP_HP_EAPD, 1023 }, 1024 }; 1025 1026 static const struct hda_quirk ad1884_fixup_tbl[] = { 1027 SND_PCI_QUIRK(0x103c, 0x2a82, "HP Touchsmart", AD1884_FIXUP_HP_TOUCHSMART), 1028 SND_PCI_QUIRK_VENDOR(0x103c, "HP", AD1884_FIXUP_HP_EAPD), 1029 SND_PCI_QUIRK_VENDOR(0x17aa, "Lenovo Thinkpad", AD1884_FIXUP_THINKPAD), 1030 {} 1031 }; 1032 1033 1034 static int ad1884_probe(struct hda_codec *codec) 1035 { 1036 struct ad198x_spec *spec = codec->spec; 1037 int err; 1038 1039 spec->gen.mixer_nid = 0x20; 1040 spec->gen.mixer_merge_nid = 0x21; 1041 spec->gen.beep_nid = 0x10; 1042 set_beep_amp(spec, 0x10, 0, HDA_OUTPUT); 1043 1044 snd_hda_pick_fixup(codec, NULL, ad1884_fixup_tbl, ad1884_fixups); 1045 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE); 1046 1047 err = ad198x_parse_auto_config(codec, true); 1048 if (err < 0) 1049 return err; 1050 err = ad1983_add_spdif_mux_ctl(codec); 1051 if (err < 0) 1052 return err; 1053 1054 snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE); 1055 1056 return 0; 1057 } 1058 1059 /* 1060 * AD1882 / AD1882A 1061 * 1062 * port-A - front hp-out 1063 * port-B - front mic-in 1064 * port-C - rear line-in, shared surr-out (3stack) 1065 * port-D - rear line-out 1066 * port-E - rear mic-in, shared clfe-out (3stack) 1067 * port-F - rear surr-out (6stack) 1068 * port-G - rear clfe-out (6stack) 1069 */ 1070 1071 static int ad1882_probe(struct hda_codec *codec) 1072 { 1073 struct ad198x_spec *spec = codec->spec; 1074 int err; 1075 1076 spec->gen.mixer_nid = 0x20; 1077 spec->gen.mixer_merge_nid = 0x21; 1078 spec->gen.beep_nid = 0x10; 1079 set_beep_amp(spec, 0x10, 0, HDA_OUTPUT); 1080 err = ad198x_parse_auto_config(codec, true); 1081 if (err < 0) 1082 return err; 1083 err = ad1988_add_spdif_mux_ctl(codec); 1084 if (err < 0) 1085 return err; 1086 return 0; 1087 } 1088 1089 /* 1090 * driver entries 1091 */ 1092 static int ad_codec_probe(struct hda_codec *codec, 1093 const struct hda_device_id *id) 1094 { 1095 struct ad198x_spec *spec; 1096 int err; 1097 1098 err = alloc_ad_spec(codec); 1099 if (err < 0) 1100 return -ENOMEM; 1101 spec = codec->spec; 1102 spec->model = id->driver_data; 1103 1104 switch (spec->model) { 1105 case MODEL_AD1882: 1106 err = ad1882_probe(codec); 1107 break; 1108 case MODEL_AD1884: 1109 err = ad1884_probe(codec); 1110 break; 1111 case MODEL_AD1981: 1112 err = ad1981_probe(codec); 1113 break; 1114 case MODEL_AD1983: 1115 err = ad1983_probe(codec); 1116 break; 1117 case MODEL_AD1986A: 1118 err = ad1986a_probe(codec); 1119 break; 1120 case MODEL_AD1988: 1121 err = ad1988_probe(codec); 1122 break; 1123 default: 1124 err = -EINVAL; 1125 break; 1126 } 1127 1128 if (err < 0) { 1129 snd_hda_gen_remove(codec); 1130 return err; 1131 } 1132 1133 return 0; 1134 } 1135 1136 static const struct hda_codec_ops ad_codec_ops = { 1137 .probe = ad_codec_probe, 1138 .remove = snd_hda_gen_remove, 1139 .build_controls = ad_codec_build_controls, 1140 .build_pcms = snd_hda_gen_build_pcms, 1141 .init = ad_codec_init, 1142 .unsol_event = snd_hda_jack_unsol_event, 1143 .suspend = ad_codec_suspend, 1144 .check_power_status = snd_hda_gen_check_power_status, 1145 .stream_pm = snd_hda_gen_stream_pm, 1146 }; 1147 1148 static const struct hda_device_id snd_hda_id_analog[] = { 1149 HDA_CODEC_ID_MODEL(0x11d4184a, "AD1884A", MODEL_AD1884), 1150 HDA_CODEC_ID_MODEL(0x11d41882, "AD1882", MODEL_AD1882), 1151 HDA_CODEC_ID_MODEL(0x11d41883, "AD1883", MODEL_AD1884), 1152 HDA_CODEC_ID_MODEL(0x11d41884, "AD1884", MODEL_AD1884), 1153 HDA_CODEC_ID_MODEL(0x11d4194a, "AD1984A", MODEL_AD1884), 1154 HDA_CODEC_ID_MODEL(0x11d4194b, "AD1984B", MODEL_AD1884), 1155 HDA_CODEC_ID_MODEL(0x11d41981, "AD1981", MODEL_AD1981), 1156 HDA_CODEC_ID_MODEL(0x11d41983, "AD1983", MODEL_AD1983), 1157 HDA_CODEC_ID_MODEL(0x11d41984, "AD1984", MODEL_AD1884), 1158 HDA_CODEC_ID_MODEL(0x11d41986, "AD1986A", MODEL_AD1986A), 1159 HDA_CODEC_ID_MODEL(0x11d41988, "AD1988", MODEL_AD1988), 1160 HDA_CODEC_ID_MODEL(0x11d4198b, "AD1988B", MODEL_AD1988), 1161 HDA_CODEC_ID_MODEL(0x11d4882a, "AD1882A", MODEL_AD1882), 1162 HDA_CODEC_ID_MODEL(0x11d4989a, "AD1989A", MODEL_AD1988), 1163 HDA_CODEC_ID_MODEL(0x11d4989b, "AD1989B", MODEL_AD1988), 1164 {} /* terminator */ 1165 }; 1166 MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_analog); 1167 1168 MODULE_LICENSE("GPL"); 1169 MODULE_DESCRIPTION("Analog Devices HD-audio codec"); 1170 1171 static struct hda_codec_driver analog_driver = { 1172 .id = snd_hda_id_analog, 1173 .ops = &ad_codec_ops, 1174 }; 1175 1176 module_hda_codec_driver(analog_driver); 1177