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