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