843d9f33f49c80188d1ddf91389d33769a507c21
[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 static const struct hda_codec_ops conexant_patch_ops = {
596         .build_controls = conexant_build_controls,
597         .build_pcms = conexant_build_pcms,
598         .init = conexant_init,
599         .free = conexant_free,
600         .set_power_state = conexant_set_power,
601 };
602
603 #ifdef CONFIG_SND_HDA_INPUT_BEEP
604 #define set_beep_amp(spec, nid, idx, dir) \
605         ((spec)->beep_amp = HDA_COMPOSE_AMP_VAL(nid, 1, idx, dir))
606 #else
607 #define set_beep_amp(spec, nid, idx, dir) /* NOP */
608 #endif
609
610 static int patch_conexant_auto(struct hda_codec *codec);
611 /*
612  * EAPD control
613  * the private value = nid | (invert << 8)
614  */
615
616 #define cxt_eapd_info           snd_ctl_boolean_mono_info
617
618 static int cxt_eapd_get(struct snd_kcontrol *kcontrol,
619                              struct snd_ctl_elem_value *ucontrol)
620 {
621         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
622         struct conexant_spec *spec = codec->spec;
623         int invert = (kcontrol->private_value >> 8) & 1;
624         if (invert)
625                 ucontrol->value.integer.value[0] = !spec->cur_eapd;
626         else
627                 ucontrol->value.integer.value[0] = spec->cur_eapd;
628         return 0;
629
630 }
631
632 static int cxt_eapd_put(struct snd_kcontrol *kcontrol,
633                              struct snd_ctl_elem_value *ucontrol)
634 {
635         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
636         struct conexant_spec *spec = codec->spec;
637         int invert = (kcontrol->private_value >> 8) & 1;
638         hda_nid_t nid = kcontrol->private_value & 0xff;
639         unsigned int eapd;
640
641         eapd = !!ucontrol->value.integer.value[0];
642         if (invert)
643                 eapd = !eapd;
644         if (eapd == spec->cur_eapd)
645                 return 0;
646         
647         spec->cur_eapd = eapd;
648         snd_hda_codec_write_cache(codec, nid,
649                                   0, AC_VERB_SET_EAPD_BTLENABLE,
650                                   eapd ? 0x02 : 0x00);
651         return 1;
652 }
653
654 /* controls for test mode */
655 #ifdef CONFIG_SND_DEBUG
656
657 #define CXT_EAPD_SWITCH(xname, nid, mask) \
658         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = 0,  \
659           .info = cxt_eapd_info, \
660           .get = cxt_eapd_get, \
661           .put = cxt_eapd_put, \
662           .private_value = nid | (mask<<16) }
663
664
665
666 static int conexant_ch_mode_info(struct snd_kcontrol *kcontrol,
667                                  struct snd_ctl_elem_info *uinfo)
668 {
669         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
670         struct conexant_spec *spec = codec->spec;
671         return snd_hda_ch_mode_info(codec, uinfo, spec->channel_mode,
672                                     spec->num_channel_mode);
673 }
674
675 static int conexant_ch_mode_get(struct snd_kcontrol *kcontrol,
676                                 struct snd_ctl_elem_value *ucontrol)
677 {
678         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
679         struct conexant_spec *spec = codec->spec;
680         return snd_hda_ch_mode_get(codec, ucontrol, spec->channel_mode,
681                                    spec->num_channel_mode,
682                                    spec->multiout.max_channels);
683 }
684
685 static int conexant_ch_mode_put(struct snd_kcontrol *kcontrol,
686                                 struct snd_ctl_elem_value *ucontrol)
687 {
688         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
689         struct conexant_spec *spec = codec->spec;
690         int err = snd_hda_ch_mode_put(codec, ucontrol, spec->channel_mode,
691                                       spec->num_channel_mode,
692                                       &spec->multiout.max_channels);
693         if (err >= 0 && spec->need_dac_fix)
694                 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
695         return err;
696 }
697
698 #define CXT_PIN_MODE(xname, nid, dir) \
699         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = 0,  \
700           .info = conexant_ch_mode_info, \
701           .get = conexant_ch_mode_get, \
702           .put = conexant_ch_mode_put, \
703           .private_value = nid | (dir<<16) }
704
705 #endif /* CONFIG_SND_DEBUG */
706
707 /* Conexant 5045 specific */
708
709 static const hda_nid_t cxt5045_dac_nids[1] = { 0x19 };
710 static const hda_nid_t cxt5045_adc_nids[1] = { 0x1a };
711 static const hda_nid_t cxt5045_capsrc_nids[1] = { 0x1a };
712 #define CXT5045_SPDIF_OUT       0x18
713
714 static const struct hda_channel_mode cxt5045_modes[1] = {
715         { 2, NULL },
716 };
717
718 static const struct hda_input_mux cxt5045_capture_source = {
719         .num_items = 2,
720         .items = {
721                 { "IntMic", 0x1 },
722                 { "ExtMic", 0x2 },
723         }
724 };
725
726 static const struct hda_input_mux cxt5045_capture_source_benq = {
727         .num_items = 5,
728         .items = {
729                 { "IntMic", 0x1 },
730                 { "ExtMic", 0x2 },
731                 { "LineIn", 0x3 },
732                 { "CD",     0x4 },
733                 { "Mixer",  0x0 },
734         }
735 };
736
737 static const struct hda_input_mux cxt5045_capture_source_hp530 = {
738         .num_items = 2,
739         .items = {
740                 { "ExtMic", 0x1 },
741                 { "IntMic", 0x2 },
742         }
743 };
744
745 /* turn on/off EAPD (+ mute HP) as a master switch */
746 static int cxt5045_hp_master_sw_put(struct snd_kcontrol *kcontrol,
747                                     struct snd_ctl_elem_value *ucontrol)
748 {
749         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
750         struct conexant_spec *spec = codec->spec;
751         unsigned int bits;
752
753         if (!cxt_eapd_put(kcontrol, ucontrol))
754                 return 0;
755
756         /* toggle internal speakers mute depending of presence of
757          * the headphone jack
758          */
759         bits = (!spec->hp_present && spec->cur_eapd) ? 0 : HDA_AMP_MUTE;
760         snd_hda_codec_amp_stereo(codec, 0x10, HDA_OUTPUT, 0,
761                                  HDA_AMP_MUTE, bits);
762
763         bits = spec->cur_eapd ? 0 : HDA_AMP_MUTE;
764         snd_hda_codec_amp_stereo(codec, 0x11, HDA_OUTPUT, 0,
765                                  HDA_AMP_MUTE, bits);
766         return 1;
767 }
768
769 /* bind volumes of both NID 0x10 and 0x11 */
770 static const struct hda_bind_ctls cxt5045_hp_bind_master_vol = {
771         .ops = &snd_hda_bind_vol,
772         .values = {
773                 HDA_COMPOSE_AMP_VAL(0x10, 3, 0, HDA_OUTPUT),
774                 HDA_COMPOSE_AMP_VAL(0x11, 3, 0, HDA_OUTPUT),
775                 0
776         },
777 };
778
779 /* toggle input of built-in and mic jack appropriately */
780 static void cxt5045_hp_automic(struct hda_codec *codec)
781 {
782         static const struct hda_verb mic_jack_on[] = {
783                 {0x14, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
784                 {0x12, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
785                 {}
786         };
787         static const struct hda_verb mic_jack_off[] = {
788                 {0x12, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
789                 {0x14, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
790                 {}
791         };
792         unsigned int present;
793
794         present = snd_hda_jack_detect(codec, 0x12);
795         if (present)
796                 snd_hda_sequence_write(codec, mic_jack_on);
797         else
798                 snd_hda_sequence_write(codec, mic_jack_off);
799 }
800
801
802 /* mute internal speaker if HP is plugged */
803 static void cxt5045_hp_automute(struct hda_codec *codec)
804 {
805         struct conexant_spec *spec = codec->spec;
806         unsigned int bits;
807
808         spec->hp_present = snd_hda_jack_detect(codec, 0x11);
809
810         bits = (spec->hp_present || !spec->cur_eapd) ? HDA_AMP_MUTE : 0; 
811         snd_hda_codec_amp_stereo(codec, 0x10, HDA_OUTPUT, 0,
812                                  HDA_AMP_MUTE, bits);
813 }
814
815 /* unsolicited event for HP jack sensing */
816 static void cxt5045_hp_unsol_event(struct hda_codec *codec,
817                                    unsigned int res)
818 {
819         res >>= 26;
820         switch (res) {
821         case CONEXANT_HP_EVENT:
822                 cxt5045_hp_automute(codec);
823                 break;
824         case CONEXANT_MIC_EVENT:
825                 cxt5045_hp_automic(codec);
826                 break;
827
828         }
829 }
830
831 static const struct snd_kcontrol_new cxt5045_mixers[] = {
832         HDA_CODEC_VOLUME("Internal Mic Capture Volume", 0x1a, 0x01, HDA_INPUT),
833         HDA_CODEC_MUTE("Internal Mic Capture Switch", 0x1a, 0x01, HDA_INPUT),
834         HDA_CODEC_VOLUME("Mic Capture Volume", 0x1a, 0x02, HDA_INPUT),
835         HDA_CODEC_MUTE("Mic Capture Switch", 0x1a, 0x02, HDA_INPUT),
836         HDA_CODEC_VOLUME("PCM Playback Volume", 0x17, 0x0, HDA_INPUT),
837         HDA_CODEC_MUTE("PCM Playback Switch", 0x17, 0x0, HDA_INPUT),
838         HDA_CODEC_VOLUME("Internal Mic Playback Volume", 0x17, 0x1, HDA_INPUT),
839         HDA_CODEC_MUTE("Internal Mic Playback Switch", 0x17, 0x1, HDA_INPUT),
840         HDA_CODEC_VOLUME("Mic Playback Volume", 0x17, 0x2, HDA_INPUT),
841         HDA_CODEC_MUTE("Mic Playback Switch", 0x17, 0x2, HDA_INPUT),
842         HDA_BIND_VOL("Master Playback Volume", &cxt5045_hp_bind_master_vol),
843         {
844                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
845                 .name = "Master Playback Switch",
846                 .info = cxt_eapd_info,
847                 .get = cxt_eapd_get,
848                 .put = cxt5045_hp_master_sw_put,
849                 .private_value = 0x10,
850         },
851
852         {}
853 };
854
855 static const struct snd_kcontrol_new cxt5045_benq_mixers[] = {
856         HDA_CODEC_VOLUME("CD Capture Volume", 0x1a, 0x04, HDA_INPUT),
857         HDA_CODEC_MUTE("CD Capture Switch", 0x1a, 0x04, HDA_INPUT),
858         HDA_CODEC_VOLUME("CD Playback Volume", 0x17, 0x4, HDA_INPUT),
859         HDA_CODEC_MUTE("CD Playback Switch", 0x17, 0x4, HDA_INPUT),
860
861         HDA_CODEC_VOLUME("Line In Capture Volume", 0x1a, 0x03, HDA_INPUT),
862         HDA_CODEC_MUTE("Line In Capture Switch", 0x1a, 0x03, HDA_INPUT),
863         HDA_CODEC_VOLUME("Line In Playback Volume", 0x17, 0x3, HDA_INPUT),
864         HDA_CODEC_MUTE("Line In Playback Switch", 0x17, 0x3, HDA_INPUT),
865
866         HDA_CODEC_VOLUME("Mixer Capture Volume", 0x1a, 0x0, HDA_INPUT),
867         HDA_CODEC_MUTE("Mixer Capture Switch", 0x1a, 0x0, HDA_INPUT),
868
869         {}
870 };
871
872 static const struct snd_kcontrol_new cxt5045_mixers_hp530[] = {
873         HDA_CODEC_VOLUME("Internal Mic Capture Volume", 0x1a, 0x02, HDA_INPUT),
874         HDA_CODEC_MUTE("Internal Mic Capture Switch", 0x1a, 0x02, HDA_INPUT),
875         HDA_CODEC_VOLUME("Mic Capture Volume", 0x1a, 0x01, HDA_INPUT),
876         HDA_CODEC_MUTE("Mic Capture Switch", 0x1a, 0x01, HDA_INPUT),
877         HDA_CODEC_VOLUME("PCM Playback Volume", 0x17, 0x0, HDA_INPUT),
878         HDA_CODEC_MUTE("PCM Playback Switch", 0x17, 0x0, HDA_INPUT),
879         HDA_CODEC_VOLUME("Internal Mic Playback Volume", 0x17, 0x2, HDA_INPUT),
880         HDA_CODEC_MUTE("Internal Mic Playback Switch", 0x17, 0x2, HDA_INPUT),
881         HDA_CODEC_VOLUME("Mic Playback Volume", 0x17, 0x1, HDA_INPUT),
882         HDA_CODEC_MUTE("Mic Playback Switch", 0x17, 0x1, HDA_INPUT),
883         HDA_BIND_VOL("Master Playback Volume", &cxt5045_hp_bind_master_vol),
884         {
885                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
886                 .name = "Master Playback Switch",
887                 .info = cxt_eapd_info,
888                 .get = cxt_eapd_get,
889                 .put = cxt5045_hp_master_sw_put,
890                 .private_value = 0x10,
891         },
892
893         {}
894 };
895
896 static const struct hda_verb cxt5045_init_verbs[] = {
897         /* Line in, Mic */
898         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN|AC_PINCTL_VREF_80 },
899         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN|AC_PINCTL_VREF_80 },
900         /* HP, Amp  */
901         {0x10, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
902         {0x10, AC_VERB_SET_CONNECT_SEL, 0x1},
903         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
904         {0x11, AC_VERB_SET_CONNECT_SEL, 0x1},
905         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
906         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
907         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
908         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
909         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
910         /* Record selector: Internal mic */
911         {0x1a, AC_VERB_SET_CONNECT_SEL,0x1},
912         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE,
913          AC_AMP_SET_INPUT|AC_AMP_SET_RIGHT|AC_AMP_SET_LEFT|0x17},
914         /* SPDIF route: PCM */
915         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
916         { 0x13, AC_VERB_SET_CONNECT_SEL, 0x0 },
917         /* EAPD */
918         {0x10, AC_VERB_SET_EAPD_BTLENABLE, 0x2 }, /* default on */ 
919         { } /* end */
920 };
921
922 static const struct hda_verb cxt5045_benq_init_verbs[] = {
923         /* Internal Mic, Mic */
924         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN|AC_PINCTL_VREF_80 },
925         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN|AC_PINCTL_VREF_80 },
926         /* Line In,HP, Amp  */
927         {0x10, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
928         {0x10, AC_VERB_SET_CONNECT_SEL, 0x1},
929         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
930         {0x11, AC_VERB_SET_CONNECT_SEL, 0x1},
931         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
932         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
933         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
934         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
935         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
936         /* Record selector: Internal mic */
937         {0x1a, AC_VERB_SET_CONNECT_SEL, 0x1},
938         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE,
939          AC_AMP_SET_INPUT|AC_AMP_SET_RIGHT|AC_AMP_SET_LEFT|0x17},
940         /* SPDIF route: PCM */
941         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
942         {0x13, AC_VERB_SET_CONNECT_SEL, 0x0},
943         /* EAPD */
944         {0x10, AC_VERB_SET_EAPD_BTLENABLE, 0x2}, /* default on */
945         { } /* end */
946 };
947
948 static const struct hda_verb cxt5045_hp_sense_init_verbs[] = {
949         /* pin sensing on HP jack */
950         {0x11, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_HP_EVENT},
951         { } /* end */
952 };
953
954 static const struct hda_verb cxt5045_mic_sense_init_verbs[] = {
955         /* pin sensing on HP jack */
956         {0x12, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_MIC_EVENT},
957         { } /* end */
958 };
959
960 #ifdef CONFIG_SND_DEBUG
961 /* Test configuration for debugging, modelled after the ALC260 test
962  * configuration.
963  */
964 static const struct hda_input_mux cxt5045_test_capture_source = {
965         .num_items = 5,
966         .items = {
967                 { "MIXER", 0x0 },
968                 { "MIC1 pin", 0x1 },
969                 { "LINE1 pin", 0x2 },
970                 { "HP-OUT pin", 0x3 },
971                 { "CD pin", 0x4 },
972         },
973 };
974
975 static const struct snd_kcontrol_new cxt5045_test_mixer[] = {
976
977         /* Output controls */
978         HDA_CODEC_VOLUME("Speaker Playback Volume", 0x10, 0x0, HDA_OUTPUT),
979         HDA_CODEC_MUTE("Speaker Playback Switch", 0x10, 0x0, HDA_OUTPUT),
980         HDA_CODEC_VOLUME("Node 11 Playback Volume", 0x11, 0x0, HDA_OUTPUT),
981         HDA_CODEC_MUTE("Node 11 Playback Switch", 0x11, 0x0, HDA_OUTPUT),
982         HDA_CODEC_VOLUME("Node 12 Playback Volume", 0x12, 0x0, HDA_OUTPUT),
983         HDA_CODEC_MUTE("Node 12 Playback Switch", 0x12, 0x0, HDA_OUTPUT),
984         
985         /* Modes for retasking pin widgets */
986         CXT_PIN_MODE("HP-OUT pin mode", 0x11, CXT_PIN_DIR_INOUT),
987         CXT_PIN_MODE("LINE1 pin mode", 0x12, CXT_PIN_DIR_INOUT),
988
989         /* EAPD Switch Control */
990         CXT_EAPD_SWITCH("External Amplifier", 0x10, 0x0),
991
992         /* Loopback mixer controls */
993
994         HDA_CODEC_VOLUME("Mixer-1 Volume", 0x17, 0x0, HDA_INPUT),
995         HDA_CODEC_MUTE("Mixer-1 Switch", 0x17, 0x0, HDA_INPUT),
996         HDA_CODEC_VOLUME("Mixer-2 Volume", 0x17, 0x1, HDA_INPUT),
997         HDA_CODEC_MUTE("Mixer-2 Switch", 0x17, 0x1, HDA_INPUT),
998         HDA_CODEC_VOLUME("Mixer-3 Volume", 0x17, 0x2, HDA_INPUT),
999         HDA_CODEC_MUTE("Mixer-3 Switch", 0x17, 0x2, HDA_INPUT),
1000         HDA_CODEC_VOLUME("Mixer-4 Volume", 0x17, 0x3, HDA_INPUT),
1001         HDA_CODEC_MUTE("Mixer-4 Switch", 0x17, 0x3, HDA_INPUT),
1002         HDA_CODEC_VOLUME("Mixer-5 Volume", 0x17, 0x4, HDA_INPUT),
1003         HDA_CODEC_MUTE("Mixer-5 Switch", 0x17, 0x4, HDA_INPUT),
1004         {
1005                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1006                 .name = "Input Source",
1007                 .info = conexant_mux_enum_info,
1008                 .get = conexant_mux_enum_get,
1009                 .put = conexant_mux_enum_put,
1010         },
1011         /* Audio input controls */
1012         HDA_CODEC_VOLUME("Input-1 Volume", 0x1a, 0x0, HDA_INPUT),
1013         HDA_CODEC_MUTE("Input-1 Switch", 0x1a, 0x0, HDA_INPUT),
1014         HDA_CODEC_VOLUME("Input-2 Volume", 0x1a, 0x1, HDA_INPUT),
1015         HDA_CODEC_MUTE("Input-2 Switch", 0x1a, 0x1, HDA_INPUT),
1016         HDA_CODEC_VOLUME("Input-3 Volume", 0x1a, 0x2, HDA_INPUT),
1017         HDA_CODEC_MUTE("Input-3 Switch", 0x1a, 0x2, HDA_INPUT),
1018         HDA_CODEC_VOLUME("Input-4 Volume", 0x1a, 0x3, HDA_INPUT),
1019         HDA_CODEC_MUTE("Input-4 Switch", 0x1a, 0x3, HDA_INPUT),
1020         HDA_CODEC_VOLUME("Input-5 Volume", 0x1a, 0x4, HDA_INPUT),
1021         HDA_CODEC_MUTE("Input-5 Switch", 0x1a, 0x4, HDA_INPUT),
1022         { } /* end */
1023 };
1024
1025 static const struct hda_verb cxt5045_test_init_verbs[] = {
1026         /* Set connections */
1027         { 0x10, AC_VERB_SET_CONNECT_SEL, 0x0 },
1028         { 0x11, AC_VERB_SET_CONNECT_SEL, 0x0 },
1029         { 0x12, AC_VERB_SET_CONNECT_SEL, 0x0 },
1030         /* Enable retasking pins as output, initially without power amp */
1031         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1032         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1033
1034         /* Disable digital (SPDIF) pins initially, but users can enable
1035          * them via a mixer switch.  In the case of SPDIF-out, this initverb
1036          * payload also sets the generation to 0, output to be in "consumer"
1037          * PCM format, copyright asserted, no pre-emphasis and no validity
1038          * control.
1039          */
1040         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1041         {0x18, AC_VERB_SET_DIGI_CONVERT_1, 0},
1042
1043         /* Start with output sum widgets muted and their output gains at min */
1044         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1045         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
1046
1047         /* Unmute retasking pin widget output buffers since the default
1048          * state appears to be output.  As the pin mode is changed by the
1049          * user the pin mode control will take care of enabling the pin's
1050          * input/output buffers as needed.
1051          */
1052         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1053         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1054
1055         /* Mute capture amp left and right */
1056         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1057
1058         /* Set ADC connection select to match default mixer setting (mic1
1059          * pin)
1060          */
1061         {0x1a, AC_VERB_SET_CONNECT_SEL, 0x00},
1062         {0x17, AC_VERB_SET_CONNECT_SEL, 0x00},
1063
1064         /* Mute all inputs to mixer widget (even unconnected ones) */
1065         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)}, /* Mixer pin */
1066         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)}, /* Mic1 pin */
1067         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)}, /* Line pin */
1068         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)}, /* HP pin */
1069         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)}, /* CD pin */
1070
1071         { }
1072 };
1073 #endif
1074
1075
1076 /* initialize jack-sensing, too */
1077 static int cxt5045_init(struct hda_codec *codec)
1078 {
1079         conexant_init(codec);
1080         cxt5045_hp_automute(codec);
1081         return 0;
1082 }
1083
1084
1085 enum {
1086         CXT5045_LAPTOP_HPSENSE,
1087         CXT5045_LAPTOP_MICSENSE,
1088         CXT5045_LAPTOP_HPMICSENSE,
1089         CXT5045_BENQ,
1090         CXT5045_LAPTOP_HP530,
1091 #ifdef CONFIG_SND_DEBUG
1092         CXT5045_TEST,
1093 #endif
1094         CXT5045_AUTO,
1095         CXT5045_MODELS
1096 };
1097
1098 static const char * const cxt5045_models[CXT5045_MODELS] = {
1099         [CXT5045_LAPTOP_HPSENSE]        = "laptop-hpsense",
1100         [CXT5045_LAPTOP_MICSENSE]       = "laptop-micsense",
1101         [CXT5045_LAPTOP_HPMICSENSE]     = "laptop-hpmicsense",
1102         [CXT5045_BENQ]                  = "benq",
1103         [CXT5045_LAPTOP_HP530]          = "laptop-hp530",
1104 #ifdef CONFIG_SND_DEBUG
1105         [CXT5045_TEST]          = "test",
1106 #endif
1107         [CXT5045_AUTO]                  = "auto",
1108 };
1109
1110 static const struct snd_pci_quirk cxt5045_cfg_tbl[] = {
1111         SND_PCI_QUIRK(0x103c, 0x30d5, "HP 530", CXT5045_LAPTOP_HP530),
1112         SND_PCI_QUIRK(0x1179, 0xff31, "Toshiba P105", CXT5045_LAPTOP_MICSENSE),
1113         SND_PCI_QUIRK(0x152d, 0x0753, "Benq R55E", CXT5045_BENQ),
1114         SND_PCI_QUIRK(0x1734, 0x10ad, "Fujitsu Si1520", CXT5045_LAPTOP_MICSENSE),
1115         SND_PCI_QUIRK(0x1734, 0x10cb, "Fujitsu Si3515", CXT5045_LAPTOP_HPMICSENSE),
1116         SND_PCI_QUIRK(0x1734, 0x110e, "Fujitsu V5505",
1117                       CXT5045_LAPTOP_HPMICSENSE),
1118         SND_PCI_QUIRK(0x1509, 0x1e40, "FIC", CXT5045_LAPTOP_HPMICSENSE),
1119         SND_PCI_QUIRK(0x1509, 0x2f05, "FIC", CXT5045_LAPTOP_HPMICSENSE),
1120         SND_PCI_QUIRK(0x1509, 0x2f06, "FIC", CXT5045_LAPTOP_HPMICSENSE),
1121         SND_PCI_QUIRK_MASK(0x1631, 0xff00, 0xc100, "Packard Bell",
1122                            CXT5045_LAPTOP_HPMICSENSE),
1123         SND_PCI_QUIRK(0x8086, 0x2111, "Conexant Reference board", CXT5045_LAPTOP_HPSENSE),
1124         {}
1125 };
1126
1127 static int patch_cxt5045(struct hda_codec *codec)
1128 {
1129         struct conexant_spec *spec;
1130         int board_config;
1131
1132         board_config = snd_hda_check_board_config(codec, CXT5045_MODELS,
1133                                                   cxt5045_models,
1134                                                   cxt5045_cfg_tbl);
1135         if (board_config < 0)
1136                 board_config = CXT5045_AUTO; /* model=auto as default */
1137         if (board_config == CXT5045_AUTO)
1138                 return patch_conexant_auto(codec);
1139
1140         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1141         if (!spec)
1142                 return -ENOMEM;
1143         codec->spec = spec;
1144         codec->pin_amp_workaround = 1;
1145
1146         spec->multiout.max_channels = 2;
1147         spec->multiout.num_dacs = ARRAY_SIZE(cxt5045_dac_nids);
1148         spec->multiout.dac_nids = cxt5045_dac_nids;
1149         spec->multiout.dig_out_nid = CXT5045_SPDIF_OUT;
1150         spec->num_adc_nids = 1;
1151         spec->adc_nids = cxt5045_adc_nids;
1152         spec->capsrc_nids = cxt5045_capsrc_nids;
1153         spec->input_mux = &cxt5045_capture_source;
1154         spec->num_mixers = 1;
1155         spec->mixers[0] = cxt5045_mixers;
1156         spec->num_init_verbs = 1;
1157         spec->init_verbs[0] = cxt5045_init_verbs;
1158         spec->spdif_route = 0;
1159         spec->num_channel_mode = ARRAY_SIZE(cxt5045_modes);
1160         spec->channel_mode = cxt5045_modes;
1161
1162         set_beep_amp(spec, 0x16, 0, 1);
1163
1164         codec->patch_ops = conexant_patch_ops;
1165
1166         switch (board_config) {
1167         case CXT5045_LAPTOP_HPSENSE:
1168                 codec->patch_ops.unsol_event = cxt5045_hp_unsol_event;
1169                 spec->input_mux = &cxt5045_capture_source;
1170                 spec->num_init_verbs = 2;
1171                 spec->init_verbs[1] = cxt5045_hp_sense_init_verbs;
1172                 spec->mixers[0] = cxt5045_mixers;
1173                 codec->patch_ops.init = cxt5045_init;
1174                 break;
1175         case CXT5045_LAPTOP_MICSENSE:
1176                 codec->patch_ops.unsol_event = cxt5045_hp_unsol_event;
1177                 spec->input_mux = &cxt5045_capture_source;
1178                 spec->num_init_verbs = 2;
1179                 spec->init_verbs[1] = cxt5045_mic_sense_init_verbs;
1180                 spec->mixers[0] = cxt5045_mixers;
1181                 codec->patch_ops.init = cxt5045_init;
1182                 break;
1183         default:
1184         case CXT5045_LAPTOP_HPMICSENSE:
1185                 codec->patch_ops.unsol_event = cxt5045_hp_unsol_event;
1186                 spec->input_mux = &cxt5045_capture_source;
1187                 spec->num_init_verbs = 3;
1188                 spec->init_verbs[1] = cxt5045_hp_sense_init_verbs;
1189                 spec->init_verbs[2] = cxt5045_mic_sense_init_verbs;
1190                 spec->mixers[0] = cxt5045_mixers;
1191                 codec->patch_ops.init = cxt5045_init;
1192                 break;
1193         case CXT5045_BENQ:
1194                 codec->patch_ops.unsol_event = cxt5045_hp_unsol_event;
1195                 spec->input_mux = &cxt5045_capture_source_benq;
1196                 spec->num_init_verbs = 1;
1197                 spec->init_verbs[0] = cxt5045_benq_init_verbs;
1198                 spec->mixers[0] = cxt5045_mixers;
1199                 spec->mixers[1] = cxt5045_benq_mixers;
1200                 spec->num_mixers = 2;
1201                 codec->patch_ops.init = cxt5045_init;
1202                 break;
1203         case CXT5045_LAPTOP_HP530:
1204                 codec->patch_ops.unsol_event = cxt5045_hp_unsol_event;
1205                 spec->input_mux = &cxt5045_capture_source_hp530;
1206                 spec->num_init_verbs = 2;
1207                 spec->init_verbs[1] = cxt5045_hp_sense_init_verbs;
1208                 spec->mixers[0] = cxt5045_mixers_hp530;
1209                 codec->patch_ops.init = cxt5045_init;
1210                 break;
1211 #ifdef CONFIG_SND_DEBUG
1212         case CXT5045_TEST:
1213                 spec->input_mux = &cxt5045_test_capture_source;
1214                 spec->mixers[0] = cxt5045_test_mixer;
1215                 spec->init_verbs[0] = cxt5045_test_init_verbs;
1216                 break;
1217                 
1218 #endif  
1219         }
1220
1221         switch (codec->subsystem_id >> 16) {
1222         case 0x103c:
1223         case 0x1631:
1224         case 0x1734:
1225         case 0x17aa:
1226                 /* HP, Packard Bell, Fujitsu-Siemens & Lenovo laptops have
1227                  * really bad sound over 0dB on NID 0x17. Fix max PCM level to
1228                  * 0 dB (originally it has 0x2b steps with 0dB offset 0x14)
1229                  */
1230                 snd_hda_override_amp_caps(codec, 0x17, HDA_INPUT,
1231                                           (0x14 << AC_AMPCAP_OFFSET_SHIFT) |
1232                                           (0x14 << AC_AMPCAP_NUM_STEPS_SHIFT) |
1233                                           (0x05 << AC_AMPCAP_STEP_SIZE_SHIFT) |
1234                                           (1 << AC_AMPCAP_MUTE_SHIFT));
1235                 break;
1236         }
1237
1238         if (spec->beep_amp)
1239                 snd_hda_attach_beep_device(codec, get_amp_nid_(spec->beep_amp));
1240
1241         return 0;
1242 }
1243
1244
1245 /* Conexant 5047 specific */
1246 #define CXT5047_SPDIF_OUT       0x11
1247
1248 static const hda_nid_t cxt5047_dac_nids[1] = { 0x10 }; /* 0x1c */
1249 static const hda_nid_t cxt5047_adc_nids[1] = { 0x12 };
1250 static const hda_nid_t cxt5047_capsrc_nids[1] = { 0x1a };
1251
1252 static const struct hda_channel_mode cxt5047_modes[1] = {
1253         { 2, NULL },
1254 };
1255
1256 static const struct hda_input_mux cxt5047_toshiba_capture_source = {
1257         .num_items = 2,
1258         .items = {
1259                 { "ExtMic", 0x2 },
1260                 { "Line-In", 0x1 },
1261         }
1262 };
1263
1264 /* turn on/off EAPD (+ mute HP) as a master switch */
1265 static int cxt5047_hp_master_sw_put(struct snd_kcontrol *kcontrol,
1266                                     struct snd_ctl_elem_value *ucontrol)
1267 {
1268         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1269         struct conexant_spec *spec = codec->spec;
1270         unsigned int bits;
1271
1272         if (!cxt_eapd_put(kcontrol, ucontrol))
1273                 return 0;
1274
1275         /* toggle internal speakers mute depending of presence of
1276          * the headphone jack
1277          */
1278         bits = (!spec->hp_present && spec->cur_eapd) ? 0 : HDA_AMP_MUTE;
1279         /* NOTE: Conexat codec needs the index for *OUTPUT* amp of
1280          * pin widgets unlike other codecs.  In this case, we need to
1281          * set index 0x01 for the volume from the mixer amp 0x19.
1282          */
1283         snd_hda_codec_amp_stereo(codec, 0x1d, HDA_OUTPUT, 0x01,
1284                                  HDA_AMP_MUTE, bits);
1285         bits = spec->cur_eapd ? 0 : HDA_AMP_MUTE;
1286         snd_hda_codec_amp_stereo(codec, 0x13, HDA_OUTPUT, 0,
1287                                  HDA_AMP_MUTE, bits);
1288         return 1;
1289 }
1290
1291 /* mute internal speaker if HP is plugged */
1292 static void cxt5047_hp_automute(struct hda_codec *codec)
1293 {
1294         struct conexant_spec *spec = codec->spec;
1295         unsigned int bits;
1296
1297         spec->hp_present = snd_hda_jack_detect(codec, 0x13);
1298
1299         bits = (spec->hp_present || !spec->cur_eapd) ? HDA_AMP_MUTE : 0;
1300         /* See the note in cxt5047_hp_master_sw_put */
1301         snd_hda_codec_amp_stereo(codec, 0x1d, HDA_OUTPUT, 0x01,
1302                                  HDA_AMP_MUTE, bits);
1303 }
1304
1305 /* toggle input of built-in and mic jack appropriately */
1306 static void cxt5047_hp_automic(struct hda_codec *codec)
1307 {
1308         static const struct hda_verb mic_jack_on[] = {
1309                 {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1310                 {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1311                 {}
1312         };
1313         static const struct hda_verb mic_jack_off[] = {
1314                 {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1315                 {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1316                 {}
1317         };
1318         unsigned int present;
1319
1320         present = snd_hda_jack_detect(codec, 0x15);
1321         if (present)
1322                 snd_hda_sequence_write(codec, mic_jack_on);
1323         else
1324                 snd_hda_sequence_write(codec, mic_jack_off);
1325 }
1326
1327 /* unsolicited event for HP jack sensing */
1328 static void cxt5047_hp_unsol_event(struct hda_codec *codec,
1329                                   unsigned int res)
1330 {
1331         switch (res >> 26) {
1332         case CONEXANT_HP_EVENT:
1333                 cxt5047_hp_automute(codec);
1334                 break;
1335         case CONEXANT_MIC_EVENT:
1336                 cxt5047_hp_automic(codec);
1337                 break;
1338         }
1339 }
1340
1341 static const struct snd_kcontrol_new cxt5047_base_mixers[] = {
1342         HDA_CODEC_VOLUME("Mic Playback Volume", 0x19, 0x02, HDA_INPUT),
1343         HDA_CODEC_MUTE("Mic Playback Switch", 0x19, 0x02, HDA_INPUT),
1344         HDA_CODEC_VOLUME("Mic Boost Volume", 0x1a, 0x0, HDA_OUTPUT),
1345         HDA_CODEC_VOLUME("Capture Volume", 0x12, 0x03, HDA_INPUT),
1346         HDA_CODEC_MUTE("Capture Switch", 0x12, 0x03, HDA_INPUT),
1347         HDA_CODEC_VOLUME("PCM Volume", 0x10, 0x00, HDA_OUTPUT),
1348         HDA_CODEC_MUTE("PCM Switch", 0x10, 0x00, HDA_OUTPUT),
1349         {
1350                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1351                 .name = "Master Playback Switch",
1352                 .info = cxt_eapd_info,
1353                 .get = cxt_eapd_get,
1354                 .put = cxt5047_hp_master_sw_put,
1355                 .private_value = 0x13,
1356         },
1357
1358         {}
1359 };
1360
1361 static const struct snd_kcontrol_new cxt5047_hp_spk_mixers[] = {
1362         /* See the note in cxt5047_hp_master_sw_put */
1363         HDA_CODEC_VOLUME("Speaker Playback Volume", 0x1d, 0x01, HDA_OUTPUT),
1364         HDA_CODEC_VOLUME("Headphone Playback Volume", 0x13, 0x00, HDA_OUTPUT),
1365         {}
1366 };
1367
1368 static const struct snd_kcontrol_new cxt5047_hp_only_mixers[] = {
1369         HDA_CODEC_VOLUME("Master Playback Volume", 0x13, 0x00, HDA_OUTPUT),
1370         { } /* end */
1371 };
1372
1373 static const struct hda_verb cxt5047_init_verbs[] = {
1374         /* Line in, Mic, Built-in Mic */
1375         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
1376         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN|AC_PINCTL_VREF_50 },
1377         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN|AC_PINCTL_VREF_50 },
1378         /* HP, Speaker  */
1379         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
1380         {0x13, AC_VERB_SET_CONNECT_SEL, 0x0}, /* mixer(0x19) */
1381         {0x1d, AC_VERB_SET_CONNECT_SEL, 0x1}, /* mixer(0x19) */
1382         /* Record selector: Mic */
1383         {0x12, AC_VERB_SET_CONNECT_SEL,0x03},
1384         {0x19, AC_VERB_SET_AMP_GAIN_MUTE,
1385          AC_AMP_SET_INPUT|AC_AMP_SET_RIGHT|AC_AMP_SET_LEFT|0x17},
1386         {0x1A, AC_VERB_SET_CONNECT_SEL,0x02},
1387         {0x1A, AC_VERB_SET_AMP_GAIN_MUTE,
1388          AC_AMP_SET_OUTPUT|AC_AMP_SET_RIGHT|AC_AMP_SET_LEFT|0x00},
1389         {0x1A, AC_VERB_SET_AMP_GAIN_MUTE,
1390          AC_AMP_SET_OUTPUT|AC_AMP_SET_RIGHT|AC_AMP_SET_LEFT|0x03},
1391         /* SPDIF route: PCM */
1392         { 0x18, AC_VERB_SET_CONNECT_SEL, 0x0 },
1393         /* Enable unsolicited events */
1394         {0x13, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_HP_EVENT},
1395         {0x15, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_MIC_EVENT},
1396         { } /* end */
1397 };
1398
1399 /* configuration for Toshiba Laptops */
1400 static const struct hda_verb cxt5047_toshiba_init_verbs[] = {
1401         {0x13, AC_VERB_SET_EAPD_BTLENABLE, 0x0}, /* default off */
1402         {}
1403 };
1404
1405 /* Test configuration for debugging, modelled after the ALC260 test
1406  * configuration.
1407  */
1408 #ifdef CONFIG_SND_DEBUG
1409 static const struct hda_input_mux cxt5047_test_capture_source = {
1410         .num_items = 4,
1411         .items = {
1412                 { "LINE1 pin", 0x0 },
1413                 { "MIC1 pin", 0x1 },
1414                 { "MIC2 pin", 0x2 },
1415                 { "CD pin", 0x3 },
1416         },
1417 };
1418
1419 static const struct snd_kcontrol_new cxt5047_test_mixer[] = {
1420
1421         /* Output only controls */
1422         HDA_CODEC_VOLUME("OutAmp-1 Volume", 0x10, 0x0, HDA_OUTPUT),
1423         HDA_CODEC_MUTE("OutAmp-1 Switch", 0x10,0x0, HDA_OUTPUT),
1424         HDA_CODEC_VOLUME("OutAmp-2 Volume", 0x1c, 0x0, HDA_OUTPUT),
1425         HDA_CODEC_MUTE("OutAmp-2 Switch", 0x1c, 0x0, HDA_OUTPUT),
1426         HDA_CODEC_VOLUME("Speaker Playback Volume", 0x1d, 0x0, HDA_OUTPUT),
1427         HDA_CODEC_MUTE("Speaker Playback Switch", 0x1d, 0x0, HDA_OUTPUT),
1428         HDA_CODEC_VOLUME("HeadPhone Playback Volume", 0x13, 0x0, HDA_OUTPUT),
1429         HDA_CODEC_MUTE("HeadPhone Playback Switch", 0x13, 0x0, HDA_OUTPUT),
1430         HDA_CODEC_VOLUME("Line1-Out Playback Volume", 0x14, 0x0, HDA_OUTPUT),
1431         HDA_CODEC_MUTE("Line1-Out Playback Switch", 0x14, 0x0, HDA_OUTPUT),
1432         HDA_CODEC_VOLUME("Line2-Out Playback Volume", 0x15, 0x0, HDA_OUTPUT),
1433         HDA_CODEC_MUTE("Line2-Out Playback Switch", 0x15, 0x0, HDA_OUTPUT),
1434
1435         /* Modes for retasking pin widgets */
1436         CXT_PIN_MODE("LINE1 pin mode", 0x14, CXT_PIN_DIR_INOUT),
1437         CXT_PIN_MODE("MIC1 pin mode", 0x15, CXT_PIN_DIR_INOUT),
1438
1439         /* EAPD Switch Control */
1440         CXT_EAPD_SWITCH("External Amplifier", 0x13, 0x0),
1441
1442         /* Loopback mixer controls */
1443         HDA_CODEC_VOLUME("MIC1 Playback Volume", 0x12, 0x01, HDA_INPUT),
1444         HDA_CODEC_MUTE("MIC1 Playback Switch", 0x12, 0x01, HDA_INPUT),
1445         HDA_CODEC_VOLUME("MIC2 Playback Volume", 0x12, 0x02, HDA_INPUT),
1446         HDA_CODEC_MUTE("MIC2 Playback Switch", 0x12, 0x02, HDA_INPUT),
1447         HDA_CODEC_VOLUME("LINE Playback Volume", 0x12, 0x0, HDA_INPUT),
1448         HDA_CODEC_MUTE("LINE Playback Switch", 0x12, 0x0, HDA_INPUT),
1449         HDA_CODEC_VOLUME("CD Playback Volume", 0x12, 0x04, HDA_INPUT),
1450         HDA_CODEC_MUTE("CD Playback Switch", 0x12, 0x04, HDA_INPUT),
1451
1452         HDA_CODEC_VOLUME("Capture-1 Volume", 0x19, 0x0, HDA_INPUT),
1453         HDA_CODEC_MUTE("Capture-1 Switch", 0x19, 0x0, HDA_INPUT),
1454         HDA_CODEC_VOLUME("Capture-2 Volume", 0x19, 0x1, HDA_INPUT),
1455         HDA_CODEC_MUTE("Capture-2 Switch", 0x19, 0x1, HDA_INPUT),
1456         HDA_CODEC_VOLUME("Capture-3 Volume", 0x19, 0x2, HDA_INPUT),
1457         HDA_CODEC_MUTE("Capture-3 Switch", 0x19, 0x2, HDA_INPUT),
1458         HDA_CODEC_VOLUME("Capture-4 Volume", 0x19, 0x3, HDA_INPUT),
1459         HDA_CODEC_MUTE("Capture-4 Switch", 0x19, 0x3, HDA_INPUT),
1460         {
1461                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1462                 .name = "Input Source",
1463                 .info = conexant_mux_enum_info,
1464                 .get = conexant_mux_enum_get,
1465                 .put = conexant_mux_enum_put,
1466         },
1467         HDA_CODEC_VOLUME("Mic Boost Volume", 0x1a, 0x0, HDA_OUTPUT),
1468
1469         { } /* end */
1470 };
1471
1472 static const struct hda_verb cxt5047_test_init_verbs[] = {
1473         /* Enable retasking pins as output, initially without power amp */
1474         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1475         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1476         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1477
1478         /* Disable digital (SPDIF) pins initially, but users can enable
1479          * them via a mixer switch.  In the case of SPDIF-out, this initverb
1480          * payload also sets the generation to 0, output to be in "consumer"
1481          * PCM format, copyright asserted, no pre-emphasis and no validity
1482          * control.
1483          */
1484         {0x18, AC_VERB_SET_DIGI_CONVERT_1, 0},
1485
1486         /* Ensure mic1, mic2, line1 pin widgets take input from the 
1487          * OUT1 sum bus when acting as an output.
1488          */
1489         {0x1a, AC_VERB_SET_CONNECT_SEL, 0},
1490         {0x1b, AC_VERB_SET_CONNECT_SEL, 0},
1491
1492         /* Start with output sum widgets muted and their output gains at min */
1493         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1494         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
1495
1496         /* Unmute retasking pin widget output buffers since the default
1497          * state appears to be output.  As the pin mode is changed by the
1498          * user the pin mode control will take care of enabling the pin's
1499          * input/output buffers as needed.
1500          */
1501         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1502         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1503         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1504
1505         /* Mute capture amp left and right */
1506         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1507
1508         /* Set ADC connection select to match default mixer setting (mic1
1509          * pin)
1510          */
1511         {0x12, AC_VERB_SET_CONNECT_SEL, 0x00},
1512
1513         /* Mute all inputs to mixer widget (even unconnected ones) */
1514         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)}, /* mic1 pin */
1515         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)}, /* mic2 pin */
1516         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)}, /* line1 pin */
1517         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)}, /* line2 pin */
1518         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)}, /* CD pin */
1519         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(5)}, /* Beep-gen pin */
1520         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(6)}, /* Line-out pin */
1521         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(7)}, /* HP-pin pin */
1522
1523         { }
1524 };
1525 #endif
1526
1527
1528 /* initialize jack-sensing, too */
1529 static int cxt5047_hp_init(struct hda_codec *codec)
1530 {
1531         conexant_init(codec);
1532         cxt5047_hp_automute(codec);
1533         return 0;
1534 }
1535
1536
1537 enum {
1538         CXT5047_LAPTOP,         /* Laptops w/o EAPD support */
1539         CXT5047_LAPTOP_HP,      /* Some HP laptops */
1540         CXT5047_LAPTOP_EAPD,    /* Laptops with EAPD support */
1541 #ifdef CONFIG_SND_DEBUG
1542         CXT5047_TEST,
1543 #endif
1544         CXT5047_AUTO,
1545         CXT5047_MODELS
1546 };
1547
1548 static const char * const cxt5047_models[CXT5047_MODELS] = {
1549         [CXT5047_LAPTOP]        = "laptop",
1550         [CXT5047_LAPTOP_HP]     = "laptop-hp",
1551         [CXT5047_LAPTOP_EAPD]   = "laptop-eapd",
1552 #ifdef CONFIG_SND_DEBUG
1553         [CXT5047_TEST]          = "test",
1554 #endif
1555         [CXT5047_AUTO]          = "auto",
1556 };
1557
1558 static const struct snd_pci_quirk cxt5047_cfg_tbl[] = {
1559         SND_PCI_QUIRK(0x103c, 0x30a5, "HP DV5200T/DV8000T", CXT5047_LAPTOP_HP),
1560         SND_PCI_QUIRK_MASK(0x103c, 0xff00, 0x3000, "HP DV Series",
1561                            CXT5047_LAPTOP),
1562         SND_PCI_QUIRK(0x1179, 0xff31, "Toshiba P100", CXT5047_LAPTOP_EAPD),
1563         {}
1564 };
1565
1566 static int patch_cxt5047(struct hda_codec *codec)
1567 {
1568         struct conexant_spec *spec;
1569         int board_config;
1570
1571         board_config = snd_hda_check_board_config(codec, CXT5047_MODELS,
1572                                                   cxt5047_models,
1573                                                   cxt5047_cfg_tbl);
1574         if (board_config < 0)
1575                 board_config = CXT5047_AUTO; /* model=auto as default */
1576         if (board_config == CXT5047_AUTO)
1577                 return patch_conexant_auto(codec);
1578
1579         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1580         if (!spec)
1581                 return -ENOMEM;
1582         codec->spec = spec;
1583         codec->pin_amp_workaround = 1;
1584
1585         spec->multiout.max_channels = 2;
1586         spec->multiout.num_dacs = ARRAY_SIZE(cxt5047_dac_nids);
1587         spec->multiout.dac_nids = cxt5047_dac_nids;
1588         spec->multiout.dig_out_nid = CXT5047_SPDIF_OUT;
1589         spec->num_adc_nids = 1;
1590         spec->adc_nids = cxt5047_adc_nids;
1591         spec->capsrc_nids = cxt5047_capsrc_nids;
1592         spec->num_mixers = 1;
1593         spec->mixers[0] = cxt5047_base_mixers;
1594         spec->num_init_verbs = 1;
1595         spec->init_verbs[0] = cxt5047_init_verbs;
1596         spec->spdif_route = 0;
1597         spec->num_channel_mode = ARRAY_SIZE(cxt5047_modes),
1598         spec->channel_mode = cxt5047_modes,
1599
1600         codec->patch_ops = conexant_patch_ops;
1601
1602         switch (board_config) {
1603         case CXT5047_LAPTOP:
1604                 spec->num_mixers = 2;
1605                 spec->mixers[1] = cxt5047_hp_spk_mixers;
1606                 codec->patch_ops.unsol_event = cxt5047_hp_unsol_event;
1607                 break;
1608         case CXT5047_LAPTOP_HP:
1609                 spec->num_mixers = 2;
1610                 spec->mixers[1] = cxt5047_hp_only_mixers;
1611                 codec->patch_ops.unsol_event = cxt5047_hp_unsol_event;
1612                 codec->patch_ops.init = cxt5047_hp_init;
1613                 break;
1614         case CXT5047_LAPTOP_EAPD:
1615                 spec->input_mux = &cxt5047_toshiba_capture_source;
1616                 spec->num_mixers = 2;
1617                 spec->mixers[1] = cxt5047_hp_spk_mixers;
1618                 spec->num_init_verbs = 2;
1619                 spec->init_verbs[1] = cxt5047_toshiba_init_verbs;
1620                 codec->patch_ops.unsol_event = cxt5047_hp_unsol_event;
1621                 break;
1622 #ifdef CONFIG_SND_DEBUG
1623         case CXT5047_TEST:
1624                 spec->input_mux = &cxt5047_test_capture_source;
1625                 spec->mixers[0] = cxt5047_test_mixer;
1626                 spec->init_verbs[0] = cxt5047_test_init_verbs;
1627                 codec->patch_ops.unsol_event = cxt5047_hp_unsol_event;
1628 #endif  
1629         }
1630         spec->vmaster_nid = 0x13;
1631
1632         switch (codec->subsystem_id >> 16) {
1633         case 0x103c:
1634                 /* HP laptops have really bad sound over 0 dB on NID 0x10.
1635                  * Fix max PCM level to 0 dB (originally it has 0x1e steps
1636                  * with 0 dB offset 0x17)
1637                  */
1638                 snd_hda_override_amp_caps(codec, 0x10, HDA_INPUT,
1639                                           (0x17 << AC_AMPCAP_OFFSET_SHIFT) |
1640                                           (0x17 << AC_AMPCAP_NUM_STEPS_SHIFT) |
1641                                           (0x05 << AC_AMPCAP_STEP_SIZE_SHIFT) |
1642                                           (1 << AC_AMPCAP_MUTE_SHIFT));
1643                 break;
1644         }
1645
1646         return 0;
1647 }
1648
1649 /* Conexant 5051 specific */
1650 static const hda_nid_t cxt5051_dac_nids[1] = { 0x10 };
1651 static const hda_nid_t cxt5051_adc_nids[2] = { 0x14, 0x15 };
1652
1653 static const struct hda_channel_mode cxt5051_modes[1] = {
1654         { 2, NULL },
1655 };
1656
1657 static void cxt5051_update_speaker(struct hda_codec *codec)
1658 {
1659         struct conexant_spec *spec = codec->spec;
1660         unsigned int pinctl;
1661         /* headphone pin */
1662         pinctl = (spec->hp_present && spec->cur_eapd) ? PIN_HP : 0;
1663         snd_hda_codec_write(codec, 0x16, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
1664                             pinctl);
1665         /* speaker pin */
1666         pinctl = (!spec->hp_present && spec->cur_eapd) ? PIN_OUT : 0;
1667         snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
1668                             pinctl);
1669         /* on ideapad there is an aditional speaker (subwoofer) to mute */
1670         if (spec->ideapad)
1671                 snd_hda_codec_write(codec, 0x1b, 0,
1672                                     AC_VERB_SET_PIN_WIDGET_CONTROL,
1673                                     pinctl);
1674 }
1675
1676 /* turn on/off EAPD (+ mute HP) as a master switch */
1677 static int cxt5051_hp_master_sw_put(struct snd_kcontrol *kcontrol,
1678                                     struct snd_ctl_elem_value *ucontrol)
1679 {
1680         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1681
1682         if (!cxt_eapd_put(kcontrol, ucontrol))
1683                 return 0;
1684         cxt5051_update_speaker(codec);
1685         return 1;
1686 }
1687
1688 /* toggle input of built-in and mic jack appropriately */
1689 static void cxt5051_portb_automic(struct hda_codec *codec)
1690 {
1691         struct conexant_spec *spec = codec->spec;
1692         unsigned int present;
1693
1694         if (!(spec->auto_mic & AUTO_MIC_PORTB))
1695                 return;
1696         present = snd_hda_jack_detect(codec, 0x17);
1697         snd_hda_codec_write(codec, 0x14, 0,
1698                             AC_VERB_SET_CONNECT_SEL,
1699                             present ? 0x01 : 0x00);
1700 }
1701
1702 /* switch the current ADC according to the jack state */
1703 static void cxt5051_portc_automic(struct hda_codec *codec)
1704 {
1705         struct conexant_spec *spec = codec->spec;
1706         unsigned int present;
1707         hda_nid_t new_adc;
1708
1709         if (!(spec->auto_mic & AUTO_MIC_PORTC))
1710                 return;
1711         present = snd_hda_jack_detect(codec, 0x18);
1712         if (present)
1713                 spec->cur_adc_idx = 1;
1714         else
1715                 spec->cur_adc_idx = 0;
1716         new_adc = spec->adc_nids[spec->cur_adc_idx];
1717         if (spec->cur_adc && spec->cur_adc != new_adc) {
1718                 /* stream is running, let's swap the current ADC */
1719                 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
1720                 spec->cur_adc = new_adc;
1721                 snd_hda_codec_setup_stream(codec, new_adc,
1722                                            spec->cur_adc_stream_tag, 0,
1723                                            spec->cur_adc_format);
1724         }
1725 }
1726
1727 /* mute internal speaker if HP is plugged */
1728 static void cxt5051_hp_automute(struct hda_codec *codec)
1729 {
1730         struct conexant_spec *spec = codec->spec;
1731
1732         spec->hp_present = snd_hda_jack_detect(codec, 0x16);
1733         cxt5051_update_speaker(codec);
1734 }
1735
1736 /* unsolicited event for HP jack sensing */
1737 static void cxt5051_hp_unsol_event(struct hda_codec *codec,
1738                                    unsigned int res)
1739 {
1740         int nid = (res & AC_UNSOL_RES_SUBTAG) >> 20;
1741         switch (res >> 26) {
1742         case CONEXANT_HP_EVENT:
1743                 cxt5051_hp_automute(codec);
1744                 break;
1745         case CXT5051_PORTB_EVENT:
1746                 cxt5051_portb_automic(codec);
1747                 break;
1748         case CXT5051_PORTC_EVENT:
1749                 cxt5051_portc_automic(codec);
1750                 break;
1751         }
1752         snd_hda_input_jack_report(codec, nid);
1753 }
1754
1755 static const struct snd_kcontrol_new cxt5051_playback_mixers[] = {
1756         HDA_CODEC_VOLUME("Master Playback Volume", 0x10, 0x00, HDA_OUTPUT),
1757         {
1758                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1759                 .name = "Master Playback Switch",
1760                 .info = cxt_eapd_info,
1761                 .get = cxt_eapd_get,
1762                 .put = cxt5051_hp_master_sw_put,
1763                 .private_value = 0x1a,
1764         },
1765         {}
1766 };
1767
1768 static const struct snd_kcontrol_new cxt5051_capture_mixers[] = {
1769         HDA_CODEC_VOLUME("Internal Mic Volume", 0x14, 0x00, HDA_INPUT),
1770         HDA_CODEC_MUTE("Internal Mic Switch", 0x14, 0x00, HDA_INPUT),
1771         HDA_CODEC_VOLUME("Mic Volume", 0x14, 0x01, HDA_INPUT),
1772         HDA_CODEC_MUTE("Mic Switch", 0x14, 0x01, HDA_INPUT),
1773         HDA_CODEC_VOLUME("Docking Mic Volume", 0x15, 0x00, HDA_INPUT),
1774         HDA_CODEC_MUTE("Docking Mic Switch", 0x15, 0x00, HDA_INPUT),
1775         {}
1776 };
1777
1778 static const struct snd_kcontrol_new cxt5051_hp_mixers[] = {
1779         HDA_CODEC_VOLUME("Internal Mic Volume", 0x14, 0x00, HDA_INPUT),
1780         HDA_CODEC_MUTE("Internal Mic Switch", 0x14, 0x00, HDA_INPUT),
1781         HDA_CODEC_VOLUME("Mic Volume", 0x15, 0x00, HDA_INPUT),
1782         HDA_CODEC_MUTE("Mic Switch", 0x15, 0x00, HDA_INPUT),
1783         {}
1784 };
1785
1786 static const struct snd_kcontrol_new cxt5051_hp_dv6736_mixers[] = {
1787         HDA_CODEC_VOLUME("Capture Volume", 0x14, 0x00, HDA_INPUT),
1788         HDA_CODEC_MUTE("Capture Switch", 0x14, 0x00, HDA_INPUT),
1789         {}
1790 };
1791
1792 static const struct snd_kcontrol_new cxt5051_f700_mixers[] = {
1793         HDA_CODEC_VOLUME("Capture Volume", 0x14, 0x01, HDA_INPUT),
1794         HDA_CODEC_MUTE("Capture Switch", 0x14, 0x01, HDA_INPUT),
1795         {}
1796 };
1797
1798 static const struct snd_kcontrol_new cxt5051_toshiba_mixers[] = {
1799         HDA_CODEC_VOLUME("Internal Mic Volume", 0x14, 0x00, HDA_INPUT),
1800         HDA_CODEC_MUTE("Internal Mic Switch", 0x14, 0x00, HDA_INPUT),
1801         HDA_CODEC_VOLUME("Mic Volume", 0x14, 0x01, HDA_INPUT),
1802         HDA_CODEC_MUTE("Mic Switch", 0x14, 0x01, HDA_INPUT),
1803         {}
1804 };
1805
1806 static const struct hda_verb cxt5051_init_verbs[] = {
1807         /* Line in, Mic */
1808         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x03},
1809         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1810         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x03},
1811         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1812         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
1813         {0x1d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x03},
1814         /* SPK  */
1815         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1816         {0x1a, AC_VERB_SET_CONNECT_SEL, 0x00},
1817         /* HP, Amp  */
1818         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1819         {0x16, AC_VERB_SET_CONNECT_SEL, 0x00},
1820         /* DAC1 */      
1821         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1822         /* Record selector: Internal mic */
1823         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x44},
1824         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1) | 0x44},
1825         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x44},
1826         /* SPDIF route: PCM */
1827         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1828         {0x1c, AC_VERB_SET_CONNECT_SEL, 0x0},
1829         /* EAPD */
1830         {0x1a, AC_VERB_SET_EAPD_BTLENABLE, 0x2}, /* default on */ 
1831         {0x16, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN|CONEXANT_HP_EVENT},
1832         { } /* end */
1833 };
1834
1835 static const struct hda_verb cxt5051_hp_dv6736_init_verbs[] = {
1836         /* Line in, Mic */
1837         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x03},
1838         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1839         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x0},
1840         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x0},
1841         /* SPK  */
1842         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1843         {0x1a, AC_VERB_SET_CONNECT_SEL, 0x00},
1844         /* HP, Amp  */
1845         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1846         {0x16, AC_VERB_SET_CONNECT_SEL, 0x00},
1847         /* DAC1 */
1848         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1849         /* Record selector: Internal mic */
1850         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1) | 0x44},
1851         {0x14, AC_VERB_SET_CONNECT_SEL, 0x1},
1852         /* SPDIF route: PCM */
1853         {0x1c, AC_VERB_SET_CONNECT_SEL, 0x0},
1854         /* EAPD */
1855         {0x1a, AC_VERB_SET_EAPD_BTLENABLE, 0x2}, /* default on */
1856         {0x16, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN|CONEXANT_HP_EVENT},
1857         { } /* end */
1858 };
1859
1860 static const struct hda_verb cxt5051_f700_init_verbs[] = {
1861         /* Line in, Mic */
1862         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x03},
1863         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1864         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x0},
1865         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x0},
1866         /* SPK  */
1867         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1868         {0x1a, AC_VERB_SET_CONNECT_SEL, 0x00},
1869         /* HP, Amp  */
1870         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1871         {0x16, AC_VERB_SET_CONNECT_SEL, 0x00},
1872         /* DAC1 */
1873         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1874         /* Record selector: Internal mic */
1875         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1) | 0x44},
1876         {0x14, AC_VERB_SET_CONNECT_SEL, 0x1},
1877         /* SPDIF route: PCM */
1878         {0x1c, AC_VERB_SET_CONNECT_SEL, 0x0},
1879         /* EAPD */
1880         {0x1a, AC_VERB_SET_EAPD_BTLENABLE, 0x2}, /* default on */
1881         {0x16, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN|CONEXANT_HP_EVENT},
1882         { } /* end */
1883 };
1884
1885 static void cxt5051_init_mic_port(struct hda_codec *codec, hda_nid_t nid,
1886                                  unsigned int event)
1887 {
1888         snd_hda_codec_write(codec, nid, 0,
1889                             AC_VERB_SET_UNSOLICITED_ENABLE,
1890                             AC_USRSP_EN | event);
1891 }
1892
1893 static void cxt5051_init_mic_jack(struct hda_codec *codec, hda_nid_t nid)
1894 {
1895         snd_hda_input_jack_add(codec, nid, SND_JACK_MICROPHONE, NULL);
1896         snd_hda_input_jack_report(codec, nid);
1897 }
1898
1899 static const struct hda_verb cxt5051_ideapad_init_verbs[] = {
1900         /* Subwoofer */
1901         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1902         {0x1b, AC_VERB_SET_CONNECT_SEL, 0x00},
1903         { } /* end */
1904 };
1905
1906 /* initialize jack-sensing, too */
1907 static int cxt5051_init(struct hda_codec *codec)
1908 {
1909         struct conexant_spec *spec = codec->spec;
1910
1911         conexant_init(codec);
1912
1913         if (spec->auto_mic & AUTO_MIC_PORTB)
1914                 cxt5051_init_mic_port(codec, 0x17, CXT5051_PORTB_EVENT);
1915         if (spec->auto_mic & AUTO_MIC_PORTC)
1916                 cxt5051_init_mic_port(codec, 0x18, CXT5051_PORTC_EVENT);
1917
1918         if (codec->patch_ops.unsol_event) {
1919                 cxt5051_hp_automute(codec);
1920                 cxt5051_portb_automic(codec);
1921                 cxt5051_portc_automic(codec);
1922         }
1923         return 0;
1924 }
1925
1926
1927 enum {
1928         CXT5051_LAPTOP,  /* Laptops w/ EAPD support */
1929         CXT5051_HP,     /* no docking */
1930         CXT5051_HP_DV6736,      /* HP without mic switch */
1931         CXT5051_F700,       /* HP Compaq Presario F700 */
1932         CXT5051_TOSHIBA,        /* Toshiba M300 & co */
1933         CXT5051_IDEAPAD,        /* Lenovo IdeaPad Y430 */
1934         CXT5051_AUTO,           /* auto-parser */
1935         CXT5051_MODELS
1936 };
1937
1938 static const char *const cxt5051_models[CXT5051_MODELS] = {
1939         [CXT5051_LAPTOP]        = "laptop",
1940         [CXT5051_HP]            = "hp",
1941         [CXT5051_HP_DV6736]     = "hp-dv6736",
1942         [CXT5051_F700]          = "hp-700",
1943         [CXT5051_TOSHIBA]       = "toshiba",
1944         [CXT5051_IDEAPAD]       = "ideapad",
1945         [CXT5051_AUTO]          = "auto",
1946 };
1947
1948 static const struct snd_pci_quirk cxt5051_cfg_tbl[] = {
1949         SND_PCI_QUIRK(0x103c, 0x30cf, "HP DV6736", CXT5051_HP_DV6736),
1950         SND_PCI_QUIRK(0x103c, 0x360b, "Compaq Presario CQ60", CXT5051_HP),
1951         SND_PCI_QUIRK(0x103c, 0x30ea, "Compaq Presario F700", CXT5051_F700),
1952         SND_PCI_QUIRK(0x1179, 0xff50, "Toshiba M30x", CXT5051_TOSHIBA),
1953         SND_PCI_QUIRK(0x14f1, 0x0101, "Conexant Reference board",
1954                       CXT5051_LAPTOP),
1955         SND_PCI_QUIRK(0x14f1, 0x5051, "HP Spartan 1.1", CXT5051_HP),
1956         SND_PCI_QUIRK(0x17aa, 0x3a0d, "Lenovo IdeaPad", CXT5051_IDEAPAD),
1957         {}
1958 };
1959
1960 static int patch_cxt5051(struct hda_codec *codec)
1961 {
1962         struct conexant_spec *spec;
1963         int board_config;
1964
1965         board_config = snd_hda_check_board_config(codec, CXT5051_MODELS,
1966                                                   cxt5051_models,
1967                                                   cxt5051_cfg_tbl);
1968         if (board_config < 0)
1969                 board_config = CXT5051_AUTO; /* model=auto as default */
1970         if (board_config == CXT5051_AUTO)
1971                 return patch_conexant_auto(codec);
1972
1973         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1974         if (!spec)
1975                 return -ENOMEM;
1976         codec->spec = spec;
1977         codec->pin_amp_workaround = 1;
1978
1979         codec->patch_ops = conexant_patch_ops;
1980         codec->patch_ops.init = cxt5051_init;
1981
1982         spec->multiout.max_channels = 2;
1983         spec->multiout.num_dacs = ARRAY_SIZE(cxt5051_dac_nids);
1984         spec->multiout.dac_nids = cxt5051_dac_nids;
1985         spec->multiout.dig_out_nid = CXT5051_SPDIF_OUT;
1986         spec->num_adc_nids = 1; /* not 2; via auto-mic switch */
1987         spec->adc_nids = cxt5051_adc_nids;
1988         spec->num_mixers = 2;
1989         spec->mixers[0] = cxt5051_capture_mixers;
1990         spec->mixers[1] = cxt5051_playback_mixers;
1991         spec->num_init_verbs = 1;
1992         spec->init_verbs[0] = cxt5051_init_verbs;
1993         spec->spdif_route = 0;
1994         spec->num_channel_mode = ARRAY_SIZE(cxt5051_modes);
1995         spec->channel_mode = cxt5051_modes;
1996         spec->cur_adc = 0;
1997         spec->cur_adc_idx = 0;
1998
1999         set_beep_amp(spec, 0x13, 0, HDA_OUTPUT);
2000
2001         codec->patch_ops.unsol_event = cxt5051_hp_unsol_event;
2002
2003         spec->auto_mic = AUTO_MIC_PORTB | AUTO_MIC_PORTC;
2004         switch (board_config) {
2005         case CXT5051_HP:
2006                 spec->mixers[0] = cxt5051_hp_mixers;
2007                 break;
2008         case CXT5051_HP_DV6736:
2009                 spec->init_verbs[0] = cxt5051_hp_dv6736_init_verbs;
2010                 spec->mixers[0] = cxt5051_hp_dv6736_mixers;
2011                 spec->auto_mic = 0;
2012                 break;
2013         case CXT5051_F700:
2014                 spec->init_verbs[0] = cxt5051_f700_init_verbs;
2015                 spec->mixers[0] = cxt5051_f700_mixers;
2016                 spec->auto_mic = 0;
2017                 break;
2018         case CXT5051_TOSHIBA:
2019                 spec->mixers[0] = cxt5051_toshiba_mixers;
2020                 spec->auto_mic = AUTO_MIC_PORTB;
2021                 break;
2022         case CXT5051_IDEAPAD:
2023                 spec->init_verbs[spec->num_init_verbs++] =
2024                         cxt5051_ideapad_init_verbs;
2025                 spec->ideapad = 1;
2026                 break;
2027         }
2028
2029         if (spec->beep_amp)
2030                 snd_hda_attach_beep_device(codec, get_amp_nid_(spec->beep_amp));
2031
2032         conexant_init_jacks(codec);
2033         if (spec->auto_mic & AUTO_MIC_PORTB)
2034                 cxt5051_init_mic_jack(codec, 0x17);
2035         if (spec->auto_mic & AUTO_MIC_PORTC)
2036                 cxt5051_init_mic_jack(codec, 0x18);
2037
2038         return 0;
2039 }
2040
2041 /* Conexant 5066 specific */
2042
2043 static const hda_nid_t cxt5066_dac_nids[1] = { 0x10 };
2044 static const hda_nid_t cxt5066_adc_nids[3] = { 0x14, 0x15, 0x16 };
2045 static const hda_nid_t cxt5066_capsrc_nids[1] = { 0x17 };
2046 static const hda_nid_t cxt5066_digout_pin_nids[2] = { 0x20, 0x22 };
2047
2048 /* OLPC's microphone port is DC coupled for use with external sensors,
2049  * therefore we use a 50% mic bias in order to center the input signal with
2050  * the DC input range of the codec. */
2051 #define CXT5066_OLPC_EXT_MIC_BIAS PIN_VREF50
2052
2053 static const struct hda_channel_mode cxt5066_modes[1] = {
2054         { 2, NULL },
2055 };
2056
2057 #define HP_PRESENT_PORT_A       (1 << 0)
2058 #define HP_PRESENT_PORT_D       (1 << 1)
2059 #define hp_port_a_present(spec) ((spec)->hp_present & HP_PRESENT_PORT_A)
2060 #define hp_port_d_present(spec) ((spec)->hp_present & HP_PRESENT_PORT_D)
2061
2062 static void cxt5066_update_speaker(struct hda_codec *codec)
2063 {
2064         struct conexant_spec *spec = codec->spec;
2065         unsigned int pinctl;
2066
2067         snd_printdd("CXT5066: update speaker, hp_present=%d, cur_eapd=%d\n",
2068                     spec->hp_present, spec->cur_eapd);
2069
2070         /* Port A (HP) */
2071         pinctl = (hp_port_a_present(spec) && spec->cur_eapd) ? PIN_HP : 0;
2072         snd_hda_codec_write(codec, 0x19, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
2073                         pinctl);
2074
2075         /* Port D (HP/LO) */
2076         pinctl = spec->cur_eapd ? spec->port_d_mode : 0;
2077         if (spec->dell_automute || spec->thinkpad) {
2078                 /* Mute if Port A is connected */
2079                 if (hp_port_a_present(spec))
2080                         pinctl = 0;
2081         } else {
2082                 /* Thinkpad/Dell doesn't give pin-D status */
2083                 if (!hp_port_d_present(spec))
2084                         pinctl = 0;
2085         }
2086         snd_hda_codec_write(codec, 0x1c, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
2087                         pinctl);
2088
2089         /* CLASS_D AMP */
2090         pinctl = (!spec->hp_present && spec->cur_eapd) ? PIN_OUT : 0;
2091         snd_hda_codec_write(codec, 0x1f, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
2092                         pinctl);
2093 }
2094
2095 /* turn on/off EAPD (+ mute HP) as a master switch */
2096 static int cxt5066_hp_master_sw_put(struct snd_kcontrol *kcontrol,
2097                                     struct snd_ctl_elem_value *ucontrol)
2098 {
2099         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2100
2101         if (!cxt_eapd_put(kcontrol, ucontrol))
2102                 return 0;
2103
2104         cxt5066_update_speaker(codec);
2105         return 1;
2106 }
2107
2108 static const struct hda_input_mux cxt5066_olpc_dc_bias = {
2109         .num_items = 3,
2110         .items = {
2111                 { "Off", PIN_IN },
2112                 { "50%", PIN_VREF50 },
2113                 { "80%", PIN_VREF80 },
2114         },
2115 };
2116
2117 static int cxt5066_set_olpc_dc_bias(struct hda_codec *codec)
2118 {
2119         struct conexant_spec *spec = codec->spec;
2120         /* Even though port F is the DC input, the bias is controlled on port B.
2121          * we also leave that port as an active input (but unselected) in DC mode
2122          * just in case that is necessary to make the bias setting take effect. */
2123         return snd_hda_codec_write_cache(codec, 0x1a, 0,
2124                 AC_VERB_SET_PIN_WIDGET_CONTROL,
2125                 cxt5066_olpc_dc_bias.items[spec->dc_input_bias].index);
2126 }
2127
2128 /* OLPC defers mic widget control until when capture is started because the
2129  * microphone LED comes on as soon as these settings are put in place. if we
2130  * did this before recording, it would give the false indication that recording
2131  * is happening when it is not. */
2132 static void cxt5066_olpc_select_mic(struct hda_codec *codec)
2133 {
2134         struct conexant_spec *spec = codec->spec;
2135         if (!spec->recording)
2136                 return;
2137
2138         if (spec->dc_enable) {
2139                 /* in DC mode we ignore presence detection and just use the jack
2140                  * through our special DC port */
2141                 const struct hda_verb enable_dc_mode[] = {
2142                         /* disble internal mic, port C */
2143                         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2144
2145                         /* enable DC capture, port F */
2146                         {0x1e, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
2147                         {},
2148                 };
2149
2150                 snd_hda_sequence_write(codec, enable_dc_mode);
2151                 /* port B input disabled (and bias set) through the following call */
2152                 cxt5066_set_olpc_dc_bias(codec);
2153                 return;
2154         }
2155
2156         /* disable DC (port F) */
2157         snd_hda_codec_write(codec, 0x1e, 0, AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
2158
2159         /* external mic, port B */
2160         snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
2161                 spec->ext_mic_present ? CXT5066_OLPC_EXT_MIC_BIAS : 0);
2162
2163         /* internal mic, port C */
2164         snd_hda_codec_write(codec, 0x1b, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
2165                 spec->ext_mic_present ? 0 : PIN_VREF80);
2166 }
2167
2168 /* toggle input of built-in and mic jack appropriately */
2169 static void cxt5066_olpc_automic(struct hda_codec *codec)
2170 {
2171         struct conexant_spec *spec = codec->spec;
2172         unsigned int present;
2173
2174         if (spec->dc_enable) /* don't do presence detection in DC mode */
2175                 return;
2176
2177         present = snd_hda_codec_read(codec, 0x1a, 0,
2178                                      AC_VERB_GET_PIN_SENSE, 0) & 0x80000000;
2179         if (present)
2180                 snd_printdd("CXT5066: external microphone detected\n");
2181         else
2182                 snd_printdd("CXT5066: external microphone absent\n");
2183
2184         snd_hda_codec_write(codec, 0x17, 0, AC_VERB_SET_CONNECT_SEL,
2185                 present ? 0 : 1);
2186         spec->ext_mic_present = !!present;
2187
2188         cxt5066_olpc_select_mic(codec);
2189 }
2190
2191 /* toggle input of built-in digital mic and mic jack appropriately */
2192 static void cxt5066_vostro_automic(struct hda_codec *codec)
2193 {
2194         unsigned int present;
2195
2196         struct hda_verb ext_mic_present[] = {
2197                 /* enable external mic, port B */
2198                 {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
2199
2200                 /* switch to external mic input */
2201                 {0x17, AC_VERB_SET_CONNECT_SEL, 0},
2202                 {0x14, AC_VERB_SET_CONNECT_SEL, 0},
2203
2204                 /* disable internal digital mic */
2205                 {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2206                 {}
2207         };
2208         static const struct hda_verb ext_mic_absent[] = {
2209                 /* enable internal mic, port C */
2210                 {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
2211
2212                 /* switch to internal mic input */
2213                 {0x14, AC_VERB_SET_CONNECT_SEL, 2},
2214
2215                 /* disable external mic, port B */
2216                 {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2217                 {}
2218         };
2219
2220         present = snd_hda_jack_detect(codec, 0x1a);
2221         if (present) {
2222                 snd_printdd("CXT5066: external microphone detected\n");
2223                 snd_hda_sequence_write(codec, ext_mic_present);
2224         } else {
2225                 snd_printdd("CXT5066: external microphone absent\n");
2226                 snd_hda_sequence_write(codec, ext_mic_absent);
2227         }
2228 }
2229
2230 /* toggle input of built-in digital mic and mic jack appropriately */
2231 static void cxt5066_ideapad_automic(struct hda_codec *codec)
2232 {
2233         unsigned int present;
2234
2235         struct hda_verb ext_mic_present[] = {
2236                 {0x14, AC_VERB_SET_CONNECT_SEL, 0},
2237                 {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
2238                 {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2239                 {}
2240         };
2241         static const struct hda_verb ext_mic_absent[] = {
2242                 {0x14, AC_VERB_SET_CONNECT_SEL, 2},
2243                 {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
2244                 {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2245                 {}
2246         };
2247
2248         present = snd_hda_jack_detect(codec, 0x1b);
2249         if (present) {
2250                 snd_printdd("CXT5066: external microphone detected\n");
2251                 snd_hda_sequence_write(codec, ext_mic_present);
2252         } else {
2253                 snd_printdd("CXT5066: external microphone absent\n");
2254                 snd_hda_sequence_write(codec, ext_mic_absent);
2255         }
2256 }
2257
2258
2259 /* toggle input of built-in digital mic and mic jack appropriately */
2260 static void cxt5066_asus_automic(struct hda_codec *codec)
2261 {
2262         unsigned int present;
2263
2264         present = snd_hda_jack_detect(codec, 0x1b);
2265         snd_printdd("CXT5066: external microphone present=%d\n", present);
2266         snd_hda_codec_write(codec, 0x17, 0, AC_VERB_SET_CONNECT_SEL,
2267                             present ? 1 : 0);
2268 }
2269
2270
2271 /* toggle input of built-in digital mic and mic jack appropriately */
2272 static void cxt5066_hp_laptop_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 : 3);
2280 }
2281
2282
2283 /* toggle input of built-in digital mic and mic jack appropriately
2284    order is: external mic -> dock mic -> interal mic */
2285 static void cxt5066_thinkpad_automic(struct hda_codec *codec)
2286 {
2287         unsigned int ext_present, dock_present;
2288
2289         static const struct hda_verb ext_mic_present[] = {
2290                 {0x14, AC_VERB_SET_CONNECT_SEL, 0},
2291                 {0x17, AC_VERB_SET_CONNECT_SEL, 1},
2292                 {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
2293                 {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2294                 {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2295                 {}
2296         };
2297         static const struct hda_verb dock_mic_present[] = {
2298                 {0x14, AC_VERB_SET_CONNECT_SEL, 0},
2299                 {0x17, AC_VERB_SET_CONNECT_SEL, 0},
2300                 {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
2301                 {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2302                 {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2303                 {}
2304         };
2305         static const struct hda_verb ext_mic_absent[] = {
2306                 {0x14, AC_VERB_SET_CONNECT_SEL, 2},
2307                 {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
2308                 {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2309                 {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2310                 {}
2311         };
2312
2313         ext_present = snd_hda_jack_detect(codec, 0x1b);
2314         dock_present = snd_hda_jack_detect(codec, 0x1a);
2315         if (ext_present) {
2316                 snd_printdd("CXT5066: external microphone detected\n");
2317                 snd_hda_sequence_write(codec, ext_mic_present);
2318         } else if (dock_present) {
2319                 snd_printdd("CXT5066: dock microphone detected\n");
2320                 snd_hda_sequence_write(codec, dock_mic_present);
2321         } else {
2322                 snd_printdd("CXT5066: external microphone absent\n");
2323                 snd_hda_sequence_write(codec, ext_mic_absent);
2324         }
2325 }
2326
2327 /* mute internal speaker if HP is plugged */
2328 static void cxt5066_hp_automute(struct hda_codec *codec)
2329 {
2330         struct conexant_spec *spec = codec->spec;
2331         unsigned int portA, portD;
2332
2333         /* Port A */
2334         portA = snd_hda_jack_detect(codec, 0x19);
2335
2336         /* Port D */
2337         portD = snd_hda_jack_detect(codec, 0x1c);
2338
2339         spec->hp_present = portA ? HP_PRESENT_PORT_A : 0;
2340         spec->hp_present |= portD ? HP_PRESENT_PORT_D : 0;
2341         snd_printdd("CXT5066: hp automute portA=%x portD=%x present=%d\n",
2342                 portA, portD, spec->hp_present);
2343         cxt5066_update_speaker(codec);
2344 }
2345
2346 /* Dispatch the right mic autoswitch function */
2347 static void cxt5066_automic(struct hda_codec *codec)
2348 {
2349         struct conexant_spec *spec = codec->spec;
2350
2351         if (spec->dell_vostro)
2352                 cxt5066_vostro_automic(codec);
2353         else if (spec->ideapad)
2354                 cxt5066_ideapad_automic(codec);
2355         else if (spec->thinkpad)
2356                 cxt5066_thinkpad_automic(codec);
2357         else if (spec->hp_laptop)
2358                 cxt5066_hp_laptop_automic(codec);
2359         else if (spec->asus)
2360                 cxt5066_asus_automic(codec);
2361 }
2362
2363 /* unsolicited event for jack sensing */
2364 static void cxt5066_olpc_unsol_event(struct hda_codec *codec, unsigned int res)
2365 {
2366         struct conexant_spec *spec = codec->spec;
2367         snd_printdd("CXT5066: unsol event %x (%x)\n", res, res >> 26);
2368         switch (res >> 26) {
2369         case CONEXANT_HP_EVENT:
2370                 cxt5066_hp_automute(codec);
2371                 break;
2372         case CONEXANT_MIC_EVENT:
2373                 /* ignore mic events in DC mode; we're always using the jack */
2374                 if (!spec->dc_enable)
2375                         cxt5066_olpc_automic(codec);
2376                 break;
2377         }
2378 }
2379
2380 /* unsolicited event for jack sensing */
2381 static void cxt5066_unsol_event(struct hda_codec *codec, unsigned int res)
2382 {
2383         snd_printdd("CXT5066: unsol event %x (%x)\n", res, res >> 26);
2384         switch (res >> 26) {
2385         case CONEXANT_HP_EVENT:
2386                 cxt5066_hp_automute(codec);
2387                 break;
2388         case CONEXANT_MIC_EVENT:
2389                 cxt5066_automic(codec);
2390                 break;
2391         }
2392 }
2393
2394
2395 static const struct hda_input_mux cxt5066_analog_mic_boost = {
2396         .num_items = 5,
2397         .items = {
2398                 { "0dB",  0 },
2399                 { "10dB", 1 },
2400                 { "20dB", 2 },
2401                 { "30dB", 3 },
2402                 { "40dB", 4 },
2403         },
2404 };
2405
2406 static void cxt5066_set_mic_boost(struct hda_codec *codec)
2407 {
2408         struct conexant_spec *spec = codec->spec;
2409         snd_hda_codec_write_cache(codec, 0x17, 0,
2410                 AC_VERB_SET_AMP_GAIN_MUTE,
2411                 AC_AMP_SET_RIGHT | AC_AMP_SET_LEFT | AC_AMP_SET_OUTPUT |
2412                         cxt5066_analog_mic_boost.items[spec->mic_boost].index);
2413         if (spec->ideapad || spec->thinkpad) {
2414                 /* adjust the internal mic as well...it is not through 0x17 */
2415                 snd_hda_codec_write_cache(codec, 0x23, 0,
2416                         AC_VERB_SET_AMP_GAIN_MUTE,
2417                         AC_AMP_SET_RIGHT | AC_AMP_SET_LEFT | AC_AMP_SET_INPUT |
2418                                 cxt5066_analog_mic_boost.
2419                                         items[spec->mic_boost].index);
2420         }
2421 }
2422
2423 static int cxt5066_mic_boost_mux_enum_info(struct snd_kcontrol *kcontrol,
2424                                            struct snd_ctl_elem_info *uinfo)
2425 {
2426         return snd_hda_input_mux_info(&cxt5066_analog_mic_boost, uinfo);
2427 }
2428
2429 static int cxt5066_mic_boost_mux_enum_get(struct snd_kcontrol *kcontrol,
2430                                           struct snd_ctl_elem_value *ucontrol)
2431 {
2432         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2433         struct conexant_spec *spec = codec->spec;
2434         ucontrol->value.enumerated.item[0] = spec->mic_boost;
2435         return 0;
2436 }
2437
2438 static int cxt5066_mic_boost_mux_enum_put(struct snd_kcontrol *kcontrol,
2439                                           struct snd_ctl_elem_value *ucontrol)
2440 {
2441         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2442         struct conexant_spec *spec = codec->spec;
2443         const struct hda_input_mux *imux = &cxt5066_analog_mic_boost;
2444         unsigned int idx;
2445         idx = ucontrol->value.enumerated.item[0];
2446         if (idx >= imux->num_items)
2447                 idx = imux->num_items - 1;
2448
2449         spec->mic_boost = idx;
2450         if (!spec->dc_enable)
2451                 cxt5066_set_mic_boost(codec);
2452         return 1;
2453 }
2454
2455 static void cxt5066_enable_dc(struct hda_codec *codec)
2456 {
2457         const struct hda_verb enable_dc_mode[] = {
2458                 /* disable gain */
2459                 {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2460
2461                 /* switch to DC input */
2462                 {0x17, AC_VERB_SET_CONNECT_SEL, 3},
2463                 {}
2464         };
2465
2466         /* configure as input source */
2467         snd_hda_sequence_write(codec, enable_dc_mode);
2468         cxt5066_olpc_select_mic(codec); /* also sets configured bias */
2469 }
2470
2471 static void cxt5066_disable_dc(struct hda_codec *codec)
2472 {
2473         /* reconfigure input source */
2474         cxt5066_set_mic_boost(codec);
2475         /* automic also selects the right mic if we're recording */
2476         cxt5066_olpc_automic(codec);
2477 }
2478
2479 static int cxt5066_olpc_dc_get(struct snd_kcontrol *kcontrol,
2480                              struct snd_ctl_elem_value *ucontrol)
2481 {
2482         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2483         struct conexant_spec *spec = codec->spec;
2484         ucontrol->value.integer.value[0] = spec->dc_enable;
2485         return 0;
2486 }
2487
2488 static int cxt5066_olpc_dc_put(struct snd_kcontrol *kcontrol,
2489                              struct snd_ctl_elem_value *ucontrol)
2490 {
2491         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2492         struct conexant_spec *spec = codec->spec;
2493         int dc_enable = !!ucontrol->value.integer.value[0];
2494
2495         if (dc_enable == spec->dc_enable)
2496                 return 0;
2497
2498         spec->dc_enable = dc_enable;
2499         if (dc_enable)
2500                 cxt5066_enable_dc(codec);
2501         else
2502                 cxt5066_disable_dc(codec);
2503
2504         return 1;
2505 }
2506
2507 static int cxt5066_olpc_dc_bias_enum_info(struct snd_kcontrol *kcontrol,
2508                                            struct snd_ctl_elem_info *uinfo)
2509 {
2510         return snd_hda_input_mux_info(&cxt5066_olpc_dc_bias, uinfo);
2511 }
2512
2513 static int cxt5066_olpc_dc_bias_enum_get(struct snd_kcontrol *kcontrol,
2514                                           struct snd_ctl_elem_value *ucontrol)
2515 {
2516         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2517         struct conexant_spec *spec = codec->spec;
2518         ucontrol->value.enumerated.item[0] = spec->dc_input_bias;
2519         return 0;
2520 }
2521
2522 static int cxt5066_olpc_dc_bias_enum_put(struct snd_kcontrol *kcontrol,
2523                                           struct snd_ctl_elem_value *ucontrol)
2524 {
2525         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2526         struct conexant_spec *spec = codec->spec;
2527         const struct hda_input_mux *imux = &cxt5066_analog_mic_boost;
2528         unsigned int idx;
2529
2530         idx = ucontrol->value.enumerated.item[0];
2531         if (idx >= imux->num_items)
2532                 idx = imux->num_items - 1;
2533
2534         spec->dc_input_bias = idx;
2535         if (spec->dc_enable)
2536                 cxt5066_set_olpc_dc_bias(codec);
2537         return 1;
2538 }
2539
2540 static void cxt5066_olpc_capture_prepare(struct hda_codec *codec)
2541 {
2542         struct conexant_spec *spec = codec->spec;
2543         /* mark as recording and configure the microphone widget so that the
2544          * recording LED comes on. */
2545         spec->recording = 1;
2546         cxt5066_olpc_select_mic(codec);
2547 }
2548
2549 static void cxt5066_olpc_capture_cleanup(struct hda_codec *codec)
2550 {
2551         struct conexant_spec *spec = codec->spec;
2552         const struct hda_verb disable_mics[] = {
2553                 /* disable external mic, port B */
2554                 {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2555
2556                 /* disble internal mic, port C */
2557                 {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2558
2559                 /* disable DC capture, port F */
2560                 {0x1e, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2561                 {},
2562         };
2563
2564         snd_hda_sequence_write(codec, disable_mics);
2565         spec->recording = 0;
2566 }
2567
2568 static void conexant_check_dig_outs(struct hda_codec *codec,
2569                                     const hda_nid_t *dig_pins,
2570                                     int num_pins)
2571 {
2572         struct conexant_spec *spec = codec->spec;
2573         hda_nid_t *nid_loc = &spec->multiout.dig_out_nid;
2574         int i;
2575
2576         for (i = 0; i < num_pins; i++, dig_pins++) {
2577                 unsigned int cfg = snd_hda_codec_get_pincfg(codec, *dig_pins);
2578                 if (get_defcfg_connect(cfg) == AC_JACK_PORT_NONE)
2579                         continue;
2580                 if (snd_hda_get_connections(codec, *dig_pins, nid_loc, 1) != 1)
2581                         continue;
2582                 if (spec->slave_dig_outs[0])
2583                         nid_loc++;
2584                 else
2585                         nid_loc = spec->slave_dig_outs;
2586         }
2587 }
2588
2589 static const struct hda_input_mux cxt5066_capture_source = {
2590         .num_items = 4,
2591         .items = {
2592                 { "Mic B", 0 },
2593                 { "Mic C", 1 },
2594                 { "Mic E", 2 },
2595                 { "Mic F", 3 },
2596         },
2597 };
2598
2599 static const struct hda_bind_ctls cxt5066_bind_capture_vol_others = {
2600         .ops = &snd_hda_bind_vol,
2601         .values = {
2602                 HDA_COMPOSE_AMP_VAL(0x14, 3, 0, HDA_INPUT),
2603                 HDA_COMPOSE_AMP_VAL(0x14, 3, 2, HDA_INPUT),
2604                 0
2605         },
2606 };
2607
2608 static const struct hda_bind_ctls cxt5066_bind_capture_sw_others = {
2609         .ops = &snd_hda_bind_sw,
2610         .values = {
2611                 HDA_COMPOSE_AMP_VAL(0x14, 3, 0, HDA_INPUT),
2612                 HDA_COMPOSE_AMP_VAL(0x14, 3, 2, HDA_INPUT),
2613                 0
2614         },
2615 };
2616
2617 static const struct snd_kcontrol_new cxt5066_mixer_master[] = {
2618         HDA_CODEC_VOLUME("Master Playback Volume", 0x10, 0x00, HDA_OUTPUT),
2619         {}
2620 };
2621
2622 static const struct snd_kcontrol_new cxt5066_mixer_master_olpc[] = {
2623         {
2624                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2625                 .name = "Master Playback Volume",
2626                 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
2627                                   SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2628                                   SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK,
2629                 .subdevice = HDA_SUBDEV_AMP_FLAG,
2630                 .info = snd_hda_mixer_amp_volume_info,
2631                 .get = snd_hda_mixer_amp_volume_get,
2632                 .put = snd_hda_mixer_amp_volume_put,
2633                 .tlv = { .c = snd_hda_mixer_amp_tlv },
2634                 /* offset by 28 volume steps to limit minimum gain to -46dB */
2635                 .private_value =
2636                         HDA_COMPOSE_AMP_VAL_OFS(0x10, 3, 0, HDA_OUTPUT, 28),
2637         },
2638         {}
2639 };
2640
2641 static const struct snd_kcontrol_new cxt5066_mixer_olpc_dc[] = {
2642         {
2643                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2644                 .name = "DC Mode Enable Switch",
2645                 .info = snd_ctl_boolean_mono_info,
2646                 .get = cxt5066_olpc_dc_get,
2647                 .put = cxt5066_olpc_dc_put,
2648         },
2649         {
2650                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2651                 .name = "DC Input Bias Enum",
2652                 .info = cxt5066_olpc_dc_bias_enum_info,
2653                 .get = cxt5066_olpc_dc_bias_enum_get,
2654                 .put = cxt5066_olpc_dc_bias_enum_put,
2655         },
2656         {}
2657 };
2658
2659 static const struct snd_kcontrol_new cxt5066_mixers[] = {
2660         {
2661                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2662                 .name = "Master Playback Switch",
2663                 .info = cxt_eapd_info,
2664                 .get = cxt_eapd_get,
2665                 .put = cxt5066_hp_master_sw_put,
2666                 .private_value = 0x1d,
2667         },
2668
2669         {
2670                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2671                 .name = "Analog Mic Boost Capture Enum",
2672                 .info = cxt5066_mic_boost_mux_enum_info,
2673                 .get = cxt5066_mic_boost_mux_enum_get,
2674                 .put = cxt5066_mic_boost_mux_enum_put,
2675         },
2676
2677         HDA_BIND_VOL("Capture Volume", &cxt5066_bind_capture_vol_others),
2678         HDA_BIND_SW("Capture Switch", &cxt5066_bind_capture_sw_others),
2679         {}
2680 };
2681
2682 static const struct snd_kcontrol_new cxt5066_vostro_mixers[] = {
2683         {
2684                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2685                 .name = "Internal Mic Boost Capture Enum",
2686                 .info = cxt5066_mic_boost_mux_enum_info,
2687                 .get = cxt5066_mic_boost_mux_enum_get,
2688                 .put = cxt5066_mic_boost_mux_enum_put,
2689                 .private_value = 0x23 | 0x100,
2690         },
2691         {}
2692 };
2693
2694 static const struct hda_verb cxt5066_init_verbs[] = {
2695         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80}, /* Port B */
2696         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80}, /* Port C */
2697         {0x1e, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN}, /* Port F */
2698         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN}, /* Port E */
2699
2700         /* Speakers  */
2701         {0x1f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2702         {0x1f, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2703
2704         /* HP, Amp  */
2705         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
2706         {0x19, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2707
2708         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
2709         {0x1c, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2710
2711         /* DAC1 */
2712         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2713
2714         /* Node 14 connections: 0x17 0x18 0x23 0x24 0x27 */
2715         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x50},
2716         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2717         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2) | 0x50},
2718         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2719         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
2720
2721         /* no digital microphone support yet */
2722         {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2723
2724         /* Audio input selector */
2725         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x3},
2726
2727         /* SPDIF route: PCM */
2728         {0x20, AC_VERB_SET_CONNECT_SEL, 0x0},
2729         {0x22, AC_VERB_SET_CONNECT_SEL, 0x0},
2730
2731         {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2732         {0x22, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2733
2734         /* EAPD */
2735         {0x1d, AC_VERB_SET_EAPD_BTLENABLE, 0x2}, /* default on */
2736
2737         /* not handling these yet */
2738         {0x19, AC_VERB_SET_UNSOLICITED_ENABLE, 0},
2739         {0x1a, AC_VERB_SET_UNSOLICITED_ENABLE, 0},
2740         {0x1b, AC_VERB_SET_UNSOLICITED_ENABLE, 0},
2741         {0x1c, AC_VERB_SET_UNSOLICITED_ENABLE, 0},
2742         {0x1d, AC_VERB_SET_UNSOLICITED_ENABLE, 0},
2743         {0x1e, AC_VERB_SET_UNSOLICITED_ENABLE, 0},
2744         {0x20, AC_VERB_SET_UNSOLICITED_ENABLE, 0},
2745         {0x22, AC_VERB_SET_UNSOLICITED_ENABLE, 0},
2746         { } /* end */
2747 };
2748
2749 static const struct hda_verb cxt5066_init_verbs_olpc[] = {
2750         /* Port A: headphones */
2751         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
2752         {0x19, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2753
2754         /* Port B: external microphone */
2755         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2756
2757         /* Port C: internal microphone */
2758         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2759
2760         /* Port D: unused */
2761         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2762
2763         /* Port E: unused, but has primary EAPD */
2764         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2765         {0x1d, AC_VERB_SET_EAPD_BTLENABLE, 0x2}, /* default on */
2766
2767         /* Port F: external DC input through microphone port */
2768         {0x1e, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2769
2770         /* Port G: internal speakers */
2771         {0x1f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2772         {0x1f, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2773
2774         /* DAC1 */
2775         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2776
2777         /* DAC2: unused */
2778         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2779
2780         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x50},
2781         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2782         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
2783         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2784         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2785         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2786         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
2787         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2788         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2789         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2790         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
2791         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2792
2793         /* Disable digital microphone port */
2794         {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2795
2796         /* Audio input selectors */
2797         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x3},
2798         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE },
2799
2800         /* Disable SPDIF */
2801         {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2802         {0x22, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2803
2804         /* enable unsolicited events for Port A and B */
2805         {0x19, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_HP_EVENT},
2806         {0x1a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_MIC_EVENT},
2807         { } /* end */
2808 };
2809
2810 static const struct hda_verb cxt5066_init_verbs_vostro[] = {
2811         /* Port A: headphones */
2812         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2813         {0x19, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2814
2815         /* Port B: external microphone */
2816         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2817
2818         /* Port C: unused */
2819         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2820
2821         /* Port D: unused */
2822         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2823
2824         /* Port E: unused, but has primary EAPD */
2825         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2826         {0x1d, AC_VERB_SET_EAPD_BTLENABLE, 0x2}, /* default on */
2827
2828         /* Port F: unused */
2829         {0x1e, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2830
2831         /* Port G: internal speakers */
2832         {0x1f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2833         {0x1f, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2834
2835         /* DAC1 */
2836         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2837
2838         /* DAC2: unused */
2839         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2840
2841         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2842         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2843         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
2844         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2845         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2846         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2847         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
2848         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2849         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2850         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2851         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
2852         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2853
2854         /* Digital microphone port */
2855         {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
2856
2857         /* Audio input selectors */
2858         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x3},
2859         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE },
2860
2861         /* Disable SPDIF */
2862         {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2863         {0x22, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2864
2865         /* enable unsolicited events for Port A and B */
2866         {0x19, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_HP_EVENT},
2867         {0x1a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_MIC_EVENT},
2868         { } /* end */
2869 };
2870
2871 static const struct hda_verb cxt5066_init_verbs_ideapad[] = {
2872         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80}, /* Port B */
2873         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80}, /* Port C */
2874         {0x1e, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN}, /* Port F */
2875         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN}, /* Port E */
2876
2877         /* Speakers  */
2878         {0x1f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2879         {0x1f, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2880
2881         /* HP, Amp  */
2882         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
2883         {0x19, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2884
2885         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
2886         {0x1c, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2887
2888         /* DAC1 */
2889         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2890
2891         /* Node 14 connections: 0x17 0x18 0x23 0x24 0x27 */
2892         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x50},
2893         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2894         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2) | 0x50},
2895         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2896         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
2897         {0x14, AC_VERB_SET_CONNECT_SEL, 2},     /* default to internal mic */
2898
2899         /* Audio input selector */
2900         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x2},
2901         {0x17, AC_VERB_SET_CONNECT_SEL, 1},     /* route ext mic */
2902
2903         /* SPDIF route: PCM */
2904         {0x20, AC_VERB_SET_CONNECT_SEL, 0x0},
2905         {0x22, AC_VERB_SET_CONNECT_SEL, 0x0},
2906
2907         {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2908         {0x22, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2909
2910         /* internal microphone */
2911         {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN}, /* enable internal mic */
2912
2913         /* EAPD */
2914         {0x1d, AC_VERB_SET_EAPD_BTLENABLE, 0x2}, /* default on */
2915
2916         {0x19, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_HP_EVENT},
2917         {0x1b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_MIC_EVENT},
2918         { } /* end */
2919 };
2920
2921 static const struct hda_verb cxt5066_init_verbs_thinkpad[] = {
2922         {0x1e, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN}, /* Port F */
2923         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN}, /* Port E */
2924
2925         /* Port G: internal speakers  */
2926         {0x1f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2927         {0x1f, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2928
2929         /* Port A: HP, Amp  */
2930         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2931         {0x19, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2932
2933         /* Port B: Mic Dock */
2934         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2935
2936         /* Port C: Mic */
2937         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2938
2939         /* Port D: HP Dock, Amp */
2940         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2941         {0x1c, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2942
2943         /* DAC1 */
2944         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2945
2946         /* Node 14 connections: 0x17 0x18 0x23 0x24 0x27 */
2947         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x50},
2948         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2949         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2) | 0x50},
2950         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2951         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
2952         {0x14, AC_VERB_SET_CONNECT_SEL, 2},     /* default to internal mic */
2953
2954         /* Audio input selector */
2955         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x2},
2956         {0x17, AC_VERB_SET_CONNECT_SEL, 1},     /* route ext mic */
2957
2958         /* SPDIF route: PCM */
2959         {0x20, AC_VERB_SET_CONNECT_SEL, 0x0},
2960         {0x22, AC_VERB_SET_CONNECT_SEL, 0x0},
2961
2962         {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2963         {0x22, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2964
2965         /* internal microphone */
2966         {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN}, /* enable internal mic */
2967
2968         /* EAPD */
2969         {0x1d, AC_VERB_SET_EAPD_BTLENABLE, 0x2}, /* default on */
2970
2971         /* enable unsolicited events for Port A, B, C and D */
2972         {0x19, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_HP_EVENT},
2973         {0x1c, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_HP_EVENT},
2974         {0x1a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_MIC_EVENT},
2975         {0x1b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_MIC_EVENT},
2976         { } /* end */
2977 };
2978
2979 static const struct hda_verb cxt5066_init_verbs_portd_lo[] = {
2980         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2981         { } /* end */
2982 };
2983
2984
2985 static const struct hda_verb cxt5066_init_verbs_hp_laptop[] = {
2986         {0x14, AC_VERB_SET_CONNECT_SEL, 0x0},
2987         {0x19, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_HP_EVENT},
2988         {0x1b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_MIC_EVENT},
2989         { } /* end */
2990 };
2991
2992 /* initialize jack-sensing, too */
2993 static int cxt5066_init(struct hda_codec *codec)
2994 {
2995         snd_printdd("CXT5066: init\n");
2996         conexant_init(codec);
2997         if (codec->patch_ops.unsol_event) {
2998                 cxt5066_hp_automute(codec);
2999                 cxt5066_automic(codec);
3000         }
3001         cxt5066_set_mic_boost(codec);
3002         return 0;
3003 }
3004
3005 static int cxt5066_olpc_init(struct hda_codec *codec)
3006 {
3007         struct conexant_spec *spec = codec->spec;
3008         snd_printdd("CXT5066: init\n");
3009         conexant_init(codec);
3010         cxt5066_hp_automute(codec);
3011         if (!spec->dc_enable) {
3012                 cxt5066_set_mic_boost(codec);
3013                 cxt5066_olpc_automic(codec);
3014         } else {
3015                 cxt5066_enable_dc(codec);
3016         }
3017         return 0;
3018 }
3019
3020 enum {
3021         CXT5066_LAPTOP,         /* Laptops w/ EAPD support */
3022         CXT5066_DELL_LAPTOP,    /* Dell Laptop */
3023         CXT5066_OLPC_XO_1_5,    /* OLPC XO 1.5 */
3024         CXT5066_DELL_VOSTRO,    /* Dell Vostro 1015i */
3025         CXT5066_IDEAPAD,        /* Lenovo IdeaPad U150 */
3026         CXT5066_THINKPAD,       /* Lenovo ThinkPad T410s, others? */
3027         CXT5066_ASUS,           /* Asus K52JU, Lenovo G560 - Int mic at 0x1a and Ext mic at 0x1b */
3028         CXT5066_HP_LAPTOP,      /* HP Laptop */
3029         CXT5066_AUTO,           /* BIOS auto-parser */
3030         CXT5066_MODELS
3031 };
3032
3033 static const char * const cxt5066_models[CXT5066_MODELS] = {
3034         [CXT5066_LAPTOP]        = "laptop",
3035         [CXT5066_DELL_LAPTOP]   = "dell-laptop",
3036         [CXT5066_OLPC_XO_1_5]   = "olpc-xo-1_5",
3037         [CXT5066_DELL_VOSTRO]   = "dell-vostro",
3038         [CXT5066_IDEAPAD]       = "ideapad",
3039         [CXT5066_THINKPAD]      = "thinkpad",
3040         [CXT5066_ASUS]          = "asus",
3041         [CXT5066_HP_LAPTOP]     = "hp-laptop",
3042         [CXT5066_AUTO]          = "auto",
3043 };
3044
3045 static const struct snd_pci_quirk cxt5066_cfg_tbl[] = {
3046         SND_PCI_QUIRK(0x1025, 0x054c, "Acer Aspire 3830TG", CXT5066_AUTO),
3047         SND_PCI_QUIRK_MASK(0x1025, 0xff00, 0x0400, "Acer", CXT5066_IDEAPAD),
3048         SND_PCI_QUIRK(0x1028, 0x02d8, "Dell Vostro", CXT5066_DELL_VOSTRO),
3049         SND_PCI_QUIRK(0x1028, 0x02f5, "Dell Vostro 320", CXT5066_IDEAPAD),
3050         SND_PCI_QUIRK(0x1028, 0x0401, "Dell Vostro 1014", CXT5066_DELL_VOSTRO),
3051         SND_PCI_QUIRK(0x1028, 0x0408, "Dell Inspiron One 19T", CXT5066_IDEAPAD),
3052         SND_PCI_QUIRK(0x1028, 0x050f, "Dell Inspiron", CXT5066_IDEAPAD),
3053         SND_PCI_QUIRK(0x1028, 0x0510, "Dell Vostro", CXT5066_IDEAPAD),
3054         SND_PCI_QUIRK(0x103c, 0x360b, "HP G60", CXT5066_HP_LAPTOP),
3055         SND_PCI_QUIRK(0x1043, 0x13f3, "Asus A52J", CXT5066_ASUS),
3056         SND_PCI_QUIRK(0x1043, 0x1643, "Asus K52JU", CXT5066_ASUS),
3057         SND_PCI_QUIRK(0x1043, 0x1993, "Asus U50F", CXT5066_ASUS),
3058         SND_PCI_QUIRK(0x1179, 0xff1e, "Toshiba Satellite C650D", CXT5066_IDEAPAD),
3059         SND_PCI_QUIRK(0x1179, 0xff50, "Toshiba Satellite P500-PSPGSC-01800T", CXT5066_OLPC_XO_1_5),
3060         SND_PCI_QUIRK(0x14f1, 0x0101, "Conexant Reference board",
3061                       CXT5066_LAPTOP),
3062         SND_PCI_QUIRK(0x152d, 0x0833, "OLPC XO-1.5", CXT5066_OLPC_XO_1_5),
3063         SND_PCI_QUIRK(0x17aa, 0x20f2, "Lenovo T400s", CXT5066_THINKPAD),
3064         SND_PCI_QUIRK(0x17aa, 0x21c5, "Thinkpad Edge 13", CXT5066_THINKPAD),
3065         SND_PCI_QUIRK(0x17aa, 0x21c6, "Thinkpad Edge 13", CXT5066_ASUS),
3066         SND_PCI_QUIRK(0x17aa, 0x215e, "Lenovo Thinkpad", CXT5066_THINKPAD),
3067         SND_PCI_QUIRK(0x17aa, 0x21cf, "Lenovo T520 & W520", CXT5066_AUTO),
3068         SND_PCI_QUIRK(0x17aa, 0x21da, "Lenovo X220", CXT5066_THINKPAD),
3069         SND_PCI_QUIRK(0x17aa, 0x21db, "Lenovo X220-tablet", CXT5066_THINKPAD),
3070         SND_PCI_QUIRK(0x17aa, 0x3a0d, "Lenovo U350", CXT5066_ASUS),
3071         SND_PCI_QUIRK(0x17aa, 0x38af, "Lenovo G560", CXT5066_ASUS),
3072         SND_PCI_QUIRK(0x17aa, 0x3938, "Lenovo G565", CXT5066_AUTO),
3073         SND_PCI_QUIRK_VENDOR(0x17aa, "Lenovo", CXT5066_IDEAPAD), /* Fallback for Lenovos without dock mic */
3074         SND_PCI_QUIRK(0x1b0a, 0x2092, "CyberpowerPC Gamer Xplorer N57001", CXT5066_AUTO),
3075         {}
3076 };
3077
3078 static int patch_cxt5066(struct hda_codec *codec)
3079 {
3080         struct conexant_spec *spec;
3081         int board_config;
3082
3083         board_config = snd_hda_check_board_config(codec, CXT5066_MODELS,
3084                                                   cxt5066_models, cxt5066_cfg_tbl);
3085         if (board_config < 0)
3086                 board_config = CXT5066_AUTO; /* model=auto as default */
3087         if (board_config == CXT5066_AUTO)
3088                 return patch_conexant_auto(codec);
3089
3090         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
3091         if (!spec)
3092                 return -ENOMEM;
3093         codec->spec = spec;
3094
3095         codec->patch_ops = conexant_patch_ops;
3096         codec->patch_ops.init = conexant_init;
3097
3098         spec->dell_automute = 0;
3099         spec->multiout.max_channels = 2;
3100         spec->multiout.num_dacs = ARRAY_SIZE(cxt5066_dac_nids);
3101         spec->multiout.dac_nids = cxt5066_dac_nids;
3102         conexant_check_dig_outs(codec, cxt5066_digout_pin_nids,
3103             ARRAY_SIZE(cxt5066_digout_pin_nids));
3104         spec->num_adc_nids = 1;
3105         spec->adc_nids = cxt5066_adc_nids;
3106         spec->capsrc_nids = cxt5066_capsrc_nids;
3107         spec->input_mux = &cxt5066_capture_source;
3108
3109         spec->port_d_mode = PIN_HP;
3110