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