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