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