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