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