[ALSA] hda-code - Clean up STAC GPIO enablement code
[powerpc.git] / sound / pci / hda / patch_conexant.c
1 /*
2  * HD audio interface patch for Conexant HDA audio codec
3  *
4  * Copyright (c) 2006 Pototskiy Akex <alex.pototskiy@gmail.com>
5  *                    Takashi Iwai <tiwai@suse.de>
6  *                    Tobin Davis  <tdavis@dsl-only.net>
7  *
8  *  This driver is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This driver is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21  */
22
23 #include <linux/init.h>
24 #include <linux/delay.h>
25 #include <linux/slab.h>
26 #include <linux/pci.h>
27 #include <sound/core.h>
28 #include "hda_codec.h"
29 #include "hda_local.h"
30
31 #define CXT_PIN_DIR_IN              0x00
32 #define CXT_PIN_DIR_OUT             0x01
33 #define CXT_PIN_DIR_INOUT           0x02
34 #define CXT_PIN_DIR_IN_NOMICBIAS    0x03
35 #define CXT_PIN_DIR_INOUT_NOMICBIAS 0x04
36
37 #define CONEXANT_HP_EVENT       0x37
38 #define CONEXANT_MIC_EVENT      0x38
39
40
41
42 struct conexant_spec {
43
44         struct snd_kcontrol_new *mixers[5];
45         int num_mixers;
46
47         const struct hda_verb *init_verbs[5];   /* initialization verbs
48                                                  * don't forget NULL
49                                                  * termination!
50                                                  */
51         unsigned int num_init_verbs;
52
53         /* playback */
54         struct hda_multi_out multiout;  /* playback set-up
55                                          * max_channels, dacs must be set
56                                          * dig_out_nid and hp_nid are optional
57                                          */
58         unsigned int cur_eapd;
59         unsigned int hp_present;
60         unsigned int need_dac_fix;
61
62         /* capture */
63         unsigned int num_adc_nids;
64         hda_nid_t *adc_nids;
65         hda_nid_t dig_in_nid;           /* digital-in NID; optional */
66
67         /* capture source */
68         const struct hda_input_mux *input_mux;
69         hda_nid_t *capsrc_nids;
70         unsigned int cur_mux[3];
71
72         /* channel model */
73         const struct hda_channel_mode *channel_mode;
74         int num_channel_mode;
75
76         /* PCM information */
77         struct hda_pcm pcm_rec[2];      /* used in build_pcms() */
78
79         struct mutex amp_mutex; /* PCM volume/mute control mutex */
80         unsigned int spdif_route;
81
82         /* dynamic controls, init_verbs and input_mux */
83         struct auto_pin_cfg autocfg;
84         unsigned int num_kctl_alloc, num_kctl_used;
85         struct snd_kcontrol_new *kctl_alloc;
86         struct hda_input_mux private_imux;
87         hda_nid_t private_dac_nids[AUTO_CFG_MAX_OUTS];
88
89 };
90
91 static int conexant_playback_pcm_open(struct hda_pcm_stream *hinfo,
92                                       struct hda_codec *codec,
93                                       struct snd_pcm_substream *substream)
94 {
95         struct conexant_spec *spec = codec->spec;
96         return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream);
97 }
98
99 static int conexant_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
100                                          struct hda_codec *codec,
101                                          unsigned int stream_tag,
102                                          unsigned int format,
103                                          struct snd_pcm_substream *substream)
104 {
105         struct conexant_spec *spec = codec->spec;
106         return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
107                                                 stream_tag,
108                                                 format, substream);
109 }
110
111 static int conexant_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
112                                          struct hda_codec *codec,
113                                          struct snd_pcm_substream *substream)
114 {
115         struct conexant_spec *spec = codec->spec;
116         return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
117 }
118
119 /*
120  * Digital out
121  */
122 static int conexant_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
123                                           struct hda_codec *codec,
124                                           struct snd_pcm_substream *substream)
125 {
126         struct conexant_spec *spec = codec->spec;
127         return snd_hda_multi_out_dig_open(codec, &spec->multiout);
128 }
129
130 static int conexant_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
131                                          struct hda_codec *codec,
132                                          struct snd_pcm_substream *substream)
133 {
134         struct conexant_spec *spec = codec->spec;
135         return snd_hda_multi_out_dig_close(codec, &spec->multiout);
136 }
137
138 static int conexant_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
139                                          struct hda_codec *codec,
140                                          unsigned int stream_tag,
141                                          unsigned int format,
142                                          struct snd_pcm_substream *substream)
143 {
144         struct conexant_spec *spec = codec->spec;
145         return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
146                                              stream_tag,
147                                              format, substream);
148 }
149
150 /*
151  * Analog capture
152  */
153 static int conexant_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
154                                       struct hda_codec *codec,
155                                       unsigned int stream_tag,
156                                       unsigned int format,
157                                       struct snd_pcm_substream *substream)
158 {
159         struct conexant_spec *spec = codec->spec;
160         snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
161                                    stream_tag, 0, format);
162         return 0;
163 }
164
165 static int conexant_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
166                                       struct hda_codec *codec,
167                                       struct snd_pcm_substream *substream)
168 {
169         struct conexant_spec *spec = codec->spec;
170         snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
171                                    0, 0, 0);
172         return 0;
173 }
174
175
176
177 static struct hda_pcm_stream conexant_pcm_analog_playback = {
178         .substreams = 1,
179         .channels_min = 2,
180         .channels_max = 2,
181         .nid = 0, /* fill later */
182         .ops = {
183                 .open = conexant_playback_pcm_open,
184                 .prepare = conexant_playback_pcm_prepare,
185                 .cleanup = conexant_playback_pcm_cleanup
186         },
187 };
188
189 static struct hda_pcm_stream conexant_pcm_analog_capture = {
190         .substreams = 1,
191         .channels_min = 2,
192         .channels_max = 2,
193         .nid = 0, /* fill later */
194         .ops = {
195                 .prepare = conexant_capture_pcm_prepare,
196                 .cleanup = conexant_capture_pcm_cleanup
197         },
198 };
199
200
201 static struct hda_pcm_stream conexant_pcm_digital_playback = {
202         .substreams = 1,
203         .channels_min = 2,
204         .channels_max = 2,
205         .nid = 0, /* fill later */
206         .ops = {
207                 .open = conexant_dig_playback_pcm_open,
208                 .close = conexant_dig_playback_pcm_close,
209                 .prepare = conexant_dig_playback_pcm_prepare
210         },
211 };
212
213 static struct hda_pcm_stream conexant_pcm_digital_capture = {
214         .substreams = 1,
215         .channels_min = 2,
216         .channels_max = 2,
217         /* NID is set in alc_build_pcms */
218 };
219
220 static int conexant_build_pcms(struct hda_codec *codec)
221 {
222         struct conexant_spec *spec = codec->spec;
223         struct hda_pcm *info = spec->pcm_rec;
224
225         codec->num_pcms = 1;
226         codec->pcm_info = info;
227
228         info->name = "CONEXANT Analog";
229         info->stream[SNDRV_PCM_STREAM_PLAYBACK] = conexant_pcm_analog_playback;
230         info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
231                 spec->multiout.max_channels;
232         info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
233                 spec->multiout.dac_nids[0];
234         info->stream[SNDRV_PCM_STREAM_CAPTURE] = conexant_pcm_analog_capture;
235         info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = spec->num_adc_nids;
236         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
237
238         if (spec->multiout.dig_out_nid) {
239                 info++;
240                 codec->num_pcms++;
241                 info->name = "Conexant Digital";
242                 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
243                         conexant_pcm_digital_playback;
244                 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
245                         spec->multiout.dig_out_nid;
246                 if (spec->dig_in_nid) {
247                         info->stream[SNDRV_PCM_STREAM_CAPTURE] =
248                                 conexant_pcm_digital_capture;
249                         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
250                                 spec->dig_in_nid;
251                 }
252         }
253
254         return 0;
255 }
256
257 static int conexant_mux_enum_info(struct snd_kcontrol *kcontrol,
258                                   struct snd_ctl_elem_info *uinfo)
259 {
260         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
261         struct conexant_spec *spec = codec->spec;
262
263         return snd_hda_input_mux_info(spec->input_mux, uinfo);
264 }
265
266 static int conexant_mux_enum_get(struct snd_kcontrol *kcontrol,
267                                  struct snd_ctl_elem_value *ucontrol)
268 {
269         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
270         struct conexant_spec *spec = codec->spec;
271         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
272
273         ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
274         return 0;
275 }
276
277 static int conexant_mux_enum_put(struct snd_kcontrol *kcontrol,
278                                  struct snd_ctl_elem_value *ucontrol)
279 {
280         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
281         struct conexant_spec *spec = codec->spec;
282         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
283
284         return snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
285                                      spec->capsrc_nids[adc_idx],
286                                      &spec->cur_mux[adc_idx]);
287 }
288
289 static int conexant_init(struct hda_codec *codec)
290 {
291         struct conexant_spec *spec = codec->spec;
292         int i;
293
294         for (i = 0; i < spec->num_init_verbs; i++)
295                 snd_hda_sequence_write(codec, spec->init_verbs[i]);
296         return 0;
297 }
298
299 static void conexant_free(struct hda_codec *codec)
300 {
301         struct conexant_spec *spec = codec->spec;
302         unsigned int i;
303
304         if (spec->kctl_alloc) {
305                 for (i = 0; i < spec->num_kctl_used; i++)
306                         kfree(spec->kctl_alloc[i].name);
307                 kfree(spec->kctl_alloc);
308         }
309
310         kfree(codec->spec);
311 }
312
313 static int conexant_build_controls(struct hda_codec *codec)
314 {
315         struct conexant_spec *spec = codec->spec;
316         unsigned int i;
317         int err;
318
319         for (i = 0; i < spec->num_mixers; i++) {
320                 err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
321                 if (err < 0)
322                         return err;
323         }
324         if (spec->multiout.dig_out_nid) {
325                 err = snd_hda_create_spdif_out_ctls(codec,
326                                                     spec->multiout.dig_out_nid);
327                 if (err < 0)
328                         return err;
329         } 
330         if (spec->dig_in_nid) {
331                 err = snd_hda_create_spdif_in_ctls(codec,spec->dig_in_nid);
332                 if (err < 0)
333                         return err;
334         }
335         return 0;
336 }
337
338 static struct hda_codec_ops conexant_patch_ops = {
339         .build_controls = conexant_build_controls,
340         .build_pcms = conexant_build_pcms,
341         .init = conexant_init,
342         .free = conexant_free,
343 };
344
345 /*
346  * EAPD control
347  * the private value = nid | (invert << 8)
348  */
349
350 #define cxt_eapd_info           snd_ctl_boolean_mono_info
351
352 static int cxt_eapd_get(struct snd_kcontrol *kcontrol,
353                              struct snd_ctl_elem_value *ucontrol)
354 {
355         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
356         struct conexant_spec *spec = codec->spec;
357         int invert = (kcontrol->private_value >> 8) & 1;
358         if (invert)
359                 ucontrol->value.integer.value[0] = !spec->cur_eapd;
360         else
361                 ucontrol->value.integer.value[0] = spec->cur_eapd;
362         return 0;
363
364 }
365
366 static int cxt_eapd_put(struct snd_kcontrol *kcontrol,
367                              struct snd_ctl_elem_value *ucontrol)
368 {
369         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
370         struct conexant_spec *spec = codec->spec;
371         int invert = (kcontrol->private_value >> 8) & 1;
372         hda_nid_t nid = kcontrol->private_value & 0xff;
373         unsigned int eapd;
374
375         eapd = !!ucontrol->value.integer.value[0];
376         if (invert)
377                 eapd = !eapd;
378         if (eapd == spec->cur_eapd)
379                 return 0;
380         
381         spec->cur_eapd = eapd;
382         snd_hda_codec_write_cache(codec, nid,
383                                   0, AC_VERB_SET_EAPD_BTLENABLE,
384                                   eapd ? 0x02 : 0x00);
385         return 1;
386 }
387
388 /* controls for test mode */
389 #ifdef CONFIG_SND_DEBUG
390
391 #define CXT_EAPD_SWITCH(xname, nid, mask) \
392         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = 0,  \
393           .info = cxt_eapd_info, \
394           .get = cxt_eapd_get, \
395           .put = cxt_eapd_put, \
396           .private_value = nid | (mask<<16) }
397
398
399
400 static int conexant_ch_mode_info(struct snd_kcontrol *kcontrol,
401                                  struct snd_ctl_elem_info *uinfo)
402 {
403         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
404         struct conexant_spec *spec = codec->spec;
405         return snd_hda_ch_mode_info(codec, uinfo, spec->channel_mode,
406                                     spec->num_channel_mode);
407 }
408
409 static int conexant_ch_mode_get(struct snd_kcontrol *kcontrol,
410                                 struct snd_ctl_elem_value *ucontrol)
411 {
412         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
413         struct conexant_spec *spec = codec->spec;
414         return snd_hda_ch_mode_get(codec, ucontrol, spec->channel_mode,
415                                    spec->num_channel_mode,
416                                    spec->multiout.max_channels);
417 }
418
419 static int conexant_ch_mode_put(struct snd_kcontrol *kcontrol,
420                                 struct snd_ctl_elem_value *ucontrol)
421 {
422         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
423         struct conexant_spec *spec = codec->spec;
424         int err = snd_hda_ch_mode_put(codec, ucontrol, spec->channel_mode,
425                                       spec->num_channel_mode,
426                                       &spec->multiout.max_channels);
427         if (err >= 0 && spec->need_dac_fix)
428                 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
429         return err;
430 }
431
432 #define CXT_PIN_MODE(xname, nid, dir) \
433         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = 0,  \
434           .info = conexant_ch_mode_info, \
435           .get = conexant_ch_mode_get, \
436           .put = conexant_ch_mode_put, \
437           .private_value = nid | (dir<<16) }
438
439 #endif /* CONFIG_SND_DEBUG */
440
441 /* Conexant 5045 specific */
442
443 static hda_nid_t cxt5045_dac_nids[1] = { 0x19 };
444 static hda_nid_t cxt5045_adc_nids[1] = { 0x1a };
445 static hda_nid_t cxt5045_capsrc_nids[1] = { 0x1a };
446 #define CXT5045_SPDIF_OUT       0x13
447
448 static struct hda_channel_mode cxt5045_modes[1] = {
449         { 2, NULL },
450 };
451
452 static struct hda_input_mux cxt5045_capture_source = {
453         .num_items = 2,
454         .items = {
455                 { "IntMic", 0x1 },
456                 { "LineIn", 0x2 },
457         }
458 };
459
460 /* turn on/off EAPD (+ mute HP) as a master switch */
461 static int cxt5045_hp_master_sw_put(struct snd_kcontrol *kcontrol,
462                                     struct snd_ctl_elem_value *ucontrol)
463 {
464         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
465         struct conexant_spec *spec = codec->spec;
466         unsigned int bits;
467
468         if (!cxt_eapd_put(kcontrol, ucontrol))
469                 return 0;
470
471         /* toggle internal speakers mute depending of presence of
472          * the headphone jack
473          */
474         bits = (!spec->hp_present && spec->cur_eapd) ? 0 : HDA_AMP_MUTE;
475         snd_hda_codec_amp_stereo(codec, 0x10, HDA_OUTPUT, 0,
476                                  HDA_AMP_MUTE, bits);
477
478         bits = spec->cur_eapd ? 0 : HDA_AMP_MUTE;
479         snd_hda_codec_amp_stereo(codec, 0x11, HDA_OUTPUT, 0,
480                                  HDA_AMP_MUTE, bits);
481         return 1;
482 }
483
484 /* bind volumes of both NID 0x10 and 0x11 */
485 static struct hda_bind_ctls cxt5045_hp_bind_master_vol = {
486         .ops = &snd_hda_bind_vol,
487         .values = {
488                 HDA_COMPOSE_AMP_VAL(0x10, 3, 0, HDA_OUTPUT),
489                 HDA_COMPOSE_AMP_VAL(0x11, 3, 0, HDA_OUTPUT),
490                 0
491         },
492 };
493
494 /* toggle input of built-in and mic jack appropriately */
495 static void cxt5045_hp_automic(struct hda_codec *codec)
496 {
497         static struct hda_verb mic_jack_on[] = {
498                 {0x14, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
499                 {0x12, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
500                 {}
501         };
502         static struct hda_verb mic_jack_off[] = {
503                 {0x12, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
504                 {0x14, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
505                 {}
506         };
507         unsigned int present;
508
509         present = snd_hda_codec_read(codec, 0x12, 0,
510                                      AC_VERB_GET_PIN_SENSE, 0) & 0x80000000;
511         if (present)
512                 snd_hda_sequence_write(codec, mic_jack_on);
513         else
514                 snd_hda_sequence_write(codec, mic_jack_off);
515 }
516
517
518 /* mute internal speaker if HP is plugged */
519 static void cxt5045_hp_automute(struct hda_codec *codec)
520 {
521         struct conexant_spec *spec = codec->spec;
522         unsigned int bits;
523
524         spec->hp_present = snd_hda_codec_read(codec, 0x11, 0,
525                                      AC_VERB_GET_PIN_SENSE, 0) & 0x80000000;
526
527         bits = (spec->hp_present || !spec->cur_eapd) ? HDA_AMP_MUTE : 0; 
528         snd_hda_codec_amp_stereo(codec, 0x10, HDA_OUTPUT, 0,
529                                  HDA_AMP_MUTE, bits);
530 }
531
532 /* unsolicited event for HP jack sensing */
533 static void cxt5045_hp_unsol_event(struct hda_codec *codec,
534                                    unsigned int res)
535 {
536         res >>= 26;
537         switch (res) {
538         case CONEXANT_HP_EVENT:
539                 cxt5045_hp_automute(codec);
540                 break;
541         case CONEXANT_MIC_EVENT:
542                 cxt5045_hp_automic(codec);
543                 break;
544
545         }
546 }
547
548 static struct snd_kcontrol_new cxt5045_mixers[] = {
549         {
550                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
551                 .name = "Capture Source",
552                 .info = conexant_mux_enum_info,
553                 .get = conexant_mux_enum_get,
554                 .put = conexant_mux_enum_put
555         },
556         HDA_CODEC_VOLUME("Int Mic Capture Volume", 0x1a, 0x01, HDA_INPUT),
557         HDA_CODEC_MUTE("Int Mic Capture Switch", 0x1a, 0x01, HDA_INPUT),
558         HDA_CODEC_VOLUME("Ext Mic Capture Volume", 0x1a, 0x02, HDA_INPUT),
559         HDA_CODEC_MUTE("Ext Mic Capture Switch", 0x1a, 0x02, HDA_INPUT),
560         HDA_CODEC_VOLUME("PCM Playback Volume", 0x17, 0x0, HDA_INPUT),
561         HDA_CODEC_MUTE("PCM Playback Switch", 0x17, 0x0, HDA_INPUT),
562         HDA_CODEC_VOLUME("Int Mic Playback Volume", 0x17, 0x1, HDA_INPUT),
563         HDA_CODEC_MUTE("Int Mic Playback Switch", 0x17, 0x1, HDA_INPUT),
564         HDA_CODEC_VOLUME("Ext Mic Playback Volume", 0x17, 0x2, HDA_INPUT),
565         HDA_CODEC_MUTE("Ext Mic Playback Switch", 0x17, 0x2, HDA_INPUT),
566         HDA_BIND_VOL("Master Playback Volume", &cxt5045_hp_bind_master_vol),
567         {
568                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
569                 .name = "Master Playback Switch",
570                 .info = cxt_eapd_info,
571                 .get = cxt_eapd_get,
572                 .put = cxt5045_hp_master_sw_put,
573                 .private_value = 0x10,
574         },
575
576         {}
577 };
578
579 static struct hda_verb cxt5045_init_verbs[] = {
580         /* Line in, Mic */
581         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
582         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN|AC_PINCTL_VREF_80 },
583         /* HP, Amp  */
584         {0x10, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
585         {0x10, AC_VERB_SET_CONNECT_SEL, 0x1},
586         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
587         {0x11, AC_VERB_SET_CONNECT_SEL, 0x1},
588         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
589         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
590         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
591         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
592         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
593         /* Record selector: Int mic */
594         {0x1a, AC_VERB_SET_CONNECT_SEL,0x1},
595         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE,
596          AC_AMP_SET_INPUT|AC_AMP_SET_RIGHT|AC_AMP_SET_LEFT|0x17},
597         /* SPDIF route: PCM */
598         { 0x13, AC_VERB_SET_CONNECT_SEL, 0x0 },
599         /* EAPD */
600         {0x10, AC_VERB_SET_EAPD_BTLENABLE, 0x2 }, /* default on */ 
601         { } /* end */
602 };
603
604
605 static struct hda_verb cxt5045_hp_sense_init_verbs[] = {
606         /* pin sensing on HP jack */
607         {0x11, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_HP_EVENT},
608         { } /* end */
609 };
610
611 static struct hda_verb cxt5045_mic_sense_init_verbs[] = {
612         /* pin sensing on HP jack */
613         {0x12, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_MIC_EVENT},
614         { } /* end */
615 };
616
617 #ifdef CONFIG_SND_DEBUG
618 /* Test configuration for debugging, modelled after the ALC260 test
619  * configuration.
620  */
621 static struct hda_input_mux cxt5045_test_capture_source = {
622         .num_items = 5,
623         .items = {
624                 { "MIXER", 0x0 },
625                 { "MIC1 pin", 0x1 },
626                 { "LINE1 pin", 0x2 },
627                 { "HP-OUT pin", 0x3 },
628                 { "CD pin", 0x4 },
629         },
630 };
631
632 static struct snd_kcontrol_new cxt5045_test_mixer[] = {
633
634         /* Output controls */
635         HDA_CODEC_VOLUME("Speaker Playback Volume", 0x10, 0x0, HDA_OUTPUT),
636         HDA_CODEC_MUTE("Speaker Playback Switch", 0x10, 0x0, HDA_OUTPUT),
637         HDA_CODEC_VOLUME("Node 11 Playback Volume", 0x11, 0x0, HDA_OUTPUT),
638         HDA_CODEC_MUTE("Node 11 Playback Switch", 0x11, 0x0, HDA_OUTPUT),
639         HDA_CODEC_VOLUME("Node 12 Playback Volume", 0x12, 0x0, HDA_OUTPUT),
640         HDA_CODEC_MUTE("Node 12 Playback Switch", 0x12, 0x0, HDA_OUTPUT),
641         
642         /* Modes for retasking pin widgets */
643         CXT_PIN_MODE("HP-OUT pin mode", 0x11, CXT_PIN_DIR_INOUT),
644         CXT_PIN_MODE("LINE1 pin mode", 0x12, CXT_PIN_DIR_INOUT),
645
646         /* EAPD Switch Control */
647         CXT_EAPD_SWITCH("External Amplifier", 0x10, 0x0),
648
649         /* Loopback mixer controls */
650
651         HDA_CODEC_VOLUME("Mixer-1 Volume", 0x17, 0x0, HDA_INPUT),
652         HDA_CODEC_MUTE("Mixer-1 Switch", 0x17, 0x0, HDA_INPUT),
653         HDA_CODEC_VOLUME("Mixer-2 Volume", 0x17, 0x1, HDA_INPUT),
654         HDA_CODEC_MUTE("Mixer-2 Switch", 0x17, 0x1, HDA_INPUT),
655         HDA_CODEC_VOLUME("Mixer-3 Volume", 0x17, 0x2, HDA_INPUT),
656         HDA_CODEC_MUTE("Mixer-3 Switch", 0x17, 0x2, HDA_INPUT),
657         HDA_CODEC_VOLUME("Mixer-4 Volume", 0x17, 0x3, HDA_INPUT),
658         HDA_CODEC_MUTE("Mixer-4 Switch", 0x17, 0x3, HDA_INPUT),
659         HDA_CODEC_VOLUME("Mixer-5 Volume", 0x17, 0x4, HDA_INPUT),
660         HDA_CODEC_MUTE("Mixer-5 Switch", 0x17, 0x4, HDA_INPUT),
661         {
662                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
663                 .name = "Input Source",
664                 .info = conexant_mux_enum_info,
665                 .get = conexant_mux_enum_get,
666                 .put = conexant_mux_enum_put,
667         },
668         /* Audio input controls */
669         HDA_CODEC_VOLUME("Input-1 Volume", 0x1a, 0x0, HDA_INPUT),
670         HDA_CODEC_MUTE("Input-1 Switch", 0x1a, 0x0, HDA_INPUT),
671         HDA_CODEC_VOLUME("Input-2 Volume", 0x1a, 0x1, HDA_INPUT),
672         HDA_CODEC_MUTE("Input-2 Switch", 0x1a, 0x1, HDA_INPUT),
673         HDA_CODEC_VOLUME("Input-3 Volume", 0x1a, 0x2, HDA_INPUT),
674         HDA_CODEC_MUTE("Input-3 Switch", 0x1a, 0x2, HDA_INPUT),
675         HDA_CODEC_VOLUME("Input-4 Volume", 0x1a, 0x3, HDA_INPUT),
676         HDA_CODEC_MUTE("Input-4 Switch", 0x1a, 0x3, HDA_INPUT),
677         HDA_CODEC_VOLUME("Input-5 Volume", 0x1a, 0x4, HDA_INPUT),
678         HDA_CODEC_MUTE("Input-5 Switch", 0x1a, 0x4, HDA_INPUT),
679         { } /* end */
680 };
681
682 static struct hda_verb cxt5045_test_init_verbs[] = {
683         /* Set connections */
684         { 0x10, AC_VERB_SET_CONNECT_SEL, 0x0 },
685         { 0x11, AC_VERB_SET_CONNECT_SEL, 0x0 },
686         { 0x12, AC_VERB_SET_CONNECT_SEL, 0x0 },
687         /* Enable retasking pins as output, initially without power amp */
688         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
689         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
690
691         /* Disable digital (SPDIF) pins initially, but users can enable
692          * them via a mixer switch.  In the case of SPDIF-out, this initverb
693          * payload also sets the generation to 0, output to be in "consumer"
694          * PCM format, copyright asserted, no pre-emphasis and no validity
695          * control.
696          */
697         {0x13, AC_VERB_SET_DIGI_CONVERT_1, 0},
698
699         /* Start with output sum widgets muted and their output gains at min */
700         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
701         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
702
703         /* Unmute retasking pin widget output buffers since the default
704          * state appears to be output.  As the pin mode is changed by the
705          * user the pin mode control will take care of enabling the pin's
706          * input/output buffers as needed.
707          */
708         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
709         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
710
711         /* Mute capture amp left and right */
712         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
713
714         /* Set ADC connection select to match default mixer setting (mic1
715          * pin)
716          */
717         {0x1a, AC_VERB_SET_CONNECT_SEL, 0x00},
718         {0x17, AC_VERB_SET_CONNECT_SEL, 0x00},
719
720         /* Mute all inputs to mixer widget (even unconnected ones) */
721         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)}, /* Mixer pin */
722         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)}, /* Mic1 pin */
723         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)}, /* Line pin */
724         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)}, /* HP pin */
725         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)}, /* CD pin */
726
727         { }
728 };
729 #endif
730
731
732 /* initialize jack-sensing, too */
733 static int cxt5045_init(struct hda_codec *codec)
734 {
735         conexant_init(codec);
736         cxt5045_hp_automute(codec);
737         return 0;
738 }
739
740
741 enum {
742         CXT5045_LAPTOP,  /* Laptops w/ EAPD support */
743         CXT5045_FUJITSU, /* Laptops w/ EAPD support */ 
744 #ifdef CONFIG_SND_DEBUG
745         CXT5045_TEST,
746 #endif
747         CXT5045_MODELS
748 };
749
750 static const char *cxt5045_models[CXT5045_MODELS] = {
751         [CXT5045_LAPTOP]        = "laptop",
752         [CXT5045_FUJITSU]       = "fujitsu",
753 #ifdef CONFIG_SND_DEBUG
754         [CXT5045_TEST]          = "test",
755 #endif
756 };
757
758 static struct snd_pci_quirk cxt5045_cfg_tbl[] = {
759         SND_PCI_QUIRK(0x103c, 0x30b2, "HP DV Series", CXT5045_LAPTOP),
760         SND_PCI_QUIRK(0x103c, 0x30b5, "HP DV2120", CXT5045_LAPTOP),
761         SND_PCI_QUIRK(0x103c, 0x30b7, "HP DV6000Z", CXT5045_LAPTOP),
762         SND_PCI_QUIRK(0x103c, 0x30bb, "HP DV8000", CXT5045_LAPTOP),
763         SND_PCI_QUIRK(0x103c, 0x30cd, "HP DV Series", CXT5045_LAPTOP),
764         SND_PCI_QUIRK(0x103c, 0x30d5, "HP 530", CXT5045_LAPTOP),
765         SND_PCI_QUIRK(0x103c, 0x30d9, "HP Spartan", CXT5045_LAPTOP),
766         SND_PCI_QUIRK(0x1734, 0x10ad, "Fujitsu Si1520", CXT5045_FUJITSU),
767         SND_PCI_QUIRK(0x1734, 0x10cb, "Fujitsu Si3515", CXT5045_LAPTOP),
768         SND_PCI_QUIRK(0x1734, 0x110e, "Fujitsu V5505", CXT5045_LAPTOP),
769         SND_PCI_QUIRK(0x8086, 0x2111, "Conexant Reference board", CXT5045_LAPTOP),
770         {}
771 };
772
773 static int patch_cxt5045(struct hda_codec *codec)
774 {
775         struct conexant_spec *spec;
776         int board_config;
777
778         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
779         if (!spec)
780                 return -ENOMEM;
781         mutex_init(&spec->amp_mutex);
782         codec->spec = spec;
783
784         spec->multiout.max_channels = 2;
785         spec->multiout.num_dacs = ARRAY_SIZE(cxt5045_dac_nids);
786         spec->multiout.dac_nids = cxt5045_dac_nids;
787         spec->multiout.dig_out_nid = CXT5045_SPDIF_OUT;
788         spec->num_adc_nids = 1;
789         spec->adc_nids = cxt5045_adc_nids;
790         spec->capsrc_nids = cxt5045_capsrc_nids;
791         spec->input_mux = &cxt5045_capture_source;
792         spec->num_mixers = 1;
793         spec->mixers[0] = cxt5045_mixers;
794         spec->num_init_verbs = 1;
795         spec->init_verbs[0] = cxt5045_init_verbs;
796         spec->spdif_route = 0;
797         spec->num_channel_mode = ARRAY_SIZE(cxt5045_modes),
798         spec->channel_mode = cxt5045_modes,
799
800
801         codec->patch_ops = conexant_patch_ops;
802
803         board_config = snd_hda_check_board_config(codec, CXT5045_MODELS,
804                                                   cxt5045_models,
805                                                   cxt5045_cfg_tbl);
806         switch (board_config) {
807         case CXT5045_LAPTOP:
808                 codec->patch_ops.unsol_event = cxt5045_hp_unsol_event;
809                 spec->input_mux = &cxt5045_capture_source;
810                 spec->num_init_verbs = 2;
811                 spec->init_verbs[1] = cxt5045_hp_sense_init_verbs;
812                 spec->mixers[0] = cxt5045_mixers;
813                 codec->patch_ops.init = cxt5045_init;
814                 break;
815         case CXT5045_FUJITSU:
816                 spec->input_mux = &cxt5045_capture_source;
817                 spec->num_init_verbs = 2;
818                 spec->init_verbs[1] = cxt5045_mic_sense_init_verbs;
819                 spec->mixers[0] = cxt5045_mixers;
820                 codec->patch_ops.init = cxt5045_init;
821                 break;
822 #ifdef CONFIG_SND_DEBUG
823         case CXT5045_TEST:
824                 spec->input_mux = &cxt5045_test_capture_source;
825                 spec->mixers[0] = cxt5045_test_mixer;
826                 spec->init_verbs[0] = cxt5045_test_init_verbs;
827 #endif  
828         }
829
830         /*
831          * Fix max PCM level to 0 dB
832          * (originall it has 0x2b steps with 0dB offset 0x14)
833          */
834         snd_hda_override_amp_caps(codec, 0x17, HDA_INPUT,
835                                   (0x14 << AC_AMPCAP_OFFSET_SHIFT) |
836                                   (0x14 << AC_AMPCAP_NUM_STEPS_SHIFT) |
837                                   (0x05 << AC_AMPCAP_STEP_SIZE_SHIFT) |
838                                   (1 << AC_AMPCAP_MUTE_SHIFT));
839
840         return 0;
841 }
842
843
844 /* Conexant 5047 specific */
845 #define CXT5047_SPDIF_OUT       0x11
846
847 static hda_nid_t cxt5047_dac_nids[2] = { 0x10, 0x1c };
848 static hda_nid_t cxt5047_adc_nids[1] = { 0x12 };
849 static hda_nid_t cxt5047_capsrc_nids[1] = { 0x1a };
850
851 static struct hda_channel_mode cxt5047_modes[1] = {
852         { 2, NULL },
853 };
854
855 static struct hda_input_mux cxt5047_capture_source = {
856         .num_items = 1,
857         .items = {
858                 { "Mic", 0x2 },
859         }
860 };
861
862 static struct hda_input_mux cxt5047_hp_capture_source = {
863         .num_items = 1,
864         .items = {
865                 { "ExtMic", 0x2 },
866         }
867 };
868
869 static struct hda_input_mux cxt5047_toshiba_capture_source = {
870         .num_items = 2,
871         .items = {
872                 { "ExtMic", 0x2 },
873                 { "Line-In", 0x1 },
874         }
875 };
876
877 /* turn on/off EAPD (+ mute HP) as a master switch */
878 static int cxt5047_hp_master_sw_put(struct snd_kcontrol *kcontrol,
879                                     struct snd_ctl_elem_value *ucontrol)
880 {
881         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
882         struct conexant_spec *spec = codec->spec;
883         unsigned int bits;
884
885         if (!cxt_eapd_put(kcontrol, ucontrol))
886                 return 0;
887
888         /* toggle internal speakers mute depending of presence of
889          * the headphone jack
890          */
891         bits = (!spec->hp_present && spec->cur_eapd) ? 0 : HDA_AMP_MUTE;
892         snd_hda_codec_amp_stereo(codec, 0x1d, HDA_OUTPUT, 0,
893                                  HDA_AMP_MUTE, bits);
894         bits = spec->cur_eapd ? 0 : HDA_AMP_MUTE;
895         snd_hda_codec_amp_stereo(codec, 0x13, HDA_OUTPUT, 0,
896                                  HDA_AMP_MUTE, bits);
897         return 1;
898 }
899
900 /* bind volumes of both NID 0x13 (Headphones) and 0x1d (Speakers) */
901 static struct hda_bind_ctls cxt5047_bind_master_vol = {
902         .ops = &snd_hda_bind_vol,
903         .values = {
904                 HDA_COMPOSE_AMP_VAL(0x13, 3, 0, HDA_OUTPUT),
905                 HDA_COMPOSE_AMP_VAL(0x1d, 3, 0, HDA_OUTPUT),
906                 0
907         },
908 };
909
910 /* mute internal speaker if HP is plugged */
911 static void cxt5047_hp_automute(struct hda_codec *codec)
912 {
913         struct conexant_spec *spec = codec->spec;
914         unsigned int bits;
915
916         spec->hp_present = snd_hda_codec_read(codec, 0x13, 0,
917                                      AC_VERB_GET_PIN_SENSE, 0) & 0x80000000;
918
919         bits = (spec->hp_present || !spec->cur_eapd) ? HDA_AMP_MUTE : 0;
920         snd_hda_codec_amp_stereo(codec, 0x1d, HDA_OUTPUT, 0,
921                                  HDA_AMP_MUTE, bits);
922         /* Mute/Unmute PCM 2 for good measure - some systems need this */
923         snd_hda_codec_amp_stereo(codec, 0x1c, HDA_OUTPUT, 0,
924                                  HDA_AMP_MUTE, bits);
925 }
926
927 /* mute internal speaker if HP is plugged */
928 static void cxt5047_hp2_automute(struct hda_codec *codec)
929 {
930         struct conexant_spec *spec = codec->spec;
931         unsigned int bits;
932
933         spec->hp_present = snd_hda_codec_read(codec, 0x13, 0,
934                                      AC_VERB_GET_PIN_SENSE, 0) & 0x80000000;
935
936         bits = spec->hp_present ? HDA_AMP_MUTE : 0;
937         snd_hda_codec_amp_stereo(codec, 0x1d, HDA_OUTPUT, 0,
938                                  HDA_AMP_MUTE, bits);
939         /* Mute/Unmute PCM 2 for good measure - some systems need this */
940         snd_hda_codec_amp_stereo(codec, 0x1c, HDA_OUTPUT, 0,
941                                  HDA_AMP_MUTE, bits);
942 }
943
944 /* toggle input of built-in and mic jack appropriately */
945 static void cxt5047_hp_automic(struct hda_codec *codec)
946 {
947         static struct hda_verb mic_jack_on[] = {
948                 {0x15, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
949                 {0x17, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
950                 {}
951         };
952         static struct hda_verb mic_jack_off[] = {
953                 {0x17, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
954                 {0x15, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
955                 {}
956         };
957         unsigned int present;
958
959         present = snd_hda_codec_read(codec, 0x15, 0,
960                                      AC_VERB_GET_PIN_SENSE, 0) & 0x80000000;
961         if (present)
962                 snd_hda_sequence_write(codec, mic_jack_on);
963         else
964                 snd_hda_sequence_write(codec, mic_jack_off);
965 }
966
967 /* unsolicited event for HP jack sensing */
968 static void cxt5047_hp_unsol_event(struct hda_codec *codec,
969                                   unsigned int res)
970 {
971         res >>= 26;
972         switch (res) {
973         case CONEXANT_HP_EVENT:
974                 cxt5047_hp_automute(codec);
975                 break;
976         case CONEXANT_MIC_EVENT:
977                 cxt5047_hp_automic(codec);
978                 break;
979         }
980 }
981
982 /* unsolicited event for HP jack sensing - non-EAPD systems */
983 static void cxt5047_hp2_unsol_event(struct hda_codec *codec,
984                                   unsigned int res)
985 {
986         res >>= 26;
987         switch (res) {
988         case CONEXANT_HP_EVENT:
989                 cxt5047_hp2_automute(codec);
990                 break;
991         case CONEXANT_MIC_EVENT:
992                 cxt5047_hp_automic(codec);
993                 break;
994         }
995 }
996
997 static struct snd_kcontrol_new cxt5047_mixers[] = {
998         HDA_CODEC_VOLUME("Mic Bypass Capture Volume", 0x19, 0x02, HDA_INPUT),
999         HDA_CODEC_MUTE("Mic Bypass Capture Switch", 0x19, 0x02, HDA_INPUT),
1000         HDA_CODEC_VOLUME("Mic Gain Volume", 0x1a, 0x0, HDA_OUTPUT),
1001         HDA_CODEC_MUTE("Mic Gain Switch", 0x1a, 0x0, HDA_OUTPUT),
1002         HDA_CODEC_VOLUME("Capture Volume", 0x12, 0x03, HDA_INPUT),
1003         HDA_CODEC_MUTE("Capture Switch", 0x12, 0x03, HDA_INPUT),
1004         HDA_CODEC_VOLUME("PCM Volume", 0x10, 0x00, HDA_OUTPUT),
1005         HDA_CODEC_MUTE("PCM Switch", 0x10, 0x00, HDA_OUTPUT),
1006         HDA_CODEC_VOLUME("PCM-2 Volume", 0x1c, 0x00, HDA_OUTPUT),
1007         HDA_CODEC_MUTE("PCM-2 Switch", 0x1c, 0x00, HDA_OUTPUT),
1008         HDA_CODEC_VOLUME("Speaker Playback Volume", 0x1d, 0x00, HDA_OUTPUT),
1009         HDA_CODEC_MUTE("Speaker Playback Switch", 0x1d, 0x00, HDA_OUTPUT),
1010         HDA_CODEC_VOLUME("Headphone Playback Volume", 0x13, 0x00, HDA_OUTPUT),
1011         HDA_CODEC_MUTE("Headphone Playback Switch", 0x13, 0x00, HDA_OUTPUT),
1012
1013         {}
1014 };
1015
1016 static struct snd_kcontrol_new cxt5047_toshiba_mixers[] = {
1017         {
1018                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1019                 .name = "Capture Source",
1020                 .info = conexant_mux_enum_info,
1021                 .get = conexant_mux_enum_get,
1022                 .put = conexant_mux_enum_put
1023         },
1024         HDA_CODEC_VOLUME("Mic Bypass Capture Volume", 0x19, 0x02, HDA_INPUT),
1025         HDA_CODEC_MUTE("Mic Bypass Capture Switch", 0x19, 0x02, HDA_INPUT),
1026         HDA_CODEC_VOLUME("Capture Volume", 0x12, 0x03, HDA_INPUT),
1027         HDA_CODEC_MUTE("Capture Switch", 0x12, 0x03, HDA_INPUT),
1028         HDA_CODEC_VOLUME("PCM Volume", 0x10, 0x00, HDA_OUTPUT),
1029         HDA_CODEC_MUTE("PCM Switch", 0x10, 0x00, HDA_OUTPUT),
1030         HDA_BIND_VOL("Master Playback Volume", &cxt5047_bind_master_vol),
1031         {
1032                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1033                 .name = "Master Playback Switch",
1034                 .info = cxt_eapd_info,
1035                 .get = cxt_eapd_get,
1036                 .put = cxt5047_hp_master_sw_put,
1037                 .private_value = 0x13,
1038         },
1039
1040         {}
1041 };
1042
1043 static struct snd_kcontrol_new cxt5047_hp_mixers[] = {
1044         {
1045                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1046                 .name = "Capture Source",
1047                 .info = conexant_mux_enum_info,
1048                 .get = conexant_mux_enum_get,
1049                 .put = conexant_mux_enum_put
1050         },
1051         HDA_CODEC_VOLUME("Mic Bypass Capture Volume", 0x19, 0x02, HDA_INPUT),
1052         HDA_CODEC_MUTE("Mic Bypass Capture Switch", 0x19,0x02,HDA_INPUT),
1053         HDA_CODEC_VOLUME("Capture Volume", 0x12, 0x03, HDA_INPUT),
1054         HDA_CODEC_MUTE("Capture Switch", 0x12, 0x03, HDA_INPUT),
1055         HDA_CODEC_VOLUME("PCM Volume", 0x10, 0x00, HDA_OUTPUT),
1056         HDA_CODEC_MUTE("PCM Switch", 0x10, 0x00, HDA_OUTPUT),
1057         HDA_CODEC_VOLUME("Master Playback Volume", 0x13, 0x00, HDA_OUTPUT),
1058         {
1059                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1060                 .name = "Master Playback Switch",
1061                 .info = cxt_eapd_info,
1062                 .get = cxt_eapd_get,
1063                 .put = cxt5047_hp_master_sw_put,
1064                 .private_value = 0x13,
1065         },
1066         { } /* end */
1067 };
1068
1069 static struct hda_verb cxt5047_init_verbs[] = {
1070         /* Line in, Mic, Built-in Mic */
1071         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
1072         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN|AC_PINCTL_VREF_50 },
1073         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN|AC_PINCTL_VREF_50 },
1074         /* HP, Speaker  */
1075         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
1076         {0x13, AC_VERB_SET_CONNECT_SEL,0x1},
1077         {0x1d, AC_VERB_SET_CONNECT_SEL,0x0},
1078         /* Record selector: Mic */
1079         {0x12, AC_VERB_SET_CONNECT_SEL,0x03},
1080         {0x19, AC_VERB_SET_AMP_GAIN_MUTE,
1081          AC_AMP_SET_INPUT|AC_AMP_SET_RIGHT|AC_AMP_SET_LEFT|0x17},
1082         {0x1A, AC_VERB_SET_CONNECT_SEL,0x02},
1083         {0x1A, AC_VERB_SET_AMP_GAIN_MUTE,
1084          AC_AMP_SET_OUTPUT|AC_AMP_SET_RIGHT|AC_AMP_SET_LEFT|0x00},
1085         {0x1A, AC_VERB_SET_AMP_GAIN_MUTE,
1086          AC_AMP_SET_OUTPUT|AC_AMP_SET_RIGHT|AC_AMP_SET_LEFT|0x03},
1087         /* SPDIF route: PCM */
1088         { 0x18, AC_VERB_SET_CONNECT_SEL, 0x0 },
1089         /* Enable unsolicited events */
1090         {0x13, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_HP_EVENT},
1091         {0x15, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_MIC_EVENT},
1092         { } /* end */
1093 };
1094
1095 /* configuration for Toshiba Laptops */
1096 static struct hda_verb cxt5047_toshiba_init_verbs[] = {
1097         {0x13, AC_VERB_SET_EAPD_BTLENABLE, 0x0 }, /* default on */
1098         /* pin sensing on HP and Mic jacks */
1099         {0x13, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_HP_EVENT},
1100         {0x15, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_MIC_EVENT},
1101         /* Speaker routing */
1102         {0x1d, AC_VERB_SET_CONNECT_SEL,0x1},
1103         {}
1104 };
1105
1106 /* configuration for HP Laptops */
1107 static struct hda_verb cxt5047_hp_init_verbs[] = {
1108         /* pin sensing on HP jack */
1109         {0x13, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_HP_EVENT},
1110         /* Record selector: Ext Mic */
1111         {0x12, AC_VERB_SET_CONNECT_SEL,0x03},
1112         {0x19, AC_VERB_SET_AMP_GAIN_MUTE,
1113          AC_AMP_SET_INPUT|AC_AMP_SET_RIGHT|AC_AMP_SET_LEFT|0x17},
1114         /* Speaker routing */
1115         {0x1d, AC_VERB_SET_CONNECT_SEL,0x1},
1116         {}
1117 };
1118
1119 /* Test configuration for debugging, modelled after the ALC260 test
1120  * configuration.
1121  */
1122 #ifdef CONFIG_SND_DEBUG
1123 static struct hda_input_mux cxt5047_test_capture_source = {
1124         .num_items = 4,
1125         .items = {
1126                 { "LINE1 pin", 0x0 },
1127                 { "MIC1 pin", 0x1 },
1128                 { "MIC2 pin", 0x2 },
1129                 { "CD pin", 0x3 },
1130         },
1131 };
1132
1133 static struct snd_kcontrol_new cxt5047_test_mixer[] = {
1134
1135         /* Output only controls */
1136         HDA_CODEC_VOLUME("OutAmp-1 Volume", 0x10, 0x0, HDA_OUTPUT),
1137         HDA_CODEC_MUTE("OutAmp-1 Switch", 0x10,0x0, HDA_OUTPUT),
1138         HDA_CODEC_VOLUME("OutAmp-2 Volume", 0x1c, 0x0, HDA_OUTPUT),
1139         HDA_CODEC_MUTE("OutAmp-2 Switch", 0x1c, 0x0, HDA_OUTPUT),
1140         HDA_CODEC_VOLUME("Speaker Playback Volume", 0x1d, 0x0, HDA_OUTPUT),
1141         HDA_CODEC_MUTE("Speaker Playback Switch", 0x1d, 0x0, HDA_OUTPUT),
1142         HDA_CODEC_VOLUME("HeadPhone Playback Volume", 0x13, 0x0, HDA_OUTPUT),
1143         HDA_CODEC_MUTE("HeadPhone Playback Switch", 0x13, 0x0, HDA_OUTPUT),
1144         HDA_CODEC_VOLUME("Line1-Out Playback Volume", 0x14, 0x0, HDA_OUTPUT),
1145         HDA_CODEC_MUTE("Line1-Out Playback Switch", 0x14, 0x0, HDA_OUTPUT),
1146         HDA_CODEC_VOLUME("Line2-Out Playback Volume", 0x15, 0x0, HDA_OUTPUT),
1147         HDA_CODEC_MUTE("Line2-Out Playback Switch", 0x15, 0x0, HDA_OUTPUT),
1148
1149         /* Modes for retasking pin widgets */
1150         CXT_PIN_MODE("LINE1 pin mode", 0x14, CXT_PIN_DIR_INOUT),
1151         CXT_PIN_MODE("MIC1 pin mode", 0x15, CXT_PIN_DIR_INOUT),
1152
1153         /* EAPD Switch Control */
1154         CXT_EAPD_SWITCH("External Amplifier", 0x13, 0x0),
1155
1156         /* Loopback mixer controls */
1157         HDA_CODEC_VOLUME("MIC1 Playback Volume", 0x12, 0x01, HDA_INPUT),
1158         HDA_CODEC_MUTE("MIC1 Playback Switch", 0x12, 0x01, HDA_INPUT),
1159         HDA_CODEC_VOLUME("MIC2 Playback Volume", 0x12, 0x02, HDA_INPUT),
1160         HDA_CODEC_MUTE("MIC2 Playback Switch", 0x12, 0x02, HDA_INPUT),
1161         HDA_CODEC_VOLUME("LINE Playback Volume", 0x12, 0x0, HDA_INPUT),
1162         HDA_CODEC_MUTE("LINE Playback Switch", 0x12, 0x0, HDA_INPUT),
1163         HDA_CODEC_VOLUME("CD Playback Volume", 0x12, 0x04, HDA_INPUT),
1164         HDA_CODEC_MUTE("CD Playback Switch", 0x12, 0x04, HDA_INPUT),
1165
1166         HDA_CODEC_VOLUME("Capture-1 Volume", 0x19, 0x0, HDA_INPUT),
1167         HDA_CODEC_MUTE("Capture-1 Switch", 0x19, 0x0, HDA_INPUT),
1168         HDA_CODEC_VOLUME("Capture-2 Volume", 0x19, 0x1, HDA_INPUT),
1169         HDA_CODEC_MUTE("Capture-2 Switch", 0x19, 0x1, HDA_INPUT),
1170         HDA_CODEC_VOLUME("Capture-3 Volume", 0x19, 0x2, HDA_INPUT),
1171         HDA_CODEC_MUTE("Capture-3 Switch", 0x19, 0x2, HDA_INPUT),
1172         HDA_CODEC_VOLUME("Capture-4 Volume", 0x19, 0x3, HDA_INPUT),
1173         HDA_CODEC_MUTE("Capture-4 Switch", 0x19, 0x3, HDA_INPUT),
1174         {
1175                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1176                 .name = "Input Source",
1177                 .info = conexant_mux_enum_info,
1178                 .get = conexant_mux_enum_get,
1179                 .put = conexant_mux_enum_put,
1180         },
1181         { } /* end */
1182 };
1183
1184 static struct hda_verb cxt5047_test_init_verbs[] = {
1185         /* Enable retasking pins as output, initially without power amp */
1186         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1187         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1188         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1189
1190         /* Disable digital (SPDIF) pins initially, but users can enable
1191          * them via a mixer switch.  In the case of SPDIF-out, this initverb
1192          * payload also sets the generation to 0, output to be in "consumer"
1193          * PCM format, copyright asserted, no pre-emphasis and no validity
1194          * control.
1195          */
1196         {0x18, AC_VERB_SET_DIGI_CONVERT_1, 0},
1197
1198         /* Ensure mic1, mic2, line1 pin widgets take input from the 
1199          * OUT1 sum bus when acting as an output.
1200          */
1201         {0x1a, AC_VERB_SET_CONNECT_SEL, 0},
1202         {0x1b, AC_VERB_SET_CONNECT_SEL, 0},
1203
1204         /* Start with output sum widgets muted and their output gains at min */
1205         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1206         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
1207
1208         /* Unmute retasking pin widget output buffers since the default
1209          * state appears to be output.  As the pin mode is changed by the
1210          * user the pin mode control will take care of enabling the pin's
1211          * input/output buffers as needed.
1212          */
1213         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1214         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1215         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1216
1217         /* Mute capture amp left and right */
1218         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1219
1220         /* Set ADC connection select to match default mixer setting (mic1
1221          * pin)
1222          */
1223         {0x12, AC_VERB_SET_CONNECT_SEL, 0x00},
1224
1225         /* Mute all inputs to mixer widget (even unconnected ones) */
1226         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)}, /* mic1 pin */
1227         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)}, /* mic2 pin */
1228         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)}, /* line1 pin */
1229         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)}, /* line2 pin */
1230         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)}, /* CD pin */
1231         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(5)}, /* Beep-gen pin */
1232         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(6)}, /* Line-out pin */
1233         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(7)}, /* HP-pin pin */
1234
1235         { }
1236 };
1237 #endif
1238
1239
1240 /* initialize jack-sensing, too */
1241 static int cxt5047_hp_init(struct hda_codec *codec)
1242 {
1243         conexant_init(codec);
1244         cxt5047_hp_automute(codec);
1245         return 0;
1246 }
1247
1248
1249 enum {
1250         CXT5047_LAPTOP,         /* Laptops w/o EAPD support */
1251         CXT5047_LAPTOP_HP,      /* Some HP laptops */
1252         CXT5047_LAPTOP_EAPD,    /* Laptops with EAPD support */
1253 #ifdef CONFIG_SND_DEBUG
1254         CXT5047_TEST,
1255 #endif
1256         CXT5047_MODELS
1257 };
1258
1259 static const char *cxt5047_models[CXT5047_MODELS] = {
1260         [CXT5047_LAPTOP]        = "laptop",
1261         [CXT5047_LAPTOP_HP]     = "laptop-hp",
1262         [CXT5047_LAPTOP_EAPD]   = "laptop-eapd",
1263 #ifdef CONFIG_SND_DEBUG
1264         [CXT5047_TEST]          = "test",
1265 #endif
1266 };
1267
1268 static struct snd_pci_quirk cxt5047_cfg_tbl[] = {
1269         SND_PCI_QUIRK(0x103c, 0x30a0, "HP DV1000", CXT5047_LAPTOP),
1270         SND_PCI_QUIRK(0x103c, 0x30a5, "HP DV5200T/DV8000T", CXT5047_LAPTOP_HP),
1271         SND_PCI_QUIRK(0x103c, 0x30b2, "HP DV2000T/DV3000T", CXT5047_LAPTOP),
1272         SND_PCI_QUIRK(0x103c, 0x30b5, "HP DV2000Z", CXT5047_LAPTOP),
1273         SND_PCI_QUIRK(0x1179, 0xff31, "Toshiba P100", CXT5047_LAPTOP_EAPD),
1274         {}
1275 };
1276
1277 static int patch_cxt5047(struct hda_codec *codec)
1278 {
1279         struct conexant_spec *spec;
1280         int board_config;
1281
1282         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1283         if (!spec)
1284                 return -ENOMEM;
1285         mutex_init(&spec->amp_mutex);
1286         codec->spec = spec;
1287
1288         spec->multiout.max_channels = 2;
1289         spec->multiout.num_dacs = ARRAY_SIZE(cxt5047_dac_nids);
1290         spec->multiout.dac_nids = cxt5047_dac_nids;
1291         spec->multiout.dig_out_nid = CXT5047_SPDIF_OUT;
1292         spec->num_adc_nids = 1;
1293         spec->adc_nids = cxt5047_adc_nids;
1294         spec->capsrc_nids = cxt5047_capsrc_nids;
1295         spec->input_mux = &cxt5047_capture_source;
1296         spec->num_mixers = 1;
1297         spec->mixers[0] = cxt5047_mixers;
1298         spec->num_init_verbs = 1;
1299         spec->init_verbs[0] = cxt5047_init_verbs;
1300         spec->spdif_route = 0;
1301         spec->num_channel_mode = ARRAY_SIZE(cxt5047_modes),
1302         spec->channel_mode = cxt5047_modes,
1303
1304         codec->patch_ops = conexant_patch_ops;
1305
1306         board_config = snd_hda_check_board_config(codec, CXT5047_MODELS,
1307                                                   cxt5047_models,
1308                                                   cxt5047_cfg_tbl);
1309         switch (board_config) {
1310         case CXT5047_LAPTOP:
1311                 codec->patch_ops.unsol_event = cxt5047_hp2_unsol_event;
1312                 break;
1313         case CXT5047_LAPTOP_HP:
1314                 spec->input_mux = &cxt5047_hp_capture_source;
1315                 spec->num_init_verbs = 2;
1316                 spec->init_verbs[1] = cxt5047_hp_init_verbs;
1317                 spec->mixers[0] = cxt5047_hp_mixers;
1318                 codec->patch_ops.unsol_event = cxt5047_hp_unsol_event;
1319                 codec->patch_ops.init = cxt5047_hp_init;
1320                 break;
1321         case CXT5047_LAPTOP_EAPD:
1322                 spec->input_mux = &cxt5047_toshiba_capture_source;
1323                 spec->num_init_verbs = 2;
1324                 spec->init_verbs[1] = cxt5047_toshiba_init_verbs;
1325                 spec->mixers[0] = cxt5047_toshiba_mixers;
1326                 codec->patch_ops.unsol_event = cxt5047_hp_unsol_event;
1327                 break;
1328 #ifdef CONFIG_SND_DEBUG
1329         case CXT5047_TEST:
1330                 spec->input_mux = &cxt5047_test_capture_source;
1331                 spec->mixers[0] = cxt5047_test_mixer;
1332                 spec->init_verbs[0] = cxt5047_test_init_verbs;
1333                 codec->patch_ops.unsol_event = cxt5047_hp_unsol_event;
1334 #endif  
1335         }
1336         return 0;
1337 }
1338
1339 struct hda_codec_preset snd_hda_preset_conexant[] = {
1340         { .id = 0x14f15045, .name = "CX20549 (Venice)",
1341           .patch = patch_cxt5045 },
1342         { .id = 0x14f15047, .name = "CX20551 (Waikiki)",
1343           .patch = patch_cxt5047 },
1344         {} /* terminator */
1345 };