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