ALSA: hda: add quirk for Lenovo X200 laptop dock
[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 <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, 0x30d5, "HP 530", CXT5045_LAPTOP_HP530),
1006         SND_PCI_QUIRK_MASK(0x103c, 0xff00, 0x3000, "HP DV Series",
1007                            CXT5045_LAPTOP_HPSENSE),
1008         SND_PCI_QUIRK(0x1179, 0xff31, "Toshiba P105", CXT5045_LAPTOP_MICSENSE),
1009         SND_PCI_QUIRK(0x152d, 0x0753, "Benq R55E", CXT5045_BENQ),
1010         SND_PCI_QUIRK(0x1734, 0x10ad, "Fujitsu Si1520", CXT5045_LAPTOP_MICSENSE),
1011         SND_PCI_QUIRK(0x1734, 0x10cb, "Fujitsu Si3515", CXT5045_LAPTOP_HPMICSENSE),
1012         SND_PCI_QUIRK(0x1734, 0x110e, "Fujitsu V5505",
1013                       CXT5045_LAPTOP_HPMICSENSE),
1014         SND_PCI_QUIRK(0x1509, 0x1e40, "FIC", CXT5045_LAPTOP_HPMICSENSE),
1015         SND_PCI_QUIRK(0x1509, 0x2f05, "FIC", CXT5045_LAPTOP_HPMICSENSE),
1016         SND_PCI_QUIRK(0x1509, 0x2f06, "FIC", CXT5045_LAPTOP_HPMICSENSE),
1017         SND_PCI_QUIRK_MASK(0x1631, 0xff00, 0xc100, "Packard Bell",
1018                            CXT5045_LAPTOP_HPMICSENSE),
1019         SND_PCI_QUIRK(0x8086, 0x2111, "Conexant Reference board", CXT5045_LAPTOP_HPSENSE),
1020         {}
1021 };
1022
1023 static int patch_cxt5045(struct hda_codec *codec)
1024 {
1025         struct conexant_spec *spec;
1026         int board_config;
1027
1028         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1029         if (!spec)
1030                 return -ENOMEM;
1031         codec->spec = spec;
1032
1033         spec->multiout.max_channels = 2;
1034         spec->multiout.num_dacs = ARRAY_SIZE(cxt5045_dac_nids);
1035         spec->multiout.dac_nids = cxt5045_dac_nids;
1036         spec->multiout.dig_out_nid = CXT5045_SPDIF_OUT;
1037         spec->num_adc_nids = 1;
1038         spec->adc_nids = cxt5045_adc_nids;
1039         spec->capsrc_nids = cxt5045_capsrc_nids;
1040         spec->input_mux = &cxt5045_capture_source;
1041         spec->num_mixers = 1;
1042         spec->mixers[0] = cxt5045_mixers;
1043         spec->num_init_verbs = 1;
1044         spec->init_verbs[0] = cxt5045_init_verbs;
1045         spec->spdif_route = 0;
1046         spec->num_channel_mode = ARRAY_SIZE(cxt5045_modes),
1047         spec->channel_mode = cxt5045_modes,
1048
1049
1050         codec->patch_ops = conexant_patch_ops;
1051
1052         board_config = snd_hda_check_board_config(codec, CXT5045_MODELS,
1053                                                   cxt5045_models,
1054                                                   cxt5045_cfg_tbl);
1055         switch (board_config) {
1056         case CXT5045_LAPTOP_HPSENSE:
1057                 codec->patch_ops.unsol_event = cxt5045_hp_unsol_event;
1058                 spec->input_mux = &cxt5045_capture_source;
1059                 spec->num_init_verbs = 2;
1060                 spec->init_verbs[1] = cxt5045_hp_sense_init_verbs;
1061                 spec->mixers[0] = cxt5045_mixers;
1062                 codec->patch_ops.init = cxt5045_init;
1063                 break;
1064         case CXT5045_LAPTOP_MICSENSE:
1065                 codec->patch_ops.unsol_event = cxt5045_hp_unsol_event;
1066                 spec->input_mux = &cxt5045_capture_source;
1067                 spec->num_init_verbs = 2;
1068                 spec->init_verbs[1] = cxt5045_mic_sense_init_verbs;
1069                 spec->mixers[0] = cxt5045_mixers;
1070                 codec->patch_ops.init = cxt5045_init;
1071                 break;
1072         default:
1073         case CXT5045_LAPTOP_HPMICSENSE:
1074                 codec->patch_ops.unsol_event = cxt5045_hp_unsol_event;
1075                 spec->input_mux = &cxt5045_capture_source;
1076                 spec->num_init_verbs = 3;
1077                 spec->init_verbs[1] = cxt5045_hp_sense_init_verbs;
1078                 spec->init_verbs[2] = cxt5045_mic_sense_init_verbs;
1079                 spec->mixers[0] = cxt5045_mixers;
1080                 codec->patch_ops.init = cxt5045_init;
1081                 break;
1082         case CXT5045_BENQ:
1083                 codec->patch_ops.unsol_event = cxt5045_hp_unsol_event;
1084                 spec->input_mux = &cxt5045_capture_source_benq;
1085                 spec->num_init_verbs = 1;
1086                 spec->init_verbs[0] = cxt5045_benq_init_verbs;
1087                 spec->mixers[0] = cxt5045_mixers;
1088                 spec->mixers[1] = cxt5045_benq_mixers;
1089                 spec->num_mixers = 2;
1090                 codec->patch_ops.init = cxt5045_init;
1091                 break;
1092         case CXT5045_LAPTOP_HP530:
1093                 codec->patch_ops.unsol_event = cxt5045_hp_unsol_event;
1094                 spec->input_mux = &cxt5045_capture_source_hp530;
1095                 spec->num_init_verbs = 2;
1096                 spec->init_verbs[1] = cxt5045_hp_sense_init_verbs;
1097                 spec->mixers[0] = cxt5045_mixers_hp530;
1098                 codec->patch_ops.init = cxt5045_init;
1099                 break;
1100 #ifdef CONFIG_SND_DEBUG
1101         case CXT5045_TEST:
1102                 spec->input_mux = &cxt5045_test_capture_source;
1103                 spec->mixers[0] = cxt5045_test_mixer;
1104                 spec->init_verbs[0] = cxt5045_test_init_verbs;
1105                 break;
1106                 
1107 #endif  
1108         }
1109
1110         switch (codec->subsystem_id >> 16) {
1111         case 0x103c:
1112                 /* HP laptop has a really bad sound over 0dB on NID 0x17.
1113                  * Fix max PCM level to 0 dB
1114                  * (originall it has 0x2b steps with 0dB offset 0x14)
1115                  */
1116                 snd_hda_override_amp_caps(codec, 0x17, HDA_INPUT,
1117                                           (0x14 << AC_AMPCAP_OFFSET_SHIFT) |
1118                                           (0x14 << AC_AMPCAP_NUM_STEPS_SHIFT) |
1119                                           (0x05 << AC_AMPCAP_STEP_SIZE_SHIFT) |
1120                                           (1 << AC_AMPCAP_MUTE_SHIFT));
1121                 break;
1122         }
1123
1124         return 0;
1125 }
1126
1127
1128 /* Conexant 5047 specific */
1129 #define CXT5047_SPDIF_OUT       0x11
1130
1131 static hda_nid_t cxt5047_dac_nids[2] = { 0x10, 0x1c };
1132 static hda_nid_t cxt5047_adc_nids[1] = { 0x12 };
1133 static hda_nid_t cxt5047_capsrc_nids[1] = { 0x1a };
1134
1135 static struct hda_channel_mode cxt5047_modes[1] = {
1136         { 2, NULL },
1137 };
1138
1139 static struct hda_input_mux cxt5047_capture_source = {
1140         .num_items = 1,
1141         .items = {
1142                 { "Mic", 0x2 },
1143         }
1144 };
1145
1146 static struct hda_input_mux cxt5047_hp_capture_source = {
1147         .num_items = 1,
1148         .items = {
1149                 { "ExtMic", 0x2 },
1150         }
1151 };
1152
1153 static struct hda_input_mux cxt5047_toshiba_capture_source = {
1154         .num_items = 2,
1155         .items = {
1156                 { "ExtMic", 0x2 },
1157                 { "Line-In", 0x1 },
1158         }
1159 };
1160
1161 /* turn on/off EAPD (+ mute HP) as a master switch */
1162 static int cxt5047_hp_master_sw_put(struct snd_kcontrol *kcontrol,
1163                                     struct snd_ctl_elem_value *ucontrol)
1164 {
1165         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1166         struct conexant_spec *spec = codec->spec;
1167         unsigned int bits;
1168
1169         if (!cxt_eapd_put(kcontrol, ucontrol))
1170                 return 0;
1171
1172         /* toggle internal speakers mute depending of presence of
1173          * the headphone jack
1174          */
1175         bits = (!spec->hp_present && spec->cur_eapd) ? 0 : HDA_AMP_MUTE;
1176         snd_hda_codec_amp_stereo(codec, 0x1d, HDA_OUTPUT, 0,
1177                                  HDA_AMP_MUTE, bits);
1178         bits = spec->cur_eapd ? 0 : HDA_AMP_MUTE;
1179         snd_hda_codec_amp_stereo(codec, 0x13, HDA_OUTPUT, 0,
1180                                  HDA_AMP_MUTE, bits);
1181         return 1;
1182 }
1183
1184 /* bind volumes of both NID 0x13 (Headphones) and 0x1d (Speakers) */
1185 static struct hda_bind_ctls cxt5047_bind_master_vol = {
1186         .ops = &snd_hda_bind_vol,
1187         .values = {
1188                 HDA_COMPOSE_AMP_VAL(0x13, 3, 0, HDA_OUTPUT),
1189                 HDA_COMPOSE_AMP_VAL(0x1d, 3, 0, HDA_OUTPUT),
1190                 0
1191         },
1192 };
1193
1194 /* mute internal speaker if HP is plugged */
1195 static void cxt5047_hp_automute(struct hda_codec *codec)
1196 {
1197         struct conexant_spec *spec = codec->spec;
1198         unsigned int bits;
1199
1200         spec->hp_present = snd_hda_codec_read(codec, 0x13, 0,
1201                                      AC_VERB_GET_PIN_SENSE, 0) & 0x80000000;
1202
1203         bits = (spec->hp_present || !spec->cur_eapd) ? HDA_AMP_MUTE : 0;
1204         snd_hda_codec_amp_stereo(codec, 0x1d, HDA_OUTPUT, 0,
1205                                  HDA_AMP_MUTE, bits);
1206         /* Mute/Unmute PCM 2 for good measure - some systems need this */
1207         snd_hda_codec_amp_stereo(codec, 0x1c, HDA_OUTPUT, 0,
1208                                  HDA_AMP_MUTE, bits);
1209 }
1210
1211 /* mute internal speaker if HP is plugged */
1212 static void cxt5047_hp2_automute(struct hda_codec *codec)
1213 {
1214         struct conexant_spec *spec = codec->spec;
1215         unsigned int bits;
1216
1217         spec->hp_present = snd_hda_codec_read(codec, 0x13, 0,
1218                                      AC_VERB_GET_PIN_SENSE, 0) & 0x80000000;
1219
1220         bits = spec->hp_present ? HDA_AMP_MUTE : 0;
1221         snd_hda_codec_amp_stereo(codec, 0x1d, HDA_OUTPUT, 0,
1222                                  HDA_AMP_MUTE, bits);
1223         /* Mute/Unmute PCM 2 for good measure - some systems need this */
1224         snd_hda_codec_amp_stereo(codec, 0x1c, HDA_OUTPUT, 0,
1225                                  HDA_AMP_MUTE, bits);
1226 }
1227
1228 /* toggle input of built-in and mic jack appropriately */
1229 static void cxt5047_hp_automic(struct hda_codec *codec)
1230 {
1231         static struct hda_verb mic_jack_on[] = {
1232                 {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1233                 {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1234                 {}
1235         };
1236         static struct hda_verb mic_jack_off[] = {
1237                 {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1238                 {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1239                 {}
1240         };
1241         unsigned int present;
1242
1243         present = snd_hda_codec_read(codec, 0x15, 0,
1244                                      AC_VERB_GET_PIN_SENSE, 0) & 0x80000000;
1245         if (present)
1246                 snd_hda_sequence_write(codec, mic_jack_on);
1247         else
1248                 snd_hda_sequence_write(codec, mic_jack_off);
1249 }
1250
1251 /* unsolicited event for HP jack sensing */
1252 static void cxt5047_hp_unsol_event(struct hda_codec *codec,
1253                                   unsigned int res)
1254 {
1255         switch (res >> 26) {
1256         case CONEXANT_HP_EVENT:
1257                 cxt5047_hp_automute(codec);
1258                 break;
1259         case CONEXANT_MIC_EVENT:
1260                 cxt5047_hp_automic(codec);
1261                 break;
1262         }
1263 }
1264
1265 /* unsolicited event for HP jack sensing - non-EAPD systems */
1266 static void cxt5047_hp2_unsol_event(struct hda_codec *codec,
1267                                   unsigned int res)
1268 {
1269         res >>= 26;
1270         switch (res) {
1271         case CONEXANT_HP_EVENT:
1272                 cxt5047_hp2_automute(codec);
1273                 break;
1274         case CONEXANT_MIC_EVENT:
1275                 cxt5047_hp_automic(codec);
1276                 break;
1277         }
1278 }
1279
1280 static struct snd_kcontrol_new cxt5047_mixers[] = {
1281         HDA_CODEC_VOLUME("Mic Bypass Capture Volume", 0x19, 0x02, HDA_INPUT),
1282         HDA_CODEC_MUTE("Mic Bypass Capture Switch", 0x19, 0x02, HDA_INPUT),
1283         HDA_CODEC_VOLUME("Mic Gain Volume", 0x1a, 0x0, HDA_OUTPUT),
1284         HDA_CODEC_MUTE("Mic Gain Switch", 0x1a, 0x0, HDA_OUTPUT),
1285         HDA_CODEC_VOLUME("Capture Volume", 0x12, 0x03, HDA_INPUT),
1286         HDA_CODEC_MUTE("Capture Switch", 0x12, 0x03, HDA_INPUT),
1287         HDA_CODEC_VOLUME("PCM Volume", 0x10, 0x00, HDA_OUTPUT),
1288         HDA_CODEC_MUTE("PCM Switch", 0x10, 0x00, HDA_OUTPUT),
1289         HDA_CODEC_VOLUME("PCM-2 Volume", 0x1c, 0x00, HDA_OUTPUT),
1290         HDA_CODEC_MUTE("PCM-2 Switch", 0x1c, 0x00, HDA_OUTPUT),
1291         HDA_CODEC_VOLUME("Speaker Playback Volume", 0x1d, 0x00, HDA_OUTPUT),
1292         HDA_CODEC_MUTE("Speaker Playback Switch", 0x1d, 0x00, HDA_OUTPUT),
1293         HDA_CODEC_VOLUME("Headphone Playback Volume", 0x13, 0x00, HDA_OUTPUT),
1294         HDA_CODEC_MUTE("Headphone Playback Switch", 0x13, 0x00, HDA_OUTPUT),
1295
1296         {}
1297 };
1298
1299 static struct snd_kcontrol_new cxt5047_toshiba_mixers[] = {
1300         {
1301                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1302                 .name = "Capture Source",
1303                 .info = conexant_mux_enum_info,
1304                 .get = conexant_mux_enum_get,
1305                 .put = conexant_mux_enum_put
1306         },
1307         HDA_CODEC_VOLUME("Mic Bypass Capture Volume", 0x19, 0x02, HDA_INPUT),
1308         HDA_CODEC_MUTE("Mic Bypass Capture Switch", 0x19, 0x02, HDA_INPUT),
1309         HDA_CODEC_VOLUME("Capture Volume", 0x12, 0x03, HDA_INPUT),
1310         HDA_CODEC_MUTE("Capture Switch", 0x12, 0x03, HDA_INPUT),
1311         HDA_CODEC_VOLUME("PCM Volume", 0x10, 0x00, HDA_OUTPUT),
1312         HDA_CODEC_MUTE("PCM Switch", 0x10, 0x00, HDA_OUTPUT),
1313         HDA_BIND_VOL("Master Playback Volume", &cxt5047_bind_master_vol),
1314         {
1315                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1316                 .name = "Master Playback Switch",
1317                 .info = cxt_eapd_info,
1318                 .get = cxt_eapd_get,
1319                 .put = cxt5047_hp_master_sw_put,
1320                 .private_value = 0x13,
1321         },
1322
1323         {}
1324 };
1325
1326 static struct snd_kcontrol_new cxt5047_hp_mixers[] = {
1327         {
1328                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1329                 .name = "Capture Source",
1330                 .info = conexant_mux_enum_info,
1331                 .get = conexant_mux_enum_get,
1332                 .put = conexant_mux_enum_put
1333         },
1334         HDA_CODEC_VOLUME("Mic Bypass Capture Volume", 0x19, 0x02, HDA_INPUT),
1335         HDA_CODEC_MUTE("Mic Bypass Capture Switch", 0x19,0x02,HDA_INPUT),
1336         HDA_CODEC_VOLUME("Capture Volume", 0x12, 0x03, HDA_INPUT),
1337         HDA_CODEC_MUTE("Capture Switch", 0x12, 0x03, HDA_INPUT),
1338         HDA_CODEC_VOLUME("PCM Volume", 0x10, 0x00, HDA_OUTPUT),
1339         HDA_CODEC_MUTE("PCM Switch", 0x10, 0x00, HDA_OUTPUT),
1340         HDA_CODEC_VOLUME("Master Playback Volume", 0x13, 0x00, HDA_OUTPUT),
1341         {
1342                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1343                 .name = "Master Playback Switch",
1344                 .info = cxt_eapd_info,
1345                 .get = cxt_eapd_get,
1346                 .put = cxt5047_hp_master_sw_put,
1347                 .private_value = 0x13,
1348         },
1349         { } /* end */
1350 };
1351
1352 static struct hda_verb cxt5047_init_verbs[] = {
1353         /* Line in, Mic, Built-in Mic */
1354         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
1355         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN|AC_PINCTL_VREF_50 },
1356         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN|AC_PINCTL_VREF_50 },
1357         /* HP, Speaker  */
1358         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
1359         {0x13, AC_VERB_SET_CONNECT_SEL,0x1},
1360         {0x1d, AC_VERB_SET_CONNECT_SEL,0x0},
1361         /* Record selector: Mic */
1362         {0x12, AC_VERB_SET_CONNECT_SEL,0x03},
1363         {0x19, AC_VERB_SET_AMP_GAIN_MUTE,
1364          AC_AMP_SET_INPUT|AC_AMP_SET_RIGHT|AC_AMP_SET_LEFT|0x17},
1365         {0x1A, AC_VERB_SET_CONNECT_SEL,0x02},
1366         {0x1A, AC_VERB_SET_AMP_GAIN_MUTE,
1367          AC_AMP_SET_OUTPUT|AC_AMP_SET_RIGHT|AC_AMP_SET_LEFT|0x00},
1368         {0x1A, AC_VERB_SET_AMP_GAIN_MUTE,
1369          AC_AMP_SET_OUTPUT|AC_AMP_SET_RIGHT|AC_AMP_SET_LEFT|0x03},
1370         /* SPDIF route: PCM */
1371         { 0x18, AC_VERB_SET_CONNECT_SEL, 0x0 },
1372         /* Enable unsolicited events */
1373         {0x13, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_HP_EVENT},
1374         {0x15, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_MIC_EVENT},
1375         { } /* end */
1376 };
1377
1378 /* configuration for Toshiba Laptops */
1379 static struct hda_verb cxt5047_toshiba_init_verbs[] = {
1380         {0x13, AC_VERB_SET_EAPD_BTLENABLE, 0x0 }, /* default on */
1381         /* pin sensing on HP and Mic jacks */
1382         {0x13, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_HP_EVENT},
1383         {0x15, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_MIC_EVENT},
1384         /* Speaker routing */
1385         {0x1d, AC_VERB_SET_CONNECT_SEL,0x1},
1386         {}
1387 };
1388
1389 /* configuration for HP Laptops */
1390 static struct hda_verb cxt5047_hp_init_verbs[] = {
1391         /* pin sensing on HP jack */
1392         {0x13, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_HP_EVENT},
1393         /* 0x13 is actually shared by both HP and speaker;
1394          * setting the connection to 0 (=0x19) makes the master volume control
1395          * working mysteriouslly...
1396          */
1397         {0x13, AC_VERB_SET_CONNECT_SEL, 0x0},
1398         /* Record selector: Ext Mic */
1399         {0x12, AC_VERB_SET_CONNECT_SEL,0x03},
1400         {0x19, AC_VERB_SET_AMP_GAIN_MUTE,
1401          AC_AMP_SET_INPUT|AC_AMP_SET_RIGHT|AC_AMP_SET_LEFT|0x17},
1402         /* Speaker routing */
1403         {0x1d, AC_VERB_SET_CONNECT_SEL,0x1},
1404         {}
1405 };
1406
1407 /* Test configuration for debugging, modelled after the ALC260 test
1408  * configuration.
1409  */
1410 #ifdef CONFIG_SND_DEBUG
1411 static struct hda_input_mux cxt5047_test_capture_source = {
1412         .num_items = 4,
1413         .items = {
1414                 { "LINE1 pin", 0x0 },
1415                 { "MIC1 pin", 0x1 },
1416                 { "MIC2 pin", 0x2 },
1417                 { "CD pin", 0x3 },
1418         },
1419 };
1420
1421 static struct snd_kcontrol_new cxt5047_test_mixer[] = {
1422
1423         /* Output only controls */
1424         HDA_CODEC_VOLUME("OutAmp-1 Volume", 0x10, 0x0, HDA_OUTPUT),
1425         HDA_CODEC_MUTE("OutAmp-1 Switch", 0x10,0x0, HDA_OUTPUT),
1426         HDA_CODEC_VOLUME("OutAmp-2 Volume", 0x1c, 0x0, HDA_OUTPUT),
1427         HDA_CODEC_MUTE("OutAmp-2 Switch", 0x1c, 0x0, HDA_OUTPUT),
1428         HDA_CODEC_VOLUME("Speaker Playback Volume", 0x1d, 0x0, HDA_OUTPUT),
1429         HDA_CODEC_MUTE("Speaker Playback Switch", 0x1d, 0x0, HDA_OUTPUT),
1430         HDA_CODEC_VOLUME("HeadPhone Playback Volume", 0x13, 0x0, HDA_OUTPUT),
1431         HDA_CODEC_MUTE("HeadPhone Playback Switch", 0x13, 0x0, HDA_OUTPUT),
1432         HDA_CODEC_VOLUME("Line1-Out Playback Volume", 0x14, 0x0, HDA_OUTPUT),
1433         HDA_CODEC_MUTE("Line1-Out Playback Switch", 0x14, 0x0, HDA_OUTPUT),
1434         HDA_CODEC_VOLUME("Line2-Out Playback Volume", 0x15, 0x0, HDA_OUTPUT),
1435         HDA_CODEC_MUTE("Line2-Out Playback Switch", 0x15, 0x0, HDA_OUTPUT),
1436
1437         /* Modes for retasking pin widgets */
1438         CXT_PIN_MODE("LINE1 pin mode", 0x14, CXT_PIN_DIR_INOUT),
1439         CXT_PIN_MODE("MIC1 pin mode", 0x15, CXT_PIN_DIR_INOUT),
1440
1441         /* EAPD Switch Control */
1442         CXT_EAPD_SWITCH("External Amplifier", 0x13, 0x0),
1443
1444         /* Loopback mixer controls */
1445         HDA_CODEC_VOLUME("MIC1 Playback Volume", 0x12, 0x01, HDA_INPUT),
1446         HDA_CODEC_MUTE("MIC1 Playback Switch", 0x12, 0x01, HDA_INPUT),
1447         HDA_CODEC_VOLUME("MIC2 Playback Volume", 0x12, 0x02, HDA_INPUT),
1448         HDA_CODEC_MUTE("MIC2 Playback Switch", 0x12, 0x02, HDA_INPUT),
1449         HDA_CODEC_VOLUME("LINE Playback Volume", 0x12, 0x0, HDA_INPUT),
1450         HDA_CODEC_MUTE("LINE Playback Switch", 0x12, 0x0, HDA_INPUT),
1451         HDA_CODEC_VOLUME("CD Playback Volume", 0x12, 0x04, HDA_INPUT),
1452         HDA_CODEC_MUTE("CD Playback Switch", 0x12, 0x04, HDA_INPUT),
1453
1454         HDA_CODEC_VOLUME("Capture-1 Volume", 0x19, 0x0, HDA_INPUT),
1455         HDA_CODEC_MUTE("Capture-1 Switch", 0x19, 0x0, HDA_INPUT),
1456         HDA_CODEC_VOLUME("Capture-2 Volume", 0x19, 0x1, HDA_INPUT),
1457         HDA_CODEC_MUTE("Capture-2 Switch", 0x19, 0x1, HDA_INPUT),
1458         HDA_CODEC_VOLUME("Capture-3 Volume", 0x19, 0x2, HDA_INPUT),
1459         HDA_CODEC_MUTE("Capture-3 Switch", 0x19, 0x2, HDA_INPUT),
1460         HDA_CODEC_VOLUME("Capture-4 Volume", 0x19, 0x3, HDA_INPUT),
1461         HDA_CODEC_MUTE("Capture-4 Switch", 0x19, 0x3, HDA_INPUT),
1462         {
1463                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1464                 .name = "Input Source",
1465                 .info = conexant_mux_enum_info,
1466                 .get = conexant_mux_enum_get,
1467                 .put = conexant_mux_enum_put,
1468         },
1469         HDA_CODEC_VOLUME("Input-1 Volume", 0x1a, 0x0, HDA_INPUT),
1470         HDA_CODEC_MUTE("Input-1 Switch", 0x1a, 0x0, HDA_INPUT),
1471         HDA_CODEC_VOLUME("Input-2 Volume", 0x1a, 0x1, HDA_INPUT),
1472         HDA_CODEC_MUTE("Input-2 Switch", 0x1a, 0x1, HDA_INPUT),
1473         HDA_CODEC_VOLUME("Input-3 Volume", 0x1a, 0x2, HDA_INPUT),
1474         HDA_CODEC_MUTE("Input-3 Switch", 0x1a, 0x2, HDA_INPUT),
1475         HDA_CODEC_VOLUME("Input-4 Volume", 0x1a, 0x3, HDA_INPUT),
1476         HDA_CODEC_MUTE("Input-4 Switch", 0x1a, 0x3, HDA_INPUT),
1477         HDA_CODEC_VOLUME("Input-5 Volume", 0x1a, 0x4, HDA_INPUT),
1478         HDA_CODEC_MUTE("Input-5 Switch", 0x1a, 0x4, HDA_INPUT),
1479
1480         { } /* end */
1481 };
1482
1483 static struct hda_verb cxt5047_test_init_verbs[] = {
1484         /* Enable retasking pins as output, initially without power amp */
1485         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1486         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1487         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1488
1489         /* Disable digital (SPDIF) pins initially, but users can enable
1490          * them via a mixer switch.  In the case of SPDIF-out, this initverb
1491          * payload also sets the generation to 0, output to be in "consumer"
1492          * PCM format, copyright asserted, no pre-emphasis and no validity
1493          * control.
1494          */
1495         {0x18, AC_VERB_SET_DIGI_CONVERT_1, 0},
1496
1497         /* Ensure mic1, mic2, line1 pin widgets take input from the 
1498          * OUT1 sum bus when acting as an output.
1499          */
1500         {0x1a, AC_VERB_SET_CONNECT_SEL, 0},
1501         {0x1b, AC_VERB_SET_CONNECT_SEL, 0},
1502
1503         /* Start with output sum widgets muted and their output gains at min */
1504         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1505         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
1506
1507         /* Unmute retasking pin widget output buffers since the default
1508          * state appears to be output.  As the pin mode is changed by the
1509          * user the pin mode control will take care of enabling the pin's
1510          * input/output buffers as needed.
1511          */
1512         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1513         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1514         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1515
1516         /* Mute capture amp left and right */
1517         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1518
1519         /* Set ADC connection select to match default mixer setting (mic1
1520          * pin)
1521          */
1522         {0x12, AC_VERB_SET_CONNECT_SEL, 0x00},
1523
1524         /* Mute all inputs to mixer widget (even unconnected ones) */
1525         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)}, /* mic1 pin */
1526         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)}, /* mic2 pin */
1527         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)}, /* line1 pin */
1528         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)}, /* line2 pin */
1529         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)}, /* CD pin */
1530         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(5)}, /* Beep-gen pin */
1531         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(6)}, /* Line-out pin */
1532         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(7)}, /* HP-pin pin */
1533
1534         { }
1535 };
1536 #endif
1537
1538
1539 /* initialize jack-sensing, too */
1540 static int cxt5047_hp_init(struct hda_codec *codec)
1541 {
1542         conexant_init(codec);
1543         cxt5047_hp_automute(codec);
1544         return 0;
1545 }
1546
1547
1548 enum {
1549         CXT5047_LAPTOP,         /* Laptops w/o EAPD support */
1550         CXT5047_LAPTOP_HP,      /* Some HP laptops */
1551         CXT5047_LAPTOP_EAPD,    /* Laptops with EAPD support */
1552 #ifdef CONFIG_SND_DEBUG
1553         CXT5047_TEST,
1554 #endif
1555         CXT5047_MODELS
1556 };
1557
1558 static const char *cxt5047_models[CXT5047_MODELS] = {
1559         [CXT5047_LAPTOP]        = "laptop",
1560         [CXT5047_LAPTOP_HP]     = "laptop-hp",
1561         [CXT5047_LAPTOP_EAPD]   = "laptop-eapd",
1562 #ifdef CONFIG_SND_DEBUG
1563         [CXT5047_TEST]          = "test",
1564 #endif
1565 };
1566
1567 static struct snd_pci_quirk cxt5047_cfg_tbl[] = {
1568         SND_PCI_QUIRK(0x103c, 0x30a5, "HP DV5200T/DV8000T", CXT5047_LAPTOP_HP),
1569         SND_PCI_QUIRK_MASK(0x103c, 0xff00, 0x3000, "HP DV Series",
1570                            CXT5047_LAPTOP),
1571         SND_PCI_QUIRK(0x1179, 0xff31, "Toshiba P100", CXT5047_LAPTOP_EAPD),
1572         {}
1573 };
1574
1575 static int patch_cxt5047(struct hda_codec *codec)
1576 {
1577         struct conexant_spec *spec;
1578         int board_config;
1579
1580         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1581         if (!spec)
1582                 return -ENOMEM;
1583         codec->spec = spec;
1584
1585         spec->multiout.max_channels = 2;
1586         spec->multiout.num_dacs = ARRAY_SIZE(cxt5047_dac_nids);
1587         spec->multiout.dac_nids = cxt5047_dac_nids;
1588         spec->multiout.dig_out_nid = CXT5047_SPDIF_OUT;
1589         spec->num_adc_nids = 1;
1590         spec->adc_nids = cxt5047_adc_nids;
1591         spec->capsrc_nids = cxt5047_capsrc_nids;
1592         spec->input_mux = &cxt5047_capture_source;
1593         spec->num_mixers = 1;
1594         spec->mixers[0] = cxt5047_mixers;
1595         spec->num_init_verbs = 1;
1596         spec->init_verbs[0] = cxt5047_init_verbs;
1597         spec->spdif_route = 0;
1598         spec->num_channel_mode = ARRAY_SIZE(cxt5047_modes),
1599         spec->channel_mode = cxt5047_modes,
1600
1601         codec->patch_ops = conexant_patch_ops;
1602
1603         board_config = snd_hda_check_board_config(codec, CXT5047_MODELS,
1604                                                   cxt5047_models,
1605                                                   cxt5047_cfg_tbl);
1606         switch (board_config) {
1607         case CXT5047_LAPTOP:
1608                 codec->patch_ops.unsol_event = cxt5047_hp2_unsol_event;
1609                 break;
1610         case CXT5047_LAPTOP_HP:
1611                 spec->input_mux = &cxt5047_hp_capture_source;
1612                 spec->num_init_verbs = 2;
1613                 spec->init_verbs[1] = cxt5047_hp_init_verbs;
1614                 spec->mixers[0] = cxt5047_hp_mixers;
1615                 codec->patch_ops.unsol_event = cxt5047_hp_unsol_event;
1616                 codec->patch_ops.init = cxt5047_hp_init;
1617                 break;
1618         case CXT5047_LAPTOP_EAPD:
1619                 spec->input_mux = &cxt5047_toshiba_capture_source;
1620                 spec->num_init_verbs = 2;
1621                 spec->init_verbs[1] = cxt5047_toshiba_init_verbs;
1622                 spec->mixers[0] = cxt5047_toshiba_mixers;
1623                 codec->patch_ops.unsol_event = cxt5047_hp_unsol_event;
1624                 break;
1625 #ifdef CONFIG_SND_DEBUG
1626         case CXT5047_TEST:
1627                 spec->input_mux = &cxt5047_test_capture_source;
1628                 spec->mixers[0] = cxt5047_test_mixer;
1629                 spec->init_verbs[0] = cxt5047_test_init_verbs;
1630                 codec->patch_ops.unsol_event = cxt5047_hp_unsol_event;
1631 #endif  
1632         }
1633         return 0;
1634 }
1635
1636 /* Conexant 5051 specific */
1637 static hda_nid_t cxt5051_dac_nids[1] = { 0x10 };
1638 static hda_nid_t cxt5051_adc_nids[2] = { 0x14, 0x15 };
1639
1640 static struct hda_channel_mode cxt5051_modes[1] = {
1641         { 2, NULL },
1642 };
1643
1644 static void cxt5051_update_speaker(struct hda_codec *codec)
1645 {
1646         struct conexant_spec *spec = codec->spec;
1647         unsigned int pinctl;
1648         pinctl = (!spec->hp_present && spec->cur_eapd) ? PIN_OUT : 0;
1649         snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
1650                             pinctl);
1651 }
1652
1653 /* turn on/off EAPD (+ mute HP) as a master switch */
1654 static int cxt5051_hp_master_sw_put(struct snd_kcontrol *kcontrol,
1655                                     struct snd_ctl_elem_value *ucontrol)
1656 {
1657         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1658
1659         if (!cxt_eapd_put(kcontrol, ucontrol))
1660                 return 0;
1661         cxt5051_update_speaker(codec);
1662         return 1;
1663 }
1664
1665 /* toggle input of built-in and mic jack appropriately */
1666 static void cxt5051_portb_automic(struct hda_codec *codec)
1667 {
1668         unsigned int present;
1669
1670         present = snd_hda_codec_read(codec, 0x17, 0,
1671                                      AC_VERB_GET_PIN_SENSE, 0) &
1672                 AC_PINSENSE_PRESENCE;
1673         snd_hda_codec_write(codec, 0x14, 0,
1674                             AC_VERB_SET_CONNECT_SEL,
1675                             present ? 0x01 : 0x00);
1676 }
1677
1678 /* switch the current ADC according to the jack state */
1679 static void cxt5051_portc_automic(struct hda_codec *codec)
1680 {
1681         struct conexant_spec *spec = codec->spec;
1682         unsigned int present;
1683         hda_nid_t new_adc;
1684
1685         present = snd_hda_codec_read(codec, 0x18, 0,
1686                                      AC_VERB_GET_PIN_SENSE, 0) &
1687                 AC_PINSENSE_PRESENCE;
1688         if (present)
1689                 spec->cur_adc_idx = 1;
1690         else
1691                 spec->cur_adc_idx = 0;
1692         new_adc = spec->adc_nids[spec->cur_adc_idx];
1693         if (spec->cur_adc && spec->cur_adc != new_adc) {
1694                 /* stream is running, let's swap the current ADC */
1695                 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
1696                 spec->cur_adc = new_adc;
1697                 snd_hda_codec_setup_stream(codec, new_adc,
1698                                            spec->cur_adc_stream_tag, 0,
1699                                            spec->cur_adc_format);
1700         }
1701 }
1702
1703 /* mute internal speaker if HP is plugged */
1704 static void cxt5051_hp_automute(struct hda_codec *codec)
1705 {
1706         struct conexant_spec *spec = codec->spec;
1707
1708         spec->hp_present = snd_hda_codec_read(codec, 0x16, 0,
1709                                      AC_VERB_GET_PIN_SENSE, 0) &
1710                 AC_PINSENSE_PRESENCE;
1711         cxt5051_update_speaker(codec);
1712 }
1713
1714 /* unsolicited event for HP jack sensing */
1715 static void cxt5051_hp_unsol_event(struct hda_codec *codec,
1716                                    unsigned int res)
1717 {
1718         int nid = (res & AC_UNSOL_RES_SUBTAG) >> 20;
1719         switch (res >> 26) {
1720         case CONEXANT_HP_EVENT:
1721                 cxt5051_hp_automute(codec);
1722                 break;
1723         case CXT5051_PORTB_EVENT:
1724                 cxt5051_portb_automic(codec);
1725                 break;
1726         case CXT5051_PORTC_EVENT:
1727                 cxt5051_portc_automic(codec);
1728                 break;
1729         }
1730         conexant_report_jack(codec, nid);
1731 }
1732
1733 static struct snd_kcontrol_new cxt5051_mixers[] = {
1734         HDA_CODEC_VOLUME("Internal Mic Volume", 0x14, 0x00, HDA_INPUT),
1735         HDA_CODEC_MUTE("Internal Mic Switch", 0x14, 0x00, HDA_INPUT),
1736         HDA_CODEC_VOLUME("External Mic Volume", 0x14, 0x01, HDA_INPUT),
1737         HDA_CODEC_MUTE("External Mic Switch", 0x14, 0x01, HDA_INPUT),
1738         HDA_CODEC_VOLUME("Docking Mic Volume", 0x15, 0x00, HDA_INPUT),
1739         HDA_CODEC_MUTE("Docking Mic Switch", 0x15, 0x00, HDA_INPUT),
1740         HDA_CODEC_VOLUME("Master Playback Volume", 0x10, 0x00, HDA_OUTPUT),
1741         {
1742                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1743                 .name = "Master Playback Switch",
1744                 .info = cxt_eapd_info,
1745                 .get = cxt_eapd_get,
1746                 .put = cxt5051_hp_master_sw_put,
1747                 .private_value = 0x1a,
1748         },
1749
1750         {}
1751 };
1752
1753 static struct snd_kcontrol_new cxt5051_hp_mixers[] = {
1754         HDA_CODEC_VOLUME("Internal Mic Volume", 0x14, 0x00, HDA_INPUT),
1755         HDA_CODEC_MUTE("Internal Mic Switch", 0x14, 0x00, HDA_INPUT),
1756         HDA_CODEC_VOLUME("External Mic Volume", 0x15, 0x00, HDA_INPUT),
1757         HDA_CODEC_MUTE("External Mic Switch", 0x15, 0x00, HDA_INPUT),
1758         HDA_CODEC_VOLUME("Master Playback Volume", 0x10, 0x00, HDA_OUTPUT),
1759         {
1760                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1761                 .name = "Master Playback Switch",
1762                 .info = cxt_eapd_info,
1763                 .get = cxt_eapd_get,
1764                 .put = cxt5051_hp_master_sw_put,
1765                 .private_value = 0x1a,
1766         },
1767
1768         {}
1769 };
1770
1771 static struct hda_verb cxt5051_init_verbs[] = {
1772         /* Line in, Mic */
1773         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x03},
1774         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1775         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x03},
1776         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1777         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
1778         {0x1d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x03},
1779         /* SPK  */
1780         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1781         {0x1a, AC_VERB_SET_CONNECT_SEL, 0x00},
1782         /* HP, Amp  */
1783         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1784         {0x16, AC_VERB_SET_CONNECT_SEL, 0x00},
1785         /* DAC1 */      
1786         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1787         /* Record selector: Int mic */
1788         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x44},
1789         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1) | 0x44},
1790         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x44},
1791         /* SPDIF route: PCM */
1792         {0x1c, AC_VERB_SET_CONNECT_SEL, 0x0},
1793         /* EAPD */
1794         {0x1a, AC_VERB_SET_EAPD_BTLENABLE, 0x2}, /* default on */ 
1795         {0x16, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN|CONEXANT_HP_EVENT},
1796         {0x17, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN|CXT5051_PORTB_EVENT},
1797         {0x18, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN|CXT5051_PORTC_EVENT},
1798         { } /* end */
1799 };
1800
1801 static struct hda_verb cxt5051_lenovo_x200_init_verbs[] = {
1802         /* Line in, Mic */
1803         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x03},
1804         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1805         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x03},
1806         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1807         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
1808         {0x1d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x03},
1809         /* SPK  */
1810         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1811         {0x1a, AC_VERB_SET_CONNECT_SEL, 0x00},
1812         /* HP, Amp  */
1813         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1814         {0x16, AC_VERB_SET_CONNECT_SEL, 0x00},
1815         /* Docking HP */
1816         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1817         {0x19, AC_VERB_SET_CONNECT_SEL, 0x00},
1818         /* DAC1 */
1819         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1820         /* Record selector: Int mic */
1821         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x44},
1822         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1) | 0x44},
1823         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x44},
1824         /* SPDIF route: PCM */
1825         {0x1c, AC_VERB_SET_CONNECT_SEL, 0x0},
1826         /* EAPD */
1827         {0x1a, AC_VERB_SET_EAPD_BTLENABLE, 0x2}, /* default on */
1828         {0x16, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN|CONEXANT_HP_EVENT},
1829         {0x17, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN|CXT5051_PORTB_EVENT},
1830         {0x18, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN|CXT5051_PORTC_EVENT},
1831         {0x19, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN|CONEXANT_HP_EVENT},
1832         { } /* end */
1833 };
1834
1835 /* initialize jack-sensing, too */
1836 static int cxt5051_init(struct hda_codec *codec)
1837 {
1838         conexant_init(codec);
1839         conexant_init_jacks(codec);
1840         if (codec->patch_ops.unsol_event) {
1841                 cxt5051_hp_automute(codec);
1842                 cxt5051_portb_automic(codec);
1843                 cxt5051_portc_automic(codec);
1844         }
1845         return 0;
1846 }
1847
1848
1849 enum {
1850         CXT5051_LAPTOP,  /* Laptops w/ EAPD support */
1851         CXT5051_HP,     /* no docking */
1852         CXT5051_LENOVO_X200,    /* Lenovo X200 laptop */
1853         CXT5051_MODELS
1854 };
1855
1856 static const char *cxt5051_models[CXT5051_MODELS] = {
1857         [CXT5051_LAPTOP]        = "laptop",
1858         [CXT5051_HP]            = "hp",
1859         [CXT5051_LENOVO_X200]   = "lenovo-x200",
1860 };
1861
1862 static struct snd_pci_quirk cxt5051_cfg_tbl[] = {
1863         SND_PCI_QUIRK(0x14f1, 0x0101, "Conexant Reference board",
1864                       CXT5051_LAPTOP),
1865         SND_PCI_QUIRK(0x14f1, 0x5051, "HP Spartan 1.1", CXT5051_HP),
1866         SND_PCI_QUIRK(0x17aa, 0x20f2, "Lenovo X200", CXT5051_LENOVO_X200),
1867         {}
1868 };
1869
1870 static int patch_cxt5051(struct hda_codec *codec)
1871 {
1872         struct conexant_spec *spec;
1873         int board_config;
1874
1875         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1876         if (!spec)
1877                 return -ENOMEM;
1878         codec->spec = spec;
1879
1880         codec->patch_ops = conexant_patch_ops;
1881         codec->patch_ops.init = cxt5051_init;
1882
1883         spec->multiout.max_channels = 2;
1884         spec->multiout.num_dacs = ARRAY_SIZE(cxt5051_dac_nids);
1885         spec->multiout.dac_nids = cxt5051_dac_nids;
1886         spec->multiout.dig_out_nid = CXT5051_SPDIF_OUT;
1887         spec->num_adc_nids = 1; /* not 2; via auto-mic switch */
1888         spec->adc_nids = cxt5051_adc_nids;
1889         spec->num_mixers = 1;
1890         spec->mixers[0] = cxt5051_mixers;
1891         spec->num_init_verbs = 1;
1892         spec->init_verbs[0] = cxt5051_init_verbs;
1893         spec->spdif_route = 0;
1894         spec->num_channel_mode = ARRAY_SIZE(cxt5051_modes);
1895         spec->channel_mode = cxt5051_modes;
1896         spec->cur_adc = 0;
1897         spec->cur_adc_idx = 0;
1898
1899         board_config = snd_hda_check_board_config(codec, CXT5051_MODELS,
1900                                                   cxt5051_models,
1901                                                   cxt5051_cfg_tbl);
1902         switch (board_config) {
1903         case CXT5051_HP:
1904                 codec->patch_ops.unsol_event = cxt5051_hp_unsol_event;
1905                 spec->mixers[0] = cxt5051_hp_mixers;
1906                 break;
1907         case CXT5051_LENOVO_X200:
1908                 spec->init_verbs[0] = cxt5051_lenovo_x200_init_verbs;
1909                 /* fallthru */
1910         default:
1911         case CXT5051_LAPTOP:
1912                 codec->patch_ops.unsol_event = cxt5051_hp_unsol_event;
1913                 break;
1914         }
1915
1916         return 0;
1917 }
1918
1919
1920 /*
1921  */
1922
1923 static struct hda_codec_preset snd_hda_preset_conexant[] = {
1924         { .id = 0x14f15045, .name = "CX20549 (Venice)",
1925           .patch = patch_cxt5045 },
1926         { .id = 0x14f15047, .name = "CX20551 (Waikiki)",
1927           .patch = patch_cxt5047 },
1928         { .id = 0x14f15051, .name = "CX20561 (Hermosa)",
1929           .patch = patch_cxt5051 },
1930         {} /* terminator */
1931 };
1932
1933 MODULE_ALIAS("snd-hda-codec-id:14f15045");
1934 MODULE_ALIAS("snd-hda-codec-id:14f15047");
1935 MODULE_ALIAS("snd-hda-codec-id:14f15051");
1936
1937 MODULE_LICENSE("GPL");
1938 MODULE_DESCRIPTION("Conexant HD-audio codec");
1939
1940 static struct hda_codec_preset_list conexant_list = {
1941         .preset = snd_hda_preset_conexant,
1942         .owner = THIS_MODULE,
1943 };
1944
1945 static int __init patch_conexant_init(void)
1946 {
1947         return snd_hda_add_codec_preset(&conexant_list);
1948 }
1949
1950 static void __exit patch_conexant_exit(void)
1951 {
1952         snd_hda_delete_codec_preset(&conexant_list);
1953 }
1954
1955 module_init(patch_conexant_init)
1956 module_exit(patch_conexant_exit)