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