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