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