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