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