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