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