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