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