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