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