4f78b58416a4d3de4f8dd65ad9dd06be004096fe
[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
54         /* playback */
55         struct hda_multi_out multiout;
56         hda_nid_t dac_nids[4];
57
58         /* capture */
59         hda_nid_t *adc_nids;
60         unsigned int num_adcs;
61         hda_nid_t *mux_nids;
62         unsigned int num_muxes;
63         hda_nid_t dig_in_nid;
64
65         /* pin widgets */
66         hda_nid_t *pin_nids;
67         unsigned int num_pins;
68         unsigned int *pin_configs;
69
70         /* codec specific stuff */
71         struct hda_verb *init;
72         struct snd_kcontrol_new *mixer;
73
74         /* capture source */
75         struct hda_input_mux *input_mux;
76         unsigned int cur_mux[2];
77
78         /* i/o switches */
79         unsigned int io_switch[2];
80
81         struct hda_pcm pcm_rec[2];      /* PCM information */
82
83         /* dynamic controls and input_mux */
84         struct auto_pin_cfg autocfg;
85         unsigned int num_kctl_alloc, num_kctl_used;
86         struct snd_kcontrol_new *kctl_alloc;
87         struct hda_input_mux private_imux;
88 };
89
90 static hda_nid_t stac9200_adc_nids[1] = {
91         0x03,
92 };
93
94 static hda_nid_t stac9200_mux_nids[1] = {
95         0x0c,
96 };
97
98 static hda_nid_t stac9200_dac_nids[1] = {
99         0x02,
100 };
101
102 static hda_nid_t stac922x_adc_nids[2] = {
103         0x06, 0x07,
104 };
105
106 static hda_nid_t stac922x_mux_nids[2] = {
107         0x12, 0x13,
108 };
109
110 static hda_nid_t stac9200_pin_nids[8] = {
111         0x08, 0x09, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12,
112 };
113
114 static hda_nid_t stac922x_pin_nids[10] = {
115         0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
116         0x0f, 0x10, 0x11, 0x15, 0x1b,
117 };
118
119 static int stac92xx_mux_enum_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
120 {
121         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
122         struct sigmatel_spec *spec = codec->spec;
123         return snd_hda_input_mux_info(spec->input_mux, uinfo);
124 }
125
126 static int stac92xx_mux_enum_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
127 {
128         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
129         struct sigmatel_spec *spec = codec->spec;
130         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
131
132         ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
133         return 0;
134 }
135
136 static int stac92xx_mux_enum_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
137 {
138         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
139         struct sigmatel_spec *spec = codec->spec;
140         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
141
142         return snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
143                                      spec->mux_nids[adc_idx], &spec->cur_mux[adc_idx]);
144 }
145
146 static struct hda_verb stac9200_core_init[] = {
147         /* set dac0mux for dac converter */
148         { 0x07, AC_VERB_SET_CONNECT_SEL, 0x00},
149         {}
150 };
151
152 static struct hda_verb stac922x_core_init[] = {
153         /* set master volume and direct control */      
154         { 0x16, AC_VERB_SET_VOLUME_KNOB_CONTROL, 0xff},
155         {}
156 };
157
158 static struct snd_kcontrol_new stac9200_mixer[] = {
159         HDA_CODEC_VOLUME("Master Playback Volume", 0xb, 0, HDA_OUTPUT),
160         HDA_CODEC_MUTE("Master Playback Switch", 0xb, 0, HDA_OUTPUT),
161         {
162                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
163                 .name = "Input Source",
164                 .count = 1,
165                 .info = stac92xx_mux_enum_info,
166                 .get = stac92xx_mux_enum_get,
167                 .put = stac92xx_mux_enum_put,
168         },
169         HDA_CODEC_VOLUME("Capture Volume", 0x0a, 0, HDA_OUTPUT),
170         HDA_CODEC_MUTE("Capture Switch", 0x0a, 0, HDA_OUTPUT),
171         HDA_CODEC_VOLUME("Capture Mux Volume", 0x0c, 0, HDA_OUTPUT),
172         { } /* end */
173 };
174
175 /* This needs to be generated dynamically based on sequence */
176 static struct snd_kcontrol_new stac922x_mixer[] = {
177         {
178                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
179                 .name = "Input Source",
180                 .count = 1,
181                 .info = stac92xx_mux_enum_info,
182                 .get = stac92xx_mux_enum_get,
183                 .put = stac92xx_mux_enum_put,
184         },
185         HDA_CODEC_VOLUME("Capture Volume", 0x17, 0x0, HDA_INPUT),
186         HDA_CODEC_MUTE("Capture Switch", 0x17, 0x0, HDA_INPUT),
187         HDA_CODEC_VOLUME("Mux Capture Volume", 0x12, 0x0, HDA_OUTPUT),
188         { } /* end */
189 };
190
191 static int stac92xx_build_controls(struct hda_codec *codec)
192 {
193         struct sigmatel_spec *spec = codec->spec;
194         int err;
195         int i;
196
197         err = snd_hda_add_new_ctls(codec, spec->mixer);
198         if (err < 0)
199                 return err;
200
201         for (i = 0; i < spec->num_mixers; i++) {
202                 err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
203                 if (err < 0)
204                         return err;
205         }
206
207         if (spec->multiout.dig_out_nid) {
208                 err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid);
209                 if (err < 0)
210                         return err;
211         }
212         if (spec->dig_in_nid) {
213                 err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
214                 if (err < 0)
215                         return err;
216         }
217         return 0;       
218 }
219
220 static unsigned int ref9200_pin_configs[8] = {
221         0x01c47010, 0x01447010, 0x0221401f, 0x01114010,
222         0x02a19020, 0x01a19021, 0x90100140, 0x01813122,
223 };
224
225 static unsigned int *stac9200_brd_tbl[] = {
226         ref9200_pin_configs,
227 };
228
229 static struct hda_board_config stac9200_cfg_tbl[] = {
230         { .modelname = "ref",
231           .pci_subvendor = PCI_VENDOR_ID_INTEL,
232           .pci_subdevice = 0x2668,      /* DFI LanParty */
233           .config = STAC_REF },
234         {} /* terminator */
235 };
236
237 static unsigned int ref922x_pin_configs[10] = {
238         0x01014010, 0x01016011, 0x01012012, 0x0221401f,
239         0x01813122, 0x01011014, 0x01441030, 0x01c41030,
240         0x40000100, 0x40000100,
241 };
242
243 static unsigned int d945gtp3_pin_configs[10] = {
244         0x0221401f, 0x01a19022, 0x01813021, 0x01114010,
245         0x40000100, 0x40000100, 0x40000100, 0x40000100,
246         0x02a19120, 0x40000100,
247 };
248
249 static unsigned int d945gtp5_pin_configs[10] = {
250         0x0221401f, 0x01111012, 0x01813024, 0x01114010,
251         0x01a19021, 0x01116011, 0x01452130, 0x40000100,
252         0x02a19320, 0x40000100,
253 };
254
255 static unsigned int *stac922x_brd_tbl[] = {
256         ref922x_pin_configs,
257         d945gtp3_pin_configs,
258         d945gtp5_pin_configs,
259 };
260
261 static struct hda_board_config stac922x_cfg_tbl[] = {
262         { .modelname = "ref",
263           .pci_subvendor = PCI_VENDOR_ID_INTEL,
264           .pci_subdevice = 0x2668,      /* DFI LanParty */
265           .config = STAC_REF },         /* SigmaTel reference board */
266         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
267           .pci_subdevice = 0x0101,
268           .config = STAC_D945GTP3 },    /* Intel D945GTP - 3 Stack */
269         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
270           .pci_subdevice = 0x0404,
271           .config = STAC_D945GTP5 },    /* Intel D945GTP - 5 Stack */
272         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
273           .pci_subdevice = 0x0303,
274           .config = STAC_D945GTP5 },    /* Intel D945GNT - 5 Stack */
275         { .pci_subvendor = PCI_VENDOR_ID_INTEL,
276           .pci_subdevice = 0x0013,
277           .config = STAC_D945GTP5 },    /* Intel D955XBK - 5 Stack */
278         {} /* terminator */
279 };
280
281 static void stac92xx_set_config_regs(struct hda_codec *codec)
282 {
283         int i;
284         struct sigmatel_spec *spec = codec->spec;
285         unsigned int pin_cfg;
286
287         for (i=0; i < spec->num_pins; i++) {
288                 snd_hda_codec_write(codec, spec->pin_nids[i], 0,
289                                     AC_VERB_SET_CONFIG_DEFAULT_BYTES_0,
290                                     spec->pin_configs[i] & 0x000000ff);
291                 snd_hda_codec_write(codec, spec->pin_nids[i], 0,
292                                     AC_VERB_SET_CONFIG_DEFAULT_BYTES_1,
293                                     (spec->pin_configs[i] & 0x0000ff00) >> 8);
294                 snd_hda_codec_write(codec, spec->pin_nids[i], 0,
295                                     AC_VERB_SET_CONFIG_DEFAULT_BYTES_2,
296                                     (spec->pin_configs[i] & 0x00ff0000) >> 16);
297                 snd_hda_codec_write(codec, spec->pin_nids[i], 0,
298                                     AC_VERB_SET_CONFIG_DEFAULT_BYTES_3,
299                                     spec->pin_configs[i] >> 24);
300                 pin_cfg = snd_hda_codec_read(codec, spec->pin_nids[i], 0,
301                                              AC_VERB_GET_CONFIG_DEFAULT,
302                                              0x00);     
303                 snd_printdd(KERN_INFO "hda_codec: pin nid %2.2x pin config %8.8x\n", spec->pin_nids[i], pin_cfg);
304         }
305 }
306
307 /*
308  * Analog playback callbacks
309  */
310 static int stac92xx_playback_pcm_open(struct hda_pcm_stream *hinfo,
311                                       struct hda_codec *codec,
312                                       struct snd_pcm_substream *substream)
313 {
314         struct sigmatel_spec *spec = codec->spec;
315         return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream);
316 }
317
318 static int stac92xx_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
319                                          struct hda_codec *codec,
320                                          unsigned int stream_tag,
321                                          unsigned int format,
322                                          struct snd_pcm_substream *substream)
323 {
324         struct sigmatel_spec *spec = codec->spec;
325         return snd_hda_multi_out_analog_prepare(codec, &spec->multiout, stream_tag, format, substream);
326 }
327
328 static int stac92xx_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
329                                         struct hda_codec *codec,
330                                         struct snd_pcm_substream *substream)
331 {
332         struct sigmatel_spec *spec = codec->spec;
333         return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
334 }
335
336 /*
337  * Digital playback callbacks
338  */
339 static int stac92xx_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
340                                           struct hda_codec *codec,
341                                           struct snd_pcm_substream *substream)
342 {
343         struct sigmatel_spec *spec = codec->spec;
344         return snd_hda_multi_out_dig_open(codec, &spec->multiout);
345 }
346
347 static int stac92xx_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
348                                            struct hda_codec *codec,
349                                            struct snd_pcm_substream *substream)
350 {
351         struct sigmatel_spec *spec = codec->spec;
352         return snd_hda_multi_out_dig_close(codec, &spec->multiout);
353 }
354
355
356 /*
357  * Analog capture callbacks
358  */
359 static int stac92xx_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
360                                         struct hda_codec *codec,
361                                         unsigned int stream_tag,
362                                         unsigned int format,
363                                         struct snd_pcm_substream *substream)
364 {
365         struct sigmatel_spec *spec = codec->spec;
366
367         snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
368                                    stream_tag, 0, format);
369         return 0;
370 }
371
372 static int stac92xx_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
373                                         struct hda_codec *codec,
374                                         struct snd_pcm_substream *substream)
375 {
376         struct sigmatel_spec *spec = codec->spec;
377
378         snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number], 0, 0, 0);
379         return 0;
380 }
381
382 static struct hda_pcm_stream stac92xx_pcm_digital_playback = {
383         .substreams = 1,
384         .channels_min = 2,
385         .channels_max = 2,
386         /* NID is set in stac92xx_build_pcms */
387         .ops = {
388                 .open = stac92xx_dig_playback_pcm_open,
389                 .close = stac92xx_dig_playback_pcm_close
390         },
391 };
392
393 static struct hda_pcm_stream stac92xx_pcm_digital_capture = {
394         .substreams = 1,
395         .channels_min = 2,
396         .channels_max = 2,
397         /* NID is set in stac92xx_build_pcms */
398 };
399
400 static struct hda_pcm_stream stac92xx_pcm_analog_playback = {
401         .substreams = 1,
402         .channels_min = 2,
403         .channels_max = 8,
404         .nid = 0x02, /* NID to query formats and rates */
405         .ops = {
406                 .open = stac92xx_playback_pcm_open,
407                 .prepare = stac92xx_playback_pcm_prepare,
408                 .cleanup = stac92xx_playback_pcm_cleanup
409         },
410 };
411
412 static struct hda_pcm_stream stac92xx_pcm_analog_capture = {
413         .substreams = 2,
414         .channels_min = 2,
415         .channels_max = 2,
416         .nid = 0x06, /* NID to query formats and rates */
417         .ops = {
418                 .prepare = stac92xx_capture_pcm_prepare,
419                 .cleanup = stac92xx_capture_pcm_cleanup
420         },
421 };
422
423 static int stac92xx_build_pcms(struct hda_codec *codec)
424 {
425         struct sigmatel_spec *spec = codec->spec;
426         struct hda_pcm *info = spec->pcm_rec;
427
428         codec->num_pcms = 1;
429         codec->pcm_info = info;
430
431         info->name = "STAC92xx Analog";
432         info->stream[SNDRV_PCM_STREAM_PLAYBACK] = stac92xx_pcm_analog_playback;
433         info->stream[SNDRV_PCM_STREAM_CAPTURE] = stac92xx_pcm_analog_capture;
434
435         if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
436                 codec->num_pcms++;
437                 info++;
438                 info->name = "STAC92xx Digital";
439                 if (spec->multiout.dig_out_nid) {
440                         info->stream[SNDRV_PCM_STREAM_PLAYBACK] = stac92xx_pcm_digital_playback;
441                         info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
442                 }
443                 if (spec->dig_in_nid) {
444                         info->stream[SNDRV_PCM_STREAM_CAPTURE] = stac92xx_pcm_digital_capture;
445                         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
446                 }
447         }
448
449         return 0;
450 }
451
452 static void stac92xx_auto_set_pinctl(struct hda_codec *codec, hda_nid_t nid, int pin_type)
453
454 {
455         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL, pin_type);
456 }
457
458 static int stac92xx_io_switch_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
459 {
460         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
461         uinfo->count = 1;
462         uinfo->value.integer.min = 0;
463         uinfo->value.integer.max = 1;
464         return 0;
465 }
466
467 static int stac92xx_io_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
468 {
469         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
470         struct sigmatel_spec *spec = codec->spec;
471         int io_idx = kcontrol-> private_value & 0xff;
472
473         ucontrol->value.integer.value[0] = spec->io_switch[io_idx];
474         return 0;
475 }
476
477 static int stac92xx_io_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
478 {
479         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
480         struct sigmatel_spec *spec = codec->spec;
481         hda_nid_t nid = kcontrol->private_value >> 8;
482         int io_idx = kcontrol-> private_value & 0xff;
483         unsigned short val = ucontrol->value.integer.value[0];
484
485         spec->io_switch[io_idx] = val;
486
487         if (val)
488                 stac92xx_auto_set_pinctl(codec, nid, AC_PINCTL_OUT_EN);
489         else
490                 stac92xx_auto_set_pinctl(codec, nid, AC_PINCTL_IN_EN);
491
492         return 1;
493 }
494
495 #define STAC_CODEC_IO_SWITCH(xname, xpval) \
496         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
497           .name = xname, \
498           .index = 0, \
499           .info = stac92xx_io_switch_info, \
500           .get = stac92xx_io_switch_get, \
501           .put = stac92xx_io_switch_put, \
502           .private_value = xpval, \
503         }
504
505
506 enum {
507         STAC_CTL_WIDGET_VOL,
508         STAC_CTL_WIDGET_MUTE,
509         STAC_CTL_WIDGET_IO_SWITCH,
510 };
511
512 static struct snd_kcontrol_new stac92xx_control_templates[] = {
513         HDA_CODEC_VOLUME(NULL, 0, 0, 0),
514         HDA_CODEC_MUTE(NULL, 0, 0, 0),
515         STAC_CODEC_IO_SWITCH(NULL, 0),
516 };
517
518 /* add dynamic controls */
519 static int stac92xx_add_control(struct sigmatel_spec *spec, int type, const char *name, unsigned long val)
520 {
521         struct snd_kcontrol_new *knew;
522
523         if (spec->num_kctl_used >= spec->num_kctl_alloc) {
524                 int num = spec->num_kctl_alloc + NUM_CONTROL_ALLOC;
525
526                 knew = kcalloc(num + 1, sizeof(*knew), GFP_KERNEL); /* array + terminator */
527                 if (! knew)
528                         return -ENOMEM;
529                 if (spec->kctl_alloc) {
530                         memcpy(knew, spec->kctl_alloc, sizeof(*knew) * spec->num_kctl_alloc);
531                         kfree(spec->kctl_alloc);
532                 }
533                 spec->kctl_alloc = knew;
534                 spec->num_kctl_alloc = num;
535         }
536
537         knew = &spec->kctl_alloc[spec->num_kctl_used];
538         *knew = stac92xx_control_templates[type];
539         knew->name = kstrdup(name, GFP_KERNEL);
540         if (! knew->name)
541                 return -ENOMEM;
542         knew->private_value = val;
543         spec->num_kctl_used++;
544         return 0;
545 }
546
547 /* flag inputs as additional dynamic lineouts */
548 static int stac92xx_add_dyn_out_pins(struct hda_codec *codec, struct auto_pin_cfg *cfg)
549 {
550         struct sigmatel_spec *spec = codec->spec;
551
552         switch (cfg->line_outs) {
553         case 3:
554                 /* add line-in as side */
555                 if (cfg->input_pins[AUTO_PIN_LINE]) {
556                         cfg->line_out_pins[3] = cfg->input_pins[AUTO_PIN_LINE];
557                         spec->line_switch = 1;
558                         cfg->line_outs++;
559                 }
560                 break;
561         case 2:
562                 /* add line-in as clfe and mic as side */
563                 if (cfg->input_pins[AUTO_PIN_LINE]) {
564                         cfg->line_out_pins[2] = cfg->input_pins[AUTO_PIN_LINE];
565                         spec->line_switch = 1;
566                         cfg->line_outs++;
567                 }
568                 if (cfg->input_pins[AUTO_PIN_MIC]) {
569                         cfg->line_out_pins[3] = cfg->input_pins[AUTO_PIN_MIC];
570                         spec->mic_switch = 1;
571                         cfg->line_outs++;
572                 }
573                 break;
574         case 1:
575                 /* add line-in as surr and mic as clfe */
576                 if (cfg->input_pins[AUTO_PIN_LINE]) {
577                         cfg->line_out_pins[1] = cfg->input_pins[AUTO_PIN_LINE];
578                         spec->line_switch = 1;
579                         cfg->line_outs++;
580                 }
581                 if (cfg->input_pins[AUTO_PIN_MIC]) {
582                         cfg->line_out_pins[2] = cfg->input_pins[AUTO_PIN_MIC];
583                         spec->mic_switch = 1;
584                         cfg->line_outs++;
585                 }
586                 break;
587         }
588
589         return 0;
590 }
591
592 /* fill in the dac_nids table from the parsed pin configuration */
593 static int stac92xx_auto_fill_dac_nids(struct hda_codec *codec, const struct auto_pin_cfg *cfg)
594 {
595         struct sigmatel_spec *spec = codec->spec;
596         hda_nid_t nid;
597         int i;
598
599         /* check the pins hardwired to audio widget */
600         for (i = 0; i < cfg->line_outs; i++) {
601                 nid = cfg->line_out_pins[i];
602                 spec->multiout.dac_nids[i] = snd_hda_codec_read(codec, nid, 0,
603                                         AC_VERB_GET_CONNECT_LIST, 0) & 0xff;
604         }
605
606         spec->multiout.num_dacs = cfg->line_outs;
607
608         return 0;
609 }
610
611 /* add playback controls from the parsed DAC table */
612 static int stac92xx_auto_create_multi_out_ctls(struct sigmatel_spec *spec, const struct auto_pin_cfg *cfg)
613 {
614         char name[32];
615         static const char *chname[4] = { "Front", "Surround", NULL /*CLFE*/, "Side" };
616         hda_nid_t nid;
617         int i, err;
618
619         for (i = 0; i < cfg->line_outs; i++) {
620                 if (!spec->multiout.dac_nids[i])
621                         continue;
622
623                 nid = spec->multiout.dac_nids[i];
624
625                 if (i == 2) {
626                         /* Center/LFE */
627                         if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_VOL, "Center Playback Volume",
628                                                HDA_COMPOSE_AMP_VAL(nid, 1, 0, HDA_OUTPUT))) < 0)
629                                 return err;
630                         if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_VOL, "LFE Playback Volume",
631                                                HDA_COMPOSE_AMP_VAL(nid, 2, 0, HDA_OUTPUT))) < 0)
632                                 return err;
633                         if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_MUTE, "Center Playback Switch",
634                                                HDA_COMPOSE_AMP_VAL(nid, 1, 0, HDA_OUTPUT))) < 0)
635                                 return err;
636                         if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_MUTE, "LFE Playback Switch",
637                                                HDA_COMPOSE_AMP_VAL(nid, 2, 0, HDA_OUTPUT))) < 0)
638                                 return err;
639                 } else {
640                         sprintf(name, "%s Playback Volume", chname[i]);
641                         if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_VOL, name,
642                                                HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
643                                 return err;
644                         sprintf(name, "%s Playback Switch", chname[i]);
645                         if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_MUTE, name,
646                                                HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
647                                 return err;
648                 }
649         }
650
651         if (spec->line_switch)
652                 if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_IO_SWITCH, "Line In as Output Switch", cfg->input_pins[AUTO_PIN_LINE] << 8)) < 0)
653                         return err;
654
655         if (spec->mic_switch)
656                 if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_IO_SWITCH, "Mic as Output Switch", (cfg->input_pins[AUTO_PIN_MIC] << 8) | 1)) < 0)
657                         return err;
658
659         return 0;
660 }
661
662 /* add playback controls for HP output */
663 static int stac92xx_auto_create_hp_ctls(struct hda_codec *codec, struct auto_pin_cfg *cfg)
664 {
665         struct sigmatel_spec *spec = codec->spec;
666         hda_nid_t pin = cfg->hp_pin;
667         hda_nid_t nid;
668         int i, err;
669         unsigned int wid_caps;
670
671         if (! pin)
672                 return 0;
673
674         wid_caps = get_wcaps(codec, pin);
675         if (wid_caps & AC_WCAP_UNSOL_CAP)
676                 /* Enable unsolicited responses on the HP widget */
677                 snd_hda_codec_write(codec, pin, 0,
678                                 AC_VERB_SET_UNSOLICITED_ENABLE,
679                                 STAC_UNSOL_ENABLE);
680
681         nid = snd_hda_codec_read(codec, pin, 0, AC_VERB_GET_CONNECT_LIST, 0) & 0xff;
682         for (i = 0; i < cfg->line_outs; i++) {
683                 if (! spec->multiout.dac_nids[i])
684                         continue;
685                 if (spec->multiout.dac_nids[i] == nid)
686                         return 0;
687         }
688
689         spec->multiout.hp_nid = nid;
690
691         /* control HP volume/switch on the output mixer amp */
692         if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_VOL, "Headphone Playback Volume",
693                                         HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
694                 return err;
695         if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_MUTE, "Headphone Playback Switch",
696                                         HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
697                 return err;
698
699         return 0;
700 }
701
702 /* create playback/capture controls for input pins */
703 static int stac92xx_auto_create_analog_input_ctls(struct hda_codec *codec, const struct auto_pin_cfg *cfg)
704 {
705         struct sigmatel_spec *spec = codec->spec;
706         struct hda_input_mux *imux = &spec->private_imux;
707         hda_nid_t con_lst[HDA_MAX_NUM_INPUTS];
708         int i, j, k;
709
710         for (i = 0; i < AUTO_PIN_LAST; i++) {
711                 int index = -1;
712                 if (cfg->input_pins[i]) {
713                         /* Enable active pin widget as an input */
714                         stac92xx_auto_set_pinctl(codec, cfg->input_pins[i], AC_PINCTL_IN_EN);
715
716                         imux->items[imux->num_items].label = auto_pin_cfg_labels[i];
717
718                         for (j=0; j<spec->num_muxes; j++) {
719                                 int num_cons = snd_hda_get_connections(codec, spec->mux_nids[j], con_lst, HDA_MAX_NUM_INPUTS);
720                                 for (k=0; k<num_cons; k++)
721                                         if (con_lst[k] == cfg->input_pins[i]) {
722                                                 index = k;
723                                                 break;
724                                         }
725                                 if (index >= 0)
726                                         break;
727                         }
728                         imux->items[imux->num_items].index = index;
729                         imux->num_items++;
730                 }
731         }
732
733         return 0;
734 }
735
736 static void stac92xx_auto_init_multi_out(struct hda_codec *codec)
737 {
738         struct sigmatel_spec *spec = codec->spec;
739         int i;
740
741         for (i = 0; i < spec->autocfg.line_outs; i++) {
742                 hda_nid_t nid = spec->autocfg.line_out_pins[i];
743                 stac92xx_auto_set_pinctl(codec, nid, AC_PINCTL_OUT_EN);
744         }
745 }
746
747 static void stac92xx_auto_init_hp_out(struct hda_codec *codec)
748 {
749         struct sigmatel_spec *spec = codec->spec;
750         hda_nid_t pin;
751
752         pin = spec->autocfg.hp_pin;
753         if (pin) /* connect to front */
754                 stac92xx_auto_set_pinctl(codec, pin, AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
755 }
756
757 static int stac922x_parse_auto_config(struct hda_codec *codec)
758 {
759         struct sigmatel_spec *spec = codec->spec;
760         int err;
761
762         if ((err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL)) < 0)
763                 return err;
764         if ((err = stac92xx_add_dyn_out_pins(codec, &spec->autocfg)) < 0)
765                 return err;
766         if ((err = stac92xx_auto_fill_dac_nids(codec, &spec->autocfg)) < 0)
767                 return err;
768         if (! spec->autocfg.line_outs && ! spec->autocfg.hp_pin)
769                 return 0; /* can't find valid pin config */
770
771         if ((err = stac92xx_auto_create_multi_out_ctls(spec, &spec->autocfg)) < 0 ||
772             (err = stac92xx_auto_create_hp_ctls(codec, &spec->autocfg)) < 0 ||
773             (err = stac92xx_auto_create_analog_input_ctls(codec, &spec->autocfg)) < 0)
774                 return err;
775
776         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
777         if (spec->multiout.max_channels > 2)
778                 spec->surr_switch = 1;
779
780         if (spec->autocfg.dig_out_pin) {
781                 spec->multiout.dig_out_nid = 0x08;
782                 stac92xx_auto_set_pinctl(codec, spec->autocfg.dig_out_pin, AC_PINCTL_OUT_EN);
783         }
784         if (spec->autocfg.dig_in_pin) {
785                 spec->dig_in_nid = 0x09;
786                 stac92xx_auto_set_pinctl(codec, spec->autocfg.dig_in_pin, AC_PINCTL_IN_EN);
787         }
788
789         if (spec->kctl_alloc)
790                 spec->mixers[spec->num_mixers++] = spec->kctl_alloc;
791
792         spec->input_mux = &spec->private_imux;
793
794         return 1;
795 }
796
797 static int stac9200_parse_auto_config(struct hda_codec *codec)
798 {
799         struct sigmatel_spec *spec = codec->spec;
800         int err;
801
802         if ((err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL)) < 0)
803                 return err;
804
805         if ((err = stac92xx_auto_create_analog_input_ctls(codec, &spec->autocfg)) < 0)
806                 return err;
807
808         if (spec->autocfg.dig_out_pin) {
809                 spec->multiout.dig_out_nid = 0x05;
810                 stac92xx_auto_set_pinctl(codec, spec->autocfg.dig_out_pin, AC_PINCTL_OUT_EN);
811         }
812         if (spec->autocfg.dig_in_pin) {
813                 spec->dig_in_nid = 0x04;
814                 stac92xx_auto_set_pinctl(codec, spec->autocfg.dig_in_pin, AC_PINCTL_IN_EN);
815         }
816
817         if (spec->kctl_alloc)
818                 spec->mixers[spec->num_mixers++] = spec->kctl_alloc;
819
820         spec->input_mux = &spec->private_imux;
821
822         return 1;
823 }
824
825 static int stac92xx_init(struct hda_codec *codec)
826 {
827         struct sigmatel_spec *spec = codec->spec;
828
829         snd_hda_sequence_write(codec, spec->init);
830
831         stac92xx_auto_init_multi_out(codec);
832         stac92xx_auto_init_hp_out(codec);
833
834         return 0;
835 }
836
837 static void stac92xx_free(struct hda_codec *codec)
838 {
839         struct sigmatel_spec *spec = codec->spec;
840         int i;
841
842         if (! spec)
843                 return;
844
845         if (spec->kctl_alloc) {
846                 for (i = 0; i < spec->num_kctl_used; i++)
847                         kfree(spec->kctl_alloc[i].name);
848                 kfree(spec->kctl_alloc);
849         }
850
851         kfree(spec);
852 }
853
854 static void stac92xx_set_pinctl(struct hda_codec *codec, hda_nid_t nid,
855                                 unsigned int flag)
856 {
857         unsigned int pin_ctl = snd_hda_codec_read(codec, nid,
858                         0, AC_VERB_GET_PIN_WIDGET_CONTROL, 0x00);
859         snd_hda_codec_write(codec, nid, 0,
860                         AC_VERB_SET_PIN_WIDGET_CONTROL,
861                         pin_ctl | flag);
862 }
863
864 static void stac92xx_reset_pinctl(struct hda_codec *codec, hda_nid_t nid,
865                                   unsigned int flag)
866 {
867         unsigned int pin_ctl = snd_hda_codec_read(codec, nid,
868                         0, AC_VERB_GET_PIN_WIDGET_CONTROL, 0x00);
869         snd_hda_codec_write(codec, nid, 0,
870                         AC_VERB_SET_PIN_WIDGET_CONTROL,
871                         pin_ctl & ~flag);
872 }
873
874 static void stac92xx_unsol_event(struct hda_codec *codec, unsigned int res)
875 {
876         struct sigmatel_spec *spec = codec->spec;
877         struct auto_pin_cfg *cfg = &spec->autocfg;
878         int i, presence;
879
880         if ((res >> 26) != STAC_HP_EVENT)
881                 return;
882
883         presence = snd_hda_codec_read(codec, cfg->hp_pin, 0,
884                         AC_VERB_GET_PIN_SENSE, 0x00) >> 31;
885
886         if (presence) {
887                 /* disable lineouts, enable hp */
888                 for (i = 0; i < cfg->line_outs; i++)
889                         stac92xx_reset_pinctl(codec, cfg->line_out_pins[i],
890                                                 AC_PINCTL_OUT_EN);
891                 stac92xx_set_pinctl(codec, cfg->hp_pin, AC_PINCTL_OUT_EN);
892         } else {
893                 /* enable lineouts, disable hp */
894                 for (i = 0; i < cfg->line_outs; i++)
895                         stac92xx_set_pinctl(codec, cfg->line_out_pins[i],
896                                                 AC_PINCTL_OUT_EN);
897                 stac92xx_reset_pinctl(codec, cfg->hp_pin, AC_PINCTL_OUT_EN);
898         }
899
900
901 #ifdef CONFIG_PM
902 static int stac92xx_resume(struct hda_codec *codec)
903 {
904         struct sigmatel_spec *spec = codec->spec;
905         int i;
906
907         stac92xx_init(codec);
908         for (i = 0; i < spec->num_mixers; i++)
909                 snd_hda_resume_ctls(codec, spec->mixers[i]);
910         if (spec->multiout.dig_out_nid)
911                 snd_hda_resume_spdif_out(codec);
912         if (spec->dig_in_nid)
913                 snd_hda_resume_spdif_in(codec);
914
915         return 0;
916 }
917 #endif
918
919 static struct hda_codec_ops stac92xx_patch_ops = {
920         .build_controls = stac92xx_build_controls,
921         .build_pcms = stac92xx_build_pcms,
922         .init = stac92xx_init,
923         .free = stac92xx_free,
924         .unsol_event = stac92xx_unsol_event,
925 #ifdef CONFIG_PM
926         .resume = stac92xx_resume,
927 #endif
928 };
929
930 static int patch_stac9200(struct hda_codec *codec)
931 {
932         struct sigmatel_spec *spec;
933         int err;
934
935         spec  = kzalloc(sizeof(*spec), GFP_KERNEL);
936         if (spec == NULL)
937                 return -ENOMEM;
938
939         codec->spec = spec;
940         spec->board_config = snd_hda_check_board_config(codec, stac9200_cfg_tbl);
941         if (spec->board_config < 0)
942                 snd_printdd(KERN_INFO "hda_codec: Unknown model for STAC9200, using BIOS defaults\n");
943         else {
944                 spec->num_pins = 8;
945                 spec->pin_nids = stac9200_pin_nids;
946                 spec->pin_configs = stac9200_brd_tbl[spec->board_config];
947                 stac92xx_set_config_regs(codec);
948         }
949
950         spec->multiout.max_channels = 2;
951         spec->multiout.num_dacs = 1;
952         spec->multiout.dac_nids = stac9200_dac_nids;
953         spec->adc_nids = stac9200_adc_nids;
954         spec->mux_nids = stac9200_mux_nids;
955         spec->num_muxes = 1;
956
957         spec->init = stac9200_core_init;
958         spec->mixer = stac9200_mixer;
959
960         err = stac9200_parse_auto_config(codec);
961         if (err < 0) {
962                 stac92xx_free(codec);
963                 return err;
964         }
965
966         codec->patch_ops = stac92xx_patch_ops;
967
968         return 0;
969 }
970
971 static int patch_stac922x(struct hda_codec *codec)
972 {
973         struct sigmatel_spec *spec;
974         int err;
975
976         spec  = kzalloc(sizeof(*spec), GFP_KERNEL);
977         if (spec == NULL)
978                 return -ENOMEM;
979
980         codec->spec = spec;
981         spec->board_config = snd_hda_check_board_config(codec, stac922x_cfg_tbl);
982         if (spec->board_config < 0)
983                 snd_printdd(KERN_INFO "hda_codec: Unknown model for STAC922x, using BIOS defaults\n");
984         else {
985                 spec->num_pins = 10;
986                 spec->pin_nids = stac922x_pin_nids;
987                 spec->pin_configs = stac922x_brd_tbl[spec->board_config];
988                 stac92xx_set_config_regs(codec);
989         }
990
991         spec->adc_nids = stac922x_adc_nids;
992         spec->mux_nids = stac922x_mux_nids;
993         spec->num_muxes = 2;
994
995         spec->init = stac922x_core_init;
996         spec->mixer = stac922x_mixer;
997
998         spec->multiout.dac_nids = spec->dac_nids;
999
1000         err = stac922x_parse_auto_config(codec);
1001         if (err < 0) {
1002                 stac92xx_free(codec);
1003                 return err;
1004         }
1005
1006         codec->patch_ops = stac92xx_patch_ops;
1007
1008         return 0;
1009 }
1010
1011 /*
1012  * patch entries
1013  */
1014 struct hda_codec_preset snd_hda_preset_sigmatel[] = {
1015         { .id = 0x83847690, .name = "STAC9200", .patch = patch_stac9200 },
1016         { .id = 0x83847882, .name = "STAC9220 A1", .patch = patch_stac922x },
1017         { .id = 0x83847680, .name = "STAC9221 A1", .patch = patch_stac922x },
1018         { .id = 0x83847880, .name = "STAC9220 A2", .patch = patch_stac922x },
1019         { .id = 0x83847681, .name = "STAC9220D/9223D A2", .patch = patch_stac922x },
1020         { .id = 0x83847682, .name = "STAC9221 A2", .patch = patch_stac922x },
1021         { .id = 0x83847683, .name = "STAC9221D A2", .patch = patch_stac922x },
1022         {} /* terminator */
1023 };