[ALSA] hda-codec - Fix VREF level of Mic inputs on STAC92xx codecs
[powerpc.git] / sound / pci / hda / patch_sigmatel.c
1 /*
2  * Universal Interface for Intel High Definition Audio Codec
3  *
4  * HD audio interface patch for SigmaTel STAC92xx
5  *
6  * Copyright (c) 2005 Embedded Alley Solutions, Inc.
7  * Matt Porter <mporter@embeddedalley.com>
8  *
9  * Based on patch_cmedia.c and patch_realtek.c
10  * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
11  *
12  *  This driver is free software; you can redistribute it and/or modify
13  *  it under the terms of the GNU General Public License as published by
14  *  the Free Software Foundation; either version 2 of the License, or
15  *  (at your option) any later version.
16  *
17  *  This driver is distributed in the hope that it will be useful,
18  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *  GNU General Public License for more details.
21  *
22  *  You should have received a copy of the GNU General Public License
23  *  along with this program; if not, write to the Free Software
24  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
25  */
26
27 #include <sound/driver.h>
28 #include <linux/init.h>
29 #include <linux/delay.h>
30 #include <linux/slab.h>
31 #include <linux/pci.h>
32 #include <sound/core.h>
33 #include <sound/asoundef.h>
34 #include "hda_codec.h"
35 #include "hda_local.h"
36
37 #define NUM_CONTROL_ALLOC       32
38 #define STAC_HP_EVENT           0x37
39 #define STAC_UNSOL_ENABLE       (AC_USRSP_EN | STAC_HP_EVENT)
40
41 #define STAC_REF                0
42 #define STAC_D945GTP3           1
43 #define STAC_D945GTP5           2
44
45 struct sigmatel_spec {
46         struct snd_kcontrol_new *mixers[4];
47         unsigned int num_mixers;
48
49         int board_config;
50         unsigned int surr_switch: 1;
51         unsigned int line_switch: 1;
52         unsigned int mic_switch: 1;
53         unsigned int alt_switch: 1;
54         unsigned int hp_detect: 1;
55
56         /* playback */
57         struct hda_multi_out multiout;
58         hda_nid_t dac_nids[5];
59
60         /* capture */
61         hda_nid_t *adc_nids;
62         unsigned int num_adcs;
63         hda_nid_t *mux_nids;
64         unsigned int num_muxes;
65         hda_nid_t dig_in_nid;
66
67         /* pin widgets */
68         hda_nid_t *pin_nids;
69         unsigned int num_pins;
70         unsigned int *pin_configs;
71
72         /* codec specific stuff */
73         struct hda_verb *init;
74         struct snd_kcontrol_new *mixer;
75
76         /* capture source */
77         struct hda_input_mux *input_mux;
78         unsigned int cur_mux[3];
79
80         /* i/o switches */
81         unsigned int io_switch[2];
82
83         struct hda_pcm pcm_rec[2];      /* PCM information */
84
85         /* dynamic controls and input_mux */
86         struct auto_pin_cfg autocfg;
87         unsigned int num_kctl_alloc, num_kctl_used;
88         struct snd_kcontrol_new *kctl_alloc;
89         struct hda_input_mux private_imux;
90 };
91
92 static hda_nid_t stac9200_adc_nids[1] = {
93         0x03,
94 };
95
96 static hda_nid_t stac9200_mux_nids[1] = {
97         0x0c,
98 };
99
100 static hda_nid_t stac9200_dac_nids[1] = {
101         0x02,
102 };
103
104 static hda_nid_t stac922x_adc_nids[2] = {
105         0x06, 0x07,
106 };
107
108 static hda_nid_t stac922x_mux_nids[2] = {
109         0x12, 0x13,
110 };
111
112 static hda_nid_t stac927x_adc_nids[3] = {
113         0x07, 0x08, 0x09
114 };
115
116 static hda_nid_t stac927x_mux_nids[3] = {
117         0x15, 0x16, 0x17
118 };
119
120 static hda_nid_t stac9200_pin_nids[8] = {
121         0x08, 0x09, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12,
122 };
123
124 static hda_nid_t stac922x_pin_nids[10] = {
125         0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
126         0x0f, 0x10, 0x11, 0x15, 0x1b,
127 };
128
129 static hda_nid_t stac927x_pin_nids[14] = {
130         0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
131         0x0f, 0x10, 0x11, 0x12, 0x13,
132         0x14, 0x21, 0x22, 0x23,
133 };
134
135 static int stac92xx_mux_enum_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
136 {
137         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
138         struct sigmatel_spec *spec = codec->spec;
139         return snd_hda_input_mux_info(spec->input_mux, uinfo);
140 }
141
142 static int stac92xx_mux_enum_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
143 {
144         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
145         struct sigmatel_spec *spec = codec->spec;
146         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
147
148         ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
149         return 0;
150 }
151
152 static int stac92xx_mux_enum_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
153 {
154         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
155         struct sigmatel_spec *spec = codec->spec;
156         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
157
158         return snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
159                                      spec->mux_nids[adc_idx], &spec->cur_mux[adc_idx]);
160 }
161
162 static struct hda_verb stac9200_core_init[] = {
163         /* set dac0mux for dac converter */
164         { 0x07, AC_VERB_SET_CONNECT_SEL, 0x00},
165         {}
166 };
167
168 static struct hda_verb stac922x_core_init[] = {
169         /* set master volume and direct control */      
170         { 0x16, AC_VERB_SET_VOLUME_KNOB_CONTROL, 0xff},
171         {}
172 };
173
174 static struct hda_verb stac927x_core_init[] = {
175         /* set master volume and direct control */      
176         { 0x24, AC_VERB_SET_VOLUME_KNOB_CONTROL, 0xff},
177         {}
178 };
179
180 static struct snd_kcontrol_new stac9200_mixer[] = {
181         HDA_CODEC_VOLUME("Master Playback Volume", 0xb, 0, HDA_OUTPUT),
182         HDA_CODEC_MUTE("Master Playback Switch", 0xb, 0, HDA_OUTPUT),
183         {
184                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
185                 .name = "Input Source",
186                 .count = 1,
187                 .info = stac92xx_mux_enum_info,
188                 .get = stac92xx_mux_enum_get,
189                 .put = stac92xx_mux_enum_put,
190         },
191         HDA_CODEC_VOLUME("Capture Volume", 0x0a, 0, HDA_OUTPUT),
192         HDA_CODEC_MUTE("Capture Switch", 0x0a, 0, HDA_OUTPUT),
193         HDA_CODEC_VOLUME("Capture Mux Volume", 0x0c, 0, HDA_OUTPUT),
194         { } /* end */
195 };
196
197 /* This needs to be generated dynamically based on sequence */
198 static struct snd_kcontrol_new stac922x_mixer[] = {
199         {
200                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
201                 .name = "Input Source",
202                 .count = 1,
203                 .info = stac92xx_mux_enum_info,
204                 .get = stac92xx_mux_enum_get,
205                 .put = stac92xx_mux_enum_put,
206         },
207         HDA_CODEC_VOLUME("Capture Volume", 0x17, 0x0, HDA_INPUT),
208         HDA_CODEC_MUTE("Capture Switch", 0x17, 0x0, HDA_INPUT),
209         HDA_CODEC_VOLUME("Mux Capture Volume", 0x12, 0x0, HDA_OUTPUT),
210         { } /* end */
211 };
212
213 static snd_kcontrol_new_t stac927x_mixer[] = {
214         {
215                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
216                 .name = "Input Source",
217                 .count = 1,
218                 .info = stac92xx_mux_enum_info,
219                 .get = stac92xx_mux_enum_get,
220                 .put = stac92xx_mux_enum_put,
221         },
222         HDA_CODEC_VOLUME("InMux Capture Volume", 0x15, 0x0, HDA_OUTPUT),
223         HDA_CODEC_VOLUME("InVol Capture Volume", 0x18, 0x0, HDA_INPUT),
224         HDA_CODEC_MUTE("ADCMux Capture Switch", 0x1b, 0x0, HDA_OUTPUT),
225         { } /* end */
226 };
227
228 static int stac92xx_build_controls(struct hda_codec *codec)
229 {
230         struct sigmatel_spec *spec = codec->spec;
231         int err;
232         int i;
233
234         err = snd_hda_add_new_ctls(codec, spec->mixer);
235         if (err < 0)
236                 return err;
237
238         for (i = 0; i < spec->num_mixers; i++) {
239                 err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
240                 if (err < 0)
241                         return err;
242         }
243
244         if (spec->multiout.dig_out_nid) {
245                 err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid);
246                 if (err < 0)
247                         return err;
248         }
249         if (spec->dig_in_nid) {
250                 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
251                 if (err < 0)
252                         return err;
253         }
254         return 0;       
255 }
256
257 static unsigned int ref9200_pin_configs[8] = {
258         0x01c47010, 0x01447010, 0x0221401f, 0x01114010,
259         0x02a19020, 0x01a19021, 0x90100140, 0x01813122,
260 };
261
262 static unsigned int *stac9200_brd_tbl[] = {
263         ref9200_pin_configs,
264 };
265
266 static struct hda_board_config stac9200_cfg_tbl[] = {
267         { .modelname = "ref",
268           .pci_subvendor = PCI_VENDOR_ID_INTEL,
269           .pci_subdevice = 0x2668,      /* DFI LanParty */
270           .config = STAC_REF },
271         {} /* terminator */
272 };
273
274 static unsigned int ref922x_pin_configs[10] = {
275         0x01014010, 0x01016011, 0x01012012, 0x0221401f,
276         0x01813122, 0x01011014, 0x01441030, 0x01c41030,
277         0x40000100, 0x40000100,
278 };
279
280 static unsigned int d945gtp3_pin_configs[10] = {
281         0x0221401f, 0x01a19022, 0x01813021, 0x01014010,
282         0x40000100, 0x40000100, 0x40000100, 0x40000100,
283         0x02a19120, 0x40000100,
284 };
285
286 static unsigned int d945gtp5_pin_configs[10] = {
287         0x0221401f, 0x01011012, 0x01813024, 0x01014010,
288         0x01a19021, 0x01016011, 0x01452130, 0x40000100,
289         0x02a19320, 0x40000100,
290 };
291
292 static unsigned int *stac922x_brd_tbl[] = {
293         ref922x_pin_configs,
294         d945gtp3_pin_configs,
295         d945gtp5_pin_configs,
296 };
297
298 static struct hda_board_config stac922x_cfg_tbl[] = {
299         { .modelname = "ref",
300           .pci_subvendor = PCI_VENDOR_ID_INTEL,
301           .pci_subdevice = 0x2668,      /* DFI LanParty */
302           .config = STAC_REF },         /* SigmaTel reference board */
303         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
304           .pci_subdevice = 0x0101,
305           .config = STAC_D945GTP3 },    /* Intel D945GTP - 3 Stack */
306         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
307           .pci_subdevice = 0x0202,
308           .config = STAC_D945GTP3 },    /* Intel D945GNT - 3 Stack, 9221 A1 */
309         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
310           .pci_subdevice = 0x0b0b,
311           .config = STAC_D945GTP3 },    /* Intel D945PSN - 3 Stack, 9221 A1 */
312         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
313           .pci_subdevice = 0x0404,
314           .config = STAC_D945GTP5 },    /* Intel D945GTP - 5 Stack */
315         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
316           .pci_subdevice = 0x0303,
317           .config = STAC_D945GTP5 },    /* Intel D945GNT - 5 Stack */
318         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
319           .pci_subdevice = 0x0013,
320           .config = STAC_D945GTP5 },    /* Intel D955XBK - 5 Stack */
321         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
322           .pci_subdevice = 0x0417,
323           .config = STAC_D945GTP5 },    /* Intel D975XBK - 5 Stack */
324         {} /* terminator */
325 };
326
327 static unsigned int ref927x_pin_configs[14] = {
328         0x01813122, 0x01a19021, 0x01014010, 0x01016011,
329         0x01012012, 0x01011014, 0x40000100, 0x40000100, 
330         0x40000100, 0x40000100, 0x40000100, 0x01441030,
331         0x01c41030, 0x40000100,
332 };
333
334 static unsigned int *stac927x_brd_tbl[] = {
335         ref927x_pin_configs,
336 };
337
338 static struct hda_board_config stac927x_cfg_tbl[] = {
339         { .modelname = "ref",
340           .pci_subvendor = PCI_VENDOR_ID_INTEL,
341           .pci_subdevice = 0x2668,      /* DFI LanParty */
342           .config = STAC_REF },         /* SigmaTel reference board */
343         {} /* terminator */
344 };
345
346 static void stac92xx_set_config_regs(struct hda_codec *codec)
347 {
348         int i;
349         struct sigmatel_spec *spec = codec->spec;
350         unsigned int pin_cfg;
351
352         for (i=0; i < spec->num_pins; i++) {
353                 snd_hda_codec_write(codec, spec->pin_nids[i], 0,
354                                     AC_VERB_SET_CONFIG_DEFAULT_BYTES_0,
355                                     spec->pin_configs[i] & 0x000000ff);
356                 snd_hda_codec_write(codec, spec->pin_nids[i], 0,
357                                     AC_VERB_SET_CONFIG_DEFAULT_BYTES_1,
358                                     (spec->pin_configs[i] & 0x0000ff00) >> 8);
359                 snd_hda_codec_write(codec, spec->pin_nids[i], 0,
360                                     AC_VERB_SET_CONFIG_DEFAULT_BYTES_2,
361                                     (spec->pin_configs[i] & 0x00ff0000) >> 16);
362                 snd_hda_codec_write(codec, spec->pin_nids[i], 0,
363                                     AC_VERB_SET_CONFIG_DEFAULT_BYTES_3,
364                                     spec->pin_configs[i] >> 24);
365                 pin_cfg = snd_hda_codec_read(codec, spec->pin_nids[i], 0,
366                                              AC_VERB_GET_CONFIG_DEFAULT,
367                                              0x00);     
368                 snd_printdd(KERN_INFO "hda_codec: pin nid %2.2x pin config %8.8x\n", spec->pin_nids[i], pin_cfg);
369         }
370 }
371
372 /*
373  * Analog playback callbacks
374  */
375 static int stac92xx_playback_pcm_open(struct hda_pcm_stream *hinfo,
376                                       struct hda_codec *codec,
377                                       struct snd_pcm_substream *substream)
378 {
379         struct sigmatel_spec *spec = codec->spec;
380         return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream);
381 }
382
383 static int stac92xx_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
384                                          struct hda_codec *codec,
385                                          unsigned int stream_tag,
386                                          unsigned int format,
387                                          struct snd_pcm_substream *substream)
388 {
389         struct sigmatel_spec *spec = codec->spec;
390         return snd_hda_multi_out_analog_prepare(codec, &spec->multiout, stream_tag, format, substream);
391 }
392
393 static int stac92xx_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
394                                         struct hda_codec *codec,
395                                         struct snd_pcm_substream *substream)
396 {
397         struct sigmatel_spec *spec = codec->spec;
398         return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
399 }
400
401 /*
402  * Digital playback callbacks
403  */
404 static int stac92xx_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
405                                           struct hda_codec *codec,
406                                           struct snd_pcm_substream *substream)
407 {
408         struct sigmatel_spec *spec = codec->spec;
409         return snd_hda_multi_out_dig_open(codec, &spec->multiout);
410 }
411
412 static int stac92xx_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
413                                            struct hda_codec *codec,
414                                            struct snd_pcm_substream *substream)
415 {
416         struct sigmatel_spec *spec = codec->spec;
417         return snd_hda_multi_out_dig_close(codec, &spec->multiout);
418 }
419
420
421 /*
422  * Analog capture callbacks
423  */
424 static int stac92xx_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
425                                         struct hda_codec *codec,
426                                         unsigned int stream_tag,
427                                         unsigned int format,
428                                         struct snd_pcm_substream *substream)
429 {
430         struct sigmatel_spec *spec = codec->spec;
431
432         snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
433                                    stream_tag, 0, format);
434         return 0;
435 }
436
437 static int stac92xx_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
438                                         struct hda_codec *codec,
439                                         struct snd_pcm_substream *substream)
440 {
441         struct sigmatel_spec *spec = codec->spec;
442
443         snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number], 0, 0, 0);
444         return 0;
445 }
446
447 static struct hda_pcm_stream stac92xx_pcm_digital_playback = {
448         .substreams = 1,
449         .channels_min = 2,
450         .channels_max = 2,
451         /* NID is set in stac92xx_build_pcms */
452         .ops = {
453                 .open = stac92xx_dig_playback_pcm_open,
454                 .close = stac92xx_dig_playback_pcm_close
455         },
456 };
457
458 static struct hda_pcm_stream stac92xx_pcm_digital_capture = {
459         .substreams = 1,
460         .channels_min = 2,
461         .channels_max = 2,
462         /* NID is set in stac92xx_build_pcms */
463 };
464
465 static struct hda_pcm_stream stac92xx_pcm_analog_playback = {
466         .substreams = 1,
467         .channels_min = 2,
468         .channels_max = 8,
469         .nid = 0x02, /* NID to query formats and rates */
470         .ops = {
471                 .open = stac92xx_playback_pcm_open,
472                 .prepare = stac92xx_playback_pcm_prepare,
473                 .cleanup = stac92xx_playback_pcm_cleanup
474         },
475 };
476
477 static struct hda_pcm_stream stac92xx_pcm_analog_alt_playback = {
478         .substreams = 1,
479         .channels_min = 2,
480         .channels_max = 2,
481         .nid = 0x06, /* NID to query formats and rates */
482         .ops = {
483                 .open = stac92xx_playback_pcm_open,
484                 .prepare = stac92xx_playback_pcm_prepare,
485                 .cleanup = stac92xx_playback_pcm_cleanup
486         },
487 };
488
489 static struct hda_pcm_stream stac92xx_pcm_analog_capture = {
490         .substreams = 2,
491         .channels_min = 2,
492         .channels_max = 2,
493         /* NID is set in stac92xx_build_pcms */
494         .ops = {
495                 .prepare = stac92xx_capture_pcm_prepare,
496                 .cleanup = stac92xx_capture_pcm_cleanup
497         },
498 };
499
500 static int stac92xx_build_pcms(struct hda_codec *codec)
501 {
502         struct sigmatel_spec *spec = codec->spec;
503         struct hda_pcm *info = spec->pcm_rec;
504
505         codec->num_pcms = 1;
506         codec->pcm_info = info;
507
508         info->name = "STAC92xx Analog";
509         info->stream[SNDRV_PCM_STREAM_PLAYBACK] = stac92xx_pcm_analog_playback;
510         info->stream[SNDRV_PCM_STREAM_CAPTURE] = stac92xx_pcm_analog_capture;
511         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
512
513         if (spec->alt_switch) {
514                 codec->num_pcms++;
515                 info++;
516                 info->name = "STAC92xx Analog Alt";
517                 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = stac92xx_pcm_analog_alt_playback;
518         }
519
520         if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
521                 codec->num_pcms++;
522                 info++;
523                 info->name = "STAC92xx Digital";
524                 if (spec->multiout.dig_out_nid) {
525                         info->stream[SNDRV_PCM_STREAM_PLAYBACK] = stac92xx_pcm_digital_playback;
526                         info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
527                 }
528                 if (spec->dig_in_nid) {
529                         info->stream[SNDRV_PCM_STREAM_CAPTURE] = stac92xx_pcm_digital_capture;
530                         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
531                 }
532         }
533
534         return 0;
535 }
536
537 static unsigned int stac92xx_get_vref(struct hda_codec *codec, hda_nid_t nid)
538 {
539         unsigned int pincap = snd_hda_param_read(codec, nid,
540                                                  AC_PAR_PIN_CAP);
541         pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
542         if (pincap & AC_PINCAP_VREF_100)
543                 return AC_PINCTL_VREF_100;
544         if (pincap & AC_PINCAP_VREF_80)
545                 return AC_PINCTL_VREF_80;
546         if (pincap & AC_PINCAP_VREF_50)
547                 return AC_PINCTL_VREF_50;
548         if (pincap & AC_PINCAP_VREF_GRD)
549                 return AC_PINCTL_VREF_GRD;
550         return 0;
551 }
552
553 static void stac92xx_auto_set_pinctl(struct hda_codec *codec, hda_nid_t nid, int pin_type)
554
555 {
556         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL, pin_type);
557 }
558
559 static int stac92xx_io_switch_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
560 {
561         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
562         uinfo->count = 1;
563         uinfo->value.integer.min = 0;
564         uinfo->value.integer.max = 1;
565         return 0;
566 }
567
568 static int stac92xx_io_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
569 {
570         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
571         struct sigmatel_spec *spec = codec->spec;
572         int io_idx = kcontrol-> private_value & 0xff;
573
574         ucontrol->value.integer.value[0] = spec->io_switch[io_idx];
575         return 0;
576 }
577
578 static int stac92xx_io_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
579 {
580         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
581         struct sigmatel_spec *spec = codec->spec;
582         hda_nid_t nid = kcontrol->private_value >> 8;
583         int io_idx = kcontrol-> private_value & 0xff;
584         unsigned short val = ucontrol->value.integer.value[0];
585
586         spec->io_switch[io_idx] = val;
587
588         if (val)
589                 stac92xx_auto_set_pinctl(codec, nid, AC_PINCTL_OUT_EN);
590         else {
591                 unsigned int pinctl = AC_PINCTL_IN_EN;
592                 if (io_idx) /* set VREF for mic */
593                         pinctl |= stac92xx_get_vref(codec, nid);
594                 stac92xx_auto_set_pinctl(codec, nid, pinctl);
595         }
596         return 1;
597 }
598
599 #define STAC_CODEC_IO_SWITCH(xname, xpval) \
600         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
601           .name = xname, \
602           .index = 0, \
603           .info = stac92xx_io_switch_info, \
604           .get = stac92xx_io_switch_get, \
605           .put = stac92xx_io_switch_put, \
606           .private_value = xpval, \
607         }
608
609
610 enum {
611         STAC_CTL_WIDGET_VOL,
612         STAC_CTL_WIDGET_MUTE,
613         STAC_CTL_WIDGET_IO_SWITCH,
614 };
615
616 static struct snd_kcontrol_new stac92xx_control_templates[] = {
617         HDA_CODEC_VOLUME(NULL, 0, 0, 0),
618         HDA_CODEC_MUTE(NULL, 0, 0, 0),
619         STAC_CODEC_IO_SWITCH(NULL, 0),
620 };
621
622 /* add dynamic controls */
623 static int stac92xx_add_control(struct sigmatel_spec *spec, int type, const char *name, unsigned long val)
624 {
625         struct snd_kcontrol_new *knew;
626
627         if (spec->num_kctl_used >= spec->num_kctl_alloc) {
628                 int num = spec->num_kctl_alloc + NUM_CONTROL_ALLOC;
629
630                 knew = kcalloc(num + 1, sizeof(*knew), GFP_KERNEL); /* array + terminator */
631                 if (! knew)
632                         return -ENOMEM;
633                 if (spec->kctl_alloc) {
634                         memcpy(knew, spec->kctl_alloc, sizeof(*knew) * spec->num_kctl_alloc);
635                         kfree(spec->kctl_alloc);
636                 }
637                 spec->kctl_alloc = knew;
638                 spec->num_kctl_alloc = num;
639         }
640
641         knew = &spec->kctl_alloc[spec->num_kctl_used];
642         *knew = stac92xx_control_templates[type];
643         knew->name = kstrdup(name, GFP_KERNEL);
644         if (! knew->name)
645                 return -ENOMEM;
646         knew->private_value = val;
647         spec->num_kctl_used++;
648         return 0;
649 }
650
651 /* flag inputs as additional dynamic lineouts */
652 static int stac92xx_add_dyn_out_pins(struct hda_codec *codec, struct auto_pin_cfg *cfg)
653 {
654         struct sigmatel_spec *spec = codec->spec;
655
656         switch (cfg->line_outs) {
657         case 3:
658                 /* add line-in as side */
659                 if (cfg->input_pins[AUTO_PIN_LINE]) {
660                         cfg->line_out_pins[3] = cfg->input_pins[AUTO_PIN_LINE];
661                         spec->line_switch = 1;
662                         cfg->line_outs++;
663                 }
664                 break;
665         case 2:
666                 /* add line-in as clfe and mic as side */
667                 if (cfg->input_pins[AUTO_PIN_LINE]) {
668                         cfg->line_out_pins[2] = cfg->input_pins[AUTO_PIN_LINE];
669                         spec->line_switch = 1;
670                         cfg->line_outs++;
671                 }
672                 if (cfg->input_pins[AUTO_PIN_MIC]) {
673                         cfg->line_out_pins[3] = cfg->input_pins[AUTO_PIN_MIC];
674                         spec->mic_switch = 1;
675                         cfg->line_outs++;
676                 }
677                 break;
678         case 1:
679                 /* add line-in as surr and mic as clfe */
680                 if (cfg->input_pins[AUTO_PIN_LINE]) {
681                         cfg->line_out_pins[1] = cfg->input_pins[AUTO_PIN_LINE];
682                         spec->line_switch = 1;
683                         cfg->line_outs++;
684                 }
685                 if (cfg->input_pins[AUTO_PIN_MIC]) {
686                         cfg->line_out_pins[2] = cfg->input_pins[AUTO_PIN_MIC];
687                         spec->mic_switch = 1;
688                         cfg->line_outs++;
689                 }
690                 break;
691         }
692
693         return 0;
694 }
695
696 /*
697  * XXX The line_out pin widget connection list may not be set to the
698  * desired DAC nid. This is the case on 927x where ports A and B can
699  * be routed to several DACs.
700  *
701  * This requires an analysis of the line-out/hp pin configuration
702  * to provide a best fit for pin/DAC configurations that are routable.
703  * For now, 927x DAC4 is not supported and 927x DAC1 output to ports
704  * A and B is not supported.
705  */
706 /* fill in the dac_nids table from the parsed pin configuration */
707 static int stac92xx_auto_fill_dac_nids(struct hda_codec *codec, const struct auto_pin_cfg *cfg)
708 {
709         struct sigmatel_spec *spec = codec->spec;
710         hda_nid_t nid;
711         int i;
712
713         /* check the pins hardwired to audio widget */
714         for (i = 0; i < cfg->line_outs; i++) {
715                 nid = cfg->line_out_pins[i];
716                 spec->multiout.dac_nids[i] = snd_hda_codec_read(codec, nid, 0,
717                                         AC_VERB_GET_CONNECT_LIST, 0) & 0xff;
718         }
719
720         spec->multiout.num_dacs = cfg->line_outs;
721
722         return 0;
723 }
724
725 /* add playback controls from the parsed DAC table */
726 static int stac92xx_auto_create_multi_out_ctls(struct sigmatel_spec *spec, const struct auto_pin_cfg *cfg)
727 {
728         char name[32];
729         static const char *chname[4] = { "Front", "Surround", NULL /*CLFE*/, "Side" };
730         hda_nid_t nid;
731         int i, err;
732
733         for (i = 0; i < cfg->line_outs; i++) {
734                 if (!spec->multiout.dac_nids[i])
735                         continue;
736
737                 nid = spec->multiout.dac_nids[i];
738
739                 if (i == 2) {
740                         /* Center/LFE */
741                         if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_VOL, "Center Playback Volume",
742                                                HDA_COMPOSE_AMP_VAL(nid, 1, 0, HDA_OUTPUT))) < 0)
743                                 return err;
744                         if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_VOL, "LFE Playback Volume",
745                                                HDA_COMPOSE_AMP_VAL(nid, 2, 0, HDA_OUTPUT))) < 0)
746                                 return err;
747                         if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_MUTE, "Center Playback Switch",
748                                                HDA_COMPOSE_AMP_VAL(nid, 1, 0, HDA_OUTPUT))) < 0)
749                                 return err;
750                         if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_MUTE, "LFE Playback Switch",
751                                                HDA_COMPOSE_AMP_VAL(nid, 2, 0, HDA_OUTPUT))) < 0)
752                                 return err;
753                 } else {
754                         sprintf(name, "%s Playback Volume", chname[i]);
755                         if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_VOL, name,
756                                                HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
757                                 return err;
758                         sprintf(name, "%s Playback Switch", chname[i]);
759                         if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_MUTE, name,
760                                                HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
761                                 return err;
762                 }
763         }
764
765         if (spec->line_switch)
766                 if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_IO_SWITCH, "Line In as Output Switch", cfg->input_pins[AUTO_PIN_LINE] << 8)) < 0)
767                         return err;
768
769         if (spec->mic_switch)
770                 if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_IO_SWITCH, "Mic as Output Switch", (cfg->input_pins[AUTO_PIN_MIC] << 8) | 1)) < 0)
771                         return err;
772
773         return 0;
774 }
775
776 /* add playback controls for HP output */
777 static int stac92xx_auto_create_hp_ctls(struct hda_codec *codec, struct auto_pin_cfg *cfg)
778 {
779         struct sigmatel_spec *spec = codec->spec;
780         hda_nid_t pin = cfg->hp_pin;
781         hda_nid_t nid;
782         int i, err;
783         unsigned int wid_caps;
784
785         if (! pin)
786                 return 0;
787
788         wid_caps = get_wcaps(codec, pin);
789         if (wid_caps & AC_WCAP_UNSOL_CAP) {
790                 /* Enable unsolicited responses on the HP widget */
791                 snd_hda_codec_write(codec, pin, 0,
792                                 AC_VERB_SET_UNSOLICITED_ENABLE,
793                                 STAC_UNSOL_ENABLE);
794                 spec->hp_detect = 1;
795         }
796
797         nid = snd_hda_codec_read(codec, pin, 0, AC_VERB_GET_CONNECT_LIST, 0) & 0xff;
798         for (i = 0; i < cfg->line_outs; i++) {
799                 if (! spec->multiout.dac_nids[i])
800                         continue;
801                 if (spec->multiout.dac_nids[i] == nid)
802                         return 0;
803         }
804
805         spec->multiout.hp_nid = nid;
806
807         /* control HP volume/switch on the output mixer amp */
808         if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_VOL, "Headphone Playback Volume",
809                                         HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
810                 return err;
811         if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_MUTE, "Headphone Playback Switch",
812                                         HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
813                 return err;
814
815         return 0;
816 }
817
818 /* create playback/capture controls for input pins */
819 static int stac92xx_auto_create_analog_input_ctls(struct hda_codec *codec, const struct auto_pin_cfg *cfg)
820 {
821         struct sigmatel_spec *spec = codec->spec;
822         struct hda_input_mux *imux = &spec->private_imux;
823         hda_nid_t con_lst[HDA_MAX_NUM_INPUTS];
824         int i, j, k;
825
826         for (i = 0; i < AUTO_PIN_LAST; i++) {
827                 int index = -1;
828                 if (cfg->input_pins[i]) {
829                         imux->items[imux->num_items].label = auto_pin_cfg_labels[i];
830
831                         for (j=0; j<spec->num_muxes; j++) {
832                                 int num_cons = snd_hda_get_connections(codec, spec->mux_nids[j], con_lst, HDA_MAX_NUM_INPUTS);
833                                 for (k=0; k<num_cons; k++)
834                                         if (con_lst[k] == cfg->input_pins[i]) {
835                                                 index = k;
836                                                 break;
837                                         }
838                                 if (index >= 0)
839                                         break;
840                         }
841                         imux->items[imux->num_items].index = index;
842                         imux->num_items++;
843                 }
844         }
845
846         return 0;
847 }
848
849 static void stac92xx_auto_init_multi_out(struct hda_codec *codec)
850 {
851         struct sigmatel_spec *spec = codec->spec;
852         int i;
853
854         for (i = 0; i < spec->autocfg.line_outs; i++) {
855                 hda_nid_t nid = spec->autocfg.line_out_pins[i];
856                 stac92xx_auto_set_pinctl(codec, nid, AC_PINCTL_OUT_EN);
857         }
858 }
859
860 static void stac92xx_auto_init_hp_out(struct hda_codec *codec)
861 {
862         struct sigmatel_spec *spec = codec->spec;
863         hda_nid_t pin;
864
865         pin = spec->autocfg.hp_pin;
866         if (pin) /* connect to front */
867                 stac92xx_auto_set_pinctl(codec, pin, AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
868 }
869
870 static int stac92xx_parse_auto_config(struct hda_codec *codec, hda_nid_t dig_out, hda_nid_t dig_in)
871 {
872         struct sigmatel_spec *spec = codec->spec;
873         int err;
874
875         if ((err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL)) < 0)
876                 return err;
877         if (! spec->autocfg.line_outs)
878                 return 0; /* can't find valid pin config */
879         if ((err = stac92xx_add_dyn_out_pins(codec, &spec->autocfg)) < 0)
880                 return err;
881         if ((err = stac92xx_auto_fill_dac_nids(codec, &spec->autocfg)) < 0)
882                 return err;
883
884         if ((err = stac92xx_auto_create_multi_out_ctls(spec, &spec->autocfg)) < 0 ||
885             (err = stac92xx_auto_create_hp_ctls(codec, &spec->autocfg)) < 0 ||
886             (err = stac92xx_auto_create_analog_input_ctls(codec, &spec->autocfg)) < 0)
887                 return err;
888
889         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
890         if (spec->multiout.max_channels > 2)
891                 spec->surr_switch = 1;
892
893         if (spec->autocfg.dig_out_pin)
894                 spec->multiout.dig_out_nid = dig_out;
895         if (spec->autocfg.dig_in_pin)
896                 spec->dig_in_nid = dig_in;
897
898         if (spec->kctl_alloc)
899                 spec->mixers[spec->num_mixers++] = spec->kctl_alloc;
900
901         spec->input_mux = &spec->private_imux;
902
903         return 1;
904 }
905
906 /* add playback controls for HP output */
907 static int stac9200_auto_create_hp_ctls(struct hda_codec *codec,
908                                         struct auto_pin_cfg *cfg)
909 {
910         struct sigmatel_spec *spec = codec->spec;
911         hda_nid_t pin = cfg->hp_pin;
912         unsigned int wid_caps;
913
914         if (! pin)
915                 return 0;
916
917         wid_caps = get_wcaps(codec, pin);
918         if (wid_caps & AC_WCAP_UNSOL_CAP) {
919                 /* Enable unsolicited responses on the HP widget */
920                 snd_hda_codec_write(codec, pin, 0,
921                                 AC_VERB_SET_UNSOLICITED_ENABLE,
922                                 STAC_UNSOL_ENABLE);
923                 spec->hp_detect = 1;
924         }
925
926         return 0;
927 }
928
929 static int stac9200_parse_auto_config(struct hda_codec *codec)
930 {
931         struct sigmatel_spec *spec = codec->spec;
932         int err;
933
934         if ((err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL)) < 0)
935                 return err;
936
937         if ((err = stac92xx_auto_create_analog_input_ctls(codec, &spec->autocfg)) < 0)
938                 return err;
939
940         if ((err = stac9200_auto_create_hp_ctls(codec, &spec->autocfg)) < 0)
941                 return err;
942
943         if (spec->autocfg.dig_out_pin)
944                 spec->multiout.dig_out_nid = 0x05;
945         if (spec->autocfg.dig_in_pin)
946                 spec->dig_in_nid = 0x04;
947
948         if (spec->kctl_alloc)
949                 spec->mixers[spec->num_mixers++] = spec->kctl_alloc;
950
951         spec->input_mux = &spec->private_imux;
952
953         return 1;
954 }
955
956 static int stac92xx_init(struct hda_codec *codec)
957 {
958         struct sigmatel_spec *spec = codec->spec;
959         struct auto_pin_cfg *cfg = &spec->autocfg;
960         int i;
961
962         snd_hda_sequence_write(codec, spec->init);
963
964         /* set up pins */
965         if (spec->hp_detect) {
966                 /* fake event to set up pins */
967                 codec->patch_ops.unsol_event(codec, STAC_HP_EVENT << 26);
968         } else {
969                 stac92xx_auto_init_multi_out(codec);
970                 stac92xx_auto_init_hp_out(codec);
971         }
972         for (i = 0; i < AUTO_PIN_LAST; i++) {
973                 hda_nid_t nid = cfg->input_pins[i];
974                 if (nid) {
975                         unsigned int pinctl = AC_PINCTL_IN_EN;
976                         if (i == AUTO_PIN_MIC || i == AUTO_PIN_FRONT_MIC)
977                                 pinctl |= stac92xx_get_vref(codec, nid);
978                         stac92xx_auto_set_pinctl(codec, nid, pinctl);
979                 }
980         }
981         if (cfg->dig_out_pin)
982                 stac92xx_auto_set_pinctl(codec, cfg->dig_out_pin,
983                                          AC_PINCTL_OUT_EN);
984         if (cfg->dig_in_pin)
985                 stac92xx_auto_set_pinctl(codec, cfg->dig_in_pin,
986                                          AC_PINCTL_IN_EN);
987
988         return 0;
989 }
990
991 static void stac92xx_free(struct hda_codec *codec)
992 {
993         struct sigmatel_spec *spec = codec->spec;
994         int i;
995
996         if (! spec)
997                 return;
998
999         if (spec->kctl_alloc) {
1000                 for (i = 0; i < spec->num_kctl_used; i++)
1001                         kfree(spec->kctl_alloc[i].name);
1002                 kfree(spec->kctl_alloc);
1003         }
1004
1005         kfree(spec);
1006 }
1007
1008 static void stac92xx_set_pinctl(struct hda_codec *codec, hda_nid_t nid,
1009                                 unsigned int flag)
1010 {
1011         unsigned int pin_ctl = snd_hda_codec_read(codec, nid,
1012                         0, AC_VERB_GET_PIN_WIDGET_CONTROL, 0x00);
1013         snd_hda_codec_write(codec, nid, 0,
1014                         AC_VERB_SET_PIN_WIDGET_CONTROL,
1015                         pin_ctl | flag);
1016 }
1017
1018 static void stac92xx_reset_pinctl(struct hda_codec *codec, hda_nid_t nid,
1019                                   unsigned int flag)
1020 {
1021         unsigned int pin_ctl = snd_hda_codec_read(codec, nid,
1022                         0, AC_VERB_GET_PIN_WIDGET_CONTROL, 0x00);
1023         snd_hda_codec_write(codec, nid, 0,
1024                         AC_VERB_SET_PIN_WIDGET_CONTROL,
1025                         pin_ctl & ~flag);
1026 }
1027
1028 static void stac92xx_unsol_event(struct hda_codec *codec, unsigned int res)
1029 {
1030         struct sigmatel_spec *spec = codec->spec;
1031         struct auto_pin_cfg *cfg = &spec->autocfg;
1032         int i, presence;
1033
1034         if ((res >> 26) != STAC_HP_EVENT)
1035                 return;
1036
1037         presence = snd_hda_codec_read(codec, cfg->hp_pin, 0,
1038                         AC_VERB_GET_PIN_SENSE, 0x00) >> 31;
1039
1040         if (presence) {
1041                 /* disable lineouts, enable hp */
1042                 for (i = 0; i < cfg->line_outs; i++)
1043                         stac92xx_reset_pinctl(codec, cfg->line_out_pins[i],
1044                                                 AC_PINCTL_OUT_EN);
1045                 stac92xx_set_pinctl(codec, cfg->hp_pin, AC_PINCTL_OUT_EN);
1046         } else {
1047                 /* enable lineouts, disable hp */
1048                 for (i = 0; i < cfg->line_outs; i++)
1049                         stac92xx_set_pinctl(codec, cfg->line_out_pins[i],
1050                                                 AC_PINCTL_OUT_EN);
1051                 stac92xx_reset_pinctl(codec, cfg->hp_pin, AC_PINCTL_OUT_EN);
1052         }
1053
1054
1055 #ifdef CONFIG_PM
1056 static int stac92xx_resume(struct hda_codec *codec)
1057 {
1058         struct sigmatel_spec *spec = codec->spec;
1059         int i;
1060
1061         stac92xx_init(codec);
1062         for (i = 0; i < spec->num_mixers; i++)
1063                 snd_hda_resume_ctls(codec, spec->mixers[i]);
1064         if (spec->multiout.dig_out_nid)
1065                 snd_hda_resume_spdif_out(codec);
1066         if (spec->dig_in_nid)
1067                 snd_hda_resume_spdif_in(codec);
1068
1069         return 0;
1070 }
1071 #endif
1072
1073 static struct hda_codec_ops stac92xx_patch_ops = {
1074         .build_controls = stac92xx_build_controls,
1075         .build_pcms = stac92xx_build_pcms,
1076         .init = stac92xx_init,
1077         .free = stac92xx_free,
1078         .unsol_event = stac92xx_unsol_event,
1079 #ifdef CONFIG_PM
1080         .resume = stac92xx_resume,
1081 #endif
1082 };
1083
1084 static int patch_stac9200(struct hda_codec *codec)
1085 {
1086         struct sigmatel_spec *spec;
1087         int err;
1088
1089         spec  = kzalloc(sizeof(*spec), GFP_KERNEL);
1090         if (spec == NULL)
1091                 return -ENOMEM;
1092
1093         codec->spec = spec;
1094         spec->board_config = snd_hda_check_board_config(codec, stac9200_cfg_tbl);
1095         if (spec->board_config < 0)
1096                 snd_printdd(KERN_INFO "hda_codec: Unknown model for STAC9200, using BIOS defaults\n");
1097         else {
1098                 spec->num_pins = 8;
1099                 spec->pin_nids = stac9200_pin_nids;
1100                 spec->pin_configs = stac9200_brd_tbl[spec->board_config];
1101                 stac92xx_set_config_regs(codec);
1102         }
1103
1104         spec->multiout.max_channels = 2;
1105         spec->multiout.num_dacs = 1;
1106         spec->multiout.dac_nids = stac9200_dac_nids;
1107         spec->adc_nids = stac9200_adc_nids;
1108         spec->mux_nids = stac9200_mux_nids;
1109         spec->num_muxes = 1;
1110
1111         spec->init = stac9200_core_init;
1112         spec->mixer = stac9200_mixer;
1113
1114         err = stac9200_parse_auto_config(codec);
1115         if (err < 0) {
1116                 stac92xx_free(codec);
1117                 return err;
1118         }
1119
1120         codec->patch_ops = stac92xx_patch_ops;
1121
1122         return 0;
1123 }
1124
1125 static int patch_stac922x(struct hda_codec *codec)
1126 {
1127         struct sigmatel_spec *spec;
1128         int err;
1129
1130         spec  = kzalloc(sizeof(*spec), GFP_KERNEL);
1131         if (spec == NULL)
1132                 return -ENOMEM;
1133
1134         codec->spec = spec;
1135         spec->board_config = snd_hda_check_board_config(codec, stac922x_cfg_tbl);
1136         if (spec->board_config < 0)
1137                 snd_printdd(KERN_INFO "hda_codec: Unknown model for STAC922x, using BIOS defaults\n");
1138         else {
1139                 spec->num_pins = 10;
1140                 spec->pin_nids = stac922x_pin_nids;
1141                 spec->pin_configs = stac922x_brd_tbl[spec->board_config];
1142                 stac92xx_set_config_regs(codec);
1143         }
1144
1145         spec->adc_nids = stac922x_adc_nids;
1146         spec->mux_nids = stac922x_mux_nids;
1147         spec->num_muxes = 2;
1148
1149         spec->init = stac922x_core_init;
1150         spec->mixer = stac922x_mixer;
1151
1152         spec->multiout.dac_nids = spec->dac_nids;
1153
1154         err = stac92xx_parse_auto_config(codec, 0x08, 0x09);
1155         if (err < 0) {
1156                 stac92xx_free(codec);
1157                 return err;
1158         }
1159
1160         codec->patch_ops = stac92xx_patch_ops;
1161
1162         return 0;
1163 }
1164
1165 static int patch_stac927x(struct hda_codec *codec)
1166 {
1167         struct sigmatel_spec *spec;
1168         int err;
1169
1170         spec  = kzalloc(sizeof(*spec), GFP_KERNEL);
1171         if (spec == NULL)
1172                 return -ENOMEM;
1173
1174         codec->spec = spec;
1175         spec->board_config = snd_hda_check_board_config(codec, stac927x_cfg_tbl);
1176         if (spec->board_config < 0)
1177                 snd_printdd(KERN_INFO "hda_codec: Unknown model for STAC927x, using BIOS defaults\n");
1178         else {
1179                 spec->num_pins = 14;
1180                 spec->pin_nids = stac927x_pin_nids;
1181                 spec->pin_configs = stac927x_brd_tbl[spec->board_config];
1182                 stac92xx_set_config_regs(codec);
1183         }
1184
1185         spec->adc_nids = stac927x_adc_nids;
1186         spec->mux_nids = stac927x_mux_nids;
1187         spec->num_muxes = 3;
1188
1189         spec->init = stac927x_core_init;
1190         spec->mixer = stac927x_mixer;
1191
1192         spec->multiout.dac_nids = spec->dac_nids;
1193
1194         err = stac92xx_parse_auto_config(codec, 0x1e, 0x20);
1195         if (err < 0) {
1196                 stac92xx_free(codec);
1197                 return err;
1198         }
1199
1200         codec->patch_ops = stac92xx_patch_ops;
1201
1202         return 0;
1203 }
1204
1205 /*
1206  * STAC 7661(?) hack
1207  */
1208
1209 /* static config for Sony VAIO FE550G */
1210 static hda_nid_t vaio_dacs[] = { 0x2 };
1211 #define VAIO_HP_DAC     0x5
1212 static hda_nid_t vaio_adcs[] = { 0x8 /*,0x6*/ };
1213 static hda_nid_t vaio_mux_nids[] = { 0x15 };
1214
1215 static struct hda_input_mux vaio_mux = {
1216         .num_items = 2,
1217         .items = {
1218                 /* { "HP", 0x0 },
1219                    { "Unknown", 0x1 }, */
1220                 { "Mic", 0x2 },
1221                 { "PCM", 0x3 },
1222         }
1223 };
1224
1225 static struct hda_verb vaio_init[] = {
1226         {0x0a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP }, /* HP <- 0x2 */
1227         {0x0f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT }, /* Speaker <- 0x5 */
1228         {0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 }, /* Mic? (<- 0x2) */
1229         {0x0e, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN }, /* CD */
1230         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 }, /* Mic? */
1231         {0x15, AC_VERB_SET_CONNECT_SEL, 0x2}, /* mic-sel: 0a,0d,14,02 */
1232         {0x02, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE}, /* HP */
1233         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE}, /* Speaker */
1234         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)}, /* capture sw/vol -> 0x8 */
1235         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)}, /* CD-in -> 0x6 */
1236         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE}, /* Mic-in -> 0x9 */
1237         {}
1238 };
1239
1240 /* bind volumes of both NID 0x02 and 0x05 */
1241 static int vaio_master_vol_put(struct snd_kcontrol *kcontrol,
1242                                struct snd_ctl_elem_value *ucontrol)
1243 {
1244         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1245         long *valp = ucontrol->value.integer.value;
1246         int change;
1247
1248         change = snd_hda_codec_amp_update(codec, 0x02, 0, HDA_OUTPUT, 0,
1249                                           0x7f, valp[0] & 0x7f);
1250         change |= snd_hda_codec_amp_update(codec, 0x02, 1, HDA_OUTPUT, 0,
1251                                            0x7f, valp[1] & 0x7f);
1252         snd_hda_codec_amp_update(codec, 0x05, 0, HDA_OUTPUT, 0,
1253                                  0x7f, valp[0] & 0x7f);
1254         snd_hda_codec_amp_update(codec, 0x05, 1, HDA_OUTPUT, 0,
1255                                  0x7f, valp[1] & 0x7f);
1256         return change;
1257 }
1258
1259 /* bind volumes of both NID 0x02 and 0x05 */
1260 static int vaio_master_sw_put(struct snd_kcontrol *kcontrol,
1261                               struct snd_ctl_elem_value *ucontrol)
1262 {
1263         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1264         long *valp = ucontrol->value.integer.value;
1265         int change;
1266
1267         change = snd_hda_codec_amp_update(codec, 0x02, 0, HDA_OUTPUT, 0,
1268                                           0x80, valp[0] & 0x80);
1269         change |= snd_hda_codec_amp_update(codec, 0x02, 1, HDA_OUTPUT, 0,
1270                                            0x80, valp[1] & 0x80);
1271         snd_hda_codec_amp_update(codec, 0x05, 0, HDA_OUTPUT, 0,
1272                                  0x80, valp[0] & 0x80);
1273         snd_hda_codec_amp_update(codec, 0x05, 1, HDA_OUTPUT, 0,
1274                                  0x80, valp[1] & 0x80);
1275         return change;
1276 }
1277
1278 static struct snd_kcontrol_new vaio_mixer[] = {
1279         {
1280                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1281                 .name = "Master Playback Volume",
1282                 .info = snd_hda_mixer_amp_volume_info,
1283                 .get = snd_hda_mixer_amp_volume_get,
1284                 .put = vaio_master_vol_put,
1285                 .private_value = HDA_COMPOSE_AMP_VAL(0x02, 3, 0, HDA_OUTPUT),
1286         },
1287         {
1288                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1289                 .name = "Master Playback Switch",
1290                 .info = snd_hda_mixer_amp_switch_info,
1291                 .get = snd_hda_mixer_amp_switch_get,
1292                 .put = vaio_master_sw_put,
1293                 .private_value = HDA_COMPOSE_AMP_VAL(0x02, 3, 0, HDA_OUTPUT),
1294         },
1295         /* HDA_CODEC_VOLUME("CD Capture Volume", 0x07, 0, HDA_INPUT), */
1296         HDA_CODEC_VOLUME("Capture Volume", 0x09, 0, HDA_INPUT),
1297         HDA_CODEC_MUTE("Capture Switch", 0x09, 0, HDA_INPUT),
1298         {
1299                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1300                 .name = "Capture Source",
1301                 .count = 1,
1302                 .info = stac92xx_mux_enum_info,
1303                 .get = stac92xx_mux_enum_get,
1304                 .put = stac92xx_mux_enum_put,
1305         },
1306         {}
1307 };
1308
1309 static struct hda_codec_ops stac7661_patch_ops = {
1310         .build_controls = stac92xx_build_controls,
1311         .build_pcms = stac92xx_build_pcms,
1312         .init = stac92xx_init,
1313         .free = stac92xx_free,
1314 #ifdef CONFIG_PM
1315         .resume = stac92xx_resume,
1316 #endif
1317 };
1318
1319 enum { STAC7661_VAIO };
1320
1321 static struct hda_board_config stac7661_cfg_tbl[] = {
1322         { .modelname = "vaio", .config = STAC7661_VAIO },
1323         { .pci_subvendor = 0x104d, .pci_subdevice = 0x81e6,
1324           .config = STAC7661_VAIO },
1325         { .pci_subvendor = 0x104d, .pci_subdevice = 0x81ef,
1326           .config = STAC7661_VAIO },
1327         {}
1328 };
1329
1330 static int patch_stac7661(struct hda_codec *codec)
1331 {
1332         struct sigmatel_spec *spec;
1333         int board_config;
1334
1335         board_config = snd_hda_check_board_config(codec, stac7661_cfg_tbl);
1336         if (board_config < 0)
1337                 /* unknown config, let generic-parser do its job... */
1338                 return snd_hda_parse_generic_codec(codec);
1339         
1340         spec  = kzalloc(sizeof(*spec), GFP_KERNEL);
1341         if (spec == NULL)
1342                 return -ENOMEM;
1343
1344         codec->spec = spec;
1345         switch (board_config) {
1346         case STAC7661_VAIO:
1347                 spec->mixer = vaio_mixer;
1348                 spec->init = vaio_init;
1349                 spec->multiout.max_channels = 2;
1350                 spec->multiout.num_dacs = ARRAY_SIZE(vaio_dacs);
1351                 spec->multiout.dac_nids = vaio_dacs;
1352                 spec->multiout.hp_nid = VAIO_HP_DAC;
1353                 spec->num_adcs = ARRAY_SIZE(vaio_adcs);
1354                 spec->adc_nids = vaio_adcs;
1355                 spec->input_mux = &vaio_mux;
1356                 spec->mux_nids = vaio_mux_nids;
1357                 break;
1358         }
1359
1360         codec->patch_ops = stac7661_patch_ops;
1361         return 0;
1362 }
1363
1364
1365 /*
1366  * patch entries
1367  */
1368 struct hda_codec_preset snd_hda_preset_sigmatel[] = {
1369         { .id = 0x83847690, .name = "STAC9200", .patch = patch_stac9200 },
1370         { .id = 0x83847882, .name = "STAC9220 A1", .patch = patch_stac922x },
1371         { .id = 0x83847680, .name = "STAC9221 A1", .patch = patch_stac922x },
1372         { .id = 0x83847880, .name = "STAC9220 A2", .patch = patch_stac922x },
1373         { .id = 0x83847681, .name = "STAC9220D/9223D A2", .patch = patch_stac922x },
1374         { .id = 0x83847682, .name = "STAC9221 A2", .patch = patch_stac922x },
1375         { .id = 0x83847683, .name = "STAC9221D A2", .patch = patch_stac922x },
1376         { .id = 0x83847620, .name = "STAC9274", .patch = patch_stac927x },
1377         { .id = 0x83847621, .name = "STAC9274D", .patch = patch_stac927x },
1378         { .id = 0x83847622, .name = "STAC9273X", .patch = patch_stac927x },
1379         { .id = 0x83847623, .name = "STAC9273D", .patch = patch_stac927x },
1380         { .id = 0x83847624, .name = "STAC9272X", .patch = patch_stac927x },
1381         { .id = 0x83847625, .name = "STAC9272D", .patch = patch_stac927x },
1382         { .id = 0x83847626, .name = "STAC9271X", .patch = patch_stac927x },
1383         { .id = 0x83847627, .name = "STAC9271D", .patch = patch_stac927x },
1384         { .id = 0x83847628, .name = "STAC9274X5NH", .patch = patch_stac927x },
1385         { .id = 0x83847629, .name = "STAC9274D5NH", .patch = patch_stac927x },
1386         { .id = 0x83847661, .name = "STAC7661", .patch = patch_stac7661 },
1387         {} /* terminator */
1388 };