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