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