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