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