Merge git://git.infradead.org/~dwmw2/rbtree-2.6
[linux-2.6] / 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 = 0x0707,
314           .config = STAC_D945GTP5 },    /* Intel D945PSV - 5 Stack */
315        { .pci_subvendor = PCI_VENDOR_ID_INTEL,
316           .pci_subdevice = 0x0404,
317           .config = STAC_D945GTP5 },    /* Intel D945GTP - 5 Stack */
318         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
319           .pci_subdevice = 0x0303,
320           .config = STAC_D945GTP5 },    /* Intel D945GNT - 5 Stack */
321         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
322           .pci_subdevice = 0x0013,
323           .config = STAC_D945GTP5 },    /* Intel D955XBK - 5 Stack */
324         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
325           .pci_subdevice = 0x0417,
326           .config = STAC_D945GTP5 },    /* Intel D975XBK - 5 Stack */
327         {} /* terminator */
328 };
329
330 static unsigned int ref927x_pin_configs[14] = {
331         0x01813122, 0x01a19021, 0x01014010, 0x01016011,
332         0x01012012, 0x01011014, 0x40000100, 0x40000100, 
333         0x40000100, 0x40000100, 0x40000100, 0x01441030,
334         0x01c41030, 0x40000100,
335 };
336
337 static unsigned int *stac927x_brd_tbl[] = {
338         ref927x_pin_configs,
339 };
340
341 static struct hda_board_config stac927x_cfg_tbl[] = {
342         { .modelname = "ref",
343           .pci_subvendor = PCI_VENDOR_ID_INTEL,
344           .pci_subdevice = 0x2668,      /* DFI LanParty */
345           .config = STAC_REF },         /* SigmaTel reference board */
346         {} /* terminator */
347 };
348
349 static void stac92xx_set_config_regs(struct hda_codec *codec)
350 {
351         int i;
352         struct sigmatel_spec *spec = codec->spec;
353         unsigned int pin_cfg;
354
355         for (i=0; i < spec->num_pins; i++) {
356                 snd_hda_codec_write(codec, spec->pin_nids[i], 0,
357                                     AC_VERB_SET_CONFIG_DEFAULT_BYTES_0,
358                                     spec->pin_configs[i] & 0x000000ff);
359                 snd_hda_codec_write(codec, spec->pin_nids[i], 0,
360                                     AC_VERB_SET_CONFIG_DEFAULT_BYTES_1,
361                                     (spec->pin_configs[i] & 0x0000ff00) >> 8);
362                 snd_hda_codec_write(codec, spec->pin_nids[i], 0,
363                                     AC_VERB_SET_CONFIG_DEFAULT_BYTES_2,
364                                     (spec->pin_configs[i] & 0x00ff0000) >> 16);
365                 snd_hda_codec_write(codec, spec->pin_nids[i], 0,
366                                     AC_VERB_SET_CONFIG_DEFAULT_BYTES_3,
367                                     spec->pin_configs[i] >> 24);
368                 pin_cfg = snd_hda_codec_read(codec, spec->pin_nids[i], 0,
369                                              AC_VERB_GET_CONFIG_DEFAULT,
370                                              0x00);     
371                 snd_printdd(KERN_INFO "hda_codec: pin nid %2.2x pin config %8.8x\n", spec->pin_nids[i], pin_cfg);
372         }
373 }
374
375 /*
376  * Analog playback callbacks
377  */
378 static int stac92xx_playback_pcm_open(struct hda_pcm_stream *hinfo,
379                                       struct hda_codec *codec,
380                                       struct snd_pcm_substream *substream)
381 {
382         struct sigmatel_spec *spec = codec->spec;
383         return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream);
384 }
385
386 static int stac92xx_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
387                                          struct hda_codec *codec,
388                                          unsigned int stream_tag,
389                                          unsigned int format,
390                                          struct snd_pcm_substream *substream)
391 {
392         struct sigmatel_spec *spec = codec->spec;
393         return snd_hda_multi_out_analog_prepare(codec, &spec->multiout, stream_tag, format, substream);
394 }
395
396 static int stac92xx_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
397                                         struct hda_codec *codec,
398                                         struct snd_pcm_substream *substream)
399 {
400         struct sigmatel_spec *spec = codec->spec;
401         return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
402 }
403
404 /*
405  * Digital playback callbacks
406  */
407 static int stac92xx_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
408                                           struct hda_codec *codec,
409                                           struct snd_pcm_substream *substream)
410 {
411         struct sigmatel_spec *spec = codec->spec;
412         return snd_hda_multi_out_dig_open(codec, &spec->multiout);
413 }
414
415 static int stac92xx_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
416                                            struct hda_codec *codec,
417                                            struct snd_pcm_substream *substream)
418 {
419         struct sigmatel_spec *spec = codec->spec;
420         return snd_hda_multi_out_dig_close(codec, &spec->multiout);
421 }
422
423
424 /*
425  * Analog capture callbacks
426  */
427 static int stac92xx_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
428                                         struct hda_codec *codec,
429                                         unsigned int stream_tag,
430                                         unsigned int format,
431                                         struct snd_pcm_substream *substream)
432 {
433         struct sigmatel_spec *spec = codec->spec;
434
435         snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
436                                    stream_tag, 0, format);
437         return 0;
438 }
439
440 static int stac92xx_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
441                                         struct hda_codec *codec,
442                                         struct snd_pcm_substream *substream)
443 {
444         struct sigmatel_spec *spec = codec->spec;
445
446         snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number], 0, 0, 0);
447         return 0;
448 }
449
450 static struct hda_pcm_stream stac92xx_pcm_digital_playback = {
451         .substreams = 1,
452         .channels_min = 2,
453         .channels_max = 2,
454         /* NID is set in stac92xx_build_pcms */
455         .ops = {
456                 .open = stac92xx_dig_playback_pcm_open,
457                 .close = stac92xx_dig_playback_pcm_close
458         },
459 };
460
461 static struct hda_pcm_stream stac92xx_pcm_digital_capture = {
462         .substreams = 1,
463         .channels_min = 2,
464         .channels_max = 2,
465         /* NID is set in stac92xx_build_pcms */
466 };
467
468 static struct hda_pcm_stream stac92xx_pcm_analog_playback = {
469         .substreams = 1,
470         .channels_min = 2,
471         .channels_max = 8,
472         .nid = 0x02, /* NID to query formats and rates */
473         .ops = {
474                 .open = stac92xx_playback_pcm_open,
475                 .prepare = stac92xx_playback_pcm_prepare,
476                 .cleanup = stac92xx_playback_pcm_cleanup
477         },
478 };
479
480 static struct hda_pcm_stream stac92xx_pcm_analog_alt_playback = {
481         .substreams = 1,
482         .channels_min = 2,
483         .channels_max = 2,
484         .nid = 0x06, /* NID to query formats and rates */
485         .ops = {
486                 .open = stac92xx_playback_pcm_open,
487                 .prepare = stac92xx_playback_pcm_prepare,
488                 .cleanup = stac92xx_playback_pcm_cleanup
489         },
490 };
491
492 static struct hda_pcm_stream stac92xx_pcm_analog_capture = {
493         .substreams = 2,
494         .channels_min = 2,
495         .channels_max = 2,
496         /* NID is set in stac92xx_build_pcms */
497         .ops = {
498                 .prepare = stac92xx_capture_pcm_prepare,
499                 .cleanup = stac92xx_capture_pcm_cleanup
500         },
501 };
502
503 static int stac92xx_build_pcms(struct hda_codec *codec)
504 {
505         struct sigmatel_spec *spec = codec->spec;
506         struct hda_pcm *info = spec->pcm_rec;
507
508         codec->num_pcms = 1;
509         codec->pcm_info = info;
510
511         info->name = "STAC92xx Analog";
512         info->stream[SNDRV_PCM_STREAM_PLAYBACK] = stac92xx_pcm_analog_playback;
513         info->stream[SNDRV_PCM_STREAM_CAPTURE] = stac92xx_pcm_analog_capture;
514         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
515
516         if (spec->alt_switch) {
517                 codec->num_pcms++;
518                 info++;
519                 info->name = "STAC92xx Analog Alt";
520                 info->stream[SNDRV_PCM_STREAM_PLAYBACK] = stac92xx_pcm_analog_alt_playback;
521         }
522
523         if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
524                 codec->num_pcms++;
525                 info++;
526                 info->name = "STAC92xx Digital";
527                 if (spec->multiout.dig_out_nid) {
528                         info->stream[SNDRV_PCM_STREAM_PLAYBACK] = stac92xx_pcm_digital_playback;
529                         info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
530                 }
531                 if (spec->dig_in_nid) {
532                         info->stream[SNDRV_PCM_STREAM_CAPTURE] = stac92xx_pcm_digital_capture;
533                         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
534                 }
535         }
536
537         return 0;
538 }
539
540 static unsigned int stac92xx_get_vref(struct hda_codec *codec, hda_nid_t nid)
541 {
542         unsigned int pincap = snd_hda_param_read(codec, nid,
543                                                  AC_PAR_PIN_CAP);
544         pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
545         if (pincap & AC_PINCAP_VREF_100)
546                 return AC_PINCTL_VREF_100;
547         if (pincap & AC_PINCAP_VREF_80)
548                 return AC_PINCTL_VREF_80;
549         if (pincap & AC_PINCAP_VREF_50)
550                 return AC_PINCTL_VREF_50;
551         if (pincap & AC_PINCAP_VREF_GRD)
552                 return AC_PINCTL_VREF_GRD;
553         return 0;
554 }
555
556 static void stac92xx_auto_set_pinctl(struct hda_codec *codec, hda_nid_t nid, int pin_type)
557
558 {
559         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL, pin_type);
560 }
561
562 static int stac92xx_io_switch_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
563 {
564         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
565         uinfo->count = 1;
566         uinfo->value.integer.min = 0;
567         uinfo->value.integer.max = 1;
568         return 0;
569 }
570
571 static int stac92xx_io_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
572 {
573         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
574         struct sigmatel_spec *spec = codec->spec;
575         int io_idx = kcontrol-> private_value & 0xff;
576
577         ucontrol->value.integer.value[0] = spec->io_switch[io_idx];
578         return 0;
579 }
580
581 static int stac92xx_io_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
582 {
583         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
584         struct sigmatel_spec *spec = codec->spec;
585         hda_nid_t nid = kcontrol->private_value >> 8;
586         int io_idx = kcontrol-> private_value & 0xff;
587         unsigned short val = ucontrol->value.integer.value[0];
588
589         spec->io_switch[io_idx] = val;
590
591         if (val)
592                 stac92xx_auto_set_pinctl(codec, nid, AC_PINCTL_OUT_EN);
593         else {
594                 unsigned int pinctl = AC_PINCTL_IN_EN;
595                 if (io_idx) /* set VREF for mic */
596                         pinctl |= stac92xx_get_vref(codec, nid);
597                 stac92xx_auto_set_pinctl(codec, nid, pinctl);
598         }
599         return 1;
600 }
601
602 #define STAC_CODEC_IO_SWITCH(xname, xpval) \
603         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
604           .name = xname, \
605           .index = 0, \
606           .info = stac92xx_io_switch_info, \
607           .get = stac92xx_io_switch_get, \
608           .put = stac92xx_io_switch_put, \
609           .private_value = xpval, \
610         }
611
612
613 enum {
614         STAC_CTL_WIDGET_VOL,
615         STAC_CTL_WIDGET_MUTE,
616         STAC_CTL_WIDGET_IO_SWITCH,
617 };
618
619 static struct snd_kcontrol_new stac92xx_control_templates[] = {
620         HDA_CODEC_VOLUME(NULL, 0, 0, 0),
621         HDA_CODEC_MUTE(NULL, 0, 0, 0),
622         STAC_CODEC_IO_SWITCH(NULL, 0),
623 };
624
625 /* add dynamic controls */
626 static int stac92xx_add_control(struct sigmatel_spec *spec, int type, const char *name, unsigned long val)
627 {
628         struct snd_kcontrol_new *knew;
629
630         if (spec->num_kctl_used >= spec->num_kctl_alloc) {
631                 int num = spec->num_kctl_alloc + NUM_CONTROL_ALLOC;
632
633                 knew = kcalloc(num + 1, sizeof(*knew), GFP_KERNEL); /* array + terminator */
634                 if (! knew)
635                         return -ENOMEM;
636                 if (spec->kctl_alloc) {
637                         memcpy(knew, spec->kctl_alloc, sizeof(*knew) * spec->num_kctl_alloc);
638                         kfree(spec->kctl_alloc);
639                 }
640                 spec->kctl_alloc = knew;
641                 spec->num_kctl_alloc = num;
642         }
643
644         knew = &spec->kctl_alloc[spec->num_kctl_used];
645         *knew = stac92xx_control_templates[type];
646         knew->name = kstrdup(name, GFP_KERNEL);
647         if (! knew->name)
648                 return -ENOMEM;
649         knew->private_value = val;
650         spec->num_kctl_used++;
651         return 0;
652 }
653
654 /* flag inputs as additional dynamic lineouts */
655 static int stac92xx_add_dyn_out_pins(struct hda_codec *codec, struct auto_pin_cfg *cfg)
656 {
657         struct sigmatel_spec *spec = codec->spec;
658
659         switch (cfg->line_outs) {
660         case 3:
661                 /* add line-in as side */
662                 if (cfg->input_pins[AUTO_PIN_LINE]) {
663                         cfg->line_out_pins[3] = cfg->input_pins[AUTO_PIN_LINE];
664                         spec->line_switch = 1;
665                         cfg->line_outs++;
666                 }
667                 break;
668         case 2:
669                 /* add line-in as clfe and mic as side */
670                 if (cfg->input_pins[AUTO_PIN_LINE]) {
671                         cfg->line_out_pins[2] = cfg->input_pins[AUTO_PIN_LINE];
672                         spec->line_switch = 1;
673                         cfg->line_outs++;
674                 }
675                 if (cfg->input_pins[AUTO_PIN_MIC]) {
676                         cfg->line_out_pins[3] = cfg->input_pins[AUTO_PIN_MIC];
677                         spec->mic_switch = 1;
678                         cfg->line_outs++;
679                 }
680                 break;
681         case 1:
682                 /* add line-in as surr and mic as clfe */
683                 if (cfg->input_pins[AUTO_PIN_LINE]) {
684                         cfg->line_out_pins[1] = cfg->input_pins[AUTO_PIN_LINE];
685                         spec->line_switch = 1;
686                         cfg->line_outs++;
687                 }
688                 if (cfg->input_pins[AUTO_PIN_MIC]) {
689                         cfg->line_out_pins[2] = cfg->input_pins[AUTO_PIN_MIC];
690                         spec->mic_switch = 1;
691                         cfg->line_outs++;
692                 }
693                 break;
694         }
695
696         return 0;
697 }
698
699 /*
700  * XXX The line_out pin widget connection list may not be set to the
701  * desired DAC nid. This is the case on 927x where ports A and B can
702  * be routed to several DACs.
703  *
704  * This requires an analysis of the line-out/hp pin configuration
705  * to provide a best fit for pin/DAC configurations that are routable.
706  * For now, 927x DAC4 is not supported and 927x DAC1 output to ports
707  * A and B is not supported.
708  */
709 /* fill in the dac_nids table from the parsed pin configuration */
710 static int stac92xx_auto_fill_dac_nids(struct hda_codec *codec, const struct auto_pin_cfg *cfg)
711 {
712         struct sigmatel_spec *spec = codec->spec;
713         hda_nid_t nid;
714         int i;
715
716         /* check the pins hardwired to audio widget */
717         for (i = 0; i < cfg->line_outs; i++) {
718                 nid = cfg->line_out_pins[i];
719                 spec->multiout.dac_nids[i] = snd_hda_codec_read(codec, nid, 0,
720                                         AC_VERB_GET_CONNECT_LIST, 0) & 0xff;
721         }
722
723         spec->multiout.num_dacs = cfg->line_outs;
724
725         return 0;
726 }
727
728 /* add playback controls from the parsed DAC table */
729 static int stac92xx_auto_create_multi_out_ctls(struct sigmatel_spec *spec, const struct auto_pin_cfg *cfg)
730 {
731         char name[32];
732         static const char *chname[4] = { "Front", "Surround", NULL /*CLFE*/, "Side" };
733         hda_nid_t nid;
734         int i, err;
735
736         for (i = 0; i < cfg->line_outs; i++) {
737                 if (!spec->multiout.dac_nids[i])
738                         continue;
739
740                 nid = spec->multiout.dac_nids[i];
741
742                 if (i == 2) {
743                         /* Center/LFE */
744                         if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_VOL, "Center Playback Volume",
745                                                HDA_COMPOSE_AMP_VAL(nid, 1, 0, HDA_OUTPUT))) < 0)
746                                 return err;
747                         if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_VOL, "LFE Playback Volume",
748                                                HDA_COMPOSE_AMP_VAL(nid, 2, 0, HDA_OUTPUT))) < 0)
749                                 return err;
750                         if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_MUTE, "Center Playback Switch",
751                                                HDA_COMPOSE_AMP_VAL(nid, 1, 0, HDA_OUTPUT))) < 0)
752                                 return err;
753                         if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_MUTE, "LFE Playback Switch",
754                                                HDA_COMPOSE_AMP_VAL(nid, 2, 0, HDA_OUTPUT))) < 0)
755                                 return err;
756                 } else {
757                         sprintf(name, "%s Playback Volume", chname[i]);
758                         if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_VOL, name,
759                                                HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
760                                 return err;
761                         sprintf(name, "%s Playback Switch", chname[i]);
762                         if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_MUTE, name,
763                                                HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
764                                 return err;
765                 }
766         }
767
768         if (spec->line_switch)
769                 if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_IO_SWITCH, "Line In as Output Switch", cfg->input_pins[AUTO_PIN_LINE] << 8)) < 0)
770                         return err;
771
772         if (spec->mic_switch)
773                 if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_IO_SWITCH, "Mic as Output Switch", (cfg->input_pins[AUTO_PIN_MIC] << 8) | 1)) < 0)
774                         return err;
775
776         return 0;
777 }
778
779 /* add playback controls for HP output */
780 static int stac92xx_auto_create_hp_ctls(struct hda_codec *codec, struct auto_pin_cfg *cfg)
781 {
782         struct sigmatel_spec *spec = codec->spec;
783         hda_nid_t pin = cfg->hp_pin;
784         hda_nid_t nid;
785         int i, err;
786         unsigned int wid_caps;
787
788         if (! pin)
789                 return 0;
790
791         wid_caps = get_wcaps(codec, pin);
792         if (wid_caps & AC_WCAP_UNSOL_CAP)
793                 spec->hp_detect = 1;
794
795         nid = snd_hda_codec_read(codec, pin, 0, AC_VERB_GET_CONNECT_LIST, 0) & 0xff;
796         for (i = 0; i < cfg->line_outs; i++) {
797                 if (! spec->multiout.dac_nids[i])
798                         continue;
799                 if (spec->multiout.dac_nids[i] == nid)
800                         return 0;
801         }
802
803         spec->multiout.hp_nid = nid;
804
805         /* control HP volume/switch on the output mixer amp */
806         if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_VOL, "Headphone Playback Volume",
807                                         HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
808                 return err;
809         if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_MUTE, "Headphone Playback Switch",
810                                         HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
811                 return err;
812
813         return 0;
814 }
815
816 /* create playback/capture controls for input pins */
817 static int stac92xx_auto_create_analog_input_ctls(struct hda_codec *codec, const struct auto_pin_cfg *cfg)
818 {
819         struct sigmatel_spec *spec = codec->spec;
820         struct hda_input_mux *imux = &spec->private_imux;
821         hda_nid_t con_lst[HDA_MAX_NUM_INPUTS];
822         int i, j, k;
823
824         for (i = 0; i < AUTO_PIN_LAST; i++) {
825                 int index = -1;
826                 if (cfg->input_pins[i]) {
827                         imux->items[imux->num_items].label = auto_pin_cfg_labels[i];
828
829                         for (j=0; j<spec->num_muxes; j++) {
830                                 int num_cons = snd_hda_get_connections(codec, spec->mux_nids[j], con_lst, HDA_MAX_NUM_INPUTS);
831                                 for (k=0; k<num_cons; k++)
832                                         if (con_lst[k] == cfg->input_pins[i]) {
833                                                 index = k;
834                                                 break;
835                                         }
836                                 if (index >= 0)
837                                         break;
838                         }
839                         imux->items[imux->num_items].index = index;
840                         imux->num_items++;
841                 }
842         }
843
844         return 0;
845 }
846
847 static void stac92xx_auto_init_multi_out(struct hda_codec *codec)
848 {
849         struct sigmatel_spec *spec = codec->spec;
850         int i;
851
852         for (i = 0; i < spec->autocfg.line_outs; i++) {
853                 hda_nid_t nid = spec->autocfg.line_out_pins[i];
854                 stac92xx_auto_set_pinctl(codec, nid, AC_PINCTL_OUT_EN);
855         }
856 }
857
858 static void stac92xx_auto_init_hp_out(struct hda_codec *codec)
859 {
860         struct sigmatel_spec *spec = codec->spec;
861         hda_nid_t pin;
862
863         pin = spec->autocfg.hp_pin;
864         if (pin) /* connect to front */
865                 stac92xx_auto_set_pinctl(codec, pin, AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
866 }
867
868 static int stac92xx_parse_auto_config(struct hda_codec *codec, hda_nid_t dig_out, hda_nid_t dig_in)
869 {
870         struct sigmatel_spec *spec = codec->spec;
871         int err;
872
873         if ((err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL)) < 0)
874                 return err;
875         if (! spec->autocfg.line_outs)
876                 return 0; /* can't find valid pin config */
877         if ((err = stac92xx_add_dyn_out_pins(codec, &spec->autocfg)) < 0)
878                 return err;
879         if ((err = stac92xx_auto_fill_dac_nids(codec, &spec->autocfg)) < 0)
880                 return err;
881
882         if ((err = stac92xx_auto_create_multi_out_ctls(spec, &spec->autocfg)) < 0 ||
883             (err = stac92xx_auto_create_hp_ctls(codec, &spec->autocfg)) < 0 ||
884             (err = stac92xx_auto_create_analog_input_ctls(codec, &spec->autocfg)) < 0)
885                 return err;
886
887         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
888         if (spec->multiout.max_channels > 2)
889                 spec->surr_switch = 1;
890
891         if (spec->autocfg.dig_out_pin)
892                 spec->multiout.dig_out_nid = dig_out;
893         if (spec->autocfg.dig_in_pin)
894                 spec->dig_in_nid = dig_in;
895
896         if (spec->kctl_alloc)
897                 spec->mixers[spec->num_mixers++] = spec->kctl_alloc;
898
899         spec->input_mux = &spec->private_imux;
900
901         return 1;
902 }
903
904 /* add playback controls for HP output */
905 static int stac9200_auto_create_hp_ctls(struct hda_codec *codec,
906                                         struct auto_pin_cfg *cfg)
907 {
908         struct sigmatel_spec *spec = codec->spec;
909         hda_nid_t pin = cfg->hp_pin;
910         unsigned int wid_caps;
911
912         if (! pin)
913                 return 0;
914
915         wid_caps = get_wcaps(codec, pin);
916         if (wid_caps & AC_WCAP_UNSOL_CAP)
917                 spec->hp_detect = 1;
918
919         return 0;
920 }
921
922 static int stac9200_parse_auto_config(struct hda_codec *codec)
923 {
924         struct sigmatel_spec *spec = codec->spec;
925         int err;
926
927         if ((err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL)) < 0)
928                 return err;
929
930         if ((err = stac92xx_auto_create_analog_input_ctls(codec, &spec->autocfg)) < 0)
931                 return err;
932
933         if ((err = stac9200_auto_create_hp_ctls(codec, &spec->autocfg)) < 0)
934                 return err;
935
936         if (spec->autocfg.dig_out_pin)
937                 spec->multiout.dig_out_nid = 0x05;
938         if (spec->autocfg.dig_in_pin)
939                 spec->dig_in_nid = 0x04;
940
941         if (spec->kctl_alloc)
942                 spec->mixers[spec->num_mixers++] = spec->kctl_alloc;
943
944         spec->input_mux = &spec->private_imux;
945
946         return 1;
947 }
948
949 static int stac92xx_init(struct hda_codec *codec)
950 {
951         struct sigmatel_spec *spec = codec->spec;
952         struct auto_pin_cfg *cfg = &spec->autocfg;
953         int i;
954
955         snd_hda_sequence_write(codec, spec->init);
956
957         /* set up pins */
958         if (spec->hp_detect) {
959                 /* Enable unsolicited responses on the HP widget */
960                 snd_hda_codec_write(codec, cfg->hp_pin, 0,
961                                 AC_VERB_SET_UNSOLICITED_ENABLE,
962                                 STAC_UNSOL_ENABLE);
963                 /* fake event to set up pins */
964                 codec->patch_ops.unsol_event(codec, STAC_HP_EVENT << 26);
965         } else {
966                 stac92xx_auto_init_multi_out(codec);
967                 stac92xx_auto_init_hp_out(codec);
968         }
969         for (i = 0; i < AUTO_PIN_LAST; i++) {
970                 hda_nid_t nid = cfg->input_pins[i];
971                 if (nid) {
972                         unsigned int pinctl = AC_PINCTL_IN_EN;
973                         if (i == AUTO_PIN_MIC || i == AUTO_PIN_FRONT_MIC)
974                                 pinctl |= stac92xx_get_vref(codec, nid);
975                         stac92xx_auto_set_pinctl(codec, nid, pinctl);
976                 }
977         }
978         if (cfg->dig_out_pin)
979                 stac92xx_auto_set_pinctl(codec, cfg->dig_out_pin,
980                                          AC_PINCTL_OUT_EN);
981         if (cfg->dig_in_pin)
982                 stac92xx_auto_set_pinctl(codec, cfg->dig_in_pin,
983                                          AC_PINCTL_IN_EN);
984
985         return 0;
986 }
987
988 static void stac92xx_free(struct hda_codec *codec)
989 {
990         struct sigmatel_spec *spec = codec->spec;
991         int i;
992
993         if (! spec)
994                 return;
995
996         if (spec->kctl_alloc) {
997                 for (i = 0; i < spec->num_kctl_used; i++)
998                         kfree(spec->kctl_alloc[i].name);
999                 kfree(spec->kctl_alloc);
1000         }
1001
1002         kfree(spec);
1003 }
1004
1005 static void stac92xx_set_pinctl(struct hda_codec *codec, hda_nid_t nid,
1006                                 unsigned int flag)
1007 {
1008         unsigned int pin_ctl = snd_hda_codec_read(codec, nid,
1009                         0, AC_VERB_GET_PIN_WIDGET_CONTROL, 0x00);
1010         snd_hda_codec_write(codec, nid, 0,
1011                         AC_VERB_SET_PIN_WIDGET_CONTROL,
1012                         pin_ctl | flag);
1013 }
1014
1015 static void stac92xx_reset_pinctl(struct hda_codec *codec, hda_nid_t nid,
1016                                   unsigned int flag)
1017 {
1018         unsigned int pin_ctl = snd_hda_codec_read(codec, nid,
1019                         0, AC_VERB_GET_PIN_WIDGET_CONTROL, 0x00);
1020         snd_hda_codec_write(codec, nid, 0,
1021                         AC_VERB_SET_PIN_WIDGET_CONTROL,
1022                         pin_ctl & ~flag);
1023 }
1024
1025 static void stac92xx_unsol_event(struct hda_codec *codec, unsigned int res)
1026 {
1027         struct sigmatel_spec *spec = codec->spec;
1028         struct auto_pin_cfg *cfg = &spec->autocfg;
1029         int i, presence;
1030
1031         if ((res >> 26) != STAC_HP_EVENT)
1032                 return;
1033
1034         presence = snd_hda_codec_read(codec, cfg->hp_pin, 0,
1035                         AC_VERB_GET_PIN_SENSE, 0x00) >> 31;
1036
1037         if (presence) {
1038                 /* disable lineouts, enable hp */
1039                 for (i = 0; i < cfg->line_outs; i++)
1040                         stac92xx_reset_pinctl(codec, cfg->line_out_pins[i],
1041                                                 AC_PINCTL_OUT_EN);
1042                 stac92xx_set_pinctl(codec, cfg->hp_pin, AC_PINCTL_OUT_EN);
1043         } else {
1044                 /* enable lineouts, disable hp */
1045                 for (i = 0; i < cfg->line_outs; i++)
1046                         stac92xx_set_pinctl(codec, cfg->line_out_pins[i],
1047                                                 AC_PINCTL_OUT_EN);
1048                 stac92xx_reset_pinctl(codec, cfg->hp_pin, AC_PINCTL_OUT_EN);
1049         }
1050
1051
1052 #ifdef CONFIG_PM
1053 static int stac92xx_resume(struct hda_codec *codec)
1054 {
1055         struct sigmatel_spec *spec = codec->spec;
1056         int i;
1057
1058         stac92xx_init(codec);
1059         for (i = 0; i < spec->num_mixers; i++)
1060                 snd_hda_resume_ctls(codec, spec->mixers[i]);
1061         if (spec->multiout.dig_out_nid)
1062                 snd_hda_resume_spdif_out(codec);
1063         if (spec->dig_in_nid)
1064                 snd_hda_resume_spdif_in(codec);
1065
1066         return 0;
1067 }
1068 #endif
1069
1070 static struct hda_codec_ops stac92xx_patch_ops = {
1071         .build_controls = stac92xx_build_controls,
1072         .build_pcms = stac92xx_build_pcms,
1073         .init = stac92xx_init,
1074         .free = stac92xx_free,
1075         .unsol_event = stac92xx_unsol_event,
1076 #ifdef CONFIG_PM
1077         .resume = stac92xx_resume,
1078 #endif
1079 };
1080
1081 static int patch_stac9200(struct hda_codec *codec)
1082 {
1083         struct sigmatel_spec *spec;
1084         int err;
1085
1086         spec  = kzalloc(sizeof(*spec), GFP_KERNEL);
1087         if (spec == NULL)
1088                 return -ENOMEM;
1089
1090         codec->spec = spec;
1091         spec->board_config = snd_hda_check_board_config(codec, stac9200_cfg_tbl);
1092         if (spec->board_config < 0)
1093                 snd_printdd(KERN_INFO "hda_codec: Unknown model for STAC9200, using BIOS defaults\n");
1094         else {
1095                 spec->num_pins = 8;
1096                 spec->pin_nids = stac9200_pin_nids;
1097                 spec->pin_configs = stac9200_brd_tbl[spec->board_config];
1098                 stac92xx_set_config_regs(codec);
1099         }
1100
1101         spec->multiout.max_channels = 2;
1102         spec->multiout.num_dacs = 1;
1103         spec->multiout.dac_nids = stac9200_dac_nids;
1104         spec->adc_nids = stac9200_adc_nids;
1105         spec->mux_nids = stac9200_mux_nids;
1106         spec->num_muxes = 1;
1107
1108         spec->init = stac9200_core_init;
1109         spec->mixer = stac9200_mixer;
1110
1111         err = stac9200_parse_auto_config(codec);
1112         if (err < 0) {
1113                 stac92xx_free(codec);
1114                 return err;
1115         }
1116
1117         codec->patch_ops = stac92xx_patch_ops;
1118
1119         return 0;
1120 }
1121
1122 static int patch_stac922x(struct hda_codec *codec)
1123 {
1124         struct sigmatel_spec *spec;
1125         int err;
1126
1127         spec  = kzalloc(sizeof(*spec), GFP_KERNEL);
1128         if (spec == NULL)
1129                 return -ENOMEM;
1130
1131         codec->spec = spec;
1132         spec->board_config = snd_hda_check_board_config(codec, stac922x_cfg_tbl);
1133         if (spec->board_config < 0)
1134                 snd_printdd(KERN_INFO "hda_codec: Unknown model for STAC922x, using BIOS defaults\n");
1135         else {
1136                 spec->num_pins = 10;
1137                 spec->pin_nids = stac922x_pin_nids;
1138                 spec->pin_configs = stac922x_brd_tbl[spec->board_config];
1139                 stac92xx_set_config_regs(codec);
1140         }
1141
1142         spec->adc_nids = stac922x_adc_nids;
1143         spec->mux_nids = stac922x_mux_nids;
1144         spec->num_muxes = 2;
1145
1146         spec->init = stac922x_core_init;
1147         spec->mixer = stac922x_mixer;
1148
1149         spec->multiout.dac_nids = spec->dac_nids;
1150
1151         err = stac92xx_parse_auto_config(codec, 0x08, 0x09);
1152         if (err < 0) {
1153                 stac92xx_free(codec);
1154                 return err;
1155         }
1156
1157         codec->patch_ops = stac92xx_patch_ops;
1158
1159         return 0;
1160 }
1161
1162 static int patch_stac927x(struct hda_codec *codec)
1163 {
1164         struct sigmatel_spec *spec;
1165         int err;
1166
1167         spec  = kzalloc(sizeof(*spec), GFP_KERNEL);
1168         if (spec == NULL)
1169                 return -ENOMEM;
1170
1171         codec->spec = spec;
1172         spec->board_config = snd_hda_check_board_config(codec, stac927x_cfg_tbl);
1173         if (spec->board_config < 0)
1174                 snd_printdd(KERN_INFO "hda_codec: Unknown model for STAC927x, using BIOS defaults\n");
1175         else {
1176                 spec->num_pins = 14;
1177                 spec->pin_nids = stac927x_pin_nids;
1178                 spec->pin_configs = stac927x_brd_tbl[spec->board_config];
1179                 stac92xx_set_config_regs(codec);
1180         }
1181
1182         spec->adc_nids = stac927x_adc_nids;
1183         spec->mux_nids = stac927x_mux_nids;
1184         spec->num_muxes = 3;
1185
1186         spec->init = stac927x_core_init;
1187         spec->mixer = stac927x_mixer;
1188
1189         spec->multiout.dac_nids = spec->dac_nids;
1190
1191         err = stac92xx_parse_auto_config(codec, 0x1e, 0x20);
1192         if (err < 0) {
1193                 stac92xx_free(codec);
1194                 return err;
1195         }
1196
1197         codec->patch_ops = stac92xx_patch_ops;
1198
1199         return 0;
1200 }
1201
1202 /*
1203  * STAC 7661(?) hack
1204  */
1205
1206 /* static config for Sony VAIO FE550G */
1207 static hda_nid_t vaio_dacs[] = { 0x2 };
1208 #define VAIO_HP_DAC     0x5
1209 static hda_nid_t vaio_adcs[] = { 0x8 /*,0x6*/ };
1210 static hda_nid_t vaio_mux_nids[] = { 0x15 };
1211
1212 static struct hda_input_mux vaio_mux = {
1213         .num_items = 2,
1214         .items = {
1215                 /* { "HP", 0x0 }, */
1216                 { "Line", 0x1 },
1217                 { "Mic", 0x2 },
1218                 { "PCM", 0x3 },
1219         }
1220 };
1221
1222 static struct hda_verb vaio_init[] = {
1223         {0x0a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP }, /* HP <- 0x2 */
1224         {0x0f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT }, /* Speaker <- 0x5 */
1225         {0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 }, /* Mic? (<- 0x2) */
1226         {0x0e, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN }, /* CD */
1227         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 }, /* Mic? */
1228         {0x15, AC_VERB_SET_CONNECT_SEL, 0x2}, /* mic-sel: 0a,0d,14,02 */
1229         {0x02, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE}, /* HP */
1230         {0x05, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE}, /* Speaker */
1231         {0x09, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)}, /* capture sw/vol -> 0x8 */
1232         {0x07, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)}, /* CD-in -> 0x6 */
1233         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE}, /* Mic-in -> 0x9 */
1234         {}
1235 };
1236
1237 /* bind volumes of both NID 0x02 and 0x05 */
1238 static int vaio_master_vol_put(struct snd_kcontrol *kcontrol,
1239                                struct snd_ctl_elem_value *ucontrol)
1240 {
1241         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1242         long *valp = ucontrol->value.integer.value;
1243         int change;
1244
1245         change = snd_hda_codec_amp_update(codec, 0x02, 0, HDA_OUTPUT, 0,
1246                                           0x7f, valp[0] & 0x7f);
1247         change |= snd_hda_codec_amp_update(codec, 0x02, 1, HDA_OUTPUT, 0,
1248                                            0x7f, valp[1] & 0x7f);
1249         snd_hda_codec_amp_update(codec, 0x05, 0, HDA_OUTPUT, 0,
1250                                  0x7f, valp[0] & 0x7f);
1251         snd_hda_codec_amp_update(codec, 0x05, 1, HDA_OUTPUT, 0,
1252                                  0x7f, valp[1] & 0x7f);
1253         return change;
1254 }
1255
1256 /* bind volumes of both NID 0x02 and 0x05 */
1257 static int vaio_master_sw_put(struct snd_kcontrol *kcontrol,
1258                               struct snd_ctl_elem_value *ucontrol)
1259 {
1260         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1261         long *valp = ucontrol->value.integer.value;
1262         int change;
1263
1264         change = snd_hda_codec_amp_update(codec, 0x02, 0, HDA_OUTPUT, 0,
1265                                           0x80, valp[0] & 0x80);
1266         change |= snd_hda_codec_amp_update(codec, 0x02, 1, HDA_OUTPUT, 0,
1267                                            0x80, valp[1] & 0x80);
1268         snd_hda_codec_amp_update(codec, 0x05, 0, HDA_OUTPUT, 0,
1269                                  0x80, valp[0] & 0x80);
1270         snd_hda_codec_amp_update(codec, 0x05, 1, HDA_OUTPUT, 0,
1271                                  0x80, valp[1] & 0x80);
1272         return change;
1273 }
1274
1275 static struct snd_kcontrol_new vaio_mixer[] = {
1276         {
1277                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1278                 .name = "Master Playback Volume",
1279                 .info = snd_hda_mixer_amp_volume_info,
1280                 .get = snd_hda_mixer_amp_volume_get,
1281                 .put = vaio_master_vol_put,
1282                 .private_value = HDA_COMPOSE_AMP_VAL(0x02, 3, 0, HDA_OUTPUT),
1283         },
1284         {
1285                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1286                 .name = "Master Playback Switch",
1287                 .info = snd_hda_mixer_amp_switch_info,
1288                 .get = snd_hda_mixer_amp_switch_get,
1289                 .put = vaio_master_sw_put,
1290                 .private_value = HDA_COMPOSE_AMP_VAL(0x02, 3, 0, HDA_OUTPUT),
1291         },
1292         /* HDA_CODEC_VOLUME("CD Capture Volume", 0x07, 0, HDA_INPUT), */
1293         HDA_CODEC_VOLUME("Capture Volume", 0x09, 0, HDA_INPUT),
1294         HDA_CODEC_MUTE("Capture Switch", 0x09, 0, HDA_INPUT),
1295         {
1296                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1297                 .name = "Capture Source",
1298                 .count = 1,
1299                 .info = stac92xx_mux_enum_info,
1300                 .get = stac92xx_mux_enum_get,
1301                 .put = stac92xx_mux_enum_put,
1302         },
1303         {}
1304 };
1305
1306 static struct hda_codec_ops stac7661_patch_ops = {
1307         .build_controls = stac92xx_build_controls,
1308         .build_pcms = stac92xx_build_pcms,
1309         .init = stac92xx_init,
1310         .free = stac92xx_free,
1311 #ifdef CONFIG_PM
1312         .resume = stac92xx_resume,
1313 #endif
1314 };
1315
1316 enum { STAC7661_VAIO };
1317
1318 static struct hda_board_config stac7661_cfg_tbl[] = {
1319         { .modelname = "vaio", .config = STAC7661_VAIO },
1320         { .pci_subvendor = 0x104d, .pci_subdevice = 0x81e6,
1321           .config = STAC7661_VAIO },
1322         { .pci_subvendor = 0x104d, .pci_subdevice = 0x81ef,
1323           .config = STAC7661_VAIO },
1324         {}
1325 };
1326
1327 static int patch_stac7661(struct hda_codec *codec)
1328 {
1329         struct sigmatel_spec *spec;
1330         int board_config;
1331
1332         board_config = snd_hda_check_board_config(codec, stac7661_cfg_tbl);
1333         if (board_config < 0)
1334                 /* unknown config, let generic-parser do its job... */
1335                 return snd_hda_parse_generic_codec(codec);
1336         
1337         spec  = kzalloc(sizeof(*spec), GFP_KERNEL);
1338         if (spec == NULL)
1339                 return -ENOMEM;
1340
1341         codec->spec = spec;
1342         switch (board_config) {
1343         case STAC7661_VAIO:
1344                 spec->mixer = vaio_mixer;
1345                 spec->init = vaio_init;
1346                 spec->multiout.max_channels = 2;
1347                 spec->multiout.num_dacs = ARRAY_SIZE(vaio_dacs);
1348                 spec->multiout.dac_nids = vaio_dacs;
1349                 spec->multiout.hp_nid = VAIO_HP_DAC;
1350                 spec->num_adcs = ARRAY_SIZE(vaio_adcs);
1351                 spec->adc_nids = vaio_adcs;
1352                 spec->input_mux = &vaio_mux;
1353                 spec->mux_nids = vaio_mux_nids;
1354                 break;
1355         }
1356
1357         codec->patch_ops = stac7661_patch_ops;
1358         return 0;
1359 }
1360
1361
1362 /*
1363  * patch entries
1364  */
1365 struct hda_codec_preset snd_hda_preset_sigmatel[] = {
1366         { .id = 0x83847690, .name = "STAC9200", .patch = patch_stac9200 },
1367         { .id = 0x83847882, .name = "STAC9220 A1", .patch = patch_stac922x },
1368         { .id = 0x83847680, .name = "STAC9221 A1", .patch = patch_stac922x },
1369         { .id = 0x83847880, .name = "STAC9220 A2", .patch = patch_stac922x },
1370         { .id = 0x83847681, .name = "STAC9220D/9223D A2", .patch = patch_stac922x },
1371         { .id = 0x83847682, .name = "STAC9221 A2", .patch = patch_stac922x },
1372         { .id = 0x83847683, .name = "STAC9221D A2", .patch = patch_stac922x },
1373         { .id = 0x83847620, .name = "STAC9274", .patch = patch_stac927x },
1374         { .id = 0x83847621, .name = "STAC9274D", .patch = patch_stac927x },
1375         { .id = 0x83847622, .name = "STAC9273X", .patch = patch_stac927x },
1376         { .id = 0x83847623, .name = "STAC9273D", .patch = patch_stac927x },
1377         { .id = 0x83847624, .name = "STAC9272X", .patch = patch_stac927x },
1378         { .id = 0x83847625, .name = "STAC9272D", .patch = patch_stac927x },
1379         { .id = 0x83847626, .name = "STAC9271X", .patch = patch_stac927x },
1380         { .id = 0x83847627, .name = "STAC9271D", .patch = patch_stac927x },
1381         { .id = 0x83847628, .name = "STAC9274X5NH", .patch = patch_stac927x },
1382         { .id = 0x83847629, .name = "STAC9274D5NH", .patch = patch_stac927x },
1383         { .id = 0x83847661, .name = "STAC7661", .patch = patch_stac7661 },
1384         {} /* terminator */
1385 };