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