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