]> git.karo-electronics.de Git - karo-tx-linux.git/blob - sound/pci/hda/patch_conexant.c
ALSA: hda - Build boost controls from selector widget in Cxt auto-parser
[karo-tx-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 <sound/jack.h>
29
30 #include "hda_codec.h"
31 #include "hda_local.h"
32 #include "hda_beep.h"
33
34 #define CXT_PIN_DIR_IN              0x00
35 #define CXT_PIN_DIR_OUT             0x01
36 #define CXT_PIN_DIR_INOUT           0x02
37 #define CXT_PIN_DIR_IN_NOMICBIAS    0x03
38 #define CXT_PIN_DIR_INOUT_NOMICBIAS 0x04
39
40 #define CONEXANT_HP_EVENT       0x37
41 #define CONEXANT_MIC_EVENT      0x38
42
43 /* Conexant 5051 specific */
44
45 #define CXT5051_SPDIF_OUT       0x12
46 #define CXT5051_PORTB_EVENT     0x38
47 #define CXT5051_PORTC_EVENT     0x39
48
49 #define AUTO_MIC_PORTB          (1 << 1)
50 #define AUTO_MIC_PORTC          (1 << 2)
51
52 struct pin_dac_pair {
53         hda_nid_t pin;
54         hda_nid_t dac;
55         int type;
56 };
57
58 struct conexant_spec {
59
60         const struct snd_kcontrol_new *mixers[5];
61         int num_mixers;
62         hda_nid_t vmaster_nid;
63
64         const struct hda_verb *init_verbs[5];   /* initialization verbs
65                                                  * don't forget NULL
66                                                  * termination!
67                                                  */
68         unsigned int num_init_verbs;
69
70         /* playback */
71         struct hda_multi_out multiout;  /* playback set-up
72                                          * max_channels, dacs must be set
73                                          * dig_out_nid and hp_nid are optional
74                                          */
75         unsigned int cur_eapd;
76         unsigned int hp_present;
77         unsigned int auto_mic;
78         int auto_mic_ext;               /* imux_pins[] index for ext mic */
79         unsigned int need_dac_fix;
80         hda_nid_t slave_dig_outs[2];
81
82         /* capture */
83         unsigned int num_adc_nids;
84         const hda_nid_t *adc_nids;
85         hda_nid_t dig_in_nid;           /* digital-in NID; optional */
86
87         unsigned int cur_adc_idx;
88         hda_nid_t cur_adc;
89         unsigned int cur_adc_stream_tag;
90         unsigned int cur_adc_format;
91
92         const struct hda_pcm_stream *capture_stream;
93
94         /* capture source */
95         const struct hda_input_mux *input_mux;
96         const hda_nid_t *capsrc_nids;
97         unsigned int cur_mux[3];
98
99         /* channel model */
100         const struct hda_channel_mode *channel_mode;
101         int num_channel_mode;
102
103         /* PCM information */
104         struct hda_pcm pcm_rec[2];      /* used in build_pcms() */
105
106         unsigned int spdif_route;
107
108         /* dynamic controls, init_verbs and input_mux */
109         struct auto_pin_cfg autocfg;
110         struct hda_input_mux private_imux;
111         int imux_cfg_idx[HDA_MAX_NUM_INPUTS]; /* corresponding autocfg.input */
112         hda_nid_t imux_adcs[HDA_MAX_NUM_INPUTS];
113         hda_nid_t imux_pins[HDA_MAX_NUM_INPUTS];
114         hda_nid_t private_adc_nids[HDA_MAX_NUM_INPUTS];
115         hda_nid_t private_dac_nids[AUTO_CFG_MAX_OUTS];
116         struct pin_dac_pair dac_info[8];
117         int dac_info_filled;
118
119         unsigned int port_d_mode;
120         unsigned int auto_mute:1;       /* used in auto-parser */
121         unsigned int dell_automute:1;
122         unsigned int dell_vostro:1;
123         unsigned int ideapad:1;
124         unsigned int thinkpad:1;
125         unsigned int hp_laptop:1;
126         unsigned int asus:1;
127
128         unsigned int adc_switching:1;
129
130         unsigned int ext_mic_present;
131         unsigned int recording;
132         void (*capture_prepare)(struct hda_codec *codec);
133         void (*capture_cleanup)(struct hda_codec *codec);
134
135         /* OLPC XO-1.5 supports DC input mode (e.g. for use with analog sensors)
136          * through the microphone jack.
137          * When the user enables this through a mixer switch, both internal and
138          * external microphones are disabled. Gain is fixed at 0dB. In this mode,
139          * we also allow the bias to be configured through a separate mixer
140          * control. */
141         unsigned int dc_enable;
142         unsigned int dc_input_bias; /* offset into cxt5066_olpc_dc_bias */
143         unsigned int mic_boost; /* offset into cxt5066_analog_mic_boost */
144
145         unsigned int beep_amp;
146 };
147
148 static int conexant_playback_pcm_open(struct hda_pcm_stream *hinfo,
149                                       struct hda_codec *codec,
150                                       struct snd_pcm_substream *substream)
151 {
152         struct conexant_spec *spec = codec->spec;
153         return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
154                                              hinfo);
155 }
156
157 static int conexant_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
158                                          struct hda_codec *codec,
159                                          unsigned int stream_tag,
160                                          unsigned int format,
161                                          struct snd_pcm_substream *substream)
162 {
163         struct conexant_spec *spec = codec->spec;
164         return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
165                                                 stream_tag,
166                                                 format, substream);
167 }
168
169 static int conexant_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
170                                          struct hda_codec *codec,
171                                          struct snd_pcm_substream *substream)
172 {
173         struct conexant_spec *spec = codec->spec;
174         return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
175 }
176
177 /*
178  * Digital out
179  */
180 static int conexant_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
181                                           struct hda_codec *codec,
182                                           struct snd_pcm_substream *substream)
183 {
184         struct conexant_spec *spec = codec->spec;
185         return snd_hda_multi_out_dig_open(codec, &spec->multiout);
186 }
187
188 static int conexant_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
189                                          struct hda_codec *codec,
190                                          struct snd_pcm_substream *substream)
191 {
192         struct conexant_spec *spec = codec->spec;
193         return snd_hda_multi_out_dig_close(codec, &spec->multiout);
194 }
195
196 static int conexant_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
197                                          struct hda_codec *codec,
198                                          unsigned int stream_tag,
199                                          unsigned int format,
200                                          struct snd_pcm_substream *substream)
201 {
202         struct conexant_spec *spec = codec->spec;
203         return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
204                                              stream_tag,
205                                              format, substream);
206 }
207
208 /*
209  * Analog capture
210  */
211 static int conexant_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
212                                       struct hda_codec *codec,
213                                       unsigned int stream_tag,
214                                       unsigned int format,
215                                       struct snd_pcm_substream *substream)
216 {
217         struct conexant_spec *spec = codec->spec;
218         if (spec->capture_prepare)
219                 spec->capture_prepare(codec);
220         snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
221                                    stream_tag, 0, format);
222         return 0;
223 }
224
225 static int conexant_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
226                                       struct hda_codec *codec,
227                                       struct snd_pcm_substream *substream)
228 {
229         struct conexant_spec *spec = codec->spec;
230         snd_hda_codec_cleanup_stream(codec, spec->adc_nids[substream->number]);
231         if (spec->capture_cleanup)
232                 spec->capture_cleanup(codec);
233         return 0;
234 }
235
236
237
238 static const struct hda_pcm_stream conexant_pcm_analog_playback = {
239         .substreams = 1,
240         .channels_min = 2,
241         .channels_max = 2,
242         .nid = 0, /* fill later */
243         .ops = {
244                 .open = conexant_playback_pcm_open,
245                 .prepare = conexant_playback_pcm_prepare,
246                 .cleanup = conexant_playback_pcm_cleanup
247         },
248 };
249
250 static const struct hda_pcm_stream conexant_pcm_analog_capture = {
251         .substreams = 1,
252         .channels_min = 2,
253         .channels_max = 2,
254         .nid = 0, /* fill later */
255         .ops = {
256                 .prepare = conexant_capture_pcm_prepare,
257                 .cleanup = conexant_capture_pcm_cleanup
258         },
259 };
260
261
262 static const struct hda_pcm_stream conexant_pcm_digital_playback = {
263         .substreams = 1,
264         .channels_min = 2,
265         .channels_max = 2,
266         .nid = 0, /* fill later */
267         .ops = {
268                 .open = conexant_dig_playback_pcm_open,
269                 .close = conexant_dig_playback_pcm_close,
270                 .prepare = conexant_dig_playback_pcm_prepare
271         },
272 };
273
274 static const struct hda_pcm_stream conexant_pcm_digital_capture = {
275         .substreams = 1,
276         .channels_min = 2,
277         .channels_max = 2,
278         /* NID is set in alc_build_pcms */
279 };
280
281 static int cx5051_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
282                                       struct hda_codec *codec,
283                                       unsigned int stream_tag,
284                                       unsigned int format,
285                                       struct snd_pcm_substream *substream)
286 {
287         struct conexant_spec *spec = codec->spec;
288         spec->cur_adc = spec->adc_nids[spec->cur_adc_idx];
289         spec->cur_adc_stream_tag = stream_tag;
290         spec->cur_adc_format = format;
291         snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
292         return 0;
293 }
294
295 static int cx5051_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
296                                       struct hda_codec *codec,
297                                       struct snd_pcm_substream *substream)
298 {
299         struct conexant_spec *spec = codec->spec;
300         snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
301         spec->cur_adc = 0;
302         return 0;
303 }
304
305 static const struct hda_pcm_stream cx5051_pcm_analog_capture = {
306         .substreams = 1,
307         .channels_min = 2,
308         .channels_max = 2,
309         .nid = 0, /* fill later */
310         .ops = {
311                 .prepare = cx5051_capture_pcm_prepare,
312                 .cleanup = cx5051_capture_pcm_cleanup
313         },
314 };
315
316 static int conexant_build_pcms(struct hda_codec *codec)
317 {
318         struct conexant_spec *spec = codec->spec;
319         struct hda_pcm *info = spec->pcm_rec;
320
321         codec->num_pcms = 1;
322         codec->pcm_info = info;
323
324         info->name = "CONEXANT Analog";
325         info->stream[SNDRV_PCM_STREAM_PLAYBACK] = conexant_pcm_analog_playback;
326         info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
327                 spec->multiout.max_channels;
328         info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
329                 spec->multiout.dac_nids[0];
330         if (spec->capture_stream)
331                 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *spec->capture_stream;
332         else {
333                 if (codec->vendor_id == 0x14f15051)
334                         info->stream[SNDRV_PCM_STREAM_CAPTURE] =
335                                 cx5051_pcm_analog_capture;
336                 else {
337                         info->stream[SNDRV_PCM_STREAM_CAPTURE] =
338                                 conexant_pcm_analog_capture;
339                         info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
340                                 spec->num_adc_nids;
341                 }
342         }
343         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
344
345         if (spec->multiout.dig_out_nid) {
346                 info++;
347                 codec->num_pcms++;
348                 info->name = "Conexant Digital";
349                 info->pcm_type = HDA_PCM_TYPE_SPDIF;
350                 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
351                         conexant_pcm_digital_playback;
352                 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
353                         spec->multiout.dig_out_nid;
354                 if (spec->dig_in_nid) {
355                         info->stream[SNDRV_PCM_STREAM_CAPTURE] =
356                                 conexant_pcm_digital_capture;
357                         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
358                                 spec->dig_in_nid;
359                 }
360                 if (spec->slave_dig_outs[0])
361                         codec->slave_dig_outs = spec->slave_dig_outs;
362         }
363
364         return 0;
365 }
366
367 static int conexant_mux_enum_info(struct snd_kcontrol *kcontrol,
368                                   struct snd_ctl_elem_info *uinfo)
369 {
370         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
371         struct conexant_spec *spec = codec->spec;
372
373         return snd_hda_input_mux_info(spec->input_mux, uinfo);
374 }
375
376 static int conexant_mux_enum_get(struct snd_kcontrol *kcontrol,
377                                  struct snd_ctl_elem_value *ucontrol)
378 {
379         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
380         struct conexant_spec *spec = codec->spec;
381         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
382
383         ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
384         return 0;
385 }
386
387 static int conexant_mux_enum_put(struct snd_kcontrol *kcontrol,
388                                  struct snd_ctl_elem_value *ucontrol)
389 {
390         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
391         struct conexant_spec *spec = codec->spec;
392         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
393
394         return snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
395                                      spec->capsrc_nids[adc_idx],
396                                      &spec->cur_mux[adc_idx]);
397 }
398
399 static int conexant_init_jacks(struct hda_codec *codec)
400 {
401 #ifdef CONFIG_SND_HDA_INPUT_JACK
402         struct conexant_spec *spec = codec->spec;
403         int i;
404
405         for (i = 0; i < spec->num_init_verbs; i++) {
406                 const struct hda_verb *hv;
407
408                 hv = spec->init_verbs[i];
409                 while (hv->nid) {
410                         int err = 0;
411                         switch (hv->param ^ AC_USRSP_EN) {
412                         case CONEXANT_HP_EVENT:
413                                 err = snd_hda_input_jack_add(codec, hv->nid,
414                                                 SND_JACK_HEADPHONE, NULL);
415                                 snd_hda_input_jack_report(codec, hv->nid);
416                                 break;
417                         case CXT5051_PORTC_EVENT:
418                         case CONEXANT_MIC_EVENT:
419                                 err = snd_hda_input_jack_add(codec, hv->nid,
420                                                 SND_JACK_MICROPHONE, NULL);
421                                 snd_hda_input_jack_report(codec, hv->nid);
422                                 break;
423                         }
424                         if (err < 0)
425                                 return err;
426                         ++hv;
427                 }
428         }
429 #endif /* CONFIG_SND_HDA_INPUT_JACK */
430         return 0;
431 }
432
433 static int conexant_init(struct hda_codec *codec)
434 {
435         struct conexant_spec *spec = codec->spec;
436         int i;
437
438         for (i = 0; i < spec->num_init_verbs; i++)
439                 snd_hda_sequence_write(codec, spec->init_verbs[i]);
440         return 0;
441 }
442
443 static void conexant_free(struct hda_codec *codec)
444 {
445         snd_hda_input_jack_free(codec);
446         snd_hda_detach_beep_device(codec);
447         kfree(codec->spec);
448 }
449
450 static const struct snd_kcontrol_new cxt_capture_mixers[] = {
451         {
452                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
453                 .name = "Capture Source",
454                 .info = conexant_mux_enum_info,
455                 .get = conexant_mux_enum_get,
456                 .put = conexant_mux_enum_put
457         },
458         {}
459 };
460
461 #ifdef CONFIG_SND_HDA_INPUT_BEEP
462 /* additional beep mixers; the actual parameters are overwritten at build */
463 static const struct snd_kcontrol_new cxt_beep_mixer[] = {
464         HDA_CODEC_VOLUME_MONO("Beep Playback Volume", 0, 1, 0, HDA_OUTPUT),
465         HDA_CODEC_MUTE_BEEP_MONO("Beep Playback Switch", 0, 1, 0, HDA_OUTPUT),
466         { } /* end */
467 };
468 #endif
469
470 static const char * const slave_vols[] = {
471         "Headphone Playback Volume",
472         "Speaker Playback Volume",
473         NULL
474 };
475
476 static const char * const slave_sws[] = {
477         "Headphone Playback Switch",
478         "Speaker Playback Switch",
479         NULL
480 };
481
482 static int conexant_build_controls(struct hda_codec *codec)
483 {
484         struct conexant_spec *spec = codec->spec;
485         unsigned int i;
486         int err;
487
488         for (i = 0; i < spec->num_mixers; i++) {
489                 err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
490                 if (err < 0)
491                         return err;
492         }
493         if (spec->multiout.dig_out_nid) {
494                 err = snd_hda_create_spdif_out_ctls(codec,
495                                                     spec->multiout.dig_out_nid);
496                 if (err < 0)
497                         return err;
498                 err = snd_hda_create_spdif_share_sw(codec,
499                                                     &spec->multiout);
500                 if (err < 0)
501                         return err;
502                 spec->multiout.share_spdif = 1;
503         } 
504         if (spec->dig_in_nid) {
505                 err = snd_hda_create_spdif_in_ctls(codec,spec->dig_in_nid);
506                 if (err < 0)
507                         return err;
508         }
509
510         /* if we have no master control, let's create it */
511         if (spec->vmaster_nid &&
512             !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
513                 unsigned int vmaster_tlv[4];
514                 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
515                                         HDA_OUTPUT, vmaster_tlv);
516                 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
517                                           vmaster_tlv, slave_vols);
518                 if (err < 0)
519                         return err;
520         }
521         if (spec->vmaster_nid &&
522             !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
523                 err = snd_hda_add_vmaster(codec, "Master Playback Switch",
524                                           NULL, slave_sws);
525                 if (err < 0)
526                         return err;
527         }
528
529         if (spec->input_mux) {
530                 err = snd_hda_add_new_ctls(codec, cxt_capture_mixers);
531                 if (err < 0)
532                         return err;
533         }
534
535 #ifdef CONFIG_SND_HDA_INPUT_BEEP
536         /* create beep controls if needed */
537         if (spec->beep_amp) {
538                 const struct snd_kcontrol_new *knew;
539                 for (knew = cxt_beep_mixer; knew->name; knew++) {
540                         struct snd_kcontrol *kctl;
541                         kctl = snd_ctl_new1(knew, codec);
542                         if (!kctl)
543                                 return -ENOMEM;
544                         kctl->private_value = spec->beep_amp;
545                         err = snd_hda_ctl_add(codec, 0, kctl);
546                         if (err < 0)
547                                 return err;
548                 }
549         }
550 #endif
551
552         return 0;
553 }
554
555 #ifdef CONFIG_SND_HDA_POWER_SAVE
556 static int conexant_suspend(struct hda_codec *codec, pm_message_t state)
557 {
558         snd_hda_shutup_pins(codec);
559         return 0;
560 }
561 #endif
562
563 static const struct hda_codec_ops conexant_patch_ops = {
564         .build_controls = conexant_build_controls,
565         .build_pcms = conexant_build_pcms,
566         .init = conexant_init,
567         .free = conexant_free,
568 #ifdef CONFIG_SND_HDA_POWER_SAVE
569         .suspend = conexant_suspend,
570 #endif
571         .reboot_notify = snd_hda_shutup_pins,
572 };
573
574 #ifdef CONFIG_SND_HDA_INPUT_BEEP
575 #define set_beep_amp(spec, nid, idx, dir) \
576         ((spec)->beep_amp = HDA_COMPOSE_AMP_VAL(nid, 1, idx, dir))
577 #else
578 #define set_beep_amp(spec, nid, idx, dir) /* NOP */
579 #endif
580
581 static int patch_conexant_auto(struct hda_codec *codec);
582 /*
583  * EAPD control
584  * the private value = nid | (invert << 8)
585  */
586
587 #define cxt_eapd_info           snd_ctl_boolean_mono_info
588
589 static int cxt_eapd_get(struct snd_kcontrol *kcontrol,
590                              struct snd_ctl_elem_value *ucontrol)
591 {
592         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
593         struct conexant_spec *spec = codec->spec;
594         int invert = (kcontrol->private_value >> 8) & 1;
595         if (invert)
596                 ucontrol->value.integer.value[0] = !spec->cur_eapd;
597         else
598                 ucontrol->value.integer.value[0] = spec->cur_eapd;
599         return 0;
600
601 }
602
603 static int cxt_eapd_put(struct snd_kcontrol *kcontrol,
604                              struct snd_ctl_elem_value *ucontrol)
605 {
606         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
607         struct conexant_spec *spec = codec->spec;
608         int invert = (kcontrol->private_value >> 8) & 1;
609         hda_nid_t nid = kcontrol->private_value & 0xff;
610         unsigned int eapd;
611
612         eapd = !!ucontrol->value.integer.value[0];
613         if (invert)
614                 eapd = !eapd;
615         if (eapd == spec->cur_eapd)
616                 return 0;
617         
618         spec->cur_eapd = eapd;
619         snd_hda_codec_write_cache(codec, nid,
620                                   0, AC_VERB_SET_EAPD_BTLENABLE,
621                                   eapd ? 0x02 : 0x00);
622         return 1;
623 }
624
625 /* controls for test mode */
626 #ifdef CONFIG_SND_DEBUG
627
628 #define CXT_EAPD_SWITCH(xname, nid, mask) \
629         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = 0,  \
630           .info = cxt_eapd_info, \
631           .get = cxt_eapd_get, \
632           .put = cxt_eapd_put, \
633           .private_value = nid | (mask<<16) }
634
635
636
637 static int conexant_ch_mode_info(struct snd_kcontrol *kcontrol,
638                                  struct snd_ctl_elem_info *uinfo)
639 {
640         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
641         struct conexant_spec *spec = codec->spec;
642         return snd_hda_ch_mode_info(codec, uinfo, spec->channel_mode,
643                                     spec->num_channel_mode);
644 }
645
646 static int conexant_ch_mode_get(struct snd_kcontrol *kcontrol,
647                                 struct snd_ctl_elem_value *ucontrol)
648 {
649         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
650         struct conexant_spec *spec = codec->spec;
651         return snd_hda_ch_mode_get(codec, ucontrol, spec->channel_mode,
652                                    spec->num_channel_mode,
653                                    spec->multiout.max_channels);
654 }
655
656 static int conexant_ch_mode_put(struct snd_kcontrol *kcontrol,
657                                 struct snd_ctl_elem_value *ucontrol)
658 {
659         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
660         struct conexant_spec *spec = codec->spec;
661         int err = snd_hda_ch_mode_put(codec, ucontrol, spec->channel_mode,
662                                       spec->num_channel_mode,
663                                       &spec->multiout.max_channels);
664         if (err >= 0 && spec->need_dac_fix)
665                 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
666         return err;
667 }
668
669 #define CXT_PIN_MODE(xname, nid, dir) \
670         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = 0,  \
671           .info = conexant_ch_mode_info, \
672           .get = conexant_ch_mode_get, \
673           .put = conexant_ch_mode_put, \
674           .private_value = nid | (dir<<16) }
675
676 #endif /* CONFIG_SND_DEBUG */
677
678 /* Conexant 5045 specific */
679
680 static const hda_nid_t cxt5045_dac_nids[1] = { 0x19 };
681 static const hda_nid_t cxt5045_adc_nids[1] = { 0x1a };
682 static const hda_nid_t cxt5045_capsrc_nids[1] = { 0x1a };
683 #define CXT5045_SPDIF_OUT       0x18
684
685 static const struct hda_channel_mode cxt5045_modes[1] = {
686         { 2, NULL },
687 };
688
689 static const struct hda_input_mux cxt5045_capture_source = {
690         .num_items = 2,
691         .items = {
692                 { "IntMic", 0x1 },
693                 { "ExtMic", 0x2 },
694         }
695 };
696
697 static const struct hda_input_mux cxt5045_capture_source_benq = {
698         .num_items = 5,
699         .items = {
700                 { "IntMic", 0x1 },
701                 { "ExtMic", 0x2 },
702                 { "LineIn", 0x3 },
703                 { "CD",     0x4 },
704                 { "Mixer",  0x0 },
705         }
706 };
707
708 static const struct hda_input_mux cxt5045_capture_source_hp530 = {
709         .num_items = 2,
710         .items = {
711                 { "ExtMic", 0x1 },
712                 { "IntMic", 0x2 },
713         }
714 };
715
716 /* turn on/off EAPD (+ mute HP) as a master switch */
717 static int cxt5045_hp_master_sw_put(struct snd_kcontrol *kcontrol,
718                                     struct snd_ctl_elem_value *ucontrol)
719 {
720         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
721         struct conexant_spec *spec = codec->spec;
722         unsigned int bits;
723
724         if (!cxt_eapd_put(kcontrol, ucontrol))
725                 return 0;
726
727         /* toggle internal speakers mute depending of presence of
728          * the headphone jack
729          */
730         bits = (!spec->hp_present && spec->cur_eapd) ? 0 : HDA_AMP_MUTE;
731         snd_hda_codec_amp_stereo(codec, 0x10, HDA_OUTPUT, 0,
732                                  HDA_AMP_MUTE, bits);
733
734         bits = spec->cur_eapd ? 0 : HDA_AMP_MUTE;
735         snd_hda_codec_amp_stereo(codec, 0x11, HDA_OUTPUT, 0,
736                                  HDA_AMP_MUTE, bits);
737         return 1;
738 }
739
740 /* bind volumes of both NID 0x10 and 0x11 */
741 static const struct hda_bind_ctls cxt5045_hp_bind_master_vol = {
742         .ops = &snd_hda_bind_vol,
743         .values = {
744                 HDA_COMPOSE_AMP_VAL(0x10, 3, 0, HDA_OUTPUT),
745                 HDA_COMPOSE_AMP_VAL(0x11, 3, 0, HDA_OUTPUT),
746                 0
747         },
748 };
749
750 /* toggle input of built-in and mic jack appropriately */
751 static void cxt5045_hp_automic(struct hda_codec *codec)
752 {
753         static const struct hda_verb mic_jack_on[] = {
754                 {0x14, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
755                 {0x12, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
756                 {}
757         };
758         static const struct hda_verb mic_jack_off[] = {
759                 {0x12, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
760                 {0x14, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
761                 {}
762         };
763         unsigned int present;
764
765         present = snd_hda_jack_detect(codec, 0x12);
766         if (present)
767                 snd_hda_sequence_write(codec, mic_jack_on);
768         else
769                 snd_hda_sequence_write(codec, mic_jack_off);
770 }
771
772
773 /* mute internal speaker if HP is plugged */
774 static void cxt5045_hp_automute(struct hda_codec *codec)
775 {
776         struct conexant_spec *spec = codec->spec;
777         unsigned int bits;
778
779         spec->hp_present = snd_hda_jack_detect(codec, 0x11);
780
781         bits = (spec->hp_present || !spec->cur_eapd) ? HDA_AMP_MUTE : 0; 
782         snd_hda_codec_amp_stereo(codec, 0x10, HDA_OUTPUT, 0,
783                                  HDA_AMP_MUTE, bits);
784 }
785
786 /* unsolicited event for HP jack sensing */
787 static void cxt5045_hp_unsol_event(struct hda_codec *codec,
788                                    unsigned int res)
789 {
790         res >>= 26;
791         switch (res) {
792         case CONEXANT_HP_EVENT:
793                 cxt5045_hp_automute(codec);
794                 break;
795         case CONEXANT_MIC_EVENT:
796                 cxt5045_hp_automic(codec);
797                 break;
798
799         }
800 }
801
802 static const struct snd_kcontrol_new cxt5045_mixers[] = {
803         HDA_CODEC_VOLUME("Internal Mic Capture Volume", 0x1a, 0x01, HDA_INPUT),
804         HDA_CODEC_MUTE("Internal Mic Capture Switch", 0x1a, 0x01, HDA_INPUT),
805         HDA_CODEC_VOLUME("Mic Capture Volume", 0x1a, 0x02, HDA_INPUT),
806         HDA_CODEC_MUTE("Mic Capture Switch", 0x1a, 0x02, HDA_INPUT),
807         HDA_CODEC_VOLUME("PCM Playback Volume", 0x17, 0x0, HDA_INPUT),
808         HDA_CODEC_MUTE("PCM Playback Switch", 0x17, 0x0, HDA_INPUT),
809         HDA_CODEC_VOLUME("Internal Mic Playback Volume", 0x17, 0x1, HDA_INPUT),
810         HDA_CODEC_MUTE("Internal Mic Playback Switch", 0x17, 0x1, HDA_INPUT),
811         HDA_CODEC_VOLUME("Mic Playback Volume", 0x17, 0x2, HDA_INPUT),
812         HDA_CODEC_MUTE("Mic Playback Switch", 0x17, 0x2, HDA_INPUT),
813         HDA_BIND_VOL("Master Playback Volume", &cxt5045_hp_bind_master_vol),
814         {
815                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
816                 .name = "Master Playback Switch",
817                 .info = cxt_eapd_info,
818                 .get = cxt_eapd_get,
819                 .put = cxt5045_hp_master_sw_put,
820                 .private_value = 0x10,
821         },
822
823         {}
824 };
825
826 static const struct snd_kcontrol_new cxt5045_benq_mixers[] = {
827         HDA_CODEC_VOLUME("CD Capture Volume", 0x1a, 0x04, HDA_INPUT),
828         HDA_CODEC_MUTE("CD Capture Switch", 0x1a, 0x04, HDA_INPUT),
829         HDA_CODEC_VOLUME("CD Playback Volume", 0x17, 0x4, HDA_INPUT),
830         HDA_CODEC_MUTE("CD Playback Switch", 0x17, 0x4, HDA_INPUT),
831
832         HDA_CODEC_VOLUME("Line In Capture Volume", 0x1a, 0x03, HDA_INPUT),
833         HDA_CODEC_MUTE("Line In Capture Switch", 0x1a, 0x03, HDA_INPUT),
834         HDA_CODEC_VOLUME("Line In Playback Volume", 0x17, 0x3, HDA_INPUT),
835         HDA_CODEC_MUTE("Line In Playback Switch", 0x17, 0x3, HDA_INPUT),
836
837         HDA_CODEC_VOLUME("Mixer Capture Volume", 0x1a, 0x0, HDA_INPUT),
838         HDA_CODEC_MUTE("Mixer Capture Switch", 0x1a, 0x0, HDA_INPUT),
839
840         {}
841 };
842
843 static const struct snd_kcontrol_new cxt5045_mixers_hp530[] = {
844         HDA_CODEC_VOLUME("Internal Mic Capture Volume", 0x1a, 0x02, HDA_INPUT),
845         HDA_CODEC_MUTE("Internal Mic Capture Switch", 0x1a, 0x02, HDA_INPUT),
846         HDA_CODEC_VOLUME("Mic Capture Volume", 0x1a, 0x01, HDA_INPUT),
847         HDA_CODEC_MUTE("Mic Capture Switch", 0x1a, 0x01, HDA_INPUT),
848         HDA_CODEC_VOLUME("PCM Playback Volume", 0x17, 0x0, HDA_INPUT),
849         HDA_CODEC_MUTE("PCM Playback Switch", 0x17, 0x0, HDA_INPUT),
850         HDA_CODEC_VOLUME("Internal Mic Playback Volume", 0x17, 0x2, HDA_INPUT),
851         HDA_CODEC_MUTE("Internal Mic Playback Switch", 0x17, 0x2, HDA_INPUT),
852         HDA_CODEC_VOLUME("Mic Playback Volume", 0x17, 0x1, HDA_INPUT),
853         HDA_CODEC_MUTE("Mic Playback Switch", 0x17, 0x1, HDA_INPUT),
854         HDA_BIND_VOL("Master Playback Volume", &cxt5045_hp_bind_master_vol),
855         {
856                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
857                 .name = "Master Playback Switch",
858                 .info = cxt_eapd_info,
859                 .get = cxt_eapd_get,
860                 .put = cxt5045_hp_master_sw_put,
861                 .private_value = 0x10,
862         },
863
864         {}
865 };
866
867 static const struct hda_verb cxt5045_init_verbs[] = {
868         /* Line in, Mic */
869         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN|AC_PINCTL_VREF_80 },
870         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN|AC_PINCTL_VREF_80 },
871         /* HP, Amp  */
872         {0x10, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
873         {0x10, AC_VERB_SET_CONNECT_SEL, 0x1},
874         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
875         {0x11, AC_VERB_SET_CONNECT_SEL, 0x1},
876         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
877         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
878         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
879         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
880         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
881         /* Record selector: Internal mic */
882         {0x1a, AC_VERB_SET_CONNECT_SEL,0x1},
883         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE,
884          AC_AMP_SET_INPUT|AC_AMP_SET_RIGHT|AC_AMP_SET_LEFT|0x17},
885         /* SPDIF route: PCM */
886         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
887         { 0x13, AC_VERB_SET_CONNECT_SEL, 0x0 },
888         /* EAPD */
889         {0x10, AC_VERB_SET_EAPD_BTLENABLE, 0x2 }, /* default on */ 
890         { } /* end */
891 };
892
893 static const struct hda_verb cxt5045_benq_init_verbs[] = {
894         /* Internal Mic, Mic */
895         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN|AC_PINCTL_VREF_80 },
896         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN|AC_PINCTL_VREF_80 },
897         /* Line In,HP, Amp  */
898         {0x10, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
899         {0x10, AC_VERB_SET_CONNECT_SEL, 0x1},
900         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
901         {0x11, AC_VERB_SET_CONNECT_SEL, 0x1},
902         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
903         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
904         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
905         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
906         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
907         /* Record selector: Internal mic */
908         {0x1a, AC_VERB_SET_CONNECT_SEL, 0x1},
909         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE,
910          AC_AMP_SET_INPUT|AC_AMP_SET_RIGHT|AC_AMP_SET_LEFT|0x17},
911         /* SPDIF route: PCM */
912         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
913         {0x13, AC_VERB_SET_CONNECT_SEL, 0x0},
914         /* EAPD */
915         {0x10, AC_VERB_SET_EAPD_BTLENABLE, 0x2}, /* default on */
916         { } /* end */
917 };
918
919 static const struct hda_verb cxt5045_hp_sense_init_verbs[] = {
920         /* pin sensing on HP jack */
921         {0x11, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_HP_EVENT},
922         { } /* end */
923 };
924
925 static const struct hda_verb cxt5045_mic_sense_init_verbs[] = {
926         /* pin sensing on HP jack */
927         {0x12, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_MIC_EVENT},
928         { } /* end */
929 };
930
931 #ifdef CONFIG_SND_DEBUG
932 /* Test configuration for debugging, modelled after the ALC260 test
933  * configuration.
934  */
935 static const struct hda_input_mux cxt5045_test_capture_source = {
936         .num_items = 5,
937         .items = {
938                 { "MIXER", 0x0 },
939                 { "MIC1 pin", 0x1 },
940                 { "LINE1 pin", 0x2 },
941                 { "HP-OUT pin", 0x3 },
942                 { "CD pin", 0x4 },
943         },
944 };
945
946 static const struct snd_kcontrol_new cxt5045_test_mixer[] = {
947
948         /* Output controls */
949         HDA_CODEC_VOLUME("Speaker Playback Volume", 0x10, 0x0, HDA_OUTPUT),
950         HDA_CODEC_MUTE("Speaker Playback Switch", 0x10, 0x0, HDA_OUTPUT),
951         HDA_CODEC_VOLUME("Node 11 Playback Volume", 0x11, 0x0, HDA_OUTPUT),
952         HDA_CODEC_MUTE("Node 11 Playback Switch", 0x11, 0x0, HDA_OUTPUT),
953         HDA_CODEC_VOLUME("Node 12 Playback Volume", 0x12, 0x0, HDA_OUTPUT),
954         HDA_CODEC_MUTE("Node 12 Playback Switch", 0x12, 0x0, HDA_OUTPUT),
955         
956         /* Modes for retasking pin widgets */
957         CXT_PIN_MODE("HP-OUT pin mode", 0x11, CXT_PIN_DIR_INOUT),
958         CXT_PIN_MODE("LINE1 pin mode", 0x12, CXT_PIN_DIR_INOUT),
959
960         /* EAPD Switch Control */
961         CXT_EAPD_SWITCH("External Amplifier", 0x10, 0x0),
962
963         /* Loopback mixer controls */
964
965         HDA_CODEC_VOLUME("Mixer-1 Volume", 0x17, 0x0, HDA_INPUT),
966         HDA_CODEC_MUTE("Mixer-1 Switch", 0x17, 0x0, HDA_INPUT),
967         HDA_CODEC_VOLUME("Mixer-2 Volume", 0x17, 0x1, HDA_INPUT),
968         HDA_CODEC_MUTE("Mixer-2 Switch", 0x17, 0x1, HDA_INPUT),
969         HDA_CODEC_VOLUME("Mixer-3 Volume", 0x17, 0x2, HDA_INPUT),
970         HDA_CODEC_MUTE("Mixer-3 Switch", 0x17, 0x2, HDA_INPUT),
971         HDA_CODEC_VOLUME("Mixer-4 Volume", 0x17, 0x3, HDA_INPUT),
972         HDA_CODEC_MUTE("Mixer-4 Switch", 0x17, 0x3, HDA_INPUT),
973         HDA_CODEC_VOLUME("Mixer-5 Volume", 0x17, 0x4, HDA_INPUT),
974         HDA_CODEC_MUTE("Mixer-5 Switch", 0x17, 0x4, HDA_INPUT),
975         {
976                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
977                 .name = "Input Source",
978                 .info = conexant_mux_enum_info,
979                 .get = conexant_mux_enum_get,
980                 .put = conexant_mux_enum_put,
981         },
982         /* Audio input controls */
983         HDA_CODEC_VOLUME("Input-1 Volume", 0x1a, 0x0, HDA_INPUT),
984         HDA_CODEC_MUTE("Input-1 Switch", 0x1a, 0x0, HDA_INPUT),
985         HDA_CODEC_VOLUME("Input-2 Volume", 0x1a, 0x1, HDA_INPUT),
986         HDA_CODEC_MUTE("Input-2 Switch", 0x1a, 0x1, HDA_INPUT),
987         HDA_CODEC_VOLUME("Input-3 Volume", 0x1a, 0x2, HDA_INPUT),
988         HDA_CODEC_MUTE("Input-3 Switch", 0x1a, 0x2, HDA_INPUT),
989         HDA_CODEC_VOLUME("Input-4 Volume", 0x1a, 0x3, HDA_INPUT),
990         HDA_CODEC_MUTE("Input-4 Switch", 0x1a, 0x3, HDA_INPUT),
991         HDA_CODEC_VOLUME("Input-5 Volume", 0x1a, 0x4, HDA_INPUT),
992         HDA_CODEC_MUTE("Input-5 Switch", 0x1a, 0x4, HDA_INPUT),
993         { } /* end */
994 };
995
996 static const struct hda_verb cxt5045_test_init_verbs[] = {
997         /* Set connections */
998         { 0x10, AC_VERB_SET_CONNECT_SEL, 0x0 },
999         { 0x11, AC_VERB_SET_CONNECT_SEL, 0x0 },
1000         { 0x12, AC_VERB_SET_CONNECT_SEL, 0x0 },
1001         /* Enable retasking pins as output, initially without power amp */
1002         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1003         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1004
1005         /* Disable digital (SPDIF) pins initially, but users can enable
1006          * them via a mixer switch.  In the case of SPDIF-out, this initverb
1007          * payload also sets the generation to 0, output to be in "consumer"
1008          * PCM format, copyright asserted, no pre-emphasis and no validity
1009          * control.
1010          */
1011         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1012         {0x18, AC_VERB_SET_DIGI_CONVERT_1, 0},
1013
1014         /* Start with output sum widgets muted and their output gains at min */
1015         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1016         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
1017
1018         /* Unmute retasking pin widget output buffers since the default
1019          * state appears to be output.  As the pin mode is changed by the
1020          * user the pin mode control will take care of enabling the pin's
1021          * input/output buffers as needed.
1022          */
1023         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1024         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1025
1026         /* Mute capture amp left and right */
1027         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1028
1029         /* Set ADC connection select to match default mixer setting (mic1
1030          * pin)
1031          */
1032         {0x1a, AC_VERB_SET_CONNECT_SEL, 0x00},
1033         {0x17, AC_VERB_SET_CONNECT_SEL, 0x00},
1034
1035         /* Mute all inputs to mixer widget (even unconnected ones) */
1036         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)}, /* Mixer pin */
1037         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)}, /* Mic1 pin */
1038         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)}, /* Line pin */
1039         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)}, /* HP pin */
1040         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)}, /* CD pin */
1041
1042         { }
1043 };
1044 #endif
1045
1046
1047 /* initialize jack-sensing, too */
1048 static int cxt5045_init(struct hda_codec *codec)
1049 {
1050         conexant_init(codec);
1051         cxt5045_hp_automute(codec);
1052         return 0;
1053 }
1054
1055
1056 enum {
1057         CXT5045_LAPTOP_HPSENSE,
1058         CXT5045_LAPTOP_MICSENSE,
1059         CXT5045_LAPTOP_HPMICSENSE,
1060         CXT5045_BENQ,
1061         CXT5045_LAPTOP_HP530,
1062 #ifdef CONFIG_SND_DEBUG
1063         CXT5045_TEST,
1064 #endif
1065         CXT5045_AUTO,
1066         CXT5045_MODELS
1067 };
1068
1069 static const char * const cxt5045_models[CXT5045_MODELS] = {
1070         [CXT5045_LAPTOP_HPSENSE]        = "laptop-hpsense",
1071         [CXT5045_LAPTOP_MICSENSE]       = "laptop-micsense",
1072         [CXT5045_LAPTOP_HPMICSENSE]     = "laptop-hpmicsense",
1073         [CXT5045_BENQ]                  = "benq",
1074         [CXT5045_LAPTOP_HP530]          = "laptop-hp530",
1075 #ifdef CONFIG_SND_DEBUG
1076         [CXT5045_TEST]          = "test",
1077 #endif
1078         [CXT5045_AUTO]                  = "auto",
1079 };
1080
1081 static const struct snd_pci_quirk cxt5045_cfg_tbl[] = {
1082         SND_PCI_QUIRK(0x103c, 0x30d5, "HP 530", CXT5045_LAPTOP_HP530),
1083         SND_PCI_QUIRK_MASK(0x103c, 0xff00, 0x3000, "HP DV Series",
1084                            CXT5045_LAPTOP_HPSENSE),
1085         SND_PCI_QUIRK(0x1179, 0xff31, "Toshiba P105", CXT5045_LAPTOP_MICSENSE),
1086         SND_PCI_QUIRK(0x152d, 0x0753, "Benq R55E", CXT5045_BENQ),
1087         SND_PCI_QUIRK(0x1734, 0x10ad, "Fujitsu Si1520", CXT5045_LAPTOP_MICSENSE),
1088         SND_PCI_QUIRK(0x1734, 0x10cb, "Fujitsu Si3515", CXT5045_LAPTOP_HPMICSENSE),
1089         SND_PCI_QUIRK(0x1734, 0x110e, "Fujitsu V5505",
1090                       CXT5045_LAPTOP_HPMICSENSE),
1091         SND_PCI_QUIRK(0x1509, 0x1e40, "FIC", CXT5045_LAPTOP_HPMICSENSE),
1092         SND_PCI_QUIRK(0x1509, 0x2f05, "FIC", CXT5045_LAPTOP_HPMICSENSE),
1093         SND_PCI_QUIRK(0x1509, 0x2f06, "FIC", CXT5045_LAPTOP_HPMICSENSE),
1094         SND_PCI_QUIRK_MASK(0x1631, 0xff00, 0xc100, "Packard Bell",
1095                            CXT5045_LAPTOP_HPMICSENSE),
1096         SND_PCI_QUIRK(0x8086, 0x2111, "Conexant Reference board", CXT5045_LAPTOP_HPSENSE),
1097         {}
1098 };
1099
1100 static int patch_cxt5045(struct hda_codec *codec)
1101 {
1102         struct conexant_spec *spec;
1103         int board_config;
1104
1105         board_config = snd_hda_check_board_config(codec, CXT5045_MODELS,
1106                                                   cxt5045_models,
1107                                                   cxt5045_cfg_tbl);
1108 #if 0 /* use the old method just for safety */
1109         if (board_config < 0)
1110                 board_config = CXT5045_AUTO;
1111 #endif
1112         if (board_config == CXT5045_AUTO)
1113                 return patch_conexant_auto(codec);
1114
1115         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1116         if (!spec)
1117                 return -ENOMEM;
1118         codec->spec = spec;
1119         codec->pin_amp_workaround = 1;
1120
1121         spec->multiout.max_channels = 2;
1122         spec->multiout.num_dacs = ARRAY_SIZE(cxt5045_dac_nids);
1123         spec->multiout.dac_nids = cxt5045_dac_nids;
1124         spec->multiout.dig_out_nid = CXT5045_SPDIF_OUT;
1125         spec->num_adc_nids = 1;
1126         spec->adc_nids = cxt5045_adc_nids;
1127         spec->capsrc_nids = cxt5045_capsrc_nids;
1128         spec->input_mux = &cxt5045_capture_source;
1129         spec->num_mixers = 1;
1130         spec->mixers[0] = cxt5045_mixers;
1131         spec->num_init_verbs = 1;
1132         spec->init_verbs[0] = cxt5045_init_verbs;
1133         spec->spdif_route = 0;
1134         spec->num_channel_mode = ARRAY_SIZE(cxt5045_modes);
1135         spec->channel_mode = cxt5045_modes;
1136
1137         set_beep_amp(spec, 0x16, 0, 1);
1138
1139         codec->patch_ops = conexant_patch_ops;
1140
1141         switch (board_config) {
1142         case CXT5045_LAPTOP_HPSENSE:
1143                 codec->patch_ops.unsol_event = cxt5045_hp_unsol_event;
1144                 spec->input_mux = &cxt5045_capture_source;
1145                 spec->num_init_verbs = 2;
1146                 spec->init_verbs[1] = cxt5045_hp_sense_init_verbs;
1147                 spec->mixers[0] = cxt5045_mixers;
1148                 codec->patch_ops.init = cxt5045_init;
1149                 break;
1150         case CXT5045_LAPTOP_MICSENSE:
1151                 codec->patch_ops.unsol_event = cxt5045_hp_unsol_event;
1152                 spec->input_mux = &cxt5045_capture_source;
1153                 spec->num_init_verbs = 2;
1154                 spec->init_verbs[1] = cxt5045_mic_sense_init_verbs;
1155                 spec->mixers[0] = cxt5045_mixers;
1156                 codec->patch_ops.init = cxt5045_init;
1157                 break;
1158         default:
1159         case CXT5045_LAPTOP_HPMICSENSE:
1160                 codec->patch_ops.unsol_event = cxt5045_hp_unsol_event;
1161                 spec->input_mux = &cxt5045_capture_source;
1162                 spec->num_init_verbs = 3;
1163                 spec->init_verbs[1] = cxt5045_hp_sense_init_verbs;
1164                 spec->init_verbs[2] = cxt5045_mic_sense_init_verbs;
1165                 spec->mixers[0] = cxt5045_mixers;
1166                 codec->patch_ops.init = cxt5045_init;
1167                 break;
1168         case CXT5045_BENQ:
1169                 codec->patch_ops.unsol_event = cxt5045_hp_unsol_event;
1170                 spec->input_mux = &cxt5045_capture_source_benq;
1171                 spec->num_init_verbs = 1;
1172                 spec->init_verbs[0] = cxt5045_benq_init_verbs;
1173                 spec->mixers[0] = cxt5045_mixers;
1174                 spec->mixers[1] = cxt5045_benq_mixers;
1175                 spec->num_mixers = 2;
1176                 codec->patch_ops.init = cxt5045_init;
1177                 break;
1178         case CXT5045_LAPTOP_HP530:
1179                 codec->patch_ops.unsol_event = cxt5045_hp_unsol_event;
1180                 spec->input_mux = &cxt5045_capture_source_hp530;
1181                 spec->num_init_verbs = 2;
1182                 spec->init_verbs[1] = cxt5045_hp_sense_init_verbs;
1183                 spec->mixers[0] = cxt5045_mixers_hp530;
1184                 codec->patch_ops.init = cxt5045_init;
1185                 break;
1186 #ifdef CONFIG_SND_DEBUG
1187         case CXT5045_TEST:
1188                 spec->input_mux = &cxt5045_test_capture_source;
1189                 spec->mixers[0] = cxt5045_test_mixer;
1190                 spec->init_verbs[0] = cxt5045_test_init_verbs;
1191                 break;
1192                 
1193 #endif  
1194         }
1195
1196         switch (codec->subsystem_id >> 16) {
1197         case 0x103c:
1198         case 0x1631:
1199         case 0x1734:
1200         case 0x17aa:
1201                 /* HP, Packard Bell, Fujitsu-Siemens & Lenovo laptops have
1202                  * really bad sound over 0dB on NID 0x17. Fix max PCM level to
1203                  * 0 dB (originally it has 0x2b steps with 0dB offset 0x14)
1204                  */
1205                 snd_hda_override_amp_caps(codec, 0x17, HDA_INPUT,
1206                                           (0x14 << AC_AMPCAP_OFFSET_SHIFT) |
1207                                           (0x14 << AC_AMPCAP_NUM_STEPS_SHIFT) |
1208                                           (0x05 << AC_AMPCAP_STEP_SIZE_SHIFT) |
1209                                           (1 << AC_AMPCAP_MUTE_SHIFT));
1210                 break;
1211         }
1212
1213         if (spec->beep_amp)
1214                 snd_hda_attach_beep_device(codec, spec->beep_amp);
1215
1216         return 0;
1217 }
1218
1219
1220 /* Conexant 5047 specific */
1221 #define CXT5047_SPDIF_OUT       0x11
1222
1223 static const hda_nid_t cxt5047_dac_nids[1] = { 0x10 }; /* 0x1c */
1224 static const hda_nid_t cxt5047_adc_nids[1] = { 0x12 };
1225 static const hda_nid_t cxt5047_capsrc_nids[1] = { 0x1a };
1226
1227 static const struct hda_channel_mode cxt5047_modes[1] = {
1228         { 2, NULL },
1229 };
1230
1231 static const struct hda_input_mux cxt5047_toshiba_capture_source = {
1232         .num_items = 2,
1233         .items = {
1234                 { "ExtMic", 0x2 },
1235                 { "Line-In", 0x1 },
1236         }
1237 };
1238
1239 /* turn on/off EAPD (+ mute HP) as a master switch */
1240 static int cxt5047_hp_master_sw_put(struct snd_kcontrol *kcontrol,
1241                                     struct snd_ctl_elem_value *ucontrol)
1242 {
1243         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1244         struct conexant_spec *spec = codec->spec;
1245         unsigned int bits;
1246
1247         if (!cxt_eapd_put(kcontrol, ucontrol))
1248                 return 0;
1249
1250         /* toggle internal speakers mute depending of presence of
1251          * the headphone jack
1252          */
1253         bits = (!spec->hp_present && spec->cur_eapd) ? 0 : HDA_AMP_MUTE;
1254         /* NOTE: Conexat codec needs the index for *OUTPUT* amp of
1255          * pin widgets unlike other codecs.  In this case, we need to
1256          * set index 0x01 for the volume from the mixer amp 0x19.
1257          */
1258         snd_hda_codec_amp_stereo(codec, 0x1d, HDA_OUTPUT, 0x01,
1259                                  HDA_AMP_MUTE, bits);
1260         bits = spec->cur_eapd ? 0 : HDA_AMP_MUTE;
1261         snd_hda_codec_amp_stereo(codec, 0x13, HDA_OUTPUT, 0,
1262                                  HDA_AMP_MUTE, bits);
1263         return 1;
1264 }
1265
1266 /* mute internal speaker if HP is plugged */
1267 static void cxt5047_hp_automute(struct hda_codec *codec)
1268 {
1269         struct conexant_spec *spec = codec->spec;
1270         unsigned int bits;
1271
1272         spec->hp_present = snd_hda_jack_detect(codec, 0x13);
1273
1274         bits = (spec->hp_present || !spec->cur_eapd) ? HDA_AMP_MUTE : 0;
1275         /* See the note in cxt5047_hp_master_sw_put */
1276         snd_hda_codec_amp_stereo(codec, 0x1d, HDA_OUTPUT, 0x01,
1277                                  HDA_AMP_MUTE, bits);
1278 }
1279
1280 /* toggle input of built-in and mic jack appropriately */
1281 static void cxt5047_hp_automic(struct hda_codec *codec)
1282 {
1283         static const struct hda_verb mic_jack_on[] = {
1284                 {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1285                 {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1286                 {}
1287         };
1288         static const struct hda_verb mic_jack_off[] = {
1289                 {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1290                 {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1291                 {}
1292         };
1293         unsigned int present;
1294
1295         present = snd_hda_jack_detect(codec, 0x15);
1296         if (present)
1297                 snd_hda_sequence_write(codec, mic_jack_on);
1298         else
1299                 snd_hda_sequence_write(codec, mic_jack_off);
1300 }
1301
1302 /* unsolicited event for HP jack sensing */
1303 static void cxt5047_hp_unsol_event(struct hda_codec *codec,
1304                                   unsigned int res)
1305 {
1306         switch (res >> 26) {
1307         case CONEXANT_HP_EVENT:
1308                 cxt5047_hp_automute(codec);
1309                 break;
1310         case CONEXANT_MIC_EVENT:
1311                 cxt5047_hp_automic(codec);
1312                 break;
1313         }
1314 }
1315
1316 static const struct snd_kcontrol_new cxt5047_base_mixers[] = {
1317         HDA_CODEC_VOLUME("Mic Playback Volume", 0x19, 0x02, HDA_INPUT),
1318         HDA_CODEC_MUTE("Mic Playback Switch", 0x19, 0x02, HDA_INPUT),
1319         HDA_CODEC_VOLUME("Mic Boost Volume", 0x1a, 0x0, HDA_OUTPUT),
1320         HDA_CODEC_VOLUME("Capture Volume", 0x12, 0x03, HDA_INPUT),
1321         HDA_CODEC_MUTE("Capture Switch", 0x12, 0x03, HDA_INPUT),
1322         HDA_CODEC_VOLUME("PCM Volume", 0x10, 0x00, HDA_OUTPUT),
1323         HDA_CODEC_MUTE("PCM Switch", 0x10, 0x00, HDA_OUTPUT),
1324         {
1325                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1326                 .name = "Master Playback Switch",
1327                 .info = cxt_eapd_info,
1328                 .get = cxt_eapd_get,
1329                 .put = cxt5047_hp_master_sw_put,
1330                 .private_value = 0x13,
1331         },
1332
1333         {}
1334 };
1335
1336 static const struct snd_kcontrol_new cxt5047_hp_spk_mixers[] = {
1337         /* See the note in cxt5047_hp_master_sw_put */
1338         HDA_CODEC_VOLUME("Speaker Playback Volume", 0x1d, 0x01, HDA_OUTPUT),
1339         HDA_CODEC_VOLUME("Headphone Playback Volume", 0x13, 0x00, HDA_OUTPUT),
1340         {}
1341 };
1342
1343 static const struct snd_kcontrol_new cxt5047_hp_only_mixers[] = {
1344         HDA_CODEC_VOLUME("Master Playback Volume", 0x13, 0x00, HDA_OUTPUT),
1345         { } /* end */
1346 };
1347
1348 static const struct hda_verb cxt5047_init_verbs[] = {
1349         /* Line in, Mic, Built-in Mic */
1350         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
1351         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN|AC_PINCTL_VREF_50 },
1352         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN|AC_PINCTL_VREF_50 },
1353         /* HP, Speaker  */
1354         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
1355         {0x13, AC_VERB_SET_CONNECT_SEL, 0x0}, /* mixer(0x19) */
1356         {0x1d, AC_VERB_SET_CONNECT_SEL, 0x1}, /* mixer(0x19) */
1357         /* Record selector: Mic */
1358         {0x12, AC_VERB_SET_CONNECT_SEL,0x03},
1359         {0x19, AC_VERB_SET_AMP_GAIN_MUTE,
1360          AC_AMP_SET_INPUT|AC_AMP_SET_RIGHT|AC_AMP_SET_LEFT|0x17},
1361         {0x1A, AC_VERB_SET_CONNECT_SEL,0x02},
1362         {0x1A, AC_VERB_SET_AMP_GAIN_MUTE,
1363          AC_AMP_SET_OUTPUT|AC_AMP_SET_RIGHT|AC_AMP_SET_LEFT|0x00},
1364         {0x1A, AC_VERB_SET_AMP_GAIN_MUTE,
1365          AC_AMP_SET_OUTPUT|AC_AMP_SET_RIGHT|AC_AMP_SET_LEFT|0x03},
1366         /* SPDIF route: PCM */
1367         { 0x18, AC_VERB_SET_CONNECT_SEL, 0x0 },
1368         /* Enable unsolicited events */
1369         {0x13, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_HP_EVENT},
1370         {0x15, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_MIC_EVENT},
1371         { } /* end */
1372 };
1373
1374 /* configuration for Toshiba Laptops */
1375 static const struct hda_verb cxt5047_toshiba_init_verbs[] = {
1376         {0x13, AC_VERB_SET_EAPD_BTLENABLE, 0x0}, /* default off */
1377         {}
1378 };
1379
1380 /* Test configuration for debugging, modelled after the ALC260 test
1381  * configuration.
1382  */
1383 #ifdef CONFIG_SND_DEBUG
1384 static const struct hda_input_mux cxt5047_test_capture_source = {
1385         .num_items = 4,
1386         .items = {
1387                 { "LINE1 pin", 0x0 },
1388                 { "MIC1 pin", 0x1 },
1389                 { "MIC2 pin", 0x2 },
1390                 { "CD pin", 0x3 },
1391         },
1392 };
1393
1394 static const struct snd_kcontrol_new cxt5047_test_mixer[] = {
1395
1396         /* Output only controls */
1397         HDA_CODEC_VOLUME("OutAmp-1 Volume", 0x10, 0x0, HDA_OUTPUT),
1398         HDA_CODEC_MUTE("OutAmp-1 Switch", 0x10,0x0, HDA_OUTPUT),
1399         HDA_CODEC_VOLUME("OutAmp-2 Volume", 0x1c, 0x0, HDA_OUTPUT),
1400         HDA_CODEC_MUTE("OutAmp-2 Switch", 0x1c, 0x0, HDA_OUTPUT),
1401         HDA_CODEC_VOLUME("Speaker Playback Volume", 0x1d, 0x0, HDA_OUTPUT),
1402         HDA_CODEC_MUTE("Speaker Playback Switch", 0x1d, 0x0, HDA_OUTPUT),
1403         HDA_CODEC_VOLUME("HeadPhone Playback Volume", 0x13, 0x0, HDA_OUTPUT),
1404         HDA_CODEC_MUTE("HeadPhone Playback Switch", 0x13, 0x0, HDA_OUTPUT),
1405         HDA_CODEC_VOLUME("Line1-Out Playback Volume", 0x14, 0x0, HDA_OUTPUT),
1406         HDA_CODEC_MUTE("Line1-Out Playback Switch", 0x14, 0x0, HDA_OUTPUT),
1407         HDA_CODEC_VOLUME("Line2-Out Playback Volume", 0x15, 0x0, HDA_OUTPUT),
1408         HDA_CODEC_MUTE("Line2-Out Playback Switch", 0x15, 0x0, HDA_OUTPUT),
1409
1410         /* Modes for retasking pin widgets */
1411         CXT_PIN_MODE("LINE1 pin mode", 0x14, CXT_PIN_DIR_INOUT),
1412         CXT_PIN_MODE("MIC1 pin mode", 0x15, CXT_PIN_DIR_INOUT),
1413
1414         /* EAPD Switch Control */
1415         CXT_EAPD_SWITCH("External Amplifier", 0x13, 0x0),
1416
1417         /* Loopback mixer controls */
1418         HDA_CODEC_VOLUME("MIC1 Playback Volume", 0x12, 0x01, HDA_INPUT),
1419         HDA_CODEC_MUTE("MIC1 Playback Switch", 0x12, 0x01, HDA_INPUT),
1420         HDA_CODEC_VOLUME("MIC2 Playback Volume", 0x12, 0x02, HDA_INPUT),
1421         HDA_CODEC_MUTE("MIC2 Playback Switch", 0x12, 0x02, HDA_INPUT),
1422         HDA_CODEC_VOLUME("LINE Playback Volume", 0x12, 0x0, HDA_INPUT),
1423         HDA_CODEC_MUTE("LINE Playback Switch", 0x12, 0x0, HDA_INPUT),
1424         HDA_CODEC_VOLUME("CD Playback Volume", 0x12, 0x04, HDA_INPUT),
1425         HDA_CODEC_MUTE("CD Playback Switch", 0x12, 0x04, HDA_INPUT),
1426
1427         HDA_CODEC_VOLUME("Capture-1 Volume", 0x19, 0x0, HDA_INPUT),
1428         HDA_CODEC_MUTE("Capture-1 Switch", 0x19, 0x0, HDA_INPUT),
1429         HDA_CODEC_VOLUME("Capture-2 Volume", 0x19, 0x1, HDA_INPUT),
1430         HDA_CODEC_MUTE("Capture-2 Switch", 0x19, 0x1, HDA_INPUT),
1431         HDA_CODEC_VOLUME("Capture-3 Volume", 0x19, 0x2, HDA_INPUT),
1432         HDA_CODEC_MUTE("Capture-3 Switch", 0x19, 0x2, HDA_INPUT),
1433         HDA_CODEC_VOLUME("Capture-4 Volume", 0x19, 0x3, HDA_INPUT),
1434         HDA_CODEC_MUTE("Capture-4 Switch", 0x19, 0x3, HDA_INPUT),
1435         {
1436                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1437                 .name = "Input Source",
1438                 .info = conexant_mux_enum_info,
1439                 .get = conexant_mux_enum_get,
1440                 .put = conexant_mux_enum_put,
1441         },
1442         HDA_CODEC_VOLUME("Mic Boost Volume", 0x1a, 0x0, HDA_OUTPUT),
1443
1444         { } /* end */
1445 };
1446
1447 static const struct hda_verb cxt5047_test_init_verbs[] = {
1448         /* Enable retasking pins as output, initially without power amp */
1449         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1450         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1451         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1452
1453         /* Disable digital (SPDIF) pins initially, but users can enable
1454          * them via a mixer switch.  In the case of SPDIF-out, this initverb
1455          * payload also sets the generation to 0, output to be in "consumer"
1456          * PCM format, copyright asserted, no pre-emphasis and no validity
1457          * control.
1458          */
1459         {0x18, AC_VERB_SET_DIGI_CONVERT_1, 0},
1460
1461         /* Ensure mic1, mic2, line1 pin widgets take input from the 
1462          * OUT1 sum bus when acting as an output.
1463          */
1464         {0x1a, AC_VERB_SET_CONNECT_SEL, 0},
1465         {0x1b, AC_VERB_SET_CONNECT_SEL, 0},
1466
1467         /* Start with output sum widgets muted and their output gains at min */
1468         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1469         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
1470
1471         /* Unmute retasking pin widget output buffers since the default
1472          * state appears to be output.  As the pin mode is changed by the
1473          * user the pin mode control will take care of enabling the pin's
1474          * input/output buffers as needed.
1475          */
1476         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1477         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1478         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1479
1480         /* Mute capture amp left and right */
1481         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1482
1483         /* Set ADC connection select to match default mixer setting (mic1
1484          * pin)
1485          */
1486         {0x12, AC_VERB_SET_CONNECT_SEL, 0x00},
1487
1488         /* Mute all inputs to mixer widget (even unconnected ones) */
1489         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)}, /* mic1 pin */
1490         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)}, /* mic2 pin */
1491         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)}, /* line1 pin */
1492         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)}, /* line2 pin */
1493         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)}, /* CD pin */
1494         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(5)}, /* Beep-gen pin */
1495         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(6)}, /* Line-out pin */
1496         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(7)}, /* HP-pin pin */
1497
1498         { }
1499 };
1500 #endif
1501
1502
1503 /* initialize jack-sensing, too */
1504 static int cxt5047_hp_init(struct hda_codec *codec)
1505 {
1506         conexant_init(codec);
1507         cxt5047_hp_automute(codec);
1508         return 0;
1509 }
1510
1511
1512 enum {
1513         CXT5047_LAPTOP,         /* Laptops w/o EAPD support */
1514         CXT5047_LAPTOP_HP,      /* Some HP laptops */
1515         CXT5047_LAPTOP_EAPD,    /* Laptops with EAPD support */
1516 #ifdef CONFIG_SND_DEBUG
1517         CXT5047_TEST,
1518 #endif
1519         CXT5047_AUTO,
1520         CXT5047_MODELS
1521 };
1522
1523 static const char * const cxt5047_models[CXT5047_MODELS] = {
1524         [CXT5047_LAPTOP]        = "laptop",
1525         [CXT5047_LAPTOP_HP]     = "laptop-hp",
1526         [CXT5047_LAPTOP_EAPD]   = "laptop-eapd",
1527 #ifdef CONFIG_SND_DEBUG
1528         [CXT5047_TEST]          = "test",
1529 #endif
1530         [CXT5047_AUTO]          = "auto",
1531 };
1532
1533 static const struct snd_pci_quirk cxt5047_cfg_tbl[] = {
1534         SND_PCI_QUIRK(0x103c, 0x30a5, "HP DV5200T/DV8000T", CXT5047_LAPTOP_HP),
1535         SND_PCI_QUIRK_MASK(0x103c, 0xff00, 0x3000, "HP DV Series",
1536                            CXT5047_LAPTOP),
1537         SND_PCI_QUIRK(0x1179, 0xff31, "Toshiba P100", CXT5047_LAPTOP_EAPD),
1538         {}
1539 };
1540
1541 static int patch_cxt5047(struct hda_codec *codec)
1542 {
1543         struct conexant_spec *spec;
1544         int board_config;
1545
1546         board_config = snd_hda_check_board_config(codec, CXT5047_MODELS,
1547                                                   cxt5047_models,
1548                                                   cxt5047_cfg_tbl);
1549 #if 0 /* not enabled as default, as BIOS often broken for this codec */
1550         if (board_config < 0)
1551                 board_config = CXT5047_AUTO;
1552 #endif
1553         if (board_config == CXT5047_AUTO)
1554                 return patch_conexant_auto(codec);
1555
1556         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1557         if (!spec)
1558                 return -ENOMEM;
1559         codec->spec = spec;
1560         codec->pin_amp_workaround = 1;
1561
1562         spec->multiout.max_channels = 2;
1563         spec->multiout.num_dacs = ARRAY_SIZE(cxt5047_dac_nids);
1564         spec->multiout.dac_nids = cxt5047_dac_nids;
1565         spec->multiout.dig_out_nid = CXT5047_SPDIF_OUT;
1566         spec->num_adc_nids = 1;
1567         spec->adc_nids = cxt5047_adc_nids;
1568         spec->capsrc_nids = cxt5047_capsrc_nids;
1569         spec->num_mixers = 1;
1570         spec->mixers[0] = cxt5047_base_mixers;
1571         spec->num_init_verbs = 1;
1572         spec->init_verbs[0] = cxt5047_init_verbs;
1573         spec->spdif_route = 0;
1574         spec->num_channel_mode = ARRAY_SIZE(cxt5047_modes),
1575         spec->channel_mode = cxt5047_modes,
1576
1577         codec->patch_ops = conexant_patch_ops;
1578
1579         switch (board_config) {
1580         case CXT5047_LAPTOP:
1581                 spec->num_mixers = 2;
1582                 spec->mixers[1] = cxt5047_hp_spk_mixers;
1583                 codec->patch_ops.unsol_event = cxt5047_hp_unsol_event;
1584                 break;
1585         case CXT5047_LAPTOP_HP:
1586                 spec->num_mixers = 2;
1587                 spec->mixers[1] = cxt5047_hp_only_mixers;
1588                 codec->patch_ops.unsol_event = cxt5047_hp_unsol_event;
1589                 codec->patch_ops.init = cxt5047_hp_init;
1590                 break;
1591         case CXT5047_LAPTOP_EAPD:
1592                 spec->input_mux = &cxt5047_toshiba_capture_source;
1593                 spec->num_mixers = 2;
1594                 spec->mixers[1] = cxt5047_hp_spk_mixers;
1595                 spec->num_init_verbs = 2;
1596                 spec->init_verbs[1] = cxt5047_toshiba_init_verbs;
1597                 codec->patch_ops.unsol_event = cxt5047_hp_unsol_event;
1598                 break;
1599 #ifdef CONFIG_SND_DEBUG
1600         case CXT5047_TEST:
1601                 spec->input_mux = &cxt5047_test_capture_source;
1602                 spec->mixers[0] = cxt5047_test_mixer;
1603                 spec->init_verbs[0] = cxt5047_test_init_verbs;
1604                 codec->patch_ops.unsol_event = cxt5047_hp_unsol_event;
1605 #endif  
1606         }
1607         spec->vmaster_nid = 0x13;
1608
1609         switch (codec->subsystem_id >> 16) {
1610         case 0x103c:
1611                 /* HP laptops have really bad sound over 0 dB on NID 0x10.
1612                  * Fix max PCM level to 0 dB (originally it has 0x1e steps
1613                  * with 0 dB offset 0x17)
1614                  */
1615                 snd_hda_override_amp_caps(codec, 0x10, HDA_INPUT,
1616                                           (0x17 << AC_AMPCAP_OFFSET_SHIFT) |
1617                                           (0x17 << AC_AMPCAP_NUM_STEPS_SHIFT) |
1618                                           (0x05 << AC_AMPCAP_STEP_SIZE_SHIFT) |
1619                                           (1 << AC_AMPCAP_MUTE_SHIFT));
1620                 break;
1621         }
1622
1623         return 0;
1624 }
1625
1626 /* Conexant 5051 specific */
1627 static const hda_nid_t cxt5051_dac_nids[1] = { 0x10 };
1628 static const hda_nid_t cxt5051_adc_nids[2] = { 0x14, 0x15 };
1629
1630 static const struct hda_channel_mode cxt5051_modes[1] = {
1631         { 2, NULL },
1632 };
1633
1634 static void cxt5051_update_speaker(struct hda_codec *codec)
1635 {
1636         struct conexant_spec *spec = codec->spec;
1637         unsigned int pinctl;
1638         /* headphone pin */
1639         pinctl = (spec->hp_present && spec->cur_eapd) ? PIN_HP : 0;
1640         snd_hda_codec_write(codec, 0x16, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
1641                             pinctl);
1642         /* speaker pin */
1643         pinctl = (!spec->hp_present && spec->cur_eapd) ? PIN_OUT : 0;
1644         snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
1645                             pinctl);
1646         /* on ideapad there is an aditional speaker (subwoofer) to mute */
1647         if (spec->ideapad)
1648                 snd_hda_codec_write(codec, 0x1b, 0,
1649                                     AC_VERB_SET_PIN_WIDGET_CONTROL,
1650                                     pinctl);
1651 }
1652
1653 /* turn on/off EAPD (+ mute HP) as a master switch */
1654 static int cxt5051_hp_master_sw_put(struct snd_kcontrol *kcontrol,
1655                                     struct snd_ctl_elem_value *ucontrol)
1656 {
1657         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1658
1659         if (!cxt_eapd_put(kcontrol, ucontrol))
1660                 return 0;
1661         cxt5051_update_speaker(codec);
1662         return 1;
1663 }
1664
1665 /* toggle input of built-in and mic jack appropriately */
1666 static void cxt5051_portb_automic(struct hda_codec *codec)
1667 {
1668         struct conexant_spec *spec = codec->spec;
1669         unsigned int present;
1670
1671         if (!(spec->auto_mic & AUTO_MIC_PORTB))
1672                 return;
1673         present = snd_hda_jack_detect(codec, 0x17);
1674         snd_hda_codec_write(codec, 0x14, 0,
1675                             AC_VERB_SET_CONNECT_SEL,
1676                             present ? 0x01 : 0x00);
1677 }
1678
1679 /* switch the current ADC according to the jack state */
1680 static void cxt5051_portc_automic(struct hda_codec *codec)
1681 {
1682         struct conexant_spec *spec = codec->spec;
1683         unsigned int present;
1684         hda_nid_t new_adc;
1685
1686         if (!(spec->auto_mic & AUTO_MIC_PORTC))
1687                 return;
1688         present = snd_hda_jack_detect(codec, 0x18);
1689         if (present)
1690                 spec->cur_adc_idx = 1;
1691         else
1692                 spec->cur_adc_idx = 0;
1693         new_adc = spec->adc_nids[spec->cur_adc_idx];
1694         if (spec->cur_adc && spec->cur_adc != new_adc) {
1695                 /* stream is running, let's swap the current ADC */
1696                 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
1697                 spec->cur_adc = new_adc;
1698                 snd_hda_codec_setup_stream(codec, new_adc,
1699                                            spec->cur_adc_stream_tag, 0,
1700                                            spec->cur_adc_format);
1701         }
1702 }
1703
1704 /* mute internal speaker if HP is plugged */
1705 static void cxt5051_hp_automute(struct hda_codec *codec)
1706 {
1707         struct conexant_spec *spec = codec->spec;
1708
1709         spec->hp_present = snd_hda_jack_detect(codec, 0x16);
1710         cxt5051_update_speaker(codec);
1711 }
1712
1713 /* unsolicited event for HP jack sensing */
1714 static void cxt5051_hp_unsol_event(struct hda_codec *codec,
1715                                    unsigned int res)
1716 {
1717         int nid = (res & AC_UNSOL_RES_SUBTAG) >> 20;
1718         switch (res >> 26) {
1719         case CONEXANT_HP_EVENT:
1720                 cxt5051_hp_automute(codec);
1721                 break;
1722         case CXT5051_PORTB_EVENT:
1723                 cxt5051_portb_automic(codec);
1724                 break;
1725         case CXT5051_PORTC_EVENT:
1726                 cxt5051_portc_automic(codec);
1727                 break;
1728         }
1729         snd_hda_input_jack_report(codec, nid);
1730 }
1731
1732 static const struct snd_kcontrol_new cxt5051_playback_mixers[] = {
1733         HDA_CODEC_VOLUME("Master Playback Volume", 0x10, 0x00, HDA_OUTPUT),
1734         {
1735                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1736                 .name = "Master Playback Switch",
1737                 .info = cxt_eapd_info,
1738                 .get = cxt_eapd_get,
1739                 .put = cxt5051_hp_master_sw_put,
1740                 .private_value = 0x1a,
1741         },
1742         {}
1743 };
1744
1745 static const struct snd_kcontrol_new cxt5051_capture_mixers[] = {
1746         HDA_CODEC_VOLUME("Internal Mic Volume", 0x14, 0x00, HDA_INPUT),
1747         HDA_CODEC_MUTE("Internal Mic Switch", 0x14, 0x00, HDA_INPUT),
1748         HDA_CODEC_VOLUME("Mic Volume", 0x14, 0x01, HDA_INPUT),
1749         HDA_CODEC_MUTE("Mic Switch", 0x14, 0x01, HDA_INPUT),
1750         HDA_CODEC_VOLUME("Docking Mic Volume", 0x15, 0x00, HDA_INPUT),
1751         HDA_CODEC_MUTE("Docking Mic Switch", 0x15, 0x00, HDA_INPUT),
1752         {}
1753 };
1754
1755 static const struct snd_kcontrol_new cxt5051_hp_mixers[] = {
1756         HDA_CODEC_VOLUME("Internal Mic Volume", 0x14, 0x00, HDA_INPUT),
1757         HDA_CODEC_MUTE("Internal Mic Switch", 0x14, 0x00, HDA_INPUT),
1758         HDA_CODEC_VOLUME("Mic Volume", 0x15, 0x00, HDA_INPUT),
1759         HDA_CODEC_MUTE("Mic Switch", 0x15, 0x00, HDA_INPUT),
1760         {}
1761 };
1762
1763 static const struct snd_kcontrol_new cxt5051_hp_dv6736_mixers[] = {
1764         HDA_CODEC_VOLUME("Capture Volume", 0x14, 0x00, HDA_INPUT),
1765         HDA_CODEC_MUTE("Capture Switch", 0x14, 0x00, HDA_INPUT),
1766         {}
1767 };
1768
1769 static const struct snd_kcontrol_new cxt5051_f700_mixers[] = {
1770         HDA_CODEC_VOLUME("Capture Volume", 0x14, 0x01, HDA_INPUT),
1771         HDA_CODEC_MUTE("Capture Switch", 0x14, 0x01, HDA_INPUT),
1772         {}
1773 };
1774
1775 static const struct snd_kcontrol_new cxt5051_toshiba_mixers[] = {
1776         HDA_CODEC_VOLUME("Internal Mic Volume", 0x14, 0x00, HDA_INPUT),
1777         HDA_CODEC_MUTE("Internal Mic Switch", 0x14, 0x00, HDA_INPUT),
1778         HDA_CODEC_VOLUME("Mic Volume", 0x14, 0x01, HDA_INPUT),
1779         HDA_CODEC_MUTE("Mic Switch", 0x14, 0x01, HDA_INPUT),
1780         {}
1781 };
1782
1783 static const struct hda_verb cxt5051_init_verbs[] = {
1784         /* Line in, Mic */
1785         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x03},
1786         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1787         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x03},
1788         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1789         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
1790         {0x1d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x03},
1791         /* SPK  */
1792         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1793         {0x1a, AC_VERB_SET_CONNECT_SEL, 0x00},
1794         /* HP, Amp  */
1795         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1796         {0x16, AC_VERB_SET_CONNECT_SEL, 0x00},
1797         /* DAC1 */      
1798         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1799         /* Record selector: Internal mic */
1800         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x44},
1801         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1) | 0x44},
1802         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x44},
1803         /* SPDIF route: PCM */
1804         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1805         {0x1c, AC_VERB_SET_CONNECT_SEL, 0x0},
1806         /* EAPD */
1807         {0x1a, AC_VERB_SET_EAPD_BTLENABLE, 0x2}, /* default on */ 
1808         {0x16, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN|CONEXANT_HP_EVENT},
1809         { } /* end */
1810 };
1811
1812 static const struct hda_verb cxt5051_hp_dv6736_init_verbs[] = {
1813         /* Line in, Mic */
1814         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x03},
1815         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1816         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x0},
1817         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x0},
1818         /* SPK  */
1819         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1820         {0x1a, AC_VERB_SET_CONNECT_SEL, 0x00},
1821         /* HP, Amp  */
1822         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1823         {0x16, AC_VERB_SET_CONNECT_SEL, 0x00},
1824         /* DAC1 */
1825         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1826         /* Record selector: Internal mic */
1827         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1) | 0x44},
1828         {0x14, AC_VERB_SET_CONNECT_SEL, 0x1},
1829         /* SPDIF route: PCM */
1830         {0x1c, AC_VERB_SET_CONNECT_SEL, 0x0},
1831         /* EAPD */
1832         {0x1a, AC_VERB_SET_EAPD_BTLENABLE, 0x2}, /* default on */
1833         {0x16, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN|CONEXANT_HP_EVENT},
1834         { } /* end */
1835 };
1836
1837 static const struct hda_verb cxt5051_lenovo_x200_init_verbs[] = {
1838         /* Line in, Mic */
1839         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x03},
1840         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1841         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x03},
1842         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1843         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
1844         {0x1d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x03},
1845         /* SPK  */
1846         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1847         {0x1a, AC_VERB_SET_CONNECT_SEL, 0x00},
1848         /* HP, Amp  */
1849         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1850         {0x16, AC_VERB_SET_CONNECT_SEL, 0x00},
1851         /* Docking HP */
1852         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1853         {0x19, AC_VERB_SET_CONNECT_SEL, 0x00},
1854         /* DAC1 */
1855         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1856         /* Record selector: Internal mic */
1857         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x44},
1858         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1) | 0x44},
1859         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x44},
1860         /* SPDIF route: PCM */
1861         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT}, /* needed for W500 Advanced Mini Dock 250410 */
1862         {0x1c, AC_VERB_SET_CONNECT_SEL, 0x0},
1863         /* EAPD */
1864         {0x1a, AC_VERB_SET_EAPD_BTLENABLE, 0x2}, /* default on */
1865         {0x16, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN|CONEXANT_HP_EVENT},
1866         {0x19, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN|CONEXANT_HP_EVENT},
1867         { } /* end */
1868 };
1869
1870 static const struct hda_verb cxt5051_f700_init_verbs[] = {
1871         /* Line in, Mic */
1872         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x03},
1873         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1874         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x0},
1875         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x0},
1876         /* SPK  */
1877         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1878         {0x1a, AC_VERB_SET_CONNECT_SEL, 0x00},
1879         /* HP, Amp  */
1880         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1881         {0x16, AC_VERB_SET_CONNECT_SEL, 0x00},
1882         /* DAC1 */
1883         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1884         /* Record selector: Internal mic */
1885         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1) | 0x44},
1886         {0x14, AC_VERB_SET_CONNECT_SEL, 0x1},
1887         /* SPDIF route: PCM */
1888         {0x1c, AC_VERB_SET_CONNECT_SEL, 0x0},
1889         /* EAPD */
1890         {0x1a, AC_VERB_SET_EAPD_BTLENABLE, 0x2}, /* default on */
1891         {0x16, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN|CONEXANT_HP_EVENT},
1892         { } /* end */
1893 };
1894
1895 static void cxt5051_init_mic_port(struct hda_codec *codec, hda_nid_t nid,
1896                                  unsigned int event)
1897 {
1898         snd_hda_codec_write(codec, nid, 0,
1899                             AC_VERB_SET_UNSOLICITED_ENABLE,
1900                             AC_USRSP_EN | event);
1901         snd_hda_input_jack_add(codec, nid, SND_JACK_MICROPHONE, NULL);
1902         snd_hda_input_jack_report(codec, nid);
1903 }
1904
1905 static const struct hda_verb cxt5051_ideapad_init_verbs[] = {
1906         /* Subwoofer */
1907         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1908         {0x1b, AC_VERB_SET_CONNECT_SEL, 0x00},
1909         { } /* end */
1910 };
1911
1912 /* initialize jack-sensing, too */
1913 static int cxt5051_init(struct hda_codec *codec)
1914 {
1915         struct conexant_spec *spec = codec->spec;
1916
1917         conexant_init(codec);
1918         conexant_init_jacks(codec);
1919
1920         if (spec->auto_mic & AUTO_MIC_PORTB)
1921                 cxt5051_init_mic_port(codec, 0x17, CXT5051_PORTB_EVENT);
1922         if (spec->auto_mic & AUTO_MIC_PORTC)
1923                 cxt5051_init_mic_port(codec, 0x18, CXT5051_PORTC_EVENT);
1924
1925         if (codec->patch_ops.unsol_event) {
1926                 cxt5051_hp_automute(codec);
1927                 cxt5051_portb_automic(codec);
1928                 cxt5051_portc_automic(codec);
1929         }
1930         return 0;
1931 }
1932
1933
1934 enum {
1935         CXT5051_LAPTOP,  /* Laptops w/ EAPD support */
1936         CXT5051_HP,     /* no docking */
1937         CXT5051_HP_DV6736,      /* HP without mic switch */
1938         CXT5051_LENOVO_X200,    /* Lenovo X200 laptop, also used for Advanced Mini Dock 250410 */
1939         CXT5051_F700,       /* HP Compaq Presario F700 */
1940         CXT5051_TOSHIBA,        /* Toshiba M300 & co */
1941         CXT5051_IDEAPAD,        /* Lenovo IdeaPad Y430 */
1942         CXT5051_AUTO,           /* auto-parser */
1943         CXT5051_MODELS
1944 };
1945
1946 static const char *const cxt5051_models[CXT5051_MODELS] = {
1947         [CXT5051_LAPTOP]        = "laptop",
1948         [CXT5051_HP]            = "hp",
1949         [CXT5051_HP_DV6736]     = "hp-dv6736",
1950         [CXT5051_LENOVO_X200]   = "lenovo-x200",
1951         [CXT5051_F700]          = "hp-700",
1952         [CXT5051_TOSHIBA]       = "toshiba",
1953         [CXT5051_IDEAPAD]       = "ideapad",
1954         [CXT5051_AUTO]          = "auto",
1955 };
1956
1957 static const struct snd_pci_quirk cxt5051_cfg_tbl[] = {
1958         SND_PCI_QUIRK(0x103c, 0x30cf, "HP DV6736", CXT5051_HP_DV6736),
1959         SND_PCI_QUIRK(0x103c, 0x360b, "Compaq Presario CQ60", CXT5051_HP),
1960         SND_PCI_QUIRK(0x103c, 0x30ea, "Compaq Presario F700", CXT5051_F700),
1961         SND_PCI_QUIRK(0x1179, 0xff50, "Toshiba M30x", CXT5051_TOSHIBA),
1962         SND_PCI_QUIRK(0x14f1, 0x0101, "Conexant Reference board",
1963                       CXT5051_LAPTOP),
1964         SND_PCI_QUIRK(0x14f1, 0x5051, "HP Spartan 1.1", CXT5051_HP),
1965         SND_PCI_QUIRK(0x17aa, 0x20f2, "Lenovo X200", CXT5051_LENOVO_X200),
1966         SND_PCI_QUIRK(0x17aa, 0x3a0d, "Lenovo IdeaPad", CXT5051_IDEAPAD),
1967         {}
1968 };
1969
1970 static int patch_cxt5051(struct hda_codec *codec)
1971 {
1972         struct conexant_spec *spec;
1973         int board_config;
1974
1975         board_config = snd_hda_check_board_config(codec, CXT5051_MODELS,
1976                                                   cxt5051_models,
1977                                                   cxt5051_cfg_tbl);
1978 #if 0 /* use the old method just for safety */
1979         if (board_config < 0)
1980                 board_config = CXT5051_AUTO;
1981 #endif
1982         if (board_config == CXT5051_AUTO)
1983                 return patch_conexant_auto(codec);
1984
1985         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1986         if (!spec)
1987                 return -ENOMEM;
1988         codec->spec = spec;
1989         codec->pin_amp_workaround = 1;
1990
1991         codec->patch_ops = conexant_patch_ops;
1992         codec->patch_ops.init = cxt5051_init;
1993
1994         spec->multiout.max_channels = 2;
1995         spec->multiout.num_dacs = ARRAY_SIZE(cxt5051_dac_nids);
1996         spec->multiout.dac_nids = cxt5051_dac_nids;
1997         spec->multiout.dig_out_nid = CXT5051_SPDIF_OUT;
1998         spec->num_adc_nids = 1; /* not 2; via auto-mic switch */
1999         spec->adc_nids = cxt5051_adc_nids;
2000         spec->num_mixers = 2;
2001         spec->mixers[0] = cxt5051_capture_mixers;
2002         spec->mixers[1] = cxt5051_playback_mixers;
2003         spec->num_init_verbs = 1;
2004         spec->init_verbs[0] = cxt5051_init_verbs;
2005         spec->spdif_route = 0;
2006         spec->num_channel_mode = ARRAY_SIZE(cxt5051_modes);
2007         spec->channel_mode = cxt5051_modes;
2008         spec->cur_adc = 0;
2009         spec->cur_adc_idx = 0;
2010
2011         set_beep_amp(spec, 0x13, 0, HDA_OUTPUT);
2012
2013         codec->patch_ops.unsol_event = cxt5051_hp_unsol_event;
2014
2015         spec->auto_mic = AUTO_MIC_PORTB | AUTO_MIC_PORTC;
2016         switch (board_config) {
2017         case CXT5051_HP:
2018                 spec->mixers[0] = cxt5051_hp_mixers;
2019                 break;
2020         case CXT5051_HP_DV6736:
2021                 spec->init_verbs[0] = cxt5051_hp_dv6736_init_verbs;
2022                 spec->mixers[0] = cxt5051_hp_dv6736_mixers;
2023                 spec->auto_mic = 0;
2024                 break;
2025         case CXT5051_LENOVO_X200:
2026                 spec->init_verbs[0] = cxt5051_lenovo_x200_init_verbs;
2027                 /* Thinkpad X301 does not have S/PDIF wired and no ability
2028                    to use a docking station. */
2029                 if (codec->subsystem_id == 0x17aa211f)
2030                         spec->multiout.dig_out_nid = 0;
2031                 break;
2032         case CXT5051_F700:
2033                 spec->init_verbs[0] = cxt5051_f700_init_verbs;
2034                 spec->mixers[0] = cxt5051_f700_mixers;
2035                 spec->auto_mic = 0;
2036                 break;
2037         case CXT5051_TOSHIBA:
2038                 spec->mixers[0] = cxt5051_toshiba_mixers;
2039                 spec->auto_mic = AUTO_MIC_PORTB;
2040                 break;
2041         case CXT5051_IDEAPAD:
2042                 spec->init_verbs[spec->num_init_verbs++] =
2043                         cxt5051_ideapad_init_verbs;
2044                 spec->ideapad = 1;
2045                 break;
2046         }
2047
2048         if (spec->beep_amp)
2049                 snd_hda_attach_beep_device(codec, spec->beep_amp);
2050
2051         return 0;
2052 }
2053
2054 /* Conexant 5066 specific */
2055
2056 static const hda_nid_t cxt5066_dac_nids[1] = { 0x10 };
2057 static const hda_nid_t cxt5066_adc_nids[3] = { 0x14, 0x15, 0x16 };
2058 static const hda_nid_t cxt5066_capsrc_nids[1] = { 0x17 };
2059 static const hda_nid_t cxt5066_digout_pin_nids[2] = { 0x20, 0x22 };
2060
2061 /* OLPC's microphone port is DC coupled for use with external sensors,
2062  * therefore we use a 50% mic bias in order to center the input signal with
2063  * the DC input range of the codec. */
2064 #define CXT5066_OLPC_EXT_MIC_BIAS PIN_VREF50
2065
2066 static const struct hda_channel_mode cxt5066_modes[1] = {
2067         { 2, NULL },
2068 };
2069
2070 #define HP_PRESENT_PORT_A       (1 << 0)
2071 #define HP_PRESENT_PORT_D       (1 << 1)
2072 #define hp_port_a_present(spec) ((spec)->hp_present & HP_PRESENT_PORT_A)
2073 #define hp_port_d_present(spec) ((spec)->hp_present & HP_PRESENT_PORT_D)
2074
2075 static void cxt5066_update_speaker(struct hda_codec *codec)
2076 {
2077         struct conexant_spec *spec = codec->spec;
2078         unsigned int pinctl;
2079
2080         snd_printdd("CXT5066: update speaker, hp_present=%d, cur_eapd=%d\n",
2081                     spec->hp_present, spec->cur_eapd);
2082
2083         /* Port A (HP) */
2084         pinctl = (hp_port_a_present(spec) && spec->cur_eapd) ? PIN_HP : 0;
2085         snd_hda_codec_write(codec, 0x19, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
2086                         pinctl);
2087
2088         /* Port D (HP/LO) */
2089         pinctl = spec->cur_eapd ? spec->port_d_mode : 0;
2090         if (spec->dell_automute || spec->thinkpad) {
2091                 /* Mute if Port A is connected */
2092                 if (hp_port_a_present(spec))
2093                         pinctl = 0;
2094         } else {
2095                 /* Thinkpad/Dell doesn't give pin-D status */
2096                 if (!hp_port_d_present(spec))
2097                         pinctl = 0;
2098         }
2099         snd_hda_codec_write(codec, 0x1c, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
2100                         pinctl);
2101
2102         /* CLASS_D AMP */
2103         pinctl = (!spec->hp_present && spec->cur_eapd) ? PIN_OUT : 0;
2104         snd_hda_codec_write(codec, 0x1f, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
2105                         pinctl);
2106 }
2107
2108 /* turn on/off EAPD (+ mute HP) as a master switch */
2109 static int cxt5066_hp_master_sw_put(struct snd_kcontrol *kcontrol,
2110                                     struct snd_ctl_elem_value *ucontrol)
2111 {
2112         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2113
2114         if (!cxt_eapd_put(kcontrol, ucontrol))
2115                 return 0;
2116
2117         cxt5066_update_speaker(codec);
2118         return 1;
2119 }
2120
2121 static const struct hda_input_mux cxt5066_olpc_dc_bias = {
2122         .num_items = 3,
2123         .items = {
2124                 { "Off", PIN_IN },
2125                 { "50%", PIN_VREF50 },
2126                 { "80%", PIN_VREF80 },
2127         },
2128 };
2129
2130 static int cxt5066_set_olpc_dc_bias(struct hda_codec *codec)
2131 {
2132         struct conexant_spec *spec = codec->spec;
2133         /* Even though port F is the DC input, the bias is controlled on port B.
2134          * we also leave that port as an active input (but unselected) in DC mode
2135          * just in case that is necessary to make the bias setting take effect. */
2136         return snd_hda_codec_write_cache(codec, 0x1a, 0,
2137                 AC_VERB_SET_PIN_WIDGET_CONTROL,
2138                 cxt5066_olpc_dc_bias.items[spec->dc_input_bias].index);
2139 }
2140
2141 /* OLPC defers mic widget control until when capture is started because the
2142  * microphone LED comes on as soon as these settings are put in place. if we
2143  * did this before recording, it would give the false indication that recording
2144  * is happening when it is not. */
2145 static void cxt5066_olpc_select_mic(struct hda_codec *codec)
2146 {
2147         struct conexant_spec *spec = codec->spec;
2148         if (!spec->recording)
2149                 return;
2150
2151         if (spec->dc_enable) {
2152                 /* in DC mode we ignore presence detection and just use the jack
2153                  * through our special DC port */
2154                 const struct hda_verb enable_dc_mode[] = {
2155                         /* disble internal mic, port C */
2156                         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2157
2158                         /* enable DC capture, port F */
2159                         {0x1e, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
2160                         {},
2161                 };
2162
2163                 snd_hda_sequence_write(codec, enable_dc_mode);
2164                 /* port B input disabled (and bias set) through the following call */
2165                 cxt5066_set_olpc_dc_bias(codec);
2166                 return;
2167         }
2168
2169         /* disable DC (port F) */
2170         snd_hda_codec_write(codec, 0x1e, 0, AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
2171
2172         /* external mic, port B */
2173         snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
2174                 spec->ext_mic_present ? CXT5066_OLPC_EXT_MIC_BIAS : 0);
2175
2176         /* internal mic, port C */
2177         snd_hda_codec_write(codec, 0x1b, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
2178                 spec->ext_mic_present ? 0 : PIN_VREF80);
2179 }
2180
2181 /* toggle input of built-in and mic jack appropriately */
2182 static void cxt5066_olpc_automic(struct hda_codec *codec)
2183 {
2184         struct conexant_spec *spec = codec->spec;
2185         unsigned int present;
2186
2187         if (spec->dc_enable) /* don't do presence detection in DC mode */
2188                 return;
2189
2190         present = snd_hda_codec_read(codec, 0x1a, 0,
2191                                      AC_VERB_GET_PIN_SENSE, 0) & 0x80000000;
2192         if (present)
2193                 snd_printdd("CXT5066: external microphone detected\n");
2194         else
2195                 snd_printdd("CXT5066: external microphone absent\n");
2196
2197         snd_hda_codec_write(codec, 0x17, 0, AC_VERB_SET_CONNECT_SEL,
2198                 present ? 0 : 1);
2199         spec->ext_mic_present = !!present;
2200
2201         cxt5066_olpc_select_mic(codec);
2202 }
2203
2204 /* toggle input of built-in digital mic and mic jack appropriately */
2205 static void cxt5066_vostro_automic(struct hda_codec *codec)
2206 {
2207         unsigned int present;
2208
2209         struct hda_verb ext_mic_present[] = {
2210                 /* enable external mic, port B */
2211                 {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
2212
2213                 /* switch to external mic input */
2214                 {0x17, AC_VERB_SET_CONNECT_SEL, 0},
2215                 {0x14, AC_VERB_SET_CONNECT_SEL, 0},
2216
2217                 /* disable internal digital mic */
2218                 {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2219                 {}
2220         };
2221         static const struct hda_verb ext_mic_absent[] = {
2222                 /* enable internal mic, port C */
2223                 {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
2224
2225                 /* switch to internal mic input */
2226                 {0x14, AC_VERB_SET_CONNECT_SEL, 2},
2227
2228                 /* disable external mic, port B */
2229                 {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2230                 {}
2231         };
2232
2233         present = snd_hda_jack_detect(codec, 0x1a);
2234         if (present) {
2235                 snd_printdd("CXT5066: external microphone detected\n");
2236                 snd_hda_sequence_write(codec, ext_mic_present);
2237         } else {
2238                 snd_printdd("CXT5066: external microphone absent\n");
2239                 snd_hda_sequence_write(codec, ext_mic_absent);
2240         }
2241 }
2242
2243 /* toggle input of built-in digital mic and mic jack appropriately */
2244 static void cxt5066_ideapad_automic(struct hda_codec *codec)
2245 {
2246         unsigned int present;
2247
2248         struct hda_verb ext_mic_present[] = {
2249                 {0x14, AC_VERB_SET_CONNECT_SEL, 0},
2250                 {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
2251                 {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2252                 {}
2253         };
2254         static const struct hda_verb ext_mic_absent[] = {
2255                 {0x14, AC_VERB_SET_CONNECT_SEL, 2},
2256                 {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
2257                 {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2258                 {}
2259         };
2260
2261         present = snd_hda_jack_detect(codec, 0x1b);
2262         if (present) {
2263                 snd_printdd("CXT5066: external microphone detected\n");
2264                 snd_hda_sequence_write(codec, ext_mic_present);
2265         } else {
2266                 snd_printdd("CXT5066: external microphone absent\n");
2267                 snd_hda_sequence_write(codec, ext_mic_absent);
2268         }
2269 }
2270
2271
2272 /* toggle input of built-in digital mic and mic jack appropriately */
2273 static void cxt5066_asus_automic(struct hda_codec *codec)
2274 {
2275         unsigned int present;
2276
2277         present = snd_hda_jack_detect(codec, 0x1b);
2278         snd_printdd("CXT5066: external microphone present=%d\n", present);
2279         snd_hda_codec_write(codec, 0x17, 0, AC_VERB_SET_CONNECT_SEL,
2280                             present ? 1 : 0);
2281 }
2282
2283
2284 /* toggle input of built-in digital mic and mic jack appropriately */
2285 static void cxt5066_hp_laptop_automic(struct hda_codec *codec)
2286 {
2287         unsigned int present;
2288
2289         present = snd_hda_jack_detect(codec, 0x1b);
2290         snd_printdd("CXT5066: external microphone present=%d\n", present);
2291         snd_hda_codec_write(codec, 0x17, 0, AC_VERB_SET_CONNECT_SEL,
2292                             present ? 1 : 3);
2293 }
2294
2295
2296 /* toggle input of built-in digital mic and mic jack appropriately
2297    order is: external mic -> dock mic -> interal mic */
2298 static void cxt5066_thinkpad_automic(struct hda_codec *codec)
2299 {
2300         unsigned int ext_present, dock_present;
2301
2302         static const struct hda_verb ext_mic_present[] = {
2303                 {0x14, AC_VERB_SET_CONNECT_SEL, 0},
2304                 {0x17, AC_VERB_SET_CONNECT_SEL, 1},
2305                 {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
2306                 {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2307                 {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2308                 {}
2309         };
2310         static const struct hda_verb dock_mic_present[] = {
2311                 {0x14, AC_VERB_SET_CONNECT_SEL, 0},
2312                 {0x17, AC_VERB_SET_CONNECT_SEL, 0},
2313                 {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
2314                 {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2315                 {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2316                 {}
2317         };
2318         static const struct hda_verb ext_mic_absent[] = {
2319                 {0x14, AC_VERB_SET_CONNECT_SEL, 2},
2320                 {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
2321                 {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2322                 {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2323                 {}
2324         };
2325
2326         ext_present = snd_hda_jack_detect(codec, 0x1b);
2327         dock_present = snd_hda_jack_detect(codec, 0x1a);
2328         if (ext_present) {
2329                 snd_printdd("CXT5066: external microphone detected\n");
2330                 snd_hda_sequence_write(codec, ext_mic_present);
2331         } else if (dock_present) {
2332                 snd_printdd("CXT5066: dock microphone detected\n");
2333                 snd_hda_sequence_write(codec, dock_mic_present);
2334         } else {
2335                 snd_printdd("CXT5066: external microphone absent\n");
2336                 snd_hda_sequence_write(codec, ext_mic_absent);
2337         }
2338 }
2339
2340 /* mute internal speaker if HP is plugged */
2341 static void cxt5066_hp_automute(struct hda_codec *codec)
2342 {
2343         struct conexant_spec *spec = codec->spec;
2344         unsigned int portA, portD;
2345
2346         /* Port A */
2347         portA = snd_hda_jack_detect(codec, 0x19);
2348
2349         /* Port D */
2350         portD = snd_hda_jack_detect(codec, 0x1c);
2351
2352         spec->hp_present = portA ? HP_PRESENT_PORT_A : 0;
2353         spec->hp_present |= portD ? HP_PRESENT_PORT_D : 0;
2354         snd_printdd("CXT5066: hp automute portA=%x portD=%x present=%d\n",
2355                 portA, portD, spec->hp_present);
2356         cxt5066_update_speaker(codec);
2357 }
2358
2359 /* Dispatch the right mic autoswitch function */
2360 static void cxt5066_automic(struct hda_codec *codec)
2361 {
2362         struct conexant_spec *spec = codec->spec;
2363
2364         if (spec->dell_vostro)
2365                 cxt5066_vostro_automic(codec);
2366         else if (spec->ideapad)
2367                 cxt5066_ideapad_automic(codec);
2368         else if (spec->thinkpad)
2369                 cxt5066_thinkpad_automic(codec);
2370         else if (spec->hp_laptop)
2371                 cxt5066_hp_laptop_automic(codec);
2372         else if (spec->asus)
2373                 cxt5066_asus_automic(codec);
2374 }
2375
2376 /* unsolicited event for jack sensing */
2377 static void cxt5066_olpc_unsol_event(struct hda_codec *codec, unsigned int res)
2378 {
2379         struct conexant_spec *spec = codec->spec;
2380         snd_printdd("CXT5066: unsol event %x (%x)\n", res, res >> 26);
2381         switch (res >> 26) {
2382         case CONEXANT_HP_EVENT:
2383                 cxt5066_hp_automute(codec);
2384                 break;
2385         case CONEXANT_MIC_EVENT:
2386                 /* ignore mic events in DC mode; we're always using the jack */
2387                 if (!spec->dc_enable)
2388                         cxt5066_olpc_automic(codec);
2389                 break;
2390         }
2391 }
2392
2393 /* unsolicited event for jack sensing */
2394 static void cxt5066_unsol_event(struct hda_codec *codec, unsigned int res)
2395 {
2396         snd_printdd("CXT5066: unsol event %x (%x)\n", res, res >> 26);
2397         switch (res >> 26) {
2398         case CONEXANT_HP_EVENT:
2399                 cxt5066_hp_automute(codec);
2400                 break;
2401         case CONEXANT_MIC_EVENT:
2402                 cxt5066_automic(codec);
2403                 break;
2404         }
2405 }
2406
2407
2408 static const struct hda_input_mux cxt5066_analog_mic_boost = {
2409         .num_items = 5,
2410         .items = {
2411                 { "0dB",  0 },
2412                 { "10dB", 1 },
2413                 { "20dB", 2 },
2414                 { "30dB", 3 },
2415                 { "40dB", 4 },
2416         },
2417 };
2418
2419 static void cxt5066_set_mic_boost(struct hda_codec *codec)
2420 {
2421         struct conexant_spec *spec = codec->spec;
2422         snd_hda_codec_write_cache(codec, 0x17, 0,
2423                 AC_VERB_SET_AMP_GAIN_MUTE,
2424                 AC_AMP_SET_RIGHT | AC_AMP_SET_LEFT | AC_AMP_SET_OUTPUT |
2425                         cxt5066_analog_mic_boost.items[spec->mic_boost].index);
2426         if (spec->ideapad || spec->thinkpad) {
2427                 /* adjust the internal mic as well...it is not through 0x17 */
2428                 snd_hda_codec_write_cache(codec, 0x23, 0,
2429                         AC_VERB_SET_AMP_GAIN_MUTE,
2430                         AC_AMP_SET_RIGHT | AC_AMP_SET_LEFT | AC_AMP_SET_INPUT |
2431                                 cxt5066_analog_mic_boost.
2432                                         items[spec->mic_boost].index);
2433         }
2434 }
2435
2436 static int cxt5066_mic_boost_mux_enum_info(struct snd_kcontrol *kcontrol,
2437                                            struct snd_ctl_elem_info *uinfo)
2438 {
2439         return snd_hda_input_mux_info(&cxt5066_analog_mic_boost, uinfo);
2440 }
2441
2442 static int cxt5066_mic_boost_mux_enum_get(struct snd_kcontrol *kcontrol,
2443                                           struct snd_ctl_elem_value *ucontrol)
2444 {
2445         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2446         struct conexant_spec *spec = codec->spec;
2447         ucontrol->value.enumerated.item[0] = spec->mic_boost;
2448         return 0;
2449 }
2450
2451 static int cxt5066_mic_boost_mux_enum_put(struct snd_kcontrol *kcontrol,
2452                                           struct snd_ctl_elem_value *ucontrol)
2453 {
2454         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2455         struct conexant_spec *spec = codec->spec;
2456         const struct hda_input_mux *imux = &cxt5066_analog_mic_boost;
2457         unsigned int idx;
2458         idx = ucontrol->value.enumerated.item[0];
2459         if (idx >= imux->num_items)
2460                 idx = imux->num_items - 1;
2461
2462         spec->mic_boost = idx;
2463         if (!spec->dc_enable)
2464                 cxt5066_set_mic_boost(codec);
2465         return 1;
2466 }
2467
2468 static void cxt5066_enable_dc(struct hda_codec *codec)
2469 {
2470         const struct hda_verb enable_dc_mode[] = {
2471                 /* disable gain */
2472                 {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2473
2474                 /* switch to DC input */
2475                 {0x17, AC_VERB_SET_CONNECT_SEL, 3},
2476                 {}
2477         };
2478
2479         /* configure as input source */
2480         snd_hda_sequence_write(codec, enable_dc_mode);
2481         cxt5066_olpc_select_mic(codec); /* also sets configured bias */
2482 }
2483
2484 static void cxt5066_disable_dc(struct hda_codec *codec)
2485 {
2486         /* reconfigure input source */
2487         cxt5066_set_mic_boost(codec);
2488         /* automic also selects the right mic if we're recording */
2489         cxt5066_olpc_automic(codec);
2490 }
2491
2492 static int cxt5066_olpc_dc_get(struct snd_kcontrol *kcontrol,
2493                              struct snd_ctl_elem_value *ucontrol)
2494 {
2495         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2496         struct conexant_spec *spec = codec->spec;
2497         ucontrol->value.integer.value[0] = spec->dc_enable;
2498         return 0;
2499 }
2500
2501 static int cxt5066_olpc_dc_put(struct snd_kcontrol *kcontrol,
2502                              struct snd_ctl_elem_value *ucontrol)
2503 {
2504         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2505         struct conexant_spec *spec = codec->spec;
2506         int dc_enable = !!ucontrol->value.integer.value[0];
2507
2508         if (dc_enable == spec->dc_enable)
2509                 return 0;
2510
2511         spec->dc_enable = dc_enable;
2512         if (dc_enable)
2513                 cxt5066_enable_dc(codec);
2514         else
2515                 cxt5066_disable_dc(codec);
2516
2517         return 1;
2518 }
2519
2520 static int cxt5066_olpc_dc_bias_enum_info(struct snd_kcontrol *kcontrol,
2521                                            struct snd_ctl_elem_info *uinfo)
2522 {
2523         return snd_hda_input_mux_info(&cxt5066_olpc_dc_bias, uinfo);
2524 }
2525
2526 static int cxt5066_olpc_dc_bias_enum_get(struct snd_kcontrol *kcontrol,
2527                                           struct snd_ctl_elem_value *ucontrol)
2528 {
2529         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2530         struct conexant_spec *spec = codec->spec;
2531         ucontrol->value.enumerated.item[0] = spec->dc_input_bias;
2532         return 0;
2533 }
2534
2535 static int cxt5066_olpc_dc_bias_enum_put(struct snd_kcontrol *kcontrol,
2536                                           struct snd_ctl_elem_value *ucontrol)
2537 {
2538         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2539         struct conexant_spec *spec = codec->spec;
2540         const struct hda_input_mux *imux = &cxt5066_analog_mic_boost;
2541         unsigned int idx;
2542
2543         idx = ucontrol->value.enumerated.item[0];
2544         if (idx >= imux->num_items)
2545                 idx = imux->num_items - 1;
2546
2547         spec->dc_input_bias = idx;
2548         if (spec->dc_enable)
2549                 cxt5066_set_olpc_dc_bias(codec);
2550         return 1;
2551 }
2552
2553 static void cxt5066_olpc_capture_prepare(struct hda_codec *codec)
2554 {
2555         struct conexant_spec *spec = codec->spec;
2556         /* mark as recording and configure the microphone widget so that the
2557          * recording LED comes on. */
2558         spec->recording = 1;
2559         cxt5066_olpc_select_mic(codec);
2560 }
2561
2562 static void cxt5066_olpc_capture_cleanup(struct hda_codec *codec)
2563 {
2564         struct conexant_spec *spec = codec->spec;
2565         const struct hda_verb disable_mics[] = {
2566                 /* disable external mic, port B */
2567                 {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2568
2569                 /* disble internal mic, port C */
2570                 {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2571
2572                 /* disable DC capture, port F */
2573                 {0x1e, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2574                 {},
2575         };
2576
2577         snd_hda_sequence_write(codec, disable_mics);
2578         spec->recording = 0;
2579 }
2580
2581 static void conexant_check_dig_outs(struct hda_codec *codec,
2582                                     const hda_nid_t *dig_pins,
2583                                     int num_pins)
2584 {
2585         struct conexant_spec *spec = codec->spec;
2586         hda_nid_t *nid_loc = &spec->multiout.dig_out_nid;
2587         int i;
2588
2589         for (i = 0; i < num_pins; i++, dig_pins++) {
2590                 unsigned int cfg = snd_hda_codec_get_pincfg(codec, *dig_pins);
2591                 if (get_defcfg_connect(cfg) == AC_JACK_PORT_NONE)
2592                         continue;
2593                 if (snd_hda_get_connections(codec, *dig_pins, nid_loc, 1) != 1)
2594                         continue;
2595                 if (spec->slave_dig_outs[0])
2596                         nid_loc++;
2597                 else
2598                         nid_loc = spec->slave_dig_outs;
2599         }
2600 }
2601
2602 static const struct hda_input_mux cxt5066_capture_source = {
2603         .num_items = 4,
2604         .items = {
2605                 { "Mic B", 0 },
2606                 { "Mic C", 1 },
2607                 { "Mic E", 2 },
2608                 { "Mic F", 3 },
2609         },
2610 };
2611
2612 static const struct hda_bind_ctls cxt5066_bind_capture_vol_others = {
2613         .ops = &snd_hda_bind_vol,
2614         .values = {
2615                 HDA_COMPOSE_AMP_VAL(0x14, 3, 0, HDA_INPUT),
2616                 HDA_COMPOSE_AMP_VAL(0x14, 3, 2, HDA_INPUT),
2617                 0
2618         },
2619 };
2620
2621 static const struct hda_bind_ctls cxt5066_bind_capture_sw_others = {
2622         .ops = &snd_hda_bind_sw,
2623         .values = {
2624                 HDA_COMPOSE_AMP_VAL(0x14, 3, 0, HDA_INPUT),
2625                 HDA_COMPOSE_AMP_VAL(0x14, 3, 2, HDA_INPUT),
2626                 0
2627         },
2628 };
2629
2630 static const struct snd_kcontrol_new cxt5066_mixer_master[] = {
2631         HDA_CODEC_VOLUME("Master Playback Volume", 0x10, 0x00, HDA_OUTPUT),
2632         {}
2633 };
2634
2635 static const struct snd_kcontrol_new cxt5066_mixer_master_olpc[] = {
2636         {
2637                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2638                 .name = "Master Playback Volume",
2639                 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
2640                                   SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2641                                   SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK,
2642                 .subdevice = HDA_SUBDEV_AMP_FLAG,
2643                 .info = snd_hda_mixer_amp_volume_info,
2644                 .get = snd_hda_mixer_amp_volume_get,
2645                 .put = snd_hda_mixer_amp_volume_put,
2646                 .tlv = { .c = snd_hda_mixer_amp_tlv },
2647                 /* offset by 28 volume steps to limit minimum gain to -46dB */
2648                 .private_value =
2649                         HDA_COMPOSE_AMP_VAL_OFS(0x10, 3, 0, HDA_OUTPUT, 28),
2650         },
2651         {}
2652 };
2653
2654 static const struct snd_kcontrol_new cxt5066_mixer_olpc_dc[] = {
2655         {
2656                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2657                 .name = "DC Mode Enable Switch",
2658                 .info = snd_ctl_boolean_mono_info,
2659                 .get = cxt5066_olpc_dc_get,
2660                 .put = cxt5066_olpc_dc_put,
2661         },
2662         {
2663                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2664                 .name = "DC Input Bias Enum",
2665                 .info = cxt5066_olpc_dc_bias_enum_info,
2666                 .get = cxt5066_olpc_dc_bias_enum_get,
2667                 .put = cxt5066_olpc_dc_bias_enum_put,
2668         },
2669         {}
2670 };
2671
2672 static const struct snd_kcontrol_new cxt5066_mixers[] = {
2673         {
2674                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2675                 .name = "Master Playback Switch",
2676                 .info = cxt_eapd_info,
2677                 .get = cxt_eapd_get,
2678                 .put = cxt5066_hp_master_sw_put,
2679                 .private_value = 0x1d,
2680         },
2681
2682         {
2683                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2684                 .name = "Analog Mic Boost Capture Enum",
2685                 .info = cxt5066_mic_boost_mux_enum_info,
2686                 .get = cxt5066_mic_boost_mux_enum_get,
2687                 .put = cxt5066_mic_boost_mux_enum_put,
2688         },
2689
2690         HDA_BIND_VOL("Capture Volume", &cxt5066_bind_capture_vol_others),
2691         HDA_BIND_SW("Capture Switch", &cxt5066_bind_capture_sw_others),
2692         {}
2693 };
2694
2695 static const struct snd_kcontrol_new cxt5066_vostro_mixers[] = {
2696         {
2697                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2698                 .name = "Internal Mic Boost Capture Enum",
2699                 .info = cxt5066_mic_boost_mux_enum_info,
2700                 .get = cxt5066_mic_boost_mux_enum_get,
2701                 .put = cxt5066_mic_boost_mux_enum_put,
2702                 .private_value = 0x23 | 0x100,
2703         },
2704         {}
2705 };
2706
2707 static const struct hda_verb cxt5066_init_verbs[] = {
2708         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80}, /* Port B */
2709         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80}, /* Port C */
2710         {0x1e, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN}, /* Port F */
2711         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN}, /* Port E */
2712
2713         /* Speakers  */
2714         {0x1f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2715         {0x1f, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2716
2717         /* HP, Amp  */
2718         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
2719         {0x19, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2720
2721         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
2722         {0x1c, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2723
2724         /* DAC1 */
2725         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2726
2727         /* Node 14 connections: 0x17 0x18 0x23 0x24 0x27 */
2728         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x50},
2729         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2730         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2) | 0x50},
2731         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2732         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
2733
2734         /* no digital microphone support yet */
2735         {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2736
2737         /* Audio input selector */
2738         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x3},
2739
2740         /* SPDIF route: PCM */
2741         {0x20, AC_VERB_SET_CONNECT_SEL, 0x0},
2742         {0x22, AC_VERB_SET_CONNECT_SEL, 0x0},
2743
2744         {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2745         {0x22, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2746
2747         /* EAPD */
2748         {0x1d, AC_VERB_SET_EAPD_BTLENABLE, 0x2}, /* default on */
2749
2750         /* not handling these yet */
2751         {0x19, AC_VERB_SET_UNSOLICITED_ENABLE, 0},
2752         {0x1a, AC_VERB_SET_UNSOLICITED_ENABLE, 0},
2753         {0x1b, AC_VERB_SET_UNSOLICITED_ENABLE, 0},
2754         {0x1c, AC_VERB_SET_UNSOLICITED_ENABLE, 0},
2755         {0x1d, AC_VERB_SET_UNSOLICITED_ENABLE, 0},
2756         {0x1e, AC_VERB_SET_UNSOLICITED_ENABLE, 0},
2757         {0x20, AC_VERB_SET_UNSOLICITED_ENABLE, 0},
2758         {0x22, AC_VERB_SET_UNSOLICITED_ENABLE, 0},
2759         { } /* end */
2760 };
2761
2762 static const struct hda_verb cxt5066_init_verbs_olpc[] = {
2763         /* Port A: headphones */
2764         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
2765         {0x19, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2766
2767         /* Port B: external microphone */
2768         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2769
2770         /* Port C: internal microphone */
2771         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2772
2773         /* Port D: unused */
2774         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2775
2776         /* Port E: unused, but has primary EAPD */
2777         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2778         {0x1d, AC_VERB_SET_EAPD_BTLENABLE, 0x2}, /* default on */
2779
2780         /* Port F: external DC input through microphone port */
2781         {0x1e, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2782
2783         /* Port G: internal speakers */
2784         {0x1f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2785         {0x1f, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2786
2787         /* DAC1 */
2788         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2789
2790         /* DAC2: unused */
2791         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2792
2793         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x50},
2794         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2795         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
2796         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2797         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2798         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2799         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
2800         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2801         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2802         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2803         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
2804         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2805
2806         /* Disable digital microphone port */
2807         {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2808
2809         /* Audio input selectors */
2810         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x3},
2811         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE },
2812
2813         /* Disable SPDIF */
2814         {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2815         {0x22, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2816
2817         /* enable unsolicited events for Port A and B */
2818         {0x19, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_HP_EVENT},
2819         {0x1a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_MIC_EVENT},
2820         { } /* end */
2821 };
2822
2823 static const struct hda_verb cxt5066_init_verbs_vostro[] = {
2824         /* Port A: headphones */
2825         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2826         {0x19, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2827
2828         /* Port B: external microphone */
2829         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2830
2831         /* Port C: unused */
2832         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2833
2834         /* Port D: unused */
2835         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2836
2837         /* Port E: unused, but has primary EAPD */
2838         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2839         {0x1d, AC_VERB_SET_EAPD_BTLENABLE, 0x2}, /* default on */
2840
2841         /* Port F: unused */
2842         {0x1e, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2843
2844         /* Port G: internal speakers */
2845         {0x1f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2846         {0x1f, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2847
2848         /* DAC1 */
2849         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2850
2851         /* DAC2: unused */
2852         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2853
2854         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2855         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2856         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
2857         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2858         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2859         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2860         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
2861         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2862         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2863         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2864         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
2865         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2866
2867         /* Digital microphone port */
2868         {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
2869
2870         /* Audio input selectors */
2871         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x3},
2872         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE },
2873
2874         /* Disable SPDIF */
2875         {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2876         {0x22, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2877
2878         /* enable unsolicited events for Port A and B */
2879         {0x19, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_HP_EVENT},
2880         {0x1a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_MIC_EVENT},
2881         { } /* end */
2882 };
2883
2884 static const struct hda_verb cxt5066_init_verbs_ideapad[] = {
2885         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80}, /* Port B */
2886         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80}, /* Port C */
2887         {0x1e, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN}, /* Port F */
2888         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN}, /* Port E */
2889
2890         /* Speakers  */
2891         {0x1f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2892         {0x1f, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2893
2894         /* HP, Amp  */
2895         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
2896         {0x19, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2897
2898         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
2899         {0x1c, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2900
2901         /* DAC1 */
2902         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2903
2904         /* Node 14 connections: 0x17 0x18 0x23 0x24 0x27 */
2905         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x50},
2906         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2907         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2) | 0x50},
2908         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2909         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
2910         {0x14, AC_VERB_SET_CONNECT_SEL, 2},     /* default to internal mic */
2911
2912         /* Audio input selector */
2913         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x2},
2914         {0x17, AC_VERB_SET_CONNECT_SEL, 1},     /* route ext mic */
2915
2916         /* SPDIF route: PCM */
2917         {0x20, AC_VERB_SET_CONNECT_SEL, 0x0},
2918         {0x22, AC_VERB_SET_CONNECT_SEL, 0x0},
2919
2920         {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2921         {0x22, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2922
2923         /* internal microphone */
2924         {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN}, /* enable internal mic */
2925
2926         /* EAPD */
2927         {0x1d, AC_VERB_SET_EAPD_BTLENABLE, 0x2}, /* default on */
2928
2929         {0x19, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_HP_EVENT},
2930         {0x1b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_MIC_EVENT},
2931         { } /* end */
2932 };
2933
2934 static const struct hda_verb cxt5066_init_verbs_thinkpad[] = {
2935         {0x1e, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN}, /* Port F */
2936         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN}, /* Port E */
2937
2938         /* Port G: internal speakers  */
2939         {0x1f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2940         {0x1f, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2941
2942         /* Port A: HP, Amp  */
2943         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2944         {0x19, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2945
2946         /* Port B: Mic Dock */
2947         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2948
2949         /* Port C: Mic */
2950         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2951
2952         /* Port D: HP Dock, Amp */
2953         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2954         {0x1c, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2955
2956         /* DAC1 */
2957         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2958
2959         /* Node 14 connections: 0x17 0x18 0x23 0x24 0x27 */
2960         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x50},
2961         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2962         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2) | 0x50},
2963         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2964         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
2965         {0x14, AC_VERB_SET_CONNECT_SEL, 2},     /* default to internal mic */
2966
2967         /* Audio input selector */
2968         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x2},
2969         {0x17, AC_VERB_SET_CONNECT_SEL, 1},     /* route ext mic */
2970
2971         /* SPDIF route: PCM */
2972         {0x20, AC_VERB_SET_CONNECT_SEL, 0x0},
2973         {0x22, AC_VERB_SET_CONNECT_SEL, 0x0},
2974
2975         {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2976         {0x22, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2977
2978         /* internal microphone */
2979         {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN}, /* enable internal mic */
2980
2981         /* EAPD */
2982         {0x1d, AC_VERB_SET_EAPD_BTLENABLE, 0x2}, /* default on */
2983
2984         /* enable unsolicited events for Port A, B, C and D */
2985         {0x19, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_HP_EVENT},
2986         {0x1c, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_HP_EVENT},
2987         {0x1a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_MIC_EVENT},
2988         {0x1b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_MIC_EVENT},
2989         { } /* end */
2990 };
2991
2992 static const struct hda_verb cxt5066_init_verbs_portd_lo[] = {
2993         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2994         { } /* end */
2995 };
2996
2997
2998 static const struct hda_verb cxt5066_init_verbs_hp_laptop[] = {
2999         {0x14, AC_VERB_SET_CONNECT_SEL, 0x0},
3000         {0x19, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_HP_EVENT},
3001         {0x1b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_MIC_EVENT},
3002         { } /* end */
3003 };
3004
3005 /* initialize jack-sensing, too */
3006 static int cxt5066_init(struct hda_codec *codec)
3007 {
3008         snd_printdd("CXT5066: init\n");
3009         conexant_init(codec);
3010         if (codec->patch_ops.unsol_event) {
3011                 cxt5066_hp_automute(codec);
3012                 cxt5066_automic(codec);
3013         }
3014         cxt5066_set_mic_boost(codec);
3015         return 0;
3016 }
3017
3018 static int cxt5066_olpc_init(struct hda_codec *codec)
3019 {
3020         struct conexant_spec *spec = codec->spec;
3021         snd_printdd("CXT5066: init\n");
3022         conexant_init(codec);
3023         cxt5066_hp_automute(codec);
3024         if (!spec->dc_enable) {
3025                 cxt5066_set_mic_boost(codec);
3026                 cxt5066_olpc_automic(codec);
3027         } else {
3028                 cxt5066_enable_dc(codec);
3029         }
3030         return 0;
3031 }
3032
3033 enum {
3034         CXT5066_LAPTOP,         /* Laptops w/ EAPD support */
3035         CXT5066_DELL_LAPTOP,    /* Dell Laptop */
3036         CXT5066_OLPC_XO_1_5,    /* OLPC XO 1.5 */
3037         CXT5066_DELL_VOSTRO,    /* Dell Vostro 1015i */
3038         CXT5066_IDEAPAD,        /* Lenovo IdeaPad U150 */
3039         CXT5066_THINKPAD,       /* Lenovo ThinkPad T410s, others? */
3040         CXT5066_ASUS,           /* Asus K52JU, Lenovo G560 - Int mic at 0x1a and Ext mic at 0x1b */
3041         CXT5066_HP_LAPTOP,      /* HP Laptop */
3042         CXT5066_MODELS
3043 };
3044
3045 static const char * const cxt5066_models[CXT5066_MODELS] = {
3046         [CXT5066_LAPTOP]        = "laptop",
3047         [CXT5066_DELL_LAPTOP]   = "dell-laptop",
3048         [CXT5066_OLPC_XO_1_5]   = "olpc-xo-1_5",
3049         [CXT5066_DELL_VOSTRO]   = "dell-vostro",
3050         [CXT5066_IDEAPAD]       = "ideapad",
3051         [CXT5066_THINKPAD]      = "thinkpad",
3052         [CXT5066_ASUS]          = "asus",
3053         [CXT5066_HP_LAPTOP]     = "hp-laptop",
3054 };
3055
3056 static const struct snd_pci_quirk cxt5066_cfg_tbl[] = {
3057         SND_PCI_QUIRK_MASK(0x1025, 0xff00, 0x0400, "Acer", CXT5066_IDEAPAD),
3058         SND_PCI_QUIRK(0x1028, 0x02d8, "Dell Vostro", CXT5066_DELL_VOSTRO),
3059         SND_PCI_QUIRK(0x1028, 0x02f5, "Dell Vostro 320", CXT5066_IDEAPAD),
3060         SND_PCI_QUIRK(0x1028, 0x0401, "Dell Vostro 1014", CXT5066_DELL_VOSTRO),
3061         SND_PCI_QUIRK(0x1028, 0x0402, "Dell Vostro", CXT5066_DELL_VOSTRO),
3062         SND_PCI_QUIRK(0x1028, 0x0408, "Dell Inspiron One 19T", CXT5066_IDEAPAD),
3063         SND_PCI_QUIRK(0x1028, 0x050f, "Dell Inspiron", CXT5066_IDEAPAD),
3064         SND_PCI_QUIRK(0x1028, 0x0510, "Dell Vostro", CXT5066_IDEAPAD),
3065         SND_PCI_QUIRK(0x103c, 0x360b, "HP G60", CXT5066_HP_LAPTOP),
3066         SND_PCI_QUIRK(0x1043, 0x13f3, "Asus A52J", CXT5066_ASUS),
3067         SND_PCI_QUIRK(0x1043, 0x1643, "Asus K52JU", CXT5066_ASUS),
3068         SND_PCI_QUIRK(0x1043, 0x1993, "Asus U50F", CXT5066_ASUS),
3069         SND_PCI_QUIRK(0x1179, 0xff1e, "Toshiba Satellite C650D", CXT5066_IDEAPAD),
3070         SND_PCI_QUIRK(0x1179, 0xff50, "Toshiba Satellite P500-PSPGSC-01800T", CXT5066_OLPC_XO_1_5),
3071         SND_PCI_QUIRK(0x1179, 0xffe0, "Toshiba Satellite Pro T130-15F", CXT5066_OLPC_XO_1_5),
3072         SND_PCI_QUIRK(0x14f1, 0x0101, "Conexant Reference board",
3073                       CXT5066_LAPTOP),
3074         SND_PCI_QUIRK(0x152d, 0x0833, "OLPC XO-1.5", CXT5066_OLPC_XO_1_5),
3075         SND_PCI_QUIRK(0x17aa, 0x20f2, "Lenovo T400s", CXT5066_THINKPAD),
3076         SND_PCI_QUIRK(0x17aa, 0x21c5, "Thinkpad Edge 13", CXT5066_THINKPAD),
3077         SND_PCI_QUIRK(0x17aa, 0x21c6, "Thinkpad Edge 13", CXT5066_ASUS),
3078         SND_PCI_QUIRK(0x17aa, 0x215e, "Lenovo Thinkpad", CXT5066_THINKPAD),
3079         SND_PCI_QUIRK(0x17aa, 0x21da, "Lenovo X220", CXT5066_THINKPAD),
3080         SND_PCI_QUIRK(0x17aa, 0x21db, "Lenovo X220-tablet", CXT5066_THINKPAD),
3081         SND_PCI_QUIRK(0x17aa, 0x38af, "Lenovo G560", CXT5066_ASUS),
3082         SND_PCI_QUIRK_VENDOR(0x17aa, "Lenovo", CXT5066_IDEAPAD), /* Fallback for Lenovos without dock mic */
3083         {}
3084 };
3085
3086 static int patch_cxt5066(struct hda_codec *codec)
3087 {
3088         struct conexant_spec *spec;
3089         int board_config;
3090
3091         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
3092         if (!spec)
3093                 return -ENOMEM;
3094         codec->spec = spec;
3095
3096         codec->patch_ops = conexant_patch_ops;
3097         codec->patch_ops.init = conexant_init;
3098
3099         spec->dell_automute = 0;
3100         spec->multiout.max_channels = 2;
3101         spec->multiout.num_dacs = ARRAY_SIZE(cxt5066_dac_nids);
3102         spec->multiout.dac_nids = cxt5066_dac_nids;
3103         conexant_check_dig_outs(codec, cxt5066_digout_pin_nids,
3104             ARRAY_SIZE(cxt5066_digout_pin_nids));
3105         spec->num_adc_nids = 1;
3106         spec->adc_nids = cxt5066_adc_nids;
3107         spec->capsrc_nids = cxt5066_capsrc_nids;
3108         spec->input_mux = &cxt5066_capture_source;
3109
3110         spec->port_d_mode = PIN_HP;
3111
3112         spec->num_init_verbs = 1;
3113         spec->init_verbs[0] = cxt5066_init_verbs;
3114         spec->num_channel_mode = ARRAY_SIZE(cxt5066_modes);
3115         spec->channel_mode = cxt5066_modes;
3116         spec->cur_adc = 0;
3117         spec->cur_adc_idx = 0;
3118
3119         set_beep_amp(spec, 0x13, 0, HDA_OUTPUT);
3120
3121         board_config = snd_hda_check_board_config(codec, CXT5066_MODELS,
3122                                                   cxt5066_models, cxt5066_cfg_tbl);
3123         switch (board_config) {
3124         default:
3125         case CXT5066_LAPTOP:
3126                 spec->mixers[spec->num_mixers++] = cxt5066_mixer_master;
3127                 spec->mixers[spec->num_mixers++] = cxt5066_mixers;
3128                 break;
3129         case CXT5066_DELL_LAPTOP:
3130                 spec->mixers[spec->num_mixers++] = cxt5066_mixer_master;
3131                 spec->mixers[spec->num_mixers++] = cxt5066_mixers;
3132
3133                 spec->port_d_mode = PIN_OUT;
3134                 spec->init_verbs[spec->num_init_verbs] = cxt5066_init_verbs_portd_lo;
3135                 spec->num_init_verbs++;
3136                 spec->dell_automute = 1;
3137                 break;
3138         case CXT5066_ASUS:
3139         case CXT5066_HP_LAPTOP:
3140                 codec->patch_ops.init = cxt5066_init;
3141                 codec->patch_ops.unsol_event = cxt5066_unsol_event;
3142                 spec->init_verbs[spec->num_init_verbs] =
3143                         cxt5066_init_verbs_hp_laptop;
3144                 spec->num_init_verbs++;
3145                 spec->hp_laptop = board_config == CXT5066_HP_LAPTOP;
3146                 spec->asus = board_config == CXT5066_ASUS;
3147                 spec->mixers[spec->num_mixers++] = cxt5066_mixer_master;
3148                 spec->mixers[spec->num_mixers++] = cxt5066_mixers;
3149                 /* no S/PDIF out */
3150                 if (board_config == CXT5066_HP_LAPTOP)
3151                         spec->multiout.dig_out_nid = 0;
3152                 /* input source automatically selected */
3153                 spec->input_mux = NULL;
3154                 spec->port_d_mode = 0;
3155                 spec->mic_boost = 3; /* default 30dB gain */
3156                 break;
3157
3158         case CXT5066_OLPC_XO_1_5:
3159                 codec->patch_ops.init = cxt5066_olpc_init;
3160                 codec->patch_ops.unsol_event = cxt5066_olpc_unsol_event;
3161                 spec->init_verbs[0] = cxt5066_init_verbs_olpc;
3162                 spec->mixers[spec->num_mixers++] = cxt5066_mixer_master_olpc;
3163                 spec->mixers[spec->num_mixers++] = cxt5066_mixer_olpc_dc;
3164                 spec->mixers[spec->num_mixers++] = cxt5066_mixers;
3165                 spec->port_d_mode = 0;
3166                 spec->mic_boost = 3; /* default 30dB gain */
3167
3168                 /* no S/PDIF out */
3169                 spec->multiout.dig_out_nid = 0;
3170
3171                 /* input source automatically selected */
3172                 spec->input_mux = NULL;
3173
3174                 /* our capture hooks which allow us to turn on the microphone LED
3175                  * at the right time */
3176                 spec->capture_prepare = cxt5066_olpc_capture_prepare;
3177                 spec->capture_cleanup = cxt5066_olpc_capture_cleanup;
3178                 break;
3179         case CXT5066_DELL_VOSTRO:
3180                 codec->patch_ops.init = cxt5066_init;
3181                 codec->patch_ops.unsol_event = cxt5066_unsol_event;
3182                 spec->init_verbs[0] = cxt5066_init_verbs_vostro;
3183                 spec->mixers[spec->num_mixers++] = cxt5066_mixer_master_olpc;
3184                 spec->mixers[spec->num_mixers++] = cxt5066_mixers;
3185                 spec->mixers[spec->num_mixers++] = cxt5066_vostro_mixers;
3186                 spec->port_d_mode = 0;
3187                 spec->dell_vostro = 1;
3188                 spec->mic_boost = 3; /* default 30dB gain */
3189
3190                 /* no S/PDIF out */
3191                 spec->multiout.dig_out_nid = 0;
3192
3193                 /* input source automatically selected */
3194                 spec->input_mux = NULL;
3195                 break;
3196         case CXT5066_IDEAPAD:
3197                 codec->patch_ops.init = cxt5066_init;
3198                 codec->patch_ops.unsol_event = cxt5066_unsol_event;
3199                 spec->mixers[spec->num_mixers++] = cxt5066_mixer_master;
3200                 spec->mixers[spec->num_mixers++] = cxt5066_mixers;
3201                 spec->init_verbs[0] = cxt5066_init_verbs_ideapad;
3202                 spec->port_d_mode = 0;
3203                 spec->ideapad = 1;
3204                 spec->mic_boost = 2;    /* default 20dB gain */
3205
3206                 /* no S/PDIF out */
3207                 spec->multiout.dig_out_nid = 0;
3208
3209                 /* input source automatically selected */
3210                 spec->input_mux = NULL;
3211                 break;
3212         case CXT5066_THINKPAD:
3213                 codec->patch_ops.init = cxt5066_init;
3214                 codec->patch_ops.unsol_event = cxt5066_unsol_event;
3215                 spec->mixers[spec->num_mixers++] = cxt5066_mixer_master;
3216                 spec->mixers[spec->num_mixers++] = cxt5066_mixers;
3217                 spec->init_verbs[0] = cxt5066_init_verbs_thinkpad;
3218                 spec->thinkpad = 1;
3219                 spec->port_d_mode = PIN_OUT;
3220                 spec->mic_boost = 2;    /* default 20dB gain */
3221
3222                 /* no S/PDIF out */
3223                 spec->multiout.dig_out_nid = 0;
3224
3225                 /* input source automatically selected */
3226                 spec->input_mux = NULL;
3227                 break;
3228         }
3229
3230         if (spec->beep_amp)
3231                 snd_hda_attach_beep_device(codec, spec->beep_amp);
3232
3233         return 0;
3234 }
3235
3236 /*
3237  * Automatic parser for CX20641 & co
3238  */
3239
3240 static int cx_auto_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3241                                        struct hda_codec *codec,
3242                                        unsigned int stream_tag,
3243                                        unsigned int format,
3244                                        struct snd_pcm_substream *substream)
3245 {
3246         struct conexant_spec *spec = codec->spec;
3247         hda_nid_t adc = spec->imux_adcs[spec->cur_mux[0]];
3248         if (spec->adc_switching) {
3249                 spec->cur_adc = adc;
3250                 spec->cur_adc_stream_tag = stream_tag;
3251                 spec->cur_adc_format = format;
3252         }
3253         snd_hda_codec_setup_stream(codec, adc, stream_tag, 0, format);
3254         return 0;
3255 }
3256
3257 static int cx_auto_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3258                                        struct hda_codec *codec,
3259                                        struct snd_pcm_substream *substream)
3260 {
3261         struct conexant_spec *spec = codec->spec;
3262         snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
3263         spec->cur_adc = 0;
3264         return 0;
3265 }
3266
3267 static const struct hda_pcm_stream cx_auto_pcm_analog_capture = {
3268         .substreams = 1,
3269         .channels_min = 2,
3270         .channels_max = 2,
3271         .nid = 0, /* fill later */
3272         .ops = {
3273                 .prepare = cx_auto_capture_pcm_prepare,
3274                 .cleanup = cx_auto_capture_pcm_cleanup
3275         },
3276 };
3277
3278 static const hda_nid_t cx_auto_adc_nids[] = { 0x14 };
3279
3280 /* get the connection index of @nid in the widget @mux */
3281 static int get_connection_index(struct hda_codec *codec, hda_nid_t mux,
3282                                 hda_nid_t nid)
3283 {
3284         hda_nid_t conn[HDA_MAX_NUM_INPUTS];
3285         int i, nums;
3286
3287         nums = snd_hda_get_connections(codec, mux, conn, ARRAY_SIZE(conn));
3288         for (i = 0; i < nums; i++)
3289                 if (conn[i] == nid)
3290                         return i;
3291         return -1;
3292 }
3293
3294 /* get an unassigned DAC from the given list.
3295  * Return the nid if found and reduce the DAC list, or return zero if
3296  * not found
3297  */
3298 static hda_nid_t get_unassigned_dac(struct hda_codec *codec, hda_nid_t pin,
3299                                     hda_nid_t *dacs, int *num_dacs)
3300 {
3301         int i, nums = *num_dacs;
3302         hda_nid_t ret = 0;
3303
3304         for (i = 0; i < nums; i++) {
3305                 if (get_connection_index(codec, pin, dacs[i]) >= 0) {
3306                         ret = dacs[i];
3307                         break;
3308                 }
3309         }
3310         if (!ret)
3311                 return 0;
3312         if (--nums > 0)
3313                 memmove(dacs, dacs + 1, nums * sizeof(hda_nid_t));
3314         *num_dacs = nums;
3315         return ret;
3316 }
3317
3318 #define MAX_AUTO_DACS   5
3319
3320 /* fill analog DAC list from the widget tree */
3321 static int fill_cx_auto_dacs(struct hda_codec *codec, hda_nid_t *dacs)
3322 {
3323         hda_nid_t nid, end_nid;
3324         int nums = 0;
3325
3326         end_nid = codec->start_nid + codec->num_nodes;
3327         for (nid = codec->start_nid; nid < end_nid; nid++) {
3328                 unsigned int wcaps = get_wcaps(codec, nid);
3329                 unsigned int type = get_wcaps_type(wcaps);
3330                 if (type == AC_WID_AUD_OUT && !(wcaps & AC_WCAP_DIGITAL)) {
3331                         dacs[nums++] = nid;
3332                         if (nums >= MAX_AUTO_DACS)
3333                                 break;
3334                 }
3335         }
3336         return nums;
3337 }
3338
3339 /* fill pin_dac_pair list from the pin and dac list */
3340 static int fill_dacs_for_pins(struct hda_codec *codec, hda_nid_t *pins,
3341                               int num_pins, hda_nid_t *dacs, int *rest,
3342                               struct pin_dac_pair *filled, int type)
3343 {
3344         int i, nums;
3345
3346         nums = 0;
3347         for (i = 0; i < num_pins; i++) {
3348                 filled[nums].pin = pins[i];
3349                 filled[nums].type = type;
3350                 filled[nums].dac = get_unassigned_dac(codec, pins[i], dacs, rest);
3351                 nums++;
3352         }
3353         return nums;
3354 }
3355
3356 /* parse analog output paths */
3357 static void cx_auto_parse_output(struct hda_codec *codec)
3358 {
3359         struct conexant_spec *spec = codec->spec;
3360         struct auto_pin_cfg *cfg = &spec->autocfg;
3361         hda_nid_t dacs[MAX_AUTO_DACS];
3362         int i, j, nums, rest;
3363
3364         rest = fill_cx_auto_dacs(codec, dacs);
3365         /* parse all analog output pins */
3366         nums = fill_dacs_for_pins(codec, cfg->line_out_pins, cfg->line_outs,
3367                                   dacs, &rest, spec->dac_info,
3368                                   AUTO_PIN_LINE_OUT);
3369         nums += fill_dacs_for_pins(codec, cfg->hp_pins, cfg->hp_outs,
3370                                   dacs, &rest, spec->dac_info + nums,
3371                                   AUTO_PIN_HP_OUT);
3372         nums += fill_dacs_for_pins(codec, cfg->speaker_pins, cfg->speaker_outs,
3373                                   dacs, &rest, spec->dac_info + nums,
3374                                   AUTO_PIN_SPEAKER_OUT);
3375         spec->dac_info_filled = nums;
3376         /* fill multiout struct */
3377         for (i = 0; i < nums; i++) {
3378                 hda_nid_t dac = spec->dac_info[i].dac;
3379                 if (!dac)
3380                         continue;
3381                 switch (spec->dac_info[i].type) {
3382                 case AUTO_PIN_LINE_OUT:
3383                         spec->private_dac_nids[spec->multiout.num_dacs] = dac;
3384                         spec->multiout.num_dacs++;
3385                         break;
3386                 case AUTO_PIN_HP_OUT:
3387                 case AUTO_PIN_SPEAKER_OUT:
3388                         if (!spec->multiout.hp_nid) {
3389                                 spec->multiout.hp_nid = dac;
3390                                 break;
3391                         }
3392                         for (j = 0; j < ARRAY_SIZE(spec->multiout.extra_out_nid); j++)
3393                                 if (!spec->multiout.extra_out_nid[j]) {
3394                                         spec->multiout.extra_out_nid[j] = dac;
3395                                         break;
3396                                 }
3397                         break;
3398                 }
3399         }
3400         spec->multiout.dac_nids = spec->private_dac_nids;
3401         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
3402
3403         if (cfg->hp_outs > 0)
3404                 spec->auto_mute = 1;
3405         spec->vmaster_nid = spec->private_dac_nids[0];
3406 }
3407
3408 static void cx_auto_turn_eapd(struct hda_codec *codec, int num_pins,
3409                               hda_nid_t *pins, bool on);
3410
3411 /* auto-mute/unmute speaker and line outs according to headphone jack */
3412 static void cx_auto_hp_automute(struct hda_codec *codec)
3413 {
3414         struct conexant_spec *spec = codec->spec;
3415         struct auto_pin_cfg *cfg = &spec->autocfg;
3416         int i, present;
3417
3418         if (!spec->auto_mute)
3419                 return;
3420         present = 0;
3421         for (i = 0; i < cfg->hp_outs; i++) {
3422                 if (snd_hda_jack_detect(codec, cfg->hp_pins[i])) {
3423                         present = 1;
3424                         break;
3425                 }
3426         }
3427         cx_auto_turn_eapd(codec, cfg->hp_outs, cfg->hp_pins, present);
3428         for (i = 0; i < cfg->line_outs; i++) {
3429                 snd_hda_codec_write(codec, cfg->line_out_pins[i], 0,
3430                                     AC_VERB_SET_PIN_WIDGET_CONTROL,
3431                                     present ? 0 : PIN_OUT);
3432         }
3433         cx_auto_turn_eapd(codec, cfg->line_outs, cfg->line_out_pins, !present);
3434         for (i = 0; !present && i < cfg->line_outs; i++)
3435                 if (snd_hda_jack_detect(codec, cfg->line_out_pins[i]))
3436                         present = 1;
3437         for (i = 0; i < cfg->speaker_outs; i++) {
3438                 snd_hda_codec_write(codec, cfg->speaker_pins[i], 0,
3439                                     AC_VERB_SET_PIN_WIDGET_CONTROL,
3440                                     present ? 0 : PIN_OUT);
3441         }
3442         cx_auto_turn_eapd(codec, cfg->speaker_outs, cfg->speaker_pins, !present);
3443 }
3444
3445 static int cx_auto_mux_enum_info(struct snd_kcontrol *kcontrol,
3446                                  struct snd_ctl_elem_info *uinfo)
3447 {
3448         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3449         struct conexant_spec *spec = codec->spec;
3450
3451         return snd_hda_input_mux_info(&spec->private_imux, uinfo);
3452 }
3453
3454 static int cx_auto_mux_enum_get(struct snd_kcontrol *kcontrol,
3455                                 struct snd_ctl_elem_value *ucontrol)
3456 {
3457         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3458         struct conexant_spec *spec = codec->spec;
3459
3460         ucontrol->value.enumerated.item[0] = spec->cur_mux[0];
3461         return 0;
3462 }
3463
3464 /* look for the route the given pin from mux and return the index;
3465  * if do_select is set, actually select the route.
3466  */
3467 static int __select_input_connection(struct hda_codec *codec, hda_nid_t mux,
3468                                      hda_nid_t pin, hda_nid_t *srcp,
3469                                      bool do_select, int depth)
3470 {
3471         hda_nid_t conn[HDA_MAX_NUM_INPUTS];
3472         int i, nums;
3473
3474         switch (get_wcaps_type(get_wcaps(codec, mux))) {
3475         case AC_WID_AUD_IN:
3476         case AC_WID_AUD_SEL:
3477         case AC_WID_AUD_MIX:
3478                 break;
3479         default:
3480                 return -1;
3481         }
3482
3483         nums = snd_hda_get_connections(codec, mux, conn, ARRAY_SIZE(conn));
3484         for (i = 0; i < nums; i++)
3485                 if (conn[i] == pin) {
3486                         if (do_select)
3487                                 snd_hda_codec_write(codec, mux, 0,
3488                                                     AC_VERB_SET_CONNECT_SEL, i);
3489                         if (srcp)
3490                                 *srcp = mux;
3491                         return i;
3492                 }
3493         depth++;
3494         if (depth == 2)
3495                 return -1;
3496         for (i = 0; i < nums; i++) {
3497                 int ret  = __select_input_connection(codec, conn[i], pin, srcp,
3498                                                      do_select, depth);
3499                 if (ret >= 0) {
3500                         if (do_select)
3501                                 snd_hda_codec_write(codec, mux, 0,
3502                                                     AC_VERB_SET_CONNECT_SEL, i);
3503                         return i;
3504                 }
3505         }
3506         return -1;
3507 }
3508
3509 static void select_input_connection(struct hda_codec *codec, hda_nid_t mux,
3510                                    hda_nid_t pin)
3511 {
3512         __select_input_connection(codec, mux, pin, NULL, true, 0);
3513 }
3514
3515 static int get_input_connection(struct hda_codec *codec, hda_nid_t mux,
3516                                 hda_nid_t pin)
3517 {
3518         return __select_input_connection(codec, mux, pin, NULL, false, 0);
3519 }
3520
3521 static int cx_auto_mux_enum_update(struct hda_codec *codec,
3522                                    const struct hda_input_mux *imux,
3523                                    unsigned int idx)
3524 {
3525         struct conexant_spec *spec = codec->spec;
3526         hda_nid_t adc;
3527
3528         if (!imux->num_items)
3529                 return 0;
3530         if (idx >= imux->num_items)
3531                 idx = imux->num_items - 1;
3532         if (spec->cur_mux[0] == idx)
3533                 return 0;
3534         adc = spec->imux_adcs[idx];
3535         select_input_connection(codec, spec->imux_adcs[idx],
3536                                 spec->imux_pins[idx]);
3537         if (spec->cur_adc && spec->cur_adc != adc) {
3538                 /* stream is running, let's swap the current ADC */
3539                 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
3540                 spec->cur_adc = adc;
3541                 snd_hda_codec_setup_stream(codec, adc,
3542                                            spec->cur_adc_stream_tag, 0,
3543                                            spec->cur_adc_format);
3544         }
3545         spec->cur_mux[0] = idx;
3546         return 1;
3547 }
3548
3549 static int cx_auto_mux_enum_put(struct snd_kcontrol *kcontrol,
3550                                 struct snd_ctl_elem_value *ucontrol)
3551 {
3552         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3553         struct conexant_spec *spec = codec->spec;
3554
3555         return cx_auto_mux_enum_update(codec, &spec->private_imux,
3556                                        ucontrol->value.enumerated.item[0]);
3557 }
3558
3559 static const struct snd_kcontrol_new cx_auto_capture_mixers[] = {
3560         {
3561                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3562                 .name = "Capture Source",
3563                 .info = cx_auto_mux_enum_info,
3564                 .get = cx_auto_mux_enum_get,
3565                 .put = cx_auto_mux_enum_put
3566         },
3567         {}
3568 };
3569
3570 /* automatic switch internal and external mic */
3571 static void cx_auto_automic(struct hda_codec *codec)
3572 {
3573         struct conexant_spec *spec = codec->spec;
3574         int ext_idx = spec->auto_mic_ext;
3575
3576         if (!spec->auto_mic)
3577                 return;
3578         if (snd_hda_jack_detect(codec, spec->imux_pins[ext_idx]))
3579                 cx_auto_mux_enum_update(codec, &spec->private_imux, ext_idx);
3580         else
3581                 cx_auto_mux_enum_update(codec, &spec->private_imux, !ext_idx);
3582 }
3583
3584 static void cx_auto_unsol_event(struct hda_codec *codec, unsigned int res)
3585 {
3586         int nid = (res & AC_UNSOL_RES_SUBTAG) >> 20;
3587         switch (res >> 26) {
3588         case CONEXANT_HP_EVENT:
3589                 cx_auto_hp_automute(codec);
3590                 snd_hda_input_jack_report(codec, nid);
3591                 break;
3592         case CONEXANT_MIC_EVENT:
3593                 cx_auto_automic(codec);
3594                 snd_hda_input_jack_report(codec, nid);
3595                 break;
3596         }
3597 }
3598
3599 /* return true if it's an internal-mic pin */
3600 static int is_int_mic(struct hda_codec *codec, hda_nid_t pin)
3601 {
3602         unsigned int def_conf = snd_hda_codec_get_pincfg(codec, pin);
3603         return get_defcfg_device(def_conf) == AC_JACK_MIC_IN &&
3604                 snd_hda_get_input_pin_attr(def_conf) == INPUT_PIN_ATTR_INT;
3605 }
3606
3607 /* return true if it's an external-mic pin */
3608 static int is_ext_mic(struct hda_codec *codec, hda_nid_t pin)
3609 {
3610         unsigned int def_conf = snd_hda_codec_get_pincfg(codec, pin);
3611         return get_defcfg_device(def_conf) == AC_JACK_MIC_IN &&
3612                 snd_hda_get_input_pin_attr(def_conf) >= INPUT_PIN_ATTR_NORMAL &&
3613                 (snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_PRES_DETECT);
3614 }
3615
3616 /* check whether the pin config is suitable for auto-mic switching;
3617  * auto-mic is enabled only when one int-mic and one-ext mic exist
3618  */
3619 static void cx_auto_check_auto_mic(struct hda_codec *codec)
3620 {
3621         struct conexant_spec *spec = codec->spec;
3622
3623         if (is_ext_mic(codec, spec->imux_pins[0]) &&
3624             is_int_mic(codec, spec->imux_pins[1])) {
3625                 spec->auto_mic = 1;
3626                 spec->auto_mic_ext = 0;
3627                 return;
3628         }
3629         if (is_int_mic(codec, spec->imux_pins[0]) &&
3630             is_ext_mic(codec, spec->imux_pins[1])) {
3631                 spec->auto_mic = 1;
3632                 spec->auto_mic_ext = 1;
3633                 return;
3634         }
3635 }
3636
3637 static void cx_auto_parse_input(struct hda_codec *codec)
3638 {
3639         struct conexant_spec *spec = codec->spec;
3640         struct auto_pin_cfg *cfg = &spec->autocfg;
3641         struct hda_input_mux *imux;
3642         int i, j;
3643
3644         imux = &spec->private_imux;
3645         for (i = 0; i < cfg->num_inputs; i++) {
3646                 for (j = 0; j < spec->num_adc_nids; j++) {
3647                         hda_nid_t adc = spec->adc_nids[j];
3648                         int idx = get_input_connection(codec, adc,
3649                                                        cfg->inputs[i].pin);
3650                         if (idx >= 0) {
3651                                 const char *label;
3652                                 label = hda_get_autocfg_input_label(codec, cfg, i);
3653                                 spec->imux_cfg_idx[imux->num_items] = i;
3654                                 spec->imux_adcs[imux->num_items] = adc;
3655                                 spec->imux_pins[imux->num_items] =
3656                                         cfg->inputs[i].pin;
3657                                 snd_hda_add_imux_item(imux, label, idx, NULL);
3658                                 break;
3659                         }
3660                 }
3661         }
3662         if (imux->num_items == 2 && cfg->num_inputs == 2)
3663                 cx_auto_check_auto_mic(codec);
3664         if (imux->num_items > 1 && !spec->auto_mic) {
3665                 for (i = 1; i < imux->num_items; i++) {
3666                         if (spec->imux_adcs[i] != spec->imux_adcs[0]) {
3667                                 spec->adc_switching = 1;
3668                                 break;
3669                         }
3670                 }
3671         }
3672 }
3673
3674 /* get digital-input audio widget corresponding to the given pin */
3675 static hda_nid_t cx_auto_get_dig_in(struct hda_codec *codec, hda_nid_t pin)
3676 {
3677         hda_nid_t nid, end_nid;
3678
3679         end_nid = codec->start_nid + codec->num_nodes;
3680         for (nid = codec->start_nid; nid < end_nid; nid++) {
3681                 unsigned int wcaps = get_wcaps(codec, nid);
3682                 unsigned int type = get_wcaps_type(wcaps);
3683                 if (type == AC_WID_AUD_IN && (wcaps & AC_WCAP_DIGITAL)) {
3684                         if (get_connection_index(codec, nid, pin) >= 0)
3685                                 return nid;
3686                 }
3687         }
3688         return 0;
3689 }
3690
3691 static void cx_auto_parse_digital(struct hda_codec *codec)
3692 {
3693         struct conexant_spec *spec = codec->spec;
3694         struct auto_pin_cfg *cfg = &spec->autocfg;
3695         hda_nid_t nid;
3696
3697         if (cfg->dig_outs &&
3698             snd_hda_get_connections(codec, cfg->dig_out_pins[0], &nid, 1) == 1)
3699                 spec->multiout.dig_out_nid = nid;
3700         if (cfg->dig_in_pin)
3701                 spec->dig_in_nid = cx_auto_get_dig_in(codec, cfg->dig_in_pin);
3702 }
3703
3704 #ifdef CONFIG_SND_HDA_INPUT_BEEP
3705 static void cx_auto_parse_beep(struct hda_codec *codec)
3706 {
3707         struct conexant_spec *spec = codec->spec;
3708         hda_nid_t nid, end_nid;
3709
3710         end_nid = codec->start_nid + codec->num_nodes;
3711         for (nid = codec->start_nid; nid < end_nid; nid++)
3712                 if (get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_BEEP) {
3713                         set_beep_amp(spec, nid, 0, HDA_OUTPUT);
3714                         break;
3715                 }
3716 }
3717 #else
3718 #define cx_auto_parse_beep(codec)
3719 #endif
3720
3721 static int cx_auto_parse_auto_config(struct hda_codec *codec)
3722 {
3723         struct conexant_spec *spec = codec->spec;
3724         int err;
3725
3726         err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
3727         if (err < 0)
3728                 return err;
3729
3730         cx_auto_parse_output(codec);
3731         cx_auto_parse_input(codec);
3732         cx_auto_parse_digital(codec);
3733         cx_auto_parse_beep(codec);
3734         return 0;
3735 }
3736
3737 static void cx_auto_turn_eapd(struct hda_codec *codec, int num_pins,
3738                               hda_nid_t *pins, bool on)
3739 {
3740         int i;
3741         for (i = 0; i < num_pins; i++) {
3742                 if (snd_hda_query_pin_caps(codec, pins[i]) & AC_PINCAP_EAPD)
3743                         snd_hda_codec_write(codec, pins[i], 0,
3744                                             AC_VERB_SET_EAPD_BTLENABLE,
3745                                             on ? 0x02 : 0);
3746         }
3747 }
3748
3749 static void select_connection(struct hda_codec *codec, hda_nid_t pin,
3750                               hda_nid_t src)
3751 {
3752         int idx = get_connection_index(codec, pin, src);
3753         if (idx >= 0)
3754                 snd_hda_codec_write(codec, pin, 0,
3755                                     AC_VERB_SET_CONNECT_SEL, idx);
3756 }
3757
3758 static void mute_outputs(struct hda_codec *codec, int num_nids,
3759                          const hda_nid_t *nids)
3760 {
3761         int i, val;
3762
3763         for (i = 0; i < num_nids; i++) {
3764                 hda_nid_t nid = nids[i];
3765                 if (!(get_wcaps(codec, nid) & AC_WCAP_OUT_AMP))
3766                         continue;
3767                 if (query_amp_caps(codec, nid, HDA_OUTPUT) & AC_AMPCAP_MUTE)
3768                         val = AMP_OUT_MUTE;
3769                 else
3770                         val = AMP_OUT_ZERO;
3771                 snd_hda_codec_write(codec, nid, 0,
3772                                     AC_VERB_SET_AMP_GAIN_MUTE, val);
3773         }
3774 }
3775
3776 static void cx_auto_init_output(struct hda_codec *codec)
3777 {
3778         struct conexant_spec *spec = codec->spec;
3779         struct auto_pin_cfg *cfg = &spec->autocfg;
3780         hda_nid_t nid;
3781         int i;
3782
3783         mute_outputs(codec, spec->multiout.num_dacs, spec->multiout.dac_nids);
3784         for (i = 0; i < cfg->hp_outs; i++)
3785                 snd_hda_codec_write(codec, cfg->hp_pins[i], 0,
3786                                     AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP);
3787         mute_outputs(codec, cfg->hp_outs, cfg->hp_pins);
3788         mute_outputs(codec, cfg->line_outs, cfg->line_out_pins);
3789         mute_outputs(codec, cfg->speaker_outs, cfg->speaker_pins);
3790         if (spec->auto_mute) {
3791                 for (i = 0; i < cfg->hp_outs; i++) {
3792                         snd_hda_codec_write(codec, cfg->hp_pins[i], 0,
3793                                     AC_VERB_SET_UNSOLICITED_ENABLE,
3794                                     AC_USRSP_EN | CONEXANT_HP_EVENT);
3795                 }
3796                 cx_auto_hp_automute(codec);
3797         } else {
3798                 for (i = 0; i < cfg->line_outs; i++)
3799                         snd_hda_codec_write(codec, cfg->line_out_pins[i], 0,
3800                                     AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
3801                 for (i = 0; i < cfg->speaker_outs; i++)
3802                         snd_hda_codec_write(codec, cfg->speaker_pins[i], 0,
3803                                     AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
3804                 /* turn on EAPD */
3805                 cx_auto_turn_eapd(codec, cfg->line_outs, cfg->line_out_pins,
3806                                   true);
3807                 cx_auto_turn_eapd(codec, cfg->hp_outs, cfg->hp_pins,
3808                                   true);
3809                 cx_auto_turn_eapd(codec, cfg->speaker_outs, cfg->speaker_pins,
3810                                   true);
3811         }
3812
3813         for (i = 0; i < spec->dac_info_filled; i++) {
3814                 nid = spec->dac_info[i].dac;
3815                 if (!nid)
3816                         nid = spec->multiout.dac_nids[0];
3817                 select_connection(codec, spec->dac_info[i].pin, nid);
3818         }
3819 }
3820
3821 static void cx_auto_init_input(struct hda_codec *codec)
3822 {
3823         struct conexant_spec *spec = codec->spec;
3824         struct auto_pin_cfg *cfg = &spec->autocfg;
3825         int i, val;
3826
3827         for (i = 0; i < spec->num_adc_nids; i++) {
3828                 hda_nid_t nid = spec->adc_nids[i];
3829                 if (!(get_wcaps(codec, nid) & AC_WCAP_IN_AMP))
3830                         continue;
3831                 if (query_amp_caps(codec, nid, HDA_INPUT) & AC_AMPCAP_MUTE)
3832                         val = AMP_IN_MUTE(0);
3833                 else
3834                         val = AMP_IN_UNMUTE(0);
3835                 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
3836                                     val);
3837         }
3838
3839         for (i = 0; i < cfg->num_inputs; i++) {
3840                 unsigned int type;
3841                 if (cfg->inputs[i].type == AUTO_PIN_MIC)
3842                         type = PIN_VREF80;
3843                 else
3844                         type = PIN_IN;
3845                 snd_hda_codec_write(codec, cfg->inputs[i].pin, 0,
3846                                     AC_VERB_SET_PIN_WIDGET_CONTROL, type);
3847         }
3848
3849         if (spec->auto_mic) {
3850                 int ext_idx = spec->auto_mic_ext;
3851                 snd_hda_codec_write(codec, cfg->inputs[ext_idx].pin, 0,
3852                                     AC_VERB_SET_UNSOLICITED_ENABLE,
3853                                     AC_USRSP_EN | CONEXANT_MIC_EVENT);
3854                 cx_auto_automic(codec);
3855         } else {
3856                 select_input_connection(codec, spec->imux_adcs[0],
3857                                         spec->imux_pins[0]);
3858         }
3859 }
3860
3861 static void cx_auto_init_digital(struct hda_codec *codec)
3862 {
3863         struct conexant_spec *spec = codec->spec;
3864         struct auto_pin_cfg *cfg = &spec->autocfg;
3865
3866         if (spec->multiout.dig_out_nid)
3867                 snd_hda_codec_write(codec, cfg->dig_out_pins[0], 0,
3868                                     AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
3869         if (spec->dig_in_nid)
3870                 snd_hda_codec_write(codec, cfg->dig_in_pin, 0,
3871                                     AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN);
3872 }
3873
3874 static int cx_auto_init(struct hda_codec *codec)
3875 {
3876         /*snd_hda_sequence_write(codec, cx_auto_init_verbs);*/
3877         cx_auto_init_output(codec);
3878         cx_auto_init_input(codec);
3879         cx_auto_init_digital(codec);
3880         return 0;
3881 }
3882
3883 static int cx_auto_add_volume_idx(struct hda_codec *codec, const char *basename,
3884                               const char *dir, int cidx,
3885                               hda_nid_t nid, int hda_dir, int amp_idx)
3886 {
3887         static char name[32];
3888         static struct snd_kcontrol_new knew[] = {
3889                 HDA_CODEC_VOLUME(name, 0, 0, 0),
3890                 HDA_CODEC_MUTE(name, 0, 0, 0),
3891         };
3892         static const char * const sfx[2] = { "Volume", "Switch" };
3893         int i, err;
3894
3895         for (i = 0; i < 2; i++) {
3896                 struct snd_kcontrol *kctl;
3897                 knew[i].private_value = HDA_COMPOSE_AMP_VAL(nid, 3, amp_idx,
3898                                                             hda_dir);
3899                 knew[i].subdevice = HDA_SUBDEV_AMP_FLAG;
3900                 knew[i].index = cidx;
3901                 snprintf(name, sizeof(name), "%s%s %s", basename, dir, sfx[i]);
3902                 kctl = snd_ctl_new1(&knew[i], codec);
3903                 if (!kctl)
3904                         return -ENOMEM;
3905                 err = snd_hda_ctl_add(codec, nid, kctl);
3906                 if (err < 0)
3907                         return err;
3908                 if (!(query_amp_caps(codec, nid, hda_dir) & AC_AMPCAP_MUTE))
3909                         break;
3910         }
3911         return 0;
3912 }
3913
3914 #define cx_auto_add_volume(codec, str, dir, cidx, nid, hda_dir)         \
3915         cx_auto_add_volume_idx(codec, str, dir, cidx, nid, hda_dir, 0)
3916
3917 #define cx_auto_add_pb_volume(codec, nid, str, idx)                     \
3918         cx_auto_add_volume(codec, str, " Playback", idx, nid, HDA_OUTPUT)
3919
3920 static int try_add_pb_volume(struct hda_codec *codec, hda_nid_t dac,
3921                              hda_nid_t pin, const char *name, int idx)
3922 {
3923         unsigned int caps;
3924         caps = query_amp_caps(codec, dac, HDA_OUTPUT);
3925         if (caps & AC_AMPCAP_NUM_STEPS)
3926                 return cx_auto_add_pb_volume(codec, dac, name, idx);
3927         caps = query_amp_caps(codec, pin, HDA_OUTPUT);
3928         if (caps & AC_AMPCAP_NUM_STEPS)
3929                 return cx_auto_add_pb_volume(codec, pin, name, idx);
3930         return 0;
3931 }
3932
3933 static int cx_auto_build_output_controls(struct hda_codec *codec)
3934 {
3935         struct conexant_spec *spec = codec->spec;
3936         int i, err;
3937         int num_line = 0, num_hp = 0, num_spk = 0;
3938         static const char * const texts[3] = { "Front", "Surround", "CLFE" };
3939
3940         if (spec->dac_info_filled == 1)
3941                 return try_add_pb_volume(codec, spec->dac_info[0].dac,
3942                                          spec->dac_info[0].pin,
3943                                          "Master", 0);
3944
3945         for (i = 0; i < spec->dac_info_filled; i++) {
3946                 const char *label;
3947                 int idx, type;
3948                 if (!spec->dac_info[i].dac)
3949                         continue;
3950                 type = spec->dac_info[i].type;
3951                 if (type == AUTO_PIN_LINE_OUT)
3952                         type = spec->autocfg.line_out_type;
3953                 switch (type) {
3954                 case AUTO_PIN_LINE_OUT:
3955                 default:
3956                         label = texts[num_line++];
3957                         idx = 0;
3958                         break;
3959                 case AUTO_PIN_HP_OUT:
3960                         label = "Headphone";
3961                         idx = num_hp++;
3962                         break;
3963                 case AUTO_PIN_SPEAKER_OUT:
3964                         label = "Speaker";
3965                         idx = num_spk++;
3966                         break;
3967                 }
3968                 err = try_add_pb_volume(codec, spec->dac_info[i].dac,
3969                                         spec->dac_info[i].pin,
3970                                         label, idx);
3971                 if (err < 0)
3972                         return err;
3973         }
3974         return 0;
3975 }
3976
3977 static int cx_auto_add_capture_volume(struct hda_codec *codec, hda_nid_t nid,
3978                                       const char *label, const char *pfx,
3979                                       int cidx)
3980 {
3981         struct conexant_spec *spec = codec->spec;
3982         int i;
3983
3984         for (i = 0; i < spec->num_adc_nids; i++) {
3985                 hda_nid_t adc_nid = spec->adc_nids[i];
3986                 int idx = get_input_connection(codec, adc_nid, nid);
3987                 if (idx < 0)
3988                         continue;
3989                 return cx_auto_add_volume_idx(codec, label, pfx,
3990                                               cidx, adc_nid, HDA_INPUT, idx);
3991         }
3992         return 0;
3993 }
3994
3995 static int cx_auto_add_boost_volume(struct hda_codec *codec, int idx,
3996                                     const char *label, int cidx)
3997 {
3998         struct conexant_spec *spec = codec->spec;
3999         hda_nid_t mux, nid;
4000         int con;
4001
4002         nid = spec->imux_pins[idx];
4003         if (get_wcaps(codec, nid) & AC_WCAP_IN_AMP)
4004                 return cx_auto_add_volume(codec, label, " Boost", cidx,
4005                                           nid, HDA_INPUT);
4006         con = __select_input_connection(codec, spec->imux_adcs[idx], nid, &mux,
4007                                         false, 0);
4008         if (con < 0)
4009                 return 0;
4010         if (get_wcaps(codec, mux) & AC_WCAP_OUT_AMP)
4011                 return cx_auto_add_volume(codec, label, " Boost", 0,
4012                                           mux, HDA_OUTPUT);
4013         return 0;
4014 }
4015
4016 static int cx_auto_build_input_controls(struct hda_codec *codec)
4017 {
4018         struct conexant_spec *spec = codec->spec;
4019         struct hda_input_mux *imux = &spec->private_imux;
4020         const char *prev_label;
4021         int input_conn[HDA_MAX_NUM_INPUTS];
4022         int i, err, cidx;
4023         int multi_connection;
4024
4025         multi_connection = 0;
4026         for (i = 0; i < imux->num_items; i++) {
4027                 cidx = get_input_connection(codec, spec->imux_adcs[i],
4028                                             spec->imux_pins[i]);
4029                 input_conn[i] = (spec->imux_adcs[i] << 8) | cidx;
4030                 if (i > 0 && input_conn[i] != input_conn[0])
4031                         multi_connection = 1;
4032         }
4033
4034         prev_label = NULL;
4035         cidx = 0;
4036         for (i = 0; i < imux->num_items; i++) {
4037                 hda_nid_t nid = spec->imux_pins[i];
4038                 const char *label;
4039
4040                 label = hda_get_autocfg_input_label(codec, &spec->autocfg,
4041                                                     spec->imux_cfg_idx[i]);
4042                 if (label == prev_label)
4043                         cidx++;
4044                 else
4045                         cidx = 0;
4046                 prev_label = label;
4047
4048                 err = cx_auto_add_boost_volume(codec, i, label, cidx);
4049                 if (err < 0)
4050                         return err;
4051
4052                 if (!multi_connection) {
4053                         err = cx_auto_add_capture_volume(codec, nid,
4054                                                          "Capture", "", cidx);
4055                 } else {
4056                         err = cx_auto_add_capture_volume(codec, nid,
4057                                                          label, " Capture", cidx);
4058                 }
4059                 if (err < 0)
4060                         return err;
4061         }
4062
4063         if (spec->private_imux.num_items > 1 && !spec->auto_mic) {
4064                 err = snd_hda_add_new_ctls(codec, cx_auto_capture_mixers);
4065                 if (err < 0)
4066                         return err;
4067         }
4068
4069         return 0;
4070 }
4071
4072 static int cx_auto_build_controls(struct hda_codec *codec)
4073 {
4074         int err;
4075
4076         err = cx_auto_build_output_controls(codec);
4077         if (err < 0)
4078                 return err;
4079         err = cx_auto_build_input_controls(codec);
4080         if (err < 0)
4081                 return err;
4082         return conexant_build_controls(codec);
4083 }
4084
4085 static int cx_auto_search_adcs(struct hda_codec *codec)
4086 {
4087         struct conexant_spec *spec = codec->spec;
4088         hda_nid_t nid, end_nid;
4089
4090         end_nid = codec->start_nid + codec->num_nodes;
4091         for (nid = codec->start_nid; nid < end_nid; nid++) {
4092                 unsigned int caps = get_wcaps(codec, nid);
4093                 if (get_wcaps_type(caps) != AC_WID_AUD_IN)
4094                         continue;
4095                 if (caps & AC_WCAP_DIGITAL)
4096                         continue;
4097                 if (snd_BUG_ON(spec->num_adc_nids >=
4098                                ARRAY_SIZE(spec->private_adc_nids)))
4099                         break;
4100                 spec->private_adc_nids[spec->num_adc_nids++] = nid;
4101         }
4102         spec->adc_nids = spec->private_adc_nids;
4103         return 0;
4104 }
4105
4106
4107 static const struct hda_codec_ops cx_auto_patch_ops = {
4108         .build_controls = cx_auto_build_controls,
4109         .build_pcms = conexant_build_pcms,
4110         .init = cx_auto_init,
4111         .free = conexant_free,
4112         .unsol_event = cx_auto_unsol_event,
4113 #ifdef CONFIG_SND_HDA_POWER_SAVE
4114         .suspend = conexant_suspend,
4115 #endif
4116         .reboot_notify = snd_hda_shutup_pins,
4117 };
4118
4119 static int patch_conexant_auto(struct hda_codec *codec)
4120 {
4121         struct conexant_spec *spec;
4122         int err;
4123
4124         printk(KERN_INFO "hda_codec: %s: BIOS auto-probing.\n",
4125                codec->chip_name);
4126
4127         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
4128         if (!spec)
4129                 return -ENOMEM;
4130         codec->spec = spec;
4131         codec->pin_amp_workaround = 1;
4132         err = cx_auto_search_adcs(codec);
4133         if (err < 0)
4134                 return err;
4135         err = cx_auto_parse_auto_config(codec);
4136         if (err < 0) {
4137                 kfree(codec->spec);
4138                 codec->spec = NULL;
4139                 return err;
4140         }
4141         spec->capture_stream = &cx_auto_pcm_analog_capture;
4142         codec->patch_ops = cx_auto_patch_ops;
4143         if (spec->beep_amp)
4144                 snd_hda_attach_beep_device(codec, spec->beep_amp);
4145         return 0;
4146 }
4147
4148 /*
4149  */
4150
4151 static const struct hda_codec_preset snd_hda_preset_conexant[] = {
4152         { .id = 0x14f15045, .name = "CX20549 (Venice)",
4153           .patch = patch_cxt5045 },
4154         { .id = 0x14f15047, .name = "CX20551 (Waikiki)",
4155           .patch = patch_cxt5047 },
4156         { .id = 0x14f15051, .name = "CX20561 (Hermosa)",
4157           .patch = patch_cxt5051 },
4158         { .id = 0x14f15066, .name = "CX20582 (Pebble)",
4159           .patch = patch_cxt5066 },
4160         { .id = 0x14f15067, .name = "CX20583 (Pebble HSF)",
4161           .patch = patch_cxt5066 },
4162         { .id = 0x14f15068, .name = "CX20584",
4163           .patch = patch_cxt5066 },
4164         { .id = 0x14f15069, .name = "CX20585",
4165           .patch = patch_cxt5066 },
4166         { .id = 0x14f1506e, .name = "CX20590",
4167           .patch = patch_cxt5066 },
4168         { .id = 0x14f15097, .name = "CX20631",
4169           .patch = patch_conexant_auto },
4170         { .id = 0x14f15098, .name = "CX20632",
4171           .patch = patch_conexant_auto },
4172         { .id = 0x14f150a1, .name = "CX20641",
4173           .patch = patch_conexant_auto },
4174         { .id = 0x14f150a2, .name = "CX20642",
4175           .patch = patch_conexant_auto },
4176         { .id = 0x14f150ab, .name = "CX20651",
4177           .patch = patch_conexant_auto },
4178         { .id = 0x14f150ac, .name = "CX20652",
4179           .patch = patch_conexant_auto },
4180         { .id = 0x14f150b8, .name = "CX20664",
4181           .patch = patch_conexant_auto },
4182         { .id = 0x14f150b9, .name = "CX20665",
4183           .patch = patch_conexant_auto },
4184         {} /* terminator */
4185 };
4186
4187 MODULE_ALIAS("snd-hda-codec-id:14f15045");
4188 MODULE_ALIAS("snd-hda-codec-id:14f15047");
4189 MODULE_ALIAS("snd-hda-codec-id:14f15051");
4190 MODULE_ALIAS("snd-hda-codec-id:14f15066");
4191 MODULE_ALIAS("snd-hda-codec-id:14f15067");
4192 MODULE_ALIAS("snd-hda-codec-id:14f15068");
4193 MODULE_ALIAS("snd-hda-codec-id:14f15069");
4194 MODULE_ALIAS("snd-hda-codec-id:14f1506e");
4195 MODULE_ALIAS("snd-hda-codec-id:14f15097");
4196 MODULE_ALIAS("snd-hda-codec-id:14f15098");
4197 MODULE_ALIAS("snd-hda-codec-id:14f150a1");
4198 MODULE_ALIAS("snd-hda-codec-id:14f150a2");
4199 MODULE_ALIAS("snd-hda-codec-id:14f150ab");
4200 MODULE_ALIAS("snd-hda-codec-id:14f150ac");
4201 MODULE_ALIAS("snd-hda-codec-id:14f150b8");
4202 MODULE_ALIAS("snd-hda-codec-id:14f150b9");
4203
4204 MODULE_LICENSE("GPL");
4205 MODULE_DESCRIPTION("Conexant HD-audio codec");
4206
4207 static struct hda_codec_preset_list conexant_list = {
4208         .preset = snd_hda_preset_conexant,
4209         .owner = THIS_MODULE,
4210 };
4211
4212 static int __init patch_conexant_init(void)
4213 {
4214         return snd_hda_add_codec_preset(&conexant_list);
4215 }
4216
4217 static void __exit patch_conexant_exit(void)
4218 {
4219         snd_hda_delete_codec_preset(&conexant_list);
4220 }
4221
4222 module_init(patch_conexant_init)
4223 module_exit(patch_conexant_exit)