114af8e19d80b515970f7bbc28c3673c70baaba9
[safe/jmp/linux-2.6] / 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 conexant_spec {
61
62         struct snd_kcontrol_new *mixers[5];
63         int num_mixers;
64         hda_nid_t vmaster_nid;
65
66         const struct hda_verb *init_verbs[5];   /* initialization verbs
67                                                  * don't forget NULL
68                                                  * termination!
69                                                  */
70         unsigned int num_init_verbs;
71
72         /* playback */
73         struct hda_multi_out multiout;  /* playback set-up
74                                          * max_channels, dacs must be set
75                                          * dig_out_nid and hp_nid are optional
76                                          */
77         unsigned int cur_eapd;
78         unsigned int hp_present;
79         unsigned int auto_mic;
80         unsigned int need_dac_fix;
81
82         /* capture */
83         unsigned int num_adc_nids;
84         hda_nid_t *adc_nids;
85         hda_nid_t dig_in_nid;           /* digital-in NID; optional */
86
87         unsigned int cur_adc_idx;
88         hda_nid_t cur_adc;
89         unsigned int cur_adc_stream_tag;
90         unsigned int cur_adc_format;
91
92         /* capture source */
93         const struct hda_input_mux *input_mux;
94         hda_nid_t *capsrc_nids;
95         unsigned int cur_mux[3];
96
97         /* channel model */
98         const struct hda_channel_mode *channel_mode;
99         int num_channel_mode;
100
101         /* PCM information */
102         struct hda_pcm pcm_rec[2];      /* used in build_pcms() */
103
104         unsigned int spdif_route;
105
106         /* jack detection */
107         struct snd_array jacks;
108
109         /* dynamic controls, init_verbs and input_mux */
110         struct auto_pin_cfg autocfg;
111         struct hda_input_mux private_imux;
112         hda_nid_t private_dac_nids[AUTO_CFG_MAX_OUTS];
113
114         unsigned int dell_automute;
115         unsigned int port_d_mode;
116         unsigned int dell_vostro:1;
117         unsigned int ideapad:1;
118         unsigned int thinkpad:1;
119
120         unsigned int ext_mic_present;
121         unsigned int recording;
122         void (*capture_prepare)(struct hda_codec *codec);
123         void (*capture_cleanup)(struct hda_codec *codec);
124
125         /* OLPC XO-1.5 supports DC input mode (e.g. for use with analog sensors)
126          * through the microphone jack.
127          * When the user enables this through a mixer switch, both internal and
128          * external microphones are disabled. Gain is fixed at 0dB. In this mode,
129          * we also allow the bias to be configured through a separate mixer
130          * control. */
131         unsigned int dc_enable;
132         unsigned int dc_input_bias; /* offset into cxt5066_olpc_dc_bias */
133         unsigned int mic_boost; /* offset into cxt5066_analog_mic_boost */
134 };
135
136 static int conexant_playback_pcm_open(struct hda_pcm_stream *hinfo,
137                                       struct hda_codec *codec,
138                                       struct snd_pcm_substream *substream)
139 {
140         struct conexant_spec *spec = codec->spec;
141         return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
142                                              hinfo);
143 }
144
145 static int conexant_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
146                                          struct hda_codec *codec,
147                                          unsigned int stream_tag,
148                                          unsigned int format,
149                                          struct snd_pcm_substream *substream)
150 {
151         struct conexant_spec *spec = codec->spec;
152         return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
153                                                 stream_tag,
154                                                 format, substream);
155 }
156
157 static int conexant_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
158                                          struct hda_codec *codec,
159                                          struct snd_pcm_substream *substream)
160 {
161         struct conexant_spec *spec = codec->spec;
162         return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
163 }
164
165 /*
166  * Digital out
167  */
168 static int conexant_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
169                                           struct hda_codec *codec,
170                                           struct snd_pcm_substream *substream)
171 {
172         struct conexant_spec *spec = codec->spec;
173         return snd_hda_multi_out_dig_open(codec, &spec->multiout);
174 }
175
176 static int conexant_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
177                                          struct hda_codec *codec,
178                                          struct snd_pcm_substream *substream)
179 {
180         struct conexant_spec *spec = codec->spec;
181         return snd_hda_multi_out_dig_close(codec, &spec->multiout);
182 }
183
184 static int conexant_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
185                                          struct hda_codec *codec,
186                                          unsigned int stream_tag,
187                                          unsigned int format,
188                                          struct snd_pcm_substream *substream)
189 {
190         struct conexant_spec *spec = codec->spec;
191         return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
192                                              stream_tag,
193                                              format, substream);
194 }
195
196 /*
197  * Analog capture
198  */
199 static int conexant_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
200                                       struct hda_codec *codec,
201                                       unsigned int stream_tag,
202                                       unsigned int format,
203                                       struct snd_pcm_substream *substream)
204 {
205         struct conexant_spec *spec = codec->spec;
206         if (spec->capture_prepare)
207                 spec->capture_prepare(codec);
208         snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
209                                    stream_tag, 0, format);
210         return 0;
211 }
212
213 static int conexant_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
214                                       struct hda_codec *codec,
215                                       struct snd_pcm_substream *substream)
216 {
217         struct conexant_spec *spec = codec->spec;
218         snd_hda_codec_cleanup_stream(codec, spec->adc_nids[substream->number]);
219         if (spec->capture_cleanup)
220                 spec->capture_cleanup(codec);
221         return 0;
222 }
223
224
225
226 static struct hda_pcm_stream conexant_pcm_analog_playback = {
227         .substreams = 1,
228         .channels_min = 2,
229         .channels_max = 2,
230         .nid = 0, /* fill later */
231         .ops = {
232                 .open = conexant_playback_pcm_open,
233                 .prepare = conexant_playback_pcm_prepare,
234                 .cleanup = conexant_playback_pcm_cleanup
235         },
236 };
237
238 static struct hda_pcm_stream conexant_pcm_analog_capture = {
239         .substreams = 1,
240         .channels_min = 2,
241         .channels_max = 2,
242         .nid = 0, /* fill later */
243         .ops = {
244                 .prepare = conexant_capture_pcm_prepare,
245                 .cleanup = conexant_capture_pcm_cleanup
246         },
247 };
248
249
250 static struct hda_pcm_stream conexant_pcm_digital_playback = {
251         .substreams = 1,
252         .channels_min = 2,
253         .channels_max = 2,
254         .nid = 0, /* fill later */
255         .ops = {
256                 .open = conexant_dig_playback_pcm_open,
257                 .close = conexant_dig_playback_pcm_close,
258                 .prepare = conexant_dig_playback_pcm_prepare
259         },
260 };
261
262 static struct hda_pcm_stream conexant_pcm_digital_capture = {
263         .substreams = 1,
264         .channels_min = 2,
265         .channels_max = 2,
266         /* NID is set in alc_build_pcms */
267 };
268
269 static int cx5051_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
270                                       struct hda_codec *codec,
271                                       unsigned int stream_tag,
272                                       unsigned int format,
273                                       struct snd_pcm_substream *substream)
274 {
275         struct conexant_spec *spec = codec->spec;
276         spec->cur_adc = spec->adc_nids[spec->cur_adc_idx];
277         spec->cur_adc_stream_tag = stream_tag;
278         spec->cur_adc_format = format;
279         snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
280         return 0;
281 }
282
283 static int cx5051_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
284                                       struct hda_codec *codec,
285                                       struct snd_pcm_substream *substream)
286 {
287         struct conexant_spec *spec = codec->spec;
288         snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
289         spec->cur_adc = 0;
290         return 0;
291 }
292
293 static struct hda_pcm_stream cx5051_pcm_analog_capture = {
294         .substreams = 1,
295         .channels_min = 2,
296         .channels_max = 2,
297         .nid = 0, /* fill later */
298         .ops = {
299                 .prepare = cx5051_capture_pcm_prepare,
300                 .cleanup = cx5051_capture_pcm_cleanup
301         },
302 };
303
304 static int conexant_build_pcms(struct hda_codec *codec)
305 {
306         struct conexant_spec *spec = codec->spec;
307         struct hda_pcm *info = spec->pcm_rec;
308
309         codec->num_pcms = 1;
310         codec->pcm_info = info;
311
312         info->name = "CONEXANT Analog";
313         info->stream[SNDRV_PCM_STREAM_PLAYBACK] = conexant_pcm_analog_playback;
314         info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
315                 spec->multiout.max_channels;
316         info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
317                 spec->multiout.dac_nids[0];
318         if (codec->vendor_id == 0x14f15051)
319                 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
320                         cx5051_pcm_analog_capture;
321         else
322                 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
323                         conexant_pcm_analog_capture;
324         info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = spec->num_adc_nids;
325         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
326
327         if (spec->multiout.dig_out_nid) {
328                 info++;
329                 codec->num_pcms++;
330                 info->name = "Conexant Digital";
331                 info->pcm_type = HDA_PCM_TYPE_SPDIF;
332                 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
333                         conexant_pcm_digital_playback;
334                 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
335                         spec->multiout.dig_out_nid;
336                 if (spec->dig_in_nid) {
337                         info->stream[SNDRV_PCM_STREAM_CAPTURE] =
338                                 conexant_pcm_digital_capture;
339                         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
340                                 spec->dig_in_nid;
341                 }
342         }
343
344         return 0;
345 }
346
347 static int conexant_mux_enum_info(struct snd_kcontrol *kcontrol,
348                                   struct snd_ctl_elem_info *uinfo)
349 {
350         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
351         struct conexant_spec *spec = codec->spec;
352
353         return snd_hda_input_mux_info(spec->input_mux, uinfo);
354 }
355
356 static int conexant_mux_enum_get(struct snd_kcontrol *kcontrol,
357                                  struct snd_ctl_elem_value *ucontrol)
358 {
359         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
360         struct conexant_spec *spec = codec->spec;
361         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
362
363         ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
364         return 0;
365 }
366
367 static int conexant_mux_enum_put(struct snd_kcontrol *kcontrol,
368                                  struct snd_ctl_elem_value *ucontrol)
369 {
370         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
371         struct conexant_spec *spec = codec->spec;
372         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
373
374         return snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
375                                      spec->capsrc_nids[adc_idx],
376                                      &spec->cur_mux[adc_idx]);
377 }
378
379 #ifdef CONFIG_SND_HDA_INPUT_JACK
380 static void conexant_free_jack_priv(struct snd_jack *jack)
381 {
382         struct conexant_jack *jacks = jack->private_data;
383         jacks->nid = 0;
384         jacks->jack = NULL;
385 }
386
387 static int conexant_add_jack(struct hda_codec *codec,
388                 hda_nid_t nid, int type)
389 {
390         struct conexant_spec *spec;
391         struct conexant_jack *jack;
392         const char *name;
393         int err;
394
395         spec = codec->spec;
396         snd_array_init(&spec->jacks, sizeof(*jack), 32);
397         jack = snd_array_new(&spec->jacks);
398         name = (type == SND_JACK_HEADPHONE) ? "Headphone" : "Mic" ;
399
400         if (!jack)
401                 return -ENOMEM;
402
403         jack->nid = nid;
404         jack->type = type;
405
406         err = snd_jack_new(codec->bus->card, name, type, &jack->jack);
407         if (err < 0)
408                 return err;
409         jack->jack->private_data = jack;
410         jack->jack->private_free = conexant_free_jack_priv;
411         return 0;
412 }
413
414 static void conexant_report_jack(struct hda_codec *codec, hda_nid_t nid)
415 {
416         struct conexant_spec *spec = codec->spec;
417         struct conexant_jack *jacks = spec->jacks.list;
418
419         if (jacks) {
420                 int i;
421                 for (i = 0; i < spec->jacks.used; i++) {
422                         if (jacks->nid == nid) {
423                                 unsigned int present;
424                                 present = snd_hda_jack_detect(codec, nid);
425
426                                 present = (present) ? jacks->type : 0 ;
427
428                                 snd_jack_report(jacks->jack,
429                                                 present);
430                         }
431                         jacks++;
432                 }
433         }
434 }
435
436 static int conexant_init_jacks(struct hda_codec *codec)
437 {
438         struct conexant_spec *spec = codec->spec;
439         int i;
440
441         for (i = 0; i < spec->num_init_verbs; i++) {
442                 const struct hda_verb *hv;
443
444                 hv = spec->init_verbs[i];
445                 while (hv->nid) {
446                         int err = 0;
447                         switch (hv->param ^ AC_USRSP_EN) {
448                         case CONEXANT_HP_EVENT:
449                                 err = conexant_add_jack(codec, hv->nid,
450                                                 SND_JACK_HEADPHONE);
451                                 conexant_report_jack(codec, hv->nid);
452                                 break;
453                         case CXT5051_PORTC_EVENT:
454                         case CONEXANT_MIC_EVENT:
455                                 err = conexant_add_jack(codec, hv->nid,
456                                                 SND_JACK_MICROPHONE);
457                                 conexant_report_jack(codec, hv->nid);
458                                 break;
459                         }
460                         if (err < 0)
461                                 return err;
462                         ++hv;
463                 }
464         }
465         return 0;
466
467 }
468 #else
469 static inline void conexant_report_jack(struct hda_codec *codec, hda_nid_t nid)
470 {
471 }
472
473 static inline int conexant_init_jacks(struct hda_codec *codec)
474 {
475         return 0;
476 }
477 #endif
478
479 static int conexant_init(struct hda_codec *codec)
480 {
481         struct conexant_spec *spec = codec->spec;
482         int i;
483
484         for (i = 0; i < spec->num_init_verbs; i++)
485                 snd_hda_sequence_write(codec, spec->init_verbs[i]);
486         return 0;
487 }
488
489 static void conexant_free(struct hda_codec *codec)
490 {
491 #ifdef CONFIG_SND_HDA_INPUT_JACK
492         struct conexant_spec *spec = codec->spec;
493         if (spec->jacks.list) {
494                 struct conexant_jack *jacks = spec->jacks.list;
495                 int i;
496                 for (i = 0; i < spec->jacks.used; i++, jacks++) {
497                         if (jacks->jack)
498                                 snd_device_free(codec->bus->card, jacks->jack);
499                 }
500                 snd_array_free(&spec->jacks);
501         }
502 #endif
503         snd_hda_detach_beep_device(codec);
504         kfree(codec->spec);
505 }
506
507 static struct snd_kcontrol_new cxt_capture_mixers[] = {
508         {
509                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
510                 .name = "Capture Source",
511                 .info = conexant_mux_enum_info,
512                 .get = conexant_mux_enum_get,
513                 .put = conexant_mux_enum_put
514         },
515         {}
516 };
517
518 static const char *slave_vols[] = {
519         "Headphone Playback Volume",
520         "Speaker Playback Volume",
521         NULL
522 };
523
524 static const char *slave_sws[] = {
525         "Headphone Playback Switch",
526         "Speaker Playback Switch",
527         NULL
528 };
529
530 static int conexant_build_controls(struct hda_codec *codec)
531 {
532         struct conexant_spec *spec = codec->spec;
533         unsigned int i;
534         int err;
535
536         for (i = 0; i < spec->num_mixers; i++) {
537                 err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
538                 if (err < 0)
539                         return err;
540         }
541         if (spec->multiout.dig_out_nid) {
542                 err = snd_hda_create_spdif_out_ctls(codec,
543                                                     spec->multiout.dig_out_nid);
544                 if (err < 0)
545                         return err;
546                 err = snd_hda_create_spdif_share_sw(codec,
547                                                     &spec->multiout);
548                 if (err < 0)
549                         return err;
550                 spec->multiout.share_spdif = 1;
551         } 
552         if (spec->dig_in_nid) {
553                 err = snd_hda_create_spdif_in_ctls(codec,spec->dig_in_nid);
554                 if (err < 0)
555                         return err;
556         }
557
558         /* if we have no master control, let's create it */
559         if (spec->vmaster_nid &&
560             !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
561                 unsigned int vmaster_tlv[4];
562                 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
563                                         HDA_OUTPUT, vmaster_tlv);
564                 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
565                                           vmaster_tlv, slave_vols);
566                 if (err < 0)
567                         return err;
568         }
569         if (spec->vmaster_nid &&
570             !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
571                 err = snd_hda_add_vmaster(codec, "Master Playback Switch",
572                                           NULL, slave_sws);
573                 if (err < 0)
574                         return err;
575         }
576
577         if (spec->input_mux) {
578                 err = snd_hda_add_new_ctls(codec, cxt_capture_mixers);
579                 if (err < 0)
580                         return err;
581         }
582
583         return 0;
584 }
585
586 static struct hda_codec_ops conexant_patch_ops = {
587         .build_controls = conexant_build_controls,
588         .build_pcms = conexant_build_pcms,
589         .init = conexant_init,
590         .free = conexant_free,
591 };
592
593 /*
594  * EAPD control
595  * the private value = nid | (invert << 8)
596  */
597
598 #define cxt_eapd_info           snd_ctl_boolean_mono_info
599
600 static int cxt_eapd_get(struct snd_kcontrol *kcontrol,
601                              struct snd_ctl_elem_value *ucontrol)
602 {
603         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
604         struct conexant_spec *spec = codec->spec;
605         int invert = (kcontrol->private_value >> 8) & 1;
606         if (invert)
607                 ucontrol->value.integer.value[0] = !spec->cur_eapd;
608         else
609                 ucontrol->value.integer.value[0] = spec->cur_eapd;
610         return 0;
611
612 }
613
614 static int cxt_eapd_put(struct snd_kcontrol *kcontrol,
615                              struct snd_ctl_elem_value *ucontrol)
616 {
617         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
618         struct conexant_spec *spec = codec->spec;
619         int invert = (kcontrol->private_value >> 8) & 1;
620         hda_nid_t nid = kcontrol->private_value & 0xff;
621         unsigned int eapd;
622
623         eapd = !!ucontrol->value.integer.value[0];
624         if (invert)
625                 eapd = !eapd;
626         if (eapd == spec->cur_eapd)
627                 return 0;
628         
629         spec->cur_eapd = eapd;
630         snd_hda_codec_write_cache(codec, nid,
631                                   0, AC_VERB_SET_EAPD_BTLENABLE,
632                                   eapd ? 0x02 : 0x00);
633         return 1;
634 }
635
636 /* controls for test mode */
637 #ifdef CONFIG_SND_DEBUG
638
639 #define CXT_EAPD_SWITCH(xname, nid, mask) \
640         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = 0,  \
641           .info = cxt_eapd_info, \
642           .get = cxt_eapd_get, \
643           .put = cxt_eapd_put, \
644           .private_value = nid | (mask<<16) }
645
646
647
648 static int conexant_ch_mode_info(struct snd_kcontrol *kcontrol,
649                                  struct snd_ctl_elem_info *uinfo)
650 {
651         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
652         struct conexant_spec *spec = codec->spec;
653         return snd_hda_ch_mode_info(codec, uinfo, spec->channel_mode,
654                                     spec->num_channel_mode);
655 }
656
657 static int conexant_ch_mode_get(struct snd_kcontrol *kcontrol,
658                                 struct snd_ctl_elem_value *ucontrol)
659 {
660         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
661         struct conexant_spec *spec = codec->spec;
662         return snd_hda_ch_mode_get(codec, ucontrol, spec->channel_mode,
663                                    spec->num_channel_mode,
664                                    spec->multiout.max_channels);
665 }
666
667 static int conexant_ch_mode_put(struct snd_kcontrol *kcontrol,
668                                 struct snd_ctl_elem_value *ucontrol)
669 {
670         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
671         struct conexant_spec *spec = codec->spec;
672         int err = snd_hda_ch_mode_put(codec, ucontrol, spec->channel_mode,
673                                       spec->num_channel_mode,
674                                       &spec->multiout.max_channels);
675         if (err >= 0 && spec->need_dac_fix)
676                 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
677         return err;
678 }
679
680 #define CXT_PIN_MODE(xname, nid, dir) \
681         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = 0,  \
682           .info = conexant_ch_mode_info, \
683           .get = conexant_ch_mode_get, \
684           .put = conexant_ch_mode_put, \
685           .private_value = nid | (dir<<16) }
686
687 #endif /* CONFIG_SND_DEBUG */
688
689 /* Conexant 5045 specific */
690
691 static hda_nid_t cxt5045_dac_nids[1] = { 0x19 };
692 static hda_nid_t cxt5045_adc_nids[1] = { 0x1a };
693 static hda_nid_t cxt5045_capsrc_nids[1] = { 0x1a };
694 #define CXT5045_SPDIF_OUT       0x18
695
696 static struct hda_channel_mode cxt5045_modes[1] = {
697         { 2, NULL },
698 };
699
700 static struct hda_input_mux cxt5045_capture_source = {
701         .num_items = 2,
702         .items = {
703                 { "IntMic", 0x1 },
704                 { "ExtMic", 0x2 },
705         }
706 };
707
708 static struct hda_input_mux cxt5045_capture_source_benq = {
709         .num_items = 5,
710         .items = {
711                 { "IntMic", 0x1 },
712                 { "ExtMic", 0x2 },
713                 { "LineIn", 0x3 },
714                 { "CD",     0x4 },
715                 { "Mixer",  0x0 },
716         }
717 };
718
719 static struct hda_input_mux cxt5045_capture_source_hp530 = {
720         .num_items = 2,
721         .items = {
722                 { "ExtMic", 0x1 },
723                 { "IntMic", 0x2 },
724         }
725 };
726
727 /* turn on/off EAPD (+ mute HP) as a master switch */
728 static int cxt5045_hp_master_sw_put(struct snd_kcontrol *kcontrol,
729                                     struct snd_ctl_elem_value *ucontrol)
730 {
731         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
732         struct conexant_spec *spec = codec->spec;
733         unsigned int bits;
734
735         if (!cxt_eapd_put(kcontrol, ucontrol))
736                 return 0;
737
738         /* toggle internal speakers mute depending of presence of
739          * the headphone jack
740          */
741         bits = (!spec->hp_present && spec->cur_eapd) ? 0 : HDA_AMP_MUTE;
742         snd_hda_codec_amp_stereo(codec, 0x10, HDA_OUTPUT, 0,
743                                  HDA_AMP_MUTE, bits);
744
745         bits = spec->cur_eapd ? 0 : HDA_AMP_MUTE;
746         snd_hda_codec_amp_stereo(codec, 0x11, HDA_OUTPUT, 0,
747                                  HDA_AMP_MUTE, bits);
748         return 1;
749 }
750
751 /* bind volumes of both NID 0x10 and 0x11 */
752 static struct hda_bind_ctls cxt5045_hp_bind_master_vol = {
753         .ops = &snd_hda_bind_vol,
754         .values = {
755                 HDA_COMPOSE_AMP_VAL(0x10, 3, 0, HDA_OUTPUT),
756                 HDA_COMPOSE_AMP_VAL(0x11, 3, 0, HDA_OUTPUT),
757                 0
758         },
759 };
760
761 /* toggle input of built-in and mic jack appropriately */
762 static void cxt5045_hp_automic(struct hda_codec *codec)
763 {
764         static struct hda_verb mic_jack_on[] = {
765                 {0x14, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
766                 {0x12, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
767                 {}
768         };
769         static struct hda_verb mic_jack_off[] = {
770                 {0x12, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
771                 {0x14, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
772                 {}
773         };
774         unsigned int present;
775
776         present = snd_hda_jack_detect(codec, 0x12);
777         if (present)
778                 snd_hda_sequence_write(codec, mic_jack_on);
779         else
780                 snd_hda_sequence_write(codec, mic_jack_off);
781 }
782
783
784 /* mute internal speaker if HP is plugged */
785 static void cxt5045_hp_automute(struct hda_codec *codec)
786 {
787         struct conexant_spec *spec = codec->spec;
788         unsigned int bits;
789
790         spec->hp_present = snd_hda_jack_detect(codec, 0x11);
791
792         bits = (spec->hp_present || !spec->cur_eapd) ? HDA_AMP_MUTE : 0; 
793         snd_hda_codec_amp_stereo(codec, 0x10, HDA_OUTPUT, 0,
794                                  HDA_AMP_MUTE, bits);
795 }
796
797 /* unsolicited event for HP jack sensing */
798 static void cxt5045_hp_unsol_event(struct hda_codec *codec,
799                                    unsigned int res)
800 {
801         res >>= 26;
802         switch (res) {
803         case CONEXANT_HP_EVENT:
804                 cxt5045_hp_automute(codec);
805                 break;
806         case CONEXANT_MIC_EVENT:
807                 cxt5045_hp_automic(codec);
808                 break;
809
810         }
811 }
812
813 static struct snd_kcontrol_new cxt5045_mixers[] = {
814         HDA_CODEC_VOLUME("Int Mic Capture Volume", 0x1a, 0x01, HDA_INPUT),
815         HDA_CODEC_MUTE("Int Mic Capture Switch", 0x1a, 0x01, HDA_INPUT),
816         HDA_CODEC_VOLUME("Ext Mic Capture Volume", 0x1a, 0x02, HDA_INPUT),
817         HDA_CODEC_MUTE("Ext Mic Capture Switch", 0x1a, 0x02, HDA_INPUT),
818         HDA_CODEC_VOLUME("PCM Playback Volume", 0x17, 0x0, HDA_INPUT),
819         HDA_CODEC_MUTE("PCM Playback Switch", 0x17, 0x0, HDA_INPUT),
820         HDA_CODEC_VOLUME("Int Mic Playback Volume", 0x17, 0x1, HDA_INPUT),
821         HDA_CODEC_MUTE("Int Mic Playback Switch", 0x17, 0x1, HDA_INPUT),
822         HDA_CODEC_VOLUME("Ext Mic Playback Volume", 0x17, 0x2, HDA_INPUT),
823         HDA_CODEC_MUTE("Ext Mic Playback Switch", 0x17, 0x2, HDA_INPUT),
824         HDA_BIND_VOL("Master Playback Volume", &cxt5045_hp_bind_master_vol),
825         {
826                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
827                 .name = "Master Playback Switch",
828                 .info = cxt_eapd_info,
829                 .get = cxt_eapd_get,
830                 .put = cxt5045_hp_master_sw_put,
831                 .private_value = 0x10,
832         },
833
834         {}
835 };
836
837 static struct snd_kcontrol_new cxt5045_benq_mixers[] = {
838         HDA_CODEC_VOLUME("CD Capture Volume", 0x1a, 0x04, HDA_INPUT),
839         HDA_CODEC_MUTE("CD Capture Switch", 0x1a, 0x04, HDA_INPUT),
840         HDA_CODEC_VOLUME("CD Playback Volume", 0x17, 0x4, HDA_INPUT),
841         HDA_CODEC_MUTE("CD Playback Switch", 0x17, 0x4, HDA_INPUT),
842
843         HDA_CODEC_VOLUME("Line In Capture Volume", 0x1a, 0x03, HDA_INPUT),
844         HDA_CODEC_MUTE("Line In Capture Switch", 0x1a, 0x03, HDA_INPUT),
845         HDA_CODEC_VOLUME("Line In Playback Volume", 0x17, 0x3, HDA_INPUT),
846         HDA_CODEC_MUTE("Line In Playback Switch", 0x17, 0x3, HDA_INPUT),
847
848         HDA_CODEC_VOLUME("Mixer Capture Volume", 0x1a, 0x0, HDA_INPUT),
849         HDA_CODEC_MUTE("Mixer Capture Switch", 0x1a, 0x0, HDA_INPUT),
850
851         {}
852 };
853
854 static struct snd_kcontrol_new cxt5045_mixers_hp530[] = {
855         HDA_CODEC_VOLUME("Int Mic Capture Volume", 0x1a, 0x02, HDA_INPUT),
856         HDA_CODEC_MUTE("Int Mic Capture Switch", 0x1a, 0x02, HDA_INPUT),
857         HDA_CODEC_VOLUME("Ext Mic Capture Volume", 0x1a, 0x01, HDA_INPUT),
858         HDA_CODEC_MUTE("Ext Mic Capture Switch", 0x1a, 0x01, HDA_INPUT),
859         HDA_CODEC_VOLUME("PCM Playback Volume", 0x17, 0x0, HDA_INPUT),
860         HDA_CODEC_MUTE("PCM Playback Switch", 0x17, 0x0, HDA_INPUT),
861         HDA_CODEC_VOLUME("Int Mic Playback Volume", 0x17, 0x2, HDA_INPUT),
862         HDA_CODEC_MUTE("Int Mic Playback Switch", 0x17, 0x2, HDA_INPUT),
863         HDA_CODEC_VOLUME("Ext Mic Playback Volume", 0x17, 0x1, HDA_INPUT),
864         HDA_CODEC_MUTE("Ext Mic Playback Switch", 0x17, 0x1, HDA_INPUT),
865         HDA_BIND_VOL("Master Playback Volume", &cxt5045_hp_bind_master_vol),
866         {
867                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
868                 .name = "Master Playback Switch",
869                 .info = cxt_eapd_info,
870                 .get = cxt_eapd_get,
871                 .put = cxt5045_hp_master_sw_put,
872                 .private_value = 0x10,
873         },
874
875         {}
876 };
877
878 static struct hda_verb cxt5045_init_verbs[] = {
879         /* Line in, Mic */
880         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN|AC_PINCTL_VREF_80 },
881         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN|AC_PINCTL_VREF_80 },
882         /* HP, Amp  */
883         {0x10, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
884         {0x10, AC_VERB_SET_CONNECT_SEL, 0x1},
885         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
886         {0x11, AC_VERB_SET_CONNECT_SEL, 0x1},
887         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
888         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
889         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
890         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
891         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
892         /* Record selector: Int mic */
893         {0x1a, AC_VERB_SET_CONNECT_SEL,0x1},
894         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE,
895          AC_AMP_SET_INPUT|AC_AMP_SET_RIGHT|AC_AMP_SET_LEFT|0x17},
896         /* SPDIF route: PCM */
897         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
898         { 0x13, AC_VERB_SET_CONNECT_SEL, 0x0 },
899         /* EAPD */
900         {0x10, AC_VERB_SET_EAPD_BTLENABLE, 0x2 }, /* default on */ 
901         { } /* end */
902 };
903
904 static struct hda_verb cxt5045_benq_init_verbs[] = {
905         /* Int Mic, Mic */
906         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN|AC_PINCTL_VREF_80 },
907         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN|AC_PINCTL_VREF_80 },
908         /* Line In,HP, Amp  */
909         {0x10, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
910         {0x10, AC_VERB_SET_CONNECT_SEL, 0x1},
911         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
912         {0x11, AC_VERB_SET_CONNECT_SEL, 0x1},
913         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
914         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
915         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
916         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
917         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
918         /* Record selector: Int mic */
919         {0x1a, AC_VERB_SET_CONNECT_SEL, 0x1},
920         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE,
921          AC_AMP_SET_INPUT|AC_AMP_SET_RIGHT|AC_AMP_SET_LEFT|0x17},
922         /* SPDIF route: PCM */
923         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
924         {0x13, AC_VERB_SET_CONNECT_SEL, 0x0},
925         /* EAPD */
926         {0x10, AC_VERB_SET_EAPD_BTLENABLE, 0x2}, /* default on */
927         { } /* end */
928 };
929
930 static struct hda_verb cxt5045_hp_sense_init_verbs[] = {
931         /* pin sensing on HP jack */
932         {0x11, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_HP_EVENT},
933         { } /* end */
934 };
935
936 static struct hda_verb cxt5045_mic_sense_init_verbs[] = {
937         /* pin sensing on HP jack */
938         {0x12, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_MIC_EVENT},
939         { } /* end */
940 };
941
942 #ifdef CONFIG_SND_DEBUG
943 /* Test configuration for debugging, modelled after the ALC260 test
944  * configuration.
945  */
946 static struct hda_input_mux cxt5045_test_capture_source = {
947         .num_items = 5,
948         .items = {
949                 { "MIXER", 0x0 },
950                 { "MIC1 pin", 0x1 },
951                 { "LINE1 pin", 0x2 },
952                 { "HP-OUT pin", 0x3 },
953                 { "CD pin", 0x4 },
954         },
955 };
956
957 static struct snd_kcontrol_new cxt5045_test_mixer[] = {
958
959         /* Output controls */
960         HDA_CODEC_VOLUME("Speaker Playback Volume", 0x10, 0x0, HDA_OUTPUT),
961         HDA_CODEC_MUTE("Speaker Playback Switch", 0x10, 0x0, HDA_OUTPUT),
962         HDA_CODEC_VOLUME("Node 11 Playback Volume", 0x11, 0x0, HDA_OUTPUT),
963         HDA_CODEC_MUTE("Node 11 Playback Switch", 0x11, 0x0, HDA_OUTPUT),
964         HDA_CODEC_VOLUME("Node 12 Playback Volume", 0x12, 0x0, HDA_OUTPUT),
965         HDA_CODEC_MUTE("Node 12 Playback Switch", 0x12, 0x0, HDA_OUTPUT),
966         
967         /* Modes for retasking pin widgets */
968         CXT_PIN_MODE("HP-OUT pin mode", 0x11, CXT_PIN_DIR_INOUT),
969         CXT_PIN_MODE("LINE1 pin mode", 0x12, CXT_PIN_DIR_INOUT),
970
971         /* EAPD Switch Control */
972         CXT_EAPD_SWITCH("External Amplifier", 0x10, 0x0),
973
974         /* Loopback mixer controls */
975
976         HDA_CODEC_VOLUME("Mixer-1 Volume", 0x17, 0x0, HDA_INPUT),
977         HDA_CODEC_MUTE("Mixer-1 Switch", 0x17, 0x0, HDA_INPUT),
978         HDA_CODEC_VOLUME("Mixer-2 Volume", 0x17, 0x1, HDA_INPUT),
979         HDA_CODEC_MUTE("Mixer-2 Switch", 0x17, 0x1, HDA_INPUT),
980         HDA_CODEC_VOLUME("Mixer-3 Volume", 0x17, 0x2, HDA_INPUT),
981         HDA_CODEC_MUTE("Mixer-3 Switch", 0x17, 0x2, HDA_INPUT),
982         HDA_CODEC_VOLUME("Mixer-4 Volume", 0x17, 0x3, HDA_INPUT),
983         HDA_CODEC_MUTE("Mixer-4 Switch", 0x17, 0x3, HDA_INPUT),
984         HDA_CODEC_VOLUME("Mixer-5 Volume", 0x17, 0x4, HDA_INPUT),
985         HDA_CODEC_MUTE("Mixer-5 Switch", 0x17, 0x4, HDA_INPUT),
986         {
987                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
988                 .name = "Input Source",
989                 .info = conexant_mux_enum_info,
990                 .get = conexant_mux_enum_get,
991                 .put = conexant_mux_enum_put,
992         },
993         /* Audio input controls */
994         HDA_CODEC_VOLUME("Input-1 Volume", 0x1a, 0x0, HDA_INPUT),
995         HDA_CODEC_MUTE("Input-1 Switch", 0x1a, 0x0, HDA_INPUT),
996         HDA_CODEC_VOLUME("Input-2 Volume", 0x1a, 0x1, HDA_INPUT),
997         HDA_CODEC_MUTE("Input-2 Switch", 0x1a, 0x1, HDA_INPUT),
998         HDA_CODEC_VOLUME("Input-3 Volume", 0x1a, 0x2, HDA_INPUT),
999         HDA_CODEC_MUTE("Input-3 Switch", 0x1a, 0x2, HDA_INPUT),
1000         HDA_CODEC_VOLUME("Input-4 Volume", 0x1a, 0x3, HDA_INPUT),
1001         HDA_CODEC_MUTE("Input-4 Switch", 0x1a, 0x3, HDA_INPUT),
1002         HDA_CODEC_VOLUME("Input-5 Volume", 0x1a, 0x4, HDA_INPUT),
1003         HDA_CODEC_MUTE("Input-5 Switch", 0x1a, 0x4, HDA_INPUT),
1004         { } /* end */
1005 };
1006
1007 static struct hda_verb cxt5045_test_init_verbs[] = {
1008         /* Set connections */
1009         { 0x10, AC_VERB_SET_CONNECT_SEL, 0x0 },
1010         { 0x11, AC_VERB_SET_CONNECT_SEL, 0x0 },
1011         { 0x12, AC_VERB_SET_CONNECT_SEL, 0x0 },
1012         /* Enable retasking pins as output, initially without power amp */
1013         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1014         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1015
1016         /* Disable digital (SPDIF) pins initially, but users can enable
1017          * them via a mixer switch.  In the case of SPDIF-out, this initverb
1018          * payload also sets the generation to 0, output to be in "consumer"
1019          * PCM format, copyright asserted, no pre-emphasis and no validity
1020          * control.
1021          */
1022         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1023         {0x18, AC_VERB_SET_DIGI_CONVERT_1, 0},
1024
1025         /* Start with output sum widgets muted and their output gains at min */
1026         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1027         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
1028
1029         /* Unmute retasking pin widget output buffers since the default
1030          * state appears to be output.  As the pin mode is changed by the
1031          * user the pin mode control will take care of enabling the pin's
1032          * input/output buffers as needed.
1033          */
1034         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1035         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1036
1037         /* Mute capture amp left and right */
1038         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1039
1040         /* Set ADC connection select to match default mixer setting (mic1
1041          * pin)
1042          */
1043         {0x1a, AC_VERB_SET_CONNECT_SEL, 0x00},
1044         {0x17, AC_VERB_SET_CONNECT_SEL, 0x00},
1045
1046         /* Mute all inputs to mixer widget (even unconnected ones) */
1047         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)}, /* Mixer pin */
1048         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)}, /* Mic1 pin */
1049         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)}, /* Line pin */
1050         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)}, /* HP pin */
1051         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)}, /* CD pin */
1052
1053         { }
1054 };
1055 #endif
1056
1057
1058 /* initialize jack-sensing, too */
1059 static int cxt5045_init(struct hda_codec *codec)
1060 {
1061         conexant_init(codec);
1062         cxt5045_hp_automute(codec);
1063         return 0;
1064 }
1065
1066
1067 enum {
1068         CXT5045_LAPTOP_HPSENSE,
1069         CXT5045_LAPTOP_MICSENSE,
1070         CXT5045_LAPTOP_HPMICSENSE,
1071         CXT5045_BENQ,
1072         CXT5045_LAPTOP_HP530,
1073 #ifdef CONFIG_SND_DEBUG
1074         CXT5045_TEST,
1075 #endif
1076         CXT5045_MODELS
1077 };
1078
1079 static const char *cxt5045_models[CXT5045_MODELS] = {
1080         [CXT5045_LAPTOP_HPSENSE]        = "laptop-hpsense",
1081         [CXT5045_LAPTOP_MICSENSE]       = "laptop-micsense",
1082         [CXT5045_LAPTOP_HPMICSENSE]     = "laptop-hpmicsense",
1083         [CXT5045_BENQ]                  = "benq",
1084         [CXT5045_LAPTOP_HP530]          = "laptop-hp530",
1085 #ifdef CONFIG_SND_DEBUG
1086         [CXT5045_TEST]          = "test",
1087 #endif
1088 };
1089
1090 static struct snd_pci_quirk cxt5045_cfg_tbl[] = {
1091         SND_PCI_QUIRK(0x103c, 0x30d5, "HP 530", CXT5045_LAPTOP_HP530),
1092         SND_PCI_QUIRK_MASK(0x103c, 0xff00, 0x3000, "HP DV Series",
1093                            CXT5045_LAPTOP_HPSENSE),
1094         SND_PCI_QUIRK(0x1179, 0xff31, "Toshiba P105", CXT5045_LAPTOP_MICSENSE),
1095         SND_PCI_QUIRK(0x152d, 0x0753, "Benq R55E", CXT5045_BENQ),
1096         SND_PCI_QUIRK(0x1734, 0x10ad, "Fujitsu Si1520", CXT5045_LAPTOP_MICSENSE),
1097         SND_PCI_QUIRK(0x1734, 0x10cb, "Fujitsu Si3515", CXT5045_LAPTOP_HPMICSENSE),
1098         SND_PCI_QUIRK(0x1734, 0x110e, "Fujitsu V5505",
1099                       CXT5045_LAPTOP_HPMICSENSE),
1100         SND_PCI_QUIRK(0x1509, 0x1e40, "FIC", CXT5045_LAPTOP_HPMICSENSE),
1101         SND_PCI_QUIRK(0x1509, 0x2f05, "FIC", CXT5045_LAPTOP_HPMICSENSE),
1102         SND_PCI_QUIRK(0x1509, 0x2f06, "FIC", CXT5045_LAPTOP_HPMICSENSE),
1103         SND_PCI_QUIRK_MASK(0x1631, 0xff00, 0xc100, "Packard Bell",
1104                            CXT5045_LAPTOP_HPMICSENSE),
1105         SND_PCI_QUIRK(0x8086, 0x2111, "Conexant Reference board", CXT5045_LAPTOP_HPSENSE),
1106         {}
1107 };
1108
1109 static int patch_cxt5045(struct hda_codec *codec)
1110 {
1111         struct conexant_spec *spec;
1112         int board_config;
1113
1114         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1115         if (!spec)
1116                 return -ENOMEM;
1117         codec->spec = spec;
1118         codec->pin_amp_workaround = 1;
1119
1120         spec->multiout.max_channels = 2;
1121         spec->multiout.num_dacs = ARRAY_SIZE(cxt5045_dac_nids);
1122         spec->multiout.dac_nids = cxt5045_dac_nids;
1123         spec->multiout.dig_out_nid = CXT5045_SPDIF_OUT;
1124         spec->num_adc_nids = 1;
1125         spec->adc_nids = cxt5045_adc_nids;
1126         spec->capsrc_nids = cxt5045_capsrc_nids;
1127         spec->input_mux = &cxt5045_capture_source;
1128         spec->num_mixers = 1;
1129         spec->mixers[0] = cxt5045_mixers;
1130         spec->num_init_verbs = 1;
1131         spec->init_verbs[0] = cxt5045_init_verbs;
1132         spec->spdif_route = 0;
1133         spec->num_channel_mode = ARRAY_SIZE(cxt5045_modes),
1134         spec->channel_mode = cxt5045_modes,
1135
1136
1137         codec->patch_ops = conexant_patch_ops;
1138
1139         board_config = snd_hda_check_board_config(codec, CXT5045_MODELS,
1140                                                   cxt5045_models,
1141                                                   cxt5045_cfg_tbl);
1142         switch (board_config) {
1143         case CXT5045_LAPTOP_HPSENSE:
1144                 codec->patch_ops.unsol_event = cxt5045_hp_unsol_event;
1145                 spec->input_mux = &cxt5045_capture_source;
1146                 spec->num_init_verbs = 2;
1147                 spec->init_verbs[1] = cxt5045_hp_sense_init_verbs;
1148                 spec->mixers[0] = cxt5045_mixers;
1149                 codec->patch_ops.init = cxt5045_init;
1150                 break;
1151         case CXT5045_LAPTOP_MICSENSE:
1152                 codec->patch_ops.unsol_event = cxt5045_hp_unsol_event;
1153                 spec->input_mux = &cxt5045_capture_source;
1154                 spec->num_init_verbs = 2;
1155                 spec->init_verbs[1] = cxt5045_mic_sense_init_verbs;
1156                 spec->mixers[0] = cxt5045_mixers;
1157                 codec->patch_ops.init = cxt5045_init;
1158                 break;
1159         default:
1160         case CXT5045_LAPTOP_HPMICSENSE:
1161                 codec->patch_ops.unsol_event = cxt5045_hp_unsol_event;
1162                 spec->input_mux = &cxt5045_capture_source;
1163                 spec->num_init_verbs = 3;
1164                 spec->init_verbs[1] = cxt5045_hp_sense_init_verbs;
1165                 spec->init_verbs[2] = cxt5045_mic_sense_init_verbs;
1166                 spec->mixers[0] = cxt5045_mixers;
1167                 codec->patch_ops.init = cxt5045_init;
1168                 break;
1169         case CXT5045_BENQ:
1170                 codec->patch_ops.unsol_event = cxt5045_hp_unsol_event;
1171                 spec->input_mux = &cxt5045_capture_source_benq;
1172                 spec->num_init_verbs = 1;
1173                 spec->init_verbs[0] = cxt5045_benq_init_verbs;
1174                 spec->mixers[0] = cxt5045_mixers;
1175                 spec->mixers[1] = cxt5045_benq_mixers;
1176                 spec->num_mixers = 2;
1177                 codec->patch_ops.init = cxt5045_init;
1178                 break;
1179         case CXT5045_LAPTOP_HP530:
1180                 codec->patch_ops.unsol_event = cxt5045_hp_unsol_event;
1181                 spec->input_mux = &cxt5045_capture_source_hp530;
1182                 spec->num_init_verbs = 2;
1183                 spec->init_verbs[1] = cxt5045_hp_sense_init_verbs;
1184                 spec->mixers[0] = cxt5045_mixers_hp530;
1185                 codec->patch_ops.init = cxt5045_init;
1186                 break;
1187 #ifdef CONFIG_SND_DEBUG
1188         case CXT5045_TEST:
1189                 spec->input_mux = &cxt5045_test_capture_source;
1190                 spec->mixers[0] = cxt5045_test_mixer;
1191                 spec->init_verbs[0] = cxt5045_test_init_verbs;
1192                 break;
1193                 
1194 #endif  
1195         }
1196
1197         switch (codec->subsystem_id >> 16) {
1198         case 0x103c:
1199         case 0x1631:
1200         case 0x1734:
1201                 /* HP, Packard Bell, & Fujitsu-Siemens laptops have really bad
1202                  * sound over 0dB on NID 0x17. Fix max PCM level to 0 dB
1203                  * (originally it has 0x2b steps with 0dB offset 0x14)
1204                  */
1205                 snd_hda_override_amp_caps(codec, 0x17, HDA_INPUT,
1206                                           (0x14 << AC_AMPCAP_OFFSET_SHIFT) |
1207                                           (0x14 << AC_AMPCAP_NUM_STEPS_SHIFT) |
1208                                           (0x05 << AC_AMPCAP_STEP_SIZE_SHIFT) |
1209                                           (1 << AC_AMPCAP_MUTE_SHIFT));
1210                 break;
1211         }
1212
1213         return 0;
1214 }
1215
1216
1217 /* Conexant 5047 specific */
1218 #define CXT5047_SPDIF_OUT       0x11
1219
1220 static hda_nid_t cxt5047_dac_nids[1] = { 0x10 }; /* 0x1c */
1221 static hda_nid_t cxt5047_adc_nids[1] = { 0x12 };
1222 static hda_nid_t cxt5047_capsrc_nids[1] = { 0x1a };
1223
1224 static struct hda_channel_mode cxt5047_modes[1] = {
1225         { 2, NULL },
1226 };
1227
1228 static struct hda_input_mux cxt5047_toshiba_capture_source = {
1229         .num_items = 2,
1230         .items = {
1231                 { "ExtMic", 0x2 },
1232                 { "Line-In", 0x1 },
1233         }
1234 };
1235
1236 /* turn on/off EAPD (+ mute HP) as a master switch */
1237 static int cxt5047_hp_master_sw_put(struct snd_kcontrol *kcontrol,
1238                                     struct snd_ctl_elem_value *ucontrol)
1239 {
1240         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1241         struct conexant_spec *spec = codec->spec;
1242         unsigned int bits;
1243
1244         if (!cxt_eapd_put(kcontrol, ucontrol))
1245                 return 0;
1246
1247         /* toggle internal speakers mute depending of presence of
1248          * the headphone jack
1249          */
1250         bits = (!spec->hp_present && spec->cur_eapd) ? 0 : HDA_AMP_MUTE;
1251         /* NOTE: Conexat codec needs the index for *OUTPUT* amp of
1252          * pin widgets unlike other codecs.  In this case, we need to
1253          * set index 0x01 for the volume from the mixer amp 0x19.
1254          */
1255         snd_hda_codec_amp_stereo(codec, 0x1d, HDA_OUTPUT, 0x01,
1256                                  HDA_AMP_MUTE, bits);
1257         bits = spec->cur_eapd ? 0 : HDA_AMP_MUTE;
1258         snd_hda_codec_amp_stereo(codec, 0x13, HDA_OUTPUT, 0,
1259                                  HDA_AMP_MUTE, bits);
1260         return 1;
1261 }
1262
1263 /* mute internal speaker if HP is plugged */
1264 static void cxt5047_hp_automute(struct hda_codec *codec)
1265 {
1266         struct conexant_spec *spec = codec->spec;
1267         unsigned int bits;
1268
1269         spec->hp_present = snd_hda_jack_detect(codec, 0x13);
1270
1271         bits = (spec->hp_present || !spec->cur_eapd) ? HDA_AMP_MUTE : 0;
1272         /* See the note in cxt5047_hp_master_sw_put */
1273         snd_hda_codec_amp_stereo(codec, 0x1d, HDA_OUTPUT, 0x01,
1274                                  HDA_AMP_MUTE, bits);
1275 }
1276
1277 /* toggle input of built-in and mic jack appropriately */
1278 static void cxt5047_hp_automic(struct hda_codec *codec)
1279 {
1280         static struct hda_verb mic_jack_on[] = {
1281                 {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1282                 {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1283                 {}
1284         };
1285         static struct hda_verb mic_jack_off[] = {
1286                 {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1287                 {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1288                 {}
1289         };
1290         unsigned int present;
1291
1292         present = snd_hda_jack_detect(codec, 0x15);
1293         if (present)
1294                 snd_hda_sequence_write(codec, mic_jack_on);
1295         else
1296                 snd_hda_sequence_write(codec, mic_jack_off);
1297 }
1298
1299 /* unsolicited event for HP jack sensing */
1300 static void cxt5047_hp_unsol_event(struct hda_codec *codec,
1301                                   unsigned int res)
1302 {
1303         switch (res >> 26) {
1304         case CONEXANT_HP_EVENT:
1305                 cxt5047_hp_automute(codec);
1306                 break;
1307         case CONEXANT_MIC_EVENT:
1308                 cxt5047_hp_automic(codec);
1309                 break;
1310         }
1311 }
1312
1313 static struct snd_kcontrol_new cxt5047_base_mixers[] = {
1314         HDA_CODEC_VOLUME("Mic Playback Volume", 0x19, 0x02, HDA_INPUT),
1315         HDA_CODEC_MUTE("Mic Playback Switch", 0x19, 0x02, HDA_INPUT),
1316         HDA_CODEC_VOLUME("Mic Boost", 0x1a, 0x0, HDA_OUTPUT),
1317         HDA_CODEC_VOLUME("Capture Volume", 0x12, 0x03, HDA_INPUT),
1318         HDA_CODEC_MUTE("Capture Switch", 0x12, 0x03, HDA_INPUT),
1319         HDA_CODEC_VOLUME("PCM Volume", 0x10, 0x00, HDA_OUTPUT),
1320         HDA_CODEC_MUTE("PCM Switch", 0x10, 0x00, HDA_OUTPUT),
1321         {
1322                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1323                 .name = "Master Playback Switch",
1324                 .info = cxt_eapd_info,
1325                 .get = cxt_eapd_get,
1326                 .put = cxt5047_hp_master_sw_put,
1327                 .private_value = 0x13,
1328         },
1329
1330         {}
1331 };
1332
1333 static struct snd_kcontrol_new cxt5047_hp_spk_mixers[] = {
1334         /* See the note in cxt5047_hp_master_sw_put */
1335         HDA_CODEC_VOLUME("Speaker Playback Volume", 0x1d, 0x01, HDA_OUTPUT),
1336         HDA_CODEC_VOLUME("Headphone Playback Volume", 0x13, 0x00, HDA_OUTPUT),
1337         {}
1338 };
1339
1340 static struct snd_kcontrol_new cxt5047_hp_only_mixers[] = {
1341         HDA_CODEC_VOLUME("Master Playback Volume", 0x13, 0x00, HDA_OUTPUT),
1342         { } /* end */
1343 };
1344
1345 static struct hda_verb cxt5047_init_verbs[] = {
1346         /* Line in, Mic, Built-in Mic */
1347         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
1348         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN|AC_PINCTL_VREF_50 },
1349         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN|AC_PINCTL_VREF_50 },
1350         /* HP, Speaker  */
1351         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
1352         {0x13, AC_VERB_SET_CONNECT_SEL, 0x0}, /* mixer(0x19) */
1353         {0x1d, AC_VERB_SET_CONNECT_SEL, 0x1}, /* mixer(0x19) */
1354         /* Record selector: Mic */
1355         {0x12, AC_VERB_SET_CONNECT_SEL,0x03},
1356         {0x19, AC_VERB_SET_AMP_GAIN_MUTE,
1357          AC_AMP_SET_INPUT|AC_AMP_SET_RIGHT|AC_AMP_SET_LEFT|0x17},
1358         {0x1A, AC_VERB_SET_CONNECT_SEL,0x02},
1359         {0x1A, AC_VERB_SET_AMP_GAIN_MUTE,
1360          AC_AMP_SET_OUTPUT|AC_AMP_SET_RIGHT|AC_AMP_SET_LEFT|0x00},
1361         {0x1A, AC_VERB_SET_AMP_GAIN_MUTE,
1362          AC_AMP_SET_OUTPUT|AC_AMP_SET_RIGHT|AC_AMP_SET_LEFT|0x03},
1363         /* SPDIF route: PCM */
1364         { 0x18, AC_VERB_SET_CONNECT_SEL, 0x0 },
1365         /* Enable unsolicited events */
1366         {0x13, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_HP_EVENT},
1367         {0x15, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_MIC_EVENT},
1368         { } /* end */
1369 };
1370
1371 /* configuration for Toshiba Laptops */
1372 static struct hda_verb cxt5047_toshiba_init_verbs[] = {
1373         {0x13, AC_VERB_SET_EAPD_BTLENABLE, 0x0}, /* default off */
1374         {}
1375 };
1376
1377 /* Test configuration for debugging, modelled after the ALC260 test
1378  * configuration.
1379  */
1380 #ifdef CONFIG_SND_DEBUG
1381 static struct hda_input_mux cxt5047_test_capture_source = {
1382         .num_items = 4,
1383         .items = {
1384                 { "LINE1 pin", 0x0 },
1385                 { "MIC1 pin", 0x1 },
1386                 { "MIC2 pin", 0x2 },
1387                 { "CD pin", 0x3 },
1388         },
1389 };
1390
1391 static struct snd_kcontrol_new cxt5047_test_mixer[] = {
1392
1393         /* Output only controls */
1394         HDA_CODEC_VOLUME("OutAmp-1 Volume", 0x10, 0x0, HDA_OUTPUT),
1395         HDA_CODEC_MUTE("OutAmp-1 Switch", 0x10,0x0, HDA_OUTPUT),
1396         HDA_CODEC_VOLUME("OutAmp-2 Volume", 0x1c, 0x0, HDA_OUTPUT),
1397         HDA_CODEC_MUTE("OutAmp-2 Switch", 0x1c, 0x0, HDA_OUTPUT),
1398         HDA_CODEC_VOLUME("Speaker Playback Volume", 0x1d, 0x0, HDA_OUTPUT),
1399         HDA_CODEC_MUTE("Speaker Playback Switch", 0x1d, 0x0, HDA_OUTPUT),
1400         HDA_CODEC_VOLUME("HeadPhone Playback Volume", 0x13, 0x0, HDA_OUTPUT),
1401         HDA_CODEC_MUTE("HeadPhone Playback Switch", 0x13, 0x0, HDA_OUTPUT),
1402         HDA_CODEC_VOLUME("Line1-Out Playback Volume", 0x14, 0x0, HDA_OUTPUT),
1403         HDA_CODEC_MUTE("Line1-Out Playback Switch", 0x14, 0x0, HDA_OUTPUT),
1404         HDA_CODEC_VOLUME("Line2-Out Playback Volume", 0x15, 0x0, HDA_OUTPUT),
1405         HDA_CODEC_MUTE("Line2-Out Playback Switch", 0x15, 0x0, HDA_OUTPUT),
1406
1407         /* Modes for retasking pin widgets */
1408         CXT_PIN_MODE("LINE1 pin mode", 0x14, CXT_PIN_DIR_INOUT),
1409         CXT_PIN_MODE("MIC1 pin mode", 0x15, CXT_PIN_DIR_INOUT),
1410
1411         /* EAPD Switch Control */
1412         CXT_EAPD_SWITCH("External Amplifier", 0x13, 0x0),
1413
1414         /* Loopback mixer controls */
1415         HDA_CODEC_VOLUME("MIC1 Playback Volume", 0x12, 0x01, HDA_INPUT),
1416         HDA_CODEC_MUTE("MIC1 Playback Switch", 0x12, 0x01, HDA_INPUT),
1417         HDA_CODEC_VOLUME("MIC2 Playback Volume", 0x12, 0x02, HDA_INPUT),
1418         HDA_CODEC_MUTE("MIC2 Playback Switch", 0x12, 0x02, HDA_INPUT),
1419         HDA_CODEC_VOLUME("LINE Playback Volume", 0x12, 0x0, HDA_INPUT),
1420         HDA_CODEC_MUTE("LINE Playback Switch", 0x12, 0x0, HDA_INPUT),
1421         HDA_CODEC_VOLUME("CD Playback Volume", 0x12, 0x04, HDA_INPUT),
1422         HDA_CODEC_MUTE("CD Playback Switch", 0x12, 0x04, HDA_INPUT),
1423
1424         HDA_CODEC_VOLUME("Capture-1 Volume", 0x19, 0x0, HDA_INPUT),
1425         HDA_CODEC_MUTE("Capture-1 Switch", 0x19, 0x0, HDA_INPUT),
1426         HDA_CODEC_VOLUME("Capture-2 Volume", 0x19, 0x1, HDA_INPUT),
1427         HDA_CODEC_MUTE("Capture-2 Switch", 0x19, 0x1, HDA_INPUT),
1428         HDA_CODEC_VOLUME("Capture-3 Volume", 0x19, 0x2, HDA_INPUT),
1429         HDA_CODEC_MUTE("Capture-3 Switch", 0x19, 0x2, HDA_INPUT),
1430         HDA_CODEC_VOLUME("Capture-4 Volume", 0x19, 0x3, HDA_INPUT),
1431         HDA_CODEC_MUTE("Capture-4 Switch", 0x19, 0x3, HDA_INPUT),
1432         {
1433                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1434                 .name = "Input Source",
1435                 .info = conexant_mux_enum_info,
1436                 .get = conexant_mux_enum_get,
1437                 .put = conexant_mux_enum_put,
1438         },
1439         HDA_CODEC_VOLUME("Mic Boost Volume", 0x1a, 0x0, HDA_OUTPUT),
1440
1441         { } /* end */
1442 };
1443
1444 static struct hda_verb cxt5047_test_init_verbs[] = {
1445         /* Enable retasking pins as output, initially without power amp */
1446         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1447         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1448         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1449
1450         /* Disable digital (SPDIF) pins initially, but users can enable
1451          * them via a mixer switch.  In the case of SPDIF-out, this initverb
1452          * payload also sets the generation to 0, output to be in "consumer"
1453          * PCM format, copyright asserted, no pre-emphasis and no validity
1454          * control.
1455          */
1456         {0x18, AC_VERB_SET_DIGI_CONVERT_1, 0},
1457
1458         /* Ensure mic1, mic2, line1 pin widgets take input from the 
1459          * OUT1 sum bus when acting as an output.
1460          */
1461         {0x1a, AC_VERB_SET_CONNECT_SEL, 0},
1462         {0x1b, AC_VERB_SET_CONNECT_SEL, 0},
1463
1464         /* Start with output sum widgets muted and their output gains at min */
1465         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1466         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
1467
1468         /* Unmute retasking pin widget output buffers since the default
1469          * state appears to be output.  As the pin mode is changed by the
1470          * user the pin mode control will take care of enabling the pin's
1471          * input/output buffers as needed.
1472          */
1473         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1474         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1475         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1476
1477         /* Mute capture amp left and right */
1478         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1479
1480         /* Set ADC connection select to match default mixer setting (mic1
1481          * pin)
1482          */
1483         {0x12, AC_VERB_SET_CONNECT_SEL, 0x00},
1484
1485         /* Mute all inputs to mixer widget (even unconnected ones) */
1486         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)}, /* mic1 pin */
1487         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)}, /* mic2 pin */
1488         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)}, /* line1 pin */
1489         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)}, /* line2 pin */
1490         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)}, /* CD pin */
1491         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(5)}, /* Beep-gen pin */
1492         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(6)}, /* Line-out pin */
1493         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(7)}, /* HP-pin pin */
1494
1495         { }
1496 };
1497 #endif
1498
1499
1500 /* initialize jack-sensing, too */
1501 static int cxt5047_hp_init(struct hda_codec *codec)
1502 {
1503         conexant_init(codec);
1504         cxt5047_hp_automute(codec);
1505         return 0;
1506 }
1507
1508
1509 enum {
1510         CXT5047_LAPTOP,         /* Laptops w/o EAPD support */
1511         CXT5047_LAPTOP_HP,      /* Some HP laptops */
1512         CXT5047_LAPTOP_EAPD,    /* Laptops with EAPD support */
1513 #ifdef CONFIG_SND_DEBUG
1514         CXT5047_TEST,
1515 #endif
1516         CXT5047_MODELS
1517 };
1518
1519 static const char *cxt5047_models[CXT5047_MODELS] = {
1520         [CXT5047_LAPTOP]        = "laptop",
1521         [CXT5047_LAPTOP_HP]     = "laptop-hp",
1522         [CXT5047_LAPTOP_EAPD]   = "laptop-eapd",
1523 #ifdef CONFIG_SND_DEBUG
1524         [CXT5047_TEST]          = "test",
1525 #endif
1526 };
1527
1528 static struct snd_pci_quirk cxt5047_cfg_tbl[] = {
1529         SND_PCI_QUIRK(0x103c, 0x30a5, "HP DV5200T/DV8000T", CXT5047_LAPTOP_HP),
1530         SND_PCI_QUIRK_MASK(0x103c, 0xff00, 0x3000, "HP DV Series",
1531                            CXT5047_LAPTOP),
1532         SND_PCI_QUIRK(0x1179, 0xff31, "Toshiba P100", CXT5047_LAPTOP_EAPD),
1533         {}
1534 };
1535
1536 static int patch_cxt5047(struct hda_codec *codec)
1537 {
1538         struct conexant_spec *spec;
1539         int board_config;
1540
1541         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1542         if (!spec)
1543                 return -ENOMEM;
1544         codec->spec = spec;
1545         codec->pin_amp_workaround = 1;
1546
1547         spec->multiout.max_channels = 2;
1548         spec->multiout.num_dacs = ARRAY_SIZE(cxt5047_dac_nids);
1549         spec->multiout.dac_nids = cxt5047_dac_nids;
1550         spec->multiout.dig_out_nid = CXT5047_SPDIF_OUT;
1551         spec->num_adc_nids = 1;
1552         spec->adc_nids = cxt5047_adc_nids;
1553         spec->capsrc_nids = cxt5047_capsrc_nids;
1554         spec->num_mixers = 1;
1555         spec->mixers[0] = cxt5047_base_mixers;
1556         spec->num_init_verbs = 1;
1557         spec->init_verbs[0] = cxt5047_init_verbs;
1558         spec->spdif_route = 0;
1559         spec->num_channel_mode = ARRAY_SIZE(cxt5047_modes),
1560         spec->channel_mode = cxt5047_modes,
1561
1562         codec->patch_ops = conexant_patch_ops;
1563
1564         board_config = snd_hda_check_board_config(codec, CXT5047_MODELS,
1565                                                   cxt5047_models,
1566                                                   cxt5047_cfg_tbl);
1567         switch (board_config) {
1568         case CXT5047_LAPTOP:
1569                 spec->num_mixers = 2;
1570                 spec->mixers[1] = cxt5047_hp_spk_mixers;
1571                 codec->patch_ops.unsol_event = cxt5047_hp_unsol_event;
1572                 break;
1573         case CXT5047_LAPTOP_HP:
1574                 spec->num_mixers = 2;
1575                 spec->mixers[1] = cxt5047_hp_only_mixers;
1576                 codec->patch_ops.unsol_event = cxt5047_hp_unsol_event;
1577                 codec->patch_ops.init = cxt5047_hp_init;
1578                 break;
1579         case CXT5047_LAPTOP_EAPD:
1580                 spec->input_mux = &cxt5047_toshiba_capture_source;
1581                 spec->num_mixers = 2;
1582                 spec->mixers[1] = cxt5047_hp_spk_mixers;
1583                 spec->num_init_verbs = 2;
1584                 spec->init_verbs[1] = cxt5047_toshiba_init_verbs;
1585                 codec->patch_ops.unsol_event = cxt5047_hp_unsol_event;
1586                 break;
1587 #ifdef CONFIG_SND_DEBUG
1588         case CXT5047_TEST:
1589                 spec->input_mux = &cxt5047_test_capture_source;
1590                 spec->mixers[0] = cxt5047_test_mixer;
1591                 spec->init_verbs[0] = cxt5047_test_init_verbs;
1592                 codec->patch_ops.unsol_event = cxt5047_hp_unsol_event;
1593 #endif  
1594         }
1595         spec->vmaster_nid = 0x13;
1596
1597         switch (codec->subsystem_id >> 16) {
1598         case 0x103c:
1599                 /* HP laptops have really bad sound over 0 dB on NID 0x10.
1600                  * Fix max PCM level to 0 dB (originally it has 0x1e steps
1601                  * with 0 dB offset 0x17)
1602                  */
1603                 snd_hda_override_amp_caps(codec, 0x10, HDA_INPUT,
1604                                           (0x17 << AC_AMPCAP_OFFSET_SHIFT) |
1605                                           (0x17 << AC_AMPCAP_NUM_STEPS_SHIFT) |
1606                                           (0x05 << AC_AMPCAP_STEP_SIZE_SHIFT) |
1607                                           (1 << AC_AMPCAP_MUTE_SHIFT));
1608                 break;
1609         }
1610
1611         return 0;
1612 }
1613
1614 /* Conexant 5051 specific */
1615 static hda_nid_t cxt5051_dac_nids[1] = { 0x10 };
1616 static hda_nid_t cxt5051_adc_nids[2] = { 0x14, 0x15 };
1617
1618 static struct hda_channel_mode cxt5051_modes[1] = {
1619         { 2, NULL },
1620 };
1621
1622 static void cxt5051_update_speaker(struct hda_codec *codec)
1623 {
1624         struct conexant_spec *spec = codec->spec;
1625         unsigned int pinctl;
1626         /* headphone pin */
1627         pinctl = (spec->hp_present && spec->cur_eapd) ? PIN_HP : 0;
1628         snd_hda_codec_write(codec, 0x16, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
1629                             pinctl);
1630         /* speaker pin */
1631         pinctl = (!spec->hp_present && spec->cur_eapd) ? PIN_OUT : 0;
1632         snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
1633                             pinctl);
1634 }
1635
1636 /* turn on/off EAPD (+ mute HP) as a master switch */
1637 static int cxt5051_hp_master_sw_put(struct snd_kcontrol *kcontrol,
1638                                     struct snd_ctl_elem_value *ucontrol)
1639 {
1640         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1641
1642         if (!cxt_eapd_put(kcontrol, ucontrol))
1643                 return 0;
1644         cxt5051_update_speaker(codec);
1645         return 1;
1646 }
1647
1648 /* toggle input of built-in and mic jack appropriately */
1649 static void cxt5051_portb_automic(struct hda_codec *codec)
1650 {
1651         struct conexant_spec *spec = codec->spec;
1652         unsigned int present;
1653
1654         if (!(spec->auto_mic & AUTO_MIC_PORTB))
1655                 return;
1656         present = snd_hda_jack_detect(codec, 0x17);
1657         snd_hda_codec_write(codec, 0x14, 0,
1658                             AC_VERB_SET_CONNECT_SEL,
1659                             present ? 0x01 : 0x00);
1660 }
1661
1662 /* switch the current ADC according to the jack state */
1663 static void cxt5051_portc_automic(struct hda_codec *codec)
1664 {
1665         struct conexant_spec *spec = codec->spec;
1666         unsigned int present;
1667         hda_nid_t new_adc;
1668
1669         if (!(spec->auto_mic & AUTO_MIC_PORTC))
1670                 return;
1671         present = snd_hda_jack_detect(codec, 0x18);
1672         if (present)
1673                 spec->cur_adc_idx = 1;
1674         else
1675                 spec->cur_adc_idx = 0;
1676         new_adc = spec->adc_nids[spec->cur_adc_idx];
1677         if (spec->cur_adc && spec->cur_adc != new_adc) {
1678                 /* stream is running, let's swap the current ADC */
1679                 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
1680                 spec->cur_adc = new_adc;
1681                 snd_hda_codec_setup_stream(codec, new_adc,
1682                                            spec->cur_adc_stream_tag, 0,
1683                                            spec->cur_adc_format);
1684         }
1685 }
1686
1687 /* mute internal speaker if HP is plugged */
1688 static void cxt5051_hp_automute(struct hda_codec *codec)
1689 {
1690         struct conexant_spec *spec = codec->spec;
1691
1692         spec->hp_present = snd_hda_jack_detect(codec, 0x16);
1693         cxt5051_update_speaker(codec);
1694 }
1695
1696 /* unsolicited event for HP jack sensing */
1697 static void cxt5051_hp_unsol_event(struct hda_codec *codec,
1698                                    unsigned int res)
1699 {
1700         int nid = (res & AC_UNSOL_RES_SUBTAG) >> 20;
1701         switch (res >> 26) {
1702         case CONEXANT_HP_EVENT:
1703                 cxt5051_hp_automute(codec);
1704                 break;
1705         case CXT5051_PORTB_EVENT:
1706                 cxt5051_portb_automic(codec);
1707                 break;
1708         case CXT5051_PORTC_EVENT:
1709                 cxt5051_portc_automic(codec);
1710                 break;
1711         }
1712         conexant_report_jack(codec, nid);
1713 }
1714
1715 static struct snd_kcontrol_new cxt5051_playback_mixers[] = {
1716         HDA_CODEC_VOLUME("Master Playback Volume", 0x10, 0x00, HDA_OUTPUT),
1717         {
1718                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1719                 .name = "Master Playback Switch",
1720                 .info = cxt_eapd_info,
1721                 .get = cxt_eapd_get,
1722                 .put = cxt5051_hp_master_sw_put,
1723                 .private_value = 0x1a,
1724         },
1725         {}
1726 };
1727
1728 static struct snd_kcontrol_new cxt5051_capture_mixers[] = {
1729         HDA_CODEC_VOLUME("Internal Mic Volume", 0x14, 0x00, HDA_INPUT),
1730         HDA_CODEC_MUTE("Internal Mic Switch", 0x14, 0x00, HDA_INPUT),
1731         HDA_CODEC_VOLUME("External Mic Volume", 0x14, 0x01, HDA_INPUT),
1732         HDA_CODEC_MUTE("External Mic Switch", 0x14, 0x01, HDA_INPUT),
1733         HDA_CODEC_VOLUME("Docking Mic Volume", 0x15, 0x00, HDA_INPUT),
1734         HDA_CODEC_MUTE("Docking Mic Switch", 0x15, 0x00, HDA_INPUT),
1735         {}
1736 };
1737
1738 static struct snd_kcontrol_new cxt5051_hp_mixers[] = {
1739         HDA_CODEC_VOLUME("Internal Mic Volume", 0x14, 0x00, HDA_INPUT),
1740         HDA_CODEC_MUTE("Internal Mic Switch", 0x14, 0x00, HDA_INPUT),
1741         HDA_CODEC_VOLUME("External Mic Volume", 0x15, 0x00, HDA_INPUT),
1742         HDA_CODEC_MUTE("External Mic Switch", 0x15, 0x00, HDA_INPUT),
1743         {}
1744 };
1745
1746 static struct snd_kcontrol_new cxt5051_hp_dv6736_mixers[] = {
1747         HDA_CODEC_VOLUME("Capture Volume", 0x14, 0x00, HDA_INPUT),
1748         HDA_CODEC_MUTE("Capture Switch", 0x14, 0x00, HDA_INPUT),
1749         {}
1750 };
1751
1752 static struct snd_kcontrol_new cxt5051_f700_mixers[] = {
1753         HDA_CODEC_VOLUME("Capture Volume", 0x14, 0x01, HDA_INPUT),
1754         HDA_CODEC_MUTE("Capture Switch", 0x14, 0x01, HDA_INPUT),
1755         {}
1756 };
1757
1758 static struct snd_kcontrol_new cxt5051_toshiba_mixers[] = {
1759         HDA_CODEC_VOLUME("Internal Mic Volume", 0x14, 0x00, HDA_INPUT),
1760         HDA_CODEC_MUTE("Internal Mic Switch", 0x14, 0x00, HDA_INPUT),
1761         HDA_CODEC_VOLUME("External Mic Volume", 0x14, 0x01, HDA_INPUT),
1762         HDA_CODEC_MUTE("External Mic Switch", 0x14, 0x01, HDA_INPUT),
1763         {}
1764 };
1765
1766 static struct hda_verb cxt5051_init_verbs[] = {
1767         /* Line in, Mic */
1768         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x03},
1769         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1770         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x03},
1771         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1772         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
1773         {0x1d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x03},
1774         /* SPK  */
1775         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1776         {0x1a, AC_VERB_SET_CONNECT_SEL, 0x00},
1777         /* HP, Amp  */
1778         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1779         {0x16, AC_VERB_SET_CONNECT_SEL, 0x00},
1780         /* DAC1 */      
1781         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1782         /* Record selector: Int mic */
1783         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x44},
1784         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1) | 0x44},
1785         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x44},
1786         /* SPDIF route: PCM */
1787         {0x1c, AC_VERB_SET_CONNECT_SEL, 0x0},
1788         /* EAPD */
1789         {0x1a, AC_VERB_SET_EAPD_BTLENABLE, 0x2}, /* default on */ 
1790         {0x16, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN|CONEXANT_HP_EVENT},
1791         { } /* end */
1792 };
1793
1794 static struct hda_verb cxt5051_hp_dv6736_init_verbs[] = {
1795         /* Line in, Mic */
1796         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x03},
1797         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1798         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x0},
1799         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x0},
1800         /* SPK  */
1801         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1802         {0x1a, AC_VERB_SET_CONNECT_SEL, 0x00},
1803         /* HP, Amp  */
1804         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1805         {0x16, AC_VERB_SET_CONNECT_SEL, 0x00},
1806         /* DAC1 */
1807         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1808         /* Record selector: Int mic */
1809         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1) | 0x44},
1810         {0x14, AC_VERB_SET_CONNECT_SEL, 0x1},
1811         /* SPDIF route: PCM */
1812         {0x1c, AC_VERB_SET_CONNECT_SEL, 0x0},
1813         /* EAPD */
1814         {0x1a, AC_VERB_SET_EAPD_BTLENABLE, 0x2}, /* default on */
1815         {0x16, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN|CONEXANT_HP_EVENT},
1816         { } /* end */
1817 };
1818
1819 static struct hda_verb cxt5051_lenovo_x200_init_verbs[] = {
1820         /* Line in, Mic */
1821         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x03},
1822         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1823         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x03},
1824         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1825         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
1826         {0x1d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x03},
1827         /* SPK  */
1828         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1829         {0x1a, AC_VERB_SET_CONNECT_SEL, 0x00},
1830         /* HP, Amp  */
1831         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1832         {0x16, AC_VERB_SET_CONNECT_SEL, 0x00},
1833         /* Docking HP */
1834         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1835         {0x19, AC_VERB_SET_CONNECT_SEL, 0x00},
1836         /* DAC1 */
1837         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1838         /* Record selector: Int mic */
1839         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x44},
1840         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1) | 0x44},
1841         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x44},
1842         /* SPDIF route: PCM */
1843         {0x1c, AC_VERB_SET_CONNECT_SEL, 0x0},
1844         /* EAPD */
1845         {0x1a, AC_VERB_SET_EAPD_BTLENABLE, 0x2}, /* default on */
1846         {0x16, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN|CONEXANT_HP_EVENT},
1847         {0x19, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN|CONEXANT_HP_EVENT},
1848         { } /* end */
1849 };
1850
1851 static struct hda_verb cxt5051_f700_init_verbs[] = {
1852         /* Line in, Mic */
1853         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x03},
1854         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1855         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x0},
1856         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x0},
1857         /* SPK  */
1858         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1859         {0x1a, AC_VERB_SET_CONNECT_SEL, 0x00},
1860         /* HP, Amp  */
1861         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1862         {0x16, AC_VERB_SET_CONNECT_SEL, 0x00},
1863         /* DAC1 */
1864         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1865         /* Record selector: Int mic */
1866         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1) | 0x44},
1867         {0x14, AC_VERB_SET_CONNECT_SEL, 0x1},
1868         /* SPDIF route: PCM */
1869         {0x1c, AC_VERB_SET_CONNECT_SEL, 0x0},
1870         /* EAPD */
1871         {0x1a, AC_VERB_SET_EAPD_BTLENABLE, 0x2}, /* default on */
1872         {0x16, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN|CONEXANT_HP_EVENT},
1873         { } /* end */
1874 };
1875
1876 static void cxt5051_init_mic_port(struct hda_codec *codec, hda_nid_t nid,
1877                                  unsigned int event)
1878 {
1879         snd_hda_codec_write(codec, nid, 0,
1880                             AC_VERB_SET_UNSOLICITED_ENABLE,
1881                             AC_USRSP_EN | event);
1882 #ifdef CONFIG_SND_HDA_INPUT_JACK
1883         conexant_add_jack(codec, nid, SND_JACK_MICROPHONE);
1884         conexant_report_jack(codec, nid);
1885 #endif
1886 }
1887
1888 /* initialize jack-sensing, too */
1889 static int cxt5051_init(struct hda_codec *codec)
1890 {
1891         struct conexant_spec *spec = codec->spec;
1892
1893         conexant_init(codec);
1894         conexant_init_jacks(codec);
1895
1896         if (spec->auto_mic & AUTO_MIC_PORTB)
1897                 cxt5051_init_mic_port(codec, 0x17, CXT5051_PORTB_EVENT);
1898         if (spec->auto_mic & AUTO_MIC_PORTC)
1899                 cxt5051_init_mic_port(codec, 0x18, CXT5051_PORTC_EVENT);
1900
1901         if (codec->patch_ops.unsol_event) {
1902                 cxt5051_hp_automute(codec);
1903                 cxt5051_portb_automic(codec);
1904                 cxt5051_portc_automic(codec);
1905         }
1906         return 0;
1907 }
1908
1909
1910 enum {
1911         CXT5051_LAPTOP,  /* Laptops w/ EAPD support */
1912         CXT5051_HP,     /* no docking */
1913         CXT5051_HP_DV6736,      /* HP without mic switch */
1914         CXT5051_LENOVO_X200,    /* Lenovo X200 laptop */
1915         CXT5051_F700,       /* HP Compaq Presario F700 */
1916         CXT5051_TOSHIBA,        /* Toshiba M300 & co */
1917         CXT5051_MODELS
1918 };
1919
1920 static const char *cxt5051_models[CXT5051_MODELS] = {
1921         [CXT5051_LAPTOP]        = "laptop",
1922         [CXT5051_HP]            = "hp",
1923         [CXT5051_HP_DV6736]     = "hp-dv6736",
1924         [CXT5051_LENOVO_X200]   = "lenovo-x200",
1925         [CXT5051_F700]          = "hp-700",
1926         [CXT5051_TOSHIBA]       = "toshiba",
1927 };
1928
1929 static struct snd_pci_quirk cxt5051_cfg_tbl[] = {
1930         SND_PCI_QUIRK(0x103c, 0x30cf, "HP DV6736", CXT5051_HP_DV6736),
1931         SND_PCI_QUIRK(0x103c, 0x360b, "Compaq Presario CQ60", CXT5051_HP),
1932         SND_PCI_QUIRK(0x103c, 0x30ea, "Compaq Presario F700", CXT5051_F700),
1933         SND_PCI_QUIRK(0x1179, 0xff50, "Toshiba M30x", CXT5051_TOSHIBA),
1934         SND_PCI_QUIRK(0x14f1, 0x0101, "Conexant Reference board",
1935                       CXT5051_LAPTOP),
1936         SND_PCI_QUIRK(0x14f1, 0x5051, "HP Spartan 1.1", CXT5051_HP),
1937         SND_PCI_QUIRK(0x17aa, 0x20f2, "Lenovo X200", CXT5051_LENOVO_X200),
1938         {}
1939 };
1940
1941 static int patch_cxt5051(struct hda_codec *codec)
1942 {
1943         struct conexant_spec *spec;
1944         int board_config;
1945
1946         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1947         if (!spec)
1948                 return -ENOMEM;
1949         codec->spec = spec;
1950         codec->pin_amp_workaround = 1;
1951
1952         codec->patch_ops = conexant_patch_ops;
1953         codec->patch_ops.init = cxt5051_init;
1954
1955         spec->multiout.max_channels = 2;
1956         spec->multiout.num_dacs = ARRAY_SIZE(cxt5051_dac_nids);
1957         spec->multiout.dac_nids = cxt5051_dac_nids;
1958         spec->multiout.dig_out_nid = CXT5051_SPDIF_OUT;
1959         spec->num_adc_nids = 1; /* not 2; via auto-mic switch */
1960         spec->adc_nids = cxt5051_adc_nids;
1961         spec->num_mixers = 2;
1962         spec->mixers[0] = cxt5051_capture_mixers;
1963         spec->mixers[1] = cxt5051_playback_mixers;
1964         spec->num_init_verbs = 1;
1965         spec->init_verbs[0] = cxt5051_init_verbs;
1966         spec->spdif_route = 0;
1967         spec->num_channel_mode = ARRAY_SIZE(cxt5051_modes);
1968         spec->channel_mode = cxt5051_modes;
1969         spec->cur_adc = 0;
1970         spec->cur_adc_idx = 0;
1971
1972         codec->patch_ops.unsol_event = cxt5051_hp_unsol_event;
1973
1974         board_config = snd_hda_check_board_config(codec, CXT5051_MODELS,
1975                                                   cxt5051_models,
1976                                                   cxt5051_cfg_tbl);
1977         spec->auto_mic = AUTO_MIC_PORTB | AUTO_MIC_PORTC;
1978         switch (board_config) {
1979         case CXT5051_HP:
1980                 spec->mixers[0] = cxt5051_hp_mixers;
1981                 break;
1982         case CXT5051_HP_DV6736:
1983                 spec->init_verbs[0] = cxt5051_hp_dv6736_init_verbs;
1984                 spec->mixers[0] = cxt5051_hp_dv6736_mixers;
1985                 spec->auto_mic = 0;
1986                 break;
1987         case CXT5051_LENOVO_X200:
1988                 spec->init_verbs[0] = cxt5051_lenovo_x200_init_verbs;
1989                 break;
1990         case CXT5051_F700:
1991                 spec->init_verbs[0] = cxt5051_f700_init_verbs;
1992                 spec->mixers[0] = cxt5051_f700_mixers;
1993                 spec->auto_mic = 0;
1994                 break;
1995         case CXT5051_TOSHIBA:
1996                 spec->mixers[0] = cxt5051_toshiba_mixers;
1997                 spec->auto_mic = AUTO_MIC_PORTB;
1998                 break;
1999         }
2000
2001         return 0;
2002 }
2003
2004 /* Conexant 5066 specific */
2005
2006 static hda_nid_t cxt5066_dac_nids[1] = { 0x10 };
2007 static hda_nid_t cxt5066_adc_nids[3] = { 0x14, 0x15, 0x16 };
2008 static hda_nid_t cxt5066_capsrc_nids[1] = { 0x17 };
2009 #define CXT5066_SPDIF_OUT       0x21
2010
2011 /* OLPC's microphone port is DC coupled for use with external sensors,
2012  * therefore we use a 50% mic bias in order to center the input signal with
2013  * the DC input range of the codec. */
2014 #define CXT5066_OLPC_EXT_MIC_BIAS PIN_VREF50
2015
2016 static struct hda_channel_mode cxt5066_modes[1] = {
2017         { 2, NULL },
2018 };
2019
2020 static void cxt5066_update_speaker(struct hda_codec *codec)
2021 {
2022         struct conexant_spec *spec = codec->spec;
2023         unsigned int pinctl;
2024
2025         snd_printdd("CXT5066: update speaker, hp_present=%d\n",
2026                 spec->hp_present);
2027
2028         /* Port A (HP) */
2029         pinctl = ((spec->hp_present & 1) && spec->cur_eapd) ? PIN_HP : 0;
2030         snd_hda_codec_write(codec, 0x19, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
2031                         pinctl);
2032
2033         /* Port D (HP/LO) */
2034         pinctl = ((spec->hp_present & 2) && spec->cur_eapd)
2035                 ? spec->port_d_mode : 0;
2036         /* Mute if Port A is connected on Thinkpad */
2037         if (spec->thinkpad && (spec->hp_present & 1))
2038                 pinctl = 0;
2039         snd_hda_codec_write(codec, 0x1c, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
2040                         pinctl);
2041
2042         /* CLASS_D AMP */
2043         pinctl = (!spec->hp_present && spec->cur_eapd) ? PIN_OUT : 0;
2044         snd_hda_codec_write(codec, 0x1f, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
2045                         pinctl);
2046
2047         if (spec->dell_automute) {
2048                 /* DELL AIO Port Rule: PortA > PortD > IntSpk */
2049                 pinctl = (!(spec->hp_present & 1) && spec->cur_eapd)
2050                         ? PIN_OUT : 0;
2051                 snd_hda_codec_write(codec, 0x1c, 0,
2052                         AC_VERB_SET_PIN_WIDGET_CONTROL, pinctl);
2053         }
2054 }
2055
2056 /* turn on/off EAPD (+ mute HP) as a master switch */
2057 static int cxt5066_hp_master_sw_put(struct snd_kcontrol *kcontrol,
2058                                     struct snd_ctl_elem_value *ucontrol)
2059 {
2060         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2061
2062         if (!cxt_eapd_put(kcontrol, ucontrol))
2063                 return 0;
2064
2065         cxt5066_update_speaker(codec);
2066         return 1;
2067 }
2068
2069 static const struct hda_input_mux cxt5066_olpc_dc_bias = {
2070         .num_items = 3,
2071         .items = {
2072                 { "Off", PIN_IN },
2073                 { "50%", PIN_VREF50 },
2074                 { "80%", PIN_VREF80 },
2075         },
2076 };
2077
2078 static int cxt5066_set_olpc_dc_bias(struct hda_codec *codec)
2079 {
2080         struct conexant_spec *spec = codec->spec;
2081         /* Even though port F is the DC input, the bias is controlled on port B.
2082          * we also leave that port as an active input (but unselected) in DC mode
2083          * just in case that is necessary to make the bias setting take effect. */
2084         return snd_hda_codec_write_cache(codec, 0x1a, 0,
2085                 AC_VERB_SET_PIN_WIDGET_CONTROL,
2086                 cxt5066_olpc_dc_bias.items[spec->dc_input_bias].index);
2087 }
2088
2089 /* OLPC defers mic widget control until when capture is started because the
2090  * microphone LED comes on as soon as these settings are put in place. if we
2091  * did this before recording, it would give the false indication that recording
2092  * is happening when it is not. */
2093 static void cxt5066_olpc_select_mic(struct hda_codec *codec)
2094 {
2095         struct conexant_spec *spec = codec->spec;
2096         if (!spec->recording)
2097                 return;
2098
2099         if (spec->dc_enable) {
2100                 /* in DC mode we ignore presence detection and just use the jack
2101                  * through our special DC port */
2102                 const struct hda_verb enable_dc_mode[] = {
2103                         /* disble internal mic, port C */
2104                         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2105
2106                         /* enable DC capture, port F */
2107                         {0x1e, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
2108                         {},
2109                 };
2110
2111                 snd_hda_sequence_write(codec, enable_dc_mode);
2112                 /* port B input disabled (and bias set) through the following call */
2113                 cxt5066_set_olpc_dc_bias(codec);
2114                 return;
2115         }
2116
2117         /* disable DC (port F) */
2118         snd_hda_codec_write(codec, 0x1e, 0, AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
2119
2120         /* external mic, port B */
2121         snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
2122                 spec->ext_mic_present ? CXT5066_OLPC_EXT_MIC_BIAS : 0);
2123
2124         /* internal mic, port C */
2125         snd_hda_codec_write(codec, 0x1b, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
2126                 spec->ext_mic_present ? 0 : PIN_VREF80);
2127 }
2128
2129 /* toggle input of built-in and mic jack appropriately */
2130 static void cxt5066_olpc_automic(struct hda_codec *codec)
2131 {
2132         struct conexant_spec *spec = codec->spec;
2133         unsigned int present;
2134
2135         if (spec->dc_enable) /* don't do presence detection in DC mode */
2136                 return;
2137
2138         present = snd_hda_codec_read(codec, 0x1a, 0,
2139                                      AC_VERB_GET_PIN_SENSE, 0) & 0x80000000;
2140         if (present)
2141                 snd_printdd("CXT5066: external microphone detected\n");
2142         else
2143                 snd_printdd("CXT5066: external microphone absent\n");
2144
2145         snd_hda_codec_write(codec, 0x17, 0, AC_VERB_SET_CONNECT_SEL,
2146                 present ? 0 : 1);
2147         spec->ext_mic_present = !!present;
2148
2149         cxt5066_olpc_select_mic(codec);
2150 }
2151
2152 /* toggle input of built-in digital mic and mic jack appropriately */
2153 static void cxt5066_vostro_automic(struct hda_codec *codec)
2154 {
2155         unsigned int present;
2156
2157         struct hda_verb ext_mic_present[] = {
2158                 /* enable external mic, port B */
2159                 {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
2160
2161                 /* switch to external mic input */
2162                 {0x17, AC_VERB_SET_CONNECT_SEL, 0},
2163                 {0x14, AC_VERB_SET_CONNECT_SEL, 0},
2164
2165                 /* disable internal digital mic */
2166                 {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2167                 {}
2168         };
2169         static struct hda_verb ext_mic_absent[] = {
2170                 /* enable internal mic, port C */
2171                 {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
2172
2173                 /* switch to internal mic input */
2174                 {0x14, AC_VERB_SET_CONNECT_SEL, 2},
2175
2176                 /* disable external mic, port B */
2177                 {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2178                 {}
2179         };
2180
2181         present = snd_hda_jack_detect(codec, 0x1a);
2182         if (present) {
2183                 snd_printdd("CXT5066: external microphone detected\n");
2184                 snd_hda_sequence_write(codec, ext_mic_present);
2185         } else {
2186                 snd_printdd("CXT5066: external microphone absent\n");
2187                 snd_hda_sequence_write(codec, ext_mic_absent);
2188         }
2189 }
2190
2191 /* toggle input of built-in digital mic and mic jack appropriately */
2192 static void cxt5066_ideapad_automic(struct hda_codec *codec)
2193 {
2194         unsigned int present;
2195
2196         struct hda_verb ext_mic_present[] = {
2197                 {0x14, AC_VERB_SET_CONNECT_SEL, 0},
2198                 {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
2199                 {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2200                 {}
2201         };
2202         static struct hda_verb ext_mic_absent[] = {
2203                 {0x14, AC_VERB_SET_CONNECT_SEL, 2},
2204                 {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
2205                 {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2206                 {}
2207         };
2208
2209         present = snd_hda_jack_detect(codec, 0x1b);
2210         if (present) {
2211                 snd_printdd("CXT5066: external microphone detected\n");
2212                 snd_hda_sequence_write(codec, ext_mic_present);
2213         } else {
2214                 snd_printdd("CXT5066: external microphone absent\n");
2215                 snd_hda_sequence_write(codec, ext_mic_absent);
2216         }
2217 }
2218
2219 /* toggle input of built-in digital mic and mic jack appropriately
2220    order is: external mic -> dock mic -> interal mic */
2221 static void cxt5066_thinkpad_automic(struct hda_codec *codec)
2222 {
2223         unsigned int ext_present, dock_present;
2224
2225         static struct hda_verb ext_mic_present[] = {
2226                 {0x14, AC_VERB_SET_CONNECT_SEL, 0},
2227                 {0x17, AC_VERB_SET_CONNECT_SEL, 1},
2228                 {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
2229                 {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2230                 {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2231                 {}
2232         };
2233         static struct hda_verb dock_mic_present[] = {
2234                 {0x14, AC_VERB_SET_CONNECT_SEL, 0},
2235                 {0x17, AC_VERB_SET_CONNECT_SEL, 0},
2236                 {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
2237                 {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2238                 {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2239                 {}
2240         };
2241         static struct hda_verb ext_mic_absent[] = {
2242                 {0x14, AC_VERB_SET_CONNECT_SEL, 2},
2243                 {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
2244                 {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2245                 {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2246                 {}
2247         };
2248
2249         ext_present = snd_hda_jack_detect(codec, 0x1b);
2250         dock_present = snd_hda_jack_detect(codec, 0x1a);
2251         if (ext_present) {
2252                 snd_printdd("CXT5066: external microphone detected\n");
2253                 snd_hda_sequence_write(codec, ext_mic_present);
2254         } else if (dock_present) {
2255                 snd_printdd("CXT5066: dock microphone detected\n");
2256                 snd_hda_sequence_write(codec, dock_mic_present);
2257         } else {
2258                 snd_printdd("CXT5066: external microphone absent\n");
2259                 snd_hda_sequence_write(codec, ext_mic_absent);
2260         }
2261 }
2262
2263 /* mute internal speaker if HP is plugged */
2264 static void cxt5066_hp_automute(struct hda_codec *codec)
2265 {
2266         struct conexant_spec *spec = codec->spec;
2267         unsigned int portA, portD;
2268
2269         /* Port A */
2270         portA = snd_hda_jack_detect(codec, 0x19);
2271
2272         /* Port D */
2273         portD = snd_hda_jack_detect(codec, 0x1c);
2274
2275         spec->hp_present = !!(portA);
2276         spec->hp_present |= portD ? 2 : 0;
2277         snd_printdd("CXT5066: hp automute portA=%x portD=%x present=%d\n",
2278                 portA, portD, spec->hp_present);
2279         cxt5066_update_speaker(codec);
2280 }
2281
2282 /* unsolicited event for jack sensing */
2283 static void cxt5066_olpc_unsol_event(struct hda_codec *codec, unsigned int res)
2284 {
2285         struct conexant_spec *spec = codec->spec;
2286         snd_printdd("CXT5066: unsol event %x (%x)\n", res, res >> 26);
2287         switch (res >> 26) {
2288         case CONEXANT_HP_EVENT:
2289                 cxt5066_hp_automute(codec);
2290                 break;
2291         case CONEXANT_MIC_EVENT:
2292                 /* ignore mic events in DC mode; we're always using the jack */
2293                 if (!spec->dc_enable)
2294                         cxt5066_olpc_automic(codec);
2295                 break;
2296         }
2297 }
2298
2299 /* unsolicited event for jack sensing */
2300 static void cxt5066_vostro_event(struct hda_codec *codec, unsigned int res)
2301 {
2302         snd_printdd("CXT5066_vostro: unsol event %x (%x)\n", res, res >> 26);
2303         switch (res >> 26) {
2304         case CONEXANT_HP_EVENT:
2305                 cxt5066_hp_automute(codec);
2306                 break;
2307         case CONEXANT_MIC_EVENT:
2308                 cxt5066_vostro_automic(codec);
2309                 break;
2310         }
2311 }
2312
2313 /* unsolicited event for jack sensing */
2314 static void cxt5066_ideapad_event(struct hda_codec *codec, unsigned int res)
2315 {
2316         snd_printdd("CXT5066_ideapad: unsol event %x (%x)\n", res, res >> 26);
2317         switch (res >> 26) {
2318         case CONEXANT_HP_EVENT:
2319                 cxt5066_hp_automute(codec);
2320                 break;
2321         case CONEXANT_MIC_EVENT:
2322                 cxt5066_ideapad_automic(codec);
2323                 break;
2324         }
2325 }
2326
2327 /* unsolicited event for jack sensing */
2328 static void cxt5066_thinkpad_event(struct hda_codec *codec, unsigned int res)
2329 {
2330         snd_printdd("CXT5066_thinkpad: unsol event %x (%x)\n", res, res >> 26);
2331         switch (res >> 26) {
2332         case CONEXANT_HP_EVENT:
2333                 cxt5066_hp_automute(codec);
2334                 break;
2335         case CONEXANT_MIC_EVENT:
2336                 cxt5066_thinkpad_automic(codec);
2337                 break;
2338         }
2339 }
2340
2341 static const struct hda_input_mux cxt5066_analog_mic_boost = {
2342         .num_items = 5,
2343         .items = {
2344                 { "0dB",  0 },
2345                 { "10dB", 1 },
2346                 { "20dB", 2 },
2347                 { "30dB", 3 },
2348                 { "40dB", 4 },
2349         },
2350 };
2351
2352 static void cxt5066_set_mic_boost(struct hda_codec *codec)
2353 {
2354         struct conexant_spec *spec = codec->spec;
2355         snd_hda_codec_write_cache(codec, 0x17, 0,
2356                 AC_VERB_SET_AMP_GAIN_MUTE,
2357                 AC_AMP_SET_RIGHT | AC_AMP_SET_LEFT | AC_AMP_SET_OUTPUT |
2358                         cxt5066_analog_mic_boost.items[spec->mic_boost].index);
2359         if (spec->ideapad || spec->thinkpad) {
2360                 /* adjust the internal mic as well...it is not through 0x17 */
2361                 snd_hda_codec_write_cache(codec, 0x23, 0,
2362                         AC_VERB_SET_AMP_GAIN_MUTE,
2363                         AC_AMP_SET_RIGHT | AC_AMP_SET_LEFT | AC_AMP_SET_INPUT |
2364                                 cxt5066_analog_mic_boost.
2365                                         items[spec->mic_boost].index);
2366         }
2367 }
2368
2369 static int cxt5066_mic_boost_mux_enum_info(struct snd_kcontrol *kcontrol,
2370                                            struct snd_ctl_elem_info *uinfo)
2371 {
2372         return snd_hda_input_mux_info(&cxt5066_analog_mic_boost, uinfo);
2373 }
2374
2375 static int cxt5066_mic_boost_mux_enum_get(struct snd_kcontrol *kcontrol,
2376                                           struct snd_ctl_elem_value *ucontrol)
2377 {
2378         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2379         struct conexant_spec *spec = codec->spec;
2380         ucontrol->value.enumerated.item[0] = spec->mic_boost;
2381         return 0;
2382 }
2383
2384 static int cxt5066_mic_boost_mux_enum_put(struct snd_kcontrol *kcontrol,
2385                                           struct snd_ctl_elem_value *ucontrol)
2386 {
2387         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2388         struct conexant_spec *spec = codec->spec;
2389         const struct hda_input_mux *imux = &cxt5066_analog_mic_boost;
2390         unsigned int idx;
2391         idx = ucontrol->value.enumerated.item[0];
2392         if (idx >= imux->num_items)
2393                 idx = imux->num_items - 1;
2394
2395         spec->mic_boost = idx;
2396         if (!spec->dc_enable)
2397                 cxt5066_set_mic_boost(codec);
2398         return 1;
2399 }
2400
2401 static void cxt5066_enable_dc(struct hda_codec *codec)
2402 {
2403         const struct hda_verb enable_dc_mode[] = {
2404                 /* disable gain */
2405                 {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2406
2407                 /* switch to DC input */
2408                 {0x17, AC_VERB_SET_CONNECT_SEL, 3},
2409                 {}
2410         };
2411
2412         /* configure as input source */
2413         snd_hda_sequence_write(codec, enable_dc_mode);
2414         cxt5066_olpc_select_mic(codec); /* also sets configured bias */
2415 }
2416
2417 static void cxt5066_disable_dc(struct hda_codec *codec)
2418 {
2419         /* reconfigure input source */
2420         cxt5066_set_mic_boost(codec);
2421         /* automic also selects the right mic if we're recording */
2422         cxt5066_olpc_automic(codec);
2423 }
2424
2425 static int cxt5066_olpc_dc_get(struct snd_kcontrol *kcontrol,
2426                              struct snd_ctl_elem_value *ucontrol)
2427 {
2428         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2429         struct conexant_spec *spec = codec->spec;
2430         ucontrol->value.integer.value[0] = spec->dc_enable;
2431         return 0;
2432 }
2433
2434 static int cxt5066_olpc_dc_put(struct snd_kcontrol *kcontrol,
2435                              struct snd_ctl_elem_value *ucontrol)
2436 {
2437         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2438         struct conexant_spec *spec = codec->spec;
2439         int dc_enable = !!ucontrol->value.integer.value[0];
2440
2441         if (dc_enable == spec->dc_enable)
2442                 return 0;
2443
2444         spec->dc_enable = dc_enable;
2445         if (dc_enable)
2446                 cxt5066_enable_dc(codec);
2447         else
2448                 cxt5066_disable_dc(codec);
2449
2450         return 1;
2451 }
2452
2453 static int cxt5066_olpc_dc_bias_enum_info(struct snd_kcontrol *kcontrol,
2454                                            struct snd_ctl_elem_info *uinfo)
2455 {
2456         return snd_hda_input_mux_info(&cxt5066_olpc_dc_bias, uinfo);
2457 }
2458
2459 static int cxt5066_olpc_dc_bias_enum_get(struct snd_kcontrol *kcontrol,
2460                                           struct snd_ctl_elem_value *ucontrol)
2461 {
2462         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2463         struct conexant_spec *spec = codec->spec;
2464         ucontrol->value.enumerated.item[0] = spec->dc_input_bias;
2465         return 0;
2466 }
2467
2468 static int cxt5066_olpc_dc_bias_enum_put(struct snd_kcontrol *kcontrol,
2469                                           struct snd_ctl_elem_value *ucontrol)
2470 {
2471         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2472         struct conexant_spec *spec = codec->spec;
2473         const struct hda_input_mux *imux = &cxt5066_analog_mic_boost;
2474         unsigned int idx;
2475
2476         idx = ucontrol->value.enumerated.item[0];
2477         if (idx >= imux->num_items)
2478                 idx = imux->num_items - 1;
2479
2480         spec->dc_input_bias = idx;
2481         if (spec->dc_enable)
2482                 cxt5066_set_olpc_dc_bias(codec);
2483         return 1;
2484 }
2485
2486 static void cxt5066_olpc_capture_prepare(struct hda_codec *codec)
2487 {
2488         struct conexant_spec *spec = codec->spec;
2489         /* mark as recording and configure the microphone widget so that the
2490          * recording LED comes on. */
2491         spec->recording = 1;
2492         cxt5066_olpc_select_mic(codec);
2493 }
2494
2495 static void cxt5066_olpc_capture_cleanup(struct hda_codec *codec)
2496 {
2497         struct conexant_spec *spec = codec->spec;
2498         const struct hda_verb disable_mics[] = {
2499                 /* disable external mic, port B */
2500                 {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2501
2502                 /* disble internal mic, port C */
2503                 {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2504
2505                 /* disable DC capture, port F */
2506                 {0x1e, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2507                 {},
2508         };
2509
2510         snd_hda_sequence_write(codec, disable_mics);
2511         spec->recording = 0;
2512 }
2513
2514 static struct hda_input_mux cxt5066_capture_source = {
2515         .num_items = 4,
2516         .items = {
2517                 { "Mic B", 0 },
2518                 { "Mic C", 1 },
2519                 { "Mic E", 2 },
2520                 { "Mic F", 3 },
2521         },
2522 };
2523
2524 static struct hda_bind_ctls cxt5066_bind_capture_vol_others = {
2525         .ops = &snd_hda_bind_vol,
2526         .values = {
2527                 HDA_COMPOSE_AMP_VAL(0x14, 3, 0, HDA_INPUT),
2528                 HDA_COMPOSE_AMP_VAL(0x14, 3, 2, HDA_INPUT),
2529                 0
2530         },
2531 };
2532
2533 static struct hda_bind_ctls cxt5066_bind_capture_sw_others = {
2534         .ops = &snd_hda_bind_sw,
2535         .values = {
2536                 HDA_COMPOSE_AMP_VAL(0x14, 3, 0, HDA_INPUT),
2537                 HDA_COMPOSE_AMP_VAL(0x14, 3, 2, HDA_INPUT),
2538                 0
2539         },
2540 };
2541
2542 static struct snd_kcontrol_new cxt5066_mixer_master[] = {
2543         HDA_CODEC_VOLUME("Master Playback Volume", 0x10, 0x00, HDA_OUTPUT),
2544         {}
2545 };
2546
2547 static struct snd_kcontrol_new cxt5066_mixer_master_olpc[] = {
2548         {
2549                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2550                 .name = "Master Playback Volume",
2551                 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
2552                                   SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2553                                   SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK,
2554                 .subdevice = HDA_SUBDEV_AMP_FLAG,
2555                 .info = snd_hda_mixer_amp_volume_info,
2556                 .get = snd_hda_mixer_amp_volume_get,
2557                 .put = snd_hda_mixer_amp_volume_put,
2558                 .tlv = { .c = snd_hda_mixer_amp_tlv },
2559                 /* offset by 28 volume steps to limit minimum gain to -46dB */
2560                 .private_value =
2561                         HDA_COMPOSE_AMP_VAL_OFS(0x10, 3, 0, HDA_OUTPUT, 28),
2562         },
2563         {}
2564 };
2565
2566 static struct snd_kcontrol_new cxt5066_mixer_olpc_dc[] = {
2567         {
2568                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2569                 .name = "DC Mode Enable Switch",
2570                 .info = snd_ctl_boolean_mono_info,
2571                 .get = cxt5066_olpc_dc_get,
2572                 .put = cxt5066_olpc_dc_put,
2573         },
2574         {
2575                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2576                 .name = "DC Input Bias Enum",
2577                 .info = cxt5066_olpc_dc_bias_enum_info,
2578                 .get = cxt5066_olpc_dc_bias_enum_get,
2579                 .put = cxt5066_olpc_dc_bias_enum_put,
2580         },
2581         {}
2582 };
2583
2584 static struct snd_kcontrol_new cxt5066_mixers[] = {
2585         {
2586                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2587                 .name = "Master Playback Switch",
2588                 .info = cxt_eapd_info,
2589                 .get = cxt_eapd_get,
2590                 .put = cxt5066_hp_master_sw_put,
2591                 .private_value = 0x1d,
2592         },
2593
2594         {
2595                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2596                 .name = "Analog Mic Boost Capture Enum",
2597                 .info = cxt5066_mic_boost_mux_enum_info,
2598                 .get = cxt5066_mic_boost_mux_enum_get,
2599                 .put = cxt5066_mic_boost_mux_enum_put,
2600         },
2601
2602         HDA_BIND_VOL("Capture Volume", &cxt5066_bind_capture_vol_others),
2603         HDA_BIND_SW("Capture Switch", &cxt5066_bind_capture_sw_others),
2604         {}
2605 };
2606
2607 static struct snd_kcontrol_new cxt5066_vostro_mixers[] = {
2608         {
2609                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2610                 .name = "Int Mic Boost Capture Enum",
2611                 .info = cxt5066_mic_boost_mux_enum_info,
2612                 .get = cxt5066_mic_boost_mux_enum_get,
2613                 .put = cxt5066_mic_boost_mux_enum_put,
2614                 .private_value = 0x23 | 0x100,
2615         },
2616         HDA_CODEC_VOLUME_MONO("Beep Playback Volume", 0x13, 1, 0x0, HDA_OUTPUT),
2617         {}
2618 };
2619
2620 static struct hda_verb cxt5066_init_verbs[] = {
2621         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80}, /* Port B */
2622         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80}, /* Port C */
2623         {0x1e, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN}, /* Port F */
2624         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN}, /* Port E */
2625
2626         /* Speakers  */
2627         {0x1f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2628         {0x1f, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2629
2630         /* HP, Amp  */
2631         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
2632         {0x19, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2633
2634         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
2635         {0x1c, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2636
2637         /* DAC1 */
2638         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2639
2640         /* Node 14 connections: 0x17 0x18 0x23 0x24 0x27 */
2641         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x50},
2642         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2643         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2) | 0x50},
2644         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2645         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
2646
2647         /* no digital microphone support yet */
2648         {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2649
2650         /* Audio input selector */
2651         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x3},
2652
2653         /* SPDIF route: PCM */
2654         {0x20, AC_VERB_SET_CONNECT_SEL, 0x0},
2655         {0x22, AC_VERB_SET_CONNECT_SEL, 0x0},
2656
2657         {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2658         {0x22, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2659
2660         /* EAPD */
2661         {0x1d, AC_VERB_SET_EAPD_BTLENABLE, 0x2}, /* default on */
2662
2663         /* not handling these yet */
2664         {0x19, AC_VERB_SET_UNSOLICITED_ENABLE, 0},
2665         {0x1a, AC_VERB_SET_UNSOLICITED_ENABLE, 0},
2666         {0x1b, AC_VERB_SET_UNSOLICITED_ENABLE, 0},
2667         {0x1c, AC_VERB_SET_UNSOLICITED_ENABLE, 0},
2668         {0x1d, AC_VERB_SET_UNSOLICITED_ENABLE, 0},
2669         {0x1e, AC_VERB_SET_UNSOLICITED_ENABLE, 0},
2670         {0x20, AC_VERB_SET_UNSOLICITED_ENABLE, 0},
2671         {0x22, AC_VERB_SET_UNSOLICITED_ENABLE, 0},
2672         { } /* end */
2673 };
2674
2675 static struct hda_verb cxt5066_init_verbs_olpc[] = {
2676         /* Port A: headphones */
2677         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
2678         {0x19, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2679
2680         /* Port B: external microphone */
2681         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2682
2683         /* Port C: internal microphone */
2684         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2685
2686         /* Port D: unused */
2687         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2688
2689         /* Port E: unused, but has primary EAPD */
2690         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2691         {0x1d, AC_VERB_SET_EAPD_BTLENABLE, 0x2}, /* default on */
2692
2693         /* Port F: external DC input through microphone port */
2694         {0x1e, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2695
2696         /* Port G: internal speakers */
2697         {0x1f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2698         {0x1f, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2699
2700         /* DAC1 */
2701         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2702
2703         /* DAC2: unused */
2704         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2705
2706         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x50},
2707         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2708         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
2709         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2710         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2711         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2712         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
2713         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2714         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2715         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2716         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
2717         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2718
2719         /* Disable digital microphone port */
2720         {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2721
2722         /* Audio input selectors */
2723         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x3},
2724         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE },
2725
2726         /* Disable SPDIF */
2727         {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2728         {0x22, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2729
2730         /* enable unsolicited events for Port A and B */
2731         {0x19, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_HP_EVENT},
2732         {0x1a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_MIC_EVENT},
2733         { } /* end */
2734 };
2735
2736 static struct hda_verb cxt5066_init_verbs_vostro[] = {
2737         /* Port A: headphones */
2738         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2739         {0x19, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2740
2741         /* Port B: external microphone */
2742         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2743
2744         /* Port C: unused */
2745         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2746
2747         /* Port D: unused */
2748         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2749
2750         /* Port E: unused, but has primary EAPD */
2751         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2752         {0x1d, AC_VERB_SET_EAPD_BTLENABLE, 0x2}, /* default on */
2753
2754         /* Port F: unused */
2755         {0x1e, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2756
2757         /* Port G: internal speakers */
2758         {0x1f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2759         {0x1f, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2760
2761         /* DAC1 */
2762         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2763
2764         /* DAC2: unused */
2765         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2766
2767         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2768         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2769         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
2770         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2771         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2772         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2773         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
2774         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2775         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2776         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2777         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
2778         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2779
2780         /* Digital microphone port */
2781         {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
2782
2783         /* Audio input selectors */
2784         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x3},
2785         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE },
2786
2787         /* Disable SPDIF */
2788         {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2789         {0x22, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2790
2791         /* enable unsolicited events for Port A and B */
2792         {0x19, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_HP_EVENT},
2793         {0x1a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_MIC_EVENT},
2794         { } /* end */
2795 };
2796
2797 static struct hda_verb cxt5066_init_verbs_ideapad[] = {
2798         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80}, /* Port B */
2799         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80}, /* Port C */
2800         {0x1e, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN}, /* Port F */
2801         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN}, /* Port E */
2802
2803         /* Speakers  */
2804         {0x1f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2805         {0x1f, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2806
2807         /* HP, Amp  */
2808         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
2809         {0x19, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2810
2811         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
2812         {0x1c, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2813
2814         /* DAC1 */
2815         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2816
2817         /* Node 14 connections: 0x17 0x18 0x23 0x24 0x27 */
2818         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x50},
2819         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2820         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2) | 0x50},
2821         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2822         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
2823         {0x14, AC_VERB_SET_CONNECT_SEL, 2},     /* default to internal mic */
2824
2825         /* Audio input selector */
2826         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x2},
2827         {0x17, AC_VERB_SET_CONNECT_SEL, 1},     /* route ext mic */
2828
2829         /* SPDIF route: PCM */
2830         {0x20, AC_VERB_SET_CONNECT_SEL, 0x0},
2831         {0x22, AC_VERB_SET_CONNECT_SEL, 0x0},
2832
2833         {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2834         {0x22, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2835
2836         /* internal microphone */
2837         {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN}, /* enable int mic */
2838
2839         /* EAPD */
2840         {0x1d, AC_VERB_SET_EAPD_BTLENABLE, 0x2}, /* default on */
2841
2842         {0x19, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_HP_EVENT},
2843         {0x1b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_MIC_EVENT},
2844         { } /* end */
2845 };
2846
2847 static struct hda_verb cxt5066_init_verbs_thinkpad[] = {
2848         {0x1e, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN}, /* Port F */
2849         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN}, /* Port E */
2850
2851         /* Port G: internal speakers  */
2852         {0x1f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2853         {0x1f, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2854
2855         /* Port A: HP, Amp  */
2856         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2857         {0x19, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2858
2859         /* Port B: Mic Dock */
2860         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2861
2862         /* Port C: Mic */
2863         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2864
2865         /* Port D: HP Dock, Amp */
2866         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2867         {0x1c, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2868
2869         /* DAC1 */
2870         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2871
2872         /* Node 14 connections: 0x17 0x18 0x23 0x24 0x27 */
2873         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x50},
2874         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2875         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2) | 0x50},
2876         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2877         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
2878         {0x14, AC_VERB_SET_CONNECT_SEL, 2},     /* default to internal mic */
2879
2880         /* Audio input selector */
2881         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x2},
2882         {0x17, AC_VERB_SET_CONNECT_SEL, 1},     /* route ext mic */
2883
2884         /* SPDIF route: PCM */
2885         {0x20, AC_VERB_SET_CONNECT_SEL, 0x0},
2886         {0x22, AC_VERB_SET_CONNECT_SEL, 0x0},
2887
2888         {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2889         {0x22, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2890
2891         /* internal microphone */
2892         {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN}, /* enable int mic */
2893
2894         /* EAPD */
2895         {0x1d, AC_VERB_SET_EAPD_BTLENABLE, 0x2}, /* default on */
2896
2897         /* enable unsolicited events for Port A, B, C and D */
2898         {0x19, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_HP_EVENT},
2899         {0x1c, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_HP_EVENT},
2900         {0x1a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_MIC_EVENT},
2901         {0x1b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_MIC_EVENT},
2902         { } /* end */
2903 };
2904
2905 static struct hda_verb cxt5066_init_verbs_portd_lo[] = {
2906         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2907         { } /* end */
2908 };
2909
2910 /* initialize jack-sensing, too */
2911 static int cxt5066_init(struct hda_codec *codec)
2912 {
2913         struct conexant_spec *spec = codec->spec;
2914
2915         snd_printdd("CXT5066: init\n");
2916         conexant_init(codec);
2917         if (codec->patch_ops.unsol_event) {
2918                 cxt5066_hp_automute(codec);
2919                 if (spec->dell_vostro)
2920                         cxt5066_vostro_automic(codec);
2921                 else if (spec->ideapad)
2922                         cxt5066_ideapad_automic(codec);
2923                 else if (spec->thinkpad)
2924                         cxt5066_thinkpad_automic(codec);
2925         }
2926         cxt5066_set_mic_boost(codec);
2927         return 0;
2928 }
2929
2930 static int cxt5066_olpc_init(struct hda_codec *codec)
2931 {
2932         struct conexant_spec *spec = codec->spec;
2933         snd_printdd("CXT5066: init\n");
2934         conexant_init(codec);
2935         cxt5066_hp_automute(codec);
2936         if (!spec->dc_enable) {
2937                 cxt5066_set_mic_boost(codec);
2938                 cxt5066_olpc_automic(codec);
2939         } else {
2940                 cxt5066_enable_dc(codec);
2941         }
2942         return 0;
2943 }
2944
2945 enum {
2946         CXT5066_LAPTOP,         /* Laptops w/ EAPD support */
2947         CXT5066_DELL_LAPTOP,    /* Dell Laptop */
2948         CXT5066_OLPC_XO_1_5,    /* OLPC XO 1.5 */
2949         CXT5066_DELL_VOSTO,     /* Dell Vostro 1015i */
2950         CXT5066_IDEAPAD,        /* Lenovo IdeaPad U150 */
2951         CXT5066_THINKPAD,       /* Lenovo ThinkPad T410s, others? */
2952         CXT5066_MODELS
2953 };
2954
2955 static const char *cxt5066_models[CXT5066_MODELS] = {
2956         [CXT5066_LAPTOP]        = "laptop",
2957         [CXT5066_DELL_LAPTOP]   = "dell-laptop",
2958         [CXT5066_OLPC_XO_1_5]   = "olpc-xo-1_5",
2959         [CXT5066_DELL_VOSTO]    = "dell-vostro",
2960         [CXT5066_IDEAPAD]       = "ideapad",
2961         [CXT5066_THINKPAD]      = "thinkpad",
2962 };
2963
2964 static struct snd_pci_quirk cxt5066_cfg_tbl[] = {
2965         SND_PCI_QUIRK(0x14f1, 0x0101, "Conexant Reference board",
2966                       CXT5066_LAPTOP),
2967         SND_PCI_QUIRK(0x1028, 0x02f5, "Dell",
2968                       CXT5066_DELL_LAPTOP),
2969         SND_PCI_QUIRK(0x152d, 0x0833, "OLPC XO-1.5", CXT5066_OLPC_XO_1_5),
2970         SND_PCI_QUIRK(0x1028, 0x0402, "Dell Vostro", CXT5066_DELL_VOSTO),
2971         SND_PCI_QUIRK(0x1028, 0x0408, "Dell Inspiron One 19T", CXT5066_IDEAPAD),
2972         SND_PCI_QUIRK(0x1179, 0xff50, "Toshiba Satellite P500-PSPGSC-01800T", CXT5066_OLPC_XO_1_5),
2973         SND_PCI_QUIRK(0x1179, 0xffe0, "Toshiba Satellite Pro T130-15F", CXT5066_OLPC_XO_1_5),
2974         SND_PCI_QUIRK(0x17aa, 0x3a0d, "ideapad", CXT5066_IDEAPAD),
2975         SND_PCI_QUIRK(0x17aa, 0x215e, "Lenovo Thinkpad", CXT5066_THINKPAD),
2976         {}
2977 };
2978
2979 static int patch_cxt5066(struct hda_codec *codec)
2980 {
2981         struct conexant_spec *spec;
2982         int board_config;
2983
2984         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
2985         if (!spec)
2986                 return -ENOMEM;
2987         codec->spec = spec;
2988
2989         codec->patch_ops = conexant_patch_ops;
2990         codec->patch_ops.init = conexant_init;
2991
2992         spec->dell_automute = 0;
2993         spec->multiout.max_channels = 2;
2994         spec->multiout.num_dacs = ARRAY_SIZE(cxt5066_dac_nids);
2995         spec->multiout.dac_nids = cxt5066_dac_nids;
2996         spec->multiout.dig_out_nid = CXT5066_SPDIF_OUT;
2997         spec->num_adc_nids = 1;
2998         spec->adc_nids = cxt5066_adc_nids;
2999         spec->capsrc_nids = cxt5066_capsrc_nids;
3000         spec->input_mux = &cxt5066_capture_source;
3001
3002         spec->port_d_mode = PIN_HP;
3003
3004         spec->num_init_verbs = 1;
3005         spec->init_verbs[0] = cxt5066_init_verbs;
3006         spec->num_channel_mode = ARRAY_SIZE(cxt5066_modes);
3007         spec->channel_mode = cxt5066_modes;
3008         spec->cur_adc = 0;
3009         spec->cur_adc_idx = 0;
3010
3011         board_config = snd_hda_check_board_config(codec, CXT5066_MODELS,
3012                                                   cxt5066_models, cxt5066_cfg_tbl);
3013         switch (board_config) {
3014         default:
3015         case CXT5066_LAPTOP:
3016                 spec->mixers[spec->num_mixers++] = cxt5066_mixer_master;
3017                 spec->mixers[spec->num_mixers++] = cxt5066_mixers;
3018                 break;
3019         case CXT5066_DELL_LAPTOP:
3020                 spec->mixers[spec->num_mixers++] = cxt5066_mixer_master;
3021                 spec->mixers[spec->num_mixers++] = cxt5066_mixers;
3022
3023                 spec->port_d_mode = PIN_OUT;
3024                 spec->init_verbs[spec->num_init_verbs] = cxt5066_init_verbs_portd_lo;
3025                 spec->num_init_verbs++;
3026                 spec->dell_automute = 1;
3027                 break;
3028         case CXT5066_OLPC_XO_1_5:
3029                 codec->patch_ops.init = cxt5066_olpc_init;
3030                 codec->patch_ops.unsol_event = cxt5066_olpc_unsol_event;
3031                 spec->init_verbs[0] = cxt5066_init_verbs_olpc;
3032                 spec->mixers[spec->num_mixers++] = cxt5066_mixer_master_olpc;
3033                 spec->mixers[spec->num_mixers++] = cxt5066_mixer_olpc_dc;
3034                 spec->mixers[spec->num_mixers++] = cxt5066_mixers;
3035                 spec->port_d_mode = 0;
3036                 spec->mic_boost = 3; /* default 30dB gain */
3037
3038                 /* no S/PDIF out */
3039                 spec->multiout.dig_out_nid = 0;
3040
3041                 /* input source automatically selected */
3042                 spec->input_mux = NULL;
3043
3044                 /* our capture hooks which allow us to turn on the microphone LED
3045                  * at the right time */
3046                 spec->capture_prepare = cxt5066_olpc_capture_prepare;
3047                 spec->capture_cleanup = cxt5066_olpc_capture_cleanup;
3048                 break;
3049         case CXT5066_DELL_VOSTO:
3050                 codec->patch_ops.init = cxt5066_init;
3051                 codec->patch_ops.unsol_event = cxt5066_vostro_event;
3052                 spec->init_verbs[0] = cxt5066_init_verbs_vostro;
3053                 spec->mixers[spec->num_mixers++] = cxt5066_mixer_master_olpc;
3054                 spec->mixers[spec->num_mixers++] = cxt5066_mixers;
3055                 spec->mixers[spec->num_mixers++] = cxt5066_vostro_mixers;
3056                 spec->port_d_mode = 0;
3057                 spec->dell_vostro = 1;
3058                 spec->mic_boost = 3; /* default 30dB gain */
3059                 snd_hda_attach_beep_device(codec, 0x13);
3060
3061                 /* no S/PDIF out */
3062                 spec->multiout.dig_out_nid = 0;
3063
3064                 /* input source automatically selected */
3065                 spec->input_mux = NULL;
3066                 break;
3067         case CXT5066_IDEAPAD:
3068                 codec->patch_ops.init = cxt5066_init;
3069                 codec->patch_ops.unsol_event = cxt5066_ideapad_event;
3070                 spec->mixers[spec->num_mixers++] = cxt5066_mixer_master;
3071                 spec->mixers[spec->num_mixers++] = cxt5066_mixers;
3072                 spec->init_verbs[0] = cxt5066_init_verbs_ideapad;
3073                 spec->port_d_mode = 0;
3074                 spec->ideapad = 1;
3075                 spec->mic_boost = 2;    /* default 20dB gain */
3076
3077                 /* no S/PDIF out */
3078                 spec->multiout.dig_out_nid = 0;
3079
3080                 /* input source automatically selected */
3081                 spec->input_mux = NULL;
3082                 break;
3083         case CXT5066_THINKPAD:
3084                 codec->patch_ops.init = cxt5066_init;
3085                 codec->patch_ops.unsol_event = cxt5066_thinkpad_event;
3086                 spec->mixers[spec->num_mixers++] = cxt5066_mixer_master;
3087                 spec->mixers[spec->num_mixers++] = cxt5066_mixers;
3088                 spec->init_verbs[0] = cxt5066_init_verbs_thinkpad;
3089                 spec->thinkpad = 1;
3090                 spec->port_d_mode = PIN_OUT;
3091                 spec->mic_boost = 2;    /* default 20dB gain */
3092
3093                 /* no S/PDIF out */
3094                 spec->multiout.dig_out_nid = 0;
3095
3096                 /* input source automatically selected */
3097                 spec->input_mux = NULL;
3098                 break;
3099         }
3100
3101         return 0;
3102 }
3103
3104 /*
3105  */
3106
3107 static struct hda_codec_preset snd_hda_preset_conexant[] = {
3108         { .id = 0x14f15045, .name = "CX20549 (Venice)",
3109           .patch = patch_cxt5045 },
3110         { .id = 0x14f15047, .name = "CX20551 (Waikiki)",
3111           .patch = patch_cxt5047 },
3112         { .id = 0x14f15051, .name = "CX20561 (Hermosa)",
3113           .patch = patch_cxt5051 },
3114         { .id = 0x14f15066, .name = "CX20582 (Pebble)",
3115           .patch = patch_cxt5066 },
3116         { .id = 0x14f15067, .name = "CX20583 (Pebble HSF)",
3117           .patch = patch_cxt5066 },
3118         { .id = 0x14f15069, .name = "CX20585",
3119           .patch = patch_cxt5066 },
3120         {} /* terminator */
3121 };
3122
3123 MODULE_ALIAS("snd-hda-codec-id:14f15045");
3124 MODULE_ALIAS("snd-hda-codec-id:14f15047");
3125 MODULE_ALIAS("snd-hda-codec-id:14f15051");
3126 MODULE_ALIAS("snd-hda-codec-id:14f15066");
3127 MODULE_ALIAS("snd-hda-codec-id:14f15067");
3128 MODULE_ALIAS("snd-hda-codec-id:14f15069");
3129
3130 MODULE_LICENSE("GPL");
3131 MODULE_DESCRIPTION("Conexant HD-audio codec");
3132
3133 static struct hda_codec_preset_list conexant_list = {
3134         .preset = snd_hda_preset_conexant,
3135         .owner = THIS_MODULE,
3136 };
3137
3138 static int __init patch_conexant_init(void)
3139 {
3140         return snd_hda_add_codec_preset(&conexant_list);
3141 }
3142
3143 static void __exit patch_conexant_exit(void)
3144 {
3145         snd_hda_delete_codec_preset(&conexant_list);
3146 }
3147
3148 module_init(patch_conexant_init)
3149 module_exit(patch_conexant_exit)