linux/sound/pci/hda/patch_via.c
<<
>>
Prefs
   1/*
   2 * Universal Interface for Intel High Definition Audio Codec
   3 *
   4 * HD audio interface patch for VIA VT17xx/VT18xx/VT20xx codec
   5 *
   6 *  (C) 2006-2009 VIA Technology, Inc.
   7 *  (C) 2006-2008 Takashi Iwai <tiwai@suse.de>
   8 *
   9 *  This driver is free software; you can redistribute it and/or modify
  10 *  it under the terms of the GNU General Public License as published by
  11 *  the Free Software Foundation; either version 2 of the License, or
  12 *  (at your option) any later version.
  13 *
  14 *  This driver is distributed in the hope that it will be useful,
  15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17 *  GNU General Public License for more details.
  18 *
  19 *  You should have received a copy of the GNU General Public License
  20 *  along with this program; if not, write to the Free Software
  21 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  22 */
  23
  24/* * * * * * * * * * * * * * Release History * * * * * * * * * * * * * * * * */
  25/*                                                                           */
  26/* 2006-03-03  Lydia Wang  Create the basic patch to support VT1708 codec    */
  27/* 2006-03-14  Lydia Wang  Modify hard code for some pin widget nid          */
  28/* 2006-08-02  Lydia Wang  Add support to VT1709 codec                       */
  29/* 2006-09-08  Lydia Wang  Fix internal loopback recording source select bug */
  30/* 2007-09-12  Lydia Wang  Add EAPD enable during driver initialization      */
  31/* 2007-09-17  Lydia Wang  Add VT1708B codec support                        */
  32/* 2007-11-14  Lydia Wang  Add VT1708A codec HP and CD pin connect config    */
  33/* 2008-02-03  Lydia Wang  Fix Rear channels and Back channels inverse issue */
  34/* 2008-03-06  Lydia Wang  Add VT1702 codec and VT1708S codec support        */
  35/* 2008-04-09  Lydia Wang  Add mute front speaker when HP plugin             */
  36/* 2008-04-09  Lydia Wang  Add Independent HP feature                        */
  37/* 2008-05-28  Lydia Wang  Add second S/PDIF Out support for VT1702          */
  38/* 2008-09-15  Logan Li    Add VT1708S Mic Boost workaround/backdoor         */
  39/* 2009-02-16  Logan Li    Add support for VT1718S                           */
  40/* 2009-03-13  Logan Li    Add support for VT1716S                           */
  41/* 2009-04-14  Lydai Wang  Add support for VT1828S and VT2020                */
  42/* 2009-07-08  Lydia Wang  Add support for VT2002P                           */
  43/* 2009-07-21  Lydia Wang  Add support for VT1812                            */
  44/* 2009-09-19  Lydia Wang  Add support for VT1818S                           */
  45/*                                                                           */
  46/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  47
  48
  49#include <linux/init.h>
  50#include <linux/delay.h>
  51#include <linux/slab.h>
  52#include <linux/module.h>
  53#include <sound/core.h>
  54#include <sound/asoundef.h>
  55#include "hda_codec.h"
  56#include "hda_local.h"
  57
  58/* Pin Widget NID */
  59#define VT1708_HP_PIN_NID       0x20
  60#define VT1708_CD_PIN_NID       0x24
  61
  62enum VIA_HDA_CODEC {
  63        UNKNOWN = -1,
  64        VT1708,
  65        VT1709_10CH,
  66        VT1709_6CH,
  67        VT1708B_8CH,
  68        VT1708B_4CH,
  69        VT1708S,
  70        VT1708BCE,
  71        VT1702,
  72        VT1718S,
  73        VT1716S,
  74        VT2002P,
  75        VT1812,
  76        VT1802,
  77        CODEC_TYPES,
  78};
  79
  80#define VT2002P_COMPATIBLE(spec) \
  81        ((spec)->codec_type == VT2002P ||\
  82         (spec)->codec_type == VT1812 ||\
  83         (spec)->codec_type == VT1802)
  84
  85#define MAX_NID_PATH_DEPTH      5
  86
  87/* output-path: DAC -> ... -> pin
  88 * idx[] contains the source index number of the next widget;
  89 * e.g. idx[0] is the index of the DAC selected by path[1] widget
  90 * multi[] indicates whether it's a selector widget with multi-connectors
  91 * (i.e. the connection selection is mandatory)
  92 * vol_ctl and mute_ctl contains the NIDs for the assigned mixers
  93 */
  94struct nid_path {
  95        int depth;
  96        hda_nid_t path[MAX_NID_PATH_DEPTH];
  97        unsigned char idx[MAX_NID_PATH_DEPTH];
  98        unsigned char multi[MAX_NID_PATH_DEPTH];
  99        unsigned int vol_ctl;
 100        unsigned int mute_ctl;
 101};
 102
 103/* input-path */
 104struct via_input {
 105        hda_nid_t pin;  /* input-pin or aa-mix */
 106        int adc_idx;    /* ADC index to be used */
 107        int mux_idx;    /* MUX index (if any) */
 108        const char *label;      /* input-source label */
 109};
 110
 111#define VIA_MAX_ADCS    3
 112
 113enum {
 114        STREAM_MULTI_OUT = (1 << 0),
 115        STREAM_INDEP_HP = (1 << 1),
 116};
 117
 118struct via_spec {
 119        /* codec parameterization */
 120        const struct snd_kcontrol_new *mixers[6];
 121        unsigned int num_mixers;
 122
 123        const struct hda_verb *init_verbs[5];
 124        unsigned int num_iverbs;
 125
 126        char stream_name_analog[32];
 127        char stream_name_hp[32];
 128        const struct hda_pcm_stream *stream_analog_playback;
 129        const struct hda_pcm_stream *stream_analog_capture;
 130
 131        char stream_name_digital[32];
 132        const struct hda_pcm_stream *stream_digital_playback;
 133        const struct hda_pcm_stream *stream_digital_capture;
 134
 135        /* playback */
 136        struct hda_multi_out multiout;
 137        hda_nid_t slave_dig_outs[2];
 138        hda_nid_t hp_dac_nid;
 139        hda_nid_t speaker_dac_nid;
 140        int hp_indep_shared;    /* indep HP-DAC is shared with side ch */
 141        int opened_streams;     /* STREAM_* bits */
 142        int active_streams;     /* STREAM_* bits */
 143        int aamix_mode;         /* loopback is enabled for output-path? */
 144
 145        /* Output-paths:
 146         * There are different output-paths depending on the setup.
 147         * out_path, hp_path and speaker_path are primary paths.  If both
 148         * direct DAC and aa-loopback routes are available, these contain
 149         * the former paths.  Meanwhile *_mix_path contain the paths with
 150         * loopback mixer.  (Since the loopback is only for front channel,
 151         * no out_mix_path for surround channels.)
 152         * The HP output has another path, hp_indep_path, which is used in
 153         * the independent-HP mode.
 154         */
 155        struct nid_path out_path[HDA_SIDE + 1];
 156        struct nid_path out_mix_path;
 157        struct nid_path hp_path;
 158        struct nid_path hp_mix_path;
 159        struct nid_path hp_indep_path;
 160        struct nid_path speaker_path;
 161        struct nid_path speaker_mix_path;
 162
 163        /* capture */
 164        unsigned int num_adc_nids;
 165        hda_nid_t adc_nids[VIA_MAX_ADCS];
 166        hda_nid_t mux_nids[VIA_MAX_ADCS];
 167        hda_nid_t aa_mix_nid;
 168        hda_nid_t dig_in_nid;
 169
 170        /* capture source */
 171        bool dyn_adc_switch;
 172        int num_inputs;
 173        struct via_input inputs[AUTO_CFG_MAX_INS + 1];
 174        unsigned int cur_mux[VIA_MAX_ADCS];
 175
 176        /* dynamic DAC switching */
 177        unsigned int cur_dac_stream_tag;
 178        unsigned int cur_dac_format;
 179        unsigned int cur_hp_stream_tag;
 180        unsigned int cur_hp_format;
 181
 182        /* dynamic ADC switching */
 183        hda_nid_t cur_adc;
 184        unsigned int cur_adc_stream_tag;
 185        unsigned int cur_adc_format;
 186
 187        /* PCM information */
 188        struct hda_pcm pcm_rec[3];
 189
 190        /* dynamic controls, init_verbs and input_mux */
 191        struct auto_pin_cfg autocfg;
 192        struct snd_array kctls;
 193        hda_nid_t private_dac_nids[AUTO_CFG_MAX_OUTS];
 194
 195        /* HP mode source */
 196        unsigned int hp_independent_mode;
 197        unsigned int dmic_enabled;
 198        unsigned int no_pin_power_ctl;
 199        enum VIA_HDA_CODEC codec_type;
 200
 201        /* smart51 setup */
 202        unsigned int smart51_nums;
 203        hda_nid_t smart51_pins[2];
 204        int smart51_idxs[2];
 205        const char *smart51_labels[2];
 206        unsigned int smart51_enabled;
 207
 208        /* work to check hp jack state */
 209        struct hda_codec *codec;
 210        struct delayed_work vt1708_hp_work;
 211        int hp_work_active;
 212        int vt1708_jack_detect;
 213        int vt1708_hp_present;
 214
 215        void (*set_widgets_power_state)(struct hda_codec *codec);
 216
 217        struct hda_loopback_check loopback;
 218        int num_loopbacks;
 219        struct hda_amp_list loopback_list[8];
 220
 221        /* bind capture-volume */
 222        struct hda_bind_ctls *bind_cap_vol;
 223        struct hda_bind_ctls *bind_cap_sw;
 224
 225        struct mutex config_mutex;
 226};
 227
 228static enum VIA_HDA_CODEC get_codec_type(struct hda_codec *codec);
 229static struct via_spec * via_new_spec(struct hda_codec *codec)
 230{
 231        struct via_spec *spec;
 232
 233        spec = kzalloc(sizeof(*spec), GFP_KERNEL);
 234        if (spec == NULL)
 235                return NULL;
 236
 237        mutex_init(&spec->config_mutex);
 238        codec->spec = spec;
 239        spec->codec = codec;
 240        spec->codec_type = get_codec_type(codec);
 241        /* VT1708BCE & VT1708S are almost same */
 242        if (spec->codec_type == VT1708BCE)
 243                spec->codec_type = VT1708S;
 244        return spec;
 245}
 246
 247static enum VIA_HDA_CODEC get_codec_type(struct hda_codec *codec)
 248{
 249        u32 vendor_id = codec->vendor_id;
 250        u16 ven_id = vendor_id >> 16;
 251        u16 dev_id = vendor_id & 0xffff;
 252        enum VIA_HDA_CODEC codec_type;
 253
 254        /* get codec type */
 255        if (ven_id != 0x1106)
 256                codec_type = UNKNOWN;
 257        else if (dev_id >= 0x1708 && dev_id <= 0x170b)
 258                codec_type = VT1708;
 259        else if (dev_id >= 0xe710 && dev_id <= 0xe713)
 260                codec_type = VT1709_10CH;
 261        else if (dev_id >= 0xe714 && dev_id <= 0xe717)
 262                codec_type = VT1709_6CH;
 263        else if (dev_id >= 0xe720 && dev_id <= 0xe723) {
 264                codec_type = VT1708B_8CH;
 265                if (snd_hda_param_read(codec, 0x16, AC_PAR_CONNLIST_LEN) == 0x7)
 266                        codec_type = VT1708BCE;
 267        } else if (dev_id >= 0xe724 && dev_id <= 0xe727)
 268                codec_type = VT1708B_4CH;
 269        else if ((dev_id & 0xfff) == 0x397
 270                 && (dev_id >> 12) < 8)
 271                codec_type = VT1708S;
 272        else if ((dev_id & 0xfff) == 0x398
 273                 && (dev_id >> 12) < 8)
 274                codec_type = VT1702;
 275        else if ((dev_id & 0xfff) == 0x428
 276                 && (dev_id >> 12) < 8)
 277                codec_type = VT1718S;
 278        else if (dev_id == 0x0433 || dev_id == 0xa721)
 279                codec_type = VT1716S;
 280        else if (dev_id == 0x0441 || dev_id == 0x4441)
 281                codec_type = VT1718S;
 282        else if (dev_id == 0x0438 || dev_id == 0x4438)
 283                codec_type = VT2002P;
 284        else if (dev_id == 0x0448)
 285                codec_type = VT1812;
 286        else if (dev_id == 0x0440)
 287                codec_type = VT1708S;
 288        else if ((dev_id & 0xfff) == 0x446)
 289                codec_type = VT1802;
 290        else
 291                codec_type = UNKNOWN;
 292        return codec_type;
 293};
 294
 295#define VIA_JACK_EVENT          0x20
 296#define VIA_HP_EVENT            0x01
 297#define VIA_GPIO_EVENT          0x02
 298#define VIA_LINE_EVENT          0x03
 299
 300enum {
 301        VIA_CTL_WIDGET_VOL,
 302        VIA_CTL_WIDGET_MUTE,
 303        VIA_CTL_WIDGET_ANALOG_MUTE,
 304};
 305
 306static void analog_low_current_mode(struct hda_codec *codec);
 307static bool is_aa_path_mute(struct hda_codec *codec);
 308
 309#define hp_detect_with_aa(codec) \
 310        (snd_hda_get_bool_hint(codec, "analog_loopback_hp_detect") == 1 && \
 311         !is_aa_path_mute(codec))
 312
 313static void vt1708_stop_hp_work(struct via_spec *spec)
 314{
 315        if (spec->codec_type != VT1708 || spec->autocfg.hp_pins[0] == 0)
 316                return;
 317        if (spec->hp_work_active) {
 318                snd_hda_codec_write(spec->codec, 0x1, 0, 0xf81, 1);
 319                cancel_delayed_work_sync(&spec->vt1708_hp_work);
 320                spec->hp_work_active = 0;
 321        }
 322}
 323
 324static void vt1708_update_hp_work(struct via_spec *spec)
 325{
 326        if (spec->codec_type != VT1708 || spec->autocfg.hp_pins[0] == 0)
 327                return;
 328        if (spec->vt1708_jack_detect &&
 329            (spec->active_streams || hp_detect_with_aa(spec->codec))) {
 330                if (!spec->hp_work_active) {
 331                        snd_hda_codec_write(spec->codec, 0x1, 0, 0xf81, 0);
 332                        schedule_delayed_work(&spec->vt1708_hp_work,
 333                                              msecs_to_jiffies(100));
 334                        spec->hp_work_active = 1;
 335                }
 336        } else if (!hp_detect_with_aa(spec->codec))
 337                vt1708_stop_hp_work(spec);
 338}
 339
 340static void set_widgets_power_state(struct hda_codec *codec)
 341{
 342        struct via_spec *spec = codec->spec;
 343        if (spec->set_widgets_power_state)
 344                spec->set_widgets_power_state(codec);
 345}
 346
 347static int analog_input_switch_put(struct snd_kcontrol *kcontrol,
 348                                   struct snd_ctl_elem_value *ucontrol)
 349{
 350        int change = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
 351        struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
 352
 353        set_widgets_power_state(codec);
 354        analog_low_current_mode(snd_kcontrol_chip(kcontrol));
 355        vt1708_update_hp_work(codec->spec);
 356        return change;
 357}
 358
 359/* modify .put = snd_hda_mixer_amp_switch_put */
 360#define ANALOG_INPUT_MUTE                                               \
 361        {               .iface = SNDRV_CTL_ELEM_IFACE_MIXER,            \
 362                        .name = NULL,                                   \
 363                        .index = 0,                                     \
 364                        .info = snd_hda_mixer_amp_switch_info,          \
 365                        .get = snd_hda_mixer_amp_switch_get,            \
 366                        .put = analog_input_switch_put,                 \
 367                        .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0) }
 368
 369static const struct snd_kcontrol_new via_control_templates[] = {
 370        HDA_CODEC_VOLUME(NULL, 0, 0, 0),
 371        HDA_CODEC_MUTE(NULL, 0, 0, 0),
 372        ANALOG_INPUT_MUTE,
 373};
 374
 375
 376/* add dynamic controls */
 377static struct snd_kcontrol_new *__via_clone_ctl(struct via_spec *spec,
 378                                const struct snd_kcontrol_new *tmpl,
 379                                const char *name)
 380{
 381        struct snd_kcontrol_new *knew;
 382
 383        snd_array_init(&spec->kctls, sizeof(*knew), 32);
 384        knew = snd_array_new(&spec->kctls);
 385        if (!knew)
 386                return NULL;
 387        *knew = *tmpl;
 388        if (!name)
 389                name = tmpl->name;
 390        if (name) {
 391                knew->name = kstrdup(name, GFP_KERNEL);
 392                if (!knew->name)
 393                        return NULL;
 394        }
 395        return knew;
 396}
 397
 398static int __via_add_control(struct via_spec *spec, int type, const char *name,
 399                             int idx, unsigned long val)
 400{
 401        struct snd_kcontrol_new *knew;
 402
 403        knew = __via_clone_ctl(spec, &via_control_templates[type], name);
 404        if (!knew)
 405                return -ENOMEM;
 406        knew->index = idx;
 407        if (get_amp_nid_(val))
 408                knew->subdevice = HDA_SUBDEV_AMP_FLAG;
 409        knew->private_value = val;
 410        return 0;
 411}
 412
 413#define via_add_control(spec, type, name, val) \
 414        __via_add_control(spec, type, name, 0, val)
 415
 416#define via_clone_control(spec, tmpl) __via_clone_ctl(spec, tmpl, NULL)
 417
 418static void via_free_kctls(struct hda_codec *codec)
 419{
 420        struct via_spec *spec = codec->spec;
 421
 422        if (spec->kctls.list) {
 423                struct snd_kcontrol_new *kctl = spec->kctls.list;
 424                int i;
 425                for (i = 0; i < spec->kctls.used; i++)
 426                        kfree(kctl[i].name);
 427        }
 428        snd_array_free(&spec->kctls);
 429}
 430
 431/* create input playback/capture controls for the given pin */
 432static int via_new_analog_input(struct via_spec *spec, const char *ctlname,
 433                                int type_idx, int idx, int mix_nid)
 434{
 435        char name[32];
 436        int err;
 437
 438        sprintf(name, "%s Playback Volume", ctlname);
 439        err = __via_add_control(spec, VIA_CTL_WIDGET_VOL, name, type_idx,
 440                              HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
 441        if (err < 0)
 442                return err;
 443        sprintf(name, "%s Playback Switch", ctlname);
 444        err = __via_add_control(spec, VIA_CTL_WIDGET_ANALOG_MUTE, name, type_idx,
 445                              HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
 446        if (err < 0)
 447                return err;
 448        return 0;
 449}
 450
 451#define get_connection_index(codec, mux, nid) \
 452        snd_hda_get_conn_index(codec, mux, nid, 0)
 453
 454static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
 455                           unsigned int mask)
 456{
 457        unsigned int caps;
 458        if (!nid)
 459                return false;
 460        caps = get_wcaps(codec, nid);
 461        if (dir == HDA_INPUT)
 462                caps &= AC_WCAP_IN_AMP;
 463        else
 464                caps &= AC_WCAP_OUT_AMP;
 465        if (!caps)
 466                return false;
 467        if (query_amp_caps(codec, nid, dir) & mask)
 468                return true;
 469        return false;
 470}
 471
 472#define have_mute(codec, nid, dir) \
 473        check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
 474
 475/* enable/disable the output-route mixers */
 476static void activate_output_mix(struct hda_codec *codec, struct nid_path *path,
 477                                hda_nid_t mix_nid, int idx, bool enable)
 478{
 479        int i, num, val;
 480
 481        if (!path)
 482                return;
 483        num = snd_hda_get_conn_list(codec, mix_nid, NULL);
 484        for (i = 0; i < num; i++) {
 485                if (i == idx)
 486                        val = AMP_IN_UNMUTE(i);
 487                else
 488                        val = AMP_IN_MUTE(i);
 489                snd_hda_codec_write(codec, mix_nid, 0,
 490                                    AC_VERB_SET_AMP_GAIN_MUTE, val);
 491        }
 492}
 493
 494/* enable/disable the output-route */
 495static void activate_output_path(struct hda_codec *codec, struct nid_path *path,
 496                                 bool enable, bool force)
 497{
 498        struct via_spec *spec = codec->spec;
 499        int i;
 500        for (i = 0; i < path->depth; i++) {
 501                hda_nid_t src, dst;
 502                int idx = path->idx[i];
 503                src = path->path[i];                    
 504                if (i < path->depth - 1)
 505                        dst = path->path[i + 1];
 506                else
 507                        dst = 0;
 508                if (enable && path->multi[i])
 509                        snd_hda_codec_write(codec, dst, 0,
 510                                            AC_VERB_SET_CONNECT_SEL, idx);
 511                if (!force && (dst == spec->aa_mix_nid))
 512                        continue;
 513                if (have_mute(codec, dst, HDA_INPUT))
 514                        activate_output_mix(codec, path, dst, idx, enable);
 515                if (!force && (src == path->vol_ctl || src == path->mute_ctl))
 516                        continue;
 517                if (have_mute(codec, src, HDA_OUTPUT)) {
 518                        int val = enable ? AMP_OUT_UNMUTE : AMP_OUT_MUTE;
 519                        snd_hda_codec_write(codec, src, 0,
 520                                            AC_VERB_SET_AMP_GAIN_MUTE, val);
 521                }
 522        }
 523}
 524
 525/* set the given pin as output */
 526static void init_output_pin(struct hda_codec *codec, hda_nid_t pin,
 527                            int pin_type)
 528{
 529        if (!pin)
 530                return;
 531        snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
 532                            pin_type);
 533        if (snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD)
 534                snd_hda_codec_write(codec, pin, 0,
 535                                    AC_VERB_SET_EAPD_BTLENABLE, 0x02);
 536}
 537
 538static void via_auto_init_output(struct hda_codec *codec,
 539                                 struct nid_path *path, int pin_type)
 540{
 541        unsigned int caps;
 542        hda_nid_t pin;
 543
 544        if (!path->depth)
 545                return;
 546        pin = path->path[path->depth - 1];
 547
 548        init_output_pin(codec, pin, pin_type);
 549        caps = query_amp_caps(codec, pin, HDA_OUTPUT);
 550        if (caps & AC_AMPCAP_MUTE) {
 551                unsigned int val;
 552                val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
 553                snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
 554                                    AMP_OUT_MUTE | val);
 555        }
 556        activate_output_path(codec, path, true, true); /* force on */
 557}
 558
 559static void via_auto_init_multi_out(struct hda_codec *codec)
 560{
 561        struct via_spec *spec = codec->spec;
 562        struct nid_path *path;
 563        int i;
 564
 565        for (i = 0; i < spec->autocfg.line_outs + spec->smart51_nums; i++) {
 566                path = &spec->out_path[i];
 567                if (!i && spec->aamix_mode && spec->out_mix_path.depth)
 568                        path = &spec->out_mix_path;
 569                via_auto_init_output(codec, path, PIN_OUT);
 570        }
 571}
 572
 573/* deactivate the inactive headphone-paths */
 574static void deactivate_hp_paths(struct hda_codec *codec)
 575{
 576        struct via_spec *spec = codec->spec;
 577        int shared = spec->hp_indep_shared;
 578
 579        if (spec->hp_independent_mode) {
 580                activate_output_path(codec, &spec->hp_path, false, false);
 581                activate_output_path(codec, &spec->hp_mix_path, false, false);
 582                if (shared)
 583                        activate_output_path(codec, &spec->out_path[shared],
 584                                             false, false);
 585        } else if (spec->aamix_mode || !spec->hp_path.depth) {
 586                activate_output_path(codec, &spec->hp_indep_path, false, false);
 587                activate_output_path(codec, &spec->hp_path, false, false);
 588        } else {
 589                activate_output_path(codec, &spec->hp_indep_path, false, false);
 590                activate_output_path(codec, &spec->hp_mix_path, false, false);
 591        }
 592}
 593
 594static void via_auto_init_hp_out(struct hda_codec *codec)
 595{
 596        struct via_spec *spec = codec->spec;
 597
 598        if (!spec->hp_path.depth) {
 599                via_auto_init_output(codec, &spec->hp_mix_path, PIN_HP);
 600                return;
 601        }
 602        deactivate_hp_paths(codec);
 603        if (spec->hp_independent_mode)
 604                via_auto_init_output(codec, &spec->hp_indep_path, PIN_HP);
 605        else if (spec->aamix_mode)
 606                via_auto_init_output(codec, &spec->hp_mix_path, PIN_HP);
 607        else
 608                via_auto_init_output(codec, &spec->hp_path, PIN_HP);
 609}
 610
 611static void via_auto_init_speaker_out(struct hda_codec *codec)
 612{
 613        struct via_spec *spec = codec->spec;
 614
 615        if (!spec->autocfg.speaker_outs)
 616                return;
 617        if (!spec->speaker_path.depth) {
 618                via_auto_init_output(codec, &spec->speaker_mix_path, PIN_OUT);
 619                return;
 620        }
 621        if (!spec->aamix_mode) {
 622                activate_output_path(codec, &spec->speaker_mix_path,
 623                                     false, false);
 624                via_auto_init_output(codec, &spec->speaker_path, PIN_OUT);
 625        } else {
 626                activate_output_path(codec, &spec->speaker_path, false, false);
 627                via_auto_init_output(codec, &spec->speaker_mix_path, PIN_OUT);
 628        }
 629}
 630
 631static bool is_smart51_pins(struct hda_codec *codec, hda_nid_t pin);
 632static void via_hp_automute(struct hda_codec *codec);
 633
 634static void via_auto_init_analog_input(struct hda_codec *codec)
 635{
 636        struct via_spec *spec = codec->spec;
 637        const struct auto_pin_cfg *cfg = &spec->autocfg;
 638        hda_nid_t conn[HDA_MAX_CONNECTIONS];
 639        unsigned int ctl;
 640        int i, num_conns;
 641
 642        /* init ADCs */
 643        for (i = 0; i < spec->num_adc_nids; i++) {
 644                snd_hda_codec_write(codec, spec->adc_nids[i], 0,
 645                                    AC_VERB_SET_AMP_GAIN_MUTE,
 646                                    AMP_IN_UNMUTE(0));
 647        }
 648
 649        /* init pins */
 650        for (i = 0; i < cfg->num_inputs; i++) {
 651                hda_nid_t nid = cfg->inputs[i].pin;
 652                if (spec->smart51_enabled && is_smart51_pins(codec, nid))
 653                        ctl = PIN_OUT;
 654                else if (cfg->inputs[i].type == AUTO_PIN_MIC)
 655                        ctl = PIN_VREF50;
 656                else
 657                        ctl = PIN_IN;
 658                snd_hda_codec_write(codec, nid, 0,
 659                                    AC_VERB_SET_PIN_WIDGET_CONTROL, ctl);
 660        }
 661
 662        /* init input-src */
 663        for (i = 0; i < spec->num_adc_nids; i++) {
 664                int adc_idx = spec->inputs[spec->cur_mux[i]].adc_idx;
 665                if (spec->mux_nids[adc_idx]) {
 666                        int mux_idx = spec->inputs[spec->cur_mux[i]].mux_idx;
 667                        snd_hda_codec_write(codec, spec->mux_nids[adc_idx], 0,
 668                                            AC_VERB_SET_CONNECT_SEL,
 669                                            mux_idx);
 670                }
 671                if (spec->dyn_adc_switch)
 672                        break; /* only one input-src */
 673        }
 674
 675        /* init aa-mixer */
 676        if (!spec->aa_mix_nid)
 677                return;
 678        num_conns = snd_hda_get_connections(codec, spec->aa_mix_nid, conn,
 679                                            ARRAY_SIZE(conn));
 680        for (i = 0; i < num_conns; i++) {
 681                unsigned int caps = get_wcaps(codec, conn[i]);
 682                if (get_wcaps_type(caps) == AC_WID_PIN)
 683                        snd_hda_codec_write(codec, spec->aa_mix_nid, 0,
 684                                            AC_VERB_SET_AMP_GAIN_MUTE,
 685                                            AMP_IN_MUTE(i));
 686        }
 687}
 688
 689static void set_pin_power_state(struct hda_codec *codec, hda_nid_t nid,
 690                                unsigned int *affected_parm)
 691{
 692        unsigned parm;
 693        unsigned def_conf = snd_hda_codec_get_pincfg(codec, nid);
 694        unsigned no_presence = (def_conf & AC_DEFCFG_MISC)
 695                >> AC_DEFCFG_MISC_SHIFT
 696                & AC_DEFCFG_MISC_NO_PRESENCE; /* do not support pin sense */
 697        struct via_spec *spec = codec->spec;
 698        unsigned present = 0;
 699
 700        no_presence |= spec->no_pin_power_ctl;
 701        if (!no_presence)
 702                present = snd_hda_jack_detect(codec, nid);
 703        if ((spec->smart51_enabled && is_smart51_pins(codec, nid))
 704            || ((no_presence || present)
 705                && get_defcfg_connect(def_conf) != AC_JACK_PORT_NONE)) {
 706                *affected_parm = AC_PWRST_D0; /* if it's connected */
 707                parm = AC_PWRST_D0;
 708        } else
 709                parm = AC_PWRST_D3;
 710
 711        snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE, parm);
 712}
 713
 714static int via_pin_power_ctl_info(struct snd_kcontrol *kcontrol,
 715                                  struct snd_ctl_elem_info *uinfo)
 716{
 717        static const char * const texts[] = {
 718                "Disabled", "Enabled"
 719        };
 720
 721        uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
 722        uinfo->count = 1;
 723        uinfo->value.enumerated.items = 2;
 724        if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
 725                uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
 726        strcpy(uinfo->value.enumerated.name,
 727               texts[uinfo->value.enumerated.item]);
 728        return 0;
 729}
 730
 731static int via_pin_power_ctl_get(struct snd_kcontrol *kcontrol,
 732                                 struct snd_ctl_elem_value *ucontrol)
 733{
 734        struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
 735        struct via_spec *spec = codec->spec;
 736        ucontrol->value.enumerated.item[0] = !spec->no_pin_power_ctl;
 737        return 0;
 738}
 739
 740static int via_pin_power_ctl_put(struct snd_kcontrol *kcontrol,
 741                                 struct snd_ctl_elem_value *ucontrol)
 742{
 743        struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
 744        struct via_spec *spec = codec->spec;
 745        unsigned int val = !ucontrol->value.enumerated.item[0];
 746
 747        if (val == spec->no_pin_power_ctl)
 748                return 0;
 749        spec->no_pin_power_ctl = val;
 750        set_widgets_power_state(codec);
 751        return 1;
 752}
 753
 754static const struct snd_kcontrol_new via_pin_power_ctl_enum = {
 755        .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
 756        .name = "Dynamic Power-Control",
 757        .info = via_pin_power_ctl_info,
 758        .get = via_pin_power_ctl_get,
 759        .put = via_pin_power_ctl_put,
 760};
 761
 762
 763static int via_independent_hp_info(struct snd_kcontrol *kcontrol,
 764                                   struct snd_ctl_elem_info *uinfo)
 765{
 766        static const char * const texts[] = { "OFF", "ON" };
 767
 768        uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
 769        uinfo->count = 1;
 770        uinfo->value.enumerated.items = 2;
 771        if (uinfo->value.enumerated.item >= 2)
 772                uinfo->value.enumerated.item = 1;
 773        strcpy(uinfo->value.enumerated.name,
 774               texts[uinfo->value.enumerated.item]);
 775        return 0;
 776}
 777
 778static int via_independent_hp_get(struct snd_kcontrol *kcontrol,
 779                                  struct snd_ctl_elem_value *ucontrol)
 780{
 781        struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
 782        struct via_spec *spec = codec->spec;
 783
 784        ucontrol->value.enumerated.item[0] = spec->hp_independent_mode;
 785        return 0;
 786}
 787
 788/* adjust spec->multiout setup according to the current flags */
 789static void setup_playback_multi_pcm(struct via_spec *spec)
 790{
 791        const struct auto_pin_cfg *cfg = &spec->autocfg;
 792        spec->multiout.num_dacs = cfg->line_outs + spec->smart51_nums;
 793        spec->multiout.hp_nid = 0;
 794        if (!spec->hp_independent_mode) {
 795                if (!spec->hp_indep_shared)
 796                        spec->multiout.hp_nid = spec->hp_dac_nid;
 797        } else {
 798                if (spec->hp_indep_shared)
 799                        spec->multiout.num_dacs = cfg->line_outs - 1;
 800        }
 801}
 802
 803/* update DAC setups according to indep-HP switch;
 804 * this function is called only when indep-HP is modified
 805 */
 806static void switch_indep_hp_dacs(struct hda_codec *codec)
 807{
 808        struct via_spec *spec = codec->spec;
 809        int shared = spec->hp_indep_shared;
 810        hda_nid_t shared_dac, hp_dac;
 811
 812        if (!spec->opened_streams)
 813                return;
 814
 815        shared_dac = shared ? spec->multiout.dac_nids[shared] : 0;
 816        hp_dac = spec->hp_dac_nid;
 817        if (spec->hp_independent_mode) {
 818                /* switch to indep-HP mode */
 819                if (spec->active_streams & STREAM_MULTI_OUT) {
 820                        __snd_hda_codec_cleanup_stream(codec, hp_dac, 1);
 821                        __snd_hda_codec_cleanup_stream(codec, shared_dac, 1);
 822                }
 823                if (spec->active_streams & STREAM_INDEP_HP)
 824                        snd_hda_codec_setup_stream(codec, hp_dac,
 825                                                   spec->cur_hp_stream_tag, 0,
 826                                                   spec->cur_hp_format);
 827        } else {
 828                /* back to HP or shared-DAC */
 829                if (spec->active_streams & STREAM_INDEP_HP)
 830                        __snd_hda_codec_cleanup_stream(codec, hp_dac, 1);
 831                if (spec->active_streams & STREAM_MULTI_OUT) {
 832                        hda_nid_t dac;
 833                        int ch;
 834                        if (shared_dac) { /* reset mutli-ch DAC */
 835                                dac = shared_dac;
 836                                ch = shared * 2;
 837                        } else { /* reset HP DAC */
 838                                dac = hp_dac;
 839                                ch = 0;
 840                        }
 841                        snd_hda_codec_setup_stream(codec, dac,
 842                                                   spec->cur_dac_stream_tag, ch,
 843                                                   spec->cur_dac_format);
 844                }
 845        }
 846        setup_playback_multi_pcm(spec);
 847}
 848
 849static int via_independent_hp_put(struct snd_kcontrol *kcontrol,
 850                                  struct snd_ctl_elem_value *ucontrol)
 851{
 852        struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
 853        struct via_spec *spec = codec->spec;
 854        int cur, shared;
 855
 856        mutex_lock(&spec->config_mutex);
 857        cur = !!ucontrol->value.enumerated.item[0];
 858        if (spec->hp_independent_mode == cur) {
 859                mutex_unlock(&spec->config_mutex);
 860                return 0;
 861        }
 862        spec->hp_independent_mode = cur;
 863        shared = spec->hp_indep_shared;
 864        deactivate_hp_paths(codec);
 865        if (cur)
 866                activate_output_path(codec, &spec->hp_indep_path, true, false);
 867        else {
 868                if (shared)
 869                        activate_output_path(codec, &spec->out_path[shared],
 870                                             true, false);
 871                if (spec->aamix_mode || !spec->hp_path.depth)
 872                        activate_output_path(codec, &spec->hp_mix_path,
 873                                             true, false);
 874                else
 875                        activate_output_path(codec, &spec->hp_path,
 876                                             true, false);
 877        }
 878
 879        switch_indep_hp_dacs(codec);
 880        mutex_unlock(&spec->config_mutex);
 881
 882        /* update jack power state */
 883        set_widgets_power_state(codec);
 884        via_hp_automute(codec);
 885        return 1;
 886}
 887
 888static const struct snd_kcontrol_new via_hp_mixer = {
 889        .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
 890        .name = "Independent HP",
 891        .info = via_independent_hp_info,
 892        .get = via_independent_hp_get,
 893        .put = via_independent_hp_put,
 894};
 895
 896static int via_hp_build(struct hda_codec *codec)
 897{
 898        struct via_spec *spec = codec->spec;
 899        struct snd_kcontrol_new *knew;
 900        hda_nid_t nid;
 901
 902        nid = spec->autocfg.hp_pins[0];
 903        knew = via_clone_control(spec, &via_hp_mixer);
 904        if (knew == NULL)
 905                return -ENOMEM;
 906
 907        knew->subdevice = HDA_SUBDEV_NID_FLAG | nid;
 908
 909        return 0;
 910}
 911
 912static void notify_aa_path_ctls(struct hda_codec *codec)
 913{
 914        struct via_spec *spec = codec->spec;
 915        int i;
 916
 917        for (i = 0; i < spec->smart51_nums; i++) {
 918                struct snd_kcontrol *ctl;
 919                struct snd_ctl_elem_id id;
 920                memset(&id, 0, sizeof(id));
 921                id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
 922                sprintf(id.name, "%s Playback Volume", spec->smart51_labels[i]);
 923                ctl = snd_hda_find_mixer_ctl(codec, id.name);
 924                if (ctl)
 925                        snd_ctl_notify(codec->bus->card,
 926                                        SNDRV_CTL_EVENT_MASK_VALUE,
 927                                        &ctl->id);
 928        }
 929}
 930
 931static void mute_aa_path(struct hda_codec *codec, int mute)
 932{
 933        struct via_spec *spec = codec->spec;
 934        int val = mute ? HDA_AMP_MUTE : HDA_AMP_UNMUTE;
 935        int i;
 936
 937        /* check AA path's mute status */
 938        for (i = 0; i < spec->smart51_nums; i++) {
 939                if (spec->smart51_idxs[i] < 0)
 940                        continue;
 941                snd_hda_codec_amp_stereo(codec, spec->aa_mix_nid,
 942                                         HDA_INPUT, spec->smart51_idxs[i],
 943                                         HDA_AMP_MUTE, val);
 944        }
 945}
 946
 947static bool is_smart51_pins(struct hda_codec *codec, hda_nid_t pin)
 948{
 949        struct via_spec *spec = codec->spec;
 950        int i;
 951
 952        for (i = 0; i < spec->smart51_nums; i++)
 953                if (spec->smart51_pins[i] == pin)
 954                        return true;
 955        return false;
 956}
 957
 958static int via_smart51_get(struct snd_kcontrol *kcontrol,
 959                           struct snd_ctl_elem_value *ucontrol)
 960{
 961        struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
 962        struct via_spec *spec = codec->spec;
 963
 964        *ucontrol->value.integer.value = spec->smart51_enabled;
 965        return 0;
 966}
 967
 968static int via_smart51_put(struct snd_kcontrol *kcontrol,
 969                           struct snd_ctl_elem_value *ucontrol)
 970{
 971        struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
 972        struct via_spec *spec = codec->spec;
 973        int out_in = *ucontrol->value.integer.value
 974                ? AC_PINCTL_OUT_EN : AC_PINCTL_IN_EN;
 975        int i;
 976
 977        for (i = 0; i < spec->smart51_nums; i++) {
 978                hda_nid_t nid = spec->smart51_pins[i];
 979                unsigned int parm;
 980
 981                parm = snd_hda_codec_read(codec, nid, 0,
 982                                          AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
 983                parm &= ~(AC_PINCTL_IN_EN | AC_PINCTL_OUT_EN);
 984                parm |= out_in;
 985                snd_hda_codec_write(codec, nid, 0,
 986                                    AC_VERB_SET_PIN_WIDGET_CONTROL,
 987                                    parm);
 988                if (out_in == AC_PINCTL_OUT_EN) {
 989                        mute_aa_path(codec, 1);
 990                        notify_aa_path_ctls(codec);
 991                }
 992        }
 993        spec->smart51_enabled = *ucontrol->value.integer.value;
 994        set_widgets_power_state(codec);
 995        return 1;
 996}
 997
 998static const struct snd_kcontrol_new via_smart51_mixer = {
 999        .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1000        .name = "Smart 5.1",
1001        .count = 1,
1002        .info = snd_ctl_boolean_mono_info,
1003        .get = via_smart51_get,
1004        .put = via_smart51_put,
1005};
1006
1007static int via_smart51_build(struct hda_codec *codec)
1008{
1009        struct via_spec *spec = codec->spec;
1010
1011        if (!spec->smart51_nums)
1012                return 0;
1013        if (!via_clone_control(spec, &via_smart51_mixer))
1014                return -ENOMEM;
1015        return 0;
1016}
1017
1018/* check AA path's mute status */
1019static bool is_aa_path_mute(struct hda_codec *codec)
1020{
1021        struct via_spec *spec = codec->spec;
1022        const struct hda_amp_list *p;
1023        int i, ch, v;
1024
1025        for (i = 0; i < spec->num_loopbacks; i++) {
1026                p = &spec->loopback_list[i];
1027                for (ch = 0; ch < 2; ch++) {
1028                        v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
1029                                                   p->idx);
1030                        if (!(v & HDA_AMP_MUTE) && v > 0)
1031                                return false;
1032                }
1033        }
1034        return true;
1035}
1036
1037/* enter/exit analog low-current mode */
1038static void analog_low_current_mode(struct hda_codec *codec)
1039{
1040        struct via_spec *spec = codec->spec;
1041        bool enable;
1042        unsigned int verb, parm;
1043
1044        enable = is_aa_path_mute(codec) && (spec->opened_streams != 0);
1045
1046        /* decide low current mode's verb & parameter */
1047        switch (spec->codec_type) {
1048        case VT1708B_8CH:
1049        case VT1708B_4CH:
1050                verb = 0xf70;
1051                parm = enable ? 0x02 : 0x00; /* 0x02: 2/3x, 0x00: 1x */
1052                break;
1053        case VT1708S:
1054        case VT1718S:
1055        case VT1716S:
1056                verb = 0xf73;
1057                parm = enable ? 0x51 : 0xe1; /* 0x51: 4/28x, 0xe1: 1x */
1058                break;
1059        case VT1702:
1060                verb = 0xf73;
1061                parm = enable ? 0x01 : 0x1d; /* 0x01: 4/40x, 0x1d: 1x */
1062                break;
1063        case VT2002P:
1064        case VT1812:
1065        case VT1802:
1066                verb = 0xf93;
1067                parm = enable ? 0x00 : 0xe0; /* 0x00: 4/40x, 0xe0: 1x */
1068                break;
1069        default:
1070                return;         /* other codecs are not supported */
1071        }
1072        /* send verb */
1073        snd_hda_codec_write(codec, codec->afg, 0, verb, parm);
1074}
1075
1076/*
1077 * generic initialization of ADC, input mixers and output mixers
1078 */
1079static const struct hda_verb vt1708_init_verbs[] = {
1080        /* power down jack detect function */
1081        {0x1, 0xf81, 0x1},
1082        { }
1083};
1084
1085static void set_stream_open(struct hda_codec *codec, int bit, bool active)
1086{
1087        struct via_spec *spec = codec->spec;
1088
1089        if (active)
1090                spec->opened_streams |= bit;
1091        else
1092                spec->opened_streams &= ~bit;
1093        analog_low_current_mode(codec);
1094}
1095
1096static int via_playback_multi_pcm_open(struct hda_pcm_stream *hinfo,
1097                                 struct hda_codec *codec,
1098                                 struct snd_pcm_substream *substream)
1099{
1100        struct via_spec *spec = codec->spec;
1101        const struct auto_pin_cfg *cfg = &spec->autocfg;
1102        int err;
1103
1104        spec->multiout.num_dacs = cfg->line_outs + spec->smart51_nums;
1105        spec->multiout.max_channels = spec->multiout.num_dacs * 2;
1106        set_stream_open(codec, STREAM_MULTI_OUT, true);
1107        err = snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
1108                                            hinfo);
1109        if (err < 0) {
1110                set_stream_open(codec, STREAM_MULTI_OUT, false);
1111                return err;
1112        }
1113        return 0;
1114}
1115
1116static int via_playback_multi_pcm_close(struct hda_pcm_stream *hinfo,
1117                                  struct hda_codec *codec,
1118                                  struct snd_pcm_substream *substream)
1119{
1120        set_stream_open(codec, STREAM_MULTI_OUT, false);
1121        return 0;
1122}
1123
1124static int via_playback_hp_pcm_open(struct hda_pcm_stream *hinfo,
1125                                    struct hda_codec *codec,
1126                                    struct snd_pcm_substream *substream)
1127{
1128        struct via_spec *spec = codec->spec;
1129
1130        if (snd_BUG_ON(!spec->hp_dac_nid))
1131                return -EINVAL;
1132        set_stream_open(codec, STREAM_INDEP_HP, true);
1133        return 0;
1134}
1135
1136static int via_playback_hp_pcm_close(struct hda_pcm_stream *hinfo,
1137                                     struct hda_codec *codec,
1138                                     struct snd_pcm_substream *substream)
1139{
1140        set_stream_open(codec, STREAM_INDEP_HP, false);
1141        return 0;
1142}
1143
1144static int via_playback_multi_pcm_prepare(struct hda_pcm_stream *hinfo,
1145                                          struct hda_codec *codec,
1146                                          unsigned int stream_tag,
1147                                          unsigned int format,
1148                                          struct snd_pcm_substream *substream)
1149{
1150        struct via_spec *spec = codec->spec;
1151
1152        mutex_lock(&spec->config_mutex);
1153        setup_playback_multi_pcm(spec);
1154        snd_hda_multi_out_analog_prepare(codec, &spec->multiout, stream_tag,
1155                                         format, substream);
1156        /* remember for dynamic DAC switch with indep-HP */
1157        spec->active_streams |= STREAM_MULTI_OUT;
1158        spec->cur_dac_stream_tag = stream_tag;
1159        spec->cur_dac_format = format;
1160        mutex_unlock(&spec->config_mutex);
1161        vt1708_update_hp_work(spec);
1162        return 0;
1163}
1164
1165static int via_playback_hp_pcm_prepare(struct hda_pcm_stream *hinfo,
1166                                       struct hda_codec *codec,
1167                                       unsigned int stream_tag,
1168                                       unsigned int format,
1169                                       struct snd_pcm_substream *substream)
1170{
1171        struct via_spec *spec = codec->spec;
1172
1173        mutex_lock(&spec->config_mutex);
1174        if (spec->hp_independent_mode)
1175                snd_hda_codec_setup_stream(codec, spec->hp_dac_nid,
1176                                           stream_tag, 0, format);
1177        spec->active_streams |= STREAM_INDEP_HP;
1178        spec->cur_hp_stream_tag = stream_tag;
1179        spec->cur_hp_format = format;
1180        mutex_unlock(&spec->config_mutex);
1181        vt1708_update_hp_work(spec);
1182        return 0;
1183}
1184
1185static int via_playback_multi_pcm_cleanup(struct hda_pcm_stream *hinfo,
1186                                    struct hda_codec *codec,
1187                                    struct snd_pcm_substream *substream)
1188{
1189        struct via_spec *spec = codec->spec;
1190
1191        mutex_lock(&spec->config_mutex);
1192        snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
1193        spec->active_streams &= ~STREAM_MULTI_OUT;
1194        mutex_unlock(&spec->config_mutex);
1195        vt1708_update_hp_work(spec);
1196        return 0;
1197}
1198
1199static int via_playback_hp_pcm_cleanup(struct hda_pcm_stream *hinfo,
1200                                       struct hda_codec *codec,
1201                                       struct snd_pcm_substream *substream)
1202{
1203        struct via_spec *spec = codec->spec;
1204
1205        mutex_lock(&spec->config_mutex);
1206        if (spec->hp_independent_mode)
1207                snd_hda_codec_setup_stream(codec, spec->hp_dac_nid, 0, 0, 0);
1208        spec->active_streams &= ~STREAM_INDEP_HP;
1209        mutex_unlock(&spec->config_mutex);
1210        vt1708_update_hp_work(spec);
1211        return 0;
1212}
1213
1214/*
1215 * Digital out
1216 */
1217static int via_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
1218                                     struct hda_codec *codec,
1219                                     struct snd_pcm_substream *substream)
1220{
1221        struct via_spec *spec = codec->spec;
1222        return snd_hda_multi_out_dig_open(codec, &spec->multiout);
1223}
1224
1225static int via_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
1226                                      struct hda_codec *codec,
1227                                      struct snd_pcm_substream *substream)
1228{
1229        struct via_spec *spec = codec->spec;
1230        return snd_hda_multi_out_dig_close(codec, &spec->multiout);
1231}
1232
1233static int via_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
1234                                        struct hda_codec *codec,
1235                                        unsigned int stream_tag,
1236                                        unsigned int format,
1237                                        struct snd_pcm_substream *substream)
1238{
1239        struct via_spec *spec = codec->spec;
1240        return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
1241                                             stream_tag, format, substream);
1242}
1243
1244static int via_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
1245                                        struct hda_codec *codec,
1246                                        struct snd_pcm_substream *substream)
1247{
1248        struct via_spec *spec = codec->spec;
1249        snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
1250        return 0;
1251}
1252
1253/*
1254 * Analog capture
1255 */
1256static int via_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
1257                                   struct hda_codec *codec,
1258                                   unsigned int stream_tag,
1259                                   unsigned int format,
1260                                   struct snd_pcm_substream *substream)
1261{
1262        struct via_spec *spec = codec->spec;
1263
1264        snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
1265                                   stream_tag, 0, format);
1266        return 0;
1267}
1268
1269static int via_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
1270                                   struct hda_codec *codec,
1271                                   struct snd_pcm_substream *substream)
1272{
1273        struct via_spec *spec = codec->spec;
1274        snd_hda_codec_cleanup_stream(codec, spec->adc_nids[substream->number]);
1275        return 0;
1276}
1277
1278/* analog capture with dynamic ADC switching */
1279static int via_dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
1280                                           struct hda_codec *codec,
1281                                           unsigned int stream_tag,
1282                                           unsigned int format,
1283                                           struct snd_pcm_substream *substream)
1284{
1285        struct via_spec *spec = codec->spec;
1286        int adc_idx = spec->inputs[spec->cur_mux[0]].adc_idx;
1287
1288        mutex_lock(&spec->config_mutex);
1289        spec->cur_adc = spec->adc_nids[adc_idx];
1290        spec->cur_adc_stream_tag = stream_tag;
1291        spec->cur_adc_format = format;
1292        snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
1293        mutex_unlock(&spec->config_mutex);
1294        return 0;
1295}
1296
1297static int via_dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
1298                                           struct hda_codec *codec,
1299                                           struct snd_pcm_substream *substream)
1300{
1301        struct via_spec *spec = codec->spec;
1302
1303        mutex_lock(&spec->config_mutex);
1304        snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
1305        spec->cur_adc = 0;
1306        mutex_unlock(&spec->config_mutex);
1307        return 0;
1308}
1309
1310/* re-setup the stream if running; called from input-src put */
1311static bool via_dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
1312{
1313        struct via_spec *spec = codec->spec;
1314        int adc_idx = spec->inputs[cur].adc_idx;
1315        hda_nid_t adc = spec->adc_nids[adc_idx];
1316        bool ret = false;
1317
1318        mutex_lock(&spec->config_mutex);
1319        if (spec->cur_adc && spec->cur_adc != adc) {
1320                /* stream is running, let's swap the current ADC */
1321                __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
1322                spec->cur_adc = adc;
1323                snd_hda_codec_setup_stream(codec, adc,
1324                                           spec->cur_adc_stream_tag, 0,
1325                                           spec->cur_adc_format);
1326                ret = true;
1327        }
1328        mutex_unlock(&spec->config_mutex);
1329        return ret;
1330}
1331
1332static const struct hda_pcm_stream via_pcm_analog_playback = {
1333        .substreams = 1,
1334        .channels_min = 2,
1335        .channels_max = 8,
1336        /* NID is set in via_build_pcms */
1337        .ops = {
1338                .open = via_playback_multi_pcm_open,
1339                .close = via_playback_multi_pcm_close,
1340                .prepare = via_playback_multi_pcm_prepare,
1341                .cleanup = via_playback_multi_pcm_cleanup
1342        },
1343};
1344
1345static const struct hda_pcm_stream via_pcm_hp_playback = {
1346        .substreams = 1,
1347        .channels_min = 2,
1348        .channels_max = 2,
1349        /* NID is set in via_build_pcms */
1350        .ops = {
1351                .open = via_playback_hp_pcm_open,
1352                .close = via_playback_hp_pcm_close,
1353                .prepare = via_playback_hp_pcm_prepare,
1354                .cleanup = via_playback_hp_pcm_cleanup
1355        },
1356};
1357
1358static const struct hda_pcm_stream vt1708_pcm_analog_s16_playback = {
1359        .substreams = 1,
1360        .channels_min = 2,
1361        .channels_max = 8,
1362        /* NID is set in via_build_pcms */
1363        /* We got noisy outputs on the right channel on VT1708 when
1364         * 24bit samples are used.  Until any workaround is found,
1365         * disable the 24bit format, so far.
1366         */
1367        .formats = SNDRV_PCM_FMTBIT_S16_LE,
1368        .ops = {
1369                .open = via_playback_multi_pcm_open,
1370                .close = via_playback_multi_pcm_close,
1371                .prepare = via_playback_multi_pcm_prepare,
1372                .cleanup = via_playback_multi_pcm_cleanup
1373        },
1374};
1375
1376static const struct hda_pcm_stream via_pcm_analog_capture = {
1377        .substreams = 1, /* will be changed in via_build_pcms() */
1378        .channels_min = 2,
1379        .channels_max = 2,
1380        /* NID is set in via_build_pcms */
1381        .ops = {
1382                .prepare = via_capture_pcm_prepare,
1383                .cleanup = via_capture_pcm_cleanup
1384        },
1385};
1386
1387static const struct hda_pcm_stream via_pcm_dyn_adc_analog_capture = {
1388        .substreams = 1,
1389        .channels_min = 2,
1390        .channels_max = 2,
1391        /* NID is set in via_build_pcms */
1392        .ops = {
1393                .prepare = via_dyn_adc_capture_pcm_prepare,
1394                .cleanup = via_dyn_adc_capture_pcm_cleanup,
1395        },
1396};
1397
1398static const struct hda_pcm_stream via_pcm_digital_playback = {
1399        .substreams = 1,
1400        .channels_min = 2,
1401        .channels_max = 2,
1402        /* NID is set in via_build_pcms */
1403        .ops = {
1404                .open = via_dig_playback_pcm_open,
1405                .close = via_dig_playback_pcm_close,
1406                .prepare = via_dig_playback_pcm_prepare,
1407                .cleanup = via_dig_playback_pcm_cleanup
1408        },
1409};
1410
1411static const struct hda_pcm_stream via_pcm_digital_capture = {
1412        .substreams = 1,
1413        .channels_min = 2,
1414        .channels_max = 2,
1415};
1416
1417/*
1418 * slave controls for virtual master
1419 */
1420static const char * const via_slave_vols[] = {
1421        "Front Playback Volume",
1422        "Surround Playback Volume",
1423        "Center Playback Volume",
1424        "LFE Playback Volume",
1425        "Side Playback Volume",
1426        "Headphone Playback Volume",
1427        "Speaker Playback Volume",
1428        NULL,
1429};
1430
1431static const char * const via_slave_sws[] = {
1432        "Front Playback Switch",
1433        "Surround Playback Switch",
1434        "Center Playback Switch",
1435        "LFE Playback Switch",
1436        "Side Playback Switch",
1437        "Headphone Playback Switch",
1438        "Speaker Playback Switch",
1439        NULL,
1440};
1441
1442static int via_build_controls(struct hda_codec *codec)
1443{
1444        struct via_spec *spec = codec->spec;
1445        struct snd_kcontrol *kctl;
1446        int err, i;
1447
1448        if (spec->set_widgets_power_state)
1449                if (!via_clone_control(spec, &via_pin_power_ctl_enum))
1450                        return -ENOMEM;
1451
1452        for (i = 0; i < spec->num_mixers; i++) {
1453                err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
1454                if (err < 0)
1455                        return err;
1456        }
1457
1458        if (spec->multiout.dig_out_nid) {
1459                err = snd_hda_create_spdif_out_ctls(codec,
1460                                                    spec->multiout.dig_out_nid,
1461                                                    spec->multiout.dig_out_nid);
1462                if (err < 0)
1463                        return err;
1464                err = snd_hda_create_spdif_share_sw(codec,
1465                                                    &spec->multiout);
1466                if (err < 0)
1467                        return err;
1468                spec->multiout.share_spdif = 1;
1469        }
1470        if (spec->dig_in_nid) {
1471                err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
1472                if (err < 0)
1473                        return err;
1474        }
1475
1476        /* if we have no master control, let's create it */
1477        if (!snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
1478                unsigned int vmaster_tlv[4];
1479                snd_hda_set_vmaster_tlv(codec, spec->multiout.dac_nids[0],
1480                                        HDA_OUTPUT, vmaster_tlv);
1481                err = snd_hda_add_vmaster(codec, "Master Playback Volume",
1482                                          vmaster_tlv, via_slave_vols);
1483                if (err < 0)
1484                        return err;
1485        }
1486        if (!snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
1487                err = snd_hda_add_vmaster(codec, "Master Playback Switch",
1488                                          NULL, via_slave_sws);
1489                if (err < 0)
1490                        return err;
1491        }
1492
1493        /* assign Capture Source enums to NID */
1494        kctl = snd_hda_find_mixer_ctl(codec, "Input Source");
1495        for (i = 0; kctl && i < kctl->count; i++) {
1496                err = snd_hda_add_nid(codec, kctl, i, spec->mux_nids[i]);
1497                if (err < 0)
1498                        return err;
1499        }
1500
1501        /* init power states */
1502        set_widgets_power_state(codec);
1503        analog_low_current_mode(codec);
1504
1505        via_free_kctls(codec); /* no longer needed */
1506        return 0;
1507}
1508
1509static int via_build_pcms(struct hda_codec *codec)
1510{
1511        struct via_spec *spec = codec->spec;
1512        struct hda_pcm *info = spec->pcm_rec;
1513
1514        codec->num_pcms = 0;
1515        codec->pcm_info = info;
1516
1517        if (spec->multiout.num_dacs || spec->num_adc_nids) {
1518                snprintf(spec->stream_name_analog,
1519                         sizeof(spec->stream_name_analog),
1520                         "%s Analog", codec->chip_name);
1521                info->name = spec->stream_name_analog;
1522
1523                if (spec->multiout.num_dacs) {
1524                        if (!spec->stream_analog_playback)
1525                                spec->stream_analog_playback =
1526                                        &via_pcm_analog_playback;
1527                        info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
1528                                *spec->stream_analog_playback;
1529                        info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
1530                                spec->multiout.dac_nids[0];
1531                        info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
1532                                spec->multiout.max_channels;
1533                }
1534
1535                if (!spec->stream_analog_capture) {
1536                        if (spec->dyn_adc_switch)
1537                                spec->stream_analog_capture =
1538                                        &via_pcm_dyn_adc_analog_capture;
1539                        else
1540                                spec->stream_analog_capture =
1541                                        &via_pcm_analog_capture;
1542                }
1543                if (spec->num_adc_nids) {
1544                        info->stream[SNDRV_PCM_STREAM_CAPTURE] =
1545                                *spec->stream_analog_capture;
1546                        info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
1547                                spec->adc_nids[0];
1548                        if (!spec->dyn_adc_switch)
1549                                info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
1550                                        spec->num_adc_nids;
1551                }
1552                codec->num_pcms++;
1553                info++;
1554        }
1555
1556        if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
1557                snprintf(spec->stream_name_digital,
1558                         sizeof(spec->stream_name_digital),
1559                         "%s Digital", codec->chip_name);
1560                info->name = spec->stream_name_digital;
1561                info->pcm_type = HDA_PCM_TYPE_SPDIF;
1562                if (spec->multiout.dig_out_nid) {
1563                        if (!spec->stream_digital_playback)
1564                                spec->stream_digital_playback =
1565                                        &via_pcm_digital_playback;
1566                        info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
1567                                *spec->stream_digital_playback;
1568                        info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
1569                                spec->multiout.dig_out_nid;
1570                }
1571                if (spec->dig_in_nid) {
1572                        if (!spec->stream_digital_capture)
1573                                spec->stream_digital_capture =
1574                                        &via_pcm_digital_capture;
1575                        info->stream[SNDRV_PCM_STREAM_CAPTURE] =
1576                                *spec->stream_digital_capture;
1577                        info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
1578                                spec->dig_in_nid;
1579                }
1580                codec->num_pcms++;
1581                info++;
1582        }
1583
1584        if (spec->hp_dac_nid) {
1585                snprintf(spec->stream_name_hp, sizeof(spec->stream_name_hp),
1586                         "%s HP", codec->chip_name);
1587                info->name = spec->stream_name_hp;
1588                info->stream[SNDRV_PCM_STREAM_PLAYBACK] = via_pcm_hp_playback;
1589                info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
1590                        spec->hp_dac_nid;
1591                codec->num_pcms++;
1592                info++;
1593        }
1594        return 0;
1595}
1596
1597static void via_free(struct hda_codec *codec)
1598{
1599        struct via_spec *spec = codec->spec;
1600
1601        if (!spec)
1602                return;
1603
1604        via_free_kctls(codec);
1605        vt1708_stop_hp_work(spec);
1606        kfree(spec->bind_cap_vol);
1607        kfree(spec->bind_cap_sw);
1608        kfree(spec);
1609}
1610
1611/* mute/unmute outputs */
1612static void toggle_output_mutes(struct hda_codec *codec, int num_pins,
1613                                hda_nid_t *pins, bool mute)
1614{
1615        int i;
1616        for (i = 0; i < num_pins; i++) {
1617                unsigned int parm = snd_hda_codec_read(codec, pins[i], 0,
1618                                          AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
1619                if (parm & AC_PINCTL_IN_EN)
1620                        continue;
1621                if (mute)
1622                        parm &= ~AC_PINCTL_OUT_EN;
1623                else
1624                        parm |= AC_PINCTL_OUT_EN;
1625                snd_hda_codec_write(codec, pins[i], 0,
1626                                    AC_VERB_SET_PIN_WIDGET_CONTROL, parm);
1627        }
1628}
1629
1630/* mute internal speaker if line-out is plugged */
1631static void via_line_automute(struct hda_codec *codec, int present)
1632{
1633        struct via_spec *spec = codec->spec;
1634
1635        if (!spec->autocfg.speaker_outs)
1636                return;
1637        if (!present)
1638                present = snd_hda_jack_detect(codec,
1639                                              spec->autocfg.line_out_pins[0]);
1640        toggle_output_mutes(codec, spec->autocfg.speaker_outs,
1641                            spec->autocfg.speaker_pins,
1642                            present);
1643}
1644
1645/* mute internal speaker if HP is plugged */
1646static void via_hp_automute(struct hda_codec *codec)
1647{
1648        int present = 0;
1649        int nums;
1650        struct via_spec *spec = codec->spec;
1651
1652        if (!spec->hp_independent_mode && spec->autocfg.hp_pins[0] &&
1653            (spec->codec_type != VT1708 || spec->vt1708_jack_detect))
1654                present = snd_hda_jack_detect(codec, spec->autocfg.hp_pins[0]);
1655
1656        if (spec->smart51_enabled)
1657                nums = spec->autocfg.line_outs + spec->smart51_nums;
1658        else
1659                nums = spec->autocfg.line_outs;
1660        toggle_output_mutes(codec, nums, spec->autocfg.line_out_pins, present);
1661
1662        via_line_automute(codec, present);
1663}
1664
1665static void via_gpio_control(struct hda_codec *codec)
1666{
1667        unsigned int gpio_data;
1668        unsigned int vol_counter;
1669        unsigned int vol;
1670        unsigned int master_vol;
1671
1672        struct via_spec *spec = codec->spec;
1673
1674        gpio_data = snd_hda_codec_read(codec, codec->afg, 0,
1675                                       AC_VERB_GET_GPIO_DATA, 0) & 0x03;
1676
1677        vol_counter = (snd_hda_codec_read(codec, codec->afg, 0,
1678                                          0xF84, 0) & 0x3F0000) >> 16;
1679
1680        vol = vol_counter & 0x1F;
1681        master_vol = snd_hda_codec_read(codec, 0x1A, 0,
1682                                        AC_VERB_GET_AMP_GAIN_MUTE,
1683                                        AC_AMP_GET_INPUT);
1684
1685        if (gpio_data == 0x02) {
1686                /* unmute line out */
1687                snd_hda_codec_write(codec, spec->autocfg.line_out_pins[0], 0,
1688                                    AC_VERB_SET_PIN_WIDGET_CONTROL,
1689                                    PIN_OUT);
1690                if (vol_counter & 0x20) {
1691                        /* decrease volume */
1692                        if (vol > master_vol)
1693                                vol = master_vol;
1694                        snd_hda_codec_amp_stereo(codec, 0x1A, HDA_INPUT,
1695                                                 0, HDA_AMP_VOLMASK,
1696                                                 master_vol-vol);
1697                } else {
1698                        /* increase volume */
1699                        snd_hda_codec_amp_stereo(codec, 0x1A, HDA_INPUT, 0,
1700                                         HDA_AMP_VOLMASK,
1701                                         ((master_vol+vol) > 0x2A) ? 0x2A :
1702                                          (master_vol+vol));
1703                }
1704        } else if (!(gpio_data & 0x02)) {
1705                /* mute line out */
1706                snd_hda_codec_write(codec, spec->autocfg.line_out_pins[0], 0,
1707                                    AC_VERB_SET_PIN_WIDGET_CONTROL,
1708                                    0);
1709        }
1710}
1711
1712/* unsolicited event for jack sensing */
1713static void via_unsol_event(struct hda_codec *codec,
1714                                  unsigned int res)
1715{
1716        res >>= 26;
1717
1718        if (res & VIA_JACK_EVENT)
1719                set_widgets_power_state(codec);
1720
1721        res &= ~VIA_JACK_EVENT;
1722
1723        if (res == VIA_HP_EVENT || res == VIA_LINE_EVENT)
1724                via_hp_automute(codec);
1725        else if (res == VIA_GPIO_EVENT)
1726                via_gpio_control(codec);
1727}
1728
1729#ifdef CONFIG_PM
1730static int via_suspend(struct hda_codec *codec, pm_message_t state)
1731{
1732        struct via_spec *spec = codec->spec;
1733        vt1708_stop_hp_work(spec);
1734        return 0;
1735}
1736#endif
1737
1738#ifdef CONFIG_SND_HDA_POWER_SAVE
1739static int via_check_power_status(struct hda_codec *codec, hda_nid_t nid)
1740{
1741        struct via_spec *spec = codec->spec;
1742        return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
1743}
1744#endif
1745
1746/*
1747 */
1748
1749static int via_init(struct hda_codec *codec);
1750
1751static const struct hda_codec_ops via_patch_ops = {
1752        .build_controls = via_build_controls,
1753        .build_pcms = via_build_pcms,
1754        .init = via_init,
1755        .free = via_free,
1756        .unsol_event = via_unsol_event,
1757#ifdef CONFIG_PM
1758        .suspend = via_suspend,
1759#endif
1760#ifdef CONFIG_SND_HDA_POWER_SAVE
1761        .check_power_status = via_check_power_status,
1762#endif
1763};
1764
1765static bool is_empty_dac(struct hda_codec *codec, hda_nid_t dac)
1766{
1767        struct via_spec *spec = codec->spec;
1768        int i;
1769
1770        for (i = 0; i < spec->multiout.num_dacs; i++) {
1771                if (spec->multiout.dac_nids[i] == dac)
1772                        return false;
1773        }
1774        if (spec->hp_dac_nid == dac)
1775                return false;
1776        return true;
1777}
1778
1779static bool __parse_output_path(struct hda_codec *codec, hda_nid_t nid,
1780                                hda_nid_t target_dac, int with_aa_mix,
1781                                struct nid_path *path, int depth)
1782{
1783        struct via_spec *spec = codec->spec;
1784        hda_nid_t conn[8];
1785        int i, nums;
1786
1787        if (nid == spec->aa_mix_nid) {
1788                if (!with_aa_mix)
1789                        return false;
1790                with_aa_mix = 2; /* mark aa-mix is included */
1791        }
1792
1793        nums = snd_hda_get_connections(codec, nid, conn, ARRAY_SIZE(conn));
1794        for (i = 0; i < nums; i++) {
1795                if (get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT)
1796                        continue;
1797                if (conn[i] == target_dac || is_empty_dac(codec, conn[i])) {
1798                        /* aa-mix is requested but not included? */
1799                        if (!(spec->aa_mix_nid && with_aa_mix == 1))
1800                                goto found;
1801                }
1802        }
1803        if (depth >= MAX_NID_PATH_DEPTH)
1804                return false;
1805        for (i = 0; i < nums; i++) {
1806                unsigned int type;
1807                type = get_wcaps_type(get_wcaps(codec, conn[i]));
1808                if (type == AC_WID_AUD_OUT)
1809                        continue;
1810                if (__parse_output_path(codec, conn[i], target_dac,
1811                                        with_aa_mix, path, depth + 1))
1812                        goto found;
1813        }
1814        return false;
1815
1816 found:
1817        path->path[path->depth] = conn[i];
1818        path->idx[path->depth] = i;
1819        if (nums > 1 && get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_MIX)
1820                path->multi[path->depth] = 1;
1821        path->depth++;
1822        return true;
1823}
1824
1825static bool parse_output_path(struct hda_codec *codec, hda_nid_t nid,
1826                              hda_nid_t target_dac, int with_aa_mix,
1827                              struct nid_path *path)
1828{
1829        if (__parse_output_path(codec, nid, target_dac, with_aa_mix, path, 1)) {
1830                path->path[path->depth] = nid;
1831                path->depth++;
1832                snd_printdd("output-path: depth=%d, %02x/%02x/%02x/%02x/%02x\n",
1833                            path->depth, path->path[0], path->path[1],
1834                            path->path[2], path->path[3], path->path[4]);
1835                return true;
1836        }
1837        return false;
1838}
1839
1840static int via_auto_fill_dac_nids(struct hda_codec *codec)
1841{
1842        struct via_spec *spec = codec->spec;
1843        const struct auto_pin_cfg *cfg = &spec->autocfg;
1844        int i, dac_num;
1845        hda_nid_t nid;
1846
1847        spec->multiout.dac_nids = spec->private_dac_nids;
1848        dac_num = 0;
1849        for (i = 0; i < cfg->line_outs; i++) {
1850                hda_nid_t dac = 0;
1851                nid = cfg->line_out_pins[i];
1852                if (!nid)
1853                        continue;
1854                if (parse_output_path(codec, nid, 0, 0, &spec->out_path[i]))
1855                        dac = spec->out_path[i].path[0];
1856                if (!i && parse_output_path(codec, nid, dac, 1,
1857                                            &spec->out_mix_path))
1858                        dac = spec->out_mix_path.path[0];
1859                if (dac) {
1860                        spec->private_dac_nids[i] = dac;
1861                        dac_num++;
1862                }
1863        }
1864        if (!spec->out_path[0].depth && spec->out_mix_path.depth) {
1865                spec->out_path[0] = spec->out_mix_path;
1866                spec->out_mix_path.depth = 0;
1867        }
1868        spec->multiout.num_dacs = dac_num;
1869        return 0;
1870}
1871
1872static int create_ch_ctls(struct hda_codec *codec, const char *pfx,
1873                          int chs, bool check_dac, struct nid_path *path)
1874{
1875        struct via_spec *spec = codec->spec;
1876        char name[32];
1877        hda_nid_t dac, pin, sel, nid;
1878        int err;
1879
1880        dac = check_dac ? path->path[0] : 0;
1881        pin = path->path[path->depth - 1];
1882        sel = path->depth > 1 ? path->path[1] : 0;
1883
1884        if (dac && check_amp_caps(codec, dac, HDA_OUTPUT, AC_AMPCAP_NUM_STEPS))
1885                nid = dac;
1886        else if (check_amp_caps(codec, pin, HDA_OUTPUT, AC_AMPCAP_NUM_STEPS))
1887                nid = pin;
1888        else if (check_amp_caps(codec, sel, HDA_OUTPUT, AC_AMPCAP_NUM_STEPS))
1889                nid = sel;
1890        else
1891                nid = 0;
1892        if (nid) {
1893                sprintf(name, "%s Playback Volume", pfx);
1894                err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
1895                              HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT));
1896                if (err < 0)
1897                        return err;
1898                path->vol_ctl = nid;
1899        }
1900
1901        if (dac && check_amp_caps(codec, dac, HDA_OUTPUT, AC_AMPCAP_MUTE))
1902                nid = dac;
1903        else if (check_amp_caps(codec, pin, HDA_OUTPUT, AC_AMPCAP_MUTE))
1904                nid = pin;
1905        else if (check_amp_caps(codec, sel, HDA_OUTPUT, AC_AMPCAP_MUTE))
1906                nid = sel;
1907        else
1908                nid = 0;
1909        if (nid) {
1910                sprintf(name, "%s Playback Switch", pfx);
1911                err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
1912                              HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT));
1913                if (err < 0)
1914                        return err;
1915                path->mute_ctl = nid;
1916        }
1917        return 0;
1918}
1919
1920static void mangle_smart51(struct hda_codec *codec)
1921{
1922        struct via_spec *spec = codec->spec;
1923        struct auto_pin_cfg *cfg = &spec->autocfg;
1924        struct auto_pin_cfg_item *ins = cfg->inputs;
1925        int i, j, nums, attr;
1926        int pins[AUTO_CFG_MAX_INS];
1927
1928        for (attr = INPUT_PIN_ATTR_REAR; attr >= INPUT_PIN_ATTR_NORMAL; attr--) {
1929                nums = 0;
1930                for (i = 0; i < cfg->num_inputs; i++) {
1931                        unsigned int def;
1932                        if (ins[i].type > AUTO_PIN_LINE_IN)
1933                                continue;
1934                        def = snd_hda_codec_get_pincfg(codec, ins[i].pin);
1935                        if (snd_hda_get_input_pin_attr(def) != attr)
1936                                continue;
1937                        for (j = 0; j < nums; j++)
1938                                if (ins[pins[j]].type < ins[i].type) {
1939                                        memmove(pins + j + 1, pins + j,
1940                                                (nums - j) * sizeof(int));
1941                                        break;
1942                                }
1943                        pins[j] = i;
1944                        nums++;
1945                }
1946                if (cfg->line_outs + nums < 3)
1947                        continue;
1948                for (i = 0; i < nums; i++) {
1949                        hda_nid_t pin = ins[pins[i]].pin;
1950                        spec->smart51_pins[spec->smart51_nums++] = pin;
1951                        cfg->line_out_pins[cfg->line_outs++] = pin;
1952                        if (cfg->line_outs == 3)
1953                                break;
1954                }
1955                return;
1956        }
1957}
1958
1959static void copy_path_mixer_ctls(struct nid_path *dst, struct nid_path *src)
1960{
1961        dst->vol_ctl = src->vol_ctl;
1962        dst->mute_ctl = src->mute_ctl;
1963}
1964
1965/* add playback controls from the parsed DAC table */
1966static int via_auto_create_multi_out_ctls(struct hda_codec *codec)
1967{
1968        struct via_spec *spec = codec->spec;
1969        struct auto_pin_cfg *cfg = &spec->autocfg;
1970        struct nid_path *path;
1971        static const char * const chname[4] = {
1972                "Front", "Surround", "C/LFE", "Side"
1973        };
1974        int i, idx, err;
1975        int old_line_outs;
1976
1977        /* check smart51 */
1978        old_line_outs = cfg->line_outs;
1979        if (cfg->line_outs == 1)
1980                mangle_smart51(codec);
1981
1982        err = via_auto_fill_dac_nids(codec);
1983        if (err < 0)
1984                return err;
1985
1986        if (spec->multiout.num_dacs < 3) {
1987                spec->smart51_nums = 0;
1988                cfg->line_outs = old_line_outs;
1989        }
1990        for (i = 0; i < cfg->line_outs; i++) {
1991                hda_nid_t pin, dac;
1992                pin = cfg->line_out_pins[i];
1993                dac = spec->multiout.dac_nids[i];
1994                if (!pin || !dac)
1995                        continue;
1996                path = spec->out_path + i;
1997                if (i == HDA_CLFE) {
1998                        err = create_ch_ctls(codec, "Center", 1, true, path);
1999                        if (err < 0)
2000                                return err;
2001                        err = create_ch_ctls(codec, "LFE", 2, true, path);
2002                        if (err < 0)
2003                                return err;
2004                } else {
2005                        const char *pfx = chname[i];
2006                        if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
2007                            cfg->line_outs == 1)
2008                                pfx = "Speaker";
2009                        err = create_ch_ctls(codec, pfx, 3, true, path);
2010                        if (err < 0)
2011                                return err;
2012                }
2013                if (path != spec->out_path + i)
2014                        copy_path_mixer_ctls(&spec->out_path[i], path);
2015                if (path == spec->out_path && spec->out_mix_path.depth)
2016                        copy_path_mixer_ctls(&spec->out_mix_path, path);
2017        }
2018
2019        idx = get_connection_index(codec, spec->aa_mix_nid,
2020                                   spec->multiout.dac_nids[0]);
2021        if (idx >= 0) {
2022                /* add control to mixer */
2023                const char *name;
2024                name = spec->out_mix_path.depth ?
2025                        "PCM Loopback Playback Volume" : "PCM Playback Volume";
2026                err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
2027                                      HDA_COMPOSE_AMP_VAL(spec->aa_mix_nid, 3,
2028                                                          idx, HDA_INPUT));
2029                if (err < 0)
2030                        return err;
2031                name = spec->out_mix_path.depth ?
2032                        "PCM Loopback Playback Switch" : "PCM Playback Switch";
2033                err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
2034                                      HDA_COMPOSE_AMP_VAL(spec->aa_mix_nid, 3,
2035                                                          idx, HDA_INPUT));
2036                if (err < 0)
2037                        return err;
2038        }
2039
2040        cfg->line_outs = old_line_outs;
2041
2042        return 0;
2043}
2044
2045static int via_auto_create_hp_ctls(struct hda_codec *codec, hda_nid_t pin)
2046{
2047        struct via_spec *spec = codec->spec;
2048        struct nid_path *path;
2049        bool check_dac;
2050        int i, err;
2051
2052        if (!pin)
2053                return 0;
2054
2055        if (!parse_output_path(codec, pin, 0, 0, &spec->hp_indep_path)) {
2056                for (i = HDA_SIDE; i >= HDA_CLFE; i--) {
2057                        if (i < spec->multiout.num_dacs &&
2058                            parse_output_path(codec, pin,
2059                                              spec->multiout.dac_nids[i], 0,
2060                                              &spec->hp_indep_path)) {
2061                                spec->hp_indep_shared = i;
2062                                break;
2063                        }
2064                }
2065        }
2066        if (spec->hp_indep_path.depth) {
2067                spec->hp_dac_nid = spec->hp_indep_path.path[0];
2068                if (!spec->hp_indep_shared)
2069                        spec->hp_path = spec->hp_indep_path;
2070        }
2071        /* optionally check front-path w/o AA-mix */
2072        if (!spec->hp_path.depth)
2073                parse_output_path(codec, pin,
2074                                  spec->multiout.dac_nids[HDA_FRONT], 0,
2075                                  &spec->hp_path);
2076
2077        if (!parse_output_path(codec, pin, spec->multiout.dac_nids[HDA_FRONT],
2078                               1, &spec->hp_mix_path) && !spec->hp_path.depth)
2079                return 0;
2080
2081        if (spec->hp_path.depth) {
2082                path = &spec->hp_path;
2083                check_dac = true;
2084        } else {
2085                path = &spec->hp_mix_path;
2086                check_dac = false;
2087        }
2088        err = create_ch_ctls(codec, "Headphone", 3, check_dac, path);
2089        if (err < 0)
2090                return err;
2091        if (check_dac)
2092                copy_path_mixer_ctls(&spec->hp_mix_path, path);
2093        else
2094                copy_path_mixer_ctls(&spec->hp_path, path);
2095        if (spec->hp_indep_path.depth)
2096                copy_path_mixer_ctls(&spec->hp_indep_path, path);
2097        return 0;
2098}
2099
2100static int via_auto_create_speaker_ctls(struct hda_codec *codec)
2101{
2102        struct via_spec *spec = codec->spec;
2103        struct nid_path *path;
2104        bool check_dac;
2105        hda_nid_t pin, dac = 0;
2106        int err;
2107
2108        pin = spec->autocfg.speaker_pins[0];
2109        if (!spec->autocfg.speaker_outs || !pin)
2110                return 0;
2111
2112        if (parse_output_path(codec, pin, 0, 0, &spec->speaker_path))
2113                dac = spec->speaker_path.path[0];
2114        if (!dac)
2115                parse_output_path(codec, pin,
2116                                  spec->multiout.dac_nids[HDA_FRONT], 0,
2117                                  &spec->speaker_path);
2118        if (!parse_output_path(codec, pin, spec->multiout.dac_nids[HDA_FRONT],
2119                               1, &spec->speaker_mix_path) && !dac)
2120                return 0;
2121
2122        /* no AA-path for front? */
2123        if (!spec->out_mix_path.depth && spec->speaker_mix_path.depth)
2124                dac = 0;
2125
2126        spec->speaker_dac_nid = dac;
2127        spec->multiout.extra_out_nid[0] = dac;
2128        if (dac) {
2129                path = &spec->speaker_path;
2130                check_dac = true;
2131        } else {
2132                path = &spec->speaker_mix_path;
2133                check_dac = false;
2134        }
2135        err = create_ch_ctls(codec, "Speaker", 3, check_dac, path);
2136        if (err < 0)
2137                return err;
2138        if (check_dac)
2139                copy_path_mixer_ctls(&spec->speaker_mix_path, path);
2140        else
2141                copy_path_mixer_ctls(&spec->speaker_path, path);
2142        return 0;
2143}
2144
2145#define via_aamix_ctl_info      via_pin_power_ctl_info
2146
2147static int via_aamix_ctl_get(struct snd_kcontrol *kcontrol,
2148                             struct snd_ctl_elem_value *ucontrol)
2149{
2150        struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2151        struct via_spec *spec = codec->spec;
2152        ucontrol->value.enumerated.item[0] = spec->aamix_mode;
2153        return 0;
2154}
2155
2156static void update_aamix_paths(struct hda_codec *codec, int do_mix,
2157                               struct nid_path *nomix, struct nid_path *mix)
2158{
2159        if (do_mix) {
2160                activate_output_path(codec, nomix, false, false);
2161                activate_output_path(codec, mix, true, false);
2162        } else {
2163                activate_output_path(codec, mix, false, false);
2164                activate_output_path(codec, nomix, true, false);
2165        }
2166}
2167
2168static int via_aamix_ctl_put(struct snd_kcontrol *kcontrol,
2169                             struct snd_ctl_elem_value *ucontrol)
2170{
2171        struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2172        struct via_spec *spec = codec->spec;
2173        unsigned int val = ucontrol->value.enumerated.item[0];
2174
2175        if (val == spec->aamix_mode)
2176                return 0;
2177        spec->aamix_mode = val;
2178        /* update front path */
2179        update_aamix_paths(codec, val, &spec->out_path[0], &spec->out_mix_path);
2180        /* update HP path */
2181        if (!spec->hp_independent_mode) {
2182                update_aamix_paths(codec, val, &spec->hp_path,
2183                                   &spec->hp_mix_path);
2184        }
2185        /* update speaker path */
2186        update_aamix_paths(codec, val, &spec->speaker_path,
2187                           &spec->speaker_mix_path);
2188        return 1;
2189}
2190
2191static const struct snd_kcontrol_new via_aamix_ctl_enum = {
2192        .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2193        .name = "Loopback Mixing",
2194        .info = via_aamix_ctl_info,
2195        .get = via_aamix_ctl_get,
2196        .put = via_aamix_ctl_put,
2197};
2198
2199static int via_auto_create_loopback_switch(struct hda_codec *codec)
2200{
2201        struct via_spec *spec = codec->spec;
2202
2203        if (!spec->aa_mix_nid)
2204                return 0; /* no loopback switching available */
2205        if (!(spec->out_mix_path.depth || spec->hp_mix_path.depth ||
2206              spec->speaker_path.depth))
2207                return 0; /* no loopback switching available */
2208        if (!via_clone_control(spec, &via_aamix_ctl_enum))
2209                return -ENOMEM;
2210        return 0;
2211}
2212
2213/* look for ADCs */
2214static int via_fill_adcs(struct hda_codec *codec)
2215{
2216        struct via_spec *spec = codec->spec;
2217        hda_nid_t nid = codec->start_nid;
2218        int i;
2219
2220        for (i = 0; i < codec->num_nodes; i++, nid++) {
2221                unsigned int wcaps = get_wcaps(codec, nid);
2222                if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
2223                        continue;
2224                if (wcaps & AC_WCAP_DIGITAL)
2225                        continue;
2226                if (!(wcaps & AC_WCAP_CONN_LIST))
2227                        continue;
2228                if (spec->num_adc_nids >= ARRAY_SIZE(spec->adc_nids))
2229                        return -ENOMEM;
2230                spec->adc_nids[spec->num_adc_nids++] = nid;
2231        }
2232        return 0;
2233}
2234
2235/* input-src control */
2236static int via_mux_enum_info(struct snd_kcontrol *kcontrol,
2237                             struct snd_ctl_elem_info *uinfo)
2238{
2239        struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2240        struct via_spec *spec = codec->spec;
2241
2242        uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2243        uinfo->count = 1;
2244        uinfo->value.enumerated.items = spec->num_inputs;
2245        if (uinfo->value.enumerated.item >= spec->num_inputs)
2246                uinfo->value.enumerated.item = spec->num_inputs - 1;
2247        strcpy(uinfo->value.enumerated.name,
2248               spec->inputs[uinfo->value.enumerated.item].label);
2249        return 0;
2250}
2251
2252static int via_mux_enum_get(struct snd_kcontrol *kcontrol,
2253                            struct snd_ctl_elem_value *ucontrol)
2254{
2255        struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2256        struct via_spec *spec = codec->spec;
2257        unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2258
2259        ucontrol->value.enumerated.item[0] = spec->cur_mux[idx];
2260        return 0;
2261}
2262
2263static int via_mux_enum_put(struct snd_kcontrol *kcontrol,
2264                            struct snd_ctl_elem_value *ucontrol)
2265{
2266        struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2267        struct via_spec *spec = codec->spec;
2268        unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
2269        hda_nid_t mux;
2270        int cur;
2271
2272        cur = ucontrol->value.enumerated.item[0];
2273        if (cur < 0 || cur >= spec->num_inputs)
2274                return -EINVAL;
2275        if (spec->cur_mux[idx] == cur)
2276                return 0;
2277        spec->cur_mux[idx] = cur;
2278        if (spec->dyn_adc_switch) {
2279                int adc_idx = spec->inputs[cur].adc_idx;
2280                mux = spec->mux_nids[adc_idx];
2281                via_dyn_adc_pcm_resetup(codec, cur);
2282        } else {
2283                mux = spec->mux_nids[idx];
2284                if (snd_BUG_ON(!mux))
2285                        return -EINVAL;
2286        }
2287
2288        if (mux) {
2289                /* switch to D0 beofre change index */
2290                if (snd_hda_codec_read(codec, mux, 0,
2291                               AC_VERB_GET_POWER_STATE, 0x00) != AC_PWRST_D0)
2292                        snd_hda_codec_write(codec, mux, 0,
2293                                    AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
2294                snd_hda_codec_write(codec, mux, 0,
2295                                    AC_VERB_SET_CONNECT_SEL,
2296                                    spec->inputs[cur].mux_idx);
2297        }
2298
2299        /* update jack power state */
2300        set_widgets_power_state(codec);
2301        return 0;
2302}
2303
2304static const struct snd_kcontrol_new via_input_src_ctl = {
2305        .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2306        /* The multiple "Capture Source" controls confuse alsamixer
2307         * So call somewhat different..
2308         */
2309        /* .name = "Capture Source", */
2310        .name = "Input Source",
2311        .info = via_mux_enum_info,
2312        .get = via_mux_enum_get,
2313        .put = via_mux_enum_put,
2314};
2315
2316static int create_input_src_ctls(struct hda_codec *codec, int count)
2317{
2318        struct via_spec *spec = codec->spec;
2319        struct snd_kcontrol_new *knew;
2320
2321        if (spec->num_inputs <= 1 || !count)
2322                return 0; /* no need for single src */
2323
2324        knew = via_clone_control(spec, &via_input_src_ctl);
2325        if (!knew)
2326                return -ENOMEM;
2327        knew->count = count;
2328        return 0;
2329}
2330
2331/* add the powersave loopback-list entry */
2332static void add_loopback_list(struct via_spec *spec, hda_nid_t mix, int idx)
2333{
2334        struct hda_amp_list *list;
2335
2336        if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
2337                return;
2338        list = spec->loopback_list + spec->num_loopbacks;
2339        list->nid = mix;
2340        list->dir = HDA_INPUT;
2341        list->idx = idx;
2342        spec->num_loopbacks++;
2343        spec->loopback.amplist = spec->loopback_list;
2344}
2345
2346static bool is_reachable_nid(struct hda_codec *codec, hda_nid_t src,
2347                             hda_nid_t dst)
2348{
2349        return snd_hda_get_conn_index(codec, src, dst, 1) >= 0;
2350}
2351
2352/* add the input-route to the given pin */
2353static bool add_input_route(struct hda_codec *codec, hda_nid_t pin)
2354{
2355        struct via_spec *spec = codec->spec;
2356        int c, idx;
2357
2358        spec->inputs[spec->num_inputs].adc_idx = -1;
2359        spec->inputs[spec->num_inputs].pin = pin;
2360        for (c = 0; c < spec->num_adc_nids; c++) {
2361                if (spec->mux_nids[c]) {
2362                        idx = get_connection_index(codec, spec->mux_nids[c],
2363                                                   pin);
2364                        if (idx < 0)
2365                                continue;
2366                        spec->inputs[spec->num_inputs].mux_idx = idx;
2367                } else {
2368                        if (!is_reachable_nid(codec, spec->adc_nids[c], pin))
2369                                continue;
2370                }
2371                spec->inputs[spec->num_inputs].adc_idx = c;
2372                /* Can primary ADC satisfy all inputs? */
2373                if (!spec->dyn_adc_switch &&
2374                    spec->num_inputs > 0 && spec->inputs[0].adc_idx != c) {
2375                        snd_printd(KERN_INFO
2376                                   "via: dynamic ADC switching enabled\n");
2377                        spec->dyn_adc_switch = 1;
2378                }
2379                return true;
2380        }
2381        return false;
2382}
2383
2384static int get_mux_nids(struct hda_codec *codec);
2385
2386/* parse input-routes; fill ADCs, MUXs and input-src entries */
2387static int parse_analog_inputs(struct hda_codec *codec)
2388{
2389        struct via_spec *spec = codec->spec;
2390        const struct auto_pin_cfg *cfg = &spec->autocfg;
2391        int i, err;
2392
2393        err = via_fill_adcs(codec);
2394        if (err < 0)
2395                return err;
2396        err = get_mux_nids(codec);
2397        if (err < 0)
2398                return err;
2399
2400        /* fill all input-routes */
2401        for (i = 0; i < cfg->num_inputs; i++) {
2402                if (add_input_route(codec, cfg->inputs[i].pin))
2403                        spec->inputs[spec->num_inputs++].label =
2404                                hda_get_autocfg_input_label(codec, cfg, i);
2405        }
2406
2407        /* check for internal loopback recording */
2408        if (spec->aa_mix_nid &&
2409            add_input_route(codec, spec->aa_mix_nid))
2410                spec->inputs[spec->num_inputs++].label = "Stereo Mixer";
2411
2412        return 0;
2413}
2414
2415/* create analog-loopback volume/switch controls */
2416static int create_loopback_ctls(struct hda_codec *codec)
2417{
2418        struct via_spec *spec = codec->spec;
2419        const struct auto_pin_cfg *cfg = &spec->autocfg;
2420        const char *prev_label = NULL;
2421        int type_idx = 0;
2422        int i, j, err, idx;
2423
2424        if (!spec->aa_mix_nid)
2425                return 0;
2426
2427        for (i = 0; i < cfg->num_inputs; i++) {
2428                hda_nid_t pin = cfg->inputs[i].pin;
2429                const char *label = hda_get_autocfg_input_label(codec, cfg, i);
2430
2431                if (prev_label && !strcmp(label, prev_label))
2432                        type_idx++;
2433                else
2434                        type_idx = 0;
2435                prev_label = label;
2436                idx = get_connection_index(codec, spec->aa_mix_nid, pin);
2437                if (idx >= 0) {
2438                        err = via_new_analog_input(spec, label, type_idx,
2439                                                   idx, spec->aa_mix_nid);
2440                        if (err < 0)
2441                                return err;
2442                        add_loopback_list(spec, spec->aa_mix_nid, idx);
2443                }
2444
2445                /* remember the label for smart51 control */
2446                for (j = 0; j < spec->smart51_nums; j++) {
2447                        if (spec->smart51_pins[j] == pin) {
2448                                spec->smart51_idxs[j] = idx;
2449                                spec->smart51_labels[j] = label;
2450                                break;
2451                        }
2452                }
2453        }
2454        return 0;
2455}
2456
2457/* create mic-boost controls (if present) */
2458static int create_mic_boost_ctls(struct hda_codec *codec)
2459{
2460        struct via_spec *spec = codec->spec;
2461        const struct auto_pin_cfg *cfg = &spec->autocfg;
2462        int i, err;
2463
2464        for (i = 0; i < cfg->num_inputs; i++) {
2465                hda_nid_t pin = cfg->inputs[i].pin;
2466                unsigned int caps;
2467                const char *label;
2468                char name[32];
2469
2470                if (cfg->inputs[i].type != AUTO_PIN_MIC)
2471                        continue;
2472                caps = query_amp_caps(codec, pin, HDA_INPUT);
2473                if (caps == -1 || !(caps & AC_AMPCAP_NUM_STEPS))
2474                        continue;
2475                label = hda_get_autocfg_input_label(codec, cfg, i);
2476                snprintf(name, sizeof(name), "%s Boost Volume", label);
2477                err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
2478                              HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT));
2479                if (err < 0)
2480                        return err;
2481        }
2482        return 0;
2483}
2484
2485/* create capture and input-src controls for multiple streams */
2486static int create_multi_adc_ctls(struct hda_codec *codec)
2487{
2488        struct via_spec *spec = codec->spec;
2489        int i, err;
2490
2491        /* create capture mixer elements */
2492        for (i = 0; i < spec->num_adc_nids; i++) {
2493                hda_nid_t adc = spec->adc_nids[i];
2494                err = __via_add_control(spec, VIA_CTL_WIDGET_VOL,
2495                                        "Capture Volume", i,
2496                                        HDA_COMPOSE_AMP_VAL(adc, 3, 0,
2497                                                            HDA_INPUT));
2498                if (err < 0)
2499                        return err;
2500                err = __via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2501                                        "Capture Switch", i,
2502                                        HDA_COMPOSE_AMP_VAL(adc, 3, 0,
2503                                                            HDA_INPUT));
2504                if (err < 0)
2505                        return err;
2506        }
2507
2508        /* input-source control */
2509        for (i = 0; i < spec->num_adc_nids; i++)
2510                if (!spec->mux_nids[i])
2511                        break;
2512        err = create_input_src_ctls(codec, i);
2513        if (err < 0)
2514                return err;
2515        return 0;
2516}
2517
2518/* bind capture volume/switch */
2519static struct snd_kcontrol_new via_bind_cap_vol_ctl =
2520        HDA_BIND_VOL("Capture Volume", 0);
2521static struct snd_kcontrol_new via_bind_cap_sw_ctl =
2522        HDA_BIND_SW("Capture Switch", 0);
2523
2524static int init_bind_ctl(struct via_spec *spec, struct hda_bind_ctls **ctl_ret,
2525                         struct hda_ctl_ops *ops)
2526{
2527        struct hda_bind_ctls *ctl;
2528        int i;
2529
2530        ctl = kzalloc(sizeof(*ctl) + sizeof(long) * 4, GFP_KERNEL);
2531        if (!ctl)
2532                return -ENOMEM;
2533        ctl->ops = ops;
2534        for (i = 0; i < spec->num_adc_nids; i++)
2535                ctl->values[i] =
2536                        HDA_COMPOSE_AMP_VAL(spec->adc_nids[i], 3, 0, HDA_INPUT);
2537        *ctl_ret = ctl;
2538        return 0;
2539}
2540
2541/* create capture and input-src controls for dynamic ADC-switch case */
2542static int create_dyn_adc_ctls(struct hda_codec *codec)
2543{
2544        struct via_spec *spec = codec->spec;
2545        struct snd_kcontrol_new *knew;
2546        int err;
2547
2548        /* set up the bind capture ctls */
2549        err = init_bind_ctl(spec, &spec->bind_cap_vol, &snd_hda_bind_vol);
2550        if (err < 0)
2551                return err;
2552        err = init_bind_ctl(spec, &spec->bind_cap_sw, &snd_hda_bind_sw);
2553        if (err < 0)
2554                return err;
2555
2556        /* create capture mixer elements */
2557        knew = via_clone_control(spec, &via_bind_cap_vol_ctl);
2558        if (!knew)
2559                return -ENOMEM;
2560        knew->private_value = (long)spec->bind_cap_vol;
2561
2562        knew = via_clone_control(spec, &via_bind_cap_sw_ctl);
2563        if (!knew)
2564                return -ENOMEM;
2565        knew->private_value = (long)spec->bind_cap_sw;
2566
2567        /* input-source control */
2568        err = create_input_src_ctls(codec, 1);
2569        if (err < 0)
2570                return err;
2571        return 0;
2572}
2573
2574/* parse and create capture-related stuff */
2575static int via_auto_create_analog_input_ctls(struct hda_codec *codec)
2576{
2577        struct via_spec *spec = codec->spec;
2578        int err;
2579
2580        err = parse_analog_inputs(codec);
2581        if (err < 0)
2582                return err;
2583        if (spec->dyn_adc_switch)
2584                err = create_dyn_adc_ctls(codec);
2585        else
2586                err = create_multi_adc_ctls(codec);
2587        if (err < 0)
2588                return err;
2589        err = create_loopback_ctls(codec);
2590        if (err < 0)
2591                return err;
2592        err = create_mic_boost_ctls(codec);
2593        if (err < 0)
2594                return err;
2595        return 0;
2596}
2597
2598static void vt1708_set_pinconfig_connect(struct hda_codec *codec, hda_nid_t nid)
2599{
2600        unsigned int def_conf;
2601        unsigned char seqassoc;
2602
2603        def_conf = snd_hda_codec_get_pincfg(codec, nid);
2604        seqassoc = (unsigned char) get_defcfg_association(def_conf);
2605        seqassoc = (seqassoc << 4) | get_defcfg_sequence(def_conf);
2606        if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE
2607            && (seqassoc == 0xf0 || seqassoc == 0xff)) {
2608                def_conf = def_conf & (~(AC_JACK_PORT_BOTH << 30));
2609                snd_hda_codec_set_pincfg(codec, nid, def_conf);
2610        }
2611
2612        return;
2613}
2614
2615static int vt1708_jack_detect_get(struct snd_kcontrol *kcontrol,
2616                                     struct snd_ctl_elem_value *ucontrol)
2617{
2618        struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2619        struct via_spec *spec = codec->spec;
2620
2621        if (spec->codec_type != VT1708)
2622                return 0;
2623        ucontrol->value.integer.value[0] = spec->vt1708_jack_detect;
2624        return 0;
2625}
2626
2627static int vt1708_jack_detect_put(struct snd_kcontrol *kcontrol,
2628                                     struct snd_ctl_elem_value *ucontrol)
2629{
2630        struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2631        struct via_spec *spec = codec->spec;
2632        int val;
2633
2634        if (spec->codec_type != VT1708)
2635                return 0;
2636        val = !!ucontrol->value.integer.value[0];
2637        if (spec->vt1708_jack_detect == val)
2638                return 0;
2639        spec->vt1708_jack_detect = val;
2640        if (spec->vt1708_jack_detect &&
2641            snd_hda_get_bool_hint(codec, "analog_loopback_hp_detect") != 1) {
2642                mute_aa_path(codec, 1);
2643                notify_aa_path_ctls(codec);
2644        }
2645        via_hp_automute(codec);
2646        vt1708_update_hp_work(spec);
2647        return 1;
2648}
2649
2650static const struct snd_kcontrol_new vt1708_jack_detect_ctl = {
2651        .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2652        .name = "Jack Detect",
2653        .count = 1,
2654        .info = snd_ctl_boolean_mono_info,
2655        .get = vt1708_jack_detect_get,
2656        .put = vt1708_jack_detect_put,
2657};
2658
2659static void fill_dig_outs(struct hda_codec *codec);
2660static void fill_dig_in(struct hda_codec *codec);
2661
2662static int via_parse_auto_config(struct hda_codec *codec)
2663{
2664        struct via_spec *spec = codec->spec;
2665        int err;
2666
2667        err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
2668        if (err < 0)
2669                return err;
2670        if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
2671                return -EINVAL;
2672
2673        err = via_auto_create_multi_out_ctls(codec);
2674        if (err < 0)
2675                return err;
2676        err = via_auto_create_hp_ctls(codec, spec->autocfg.hp_pins[0]);
2677        if (err < 0)
2678                return err;
2679        err = via_auto_create_speaker_ctls(codec);
2680        if (err < 0)
2681                return err;
2682        err = via_auto_create_loopback_switch(codec);
2683        if (err < 0)
2684                return err;
2685        err = via_auto_create_analog_input_ctls(codec);
2686        if (err < 0)
2687                return err;
2688
2689        spec->multiout.max_channels = spec->multiout.num_dacs * 2;
2690
2691        fill_dig_outs(codec);
2692        fill_dig_in(codec);
2693
2694        if (spec->kctls.list)
2695                spec->mixers[spec->num_mixers++] = spec->kctls.list;
2696
2697
2698        if (spec->hp_dac_nid && spec->hp_mix_path.depth) {
2699                err = via_hp_build(codec);
2700                if (err < 0)
2701                        return err;
2702        }
2703
2704        err = via_smart51_build(codec);
2705        if (err < 0)
2706                return err;
2707
2708        /* assign slave outs */
2709        if (spec->slave_dig_outs[0])
2710                codec->slave_dig_outs = spec->slave_dig_outs;
2711
2712        return 1;
2713}
2714
2715static void via_auto_init_dig_outs(struct hda_codec *codec)
2716{
2717        struct via_spec *spec = codec->spec;
2718        if (spec->multiout.dig_out_nid)
2719                init_output_pin(codec, spec->autocfg.dig_out_pins[0], PIN_OUT);
2720        if (spec->slave_dig_outs[0])
2721                init_output_pin(codec, spec->autocfg.dig_out_pins[1], PIN_OUT);
2722}
2723
2724static void via_auto_init_dig_in(struct hda_codec *codec)
2725{
2726        struct via_spec *spec = codec->spec;
2727        if (!spec->dig_in_nid)
2728                return;
2729        snd_hda_codec_write(codec, spec->autocfg.dig_in_pin, 0,
2730                            AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN);
2731}
2732
2733/* initialize the unsolicited events */
2734static void via_auto_init_unsol_event(struct hda_codec *codec)
2735{
2736        struct via_spec *spec = codec->spec;
2737        struct auto_pin_cfg *cfg = &spec->autocfg;
2738        unsigned int ev;
2739        int i;
2740
2741        if (cfg->hp_pins[0] && is_jack_detectable(codec, cfg->hp_pins[0]))
2742                snd_hda_codec_write(codec, cfg->hp_pins[0], 0,
2743                                AC_VERB_SET_UNSOLICITED_ENABLE,
2744                                AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT);
2745
2746        if (cfg->speaker_pins[0])
2747                ev = VIA_LINE_EVENT;
2748        else
2749                ev = 0;
2750        for (i = 0; i < cfg->line_outs; i++) {
2751                if (cfg->line_out_pins[i] &&
2752                    is_jack_detectable(codec, cfg->line_out_pins[i]))
2753                        snd_hda_codec_write(codec, cfg->line_out_pins[i], 0,
2754                                AC_VERB_SET_UNSOLICITED_ENABLE,
2755                                AC_USRSP_EN | ev | VIA_JACK_EVENT);
2756        }
2757
2758        for (i = 0; i < cfg->num_inputs; i++) {
2759                if (is_jack_detectable(codec, cfg->inputs[i].pin))
2760                        snd_hda_codec_write(codec, cfg->inputs[i].pin, 0,
2761                                AC_VERB_SET_UNSOLICITED_ENABLE,
2762                                AC_USRSP_EN | VIA_JACK_EVENT);
2763        }
2764}
2765
2766static int via_init(struct hda_codec *codec)
2767{
2768        struct via_spec *spec = codec->spec;
2769        int i;
2770
2771        for (i = 0; i < spec->num_iverbs; i++)
2772                snd_hda_sequence_write(codec, spec->init_verbs[i]);
2773
2774        via_auto_init_multi_out(codec);
2775        via_auto_init_hp_out(codec);
2776        via_auto_init_speaker_out(codec);
2777        via_auto_init_analog_input(codec);
2778        via_auto_init_dig_outs(codec);
2779        via_auto_init_dig_in(codec);
2780
2781        via_auto_init_unsol_event(codec);
2782
2783        via_hp_automute(codec);
2784        vt1708_update_hp_work(spec);
2785
2786        return 0;
2787}
2788
2789static void vt1708_update_hp_jack_state(struct work_struct *work)
2790{
2791        struct via_spec *spec = container_of(work, struct via_spec,
2792                                             vt1708_hp_work.work);
2793        if (spec->codec_type != VT1708)
2794                return;
2795        /* if jack state toggled */
2796        if (spec->vt1708_hp_present
2797            != snd_hda_jack_detect(spec->codec, spec->autocfg.hp_pins[0])) {
2798                spec->vt1708_hp_present ^= 1;
2799                via_hp_automute(spec->codec);
2800        }
2801        if (spec->vt1708_jack_detect)
2802                schedule_delayed_work(&spec->vt1708_hp_work,
2803                                      msecs_to_jiffies(100));
2804}
2805
2806static int get_mux_nids(struct hda_codec *codec)
2807{
2808        struct via_spec *spec = codec->spec;
2809        hda_nid_t nid, conn[8];
2810        unsigned int type;
2811        int i, n;
2812
2813        for (i = 0; i < spec->num_adc_nids; i++) {
2814                nid = spec->adc_nids[i];
2815                while (nid) {
2816                        type = get_wcaps_type(get_wcaps(codec, nid));
2817                        if (type == AC_WID_PIN)
2818                                break;
2819                        n = snd_hda_get_connections(codec, nid, conn,
2820                                                    ARRAY_SIZE(conn));
2821                        if (n <= 0)
2822                                break;
2823                        if (n > 1) {
2824                                spec->mux_nids[i] = nid;
2825                                break;
2826                        }
2827                        nid = conn[0];
2828                }
2829        }
2830        return 0;
2831}
2832
2833static int patch_vt1708(struct hda_codec *codec)
2834{
2835        struct via_spec *spec;
2836        int err;
2837
2838        /* create a codec specific record */
2839        spec = via_new_spec(codec);
2840        if (spec == NULL)
2841                return -ENOMEM;
2842
2843        spec->aa_mix_nid = 0x17;
2844
2845        /* Add HP and CD pin config connect bit re-config action */
2846        vt1708_set_pinconfig_connect(codec, VT1708_HP_PIN_NID);
2847        vt1708_set_pinconfig_connect(codec, VT1708_CD_PIN_NID);
2848
2849        /* automatic parse from the BIOS config */
2850        err = via_parse_auto_config(codec);
2851        if (err < 0) {
2852                via_free(codec);
2853                return err;
2854        }
2855
2856        /* add jack detect on/off control */
2857        if (!via_clone_control(spec, &vt1708_jack_detect_ctl))
2858                return -ENOMEM;
2859
2860        /* disable 32bit format on VT1708 */
2861        if (codec->vendor_id == 0x11061708)
2862                spec->stream_analog_playback = &vt1708_pcm_analog_s16_playback;
2863
2864        spec->init_verbs[spec->num_iverbs++] = vt1708_init_verbs;
2865
2866        codec->patch_ops = via_patch_ops;
2867
2868        INIT_DELAYED_WORK(&spec->vt1708_hp_work, vt1708_update_hp_jack_state);
2869        return 0;
2870}
2871
2872static int patch_vt1709(struct hda_codec *codec)
2873{
2874        struct via_spec *spec;
2875        int err;
2876
2877        /* create a codec specific record */
2878        spec = via_new_spec(codec);
2879        if (spec == NULL)
2880                return -ENOMEM;
2881
2882        spec->aa_mix_nid = 0x18;
2883
2884        err = via_parse_auto_config(codec);
2885        if (err < 0) {
2886                via_free(codec);
2887                return err;
2888        }
2889
2890        codec->patch_ops = via_patch_ops;
2891
2892        return 0;
2893}
2894
2895static void set_widgets_power_state_vt1708B(struct hda_codec *codec)
2896{
2897        struct via_spec *spec = codec->spec;
2898        int imux_is_smixer;
2899        unsigned int parm;
2900        int is_8ch = 0;
2901        if ((spec->codec_type != VT1708B_4CH) &&
2902            (codec->vendor_id != 0x11064397))
2903                is_8ch = 1;
2904
2905        /* SW0 (17h) = stereo mixer */
2906        imux_is_smixer =
2907        (snd_hda_codec_read(codec, 0x17, 0, AC_VERB_GET_CONNECT_SEL, 0x00)
2908         == ((spec->codec_type == VT1708S) ? 5 : 0));
2909        /* inputs */
2910        /* PW 1/2/5 (1ah/1bh/1eh) */
2911        parm = AC_PWRST_D3;
2912        set_pin_power_state(codec, 0x1a, &parm);
2913        set_pin_power_state(codec, 0x1b, &parm);
2914        set_pin_power_state(codec, 0x1e, &parm);
2915        if (imux_is_smixer)
2916                parm = AC_PWRST_D0;
2917        /* SW0 (17h), AIW 0/1 (13h/14h) */
2918        snd_hda_codec_write(codec, 0x17, 0, AC_VERB_SET_POWER_STATE, parm);
2919        snd_hda_codec_write(codec, 0x13, 0, AC_VERB_SET_POWER_STATE, parm);
2920        snd_hda_codec_write(codec, 0x14, 0, AC_VERB_SET_POWER_STATE, parm);
2921
2922        /* outputs */
2923        /* PW0 (19h), SW1 (18h), AOW1 (11h) */
2924        parm = AC_PWRST_D3;
2925        set_pin_power_state(codec, 0x19, &parm);
2926        if (spec->smart51_enabled)
2927                set_pin_power_state(codec, 0x1b, &parm);
2928        snd_hda_codec_write(codec, 0x18, 0, AC_VERB_SET_POWER_STATE, parm);
2929        snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE, parm);
2930
2931        /* PW6 (22h), SW2 (26h), AOW2 (24h) */
2932        if (is_8ch) {
2933                parm = AC_PWRST_D3;
2934                set_pin_power_state(codec, 0x22, &parm);
2935                if (spec->smart51_enabled)
2936                        set_pin_power_state(codec, 0x1a, &parm);
2937                snd_hda_codec_write(codec, 0x26, 0,
2938                                    AC_VERB_SET_POWER_STATE, parm);
2939                snd_hda_codec_write(codec, 0x24, 0,
2940                                    AC_VERB_SET_POWER_STATE, parm);
2941        } else if (codec->vendor_id == 0x11064397) {
2942                /* PW7(23h), SW2(27h), AOW2(25h) */
2943                parm = AC_PWRST_D3;
2944                set_pin_power_state(codec, 0x23, &parm);
2945                if (spec->smart51_enabled)
2946                        set_pin_power_state(codec, 0x1a, &parm);
2947                snd_hda_codec_write(codec, 0x27, 0,
2948                                    AC_VERB_SET_POWER_STATE, parm);
2949                snd_hda_codec_write(codec, 0x25, 0,
2950                                    AC_VERB_SET_POWER_STATE, parm);
2951        }
2952
2953        /* PW 3/4/7 (1ch/1dh/23h) */
2954        parm = AC_PWRST_D3;
2955        /* force to D0 for internal Speaker */
2956        set_pin_power_state(codec, 0x1c, &parm);
2957        set_pin_power_state(codec, 0x1d, &parm);
2958        if (is_8ch)
2959                set_pin_power_state(codec, 0x23, &parm);
2960
2961        /* MW0 (16h), Sw3 (27h), AOW 0/3 (10h/25h) */
2962        snd_hda_codec_write(codec, 0x16, 0, AC_VERB_SET_POWER_STATE,
2963                            imux_is_smixer ? AC_PWRST_D0 : parm);
2964        snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE, parm);
2965        if (is_8ch) {
2966                snd_hda_codec_write(codec, 0x25, 0,
2967                                    AC_VERB_SET_POWER_STATE, parm);
2968                snd_hda_codec_write(codec, 0x27, 0,
2969                                    AC_VERB_SET_POWER_STATE, parm);
2970        } else if (codec->vendor_id == 0x11064397 && spec->hp_independent_mode)
2971                snd_hda_codec_write(codec, 0x25, 0,
2972                                    AC_VERB_SET_POWER_STATE, parm);
2973}
2974
2975static int patch_vt1708S(struct hda_codec *codec);
2976static int patch_vt1708B(struct hda_codec *codec)
2977{
2978        struct via_spec *spec;
2979        int err;
2980
2981        if (get_codec_type(codec) == VT1708BCE)
2982                return patch_vt1708S(codec);
2983
2984        /* create a codec specific record */
2985        spec = via_new_spec(codec);
2986        if (spec == NULL)
2987                return -ENOMEM;
2988
2989        spec->aa_mix_nid = 0x16;
2990
2991        /* automatic parse from the BIOS config */
2992        err = via_parse_auto_config(codec);
2993        if (err < 0) {
2994                via_free(codec);
2995                return err;
2996        }
2997
2998        codec->patch_ops = via_patch_ops;
2999
3000        spec->set_widgets_power_state =  set_widgets_power_state_vt1708B;
3001
3002        return 0;
3003}
3004
3005/* Patch for VT1708S */
3006static const struct hda_verb vt1708S_init_verbs[] = {
3007        /* Enable Mic Boost Volume backdoor */
3008        {0x1, 0xf98, 0x1},
3009        /* don't bybass mixer */
3010        {0x1, 0xf88, 0xc0},
3011        { }
3012};
3013
3014/* fill out digital output widgets; one for master and one for slave outputs */
3015static void fill_dig_outs(struct hda_codec *codec)
3016{
3017        struct via_spec *spec = codec->spec;
3018        int i;
3019
3020        for (i = 0; i < spec->autocfg.dig_outs; i++) {
3021                hda_nid_t nid;
3022                int conn;
3023
3024                nid = spec->autocfg.dig_out_pins[i];
3025                if (!nid)
3026                        continue;
3027                conn = snd_hda_get_connections(codec, nid, &nid, 1);
3028                if (conn < 1)
3029                        continue;
3030                if (!spec->multiout.dig_out_nid)
3031                        spec->multiout.dig_out_nid = nid;
3032                else {
3033                        spec->slave_dig_outs[0] = nid;
3034                        break; /* at most two dig outs */
3035                }
3036        }
3037}
3038
3039static void fill_dig_in(struct hda_codec *codec)
3040{
3041        struct via_spec *spec = codec->spec;
3042        hda_nid_t dig_nid;
3043        int i, err;
3044
3045        if (!spec->autocfg.dig_in_pin)
3046                return;
3047
3048        dig_nid = codec->start_nid;
3049        for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
3050                unsigned int wcaps = get_wcaps(codec, dig_nid);
3051                if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
3052                        continue;
3053                if (!(wcaps & AC_WCAP_DIGITAL))
3054                        continue;
3055                if (!(wcaps & AC_WCAP_CONN_LIST))
3056                        continue;
3057                err = get_connection_index(codec, dig_nid,
3058                                           spec->autocfg.dig_in_pin);
3059                if (err >= 0) {
3060                        spec->dig_in_nid = dig_nid;
3061                        break;
3062                }
3063        }
3064}
3065
3066static void override_mic_boost(struct hda_codec *codec, hda_nid_t pin,
3067                               int offset, int num_steps, int step_size)
3068{
3069        snd_hda_override_amp_caps(codec, pin, HDA_INPUT,
3070                                  (offset << AC_AMPCAP_OFFSET_SHIFT) |
3071                                  (num_steps << AC_AMPCAP_NUM_STEPS_SHIFT) |
3072                                  (step_size << AC_AMPCAP_STEP_SIZE_SHIFT) |
3073                                  (0 << AC_AMPCAP_MUTE_SHIFT));
3074}
3075
3076static int patch_vt1708S(struct hda_codec *codec)
3077{
3078        struct via_spec *spec;
3079        int err;
3080
3081        /* create a codec specific record */
3082        spec = via_new_spec(codec);
3083        if (spec == NULL)
3084                return -ENOMEM;
3085
3086        spec->aa_mix_nid = 0x16;
3087        override_mic_boost(codec, 0x1a, 0, 3, 40);
3088        override_mic_boost(codec, 0x1e, 0, 3, 40);
3089
3090        /* automatic parse from the BIOS config */
3091        err = via_parse_auto_config(codec);
3092        if (err < 0) {
3093                via_free(codec);
3094                return err;
3095        }
3096
3097        spec->init_verbs[spec->num_iverbs++] = vt1708S_init_verbs;
3098
3099        codec->patch_ops = via_patch_ops;
3100
3101        /* correct names for VT1708BCE */
3102        if (get_codec_type(codec) == VT1708BCE) {
3103                kfree(codec->chip_name);
3104                codec->chip_name = kstrdup("VT1708BCE", GFP_KERNEL);
3105                snprintf(codec->bus->card->mixername,
3106                         sizeof(codec->bus->card->mixername),
3107                         "%s %s", codec->vendor_name, codec->chip_name);
3108        }
3109        /* correct names for VT1705 */
3110        if (codec->vendor_id == 0x11064397)     {
3111                kfree(codec->chip_name);
3112                codec->chip_name = kstrdup("VT1705", GFP_KERNEL);
3113                snprintf(codec->bus->card->mixername,
3114                         sizeof(codec->bus->card->mixername),
3115                         "%s %s", codec->vendor_name, codec->chip_name);
3116        }
3117        spec->set_widgets_power_state =  set_widgets_power_state_vt1708B;
3118        return 0;
3119}
3120
3121/* Patch for VT1702 */
3122
3123static const struct hda_verb vt1702_init_verbs[] = {
3124        /* mixer enable */
3125        {0x1, 0xF88, 0x3},
3126        /* GPIO 0~2 */
3127        {0x1, 0xF82, 0x3F},
3128        { }
3129};
3130
3131static void set_widgets_power_state_vt1702(struct hda_codec *codec)
3132{
3133        int imux_is_smixer =
3134        snd_hda_codec_read(codec, 0x13, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
3135        unsigned int parm;
3136        /* inputs */
3137        /* PW 1/2/5 (14h/15h/18h) */
3138        parm = AC_PWRST_D3;
3139        set_pin_power_state(codec, 0x14, &parm);
3140        set_pin_power_state(codec, 0x15, &parm);
3141        set_pin_power_state(codec, 0x18, &parm);
3142        if (imux_is_smixer)
3143                parm = AC_PWRST_D0; /* SW0 (13h) = stereo mixer (idx 3) */
3144        /* SW0 (13h), AIW 0/1/2 (12h/1fh/20h) */
3145        snd_hda_codec_write(codec, 0x13, 0, AC_VERB_SET_POWER_STATE, parm);
3146        snd_hda_codec_write(codec, 0x12, 0, AC_VERB_SET_POWER_STATE, parm);
3147        snd_hda_codec_write(codec, 0x1f, 0, AC_VERB_SET_POWER_STATE, parm);
3148        snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_POWER_STATE, parm);
3149
3150        /* outputs */
3151        /* PW 3/4 (16h/17h) */
3152        parm = AC_PWRST_D3;
3153        set_pin_power_state(codec, 0x17, &parm);
3154        set_pin_power_state(codec, 0x16, &parm);
3155        /* MW0 (1ah), AOW 0/1 (10h/1dh) */
3156        snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_POWER_STATE,
3157                            imux_is_smixer ? AC_PWRST_D0 : parm);
3158        snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE, parm);
3159        snd_hda_codec_write(codec, 0x1d, 0, AC_VERB_SET_POWER_STATE, parm);
3160}
3161
3162static int patch_vt1702(struct hda_codec *codec)
3163{
3164        struct via_spec *spec;
3165        int err;
3166
3167        /* create a codec specific record */
3168        spec = via_new_spec(codec);
3169        if (spec == NULL)
3170                return -ENOMEM;
3171
3172        spec->aa_mix_nid = 0x1a;
3173
3174        /* limit AA path volume to 0 dB */
3175        snd_hda_override_amp_caps(codec, 0x1A, HDA_INPUT,
3176                                  (0x17 << AC_AMPCAP_OFFSET_SHIFT) |
3177                                  (0x17 << AC_AMPCAP_NUM_STEPS_SHIFT) |
3178                                  (0x5 << AC_AMPCAP_STEP_SIZE_SHIFT) |
3179                                  (1 << AC_AMPCAP_MUTE_SHIFT));
3180
3181        /* automatic parse from the BIOS config */
3182        err = via_parse_auto_config(codec);
3183        if (err < 0) {
3184                via_free(codec);
3185                return err;
3186        }
3187
3188        spec->init_verbs[spec->num_iverbs++] = vt1702_init_verbs;
3189
3190        codec->patch_ops = via_patch_ops;
3191
3192        spec->set_widgets_power_state =  set_widgets_power_state_vt1702;
3193        return 0;
3194}
3195
3196/* Patch for VT1718S */
3197
3198static const struct hda_verb vt1718S_init_verbs[] = {
3199        /* Enable MW0 adjust Gain 5 */
3200        {0x1, 0xfb2, 0x10},
3201        /* Enable Boost Volume backdoor */
3202        {0x1, 0xf88, 0x8},
3203
3204        { }
3205};
3206
3207static void set_widgets_power_state_vt1718S(struct hda_codec *codec)
3208{
3209        struct via_spec *spec = codec->spec;
3210        int imux_is_smixer;
3211        unsigned int parm;
3212        /* MUX6 (1eh) = stereo mixer */
3213        imux_is_smixer =
3214        snd_hda_codec_read(codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 5;
3215        /* inputs */
3216        /* PW 5/6/7 (29h/2ah/2bh) */
3217        parm = AC_PWRST_D3;
3218        set_pin_power_state(codec, 0x29, &parm);
3219        set_pin_power_state(codec, 0x2a, &parm);
3220        set_pin_power_state(codec, 0x2b, &parm);
3221        if (imux_is_smixer)
3222                parm = AC_PWRST_D0;
3223        /* MUX6/7 (1eh/1fh), AIW 0/1 (10h/11h) */
3224        snd_hda_codec_write(codec, 0x1e, 0, AC_VERB_SET_POWER_STATE, parm);
3225        snd_hda_codec_write(codec, 0x1f, 0, AC_VERB_SET_POWER_STATE, parm);
3226        snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE, parm);
3227        snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE, parm);
3228
3229        /* outputs */
3230        /* PW3 (27h), MW2 (1ah), AOW3 (bh) */
3231        parm = AC_PWRST_D3;
3232        set_pin_power_state(codec, 0x27, &parm);
3233        snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_POWER_STATE, parm);
3234        snd_hda_codec_write(codec, 0xb, 0, AC_VERB_SET_POWER_STATE, parm);
3235
3236        /* PW2 (26h), AOW2 (ah) */
3237        parm = AC_PWRST_D3;
3238        set_pin_power_state(codec, 0x26, &parm);
3239        if (spec->smart51_enabled)
3240                set_pin_power_state(codec, 0x2b, &parm);
3241        snd_hda_codec_write(codec, 0xa, 0, AC_VERB_SET_POWER_STATE, parm);
3242
3243        /* PW0 (24h), AOW0 (8h) */
3244        parm = AC_PWRST_D3;
3245        set_pin_power_state(codec, 0x24, &parm);
3246        if (!spec->hp_independent_mode) /* check for redirected HP */
3247                set_pin_power_state(codec, 0x28, &parm);
3248        snd_hda_codec_write(codec, 0x8, 0, AC_VERB_SET_POWER_STATE, parm);
3249        /* MW9 (21h), Mw2 (1ah), AOW0 (8h) */
3250        snd_hda_codec_write(codec, 0x21, 0, AC_VERB_SET_POWER_STATE,
3251                            imux_is_smixer ? AC_PWRST_D0 : parm);
3252
3253        /* PW1 (25h), AOW1 (9h) */
3254        parm = AC_PWRST_D3;
3255        set_pin_power_state(codec, 0x25, &parm);
3256        if (spec->smart51_enabled)
3257                set_pin_power_state(codec, 0x2a, &parm);
3258        snd_hda_codec_write(codec, 0x9, 0, AC_VERB_SET_POWER_STATE, parm);
3259
3260        if (spec->hp_independent_mode) {
3261                /* PW4 (28h), MW3 (1bh), MUX1(34h), AOW4 (ch) */
3262                parm = AC_PWRST_D3;
3263                set_pin_power_state(codec, 0x28, &parm);
3264                snd_hda_codec_write(codec, 0x1b, 0,
3265                                    AC_VERB_SET_POWER_STATE, parm);
3266                snd_hda_codec_write(codec, 0x34, 0,
3267                                    AC_VERB_SET_POWER_STATE, parm);
3268                snd_hda_codec_write(codec, 0xc, 0,
3269                                    AC_VERB_SET_POWER_STATE, parm);
3270        }
3271}
3272
3273/* Add a connection to the primary DAC from AA-mixer for some codecs
3274 * This isn't listed from the raw info, but the chip has a secret connection.
3275 */
3276static int add_secret_dac_path(struct hda_codec *codec)
3277{
3278        struct via_spec *spec = codec->spec;
3279        int i, nums;
3280        hda_nid_t conn[8];
3281        hda_nid_t nid;
3282
3283        if (!spec->aa_mix_nid)
3284                return 0;
3285        nums = snd_hda_get_connections(codec, spec->aa_mix_nid, conn,
3286                                       ARRAY_SIZE(conn) - 1);
3287        for (i = 0; i < nums; i++) {
3288                if (get_wcaps_type(get_wcaps(codec, conn[i])) == AC_WID_AUD_OUT)
3289                        return 0;
3290        }
3291
3292        /* find the primary DAC and add to the connection list */
3293        nid = codec->start_nid;
3294        for (i = 0; i < codec->num_nodes; i++, nid++) {
3295                unsigned int caps = get_wcaps(codec, nid);
3296                if (get_wcaps_type(caps) == AC_WID_AUD_OUT &&
3297                    !(caps & AC_WCAP_DIGITAL)) {
3298                        conn[nums++] = nid;
3299                        return snd_hda_override_conn_list(codec,
3300                                                          spec->aa_mix_nid,
3301                                                          nums, conn);
3302                }
3303        }
3304        return 0;
3305}
3306
3307
3308static int patch_vt1718S(struct hda_codec *codec)
3309{
3310        struct via_spec *spec;
3311        int err;
3312
3313        /* create a codec specific record */
3314        spec = via_new_spec(codec);
3315        if (spec == NULL)
3316                return -ENOMEM;
3317
3318        spec->aa_mix_nid = 0x21;
3319        override_mic_boost(codec, 0x2b, 0, 3, 40);
3320        override_mic_boost(codec, 0x29, 0, 3, 40);
3321        add_secret_dac_path(codec);
3322
3323        /* automatic parse from the BIOS config */
3324        err = via_parse_auto_config(codec);
3325        if (err < 0) {
3326                via_free(codec);
3327                return err;
3328        }
3329
3330        spec->init_verbs[spec->num_iverbs++] = vt1718S_init_verbs;
3331
3332        codec->patch_ops = via_patch_ops;
3333
3334        spec->set_widgets_power_state =  set_widgets_power_state_vt1718S;
3335
3336        return 0;
3337}
3338
3339/* Patch for VT1716S */
3340
3341static int vt1716s_dmic_info(struct snd_kcontrol *kcontrol,
3342                            struct snd_ctl_elem_info *uinfo)
3343{
3344        uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3345        uinfo->count = 1;
3346        uinfo->value.integer.min = 0;
3347        uinfo->value.integer.max = 1;
3348        return 0;
3349}
3350
3351static int vt1716s_dmic_get(struct snd_kcontrol *kcontrol,
3352                           struct snd_ctl_elem_value *ucontrol)
3353{
3354        struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3355        int index = 0;
3356
3357        index = snd_hda_codec_read(codec, 0x26, 0,
3358                                               AC_VERB_GET_CONNECT_SEL, 0);
3359        if (index != -1)
3360                *ucontrol->value.integer.value = index;
3361
3362        return 0;
3363}
3364
3365static int vt1716s_dmic_put(struct snd_kcontrol *kcontrol,
3366                           struct snd_ctl_elem_value *ucontrol)
3367{
3368        struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3369        struct via_spec *spec = codec->spec;
3370        int index = *ucontrol->value.integer.value;
3371
3372        snd_hda_codec_write(codec, 0x26, 0,
3373                                               AC_VERB_SET_CONNECT_SEL, index);
3374        spec->dmic_enabled = index;
3375        set_widgets_power_state(codec);
3376        return 1;
3377}
3378
3379static const struct snd_kcontrol_new vt1716s_dmic_mixer[] = {
3380        HDA_CODEC_VOLUME("Digital Mic Capture Volume", 0x22, 0x0, HDA_INPUT),
3381        {
3382         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3383         .name = "Digital Mic Capture Switch",
3384         .subdevice = HDA_SUBDEV_NID_FLAG | 0x26,
3385         .count = 1,
3386         .info = vt1716s_dmic_info,
3387         .get = vt1716s_dmic_get,
3388         .put = vt1716s_dmic_put,
3389         },
3390        {}                      /* end */
3391};
3392
3393
3394/* mono-out mixer elements */
3395static const struct snd_kcontrol_new vt1716S_mono_out_mixer[] = {
3396        HDA_CODEC_MUTE("Mono Playback Switch", 0x2a, 0x0, HDA_OUTPUT),
3397        { } /* end */
3398};
3399
3400static const struct hda_verb vt1716S_init_verbs[] = {
3401        /* Enable Boost Volume backdoor */
3402        {0x1, 0xf8a, 0x80},
3403        /* don't bybass mixer */
3404        {0x1, 0xf88, 0xc0},
3405        /* Enable mono output */
3406        {0x1, 0xf90, 0x08},
3407        { }
3408};
3409
3410static void set_widgets_power_state_vt1716S(struct hda_codec *codec)
3411{
3412        struct via_spec *spec = codec->spec;
3413        int imux_is_smixer;
3414        unsigned int parm;
3415        unsigned int mono_out, present;
3416        /* SW0 (17h) = stereo mixer */
3417        imux_is_smixer =
3418        (snd_hda_codec_read(codec, 0x17, 0,
3419                            AC_VERB_GET_CONNECT_SEL, 0x00) ==  5);
3420        /* inputs */
3421        /* PW 1/2/5 (1ah/1bh/1eh) */
3422        parm = AC_PWRST_D3;
3423        set_pin_power_state(codec, 0x1a, &parm);
3424        set_pin_power_state(codec, 0x1b, &parm);
3425        set_pin_power_state(codec, 0x1e, &parm);
3426        if (imux_is_smixer)
3427                parm = AC_PWRST_D0;
3428        /* SW0 (17h), AIW0(13h) */
3429        snd_hda_codec_write(codec, 0x17, 0, AC_VERB_SET_POWER_STATE, parm);
3430        snd_hda_codec_write(codec, 0x13, 0, AC_VERB_SET_POWER_STATE, parm);
3431
3432        parm = AC_PWRST_D3;
3433        set_pin_power_state(codec, 0x1e, &parm);
3434        /* PW11 (22h) */
3435        if (spec->dmic_enabled)
3436                set_pin_power_state(codec, 0x22, &parm);
3437        else
3438                snd_hda_codec_write(codec, 0x22, 0,
3439                                    AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
3440
3441        /* SW2(26h), AIW1(14h) */
3442        snd_hda_codec_write(codec, 0x26, 0, AC_VERB_SET_POWER_STATE, parm);
3443        snd_hda_codec_write(codec, 0x14, 0, AC_VERB_SET_POWER_STATE, parm);
3444
3445        /* outputs */
3446        /* PW0 (19h), SW1 (18h), AOW1 (11h) */
3447        parm = AC_PWRST_D3;
3448        set_pin_power_state(codec, 0x19, &parm);
3449        /* Smart 5.1 PW2(1bh) */
3450        if (spec->smart51_enabled)
3451                set_pin_power_state(codec, 0x1b, &parm);
3452        snd_hda_codec_write(codec, 0x18, 0, AC_VERB_SET_POWER_STATE, parm);
3453        snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE, parm);
3454
3455        /* PW7 (23h), SW3 (27h), AOW3 (25h) */
3456        parm = AC_PWRST_D3;
3457        set_pin_power_state(codec, 0x23, &parm);
3458        /* Smart 5.1 PW1(1ah) */
3459        if (spec->smart51_enabled)
3460                set_pin_power_state(codec, 0x1a, &parm);
3461        snd_hda_codec_write(codec, 0x27, 0, AC_VERB_SET_POWER_STATE, parm);
3462
3463        /* Smart 5.1 PW5(1eh) */
3464        if (spec->smart51_enabled)
3465                set_pin_power_state(codec, 0x1e, &parm);
3466        snd_hda_codec_write(codec, 0x25, 0, AC_VERB_SET_POWER_STATE, parm);
3467
3468        /* Mono out */
3469        /* SW4(28h)->MW1(29h)-> PW12 (2ah)*/
3470        present = snd_hda_jack_detect(codec, 0x1c);
3471
3472        if (present)
3473                mono_out = 0;
3474        else {
3475                present = snd_hda_jack_detect(codec, 0x1d);
3476                if (!spec->hp_independent_mode && present)
3477                        mono_out = 0;
3478                else
3479                        mono_out = 1;
3480        }
3481        parm = mono_out ? AC_PWRST_D0 : AC_PWRST_D3;
3482        snd_hda_codec_write(codec, 0x28, 0, AC_VERB_SET_POWER_STATE, parm);
3483        snd_hda_codec_write(codec, 0x29, 0, AC_VERB_SET_POWER_STATE, parm);
3484        snd_hda_codec_write(codec, 0x2a, 0, AC_VERB_SET_POWER_STATE, parm);
3485
3486        /* PW 3/4 (1ch/1dh) */
3487        parm = AC_PWRST_D3;
3488        set_pin_power_state(codec, 0x1c, &parm);
3489        set_pin_power_state(codec, 0x1d, &parm);
3490        /* HP Independent Mode, power on AOW3 */
3491        if (spec->hp_independent_mode)
3492                snd_hda_codec_write(codec, 0x25, 0,
3493                                    AC_VERB_SET_POWER_STATE, parm);
3494
3495        /* force to D0 for internal Speaker */
3496        /* MW0 (16h), AOW0 (10h) */
3497        snd_hda_codec_write(codec, 0x16, 0, AC_VERB_SET_POWER_STATE,
3498                            imux_is_smixer ? AC_PWRST_D0 : parm);
3499        snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE,
3500                            mono_out ? AC_PWRST_D0 : parm);
3501}
3502
3503static int patch_vt1716S(struct hda_codec *codec)
3504{
3505        struct via_spec *spec;
3506        int err;
3507
3508        /* create a codec specific record */
3509        spec = via_new_spec(codec);
3510        if (spec == NULL)
3511                return -ENOMEM;
3512
3513        spec->aa_mix_nid = 0x16;
3514        override_mic_boost(codec, 0x1a, 0, 3, 40);
3515        override_mic_boost(codec, 0x1e, 0, 3, 40);
3516
3517        /* automatic parse from the BIOS config */
3518        err = via_parse_auto_config(codec);
3519        if (err < 0) {
3520                via_free(codec);
3521                return err;
3522        }
3523
3524        spec->init_verbs[spec->num_iverbs++]  = vt1716S_init_verbs;
3525
3526        spec->mixers[spec->num_mixers] = vt1716s_dmic_mixer;
3527        spec->num_mixers++;
3528
3529        spec->mixers[spec->num_mixers++] = vt1716S_mono_out_mixer;
3530
3531        codec->patch_ops = via_patch_ops;
3532
3533        spec->set_widgets_power_state = set_widgets_power_state_vt1716S;
3534        return 0;
3535}
3536
3537/* for vt2002P */
3538
3539static const struct hda_verb vt2002P_init_verbs[] = {
3540        /* Class-D speaker related verbs */
3541        {0x1, 0xfe0, 0x4},
3542        {0x1, 0xfe9, 0x80},
3543        {0x1, 0xfe2, 0x22},
3544        /* Enable Boost Volume backdoor */
3545        {0x1, 0xfb9, 0x24},
3546        /* Enable AOW0 to MW9 */
3547        {0x1, 0xfb8, 0x88},
3548        { }
3549};
3550
3551static const struct hda_verb vt1802_init_verbs[] = {
3552        /* Enable Boost Volume backdoor */
3553        {0x1, 0xfb9, 0x24},
3554        /* Enable AOW0 to MW9 */
3555        {0x1, 0xfb8, 0x88},
3556        { }
3557};
3558
3559static void set_widgets_power_state_vt2002P(struct hda_codec *codec)
3560{
3561        struct via_spec *spec = codec->spec;
3562        int imux_is_smixer;
3563        unsigned int parm;
3564        unsigned int present;
3565        /* MUX9 (1eh) = stereo mixer */
3566        imux_is_smixer =
3567        snd_hda_codec_read(codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
3568        /* inputs */
3569        /* PW 5/6/7 (29h/2ah/2bh) */
3570        parm = AC_PWRST_D3;
3571        set_pin_power_state(codec, 0x29, &parm);
3572        set_pin_power_state(codec, 0x2a, &parm);
3573        set_pin_power_state(codec, 0x2b, &parm);
3574        parm = AC_PWRST_D0;
3575        /* MUX9/10 (1eh/1fh), AIW 0/1 (10h/11h) */
3576        snd_hda_codec_write(codec, 0x1e, 0, AC_VERB_SET_POWER_STATE, parm);
3577        snd_hda_codec_write(codec, 0x1f, 0, AC_VERB_SET_POWER_STATE, parm);
3578        snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE, parm);
3579        snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE, parm);
3580
3581        /* outputs */
3582        /* AOW0 (8h)*/
3583        snd_hda_codec_write(codec, 0x8, 0, AC_VERB_SET_POWER_STATE, parm);
3584
3585        if (spec->codec_type == VT1802) {
3586                /* PW4 (28h), MW4 (18h), MUX4(38h) */
3587                parm = AC_PWRST_D3;
3588                set_pin_power_state(codec, 0x28, &parm);
3589                snd_hda_codec_write(codec, 0x18, 0,
3590                                    AC_VERB_SET_POWER_STATE, parm);
3591                snd_hda_codec_write(codec, 0x38, 0,
3592                                    AC_VERB_SET_POWER_STATE, parm);
3593        } else {
3594                /* PW4 (26h), MW4 (1ch), MUX4(37h) */
3595                parm = AC_PWRST_D3;
3596                set_pin_power_state(codec, 0x26, &parm);
3597                snd_hda_codec_write(codec, 0x1c, 0,
3598                                    AC_VERB_SET_POWER_STATE, parm);
3599                snd_hda_codec_write(codec, 0x37, 0,
3600                                    AC_VERB_SET_POWER_STATE, parm);
3601        }
3602
3603        if (spec->codec_type == VT1802) {
3604                /* PW1 (25h), MW1 (15h), MUX1(35h), AOW1 (9h) */
3605                parm = AC_PWRST_D3;
3606                set_pin_power_state(codec, 0x25, &parm);
3607                snd_hda_codec_write(codec, 0x15, 0,
3608                                    AC_VERB_SET_POWER_STATE, parm);
3609                snd_hda_codec_write(codec, 0x35, 0,
3610                                    AC_VERB_SET_POWER_STATE, parm);
3611        } else {
3612                /* PW1 (25h), MW1 (19h), MUX1(35h), AOW1 (9h) */
3613                parm = AC_PWRST_D3;
3614                set_pin_power_state(codec, 0x25, &parm);
3615                snd_hda_codec_write(codec, 0x19, 0,
3616                                    AC_VERB_SET_POWER_STATE, parm);
3617                snd_hda_codec_write(codec, 0x35, 0,
3618                                    AC_VERB_SET_POWER_STATE, parm);
3619        }
3620
3621        if (spec->hp_independent_mode)
3622                snd_hda_codec_write(codec, 0x9, 0,
3623                                    AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
3624
3625        /* Class-D */
3626        /* PW0 (24h), MW0(18h/14h), MUX0(34h) */
3627        present = snd_hda_jack_detect(codec, 0x25);
3628
3629        parm = AC_PWRST_D3;
3630        set_pin_power_state(codec, 0x24, &parm);
3631        parm = present ? AC_PWRST_D3 : AC_PWRST_D0;
3632        if (spec->codec_type == VT1802)
3633                snd_hda_codec_write(codec, 0x14, 0,
3634                                    AC_VERB_SET_POWER_STATE, parm);
3635        else
3636                snd_hda_codec_write(codec, 0x18, 0,
3637                                    AC_VERB_SET_POWER_STATE, parm);
3638        snd_hda_codec_write(codec, 0x34, 0, AC_VERB_SET_POWER_STATE, parm);
3639
3640        /* Mono Out */
3641        present = snd_hda_jack_detect(codec, 0x26);
3642
3643        parm = present ? AC_PWRST_D3 : AC_PWRST_D0;
3644        if (spec->codec_type == VT1802) {
3645                /* PW15 (33h), MW8(1ch), MUX8(3ch) */
3646                snd_hda_codec_write(codec, 0x33, 0,
3647                                    AC_VERB_SET_POWER_STATE, parm);
3648                snd_hda_codec_write(codec, 0x1c, 0,
3649                                    AC_VERB_SET_POWER_STATE, parm);
3650                snd_hda_codec_write(codec, 0x3c, 0,
3651                                    AC_VERB_SET_POWER_STATE, parm);
3652        } else {
3653                /* PW15 (31h), MW8(17h), MUX8(3bh) */
3654                snd_hda_codec_write(codec, 0x31, 0,
3655                                    AC_VERB_SET_POWER_STATE, parm);
3656                snd_hda_codec_write(codec, 0x17, 0,
3657                                    AC_VERB_SET_POWER_STATE, parm);
3658                snd_hda_codec_write(codec, 0x3b, 0,
3659                                    AC_VERB_SET_POWER_STATE, parm);
3660        }
3661        /* MW9 (21h) */
3662        if (imux_is_smixer || !is_aa_path_mute(codec))
3663                snd_hda_codec_write(codec, 0x21, 0,
3664                                    AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
3665        else
3666                snd_hda_codec_write(codec, 0x21, 0,
3667                                    AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
3668}
3669
3670/* patch for vt2002P */
3671static int patch_vt2002P(struct hda_codec *codec)
3672{
3673        struct via_spec *spec;
3674        int err;
3675
3676        /* create a codec specific record */
3677        spec = via_new_spec(codec);
3678        if (spec == NULL)
3679                return -ENOMEM;
3680
3681        spec->aa_mix_nid = 0x21;
3682        override_mic_boost(codec, 0x2b, 0, 3, 40);
3683        override_mic_boost(codec, 0x29, 0, 3, 40);
3684        add_secret_dac_path(codec);
3685
3686        /* automatic parse from the BIOS config */
3687        err = via_parse_auto_config(codec);
3688        if (err < 0) {
3689                via_free(codec);
3690                return err;
3691        }
3692
3693        if (spec->codec_type == VT1802)
3694                spec->init_verbs[spec->num_iverbs++] = vt1802_init_verbs;
3695        else
3696                spec->init_verbs[spec->num_iverbs++] = vt2002P_init_verbs;
3697
3698        codec->patch_ops = via_patch_ops;
3699
3700        spec->set_widgets_power_state =  set_widgets_power_state_vt2002P;
3701        return 0;
3702}
3703
3704/* for vt1812 */
3705
3706static const struct hda_verb vt1812_init_verbs[] = {
3707        /* Enable Boost Volume backdoor */
3708        {0x1, 0xfb9, 0x24},
3709        /* Enable AOW0 to MW9 */
3710        {0x1, 0xfb8, 0xa8},
3711        { }
3712};
3713
3714static void set_widgets_power_state_vt1812(struct hda_codec *codec)
3715{
3716        struct via_spec *spec = codec->spec;
3717        unsigned int parm;
3718        unsigned int present;
3719        /* inputs */
3720        /* PW 5/6/7 (29h/2ah/2bh) */
3721        parm = AC_PWRST_D3;
3722        set_pin_power_state(codec, 0x29, &parm);
3723        set_pin_power_state(codec, 0x2a, &parm);
3724        set_pin_power_state(codec, 0x2b, &parm);
3725        parm = AC_PWRST_D0;
3726        /* MUX10/11 (1eh/1fh), AIW 0/1 (10h/11h) */
3727        snd_hda_codec_write(codec, 0x1e, 0, AC_VERB_SET_POWER_STATE, parm);
3728        snd_hda_codec_write(codec, 0x1f, 0, AC_VERB_SET_POWER_STATE, parm);
3729        snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE, parm);
3730        snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE, parm);
3731
3732        /* outputs */
3733        /* AOW0 (8h)*/
3734        snd_hda_codec_write(codec, 0x8, 0,
3735                            AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
3736
3737        /* PW4 (28h), MW4 (18h), MUX4(38h) */
3738        parm = AC_PWRST_D3;
3739        set_pin_power_state(codec, 0x28, &parm);
3740        snd_hda_codec_write(codec, 0x18, 0, AC_VERB_SET_POWER_STATE, parm);
3741        snd_hda_codec_write(codec, 0x38, 0, AC_VERB_SET_POWER_STATE, parm);
3742
3743        /* PW1 (25h), MW1 (15h), MUX1(35h), AOW1 (9h) */
3744        parm = AC_PWRST_D3;
3745        set_pin_power_state(codec, 0x25, &parm);
3746        snd_hda_codec_write(codec, 0x15, 0, AC_VERB_SET_POWER_STATE, parm);
3747        snd_hda_codec_write(codec, 0x35, 0, AC_VERB_SET_POWER_STATE, parm);
3748        if (spec->hp_independent_mode)
3749                snd_hda_codec_write(codec, 0x9, 0,
3750                                    AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
3751
3752        /* Internal Speaker */
3753        /* PW0 (24h), MW0(14h), MUX0(34h) */
3754        present = snd_hda_jack_detect(codec, 0x25);
3755
3756        parm = AC_PWRST_D3;
3757        set_pin_power_state(codec, 0x24, &parm);
3758        if (present) {
3759                snd_hda_codec_write(codec, 0x14, 0,
3760                                    AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
3761                snd_hda_codec_write(codec, 0x34, 0,
3762                                    AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
3763        } else {
3764                snd_hda_codec_write(codec, 0x14, 0,
3765                                    AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
3766                snd_hda_codec_write(codec, 0x34, 0,
3767                                    AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
3768        }
3769
3770
3771        /* Mono Out */
3772        /* PW13 (31h), MW13(1ch), MUX13(3ch), MW14(3eh) */
3773        present = snd_hda_jack_detect(codec, 0x28);
3774
3775        parm = AC_PWRST_D3;
3776        set_pin_power_state(codec, 0x31, &parm);
3777        if (present) {
3778                snd_hda_codec_write(codec, 0x1c, 0,
3779                                    AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
3780                snd_hda_codec_write(codec, 0x3c, 0,
3781                                    AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
3782                snd_hda_codec_write(codec, 0x3e, 0,
3783                                    AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
3784        } else {
3785                snd_hda_codec_write(codec, 0x1c, 0,
3786                                    AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
3787                snd_hda_codec_write(codec, 0x3c, 0,
3788                                    AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
3789                snd_hda_codec_write(codec, 0x3e, 0,
3790                                    AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
3791        }
3792
3793        /* PW15 (33h), MW15 (1dh), MUX15(3dh) */
3794        parm = AC_PWRST_D3;
3795        set_pin_power_state(codec, 0x33, &parm);
3796        snd_hda_codec_write(codec, 0x1d, 0, AC_VERB_SET_POWER_STATE, parm);
3797        snd_hda_codec_write(codec, 0x3d, 0, AC_VERB_SET_POWER_STATE, parm);
3798
3799}
3800
3801/* patch for vt1812 */
3802static int patch_vt1812(struct hda_codec *codec)
3803{
3804        struct via_spec *spec;
3805        int err;
3806
3807        /* create a codec specific record */
3808        spec = via_new_spec(codec);
3809        if (spec == NULL)
3810                return -ENOMEM;
3811
3812        spec->aa_mix_nid = 0x21;
3813        override_mic_boost(codec, 0x2b, 0, 3, 40);
3814        override_mic_boost(codec, 0x29, 0, 3, 40);
3815        add_secret_dac_path(codec);
3816
3817        /* automatic parse from the BIOS config */
3818        err = via_parse_auto_config(codec);
3819        if (err < 0) {
3820                via_free(codec);
3821                return err;
3822        }
3823
3824        spec->init_verbs[spec->num_iverbs++]  = vt1812_init_verbs;
3825
3826        codec->patch_ops = via_patch_ops;
3827
3828        spec->set_widgets_power_state =  set_widgets_power_state_vt1812;
3829        return 0;
3830}
3831
3832/*
3833 * patch entries
3834 */
3835static const struct hda_codec_preset snd_hda_preset_via[] = {
3836        { .id = 0x11061708, .name = "VT1708", .patch = patch_vt1708},
3837        { .id = 0x11061709, .name = "VT1708", .patch = patch_vt1708},
3838        { .id = 0x1106170a, .name = "VT1708", .patch = patch_vt1708},
3839        { .id = 0x1106170b, .name = "VT1708", .patch = patch_vt1708},
3840        { .id = 0x1106e710, .name = "VT1709 10-Ch",
3841          .patch = patch_vt1709},
3842        { .id = 0x1106e711, .name = "VT1709 10-Ch",
3843          .patch = patch_vt1709},
3844        { .id = 0x1106e712, .name = "VT1709 10-Ch",
3845          .patch = patch_vt1709},
3846        { .id = 0x1106e713, .name = "VT1709 10-Ch",
3847          .patch = patch_vt1709},
3848        { .id = 0x1106e714, .name = "VT1709 6-Ch",
3849          .patch = patch_vt1709},
3850        { .id = 0x1106e715, .name = "VT1709 6-Ch",
3851          .patch = patch_vt1709},
3852        { .id = 0x1106e716, .name = "VT1709 6-Ch",
3853          .patch = patch_vt1709},
3854        { .id = 0x1106e717, .name = "VT1709 6-Ch",
3855          .patch = patch_vt1709},
3856        { .id = 0x1106e720, .name = "VT1708B 8-Ch",
3857          .patch = patch_vt1708B},
3858        { .id = 0x1106e721, .name = "VT1708B 8-Ch",
3859          .patch = patch_vt1708B},
3860        { .id = 0x1106e722, .name = "VT1708B 8-Ch",
3861          .patch = patch_vt1708B},
3862        { .id = 0x1106e723, .name = "VT1708B 8-Ch",
3863          .patch = patch_vt1708B},
3864        { .id = 0x1106e724, .name = "VT1708B 4-Ch",
3865          .patch = patch_vt1708B},
3866        { .id = 0x1106e725, .name = "VT1708B 4-Ch",
3867          .patch = patch_vt1708B},
3868        { .id = 0x1106e726, .name = "VT1708B 4-Ch",
3869          .patch = patch_vt1708B},
3870        { .id = 0x1106e727, .name = "VT1708B 4-Ch",
3871          .patch = patch_vt1708B},
3872        { .id = 0x11060397, .name = "VT1708S",
3873          .patch = patch_vt1708S},
3874        { .id = 0x11061397, .name = "VT1708S",
3875          .patch = patch_vt1708S},
3876        { .id = 0x11062397, .name = "VT1708S",
3877          .patch = patch_vt1708S},
3878        { .id = 0x11063397, .name = "VT1708S",
3879          .patch = patch_vt1708S},
3880        { .id = 0x11064397, .name = "VT1705",
3881          .patch = patch_vt1708S},
3882        { .id = 0x11065397, .name = "VT1708S",
3883          .patch = patch_vt1708S},
3884        { .id = 0x11066397, .name = "VT1708S",
3885          .patch = patch_vt1708S},
3886        { .id = 0x11067397, .name = "VT1708S",
3887          .patch = patch_vt1708S},
3888        { .id = 0x11060398, .name = "VT1702",
3889          .patch = patch_vt1702},
3890        { .id = 0x11061398, .name = "VT1702",
3891          .patch = patch_vt1702},
3892        { .id = 0x11062398, .name = "VT1702",
3893          .patch = patch_vt1702},
3894        { .id = 0x11063398, .name = "VT1702",
3895          .patch = patch_vt1702},
3896        { .id = 0x11064398, .name = "VT1702",
3897          .patch = patch_vt1702},
3898        { .id = 0x11065398, .name = "VT1702",
3899          .patch = patch_vt1702},
3900        { .id = 0x11066398, .name = "VT1702",
3901          .patch = patch_vt1702},
3902        { .id = 0x11067398, .name = "VT1702",
3903          .patch = patch_vt1702},
3904        { .id = 0x11060428, .name = "VT1718S",
3905          .patch = patch_vt1718S},
3906        { .id = 0x11064428, .name = "VT1718S",
3907          .patch = patch_vt1718S},
3908        { .id = 0x11060441, .name = "VT2020",
3909          .patch = patch_vt1718S},
3910        { .id = 0x11064441, .name = "VT1828S",
3911          .patch = patch_vt1718S},
3912        { .id = 0x11060433, .name = "VT1716S",
3913          .patch = patch_vt1716S},
3914        { .id = 0x1106a721, .name = "VT1716S",
3915          .patch = patch_vt1716S},
3916        { .id = 0x11060438, .name = "VT2002P", .patch = patch_vt2002P},
3917        { .id = 0x11064438, .name = "VT2002P", .patch = patch_vt2002P},
3918        { .id = 0x11060448, .name = "VT1812", .patch = patch_vt1812},
3919        { .id = 0x11060440, .name = "VT1818S",
3920          .patch = patch_vt1708S},
3921        { .id = 0x11060446, .name = "VT1802",
3922                .patch = patch_vt2002P},
3923        { .id = 0x11068446, .name = "VT1802",
3924                .patch = patch_vt2002P},
3925        {} /* terminator */
3926};
3927
3928MODULE_ALIAS("snd-hda-codec-id:1106*");
3929
3930static struct hda_codec_preset_list via_list = {
3931        .preset = snd_hda_preset_via,
3932        .owner = THIS_MODULE,
3933};
3934
3935MODULE_LICENSE("GPL");
3936MODULE_DESCRIPTION("VIA HD-audio codec");
3937
3938static int __init patch_via_init(void)
3939{
3940        return snd_hda_add_codec_preset(&via_list);
3941}
3942
3943static void __exit patch_via_exit(void)
3944{
3945        snd_hda_delete_codec_preset(&via_list);
3946}
3947
3948module_init(patch_via_init)
3949module_exit(patch_via_exit)
3950
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.