1 /* 2 * Universal Interface for Intel High Definition Audio Codec 3 * 4 * Generic widget tree parser 5 * 6 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de> 7 * 8 * This driver is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License as published by 10 * the Free Software Foundation; either version 2 of the License, or 11 * (at your option) any later version. 12 * 13 * This driver is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details. 17 * 18 * You should have received a copy of the GNU General Public License 19 * along with this program; if not, write to the Free Software 20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 21 */ 22 23 #include <linux/init.h> 24 #include <linux/slab.h> 25 #include <linux/export.h> 26 #include <linux/sort.h> 27 #include <linux/delay.h> 28 #include <linux/ctype.h> 29 #include <linux/string.h> 30 #include <linux/bitops.h> 31 #include <sound/core.h> 32 #include <sound/jack.h> 33 #include "hda_codec.h" 34 #include "hda_local.h" 35 #include "hda_auto_parser.h" 36 #include "hda_jack.h" 37 #include "hda_beep.h" 38 #include "hda_generic.h" 39 40 41 /* initialize hda_gen_spec struct */ 42 int snd_hda_gen_spec_init(struct hda_gen_spec *spec) 43 { 44 snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32); 45 snd_array_init(&spec->paths, sizeof(struct nid_path), 8); 46 snd_array_init(&spec->loopback_list, sizeof(struct hda_amp_list), 8); 47 mutex_init(&spec->pcm_mutex); 48 return 0; 49 } 50 EXPORT_SYMBOL_HDA(snd_hda_gen_spec_init); 51 52 struct snd_kcontrol_new * 53 snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name, 54 const struct snd_kcontrol_new *temp) 55 { 56 struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls); 57 if (!knew) 58 return NULL; 59 *knew = *temp; 60 if (name) 61 knew->name = kstrdup(name, GFP_KERNEL); 62 else if (knew->name) 63 knew->name = kstrdup(knew->name, GFP_KERNEL); 64 if (!knew->name) 65 return NULL; 66 return knew; 67 } 68 EXPORT_SYMBOL_HDA(snd_hda_gen_add_kctl); 69 70 static void free_kctls(struct hda_gen_spec *spec) 71 { 72 if (spec->kctls.list) { 73 struct snd_kcontrol_new *kctl = spec->kctls.list; 74 int i; 75 for (i = 0; i < spec->kctls.used; i++) 76 kfree(kctl[i].name); 77 } 78 snd_array_free(&spec->kctls); 79 } 80 81 void snd_hda_gen_spec_free(struct hda_gen_spec *spec) 82 { 83 if (!spec) 84 return; 85 free_kctls(spec); 86 snd_array_free(&spec->paths); 87 snd_array_free(&spec->loopback_list); 88 } 89 EXPORT_SYMBOL_HDA(snd_hda_gen_spec_free); 90 91 /* 92 * store user hints 93 */ 94 static void parse_user_hints(struct hda_codec *codec) 95 { 96 struct hda_gen_spec *spec = codec->spec; 97 int val; 98 99 val = snd_hda_get_bool_hint(codec, "jack_detect"); 100 if (val >= 0) 101 codec->no_jack_detect = !val; 102 val = snd_hda_get_bool_hint(codec, "inv_jack_detect"); 103 if (val >= 0) 104 codec->inv_jack_detect = !!val; 105 val = snd_hda_get_bool_hint(codec, "trigger_sense"); 106 if (val >= 0) 107 codec->no_trigger_sense = !val; 108 val = snd_hda_get_bool_hint(codec, "inv_eapd"); 109 if (val >= 0) 110 codec->inv_eapd = !!val; 111 val = snd_hda_get_bool_hint(codec, "pcm_format_first"); 112 if (val >= 0) 113 codec->pcm_format_first = !!val; 114 val = snd_hda_get_bool_hint(codec, "sticky_stream"); 115 if (val >= 0) 116 codec->no_sticky_stream = !val; 117 val = snd_hda_get_bool_hint(codec, "spdif_status_reset"); 118 if (val >= 0) 119 codec->spdif_status_reset = !!val; 120 val = snd_hda_get_bool_hint(codec, "pin_amp_workaround"); 121 if (val >= 0) 122 codec->pin_amp_workaround = !!val; 123 val = snd_hda_get_bool_hint(codec, "single_adc_amp"); 124 if (val >= 0) 125 codec->single_adc_amp = !!val; 126 127 val = snd_hda_get_bool_hint(codec, "auto_mute"); 128 if (val >= 0) 129 spec->suppress_auto_mute = !val; 130 val = snd_hda_get_bool_hint(codec, "auto_mic"); 131 if (val >= 0) 132 spec->suppress_auto_mic = !val; 133 val = snd_hda_get_bool_hint(codec, "line_in_auto_switch"); 134 if (val >= 0) 135 spec->line_in_auto_switch = !!val; 136 val = snd_hda_get_bool_hint(codec, "auto_mute_via_amp"); 137 if (val >= 0) 138 spec->auto_mute_via_amp = !!val; 139 val = snd_hda_get_bool_hint(codec, "need_dac_fix"); 140 if (val >= 0) 141 spec->need_dac_fix = !!val; 142 val = snd_hda_get_bool_hint(codec, "primary_hp"); 143 if (val >= 0) 144 spec->no_primary_hp = !val; 145 val = snd_hda_get_bool_hint(codec, "multi_io"); 146 if (val >= 0) 147 spec->no_multi_io = !val; 148 val = snd_hda_get_bool_hint(codec, "multi_cap_vol"); 149 if (val >= 0) 150 spec->multi_cap_vol = !!val; 151 val = snd_hda_get_bool_hint(codec, "inv_dmic_split"); 152 if (val >= 0) 153 spec->inv_dmic_split = !!val; 154 val = snd_hda_get_bool_hint(codec, "indep_hp"); 155 if (val >= 0) 156 spec->indep_hp = !!val; 157 val = snd_hda_get_bool_hint(codec, "add_stereo_mix_input"); 158 if (val >= 0) 159 spec->add_stereo_mix_input = !!val; 160 /* the following two are just for compatibility */ 161 val = snd_hda_get_bool_hint(codec, "add_out_jack_modes"); 162 if (val >= 0) 163 spec->add_jack_modes = !!val; 164 val = snd_hda_get_bool_hint(codec, "add_in_jack_modes"); 165 if (val >= 0) 166 spec->add_jack_modes = !!val; 167 val = snd_hda_get_bool_hint(codec, "add_jack_modes"); 168 if (val >= 0) 169 spec->add_jack_modes = !!val; 170 val = snd_hda_get_bool_hint(codec, "power_down_unused"); 171 if (val >= 0) 172 spec->power_down_unused = !!val; 173 val = snd_hda_get_bool_hint(codec, "add_hp_mic"); 174 if (val >= 0) 175 spec->hp_mic = !!val; 176 val = snd_hda_get_bool_hint(codec, "hp_mic_detect"); 177 if (val >= 0) 178 spec->suppress_hp_mic_detect = !val; 179 180 if (!snd_hda_get_int_hint(codec, "mixer_nid", &val)) 181 spec->mixer_nid = val; 182 } 183 184 /* 185 * pin control value accesses 186 */ 187 188 #define update_pin_ctl(codec, pin, val) \ 189 snd_hda_codec_update_cache(codec, pin, 0, \ 190 AC_VERB_SET_PIN_WIDGET_CONTROL, val) 191 192 /* restore the pinctl based on the cached value */ 193 static inline void restore_pin_ctl(struct hda_codec *codec, hda_nid_t pin) 194 { 195 update_pin_ctl(codec, pin, snd_hda_codec_get_pin_target(codec, pin)); 196 } 197 198 /* set the pinctl target value and write it if requested */ 199 static void set_pin_target(struct hda_codec *codec, hda_nid_t pin, 200 unsigned int val, bool do_write) 201 { 202 if (!pin) 203 return; 204 val = snd_hda_correct_pin_ctl(codec, pin, val); 205 snd_hda_codec_set_pin_target(codec, pin, val); 206 if (do_write) 207 update_pin_ctl(codec, pin, val); 208 } 209 210 /* set pinctl target values for all given pins */ 211 static void set_pin_targets(struct hda_codec *codec, int num_pins, 212 hda_nid_t *pins, unsigned int val) 213 { 214 int i; 215 for (i = 0; i < num_pins; i++) 216 set_pin_target(codec, pins[i], val, false); 217 } 218 219 /* 220 * parsing paths 221 */ 222 223 /* return the position of NID in the list, or -1 if not found */ 224 static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums) 225 { 226 int i; 227 for (i = 0; i < nums; i++) 228 if (list[i] == nid) 229 return i; 230 return -1; 231 } 232 233 /* return true if the given NID is contained in the path */ 234 static bool is_nid_contained(struct nid_path *path, hda_nid_t nid) 235 { 236 return find_idx_in_nid_list(nid, path->path, path->depth) >= 0; 237 } 238 239 static struct nid_path *get_nid_path(struct hda_codec *codec, 240 hda_nid_t from_nid, hda_nid_t to_nid, 241 int anchor_nid) 242 { 243 struct hda_gen_spec *spec = codec->spec; 244 int i; 245 246 for (i = 0; i < spec->paths.used; i++) { 247 struct nid_path *path = snd_array_elem(&spec->paths, i); 248 if (path->depth <= 0) 249 continue; 250 if ((!from_nid || path->path[0] == from_nid) && 251 (!to_nid || path->path[path->depth - 1] == to_nid)) { 252 if (!anchor_nid || 253 (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) || 254 (anchor_nid < 0 && !is_nid_contained(path, anchor_nid))) 255 return path; 256 } 257 } 258 return NULL; 259 } 260 261 /* get the path between the given NIDs; 262 * passing 0 to either @pin or @dac behaves as a wildcard 263 */ 264 struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec, 265 hda_nid_t from_nid, hda_nid_t to_nid) 266 { 267 return get_nid_path(codec, from_nid, to_nid, 0); 268 } 269 EXPORT_SYMBOL_HDA(snd_hda_get_nid_path); 270 271 /* get the index number corresponding to the path instance; 272 * the index starts from 1, for easier checking the invalid value 273 */ 274 int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path) 275 { 276 struct hda_gen_spec *spec = codec->spec; 277 struct nid_path *array = spec->paths.list; 278 ssize_t idx; 279 280 if (!spec->paths.used) 281 return 0; 282 idx = path - array; 283 if (idx < 0 || idx >= spec->paths.used) 284 return 0; 285 return idx + 1; 286 } 287 EXPORT_SYMBOL_HDA(snd_hda_get_path_idx); 288 289 /* get the path instance corresponding to the given index number */ 290 struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx) 291 { 292 struct hda_gen_spec *spec = codec->spec; 293 294 if (idx <= 0 || idx > spec->paths.used) 295 return NULL; 296 return snd_array_elem(&spec->paths, idx - 1); 297 } 298 EXPORT_SYMBOL_HDA(snd_hda_get_path_from_idx); 299 300 /* check whether the given DAC is already found in any existing paths */ 301 static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid) 302 { 303 struct hda_gen_spec *spec = codec->spec; 304 int i; 305 306 for (i = 0; i < spec->paths.used; i++) { 307 struct nid_path *path = snd_array_elem(&spec->paths, i); 308 if (path->path[0] == nid) 309 return true; 310 } 311 return false; 312 } 313 314 /* check whether the given two widgets can be connected */ 315 static bool is_reachable_path(struct hda_codec *codec, 316 hda_nid_t from_nid, hda_nid_t to_nid) 317 { 318 if (!from_nid || !to_nid) 319 return false; 320 return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0; 321 } 322 323 /* nid, dir and idx */ 324 #define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19)) 325 326 /* check whether the given ctl is already assigned in any path elements */ 327 static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type) 328 { 329 struct hda_gen_spec *spec = codec->spec; 330 int i; 331 332 val &= AMP_VAL_COMPARE_MASK; 333 for (i = 0; i < spec->paths.used; i++) { 334 struct nid_path *path = snd_array_elem(&spec->paths, i); 335 if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val) 336 return true; 337 } 338 return false; 339 } 340 341 /* check whether a control with the given (nid, dir, idx) was assigned */ 342 static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid, 343 int dir, int idx, int type) 344 { 345 unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir); 346 return is_ctl_used(codec, val, type); 347 } 348 349 static void print_nid_path(const char *pfx, struct nid_path *path) 350 { 351 char buf[40]; 352 int i; 353 354 355 buf[0] = 0; 356 for (i = 0; i < path->depth; i++) { 357 char tmp[4]; 358 sprintf(tmp, ":%02x", path->path[i]); 359 strlcat(buf, tmp, sizeof(buf)); 360 } 361 snd_printdd("%s path: depth=%d %s\n", pfx, path->depth, buf); 362 } 363 364 /* called recursively */ 365 static bool __parse_nid_path(struct hda_codec *codec, 366 hda_nid_t from_nid, hda_nid_t to_nid, 367 int anchor_nid, struct nid_path *path, 368 int depth) 369 { 370 const hda_nid_t *conn; 371 int i, nums; 372 373 if (to_nid == anchor_nid) 374 anchor_nid = 0; /* anchor passed */ 375 else if (to_nid == (hda_nid_t)(-anchor_nid)) 376 return false; /* hit the exclusive nid */ 377 378 nums = snd_hda_get_conn_list(codec, to_nid, &conn); 379 for (i = 0; i < nums; i++) { 380 if (conn[i] != from_nid) { 381 /* special case: when from_nid is 0, 382 * try to find an empty DAC 383 */ 384 if (from_nid || 385 get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT || 386 is_dac_already_used(codec, conn[i])) 387 continue; 388 } 389 /* anchor is not requested or already passed? */ 390 if (anchor_nid <= 0) 391 goto found; 392 } 393 if (depth >= MAX_NID_PATH_DEPTH) 394 return false; 395 for (i = 0; i < nums; i++) { 396 unsigned int type; 397 type = get_wcaps_type(get_wcaps(codec, conn[i])); 398 if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN || 399 type == AC_WID_PIN) 400 continue; 401 if (__parse_nid_path(codec, from_nid, conn[i], 402 anchor_nid, path, depth + 1)) 403 goto found; 404 } 405 return false; 406 407 found: 408 path->path[path->depth] = conn[i]; 409 path->idx[path->depth + 1] = i; 410 if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX) 411 path->multi[path->depth + 1] = 1; 412 path->depth++; 413 return true; 414 } 415 416 /* parse the widget path from the given nid to the target nid; 417 * when @from_nid is 0, try to find an empty DAC; 418 * when @anchor_nid is set to a positive value, only paths through the widget 419 * with the given value are evaluated. 420 * when @anchor_nid is set to a negative value, paths through the widget 421 * with the negative of given value are excluded, only other paths are chosen. 422 * when @anchor_nid is zero, no special handling about path selection. 423 */ 424 bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid, 425 hda_nid_t to_nid, int anchor_nid, 426 struct nid_path *path) 427 { 428 if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) { 429 path->path[path->depth] = to_nid; 430 path->depth++; 431 return true; 432 } 433 return false; 434 } 435 EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path); 436 437 /* 438 * parse the path between the given NIDs and add to the path list. 439 * if no valid path is found, return NULL 440 */ 441 struct nid_path * 442 snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid, 443 hda_nid_t to_nid, int anchor_nid) 444 { 445 struct hda_gen_spec *spec = codec->spec; 446 struct nid_path *path; 447 448 if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid)) 449 return NULL; 450 451 /* check whether the path has been already added */ 452 path = get_nid_path(codec, from_nid, to_nid, anchor_nid); 453 if (path) 454 return path; 455 456 path = snd_array_new(&spec->paths); 457 if (!path) 458 return NULL; 459 memset(path, 0, sizeof(*path)); 460 if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path)) 461 return path; 462 /* push back */ 463 spec->paths.used--; 464 return NULL; 465 } 466 EXPORT_SYMBOL_HDA(snd_hda_add_new_path); 467 468 /* clear the given path as invalid so that it won't be picked up later */ 469 static void invalidate_nid_path(struct hda_codec *codec, int idx) 470 { 471 struct nid_path *path = snd_hda_get_path_from_idx(codec, idx); 472 if (!path) 473 return; 474 memset(path, 0, sizeof(*path)); 475 } 476 477 /* look for an empty DAC slot */ 478 static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin, 479 bool is_digital) 480 { 481 struct hda_gen_spec *spec = codec->spec; 482 bool cap_digital; 483 int i; 484 485 for (i = 0; i < spec->num_all_dacs; i++) { 486 hda_nid_t nid = spec->all_dacs[i]; 487 if (!nid || is_dac_already_used(codec, nid)) 488 continue; 489 cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL); 490 if (is_digital != cap_digital) 491 continue; 492 if (is_reachable_path(codec, nid, pin)) 493 return nid; 494 } 495 return 0; 496 } 497 498 /* replace the channels in the composed amp value with the given number */ 499 static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs) 500 { 501 val &= ~(0x3U << 16); 502 val |= chs << 16; 503 return val; 504 } 505 506 /* check whether the widget has the given amp capability for the direction */ 507 static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid, 508 int dir, unsigned int bits) 509 { 510 if (!nid) 511 return false; 512 if (get_wcaps(codec, nid) & (1 << (dir + 1))) 513 if (query_amp_caps(codec, nid, dir) & bits) 514 return true; 515 return false; 516 } 517 518 static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1, 519 hda_nid_t nid2, int dir) 520 { 521 if (!(get_wcaps(codec, nid1) & (1 << (dir + 1)))) 522 return !(get_wcaps(codec, nid2) & (1 << (dir + 1))); 523 return (query_amp_caps(codec, nid1, dir) == 524 query_amp_caps(codec, nid2, dir)); 525 } 526 527 #define nid_has_mute(codec, nid, dir) \ 528 check_amp_caps(codec, nid, dir, (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) 529 #define nid_has_volume(codec, nid, dir) \ 530 check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS) 531 532 /* look for a widget suitable for assigning a mute switch in the path */ 533 static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec, 534 struct nid_path *path) 535 { 536 int i; 537 538 for (i = path->depth - 1; i >= 0; i--) { 539 if (nid_has_mute(codec, path->path[i], HDA_OUTPUT)) 540 return path->path[i]; 541 if (i != path->depth - 1 && i != 0 && 542 nid_has_mute(codec, path->path[i], HDA_INPUT)) 543 return path->path[i]; 544 } 545 return 0; 546 } 547 548 /* look for a widget suitable for assigning a volume ctl in the path */ 549 static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec, 550 struct nid_path *path) 551 { 552 int i; 553 554 for (i = path->depth - 1; i >= 0; i--) { 555 if (nid_has_volume(codec, path->path[i], HDA_OUTPUT)) 556 return path->path[i]; 557 } 558 return 0; 559 } 560 561 /* 562 * path activation / deactivation 563 */ 564 565 /* can have the amp-in capability? */ 566 static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx) 567 { 568 hda_nid_t nid = path->path[idx]; 569 unsigned int caps = get_wcaps(codec, nid); 570 unsigned int type = get_wcaps_type(caps); 571 572 if (!(caps & AC_WCAP_IN_AMP)) 573 return false; 574 if (type == AC_WID_PIN && idx > 0) /* only for input pins */ 575 return false; 576 return true; 577 } 578 579 /* can have the amp-out capability? */ 580 static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx) 581 { 582 hda_nid_t nid = path->path[idx]; 583 unsigned int caps = get_wcaps(codec, nid); 584 unsigned int type = get_wcaps_type(caps); 585 586 if (!(caps & AC_WCAP_OUT_AMP)) 587 return false; 588 if (type == AC_WID_PIN && !idx) /* only for output pins */ 589 return false; 590 return true; 591 } 592 593 /* check whether the given (nid,dir,idx) is active */ 594 static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid, 595 unsigned int dir, unsigned int idx) 596 { 597 struct hda_gen_spec *spec = codec->spec; 598 int i, n; 599 600 for (n = 0; n < spec->paths.used; n++) { 601 struct nid_path *path = snd_array_elem(&spec->paths, n); 602 if (!path->active) 603 continue; 604 for (i = 0; i < path->depth; i++) { 605 if (path->path[i] == nid) { 606 if (dir == HDA_OUTPUT || path->idx[i] == idx) 607 return true; 608 break; 609 } 610 } 611 } 612 return false; 613 } 614 615 /* check whether the NID is referred by any active paths */ 616 #define is_active_nid_for_any(codec, nid) \ 617 is_active_nid(codec, nid, HDA_OUTPUT, 0) 618 619 /* get the default amp value for the target state */ 620 static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid, 621 int dir, unsigned int caps, bool enable) 622 { 623 unsigned int val = 0; 624 625 if (caps & AC_AMPCAP_NUM_STEPS) { 626 /* set to 0dB */ 627 if (enable) 628 val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT; 629 } 630 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) { 631 if (!enable) 632 val |= HDA_AMP_MUTE; 633 } 634 return val; 635 } 636 637 /* initialize the amp value (only at the first time) */ 638 static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx) 639 { 640 unsigned int caps = query_amp_caps(codec, nid, dir); 641 int val = get_amp_val_to_activate(codec, nid, dir, caps, false); 642 snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val); 643 } 644 645 /* calculate amp value mask we can modify; 646 * if the given amp is controlled by mixers, don't touch it 647 */ 648 static unsigned int get_amp_mask_to_modify(struct hda_codec *codec, 649 hda_nid_t nid, int dir, int idx, 650 unsigned int caps) 651 { 652 unsigned int mask = 0xff; 653 654 if (caps & (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) { 655 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL)) 656 mask &= ~0x80; 657 } 658 if (caps & AC_AMPCAP_NUM_STEPS) { 659 if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) || 660 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL)) 661 mask &= ~0x7f; 662 } 663 return mask; 664 } 665 666 static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir, 667 int idx, int idx_to_check, bool enable) 668 { 669 unsigned int caps; 670 unsigned int mask, val; 671 672 if (!enable && is_active_nid(codec, nid, dir, idx_to_check)) 673 return; 674 675 caps = query_amp_caps(codec, nid, dir); 676 val = get_amp_val_to_activate(codec, nid, dir, caps, enable); 677 mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps); 678 if (!mask) 679 return; 680 681 val &= mask; 682 snd_hda_codec_amp_stereo(codec, nid, dir, idx, mask, val); 683 } 684 685 static void activate_amp_out(struct hda_codec *codec, struct nid_path *path, 686 int i, bool enable) 687 { 688 hda_nid_t nid = path->path[i]; 689 init_amp(codec, nid, HDA_OUTPUT, 0); 690 activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable); 691 } 692 693 static void activate_amp_in(struct hda_codec *codec, struct nid_path *path, 694 int i, bool enable, bool add_aamix) 695 { 696 struct hda_gen_spec *spec = codec->spec; 697 const hda_nid_t *conn; 698 int n, nums, idx; 699 int type; 700 hda_nid_t nid = path->path[i]; 701 702 nums = snd_hda_get_conn_list(codec, nid, &conn); 703 type = get_wcaps_type(get_wcaps(codec, nid)); 704 if (type == AC_WID_PIN || 705 (type == AC_WID_AUD_IN && codec->single_adc_amp)) { 706 nums = 1; 707 idx = 0; 708 } else 709 idx = path->idx[i]; 710 711 for (n = 0; n < nums; n++) 712 init_amp(codec, nid, HDA_INPUT, n); 713 714 /* here is a little bit tricky in comparison with activate_amp_out(); 715 * when aa-mixer is available, we need to enable the path as well 716 */ 717 for (n = 0; n < nums; n++) { 718 if (n != idx && (!add_aamix || conn[n] != spec->mixer_merge_nid)) 719 continue; 720 activate_amp(codec, nid, HDA_INPUT, n, idx, enable); 721 } 722 } 723 724 /* activate or deactivate the given path 725 * if @add_aamix is set, enable the input from aa-mix NID as well (if any) 726 */ 727 void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path, 728 bool enable, bool add_aamix) 729 { 730 struct hda_gen_spec *spec = codec->spec; 731 int i; 732 733 if (!enable) 734 path->active = false; 735 736 for (i = path->depth - 1; i >= 0; i--) { 737 hda_nid_t nid = path->path[i]; 738 if (enable && spec->power_down_unused) { 739 /* make sure the widget is powered up */ 740 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0)) 741 snd_hda_codec_write(codec, nid, 0, 742 AC_VERB_SET_POWER_STATE, 743 AC_PWRST_D0); 744 } 745 if (enable && path->multi[i]) 746 snd_hda_codec_write_cache(codec, nid, 0, 747 AC_VERB_SET_CONNECT_SEL, 748 path->idx[i]); 749 if (has_amp_in(codec, path, i)) 750 activate_amp_in(codec, path, i, enable, add_aamix); 751 if (has_amp_out(codec, path, i)) 752 activate_amp_out(codec, path, i, enable); 753 } 754 755 if (enable) 756 path->active = true; 757 } 758 EXPORT_SYMBOL_HDA(snd_hda_activate_path); 759 760 /* if the given path is inactive, put widgets into D3 (only if suitable) */ 761 static void path_power_down_sync(struct hda_codec *codec, struct nid_path *path) 762 { 763 struct hda_gen_spec *spec = codec->spec; 764 bool changed = false; 765 int i; 766 767 if (!spec->power_down_unused || path->active) 768 return; 769 770 for (i = 0; i < path->depth; i++) { 771 hda_nid_t nid = path->path[i]; 772 if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D3) && 773 !is_active_nid_for_any(codec, nid)) { 774 snd_hda_codec_write(codec, nid, 0, 775 AC_VERB_SET_POWER_STATE, 776 AC_PWRST_D3); 777 changed = true; 778 } 779 } 780 781 if (changed) { 782 msleep(10); 783 snd_hda_codec_read(codec, path->path[0], 0, 784 AC_VERB_GET_POWER_STATE, 0); 785 } 786 } 787 788 /* turn on/off EAPD on the given pin */ 789 static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable) 790 { 791 struct hda_gen_spec *spec = codec->spec; 792 if (spec->own_eapd_ctl || 793 !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD)) 794 return; 795 if (codec->inv_eapd) 796 enable = !enable; 797 if (spec->keep_eapd_on && !enable) 798 return; 799 snd_hda_codec_update_cache(codec, pin, 0, 800 AC_VERB_SET_EAPD_BTLENABLE, 801 enable ? 0x02 : 0x00); 802 } 803 804 /* re-initialize the path specified by the given path index */ 805 static void resume_path_from_idx(struct hda_codec *codec, int path_idx) 806 { 807 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx); 808 if (path) 809 snd_hda_activate_path(codec, path, path->active, false); 810 } 811 812 813 /* 814 * Helper functions for creating mixer ctl elements 815 */ 816 817 static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol, 818 struct snd_ctl_elem_value *ucontrol); 819 static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol, 820 struct snd_ctl_elem_value *ucontrol); 821 822 enum { 823 HDA_CTL_WIDGET_VOL, 824 HDA_CTL_WIDGET_MUTE, 825 HDA_CTL_BIND_MUTE, 826 }; 827 static const struct snd_kcontrol_new control_templates[] = { 828 HDA_CODEC_VOLUME(NULL, 0, 0, 0), 829 /* only the put callback is replaced for handling the special mute */ 830 { 831 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 832 .subdevice = HDA_SUBDEV_AMP_FLAG, 833 .info = snd_hda_mixer_amp_switch_info, 834 .get = snd_hda_mixer_amp_switch_get, 835 .put = hda_gen_mixer_mute_put, /* replaced */ 836 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0), 837 }, 838 { 839 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 840 .info = snd_hda_mixer_amp_switch_info, 841 .get = snd_hda_mixer_bind_switch_get, 842 .put = hda_gen_bind_mute_put, /* replaced */ 843 .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0), 844 }, 845 }; 846 847 /* add dynamic controls from template */ 848 static struct snd_kcontrol_new * 849 add_control(struct hda_gen_spec *spec, int type, const char *name, 850 int cidx, unsigned long val) 851 { 852 struct snd_kcontrol_new *knew; 853 854 knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]); 855 if (!knew) 856 return NULL; 857 knew->index = cidx; 858 if (get_amp_nid_(val)) 859 knew->subdevice = HDA_SUBDEV_AMP_FLAG; 860 knew->private_value = val; 861 return knew; 862 } 863 864 static int add_control_with_pfx(struct hda_gen_spec *spec, int type, 865 const char *pfx, const char *dir, 866 const char *sfx, int cidx, unsigned long val) 867 { 868 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN]; 869 snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx); 870 if (!add_control(spec, type, name, cidx, val)) 871 return -ENOMEM; 872 return 0; 873 } 874 875 #define add_pb_vol_ctrl(spec, type, pfx, val) \ 876 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val) 877 #define add_pb_sw_ctrl(spec, type, pfx, val) \ 878 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val) 879 #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \ 880 add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val) 881 #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \ 882 add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val) 883 884 static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx, 885 unsigned int chs, struct nid_path *path) 886 { 887 unsigned int val; 888 if (!path) 889 return 0; 890 val = path->ctls[NID_PATH_VOL_CTL]; 891 if (!val) 892 return 0; 893 val = amp_val_replace_channels(val, chs); 894 return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val); 895 } 896 897 /* return the channel bits suitable for the given path->ctls[] */ 898 static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path, 899 int type) 900 { 901 int chs = 1; /* mono (left only) */ 902 if (path) { 903 hda_nid_t nid = get_amp_nid_(path->ctls[type]); 904 if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO)) 905 chs = 3; /* stereo */ 906 } 907 return chs; 908 } 909 910 static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx, 911 struct nid_path *path) 912 { 913 int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL); 914 return add_vol_ctl(codec, pfx, cidx, chs, path); 915 } 916 917 /* create a mute-switch for the given mixer widget; 918 * if it has multiple sources (e.g. DAC and loopback), create a bind-mute 919 */ 920 static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx, 921 unsigned int chs, struct nid_path *path) 922 { 923 unsigned int val; 924 int type = HDA_CTL_WIDGET_MUTE; 925 926 if (!path) 927 return 0; 928 val = path->ctls[NID_PATH_MUTE_CTL]; 929 if (!val) 930 return 0; 931 val = amp_val_replace_channels(val, chs); 932 if (get_amp_direction_(val) == HDA_INPUT) { 933 hda_nid_t nid = get_amp_nid_(val); 934 int nums = snd_hda_get_num_conns(codec, nid); 935 if (nums > 1) { 936 type = HDA_CTL_BIND_MUTE; 937 val |= nums << 19; 938 } 939 } 940 return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val); 941 } 942 943 static int add_stereo_sw(struct hda_codec *codec, const char *pfx, 944 int cidx, struct nid_path *path) 945 { 946 int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL); 947 return add_sw_ctl(codec, pfx, cidx, chs, path); 948 } 949 950 /* playback mute control with the software mute bit check */ 951 static void sync_auto_mute_bits(struct snd_kcontrol *kcontrol, 952 struct snd_ctl_elem_value *ucontrol) 953 { 954 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 955 struct hda_gen_spec *spec = codec->spec; 956 957 if (spec->auto_mute_via_amp) { 958 hda_nid_t nid = get_amp_nid(kcontrol); 959 bool enabled = !((spec->mute_bits >> nid) & 1); 960 ucontrol->value.integer.value[0] &= enabled; 961 ucontrol->value.integer.value[1] &= enabled; 962 } 963 } 964 965 static int hda_gen_mixer_mute_put(struct snd_kcontrol *kcontrol, 966 struct snd_ctl_elem_value *ucontrol) 967 { 968 sync_auto_mute_bits(kcontrol, ucontrol); 969 return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol); 970 } 971 972 static int hda_gen_bind_mute_put(struct snd_kcontrol *kcontrol, 973 struct snd_ctl_elem_value *ucontrol) 974 { 975 sync_auto_mute_bits(kcontrol, ucontrol); 976 return snd_hda_mixer_bind_switch_put(kcontrol, ucontrol); 977 } 978 979 /* any ctl assigned to the path with the given index? */ 980 static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type) 981 { 982 struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx); 983 return path && path->ctls[ctl_type]; 984 } 985 986 static const char * const channel_name[4] = { 987 "Front", "Surround", "CLFE", "Side" 988 }; 989 990 /* give some appropriate ctl name prefix for the given line out channel */ 991 static const char *get_line_out_pfx(struct hda_codec *codec, int ch, 992 int *index, int ctl_type) 993 { 994 struct hda_gen_spec *spec = codec->spec; 995 struct auto_pin_cfg *cfg = &spec->autocfg; 996 997 *index = 0; 998 if (cfg->line_outs == 1 && !spec->multi_ios && 999 !cfg->hp_outs && !cfg->speaker_outs) 1000 return spec->vmaster_mute.hook ? "PCM" : "Master"; 1001 1002 /* if there is really a single DAC used in the whole output paths, 1003 * use it master (or "PCM" if a vmaster hook is present) 1004 */ 1005 if (spec->multiout.num_dacs == 1 && !spec->mixer_nid && 1006 !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0]) 1007 return spec->vmaster_mute.hook ? "PCM" : "Master"; 1008 1009 /* multi-io channels */ 1010 if (ch >= cfg->line_outs) 1011 return channel_name[ch]; 1012 1013 switch (cfg->line_out_type) { 1014 case AUTO_PIN_SPEAKER_OUT: 1015 /* if the primary channel vol/mute is shared with HP volume, 1016 * don't name it as Speaker 1017 */ 1018 if (!ch && cfg->hp_outs && 1019 !path_has_mixer(codec, spec->hp_paths[0], ctl_type)) 1020 break; 1021 if (cfg->line_outs == 1) 1022 return "Speaker"; 1023 if (cfg->line_outs == 2) 1024 return ch ? "Bass Speaker" : "Speaker"; 1025 break; 1026 case AUTO_PIN_HP_OUT: 1027 /* if the primary channel vol/mute is shared with spk volume, 1028 * don't name it as Headphone 1029 */ 1030 if (!ch && cfg->speaker_outs && 1031 !path_has_mixer(codec, spec->speaker_paths[0], ctl_type)) 1032 break; 1033 /* for multi-io case, only the primary out */ 1034 if (ch && spec->multi_ios) 1035 break; 1036 *index = ch; 1037 return "Headphone"; 1038 } 1039 1040 /* for a single channel output, we don't have to name the channel */ 1041 if (cfg->line_outs == 1 && !spec->multi_ios) 1042 return "PCM"; 1043 1044 if (ch >= ARRAY_SIZE(channel_name)) { 1045 snd_BUG(); 1046 return "PCM"; 1047 } 1048 1049 return channel_name[ch]; 1050 } 1051 1052 /* 1053 * Parse output paths 1054 */ 1055 1056 /* badness definition */ 1057 enum { 1058 /* No primary DAC is found for the main output */ 1059 BAD_NO_PRIMARY_DAC = 0x10000, 1060 /* No DAC is found for the extra output */ 1061 BAD_NO_DAC = 0x4000, 1062 /* No possible multi-ios */ 1063 BAD_MULTI_IO = 0x120, 1064 /* No individual DAC for extra output */ 1065 BAD_NO_EXTRA_DAC = 0x102, 1066 /* No individual DAC for extra surrounds */ 1067 BAD_NO_EXTRA_SURR_DAC = 0x101, 1068 /* Primary DAC shared with main surrounds */ 1069 BAD_SHARED_SURROUND = 0x100, 1070 /* No independent HP possible */ 1071 BAD_NO_INDEP_HP = 0x10, 1072 /* Primary DAC shared with main CLFE */ 1073 BAD_SHARED_CLFE = 0x10, 1074 /* Primary DAC shared with extra surrounds */ 1075 BAD_SHARED_EXTRA_SURROUND = 0x10, 1076 /* Volume widget is shared */ 1077 BAD_SHARED_VOL = 0x10, 1078 }; 1079 1080 /* look for widgets in the given path which are appropriate for 1081 * volume and mute controls, and assign the values to ctls[]. 1082 * 1083 * When no appropriate widget is found in the path, the badness value 1084 * is incremented depending on the situation. The function returns the 1085 * total badness for both volume and mute controls. 1086 */ 1087 static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path) 1088 { 1089 hda_nid_t nid; 1090 unsigned int val; 1091 int badness = 0; 1092 1093 if (!path) 1094 return BAD_SHARED_VOL * 2; 1095 1096 if (path->ctls[NID_PATH_VOL_CTL] || 1097 path->ctls[NID_PATH_MUTE_CTL]) 1098 return 0; /* already evaluated */ 1099 1100 nid = look_for_out_vol_nid(codec, path); 1101 if (nid) { 1102 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT); 1103 if (is_ctl_used(codec, val, NID_PATH_VOL_CTL)) 1104 badness += BAD_SHARED_VOL; 1105 else 1106 path->ctls[NID_PATH_VOL_CTL] = val; 1107 } else 1108 badness += BAD_SHARED_VOL; 1109 nid = look_for_out_mute_nid(codec, path); 1110 if (nid) { 1111 unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid)); 1112 if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT || 1113 nid_has_mute(codec, nid, HDA_OUTPUT)) 1114 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT); 1115 else 1116 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT); 1117 if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL)) 1118 badness += BAD_SHARED_VOL; 1119 else 1120 path->ctls[NID_PATH_MUTE_CTL] = val; 1121 } else 1122 badness += BAD_SHARED_VOL; 1123 return badness; 1124 } 1125 1126 const struct badness_table hda_main_out_badness = { 1127 .no_primary_dac = BAD_NO_PRIMARY_DAC, 1128 .no_dac = BAD_NO_DAC, 1129 .shared_primary = BAD_NO_PRIMARY_DAC, 1130 .shared_surr = BAD_SHARED_SURROUND, 1131 .shared_clfe = BAD_SHARED_CLFE, 1132 .shared_surr_main = BAD_SHARED_SURROUND, 1133 }; 1134 EXPORT_SYMBOL_HDA(hda_main_out_badness); 1135 1136 const struct badness_table hda_extra_out_badness = { 1137 .no_primary_dac = BAD_NO_DAC, 1138 .no_dac = BAD_NO_DAC, 1139 .shared_primary = BAD_NO_EXTRA_DAC, 1140 .shared_surr = BAD_SHARED_EXTRA_SURROUND, 1141 .shared_clfe = BAD_SHARED_EXTRA_SURROUND, 1142 .shared_surr_main = BAD_NO_EXTRA_SURR_DAC, 1143 }; 1144 EXPORT_SYMBOL_HDA(hda_extra_out_badness); 1145 1146 /* get the DAC of the primary output corresponding to the given array index */ 1147 static hda_nid_t get_primary_out(struct hda_codec *codec, int idx) 1148 { 1149 struct hda_gen_spec *spec = codec->spec; 1150 struct auto_pin_cfg *cfg = &spec->autocfg; 1151 1152 if (cfg->line_outs > idx) 1153 return spec->private_dac_nids[idx]; 1154 idx -= cfg->line_outs; 1155 if (spec->multi_ios > idx) 1156 return spec->multi_io[idx].dac; 1157 return 0; 1158 } 1159 1160 /* return the DAC if it's reachable, otherwise zero */ 1161 static inline hda_nid_t try_dac(struct hda_codec *codec, 1162 hda_nid_t dac, hda_nid_t pin) 1163 { 1164 return is_reachable_path(codec, dac, pin) ? dac : 0; 1165 } 1166 1167 /* try to assign DACs to pins and return the resultant badness */ 1168 static int try_assign_dacs(struct hda_codec *codec, int num_outs, 1169 const hda_nid_t *pins, hda_nid_t *dacs, 1170 int *path_idx, 1171 const struct badness_table *bad) 1172 { 1173 struct hda_gen_spec *spec = codec->spec; 1174 int i, j; 1175 int badness = 0; 1176 hda_nid_t dac; 1177 1178 if (!num_outs) 1179 return 0; 1180 1181 for (i = 0; i < num_outs; i++) { 1182 struct nid_path *path; 1183 hda_nid_t pin = pins[i]; 1184 1185 path = snd_hda_get_path_from_idx(codec, path_idx[i]); 1186 if (path) { 1187 badness += assign_out_path_ctls(codec, path); 1188 continue; 1189 } 1190 1191 dacs[i] = look_for_dac(codec, pin, false); 1192 if (!dacs[i] && !i) { 1193 /* try to steal the DAC of surrounds for the front */ 1194 for (j = 1; j < num_outs; j++) { 1195 if (is_reachable_path(codec, dacs[j], pin)) { 1196 dacs[0] = dacs[j]; 1197 dacs[j] = 0; 1198 invalidate_nid_path(codec, path_idx[j]); 1199 path_idx[j] = 0; 1200 break; 1201 } 1202 } 1203 } 1204 dac = dacs[i]; 1205 if (!dac) { 1206 if (num_outs > 2) 1207 dac = try_dac(codec, get_primary_out(codec, i), pin); 1208 if (!dac) 1209 dac = try_dac(codec, dacs[0], pin); 1210 if (!dac) 1211 dac = try_dac(codec, get_primary_out(codec, i), pin); 1212 if (dac) { 1213 if (!i) 1214 badness += bad->shared_primary; 1215 else if (i == 1) 1216 badness += bad->shared_surr; 1217 else 1218 badness += bad->shared_clfe; 1219 } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) { 1220 dac = spec->private_dac_nids[0]; 1221 badness += bad->shared_surr_main; 1222 } else if (!i) 1223 badness += bad->no_primary_dac; 1224 else 1225 badness += bad->no_dac; 1226 } 1227 if (!dac) 1228 continue; 1229 path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid); 1230 if (!path && !i && spec->mixer_nid) { 1231 /* try with aamix */ 1232 path = snd_hda_add_new_path(codec, dac, pin, 0); 1233 } 1234 if (!path) { 1235 dac = dacs[i] = 0; 1236 badness += bad->no_dac; 1237 } else { 1238 /* print_nid_path("output", path); */ 1239 path->active = true; 1240 path_idx[i] = snd_hda_get_path_idx(codec, path); 1241 badness += assign_out_path_ctls(codec, path); 1242 } 1243 } 1244 1245 return badness; 1246 } 1247 1248 /* return NID if the given pin has only a single connection to a certain DAC */ 1249 static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin) 1250 { 1251 struct hda_gen_spec *spec = codec->spec; 1252 int i; 1253 hda_nid_t nid_found = 0; 1254 1255 for (i = 0; i < spec->num_all_dacs; i++) { 1256 hda_nid_t nid = spec->all_dacs[i]; 1257 if (!nid || is_dac_already_used(codec, nid)) 1258 continue; 1259 if (is_reachable_path(codec, nid, pin)) { 1260 if (nid_found) 1261 return 0; 1262 nid_found = nid; 1263 } 1264 } 1265 return nid_found; 1266 } 1267 1268 /* check whether the given pin can be a multi-io pin */ 1269 static bool can_be_multiio_pin(struct hda_codec *codec, 1270 unsigned int location, hda_nid_t nid) 1271 { 1272 unsigned int defcfg, caps; 1273 1274 defcfg = snd_hda_codec_get_pincfg(codec, nid); 1275 if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX) 1276 return false; 1277 if (location && get_defcfg_location(defcfg) != location) 1278 return false; 1279 caps = snd_hda_query_pin_caps(codec, nid); 1280 if (!(caps & AC_PINCAP_OUT)) 1281 return false; 1282 return true; 1283 } 1284 1285 /* count the number of input pins that are capable to be multi-io */ 1286 static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin) 1287 { 1288 struct hda_gen_spec *spec = codec->spec; 1289 struct auto_pin_cfg *cfg = &spec->autocfg; 1290 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin); 1291 unsigned int location = get_defcfg_location(defcfg); 1292 int type, i; 1293 int num_pins = 0; 1294 1295 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) { 1296 for (i = 0; i < cfg->num_inputs; i++) { 1297 if (cfg->inputs[i].type != type) 1298 continue; 1299 if (can_be_multiio_pin(codec, location, 1300 cfg->inputs[i].pin)) 1301 num_pins++; 1302 } 1303 } 1304 return num_pins; 1305 } 1306 1307 /* 1308 * multi-io helper 1309 * 1310 * When hardwired is set, try to fill ony hardwired pins, and returns 1311 * zero if any pins are filled, non-zero if nothing found. 1312 * When hardwired is off, try to fill possible input pins, and returns 1313 * the badness value. 1314 */ 1315 static int fill_multi_ios(struct hda_codec *codec, 1316 hda_nid_t reference_pin, 1317 bool hardwired) 1318 { 1319 struct hda_gen_spec *spec = codec->spec; 1320 struct auto_pin_cfg *cfg = &spec->autocfg; 1321 int type, i, j, num_pins, old_pins; 1322 unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin); 1323 unsigned int location = get_defcfg_location(defcfg); 1324 int badness = 0; 1325 struct nid_path *path; 1326 1327 old_pins = spec->multi_ios; 1328 if (old_pins >= 2) 1329 goto end_fill; 1330 1331 num_pins = count_multiio_pins(codec, reference_pin); 1332 if (num_pins < 2) 1333 goto end_fill; 1334 1335 for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) { 1336 for (i = 0; i < cfg->num_inputs; i++) { 1337 hda_nid_t nid = cfg->inputs[i].pin; 1338 hda_nid_t dac = 0; 1339 1340 if (cfg->inputs[i].type != type) 1341 continue; 1342 if (!can_be_multiio_pin(codec, location, nid)) 1343 continue; 1344 for (j = 0; j < spec->multi_ios; j++) { 1345 if (nid == spec->multi_io[j].pin) 1346 break; 1347 } 1348 if (j < spec->multi_ios) 1349 continue; 1350 1351 if (hardwired) 1352 dac = get_dac_if_single(codec, nid); 1353 else if (!dac) 1354 dac = look_for_dac(codec, nid, false); 1355 if (!dac) { 1356 badness++; 1357 continue; 1358 } 1359 path = snd_hda_add_new_path(codec, dac, nid, 1360 -spec->mixer_nid); 1361 if (!path) { 1362 badness++; 1363 continue; 1364 } 1365 /* print_nid_path("multiio", path); */ 1366 spec->multi_io[spec->multi_ios].pin = nid; 1367 spec->multi_io[spec->multi_ios].dac = dac; 1368 spec->out_paths[cfg->line_outs + spec->multi_ios] = 1369 snd_hda_get_path_idx(codec, path); 1370 spec->multi_ios++; 1371 if (spec->multi_ios >= 2) 1372 break; 1373 } 1374 } 1375 end_fill: 1376 if (badness) 1377 badness = BAD_MULTI_IO; 1378 if (old_pins == spec->multi_ios) { 1379 if (hardwired) 1380 return 1; /* nothing found */ 1381 else 1382 return badness; /* no badness if nothing found */ 1383 } 1384 if (!hardwired && spec->multi_ios < 2) { 1385 /* cancel newly assigned paths */ 1386 spec->paths.used -= spec->multi_ios - old_pins; 1387 spec->multi_ios = old_pins; 1388 return badness; 1389 } 1390 1391 /* assign volume and mute controls */ 1392 for (i = old_pins; i < spec->multi_ios; i++) { 1393 path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]); 1394 badness += assign_out_path_ctls(codec, path); 1395 } 1396 1397 return badness; 1398 } 1399 1400 /* map DACs for all pins in the list if they are single connections */ 1401 static bool map_singles(struct hda_codec *codec, int outs, 1402 const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx) 1403 { 1404 struct hda_gen_spec *spec = codec->spec; 1405 int i; 1406 bool found = false; 1407 for (i = 0; i < outs; i++) { 1408 struct nid_path *path; 1409 hda_nid_t dac; 1410 if (dacs[i]) 1411 continue; 1412 dac = get_dac_if_single(codec, pins[i]); 1413 if (!dac) 1414 continue; 1415 path = snd_hda_add_new_path(codec, dac, pins[i], 1416 -spec->mixer_nid); 1417 if (!path && !i && spec->mixer_nid) 1418 path = snd_hda_add_new_path(codec, dac, pins[i], 0); 1419 if (path) { 1420 dacs[i] = dac; 1421 found = true; 1422 /* print_nid_path("output", path); */ 1423 path->active = true; 1424 path_idx[i] = snd_hda_get_path_idx(codec, path); 1425 } 1426 } 1427 return found; 1428 } 1429 1430 /* create a new path including aamix if available, and return its index */ 1431 static int check_aamix_out_path(struct hda_codec *codec, int path_idx) 1432 { 1433 struct hda_gen_spec *spec = codec->spec; 1434 struct nid_path *path; 1435 hda_nid_t path_dac, dac, pin; 1436 1437 path = snd_hda_get_path_from_idx(codec, path_idx); 1438 if (!path || !path->depth || 1439 is_nid_contained(path, spec->mixer_nid)) 1440 return 0; 1441 path_dac = path->path[0]; 1442 dac = spec->private_dac_nids[0]; 1443 pin = path->path[path->depth - 1]; 1444 path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid); 1445 if (!path) { 1446 if (dac != path_dac) 1447 dac = path_dac; 1448 else if (spec->multiout.hp_out_nid[0]) 1449 dac = spec->multiout.hp_out_nid[0]; 1450 else if (spec->multiout.extra_out_nid[0]) 1451 dac = spec->multiout.extra_out_nid[0]; 1452 else 1453 dac = 0; 1454 if (dac) 1455 path = snd_hda_add_new_path(codec, dac, pin, 1456 spec->mixer_nid); 1457 } 1458 if (!path) 1459 return 0; 1460 /* print_nid_path("output-aamix", path); */ 1461 path->active = false; /* unused as default */ 1462 return snd_hda_get_path_idx(codec, path); 1463 } 1464 1465 /* check whether the independent HP is available with the current config */ 1466 static bool indep_hp_possible(struct hda_codec *codec) 1467 { 1468 struct hda_gen_spec *spec = codec->spec; 1469 struct auto_pin_cfg *cfg = &spec->autocfg; 1470 struct nid_path *path; 1471 int i, idx; 1472 1473 if (cfg->line_out_type == AUTO_PIN_HP_OUT) 1474 idx = spec->out_paths[0]; 1475 else 1476 idx = spec->hp_paths[0]; 1477 path = snd_hda_get_path_from_idx(codec, idx); 1478 if (!path) 1479 return false; 1480 1481 /* assume no path conflicts unless aamix is involved */ 1482 if (!spec->mixer_nid || !is_nid_contained(path, spec->mixer_nid)) 1483 return true; 1484 1485 /* check whether output paths contain aamix */ 1486 for (i = 0; i < cfg->line_outs; i++) { 1487 if (spec->out_paths[i] == idx) 1488 break; 1489 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]); 1490 if (path && is_nid_contained(path, spec->mixer_nid)) 1491 return false; 1492 } 1493 for (i = 0; i < cfg->speaker_outs; i++) { 1494 path = snd_hda_get_path_from_idx(codec, spec->speaker_paths[i]); 1495 if (path && is_nid_contained(path, spec->mixer_nid)) 1496 return false; 1497 } 1498 1499 return true; 1500 } 1501 1502 /* fill the empty entries in the dac array for speaker/hp with the 1503 * shared dac pointed by the paths 1504 */ 1505 static void refill_shared_dacs(struct hda_codec *codec, int num_outs, 1506 hda_nid_t *dacs, int *path_idx) 1507 { 1508 struct nid_path *path; 1509 int i; 1510 1511 for (i = 0; i < num_outs; i++) { 1512 if (dacs[i]) 1513 continue; 1514 path = snd_hda_get_path_from_idx(codec, path_idx[i]); 1515 if (!path) 1516 continue; 1517 dacs[i] = path->path[0]; 1518 } 1519 } 1520 1521 /* fill in the dac_nids table from the parsed pin configuration */ 1522 static int fill_and_eval_dacs(struct hda_codec *codec, 1523 bool fill_hardwired, 1524 bool fill_mio_first) 1525 { 1526 struct hda_gen_spec *spec = codec->spec; 1527 struct auto_pin_cfg *cfg = &spec->autocfg; 1528 int i, err, badness; 1529 1530 /* set num_dacs once to full for look_for_dac() */ 1531 spec->multiout.num_dacs = cfg->line_outs; 1532 spec->multiout.dac_nids = spec->private_dac_nids; 1533 memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids)); 1534 memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid)); 1535 memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid)); 1536 spec->multi_ios = 0; 1537 snd_array_free(&spec->paths); 1538 1539 /* clear path indices */ 1540 memset(spec->out_paths, 0, sizeof(spec->out_paths)); 1541 memset(spec->hp_paths, 0, sizeof(spec->hp_paths)); 1542 memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths)); 1543 memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths)); 1544 memset(spec->digout_paths, 0, sizeof(spec->digout_paths)); 1545 memset(spec->input_paths, 0, sizeof(spec->input_paths)); 1546 memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths)); 1547 memset(&spec->digin_path, 0, sizeof(spec->digin_path)); 1548 1549 badness = 0; 1550 1551 /* fill hard-wired DACs first */ 1552 if (fill_hardwired) { 1553 bool mapped; 1554 do { 1555 mapped = map_singles(codec, cfg->line_outs, 1556 cfg->line_out_pins, 1557 spec->private_dac_nids, 1558 spec->out_paths); 1559 mapped |= map_singles(codec, cfg->hp_outs, 1560 cfg->hp_pins, 1561 spec->multiout.hp_out_nid, 1562 spec->hp_paths); 1563 mapped |= map_singles(codec, cfg->speaker_outs, 1564 cfg->speaker_pins, 1565 spec->multiout.extra_out_nid, 1566 spec->speaker_paths); 1567 if (!spec->no_multi_io && 1568 fill_mio_first && cfg->line_outs == 1 && 1569 cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) { 1570 err = fill_multi_ios(codec, cfg->line_out_pins[0], true); 1571 if (!err) 1572 mapped = true; 1573 } 1574 } while (mapped); 1575 } 1576 1577 badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins, 1578 spec->private_dac_nids, spec->out_paths, 1579 spec->main_out_badness); 1580 1581 if (!spec->no_multi_io && fill_mio_first && 1582 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) { 1583 /* try to fill multi-io first */ 1584 err = fill_multi_ios(codec, cfg->line_out_pins[0], false); 1585 if (err < 0) 1586 return err; 1587 /* we don't count badness at this stage yet */ 1588 } 1589 1590 if (cfg->line_out_type != AUTO_PIN_HP_OUT) { 1591 err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins, 1592 spec->multiout.hp_out_nid, 1593 spec->hp_paths, 1594 spec->extra_out_badness); 1595 if (err < 0) 1596 return err; 1597 badness += err; 1598 } 1599 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) { 1600 err = try_assign_dacs(codec, cfg->speaker_outs, 1601 cfg->speaker_pins, 1602 spec->multiout.extra_out_nid, 1603 spec->speaker_paths, 1604 spec->extra_out_badness); 1605 if (err < 0) 1606 return err; 1607 badness += err; 1608 } 1609 if (!spec->no_multi_io && 1610 cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) { 1611 err = fill_multi_ios(codec, cfg->line_out_pins[0], false); 1612 if (err < 0) 1613 return err; 1614 badness += err; 1615 } 1616 1617 if (spec->mixer_nid) { 1618 spec->aamix_out_paths[0] = 1619 check_aamix_out_path(codec, spec->out_paths[0]); 1620 if (cfg->line_out_type != AUTO_PIN_HP_OUT) 1621 spec->aamix_out_paths[1] = 1622 check_aamix_out_path(codec, spec->hp_paths[0]); 1623 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) 1624 spec->aamix_out_paths[2] = 1625 check_aamix_out_path(codec, spec->speaker_paths[0]); 1626 } 1627 1628 if (!spec->no_multi_io && 1629 cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) 1630 if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2) 1631 spec->multi_ios = 1; /* give badness */ 1632 1633 /* re-count num_dacs and squash invalid entries */ 1634 spec->multiout.num_dacs = 0; 1635 for (i = 0; i < cfg->line_outs; i++) { 1636 if (spec->private_dac_nids[i]) 1637 spec->multiout.num_dacs++; 1638 else { 1639 memmove(spec->private_dac_nids + i, 1640 spec->private_dac_nids + i + 1, 1641 sizeof(hda_nid_t) * (cfg->line_outs - i - 1)); 1642 spec->private_dac_nids[cfg->line_outs - 1] = 0; 1643 } 1644 } 1645 1646 spec->ext_channel_count = spec->min_channel_count = 1647 spec->multiout.num_dacs * 2; 1648 1649 if (spec->multi_ios == 2) { 1650 for (i = 0; i < 2; i++) 1651 spec->private_dac_nids[spec->multiout.num_dacs++] = 1652 spec->multi_io[i].dac; 1653 } else if (spec->multi_ios) { 1654 spec->multi_ios = 0; 1655 badness += BAD_MULTI_IO; 1656 } 1657 1658 if (spec->indep_hp && !indep_hp_possible(codec)) 1659 badness += BAD_NO_INDEP_HP; 1660 1661 /* re-fill the shared DAC for speaker / headphone */ 1662 if (cfg->line_out_type != AUTO_PIN_HP_OUT) 1663 refill_shared_dacs(codec, cfg->hp_outs, 1664 spec->multiout.hp_out_nid, 1665 spec->hp_paths); 1666 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) 1667 refill_shared_dacs(codec, cfg->speaker_outs, 1668 spec->multiout.extra_out_nid, 1669 spec->speaker_paths); 1670 1671 return badness; 1672 } 1673 1674 #define DEBUG_BADNESS 1675 1676 #ifdef DEBUG_BADNESS 1677 #define debug_badness snd_printdd 1678 #else 1679 #define debug_badness(...) 1680 #endif 1681 1682 #ifdef DEBUG_BADNESS 1683 static inline void print_nid_path_idx(struct hda_codec *codec, 1684 const char *pfx, int idx) 1685 { 1686 struct nid_path *path; 1687 1688 path = snd_hda_get_path_from_idx(codec, idx); 1689 if (path) 1690 print_nid_path(pfx, path); 1691 } 1692 1693 static void debug_show_configs(struct hda_codec *codec, 1694 struct auto_pin_cfg *cfg) 1695 { 1696 struct hda_gen_spec *spec = codec->spec; 1697 static const char * const lo_type[3] = { "LO", "SP", "HP" }; 1698 int i; 1699 1700 debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n", 1701 cfg->line_out_pins[0], cfg->line_out_pins[1], 1702 cfg->line_out_pins[2], cfg->line_out_pins[3], 1703 spec->multiout.dac_nids[0], 1704 spec->multiout.dac_nids[1], 1705 spec->multiout.dac_nids[2], 1706 spec->multiout.dac_nids[3], 1707 lo_type[cfg->line_out_type]); 1708 for (i = 0; i < cfg->line_outs; i++) 1709 print_nid_path_idx(codec, " out", spec->out_paths[i]); 1710 if (spec->multi_ios > 0) 1711 debug_badness("multi_ios(%d) = %x/%x : %x/%x\n", 1712 spec->multi_ios, 1713 spec->multi_io[0].pin, spec->multi_io[1].pin, 1714 spec->multi_io[0].dac, spec->multi_io[1].dac); 1715 for (i = 0; i < spec->multi_ios; i++) 1716 print_nid_path_idx(codec, " mio", 1717 spec->out_paths[cfg->line_outs + i]); 1718 if (cfg->hp_outs) 1719 debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n", 1720 cfg->hp_pins[0], cfg->hp_pins[1], 1721 cfg->hp_pins[2], cfg->hp_pins[3], 1722 spec->multiout.hp_out_nid[0], 1723 spec->multiout.hp_out_nid[1], 1724 spec->multiout.hp_out_nid[2], 1725 spec->multiout.hp_out_nid[3]); 1726 for (i = 0; i < cfg->hp_outs; i++) 1727 print_nid_path_idx(codec, " hp ", spec->hp_paths[i]); 1728 if (cfg->speaker_outs) 1729 debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n", 1730 cfg->speaker_pins[0], cfg->speaker_pins[1], 1731 cfg->speaker_pins[2], cfg->speaker_pins[3], 1732 spec->multiout.extra_out_nid[0], 1733 spec->multiout.extra_out_nid[1], 1734 spec->multiout.extra_out_nid[2], 1735 spec->multiout.extra_out_nid[3]); 1736 for (i = 0; i < cfg->speaker_outs; i++) 1737 print_nid_path_idx(codec, " spk", spec->speaker_paths[i]); 1738 for (i = 0; i < 3; i++) 1739 print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]); 1740 } 1741 #else 1742 #define debug_show_configs(codec, cfg) /* NOP */ 1743 #endif 1744 1745 /* find all available DACs of the codec */ 1746 static void fill_all_dac_nids(struct hda_codec *codec) 1747 { 1748 struct hda_gen_spec *spec = codec->spec; 1749 int i; 1750 hda_nid_t nid = codec->start_nid; 1751 1752 spec->num_all_dacs = 0; 1753 memset(spec->all_dacs, 0, sizeof(spec->all_dacs)); 1754 for (i = 0; i < codec->num_nodes; i++, nid++) { 1755 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT) 1756 continue; 1757 if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) { 1758 snd_printk(KERN_ERR "hda: Too many DACs!\n"); 1759 break; 1760 } 1761 spec->all_dacs[spec->num_all_dacs++] = nid; 1762 } 1763 } 1764 1765 static int parse_output_paths(struct hda_codec *codec) 1766 { 1767 struct hda_gen_spec *spec = codec->spec; 1768 struct auto_pin_cfg *cfg = &spec->autocfg; 1769 struct auto_pin_cfg *best_cfg; 1770 unsigned int val; 1771 int best_badness = INT_MAX; 1772 int badness; 1773 bool fill_hardwired = true, fill_mio_first = true; 1774 bool best_wired = true, best_mio = true; 1775 bool hp_spk_swapped = false; 1776 1777 best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL); 1778 if (!best_cfg) 1779 return -ENOMEM; 1780 *best_cfg = *cfg; 1781 1782 for (;;) { 1783 badness = fill_and_eval_dacs(codec, fill_hardwired, 1784 fill_mio_first); 1785 if (badness < 0) { 1786 kfree(best_cfg); 1787 return badness; 1788 } 1789 debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n", 1790 cfg->line_out_type, fill_hardwired, fill_mio_first, 1791 badness); 1792 debug_show_configs(codec, cfg); 1793 if (badness < best_badness) { 1794 best_badness = badness; 1795 *best_cfg = *cfg; 1796 best_wired = fill_hardwired; 1797 best_mio = fill_mio_first; 1798 } 1799 if (!badness) 1800 break; 1801 fill_mio_first = !fill_mio_first; 1802 if (!fill_mio_first) 1803 continue; 1804 fill_hardwired = !fill_hardwired; 1805 if (!fill_hardwired) 1806 continue; 1807 if (hp_spk_swapped) 1808 break; 1809 hp_spk_swapped = true; 1810 if (cfg->speaker_outs > 0 && 1811 cfg->line_out_type == AUTO_PIN_HP_OUT) { 1812 cfg->hp_outs = cfg->line_outs; 1813 memcpy(cfg->hp_pins, cfg->line_out_pins, 1814 sizeof(cfg->hp_pins)); 1815 cfg->line_outs = cfg->speaker_outs; 1816 memcpy(cfg->line_out_pins, cfg->speaker_pins, 1817 sizeof(cfg->speaker_pins)); 1818 cfg->speaker_outs = 0; 1819 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins)); 1820 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT; 1821 fill_hardwired = true; 1822 continue; 1823 } 1824 if (cfg->hp_outs > 0 && 1825 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) { 1826 cfg->speaker_outs = cfg->line_outs; 1827 memcpy(cfg->speaker_pins, cfg->line_out_pins, 1828 sizeof(cfg->speaker_pins)); 1829 cfg->line_outs = cfg->hp_outs; 1830 memcpy(cfg->line_out_pins, cfg->hp_pins, 1831 sizeof(cfg->hp_pins)); 1832 cfg->hp_outs = 0; 1833 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins)); 1834 cfg->line_out_type = AUTO_PIN_HP_OUT; 1835 fill_hardwired = true; 1836 continue; 1837 } 1838 break; 1839 } 1840 1841 if (badness) { 1842 debug_badness("==> restoring best_cfg\n"); 1843 *cfg = *best_cfg; 1844 fill_and_eval_dacs(codec, best_wired, best_mio); 1845 } 1846 debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n", 1847 cfg->line_out_type, best_wired, best_mio); 1848 debug_show_configs(codec, cfg); 1849 1850 if (cfg->line_out_pins[0]) { 1851 struct nid_path *path; 1852 path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]); 1853 if (path) 1854 spec->vmaster_nid = look_for_out_vol_nid(codec, path); 1855 if (spec->vmaster_nid) 1856 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid, 1857 HDA_OUTPUT, spec->vmaster_tlv); 1858 } 1859 1860 /* set initial pinctl targets */ 1861 if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT) 1862 val = PIN_HP; 1863 else 1864 val = PIN_OUT; 1865 set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val); 1866 if (cfg->line_out_type != AUTO_PIN_HP_OUT) 1867 set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP); 1868 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) { 1869 val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT; 1870 set_pin_targets(codec, cfg->speaker_outs, 1871 cfg->speaker_pins, val); 1872 } 1873 1874 /* clear indep_hp flag if not available */ 1875 if (spec->indep_hp && !indep_hp_possible(codec)) 1876 spec->indep_hp = 0; 1877 1878 kfree(best_cfg); 1879 return 0; 1880 } 1881 1882 /* add playback controls from the parsed DAC table */ 1883 static int create_multi_out_ctls(struct hda_codec *codec, 1884 const struct auto_pin_cfg *cfg) 1885 { 1886 struct hda_gen_spec *spec = codec->spec; 1887 int i, err, noutputs; 1888 1889 noutputs = cfg->line_outs; 1890 if (spec->multi_ios > 0 && cfg->line_outs < 3) 1891 noutputs += spec->multi_ios; 1892 1893 for (i = 0; i < noutputs; i++) { 1894 const char *name; 1895 int index; 1896 struct nid_path *path; 1897 1898 path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]); 1899 if (!path) 1900 continue; 1901 1902 name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL); 1903 if (!name || !strcmp(name, "CLFE")) { 1904 /* Center/LFE */ 1905 err = add_vol_ctl(codec, "Center", 0, 1, path); 1906 if (err < 0) 1907 return err; 1908 err = add_vol_ctl(codec, "LFE", 0, 2, path); 1909 if (err < 0) 1910 return err; 1911 } else { 1912 err = add_stereo_vol(codec, name, index, path); 1913 if (err < 0) 1914 return err; 1915 } 1916 1917 name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL); 1918 if (!name || !strcmp(name, "CLFE")) { 1919 err = add_sw_ctl(codec, "Center", 0, 1, path); 1920 if (err < 0) 1921 return err; 1922 err = add_sw_ctl(codec, "LFE", 0, 2, path); 1923 if (err < 0) 1924 return err; 1925 } else { 1926 err = add_stereo_sw(codec, name, index, path); 1927 if (err < 0) 1928 return err; 1929 } 1930 } 1931 return 0; 1932 } 1933 1934 static int create_extra_out(struct hda_codec *codec, int path_idx, 1935 const char *pfx, int cidx) 1936 { 1937 struct nid_path *path; 1938 int err; 1939 1940 path = snd_hda_get_path_from_idx(codec, path_idx); 1941 if (!path) 1942 return 0; 1943 err = add_stereo_vol(codec, pfx, cidx, path); 1944 if (err < 0) 1945 return err; 1946 err = add_stereo_sw(codec, pfx, cidx, path); 1947 if (err < 0) 1948 return err; 1949 return 0; 1950 } 1951 1952 /* add playback controls for speaker and HP outputs */ 1953 static int create_extra_outs(struct hda_codec *codec, int num_pins, 1954 const int *paths, const char *pfx) 1955 { 1956 int i; 1957 1958 for (i = 0; i < num_pins; i++) { 1959 const char *name; 1960 char tmp[SNDRV_CTL_ELEM_ID_NAME_MAXLEN]; 1961 int err, idx = 0; 1962 1963 if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker")) 1964 name = "Bass Speaker"; 1965 else if (num_pins >= 3) { 1966 snprintf(tmp, sizeof(tmp), "%s %s", 1967 pfx, channel_name[i]); 1968 name = tmp; 1969 } else { 1970 name = pfx; 1971 idx = i; 1972 } 1973 err = create_extra_out(codec, paths[i], name, idx); 1974 if (err < 0) 1975 return err; 1976 } 1977 return 0; 1978 } 1979 1980 static int create_hp_out_ctls(struct hda_codec *codec) 1981 { 1982 struct hda_gen_spec *spec = codec->spec; 1983 return create_extra_outs(codec, spec->autocfg.hp_outs, 1984 spec->hp_paths, 1985 "Headphone"); 1986 } 1987 1988 static int create_speaker_out_ctls(struct hda_codec *codec) 1989 { 1990 struct hda_gen_spec *spec = codec->spec; 1991 return create_extra_outs(codec, spec->autocfg.speaker_outs, 1992 spec->speaker_paths, 1993 "Speaker"); 1994 } 1995 1996 /* 1997 * independent HP controls 1998 */ 1999 2000 static void call_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack); 2001 static int indep_hp_info(struct snd_kcontrol *kcontrol, 2002 struct snd_ctl_elem_info *uinfo) 2003 { 2004 return snd_hda_enum_bool_helper_info(kcontrol, uinfo); 2005 } 2006 2007 static int indep_hp_get(struct snd_kcontrol *kcontrol, 2008 struct snd_ctl_elem_value *ucontrol) 2009 { 2010 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 2011 struct hda_gen_spec *spec = codec->spec; 2012 ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled; 2013 return 0; 2014 } 2015 2016 static void update_aamix_paths(struct hda_codec *codec, bool do_mix, 2017 int nomix_path_idx, int mix_path_idx, 2018 int out_type); 2019 2020 static int indep_hp_put(struct snd_kcontrol *kcontrol, 2021 struct snd_ctl_elem_value *ucontrol) 2022 { 2023 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 2024 struct hda_gen_spec *spec = codec->spec; 2025 unsigned int select = ucontrol->value.enumerated.item[0]; 2026 int ret = 0; 2027 2028 mutex_lock(&spec->pcm_mutex); 2029 if (spec->active_streams) { 2030 ret = -EBUSY; 2031 goto unlock; 2032 } 2033 2034 if (spec->indep_hp_enabled != select) { 2035 hda_nid_t *dacp; 2036 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT) 2037 dacp = &spec->private_dac_nids[0]; 2038 else 2039 dacp = &spec->multiout.hp_out_nid[0]; 2040 2041 /* update HP aamix paths in case it conflicts with indep HP */ 2042 if (spec->have_aamix_ctl) { 2043 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT) 2044 update_aamix_paths(codec, spec->aamix_mode, 2045 spec->out_paths[0], 2046 spec->aamix_out_paths[0], 2047 spec->autocfg.line_out_type); 2048 else 2049 update_aamix_paths(codec, spec->aamix_mode, 2050 spec->hp_paths[0], 2051 spec->aamix_out_paths[1], 2052 AUTO_PIN_HP_OUT); 2053 } 2054 2055 spec->indep_hp_enabled = select; 2056 if (spec->indep_hp_enabled) 2057 *dacp = 0; 2058 else 2059 *dacp = spec->alt_dac_nid; 2060 2061 call_hp_automute(codec, NULL); 2062 ret = 1; 2063 } 2064 unlock: 2065 mutex_unlock(&spec->pcm_mutex); 2066 return ret; 2067 } 2068 2069 static const struct snd_kcontrol_new indep_hp_ctl = { 2070 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 2071 .name = "Independent HP", 2072 .info = indep_hp_info, 2073 .get = indep_hp_get, 2074 .put = indep_hp_put, 2075 }; 2076 2077 2078 static int create_indep_hp_ctls(struct hda_codec *codec) 2079 { 2080 struct hda_gen_spec *spec = codec->spec; 2081 hda_nid_t dac; 2082 2083 if (!spec->indep_hp) 2084 return 0; 2085 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT) 2086 dac = spec->multiout.dac_nids[0]; 2087 else 2088 dac = spec->multiout.hp_out_nid[0]; 2089 if (!dac) { 2090 spec->indep_hp = 0; 2091 return 0; 2092 } 2093 2094 spec->indep_hp_enabled = false; 2095 spec->alt_dac_nid = dac; 2096 if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl)) 2097 return -ENOMEM; 2098 return 0; 2099 } 2100 2101 /* 2102 * channel mode enum control 2103 */ 2104 2105 static int ch_mode_info(struct snd_kcontrol *kcontrol, 2106 struct snd_ctl_elem_info *uinfo) 2107 { 2108 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 2109 struct hda_gen_spec *spec = codec->spec; 2110 int chs; 2111 2112 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; 2113 uinfo->count = 1; 2114 uinfo->value.enumerated.items = spec->multi_ios + 1; 2115 if (uinfo->value.enumerated.item > spec->multi_ios) 2116 uinfo->value.enumerated.item = spec->multi_ios; 2117 chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count; 2118 sprintf(uinfo->value.enumerated.name, "%dch", chs); 2119 return 0; 2120 } 2121 2122 static int ch_mode_get(struct snd_kcontrol *kcontrol, 2123 struct snd_ctl_elem_value *ucontrol) 2124 { 2125 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 2126 struct hda_gen_spec *spec = codec->spec; 2127 ucontrol->value.enumerated.item[0] = 2128 (spec->ext_channel_count - spec->min_channel_count) / 2; 2129 return 0; 2130 } 2131 2132 static inline struct nid_path * 2133 get_multiio_path(struct hda_codec *codec, int idx) 2134 { 2135 struct hda_gen_spec *spec = codec->spec; 2136 return snd_hda_get_path_from_idx(codec, 2137 spec->out_paths[spec->autocfg.line_outs + idx]); 2138 } 2139 2140 static void update_automute_all(struct hda_codec *codec); 2141 2142 /* Default value to be passed as aamix argument for snd_hda_activate_path(); 2143 * used for output paths 2144 */ 2145 static bool aamix_default(struct hda_gen_spec *spec) 2146 { 2147 return !spec->have_aamix_ctl || spec->aamix_mode; 2148 } 2149 2150 static int set_multi_io(struct hda_codec *codec, int idx, bool output) 2151 { 2152 struct hda_gen_spec *spec = codec->spec; 2153 hda_nid_t nid = spec->multi_io[idx].pin; 2154 struct nid_path *path; 2155 2156 path = get_multiio_path(codec, idx); 2157 if (!path) 2158 return -EINVAL; 2159 2160 if (path->active == output) 2161 return 0; 2162 2163 if (output) { 2164 set_pin_target(codec, nid, PIN_OUT, true); 2165 snd_hda_activate_path(codec, path, true, aamix_default(spec)); 2166 set_pin_eapd(codec, nid, true); 2167 } else { 2168 set_pin_eapd(codec, nid, false); 2169 snd_hda_activate_path(codec, path, false, aamix_default(spec)); 2170 set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true); 2171 path_power_down_sync(codec, path); 2172 } 2173 2174 /* update jack retasking in case it modifies any of them */ 2175 update_automute_all(codec); 2176 2177 return 0; 2178 } 2179 2180 static int ch_mode_put(struct snd_kcontrol *kcontrol, 2181 struct snd_ctl_elem_value *ucontrol) 2182 { 2183 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 2184 struct hda_gen_spec *spec = codec->spec; 2185 int i, ch; 2186 2187 ch = ucontrol->value.enumerated.item[0]; 2188 if (ch < 0 || ch > spec->multi_ios) 2189 return -EINVAL; 2190 if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2) 2191 return 0; 2192 spec->ext_channel_count = ch * 2 + spec->min_channel_count; 2193 for (i = 0; i < spec->multi_ios; i++) 2194 set_multi_io(codec, i, i < ch); 2195 spec->multiout.max_channels = max(spec->ext_channel_count, 2196 spec->const_channel_count); 2197 if (spec->need_dac_fix) 2198 spec->multiout.num_dacs = spec->multiout.max_channels / 2; 2199 return 1; 2200 } 2201 2202 static const struct snd_kcontrol_new channel_mode_enum = { 2203 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 2204 .name = "Channel Mode", 2205 .info = ch_mode_info, 2206 .get = ch_mode_get, 2207 .put = ch_mode_put, 2208 }; 2209 2210 static int create_multi_channel_mode(struct hda_codec *codec) 2211 { 2212 struct hda_gen_spec *spec = codec->spec; 2213 2214 if (spec->multi_ios > 0) { 2215 if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum)) 2216 return -ENOMEM; 2217 } 2218 return 0; 2219 } 2220 2221 /* 2222 * aamix loopback enable/disable switch 2223 */ 2224 2225 #define loopback_mixing_info indep_hp_info 2226 2227 static int loopback_mixing_get(struct snd_kcontrol *kcontrol, 2228 struct snd_ctl_elem_value *ucontrol) 2229 { 2230 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 2231 struct hda_gen_spec *spec = codec->spec; 2232 ucontrol->value.enumerated.item[0] = spec->aamix_mode; 2233 return 0; 2234 } 2235 2236 static void update_aamix_paths(struct hda_codec *codec, bool do_mix, 2237 int nomix_path_idx, int mix_path_idx, 2238 int out_type) 2239 { 2240 struct hda_gen_spec *spec = codec->spec; 2241 struct nid_path *nomix_path, *mix_path; 2242 2243 nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx); 2244 mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx); 2245 if (!nomix_path || !mix_path) 2246 return; 2247 2248 /* if HP aamix path is driven from a different DAC and the 2249 * independent HP mode is ON, can't turn on aamix path 2250 */ 2251 if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled && 2252 mix_path->path[0] != spec->alt_dac_nid) 2253 do_mix = false; 2254 2255 if (do_mix) { 2256 snd_hda_activate_path(codec, nomix_path, false, true); 2257 snd_hda_activate_path(codec, mix_path, true, true); 2258 path_power_down_sync(codec, nomix_path); 2259 } else { 2260 snd_hda_activate_path(codec, mix_path, false, false); 2261 snd_hda_activate_path(codec, nomix_path, true, false); 2262 path_power_down_sync(codec, mix_path); 2263 } 2264 } 2265 2266 static int loopback_mixing_put(struct snd_kcontrol *kcontrol, 2267 struct snd_ctl_elem_value *ucontrol) 2268 { 2269 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 2270 struct hda_gen_spec *spec = codec->spec; 2271 unsigned int val = ucontrol->value.enumerated.item[0]; 2272 2273 if (val == spec->aamix_mode) 2274 return 0; 2275 spec->aamix_mode = val; 2276 update_aamix_paths(codec, val, spec->out_paths[0], 2277 spec->aamix_out_paths[0], 2278 spec->autocfg.line_out_type); 2279 update_aamix_paths(codec, val, spec->hp_paths[0], 2280 spec->aamix_out_paths[1], 2281 AUTO_PIN_HP_OUT); 2282 update_aamix_paths(codec, val, spec->speaker_paths[0], 2283 spec->aamix_out_paths[2], 2284 AUTO_PIN_SPEAKER_OUT); 2285 return 1; 2286 } 2287 2288 static const struct snd_kcontrol_new loopback_mixing_enum = { 2289 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 2290 .name = "Loopback Mixing", 2291 .info = loopback_mixing_info, 2292 .get = loopback_mixing_get, 2293 .put = loopback_mixing_put, 2294 }; 2295 2296 static int create_loopback_mixing_ctl(struct hda_codec *codec) 2297 { 2298 struct hda_gen_spec *spec = codec->spec; 2299 2300 if (!spec->mixer_nid) 2301 return 0; 2302 if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] || 2303 spec->aamix_out_paths[2])) 2304 return 0; 2305 if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum)) 2306 return -ENOMEM; 2307 spec->have_aamix_ctl = 1; 2308 return 0; 2309 } 2310 2311 /* 2312 * shared headphone/mic handling 2313 */ 2314 2315 static void call_update_outputs(struct hda_codec *codec); 2316 2317 /* for shared I/O, change the pin-control accordingly */ 2318 static void update_hp_mic(struct hda_codec *codec, int adc_mux, bool force) 2319 { 2320 struct hda_gen_spec *spec = codec->spec; 2321 bool as_mic; 2322 unsigned int val; 2323 hda_nid_t pin; 2324 2325 pin = spec->hp_mic_pin; 2326 as_mic = spec->cur_mux[adc_mux] == spec->hp_mic_mux_idx; 2327 2328 if (!force) { 2329 val = snd_hda_codec_get_pin_target(codec, pin); 2330 if (as_mic) { 2331 if (val & PIN_IN) 2332 return; 2333 } else { 2334 if (val & PIN_OUT) 2335 return; 2336 } 2337 } 2338 2339 val = snd_hda_get_default_vref(codec, pin); 2340 /* if the HP pin doesn't support VREF and the codec driver gives an 2341 * alternative pin, set up the VREF on that pin instead 2342 */ 2343 if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) { 2344 const hda_nid_t vref_pin = spec->shared_mic_vref_pin; 2345 unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin); 2346 if (vref_val != AC_PINCTL_VREF_HIZ) 2347 snd_hda_set_pin_ctl_cache(codec, vref_pin, 2348 PIN_IN | (as_mic ? vref_val : 0)); 2349 } 2350 2351 if (!spec->hp_mic_jack_modes) { 2352 if (as_mic) 2353 val |= PIN_IN; 2354 else 2355 val = PIN_HP; 2356 set_pin_target(codec, pin, val, true); 2357 call_hp_automute(codec, NULL); 2358 } 2359 } 2360 2361 /* create a shared input with the headphone out */ 2362 static int create_hp_mic(struct hda_codec *codec) 2363 { 2364 struct hda_gen_spec *spec = codec->spec; 2365 struct auto_pin_cfg *cfg = &spec->autocfg; 2366 unsigned int defcfg; 2367 hda_nid_t nid; 2368 2369 if (!spec->hp_mic) { 2370 if (spec->suppress_hp_mic_detect) 2371 return 0; 2372 /* automatic detection: only if no input or a single internal 2373 * input pin is found, try to detect the shared hp/mic 2374 */ 2375 if (cfg->num_inputs > 1) 2376 return 0; 2377 else if (cfg->num_inputs == 1) { 2378 defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin); 2379 if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT) 2380 return 0; 2381 } 2382 } 2383 2384 spec->hp_mic = 0; /* clear once */ 2385 if (cfg->num_inputs >= AUTO_CFG_MAX_INS) 2386 return 0; 2387 2388 nid = 0; 2389 if (cfg->line_out_type == AUTO_PIN_HP_OUT && cfg->line_outs > 0) 2390 nid = cfg->line_out_pins[0]; 2391 else if (cfg->hp_outs > 0) 2392 nid = cfg->hp_pins[0]; 2393 if (!nid) 2394 return 0; 2395 2396 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN)) 2397 return 0; /* no input */ 2398 2399 cfg->inputs[cfg->num_inputs].pin = nid; 2400 cfg->inputs[cfg->num_inputs].type = AUTO_PIN_MIC; 2401 cfg->inputs[cfg->num_inputs].is_headphone_mic = 1; 2402 cfg->num_inputs++; 2403 spec->hp_mic = 1; 2404 spec->hp_mic_pin = nid; 2405 /* we can't handle auto-mic together with HP-mic */ 2406 spec->suppress_auto_mic = 1; 2407 snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid); 2408 return 0; 2409 } 2410 2411 /* 2412 * output jack mode 2413 */ 2414 2415 static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin); 2416 2417 static const char * const out_jack_texts[] = { 2418 "Line Out", "Headphone Out", 2419 }; 2420 2421 static int out_jack_mode_info(struct snd_kcontrol *kcontrol, 2422 struct snd_ctl_elem_info *uinfo) 2423 { 2424 return snd_hda_enum_helper_info(kcontrol, uinfo, 2, out_jack_texts); 2425 } 2426 2427 static int out_jack_mode_get(struct snd_kcontrol *kcontrol, 2428 struct snd_ctl_elem_value *ucontrol) 2429 { 2430 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 2431 hda_nid_t nid = kcontrol->private_value; 2432 if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP) 2433 ucontrol->value.enumerated.item[0] = 1; 2434 else 2435 ucontrol->value.enumerated.item[0] = 0; 2436 return 0; 2437 } 2438 2439 static int out_jack_mode_put(struct snd_kcontrol *kcontrol, 2440 struct snd_ctl_elem_value *ucontrol) 2441 { 2442 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 2443 hda_nid_t nid = kcontrol->private_value; 2444 unsigned int val; 2445 2446 val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT; 2447 if (snd_hda_codec_get_pin_target(codec, nid) == val) 2448 return 0; 2449 snd_hda_set_pin_ctl_cache(codec, nid, val); 2450 return 1; 2451 } 2452 2453 static const struct snd_kcontrol_new out_jack_mode_enum = { 2454 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 2455 .info = out_jack_mode_info, 2456 .get = out_jack_mode_get, 2457 .put = out_jack_mode_put, 2458 }; 2459 2460 static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx) 2461 { 2462 struct hda_gen_spec *spec = codec->spec; 2463 int i; 2464 2465 for (i = 0; i < spec->kctls.used; i++) { 2466 struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i); 2467 if (!strcmp(kctl->name, name) && kctl->index == idx) 2468 return true; 2469 } 2470 return false; 2471 } 2472 2473 static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin, 2474 char *name, size_t name_len) 2475 { 2476 struct hda_gen_spec *spec = codec->spec; 2477 int idx = 0; 2478 2479 snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx); 2480 strlcat(name, " Jack Mode", name_len); 2481 2482 for (; find_kctl_name(codec, name, idx); idx++) 2483 ; 2484 } 2485 2486 static int get_out_jack_num_items(struct hda_codec *codec, hda_nid_t pin) 2487 { 2488 struct hda_gen_spec *spec = codec->spec; 2489 if (spec->add_jack_modes) { 2490 unsigned int pincap = snd_hda_query_pin_caps(codec, pin); 2491 if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV)) 2492 return 2; 2493 } 2494 return 1; 2495 } 2496 2497 static int create_out_jack_modes(struct hda_codec *codec, int num_pins, 2498 hda_nid_t *pins) 2499 { 2500 struct hda_gen_spec *spec = codec->spec; 2501 int i; 2502 2503 for (i = 0; i < num_pins; i++) { 2504 hda_nid_t pin = pins[i]; 2505 if (pin == spec->hp_mic_pin) { 2506 int ret = create_hp_mic_jack_mode(codec, pin); 2507 if (ret < 0) 2508 return ret; 2509 continue; 2510 } 2511 if (get_out_jack_num_items(codec, pin) > 1) { 2512 struct snd_kcontrol_new *knew; 2513 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN]; 2514 get_jack_mode_name(codec, pin, name, sizeof(name)); 2515 knew = snd_hda_gen_add_kctl(spec, name, 2516 &out_jack_mode_enum); 2517 if (!knew) 2518 return -ENOMEM; 2519 knew->private_value = pin; 2520 } 2521 } 2522 2523 return 0; 2524 } 2525 2526 /* 2527 * input jack mode 2528 */ 2529 2530 /* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */ 2531 #define NUM_VREFS 6 2532 2533 static const char * const vref_texts[NUM_VREFS] = { 2534 "Line In", "Mic 50pc Bias", "Mic 0V Bias", 2535 "", "Mic 80pc Bias", "Mic 100pc Bias" 2536 }; 2537 2538 static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin) 2539 { 2540 unsigned int pincap; 2541 2542 pincap = snd_hda_query_pin_caps(codec, pin); 2543 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT; 2544 /* filter out unusual vrefs */ 2545 pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100); 2546 return pincap; 2547 } 2548 2549 /* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */ 2550 static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx) 2551 { 2552 unsigned int i, n = 0; 2553 2554 for (i = 0; i < NUM_VREFS; i++) { 2555 if (vref_caps & (1 << i)) { 2556 if (n == item_idx) 2557 return i; 2558 n++; 2559 } 2560 } 2561 return 0; 2562 } 2563 2564 /* convert back from the vref ctl index to the enum item index */ 2565 static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx) 2566 { 2567 unsigned int i, n = 0; 2568 2569 for (i = 0; i < NUM_VREFS; i++) { 2570 if (i == idx) 2571 return n; 2572 if (vref_caps & (1 << i)) 2573 n++; 2574 } 2575 return 0; 2576 } 2577 2578 static int in_jack_mode_info(struct snd_kcontrol *kcontrol, 2579 struct snd_ctl_elem_info *uinfo) 2580 { 2581 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 2582 hda_nid_t nid = kcontrol->private_value; 2583 unsigned int vref_caps = get_vref_caps(codec, nid); 2584 2585 snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps), 2586 vref_texts); 2587 /* set the right text */ 2588 strcpy(uinfo->value.enumerated.name, 2589 vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]); 2590 return 0; 2591 } 2592 2593 static int in_jack_mode_get(struct snd_kcontrol *kcontrol, 2594 struct snd_ctl_elem_value *ucontrol) 2595 { 2596 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 2597 hda_nid_t nid = kcontrol->private_value; 2598 unsigned int vref_caps = get_vref_caps(codec, nid); 2599 unsigned int idx; 2600 2601 idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN; 2602 ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx); 2603 return 0; 2604 } 2605 2606 static int in_jack_mode_put(struct snd_kcontrol *kcontrol, 2607 struct snd_ctl_elem_value *ucontrol) 2608 { 2609 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 2610 hda_nid_t nid = kcontrol->private_value; 2611 unsigned int vref_caps = get_vref_caps(codec, nid); 2612 unsigned int val, idx; 2613 2614 val = snd_hda_codec_get_pin_target(codec, nid); 2615 idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN); 2616 if (idx == ucontrol->value.enumerated.item[0]) 2617 return 0; 2618 2619 val &= ~AC_PINCTL_VREFEN; 2620 val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]); 2621 snd_hda_set_pin_ctl_cache(codec, nid, val); 2622 return 1; 2623 } 2624 2625 static const struct snd_kcontrol_new in_jack_mode_enum = { 2626 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 2627 .info = in_jack_mode_info, 2628 .get = in_jack_mode_get, 2629 .put = in_jack_mode_put, 2630 }; 2631 2632 static int get_in_jack_num_items(struct hda_codec *codec, hda_nid_t pin) 2633 { 2634 struct hda_gen_spec *spec = codec->spec; 2635 int nitems = 0; 2636 if (spec->add_jack_modes) 2637 nitems = hweight32(get_vref_caps(codec, pin)); 2638 return nitems ? nitems : 1; 2639 } 2640 2641 static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin) 2642 { 2643 struct hda_gen_spec *spec = codec->spec; 2644 struct snd_kcontrol_new *knew; 2645 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN]; 2646 unsigned int defcfg; 2647 2648 if (pin == spec->hp_mic_pin) 2649 return 0; /* already done in create_out_jack_mode() */ 2650 2651 /* no jack mode for fixed pins */ 2652 defcfg = snd_hda_codec_get_pincfg(codec, pin); 2653 if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT) 2654 return 0; 2655 2656 /* no multiple vref caps? */ 2657 if (get_in_jack_num_items(codec, pin) <= 1) 2658 return 0; 2659 2660 get_jack_mode_name(codec, pin, name, sizeof(name)); 2661 knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum); 2662 if (!knew) 2663 return -ENOMEM; 2664 knew->private_value = pin; 2665 return 0; 2666 } 2667 2668 /* 2669 * HP/mic shared jack mode 2670 */ 2671 static int hp_mic_jack_mode_info(struct snd_kcontrol *kcontrol, 2672 struct snd_ctl_elem_info *uinfo) 2673 { 2674 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 2675 hda_nid_t nid = kcontrol->private_value; 2676 int out_jacks = get_out_jack_num_items(codec, nid); 2677 int in_jacks = get_in_jack_num_items(codec, nid); 2678 const char *text = NULL; 2679 int idx; 2680 2681 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; 2682 uinfo->count = 1; 2683 uinfo->value.enumerated.items = out_jacks + in_jacks; 2684 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items) 2685 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1; 2686 idx = uinfo->value.enumerated.item; 2687 if (idx < out_jacks) { 2688 if (out_jacks > 1) 2689 text = out_jack_texts[idx]; 2690 else 2691 text = "Headphone Out"; 2692 } else { 2693 idx -= out_jacks; 2694 if (in_jacks > 1) { 2695 unsigned int vref_caps = get_vref_caps(codec, nid); 2696 text = vref_texts[get_vref_idx(vref_caps, idx)]; 2697 } else 2698 text = "Mic In"; 2699 } 2700 2701 strcpy(uinfo->value.enumerated.name, text); 2702 return 0; 2703 } 2704 2705 static int get_cur_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t nid) 2706 { 2707 int out_jacks = get_out_jack_num_items(codec, nid); 2708 int in_jacks = get_in_jack_num_items(codec, nid); 2709 unsigned int val = snd_hda_codec_get_pin_target(codec, nid); 2710 int idx = 0; 2711 2712 if (val & PIN_OUT) { 2713 if (out_jacks > 1 && val == PIN_HP) 2714 idx = 1; 2715 } else if (val & PIN_IN) { 2716 idx = out_jacks; 2717 if (in_jacks > 1) { 2718 unsigned int vref_caps = get_vref_caps(codec, nid); 2719 val &= AC_PINCTL_VREFEN; 2720 idx += cvt_from_vref_idx(vref_caps, val); 2721 } 2722 } 2723 return idx; 2724 } 2725 2726 static int hp_mic_jack_mode_get(struct snd_kcontrol *kcontrol, 2727 struct snd_ctl_elem_value *ucontrol) 2728 { 2729 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 2730 hda_nid_t nid = kcontrol->private_value; 2731 ucontrol->value.enumerated.item[0] = 2732 get_cur_hp_mic_jack_mode(codec, nid); 2733 return 0; 2734 } 2735 2736 static int hp_mic_jack_mode_put(struct snd_kcontrol *kcontrol, 2737 struct snd_ctl_elem_value *ucontrol) 2738 { 2739 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 2740 hda_nid_t nid = kcontrol->private_value; 2741 int out_jacks = get_out_jack_num_items(codec, nid); 2742 int in_jacks = get_in_jack_num_items(codec, nid); 2743 unsigned int val, oldval, idx; 2744 2745 oldval = get_cur_hp_mic_jack_mode(codec, nid); 2746 idx = ucontrol->value.enumerated.item[0]; 2747 if (oldval == idx) 2748 return 0; 2749 2750 if (idx < out_jacks) { 2751 if (out_jacks > 1) 2752 val = idx ? PIN_HP : PIN_OUT; 2753 else 2754 val = PIN_HP; 2755 } else { 2756 idx -= out_jacks; 2757 if (in_jacks > 1) { 2758 unsigned int vref_caps = get_vref_caps(codec, nid); 2759 val = snd_hda_codec_get_pin_target(codec, nid); 2760 val &= ~(AC_PINCTL_VREFEN | PIN_HP); 2761 val |= get_vref_idx(vref_caps, idx) | PIN_IN; 2762 } else 2763 val = snd_hda_get_default_vref(codec, nid); 2764 } 2765 snd_hda_set_pin_ctl_cache(codec, nid, val); 2766 call_hp_automute(codec, NULL); 2767 2768 return 1; 2769 } 2770 2771 static const struct snd_kcontrol_new hp_mic_jack_mode_enum = { 2772 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 2773 .info = hp_mic_jack_mode_info, 2774 .get = hp_mic_jack_mode_get, 2775 .put = hp_mic_jack_mode_put, 2776 }; 2777 2778 static int create_hp_mic_jack_mode(struct hda_codec *codec, hda_nid_t pin) 2779 { 2780 struct hda_gen_spec *spec = codec->spec; 2781 struct snd_kcontrol_new *knew; 2782 2783 if (get_out_jack_num_items(codec, pin) <= 1 && 2784 get_in_jack_num_items(codec, pin) <= 1) 2785 return 0; /* no need */ 2786 knew = snd_hda_gen_add_kctl(spec, "Headphone Mic Jack Mode", 2787 &hp_mic_jack_mode_enum); 2788 if (!knew) 2789 return -ENOMEM; 2790 knew->private_value = pin; 2791 spec->hp_mic_jack_modes = 1; 2792 return 0; 2793 } 2794 2795 /* 2796 * Parse input paths 2797 */ 2798 2799 /* add the powersave loopback-list entry */ 2800 static int add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx) 2801 { 2802 struct hda_amp_list *list; 2803 2804 list = snd_array_new(&spec->loopback_list); 2805 if (!list) 2806 return -ENOMEM; 2807 list->nid = mix; 2808 list->dir = HDA_INPUT; 2809 list->idx = idx; 2810 spec->loopback.amplist = spec->loopback_list.list; 2811 return 0; 2812 } 2813 2814 /* create input playback/capture controls for the given pin */ 2815 static int new_analog_input(struct hda_codec *codec, int input_idx, 2816 hda_nid_t pin, const char *ctlname, int ctlidx, 2817 hda_nid_t mix_nid) 2818 { 2819 struct hda_gen_spec *spec = codec->spec; 2820 struct nid_path *path; 2821 unsigned int val; 2822 int err, idx; 2823 2824 if (!nid_has_volume(codec, mix_nid, HDA_INPUT) && 2825 !nid_has_mute(codec, mix_nid, HDA_INPUT)) 2826 return 0; /* no need for analog loopback */ 2827 2828 path = snd_hda_add_new_path(codec, pin, mix_nid, 0); 2829 if (!path) 2830 return -EINVAL; 2831 print_nid_path("loopback", path); 2832 spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path); 2833 2834 idx = path->idx[path->depth - 1]; 2835 if (nid_has_volume(codec, mix_nid, HDA_INPUT)) { 2836 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT); 2837 err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val); 2838 if (err < 0) 2839 return err; 2840 path->ctls[NID_PATH_VOL_CTL] = val; 2841 } 2842 2843 if (nid_has_mute(codec, mix_nid, HDA_INPUT)) { 2844 val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT); 2845 err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val); 2846 if (err < 0) 2847 return err; 2848 path->ctls[NID_PATH_MUTE_CTL] = val; 2849 } 2850 2851 path->active = true; 2852 err = add_loopback_list(spec, mix_nid, idx); 2853 if (err < 0) 2854 return err; 2855 2856 if (spec->mixer_nid != spec->mixer_merge_nid && 2857 !spec->loopback_merge_path) { 2858 path = snd_hda_add_new_path(codec, spec->mixer_nid, 2859 spec->mixer_merge_nid, 0); 2860 if (path) { 2861 print_nid_path("loopback-merge", path); 2862 path->active = true; 2863 spec->loopback_merge_path = 2864 snd_hda_get_path_idx(codec, path); 2865 } 2866 } 2867 2868 return 0; 2869 } 2870 2871 static int is_input_pin(struct hda_codec *codec, hda_nid_t nid) 2872 { 2873 unsigned int pincap = snd_hda_query_pin_caps(codec, nid); 2874 return (pincap & AC_PINCAP_IN) != 0; 2875 } 2876 2877 /* Parse the codec tree and retrieve ADCs */ 2878 static int fill_adc_nids(struct hda_codec *codec) 2879 { 2880 struct hda_gen_spec *spec = codec->spec; 2881 hda_nid_t nid; 2882 hda_nid_t *adc_nids = spec->adc_nids; 2883 int max_nums = ARRAY_SIZE(spec->adc_nids); 2884 int i, nums = 0; 2885 2886 nid = codec->start_nid; 2887 for (i = 0; i < codec->num_nodes; i++, nid++) { 2888 unsigned int caps = get_wcaps(codec, nid); 2889 int type = get_wcaps_type(caps); 2890 2891 if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL)) 2892 continue; 2893 adc_nids[nums] = nid; 2894 if (++nums >= max_nums) 2895 break; 2896 } 2897 spec->num_adc_nids = nums; 2898 2899 /* copy the detected ADCs to all_adcs[] */ 2900 spec->num_all_adcs = nums; 2901 memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t)); 2902 2903 return nums; 2904 } 2905 2906 /* filter out invalid adc_nids that don't give all active input pins; 2907 * if needed, check whether dynamic ADC-switching is available 2908 */ 2909 static int check_dyn_adc_switch(struct hda_codec *codec) 2910 { 2911 struct hda_gen_spec *spec = codec->spec; 2912 struct hda_input_mux *imux = &spec->input_mux; 2913 unsigned int ok_bits; 2914 int i, n, nums; 2915 2916 nums = 0; 2917 ok_bits = 0; 2918 for (n = 0; n < spec->num_adc_nids; n++) { 2919 for (i = 0; i < imux->num_items; i++) { 2920 if (!spec->input_paths[i][n]) 2921 break; 2922 } 2923 if (i >= imux->num_items) { 2924 ok_bits |= (1 << n); 2925 nums++; 2926 } 2927 } 2928 2929 if (!ok_bits) { 2930 /* check whether ADC-switch is possible */ 2931 for (i = 0; i < imux->num_items; i++) { 2932 for (n = 0; n < spec->num_adc_nids; n++) { 2933 if (spec->input_paths[i][n]) { 2934 spec->dyn_adc_idx[i] = n; 2935 break; 2936 } 2937 } 2938 } 2939 2940 snd_printdd("hda-codec: enabling ADC switching\n"); 2941 spec->dyn_adc_switch = 1; 2942 } else if (nums != spec->num_adc_nids) { 2943 /* shrink the invalid adcs and input paths */ 2944 nums = 0; 2945 for (n = 0; n < spec->num_adc_nids; n++) { 2946 if (!(ok_bits & (1 << n))) 2947 continue; 2948 if (n != nums) { 2949 spec->adc_nids[nums] = spec->adc_nids[n]; 2950 for (i = 0; i < imux->num_items; i++) { 2951 invalidate_nid_path(codec, 2952 spec->input_paths[i][nums]); 2953 spec->input_paths[i][nums] = 2954 spec->input_paths[i][n]; 2955 } 2956 } 2957 nums++; 2958 } 2959 spec->num_adc_nids = nums; 2960 } 2961 2962 if (imux->num_items == 1 || 2963 (imux->num_items == 2 && spec->hp_mic)) { 2964 snd_printdd("hda-codec: reducing to a single ADC\n"); 2965 spec->num_adc_nids = 1; /* reduce to a single ADC */ 2966 } 2967 2968 /* single index for individual volumes ctls */ 2969 if (!spec->dyn_adc_switch && spec->multi_cap_vol) 2970 spec->num_adc_nids = 1; 2971 2972 return 0; 2973 } 2974 2975 /* parse capture source paths from the given pin and create imux items */ 2976 static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin, 2977 int cfg_idx, int num_adcs, 2978 const char *label, int anchor) 2979 { 2980 struct hda_gen_spec *spec = codec->spec; 2981 struct hda_input_mux *imux = &spec->input_mux; 2982 int imux_idx = imux->num_items; 2983 bool imux_added = false; 2984 int c; 2985 2986 for (c = 0; c < num_adcs; c++) { 2987 struct nid_path *path; 2988 hda_nid_t adc = spec->adc_nids[c]; 2989 2990 if (!is_reachable_path(codec, pin, adc)) 2991 continue; 2992 path = snd_hda_add_new_path(codec, pin, adc, anchor); 2993 if (!path) 2994 continue; 2995 print_nid_path("input", path); 2996 spec->input_paths[imux_idx][c] = 2997 snd_hda_get_path_idx(codec, path); 2998 2999 if (!imux_added) { 3000 if (spec->hp_mic_pin == pin) 3001 spec->hp_mic_mux_idx = imux->num_items; 3002 spec->imux_pins[imux->num_items] = pin; 3003 snd_hda_add_imux_item(imux, label, cfg_idx, NULL); 3004 imux_added = true; 3005 } 3006 } 3007 3008 return 0; 3009 } 3010 3011 /* 3012 * create playback/capture controls for input pins 3013 */ 3014 3015 /* fill the label for each input at first */ 3016 static int fill_input_pin_labels(struct hda_codec *codec) 3017 { 3018 struct hda_gen_spec *spec = codec->spec; 3019 const struct auto_pin_cfg *cfg = &spec->autocfg; 3020 int i; 3021 3022 for (i = 0; i < cfg->num_inputs; i++) { 3023 hda_nid_t pin = cfg->inputs[i].pin; 3024 const char *label; 3025 int j, idx; 3026 3027 if (!is_input_pin(codec, pin)) 3028 continue; 3029 3030 label = hda_get_autocfg_input_label(codec, cfg, i); 3031 idx = 0; 3032 for (j = i - 1; j >= 0; j--) { 3033 if (spec->input_labels[j] && 3034 !strcmp(spec->input_labels[j], label)) { 3035 idx = spec->input_label_idxs[j] + 1; 3036 break; 3037 } 3038 } 3039 3040 spec->input_labels[i] = label; 3041 spec->input_label_idxs[i] = idx; 3042 } 3043 3044 return 0; 3045 } 3046 3047 #define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */ 3048 3049 static int create_input_ctls(struct hda_codec *codec) 3050 { 3051 struct hda_gen_spec *spec = codec->spec; 3052 const struct auto_pin_cfg *cfg = &spec->autocfg; 3053 hda_nid_t mixer = spec->mixer_nid; 3054 int num_adcs; 3055 int i, err; 3056 unsigned int val; 3057 3058 num_adcs = fill_adc_nids(codec); 3059 if (num_adcs < 0) 3060 return 0; 3061 3062 err = fill_input_pin_labels(codec); 3063 if (err < 0) 3064 return err; 3065 3066 for (i = 0; i < cfg->num_inputs; i++) { 3067 hda_nid_t pin; 3068 3069 pin = cfg->inputs[i].pin; 3070 if (!is_input_pin(codec, pin)) 3071 continue; 3072 3073 val = PIN_IN; 3074 if (cfg->inputs[i].type == AUTO_PIN_MIC) 3075 val |= snd_hda_get_default_vref(codec, pin); 3076 if (pin != spec->hp_mic_pin) 3077 set_pin_target(codec, pin, val, false); 3078 3079 if (mixer) { 3080 if (is_reachable_path(codec, pin, mixer)) { 3081 err = new_analog_input(codec, i, pin, 3082 spec->input_labels[i], 3083 spec->input_label_idxs[i], 3084 mixer); 3085 if (err < 0) 3086 return err; 3087 } 3088 } 3089 3090 err = parse_capture_source(codec, pin, i, num_adcs, 3091 spec->input_labels[i], -mixer); 3092 if (err < 0) 3093 return err; 3094 3095 if (spec->add_jack_modes) { 3096 err = create_in_jack_mode(codec, pin); 3097 if (err < 0) 3098 return err; 3099 } 3100 } 3101 3102 if (mixer && spec->add_stereo_mix_input) { 3103 err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs, 3104 "Stereo Mix", 0); 3105 if (err < 0) 3106 return err; 3107 } 3108 3109 return 0; 3110 } 3111 3112 3113 /* 3114 * input source mux 3115 */ 3116 3117 /* get the input path specified by the given adc and imux indices */ 3118 static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx) 3119 { 3120 struct hda_gen_spec *spec = codec->spec; 3121 if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) { 3122 snd_BUG(); 3123 return NULL; 3124 } 3125 if (spec->dyn_adc_switch) 3126 adc_idx = spec->dyn_adc_idx[imux_idx]; 3127 if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) { 3128 snd_BUG(); 3129 return NULL; 3130 } 3131 return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]); 3132 } 3133 3134 static int mux_select(struct hda_codec *codec, unsigned int adc_idx, 3135 unsigned int idx); 3136 3137 static int mux_enum_info(struct snd_kcontrol *kcontrol, 3138 struct snd_ctl_elem_info *uinfo) 3139 { 3140 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 3141 struct hda_gen_spec *spec = codec->spec; 3142 return snd_hda_input_mux_info(&spec->input_mux, uinfo); 3143 } 3144 3145 static int mux_enum_get(struct snd_kcontrol *kcontrol, 3146 struct snd_ctl_elem_value *ucontrol) 3147 { 3148 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 3149 struct hda_gen_spec *spec = codec->spec; 3150 /* the ctls are created at once with multiple counts */ 3151 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id); 3152 3153 ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx]; 3154 return 0; 3155 } 3156 3157 static int mux_enum_put(struct snd_kcontrol *kcontrol, 3158 struct snd_ctl_elem_value *ucontrol) 3159 { 3160 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 3161 unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id); 3162 return mux_select(codec, adc_idx, 3163 ucontrol->value.enumerated.item[0]); 3164 } 3165 3166 static const struct snd_kcontrol_new cap_src_temp = { 3167 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 3168 .name = "Input Source", 3169 .info = mux_enum_info, 3170 .get = mux_enum_get, 3171 .put = mux_enum_put, 3172 }; 3173 3174 /* 3175 * capture volume and capture switch ctls 3176 */ 3177 3178 typedef int (*put_call_t)(struct snd_kcontrol *kcontrol, 3179 struct snd_ctl_elem_value *ucontrol); 3180 3181 /* call the given amp update function for all amps in the imux list at once */ 3182 static int cap_put_caller(struct snd_kcontrol *kcontrol, 3183 struct snd_ctl_elem_value *ucontrol, 3184 put_call_t func, int type) 3185 { 3186 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 3187 struct hda_gen_spec *spec = codec->spec; 3188 const struct hda_input_mux *imux; 3189 struct nid_path *path; 3190 int i, adc_idx, err = 0; 3191 3192 imux = &spec->input_mux; 3193 adc_idx = kcontrol->id.index; 3194 mutex_lock(&codec->control_mutex); 3195 /* we use the cache-only update at first since multiple input paths 3196 * may shared the same amp; by updating only caches, the redundant 3197 * writes to hardware can be reduced. 3198 */ 3199 codec->cached_write = 1; 3200 for (i = 0; i < imux->num_items; i++) { 3201 path = get_input_path(codec, adc_idx, i); 3202 if (!path || !path->ctls[type]) 3203 continue; 3204 kcontrol->private_value = path->ctls[type]; 3205 err = func(kcontrol, ucontrol); 3206 if (err < 0) 3207 goto error; 3208 } 3209 error: 3210 codec->cached_write = 0; 3211 mutex_unlock(&codec->control_mutex); 3212 snd_hda_codec_flush_cache(codec); /* flush the updates */ 3213 if (err >= 0 && spec->cap_sync_hook) 3214 spec->cap_sync_hook(codec, ucontrol); 3215 return err; 3216 } 3217 3218 /* capture volume ctl callbacks */ 3219 #define cap_vol_info snd_hda_mixer_amp_volume_info 3220 #define cap_vol_get snd_hda_mixer_amp_volume_get 3221 #define cap_vol_tlv snd_hda_mixer_amp_tlv 3222 3223 static int cap_vol_put(struct snd_kcontrol *kcontrol, 3224 struct snd_ctl_elem_value *ucontrol) 3225 { 3226 return cap_put_caller(kcontrol, ucontrol, 3227 snd_hda_mixer_amp_volume_put, 3228 NID_PATH_VOL_CTL); 3229 } 3230 3231 static const struct snd_kcontrol_new cap_vol_temp = { 3232 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 3233 .name = "Capture Volume", 3234 .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE | 3235 SNDRV_CTL_ELEM_ACCESS_TLV_READ | 3236 SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK), 3237 .info = cap_vol_info, 3238 .get = cap_vol_get, 3239 .put = cap_vol_put, 3240 .tlv = { .c = cap_vol_tlv }, 3241 }; 3242 3243 /* capture switch ctl callbacks */ 3244 #define cap_sw_info snd_ctl_boolean_stereo_info 3245 #define cap_sw_get snd_hda_mixer_amp_switch_get 3246 3247 static int cap_sw_put(struct snd_kcontrol *kcontrol, 3248 struct snd_ctl_elem_value *ucontrol) 3249 { 3250 return cap_put_caller(kcontrol, ucontrol, 3251 snd_hda_mixer_amp_switch_put, 3252 NID_PATH_MUTE_CTL); 3253 } 3254 3255 static const struct snd_kcontrol_new cap_sw_temp = { 3256 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 3257 .name = "Capture Switch", 3258 .info = cap_sw_info, 3259 .get = cap_sw_get, 3260 .put = cap_sw_put, 3261 }; 3262 3263 static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path) 3264 { 3265 hda_nid_t nid; 3266 int i, depth; 3267 3268 path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0; 3269 for (depth = 0; depth < 3; depth++) { 3270 if (depth >= path->depth) 3271 return -EINVAL; 3272 i = path->depth - depth - 1; 3273 nid = path->path[i]; 3274 if (!path->ctls[NID_PATH_VOL_CTL]) { 3275 if (nid_has_volume(codec, nid, HDA_OUTPUT)) 3276 path->ctls[NID_PATH_VOL_CTL] = 3277 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT); 3278 else if (nid_has_volume(codec, nid, HDA_INPUT)) { 3279 int idx = path->idx[i]; 3280 if (!depth && codec->single_adc_amp) 3281 idx = 0; 3282 path->ctls[NID_PATH_VOL_CTL] = 3283 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT); 3284 } 3285 } 3286 if (!path->ctls[NID_PATH_MUTE_CTL]) { 3287 if (nid_has_mute(codec, nid, HDA_OUTPUT)) 3288 path->ctls[NID_PATH_MUTE_CTL] = 3289 HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT); 3290 else if (nid_has_mute(codec, nid, HDA_INPUT)) { 3291 int idx = path->idx[i]; 3292 if (!depth && codec->single_adc_amp) 3293 idx = 0; 3294 path->ctls[NID_PATH_MUTE_CTL] = 3295 HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT); 3296 } 3297 } 3298 } 3299 return 0; 3300 } 3301 3302 static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid) 3303 { 3304 struct hda_gen_spec *spec = codec->spec; 3305 struct auto_pin_cfg *cfg = &spec->autocfg; 3306 unsigned int val; 3307 int i; 3308 3309 if (!spec->inv_dmic_split) 3310 return false; 3311 for (i = 0; i < cfg->num_inputs; i++) { 3312 if (cfg->inputs[i].pin != nid) 3313 continue; 3314 if (cfg->inputs[i].type != AUTO_PIN_MIC) 3315 return false; 3316 val = snd_hda_codec_get_pincfg(codec, nid); 3317 return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT; 3318 } 3319 return false; 3320 } 3321 3322 /* capture switch put callback for a single control with hook call */ 3323 static int cap_single_sw_put(struct snd_kcontrol *kcontrol, 3324 struct snd_ctl_elem_value *ucontrol) 3325 { 3326 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 3327 struct hda_gen_spec *spec = codec->spec; 3328 int ret; 3329 3330 ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol); 3331 if (ret < 0) 3332 return ret; 3333 3334 if (spec->cap_sync_hook) 3335 spec->cap_sync_hook(codec, ucontrol); 3336 3337 return ret; 3338 } 3339 3340 static int add_single_cap_ctl(struct hda_codec *codec, const char *label, 3341 int idx, bool is_switch, unsigned int ctl, 3342 bool inv_dmic) 3343 { 3344 struct hda_gen_spec *spec = codec->spec; 3345 char tmpname[SNDRV_CTL_ELEM_ID_NAME_MAXLEN]; 3346 int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL; 3347 const char *sfx = is_switch ? "Switch" : "Volume"; 3348 unsigned int chs = inv_dmic ? 1 : 3; 3349 struct snd_kcontrol_new *knew; 3350 3351 if (!ctl) 3352 return 0; 3353 3354 if (label) 3355 snprintf(tmpname, sizeof(tmpname), 3356 "%s Capture %s", label, sfx); 3357 else 3358 snprintf(tmpname, sizeof(tmpname), 3359 "Capture %s", sfx); 3360 knew = add_control(spec, type, tmpname, idx, 3361 amp_val_replace_channels(ctl, chs)); 3362 if (!knew) 3363 return -ENOMEM; 3364 if (is_switch) 3365 knew->put = cap_single_sw_put; 3366 if (!inv_dmic) 3367 return 0; 3368 3369 /* Make independent right kcontrol */ 3370 if (label) 3371 snprintf(tmpname, sizeof(tmpname), 3372 "Inverted %s Capture %s", label, sfx); 3373 else 3374 snprintf(tmpname, sizeof(tmpname), 3375 "Inverted Capture %s", sfx); 3376 knew = add_control(spec, type, tmpname, idx, 3377 amp_val_replace_channels(ctl, 2)); 3378 if (!knew) 3379 return -ENOMEM; 3380 if (is_switch) 3381 knew->put = cap_single_sw_put; 3382 return 0; 3383 } 3384 3385 /* create single (and simple) capture volume and switch controls */ 3386 static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx, 3387 unsigned int vol_ctl, unsigned int sw_ctl, 3388 bool inv_dmic) 3389 { 3390 int err; 3391 err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic); 3392 if (err < 0) 3393 return err; 3394 err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic); 3395 if (err < 0) 3396 return err; 3397 return 0; 3398 } 3399 3400 /* create bound capture volume and switch controls */ 3401 static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx, 3402 unsigned int vol_ctl, unsigned int sw_ctl) 3403 { 3404 struct hda_gen_spec *spec = codec->spec; 3405 struct snd_kcontrol_new *knew; 3406 3407 if (vol_ctl) { 3408 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp); 3409 if (!knew) 3410 return -ENOMEM; 3411 knew->index = idx; 3412 knew->private_value = vol_ctl; 3413 knew->subdevice = HDA_SUBDEV_AMP_FLAG; 3414 } 3415 if (sw_ctl) { 3416 knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp); 3417 if (!knew) 3418 return -ENOMEM; 3419 knew->index = idx; 3420 knew->private_value = sw_ctl; 3421 knew->subdevice = HDA_SUBDEV_AMP_FLAG; 3422 } 3423 return 0; 3424 } 3425 3426 /* return the vol ctl when used first in the imux list */ 3427 static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type) 3428 { 3429 struct nid_path *path; 3430 unsigned int ctl; 3431 int i; 3432 3433 path = get_input_path(codec, 0, idx); 3434 if (!path) 3435 return 0; 3436 ctl = path->ctls[type]; 3437 if (!ctl) 3438 return 0; 3439 for (i = 0; i < idx - 1; i++) { 3440 path = get_input_path(codec, 0, i); 3441 if (path && path->ctls[type] == ctl) 3442 return 0; 3443 } 3444 return ctl; 3445 } 3446 3447 /* create individual capture volume and switch controls per input */ 3448 static int create_multi_cap_vol_ctl(struct hda_codec *codec) 3449 { 3450 struct hda_gen_spec *spec = codec->spec; 3451 struct hda_input_mux *imux = &spec->input_mux; 3452 int i, err, type; 3453 3454 for (i = 0; i < imux->num_items; i++) { 3455 bool inv_dmic; 3456 int idx; 3457 3458 idx = imux->items[i].index; 3459 if (idx >= spec->autocfg.num_inputs) 3460 continue; 3461 inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]); 3462 3463 for (type = 0; type < 2; type++) { 3464 err = add_single_cap_ctl(codec, 3465 spec->input_labels[idx], 3466 spec->input_label_idxs[idx], 3467 type, 3468 get_first_cap_ctl(codec, i, type), 3469 inv_dmic); 3470 if (err < 0) 3471 return err; 3472 } 3473 } 3474 return 0; 3475 } 3476 3477 static int create_capture_mixers(struct hda_codec *codec) 3478 { 3479 struct hda_gen_spec *spec = codec->spec; 3480 struct hda_input_mux *imux = &spec->input_mux; 3481 int i, n, nums, err; 3482 3483 if (spec->dyn_adc_switch) 3484 nums = 1; 3485 else 3486 nums = spec->num_adc_nids; 3487 3488 if (!spec->auto_mic && imux->num_items > 1) { 3489 struct snd_kcontrol_new *knew; 3490 const char *name; 3491 name = nums > 1 ? "Input Source" : "Capture Source"; 3492 knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp); 3493 if (!knew) 3494 return -ENOMEM; 3495 knew->count = nums; 3496 } 3497 3498 for (n = 0; n < nums; n++) { 3499 bool multi = false; 3500 bool multi_cap_vol = spec->multi_cap_vol; 3501 bool inv_dmic = false; 3502 int vol, sw; 3503 3504 vol = sw = 0; 3505 for (i = 0; i < imux->num_items; i++) { 3506 struct nid_path *path; 3507 path = get_input_path(codec, n, i); 3508 if (!path) 3509 continue; 3510 parse_capvol_in_path(codec, path); 3511 if (!vol) 3512 vol = path->ctls[NID_PATH_VOL_CTL]; 3513 else if (vol != path->ctls[NID_PATH_VOL_CTL]) { 3514 multi = true; 3515 if (!same_amp_caps(codec, vol, 3516 path->ctls[NID_PATH_VOL_CTL], HDA_INPUT)) 3517 multi_cap_vol = true; 3518 } 3519 if (!sw) 3520 sw = path->ctls[NID_PATH_MUTE_CTL]; 3521 else if (sw != path->ctls[NID_PATH_MUTE_CTL]) { 3522 multi = true; 3523 if (!same_amp_caps(codec, sw, 3524 path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT)) 3525 multi_cap_vol = true; 3526 } 3527 if (is_inv_dmic_pin(codec, spec->imux_pins[i])) 3528 inv_dmic = true; 3529 } 3530 3531 if (!multi) 3532 err = create_single_cap_vol_ctl(codec, n, vol, sw, 3533 inv_dmic); 3534 else if (!multi_cap_vol) 3535 err = create_bind_cap_vol_ctl(codec, n, vol, sw); 3536 else 3537 err = create_multi_cap_vol_ctl(codec); 3538 if (err < 0) 3539 return err; 3540 } 3541 3542 return 0; 3543 } 3544 3545 /* 3546 * add mic boosts if needed 3547 */ 3548 3549 /* check whether the given amp is feasible as a boost volume */ 3550 static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid, 3551 int dir, int idx) 3552 { 3553 unsigned int step; 3554 3555 if (!nid_has_volume(codec, nid, dir) || 3556 is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) || 3557 is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL)) 3558 return false; 3559 3560 step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE) 3561 >> AC_AMPCAP_STEP_SIZE_SHIFT; 3562 if (step < 0x20) 3563 return false; 3564 return true; 3565 } 3566 3567 /* look for a boost amp in a widget close to the pin */ 3568 static unsigned int look_for_boost_amp(struct hda_codec *codec, 3569 struct nid_path *path) 3570 { 3571 unsigned int val = 0; 3572 hda_nid_t nid; 3573 int depth; 3574 3575 for (depth = 0; depth < 3; depth++) { 3576 if (depth >= path->depth - 1) 3577 break; 3578 nid = path->path[depth]; 3579 if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) { 3580 val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT); 3581 break; 3582 } else if (check_boost_vol(codec, nid, HDA_INPUT, 3583 path->idx[depth])) { 3584 val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth], 3585 HDA_INPUT); 3586 break; 3587 } 3588 } 3589 3590 return val; 3591 } 3592 3593 static int parse_mic_boost(struct hda_codec *codec) 3594 { 3595 struct hda_gen_spec *spec = codec->spec; 3596 struct auto_pin_cfg *cfg = &spec->autocfg; 3597 struct hda_input_mux *imux = &spec->input_mux; 3598 int i; 3599 3600 if (!spec->num_adc_nids) 3601 return 0; 3602 3603 for (i = 0; i < imux->num_items; i++) { 3604 struct nid_path *path; 3605 unsigned int val; 3606 int idx; 3607 char boost_label[SNDRV_CTL_ELEM_ID_NAME_MAXLEN]; 3608 3609 idx = imux->items[i].index; 3610 if (idx >= imux->num_items) 3611 continue; 3612 3613 /* check only line-in and mic pins */ 3614 if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN) 3615 continue; 3616 3617 path = get_input_path(codec, 0, i); 3618 if (!path) 3619 continue; 3620 3621 val = look_for_boost_amp(codec, path); 3622 if (!val) 3623 continue; 3624 3625 /* create a boost control */ 3626 snprintf(boost_label, sizeof(boost_label), 3627 "%s Boost Volume", spec->input_labels[idx]); 3628 if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label, 3629 spec->input_label_idxs[idx], val)) 3630 return -ENOMEM; 3631 3632 path->ctls[NID_PATH_BOOST_CTL] = val; 3633 } 3634 return 0; 3635 } 3636 3637 /* 3638 * parse digital I/Os and set up NIDs in BIOS auto-parse mode 3639 */ 3640 static void parse_digital(struct hda_codec *codec) 3641 { 3642 struct hda_gen_spec *spec = codec->spec; 3643 struct nid_path *path; 3644 int i, nums; 3645 hda_nid_t dig_nid, pin; 3646 3647 /* support multiple SPDIFs; the secondary is set up as a slave */ 3648 nums = 0; 3649 for (i = 0; i < spec->autocfg.dig_outs; i++) { 3650 pin = spec->autocfg.dig_out_pins[i]; 3651 dig_nid = look_for_dac(codec, pin, true); 3652 if (!dig_nid) 3653 continue; 3654 path = snd_hda_add_new_path(codec, dig_nid, pin, 0); 3655 if (!path) 3656 continue; 3657 print_nid_path("digout", path); 3658 path->active = true; 3659 spec->digout_paths[i] = snd_hda_get_path_idx(codec, path); 3660 set_pin_target(codec, pin, PIN_OUT, false); 3661 if (!nums) { 3662 spec->multiout.dig_out_nid = dig_nid; 3663 spec->dig_out_type = spec->autocfg.dig_out_type[0]; 3664 } else { 3665 spec->multiout.slave_dig_outs = spec->slave_dig_outs; 3666 if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1) 3667 break; 3668 spec->slave_dig_outs[nums - 1] = dig_nid; 3669 } 3670 nums++; 3671 } 3672 3673 if (spec->autocfg.dig_in_pin) { 3674 pin = spec->autocfg.dig_in_pin; 3675 dig_nid = codec->start_nid; 3676 for (i = 0; i < codec->num_nodes; i++, dig_nid++) { 3677 unsigned int wcaps = get_wcaps(codec, dig_nid); 3678 if (get_wcaps_type(wcaps) != AC_WID_AUD_IN) 3679 continue; 3680 if (!(wcaps & AC_WCAP_DIGITAL)) 3681 continue; 3682 path = snd_hda_add_new_path(codec, pin, dig_nid, 0); 3683 if (path) { 3684 print_nid_path("digin", path); 3685 path->active = true; 3686 spec->dig_in_nid = dig_nid; 3687 spec->digin_path = snd_hda_get_path_idx(codec, path); 3688 set_pin_target(codec, pin, PIN_IN, false); 3689 break; 3690 } 3691 } 3692 } 3693 } 3694 3695 3696 /* 3697 * input MUX handling 3698 */ 3699 3700 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur); 3701 3702 /* select the given imux item; either unmute exclusively or select the route */ 3703 static int mux_select(struct hda_codec *codec, unsigned int adc_idx, 3704 unsigned int idx) 3705 { 3706 struct hda_gen_spec *spec = codec->spec; 3707 const struct hda_input_mux *imux; 3708 struct nid_path *old_path, *path; 3709 3710 imux = &spec->input_mux; 3711 if (!imux->num_items) 3712 return 0; 3713 3714 if (idx >= imux->num_items) 3715 idx = imux->num_items - 1; 3716 if (spec->cur_mux[adc_idx] == idx) 3717 return 0; 3718 3719 old_path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]); 3720 if (!old_path) 3721 return 0; 3722 if (old_path->active) 3723 snd_hda_activate_path(codec, old_path, false, false); 3724 3725 spec->cur_mux[adc_idx] = idx; 3726 3727 if (spec->hp_mic) 3728 update_hp_mic(codec, adc_idx, false); 3729 3730 if (spec->dyn_adc_switch) 3731 dyn_adc_pcm_resetup(codec, idx); 3732 3733 path = get_input_path(codec, adc_idx, idx); 3734 if (!path) 3735 return 0; 3736 if (path->active) 3737 return 0; 3738 snd_hda_activate_path(codec, path, true, false); 3739 if (spec->cap_sync_hook) 3740 spec->cap_sync_hook(codec, NULL); 3741 path_power_down_sync(codec, old_path); 3742 return 1; 3743 } 3744 3745 3746 /* 3747 * Jack detections for HP auto-mute and mic-switch 3748 */ 3749 3750 /* check each pin in the given array; returns true if any of them is plugged */ 3751 static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins) 3752 { 3753 int i; 3754 bool present = false; 3755 3756 for (i = 0; i < num_pins; i++) { 3757 hda_nid_t nid = pins[i]; 3758 if (!nid) 3759 break; 3760 /* don't detect pins retasked as inputs */ 3761 if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN) 3762 continue; 3763 if (snd_hda_jack_detect_state(codec, nid) == HDA_JACK_PRESENT) 3764 present = true; 3765 } 3766 return present; 3767 } 3768 3769 /* standard HP/line-out auto-mute helper */ 3770 static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins, 3771 int *paths, bool mute) 3772 { 3773 struct hda_gen_spec *spec = codec->spec; 3774 int i; 3775 3776 for (i = 0; i < num_pins; i++) { 3777 hda_nid_t nid = pins[i]; 3778 unsigned int val, oldval; 3779 if (!nid) 3780 break; 3781 3782 if (spec->auto_mute_via_amp) { 3783 struct nid_path *path; 3784 hda_nid_t mute_nid; 3785 3786 path = snd_hda_get_path_from_idx(codec, paths[i]); 3787 if (!path) 3788 continue; 3789 mute_nid = get_amp_nid_(path->ctls[NID_PATH_MUTE_CTL]); 3790 if (!mute_nid) 3791 continue; 3792 if (mute) 3793 spec->mute_bits |= (1ULL << mute_nid); 3794 else 3795 spec->mute_bits &= ~(1ULL << mute_nid); 3796 set_pin_eapd(codec, nid, !mute); 3797 continue; 3798 } 3799 3800 oldval = snd_hda_codec_get_pin_target(codec, nid); 3801 if (oldval & PIN_IN) 3802 continue; /* no mute for inputs */ 3803 /* don't reset VREF value in case it's controlling 3804 * the amp (see alc861_fixup_asus_amp_vref_0f()) 3805 */ 3806 if (spec->keep_vref_in_automute) 3807 val = oldval & ~PIN_HP; 3808 else 3809 val = 0; 3810 if (!mute) 3811 val |= oldval; 3812 /* here we call update_pin_ctl() so that the pinctl is changed 3813 * without changing the pinctl target value; 3814 * the original target value will be still referred at the 3815 * init / resume again 3816 */ 3817 update_pin_ctl(codec, nid, val); 3818 set_pin_eapd(codec, nid, !mute); 3819 } 3820 } 3821 3822 /* Toggle outputs muting */ 3823 void snd_hda_gen_update_outputs(struct hda_codec *codec) 3824 { 3825 struct hda_gen_spec *spec = codec->spec; 3826 int *paths; 3827 int on; 3828 3829 /* Control HP pins/amps depending on master_mute state; 3830 * in general, HP pins/amps control should be enabled in all cases, 3831 * but currently set only for master_mute, just to be safe 3832 */ 3833 if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT) 3834 paths = spec->out_paths; 3835 else 3836 paths = spec->hp_paths; 3837 do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins), 3838 spec->autocfg.hp_pins, paths, spec->master_mute); 3839 3840 if (!spec->automute_speaker) 3841 on = 0; 3842 else 3843 on = spec->hp_jack_present | spec->line_jack_present; 3844 on |= spec->master_mute; 3845 spec->speaker_muted = on; 3846 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT) 3847 paths = spec->out_paths; 3848 else 3849 paths = spec->speaker_paths; 3850 do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins), 3851 spec->autocfg.speaker_pins, paths, on); 3852 3853 /* toggle line-out mutes if needed, too */ 3854 /* if LO is a copy of either HP or Speaker, don't need to handle it */ 3855 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] || 3856 spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0]) 3857 return; 3858 if (!spec->automute_lo) 3859 on = 0; 3860 else 3861 on = spec->hp_jack_present; 3862 on |= spec->master_mute; 3863 spec->line_out_muted = on; 3864 paths = spec->out_paths; 3865 do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins), 3866 spec->autocfg.line_out_pins, paths, on); 3867 } 3868 EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs); 3869 3870 static void call_update_outputs(struct hda_codec *codec) 3871 { 3872 struct hda_gen_spec *spec = codec->spec; 3873 if (spec->automute_hook) 3874 spec->automute_hook(codec); 3875 else 3876 snd_hda_gen_update_outputs(codec); 3877 3878 /* sync the whole vmaster slaves to reflect the new auto-mute status */ 3879 if (spec->auto_mute_via_amp && !codec->bus->shutdown) 3880 snd_ctl_sync_vmaster(spec->vmaster_mute.sw_kctl, false); 3881 } 3882 3883 /* standard HP-automute helper */ 3884 void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack) 3885 { 3886 struct hda_gen_spec *spec = codec->spec; 3887 hda_nid_t *pins = spec->autocfg.hp_pins; 3888 int num_pins = ARRAY_SIZE(spec->autocfg.hp_pins); 3889 3890 /* No detection for the first HP jack during indep-HP mode */ 3891 if (spec->indep_hp_enabled) { 3892 pins++; 3893 num_pins--; 3894 } 3895 3896 spec->hp_jack_present = detect_jacks(codec, num_pins, pins); 3897 if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo)) 3898 return; 3899 call_update_outputs(codec); 3900 } 3901 EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute); 3902 3903 /* standard line-out-automute helper */ 3904 void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack) 3905 { 3906 struct hda_gen_spec *spec = codec->spec; 3907 3908 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT) 3909 return; 3910 /* check LO jack only when it's different from HP */ 3911 if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0]) 3912 return; 3913 3914 spec->line_jack_present = 3915 detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins), 3916 spec->autocfg.line_out_pins); 3917 if (!spec->automute_speaker || !spec->detect_lo) 3918 return; 3919 call_update_outputs(codec); 3920 } 3921 EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute); 3922 3923 /* standard mic auto-switch helper */ 3924 void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack) 3925 { 3926 struct hda_gen_spec *spec = codec->spec; 3927 int i; 3928 3929 if (!spec->auto_mic) 3930 return; 3931 3932 for (i = spec->am_num_entries - 1; i > 0; i--) { 3933 hda_nid_t pin = spec->am_entry[i].pin; 3934 /* don't detect pins retasked as outputs */ 3935 if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN) 3936 continue; 3937 if (snd_hda_jack_detect_state(codec, pin) == HDA_JACK_PRESENT) { 3938 mux_select(codec, 0, spec->am_entry[i].idx); 3939 return; 3940 } 3941 } 3942 mux_select(codec, 0, spec->am_entry[0].idx); 3943 } 3944 EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch); 3945 3946 /* call appropriate hooks */ 3947 static void call_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack) 3948 { 3949 struct hda_gen_spec *spec = codec->spec; 3950 if (spec->hp_automute_hook) 3951 spec->hp_automute_hook(codec, jack); 3952 else 3953 snd_hda_gen_hp_automute(codec, jack); 3954 } 3955 3956 static void call_line_automute(struct hda_codec *codec, 3957 struct hda_jack_tbl *jack) 3958 { 3959 struct hda_gen_spec *spec = codec->spec; 3960 if (spec->line_automute_hook) 3961 spec->line_automute_hook(codec, jack); 3962 else 3963 snd_hda_gen_line_automute(codec, jack); 3964 } 3965 3966 static void call_mic_autoswitch(struct hda_codec *codec, 3967 struct hda_jack_tbl *jack) 3968 { 3969 struct hda_gen_spec *spec = codec->spec; 3970 if (spec->mic_autoswitch_hook) 3971 spec->mic_autoswitch_hook(codec, jack); 3972 else 3973 snd_hda_gen_mic_autoswitch(codec, jack); 3974 } 3975 3976 /* update jack retasking */ 3977 static void update_automute_all(struct hda_codec *codec) 3978 { 3979 call_hp_automute(codec, NULL); 3980 call_line_automute(codec, NULL); 3981 call_mic_autoswitch(codec, NULL); 3982 } 3983 3984 /* 3985 * Auto-Mute mode mixer enum support 3986 */ 3987 static int automute_mode_info(struct snd_kcontrol *kcontrol, 3988 struct snd_ctl_elem_info *uinfo) 3989 { 3990 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 3991 struct hda_gen_spec *spec = codec->spec; 3992 static const char * const texts3[] = { 3993 "Disabled", "Speaker Only", "Line Out+Speaker" 3994 }; 3995 3996 if (spec->automute_speaker_possible && spec->automute_lo_possible) 3997 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3); 3998 return snd_hda_enum_bool_helper_info(kcontrol, uinfo); 3999 } 4000 4001 static int automute_mode_get(struct snd_kcontrol *kcontrol, 4002 struct snd_ctl_elem_value *ucontrol) 4003 { 4004 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 4005 struct hda_gen_spec *spec = codec->spec; 4006 unsigned int val = 0; 4007 if (spec->automute_speaker) 4008 val++; 4009 if (spec->automute_lo) 4010 val++; 4011 4012 ucontrol->value.enumerated.item[0] = val; 4013 return 0; 4014 } 4015 4016 static int automute_mode_put(struct snd_kcontrol *kcontrol, 4017 struct snd_ctl_elem_value *ucontrol) 4018 { 4019 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 4020 struct hda_gen_spec *spec = codec->spec; 4021 4022 switch (ucontrol->value.enumerated.item[0]) { 4023 case 0: 4024 if (!spec->automute_speaker && !spec->automute_lo) 4025 return 0; 4026 spec->automute_speaker = 0; 4027 spec->automute_lo = 0; 4028 break; 4029 case 1: 4030 if (spec->automute_speaker_possible) { 4031 if (!spec->automute_lo && spec->automute_speaker) 4032 return 0; 4033 spec->automute_speaker = 1; 4034 spec->automute_lo = 0; 4035 } else if (spec->automute_lo_possible) { 4036 if (spec->automute_lo) 4037 return 0; 4038 spec->automute_lo = 1; 4039 } else 4040 return -EINVAL; 4041 break; 4042 case 2: 4043 if (!spec->automute_lo_possible || !spec->automute_speaker_possible) 4044 return -EINVAL; 4045 if (spec->automute_speaker && spec->automute_lo) 4046 return 0; 4047 spec->automute_speaker = 1; 4048 spec->automute_lo = 1; 4049 break; 4050 default: 4051 return -EINVAL; 4052 } 4053 call_update_outputs(codec); 4054 return 1; 4055 } 4056 4057 static const struct snd_kcontrol_new automute_mode_enum = { 4058 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 4059 .name = "Auto-Mute Mode", 4060 .info = automute_mode_info, 4061 .get = automute_mode_get, 4062 .put = automute_mode_put, 4063 }; 4064 4065 static int add_automute_mode_enum(struct hda_codec *codec) 4066 { 4067 struct hda_gen_spec *spec = codec->spec; 4068 4069 if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum)) 4070 return -ENOMEM; 4071 return 0; 4072 } 4073 4074 /* 4075 * Check the availability of HP/line-out auto-mute; 4076 * Set up appropriately if really supported 4077 */ 4078 static int check_auto_mute_availability(struct hda_codec *codec) 4079 { 4080 struct hda_gen_spec *spec = codec->spec; 4081 struct auto_pin_cfg *cfg = &spec->autocfg; 4082 int present = 0; 4083 int i, err; 4084 4085 if (spec->suppress_auto_mute) 4086 return 0; 4087 4088 if (cfg->hp_pins[0]) 4089 present++; 4090 if (cfg->line_out_pins[0]) 4091 present++; 4092 if (cfg->speaker_pins[0]) 4093 present++; 4094 if (present < 2) /* need two different output types */ 4095 return 0; 4096 4097 if (!cfg->speaker_pins[0] && 4098 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) { 4099 memcpy(cfg->speaker_pins, cfg->line_out_pins, 4100 sizeof(cfg->speaker_pins)); 4101 cfg->speaker_outs = cfg->line_outs; 4102 } 4103 4104 if (!cfg->hp_pins[0] && 4105 cfg->line_out_type == AUTO_PIN_HP_OUT) { 4106 memcpy(cfg->hp_pins, cfg->line_out_pins, 4107 sizeof(cfg->hp_pins)); 4108 cfg->hp_outs = cfg->line_outs; 4109 } 4110 4111 for (i = 0; i < cfg->hp_outs; i++) { 4112 hda_nid_t nid = cfg->hp_pins[i]; 4113 if (!is_jack_detectable(codec, nid)) 4114 continue; 4115 snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n", 4116 nid); 4117 snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT, 4118 call_hp_automute); 4119 spec->detect_hp = 1; 4120 } 4121 4122 if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) { 4123 if (cfg->speaker_outs) 4124 for (i = 0; i < cfg->line_outs; i++) { 4125 hda_nid_t nid = cfg->line_out_pins[i]; 4126 if (!is_jack_detectable(codec, nid)) 4127 continue; 4128 snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid); 4129 snd_hda_jack_detect_enable_callback(codec, nid, 4130 HDA_GEN_FRONT_EVENT, 4131 call_line_automute); 4132 spec->detect_lo = 1; 4133 } 4134 spec->automute_lo_possible = spec->detect_hp; 4135 } 4136 4137 spec->automute_speaker_possible = cfg->speaker_outs && 4138 (spec->detect_hp || spec->detect_lo); 4139 4140 spec->automute_lo = spec->automute_lo_possible; 4141 spec->automute_speaker = spec->automute_speaker_possible; 4142 4143 if (spec->automute_speaker_possible || spec->automute_lo_possible) { 4144 /* create a control for automute mode */ 4145 err = add_automute_mode_enum(codec); 4146 if (err < 0) 4147 return err; 4148 } 4149 return 0; 4150 } 4151 4152 /* check whether all auto-mic pins are valid; setup indices if OK */ 4153 static bool auto_mic_check_imux(struct hda_codec *codec) 4154 { 4155 struct hda_gen_spec *spec = codec->spec; 4156 const struct hda_input_mux *imux; 4157 int i; 4158 4159 imux = &spec->input_mux; 4160 for (i = 0; i < spec->am_num_entries; i++) { 4161 spec->am_entry[i].idx = 4162 find_idx_in_nid_list(spec->am_entry[i].pin, 4163 spec->imux_pins, imux->num_items); 4164 if (spec->am_entry[i].idx < 0) 4165 return false; /* no corresponding imux */ 4166 } 4167 4168 /* we don't need the jack detection for the first pin */ 4169 for (i = 1; i < spec->am_num_entries; i++) 4170 snd_hda_jack_detect_enable_callback(codec, 4171 spec->am_entry[i].pin, 4172 HDA_GEN_MIC_EVENT, 4173 call_mic_autoswitch); 4174 return true; 4175 } 4176 4177 static int compare_attr(const void *ap, const void *bp) 4178 { 4179 const struct automic_entry *a = ap; 4180 const struct automic_entry *b = bp; 4181 return (int)(a->attr - b->attr); 4182 } 4183 4184 /* 4185 * Check the availability of auto-mic switch; 4186 * Set up if really supported 4187 */ 4188 static int check_auto_mic_availability(struct hda_codec *codec) 4189 { 4190 struct hda_gen_spec *spec = codec->spec; 4191 struct auto_pin_cfg *cfg = &spec->autocfg; 4192 unsigned int types; 4193 int i, num_pins; 4194 4195 if (spec->suppress_auto_mic) 4196 return 0; 4197 4198 types = 0; 4199 num_pins = 0; 4200 for (i = 0; i < cfg->num_inputs; i++) { 4201 hda_nid_t nid = cfg->inputs[i].pin; 4202 unsigned int attr; 4203 attr = snd_hda_codec_get_pincfg(codec, nid); 4204 attr = snd_hda_get_input_pin_attr(attr); 4205 if (types & (1 << attr)) 4206 return 0; /* already occupied */ 4207 switch (attr) { 4208 case INPUT_PIN_ATTR_INT: 4209 if (cfg->inputs[i].type != AUTO_PIN_MIC) 4210 return 0; /* invalid type */ 4211 break; 4212 case INPUT_PIN_ATTR_UNUSED: 4213 return 0; /* invalid entry */ 4214 default: 4215 if (cfg->inputs[i].type > AUTO_PIN_LINE_IN) 4216 return 0; /* invalid type */ 4217 if (!spec->line_in_auto_switch && 4218 cfg->inputs[i].type != AUTO_PIN_MIC) 4219 return 0; /* only mic is allowed */ 4220 if (!is_jack_detectable(codec, nid)) 4221 return 0; /* no unsol support */ 4222 break; 4223 } 4224 if (num_pins >= MAX_AUTO_MIC_PINS) 4225 return 0; 4226 types |= (1 << attr); 4227 spec->am_entry[num_pins].pin = nid; 4228 spec->am_entry[num_pins].attr = attr; 4229 num_pins++; 4230 } 4231 4232 if (num_pins < 2) 4233 return 0; 4234 4235 spec->am_num_entries = num_pins; 4236 /* sort the am_entry in the order of attr so that the pin with a 4237 * higher attr will be selected when the jack is plugged. 4238 */ 4239 sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]), 4240 compare_attr, NULL); 4241 4242 if (!auto_mic_check_imux(codec)) 4243 return 0; 4244 4245 spec->auto_mic = 1; 4246 spec->num_adc_nids = 1; 4247 spec->cur_mux[0] = spec->am_entry[0].idx; 4248 snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n", 4249 spec->am_entry[0].pin, 4250 spec->am_entry[1].pin, 4251 spec->am_entry[2].pin); 4252 4253 return 0; 4254 } 4255 4256 /* power_filter hook; make inactive widgets into power down */ 4257 static unsigned int snd_hda_gen_path_power_filter(struct hda_codec *codec, 4258 hda_nid_t nid, 4259 unsigned int power_state) 4260 { 4261 if (power_state != AC_PWRST_D0) 4262 return power_state; 4263 if (get_wcaps_type(get_wcaps(codec, nid)) >= AC_WID_POWER) 4264 return power_state; 4265 if (is_active_nid_for_any(codec, nid)) 4266 return power_state; 4267 return AC_PWRST_D3; 4268 } 4269 4270 4271 /* 4272 * Parse the given BIOS configuration and set up the hda_gen_spec 4273 * 4274 * return 1 if successful, 0 if the proper config is not found, 4275 * or a negative error code 4276 */ 4277 int snd_hda_gen_parse_auto_config(struct hda_codec *codec, 4278 struct auto_pin_cfg *cfg) 4279 { 4280 struct hda_gen_spec *spec = codec->spec; 4281 int err; 4282 4283 parse_user_hints(codec); 4284 4285 if (spec->mixer_nid && !spec->mixer_merge_nid) 4286 spec->mixer_merge_nid = spec->mixer_nid; 4287 4288 if (cfg != &spec->autocfg) { 4289 spec->autocfg = *cfg; 4290 cfg = &spec->autocfg; 4291 } 4292 4293 if (!spec->main_out_badness) 4294 spec->main_out_badness = &hda_main_out_badness; 4295 if (!spec->extra_out_badness) 4296 spec->extra_out_badness = &hda_extra_out_badness; 4297 4298 fill_all_dac_nids(codec); 4299 4300 if (!cfg->line_outs) { 4301 if (cfg->dig_outs || cfg->dig_in_pin) { 4302 spec->multiout.max_channels = 2; 4303 spec->no_analog = 1; 4304 goto dig_only; 4305 } 4306 return 0; /* can't find valid BIOS pin config */ 4307 } 4308 4309 if (!spec->no_primary_hp && 4310 cfg->line_out_type == AUTO_PIN_SPEAKER_OUT && 4311 cfg->line_outs <= cfg->hp_outs) { 4312 /* use HP as primary out */ 4313 cfg->speaker_outs = cfg->line_outs; 4314 memcpy(cfg->speaker_pins, cfg->line_out_pins, 4315 sizeof(cfg->speaker_pins)); 4316 cfg->line_outs = cfg->hp_outs; 4317 memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins)); 4318 cfg->hp_outs = 0; 4319 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins)); 4320 cfg->line_out_type = AUTO_PIN_HP_OUT; 4321 } 4322 4323 err = parse_output_paths(codec); 4324 if (err < 0) 4325 return err; 4326 err = create_multi_channel_mode(codec); 4327 if (err < 0) 4328 return err; 4329 err = create_multi_out_ctls(codec, cfg); 4330 if (err < 0) 4331 return err; 4332 err = create_hp_out_ctls(codec); 4333 if (err < 0) 4334 return err; 4335 err = create_speaker_out_ctls(codec); 4336 if (err < 0) 4337 return err; 4338 err = create_indep_hp_ctls(codec); 4339 if (err < 0) 4340 return err; 4341 err = create_loopback_mixing_ctl(codec); 4342 if (err < 0) 4343 return err; 4344 err = create_hp_mic(codec); 4345 if (err < 0) 4346 return err; 4347 err = create_input_ctls(codec); 4348 if (err < 0) 4349 return err; 4350 4351 spec->const_channel_count = spec->ext_channel_count; 4352 /* check the multiple speaker and headphone pins */ 4353 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) 4354 spec->const_channel_count = max(spec->const_channel_count, 4355 cfg->speaker_outs * 2); 4356 if (cfg->line_out_type != AUTO_PIN_HP_OUT) 4357 spec->const_channel_count = max(spec->const_channel_count, 4358 cfg->hp_outs * 2); 4359 spec->multiout.max_channels = max(spec->ext_channel_count, 4360 spec->const_channel_count); 4361 4362 err = check_auto_mute_availability(codec); 4363 if (err < 0) 4364 return err; 4365 4366 err = check_dyn_adc_switch(codec); 4367 if (err < 0) 4368 return err; 4369 4370 err = check_auto_mic_availability(codec); 4371 if (err < 0) 4372 return err; 4373 4374 err = create_capture_mixers(codec); 4375 if (err < 0) 4376 return err; 4377 4378 err = parse_mic_boost(codec); 4379 if (err < 0) 4380 return err; 4381 4382 if (spec->add_jack_modes) { 4383 if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) { 4384 err = create_out_jack_modes(codec, cfg->line_outs, 4385 cfg->line_out_pins); 4386 if (err < 0) 4387 return err; 4388 } 4389 if (cfg->line_out_type != AUTO_PIN_HP_OUT) { 4390 err = create_out_jack_modes(codec, cfg->hp_outs, 4391 cfg->hp_pins); 4392 if (err < 0) 4393 return err; 4394 } 4395 } 4396 4397 dig_only: 4398 parse_digital(codec); 4399 4400 if (spec->power_down_unused) 4401 codec->power_filter = snd_hda_gen_path_power_filter; 4402 4403 if (!spec->no_analog && spec->beep_nid) { 4404 err = snd_hda_attach_beep_device(codec, spec->beep_nid); 4405 if (err < 0) 4406 return err; 4407 } 4408 4409 return 1; 4410 } 4411 EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config); 4412 4413 4414 /* 4415 * Build control elements 4416 */ 4417 4418 /* slave controls for virtual master */ 4419 static const char * const slave_pfxs[] = { 4420 "Front", "Surround", "Center", "LFE", "Side", 4421 "Headphone", "Speaker", "Mono", "Line Out", 4422 "CLFE", "Bass Speaker", "PCM", 4423 "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side", 4424 "Headphone Front", "Headphone Surround", "Headphone CLFE", 4425 "Headphone Side", 4426 NULL, 4427 }; 4428 4429 int snd_hda_gen_build_controls(struct hda_codec *codec) 4430 { 4431 struct hda_gen_spec *spec = codec->spec; 4432 int err; 4433 4434 if (spec->kctls.used) { 4435 err = snd_hda_add_new_ctls(codec, spec->kctls.list); 4436 if (err < 0) 4437 return err; 4438 } 4439 4440 if (spec->multiout.dig_out_nid) { 4441 err = snd_hda_create_dig_out_ctls(codec, 4442 spec->multiout.dig_out_nid, 4443 spec->multiout.dig_out_nid, 4444 spec->pcm_rec[1].pcm_type); 4445 if (err < 0) 4446 return err; 4447 if (!spec->no_analog) { 4448 err = snd_hda_create_spdif_share_sw(codec, 4449 &spec->multiout); 4450 if (err < 0) 4451 return err; 4452 spec->multiout.share_spdif = 1; 4453 } 4454 } 4455 if (spec->dig_in_nid) { 4456 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid); 4457 if (err < 0) 4458 return err; 4459 } 4460 4461 /* if we have no master control, let's create it */ 4462 if (!spec->no_analog && 4463 !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) { 4464 err = snd_hda_add_vmaster(codec, "Master Playback Volume", 4465 spec->vmaster_tlv, slave_pfxs, 4466 "Playback Volume"); 4467 if (err < 0) 4468 return err; 4469 } 4470 if (!spec->no_analog && 4471 !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) { 4472 err = __snd_hda_add_vmaster(codec, "Master Playback Switch", 4473 NULL, slave_pfxs, 4474 "Playback Switch", 4475 true, &spec->vmaster_mute.sw_kctl); 4476 if (err < 0) 4477 return err; 4478 if (spec->vmaster_mute.hook) 4479 snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute, 4480 spec->vmaster_mute_enum); 4481 } 4482 4483 free_kctls(spec); /* no longer needed */ 4484 4485 err = snd_hda_jack_add_kctls(codec, &spec->autocfg); 4486 if (err < 0) 4487 return err; 4488 4489 return 0; 4490 } 4491 EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls); 4492 4493 4494 /* 4495 * PCM definitions 4496 */ 4497 4498 static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo, 4499 struct hda_codec *codec, 4500 struct snd_pcm_substream *substream, 4501 int action) 4502 { 4503 struct hda_gen_spec *spec = codec->spec; 4504 if (spec->pcm_playback_hook) 4505 spec->pcm_playback_hook(hinfo, codec, substream, action); 4506 } 4507 4508 static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo, 4509 struct hda_codec *codec, 4510 struct snd_pcm_substream *substream, 4511 int action) 4512 { 4513 struct hda_gen_spec *spec = codec->spec; 4514 if (spec->pcm_capture_hook) 4515 spec->pcm_capture_hook(hinfo, codec, substream, action); 4516 } 4517 4518 /* 4519 * Analog playback callbacks 4520 */ 4521 static int playback_pcm_open(struct hda_pcm_stream *hinfo, 4522 struct hda_codec *codec, 4523 struct snd_pcm_substream *substream) 4524 { 4525 struct hda_gen_spec *spec = codec->spec; 4526 int err; 4527 4528 mutex_lock(&spec->pcm_mutex); 4529 err = snd_hda_multi_out_analog_open(codec, 4530 &spec->multiout, substream, 4531 hinfo); 4532 if (!err) { 4533 spec->active_streams |= 1 << STREAM_MULTI_OUT; 4534 call_pcm_playback_hook(hinfo, codec, substream, 4535 HDA_GEN_PCM_ACT_OPEN); 4536 } 4537 mutex_unlock(&spec->pcm_mutex); 4538 return err; 4539 } 4540 4541 static int playback_pcm_prepare(struct hda_pcm_stream *hinfo, 4542 struct hda_codec *codec, 4543 unsigned int stream_tag, 4544 unsigned int format, 4545 struct snd_pcm_substream *substream) 4546 { 4547 struct hda_gen_spec *spec = codec->spec; 4548 int err; 4549 4550 err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout, 4551 stream_tag, format, substream); 4552 if (!err) 4553 call_pcm_playback_hook(hinfo, codec, substream, 4554 HDA_GEN_PCM_ACT_PREPARE); 4555 return err; 4556 } 4557 4558 static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo, 4559 struct hda_codec *codec, 4560 struct snd_pcm_substream *substream) 4561 { 4562 struct hda_gen_spec *spec = codec->spec; 4563 int err; 4564 4565 err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout); 4566 if (!err) 4567 call_pcm_playback_hook(hinfo, codec, substream, 4568 HDA_GEN_PCM_ACT_CLEANUP); 4569 return err; 4570 } 4571 4572 static int playback_pcm_close(struct hda_pcm_stream *hinfo, 4573 struct hda_codec *codec, 4574 struct snd_pcm_substream *substream) 4575 { 4576 struct hda_gen_spec *spec = codec->spec; 4577 mutex_lock(&spec->pcm_mutex); 4578 spec->active_streams &= ~(1 << STREAM_MULTI_OUT); 4579 call_pcm_playback_hook(hinfo, codec, substream, 4580 HDA_GEN_PCM_ACT_CLOSE); 4581 mutex_unlock(&spec->pcm_mutex); 4582 return 0; 4583 } 4584 4585 static int capture_pcm_open(struct hda_pcm_stream *hinfo, 4586 struct hda_codec *codec, 4587 struct snd_pcm_substream *substream) 4588 { 4589 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN); 4590 return 0; 4591 } 4592 4593 static int capture_pcm_prepare(struct hda_pcm_stream *hinfo, 4594 struct hda_codec *codec, 4595 unsigned int stream_tag, 4596 unsigned int format, 4597 struct snd_pcm_substream *substream) 4598 { 4599 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format); 4600 call_pcm_capture_hook(hinfo, codec, substream, 4601 HDA_GEN_PCM_ACT_PREPARE); 4602 return 0; 4603 } 4604 4605 static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo, 4606 struct hda_codec *codec, 4607 struct snd_pcm_substream *substream) 4608 { 4609 snd_hda_codec_cleanup_stream(codec, hinfo->nid); 4610 call_pcm_capture_hook(hinfo, codec, substream, 4611 HDA_GEN_PCM_ACT_CLEANUP); 4612 return 0; 4613 } 4614 4615 static int capture_pcm_close(struct hda_pcm_stream *hinfo, 4616 struct hda_codec *codec, 4617 struct snd_pcm_substream *substream) 4618 { 4619 call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE); 4620 return 0; 4621 } 4622 4623 static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo, 4624 struct hda_codec *codec, 4625 struct snd_pcm_substream *substream) 4626 { 4627 struct hda_gen_spec *spec = codec->spec; 4628 int err = 0; 4629 4630 mutex_lock(&spec->pcm_mutex); 4631 if (!spec->indep_hp_enabled) 4632 err = -EBUSY; 4633 else 4634 spec->active_streams |= 1 << STREAM_INDEP_HP; 4635 call_pcm_playback_hook(hinfo, codec, substream, 4636 HDA_GEN_PCM_ACT_OPEN); 4637 mutex_unlock(&spec->pcm_mutex); 4638 return err; 4639 } 4640 4641 static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo, 4642 struct hda_codec *codec, 4643 struct snd_pcm_substream *substream) 4644 { 4645 struct hda_gen_spec *spec = codec->spec; 4646 mutex_lock(&spec->pcm_mutex); 4647 spec->active_streams &= ~(1 << STREAM_INDEP_HP); 4648 call_pcm_playback_hook(hinfo, codec, substream, 4649 HDA_GEN_PCM_ACT_CLOSE); 4650 mutex_unlock(&spec->pcm_mutex); 4651 return 0; 4652 } 4653 4654 static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo, 4655 struct hda_codec *codec, 4656 unsigned int stream_tag, 4657 unsigned int format, 4658 struct snd_pcm_substream *substream) 4659 { 4660 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format); 4661 call_pcm_playback_hook(hinfo, codec, substream, 4662 HDA_GEN_PCM_ACT_PREPARE); 4663 return 0; 4664 } 4665 4666 static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo, 4667 struct hda_codec *codec, 4668 struct snd_pcm_substream *substream) 4669 { 4670 snd_hda_codec_cleanup_stream(codec, hinfo->nid); 4671 call_pcm_playback_hook(hinfo, codec, substream, 4672 HDA_GEN_PCM_ACT_CLEANUP); 4673 return 0; 4674 } 4675 4676 /* 4677 * Digital out 4678 */ 4679 static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo, 4680 struct hda_codec *codec, 4681 struct snd_pcm_substream *substream) 4682 { 4683 struct hda_gen_spec *spec = codec->spec; 4684 return snd_hda_multi_out_dig_open(codec, &spec->multiout); 4685 } 4686 4687 static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo, 4688 struct hda_codec *codec, 4689 unsigned int stream_tag, 4690 unsigned int format, 4691 struct snd_pcm_substream *substream) 4692 { 4693 struct hda_gen_spec *spec = codec->spec; 4694 return snd_hda_multi_out_dig_prepare(codec, &spec->multiout, 4695 stream_tag, format, substream); 4696 } 4697 4698 static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo, 4699 struct hda_codec *codec, 4700 struct snd_pcm_substream *substream) 4701 { 4702 struct hda_gen_spec *spec = codec->spec; 4703 return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout); 4704 } 4705 4706 static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo, 4707 struct hda_codec *codec, 4708 struct snd_pcm_substream *substream) 4709 { 4710 struct hda_gen_spec *spec = codec->spec; 4711 return snd_hda_multi_out_dig_close(codec, &spec->multiout); 4712 } 4713 4714 /* 4715 * Analog capture 4716 */ 4717 #define alt_capture_pcm_open capture_pcm_open 4718 #define alt_capture_pcm_close capture_pcm_close 4719 4720 static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo, 4721 struct hda_codec *codec, 4722 unsigned int stream_tag, 4723 unsigned int format, 4724 struct snd_pcm_substream *substream) 4725 { 4726 struct hda_gen_spec *spec = codec->spec; 4727 4728 snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1], 4729 stream_tag, 0, format); 4730 call_pcm_capture_hook(hinfo, codec, substream, 4731 HDA_GEN_PCM_ACT_PREPARE); 4732 return 0; 4733 } 4734 4735 static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo, 4736 struct hda_codec *codec, 4737 struct snd_pcm_substream *substream) 4738 { 4739 struct hda_gen_spec *spec = codec->spec; 4740 4741 snd_hda_codec_cleanup_stream(codec, 4742 spec->adc_nids[substream->number + 1]); 4743 call_pcm_capture_hook(hinfo, codec, substream, 4744 HDA_GEN_PCM_ACT_CLEANUP); 4745 return 0; 4746 } 4747 4748 /* 4749 */ 4750 static const struct hda_pcm_stream pcm_analog_playback = { 4751 .substreams = 1, 4752 .channels_min = 2, 4753 .channels_max = 8, 4754 /* NID is set in build_pcms */ 4755 .ops = { 4756 .open = playback_pcm_open, 4757 .close = playback_pcm_close, 4758 .prepare = playback_pcm_prepare, 4759 .cleanup = playback_pcm_cleanup 4760 }, 4761 }; 4762 4763 static const struct hda_pcm_stream pcm_analog_capture = { 4764 .substreams = 1, 4765 .channels_min = 2, 4766 .channels_max = 2, 4767 /* NID is set in build_pcms */ 4768 .ops = { 4769 .open = capture_pcm_open, 4770 .close = capture_pcm_close, 4771 .prepare = capture_pcm_prepare, 4772 .cleanup = capture_pcm_cleanup 4773 }, 4774 }; 4775 4776 static const struct hda_pcm_stream pcm_analog_alt_playback = { 4777 .substreams = 1, 4778 .channels_min = 2, 4779 .channels_max = 2, 4780 /* NID is set in build_pcms */ 4781 .ops = { 4782 .open = alt_playback_pcm_open, 4783 .close = alt_playback_pcm_close, 4784 .prepare = alt_playback_pcm_prepare, 4785 .cleanup = alt_playback_pcm_cleanup 4786 }, 4787 }; 4788 4789 static const struct hda_pcm_stream pcm_analog_alt_capture = { 4790 .substreams = 2, /* can be overridden */ 4791 .channels_min = 2, 4792 .channels_max = 2, 4793 /* NID is set in build_pcms */ 4794 .ops = { 4795 .open = alt_capture_pcm_open, 4796 .close = alt_capture_pcm_close, 4797 .prepare = alt_capture_pcm_prepare, 4798 .cleanup = alt_capture_pcm_cleanup 4799 }, 4800 }; 4801 4802 static const struct hda_pcm_stream pcm_digital_playback = { 4803 .substreams = 1, 4804 .channels_min = 2, 4805 .channels_max = 2, 4806 /* NID is set in build_pcms */ 4807 .ops = { 4808 .open = dig_playback_pcm_open, 4809 .close = dig_playback_pcm_close, 4810 .prepare = dig_playback_pcm_prepare, 4811 .cleanup = dig_playback_pcm_cleanup 4812 }, 4813 }; 4814 4815 static const struct hda_pcm_stream pcm_digital_capture = { 4816 .substreams = 1, 4817 .channels_min = 2, 4818 .channels_max = 2, 4819 /* NID is set in build_pcms */ 4820 }; 4821 4822 /* Used by build_pcms to flag that a PCM has no playback stream */ 4823 static const struct hda_pcm_stream pcm_null_stream = { 4824 .substreams = 0, 4825 .channels_min = 0, 4826 .channels_max = 0, 4827 }; 4828 4829 /* 4830 * dynamic changing ADC PCM streams 4831 */ 4832 static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur) 4833 { 4834 struct hda_gen_spec *spec = codec->spec; 4835 hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]]; 4836 4837 if (spec->cur_adc && spec->cur_adc != new_adc) { 4838 /* stream is running, let's swap the current ADC */ 4839 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1); 4840 spec->cur_adc = new_adc; 4841 snd_hda_codec_setup_stream(codec, new_adc, 4842 spec->cur_adc_stream_tag, 0, 4843 spec->cur_adc_format); 4844 return true; 4845 } 4846 return false; 4847 } 4848 4849 /* analog capture with dynamic dual-adc changes */ 4850 static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo, 4851 struct hda_codec *codec, 4852 unsigned int stream_tag, 4853 unsigned int format, 4854 struct snd_pcm_substream *substream) 4855 { 4856 struct hda_gen_spec *spec = codec->spec; 4857 spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]]; 4858 spec->cur_adc_stream_tag = stream_tag; 4859 spec->cur_adc_format = format; 4860 snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format); 4861 return 0; 4862 } 4863 4864 static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo, 4865 struct hda_codec *codec, 4866 struct snd_pcm_substream *substream) 4867 { 4868 struct hda_gen_spec *spec = codec->spec; 4869 snd_hda_codec_cleanup_stream(codec, spec->cur_adc); 4870 spec->cur_adc = 0; 4871 return 0; 4872 } 4873 4874 static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = { 4875 .substreams = 1, 4876 .channels_min = 2, 4877 .channels_max = 2, 4878 .nid = 0, /* fill later */ 4879 .ops = { 4880 .prepare = dyn_adc_capture_pcm_prepare, 4881 .cleanup = dyn_adc_capture_pcm_cleanup 4882 }, 4883 }; 4884 4885 static void fill_pcm_stream_name(char *str, size_t len, const char *sfx, 4886 const char *chip_name) 4887 { 4888 char *p; 4889 4890 if (*str) 4891 return; 4892 strlcpy(str, chip_name, len); 4893 4894 /* drop non-alnum chars after a space */ 4895 for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) { 4896 if (!isalnum(p[1])) { 4897 *p = 0; 4898 break; 4899 } 4900 } 4901 strlcat(str, sfx, len); 4902 } 4903 4904 /* build PCM streams based on the parsed results */ 4905 int snd_hda_gen_build_pcms(struct hda_codec *codec) 4906 { 4907 struct hda_gen_spec *spec = codec->spec; 4908 struct hda_pcm *info = spec->pcm_rec; 4909 const struct hda_pcm_stream *p; 4910 bool have_multi_adcs; 4911 4912 codec->num_pcms = 1; 4913 codec->pcm_info = info; 4914 4915 if (spec->no_analog) 4916 goto skip_analog; 4917 4918 fill_pcm_stream_name(spec->stream_name_analog, 4919 sizeof(spec->stream_name_analog), 4920 " Analog", codec->chip_name); 4921 info->name = spec->stream_name_analog; 4922 4923 if (spec->multiout.num_dacs > 0) { 4924 p = spec->stream_analog_playback; 4925 if (!p) 4926 p = &pcm_analog_playback; 4927 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p; 4928 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0]; 4929 info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max = 4930 spec->multiout.max_channels; 4931 if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT && 4932 spec->autocfg.line_outs == 2) 4933 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap = 4934 snd_pcm_2_1_chmaps; 4935 } 4936 if (spec->num_adc_nids) { 4937 p = spec->stream_analog_capture; 4938 if (!p) { 4939 if (spec->dyn_adc_switch) 4940 p = &dyn_adc_pcm_analog_capture; 4941 else 4942 p = &pcm_analog_capture; 4943 } 4944 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p; 4945 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0]; 4946 } 4947 4948 skip_analog: 4949 /* SPDIF for stream index #1 */ 4950 if (spec->multiout.dig_out_nid || spec->dig_in_nid) { 4951 fill_pcm_stream_name(spec->stream_name_digital, 4952 sizeof(spec->stream_name_digital), 4953 " Digital", codec->chip_name); 4954 codec->num_pcms = 2; 4955 codec->slave_dig_outs = spec->multiout.slave_dig_outs; 4956 info = spec->pcm_rec + 1; 4957 info->name = spec->stream_name_digital; 4958 if (spec->dig_out_type) 4959 info->pcm_type = spec->dig_out_type; 4960 else 4961 info->pcm_type = HDA_PCM_TYPE_SPDIF; 4962 if (spec->multiout.dig_out_nid) { 4963 p = spec->stream_digital_playback; 4964 if (!p) 4965 p = &pcm_digital_playback; 4966 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p; 4967 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid; 4968 } 4969 if (spec->dig_in_nid) { 4970 p = spec->stream_digital_capture; 4971 if (!p) 4972 p = &pcm_digital_capture; 4973 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p; 4974 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid; 4975 } 4976 } 4977 4978 if (spec->no_analog) 4979 return 0; 4980 4981 /* If the use of more than one ADC is requested for the current 4982 * model, configure a second analog capture-only PCM. 4983 */ 4984 have_multi_adcs = (spec->num_adc_nids > 1) && 4985 !spec->dyn_adc_switch && !spec->auto_mic; 4986 /* Additional Analaog capture for index #2 */ 4987 if (spec->alt_dac_nid || have_multi_adcs) { 4988 fill_pcm_stream_name(spec->stream_name_alt_analog, 4989 sizeof(spec->stream_name_alt_analog), 4990 " Alt Analog", codec->chip_name); 4991 codec->num_pcms = 3; 4992 info = spec->pcm_rec + 2; 4993 info->name = spec->stream_name_alt_analog; 4994 if (spec->alt_dac_nid) { 4995 p = spec->stream_analog_alt_playback; 4996 if (!p) 4997 p = &pcm_analog_alt_playback; 4998 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p; 4999 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 5000 spec->alt_dac_nid; 5001 } else { 5002 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = 5003 pcm_null_stream; 5004 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0; 5005 } 5006 if (have_multi_adcs) { 5007 p = spec->stream_analog_alt_capture; 5008 if (!p) 5009 p = &pcm_analog_alt_capture; 5010 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p; 5011 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 5012 spec->adc_nids[1]; 5013 info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = 5014 spec->num_adc_nids - 1; 5015 } else { 5016 info->stream[SNDRV_PCM_STREAM_CAPTURE] = 5017 pcm_null_stream; 5018 info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0; 5019 } 5020 } 5021 5022 return 0; 5023 } 5024 EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms); 5025 5026 5027 /* 5028 * Standard auto-parser initializations 5029 */ 5030 5031 /* configure the given path as a proper output */ 5032 static void set_output_and_unmute(struct hda_codec *codec, int path_idx) 5033 { 5034 struct nid_path *path; 5035 hda_nid_t pin; 5036 5037 path = snd_hda_get_path_from_idx(codec, path_idx); 5038 if (!path || !path->depth) 5039 return; 5040 pin = path->path[path->depth - 1]; 5041 restore_pin_ctl(codec, pin); 5042 snd_hda_activate_path(codec, path, path->active, 5043 aamix_default(codec->spec)); 5044 set_pin_eapd(codec, pin, path->active); 5045 } 5046 5047 /* initialize primary output paths */ 5048 static void init_multi_out(struct hda_codec *codec) 5049 { 5050 struct hda_gen_spec *spec = codec->spec; 5051 int i; 5052 5053 for (i = 0; i < spec->autocfg.line_outs; i++) 5054 set_output_and_unmute(codec, spec->out_paths[i]); 5055 } 5056 5057 5058 static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths) 5059 { 5060 int i; 5061 5062 for (i = 0; i < num_outs; i++) 5063 set_output_and_unmute(codec, paths[i]); 5064 } 5065 5066 /* initialize hp and speaker paths */ 5067 static void init_extra_out(struct hda_codec *codec) 5068 { 5069 struct hda_gen_spec *spec = codec->spec; 5070 5071 if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT) 5072 __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths); 5073 if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT) 5074 __init_extra_out(codec, spec->autocfg.speaker_outs, 5075 spec->speaker_paths); 5076 } 5077 5078 /* initialize multi-io paths */ 5079 static void init_multi_io(struct hda_codec *codec) 5080 { 5081 struct hda_gen_spec *spec = codec->spec; 5082 int i; 5083 5084 for (i = 0; i < spec->multi_ios; i++) { 5085 hda_nid_t pin = spec->multi_io[i].pin; 5086 struct nid_path *path; 5087 path = get_multiio_path(codec, i); 5088 if (!path) 5089 continue; 5090 if (!spec->multi_io[i].ctl_in) 5091 spec->multi_io[i].ctl_in = 5092 snd_hda_codec_get_pin_target(codec, pin); 5093 snd_hda_activate_path(codec, path, path->active, 5094 aamix_default(spec)); 5095 } 5096 } 5097 5098 /* set up input pins and loopback paths */ 5099 static void init_analog_input(struct hda_codec *codec) 5100 { 5101 struct hda_gen_spec *spec = codec->spec; 5102 struct auto_pin_cfg *cfg = &spec->autocfg; 5103 int i; 5104 5105 for (i = 0; i < cfg->num_inputs; i++) { 5106 hda_nid_t nid = cfg->inputs[i].pin; 5107 if (is_input_pin(codec, nid)) 5108 restore_pin_ctl(codec, nid); 5109 5110 /* init loopback inputs */ 5111 if (spec->mixer_nid) { 5112 resume_path_from_idx(codec, spec->loopback_paths[i]); 5113 resume_path_from_idx(codec, spec->loopback_merge_path); 5114 } 5115 } 5116 } 5117 5118 /* initialize ADC paths */ 5119 static void init_input_src(struct hda_codec *codec) 5120 { 5121 struct hda_gen_spec *spec = codec->spec; 5122 struct hda_input_mux *imux = &spec->input_mux; 5123 struct nid_path *path; 5124 int i, c, nums; 5125 5126 if (spec->dyn_adc_switch) 5127 nums = 1; 5128 else 5129 nums = spec->num_adc_nids; 5130 5131 for (c = 0; c < nums; c++) { 5132 for (i = 0; i < imux->num_items; i++) { 5133 path = get_input_path(codec, c, i); 5134 if (path) { 5135 bool active = path->active; 5136 if (i == spec->cur_mux[c]) 5137 active = true; 5138 snd_hda_activate_path(codec, path, active, false); 5139 } 5140 } 5141 if (spec->hp_mic) 5142 update_hp_mic(codec, c, true); 5143 } 5144 5145 if (spec->cap_sync_hook) 5146 spec->cap_sync_hook(codec, NULL); 5147 } 5148 5149 /* set right pin controls for digital I/O */ 5150 static void init_digital(struct hda_codec *codec) 5151 { 5152 struct hda_gen_spec *spec = codec->spec; 5153 int i; 5154 hda_nid_t pin; 5155 5156 for (i = 0; i < spec->autocfg.dig_outs; i++) 5157 set_output_and_unmute(codec, spec->digout_paths[i]); 5158 pin = spec->autocfg.dig_in_pin; 5159 if (pin) { 5160 restore_pin_ctl(codec, pin); 5161 resume_path_from_idx(codec, spec->digin_path); 5162 } 5163 } 5164 5165 /* clear unsol-event tags on unused pins; Conexant codecs seem to leave 5166 * invalid unsol tags by some reason 5167 */ 5168 static void clear_unsol_on_unused_pins(struct hda_codec *codec) 5169 { 5170 int i; 5171 5172 for (i = 0; i < codec->init_pins.used; i++) { 5173 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i); 5174 hda_nid_t nid = pin->nid; 5175 if (is_jack_detectable(codec, nid) && 5176 !snd_hda_jack_tbl_get(codec, nid)) 5177 snd_hda_codec_update_cache(codec, nid, 0, 5178 AC_VERB_SET_UNSOLICITED_ENABLE, 0); 5179 } 5180 } 5181 5182 /* 5183 * initialize the generic spec; 5184 * this can be put as patch_ops.init function 5185 */ 5186 int snd_hda_gen_init(struct hda_codec *codec) 5187 { 5188 struct hda_gen_spec *spec = codec->spec; 5189 5190 if (spec->init_hook) 5191 spec->init_hook(codec); 5192 5193 snd_hda_apply_verbs(codec); 5194 5195 codec->cached_write = 1; 5196 5197 init_multi_out(codec); 5198 init_extra_out(codec); 5199 init_multi_io(codec); 5200 init_analog_input(codec); 5201 init_input_src(codec); 5202 init_digital(codec); 5203 5204 clear_unsol_on_unused_pins(codec); 5205 5206 /* call init functions of standard auto-mute helpers */ 5207 update_automute_all(codec); 5208 5209 snd_hda_codec_flush_cache(codec); 5210 5211 if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook) 5212 snd_hda_sync_vmaster_hook(&spec->vmaster_mute); 5213 5214 hda_call_check_power_status(codec, 0x01); 5215 return 0; 5216 } 5217 EXPORT_SYMBOL_HDA(snd_hda_gen_init); 5218 5219 /* 5220 * free the generic spec; 5221 * this can be put as patch_ops.free function 5222 */ 5223 void snd_hda_gen_free(struct hda_codec *codec) 5224 { 5225 snd_hda_detach_beep_device(codec); 5226 snd_hda_gen_spec_free(codec->spec); 5227 kfree(codec->spec); 5228 codec->spec = NULL; 5229 } 5230 EXPORT_SYMBOL_HDA(snd_hda_gen_free); 5231 5232 #ifdef CONFIG_PM 5233 /* 5234 * check the loopback power save state; 5235 * this can be put as patch_ops.check_power_status function 5236 */ 5237 int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid) 5238 { 5239 struct hda_gen_spec *spec = codec->spec; 5240 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid); 5241 } 5242 EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status); 5243 #endif 5244 5245 5246 /* 5247 * the generic codec support 5248 */ 5249 5250 static const struct hda_codec_ops generic_patch_ops = { 5251 .build_controls = snd_hda_gen_build_controls, 5252 .build_pcms = snd_hda_gen_build_pcms, 5253 .init = snd_hda_gen_init, 5254 .free = snd_hda_gen_free, 5255 .unsol_event = snd_hda_jack_unsol_event, 5256 #ifdef CONFIG_PM 5257 .check_power_status = snd_hda_gen_check_power_status, 5258 #endif 5259 }; 5260 5261 int snd_hda_parse_generic_codec(struct hda_codec *codec) 5262 { 5263 struct hda_gen_spec *spec; 5264 int err; 5265 5266 spec = kzalloc(sizeof(*spec), GFP_KERNEL); 5267 if (!spec) 5268 return -ENOMEM; 5269 snd_hda_gen_spec_init(spec); 5270 codec->spec = spec; 5271 5272 err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0); 5273 if (err < 0) 5274 return err; 5275 5276 err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg); 5277 if (err < 0) 5278 goto error; 5279 5280 codec->patch_ops = generic_patch_ops; 5281 return 0; 5282 5283 error: 5284 snd_hda_gen_free(codec); 5285 return err; 5286 } 5287 EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec); 5288