[ALSA] hda-codec - Add Conexant 5051 codec support
[linux-2.6] / 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         unsigned int cur_adc_idx;
68         hda_nid_t cur_adc;
69         unsigned int cur_adc_stream_tag;
70         unsigned int cur_adc_format;
71
72         /* capture source */
73         const struct hda_input_mux *input_mux;
74         hda_nid_t *capsrc_nids;
75         unsigned int cur_mux[3];
76
77         /* channel model */
78         const struct hda_channel_mode *channel_mode;
79         int num_channel_mode;
80
81         /* PCM information */
82         struct hda_pcm pcm_rec[2];      /* used in build_pcms() */
83
84         struct mutex amp_mutex; /* PCM volume/mute control mutex */
85         unsigned int spdif_route;
86
87         /* dynamic controls, init_verbs and input_mux */
88         struct auto_pin_cfg autocfg;
89         unsigned int num_kctl_alloc, num_kctl_used;
90         struct snd_kcontrol_new *kctl_alloc;
91         struct hda_input_mux private_imux;
92         hda_nid_t private_dac_nids[AUTO_CFG_MAX_OUTS];
93
94 };
95
96 static int conexant_playback_pcm_open(struct hda_pcm_stream *hinfo,
97                                       struct hda_codec *codec,
98                                       struct snd_pcm_substream *substream)
99 {
100         struct conexant_spec *spec = codec->spec;
101         return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream);
102 }
103
104 static int conexant_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
105                                          struct hda_codec *codec,
106                                          unsigned int stream_tag,
107                                          unsigned int format,
108                                          struct snd_pcm_substream *substream)
109 {
110         struct conexant_spec *spec = codec->spec;
111         return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
112                                                 stream_tag,
113                                                 format, substream);
114 }
115
116 static int conexant_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
117                                          struct hda_codec *codec,
118                                          struct snd_pcm_substream *substream)
119 {
120         struct conexant_spec *spec = codec->spec;
121         return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
122 }
123
124 /*
125  * Digital out
126  */
127 static int conexant_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
128                                           struct hda_codec *codec,
129                                           struct snd_pcm_substream *substream)
130 {
131         struct conexant_spec *spec = codec->spec;
132         return snd_hda_multi_out_dig_open(codec, &spec->multiout);
133 }
134
135 static int conexant_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
136                                          struct hda_codec *codec,
137                                          struct snd_pcm_substream *substream)
138 {
139         struct conexant_spec *spec = codec->spec;
140         return snd_hda_multi_out_dig_close(codec, &spec->multiout);
141 }
142
143 static int conexant_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
144                                          struct hda_codec *codec,
145                                          unsigned int stream_tag,
146                                          unsigned int format,
147                                          struct snd_pcm_substream *substream)
148 {
149         struct conexant_spec *spec = codec->spec;
150         return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
151                                              stream_tag,
152                                              format, substream);
153 }
154
155 /*
156  * Analog capture
157  */
158 static int conexant_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
159                                       struct hda_codec *codec,
160                                       unsigned int stream_tag,
161                                       unsigned int format,
162                                       struct snd_pcm_substream *substream)
163 {
164         struct conexant_spec *spec = codec->spec;
165         snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
166                                    stream_tag, 0, format);
167         return 0;
168 }
169
170 static int conexant_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
171                                       struct hda_codec *codec,
172                                       struct snd_pcm_substream *substream)
173 {
174         struct conexant_spec *spec = codec->spec;
175         snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
176                                    0, 0, 0);
177         return 0;
178 }
179
180
181
182 static struct hda_pcm_stream conexant_pcm_analog_playback = {
183         .substreams = 1,
184         .channels_min = 2,
185         .channels_max = 2,
186         .nid = 0, /* fill later */
187         .ops = {
188                 .open = conexant_playback_pcm_open,
189                 .prepare = conexant_playback_pcm_prepare,
190                 .cleanup = conexant_playback_pcm_cleanup
191         },
192 };
193
194 static struct hda_pcm_stream conexant_pcm_analog_capture = {
195         .substreams = 1,
196         .channels_min = 2,
197         .channels_max = 2,
198         .nid = 0, /* fill later */
199         .ops = {
200                 .prepare = conexant_capture_pcm_prepare,
201                 .cleanup = conexant_capture_pcm_cleanup
202         },
203 };
204
205
206 static struct hda_pcm_stream conexant_pcm_digital_playback = {
207         .substreams = 1,
208         .channels_min = 2,
209         .channels_max = 2,
210         .nid = 0, /* fill later */
211         .ops = {
212                 .open = conexant_dig_playback_pcm_open,
213                 .close = conexant_dig_playback_pcm_close,
214                 .prepare = conexant_dig_playback_pcm_prepare
215         },
216 };
217
218 static struct hda_pcm_stream conexant_pcm_digital_capture = {
219         .substreams = 1,
220         .channels_min = 2,
221         .channels_max = 2,
222         /* NID is set in alc_build_pcms */
223 };
224
225 static int cx5051_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
226                                       struct hda_codec *codec,
227                                       unsigned int stream_tag,
228                                       unsigned int format,
229                                       struct snd_pcm_substream *substream)
230 {
231         struct conexant_spec *spec = codec->spec;
232         spec->cur_adc = spec->adc_nids[spec->cur_adc_idx];
233         spec->cur_adc_stream_tag = stream_tag;
234         spec->cur_adc_format = format;
235         snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
236         return 0;
237 }
238
239 static int cx5051_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
240                                       struct hda_codec *codec,
241                                       struct snd_pcm_substream *substream)
242 {
243         struct conexant_spec *spec = codec->spec;
244         snd_hda_codec_setup_stream(codec, spec->cur_adc, 0, 0, 0);
245         spec->cur_adc = 0;
246         return 0;
247 }
248
249 static struct hda_pcm_stream cx5051_pcm_analog_capture = {
250         .substreams = 1,
251         .channels_min = 2,
252         .channels_max = 2,
253         .nid = 0, /* fill later */
254         .ops = {
255                 .prepare = cx5051_capture_pcm_prepare,
256                 .cleanup = cx5051_capture_pcm_cleanup
257         },
258 };
259
260 static int conexant_build_pcms(struct hda_codec *codec)
261 {
262         struct conexant_spec *spec = codec->spec;
263         struct hda_pcm *info = spec->pcm_rec;
264
265         codec->num_pcms = 1;
266         codec->pcm_info = info;
267
268         info->name = "CONEXANT Analog";
269         info->stream[SNDRV_PCM_STREAM_PLAYBACK] = conexant_pcm_analog_playback;
270         info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
271                 spec->multiout.max_channels;
272         info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
273                 spec->multiout.dac_nids[0];
274         if (codec->vendor_id == 0x14f15051)
275                 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
276                         cx5051_pcm_analog_capture;
277         else
278                 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
279                         conexant_pcm_analog_capture;
280         info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = spec->num_adc_nids;
281         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
282
283         if (spec->multiout.dig_out_nid) {
284                 info++;
285                 codec->num_pcms++;
286                 info->name = "Conexant Digital";
287                 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
288                         conexant_pcm_digital_playback;
289                 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
290                         spec->multiout.dig_out_nid;
291                 if (spec->dig_in_nid) {
292                         info->stream[SNDRV_PCM_STREAM_CAPTURE] =
293                                 conexant_pcm_digital_capture;
294                         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
295                                 spec->dig_in_nid;
296                 }
297         }
298
299         return 0;
300 }
301
302 static int conexant_mux_enum_info(struct snd_kcontrol *kcontrol,
303                                   struct snd_ctl_elem_info *uinfo)
304 {
305         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
306         struct conexant_spec *spec = codec->spec;
307
308         return snd_hda_input_mux_info(spec->input_mux, uinfo);
309 }
310
311 static int conexant_mux_enum_get(struct snd_kcontrol *kcontrol,
312                                  struct snd_ctl_elem_value *ucontrol)
313 {
314         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
315         struct conexant_spec *spec = codec->spec;
316         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
317
318         ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
319         return 0;
320 }
321
322 static int conexant_mux_enum_put(struct snd_kcontrol *kcontrol,
323                                  struct snd_ctl_elem_value *ucontrol)
324 {
325         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
326         struct conexant_spec *spec = codec->spec;
327         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
328
329         return snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
330                                      spec->capsrc_nids[adc_idx],
331                                      &spec->cur_mux[adc_idx]);
332 }
333
334 static int conexant_init(struct hda_codec *codec)
335 {
336         struct conexant_spec *spec = codec->spec;
337         int i;
338
339         for (i = 0; i < spec->num_init_verbs; i++)
340                 snd_hda_sequence_write(codec, spec->init_verbs[i]);
341         return 0;
342 }
343
344 static void conexant_free(struct hda_codec *codec)
345 {
346         struct conexant_spec *spec = codec->spec;
347         unsigned int i;
348
349         if (spec->kctl_alloc) {
350                 for (i = 0; i < spec->num_kctl_used; i++)
351                         kfree(spec->kctl_alloc[i].name);
352                 kfree(spec->kctl_alloc);
353         }
354
355         kfree(codec->spec);
356 }
357
358 static int conexant_build_controls(struct hda_codec *codec)
359 {
360         struct conexant_spec *spec = codec->spec;
361         unsigned int i;
362         int err;
363
364         for (i = 0; i < spec->num_mixers; i++) {
365                 err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
366                 if (err < 0)
367                         return err;
368         }
369         if (spec->multiout.dig_out_nid) {
370                 err = snd_hda_create_spdif_out_ctls(codec,
371                                                     spec->multiout.dig_out_nid);
372                 if (err < 0)
373                         return err;
374         } 
375         if (spec->dig_in_nid) {
376                 err = snd_hda_create_spdif_in_ctls(codec,spec->dig_in_nid);
377                 if (err < 0)
378                         return err;
379         }
380         return 0;
381 }
382
383 static struct hda_codec_ops conexant_patch_ops = {
384         .build_controls = conexant_build_controls,
385         .build_pcms = conexant_build_pcms,
386         .init = conexant_init,
387         .free = conexant_free,
388 };
389
390 /*
391  * EAPD control
392  * the private value = nid | (invert << 8)
393  */
394
395 #define cxt_eapd_info           snd_ctl_boolean_mono_info
396
397 static int cxt_eapd_get(struct snd_kcontrol *kcontrol,
398                              struct snd_ctl_elem_value *ucontrol)
399 {
400         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
401         struct conexant_spec *spec = codec->spec;
402         int invert = (kcontrol->private_value >> 8) & 1;
403         if (invert)
404                 ucontrol->value.integer.value[0] = !spec->cur_eapd;
405         else
406                 ucontrol->value.integer.value[0] = spec->cur_eapd;
407         return 0;
408
409 }
410
411 static int cxt_eapd_put(struct snd_kcontrol *kcontrol,
412                              struct snd_ctl_elem_value *ucontrol)
413 {
414         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
415         struct conexant_spec *spec = codec->spec;
416         int invert = (kcontrol->private_value >> 8) & 1;
417         hda_nid_t nid = kcontrol->private_value & 0xff;
418         unsigned int eapd;
419
420         eapd = !!ucontrol->value.integer.value[0];
421         if (invert)
422                 eapd = !eapd;
423         if (eapd == spec->cur_eapd)
424                 return 0;
425         
426         spec->cur_eapd = eapd;
427         snd_hda_codec_write_cache(codec, nid,
428                                   0, AC_VERB_SET_EAPD_BTLENABLE,
429                                   eapd ? 0x02 : 0x00);
430         return 1;
431 }
432
433 /* controls for test mode */
434 #ifdef CONFIG_SND_DEBUG
435
436 #define CXT_EAPD_SWITCH(xname, nid, mask) \
437         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = 0,  \
438           .info = cxt_eapd_info, \
439           .get = cxt_eapd_get, \
440           .put = cxt_eapd_put, \
441           .private_value = nid | (mask<<16) }
442
443
444
445 static int conexant_ch_mode_info(struct snd_kcontrol *kcontrol,
446                                  struct snd_ctl_elem_info *uinfo)
447 {
448         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
449         struct conexant_spec *spec = codec->spec;
450         return snd_hda_ch_mode_info(codec, uinfo, spec->channel_mode,
451                                     spec->num_channel_mode);
452 }
453
454 static int conexant_ch_mode_get(struct snd_kcontrol *kcontrol,
455                                 struct snd_ctl_elem_value *ucontrol)
456 {
457         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
458         struct conexant_spec *spec = codec->spec;
459         return snd_hda_ch_mode_get(codec, ucontrol, spec->channel_mode,
460                                    spec->num_channel_mode,
461                                    spec->multiout.max_channels);
462 }
463
464 static int conexant_ch_mode_put(struct snd_kcontrol *kcontrol,
465                                 struct snd_ctl_elem_value *ucontrol)
466 {
467         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
468         struct conexant_spec *spec = codec->spec;
469         int err = snd_hda_ch_mode_put(codec, ucontrol, spec->channel_mode,
470                                       spec->num_channel_mode,
471                                       &spec->multiout.max_channels);
472         if (err >= 0 && spec->need_dac_fix)
473                 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
474         return err;
475 }
476
477 #define CXT_PIN_MODE(xname, nid, dir) \
478         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = 0,  \
479           .info = conexant_ch_mode_info, \
480           .get = conexant_ch_mode_get, \
481           .put = conexant_ch_mode_put, \
482           .private_value = nid | (dir<<16) }
483
484 #endif /* CONFIG_SND_DEBUG */
485
486 /* Conexant 5045 specific */
487
488 static hda_nid_t cxt5045_dac_nids[1] = { 0x19 };
489 static hda_nid_t cxt5045_adc_nids[1] = { 0x1a };
490 static hda_nid_t cxt5045_capsrc_nids[1] = { 0x1a };
491 #define CXT5045_SPDIF_OUT       0x13
492
493 static struct hda_channel_mode cxt5045_modes[1] = {
494         { 2, NULL },
495 };
496
497 static struct hda_input_mux cxt5045_capture_source = {
498         .num_items = 2,
499         .items = {
500                 { "IntMic", 0x1 },
501                 { "ExtMic", 0x2 },
502         }
503 };
504
505 static struct hda_input_mux cxt5045_capture_source_benq = {
506         .num_items = 3,
507         .items = {
508                 { "IntMic", 0x1 },
509                 { "ExtMic", 0x2 },
510                 { "LineIn", 0x3 },
511         }
512 };
513
514 /* turn on/off EAPD (+ mute HP) as a master switch */
515 static int cxt5045_hp_master_sw_put(struct snd_kcontrol *kcontrol,
516                                     struct snd_ctl_elem_value *ucontrol)
517 {
518         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
519         struct conexant_spec *spec = codec->spec;
520         unsigned int bits;
521
522         if (!cxt_eapd_put(kcontrol, ucontrol))
523                 return 0;
524
525         /* toggle internal speakers mute depending of presence of
526          * the headphone jack
527          */
528         bits = (!spec->hp_present && spec->cur_eapd) ? 0 : HDA_AMP_MUTE;
529         snd_hda_codec_amp_stereo(codec, 0x10, HDA_OUTPUT, 0,
530                                  HDA_AMP_MUTE, bits);
531
532         bits = spec->cur_eapd ? 0 : HDA_AMP_MUTE;
533         snd_hda_codec_amp_stereo(codec, 0x11, HDA_OUTPUT, 0,
534                                  HDA_AMP_MUTE, bits);
535         return 1;
536 }
537
538 /* bind volumes of both NID 0x10 and 0x11 */
539 static struct hda_bind_ctls cxt5045_hp_bind_master_vol = {
540         .ops = &snd_hda_bind_vol,
541         .values = {
542                 HDA_COMPOSE_AMP_VAL(0x10, 3, 0, HDA_OUTPUT),
543                 HDA_COMPOSE_AMP_VAL(0x11, 3, 0, HDA_OUTPUT),
544                 0
545         },
546 };
547
548 /* toggle input of built-in and mic jack appropriately */
549 static void cxt5045_hp_automic(struct hda_codec *codec)
550 {
551         static struct hda_verb mic_jack_on[] = {
552                 {0x14, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
553                 {0x12, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
554                 {}
555         };
556         static struct hda_verb mic_jack_off[] = {
557                 {0x12, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
558                 {0x14, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
559                 {}
560         };
561         unsigned int present;
562
563         present = snd_hda_codec_read(codec, 0x12, 0,
564                                      AC_VERB_GET_PIN_SENSE, 0) & 0x80000000;
565         if (present)
566                 snd_hda_sequence_write(codec, mic_jack_on);
567         else
568                 snd_hda_sequence_write(codec, mic_jack_off);
569 }
570
571
572 /* mute internal speaker if HP is plugged */
573 static void cxt5045_hp_automute(struct hda_codec *codec)
574 {
575         struct conexant_spec *spec = codec->spec;
576         unsigned int bits;
577
578         spec->hp_present = snd_hda_codec_read(codec, 0x11, 0,
579                                      AC_VERB_GET_PIN_SENSE, 0) & 0x80000000;
580
581         bits = (spec->hp_present || !spec->cur_eapd) ? HDA_AMP_MUTE : 0; 
582         snd_hda_codec_amp_stereo(codec, 0x10, HDA_OUTPUT, 0,
583                                  HDA_AMP_MUTE, bits);
584 }
585
586 /* unsolicited event for HP jack sensing */
587 static void cxt5045_hp_unsol_event(struct hda_codec *codec,
588                                    unsigned int res)
589 {
590         res >>= 26;
591         switch (res) {
592         case CONEXANT_HP_EVENT:
593                 cxt5045_hp_automute(codec);
594                 break;
595         case CONEXANT_MIC_EVENT:
596                 cxt5045_hp_automic(codec);
597                 break;
598
599         }
600 }
601
602 static struct snd_kcontrol_new cxt5045_mixers[] = {
603         {
604                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
605                 .name = "Capture Source",
606                 .info = conexant_mux_enum_info,
607                 .get = conexant_mux_enum_get,
608                 .put = conexant_mux_enum_put
609         },
610         HDA_CODEC_VOLUME("Int Mic Capture Volume", 0x1a, 0x01, HDA_INPUT),
611         HDA_CODEC_MUTE("Int Mic Capture Switch", 0x1a, 0x01, HDA_INPUT),
612         HDA_CODEC_VOLUME("Ext Mic Capture Volume", 0x1a, 0x02, HDA_INPUT),
613         HDA_CODEC_MUTE("Ext Mic Capture Switch", 0x1a, 0x02, HDA_INPUT),
614         HDA_CODEC_VOLUME("PCM Playback Volume", 0x17, 0x0, HDA_INPUT),
615         HDA_CODEC_MUTE("PCM Playback Switch", 0x17, 0x0, HDA_INPUT),
616         HDA_CODEC_VOLUME("Int Mic Playback Volume", 0x17, 0x1, HDA_INPUT),
617         HDA_CODEC_MUTE("Int Mic Playback Switch", 0x17, 0x1, HDA_INPUT),
618         HDA_CODEC_VOLUME("Ext Mic Playback Volume", 0x17, 0x2, HDA_INPUT),
619         HDA_CODEC_MUTE("Ext Mic Playback Switch", 0x17, 0x2, HDA_INPUT),
620         HDA_BIND_VOL("Master Playback Volume", &cxt5045_hp_bind_master_vol),
621         {
622                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
623                 .name = "Master Playback Switch",
624                 .info = cxt_eapd_info,
625                 .get = cxt_eapd_get,
626                 .put = cxt5045_hp_master_sw_put,
627                 .private_value = 0x10,
628         },
629
630         {}
631 };
632
633 static struct snd_kcontrol_new cxt5045_benq_mixers[] = {
634         HDA_CODEC_VOLUME("Line In Capture Volume", 0x1a, 0x03, HDA_INPUT),
635         HDA_CODEC_MUTE("Line In Capture Switch", 0x1a, 0x03, HDA_INPUT),
636         HDA_CODEC_VOLUME("Line In Playback Volume", 0x17, 0x3, HDA_INPUT),
637         HDA_CODEC_MUTE("Line In Playback Switch", 0x17, 0x3, HDA_INPUT),
638
639         {}
640 };
641
642 static struct hda_verb cxt5045_init_verbs[] = {
643         /* Line in, Mic */
644         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
645         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN|AC_PINCTL_VREF_80 },
646         /* HP, Amp  */
647         {0x10, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
648         {0x10, AC_VERB_SET_CONNECT_SEL, 0x1},
649         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
650         {0x11, AC_VERB_SET_CONNECT_SEL, 0x1},
651         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
652         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
653         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
654         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
655         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
656         /* Record selector: Int mic */
657         {0x1a, AC_VERB_SET_CONNECT_SEL,0x1},
658         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE,
659          AC_AMP_SET_INPUT|AC_AMP_SET_RIGHT|AC_AMP_SET_LEFT|0x17},
660         /* SPDIF route: PCM */
661         { 0x13, AC_VERB_SET_CONNECT_SEL, 0x0 },
662         /* EAPD */
663         {0x10, AC_VERB_SET_EAPD_BTLENABLE, 0x2 }, /* default on */ 
664         { } /* end */
665 };
666
667 static struct hda_verb cxt5045_benq_init_verbs[] = {
668         /* Int Mic, Mic */
669         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN|AC_PINCTL_VREF_80 },
670         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN|AC_PINCTL_VREF_80 },
671         /* Line In,HP, Amp  */
672         {0x10, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
673         {0x10, AC_VERB_SET_CONNECT_SEL, 0x1},
674         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
675         {0x11, AC_VERB_SET_CONNECT_SEL, 0x1},
676         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
677         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
678         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
679         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
680         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
681         /* Record selector: Int mic */
682         {0x1a, AC_VERB_SET_CONNECT_SEL, 0x1},
683         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE,
684          AC_AMP_SET_INPUT|AC_AMP_SET_RIGHT|AC_AMP_SET_LEFT|0x17},
685         /* SPDIF route: PCM */
686         {0x13, AC_VERB_SET_CONNECT_SEL, 0x0},
687         /* EAPD */
688         {0x10, AC_VERB_SET_EAPD_BTLENABLE, 0x2}, /* default on */
689         { } /* end */
690 };
691
692 static struct hda_verb cxt5045_hp_sense_init_verbs[] = {
693         /* pin sensing on HP jack */
694         {0x11, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_HP_EVENT},
695         { } /* end */
696 };
697
698 static struct hda_verb cxt5045_mic_sense_init_verbs[] = {
699         /* pin sensing on HP jack */
700         {0x12, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_MIC_EVENT},
701         { } /* end */
702 };
703
704 #ifdef CONFIG_SND_DEBUG
705 /* Test configuration for debugging, modelled after the ALC260 test
706  * configuration.
707  */
708 static struct hda_input_mux cxt5045_test_capture_source = {
709         .num_items = 5,
710         .items = {
711                 { "MIXER", 0x0 },
712                 { "MIC1 pin", 0x1 },
713                 { "LINE1 pin", 0x2 },
714                 { "HP-OUT pin", 0x3 },
715                 { "CD pin", 0x4 },
716         },
717 };
718
719 static struct snd_kcontrol_new cxt5045_test_mixer[] = {
720
721         /* Output controls */
722         HDA_CODEC_VOLUME("Speaker Playback Volume", 0x10, 0x0, HDA_OUTPUT),
723         HDA_CODEC_MUTE("Speaker Playback Switch", 0x10, 0x0, HDA_OUTPUT),
724         HDA_CODEC_VOLUME("Node 11 Playback Volume", 0x11, 0x0, HDA_OUTPUT),
725         HDA_CODEC_MUTE("Node 11 Playback Switch", 0x11, 0x0, HDA_OUTPUT),
726         HDA_CODEC_VOLUME("Node 12 Playback Volume", 0x12, 0x0, HDA_OUTPUT),
727         HDA_CODEC_MUTE("Node 12 Playback Switch", 0x12, 0x0, HDA_OUTPUT),
728         
729         /* Modes for retasking pin widgets */
730         CXT_PIN_MODE("HP-OUT pin mode", 0x11, CXT_PIN_DIR_INOUT),
731         CXT_PIN_MODE("LINE1 pin mode", 0x12, CXT_PIN_DIR_INOUT),
732
733         /* EAPD Switch Control */
734         CXT_EAPD_SWITCH("External Amplifier", 0x10, 0x0),
735
736         /* Loopback mixer controls */
737
738         HDA_CODEC_VOLUME("Mixer-1 Volume", 0x17, 0x0, HDA_INPUT),
739         HDA_CODEC_MUTE("Mixer-1 Switch", 0x17, 0x0, HDA_INPUT),
740         HDA_CODEC_VOLUME("Mixer-2 Volume", 0x17, 0x1, HDA_INPUT),
741         HDA_CODEC_MUTE("Mixer-2 Switch", 0x17, 0x1, HDA_INPUT),
742         HDA_CODEC_VOLUME("Mixer-3 Volume", 0x17, 0x2, HDA_INPUT),
743         HDA_CODEC_MUTE("Mixer-3 Switch", 0x17, 0x2, HDA_INPUT),
744         HDA_CODEC_VOLUME("Mixer-4 Volume", 0x17, 0x3, HDA_INPUT),
745         HDA_CODEC_MUTE("Mixer-4 Switch", 0x17, 0x3, HDA_INPUT),
746         HDA_CODEC_VOLUME("Mixer-5 Volume", 0x17, 0x4, HDA_INPUT),
747         HDA_CODEC_MUTE("Mixer-5 Switch", 0x17, 0x4, HDA_INPUT),
748         {
749                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
750                 .name = "Input Source",
751                 .info = conexant_mux_enum_info,
752                 .get = conexant_mux_enum_get,
753                 .put = conexant_mux_enum_put,
754         },
755         /* Audio input controls */
756         HDA_CODEC_VOLUME("Input-1 Volume", 0x1a, 0x0, HDA_INPUT),
757         HDA_CODEC_MUTE("Input-1 Switch", 0x1a, 0x0, HDA_INPUT),
758         HDA_CODEC_VOLUME("Input-2 Volume", 0x1a, 0x1, HDA_INPUT),
759         HDA_CODEC_MUTE("Input-2 Switch", 0x1a, 0x1, HDA_INPUT),
760         HDA_CODEC_VOLUME("Input-3 Volume", 0x1a, 0x2, HDA_INPUT),
761         HDA_CODEC_MUTE("Input-3 Switch", 0x1a, 0x2, HDA_INPUT),
762         HDA_CODEC_VOLUME("Input-4 Volume", 0x1a, 0x3, HDA_INPUT),
763         HDA_CODEC_MUTE("Input-4 Switch", 0x1a, 0x3, HDA_INPUT),
764         HDA_CODEC_VOLUME("Input-5 Volume", 0x1a, 0x4, HDA_INPUT),
765         HDA_CODEC_MUTE("Input-5 Switch", 0x1a, 0x4, HDA_INPUT),
766         { } /* end */
767 };
768
769 static struct hda_verb cxt5045_test_init_verbs[] = {
770         /* Set connections */
771         { 0x10, AC_VERB_SET_CONNECT_SEL, 0x0 },
772         { 0x11, AC_VERB_SET_CONNECT_SEL, 0x0 },
773         { 0x12, AC_VERB_SET_CONNECT_SEL, 0x0 },
774         /* Enable retasking pins as output, initially without power amp */
775         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
776         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
777
778         /* Disable digital (SPDIF) pins initially, but users can enable
779          * them via a mixer switch.  In the case of SPDIF-out, this initverb
780          * payload also sets the generation to 0, output to be in "consumer"
781          * PCM format, copyright asserted, no pre-emphasis and no validity
782          * control.
783          */
784         {0x13, AC_VERB_SET_DIGI_CONVERT_1, 0},
785
786         /* Start with output sum widgets muted and their output gains at min */
787         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
788         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
789
790         /* Unmute retasking pin widget output buffers since the default
791          * state appears to be output.  As the pin mode is changed by the
792          * user the pin mode control will take care of enabling the pin's
793          * input/output buffers as needed.
794          */
795         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
796         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
797
798         /* Mute capture amp left and right */
799         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
800
801         /* Set ADC connection select to match default mixer setting (mic1
802          * pin)
803          */
804         {0x1a, AC_VERB_SET_CONNECT_SEL, 0x00},
805         {0x17, AC_VERB_SET_CONNECT_SEL, 0x00},
806
807         /* Mute all inputs to mixer widget (even unconnected ones) */
808         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)}, /* Mixer pin */
809         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)}, /* Mic1 pin */
810         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)}, /* Line pin */
811         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)}, /* HP pin */
812         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)}, /* CD pin */
813
814         { }
815 };
816 #endif
817
818
819 /* initialize jack-sensing, too */
820 static int cxt5045_init(struct hda_codec *codec)
821 {
822         conexant_init(codec);
823         cxt5045_hp_automute(codec);
824         return 0;
825 }
826
827
828 enum {
829         CXT5045_LAPTOP_HPSENSE,
830         CXT5045_LAPTOP_MICSENSE,
831         CXT5045_LAPTOP_HPMICSENSE,
832         CXT5045_BENQ,
833 #ifdef CONFIG_SND_DEBUG
834         CXT5045_TEST,
835 #endif
836         CXT5045_MODELS
837 };
838
839 static const char *cxt5045_models[CXT5045_MODELS] = {
840         [CXT5045_LAPTOP_HPSENSE]        = "laptop-hpsense",
841         [CXT5045_LAPTOP_MICSENSE]       = "laptop-micsense",
842         [CXT5045_LAPTOP_HPMICSENSE]     = "laptop-hpmicsense",
843         [CXT5045_BENQ]                  = "benq",
844 #ifdef CONFIG_SND_DEBUG
845         [CXT5045_TEST]          = "test",
846 #endif
847 };
848
849 static struct snd_pci_quirk cxt5045_cfg_tbl[] = {
850         SND_PCI_QUIRK(0x103c, 0x30a5, "HP", CXT5045_LAPTOP_HPSENSE),
851         SND_PCI_QUIRK(0x103c, 0x30b2, "HP DV Series", CXT5045_LAPTOP_HPSENSE),
852         SND_PCI_QUIRK(0x103c, 0x30b5, "HP DV2120", CXT5045_LAPTOP_HPSENSE),
853         SND_PCI_QUIRK(0x103c, 0x30b7, "HP DV6000Z", CXT5045_LAPTOP_HPSENSE),
854         SND_PCI_QUIRK(0x103c, 0x30bb, "HP DV8000", CXT5045_LAPTOP_HPSENSE),
855         SND_PCI_QUIRK(0x103c, 0x30cd, "HP DV Series", CXT5045_LAPTOP_HPSENSE),
856         SND_PCI_QUIRK(0x103c, 0x30d5, "HP 530", CXT5045_LAPTOP_HPSENSE),
857         SND_PCI_QUIRK(0x103c, 0x30d9, "HP Spartan", CXT5045_LAPTOP_HPSENSE),
858         SND_PCI_QUIRK(0x152d, 0x0753, "Benq R55E", CXT5045_BENQ),
859         SND_PCI_QUIRK(0x1734, 0x10ad, "Fujitsu Si1520", CXT5045_LAPTOP_MICSENSE),
860         SND_PCI_QUIRK(0x1734, 0x10cb, "Fujitsu Si3515", CXT5045_LAPTOP_HPMICSENSE),
861         SND_PCI_QUIRK(0x1734, 0x110e, "Fujitsu V5505", CXT5045_LAPTOP_HPSENSE),
862         SND_PCI_QUIRK(0x1509, 0x1e40, "FIC", CXT5045_LAPTOP_HPMICSENSE),
863         SND_PCI_QUIRK(0x1509, 0x2f05, "FIC", CXT5045_LAPTOP_HPMICSENSE),
864         SND_PCI_QUIRK(0x1509, 0x2f06, "FIC", CXT5045_LAPTOP_HPMICSENSE),
865         SND_PCI_QUIRK(0x1631, 0xc106, "Packard Bell", CXT5045_LAPTOP_HPMICSENSE),
866         SND_PCI_QUIRK(0x1631, 0xc107, "Packard Bell", CXT5045_LAPTOP_HPMICSENSE),
867         SND_PCI_QUIRK(0x8086, 0x2111, "Conexant Reference board", CXT5045_LAPTOP_HPSENSE),
868         {}
869 };
870
871 static int patch_cxt5045(struct hda_codec *codec)
872 {
873         struct conexant_spec *spec;
874         int board_config;
875
876         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
877         if (!spec)
878                 return -ENOMEM;
879         mutex_init(&spec->amp_mutex);
880         codec->spec = spec;
881
882         spec->multiout.max_channels = 2;
883         spec->multiout.num_dacs = ARRAY_SIZE(cxt5045_dac_nids);
884         spec->multiout.dac_nids = cxt5045_dac_nids;
885         spec->multiout.dig_out_nid = CXT5045_SPDIF_OUT;
886         spec->num_adc_nids = 1;
887         spec->adc_nids = cxt5045_adc_nids;
888         spec->capsrc_nids = cxt5045_capsrc_nids;
889         spec->input_mux = &cxt5045_capture_source;
890         spec->num_mixers = 1;
891         spec->mixers[0] = cxt5045_mixers;
892         spec->num_init_verbs = 1;
893         spec->init_verbs[0] = cxt5045_init_verbs;
894         spec->spdif_route = 0;
895         spec->num_channel_mode = ARRAY_SIZE(cxt5045_modes),
896         spec->channel_mode = cxt5045_modes,
897
898
899         codec->patch_ops = conexant_patch_ops;
900
901         board_config = snd_hda_check_board_config(codec, CXT5045_MODELS,
902                                                   cxt5045_models,
903                                                   cxt5045_cfg_tbl);
904         switch (board_config) {
905         case CXT5045_LAPTOP_HPSENSE:
906                 codec->patch_ops.unsol_event = cxt5045_hp_unsol_event;
907                 spec->input_mux = &cxt5045_capture_source;
908                 spec->num_init_verbs = 2;
909                 spec->init_verbs[1] = cxt5045_hp_sense_init_verbs;
910                 spec->mixers[0] = cxt5045_mixers;
911                 codec->patch_ops.init = cxt5045_init;
912                 break;
913         case CXT5045_LAPTOP_MICSENSE:
914                 spec->input_mux = &cxt5045_capture_source;
915                 spec->num_init_verbs = 2;
916                 spec->init_verbs[1] = cxt5045_mic_sense_init_verbs;
917                 spec->mixers[0] = cxt5045_mixers;
918                 codec->patch_ops.init = cxt5045_init;
919                 break;
920         default:
921         case CXT5045_LAPTOP_HPMICSENSE:
922                 codec->patch_ops.unsol_event = cxt5045_hp_unsol_event;
923                 spec->input_mux = &cxt5045_capture_source;
924                 spec->num_init_verbs = 3;
925                 spec->init_verbs[1] = cxt5045_hp_sense_init_verbs;
926                 spec->init_verbs[2] = cxt5045_mic_sense_init_verbs;
927                 spec->mixers[0] = cxt5045_mixers;
928                 codec->patch_ops.init = cxt5045_init;
929                 break;
930         case CXT5045_BENQ:
931                 codec->patch_ops.unsol_event = cxt5045_hp_unsol_event;
932                 spec->input_mux = &cxt5045_capture_source_benq;
933                 spec->num_init_verbs = 1;
934                 spec->init_verbs[0] = cxt5045_benq_init_verbs;
935                 spec->mixers[0] = cxt5045_mixers;
936                 spec->mixers[1] = cxt5045_benq_mixers;
937                 spec->num_mixers = 2;
938                 codec->patch_ops.init = cxt5045_init;
939                 break;
940 #ifdef CONFIG_SND_DEBUG
941         case CXT5045_TEST:
942                 spec->input_mux = &cxt5045_test_capture_source;
943                 spec->mixers[0] = cxt5045_test_mixer;
944                 spec->init_verbs[0] = cxt5045_test_init_verbs;
945                 break;
946                 
947 #endif  
948         }
949
950         /*
951          * Fix max PCM level to 0 dB
952          * (originall it has 0x2b steps with 0dB offset 0x14)
953          */
954         snd_hda_override_amp_caps(codec, 0x17, HDA_INPUT,
955                                   (0x14 << AC_AMPCAP_OFFSET_SHIFT) |
956                                   (0x14 << AC_AMPCAP_NUM_STEPS_SHIFT) |
957                                   (0x05 << AC_AMPCAP_STEP_SIZE_SHIFT) |
958                                   (1 << AC_AMPCAP_MUTE_SHIFT));
959
960         return 0;
961 }
962
963
964 /* Conexant 5047 specific */
965 #define CXT5047_SPDIF_OUT       0x11
966
967 static hda_nid_t cxt5047_dac_nids[2] = { 0x10, 0x1c };
968 static hda_nid_t cxt5047_adc_nids[1] = { 0x12 };
969 static hda_nid_t cxt5047_capsrc_nids[1] = { 0x1a };
970
971 static struct hda_channel_mode cxt5047_modes[1] = {
972         { 2, NULL },
973 };
974
975 static struct hda_input_mux cxt5047_capture_source = {
976         .num_items = 1,
977         .items = {
978                 { "Mic", 0x2 },
979         }
980 };
981
982 static struct hda_input_mux cxt5047_hp_capture_source = {
983         .num_items = 1,
984         .items = {
985                 { "ExtMic", 0x2 },
986         }
987 };
988
989 static struct hda_input_mux cxt5047_toshiba_capture_source = {
990         .num_items = 2,
991         .items = {
992                 { "ExtMic", 0x2 },
993                 { "Line-In", 0x1 },
994         }
995 };
996
997 /* turn on/off EAPD (+ mute HP) as a master switch */
998 static int cxt5047_hp_master_sw_put(struct snd_kcontrol *kcontrol,
999                                     struct snd_ctl_elem_value *ucontrol)
1000 {
1001         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1002         struct conexant_spec *spec = codec->spec;
1003         unsigned int bits;
1004
1005         if (!cxt_eapd_put(kcontrol, ucontrol))
1006                 return 0;
1007
1008         /* toggle internal speakers mute depending of presence of
1009          * the headphone jack
1010          */
1011         bits = (!spec->hp_present && spec->cur_eapd) ? 0 : HDA_AMP_MUTE;
1012         snd_hda_codec_amp_stereo(codec, 0x1d, HDA_OUTPUT, 0,
1013                                  HDA_AMP_MUTE, bits);
1014         bits = spec->cur_eapd ? 0 : HDA_AMP_MUTE;
1015         snd_hda_codec_amp_stereo(codec, 0x13, HDA_OUTPUT, 0,
1016                                  HDA_AMP_MUTE, bits);
1017         return 1;
1018 }
1019
1020 /* bind volumes of both NID 0x13 (Headphones) and 0x1d (Speakers) */
1021 static struct hda_bind_ctls cxt5047_bind_master_vol = {
1022         .ops = &snd_hda_bind_vol,
1023         .values = {
1024                 HDA_COMPOSE_AMP_VAL(0x13, 3, 0, HDA_OUTPUT),
1025                 HDA_COMPOSE_AMP_VAL(0x1d, 3, 0, HDA_OUTPUT),
1026                 0
1027         },
1028 };
1029
1030 /* mute internal speaker if HP is plugged */
1031 static void cxt5047_hp_automute(struct hda_codec *codec)
1032 {
1033         struct conexant_spec *spec = codec->spec;
1034         unsigned int bits;
1035
1036         spec->hp_present = snd_hda_codec_read(codec, 0x13, 0,
1037                                      AC_VERB_GET_PIN_SENSE, 0) & 0x80000000;
1038
1039         bits = (spec->hp_present || !spec->cur_eapd) ? HDA_AMP_MUTE : 0;
1040         snd_hda_codec_amp_stereo(codec, 0x1d, HDA_OUTPUT, 0,
1041                                  HDA_AMP_MUTE, bits);
1042         /* Mute/Unmute PCM 2 for good measure - some systems need this */
1043         snd_hda_codec_amp_stereo(codec, 0x1c, HDA_OUTPUT, 0,
1044                                  HDA_AMP_MUTE, bits);
1045 }
1046
1047 /* mute internal speaker if HP is plugged */
1048 static void cxt5047_hp2_automute(struct hda_codec *codec)
1049 {
1050         struct conexant_spec *spec = codec->spec;
1051         unsigned int bits;
1052
1053         spec->hp_present = snd_hda_codec_read(codec, 0x13, 0,
1054                                      AC_VERB_GET_PIN_SENSE, 0) & 0x80000000;
1055
1056         bits = spec->hp_present ? HDA_AMP_MUTE : 0;
1057         snd_hda_codec_amp_stereo(codec, 0x1d, HDA_OUTPUT, 0,
1058                                  HDA_AMP_MUTE, bits);
1059         /* Mute/Unmute PCM 2 for good measure - some systems need this */
1060         snd_hda_codec_amp_stereo(codec, 0x1c, HDA_OUTPUT, 0,
1061                                  HDA_AMP_MUTE, bits);
1062 }
1063
1064 /* toggle input of built-in and mic jack appropriately */
1065 static void cxt5047_hp_automic(struct hda_codec *codec)
1066 {
1067         static struct hda_verb mic_jack_on[] = {
1068                 {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1069                 {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1070                 {}
1071         };
1072         static struct hda_verb mic_jack_off[] = {
1073                 {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1074                 {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1075                 {}
1076         };
1077         unsigned int present;
1078
1079         present = snd_hda_codec_read(codec, 0x15, 0,
1080                                      AC_VERB_GET_PIN_SENSE, 0) & 0x80000000;
1081         if (present)
1082                 snd_hda_sequence_write(codec, mic_jack_on);
1083         else
1084                 snd_hda_sequence_write(codec, mic_jack_off);
1085 }
1086
1087 /* unsolicited event for HP jack sensing */
1088 static void cxt5047_hp_unsol_event(struct hda_codec *codec,
1089                                   unsigned int res)
1090 {
1091         switch (res >> 26) {
1092         case CONEXANT_HP_EVENT:
1093                 cxt5047_hp_automute(codec);
1094                 break;
1095         case CONEXANT_MIC_EVENT:
1096                 cxt5047_hp_automic(codec);
1097                 break;
1098         }
1099 }
1100
1101 /* unsolicited event for HP jack sensing - non-EAPD systems */
1102 static void cxt5047_hp2_unsol_event(struct hda_codec *codec,
1103                                   unsigned int res)
1104 {
1105         res >>= 26;
1106         switch (res) {
1107         case CONEXANT_HP_EVENT:
1108                 cxt5047_hp2_automute(codec);
1109                 break;
1110         case CONEXANT_MIC_EVENT:
1111                 cxt5047_hp_automic(codec);
1112                 break;
1113         }
1114 }
1115
1116 static struct snd_kcontrol_new cxt5047_mixers[] = {
1117         HDA_CODEC_VOLUME("Mic Bypass Capture Volume", 0x19, 0x02, HDA_INPUT),
1118         HDA_CODEC_MUTE("Mic Bypass Capture Switch", 0x19, 0x02, HDA_INPUT),
1119         HDA_CODEC_VOLUME("Mic Gain Volume", 0x1a, 0x0, HDA_OUTPUT),
1120         HDA_CODEC_MUTE("Mic Gain Switch", 0x1a, 0x0, HDA_OUTPUT),
1121         HDA_CODEC_VOLUME("Capture Volume", 0x12, 0x03, HDA_INPUT),
1122         HDA_CODEC_MUTE("Capture Switch", 0x12, 0x03, HDA_INPUT),
1123         HDA_CODEC_VOLUME("PCM Volume", 0x10, 0x00, HDA_OUTPUT),
1124         HDA_CODEC_MUTE("PCM Switch", 0x10, 0x00, HDA_OUTPUT),
1125         HDA_CODEC_VOLUME("PCM-2 Volume", 0x1c, 0x00, HDA_OUTPUT),
1126         HDA_CODEC_MUTE("PCM-2 Switch", 0x1c, 0x00, HDA_OUTPUT),
1127         HDA_CODEC_VOLUME("Speaker Playback Volume", 0x1d, 0x00, HDA_OUTPUT),
1128         HDA_CODEC_MUTE("Speaker Playback Switch", 0x1d, 0x00, HDA_OUTPUT),
1129         HDA_CODEC_VOLUME("Headphone Playback Volume", 0x13, 0x00, HDA_OUTPUT),
1130         HDA_CODEC_MUTE("Headphone Playback Switch", 0x13, 0x00, HDA_OUTPUT),
1131
1132         {}
1133 };
1134
1135 static struct snd_kcontrol_new cxt5047_toshiba_mixers[] = {
1136         {
1137                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1138                 .name = "Capture Source",
1139                 .info = conexant_mux_enum_info,
1140                 .get = conexant_mux_enum_get,
1141                 .put = conexant_mux_enum_put
1142         },
1143         HDA_CODEC_VOLUME("Mic Bypass Capture Volume", 0x19, 0x02, HDA_INPUT),
1144         HDA_CODEC_MUTE("Mic Bypass Capture Switch", 0x19, 0x02, HDA_INPUT),
1145         HDA_CODEC_VOLUME("Capture Volume", 0x12, 0x03, HDA_INPUT),
1146         HDA_CODEC_MUTE("Capture Switch", 0x12, 0x03, HDA_INPUT),
1147         HDA_CODEC_VOLUME("PCM Volume", 0x10, 0x00, HDA_OUTPUT),
1148         HDA_CODEC_MUTE("PCM Switch", 0x10, 0x00, HDA_OUTPUT),
1149         HDA_BIND_VOL("Master Playback Volume", &cxt5047_bind_master_vol),
1150         {
1151                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1152                 .name = "Master Playback Switch",
1153                 .info = cxt_eapd_info,
1154                 .get = cxt_eapd_get,
1155                 .put = cxt5047_hp_master_sw_put,
1156                 .private_value = 0x13,
1157         },
1158
1159         {}
1160 };
1161
1162 static struct snd_kcontrol_new cxt5047_hp_mixers[] = {
1163         {
1164                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1165                 .name = "Capture Source",
1166                 .info = conexant_mux_enum_info,
1167                 .get = conexant_mux_enum_get,
1168                 .put = conexant_mux_enum_put
1169         },
1170         HDA_CODEC_VOLUME("Mic Bypass Capture Volume", 0x19, 0x02, HDA_INPUT),
1171         HDA_CODEC_MUTE("Mic Bypass Capture Switch", 0x19,0x02,HDA_INPUT),
1172         HDA_CODEC_VOLUME("Capture Volume", 0x12, 0x03, HDA_INPUT),
1173         HDA_CODEC_MUTE("Capture Switch", 0x12, 0x03, HDA_INPUT),
1174         HDA_CODEC_VOLUME("PCM Volume", 0x10, 0x00, HDA_OUTPUT),
1175         HDA_CODEC_MUTE("PCM Switch", 0x10, 0x00, HDA_OUTPUT),
1176         HDA_CODEC_VOLUME("Master Playback Volume", 0x13, 0x00, HDA_OUTPUT),
1177         {
1178                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1179                 .name = "Master Playback Switch",
1180                 .info = cxt_eapd_info,
1181                 .get = cxt_eapd_get,
1182                 .put = cxt5047_hp_master_sw_put,
1183                 .private_value = 0x13,
1184         },
1185         { } /* end */
1186 };
1187
1188 static struct hda_verb cxt5047_init_verbs[] = {
1189         /* Line in, Mic, Built-in Mic */
1190         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
1191         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN|AC_PINCTL_VREF_50 },
1192         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN|AC_PINCTL_VREF_50 },
1193         /* HP, Speaker  */
1194         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
1195         {0x13, AC_VERB_SET_CONNECT_SEL,0x1},
1196         {0x1d, AC_VERB_SET_CONNECT_SEL,0x0},
1197         /* Record selector: Mic */
1198         {0x12, AC_VERB_SET_CONNECT_SEL,0x03},
1199         {0x19, AC_VERB_SET_AMP_GAIN_MUTE,
1200          AC_AMP_SET_INPUT|AC_AMP_SET_RIGHT|AC_AMP_SET_LEFT|0x17},
1201         {0x1A, AC_VERB_SET_CONNECT_SEL,0x02},
1202         {0x1A, AC_VERB_SET_AMP_GAIN_MUTE,
1203          AC_AMP_SET_OUTPUT|AC_AMP_SET_RIGHT|AC_AMP_SET_LEFT|0x00},
1204         {0x1A, AC_VERB_SET_AMP_GAIN_MUTE,
1205          AC_AMP_SET_OUTPUT|AC_AMP_SET_RIGHT|AC_AMP_SET_LEFT|0x03},
1206         /* SPDIF route: PCM */
1207         { 0x18, AC_VERB_SET_CONNECT_SEL, 0x0 },
1208         /* Enable unsolicited events */
1209         {0x13, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_HP_EVENT},
1210         {0x15, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_MIC_EVENT},
1211         { } /* end */
1212 };
1213
1214 /* configuration for Toshiba Laptops */
1215 static struct hda_verb cxt5047_toshiba_init_verbs[] = {
1216         {0x13, AC_VERB_SET_EAPD_BTLENABLE, 0x0 }, /* default on */
1217         /* pin sensing on HP and Mic jacks */
1218         {0x13, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_HP_EVENT},
1219         {0x15, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_MIC_EVENT},
1220         /* Speaker routing */
1221         {0x1d, AC_VERB_SET_CONNECT_SEL,0x1},
1222         {}
1223 };
1224
1225 /* configuration for HP Laptops */
1226 static struct hda_verb cxt5047_hp_init_verbs[] = {
1227         /* pin sensing on HP jack */
1228         {0x13, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_HP_EVENT},
1229         /* Record selector: Ext Mic */
1230         {0x12, AC_VERB_SET_CONNECT_SEL,0x03},
1231         {0x19, AC_VERB_SET_AMP_GAIN_MUTE,
1232          AC_AMP_SET_INPUT|AC_AMP_SET_RIGHT|AC_AMP_SET_LEFT|0x17},
1233         /* Speaker routing */
1234         {0x1d, AC_VERB_SET_CONNECT_SEL,0x1},
1235         {}
1236 };
1237
1238 /* Test configuration for debugging, modelled after the ALC260 test
1239  * configuration.
1240  */
1241 #ifdef CONFIG_SND_DEBUG
1242 static struct hda_input_mux cxt5047_test_capture_source = {
1243         .num_items = 4,
1244         .items = {
1245                 { "LINE1 pin", 0x0 },
1246                 { "MIC1 pin", 0x1 },
1247                 { "MIC2 pin", 0x2 },
1248                 { "CD pin", 0x3 },
1249         },
1250 };
1251
1252 static struct snd_kcontrol_new cxt5047_test_mixer[] = {
1253
1254         /* Output only controls */
1255         HDA_CODEC_VOLUME("OutAmp-1 Volume", 0x10, 0x0, HDA_OUTPUT),
1256         HDA_CODEC_MUTE("OutAmp-1 Switch", 0x10,0x0, HDA_OUTPUT),
1257         HDA_CODEC_VOLUME("OutAmp-2 Volume", 0x1c, 0x0, HDA_OUTPUT),
1258         HDA_CODEC_MUTE("OutAmp-2 Switch", 0x1c, 0x0, HDA_OUTPUT),
1259         HDA_CODEC_VOLUME("Speaker Playback Volume", 0x1d, 0x0, HDA_OUTPUT),
1260         HDA_CODEC_MUTE("Speaker Playback Switch", 0x1d, 0x0, HDA_OUTPUT),
1261         HDA_CODEC_VOLUME("HeadPhone Playback Volume", 0x13, 0x0, HDA_OUTPUT),
1262         HDA_CODEC_MUTE("HeadPhone Playback Switch", 0x13, 0x0, HDA_OUTPUT),
1263         HDA_CODEC_VOLUME("Line1-Out Playback Volume", 0x14, 0x0, HDA_OUTPUT),
1264         HDA_CODEC_MUTE("Line1-Out Playback Switch", 0x14, 0x0, HDA_OUTPUT),
1265         HDA_CODEC_VOLUME("Line2-Out Playback Volume", 0x15, 0x0, HDA_OUTPUT),
1266         HDA_CODEC_MUTE("Line2-Out Playback Switch", 0x15, 0x0, HDA_OUTPUT),
1267
1268         /* Modes for retasking pin widgets */
1269         CXT_PIN_MODE("LINE1 pin mode", 0x14, CXT_PIN_DIR_INOUT),
1270         CXT_PIN_MODE("MIC1 pin mode", 0x15, CXT_PIN_DIR_INOUT),
1271
1272         /* EAPD Switch Control */
1273         CXT_EAPD_SWITCH("External Amplifier", 0x13, 0x0),
1274
1275         /* Loopback mixer controls */
1276         HDA_CODEC_VOLUME("MIC1 Playback Volume", 0x12, 0x01, HDA_INPUT),
1277         HDA_CODEC_MUTE("MIC1 Playback Switch", 0x12, 0x01, HDA_INPUT),
1278         HDA_CODEC_VOLUME("MIC2 Playback Volume", 0x12, 0x02, HDA_INPUT),
1279         HDA_CODEC_MUTE("MIC2 Playback Switch", 0x12, 0x02, HDA_INPUT),
1280         HDA_CODEC_VOLUME("LINE Playback Volume", 0x12, 0x0, HDA_INPUT),
1281         HDA_CODEC_MUTE("LINE Playback Switch", 0x12, 0x0, HDA_INPUT),
1282         HDA_CODEC_VOLUME("CD Playback Volume", 0x12, 0x04, HDA_INPUT),
1283         HDA_CODEC_MUTE("CD Playback Switch", 0x12, 0x04, HDA_INPUT),
1284
1285         HDA_CODEC_VOLUME("Capture-1 Volume", 0x19, 0x0, HDA_INPUT),
1286         HDA_CODEC_MUTE("Capture-1 Switch", 0x19, 0x0, HDA_INPUT),
1287         HDA_CODEC_VOLUME("Capture-2 Volume", 0x19, 0x1, HDA_INPUT),
1288         HDA_CODEC_MUTE("Capture-2 Switch", 0x19, 0x1, HDA_INPUT),
1289         HDA_CODEC_VOLUME("Capture-3 Volume", 0x19, 0x2, HDA_INPUT),
1290         HDA_CODEC_MUTE("Capture-3 Switch", 0x19, 0x2, HDA_INPUT),
1291         HDA_CODEC_VOLUME("Capture-4 Volume", 0x19, 0x3, HDA_INPUT),
1292         HDA_CODEC_MUTE("Capture-4 Switch", 0x19, 0x3, HDA_INPUT),
1293         {
1294                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1295                 .name = "Input Source",
1296                 .info = conexant_mux_enum_info,
1297                 .get = conexant_mux_enum_get,
1298                 .put = conexant_mux_enum_put,
1299         },
1300         HDA_CODEC_VOLUME("Input-1 Volume", 0x1a, 0x0, HDA_INPUT),
1301         HDA_CODEC_MUTE("Input-1 Switch", 0x1a, 0x0, HDA_INPUT),
1302         HDA_CODEC_VOLUME("Input-2 Volume", 0x1a, 0x1, HDA_INPUT),
1303         HDA_CODEC_MUTE("Input-2 Switch", 0x1a, 0x1, HDA_INPUT),
1304         HDA_CODEC_VOLUME("Input-3 Volume", 0x1a, 0x2, HDA_INPUT),
1305         HDA_CODEC_MUTE("Input-3 Switch", 0x1a, 0x2, HDA_INPUT),
1306         HDA_CODEC_VOLUME("Input-4 Volume", 0x1a, 0x3, HDA_INPUT),
1307         HDA_CODEC_MUTE("Input-4 Switch", 0x1a, 0x3, HDA_INPUT),
1308         HDA_CODEC_VOLUME("Input-5 Volume", 0x1a, 0x4, HDA_INPUT),
1309         HDA_CODEC_MUTE("Input-5 Switch", 0x1a, 0x4, HDA_INPUT),
1310
1311         { } /* end */
1312 };
1313
1314 static struct hda_verb cxt5047_test_init_verbs[] = {
1315         /* Enable retasking pins as output, initially without power amp */
1316         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1317         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1318         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1319
1320         /* Disable digital (SPDIF) pins initially, but users can enable
1321          * them via a mixer switch.  In the case of SPDIF-out, this initverb
1322          * payload also sets the generation to 0, output to be in "consumer"
1323          * PCM format, copyright asserted, no pre-emphasis and no validity
1324          * control.
1325          */
1326         {0x18, AC_VERB_SET_DIGI_CONVERT_1, 0},
1327
1328         /* Ensure mic1, mic2, line1 pin widgets take input from the 
1329          * OUT1 sum bus when acting as an output.
1330          */
1331         {0x1a, AC_VERB_SET_CONNECT_SEL, 0},
1332         {0x1b, AC_VERB_SET_CONNECT_SEL, 0},
1333
1334         /* Start with output sum widgets muted and their output gains at min */
1335         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1336         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
1337
1338         /* Unmute retasking pin widget output buffers since the default
1339          * state appears to be output.  As the pin mode is changed by the
1340          * user the pin mode control will take care of enabling the pin's
1341          * input/output buffers as needed.
1342          */
1343         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1344         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1345         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1346
1347         /* Mute capture amp left and right */
1348         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1349
1350         /* Set ADC connection select to match default mixer setting (mic1
1351          * pin)
1352          */
1353         {0x12, AC_VERB_SET_CONNECT_SEL, 0x00},
1354
1355         /* Mute all inputs to mixer widget (even unconnected ones) */
1356         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)}, /* mic1 pin */
1357         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)}, /* mic2 pin */
1358         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)}, /* line1 pin */
1359         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)}, /* line2 pin */
1360         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)}, /* CD pin */
1361         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(5)}, /* Beep-gen pin */
1362         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(6)}, /* Line-out pin */
1363         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(7)}, /* HP-pin pin */
1364
1365         { }
1366 };
1367 #endif
1368
1369
1370 /* initialize jack-sensing, too */
1371 static int cxt5047_hp_init(struct hda_codec *codec)
1372 {
1373         conexant_init(codec);
1374         cxt5047_hp_automute(codec);
1375         return 0;
1376 }
1377
1378
1379 enum {
1380         CXT5047_LAPTOP,         /* Laptops w/o EAPD support */
1381         CXT5047_LAPTOP_HP,      /* Some HP laptops */
1382         CXT5047_LAPTOP_EAPD,    /* Laptops with EAPD support */
1383 #ifdef CONFIG_SND_DEBUG
1384         CXT5047_TEST,
1385 #endif
1386         CXT5047_MODELS
1387 };
1388
1389 static const char *cxt5047_models[CXT5047_MODELS] = {
1390         [CXT5047_LAPTOP]        = "laptop",
1391         [CXT5047_LAPTOP_HP]     = "laptop-hp",
1392         [CXT5047_LAPTOP_EAPD]   = "laptop-eapd",
1393 #ifdef CONFIG_SND_DEBUG
1394         [CXT5047_TEST]          = "test",
1395 #endif
1396 };
1397
1398 static struct snd_pci_quirk cxt5047_cfg_tbl[] = {
1399         SND_PCI_QUIRK(0x103c, 0x30a0, "HP DV1000", CXT5047_LAPTOP),
1400         SND_PCI_QUIRK(0x103c, 0x30a5, "HP DV5200T/DV8000T", CXT5047_LAPTOP_HP),
1401         SND_PCI_QUIRK(0x103c, 0x30b2, "HP DV2000T/DV3000T", CXT5047_LAPTOP),
1402         SND_PCI_QUIRK(0x103c, 0x30b5, "HP DV2000Z", CXT5047_LAPTOP),
1403         SND_PCI_QUIRK(0x1179, 0xff31, "Toshiba P100", CXT5047_LAPTOP_EAPD),
1404         {}
1405 };
1406
1407 static int patch_cxt5047(struct hda_codec *codec)
1408 {
1409         struct conexant_spec *spec;
1410         int board_config;
1411
1412         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1413         if (!spec)
1414                 return -ENOMEM;
1415         mutex_init(&spec->amp_mutex);
1416         codec->spec = spec;
1417
1418         spec->multiout.max_channels = 2;
1419         spec->multiout.num_dacs = ARRAY_SIZE(cxt5047_dac_nids);
1420         spec->multiout.dac_nids = cxt5047_dac_nids;
1421         spec->multiout.dig_out_nid = CXT5047_SPDIF_OUT;
1422         spec->num_adc_nids = 1;
1423         spec->adc_nids = cxt5047_adc_nids;
1424         spec->capsrc_nids = cxt5047_capsrc_nids;
1425         spec->input_mux = &cxt5047_capture_source;
1426         spec->num_mixers = 1;
1427         spec->mixers[0] = cxt5047_mixers;
1428         spec->num_init_verbs = 1;
1429         spec->init_verbs[0] = cxt5047_init_verbs;
1430         spec->spdif_route = 0;
1431         spec->num_channel_mode = ARRAY_SIZE(cxt5047_modes),
1432         spec->channel_mode = cxt5047_modes,
1433
1434         codec->patch_ops = conexant_patch_ops;
1435
1436         board_config = snd_hda_check_board_config(codec, CXT5047_MODELS,
1437                                                   cxt5047_models,
1438                                                   cxt5047_cfg_tbl);
1439         switch (board_config) {
1440         case CXT5047_LAPTOP:
1441                 codec->patch_ops.unsol_event = cxt5047_hp2_unsol_event;
1442                 break;
1443         case CXT5047_LAPTOP_HP:
1444                 spec->input_mux = &cxt5047_hp_capture_source;
1445                 spec->num_init_verbs = 2;
1446                 spec->init_verbs[1] = cxt5047_hp_init_verbs;
1447                 spec->mixers[0] = cxt5047_hp_mixers;
1448                 codec->patch_ops.unsol_event = cxt5047_hp_unsol_event;
1449                 codec->patch_ops.init = cxt5047_hp_init;
1450                 break;
1451         case CXT5047_LAPTOP_EAPD:
1452                 spec->input_mux = &cxt5047_toshiba_capture_source;
1453                 spec->num_init_verbs = 2;
1454                 spec->init_verbs[1] = cxt5047_toshiba_init_verbs;
1455                 spec->mixers[0] = cxt5047_toshiba_mixers;
1456                 codec->patch_ops.unsol_event = cxt5047_hp_unsol_event;
1457                 break;
1458 #ifdef CONFIG_SND_DEBUG
1459         case CXT5047_TEST:
1460                 spec->input_mux = &cxt5047_test_capture_source;
1461                 spec->mixers[0] = cxt5047_test_mixer;
1462                 spec->init_verbs[0] = cxt5047_test_init_verbs;
1463                 codec->patch_ops.unsol_event = cxt5047_hp_unsol_event;
1464 #endif  
1465         }
1466         return 0;
1467 }
1468
1469 /* Conexant 5051 specific */
1470 static hda_nid_t cxt5051_dac_nids[1] = { 0x10 };
1471 static hda_nid_t cxt5051_adc_nids[2] = { 0x14, 0x15 };
1472 #define CXT5051_SPDIF_OUT       0x1C
1473 #define CXT5051_PORTB_EVENT     0x38
1474 #define CXT5051_PORTC_EVENT     0x39
1475
1476 static struct hda_channel_mode cxt5051_modes[1] = {
1477         { 2, NULL },
1478 };
1479
1480 static void cxt5051_update_speaker(struct hda_codec *codec)
1481 {
1482         struct conexant_spec *spec = codec->spec;
1483         unsigned int pinctl;
1484         pinctl = (!spec->hp_present && spec->cur_eapd) ? PIN_OUT : 0;
1485         snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
1486                             pinctl);
1487 }
1488
1489 /* turn on/off EAPD (+ mute HP) as a master switch */
1490 static int cxt5051_hp_master_sw_put(struct snd_kcontrol *kcontrol,
1491                                     struct snd_ctl_elem_value *ucontrol)
1492 {
1493         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1494
1495         if (!cxt_eapd_put(kcontrol, ucontrol))
1496                 return 0;
1497         cxt5051_update_speaker(codec);
1498         return 1;
1499 }
1500
1501 /* toggle input of built-in and mic jack appropriately */
1502 static void cxt5051_portb_automic(struct hda_codec *codec)
1503 {
1504         unsigned int present;
1505
1506         present = snd_hda_codec_read(codec, 0x17, 0,
1507                                      AC_VERB_GET_PIN_SENSE, 0) &
1508                 AC_PINSENSE_PRESENCE;
1509         snd_hda_codec_write(codec, 0x14, 0,
1510                             AC_VERB_SET_CONNECT_SEL,
1511                             present ? 0x01 : 0x00);
1512 }
1513
1514 /* switch the current ADC according to the jack state */
1515 static void cxt5051_portc_automic(struct hda_codec *codec)
1516 {
1517         struct conexant_spec *spec = codec->spec;
1518         unsigned int present;
1519         hda_nid_t new_adc;
1520
1521         present = snd_hda_codec_read(codec, 0x18, 0,
1522                                      AC_VERB_GET_PIN_SENSE, 0) &
1523                 AC_PINSENSE_PRESENCE;
1524         if (present)
1525                 spec->cur_adc_idx = 1;
1526         else
1527                 spec->cur_adc_idx = 0;
1528         new_adc = spec->adc_nids[spec->cur_adc_idx];
1529         if (spec->cur_adc && spec->cur_adc != new_adc) {
1530                 /* stream is running, let's swap the current ADC */
1531                 snd_hda_codec_setup_stream(codec, spec->cur_adc, 0, 0, 0);
1532                 spec->cur_adc = new_adc;
1533                 snd_hda_codec_setup_stream(codec, new_adc,
1534                                            spec->cur_adc_stream_tag, 0,
1535                                            spec->cur_adc_format);
1536         }
1537 }
1538
1539 /* mute internal speaker if HP is plugged */
1540 static void cxt5051_hp_automute(struct hda_codec *codec)
1541 {
1542         struct conexant_spec *spec = codec->spec;
1543
1544         spec->hp_present = snd_hda_codec_read(codec, 0x16, 0,
1545                                      AC_VERB_GET_PIN_SENSE, 0) &
1546                 AC_PINSENSE_PRESENCE;
1547         cxt5051_update_speaker(codec);
1548 }
1549
1550 /* unsolicited event for HP jack sensing */
1551 static void cxt5051_hp_unsol_event(struct hda_codec *codec,
1552                                    unsigned int res)
1553 {
1554         switch (res >> 26) {
1555         case CONEXANT_HP_EVENT:
1556                 cxt5051_hp_automute(codec);
1557                 break;
1558         case CXT5051_PORTB_EVENT:
1559                 cxt5051_portb_automic(codec);
1560                 break;
1561         case CXT5051_PORTC_EVENT:
1562                 cxt5051_portc_automic(codec);
1563                 break;
1564         }
1565 }
1566
1567 static struct snd_kcontrol_new cxt5051_mixers[] = {
1568         HDA_CODEC_VOLUME("Internal Mic Volume", 0x14, 0x00, HDA_INPUT),
1569         HDA_CODEC_MUTE("Internal Mic Switch", 0x14, 0x00, HDA_INPUT),
1570         HDA_CODEC_VOLUME("External Mic Volume", 0x14, 0x01, HDA_INPUT),
1571         HDA_CODEC_MUTE("External Mic Switch", 0x14, 0x01, HDA_INPUT),
1572         HDA_CODEC_VOLUME("Docking Mic Volume", 0x15, 0x00, HDA_INPUT),
1573         HDA_CODEC_MUTE("Docking Mic Switch", 0x15, 0x00, HDA_INPUT),
1574         HDA_CODEC_VOLUME("Master Playback Volume", 0x10, 0x00, HDA_OUTPUT),
1575         {
1576                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1577                 .name = "Master Playback Switch",
1578                 .info = cxt_eapd_info,
1579                 .get = cxt_eapd_get,
1580                 .put = cxt5051_hp_master_sw_put,
1581                 .private_value = 0x1a,
1582         },
1583
1584         {}
1585 };
1586
1587 static struct snd_kcontrol_new cxt5051_hp_mixers[] = {
1588         HDA_CODEC_VOLUME("Internal Mic Volume", 0x14, 0x00, HDA_INPUT),
1589         HDA_CODEC_MUTE("Internal Mic Switch", 0x14, 0x00, HDA_INPUT),
1590         HDA_CODEC_VOLUME("External Mic Volume", 0x15, 0x00, HDA_INPUT),
1591         HDA_CODEC_MUTE("External Mic Switch", 0x15, 0x00, HDA_INPUT),
1592         HDA_CODEC_VOLUME("Master Playback Volume", 0x10, 0x00, HDA_OUTPUT),
1593         {
1594                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1595                 .name = "Master Playback Switch",
1596                 .info = cxt_eapd_info,
1597                 .get = cxt_eapd_get,
1598                 .put = cxt5051_hp_master_sw_put,
1599                 .private_value = 0x1a,
1600         },
1601
1602         {}
1603 };
1604
1605 static struct hda_verb cxt5051_init_verbs[] = {
1606         /* Line in, Mic */
1607         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x03},
1608         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1609         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x03},
1610         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1611         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
1612         {0x1d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x03},
1613         /* SPK  */
1614         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1615         {0x1a, AC_VERB_SET_CONNECT_SEL, 0x00},
1616         /* HP, Amp  */
1617         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1618         {0x16, AC_VERB_SET_CONNECT_SEL, 0x00},
1619         /* DAC1 */      
1620         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1621         /* Record selector: Int mic */
1622         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x44},
1623         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1) | 0x44},
1624         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x44},
1625         /* SPDIF route: PCM */
1626         {0x1c, AC_VERB_SET_CONNECT_SEL, 0x0},
1627         /* EAPD */
1628         {0x1a, AC_VERB_SET_EAPD_BTLENABLE, 0x2}, /* default on */ 
1629         {0x16, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN|CONEXANT_HP_EVENT},
1630         {0x17, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN|CXT5051_PORTB_EVENT},
1631         {0x18, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN|CXT5051_PORTC_EVENT},
1632         { } /* end */
1633 };
1634
1635 /* initialize jack-sensing, too */
1636 static int cxt5051_init(struct hda_codec *codec)
1637 {
1638         conexant_init(codec);
1639         if (codec->patch_ops.unsol_event) {
1640                 cxt5051_hp_automute(codec);
1641                 cxt5051_portb_automic(codec);
1642                 cxt5051_portc_automic(codec);
1643         }
1644         return 0;
1645 }
1646
1647
1648 enum {
1649         CXT5051_LAPTOP,  /* Laptops w/ EAPD support */
1650         CXT5051_HP,     /* no docking */
1651         CXT5051_MODELS
1652 };
1653
1654 static const char *cxt5051_models[CXT5051_MODELS] = {
1655         [CXT5051_LAPTOP]        = "laptop",
1656         [CXT5051_HP]            = "hp",
1657 };
1658
1659 static struct snd_pci_quirk cxt5051_cfg_tbl[] = {
1660         SND_PCI_QUIRK(0x14f1, 0x0101, "Conexant Reference board",
1661                       CXT5051_LAPTOP),
1662         SND_PCI_QUIRK(0x14f1, 0x5051, "HP Spartan 1.1", CXT5051_HP),
1663         {}
1664 };
1665
1666 static int patch_cxt5051(struct hda_codec *codec)
1667 {
1668         struct conexant_spec *spec;
1669         int board_config;
1670
1671         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1672         if (!spec)
1673                 return -ENOMEM;
1674         mutex_init(&spec->amp_mutex);
1675         codec->spec = spec;
1676
1677         codec->patch_ops = conexant_patch_ops;
1678         codec->patch_ops.init = cxt5051_init;
1679
1680         spec->multiout.max_channels = 2;
1681         spec->multiout.num_dacs = ARRAY_SIZE(cxt5051_dac_nids);
1682         spec->multiout.dac_nids = cxt5051_dac_nids;
1683         spec->multiout.dig_out_nid = CXT5051_SPDIF_OUT;
1684         spec->num_adc_nids = 1; /* not 2; via auto-mic switch */
1685         spec->adc_nids = cxt5051_adc_nids;
1686         spec->num_mixers = 1;
1687         spec->mixers[0] = cxt5051_mixers;
1688         spec->num_init_verbs = 1;
1689         spec->init_verbs[0] = cxt5051_init_verbs;
1690         spec->spdif_route = 0;
1691         spec->num_channel_mode = ARRAY_SIZE(cxt5051_modes);
1692         spec->channel_mode = cxt5051_modes;
1693         spec->cur_adc = 0;
1694         spec->cur_adc_idx = 0;
1695
1696         board_config = snd_hda_check_board_config(codec, CXT5051_MODELS,
1697                                                   cxt5051_models,
1698                                                   cxt5051_cfg_tbl);
1699         switch (board_config) {
1700         case CXT5051_HP:
1701                 codec->patch_ops.unsol_event = cxt5051_hp_unsol_event;
1702                 spec->mixers[0] = cxt5051_hp_mixers;
1703                 break;
1704         default:
1705         case CXT5051_LAPTOP:
1706                 codec->patch_ops.unsol_event = cxt5051_hp_unsol_event;
1707                 break;
1708         }
1709
1710         return 0;
1711 }
1712
1713
1714 /*
1715  */
1716
1717 struct hda_codec_preset snd_hda_preset_conexant[] = {
1718         { .id = 0x14f15045, .name = "CX20549 (Venice)",
1719           .patch = patch_cxt5045 },
1720         { .id = 0x14f15047, .name = "CX20551 (Waikiki)",
1721           .patch = patch_cxt5047 },
1722         { .id = 0x14f15051, .name = "CX20561 (Hermosa)",
1723           .patch = patch_cxt5051 },
1724         {} /* terminator */
1725 };