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 VT1702/VT1708/VT1709 codec
   5 *
   6 * Copyright (c) 2006-2008 Lydia Wang <lydiawang@viatech.com>
   7 *                         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/*                                                                           */
  40/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  41
  42
  43#include <linux/init.h>
  44#include <linux/delay.h>
  45#include <linux/slab.h>
  46#include <sound/core.h>
  47#include <sound/asoundef.h>
  48#include "hda_codec.h"
  49#include "hda_local.h"
  50
  51/* amp values */
  52#define AMP_VAL_IDX_SHIFT       19
  53#define AMP_VAL_IDX_MASK        (0x0f<<19)
  54
  55/* Pin Widget NID */
  56#define VT1708_HP_NID           0x13
  57#define VT1708_DIGOUT_NID       0x14
  58#define VT1708_DIGIN_NID        0x16
  59#define VT1708_DIGIN_PIN        0x26
  60#define VT1708_HP_PIN_NID       0x20
  61#define VT1708_CD_PIN_NID       0x24
  62
  63#define VT1709_HP_DAC_NID       0x28
  64#define VT1709_DIGOUT_NID       0x13
  65#define VT1709_DIGIN_NID        0x17
  66#define VT1709_DIGIN_PIN        0x25
  67
  68#define VT1708B_HP_NID          0x25
  69#define VT1708B_DIGOUT_NID      0x12
  70#define VT1708B_DIGIN_NID       0x15
  71#define VT1708B_DIGIN_PIN       0x21
  72
  73#define VT1708S_HP_NID          0x25
  74#define VT1708S_DIGOUT_NID      0x12
  75
  76#define VT1702_HP_NID           0x17
  77#define VT1702_DIGOUT_NID       0x11
  78
  79#define IS_VT1708_VENDORID(x)           ((x) >= 0x11061708 && (x) <= 0x1106170b)
  80#define IS_VT1709_10CH_VENDORID(x)      ((x) >= 0x1106e710 && (x) <= 0x1106e713)
  81#define IS_VT1709_6CH_VENDORID(x)       ((x) >= 0x1106e714 && (x) <= 0x1106e717)
  82#define IS_VT1708B_8CH_VENDORID(x)      ((x) >= 0x1106e720 && (x) <= 0x1106e723)
  83#define IS_VT1708B_4CH_VENDORID(x)      ((x) >= 0x1106e724 && (x) <= 0x1106e727)
  84#define IS_VT1708S_VENDORID(x)          ((x) >= 0x11060397 && (x) <= 0x11067397)
  85#define IS_VT1702_VENDORID(x)           ((x) >= 0x11060398 && (x) <= 0x11067398)
  86
  87enum VIA_HDA_CODEC {
  88        UNKNOWN = -1,
  89        VT1708,
  90        VT1709_10CH,
  91        VT1709_6CH,
  92        VT1708B_8CH,
  93        VT1708B_4CH,
  94        VT1708S,
  95        VT1702,
  96        CODEC_TYPES,
  97};
  98
  99static enum VIA_HDA_CODEC get_codec_type(u32 vendor_id)
 100{
 101        u16 ven_id = vendor_id >> 16;
 102        u16 dev_id = vendor_id & 0xffff;
 103        enum VIA_HDA_CODEC codec_type;
 104
 105        /* get codec type */
 106        if (ven_id != 0x1106)
 107                codec_type = UNKNOWN;
 108        else if (dev_id >= 0x1708 && dev_id <= 0x170b)
 109                codec_type = VT1708;
 110        else if (dev_id >= 0xe710 && dev_id <= 0xe713)
 111                codec_type = VT1709_10CH;
 112        else if (dev_id >= 0xe714 && dev_id <= 0xe717)
 113                codec_type = VT1709_6CH;
 114        else if (dev_id >= 0xe720 && dev_id <= 0xe723)
 115                codec_type = VT1708B_8CH;
 116        else if (dev_id >= 0xe724 && dev_id <= 0xe727)
 117                codec_type = VT1708B_4CH;
 118        else if ((dev_id & 0xfff) == 0x397
 119                 && (dev_id >> 12) < 8)
 120                codec_type = VT1708S;
 121        else if ((dev_id & 0xfff) == 0x398
 122                 && (dev_id >> 12) < 8)
 123                codec_type = VT1702;
 124        else
 125                codec_type = UNKNOWN;
 126        return codec_type;
 127};
 128
 129#define VIA_HP_EVENT            0x01
 130#define VIA_GPIO_EVENT          0x02
 131
 132enum {
 133        VIA_CTL_WIDGET_VOL,
 134        VIA_CTL_WIDGET_MUTE,
 135};
 136
 137enum {
 138        AUTO_SEQ_FRONT = 0,
 139        AUTO_SEQ_SURROUND,
 140        AUTO_SEQ_CENLFE,
 141        AUTO_SEQ_SIDE
 142};
 143
 144/* Some VT1708S based boards gets the micboost setting wrong, so we have
 145 * to apply some brute-force and re-write the TLV's by software. */
 146static int mic_boost_tlv(struct snd_kcontrol *kcontrol, int op_flag,
 147                         unsigned int size, unsigned int __user *_tlv)
 148{
 149        struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
 150        hda_nid_t nid = get_amp_nid(kcontrol);
 151
 152        if (get_codec_type(codec->vendor_id) == VT1708S
 153            && (nid == 0x1a || nid == 0x1e)) {
 154                if (size < 4 * sizeof(unsigned int))
 155                        return -ENOMEM;
 156                if (put_user(1, _tlv))  /* SNDRV_CTL_TLVT_DB_SCALE */
 157                        return -EFAULT;
 158                if (put_user(2 * sizeof(unsigned int), _tlv + 1))
 159                        return -EFAULT;
 160                if (put_user(0, _tlv + 2)) /* offset = 0 */
 161                        return -EFAULT;
 162                if (put_user(1000, _tlv + 3)) /* step size = 10 dB */
 163                        return -EFAULT;
 164        }
 165        return 0;
 166}
 167
 168static int mic_boost_volume_info(struct snd_kcontrol *kcontrol,
 169                                 struct snd_ctl_elem_info *uinfo)
 170{
 171        struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
 172        hda_nid_t nid = get_amp_nid(kcontrol);
 173
 174        if (get_codec_type(codec->vendor_id) == VT1708S
 175            && (nid == 0x1a || nid == 0x1e)) {
 176                uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
 177                uinfo->count = 2;
 178                uinfo->value.integer.min = 0;
 179                uinfo->value.integer.max = 3;
 180        }
 181        return 0;
 182}
 183
 184static struct snd_kcontrol_new vt1708_control_templates[] = {
 185        HDA_CODEC_VOLUME(NULL, 0, 0, 0),
 186        HDA_CODEC_MUTE(NULL, 0, 0, 0),
 187};
 188
 189
 190struct via_spec {
 191        /* codec parameterization */
 192        struct snd_kcontrol_new *mixers[3];
 193        unsigned int num_mixers;
 194
 195        struct hda_verb *init_verbs[5];
 196        unsigned int num_iverbs;
 197
 198        char *stream_name_analog;
 199        struct hda_pcm_stream *stream_analog_playback;
 200        struct hda_pcm_stream *stream_analog_capture;
 201
 202        char *stream_name_digital;
 203        struct hda_pcm_stream *stream_digital_playback;
 204        struct hda_pcm_stream *stream_digital_capture;
 205
 206        /* playback */
 207        struct hda_multi_out multiout;
 208        hda_nid_t slave_dig_outs[2];
 209
 210        /* capture */
 211        unsigned int num_adc_nids;
 212        hda_nid_t *adc_nids;
 213        hda_nid_t mux_nids[3];
 214        hda_nid_t dig_in_nid;
 215        hda_nid_t dig_in_pin;
 216
 217        /* capture source */
 218        const struct hda_input_mux *input_mux;
 219        unsigned int cur_mux[3];
 220
 221        /* PCM information */
 222        struct hda_pcm pcm_rec[3];
 223
 224        /* dynamic controls, init_verbs and input_mux */
 225        struct auto_pin_cfg autocfg;
 226        struct snd_array kctls;
 227        struct hda_input_mux private_imux[2];
 228        hda_nid_t private_dac_nids[AUTO_CFG_MAX_OUTS];
 229
 230        /* HP mode source */
 231        const struct hda_input_mux *hp_mux;
 232        unsigned int hp_independent_mode;
 233
 234#ifdef CONFIG_SND_HDA_POWER_SAVE
 235        struct hda_loopback_check loopback;
 236#endif
 237};
 238
 239static hda_nid_t vt1708_adc_nids[2] = {
 240        /* ADC1-2 */
 241        0x15, 0x27
 242};
 243
 244static hda_nid_t vt1709_adc_nids[3] = {
 245        /* ADC1-2 */
 246        0x14, 0x15, 0x16
 247};
 248
 249static hda_nid_t vt1708B_adc_nids[2] = {
 250        /* ADC1-2 */
 251        0x13, 0x14
 252};
 253
 254static hda_nid_t vt1708S_adc_nids[2] = {
 255        /* ADC1-2 */
 256        0x13, 0x14
 257};
 258
 259static hda_nid_t vt1702_adc_nids[3] = {
 260        /* ADC1-2 */
 261        0x12, 0x20, 0x1F
 262};
 263
 264/* add dynamic controls */
 265static int via_add_control(struct via_spec *spec, int type, const char *name,
 266                           unsigned long val)
 267{
 268        struct snd_kcontrol_new *knew;
 269
 270        snd_array_init(&spec->kctls, sizeof(*knew), 32);
 271        knew = snd_array_new(&spec->kctls);
 272        if (!knew)
 273                return -ENOMEM;
 274        *knew = vt1708_control_templates[type];
 275        knew->name = kstrdup(name, GFP_KERNEL);
 276        if (!knew->name)
 277                return -ENOMEM;
 278        knew->private_value = val;
 279        return 0;
 280}
 281
 282static void via_free_kctls(struct hda_codec *codec)
 283{
 284        struct via_spec *spec = codec->spec;
 285
 286        if (spec->kctls.list) {
 287                struct snd_kcontrol_new *kctl = spec->kctls.list;
 288                int i;
 289                for (i = 0; i < spec->kctls.used; i++)
 290                        kfree(kctl[i].name);
 291        }
 292        snd_array_free(&spec->kctls);
 293}
 294
 295/* create input playback/capture controls for the given pin */
 296static int via_new_analog_input(struct via_spec *spec, hda_nid_t pin,
 297                                const char *ctlname, int idx, int mix_nid)
 298{
 299        char name[32];
 300        int err;
 301
 302        sprintf(name, "%s Playback Volume", ctlname);
 303        err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
 304                              HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
 305        if (err < 0)
 306                return err;
 307        sprintf(name, "%s Playback Switch", ctlname);
 308        err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
 309                              HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
 310        if (err < 0)
 311                return err;
 312        return 0;
 313}
 314
 315static void via_auto_set_output_and_unmute(struct hda_codec *codec,
 316                                           hda_nid_t nid, int pin_type,
 317                                           int dac_idx)
 318{
 319        /* set as output */
 320        snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
 321                            pin_type);
 322        snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
 323                            AMP_OUT_UNMUTE);
 324        if (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)
 325                snd_hda_codec_write(codec, nid, 0, 
 326                                    AC_VERB_SET_EAPD_BTLENABLE, 0x02);
 327}
 328
 329
 330static void via_auto_init_multi_out(struct hda_codec *codec)
 331{
 332        struct via_spec *spec = codec->spec;
 333        int i;
 334
 335        for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
 336                hda_nid_t nid = spec->autocfg.line_out_pins[i];
 337                if (nid)
 338                        via_auto_set_output_and_unmute(codec, nid, PIN_OUT, i);
 339        }
 340}
 341
 342static void via_auto_init_hp_out(struct hda_codec *codec)
 343{
 344        struct via_spec *spec = codec->spec;
 345        hda_nid_t pin;
 346
 347        pin = spec->autocfg.hp_pins[0];
 348        if (pin) /* connect to front */
 349                via_auto_set_output_and_unmute(codec, pin, PIN_HP, 0);
 350}
 351
 352static void via_auto_init_analog_input(struct hda_codec *codec)
 353{
 354        struct via_spec *spec = codec->spec;
 355        int i;
 356
 357        for (i = 0; i < AUTO_PIN_LAST; i++) {
 358                hda_nid_t nid = spec->autocfg.input_pins[i];
 359
 360                snd_hda_codec_write(codec, nid, 0,
 361                                    AC_VERB_SET_PIN_WIDGET_CONTROL,
 362                                    (i <= AUTO_PIN_FRONT_MIC ?
 363                                     PIN_VREF50 : PIN_IN));
 364
 365        }
 366}
 367/*
 368 * input MUX handling
 369 */
 370static int via_mux_enum_info(struct snd_kcontrol *kcontrol,
 371                             struct snd_ctl_elem_info *uinfo)
 372{
 373        struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
 374        struct via_spec *spec = codec->spec;
 375        return snd_hda_input_mux_info(spec->input_mux, uinfo);
 376}
 377
 378static int via_mux_enum_get(struct snd_kcontrol *kcontrol,
 379                            struct snd_ctl_elem_value *ucontrol)
 380{
 381        struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
 382        struct via_spec *spec = codec->spec;
 383        unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
 384
 385        ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
 386        return 0;
 387}
 388
 389static int via_mux_enum_put(struct snd_kcontrol *kcontrol,
 390                            struct snd_ctl_elem_value *ucontrol)
 391{
 392        struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
 393        struct via_spec *spec = codec->spec;
 394        unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
 395
 396        if (!spec->mux_nids[adc_idx])
 397                return -EINVAL;
 398        return snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
 399                                     spec->mux_nids[adc_idx],
 400                                     &spec->cur_mux[adc_idx]);
 401}
 402
 403static int via_independent_hp_info(struct snd_kcontrol *kcontrol,
 404                                   struct snd_ctl_elem_info *uinfo)
 405{
 406        struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
 407        struct via_spec *spec = codec->spec;
 408        return snd_hda_input_mux_info(spec->hp_mux, uinfo);
 409}
 410
 411static int via_independent_hp_get(struct snd_kcontrol *kcontrol,
 412                                  struct snd_ctl_elem_value *ucontrol)
 413{
 414        struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
 415        struct via_spec *spec = codec->spec;
 416        hda_nid_t nid = spec->autocfg.hp_pins[0];
 417        unsigned int pinsel = snd_hda_codec_read(codec, nid, 0,
 418                                                 AC_VERB_GET_CONNECT_SEL,
 419                                                 0x00);
 420
 421        ucontrol->value.enumerated.item[0] = pinsel;
 422
 423        return 0;
 424}
 425
 426static int via_independent_hp_put(struct snd_kcontrol *kcontrol,
 427                                  struct snd_ctl_elem_value *ucontrol)
 428{
 429        struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
 430        struct via_spec *spec = codec->spec;
 431        hda_nid_t nid = spec->autocfg.hp_pins[0];
 432        unsigned int pinsel = ucontrol->value.enumerated.item[0];
 433        unsigned int con_nid = snd_hda_codec_read(codec, nid, 0,
 434                                         AC_VERB_GET_CONNECT_LIST, 0) & 0xff;
 435
 436        if (con_nid == spec->multiout.hp_nid) {
 437                if (pinsel == 0) {
 438                        if (!spec->hp_independent_mode) {
 439                                if (spec->multiout.num_dacs > 1)
 440                                        spec->multiout.num_dacs -= 1;
 441                                spec->hp_independent_mode = 1;
 442                        }
 443                } else if (pinsel == 1) {
 444                       if (spec->hp_independent_mode) {
 445                                if (spec->multiout.num_dacs > 1)
 446                                        spec->multiout.num_dacs += 1;
 447                                spec->hp_independent_mode = 0;
 448                       }
 449                }
 450        } else {
 451                if (pinsel == 0) {
 452                        if (spec->hp_independent_mode) {
 453                                if (spec->multiout.num_dacs > 1)
 454                                        spec->multiout.num_dacs += 1;
 455                                spec->hp_independent_mode = 0;
 456                        }
 457                } else if (pinsel == 1) {
 458                       if (!spec->hp_independent_mode) {
 459                                if (spec->multiout.num_dacs > 1)
 460                                        spec->multiout.num_dacs -= 1;
 461                                spec->hp_independent_mode = 1;
 462                       }
 463                }
 464        }
 465        snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
 466                            pinsel);
 467
 468        if (spec->multiout.hp_nid &&
 469            spec->multiout.hp_nid != spec->multiout.dac_nids[HDA_FRONT])
 470                        snd_hda_codec_setup_stream(codec,
 471                                                   spec->multiout.hp_nid,
 472                                                   0, 0, 0);
 473
 474        return 0;
 475}
 476
 477static struct snd_kcontrol_new via_hp_mixer[] = {
 478        {
 479                .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
 480                .name = "Independent HP",
 481                .count = 1,
 482                .info = via_independent_hp_info,
 483                .get = via_independent_hp_get,
 484                .put = via_independent_hp_put,
 485        },
 486        { } /* end */
 487};
 488
 489/* capture mixer elements */
 490static struct snd_kcontrol_new vt1708_capture_mixer[] = {
 491        HDA_CODEC_VOLUME("Capture Volume", 0x15, 0x0, HDA_INPUT),
 492        HDA_CODEC_MUTE("Capture Switch", 0x15, 0x0, HDA_INPUT),
 493        HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x27, 0x0, HDA_INPUT),
 494        HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x27, 0x0, HDA_INPUT),
 495        {
 496                .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
 497                /* The multiple "Capture Source" controls confuse alsamixer
 498                 * So call somewhat different..
 499                 */
 500                /* .name = "Capture Source", */
 501                .name = "Input Source",
 502                .count = 1,
 503                .info = via_mux_enum_info,
 504                .get = via_mux_enum_get,
 505                .put = via_mux_enum_put,
 506        },
 507        { } /* end */
 508};
 509/*
 510 * generic initialization of ADC, input mixers and output mixers
 511 */
 512static struct hda_verb vt1708_volume_init_verbs[] = {
 513        /*
 514         * Unmute ADC0-1 and set the default input to mic-in
 515         */
 516        {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
 517        {0x27, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
 518
 519
 520        /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
 521         * mixer widget
 522         */
 523        /* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
 524        {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
 525        {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
 526        {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
 527        {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
 528        {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
 529
 530        /*
 531         * Set up output mixers (0x19 - 0x1b)
 532         */
 533        /* set vol=0 to output mixers */
 534        {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
 535        {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
 536        {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
 537        
 538        /* Setup default input to PW4 */
 539        {0x20, AC_VERB_SET_CONNECT_SEL, 0x1},
 540        /* PW9 Output enable */
 541        {0x25, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
 542        { }
 543};
 544
 545static int via_playback_pcm_open(struct hda_pcm_stream *hinfo,
 546                                 struct hda_codec *codec,
 547                                 struct snd_pcm_substream *substream)
 548{
 549        struct via_spec *spec = codec->spec;
 550        return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
 551                                             hinfo);
 552}
 553
 554static int via_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
 555                                    struct hda_codec *codec,
 556                                    unsigned int stream_tag,
 557                                    unsigned int format,
 558                                    struct snd_pcm_substream *substream)
 559{
 560        struct via_spec *spec = codec->spec;
 561        return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
 562                                                stream_tag, format, substream);
 563}
 564
 565static int via_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
 566                                    struct hda_codec *codec,
 567                                    struct snd_pcm_substream *substream)
 568{
 569        struct via_spec *spec = codec->spec;
 570        return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
 571}
 572
 573
 574static void playback_multi_pcm_prep_0(struct hda_codec *codec,
 575                                      unsigned int stream_tag,
 576                                      unsigned int format,
 577                                      struct snd_pcm_substream *substream)
 578{
 579        struct via_spec *spec = codec->spec;
 580        struct hda_multi_out *mout = &spec->multiout;
 581        hda_nid_t *nids = mout->dac_nids;
 582        int chs = substream->runtime->channels;
 583        int i;
 584
 585        mutex_lock(&codec->spdif_mutex);
 586        if (mout->dig_out_nid && mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
 587                if (chs == 2 &&
 588                    snd_hda_is_supported_format(codec, mout->dig_out_nid,
 589                                                format) &&
 590                    !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
 591                        mout->dig_out_used = HDA_DIG_ANALOG_DUP;
 592                        /* turn off SPDIF once; otherwise the IEC958 bits won't
 593                         * be updated */
 594                        if (codec->spdif_ctls & AC_DIG1_ENABLE)
 595                                snd_hda_codec_write(codec, mout->dig_out_nid, 0,
 596                                                    AC_VERB_SET_DIGI_CONVERT_1,
 597                                                    codec->spdif_ctls &
 598                                                        ~AC_DIG1_ENABLE & 0xff);
 599                        snd_hda_codec_setup_stream(codec, mout->dig_out_nid,
 600                                                   stream_tag, 0, format);
 601                        /* turn on again (if needed) */
 602                        if (codec->spdif_ctls & AC_DIG1_ENABLE)
 603                                snd_hda_codec_write(codec, mout->dig_out_nid, 0,
 604                                                    AC_VERB_SET_DIGI_CONVERT_1,
 605                                                    codec->spdif_ctls & 0xff);
 606                } else {
 607                        mout->dig_out_used = 0;
 608                        snd_hda_codec_setup_stream(codec, mout->dig_out_nid,
 609                                                   0, 0, 0);
 610                }
 611        }
 612        mutex_unlock(&codec->spdif_mutex);
 613
 614        /* front */
 615        snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
 616                                   0, format);
 617
 618        if (mout->hp_nid && mout->hp_nid != nids[HDA_FRONT] &&
 619            !spec->hp_independent_mode)
 620                /* headphone out will just decode front left/right (stereo) */
 621                snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
 622                                           0, format);
 623
 624        /* extra outputs copied from front */
 625        for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
 626                if (mout->extra_out_nid[i])
 627                        snd_hda_codec_setup_stream(codec,
 628                                                   mout->extra_out_nid[i],
 629                                                   stream_tag, 0, format);
 630
 631        /* surrounds */
 632        for (i = 1; i < mout->num_dacs; i++) {
 633                if (chs >= (i + 1) * 2) /* independent out */
 634                        snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
 635                                                   i * 2, format);
 636                else /* copy front */
 637                        snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
 638                                                   0, format);
 639        }
 640}
 641
 642static int via_playback_multi_pcm_prepare(struct hda_pcm_stream *hinfo,
 643                                          struct hda_codec *codec,
 644                                          unsigned int stream_tag,
 645                                          unsigned int format,
 646                                          struct snd_pcm_substream *substream)
 647{
 648        struct via_spec *spec = codec->spec;
 649        struct hda_multi_out *mout = &spec->multiout;
 650        hda_nid_t *nids = mout->dac_nids;
 651
 652        if (substream->number == 0)
 653                playback_multi_pcm_prep_0(codec, stream_tag, format,
 654                                          substream);
 655        else {
 656                if (mout->hp_nid && mout->hp_nid != nids[HDA_FRONT] &&
 657                    spec->hp_independent_mode)
 658                        snd_hda_codec_setup_stream(codec, mout->hp_nid,
 659                                                   stream_tag, 0, format);
 660        }
 661
 662        return 0;
 663}
 664
 665static int via_playback_multi_pcm_cleanup(struct hda_pcm_stream *hinfo,
 666                                    struct hda_codec *codec,
 667                                    struct snd_pcm_substream *substream)
 668{
 669        struct via_spec *spec = codec->spec;
 670        struct hda_multi_out *mout = &spec->multiout;
 671        hda_nid_t *nids = mout->dac_nids;
 672        int i;
 673
 674        if (substream->number == 0) {
 675                for (i = 0; i < mout->num_dacs; i++)
 676                        snd_hda_codec_setup_stream(codec, nids[i], 0, 0, 0);
 677
 678                if (mout->hp_nid && !spec->hp_independent_mode)
 679                        snd_hda_codec_setup_stream(codec, mout->hp_nid,
 680                                                   0, 0, 0);
 681
 682                for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
 683                        if (mout->extra_out_nid[i])
 684                                snd_hda_codec_setup_stream(codec,
 685                                                        mout->extra_out_nid[i],
 686                                                        0, 0, 0);
 687                mutex_lock(&codec->spdif_mutex);
 688                if (mout->dig_out_nid &&
 689                    mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
 690                        snd_hda_codec_setup_stream(codec, mout->dig_out_nid,
 691                                                   0, 0, 0);
 692                        mout->dig_out_used = 0;
 693                }
 694                mutex_unlock(&codec->spdif_mutex);
 695        } else {
 696                if (mout->hp_nid && mout->hp_nid != nids[HDA_FRONT] &&
 697                    spec->hp_independent_mode)
 698                        snd_hda_codec_setup_stream(codec, mout->hp_nid,
 699                                                   0, 0, 0);
 700        }
 701
 702        return 0;
 703}
 704
 705/*
 706 * Digital out
 707 */
 708static int via_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
 709                                     struct hda_codec *codec,
 710                                     struct snd_pcm_substream *substream)
 711{
 712        struct via_spec *spec = codec->spec;
 713        return snd_hda_multi_out_dig_open(codec, &spec->multiout);
 714}
 715
 716static int via_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
 717                                      struct hda_codec *codec,
 718                                      struct snd_pcm_substream *substream)
 719{
 720        struct via_spec *spec = codec->spec;
 721        return snd_hda_multi_out_dig_close(codec, &spec->multiout);
 722}
 723
 724static int via_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
 725                                        struct hda_codec *codec,
 726                                        unsigned int stream_tag,
 727                                        unsigned int format,
 728                                        struct snd_pcm_substream *substream)
 729{
 730        struct via_spec *spec = codec->spec;
 731        return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
 732                                             stream_tag, format, substream);
 733}
 734
 735static int via_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
 736                                        struct hda_codec *codec,
 737                                        struct snd_pcm_substream *substream)
 738{
 739        struct via_spec *spec = codec->spec;
 740        snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
 741        return 0;
 742}
 743
 744/*
 745 * Analog capture
 746 */
 747static int via_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
 748                                   struct hda_codec *codec,
 749                                   unsigned int stream_tag,
 750                                   unsigned int format,
 751                                   struct snd_pcm_substream *substream)
 752{
 753        struct via_spec *spec = codec->spec;
 754
 755        snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
 756                                   stream_tag, 0, format);
 757        return 0;
 758}
 759
 760static int via_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
 761                                   struct hda_codec *codec,
 762                                   struct snd_pcm_substream *substream)
 763{
 764        struct via_spec *spec = codec->spec;
 765        snd_hda_codec_cleanup_stream(codec, spec->adc_nids[substream->number]);
 766        return 0;
 767}
 768
 769static struct hda_pcm_stream vt1708_pcm_analog_playback = {
 770        .substreams = 2,
 771        .channels_min = 2,
 772        .channels_max = 8,
 773        .nid = 0x10, /* NID to query formats and rates */
 774        .ops = {
 775                .open = via_playback_pcm_open,
 776                .prepare = via_playback_multi_pcm_prepare,
 777                .cleanup = via_playback_multi_pcm_cleanup
 778        },
 779};
 780
 781static struct hda_pcm_stream vt1708_pcm_analog_s16_playback = {
 782        .substreams = 1,
 783        .channels_min = 2,
 784        .channels_max = 8,
 785        .nid = 0x10, /* NID to query formats and rates */
 786        /* We got noisy outputs on the right channel on VT1708 when
 787         * 24bit samples are used.  Until any workaround is found,
 788         * disable the 24bit format, so far.
 789         */
 790        .formats = SNDRV_PCM_FMTBIT_S16_LE,
 791        .ops = {
 792                .open = via_playback_pcm_open,
 793                .prepare = via_playback_pcm_prepare,
 794                .cleanup = via_playback_pcm_cleanup
 795        },
 796};
 797
 798static struct hda_pcm_stream vt1708_pcm_analog_capture = {
 799        .substreams = 2,
 800        .channels_min = 2,
 801        .channels_max = 2,
 802        .nid = 0x15, /* NID to query formats and rates */
 803        .ops = {
 804                .prepare = via_capture_pcm_prepare,
 805                .cleanup = via_capture_pcm_cleanup
 806        },
 807};
 808
 809static struct hda_pcm_stream vt1708_pcm_digital_playback = {
 810        .substreams = 1,
 811        .channels_min = 2,
 812        .channels_max = 2,
 813        /* NID is set in via_build_pcms */
 814        .ops = {
 815                .open = via_dig_playback_pcm_open,
 816                .close = via_dig_playback_pcm_close,
 817                .prepare = via_dig_playback_pcm_prepare,
 818                .cleanup = via_dig_playback_pcm_cleanup
 819        },
 820};
 821
 822static struct hda_pcm_stream vt1708_pcm_digital_capture = {
 823        .substreams = 1,
 824        .channels_min = 2,
 825        .channels_max = 2,
 826};
 827
 828static int via_build_controls(struct hda_codec *codec)
 829{
 830        struct via_spec *spec = codec->spec;
 831        int err;
 832        int i;
 833
 834        for (i = 0; i < spec->num_mixers; i++) {
 835                err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
 836                if (err < 0)
 837                        return err;
 838        }
 839
 840        if (spec->multiout.dig_out_nid) {
 841                err = snd_hda_create_spdif_out_ctls(codec,
 842                                                    spec->multiout.dig_out_nid);
 843                if (err < 0)
 844                        return err;
 845                err = snd_hda_create_spdif_share_sw(codec,
 846                                                    &spec->multiout);
 847                if (err < 0)
 848                        return err;
 849                spec->multiout.share_spdif = 1;
 850        }
 851        if (spec->dig_in_nid) {
 852                err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
 853                if (err < 0)
 854                        return err;
 855        }
 856        via_free_kctls(codec); /* no longer needed */
 857        return 0;
 858}
 859
 860static int via_build_pcms(struct hda_codec *codec)
 861{
 862        struct via_spec *spec = codec->spec;
 863        struct hda_pcm *info = spec->pcm_rec;
 864
 865        codec->num_pcms = 1;
 866        codec->pcm_info = info;
 867
 868        info->name = spec->stream_name_analog;
 869        info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *(spec->stream_analog_playback);
 870        info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
 871        info->stream[SNDRV_PCM_STREAM_CAPTURE] = *(spec->stream_analog_capture);
 872        info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
 873
 874        info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
 875                spec->multiout.max_channels;
 876
 877        if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
 878                codec->num_pcms++;
 879                info++;
 880                info->name = spec->stream_name_digital;
 881                info->pcm_type = HDA_PCM_TYPE_SPDIF;
 882                if (spec->multiout.dig_out_nid) {
 883                        info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
 884                                *(spec->stream_digital_playback);
 885                        info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
 886                                spec->multiout.dig_out_nid;
 887                }
 888                if (spec->dig_in_nid) {
 889                        info->stream[SNDRV_PCM_STREAM_CAPTURE] =
 890                                *(spec->stream_digital_capture);
 891                        info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
 892                                spec->dig_in_nid;
 893                }
 894        }
 895
 896        return 0;
 897}
 898
 899static void via_free(struct hda_codec *codec)
 900{
 901        struct via_spec *spec = codec->spec;
 902
 903        if (!spec)
 904                return;
 905
 906        via_free_kctls(codec);
 907        kfree(codec->spec);
 908}
 909
 910/* mute internal speaker if HP is plugged */
 911static void via_hp_automute(struct hda_codec *codec)
 912{
 913        unsigned int present;
 914        struct via_spec *spec = codec->spec;
 915
 916        present = snd_hda_codec_read(codec, spec->autocfg.hp_pins[0], 0,
 917                                     AC_VERB_GET_PIN_SENSE, 0) & 0x80000000;
 918        snd_hda_codec_amp_stereo(codec, spec->autocfg.line_out_pins[0],
 919                                 HDA_OUTPUT, 0, HDA_AMP_MUTE,
 920                                 present ? HDA_AMP_MUTE : 0);
 921}
 922
 923static void via_gpio_control(struct hda_codec *codec)
 924{
 925        unsigned int gpio_data;
 926        unsigned int vol_counter;
 927        unsigned int vol;
 928        unsigned int master_vol;
 929
 930        struct via_spec *spec = codec->spec;
 931
 932        gpio_data = snd_hda_codec_read(codec, codec->afg, 0,
 933                                       AC_VERB_GET_GPIO_DATA, 0) & 0x03;
 934
 935        vol_counter = (snd_hda_codec_read(codec, codec->afg, 0,
 936                                          0xF84, 0) & 0x3F0000) >> 16;
 937
 938        vol = vol_counter & 0x1F;
 939        master_vol = snd_hda_codec_read(codec, 0x1A, 0,
 940                                        AC_VERB_GET_AMP_GAIN_MUTE,
 941                                        AC_AMP_GET_INPUT);
 942
 943        if (gpio_data == 0x02) {
 944                /* unmute line out */
 945                snd_hda_codec_amp_stereo(codec, spec->autocfg.line_out_pins[0],
 946                                         HDA_OUTPUT, 0, HDA_AMP_MUTE, 0);
 947
 948                if (vol_counter & 0x20) {
 949                        /* decrease volume */
 950                        if (vol > master_vol)
 951                                vol = master_vol;
 952                        snd_hda_codec_amp_stereo(codec, 0x1A, HDA_INPUT,
 953                                                 0, HDA_AMP_VOLMASK,
 954                                                 master_vol-vol);
 955                } else {
 956                        /* increase volume */
 957                        snd_hda_codec_amp_stereo(codec, 0x1A, HDA_INPUT, 0,
 958                                         HDA_AMP_VOLMASK,
 959                                         ((master_vol+vol) > 0x2A) ? 0x2A :
 960                                          (master_vol+vol));
 961                }
 962        } else if (!(gpio_data & 0x02)) {
 963                /* mute line out */
 964                snd_hda_codec_amp_stereo(codec,
 965                                         spec->autocfg.line_out_pins[0],
 966                                         HDA_OUTPUT, 0, HDA_AMP_MUTE,
 967                                         HDA_AMP_MUTE);
 968        }
 969}
 970
 971/* unsolicited event for jack sensing */
 972static void via_unsol_event(struct hda_codec *codec,
 973                                  unsigned int res)
 974{
 975        res >>= 26;
 976        if (res == VIA_HP_EVENT)
 977                via_hp_automute(codec);
 978        else if (res == VIA_GPIO_EVENT)
 979                via_gpio_control(codec);
 980}
 981
 982static int via_init(struct hda_codec *codec)
 983{
 984        struct via_spec *spec = codec->spec;
 985        int i;
 986        for (i = 0; i < spec->num_iverbs; i++)
 987                snd_hda_sequence_write(codec, spec->init_verbs[i]);
 988
 989        /* Lydia Add for EAPD enable */
 990        if (!spec->dig_in_nid) { /* No Digital In connection */
 991                if (spec->dig_in_pin) {
 992                        snd_hda_codec_write(codec, spec->dig_in_pin, 0,
 993                                            AC_VERB_SET_PIN_WIDGET_CONTROL,
 994                                            PIN_OUT);
 995                        snd_hda_codec_write(codec, spec->dig_in_pin, 0,
 996                                            AC_VERB_SET_EAPD_BTLENABLE, 0x02);
 997                }
 998        } else /* enable SPDIF-input pin */
 999                snd_hda_codec_write(codec, spec->autocfg.dig_in_pin, 0,
1000                                    AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN);
1001
1002        /* assign slave outs */
1003        if (spec->slave_dig_outs[0])
1004                codec->slave_dig_outs = spec->slave_dig_outs;
1005
1006        return 0;
1007}
1008
1009#ifdef CONFIG_SND_HDA_POWER_SAVE
1010static int via_check_power_status(struct hda_codec *codec, hda_nid_t nid)
1011{
1012        struct via_spec *spec = codec->spec;
1013        return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
1014}
1015#endif
1016
1017/*
1018 */
1019static struct hda_codec_ops via_patch_ops = {
1020        .build_controls = via_build_controls,
1021        .build_pcms = via_build_pcms,
1022        .init = via_init,
1023        .free = via_free,
1024#ifdef CONFIG_SND_HDA_POWER_SAVE
1025        .check_power_status = via_check_power_status,
1026#endif
1027};
1028
1029/* fill in the dac_nids table from the parsed pin configuration */
1030static int vt1708_auto_fill_dac_nids(struct via_spec *spec,
1031                                     const struct auto_pin_cfg *cfg)
1032{
1033        int i;
1034        hda_nid_t nid;
1035
1036        spec->multiout.num_dacs = cfg->line_outs;
1037
1038        spec->multiout.dac_nids = spec->private_dac_nids;
1039        
1040        for(i = 0; i < 4; i++) {
1041                nid = cfg->line_out_pins[i];
1042                if (nid) {
1043                        /* config dac list */
1044                        switch (i) {
1045                        case AUTO_SEQ_FRONT:
1046                                spec->multiout.dac_nids[i] = 0x10;
1047                                break;
1048                        case AUTO_SEQ_CENLFE:
1049                                spec->multiout.dac_nids[i] = 0x12;
1050                                break;
1051                        case AUTO_SEQ_SURROUND:
1052                                spec->multiout.dac_nids[i] = 0x11;
1053                                break;
1054                        case AUTO_SEQ_SIDE:
1055                                spec->multiout.dac_nids[i] = 0x13;
1056                                break;
1057                        }
1058                }
1059        }
1060
1061        return 0;
1062}
1063
1064/* add playback controls from the parsed DAC table */
1065static int vt1708_auto_create_multi_out_ctls(struct via_spec *spec,
1066                                             const struct auto_pin_cfg *cfg)
1067{
1068        char name[32];
1069        static const char *chname[4] = { "Front", "Surround", "C/LFE", "Side" };
1070        hda_nid_t nid, nid_vol = 0;
1071        int i, err;
1072
1073        for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
1074                nid = cfg->line_out_pins[i];
1075
1076                if (!nid)
1077                        continue;
1078                
1079                if (i != AUTO_SEQ_FRONT)
1080                        nid_vol = 0x18 + i;
1081
1082                if (i == AUTO_SEQ_CENLFE) {
1083                        /* Center/LFE */
1084                        err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
1085                                        "Center Playback Volume",
1086                                        HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
1087                                                            HDA_OUTPUT));
1088                        if (err < 0)
1089                                return err;
1090                        err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
1091                                              "LFE Playback Volume",
1092                                              HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
1093                                                                  HDA_OUTPUT));
1094                        if (err < 0)
1095                                return err;
1096                        err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
1097                                              "Center Playback Switch",
1098                                              HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
1099                                                                  HDA_OUTPUT));
1100                        if (err < 0)
1101                                return err;
1102                        err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
1103                                              "LFE Playback Switch",
1104                                              HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
1105                                                                  HDA_OUTPUT));
1106                        if (err < 0)
1107                                return err;
1108                } else if (i == AUTO_SEQ_FRONT){
1109                        /* add control to mixer index 0 */
1110                        err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
1111                                              "Master Front Playback Volume",
1112                                              HDA_COMPOSE_AMP_VAL(0x17, 3, 0,
1113                                                                  HDA_INPUT));
1114                        if (err < 0)
1115                                return err;
1116                        err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
1117                                              "Master Front Playback Switch",
1118                                              HDA_COMPOSE_AMP_VAL(0x17, 3, 0,
1119                                                                  HDA_INPUT));
1120                        if (err < 0)
1121                                return err;
1122                        
1123                        /* add control to PW3 */
1124                        sprintf(name, "%s Playback Volume", chname[i]);
1125                        err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
1126                                              HDA_COMPOSE_AMP_VAL(nid, 3, 0,
1127                                                                  HDA_OUTPUT));
1128                        if (err < 0)
1129                                return err;
1130                        sprintf(name, "%s Playback Switch", chname[i]);
1131                        err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
1132                                              HDA_COMPOSE_AMP_VAL(nid, 3, 0,
1133                                                                  HDA_OUTPUT));
1134                        if (err < 0)
1135                                return err;
1136                } else {
1137                        sprintf(name, "%s Playback Volume", chname[i]);
1138                        err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
1139                                              HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
1140                                                                  HDA_OUTPUT));
1141                        if (err < 0)
1142                                return err;
1143                        sprintf(name, "%s Playback Switch", chname[i]);
1144                        err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
1145                                              HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
1146                                                                  HDA_OUTPUT));
1147                        if (err < 0)
1148                                return err;
1149                }
1150        }
1151
1152        return 0;
1153}
1154
1155static void create_hp_imux(struct via_spec *spec)
1156{
1157        int i;
1158        struct hda_input_mux *imux = &spec->private_imux[1];
1159        static const char *texts[] = { "OFF", "ON", NULL};
1160
1161        /* for hp mode select */
1162        i = 0;
1163        while (texts[i] != NULL) {
1164                imux->items[imux->num_items].label =  texts[i];
1165                imux->items[imux->num_items].index = i;
1166                imux->num_items++;
1167                i++;
1168        }
1169
1170        spec->hp_mux = &spec->private_imux[1];
1171}
1172
1173static int vt1708_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
1174{
1175        int err;
1176
1177        if (!pin)
1178                return 0;
1179
1180        spec->multiout.hp_nid = VT1708_HP_NID; /* AOW3 */
1181
1182        err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
1183                              "Headphone Playback Volume",
1184                              HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
1185        if (err < 0)
1186                return err;
1187        err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
1188                              "Headphone Playback Switch",
1189                              HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
1190        if (err < 0)
1191                return err;
1192
1193        create_hp_imux(spec);
1194
1195        return 0;
1196}
1197
1198/* create playback/capture controls for input pins */
1199static int vt1708_auto_create_analog_input_ctls(struct via_spec *spec,
1200                                                const struct auto_pin_cfg *cfg)
1201{
1202        static char *labels[] = {
1203                "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux", NULL
1204        };
1205        struct hda_input_mux *imux = &spec->private_imux[0];
1206        int i, err, idx = 0;
1207
1208        /* for internal loopback recording select */
1209        imux->items[imux->num_items].label = "Stereo Mixer";
1210        imux->items[imux->num_items].index = idx;
1211        imux->num_items++;
1212
1213        for (i = 0; i < AUTO_PIN_LAST; i++) {
1214                if (!cfg->input_pins[i])
1215                        continue;
1216
1217                switch (cfg->input_pins[i]) {
1218                case 0x1d: /* Mic */
1219                        idx = 2;
1220                        break;
1221                                
1222                case 0x1e: /* Line In */
1223                        idx = 3;
1224                        break;
1225
1226                case 0x21: /* Front Mic */
1227                        idx = 4;
1228                        break;
1229
1230                case 0x24: /* CD */
1231                        idx = 1;
1232                        break;
1233                }
1234                err = via_new_analog_input(spec, cfg->input_pins[i], labels[i],
1235                                           idx, 0x17);
1236                if (err < 0)
1237                        return err;
1238                imux->items[imux->num_items].label = labels[i];
1239                imux->items[imux->num_items].index = idx;
1240                imux->num_items++;
1241        }
1242        return 0;
1243}
1244
1245#ifdef CONFIG_SND_HDA_POWER_SAVE
1246static struct hda_amp_list vt1708_loopbacks[] = {
1247        { 0x17, HDA_INPUT, 1 },
1248        { 0x17, HDA_INPUT, 2 },
1249        { 0x17, HDA_INPUT, 3 },
1250        { 0x17, HDA_INPUT, 4 },
1251        { } /* end */
1252};
1253#endif
1254
1255static void vt1708_set_pinconfig_connect(struct hda_codec *codec, hda_nid_t nid)
1256{
1257        unsigned int def_conf;
1258        unsigned char seqassoc;
1259
1260        def_conf = snd_hda_codec_get_pincfg(codec, nid);
1261        seqassoc = (unsigned char) get_defcfg_association(def_conf);
1262        seqassoc = (seqassoc << 4) | get_defcfg_sequence(def_conf);
1263        if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE) {
1264                if (seqassoc == 0xff) {
1265                        def_conf = def_conf & (~(AC_JACK_PORT_BOTH << 30));
1266                        snd_hda_codec_set_pincfg(codec, nid, def_conf);
1267                }
1268        }
1269
1270        return;
1271}
1272
1273static int vt1708_parse_auto_config(struct hda_codec *codec)
1274{
1275        struct via_spec *spec = codec->spec;
1276        int err;
1277
1278        /* Add HP and CD pin config connect bit re-config action */
1279        vt1708_set_pinconfig_connect(codec, VT1708_HP_PIN_NID);
1280        vt1708_set_pinconfig_connect(codec, VT1708_CD_PIN_NID);
1281
1282        err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1283        if (err < 0)
1284                return err;
1285        err = vt1708_auto_fill_dac_nids(spec, &spec->autocfg);
1286        if (err < 0)
1287                return err;
1288        if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
1289                return 0; /* can't find valid BIOS pin config */
1290
1291        err = vt1708_auto_create_multi_out_ctls(spec, &spec->autocfg);
1292        if (err < 0)
1293                return err;
1294        err = vt1708_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
1295        if (err < 0)
1296                return err;
1297        err = vt1708_auto_create_analog_input_ctls(spec, &spec->autocfg);
1298        if (err < 0)
1299                return err;
1300
1301        spec->multiout.max_channels = spec->multiout.num_dacs * 2;
1302
1303        if (spec->autocfg.dig_outs)
1304                spec->multiout.dig_out_nid = VT1708_DIGOUT_NID;
1305        spec->dig_in_pin = VT1708_DIGIN_PIN;
1306        if (spec->autocfg.dig_in_pin)
1307                spec->dig_in_nid = VT1708_DIGIN_NID;
1308
1309        if (spec->kctls.list)
1310                spec->mixers[spec->num_mixers++] = spec->kctls.list;
1311
1312        spec->init_verbs[spec->num_iverbs++] = vt1708_volume_init_verbs;
1313
1314        spec->input_mux = &spec->private_imux[0];
1315
1316        if (spec->hp_mux)
1317                spec->mixers[spec->num_mixers++] = via_hp_mixer;
1318
1319        return 1;
1320}
1321
1322/* init callback for auto-configuration model -- overriding the default init */
1323static int via_auto_init(struct hda_codec *codec)
1324{
1325        via_init(codec);
1326        via_auto_init_multi_out(codec);
1327        via_auto_init_hp_out(codec);
1328        via_auto_init_analog_input(codec);
1329        return 0;
1330}
1331
1332static int get_mux_nids(struct hda_codec *codec)
1333{
1334        struct via_spec *spec = codec->spec;
1335        hda_nid_t nid, conn[8];
1336        unsigned int type;
1337        int i, n;
1338
1339        for (i = 0; i < spec->num_adc_nids; i++) {
1340                nid = spec->adc_nids[i];
1341                while (nid) {
1342                        type = get_wcaps_type(get_wcaps(codec, nid));
1343                        if (type == AC_WID_PIN)
1344                                break;
1345                        n = snd_hda_get_connections(codec, nid, conn,
1346                                                    ARRAY_SIZE(conn));
1347                        if (n <= 0)
1348                                break;
1349                        if (n > 1) {
1350                                spec->mux_nids[i] = nid;
1351                                break;
1352                        }
1353                        nid = conn[0];
1354                }
1355        }
1356        return 0;
1357}
1358
1359static int patch_vt1708(struct hda_codec *codec)
1360{
1361        struct via_spec *spec;
1362        int err;
1363
1364        /* create a codec specific record */
1365        spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1366        if (spec == NULL)
1367                return -ENOMEM;
1368
1369        codec->spec = spec;
1370
1371        /* automatic parse from the BIOS config */
1372        err = vt1708_parse_auto_config(codec);
1373        if (err < 0) {
1374                via_free(codec);
1375                return err;
1376        } else if (!err) {
1377                printk(KERN_INFO "hda_codec: Cannot set up configuration "
1378                       "from BIOS.  Using genenic mode...\n");
1379        }
1380
1381        
1382        spec->stream_name_analog = "VT1708 Analog";
1383        spec->stream_analog_playback = &vt1708_pcm_analog_playback;
1384        /* disable 32bit format on VT1708 */
1385        if (codec->vendor_id == 0x11061708)
1386                spec->stream_analog_playback = &vt1708_pcm_analog_s16_playback;
1387        spec->stream_analog_capture = &vt1708_pcm_analog_capture;
1388
1389        spec->stream_name_digital = "VT1708 Digital";
1390        spec->stream_digital_playback = &vt1708_pcm_digital_playback;
1391        spec->stream_digital_capture = &vt1708_pcm_digital_capture;
1392
1393        
1394        if (!spec->adc_nids && spec->input_mux) {
1395                spec->adc_nids = vt1708_adc_nids;
1396                spec->num_adc_nids = ARRAY_SIZE(vt1708_adc_nids);
1397                get_mux_nids(codec);
1398                spec->mixers[spec->num_mixers] = vt1708_capture_mixer;
1399                spec->num_mixers++;
1400        }
1401
1402        codec->patch_ops = via_patch_ops;
1403
1404        codec->patch_ops.init = via_auto_init;
1405#ifdef CONFIG_SND_HDA_POWER_SAVE
1406        spec->loopback.amplist = vt1708_loopbacks;
1407#endif
1408
1409        return 0;
1410}
1411
1412/* capture mixer elements */
1413static struct snd_kcontrol_new vt1709_capture_mixer[] = {
1414        HDA_CODEC_VOLUME("Capture Volume", 0x14, 0x0, HDA_INPUT),
1415        HDA_CODEC_MUTE("Capture Switch", 0x14, 0x0, HDA_INPUT),
1416        HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x15, 0x0, HDA_INPUT),
1417        HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x15, 0x0, HDA_INPUT),
1418        HDA_CODEC_VOLUME_IDX("Capture Volume", 2, 0x16, 0x0, HDA_INPUT),
1419        HDA_CODEC_MUTE_IDX("Capture Switch", 2, 0x16, 0x0, HDA_INPUT),
1420        {
1421                .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1422                /* The multiple "Capture Source" controls confuse alsamixer
1423                 * So call somewhat different..
1424                 */
1425                /* .name = "Capture Source", */
1426                .name = "Input Source",
1427                .count = 1,
1428                .info = via_mux_enum_info,
1429                .get = via_mux_enum_get,
1430                .put = via_mux_enum_put,
1431        },
1432        { } /* end */
1433};
1434
1435static struct hda_verb vt1709_uniwill_init_verbs[] = {
1436        {0x20, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_HP_EVENT},
1437        { }
1438};
1439
1440/*
1441 * generic initialization of ADC, input mixers and output mixers
1442 */
1443static struct hda_verb vt1709_10ch_volume_init_verbs[] = {
1444        /*
1445         * Unmute ADC0-2 and set the default input to mic-in
1446         */
1447        {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1448        {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1449        {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1450
1451
1452        /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
1453         * mixer widget
1454         */
1455        /* Amp Indices: AOW0=0, CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
1456        {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1457        {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
1458        {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
1459        {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
1460        {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
1461
1462        /*
1463         * Set up output selector (0x1a, 0x1b, 0x29)
1464         */
1465        /* set vol=0 to output mixers */
1466        {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1467        {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1468        {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1469
1470        /*
1471         *  Unmute PW3 and PW4
1472         */
1473        {0x1f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1474        {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1475
1476        /* Set input of PW4 as AOW4 */
1477        {0x20, AC_VERB_SET_CONNECT_SEL, 0x1},
1478        /* PW9 Output enable */
1479        {0x24, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
1480        { }
1481};
1482
1483static struct hda_pcm_stream vt1709_10ch_pcm_analog_playback = {
1484        .substreams = 1,
1485        .channels_min = 2,
1486        .channels_max = 10,
1487        .nid = 0x10, /* NID to query formats and rates */
1488        .ops = {
1489                .open = via_playback_pcm_open,
1490                .prepare = via_playback_pcm_prepare,
1491                .cleanup = via_playback_pcm_cleanup
1492        },
1493};
1494
1495static struct hda_pcm_stream vt1709_6ch_pcm_analog_playback = {
1496        .substreams = 1,
1497        .channels_min = 2,
1498        .channels_max = 6,
1499        .nid = 0x10, /* NID to query formats and rates */
1500        .ops = {
1501                .open = via_playback_pcm_open,
1502                .prepare = via_playback_pcm_prepare,
1503                .cleanup = via_playback_pcm_cleanup
1504        },
1505};
1506
1507static struct hda_pcm_stream vt1709_pcm_analog_capture = {
1508        .substreams = 2,
1509        .channels_min = 2,
1510        .channels_max = 2,
1511        .nid = 0x14, /* NID to query formats and rates */
1512        .ops = {
1513                .prepare = via_capture_pcm_prepare,
1514                .cleanup = via_capture_pcm_cleanup
1515        },
1516};
1517
1518static struct hda_pcm_stream vt1709_pcm_digital_playback = {
1519        .substreams = 1,
1520        .channels_min = 2,
1521        .channels_max = 2,
1522        /* NID is set in via_build_pcms */
1523        .ops = {
1524                .open = via_dig_playback_pcm_open,
1525                .close = via_dig_playback_pcm_close
1526        },
1527};
1528
1529static struct hda_pcm_stream vt1709_pcm_digital_capture = {
1530        .substreams = 1,
1531        .channels_min = 2,
1532        .channels_max = 2,
1533};
1534
1535static int vt1709_auto_fill_dac_nids(struct via_spec *spec,
1536                                     const struct auto_pin_cfg *cfg)
1537{
1538        int i;
1539        hda_nid_t nid;
1540
1541        if (cfg->line_outs == 4)  /* 10 channels */
1542                spec->multiout.num_dacs = cfg->line_outs+1; /* AOW0~AOW4 */
1543        else if (cfg->line_outs == 3) /* 6 channels */
1544                spec->multiout.num_dacs = cfg->line_outs; /* AOW0~AOW2 */
1545
1546        spec->multiout.dac_nids = spec->private_dac_nids;
1547
1548        if (cfg->line_outs == 4) { /* 10 channels */
1549                for (i = 0; i < cfg->line_outs; i++) {
1550                        nid = cfg->line_out_pins[i];
1551                        if (nid) {
1552                                /* config dac list */
1553                                switch (i) {
1554                                case AUTO_SEQ_FRONT:
1555                                        /* AOW0 */
1556                                        spec->multiout.dac_nids[i] = 0x10;
1557                                        break;
1558                                case AUTO_SEQ_CENLFE:
1559                                        /* AOW2 */
1560                                        spec->multiout.dac_nids[i] = 0x12;
1561                                        break;
1562                                case AUTO_SEQ_SURROUND:
1563                                        /* AOW3 */
1564                                        spec->multiout.dac_nids[i] = 0x11;
1565                                        break;
1566                                case AUTO_SEQ_SIDE:
1567                                        /* AOW1 */
1568                                        spec->multiout.dac_nids[i] = 0x27;
1569                                        break;
1570                                default:
1571                                        break;
1572                                }
1573                        }
1574                }
1575                spec->multiout.dac_nids[cfg->line_outs] = 0x28; /* AOW4 */
1576
1577        } else if (cfg->line_outs == 3) { /* 6 channels */
1578                for(i = 0; i < cfg->line_outs; i++) {
1579                        nid = cfg->line_out_pins[i];
1580                        if (nid) {
1581                                /* config dac list */
1582                                switch(i) {
1583                                case AUTO_SEQ_FRONT:
1584                                        /* AOW0 */
1585                                        spec->multiout.dac_nids[i] = 0x10;
1586                                        break;
1587                                case AUTO_SEQ_CENLFE:
1588                                        /* AOW2 */
1589                                        spec->multiout.dac_nids[i] = 0x12;
1590                                        break;
1591                                case AUTO_SEQ_SURROUND:
1592                                        /* AOW1 */
1593                                        spec->multiout.dac_nids[i] = 0x11;
1594                                        break;
1595                                default:
1596                                        break;
1597                                }
1598                        }
1599                }
1600        }
1601
1602        return 0;
1603}
1604
1605/* add playback controls from the parsed DAC table */
1606static int vt1709_auto_create_multi_out_ctls(struct via_spec *spec,
1607                                             const struct auto_pin_cfg *cfg)
1608{
1609        char name[32];
1610        static const char *chname[4] = { "Front", "Surround", "C/LFE", "Side" };
1611        hda_nid_t nid = 0;
1612        int i, err;
1613
1614        for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
1615                nid = cfg->line_out_pins[i];
1616
1617                if (!nid)       
1618                        continue;
1619
1620                if (i == AUTO_SEQ_CENLFE) {
1621                        /* Center/LFE */
1622                        err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
1623                                              "Center Playback Volume",
1624                                              HDA_COMPOSE_AMP_VAL(0x1b, 1, 0,
1625                                                                  HDA_OUTPUT));
1626                        if (err < 0)
1627                                return err;
1628                        err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
1629                                              "LFE Playback Volume",
1630                                              HDA_COMPOSE_AMP_VAL(0x1b, 2, 0,
1631                                                                  HDA_OUTPUT));
1632                        if (err < 0)
1633                                return err;
1634                        err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
1635                                              "Center Playback Switch",
1636                                              HDA_COMPOSE_AMP_VAL(0x1b, 1, 0,
1637                                                                  HDA_OUTPUT));
1638                        if (err < 0)
1639                                return err;
1640                        err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
1641                                              "LFE Playback Switch",
1642                                              HDA_COMPOSE_AMP_VAL(0x1b, 2, 0,
1643                                                                  HDA_OUTPUT));
1644                        if (err < 0)
1645                                return err;
1646                } else if (i == AUTO_SEQ_FRONT){
1647                        /* add control to mixer index 0 */
1648                        err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
1649                                              "Master Front Playback Volume",
1650                                              HDA_COMPOSE_AMP_VAL(0x18, 3, 0,
1651                                                                  HDA_INPUT));
1652                        if (err < 0)
1653                                return err;
1654                        err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
1655                                              "Master Front Playback Switch",
1656                                              HDA_COMPOSE_AMP_VAL(0x18, 3, 0,
1657                                                                  HDA_INPUT));
1658                        if (err < 0)
1659                                return err;
1660                        
1661                        /* add control to PW3 */
1662                        sprintf(name, "%s Playback Volume", chname[i]);
1663                        err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
1664                                              HDA_COMPOSE_AMP_VAL(nid, 3, 0,
1665                                                                  HDA_OUTPUT));
1666                        if (err < 0)
1667                                return err;
1668                        sprintf(name, "%s Playback Switch", chname[i]);
1669                        err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
1670                                              HDA_COMPOSE_AMP_VAL(nid, 3, 0,
1671                                                                  HDA_OUTPUT));
1672                        if (err < 0)
1673                                return err;
1674                } else if (i == AUTO_SEQ_SURROUND) {
1675                        sprintf(name, "%s Playback Volume", chname[i]);
1676                        err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
1677                                              HDA_COMPOSE_AMP_VAL(0x1a, 3, 0,
1678                                                                  HDA_OUTPUT));
1679                        if (err < 0)
1680                                return err;
1681                        sprintf(name, "%s Playback Switch", chname[i]);
1682                        err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
1683                                              HDA_COMPOSE_AMP_VAL(0x1a, 3, 0,
1684                                                                  HDA_OUTPUT));
1685                        if (err < 0)
1686                                return err;
1687                } else if (i == AUTO_SEQ_SIDE) {
1688                        sprintf(name, "%s Playback Volume", chname[i]);
1689                        err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
1690                                              HDA_COMPOSE_AMP_VAL(0x29, 3, 0,
1691                                                                  HDA_OUTPUT));
1692                        if (err < 0)
1693                                return err;
1694                        sprintf(name, "%s Playback Switch", chname[i]);
1695                        err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
1696                                              HDA_COMPOSE_AMP_VAL(0x29, 3, 0,
1697                                                                  HDA_OUTPUT));
1698                        if (err < 0)
1699                                return err;
1700                }
1701        }
1702
1703        return 0;
1704}
1705
1706static int vt1709_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
1707{
1708        int err;
1709
1710        if (!pin)
1711                return 0;
1712
1713        if (spec->multiout.num_dacs == 5) /* 10 channels */
1714                spec->multiout.hp_nid = VT1709_HP_DAC_NID;
1715        else if (spec->multiout.num_dacs == 3) /* 6 channels */
1716                spec->multiout.hp_nid = 0;
1717
1718        err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
1719                              "Headphone Playback Volume",
1720                              HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
1721        if (err < 0)
1722                return err;
1723        err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
1724                              "Headphone Playback Switch",
1725                              HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
1726        if (err < 0)
1727                return err;
1728
1729        return 0;
1730}
1731
1732/* create playback/capture controls for input pins */
1733static int vt1709_auto_create_analog_input_ctls(struct via_spec *spec,
1734                                                const struct auto_pin_cfg *cfg)
1735{
1736        static char *labels[] = {
1737                "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux", NULL
1738        };
1739        struct hda_input_mux *imux = &spec->private_imux[0];
1740        int i, err, idx = 0;
1741
1742        /* for internal loopback recording select */
1743        imux->items[imux->num_items].label = "Stereo Mixer";
1744        imux->items[imux->num_items].index = idx;
1745        imux->num_items++;
1746
1747        for (i = 0; i < AUTO_PIN_LAST; i++) {
1748                if (!cfg->input_pins[i])
1749                        continue;
1750
1751                switch (cfg->input_pins[i]) {
1752                case 0x1d: /* Mic */
1753                        idx = 2;
1754                        break;
1755                                
1756                case 0x1e: /* Line In */
1757                        idx = 3;
1758                        break;
1759
1760                case 0x21: /* Front Mic */
1761                        idx = 4;
1762                        break;
1763
1764                case 0x23: /* CD */
1765                        idx = 1;
1766                        break;
1767                }
1768                err = via_new_analog_input(spec, cfg->input_pins[i], labels[i],
1769                                           idx, 0x18);
1770                if (err < 0)
1771                        return err;
1772                imux->items[imux->num_items].label = labels[i];
1773                imux->items[imux->num_items].index = idx;
1774                imux->num_items++;
1775        }
1776        return 0;
1777}
1778
1779static int vt1709_parse_auto_config(struct hda_codec *codec)
1780{
1781        struct via_spec *spec = codec->spec;
1782        int err;
1783
1784        err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
1785        if (err < 0)
1786                return err;
1787        err = vt1709_auto_fill_dac_nids(spec, &spec->autocfg);
1788        if (err < 0)
1789                return err;
1790        if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
1791                return 0; /* can't find valid BIOS pin config */
1792
1793        err = vt1709_auto_create_multi_out_ctls(spec, &spec->autocfg);
1794        if (err < 0)
1795                return err;
1796        err = vt1709_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
1797        if (err < 0)
1798                return err;
1799        err = vt1709_auto_create_analog_input_ctls(spec, &spec->autocfg);
1800        if (err < 0)
1801                return err;
1802
1803        spec->multiout.max_channels = spec->multiout.num_dacs * 2;
1804
1805        if (spec->autocfg.dig_outs)
1806                spec->multiout.dig_out_nid = VT1709_DIGOUT_NID;
1807        spec->dig_in_pin = VT1709_DIGIN_PIN;
1808        if (spec->autocfg.dig_in_pin)
1809                spec->dig_in_nid = VT1709_DIGIN_NID;
1810
1811        if (spec->kctls.list)
1812                spec->mixers[spec->num_mixers++] = spec->kctls.list;
1813
1814        spec->input_mux = &spec->private_imux[0];
1815
1816        if (spec->hp_mux)
1817                spec->mixers[spec->num_mixers++] = via_hp_mixer;
1818
1819        return 1;
1820}
1821
1822#ifdef CONFIG_SND_HDA_POWER_SAVE
1823static struct hda_amp_list vt1709_loopbacks[] = {
1824        { 0x18, HDA_INPUT, 1 },
1825        { 0x18, HDA_INPUT, 2 },
1826        { 0x18, HDA_INPUT, 3 },
1827        { 0x18, HDA_INPUT, 4 },
1828        { } /* end */
1829};
1830#endif
1831
1832static int patch_vt1709_10ch(struct hda_codec *codec)
1833{
1834        struct via_spec *spec;
1835        int err;
1836
1837        /* create a codec specific record */
1838        spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1839        if (spec == NULL)
1840                return -ENOMEM;
1841
1842        codec->spec = spec;
1843
1844        err = vt1709_parse_auto_config(codec);
1845        if (err < 0) {
1846                via_free(codec);
1847                return err;
1848        } else if (!err) {
1849                printk(KERN_INFO "hda_codec: Cannot set up configuration.  "
1850                       "Using genenic mode...\n");
1851        }
1852
1853        spec->init_verbs[spec->num_iverbs++] = vt1709_10ch_volume_init_verbs;
1854        spec->init_verbs[spec->num_iverbs++] = vt1709_uniwill_init_verbs;
1855
1856        spec->stream_name_analog = "VT1709 Analog";
1857        spec->stream_analog_playback = &vt1709_10ch_pcm_analog_playback;
1858        spec->stream_analog_capture = &vt1709_pcm_analog_capture;
1859
1860        spec->stream_name_digital = "VT1709 Digital";
1861        spec->stream_digital_playback = &vt1709_pcm_digital_playback;
1862        spec->stream_digital_capture = &vt1709_pcm_digital_capture;
1863
1864        
1865        if (!spec->adc_nids && spec->input_mux) {
1866                spec->adc_nids = vt1709_adc_nids;
1867                spec->num_adc_nids = ARRAY_SIZE(vt1709_adc_nids);
1868                get_mux_nids(codec);
1869                spec->mixers[spec->num_mixers] = vt1709_capture_mixer;
1870                spec->num_mixers++;
1871        }
1872
1873        codec->patch_ops = via_patch_ops;
1874
1875        codec->patch_ops.init = via_auto_init;
1876        codec->patch_ops.unsol_event = via_unsol_event;
1877#ifdef CONFIG_SND_HDA_POWER_SAVE
1878        spec->loopback.amplist = vt1709_loopbacks;
1879#endif
1880
1881        return 0;
1882}
1883/*
1884 * generic initialization of ADC, input mixers and output mixers
1885 */
1886static struct hda_verb vt1709_6ch_volume_init_verbs[] = {
1887        /*
1888         * Unmute ADC0-2 and set the default input to mic-in
1889         */
1890        {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1891        {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1892        {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1893
1894
1895        /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
1896         * mixer widget
1897         */
1898        /* Amp Indices: AOW0=0, CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
1899        {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
1900        {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
1901        {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
1902        {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
1903        {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
1904
1905        /*
1906         * Set up output selector (0x1a, 0x1b, 0x29)
1907         */
1908        /* set vol=0 to output mixers */
1909        {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1910        {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1911        {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1912
1913        /*
1914         *  Unmute PW3 and PW4
1915         */
1916        {0x1f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1917        {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
1918
1919        /* Set input of PW4 as MW0 */
1920        {0x20, AC_VERB_SET_CONNECT_SEL, 0},
1921        /* PW9 Output enable */
1922        {0x24, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
1923        { }
1924};
1925
1926static int patch_vt1709_6ch(struct hda_codec *codec)
1927{
1928        struct via_spec *spec;
1929        int err;
1930
1931        /* create a codec specific record */
1932        spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1933        if (spec == NULL)
1934                return -ENOMEM;
1935
1936        codec->spec = spec;
1937
1938        err = vt1709_parse_auto_config(codec);
1939        if (err < 0) {
1940                via_free(codec);
1941                return err;
1942        } else if (!err) {
1943                printk(KERN_INFO "hda_codec: Cannot set up configuration.  "
1944                       "Using genenic mode...\n");
1945        }
1946
1947        spec->init_verbs[spec->num_iverbs++] = vt1709_6ch_volume_init_verbs;
1948        spec->init_verbs[spec->num_iverbs++] = vt1709_uniwill_init_verbs;
1949
1950        spec->stream_name_analog = "VT1709 Analog";
1951        spec->stream_analog_playback = &vt1709_6ch_pcm_analog_playback;
1952        spec->stream_analog_capture = &vt1709_pcm_analog_capture;
1953
1954        spec->stream_name_digital = "VT1709 Digital";
1955        spec->stream_digital_playback = &vt1709_pcm_digital_playback;
1956        spec->stream_digital_capture = &vt1709_pcm_digital_capture;
1957
1958        
1959        if (!spec->adc_nids && spec->input_mux) {
1960                spec->adc_nids = vt1709_adc_nids;
1961                spec->num_adc_nids = ARRAY_SIZE(vt1709_adc_nids);
1962                get_mux_nids(codec);
1963                spec->mixers[spec->num_mixers] = vt1709_capture_mixer;
1964                spec->num_mixers++;
1965        }
1966
1967        codec->patch_ops = via_patch_ops;
1968
1969        codec->patch_ops.init = via_auto_init;
1970        codec->patch_ops.unsol_event = via_unsol_event;
1971#ifdef CONFIG_SND_HDA_POWER_SAVE
1972        spec->loopback.amplist = vt1709_loopbacks;
1973#endif
1974        return 0;
1975}
1976
1977/* capture mixer elements */
1978static struct snd_kcontrol_new vt1708B_capture_mixer[] = {
1979        HDA_CODEC_VOLUME("Capture Volume", 0x13, 0x0, HDA_INPUT),
1980        HDA_CODEC_MUTE("Capture Switch", 0x13, 0x0, HDA_INPUT),
1981        HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x14, 0x0, HDA_INPUT),
1982        HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x14, 0x0, HDA_INPUT),
1983        {
1984                .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1985                /* The multiple "Capture Source" controls confuse alsamixer
1986                 * So call somewhat different..
1987                 */
1988                /* .name = "Capture Source", */
1989                .name = "Input Source",
1990                .count = 1,
1991                .info = via_mux_enum_info,
1992                .get = via_mux_enum_get,
1993                .put = via_mux_enum_put,
1994        },
1995        { } /* end */
1996};
1997/*
1998 * generic initialization of ADC, input mixers and output mixers
1999 */
2000static struct hda_verb vt1708B_8ch_volume_init_verbs[] = {
2001        /*
2002         * Unmute ADC0-1 and set the default input to mic-in
2003         */
2004        {0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2005        {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2006
2007
2008        /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
2009         * mixer widget
2010         */
2011        /* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
2012        {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2013        {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2014        {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
2015        {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
2016        {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
2017
2018        /*
2019         * Set up output mixers
2020         */
2021        /* set vol=0 to output mixers */
2022        {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2023        {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2024        {0x27, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2025
2026        /* Setup default input to PW4 */
2027        {0x1d, AC_VERB_SET_CONNECT_SEL, 0x1},
2028        /* PW9 Output enable */
2029        {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
2030        /* PW10 Input enable */
2031        {0x21, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20},
2032        { }
2033};
2034
2035static struct hda_verb vt1708B_4ch_volume_init_verbs[] = {
2036        /*
2037         * Unmute ADC0-1 and set the default input to mic-in
2038         */
2039        {0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2040        {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2041
2042
2043        /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
2044         * mixer widget
2045         */
2046        /* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
2047        {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2048        {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2049        {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
2050        {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
2051        {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
2052
2053        /*
2054         * Set up output mixers
2055         */
2056        /* set vol=0 to output mixers */
2057        {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2058        {0x26, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2059        {0x27, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
2060
2061        /* Setup default input of PW4 to MW0 */
2062        {0x1d, AC_VERB_SET_CONNECT_SEL, 0x0},
2063        /* PW9 Output enable */
2064        {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
2065        /* PW10 Input enable */
2066        {0x21, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20},
2067        { }
2068};
2069
2070static struct hda_verb vt1708B_uniwill_init_verbs[] = {
2071        {0x1D, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_HP_EVENT},
2072        { }
2073};
2074
2075static struct hda_pcm_stream vt1708B_8ch_pcm_analog_playback = {
2076        .substreams = 2,
2077        .channels_min = 2,
2078        .channels_max = 8,
2079        .nid = 0x10, /* NID to query formats and rates */
2080        .ops = {
2081                .open = via_playback_pcm_open,
2082                .prepare = via_playback_multi_pcm_prepare,
2083                .cleanup = via_playback_multi_pcm_cleanup
2084        },
2085};
2086
2087static struct hda_pcm_stream vt1708B_4ch_pcm_analog_playback = {
2088        .substreams = 2,
2089        .channels_min = 2,
2090        .channels_max = 4,
2091        .nid = 0x10, /* NID to query formats and rates */
2092        .ops = {
2093                .open = via_playback_pcm_open,
2094                .prepare = via_playback_multi_pcm_prepare,
2095                .cleanup = via_playback_multi_pcm_cleanup
2096        },
2097};
2098
2099static struct hda_pcm_stream vt1708B_pcm_analog_capture = {
2100        .substreams = 2,
2101        .channels_min = 2,
2102        .channels_max = 2,
2103        .nid = 0x13, /* NID to query formats and rates */
2104        .ops = {
2105                .prepare = via_capture_pcm_prepare,
2106                .cleanup = via_capture_pcm_cleanup
2107        },
2108};
2109
2110static struct hda_pcm_stream vt1708B_pcm_digital_playback = {
2111        .substreams = 1,
2112        .channels_min = 2,
2113        .channels_max = 2,
2114        /* NID is set in via_build_pcms */
2115        .ops = {
2116                .open = via_dig_playback_pcm_open,
2117                .close = via_dig_playback_pcm_close,
2118                .prepare = via_dig_playback_pcm_prepare,
2119                .cleanup = via_dig_playback_pcm_cleanup
2120        },
2121};
2122
2123static struct hda_pcm_stream vt1708B_pcm_digital_capture = {
2124        .substreams = 1,
2125        .channels_min = 2,
2126        .channels_max = 2,
2127};
2128
2129/* fill in the dac_nids table from the parsed pin configuration */
2130static int vt1708B_auto_fill_dac_nids(struct via_spec *spec,
2131                                     const struct auto_pin_cfg *cfg)
2132{
2133        int i;
2134        hda_nid_t nid;
2135
2136        spec->multiout.num_dacs = cfg->line_outs;
2137
2138        spec->multiout.dac_nids = spec->private_dac_nids;
2139
2140        for (i = 0; i < 4; i++) {
2141                nid = cfg->line_out_pins[i];
2142                if (nid) {
2143                        /* config dac list */
2144                        switch (i) {
2145                        case AUTO_SEQ_FRONT:
2146                                spec->multiout.dac_nids[i] = 0x10;
2147                                break;
2148                        case AUTO_SEQ_CENLFE:
2149                                spec->multiout.dac_nids[i] = 0x24;
2150                                break;
2151                        case AUTO_SEQ_SURROUND:
2152                                spec->multiout.dac_nids[i] = 0x11;
2153                                break;
2154                        case AUTO_SEQ_SIDE:
2155                                spec->multiout.dac_nids[i] = 0x25;
2156                                break;
2157                        }
2158                }
2159        }
2160
2161        return 0;
2162}
2163
2164/* add playback controls from the parsed DAC table */
2165static int vt1708B_auto_create_multi_out_ctls(struct via_spec *spec,
2166                                             const struct auto_pin_cfg *cfg)
2167{
2168        char name[32];
2169        static const char *chname[4] = { "Front", "Surround", "C/LFE", "Side" };
2170        hda_nid_t nid_vols[] = {0x16, 0x18, 0x26, 0x27};
2171        hda_nid_t nid, nid_vol = 0;
2172        int i, err;
2173
2174        for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
2175                nid = cfg->line_out_pins[i];
2176
2177                if (!nid)
2178                        continue;
2179
2180                nid_vol = nid_vols[i];
2181
2182                if (i == AUTO_SEQ_CENLFE) {
2183                        /* Center/LFE */
2184                        err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2185                                              "Center Playback Volume",
2186                                              HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
2187                                                                  HDA_OUTPUT));
2188                        if (err < 0)
2189                                return err;
2190                        err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2191                                              "LFE Playback Volume",
2192                                              HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
2193                                                                  HDA_OUTPUT));
2194                        if (err < 0)
2195                                return err;
2196                        err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2197                                              "Center Playback Switch",
2198                                              HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
2199                                                                  HDA_OUTPUT));
2200                        if (err < 0)
2201                                return err;
2202                        err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2203                                              "LFE Playback Switch",
2204                                              HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
2205                                                                  HDA_OUTPUT));
2206                        if (err < 0)
2207                                return err;
2208                } else if (i == AUTO_SEQ_FRONT) {
2209                        /* add control to mixer index 0 */
2210                        err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2211                                              "Master Front Playback Volume",
2212                                              HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2213                                                                  HDA_INPUT));
2214                        if (err < 0)
2215                                return err;
2216                        err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2217                                              "Master Front Playback Switch",
2218                                              HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2219                                                                  HDA_INPUT));
2220                        if (err < 0)
2221                                return err;
2222
2223                        /* add control to PW3 */
2224                        sprintf(name, "%s Playback Volume", chname[i]);
2225                        err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
2226                                              HDA_COMPOSE_AMP_VAL(nid, 3, 0,
2227                                                                  HDA_OUTPUT));
2228                        if (err < 0)
2229                                return err;
2230                        sprintf(name, "%s Playback Switch", chname[i]);
2231                        err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
2232                                              HDA_COMPOSE_AMP_VAL(nid, 3, 0,
2233                                                                  HDA_OUTPUT));
2234                        if (err < 0)
2235                                return err;
2236                } else {
2237                        sprintf(name, "%s Playback Volume", chname[i]);
2238                        err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
2239                                              HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2240                                                                  HDA_OUTPUT));
2241                        if (err < 0)
2242                                return err;
2243                        sprintf(name, "%s Playback Switch", chname[i]);
2244                        err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
2245                                              HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2246                                                                  HDA_OUTPUT));
2247                        if (err < 0)
2248                                return err;
2249                }
2250        }
2251
2252        return 0;
2253}
2254
2255static int vt1708B_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
2256{
2257        int err;
2258
2259        if (!pin)
2260                return 0;
2261
2262        spec->multiout.hp_nid = VT1708B_HP_NID; /* AOW3 */
2263
2264        err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2265                              "Headphone Playback Volume",
2266                              HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
2267        if (err < 0)
2268                return err;
2269        err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2270                              "Headphone Playback Switch",
2271                              HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
2272        if (err < 0)
2273                return err;
2274
2275        create_hp_imux(spec);
2276
2277        return 0;
2278}
2279
2280/* create playback/capture controls for input pins */
2281static int vt1708B_auto_create_analog_input_ctls(struct via_spec *spec,
2282                                                const struct auto_pin_cfg *cfg)
2283{
2284        static char *labels[] = {
2285                "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux", NULL
2286        };
2287        struct hda_input_mux *imux = &spec->private_imux[0];
2288        int i, err, idx = 0;
2289
2290        /* for internal loopback recording select */
2291        imux->items[imux->num_items].label = "Stereo Mixer";
2292        imux->items[imux->num_items].index = idx;
2293        imux->num_items++;
2294
2295        for (i = 0; i < AUTO_PIN_LAST; i++) {
2296                if (!cfg->input_pins[i])
2297                        continue;
2298
2299                switch (cfg->input_pins[i]) {
2300                case 0x1a: /* Mic */
2301                        idx = 2;
2302                        break;
2303
2304                case 0x1b: /* Line In */
2305                        idx = 3;
2306                        break;
2307
2308                case 0x1e: /* Front Mic */
2309                        idx = 4;
2310                        break;
2311
2312                case 0x1f: /* CD */
2313                        idx = 1;
2314                        break;
2315                }
2316                err = via_new_analog_input(spec, cfg->input_pins[i], labels[i],
2317                                           idx, 0x16);
2318                if (err < 0)
2319                        return err;
2320                imux->items[imux->num_items].label = labels[i];
2321                imux->items[imux->num_items].index = idx;
2322                imux->num_items++;
2323        }
2324        return 0;
2325}
2326
2327static int vt1708B_parse_auto_config(struct hda_codec *codec)
2328{
2329        struct via_spec *spec = codec->spec;
2330        int err;
2331
2332        err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
2333        if (err < 0)
2334                return err;
2335        err = vt1708B_auto_fill_dac_nids(spec, &spec->autocfg);
2336        if (err < 0)
2337                return err;
2338        if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
2339                return 0; /* can't find valid BIOS pin config */
2340
2341        err = vt1708B_auto_create_multi_out_ctls(spec, &spec->autocfg);
2342        if (err < 0)
2343                return err;
2344        err = vt1708B_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
2345        if (err < 0)
2346                return err;
2347        err = vt1708B_auto_create_analog_input_ctls(spec, &spec->autocfg);
2348        if (err < 0)
2349                return err;
2350
2351        spec->multiout.max_channels = spec->multiout.num_dacs * 2;
2352
2353        if (spec->autocfg.dig_outs)
2354                spec->multiout.dig_out_nid = VT1708B_DIGOUT_NID;
2355        spec->dig_in_pin = VT1708B_DIGIN_PIN;
2356        if (spec->autocfg.dig_in_pin)
2357                spec->dig_in_nid = VT1708B_DIGIN_NID;
2358
2359        if (spec->kctls.list)
2360                spec->mixers[spec->num_mixers++] = spec->kctls.list;
2361
2362        spec->input_mux = &spec->private_imux[0];
2363
2364        if (spec->hp_mux)
2365                spec->mixers[spec->num_mixers++] = via_hp_mixer;
2366
2367        return 1;
2368}
2369
2370#ifdef CONFIG_SND_HDA_POWER_SAVE
2371static struct hda_amp_list vt1708B_loopbacks[] = {
2372        { 0x16, HDA_INPUT, 1 },
2373        { 0x16, HDA_INPUT, 2 },
2374        { 0x16, HDA_INPUT, 3 },
2375        { 0x16, HDA_INPUT, 4 },
2376        { } /* end */
2377};
2378#endif
2379
2380static int patch_vt1708B_8ch(struct hda_codec *codec)
2381{
2382        struct via_spec *spec;
2383        int err;
2384
2385        /* create a codec specific record */
2386        spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2387        if (spec == NULL)
2388                return -ENOMEM;
2389
2390        codec->spec = spec;
2391
2392        /* automatic parse from the BIOS config */
2393        err = vt1708B_parse_auto_config(codec);
2394        if (err < 0) {
2395                via_free(codec);
2396                return err;
2397        } else if (!err) {
2398                printk(KERN_INFO "hda_codec: Cannot set up configuration "
2399                       "from BIOS.  Using genenic mode...\n");
2400        }
2401
2402        spec->init_verbs[spec->num_iverbs++] = vt1708B_8ch_volume_init_verbs;
2403        spec->init_verbs[spec->num_iverbs++] = vt1708B_uniwill_init_verbs;
2404
2405        spec->stream_name_analog = "VT1708B Analog";
2406        spec->stream_analog_playback = &vt1708B_8ch_pcm_analog_playback;
2407        spec->stream_analog_capture = &vt1708B_pcm_analog_capture;
2408
2409        spec->stream_name_digital = "VT1708B Digital";
2410        spec->stream_digital_playback = &vt1708B_pcm_digital_playback;
2411        spec->stream_digital_capture = &vt1708B_pcm_digital_capture;
2412
2413        if (!spec->adc_nids && spec->input_mux) {
2414                spec->adc_nids = vt1708B_adc_nids;
2415                spec->num_adc_nids = ARRAY_SIZE(vt1708B_adc_nids);
2416                get_mux_nids(codec);
2417                spec->mixers[spec->num_mixers] = vt1708B_capture_mixer;
2418                spec->num_mixers++;
2419        }
2420
2421        codec->patch_ops = via_patch_ops;
2422
2423        codec->patch_ops.init = via_auto_init;
2424        codec->patch_ops.unsol_event = via_unsol_event;
2425#ifdef CONFIG_SND_HDA_POWER_SAVE
2426        spec->loopback.amplist = vt1708B_loopbacks;
2427#endif
2428
2429        return 0;
2430}
2431
2432static int patch_vt1708B_4ch(struct hda_codec *codec)
2433{
2434        struct via_spec *spec;
2435        int err;
2436
2437        /* create a codec specific record */
2438        spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2439        if (spec == NULL)
2440                return -ENOMEM;
2441
2442        codec->spec = spec;
2443
2444        /* automatic parse from the BIOS config */
2445        err = vt1708B_parse_auto_config(codec);
2446        if (err < 0) {
2447                via_free(codec);
2448                return err;
2449        } else if (!err) {
2450                printk(KERN_INFO "hda_codec: Cannot set up configuration "
2451                       "from BIOS.  Using genenic mode...\n");
2452        }
2453
2454        spec->init_verbs[spec->num_iverbs++] = vt1708B_4ch_volume_init_verbs;
2455        spec->init_verbs[spec->num_iverbs++] = vt1708B_uniwill_init_verbs;
2456
2457        spec->stream_name_analog = "VT1708B Analog";
2458        spec->stream_analog_playback = &vt1708B_4ch_pcm_analog_playback;
2459        spec->stream_analog_capture = &vt1708B_pcm_analog_capture;
2460
2461        spec->stream_name_digital = "VT1708B Digital";
2462        spec->stream_digital_playback = &vt1708B_pcm_digital_playback;
2463        spec->stream_digital_capture = &vt1708B_pcm_digital_capture;
2464
2465        if (!spec->adc_nids && spec->input_mux) {
2466                spec->adc_nids = vt1708B_adc_nids;
2467                spec->num_adc_nids = ARRAY_SIZE(vt1708B_adc_nids);
2468                get_mux_nids(codec);
2469                spec->mixers[spec->num_mixers] = vt1708B_capture_mixer;
2470                spec->num_mixers++;
2471        }
2472
2473        codec->patch_ops = via_patch_ops;
2474
2475        codec->patch_ops.init = via_auto_init;
2476        codec->patch_ops.unsol_event = via_unsol_event;
2477#ifdef CONFIG_SND_HDA_POWER_SAVE
2478        spec->loopback.amplist = vt1708B_loopbacks;
2479#endif
2480
2481        return 0;
2482}
2483
2484/* Patch for VT1708S */
2485
2486/* VT1708S software backdoor based override for buggy hardware micboost
2487 * setting */
2488#define MIC_BOOST_VOLUME(xname, nid) {                          \
2489        .iface = SNDRV_CTL_ELEM_IFACE_MIXER,            \
2490        .name = xname,                                  \
2491        .index = 0,                                     \
2492        .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |     \
2493        SNDRV_CTL_ELEM_ACCESS_TLV_READ |                \
2494        SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK,             \
2495        .info = mic_boost_volume_info,                  \
2496        .get = snd_hda_mixer_amp_volume_get,            \
2497        .put = snd_hda_mixer_amp_volume_put,            \
2498        .tlv = { .c = mic_boost_tlv },                  \
2499        .private_value = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT) }
2500
2501/* capture mixer elements */
2502static struct snd_kcontrol_new vt1708S_capture_mixer[] = {
2503        HDA_CODEC_VOLUME("Capture Volume", 0x13, 0x0, HDA_INPUT),
2504        HDA_CODEC_MUTE("Capture Switch", 0x13, 0x0, HDA_INPUT),
2505        HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x14, 0x0, HDA_INPUT),
2506        HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x14, 0x0, HDA_INPUT),
2507        MIC_BOOST_VOLUME("Mic Boost Capture Volume", 0x1A),
2508        MIC_BOOST_VOLUME("Front Mic Boost Capture Volume", 0x1E),
2509        {
2510                .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2511                /* The multiple "Capture Source" controls confuse alsamixer
2512                 * So call somewhat different..
2513                 */
2514                /* .name = "Capture Source", */
2515                .name = "Input Source",
2516                .count = 1,
2517                .info = via_mux_enum_info,
2518                .get = via_mux_enum_get,
2519                .put = via_mux_enum_put,
2520        },
2521        { } /* end */
2522};
2523
2524static struct hda_verb vt1708S_volume_init_verbs[] = {
2525        /* Unmute ADC0-1 and set the default input to mic-in */
2526        {0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2527        {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2528
2529        /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the
2530         * analog-loopback mixer widget */
2531        /* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
2532        {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2533        {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2534        {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
2535        {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
2536        {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
2537
2538        /* Setup default input of PW4 to MW0 */
2539        {0x1d, AC_VERB_SET_CONNECT_SEL, 0x0},
2540        /* PW9, PW10  Output enable */
2541        {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
2542        {0x21, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
2543        /* Enable Mic Boost Volume backdoor */
2544        {0x1, 0xf98, 0x1},
2545        { }
2546};
2547
2548static struct hda_verb vt1708S_uniwill_init_verbs[] = {
2549        {0x1D, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_HP_EVENT},
2550        { }
2551};
2552
2553static struct hda_pcm_stream vt1708S_pcm_analog_playback = {
2554        .substreams = 2,
2555        .channels_min = 2,
2556        .channels_max = 8,
2557        .nid = 0x10, /* NID to query formats and rates */
2558        .ops = {
2559                .open = via_playback_pcm_open,
2560                .prepare = via_playback_pcm_prepare,
2561                .cleanup = via_playback_pcm_cleanup
2562        },
2563};
2564
2565static struct hda_pcm_stream vt1708S_pcm_analog_capture = {
2566        .substreams = 2,
2567        .channels_min = 2,
2568        .channels_max = 2,
2569        .nid = 0x13, /* NID to query formats and rates */
2570        .ops = {
2571                .prepare = via_capture_pcm_prepare,
2572                .cleanup = via_capture_pcm_cleanup
2573        },
2574};
2575
2576static struct hda_pcm_stream vt1708S_pcm_digital_playback = {
2577        .substreams = 1,
2578        .channels_min = 2,
2579        .channels_max = 2,
2580        /* NID is set in via_build_pcms */
2581        .ops = {
2582                .open = via_dig_playback_pcm_open,
2583                .close = via_dig_playback_pcm_close,
2584                .prepare = via_dig_playback_pcm_prepare,
2585                .cleanup = via_dig_playback_pcm_cleanup
2586        },
2587};
2588
2589/* fill in the dac_nids table from the parsed pin configuration */
2590static int vt1708S_auto_fill_dac_nids(struct via_spec *spec,
2591                                     const struct auto_pin_cfg *cfg)
2592{
2593        int i;
2594        hda_nid_t nid;
2595
2596        spec->multiout.num_dacs = cfg->line_outs;
2597
2598        spec->multiout.dac_nids = spec->private_dac_nids;
2599
2600        for (i = 0; i < 4; i++) {
2601                nid = cfg->line_out_pins[i];
2602                if (nid) {
2603                        /* config dac list */
2604                        switch (i) {
2605                        case AUTO_SEQ_FRONT:
2606                                spec->multiout.dac_nids[i] = 0x10;
2607                                break;
2608                        case AUTO_SEQ_CENLFE:
2609                                spec->multiout.dac_nids[i] = 0x24;
2610                                break;
2611                        case AUTO_SEQ_SURROUND:
2612                                spec->multiout.dac_nids[i] = 0x11;
2613                                break;
2614                        case AUTO_SEQ_SIDE:
2615                                spec->multiout.dac_nids[i] = 0x25;
2616                                break;
2617                        }
2618                }
2619        }
2620
2621        return 0;
2622}
2623
2624/* add playback controls from the parsed DAC table */
2625static int vt1708S_auto_create_multi_out_ctls(struct via_spec *spec,
2626                                             const struct auto_pin_cfg *cfg)
2627{
2628        char name[32];
2629        static const char *chname[4] = { "Front", "Surround", "C/LFE", "Side" };
2630        hda_nid_t nid_vols[] = {0x10, 0x11, 0x24, 0x25};
2631        hda_nid_t nid_mutes[] = {0x1C, 0x18, 0x26, 0x27};
2632        hda_nid_t nid, nid_vol, nid_mute;
2633        int i, err;
2634
2635        for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
2636                nid = cfg->line_out_pins[i];
2637
2638                if (!nid)
2639                        continue;
2640
2641                nid_vol = nid_vols[i];
2642                nid_mute = nid_mutes[i];
2643
2644                if (i == AUTO_SEQ_CENLFE) {
2645                        /* Center/LFE */
2646                        err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2647                                              "Center Playback Volume",
2648                                              HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0,
2649                                                                  HDA_OUTPUT));
2650                        if (err < 0)
2651                                return err;
2652                        err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2653                                              "LFE Playback Volume",
2654                                              HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0,
2655                                                                  HDA_OUTPUT));
2656                        if (err < 0)
2657                                return err;
2658                        err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2659                                              "Center Playback Switch",
2660                                              HDA_COMPOSE_AMP_VAL(nid_mute,
2661                                                                  1, 0,
2662                                                                  HDA_OUTPUT));
2663                        if (err < 0)
2664                                return err;
2665                        err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2666                                              "LFE Playback Switch",
2667                                              HDA_COMPOSE_AMP_VAL(nid_mute,
2668                                                                  2, 0,
2669                                                                  HDA_OUTPUT));
2670                        if (err < 0)
2671                                return err;
2672                } else if (i == AUTO_SEQ_FRONT) {
2673                        /* add control to mixer index 0 */
2674                        err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2675                                              "Master Front Playback Volume",
2676                                              HDA_COMPOSE_AMP_VAL(0x16, 3, 0,
2677                                                                  HDA_INPUT));
2678                        if (err < 0)
2679                                return err;
2680                        err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2681                                              "Master Front Playback Switch",
2682                                              HDA_COMPOSE_AMP_VAL(0x16, 3, 0,
2683                                                                  HDA_INPUT));
2684                        if (err < 0)
2685                                return err;
2686
2687                        /* Front */
2688                        sprintf(name, "%s Playback Volume", chname[i]);
2689                        err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
2690                                              HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2691                                                                  HDA_OUTPUT));
2692                        if (err < 0)
2693                                return err;
2694                        sprintf(name, "%s Playback Switch", chname[i]);
2695                        err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
2696                                              HDA_COMPOSE_AMP_VAL(nid_mute,
2697                                                                  3, 0,
2698                                                                  HDA_OUTPUT));
2699                        if (err < 0)
2700                                return err;
2701                } else {
2702                        sprintf(name, "%s Playback Volume", chname[i]);
2703                        err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
2704                                              HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0,
2705                                                                  HDA_OUTPUT));
2706                        if (err < 0)
2707                                return err;
2708                        sprintf(name, "%s Playback Switch", chname[i]);
2709                        err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
2710                                              HDA_COMPOSE_AMP_VAL(nid_mute,
2711                                                                  3, 0,
2712                                                                  HDA_OUTPUT));
2713                        if (err < 0)
2714                                return err;
2715                }
2716        }
2717
2718        return 0;
2719}
2720
2721static int vt1708S_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
2722{
2723        int err;
2724
2725        if (!pin)
2726                return 0;
2727
2728        spec->multiout.hp_nid = VT1708S_HP_NID; /* AOW3 */
2729
2730        err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
2731                              "Headphone Playback Volume",
2732                              HDA_COMPOSE_AMP_VAL(0x25, 3, 0, HDA_OUTPUT));
2733        if (err < 0)
2734                return err;
2735
2736        err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
2737                              "Headphone Playback Switch",
2738                              HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
2739        if (err < 0)
2740                return err;
2741
2742        create_hp_imux(spec);
2743
2744        return 0;
2745}
2746
2747/* create playback/capture controls for input pins */
2748static int vt1708S_auto_create_analog_input_ctls(struct via_spec *spec,
2749                                                const struct auto_pin_cfg *cfg)
2750{
2751        static char *labels[] = {
2752                "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux", NULL
2753        };
2754        struct hda_input_mux *imux = &spec->private_imux[0];
2755        int i, err, idx = 0;
2756
2757        /* for internal loopback recording select */
2758        imux->items[imux->num_items].label = "Stereo Mixer";
2759        imux->items[imux->num_items].index = 5;
2760        imux->num_items++;
2761
2762        for (i = 0; i < AUTO_PIN_LAST; i++) {
2763                if (!cfg->input_pins[i])
2764                        continue;
2765
2766                switch (cfg->input_pins[i]) {
2767                case 0x1a: /* Mic */
2768                        idx = 2;
2769                        break;
2770
2771                case 0x1b: /* Line In */
2772                        idx = 3;
2773                        break;
2774
2775                case 0x1e: /* Front Mic */
2776                        idx = 4;
2777                        break;
2778
2779                case 0x1f: /* CD */
2780                        idx = 1;
2781                        break;
2782                }
2783                err = via_new_analog_input(spec, cfg->input_pins[i], labels[i],
2784                                           idx, 0x16);
2785                if (err < 0)
2786                        return err;
2787                imux->items[imux->num_items].label = labels[i];
2788                imux->items[imux->num_items].index = idx-1;
2789                imux->num_items++;
2790        }
2791        return 0;
2792}
2793
2794/* fill out digital output widgets; one for master and one for slave outputs */
2795static void fill_dig_outs(struct hda_codec *codec)
2796{
2797        struct via_spec *spec = codec->spec;
2798        int i;
2799
2800        for (i = 0; i < spec->autocfg.dig_outs; i++) {
2801                hda_nid_t nid;
2802                int conn;
2803
2804                nid = spec->autocfg.dig_out_pins[i];
2805                if (!nid)
2806                        continue;
2807                conn = snd_hda_get_connections(codec, nid, &nid, 1);
2808                if (conn < 1)
2809                        continue;
2810                if (!spec->multiout.dig_out_nid)
2811                        spec->multiout.dig_out_nid = nid;
2812                else {
2813                        spec->slave_dig_outs[0] = nid;
2814                        break; /* at most two dig outs */
2815                }
2816        }
2817}
2818
2819static int vt1708S_parse_auto_config(struct hda_codec *codec)
2820{
2821        struct via_spec *spec = codec->spec;
2822        int err;
2823
2824        err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
2825        if (err < 0)
2826                return err;
2827        err = vt1708S_auto_fill_dac_nids(spec, &spec->autocfg);
2828        if (err < 0)
2829                return err;
2830        if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
2831                return 0; /* can't find valid BIOS pin config */
2832
2833        err = vt1708S_auto_create_multi_out_ctls(spec, &spec->autocfg);
2834        if (err < 0)
2835                return err;
2836        err = vt1708S_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
2837        if (err < 0)
2838                return err;
2839        err = vt1708S_auto_create_analog_input_ctls(spec, &spec->autocfg);
2840        if (err < 0)
2841                return err;
2842
2843        spec->multiout.max_channels = spec->multiout.num_dacs * 2;
2844
2845        fill_dig_outs(codec);
2846
2847        if (spec->kctls.list)
2848                spec->mixers[spec->num_mixers++] = spec->kctls.list;
2849
2850        spec->input_mux = &spec->private_imux[0];
2851
2852        if (spec->hp_mux)
2853                spec->mixers[spec->num_mixers++] = via_hp_mixer;
2854
2855        return 1;
2856}
2857
2858#ifdef CONFIG_SND_HDA_POWER_SAVE
2859static struct hda_amp_list vt1708S_loopbacks[] = {
2860        { 0x16, HDA_INPUT, 1 },
2861        { 0x16, HDA_INPUT, 2 },
2862        { 0x16, HDA_INPUT, 3 },
2863        { 0x16, HDA_INPUT, 4 },
2864        { } /* end */
2865};
2866#endif
2867
2868static int patch_vt1708S(struct hda_codec *codec)
2869{
2870        struct via_spec *spec;
2871        int err;
2872
2873        /* create a codec specific record */
2874        spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2875        if (spec == NULL)
2876                return -ENOMEM;
2877
2878        codec->spec = spec;
2879
2880        /* automatic parse from the BIOS config */
2881        err = vt1708S_parse_auto_config(codec);
2882        if (err < 0) {
2883                via_free(codec);
2884                return err;
2885        } else if (!err) {
2886                printk(KERN_INFO "hda_codec: Cannot set up configuration "
2887                       "from BIOS.  Using genenic mode...\n");
2888        }
2889
2890        spec->init_verbs[spec->num_iverbs++] = vt1708S_volume_init_verbs;
2891        spec->init_verbs[spec->num_iverbs++] = vt1708S_uniwill_init_verbs;
2892
2893        spec->stream_name_analog = "VT1708S Analog";
2894        spec->stream_analog_playback = &vt1708S_pcm_analog_playback;
2895        spec->stream_analog_capture = &vt1708S_pcm_analog_capture;
2896
2897        spec->stream_name_digital = "VT1708S Digital";
2898        spec->stream_digital_playback = &vt1708S_pcm_digital_playback;
2899
2900        if (!spec->adc_nids && spec->input_mux) {
2901                spec->adc_nids = vt1708S_adc_nids;
2902                spec->num_adc_nids = ARRAY_SIZE(vt1708S_adc_nids);
2903                get_mux_nids(codec);
2904                spec->mixers[spec->num_mixers] = vt1708S_capture_mixer;
2905                spec->num_mixers++;
2906        }
2907
2908        codec->patch_ops = via_patch_ops;
2909
2910        codec->patch_ops.init = via_auto_init;
2911        codec->patch_ops.unsol_event = via_unsol_event;
2912#ifdef CONFIG_SND_HDA_POWER_SAVE
2913        spec->loopback.amplist = vt1708S_loopbacks;
2914#endif
2915
2916        return 0;
2917}
2918
2919/* Patch for VT1702 */
2920
2921/* capture mixer elements */
2922static struct snd_kcontrol_new vt1702_capture_mixer[] = {
2923        HDA_CODEC_VOLUME("Capture Volume", 0x12, 0x0, HDA_INPUT),
2924        HDA_CODEC_MUTE("Capture Switch", 0x12, 0x0, HDA_INPUT),
2925        HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x20, 0x0, HDA_INPUT),
2926        HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x20, 0x0, HDA_INPUT),
2927        HDA_CODEC_VOLUME("Digital Mic Capture Volume", 0x1F, 0x0, HDA_INPUT),
2928        HDA_CODEC_MUTE("Digital Mic Capture Switch", 0x1F, 0x0, HDA_INPUT),
2929        HDA_CODEC_VOLUME("Digital Mic Boost Capture Volume", 0x1E, 0x0,
2930                         HDA_INPUT),
2931        {
2932                .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2933                /* The multiple "Capture Source" controls confuse alsamixer
2934                 * So call somewhat different..
2935                 */
2936                /* .name = "Capture Source", */
2937                .name = "Input Source",
2938                .count = 1,
2939                .info = via_mux_enum_info,
2940                .get = via_mux_enum_get,
2941                .put = via_mux_enum_put,
2942        },
2943        { } /* end */
2944};
2945
2946static struct hda_verb vt1702_volume_init_verbs[] = {
2947        /*
2948         * Unmute ADC0-1 and set the default input to mic-in
2949         */
2950        {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2951        {0x1F, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2952        {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2953
2954
2955        /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
2956         * mixer widget
2957         */
2958        /* Amp Indices: Mic1 = 1, Line = 1, Mic2 = 3 */
2959        {0x1A, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
2960        {0x1A, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
2961        {0x1A, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
2962        {0x1A, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
2963        {0x1A, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
2964
2965        /* Setup default input of PW4 to MW0 */
2966        {0x17, AC_VERB_SET_CONNECT_SEL, 0x1},
2967        /* PW6 PW7 Output enable */
2968        {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
2969        {0x1C, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
2970        { }
2971};
2972
2973static struct hda_verb vt1702_uniwill_init_verbs[] = {
2974        {0x01, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_GPIO_EVENT},
2975        {0x17, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | VIA_HP_EVENT},
2976        { }
2977};
2978
2979static struct hda_pcm_stream vt1702_pcm_analog_playback = {
2980        .substreams = 2,
2981        .channels_min = 2,
2982        .channels_max = 2,
2983        .nid = 0x10, /* NID to query formats and rates */
2984        .ops = {
2985                .open = via_playback_pcm_open,
2986                .prepare = via_playback_multi_pcm_prepare,
2987                .cleanup = via_playback_multi_pcm_cleanup
2988        },
2989};
2990
2991static struct hda_pcm_stream vt1702_pcm_analog_capture = {
2992        .substreams = 3,
2993        .channels_min = 2,
2994        .channels_max = 2,
2995        .nid = 0x12, /* NID to query formats and rates */
2996        .ops = {
2997                .prepare = via_capture_pcm_prepare,
2998                .cleanup = via_capture_pcm_cleanup
2999        },
3000};
3001
3002static struct hda_pcm_stream vt1702_pcm_digital_playback = {
3003        .substreams = 2,
3004        .channels_min = 2,
3005        .channels_max = 2,
3006        /* NID is set in via_build_pcms */
3007        .ops = {
3008                .open = via_dig_playback_pcm_open,
3009                .close = via_dig_playback_pcm_close,
3010                .prepare = via_dig_playback_pcm_prepare,
3011                .cleanup = via_dig_playback_pcm_cleanup
3012        },
3013};
3014
3015/* fill in the dac_nids table from the parsed pin configuration */
3016static int vt1702_auto_fill_dac_nids(struct via_spec *spec,
3017                                     const struct auto_pin_cfg *cfg)
3018{
3019        spec->multiout.num_dacs = 1;
3020        spec->multiout.dac_nids = spec->private_dac_nids;
3021
3022        if (cfg->line_out_pins[0]) {
3023                /* config dac list */
3024                spec->multiout.dac_nids[0] = 0x10;
3025        }
3026
3027        return 0;
3028}
3029
3030/* add playback controls from the parsed DAC table */
3031static int vt1702_auto_create_line_out_ctls(struct via_spec *spec,
3032                                             const struct auto_pin_cfg *cfg)
3033{
3034        int err;
3035
3036        if (!cfg->line_out_pins[0])
3037                return -1;
3038
3039        /* add control to mixer index 0 */
3040        err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3041                              "Master Front Playback Volume",
3042                              HDA_COMPOSE_AMP_VAL(0x1A, 3, 0, HDA_INPUT));
3043        if (err < 0)
3044                return err;
3045        err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3046                              "Master Front Playback Switch",
3047                              HDA_COMPOSE_AMP_VAL(0x1A, 3, 0, HDA_INPUT));
3048        if (err < 0)
3049                return err;
3050
3051        /* Front */
3052        err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3053                              "Front Playback Volume",
3054                              HDA_COMPOSE_AMP_VAL(0x10, 3, 0, HDA_OUTPUT));
3055        if (err < 0)
3056                return err;
3057        err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3058                              "Front Playback Switch",
3059                              HDA_COMPOSE_AMP_VAL(0x16, 3, 0, HDA_OUTPUT));
3060        if (err < 0)
3061                return err;
3062
3063        return 0;
3064}
3065
3066static int vt1702_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
3067{
3068        int err;
3069
3070        if (!pin)
3071                return 0;
3072
3073        spec->multiout.hp_nid = 0x1D;
3074
3075        err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
3076                              "Headphone Playback Volume",
3077                              HDA_COMPOSE_AMP_VAL(0x1D, 3, 0, HDA_OUTPUT));
3078        if (err < 0)
3079                return err;
3080
3081        err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
3082                              "Headphone Playback Switch",
3083                              HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
3084        if (err < 0)
3085                return err;
3086
3087        create_hp_imux(spec);
3088
3089        return 0;
3090}
3091
3092/* create playback/capture controls for input pins */
3093static int vt1702_auto_create_analog_input_ctls(struct via_spec *spec,
3094                                                const struct auto_pin_cfg *cfg)
3095{
3096        static char *labels[] = {
3097                "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux", NULL
3098        };
3099        struct hda_input_mux *imux = &spec->private_imux[0];
3100        int i, err, idx = 0;
3101
3102        /* for internal loopback recording select */
3103        imux->items[imux->num_items].label = "Stereo Mixer";
3104        imux->items[imux->num_items].index = 3;
3105        imux->num_items++;
3106
3107        for (i = 0; i < AUTO_PIN_LAST; i++) {
3108                if (!cfg->input_pins[i])
3109                        continue;
3110
3111                switch (cfg->input_pins[i]) {
3112                case 0x14: /* Mic */
3113                        idx = 1;
3114                        break;
3115
3116                case 0x15: /* Line In */
3117                        idx = 2;
3118                        break;
3119
3120                case 0x18: /* Front Mic */
3121                        idx = 3;
3122                        break;
3123                }
3124                err = via_new_analog_input(spec, cfg->input_pins[i],
3125                                           labels[i], idx, 0x1A);
3126                if (err < 0)
3127                        return err;
3128                imux->items[imux->num_items].label = labels[i];
3129                imux->items[imux->num_items].index = idx-1;
3130                imux->num_items++;
3131        }
3132        return 0;
3133}
3134
3135static int vt1702_parse_auto_config(struct hda_codec *codec)
3136{
3137        struct via_spec *spec = codec->spec;
3138        int err;
3139
3140        err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
3141        if (err < 0)
3142                return err;
3143        err = vt1702_auto_fill_dac_nids(spec, &spec->autocfg);
3144        if (err < 0)
3145                return err;
3146        if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
3147                return 0; /* can't find valid BIOS pin config */
3148
3149        err = vt1702_auto_create_line_out_ctls(spec, &spec->autocfg);
3150        if (err < 0)
3151                return err;
3152        err = vt1702_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
3153        if (err < 0)
3154                return err;
3155        err = vt1702_auto_create_analog_input_ctls(spec, &spec->autocfg);
3156        if (err < 0)
3157                return err;
3158
3159        spec->multiout.max_channels = spec->multiout.num_dacs * 2;
3160
3161        fill_dig_outs(codec);
3162
3163        if (spec->kctls.list)
3164                spec->mixers[spec->num_mixers++] = spec->kctls.list;
3165
3166        spec->input_mux = &spec->private_imux[0];
3167
3168        if (spec->hp_mux)
3169                spec->mixers[spec->num_mixers++] = via_hp_mixer;
3170
3171        return 1;
3172}
3173
3174#ifdef CONFIG_SND_HDA_POWER_SAVE
3175static struct hda_amp_list vt1702_loopbacks[] = {
3176        { 0x1A, HDA_INPUT, 1 },
3177        { 0x1A, HDA_INPUT, 2 },
3178        { 0x1A, HDA_INPUT, 3 },
3179        { 0x1A, HDA_INPUT, 4 },
3180        { } /* end */
3181};
3182#endif
3183
3184static int patch_vt1702(struct hda_codec *codec)
3185{
3186        struct via_spec *spec;
3187        int err;
3188        unsigned int response;
3189        unsigned char control;
3190
3191        /* create a codec specific record */
3192        spec = kzalloc(sizeof(*spec), GFP_KERNEL);
3193        if (spec == NULL)
3194                return -ENOMEM;
3195
3196        codec->spec = spec;
3197
3198        /* automatic parse from the BIOS config */
3199        err = vt1702_parse_auto_config(codec);
3200        if (err < 0) {
3201                via_free(codec);
3202                return err;
3203        } else if (!err) {
3204                printk(KERN_INFO "hda_codec: Cannot set up configuration "
3205                       "from BIOS.  Using genenic mode...\n");
3206        }
3207
3208        spec->init_verbs[spec->num_iverbs++] = vt1702_volume_init_verbs;
3209        spec->init_verbs[spec->num_iverbs++] = vt1702_uniwill_init_verbs;
3210
3211        spec->stream_name_analog = "VT1702 Analog";
3212        spec->stream_analog_playback = &vt1702_pcm_analog_playback;
3213        spec->stream_analog_capture = &vt1702_pcm_analog_capture;
3214
3215        spec->stream_name_digital = "VT1702 Digital";
3216        spec->stream_digital_playback = &vt1702_pcm_digital_playback;
3217
3218        if (!spec->adc_nids && spec->input_mux) {
3219                spec->adc_nids = vt1702_adc_nids;
3220                spec->num_adc_nids = ARRAY_SIZE(vt1702_adc_nids);
3221                get_mux_nids(codec);
3222                spec->mixers[spec->num_mixers] = vt1702_capture_mixer;
3223                spec->num_mixers++;
3224        }
3225
3226        codec->patch_ops = via_patch_ops;
3227
3228        codec->patch_ops.init = via_auto_init;
3229        codec->patch_ops.unsol_event = via_unsol_event;
3230#ifdef CONFIG_SND_HDA_POWER_SAVE
3231        spec->loopback.amplist = vt1702_loopbacks;
3232#endif
3233
3234        /* Open backdoor */
3235        response = snd_hda_codec_read(codec, codec->afg, 0, 0xF8C, 0);
3236        control = (unsigned char)(response & 0xff);
3237        control |= 0x3;
3238        snd_hda_codec_write(codec,  codec->afg, 0, 0xF88, control);
3239
3240        /* Enable GPIO 0&1 for volume&mute control */
3241        /* Enable GPIO 2 for DMIC-DATA */
3242        response = snd_hda_codec_read(codec, codec->afg, 0, 0xF84, 0);
3243        control = (unsigned char)((response >> 16) & 0x3f);
3244        snd_hda_codec_write(codec,  codec->afg, 0, 0xF82, control);
3245
3246        return 0;
3247}
3248
3249/*
3250 * patch entries
3251 */
3252static struct hda_codec_preset snd_hda_preset_via[] = {
3253        { .id = 0x11061708, .name = "VT1708", .patch = patch_vt1708},
3254        { .id = 0x11061709, .name = "VT1708", .patch = patch_vt1708},
3255        { .id = 0x1106170a, .name = "VT1708", .patch = patch_vt1708},
3256        { .id = 0x1106170b, .name = "VT1708", .patch = patch_vt1708},
3257        { .id = 0x1106e710, .name = "VT1709 10-Ch",
3258          .patch = patch_vt1709_10ch},
3259        { .id = 0x1106e711, .name = "VT1709 10-Ch",
3260          .patch = patch_vt1709_10ch},
3261        { .id = 0x1106e712, .name = "VT1709 10-Ch",
3262          .patch = patch_vt1709_10ch},
3263        { .id = 0x1106e713, .name = "VT1709 10-Ch",
3264          .patch = patch_vt1709_10ch},
3265        { .id = 0x1106e714, .name = "VT1709 6-Ch",
3266          .patch = patch_vt1709_6ch},
3267        { .id = 0x1106e715, .name = "VT1709 6-Ch",
3268          .patch = patch_vt1709_6ch},
3269        { .id = 0x1106e716, .name = "VT1709 6-Ch",
3270          .patch = patch_vt1709_6ch},
3271        { .id = 0x1106e717, .name = "VT1709 6-Ch",
3272          .patch = patch_vt1709_6ch},
3273        { .id = 0x1106e720, .name = "VT1708B 8-Ch",
3274          .patch = patch_vt1708B_8ch},
3275        { .id = 0x1106e721, .name = "VT1708B 8-Ch",
3276          .patch = patch_vt1708B_8ch},
3277        { .id = 0x1106e722, .name = "VT1708B 8-Ch",
3278          .patch = patch_vt1708B_8ch},
3279        { .id = 0x1106e723, .name = "VT1708B 8-Ch",
3280          .patch = patch_vt1708B_8ch},
3281        { .id = 0x1106e724, .name = "VT1708B 4-Ch",
3282          .patch = patch_vt1708B_4ch},
3283        { .id = 0x1106e725, .name = "VT1708B 4-Ch",
3284          .patch = patch_vt1708B_4ch},
3285        { .id = 0x1106e726, .name = "VT1708B 4-Ch",
3286          .patch = patch_vt1708B_4ch},
3287        { .id = 0x1106e727, .name = "VT1708B 4-Ch",
3288          .patch = patch_vt1708B_4ch},
3289        { .id = 0x11060397, .name = "VT1708S",
3290          .patch = patch_vt1708S},
3291        { .id = 0x11061397, .name = "VT1708S",
3292          .patch = patch_vt1708S},
3293        { .id = 0x11062397, .name = "VT1708S",
3294          .patch = patch_vt1708S},
3295        { .id = 0x11063397, .name = "VT1708S",
3296          .patch = patch_vt1708S},
3297        { .id = 0x11064397, .name = "VT1708S",
3298          .patch = patch_vt1708S},
3299        { .id = 0x11065397, .name = "VT1708S",
3300          .patch = patch_vt1708S},
3301        { .id = 0x11066397, .name = "VT1708S",
3302          .patch = patch_vt1708S},
3303        { .id = 0x11067397, .name = "VT1708S",
3304          .patch = patch_vt1708S},
3305        { .id = 0x11060398, .name = "VT1702",
3306          .patch = patch_vt1702},
3307        { .id = 0x11061398, .name = "VT1702",
3308          .patch = patch_vt1702},
3309        { .id = 0x11062398, .name = "VT1702",
3310          .patch = patch_vt1702},
3311        { .id = 0x11063398, .name = "VT1702",
3312          .patch = patch_vt1702},
3313        { .id = 0x11064398, .name = "VT1702",
3314          .patch = patch_vt1702},
3315        { .id = 0x11065398, .name = "VT1702",
3316          .patch = patch_vt1702},
3317        { .id = 0x11066398, .name = "VT1702",
3318          .patch = patch_vt1702},
3319        { .id = 0x11067398, .name = "VT1702",
3320          .patch = patch_vt1702},
3321        {} /* terminator */
3322};
3323
3324MODULE_ALIAS("snd-hda-codec-id:1106*");
3325
3326static struct hda_codec_preset_list via_list = {
3327        .preset = snd_hda_preset_via,
3328        .owner = THIS_MODULE,
3329};
3330
3331MODULE_LICENSE("GPL");
3332MODULE_DESCRIPTION("VIA HD-audio codec");
3333
3334static int __init patch_via_init(void)
3335{
3336        return snd_hda_add_codec_preset(&via_list);
3337}
3338
3339static void __exit patch_via_exit(void)
3340{
3341        snd_hda_delete_codec_preset(&via_list);
3342}
3343
3344module_init(patch_via_init)
3345module_exit(patch_via_exit)
3346