]> bbs.cooldavid.org Git - net-next-2.6.git/blob - sound/pci/hda/patch_conexant.c
54f74191ebca634a429b15450ba772294daadba7
[net-next-2.6.git] / sound / pci / hda / patch_conexant.c
1 /*
2  * HD audio interface patch for Conexant HDA audio codec
3  *
4  * Copyright (c) 2006 Pototskiy Akex <alex.pototskiy@gmail.com>
5  *                    Takashi Iwai <tiwai@suse.de>
6  *                    Tobin Davis  <tdavis@dsl-only.net>
7  *
8  *  This driver is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This driver is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21  */
22
23 #include <linux/init.h>
24 #include <linux/delay.h>
25 #include <linux/slab.h>
26 #include <linux/pci.h>
27 #include <sound/core.h>
28 #include <sound/jack.h>
29
30 #include "hda_codec.h"
31 #include "hda_local.h"
32 #include "hda_beep.h"
33
34 #define CXT_PIN_DIR_IN              0x00
35 #define CXT_PIN_DIR_OUT             0x01
36 #define CXT_PIN_DIR_INOUT           0x02
37 #define CXT_PIN_DIR_IN_NOMICBIAS    0x03
38 #define CXT_PIN_DIR_INOUT_NOMICBIAS 0x04
39
40 #define CONEXANT_HP_EVENT       0x37
41 #define CONEXANT_MIC_EVENT      0x38
42
43 /* Conexant 5051 specific */
44
45 #define CXT5051_SPDIF_OUT       0x12
46 #define CXT5051_PORTB_EVENT     0x38
47 #define CXT5051_PORTC_EVENT     0x39
48
49 #define AUTO_MIC_PORTB          (1 << 1)
50 #define AUTO_MIC_PORTC          (1 << 2)
51
52 struct conexant_jack {
53
54         hda_nid_t nid;
55         int type;
56         struct snd_jack *jack;
57
58 };
59
60 struct 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         case 0x17aa:
1202                 /* HP, Packard Bell, Fujitsu-Siemens & Lenovo laptops have
1203                  * really bad sound over 0dB on NID 0x17. Fix max PCM level to
1204                  * 0 dB (originally it has 0x2b steps with 0dB offset 0x14)
1205                  */
1206                 snd_hda_override_amp_caps(codec, 0x17, HDA_INPUT,
1207                                           (0x14 << AC_AMPCAP_OFFSET_SHIFT) |
1208                                           (0x14 << AC_AMPCAP_NUM_STEPS_SHIFT) |
1209                                           (0x05 << AC_AMPCAP_STEP_SIZE_SHIFT) |
1210                                           (1 << AC_AMPCAP_MUTE_SHIFT));
1211                 break;
1212         }
1213
1214         return 0;
1215 }
1216
1217
1218 /* Conexant 5047 specific */
1219 #define CXT5047_SPDIF_OUT       0x11
1220
1221 static hda_nid_t cxt5047_dac_nids[1] = { 0x10 }; /* 0x1c */
1222 static hda_nid_t cxt5047_adc_nids[1] = { 0x12 };
1223 static hda_nid_t cxt5047_capsrc_nids[1] = { 0x1a };
1224
1225 static struct hda_channel_mode cxt5047_modes[1] = {
1226         { 2, NULL },
1227 };
1228
1229 static struct hda_input_mux cxt5047_toshiba_capture_source = {
1230         .num_items = 2,
1231         .items = {
1232                 { "ExtMic", 0x2 },
1233                 { "Line-In", 0x1 },
1234         }
1235 };
1236
1237 /* turn on/off EAPD (+ mute HP) as a master switch */
1238 static int cxt5047_hp_master_sw_put(struct snd_kcontrol *kcontrol,
1239                                     struct snd_ctl_elem_value *ucontrol)
1240 {
1241         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1242         struct conexant_spec *spec = codec->spec;
1243         unsigned int bits;
1244
1245         if (!cxt_eapd_put(kcontrol, ucontrol))
1246                 return 0;
1247
1248         /* toggle internal speakers mute depending of presence of
1249          * the headphone jack
1250          */
1251         bits = (!spec->hp_present && spec->cur_eapd) ? 0 : HDA_AMP_MUTE;
1252         /* NOTE: Conexat codec needs the index for *OUTPUT* amp of
1253          * pin widgets unlike other codecs.  In this case, we need to
1254          * set index 0x01 for the volume from the mixer amp 0x19.
1255          */
1256         snd_hda_codec_amp_stereo(codec, 0x1d, HDA_OUTPUT, 0x01,
1257                                  HDA_AMP_MUTE, bits);
1258         bits = spec->cur_eapd ? 0 : HDA_AMP_MUTE;
1259         snd_hda_codec_amp_stereo(codec, 0x13, HDA_OUTPUT, 0,
1260                                  HDA_AMP_MUTE, bits);
1261         return 1;
1262 }
1263
1264 /* mute internal speaker if HP is plugged */
1265 static void cxt5047_hp_automute(struct hda_codec *codec)
1266 {
1267         struct conexant_spec *spec = codec->spec;
1268         unsigned int bits;
1269
1270         spec->hp_present = snd_hda_jack_detect(codec, 0x13);
1271
1272         bits = (spec->hp_present || !spec->cur_eapd) ? HDA_AMP_MUTE : 0;
1273         /* See the note in cxt5047_hp_master_sw_put */
1274         snd_hda_codec_amp_stereo(codec, 0x1d, HDA_OUTPUT, 0x01,
1275                                  HDA_AMP_MUTE, bits);
1276 }
1277
1278 /* toggle input of built-in and mic jack appropriately */
1279 static void cxt5047_hp_automic(struct hda_codec *codec)
1280 {
1281         static struct hda_verb mic_jack_on[] = {
1282                 {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1283                 {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1284                 {}
1285         };
1286         static struct hda_verb mic_jack_off[] = {
1287                 {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1288                 {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1289                 {}
1290         };
1291         unsigned int present;
1292
1293         present = snd_hda_jack_detect(codec, 0x15);
1294         if (present)
1295                 snd_hda_sequence_write(codec, mic_jack_on);
1296         else
1297                 snd_hda_sequence_write(codec, mic_jack_off);
1298 }
1299
1300 /* unsolicited event for HP jack sensing */
1301 static void cxt5047_hp_unsol_event(struct hda_codec *codec,
1302                                   unsigned int res)
1303 {
1304         switch (res >> 26) {
1305         case CONEXANT_HP_EVENT:
1306                 cxt5047_hp_automute(codec);
1307                 break;
1308         case CONEXANT_MIC_EVENT:
1309                 cxt5047_hp_automic(codec);
1310                 break;
1311         }
1312 }
1313
1314 static struct snd_kcontrol_new cxt5047_base_mixers[] = {
1315         HDA_CODEC_VOLUME("Mic Playback Volume", 0x19, 0x02, HDA_INPUT),
1316         HDA_CODEC_MUTE("Mic Playback Switch", 0x19, 0x02, HDA_INPUT),
1317         HDA_CODEC_VOLUME("Mic Boost", 0x1a, 0x0, HDA_OUTPUT),
1318         HDA_CODEC_VOLUME("Capture Volume", 0x12, 0x03, HDA_INPUT),
1319         HDA_CODEC_MUTE("Capture Switch", 0x12, 0x03, HDA_INPUT),
1320         HDA_CODEC_VOLUME("PCM Volume", 0x10, 0x00, HDA_OUTPUT),
1321         HDA_CODEC_MUTE("PCM Switch", 0x10, 0x00, HDA_OUTPUT),
1322         {
1323                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1324                 .name = "Master Playback Switch",
1325                 .info = cxt_eapd_info,
1326                 .get = cxt_eapd_get,
1327                 .put = cxt5047_hp_master_sw_put,
1328                 .private_value = 0x13,
1329         },
1330
1331         {}
1332 };
1333
1334 static struct snd_kcontrol_new cxt5047_hp_spk_mixers[] = {
1335         /* See the note in cxt5047_hp_master_sw_put */
1336         HDA_CODEC_VOLUME("Speaker Playback Volume", 0x1d, 0x01, HDA_OUTPUT),
1337         HDA_CODEC_VOLUME("Headphone Playback Volume", 0x13, 0x00, HDA_OUTPUT),
1338         {}
1339 };
1340
1341 static struct snd_kcontrol_new cxt5047_hp_only_mixers[] = {
1342         HDA_CODEC_VOLUME("Master Playback Volume", 0x13, 0x00, HDA_OUTPUT),
1343         { } /* end */
1344 };
1345
1346 static struct hda_verb cxt5047_init_verbs[] = {
1347         /* Line in, Mic, Built-in Mic */
1348         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
1349         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN|AC_PINCTL_VREF_50 },
1350         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN|AC_PINCTL_VREF_50 },
1351         /* HP, Speaker  */
1352         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
1353         {0x13, AC_VERB_SET_CONNECT_SEL, 0x0}, /* mixer(0x19) */
1354         {0x1d, AC_VERB_SET_CONNECT_SEL, 0x1}, /* mixer(0x19) */
1355         /* Record selector: Mic */
1356         {0x12, AC_VERB_SET_CONNECT_SEL,0x03},
1357         {0x19, AC_VERB_SET_AMP_GAIN_MUTE,
1358          AC_AMP_SET_INPUT|AC_AMP_SET_RIGHT|AC_AMP_SET_LEFT|0x17},
1359         {0x1A, AC_VERB_SET_CONNECT_SEL,0x02},
1360         {0x1A, AC_VERB_SET_AMP_GAIN_MUTE,
1361          AC_AMP_SET_OUTPUT|AC_AMP_SET_RIGHT|AC_AMP_SET_LEFT|0x00},
1362         {0x1A, AC_VERB_SET_AMP_GAIN_MUTE,
1363          AC_AMP_SET_OUTPUT|AC_AMP_SET_RIGHT|AC_AMP_SET_LEFT|0x03},
1364         /* SPDIF route: PCM */
1365         { 0x18, AC_VERB_SET_CONNECT_SEL, 0x0 },
1366         /* Enable unsolicited events */
1367         {0x13, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_HP_EVENT},
1368         {0x15, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_MIC_EVENT},
1369         { } /* end */
1370 };
1371
1372 /* configuration for Toshiba Laptops */
1373 static struct hda_verb cxt5047_toshiba_init_verbs[] = {
1374         {0x13, AC_VERB_SET_EAPD_BTLENABLE, 0x0}, /* default off */
1375         {}
1376 };
1377
1378 /* Test configuration for debugging, modelled after the ALC260 test
1379  * configuration.
1380  */
1381 #ifdef CONFIG_SND_DEBUG
1382 static struct hda_input_mux cxt5047_test_capture_source = {
1383         .num_items = 4,
1384         .items = {
1385                 { "LINE1 pin", 0x0 },
1386                 { "MIC1 pin", 0x1 },
1387                 { "MIC2 pin", 0x2 },
1388                 { "CD pin", 0x3 },
1389         },
1390 };
1391
1392 static struct snd_kcontrol_new cxt5047_test_mixer[] = {
1393
1394         /* Output only controls */
1395         HDA_CODEC_VOLUME("OutAmp-1 Volume", 0x10, 0x0, HDA_OUTPUT),
1396         HDA_CODEC_MUTE("OutAmp-1 Switch", 0x10,0x0, HDA_OUTPUT),
1397         HDA_CODEC_VOLUME("OutAmp-2 Volume", 0x1c, 0x0, HDA_OUTPUT),
1398         HDA_CODEC_MUTE("OutAmp-2 Switch", 0x1c, 0x0, HDA_OUTPUT),
1399         HDA_CODEC_VOLUME("Speaker Playback Volume", 0x1d, 0x0, HDA_OUTPUT),
1400         HDA_CODEC_MUTE("Speaker Playback Switch", 0x1d, 0x0, HDA_OUTPUT),
1401         HDA_CODEC_VOLUME("HeadPhone Playback Volume", 0x13, 0x0, HDA_OUTPUT),
1402         HDA_CODEC_MUTE("HeadPhone Playback Switch", 0x13, 0x0, HDA_OUTPUT),
1403         HDA_CODEC_VOLUME("Line1-Out Playback Volume", 0x14, 0x0, HDA_OUTPUT),
1404         HDA_CODEC_MUTE("Line1-Out Playback Switch", 0x14, 0x0, HDA_OUTPUT),
1405         HDA_CODEC_VOLUME("Line2-Out Playback Volume", 0x15, 0x0, HDA_OUTPUT),
1406         HDA_CODEC_MUTE("Line2-Out Playback Switch", 0x15, 0x0, HDA_OUTPUT),
1407
1408         /* Modes for retasking pin widgets */
1409         CXT_PIN_MODE("LINE1 pin mode", 0x14, CXT_PIN_DIR_INOUT),
1410         CXT_PIN_MODE("MIC1 pin mode", 0x15, CXT_PIN_DIR_INOUT),
1411
1412         /* EAPD Switch Control */
1413         CXT_EAPD_SWITCH("External Amplifier", 0x13, 0x0),
1414
1415         /* Loopback mixer controls */
1416         HDA_CODEC_VOLUME("MIC1 Playback Volume", 0x12, 0x01, HDA_INPUT),
1417         HDA_CODEC_MUTE("MIC1 Playback Switch", 0x12, 0x01, HDA_INPUT),
1418         HDA_CODEC_VOLUME("MIC2 Playback Volume", 0x12, 0x02, HDA_INPUT),
1419         HDA_CODEC_MUTE("MIC2 Playback Switch", 0x12, 0x02, HDA_INPUT),
1420         HDA_CODEC_VOLUME("LINE Playback Volume", 0x12, 0x0, HDA_INPUT),
1421         HDA_CODEC_MUTE("LINE Playback Switch", 0x12, 0x0, HDA_INPUT),
1422         HDA_CODEC_VOLUME("CD Playback Volume", 0x12, 0x04, HDA_INPUT),
1423         HDA_CODEC_MUTE("CD Playback Switch", 0x12, 0x04, HDA_INPUT),
1424
1425         HDA_CODEC_VOLUME("Capture-1 Volume", 0x19, 0x0, HDA_INPUT),
1426         HDA_CODEC_MUTE("Capture-1 Switch", 0x19, 0x0, HDA_INPUT),
1427         HDA_CODEC_VOLUME("Capture-2 Volume", 0x19, 0x1, HDA_INPUT),
1428         HDA_CODEC_MUTE("Capture-2 Switch", 0x19, 0x1, HDA_INPUT),
1429         HDA_CODEC_VOLUME("Capture-3 Volume", 0x19, 0x2, HDA_INPUT),
1430         HDA_CODEC_MUTE("Capture-3 Switch", 0x19, 0x2, HDA_INPUT),
1431         HDA_CODEC_VOLUME("Capture-4 Volume", 0x19, 0x3, HDA_INPUT),
1432         HDA_CODEC_MUTE("Capture-4 Switch", 0x19, 0x3, HDA_INPUT),
1433         {
1434                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1435                 .name = "Input Source",
1436                 .info = conexant_mux_enum_info,
1437                 .get = conexant_mux_enum_get,
1438                 .put = conexant_mux_enum_put,
1439         },
1440         HDA_CODEC_VOLUME("Mic Boost Volume", 0x1a, 0x0, HDA_OUTPUT),
1441
1442         { } /* end */
1443 };
1444
1445 static struct hda_verb cxt5047_test_init_verbs[] = {
1446         /* Enable retasking pins as output, initially without power amp */
1447         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1448         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1449         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1450
1451         /* Disable digital (SPDIF) pins initially, but users can enable
1452          * them via a mixer switch.  In the case of SPDIF-out, this initverb
1453          * payload also sets the generation to 0, output to be in "consumer"
1454          * PCM format, copyright asserted, no pre-emphasis and no validity
1455          * control.
1456          */
1457         {0x18, AC_VERB_SET_DIGI_CONVERT_1, 0},
1458
1459         /* Ensure mic1, mic2, line1 pin widgets take input from the 
1460          * OUT1 sum bus when acting as an output.
1461          */
1462         {0x1a, AC_VERB_SET_CONNECT_SEL, 0},
1463         {0x1b, AC_VERB_SET_CONNECT_SEL, 0},
1464
1465         /* Start with output sum widgets muted and their output gains at min */
1466         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1467         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
1468
1469         /* Unmute retasking pin widget output buffers since the default
1470          * state appears to be output.  As the pin mode is changed by the
1471          * user the pin mode control will take care of enabling the pin's
1472          * input/output buffers as needed.
1473          */
1474         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1475         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1476         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1477
1478         /* Mute capture amp left and right */
1479         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1480
1481         /* Set ADC connection select to match default mixer setting (mic1
1482          * pin)
1483          */
1484         {0x12, AC_VERB_SET_CONNECT_SEL, 0x00},
1485
1486         /* Mute all inputs to mixer widget (even unconnected ones) */
1487         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)}, /* mic1 pin */
1488         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)}, /* mic2 pin */
1489         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)}, /* line1 pin */
1490         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)}, /* line2 pin */
1491         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)}, /* CD pin */
1492         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(5)}, /* Beep-gen pin */
1493         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(6)}, /* Line-out pin */
1494         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(7)}, /* HP-pin pin */
1495
1496         { }
1497 };
1498 #endif
1499
1500
1501 /* initialize jack-sensing, too */
1502 static int cxt5047_hp_init(struct hda_codec *codec)
1503 {
1504         conexant_init(codec);
1505         cxt5047_hp_automute(codec);
1506         return 0;
1507 }
1508
1509
1510 enum {
1511         CXT5047_LAPTOP,         /* Laptops w/o EAPD support */
1512         CXT5047_LAPTOP_HP,      /* Some HP laptops */
1513         CXT5047_LAPTOP_EAPD,    /* Laptops with EAPD support */
1514 #ifdef CONFIG_SND_DEBUG
1515         CXT5047_TEST,
1516 #endif
1517         CXT5047_MODELS
1518 };
1519
1520 static const char *cxt5047_models[CXT5047_MODELS] = {
1521         [CXT5047_LAPTOP]        = "laptop",
1522         [CXT5047_LAPTOP_HP]     = "laptop-hp",
1523         [CXT5047_LAPTOP_EAPD]   = "laptop-eapd",
1524 #ifdef CONFIG_SND_DEBUG
1525         [CXT5047_TEST]          = "test",
1526 #endif
1527 };
1528
1529 static struct snd_pci_quirk cxt5047_cfg_tbl[] = {
1530         SND_PCI_QUIRK(0x103c, 0x30a5, "HP DV5200T/DV8000T", CXT5047_LAPTOP_HP),
1531         SND_PCI_QUIRK_MASK(0x103c, 0xff00, 0x3000, "HP DV Series",
1532                            CXT5047_LAPTOP),
1533         SND_PCI_QUIRK(0x1179, 0xff31, "Toshiba P100", CXT5047_LAPTOP_EAPD),
1534         {}
1535 };
1536
1537 static int patch_cxt5047(struct hda_codec *codec)
1538 {
1539         struct conexant_spec *spec;
1540         int board_config;
1541
1542         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1543         if (!spec)
1544                 return -ENOMEM;
1545         codec->spec = spec;
1546         codec->pin_amp_workaround = 1;
1547
1548         spec->multiout.max_channels = 2;
1549         spec->multiout.num_dacs = ARRAY_SIZE(cxt5047_dac_nids);
1550         spec->multiout.dac_nids = cxt5047_dac_nids;
1551         spec->multiout.dig_out_nid = CXT5047_SPDIF_OUT;
1552         spec->num_adc_nids = 1;
1553         spec->adc_nids = cxt5047_adc_nids;
1554         spec->capsrc_nids = cxt5047_capsrc_nids;
1555         spec->num_mixers = 1;
1556         spec->mixers[0] = cxt5047_base_mixers;
1557         spec->num_init_verbs = 1;
1558         spec->init_verbs[0] = cxt5047_init_verbs;
1559         spec->spdif_route = 0;
1560         spec->num_channel_mode = ARRAY_SIZE(cxt5047_modes),
1561         spec->channel_mode = cxt5047_modes,
1562
1563         codec->patch_ops = conexant_patch_ops;
1564
1565         board_config = snd_hda_check_board_config(codec, CXT5047_MODELS,
1566                                                   cxt5047_models,
1567                                                   cxt5047_cfg_tbl);
1568         switch (board_config) {
1569         case CXT5047_LAPTOP:
1570                 spec->num_mixers = 2;
1571                 spec->mixers[1] = cxt5047_hp_spk_mixers;
1572                 codec->patch_ops.unsol_event = cxt5047_hp_unsol_event;
1573                 break;
1574         case CXT5047_LAPTOP_HP:
1575                 spec->num_mixers = 2;
1576                 spec->mixers[1] = cxt5047_hp_only_mixers;
1577                 codec->patch_ops.unsol_event = cxt5047_hp_unsol_event;
1578                 codec->patch_ops.init = cxt5047_hp_init;
1579                 break;
1580         case CXT5047_LAPTOP_EAPD:
1581                 spec->input_mux = &cxt5047_toshiba_capture_source;
1582                 spec->num_mixers = 2;
1583                 spec->mixers[1] = cxt5047_hp_spk_mixers;
1584                 spec->num_init_verbs = 2;
1585                 spec->init_verbs[1] = cxt5047_toshiba_init_verbs;
1586                 codec->patch_ops.unsol_event = cxt5047_hp_unsol_event;
1587                 break;
1588 #ifdef CONFIG_SND_DEBUG
1589         case CXT5047_TEST:
1590                 spec->input_mux = &cxt5047_test_capture_source;
1591                 spec->mixers[0] = cxt5047_test_mixer;
1592                 spec->init_verbs[0] = cxt5047_test_init_verbs;
1593                 codec->patch_ops.unsol_event = cxt5047_hp_unsol_event;
1594 #endif  
1595         }
1596         spec->vmaster_nid = 0x13;
1597
1598         switch (codec->subsystem_id >> 16) {
1599         case 0x103c:
1600                 /* HP laptops have really bad sound over 0 dB on NID 0x10.
1601                  * Fix max PCM level to 0 dB (originally it has 0x1e steps
1602                  * with 0 dB offset 0x17)
1603                  */
1604                 snd_hda_override_amp_caps(codec, 0x10, HDA_INPUT,
1605                                           (0x17 << AC_AMPCAP_OFFSET_SHIFT) |
1606                                           (0x17 << AC_AMPCAP_NUM_STEPS_SHIFT) |
1607                                           (0x05 << AC_AMPCAP_STEP_SIZE_SHIFT) |
1608                                           (1 << AC_AMPCAP_MUTE_SHIFT));
1609                 break;
1610         }
1611
1612         return 0;
1613 }
1614
1615 /* Conexant 5051 specific */
1616 static hda_nid_t cxt5051_dac_nids[1] = { 0x10 };
1617 static hda_nid_t cxt5051_adc_nids[2] = { 0x14, 0x15 };
1618
1619 static struct hda_channel_mode cxt5051_modes[1] = {
1620         { 2, NULL },
1621 };
1622
1623 static void cxt5051_update_speaker(struct hda_codec *codec)
1624 {
1625         struct conexant_spec *spec = codec->spec;
1626         unsigned int pinctl;
1627         /* headphone pin */
1628         pinctl = (spec->hp_present && spec->cur_eapd) ? PIN_HP : 0;
1629         snd_hda_codec_write(codec, 0x16, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
1630                             pinctl);
1631         /* speaker pin */
1632         pinctl = (!spec->hp_present && spec->cur_eapd) ? PIN_OUT : 0;
1633         snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
1634                             pinctl);
1635         /* on ideapad there is an aditional speaker (subwoofer) to mute */
1636         if (spec->ideapad)
1637                 snd_hda_codec_write(codec, 0x1b, 0,
1638                                     AC_VERB_SET_PIN_WIDGET_CONTROL,
1639                                     pinctl);
1640 }
1641
1642 /* turn on/off EAPD (+ mute HP) as a master switch */
1643 static int cxt5051_hp_master_sw_put(struct snd_kcontrol *kcontrol,
1644                                     struct snd_ctl_elem_value *ucontrol)
1645 {
1646         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1647
1648         if (!cxt_eapd_put(kcontrol, ucontrol))
1649                 return 0;
1650         cxt5051_update_speaker(codec);
1651         return 1;
1652 }
1653
1654 /* toggle input of built-in and mic jack appropriately */
1655 static void cxt5051_portb_automic(struct hda_codec *codec)
1656 {
1657         struct conexant_spec *spec = codec->spec;
1658         unsigned int present;
1659
1660         if (!(spec->auto_mic & AUTO_MIC_PORTB))
1661                 return;
1662         present = snd_hda_jack_detect(codec, 0x17);
1663         snd_hda_codec_write(codec, 0x14, 0,
1664                             AC_VERB_SET_CONNECT_SEL,
1665                             present ? 0x01 : 0x00);
1666 }
1667
1668 /* switch the current ADC according to the jack state */
1669 static void cxt5051_portc_automic(struct hda_codec *codec)
1670 {
1671         struct conexant_spec *spec = codec->spec;
1672         unsigned int present;
1673         hda_nid_t new_adc;
1674
1675         if (!(spec->auto_mic & AUTO_MIC_PORTC))
1676                 return;
1677         present = snd_hda_jack_detect(codec, 0x18);
1678         if (present)
1679                 spec->cur_adc_idx = 1;
1680         else
1681                 spec->cur_adc_idx = 0;
1682         new_adc = spec->adc_nids[spec->cur_adc_idx];
1683         if (spec->cur_adc && spec->cur_adc != new_adc) {
1684                 /* stream is running, let's swap the current ADC */
1685                 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
1686                 spec->cur_adc = new_adc;
1687                 snd_hda_codec_setup_stream(codec, new_adc,
1688                                            spec->cur_adc_stream_tag, 0,
1689                                            spec->cur_adc_format);
1690         }
1691 }
1692
1693 /* mute internal speaker if HP is plugged */
1694 static void cxt5051_hp_automute(struct hda_codec *codec)
1695 {
1696         struct conexant_spec *spec = codec->spec;
1697
1698         spec->hp_present = snd_hda_jack_detect(codec, 0x16);
1699         cxt5051_update_speaker(codec);
1700 }
1701
1702 /* unsolicited event for HP jack sensing */
1703 static void cxt5051_hp_unsol_event(struct hda_codec *codec,
1704                                    unsigned int res)
1705 {
1706         int nid = (res & AC_UNSOL_RES_SUBTAG) >> 20;
1707         switch (res >> 26) {
1708         case CONEXANT_HP_EVENT:
1709                 cxt5051_hp_automute(codec);
1710                 break;
1711         case CXT5051_PORTB_EVENT:
1712                 cxt5051_portb_automic(codec);
1713                 break;
1714         case CXT5051_PORTC_EVENT:
1715                 cxt5051_portc_automic(codec);
1716                 break;
1717         }
1718         conexant_report_jack(codec, nid);
1719 }
1720
1721 static struct snd_kcontrol_new cxt5051_playback_mixers[] = {
1722         HDA_CODEC_VOLUME("Master Playback Volume", 0x10, 0x00, HDA_OUTPUT),
1723         {
1724                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1725                 .name = "Master Playback Switch",
1726                 .info = cxt_eapd_info,
1727                 .get = cxt_eapd_get,
1728                 .put = cxt5051_hp_master_sw_put,
1729                 .private_value = 0x1a,
1730         },
1731         {}
1732 };
1733
1734 static struct snd_kcontrol_new cxt5051_capture_mixers[] = {
1735         HDA_CODEC_VOLUME("Internal Mic Volume", 0x14, 0x00, HDA_INPUT),
1736         HDA_CODEC_MUTE("Internal Mic Switch", 0x14, 0x00, HDA_INPUT),
1737         HDA_CODEC_VOLUME("External Mic Volume", 0x14, 0x01, HDA_INPUT),
1738         HDA_CODEC_MUTE("External Mic Switch", 0x14, 0x01, HDA_INPUT),
1739         HDA_CODEC_VOLUME("Docking Mic Volume", 0x15, 0x00, HDA_INPUT),
1740         HDA_CODEC_MUTE("Docking Mic Switch", 0x15, 0x00, HDA_INPUT),
1741         {}
1742 };
1743
1744 static struct snd_kcontrol_new cxt5051_hp_mixers[] = {
1745         HDA_CODEC_VOLUME("Internal Mic Volume", 0x14, 0x00, HDA_INPUT),
1746         HDA_CODEC_MUTE("Internal Mic Switch", 0x14, 0x00, HDA_INPUT),
1747         HDA_CODEC_VOLUME("External Mic Volume", 0x15, 0x00, HDA_INPUT),
1748         HDA_CODEC_MUTE("External Mic Switch", 0x15, 0x00, HDA_INPUT),
1749         {}
1750 };
1751
1752 static struct snd_kcontrol_new cxt5051_hp_dv6736_mixers[] = {
1753         HDA_CODEC_VOLUME("Capture Volume", 0x14, 0x00, HDA_INPUT),
1754         HDA_CODEC_MUTE("Capture Switch", 0x14, 0x00, HDA_INPUT),
1755         {}
1756 };
1757
1758 static struct snd_kcontrol_new cxt5051_f700_mixers[] = {
1759         HDA_CODEC_VOLUME("Capture Volume", 0x14, 0x01, HDA_INPUT),
1760         HDA_CODEC_MUTE("Capture Switch", 0x14, 0x01, HDA_INPUT),
1761         {}
1762 };
1763
1764 static struct snd_kcontrol_new cxt5051_toshiba_mixers[] = {
1765         HDA_CODEC_VOLUME("Internal Mic Volume", 0x14, 0x00, HDA_INPUT),
1766         HDA_CODEC_MUTE("Internal Mic Switch", 0x14, 0x00, HDA_INPUT),
1767         HDA_CODEC_VOLUME("External Mic Volume", 0x14, 0x01, HDA_INPUT),
1768         HDA_CODEC_MUTE("External Mic Switch", 0x14, 0x01, HDA_INPUT),
1769         {}
1770 };
1771
1772 static struct hda_verb cxt5051_init_verbs[] = {
1773         /* Line in, Mic */
1774         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x03},
1775         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1776         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x03},
1777         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1778         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
1779         {0x1d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x03},
1780         /* SPK  */
1781         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1782         {0x1a, AC_VERB_SET_CONNECT_SEL, 0x00},
1783         /* HP, Amp  */
1784         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1785         {0x16, AC_VERB_SET_CONNECT_SEL, 0x00},
1786         /* DAC1 */      
1787         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1788         /* Record selector: Int mic */
1789         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x44},
1790         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1) | 0x44},
1791         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x44},
1792         /* SPDIF route: PCM */
1793         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1794         {0x1c, AC_VERB_SET_CONNECT_SEL, 0x0},
1795         /* EAPD */
1796         {0x1a, AC_VERB_SET_EAPD_BTLENABLE, 0x2}, /* default on */ 
1797         {0x16, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN|CONEXANT_HP_EVENT},
1798         { } /* end */
1799 };
1800
1801 static struct hda_verb cxt5051_hp_dv6736_init_verbs[] = {
1802         /* Line in, Mic */
1803         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x03},
1804         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1805         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x0},
1806         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x0},
1807         /* SPK  */
1808         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1809         {0x1a, AC_VERB_SET_CONNECT_SEL, 0x00},
1810         /* HP, Amp  */
1811         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1812         {0x16, AC_VERB_SET_CONNECT_SEL, 0x00},
1813         /* DAC1 */
1814         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1815         /* Record selector: Int mic */
1816         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1) | 0x44},
1817         {0x14, AC_VERB_SET_CONNECT_SEL, 0x1},
1818         /* SPDIF route: PCM */
1819         {0x1c, AC_VERB_SET_CONNECT_SEL, 0x0},
1820         /* EAPD */
1821         {0x1a, AC_VERB_SET_EAPD_BTLENABLE, 0x2}, /* default on */
1822         {0x16, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN|CONEXANT_HP_EVENT},
1823         { } /* end */
1824 };
1825
1826 static struct hda_verb cxt5051_lenovo_x200_init_verbs[] = {
1827         /* Line in, Mic */
1828         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x03},
1829         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1830         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x03},
1831         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1832         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
1833         {0x1d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x03},
1834         /* SPK  */
1835         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1836         {0x1a, AC_VERB_SET_CONNECT_SEL, 0x00},
1837         /* HP, Amp  */
1838         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1839         {0x16, AC_VERB_SET_CONNECT_SEL, 0x00},
1840         /* Docking HP */
1841         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1842         {0x19, AC_VERB_SET_CONNECT_SEL, 0x00},
1843         /* DAC1 */
1844         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1845         /* Record selector: Int mic */
1846         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x44},
1847         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1) | 0x44},
1848         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x44},
1849         /* SPDIF route: PCM */
1850         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT}, /* needed for W500 Advanced Mini Dock 250410 */
1851         {0x1c, AC_VERB_SET_CONNECT_SEL, 0x0},
1852         /* EAPD */
1853         {0x1a, AC_VERB_SET_EAPD_BTLENABLE, 0x2}, /* default on */
1854         {0x16, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN|CONEXANT_HP_EVENT},
1855         {0x19, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN|CONEXANT_HP_EVENT},
1856         { } /* end */
1857 };
1858
1859 static struct hda_verb cxt5051_f700_init_verbs[] = {
1860         /* Line in, Mic */
1861         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x03},
1862         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1863         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x0},
1864         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x0},
1865         /* SPK  */
1866         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1867         {0x1a, AC_VERB_SET_CONNECT_SEL, 0x00},
1868         /* HP, Amp  */
1869         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1870         {0x16, AC_VERB_SET_CONNECT_SEL, 0x00},
1871         /* DAC1 */
1872         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1873         /* Record selector: Int mic */
1874         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1) | 0x44},
1875         {0x14, AC_VERB_SET_CONNECT_SEL, 0x1},
1876         /* SPDIF route: PCM */
1877         {0x1c, AC_VERB_SET_CONNECT_SEL, 0x0},
1878         /* EAPD */
1879         {0x1a, AC_VERB_SET_EAPD_BTLENABLE, 0x2}, /* default on */
1880         {0x16, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN|CONEXANT_HP_EVENT},
1881         { } /* end */
1882 };
1883
1884 static void cxt5051_init_mic_port(struct hda_codec *codec, hda_nid_t nid,
1885                                  unsigned int event)
1886 {
1887         snd_hda_codec_write(codec, nid, 0,
1888                             AC_VERB_SET_UNSOLICITED_ENABLE,
1889                             AC_USRSP_EN | event);
1890 #ifdef CONFIG_SND_HDA_INPUT_JACK
1891         conexant_add_jack(codec, nid, SND_JACK_MICROPHONE);
1892         conexant_report_jack(codec, nid);
1893 #endif
1894 }
1895
1896 static struct hda_verb cxt5051_ideapad_init_verbs[] = {
1897         /* Subwoofer */
1898         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1899         {0x1b, AC_VERB_SET_CONNECT_SEL, 0x00},
1900         { } /* end */
1901 };
1902
1903 /* initialize jack-sensing, too */
1904 static int cxt5051_init(struct hda_codec *codec)
1905 {
1906         struct conexant_spec *spec = codec->spec;
1907
1908         conexant_init(codec);
1909         conexant_init_jacks(codec);
1910
1911         if (spec->auto_mic & AUTO_MIC_PORTB)
1912                 cxt5051_init_mic_port(codec, 0x17, CXT5051_PORTB_EVENT);
1913         if (spec->auto_mic & AUTO_MIC_PORTC)
1914                 cxt5051_init_mic_port(codec, 0x18, CXT5051_PORTC_EVENT);
1915
1916         if (codec->patch_ops.unsol_event) {
1917                 cxt5051_hp_automute(codec);
1918                 cxt5051_portb_automic(codec);
1919                 cxt5051_portc_automic(codec);
1920         }
1921         return 0;
1922 }
1923
1924
1925 enum {
1926         CXT5051_LAPTOP,  /* Laptops w/ EAPD support */
1927         CXT5051_HP,     /* no docking */
1928         CXT5051_HP_DV6736,      /* HP without mic switch */
1929         CXT5051_LENOVO_X200,    /* Lenovo X200 laptop, also used for Advanced Mini Dock 250410 */
1930         CXT5051_F700,       /* HP Compaq Presario F700 */
1931         CXT5051_TOSHIBA,        /* Toshiba M300 & co */
1932         CXT5051_IDEAPAD,        /* Lenovo IdeaPad Y430 */
1933         CXT5051_MODELS
1934 };
1935
1936 static const char *cxt5051_models[CXT5051_MODELS] = {
1937         [CXT5051_LAPTOP]        = "laptop",
1938         [CXT5051_HP]            = "hp",
1939         [CXT5051_HP_DV6736]     = "hp-dv6736",
1940         [CXT5051_LENOVO_X200]   = "lenovo-x200",
1941         [CXT5051_F700]          = "hp-700",
1942         [CXT5051_TOSHIBA]       = "toshiba",
1943         [CXT5051_IDEAPAD]       = "ideapad",
1944 };
1945
1946 static struct snd_pci_quirk cxt5051_cfg_tbl[] = {
1947         SND_PCI_QUIRK(0x103c, 0x30cf, "HP DV6736", CXT5051_HP_DV6736),
1948         SND_PCI_QUIRK(0x103c, 0x360b, "Compaq Presario CQ60", CXT5051_HP),
1949         SND_PCI_QUIRK(0x103c, 0x30ea, "Compaq Presario F700", CXT5051_F700),
1950         SND_PCI_QUIRK(0x1179, 0xff50, "Toshiba M30x", CXT5051_TOSHIBA),
1951         SND_PCI_QUIRK(0x14f1, 0x0101, "Conexant Reference board",
1952                       CXT5051_LAPTOP),
1953         SND_PCI_QUIRK(0x14f1, 0x5051, "HP Spartan 1.1", CXT5051_HP),
1954         SND_PCI_QUIRK(0x17aa, 0x20f2, "Lenovo X200", CXT5051_LENOVO_X200),
1955         SND_PCI_QUIRK(0x17aa, 0x3a0d, "Lenovo IdeaPad", CXT5051_IDEAPAD),
1956         {}
1957 };
1958
1959 static int patch_cxt5051(struct hda_codec *codec)
1960 {
1961         struct conexant_spec *spec;
1962         int board_config;
1963
1964         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1965         if (!spec)
1966                 return -ENOMEM;
1967         codec->spec = spec;
1968         codec->pin_amp_workaround = 1;
1969
1970         codec->patch_ops = conexant_patch_ops;
1971         codec->patch_ops.init = cxt5051_init;
1972
1973         spec->multiout.max_channels = 2;
1974         spec->multiout.num_dacs = ARRAY_SIZE(cxt5051_dac_nids);
1975         spec->multiout.dac_nids = cxt5051_dac_nids;
1976         spec->multiout.dig_out_nid = CXT5051_SPDIF_OUT;
1977         spec->num_adc_nids = 1; /* not 2; via auto-mic switch */
1978         spec->adc_nids = cxt5051_adc_nids;
1979         spec->num_mixers = 2;
1980         spec->mixers[0] = cxt5051_capture_mixers;
1981         spec->mixers[1] = cxt5051_playback_mixers;
1982         spec->num_init_verbs = 1;
1983         spec->init_verbs[0] = cxt5051_init_verbs;
1984         spec->spdif_route = 0;
1985         spec->num_channel_mode = ARRAY_SIZE(cxt5051_modes);
1986         spec->channel_mode = cxt5051_modes;
1987         spec->cur_adc = 0;
1988         spec->cur_adc_idx = 0;
1989
1990         codec->patch_ops.unsol_event = cxt5051_hp_unsol_event;
1991
1992         board_config = snd_hda_check_board_config(codec, CXT5051_MODELS,
1993                                                   cxt5051_models,
1994                                                   cxt5051_cfg_tbl);
1995         spec->auto_mic = AUTO_MIC_PORTB | AUTO_MIC_PORTC;
1996         switch (board_config) {
1997         case CXT5051_HP:
1998                 spec->mixers[0] = cxt5051_hp_mixers;
1999                 break;
2000         case CXT5051_HP_DV6736:
2001                 spec->init_verbs[0] = cxt5051_hp_dv6736_init_verbs;
2002                 spec->mixers[0] = cxt5051_hp_dv6736_mixers;
2003                 spec->auto_mic = 0;
2004                 break;
2005         case CXT5051_LENOVO_X200:
2006                 spec->init_verbs[0] = cxt5051_lenovo_x200_init_verbs;
2007                 break;
2008         case CXT5051_F700:
2009                 spec->init_verbs[0] = cxt5051_f700_init_verbs;
2010                 spec->mixers[0] = cxt5051_f700_mixers;
2011                 spec->auto_mic = 0;
2012                 break;
2013         case CXT5051_TOSHIBA:
2014                 spec->mixers[0] = cxt5051_toshiba_mixers;
2015                 spec->auto_mic = AUTO_MIC_PORTB;
2016                 break;
2017         case CXT5051_IDEAPAD:
2018                 spec->init_verbs[spec->num_init_verbs++] =
2019                         cxt5051_ideapad_init_verbs;
2020                 spec->ideapad = 1;
2021                 break;
2022         }
2023
2024         return 0;
2025 }
2026
2027 /* Conexant 5066 specific */
2028
2029 static hda_nid_t cxt5066_dac_nids[1] = { 0x10 };
2030 static hda_nid_t cxt5066_adc_nids[3] = { 0x14, 0x15, 0x16 };
2031 static hda_nid_t cxt5066_capsrc_nids[1] = { 0x17 };
2032 #define CXT5066_SPDIF_OUT       0x21
2033
2034 /* OLPC's microphone port is DC coupled for use with external sensors,
2035  * therefore we use a 50% mic bias in order to center the input signal with
2036  * the DC input range of the codec. */
2037 #define CXT5066_OLPC_EXT_MIC_BIAS PIN_VREF50
2038
2039 static struct hda_channel_mode cxt5066_modes[1] = {
2040         { 2, NULL },
2041 };
2042
2043 static void cxt5066_update_speaker(struct hda_codec *codec)
2044 {
2045         struct conexant_spec *spec = codec->spec;
2046         unsigned int pinctl;
2047
2048         snd_printdd("CXT5066: update speaker, hp_present=%d\n",
2049                 spec->hp_present);
2050
2051         /* Port A (HP) */
2052         pinctl = ((spec->hp_present & 1) && spec->cur_eapd) ? PIN_HP : 0;
2053         snd_hda_codec_write(codec, 0x19, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
2054                         pinctl);
2055
2056         /* Port D (HP/LO) */
2057         pinctl = ((spec->hp_present & 2) && spec->cur_eapd)
2058                 ? spec->port_d_mode : 0;
2059         /* Mute if Port A is connected on Thinkpad */
2060         if (spec->thinkpad && (spec->hp_present & 1))
2061                 pinctl = 0;
2062         snd_hda_codec_write(codec, 0x1c, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
2063                         pinctl);
2064
2065         /* CLASS_D AMP */
2066         pinctl = (!spec->hp_present && spec->cur_eapd) ? PIN_OUT : 0;
2067         snd_hda_codec_write(codec, 0x1f, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
2068                         pinctl);
2069
2070         if (spec->dell_automute) {
2071                 /* DELL AIO Port Rule: PortA > PortD > IntSpk */
2072                 pinctl = (!(spec->hp_present & 1) && spec->cur_eapd)
2073                         ? PIN_OUT : 0;
2074                 snd_hda_codec_write(codec, 0x1c, 0,
2075                         AC_VERB_SET_PIN_WIDGET_CONTROL, pinctl);
2076         }
2077 }
2078
2079 /* turn on/off EAPD (+ mute HP) as a master switch */
2080 static int cxt5066_hp_master_sw_put(struct snd_kcontrol *kcontrol,
2081                                     struct snd_ctl_elem_value *ucontrol)
2082 {
2083         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2084
2085         if (!cxt_eapd_put(kcontrol, ucontrol))
2086                 return 0;
2087
2088         cxt5066_update_speaker(codec);
2089         return 1;
2090 }
2091
2092 static const struct hda_input_mux cxt5066_olpc_dc_bias = {
2093         .num_items = 3,
2094         .items = {
2095                 { "Off", PIN_IN },
2096                 { "50%", PIN_VREF50 },
2097                 { "80%", PIN_VREF80 },
2098         },
2099 };
2100
2101 static int cxt5066_set_olpc_dc_bias(struct hda_codec *codec)
2102 {
2103         struct conexant_spec *spec = codec->spec;
2104         /* Even though port F is the DC input, the bias is controlled on port B.
2105          * we also leave that port as an active input (but unselected) in DC mode
2106          * just in case that is necessary to make the bias setting take effect. */
2107         return snd_hda_codec_write_cache(codec, 0x1a, 0,
2108                 AC_VERB_SET_PIN_WIDGET_CONTROL,
2109                 cxt5066_olpc_dc_bias.items[spec->dc_input_bias].index);
2110 }
2111
2112 /* OLPC defers mic widget control until when capture is started because the
2113  * microphone LED comes on as soon as these settings are put in place. if we
2114  * did this before recording, it would give the false indication that recording
2115  * is happening when it is not. */
2116 static void cxt5066_olpc_select_mic(struct hda_codec *codec)
2117 {
2118         struct conexant_spec *spec = codec->spec;
2119         if (!spec->recording)
2120                 return;
2121
2122         if (spec->dc_enable) {
2123                 /* in DC mode we ignore presence detection and just use the jack
2124                  * through our special DC port */
2125                 const struct hda_verb enable_dc_mode[] = {
2126                         /* disble internal mic, port C */
2127                         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2128
2129                         /* enable DC capture, port F */
2130                         {0x1e, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
2131                         {},
2132                 };
2133
2134                 snd_hda_sequence_write(codec, enable_dc_mode);
2135                 /* port B input disabled (and bias set) through the following call */
2136                 cxt5066_set_olpc_dc_bias(codec);
2137                 return;
2138         }
2139
2140         /* disable DC (port F) */
2141         snd_hda_codec_write(codec, 0x1e, 0, AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
2142
2143         /* external mic, port B */
2144         snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
2145                 spec->ext_mic_present ? CXT5066_OLPC_EXT_MIC_BIAS : 0);
2146
2147         /* internal mic, port C */
2148         snd_hda_codec_write(codec, 0x1b, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
2149                 spec->ext_mic_present ? 0 : PIN_VREF80);
2150 }
2151
2152 /* toggle input of built-in and mic jack appropriately */
2153 static void cxt5066_olpc_automic(struct hda_codec *codec)
2154 {
2155         struct conexant_spec *spec = codec->spec;
2156         unsigned int present;
2157
2158         if (spec->dc_enable) /* don't do presence detection in DC mode */
2159                 return;
2160
2161         present = snd_hda_codec_read(codec, 0x1a, 0,
2162                                      AC_VERB_GET_PIN_SENSE, 0) & 0x80000000;
2163         if (present)
2164                 snd_printdd("CXT5066: external microphone detected\n");
2165         else
2166                 snd_printdd("CXT5066: external microphone absent\n");
2167
2168         snd_hda_codec_write(codec, 0x17, 0, AC_VERB_SET_CONNECT_SEL,
2169                 present ? 0 : 1);
2170         spec->ext_mic_present = !!present;
2171
2172         cxt5066_olpc_select_mic(codec);
2173 }
2174
2175 /* toggle input of built-in digital mic and mic jack appropriately */
2176 static void cxt5066_vostro_automic(struct hda_codec *codec)
2177 {
2178         unsigned int present;
2179
2180         struct hda_verb ext_mic_present[] = {
2181                 /* enable external mic, port B */
2182                 {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
2183
2184                 /* switch to external mic input */
2185                 {0x17, AC_VERB_SET_CONNECT_SEL, 0},
2186                 {0x14, AC_VERB_SET_CONNECT_SEL, 0},
2187
2188                 /* disable internal digital mic */
2189                 {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2190                 {}
2191         };
2192         static struct hda_verb ext_mic_absent[] = {
2193                 /* enable internal mic, port C */
2194                 {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
2195
2196                 /* switch to internal mic input */
2197                 {0x14, AC_VERB_SET_CONNECT_SEL, 2},
2198
2199                 /* disable external mic, port B */
2200                 {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2201                 {}
2202         };
2203
2204         present = snd_hda_jack_detect(codec, 0x1a);
2205         if (present) {
2206                 snd_printdd("CXT5066: external microphone detected\n");
2207                 snd_hda_sequence_write(codec, ext_mic_present);
2208         } else {
2209                 snd_printdd("CXT5066: external microphone absent\n");
2210                 snd_hda_sequence_write(codec, ext_mic_absent);
2211         }
2212 }
2213
2214 /* toggle input of built-in digital mic and mic jack appropriately */
2215 static void cxt5066_ideapad_automic(struct hda_codec *codec)
2216 {
2217         unsigned int present;
2218
2219         struct hda_verb ext_mic_present[] = {
2220                 {0x14, AC_VERB_SET_CONNECT_SEL, 0},
2221                 {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
2222                 {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2223                 {}
2224         };
2225         static struct hda_verb ext_mic_absent[] = {
2226                 {0x14, AC_VERB_SET_CONNECT_SEL, 2},
2227                 {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
2228                 {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2229                 {}
2230         };
2231
2232         present = snd_hda_jack_detect(codec, 0x1b);
2233         if (present) {
2234                 snd_printdd("CXT5066: external microphone detected\n");
2235                 snd_hda_sequence_write(codec, ext_mic_present);
2236         } else {
2237                 snd_printdd("CXT5066: external microphone absent\n");
2238                 snd_hda_sequence_write(codec, ext_mic_absent);
2239         }
2240 }
2241
2242 /* toggle input of built-in digital mic and mic jack appropriately
2243    order is: external mic -> dock mic -> interal mic */
2244 static void cxt5066_thinkpad_automic(struct hda_codec *codec)
2245 {
2246         unsigned int ext_present, dock_present;
2247
2248         static struct hda_verb ext_mic_present[] = {
2249                 {0x14, AC_VERB_SET_CONNECT_SEL, 0},
2250                 {0x17, AC_VERB_SET_CONNECT_SEL, 1},
2251                 {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
2252                 {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2253                 {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2254                 {}
2255         };
2256         static struct hda_verb dock_mic_present[] = {
2257                 {0x14, AC_VERB_SET_CONNECT_SEL, 0},
2258                 {0x17, AC_VERB_SET_CONNECT_SEL, 0},
2259                 {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
2260                 {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2261                 {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2262                 {}
2263         };
2264         static struct hda_verb ext_mic_absent[] = {
2265                 {0x14, AC_VERB_SET_CONNECT_SEL, 2},
2266                 {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
2267                 {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2268                 {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2269                 {}
2270         };
2271
2272         ext_present = snd_hda_jack_detect(codec, 0x1b);
2273         dock_present = snd_hda_jack_detect(codec, 0x1a);
2274         if (ext_present) {
2275                 snd_printdd("CXT5066: external microphone detected\n");
2276                 snd_hda_sequence_write(codec, ext_mic_present);
2277         } else if (dock_present) {
2278                 snd_printdd("CXT5066: dock microphone detected\n");
2279                 snd_hda_sequence_write(codec, dock_mic_present);
2280         } else {
2281                 snd_printdd("CXT5066: external microphone absent\n");
2282                 snd_hda_sequence_write(codec, ext_mic_absent);
2283         }
2284 }
2285
2286 /* mute internal speaker if HP is plugged */
2287 static void cxt5066_hp_automute(struct hda_codec *codec)
2288 {
2289         struct conexant_spec *spec = codec->spec;
2290         unsigned int portA, portD;
2291
2292         /* Port A */
2293         portA = snd_hda_jack_detect(codec, 0x19);
2294
2295         /* Port D */
2296         portD = snd_hda_jack_detect(codec, 0x1c);
2297
2298         spec->hp_present = !!(portA);
2299         spec->hp_present |= portD ? 2 : 0;
2300         snd_printdd("CXT5066: hp automute portA=%x portD=%x present=%d\n",
2301                 portA, portD, spec->hp_present);
2302         cxt5066_update_speaker(codec);
2303 }
2304
2305 /* unsolicited event for jack sensing */
2306 static void cxt5066_olpc_unsol_event(struct hda_codec *codec, unsigned int res)
2307 {
2308         struct conexant_spec *spec = codec->spec;
2309         snd_printdd("CXT5066: unsol event %x (%x)\n", res, res >> 26);
2310         switch (res >> 26) {
2311         case CONEXANT_HP_EVENT:
2312                 cxt5066_hp_automute(codec);
2313                 break;
2314         case CONEXANT_MIC_EVENT:
2315                 /* ignore mic events in DC mode; we're always using the jack */
2316                 if (!spec->dc_enable)
2317                         cxt5066_olpc_automic(codec);
2318                 break;
2319         }
2320 }
2321
2322 /* unsolicited event for jack sensing */
2323 static void cxt5066_vostro_event(struct hda_codec *codec, unsigned int res)
2324 {
2325         snd_printdd("CXT5066_vostro: unsol event %x (%x)\n", res, res >> 26);
2326         switch (res >> 26) {
2327         case CONEXANT_HP_EVENT:
2328                 cxt5066_hp_automute(codec);
2329                 break;
2330         case CONEXANT_MIC_EVENT:
2331                 cxt5066_vostro_automic(codec);
2332                 break;
2333         }
2334 }
2335
2336 /* unsolicited event for jack sensing */
2337 static void cxt5066_ideapad_event(struct hda_codec *codec, unsigned int res)
2338 {
2339         snd_printdd("CXT5066_ideapad: unsol event %x (%x)\n", res, res >> 26);
2340         switch (res >> 26) {
2341         case CONEXANT_HP_EVENT:
2342                 cxt5066_hp_automute(codec);
2343                 break;
2344         case CONEXANT_MIC_EVENT:
2345                 cxt5066_ideapad_automic(codec);
2346                 break;
2347         }
2348 }
2349
2350 /* unsolicited event for jack sensing */
2351 static void cxt5066_thinkpad_event(struct hda_codec *codec, unsigned int res)
2352 {
2353         snd_printdd("CXT5066_thinkpad: unsol event %x (%x)\n", res, res >> 26);
2354         switch (res >> 26) {
2355         case CONEXANT_HP_EVENT:
2356                 cxt5066_hp_automute(codec);
2357                 break;
2358         case CONEXANT_MIC_EVENT:
2359                 cxt5066_thinkpad_automic(codec);
2360                 break;
2361         }
2362 }
2363
2364 static const struct hda_input_mux cxt5066_analog_mic_boost = {
2365         .num_items = 5,
2366         .items = {
2367                 { "0dB",  0 },
2368                 { "10dB", 1 },
2369                 { "20dB", 2 },
2370                 { "30dB", 3 },
2371                 { "40dB", 4 },
2372         },
2373 };
2374
2375 static void cxt5066_set_mic_boost(struct hda_codec *codec)
2376 {
2377         struct conexant_spec *spec = codec->spec;
2378         snd_hda_codec_write_cache(codec, 0x17, 0,
2379                 AC_VERB_SET_AMP_GAIN_MUTE,
2380                 AC_AMP_SET_RIGHT | AC_AMP_SET_LEFT | AC_AMP_SET_OUTPUT |
2381                         cxt5066_analog_mic_boost.items[spec->mic_boost].index);
2382         if (spec->ideapad || spec->thinkpad) {
2383                 /* adjust the internal mic as well...it is not through 0x17 */
2384                 snd_hda_codec_write_cache(codec, 0x23, 0,
2385                         AC_VERB_SET_AMP_GAIN_MUTE,
2386                         AC_AMP_SET_RIGHT | AC_AMP_SET_LEFT | AC_AMP_SET_INPUT |
2387                                 cxt5066_analog_mic_boost.
2388                                         items[spec->mic_boost].index);
2389         }
2390 }
2391
2392 static int cxt5066_mic_boost_mux_enum_info(struct snd_kcontrol *kcontrol,
2393                                            struct snd_ctl_elem_info *uinfo)
2394 {
2395         return snd_hda_input_mux_info(&cxt5066_analog_mic_boost, uinfo);
2396 }
2397
2398 static int cxt5066_mic_boost_mux_enum_get(struct snd_kcontrol *kcontrol,
2399                                           struct snd_ctl_elem_value *ucontrol)
2400 {
2401         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2402         struct conexant_spec *spec = codec->spec;
2403         ucontrol->value.enumerated.item[0] = spec->mic_boost;
2404         return 0;
2405 }
2406
2407 static int cxt5066_mic_boost_mux_enum_put(struct snd_kcontrol *kcontrol,
2408                                           struct snd_ctl_elem_value *ucontrol)
2409 {
2410         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2411         struct conexant_spec *spec = codec->spec;
2412         const struct hda_input_mux *imux = &cxt5066_analog_mic_boost;
2413         unsigned int idx;
2414         idx = ucontrol->value.enumerated.item[0];
2415         if (idx >= imux->num_items)
2416                 idx = imux->num_items - 1;
2417
2418         spec->mic_boost = idx;
2419         if (!spec->dc_enable)
2420                 cxt5066_set_mic_boost(codec);
2421         return 1;
2422 }
2423
2424 static void cxt5066_enable_dc(struct hda_codec *codec)
2425 {
2426         const struct hda_verb enable_dc_mode[] = {
2427                 /* disable gain */
2428                 {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2429
2430                 /* switch to DC input */
2431                 {0x17, AC_VERB_SET_CONNECT_SEL, 3},
2432                 {}
2433         };
2434
2435         /* configure as input source */
2436         snd_hda_sequence_write(codec, enable_dc_mode);
2437         cxt5066_olpc_select_mic(codec); /* also sets configured bias */
2438 }
2439
2440 static void cxt5066_disable_dc(struct hda_codec *codec)
2441 {
2442         /* reconfigure input source */
2443         cxt5066_set_mic_boost(codec);
2444         /* automic also selects the right mic if we're recording */
2445         cxt5066_olpc_automic(codec);
2446 }
2447
2448 static int cxt5066_olpc_dc_get(struct snd_kcontrol *kcontrol,
2449                              struct snd_ctl_elem_value *ucontrol)
2450 {
2451         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2452         struct conexant_spec *spec = codec->spec;
2453         ucontrol->value.integer.value[0] = spec->dc_enable;
2454         return 0;
2455 }
2456
2457 static int cxt5066_olpc_dc_put(struct snd_kcontrol *kcontrol,
2458                              struct snd_ctl_elem_value *ucontrol)
2459 {
2460         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2461         struct conexant_spec *spec = codec->spec;
2462         int dc_enable = !!ucontrol->value.integer.value[0];
2463
2464         if (dc_enable == spec->dc_enable)
2465                 return 0;
2466
2467         spec->dc_enable = dc_enable;
2468         if (dc_enable)
2469                 cxt5066_enable_dc(codec);
2470         else
2471                 cxt5066_disable_dc(codec);
2472
2473         return 1;
2474 }
2475
2476 static int cxt5066_olpc_dc_bias_enum_info(struct snd_kcontrol *kcontrol,
2477                                            struct snd_ctl_elem_info *uinfo)
2478 {
2479         return snd_hda_input_mux_info(&cxt5066_olpc_dc_bias, uinfo);
2480 }
2481
2482 static int cxt5066_olpc_dc_bias_enum_get(struct snd_kcontrol *kcontrol,
2483                                           struct snd_ctl_elem_value *ucontrol)
2484 {
2485         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2486         struct conexant_spec *spec = codec->spec;
2487         ucontrol->value.enumerated.item[0] = spec->dc_input_bias;
2488         return 0;
2489 }
2490
2491 static int cxt5066_olpc_dc_bias_enum_put(struct snd_kcontrol *kcontrol,
2492                                           struct snd_ctl_elem_value *ucontrol)
2493 {
2494         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2495         struct conexant_spec *spec = codec->spec;
2496         const struct hda_input_mux *imux = &cxt5066_analog_mic_boost;
2497         unsigned int idx;
2498
2499         idx = ucontrol->value.enumerated.item[0];
2500         if (idx >= imux->num_items)
2501                 idx = imux->num_items - 1;
2502
2503         spec->dc_input_bias = idx;
2504         if (spec->dc_enable)
2505                 cxt5066_set_olpc_dc_bias(codec);
2506         return 1;
2507 }
2508
2509 static void cxt5066_olpc_capture_prepare(struct hda_codec *codec)
2510 {
2511         struct conexant_spec *spec = codec->spec;
2512         /* mark as recording and configure the microphone widget so that the
2513          * recording LED comes on. */
2514         spec->recording = 1;
2515         cxt5066_olpc_select_mic(codec);
2516 }
2517
2518 static void cxt5066_olpc_capture_cleanup(struct hda_codec *codec)
2519 {
2520         struct conexant_spec *spec = codec->spec;
2521         const struct hda_verb disable_mics[] = {
2522                 /* disable external mic, port B */
2523                 {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2524
2525                 /* disble internal mic, port C */
2526                 {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2527
2528                 /* disable DC capture, port F */
2529                 {0x1e, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2530                 {},
2531         };
2532
2533         snd_hda_sequence_write(codec, disable_mics);
2534         spec->recording = 0;
2535 }
2536
2537 static struct hda_input_mux cxt5066_capture_source = {
2538         .num_items = 4,
2539         .items = {
2540                 { "Mic B", 0 },
2541                 { "Mic C", 1 },
2542                 { "Mic E", 2 },
2543                 { "Mic F", 3 },
2544         },
2545 };
2546
2547 static struct hda_bind_ctls cxt5066_bind_capture_vol_others = {
2548         .ops = &snd_hda_bind_vol,
2549         .values = {
2550                 HDA_COMPOSE_AMP_VAL(0x14, 3, 0, HDA_INPUT),
2551                 HDA_COMPOSE_AMP_VAL(0x14, 3, 2, HDA_INPUT),
2552                 0
2553         },
2554 };
2555
2556 static struct hda_bind_ctls cxt5066_bind_capture_sw_others = {
2557         .ops = &snd_hda_bind_sw,
2558         .values = {
2559                 HDA_COMPOSE_AMP_VAL(0x14, 3, 0, HDA_INPUT),
2560                 HDA_COMPOSE_AMP_VAL(0x14, 3, 2, HDA_INPUT),
2561                 0
2562         },
2563 };
2564
2565 static struct snd_kcontrol_new cxt5066_mixer_master[] = {
2566         HDA_CODEC_VOLUME("Master Playback Volume", 0x10, 0x00, HDA_OUTPUT),
2567         {}
2568 };
2569
2570 static struct snd_kcontrol_new cxt5066_mixer_master_olpc[] = {
2571         {
2572                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2573                 .name = "Master Playback Volume",
2574                 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
2575                                   SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2576                                   SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK,
2577                 .subdevice = HDA_SUBDEV_AMP_FLAG,
2578                 .info = snd_hda_mixer_amp_volume_info,
2579                 .get = snd_hda_mixer_amp_volume_get,
2580                 .put = snd_hda_mixer_amp_volume_put,
2581                 .tlv = { .c = snd_hda_mixer_amp_tlv },
2582                 /* offset by 28 volume steps to limit minimum gain to -46dB */
2583                 .private_value =
2584                         HDA_COMPOSE_AMP_VAL_OFS(0x10, 3, 0, HDA_OUTPUT, 28),
2585         },
2586         {}
2587 };
2588
2589 static struct snd_kcontrol_new cxt5066_mixer_olpc_dc[] = {
2590         {
2591                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2592                 .name = "DC Mode Enable Switch",
2593                 .info = snd_ctl_boolean_mono_info,
2594                 .get = cxt5066_olpc_dc_get,
2595                 .put = cxt5066_olpc_dc_put,
2596         },
2597         {
2598                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2599                 .name = "DC Input Bias Enum",
2600                 .info = cxt5066_olpc_dc_bias_enum_info,
2601                 .get = cxt5066_olpc_dc_bias_enum_get,
2602                 .put = cxt5066_olpc_dc_bias_enum_put,
2603         },
2604         {}
2605 };
2606
2607 static struct snd_kcontrol_new cxt5066_mixers[] = {
2608         {
2609                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2610                 .name = "Master Playback Switch",
2611                 .info = cxt_eapd_info,
2612                 .get = cxt_eapd_get,
2613                 .put = cxt5066_hp_master_sw_put,
2614                 .private_value = 0x1d,
2615         },
2616
2617         {
2618                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2619                 .name = "Analog Mic Boost Capture Enum",
2620                 .info = cxt5066_mic_boost_mux_enum_info,
2621                 .get = cxt5066_mic_boost_mux_enum_get,
2622                 .put = cxt5066_mic_boost_mux_enum_put,
2623         },
2624
2625         HDA_BIND_VOL("Capture Volume", &cxt5066_bind_capture_vol_others),
2626         HDA_BIND_SW("Capture Switch", &cxt5066_bind_capture_sw_others),
2627         {}
2628 };
2629
2630 static struct snd_kcontrol_new cxt5066_vostro_mixers[] = {
2631         {
2632                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2633                 .name = "Int Mic Boost Capture Enum",
2634                 .info = cxt5066_mic_boost_mux_enum_info,
2635                 .get = cxt5066_mic_boost_mux_enum_get,
2636                 .put = cxt5066_mic_boost_mux_enum_put,
2637                 .private_value = 0x23 | 0x100,
2638         },
2639         HDA_CODEC_VOLUME_MONO("Beep Playback Volume", 0x13, 1, 0x0, HDA_OUTPUT),
2640         {}
2641 };
2642
2643 static struct hda_verb cxt5066_init_verbs[] = {
2644         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80}, /* Port B */
2645         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80}, /* Port C */
2646         {0x1e, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN}, /* Port F */
2647         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN}, /* Port E */
2648
2649         /* Speakers  */
2650         {0x1f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2651         {0x1f, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2652
2653         /* HP, Amp  */
2654         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
2655         {0x19, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2656
2657         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
2658         {0x1c, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2659
2660         /* DAC1 */
2661         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2662
2663         /* Node 14 connections: 0x17 0x18 0x23 0x24 0x27 */
2664         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x50},
2665         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2666         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2) | 0x50},
2667         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2668         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
2669
2670         /* no digital microphone support yet */
2671         {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2672
2673         /* Audio input selector */
2674         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x3},
2675
2676         /* SPDIF route: PCM */
2677         {0x20, AC_VERB_SET_CONNECT_SEL, 0x0},
2678         {0x22, AC_VERB_SET_CONNECT_SEL, 0x0},
2679
2680         {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2681         {0x22, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2682
2683         /* EAPD */
2684         {0x1d, AC_VERB_SET_EAPD_BTLENABLE, 0x2}, /* default on */
2685
2686         /* not handling these yet */
2687         {0x19, AC_VERB_SET_UNSOLICITED_ENABLE, 0},
2688         {0x1a, AC_VERB_SET_UNSOLICITED_ENABLE, 0},
2689         {0x1b, AC_VERB_SET_UNSOLICITED_ENABLE, 0},
2690         {0x1c, AC_VERB_SET_UNSOLICITED_ENABLE, 0},
2691         {0x1d, AC_VERB_SET_UNSOLICITED_ENABLE, 0},
2692         {0x1e, AC_VERB_SET_UNSOLICITED_ENABLE, 0},
2693         {0x20, AC_VERB_SET_UNSOLICITED_ENABLE, 0},
2694         {0x22, AC_VERB_SET_UNSOLICITED_ENABLE, 0},
2695         { } /* end */
2696 };
2697
2698 static struct hda_verb cxt5066_init_verbs_olpc[] = {
2699         /* Port A: headphones */
2700         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
2701         {0x19, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2702
2703         /* Port B: external microphone */
2704         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2705
2706         /* Port C: internal microphone */
2707         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2708
2709         /* Port D: unused */
2710         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2711
2712         /* Port E: unused, but has primary EAPD */
2713         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2714         {0x1d, AC_VERB_SET_EAPD_BTLENABLE, 0x2}, /* default on */
2715
2716         /* Port F: external DC input through microphone port */
2717         {0x1e, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2718
2719         /* Port G: internal speakers */
2720         {0x1f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2721         {0x1f, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2722
2723         /* DAC1 */
2724         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2725
2726         /* DAC2: unused */
2727         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2728
2729         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x50},
2730         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2731         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
2732         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2733         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2734         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2735         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
2736         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2737         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2738         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2739         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
2740         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2741
2742         /* Disable digital microphone port */
2743         {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2744
2745         /* Audio input selectors */
2746         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x3},
2747         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE },
2748
2749         /* Disable SPDIF */
2750         {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2751         {0x22, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2752
2753         /* enable unsolicited events for Port A and B */
2754         {0x19, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_HP_EVENT},
2755         {0x1a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_MIC_EVENT},
2756         { } /* end */
2757 };
2758
2759 static struct hda_verb cxt5066_init_verbs_vostro[] = {
2760         /* Port A: headphones */
2761         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2762         {0x19, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2763
2764         /* Port B: external microphone */
2765         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2766
2767         /* Port C: unused */
2768         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2769
2770         /* Port D: unused */
2771         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2772
2773         /* Port E: unused, but has primary EAPD */
2774         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2775         {0x1d, AC_VERB_SET_EAPD_BTLENABLE, 0x2}, /* default on */
2776
2777         /* Port F: unused */
2778         {0x1e, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2779
2780         /* Port G: internal speakers */
2781         {0x1f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2782         {0x1f, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2783
2784         /* DAC1 */
2785         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2786
2787         /* DAC2: unused */
2788         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2789
2790         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2791         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2792         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
2793         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2794         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2795         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2796         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
2797         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2798         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2799         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2800         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
2801         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2802
2803         /* Digital microphone port */
2804         {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
2805
2806         /* Audio input selectors */
2807         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x3},
2808         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE },
2809
2810         /* Disable SPDIF */
2811         {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2812         {0x22, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2813
2814         /* enable unsolicited events for Port A and B */
2815         {0x19, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_HP_EVENT},
2816         {0x1a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_MIC_EVENT},
2817         { } /* end */
2818 };
2819
2820 static struct hda_verb cxt5066_init_verbs_ideapad[] = {
2821         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80}, /* Port B */
2822         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80}, /* Port C */
2823         {0x1e, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN}, /* Port F */
2824         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN}, /* Port E */
2825
2826         /* Speakers  */
2827         {0x1f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2828         {0x1f, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2829
2830         /* HP, Amp  */
2831         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
2832         {0x19, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2833
2834         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
2835         {0x1c, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2836
2837         /* DAC1 */
2838         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2839
2840         /* Node 14 connections: 0x17 0x18 0x23 0x24 0x27 */
2841         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x50},
2842         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2843         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2) | 0x50},
2844         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2845         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
2846         {0x14, AC_VERB_SET_CONNECT_SEL, 2},     /* default to internal mic */
2847
2848         /* Audio input selector */
2849         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x2},
2850         {0x17, AC_VERB_SET_CONNECT_SEL, 1},     /* route ext mic */
2851
2852         /* SPDIF route: PCM */
2853         {0x20, AC_VERB_SET_CONNECT_SEL, 0x0},
2854         {0x22, AC_VERB_SET_CONNECT_SEL, 0x0},
2855
2856         {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2857         {0x22, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2858
2859         /* internal microphone */
2860         {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN}, /* enable int mic */
2861
2862         /* EAPD */
2863         {0x1d, AC_VERB_SET_EAPD_BTLENABLE, 0x2}, /* default on */
2864
2865         {0x19, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_HP_EVENT},
2866         {0x1b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_MIC_EVENT},
2867         { } /* end */
2868 };
2869
2870 static struct hda_verb cxt5066_init_verbs_thinkpad[] = {
2871         {0x1e, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN}, /* Port F */
2872         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN}, /* Port E */
2873
2874         /* Port G: internal speakers  */
2875         {0x1f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2876         {0x1f, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2877
2878         /* Port A: HP, Amp  */
2879         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2880         {0x19, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2881
2882         /* Port B: Mic Dock */
2883         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2884
2885         /* Port C: Mic */
2886         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2887
2888         /* Port D: HP Dock, Amp */
2889         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2890         {0x1c, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2891
2892         /* DAC1 */
2893         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2894
2895         /* Node 14 connections: 0x17 0x18 0x23 0x24 0x27 */
2896         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x50},
2897         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2898         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2) | 0x50},
2899         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2900         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
2901         {0x14, AC_VERB_SET_CONNECT_SEL, 2},     /* default to internal mic */
2902
2903         /* Audio input selector */
2904         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x2},
2905         {0x17, AC_VERB_SET_CONNECT_SEL, 1},     /* route ext mic */
2906
2907         /* SPDIF route: PCM */
2908         {0x20, AC_VERB_SET_CONNECT_SEL, 0x0},
2909         {0x22, AC_VERB_SET_CONNECT_SEL, 0x0},
2910
2911         {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2912         {0x22, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2913
2914         /* internal microphone */
2915         {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN}, /* enable int mic */
2916
2917         /* EAPD */
2918         {0x1d, AC_VERB_SET_EAPD_BTLENABLE, 0x2}, /* default on */
2919
2920         /* enable unsolicited events for Port A, B, C and D */
2921         {0x19, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_HP_EVENT},
2922         {0x1c, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_HP_EVENT},
2923         {0x1a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_MIC_EVENT},
2924         {0x1b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_MIC_EVENT},
2925         { } /* end */
2926 };
2927
2928 static struct hda_verb cxt5066_init_verbs_portd_lo[] = {
2929         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2930         { } /* end */
2931 };
2932
2933 /* initialize jack-sensing, too */
2934 static int cxt5066_init(struct hda_codec *codec)
2935 {
2936         struct conexant_spec *spec = codec->spec;
2937
2938         snd_printdd("CXT5066: init\n");
2939         conexant_init(codec);
2940         if (codec->patch_ops.unsol_event) {
2941                 cxt5066_hp_automute(codec);
2942                 if (spec->dell_vostro)
2943                         cxt5066_vostro_automic(codec);
2944                 else if (spec->ideapad)
2945                         cxt5066_ideapad_automic(codec);
2946                 else if (spec->thinkpad)
2947                         cxt5066_thinkpad_automic(codec);
2948         }
2949         cxt5066_set_mic_boost(codec);
2950         return 0;
2951 }
2952
2953 static int cxt5066_olpc_init(struct hda_codec *codec)
2954 {
2955         struct conexant_spec *spec = codec->spec;
2956         snd_printdd("CXT5066: init\n");
2957         conexant_init(codec);
2958         cxt5066_hp_automute(codec);
2959         if (!spec->dc_enable) {
2960                 cxt5066_set_mic_boost(codec);
2961                 cxt5066_olpc_automic(codec);
2962         } else {
2963                 cxt5066_enable_dc(codec);
2964         }
2965         return 0;
2966 }
2967
2968 enum {
2969         CXT5066_LAPTOP,         /* Laptops w/ EAPD support */
2970         CXT5066_DELL_LAPTOP,    /* Dell Laptop */
2971         CXT5066_OLPC_XO_1_5,    /* OLPC XO 1.5 */
2972         CXT5066_DELL_VOSTO,     /* Dell Vostro 1015i */
2973         CXT5066_IDEAPAD,        /* Lenovo IdeaPad U150 */
2974         CXT5066_THINKPAD,       /* Lenovo ThinkPad T410s, others? */
2975         CXT5066_MODELS
2976 };
2977
2978 static const char *cxt5066_models[CXT5066_MODELS] = {
2979         [CXT5066_LAPTOP]        = "laptop",
2980         [CXT5066_DELL_LAPTOP]   = "dell-laptop",
2981         [CXT5066_OLPC_XO_1_5]   = "olpc-xo-1_5",
2982         [CXT5066_DELL_VOSTO]    = "dell-vostro",
2983         [CXT5066_IDEAPAD]       = "ideapad",
2984         [CXT5066_THINKPAD]      = "thinkpad",
2985 };
2986
2987 static struct snd_pci_quirk cxt5066_cfg_tbl[] = {
2988         SND_PCI_QUIRK(0x14f1, 0x0101, "Conexant Reference board",
2989                       CXT5066_LAPTOP),
2990         SND_PCI_QUIRK(0x1028, 0x02f5, "Dell",
2991                       CXT5066_DELL_LAPTOP),
2992         SND_PCI_QUIRK(0x152d, 0x0833, "OLPC XO-1.5", CXT5066_OLPC_XO_1_5),
2993         SND_PCI_QUIRK(0x1028, 0x0402, "Dell Vostro", CXT5066_DELL_VOSTO),
2994         SND_PCI_QUIRK(0x1028, 0x0408, "Dell Inspiron One 19T", CXT5066_IDEAPAD),
2995         SND_PCI_QUIRK(0x1179, 0xff50, "Toshiba Satellite P500-PSPGSC-01800T", CXT5066_OLPC_XO_1_5),
2996         SND_PCI_QUIRK(0x1179, 0xffe0, "Toshiba Satellite Pro T130-15F", CXT5066_OLPC_XO_1_5),
2997         SND_PCI_QUIRK(0x17aa, 0x21b2, "Thinkpad X100e", CXT5066_IDEAPAD),
2998         SND_PCI_QUIRK(0x17aa, 0x21b3, "Thinkpad Edge 13 (197)", CXT5066_IDEAPAD),
2999         SND_PCI_QUIRK(0x17aa, 0x21b4, "Thinkpad Edge", CXT5066_IDEAPAD),
3000         SND_PCI_QUIRK(0x17aa, 0x3a0d, "ideapad", CXT5066_IDEAPAD),
3001         SND_PCI_QUIRK(0x17aa, 0x215e, "Lenovo Thinkpad", CXT5066_THINKPAD),
3002         {}
3003 };
3004
3005 static int patch_cxt5066(struct hda_codec *codec)
3006 {
3007         struct conexant_spec *spec;
3008         int board_config;
3009
3010         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
3011         if (!spec)
3012                 return -ENOMEM;
3013         codec->spec = spec;
3014
3015         codec->patch_ops = conexant_patch_ops;
3016         codec->patch_ops.init = conexant_init;
3017
3018         spec->dell_automute = 0;
3019         spec->multiout.max_channels = 2;
3020         spec->multiout.num_dacs = ARRAY_SIZE(cxt5066_dac_nids);
3021         spec->multiout.dac_nids = cxt5066_dac_nids;
3022         spec->multiout.dig_out_nid = CXT5066_SPDIF_OUT;
3023         spec->num_adc_nids = 1;
3024         spec->adc_nids = cxt5066_adc_nids;
3025         spec->capsrc_nids = cxt5066_capsrc_nids;
3026         spec->input_mux = &cxt5066_capture_source;
3027
3028         spec->port_d_mode = PIN_HP;
3029
3030         spec->num_init_verbs = 1;
3031         spec->init_verbs[0] = cxt5066_init_verbs;
3032         spec->num_channel_mode = ARRAY_SIZE(cxt5066_modes);
3033         spec->channel_mode = cxt5066_modes;
3034         spec->cur_adc = 0;
3035         spec->cur_adc_idx = 0;
3036
3037         board_config = snd_hda_check_board_config(codec, CXT5066_MODELS,
3038                                                   cxt5066_models, cxt5066_cfg_tbl);
3039         switch (board_config) {
3040         default:
3041         case CXT5066_LAPTOP:
3042                 spec->mixers[spec->num_mixers++] = cxt5066_mixer_master;
3043                 spec->mixers[spec->num_mixers++] = cxt5066_mixers;
3044                 break;
3045         case CXT5066_DELL_LAPTOP:
3046                 spec->mixers[spec->num_mixers++] = cxt5066_mixer_master;
3047                 spec->mixers[spec->num_mixers++] = cxt5066_mixers;
3048
3049                 spec->port_d_mode = PIN_OUT;
3050                 spec->init_verbs[spec->num_init_verbs] = cxt5066_init_verbs_portd_lo;
3051                 spec->num_init_verbs++;
3052                 spec->dell_automute = 1;
3053                 break;
3054         case CXT5066_OLPC_XO_1_5:
3055                 codec->patch_ops.init = cxt5066_olpc_init;
3056                 codec->patch_ops.unsol_event = cxt5066_olpc_unsol_event;
3057                 spec->init_verbs[0] = cxt5066_init_verbs_olpc;
3058                 spec->mixers[spec->num_mixers++] = cxt5066_mixer_master_olpc;
3059                 spec->mixers[spec->num_mixers++] = cxt5066_mixer_olpc_dc;
3060                 spec->mixers[spec->num_mixers++] = cxt5066_mixers;
3061                 spec->port_d_mode = 0;
3062                 spec->mic_boost = 3; /* default 30dB gain */
3063
3064                 /* no S/PDIF out */
3065                 spec->multiout.dig_out_nid = 0;
3066
3067                 /* input source automatically selected */
3068                 spec->input_mux = NULL;
3069
3070                 /* our capture hooks which allow us to turn on the microphone LED
3071                  * at the right time */
3072                 spec->capture_prepare = cxt5066_olpc_capture_prepare;
3073                 spec->capture_cleanup = cxt5066_olpc_capture_cleanup;
3074                 break;
3075         case CXT5066_DELL_VOSTO:
3076                 codec->patch_ops.init = cxt5066_init;
3077                 codec->patch_ops.unsol_event = cxt5066_vostro_event;
3078                 spec->init_verbs[0] = cxt5066_init_verbs_vostro;
3079                 spec->mixers[spec->num_mixers++] = cxt5066_mixer_master_olpc;
3080                 spec->mixers[spec->num_mixers++] = cxt5066_mixers;
3081                 spec->mixers[spec->num_mixers++] = cxt5066_vostro_mixers;
3082                 spec->port_d_mode = 0;
3083                 spec->dell_vostro = 1;
3084                 spec->mic_boost = 3; /* default 30dB gain */
3085                 snd_hda_attach_beep_device(codec, 0x13);
3086
3087                 /* no S/PDIF out */
3088                 spec->multiout.dig_out_nid = 0;
3089
3090                 /* input source automatically selected */
3091                 spec->input_mux = NULL;
3092                 break;
3093         case CXT5066_IDEAPAD:
3094                 codec->patch_ops.init = cxt5066_init;
3095                 codec->patch_ops.unsol_event = cxt5066_ideapad_event;
3096                 spec->mixers[spec->num_mixers++] = cxt5066_mixer_master;
3097                 spec->mixers[spec->num_mixers++] = cxt5066_mixers;
3098                 spec->init_verbs[0] = cxt5066_init_verbs_ideapad;
3099                 spec->port_d_mode = 0;
3100                 spec->ideapad = 1;
3101                 spec->mic_boost = 2;    /* default 20dB gain */
3102
3103                 /* no S/PDIF out */
3104                 spec->multiout.dig_out_nid = 0;
3105
3106                 /* input source automatically selected */
3107                 spec->input_mux = NULL;
3108                 break;
3109         case CXT5066_THINKPAD:
3110                 codec->patch_ops.init = cxt5066_init;
3111                 codec->patch_ops.unsol_event = cxt5066_thinkpad_event;
3112                 spec->mixers[spec->num_mixers++] = cxt5066_mixer_master;
3113                 spec->mixers[spec->num_mixers++] = cxt5066_mixers;
3114                 spec->init_verbs[0] = cxt5066_init_verbs_thinkpad;
3115                 spec->thinkpad = 1;
3116                 spec->port_d_mode = PIN_OUT;
3117                 spec->mic_boost = 2;    /* default 20dB gain */
3118
3119                 /* no S/PDIF out */
3120                 spec->multiout.dig_out_nid = 0;
3121
3122                 /* input source automatically selected */
3123                 spec->input_mux = NULL;
3124                 break;
3125         }
3126
3127         return 0;
3128 }
3129
3130 /*
3131  */
3132
3133 static struct hda_codec_preset snd_hda_preset_conexant[] = {
3134         { .id = 0x14f15045, .name = "CX20549 (Venice)",
3135           .patch = patch_cxt5045 },
3136         { .id = 0x14f15047, .name = "CX20551 (Waikiki)",
3137           .patch = patch_cxt5047 },
3138         { .id = 0x14f15051, .name = "CX20561 (Hermosa)",
3139           .patch = patch_cxt5051 },
3140         { .id = 0x14f15066, .name = "CX20582 (Pebble)",
3141           .patch = patch_cxt5066 },
3142         { .id = 0x14f15067, .name = "CX20583 (Pebble HSF)",
3143           .patch = patch_cxt5066 },
3144         { .id = 0x14f15069, .name = "CX20585",
3145           .patch = patch_cxt5066 },
3146         {} /* terminator */
3147 };
3148
3149 MODULE_ALIAS("snd-hda-codec-id:14f15045");
3150 MODULE_ALIAS("snd-hda-codec-id:14f15047");
3151 MODULE_ALIAS("snd-hda-codec-id:14f15051");
3152 MODULE_ALIAS("snd-hda-codec-id:14f15066");
3153 MODULE_ALIAS("snd-hda-codec-id:14f15067");
3154 MODULE_ALIAS("snd-hda-codec-id:14f15069");
3155
3156 MODULE_LICENSE("GPL");
3157 MODULE_DESCRIPTION("Conexant HD-audio codec");
3158
3159 static struct hda_codec_preset_list conexant_list = {
3160         .preset = snd_hda_preset_conexant,
3161         .owner = THIS_MODULE,
3162 };
3163
3164 static int __init patch_conexant_init(void)
3165 {
3166         return snd_hda_add_codec_preset(&conexant_list);
3167 }
3168
3169 static void __exit patch_conexant_exit(void)
3170 {
3171         snd_hda_delete_codec_preset(&conexant_list);
3172 }
3173
3174 module_init(patch_conexant_init)
3175 module_exit(patch_conexant_exit)