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