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