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