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