ALSA: hda - Fix pin configuration of HP Pavilion dv7
[pandora-kernel.git] / sound / pci / hda / patch_conexant.c
1 /*
2  * HD audio interface patch for Conexant HDA audio codec
3  *
4  * Copyright (c) 2006 Pototskiy Akex <alex.pototskiy@gmail.com>
5  *                    Takashi Iwai <tiwai@suse.de>
6  *                    Tobin Davis  <tdavis@dsl-only.net>
7  *
8  *  This driver is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This driver is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21  */
22
23 #include <linux/init.h>
24 #include <linux/delay.h>
25 #include <linux/slab.h>
26 #include <linux/pci.h>
27 #include <linux/module.h>
28 #include <sound/core.h>
29 #include <sound/jack.h>
30
31 #include "hda_codec.h"
32 #include "hda_local.h"
33 #include "hda_auto_parser.h"
34 #include "hda_beep.h"
35 #include "hda_jack.h"
36
37 #define CXT_PIN_DIR_IN              0x00
38 #define CXT_PIN_DIR_OUT             0x01
39 #define CXT_PIN_DIR_INOUT           0x02
40 #define CXT_PIN_DIR_IN_NOMICBIAS    0x03
41 #define CXT_PIN_DIR_INOUT_NOMICBIAS 0x04
42
43 #define CONEXANT_HP_EVENT       0x37
44 #define CONEXANT_MIC_EVENT      0x38
45 #define CONEXANT_LINE_EVENT     0x39
46
47 /* Conexant 5051 specific */
48
49 #define CXT5051_SPDIF_OUT       0x12
50 #define CXT5051_PORTB_EVENT     0x38
51 #define CXT5051_PORTC_EVENT     0x39
52
53 #define AUTO_MIC_PORTB          (1 << 1)
54 #define AUTO_MIC_PORTC          (1 << 2)
55
56 struct pin_dac_pair {
57         hda_nid_t pin;
58         hda_nid_t dac;
59         int type;
60 };
61
62 struct imux_info {
63         hda_nid_t pin;          /* input pin NID */
64         hda_nid_t adc;          /* connected ADC NID */ 
65         hda_nid_t boost;        /* optional boost volume NID */
66         int index;              /* corresponding to autocfg.input */
67 };
68
69 struct conexant_spec {
70         struct hda_gen_spec gen;
71
72         const struct snd_kcontrol_new *mixers[5];
73         int num_mixers;
74         hda_nid_t vmaster_nid;
75         struct hda_vmaster_mute_hook vmaster_mute;
76         bool vmaster_mute_led;
77
78         const struct hda_verb *init_verbs[5];   /* initialization verbs
79                                                  * don't forget NULL
80                                                  * termination!
81                                                  */
82         unsigned int num_init_verbs;
83
84         /* playback */
85         struct hda_multi_out multiout;  /* playback set-up
86                                          * max_channels, dacs must be set
87                                          * dig_out_nid and hp_nid are optional
88                                          */
89         unsigned int cur_eapd;
90         unsigned int hp_present;
91         unsigned int line_present;
92         unsigned int auto_mic;
93         int auto_mic_ext;               /* imux_pins[] index for ext mic */
94         int auto_mic_dock;              /* imux_pins[] index for dock mic */
95         int auto_mic_int;               /* imux_pins[] index for int mic */
96         unsigned int need_dac_fix;
97         hda_nid_t slave_dig_outs[2];
98
99         /* capture */
100         unsigned int num_adc_nids;
101         const hda_nid_t *adc_nids;
102         hda_nid_t dig_in_nid;           /* digital-in NID; optional */
103
104         unsigned int cur_adc_idx;
105         hda_nid_t cur_adc;
106         unsigned int cur_adc_stream_tag;
107         unsigned int cur_adc_format;
108
109         const struct hda_pcm_stream *capture_stream;
110
111         /* capture source */
112         const struct hda_input_mux *input_mux;
113         const hda_nid_t *capsrc_nids;
114         unsigned int cur_mux[3];
115
116         /* channel model */
117         const struct hda_channel_mode *channel_mode;
118         int num_channel_mode;
119
120         /* PCM information */
121         struct hda_pcm pcm_rec[2];      /* used in build_pcms() */
122
123         unsigned int spdif_route;
124
125         /* dynamic controls, init_verbs and input_mux */
126         struct auto_pin_cfg autocfg;
127         struct hda_input_mux private_imux;
128         struct imux_info imux_info[HDA_MAX_NUM_INPUTS];
129         hda_nid_t private_adc_nids[HDA_MAX_NUM_INPUTS];
130         hda_nid_t private_dac_nids[AUTO_CFG_MAX_OUTS];
131         struct pin_dac_pair dac_info[8];
132         int dac_info_filled;
133
134         unsigned int port_d_mode;
135         unsigned int auto_mute:1;       /* used in auto-parser */
136         unsigned int detect_line:1;     /* Line-out detection enabled */
137         unsigned int automute_lines:1;  /* automute line-out as well */
138         unsigned int automute_hp_lo:1;  /* both HP and LO available */
139         unsigned int dell_automute:1;
140         unsigned int dell_vostro:1;
141         unsigned int ideapad:1;
142         unsigned int thinkpad:1;
143         unsigned int hp_laptop:1;
144         unsigned int asus:1;
145         unsigned int pin_eapd_ctrls:1;
146         unsigned int fixup_stereo_dmic:1;
147
148         unsigned int adc_switching:1;
149
150         unsigned int ext_mic_present;
151         unsigned int recording;
152         void (*capture_prepare)(struct hda_codec *codec);
153         void (*capture_cleanup)(struct hda_codec *codec);
154
155         /* OLPC XO-1.5 supports DC input mode (e.g. for use with analog sensors)
156          * through the microphone jack.
157          * When the user enables this through a mixer switch, both internal and
158          * external microphones are disabled. Gain is fixed at 0dB. In this mode,
159          * we also allow the bias to be configured through a separate mixer
160          * control. */
161         unsigned int dc_enable;
162         unsigned int dc_input_bias; /* offset into cxt5066_olpc_dc_bias */
163         unsigned int mic_boost; /* offset into cxt5066_analog_mic_boost */
164
165         unsigned int beep_amp;
166
167         /* extra EAPD pins */
168         unsigned int num_eapds;
169         hda_nid_t eapds[4];
170 };
171
172 static int conexant_playback_pcm_open(struct hda_pcm_stream *hinfo,
173                                       struct hda_codec *codec,
174                                       struct snd_pcm_substream *substream)
175 {
176         struct conexant_spec *spec = codec->spec;
177         return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
178                                              hinfo);
179 }
180
181 static int conexant_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
182                                          struct hda_codec *codec,
183                                          unsigned int stream_tag,
184                                          unsigned int format,
185                                          struct snd_pcm_substream *substream)
186 {
187         struct conexant_spec *spec = codec->spec;
188         return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
189                                                 stream_tag,
190                                                 format, substream);
191 }
192
193 static int conexant_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
194                                          struct hda_codec *codec,
195                                          struct snd_pcm_substream *substream)
196 {
197         struct conexant_spec *spec = codec->spec;
198         return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
199 }
200
201 /*
202  * Digital out
203  */
204 static int conexant_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
205                                           struct hda_codec *codec,
206                                           struct snd_pcm_substream *substream)
207 {
208         struct conexant_spec *spec = codec->spec;
209         return snd_hda_multi_out_dig_open(codec, &spec->multiout);
210 }
211
212 static int conexant_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
213                                          struct hda_codec *codec,
214                                          struct snd_pcm_substream *substream)
215 {
216         struct conexant_spec *spec = codec->spec;
217         return snd_hda_multi_out_dig_close(codec, &spec->multiout);
218 }
219
220 static int conexant_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
221                                          struct hda_codec *codec,
222                                          unsigned int stream_tag,
223                                          unsigned int format,
224                                          struct snd_pcm_substream *substream)
225 {
226         struct conexant_spec *spec = codec->spec;
227         return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
228                                              stream_tag,
229                                              format, substream);
230 }
231
232 /*
233  * Analog capture
234  */
235 static int conexant_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
236                                       struct hda_codec *codec,
237                                       unsigned int stream_tag,
238                                       unsigned int format,
239                                       struct snd_pcm_substream *substream)
240 {
241         struct conexant_spec *spec = codec->spec;
242         if (spec->capture_prepare)
243                 spec->capture_prepare(codec);
244         snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
245                                    stream_tag, 0, format);
246         return 0;
247 }
248
249 static int conexant_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
250                                       struct hda_codec *codec,
251                                       struct snd_pcm_substream *substream)
252 {
253         struct conexant_spec *spec = codec->spec;
254         snd_hda_codec_cleanup_stream(codec, spec->adc_nids[substream->number]);
255         if (spec->capture_cleanup)
256                 spec->capture_cleanup(codec);
257         return 0;
258 }
259
260
261
262 static const struct hda_pcm_stream conexant_pcm_analog_playback = {
263         .substreams = 1,
264         .channels_min = 2,
265         .channels_max = 2,
266         .nid = 0, /* fill later */
267         .ops = {
268                 .open = conexant_playback_pcm_open,
269                 .prepare = conexant_playback_pcm_prepare,
270                 .cleanup = conexant_playback_pcm_cleanup
271         },
272 };
273
274 static const struct hda_pcm_stream conexant_pcm_analog_capture = {
275         .substreams = 1,
276         .channels_min = 2,
277         .channels_max = 2,
278         .nid = 0, /* fill later */
279         .ops = {
280                 .prepare = conexant_capture_pcm_prepare,
281                 .cleanup = conexant_capture_pcm_cleanup
282         },
283 };
284
285
286 static const struct hda_pcm_stream conexant_pcm_digital_playback = {
287         .substreams = 1,
288         .channels_min = 2,
289         .channels_max = 2,
290         .nid = 0, /* fill later */
291         .ops = {
292                 .open = conexant_dig_playback_pcm_open,
293                 .close = conexant_dig_playback_pcm_close,
294                 .prepare = conexant_dig_playback_pcm_prepare
295         },
296 };
297
298 static const struct hda_pcm_stream conexant_pcm_digital_capture = {
299         .substreams = 1,
300         .channels_min = 2,
301         .channels_max = 2,
302         /* NID is set in alc_build_pcms */
303 };
304
305 static int cx5051_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
306                                       struct hda_codec *codec,
307                                       unsigned int stream_tag,
308                                       unsigned int format,
309                                       struct snd_pcm_substream *substream)
310 {
311         struct conexant_spec *spec = codec->spec;
312         spec->cur_adc = spec->adc_nids[spec->cur_adc_idx];
313         spec->cur_adc_stream_tag = stream_tag;
314         spec->cur_adc_format = format;
315         snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
316         return 0;
317 }
318
319 static int cx5051_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
320                                       struct hda_codec *codec,
321                                       struct snd_pcm_substream *substream)
322 {
323         struct conexant_spec *spec = codec->spec;
324         snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
325         spec->cur_adc = 0;
326         return 0;
327 }
328
329 static const struct hda_pcm_stream cx5051_pcm_analog_capture = {
330         .substreams = 1,
331         .channels_min = 2,
332         .channels_max = 2,
333         .nid = 0, /* fill later */
334         .ops = {
335                 .prepare = cx5051_capture_pcm_prepare,
336                 .cleanup = cx5051_capture_pcm_cleanup
337         },
338 };
339
340 static bool is_2_1_speaker(struct conexant_spec *spec);
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 (is_2_1_speaker(spec))
357                 info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
358                         snd_pcm_2_1_chmaps;
359         if (spec->capture_stream)
360                 info->stream[SNDRV_PCM_STREAM_CAPTURE] = *spec->capture_stream;
361         else {
362                 if (codec->vendor_id == 0x14f15051)
363                         info->stream[SNDRV_PCM_STREAM_CAPTURE] =
364                                 cx5051_pcm_analog_capture;
365                 else {
366                         info->stream[SNDRV_PCM_STREAM_CAPTURE] =
367                                 conexant_pcm_analog_capture;
368                         info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
369                                 spec->num_adc_nids;
370                 }
371         }
372         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
373
374         if (spec->multiout.dig_out_nid) {
375                 info++;
376                 codec->num_pcms++;
377                 info->name = "Conexant Digital";
378                 info->pcm_type = HDA_PCM_TYPE_SPDIF;
379                 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
380                         conexant_pcm_digital_playback;
381                 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
382                         spec->multiout.dig_out_nid;
383                 if (spec->dig_in_nid) {
384                         info->stream[SNDRV_PCM_STREAM_CAPTURE] =
385                                 conexant_pcm_digital_capture;
386                         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
387                                 spec->dig_in_nid;
388                 }
389                 if (spec->slave_dig_outs[0])
390                         codec->slave_dig_outs = spec->slave_dig_outs;
391         }
392
393         return 0;
394 }
395
396 static int conexant_mux_enum_info(struct snd_kcontrol *kcontrol,
397                                   struct snd_ctl_elem_info *uinfo)
398 {
399         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
400         struct conexant_spec *spec = codec->spec;
401
402         return snd_hda_input_mux_info(spec->input_mux, uinfo);
403 }
404
405 static int conexant_mux_enum_get(struct snd_kcontrol *kcontrol,
406                                  struct snd_ctl_elem_value *ucontrol)
407 {
408         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
409         struct conexant_spec *spec = codec->spec;
410         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
411
412         ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
413         return 0;
414 }
415
416 static int conexant_mux_enum_put(struct snd_kcontrol *kcontrol,
417                                  struct snd_ctl_elem_value *ucontrol)
418 {
419         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
420         struct conexant_spec *spec = codec->spec;
421         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
422
423         return snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
424                                      spec->capsrc_nids[adc_idx],
425                                      &spec->cur_mux[adc_idx]);
426 }
427
428 static void conexant_set_power(struct hda_codec *codec, hda_nid_t fg,
429                                unsigned int power_state)
430 {
431         if (power_state == AC_PWRST_D3)
432                 msleep(100);
433         snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
434                             power_state);
435         /* partial workaround for "azx_get_response timeout" */
436         if (power_state == AC_PWRST_D0)
437                 msleep(10);
438         snd_hda_codec_set_power_to_all(codec, fg, power_state, true);
439 }
440
441 static int conexant_init(struct hda_codec *codec)
442 {
443         struct conexant_spec *spec = codec->spec;
444         int i;
445
446         for (i = 0; i < spec->num_init_verbs; i++)
447                 snd_hda_sequence_write(codec, spec->init_verbs[i]);
448         return 0;
449 }
450
451 static void conexant_free(struct hda_codec *codec)
452 {
453         struct conexant_spec *spec = codec->spec;
454         snd_hda_gen_free(&spec->gen);
455         snd_hda_detach_beep_device(codec);
456         kfree(spec);
457 }
458
459 static const struct snd_kcontrol_new cxt_capture_mixers[] = {
460         {
461                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
462                 .name = "Capture Source",
463                 .info = conexant_mux_enum_info,
464                 .get = conexant_mux_enum_get,
465                 .put = conexant_mux_enum_put
466         },
467         {}
468 };
469
470 #ifdef CONFIG_SND_HDA_INPUT_BEEP
471 /* additional beep mixers; the actual parameters are overwritten at build */
472 static const struct snd_kcontrol_new cxt_beep_mixer[] = {
473         HDA_CODEC_VOLUME_MONO("Beep Playback Volume", 0, 1, 0, HDA_OUTPUT),
474         HDA_CODEC_MUTE_BEEP_MONO("Beep Playback Switch", 0, 1, 0, HDA_OUTPUT),
475         { } /* end */
476 };
477 #endif
478
479 static const char * const slave_pfxs[] = {
480         "Headphone", "Speaker", "Bass Speaker", "Front", "Surround", "CLFE",
481         NULL
482 };
483
484 static int conexant_build_controls(struct hda_codec *codec)
485 {
486         struct conexant_spec *spec = codec->spec;
487         unsigned int i;
488         int err;
489
490         for (i = 0; i < spec->num_mixers; i++) {
491                 err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
492                 if (err < 0)
493                         return err;
494         }
495         if (spec->multiout.dig_out_nid) {
496                 err = snd_hda_create_spdif_out_ctls(codec,
497                                                     spec->multiout.dig_out_nid,
498                                                     spec->multiout.dig_out_nid);
499                 if (err < 0)
500                         return err;
501                 err = snd_hda_create_spdif_share_sw(codec,
502                                                     &spec->multiout);
503                 if (err < 0)
504                         return err;
505                 spec->multiout.share_spdif = 1;
506         } 
507         if (spec->dig_in_nid) {
508                 err = snd_hda_create_spdif_in_ctls(codec,spec->dig_in_nid);
509                 if (err < 0)
510                         return err;
511         }
512
513         /* if we have no master control, let's create it */
514         if (spec->vmaster_nid &&
515             !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
516                 unsigned int vmaster_tlv[4];
517                 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
518                                         HDA_OUTPUT, vmaster_tlv);
519                 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
520                                           vmaster_tlv, slave_pfxs,
521                                           "Playback Volume");
522                 if (err < 0)
523                         return err;
524         }
525         if (spec->vmaster_nid &&
526             !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
527                 err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
528                                             NULL, slave_pfxs,
529                                             "Playback Switch", true,
530                                             &spec->vmaster_mute.sw_kctl);
531                 if (err < 0)
532                         return err;
533         }
534
535         if (spec->input_mux) {
536                 err = snd_hda_add_new_ctls(codec, cxt_capture_mixers);
537                 if (err < 0)
538                         return err;
539         }
540
541 #ifdef CONFIG_SND_HDA_INPUT_BEEP
542         /* create beep controls if needed */
543         if (spec->beep_amp) {
544                 const struct snd_kcontrol_new *knew;
545                 for (knew = cxt_beep_mixer; knew->name; knew++) {
546                         struct snd_kcontrol *kctl;
547                         kctl = snd_ctl_new1(knew, codec);
548                         if (!kctl)
549                                 return -ENOMEM;
550                         kctl->private_value = spec->beep_amp;
551                         err = snd_hda_ctl_add(codec, 0, kctl);
552                         if (err < 0)
553                                 return err;
554                 }
555         }
556 #endif
557
558         return 0;
559 }
560
561 #ifdef CONFIG_PM
562 static int conexant_suspend(struct hda_codec *codec)
563 {
564         snd_hda_shutup_pins(codec);
565         return 0;
566 }
567 #endif
568
569 static const struct hda_codec_ops conexant_patch_ops = {
570         .build_controls = conexant_build_controls,
571         .build_pcms = conexant_build_pcms,
572         .init = conexant_init,
573         .free = conexant_free,
574         .set_power_state = conexant_set_power,
575 #ifdef CONFIG_PM
576         .suspend = conexant_suspend,
577 #endif
578         .reboot_notify = snd_hda_shutup_pins,
579 };
580
581 #ifdef CONFIG_SND_HDA_INPUT_BEEP
582 #define set_beep_amp(spec, nid, idx, dir) \
583         ((spec)->beep_amp = HDA_COMPOSE_AMP_VAL(nid, 1, idx, dir))
584 #else
585 #define set_beep_amp(spec, nid, idx, dir) /* NOP */
586 #endif
587
588 static int patch_conexant_auto(struct hda_codec *codec);
589 /*
590  * EAPD control
591  * the private value = nid | (invert << 8)
592  */
593
594 #define cxt_eapd_info           snd_ctl_boolean_mono_info
595
596 static int cxt_eapd_get(struct snd_kcontrol *kcontrol,
597                              struct snd_ctl_elem_value *ucontrol)
598 {
599         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
600         struct conexant_spec *spec = codec->spec;
601         int invert = (kcontrol->private_value >> 8) & 1;
602         if (invert)
603                 ucontrol->value.integer.value[0] = !spec->cur_eapd;
604         else
605                 ucontrol->value.integer.value[0] = spec->cur_eapd;
606         return 0;
607
608 }
609
610 static int cxt_eapd_put(struct snd_kcontrol *kcontrol,
611                              struct snd_ctl_elem_value *ucontrol)
612 {
613         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
614         struct conexant_spec *spec = codec->spec;
615         int invert = (kcontrol->private_value >> 8) & 1;
616         hda_nid_t nid = kcontrol->private_value & 0xff;
617         unsigned int eapd;
618
619         eapd = !!ucontrol->value.integer.value[0];
620         if (invert)
621                 eapd = !eapd;
622         if (eapd == spec->cur_eapd)
623                 return 0;
624         
625         spec->cur_eapd = eapd;
626         snd_hda_codec_write_cache(codec, nid,
627                                   0, AC_VERB_SET_EAPD_BTLENABLE,
628                                   eapd ? 0x02 : 0x00);
629         return 1;
630 }
631
632 /* controls for test mode */
633 #ifdef CONFIG_SND_DEBUG
634
635 #define CXT_EAPD_SWITCH(xname, nid, mask) \
636         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = 0,  \
637           .info = cxt_eapd_info, \
638           .get = cxt_eapd_get, \
639           .put = cxt_eapd_put, \
640           .private_value = nid | (mask<<16) }
641
642
643
644 static int conexant_ch_mode_info(struct snd_kcontrol *kcontrol,
645                                  struct snd_ctl_elem_info *uinfo)
646 {
647         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
648         struct conexant_spec *spec = codec->spec;
649         return snd_hda_ch_mode_info(codec, uinfo, spec->channel_mode,
650                                     spec->num_channel_mode);
651 }
652
653 static int conexant_ch_mode_get(struct snd_kcontrol *kcontrol,
654                                 struct snd_ctl_elem_value *ucontrol)
655 {
656         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
657         struct conexant_spec *spec = codec->spec;
658         return snd_hda_ch_mode_get(codec, ucontrol, spec->channel_mode,
659                                    spec->num_channel_mode,
660                                    spec->multiout.max_channels);
661 }
662
663 static int conexant_ch_mode_put(struct snd_kcontrol *kcontrol,
664                                 struct snd_ctl_elem_value *ucontrol)
665 {
666         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
667         struct conexant_spec *spec = codec->spec;
668         int err = snd_hda_ch_mode_put(codec, ucontrol, spec->channel_mode,
669                                       spec->num_channel_mode,
670                                       &spec->multiout.max_channels);
671         if (err >= 0 && spec->need_dac_fix)
672                 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
673         return err;
674 }
675
676 #define CXT_PIN_MODE(xname, nid, dir) \
677         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = 0,  \
678           .info = conexant_ch_mode_info, \
679           .get = conexant_ch_mode_get, \
680           .put = conexant_ch_mode_put, \
681           .private_value = nid | (dir<<16) }
682
683 #endif /* CONFIG_SND_DEBUG */
684
685 /* Conexant 5045 specific */
686
687 static const hda_nid_t cxt5045_dac_nids[1] = { 0x19 };
688 static const hda_nid_t cxt5045_adc_nids[1] = { 0x1a };
689 static const hda_nid_t cxt5045_capsrc_nids[1] = { 0x1a };
690 #define CXT5045_SPDIF_OUT       0x18
691
692 static const struct hda_channel_mode cxt5045_modes[1] = {
693         { 2, NULL },
694 };
695
696 static const struct hda_input_mux cxt5045_capture_source = {
697         .num_items = 2,
698         .items = {
699                 { "Internal Mic", 0x1 },
700                 { "Mic",          0x2 },
701         }
702 };
703
704 static const struct hda_input_mux cxt5045_capture_source_benq = {
705         .num_items = 4,
706         .items = {
707                 { "Internal Mic", 0x1 },
708                 { "Mic",          0x2 },
709                 { "Line",         0x3 },
710                 { "Mixer",        0x0 },
711         }
712 };
713
714 static const struct hda_input_mux cxt5045_capture_source_hp530 = {
715         .num_items = 2,
716         .items = {
717                 { "Mic",          0x1 },
718                 { "Internal Mic", 0x2 },
719         }
720 };
721
722 /* turn on/off EAPD (+ mute HP) as a master switch */
723 static int cxt5045_hp_master_sw_put(struct snd_kcontrol *kcontrol,
724                                     struct snd_ctl_elem_value *ucontrol)
725 {
726         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
727         struct conexant_spec *spec = codec->spec;
728         unsigned int bits;
729
730         if (!cxt_eapd_put(kcontrol, ucontrol))
731                 return 0;
732
733         /* toggle internal speakers mute depending of presence of
734          * the headphone jack
735          */
736         bits = (!spec->hp_present && spec->cur_eapd) ? 0 : HDA_AMP_MUTE;
737         snd_hda_codec_amp_stereo(codec, 0x10, HDA_OUTPUT, 0,
738                                  HDA_AMP_MUTE, bits);
739
740         bits = spec->cur_eapd ? 0 : HDA_AMP_MUTE;
741         snd_hda_codec_amp_stereo(codec, 0x11, HDA_OUTPUT, 0,
742                                  HDA_AMP_MUTE, bits);
743         return 1;
744 }
745
746 /* bind volumes of both NID 0x10 and 0x11 */
747 static const struct hda_bind_ctls cxt5045_hp_bind_master_vol = {
748         .ops = &snd_hda_bind_vol,
749         .values = {
750                 HDA_COMPOSE_AMP_VAL(0x10, 3, 0, HDA_OUTPUT),
751                 HDA_COMPOSE_AMP_VAL(0x11, 3, 0, HDA_OUTPUT),
752                 0
753         },
754 };
755
756 /* toggle input of built-in and mic jack appropriately */
757 static void cxt5045_hp_automic(struct hda_codec *codec)
758 {
759         static const struct hda_verb mic_jack_on[] = {
760                 {0x14, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
761                 {0x12, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
762                 {}
763         };
764         static const struct hda_verb mic_jack_off[] = {
765                 {0x12, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
766                 {0x14, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
767                 {}
768         };
769         unsigned int present;
770
771         present = snd_hda_jack_detect(codec, 0x12);
772         if (present)
773                 snd_hda_sequence_write(codec, mic_jack_on);
774         else
775                 snd_hda_sequence_write(codec, mic_jack_off);
776 }
777
778
779 /* mute internal speaker if HP is plugged */
780 static void cxt5045_hp_automute(struct hda_codec *codec)
781 {
782         struct conexant_spec *spec = codec->spec;
783         unsigned int bits;
784
785         spec->hp_present = snd_hda_jack_detect(codec, 0x11);
786
787         bits = (spec->hp_present || !spec->cur_eapd) ? HDA_AMP_MUTE : 0; 
788         snd_hda_codec_amp_stereo(codec, 0x10, HDA_OUTPUT, 0,
789                                  HDA_AMP_MUTE, bits);
790 }
791
792 /* unsolicited event for HP jack sensing */
793 static void cxt5045_hp_unsol_event(struct hda_codec *codec,
794                                    unsigned int res)
795 {
796         res >>= 26;
797         switch (res) {
798         case CONEXANT_HP_EVENT:
799                 cxt5045_hp_automute(codec);
800                 break;
801         case CONEXANT_MIC_EVENT:
802                 cxt5045_hp_automic(codec);
803                 break;
804
805         }
806 }
807
808 static const struct snd_kcontrol_new cxt5045_mixers[] = {
809         HDA_CODEC_VOLUME("Capture Volume", 0x1a, 0x00, HDA_INPUT),
810         HDA_CODEC_MUTE("Capture Switch", 0x1a, 0x0, HDA_INPUT),
811         HDA_CODEC_VOLUME("PCM Playback Volume", 0x17, 0x0, HDA_INPUT),
812         HDA_CODEC_MUTE("PCM Playback Switch", 0x17, 0x0, HDA_INPUT),
813         HDA_CODEC_VOLUME("Internal Mic Playback Volume", 0x17, 0x1, HDA_INPUT),
814         HDA_CODEC_MUTE("Internal Mic Playback Switch", 0x17, 0x1, HDA_INPUT),
815         HDA_CODEC_VOLUME("Mic Playback Volume", 0x17, 0x2, HDA_INPUT),
816         HDA_CODEC_MUTE("Mic Playback Switch", 0x17, 0x2, HDA_INPUT),
817         HDA_BIND_VOL("Master Playback Volume", &cxt5045_hp_bind_master_vol),
818         {
819                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
820                 .name = "Master Playback Switch",
821                 .info = cxt_eapd_info,
822                 .get = cxt_eapd_get,
823                 .put = cxt5045_hp_master_sw_put,
824                 .private_value = 0x10,
825         },
826
827         {}
828 };
829
830 static const struct snd_kcontrol_new cxt5045_benq_mixers[] = {
831         HDA_CODEC_VOLUME("Line Playback Volume", 0x17, 0x3, HDA_INPUT),
832         HDA_CODEC_MUTE("Line Playback Switch", 0x17, 0x3, HDA_INPUT),
833
834         {}
835 };
836
837 static const struct snd_kcontrol_new cxt5045_mixers_hp530[] = {
838         HDA_CODEC_VOLUME("Capture Volume", 0x1a, 0x00, HDA_INPUT),
839         HDA_CODEC_MUTE("Capture Switch", 0x1a, 0x0, HDA_INPUT),
840         HDA_CODEC_VOLUME("PCM Playback Volume", 0x17, 0x0, HDA_INPUT),
841         HDA_CODEC_MUTE("PCM Playback Switch", 0x17, 0x0, HDA_INPUT),
842         HDA_CODEC_VOLUME("Internal Mic Playback Volume", 0x17, 0x2, HDA_INPUT),
843         HDA_CODEC_MUTE("Internal Mic Playback Switch", 0x17, 0x2, HDA_INPUT),
844         HDA_CODEC_VOLUME("Mic Playback Volume", 0x17, 0x1, HDA_INPUT),
845         HDA_CODEC_MUTE("Mic Playback Switch", 0x17, 0x1, HDA_INPUT),
846         HDA_BIND_VOL("Master Playback Volume", &cxt5045_hp_bind_master_vol),
847         {
848                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
849                 .name = "Master Playback Switch",
850                 .info = cxt_eapd_info,
851                 .get = cxt_eapd_get,
852                 .put = cxt5045_hp_master_sw_put,
853                 .private_value = 0x10,
854         },
855
856         {}
857 };
858
859 static const struct hda_verb cxt5045_init_verbs[] = {
860         /* Line in, Mic */
861         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN|AC_PINCTL_VREF_80 },
862         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN|AC_PINCTL_VREF_80 },
863         /* HP, Amp  */
864         {0x10, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
865         {0x10, AC_VERB_SET_CONNECT_SEL, 0x1},
866         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
867         {0x11, AC_VERB_SET_CONNECT_SEL, 0x1},
868         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
869         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
870         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
871         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
872         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
873         /* Record selector: Internal mic */
874         {0x1a, AC_VERB_SET_CONNECT_SEL,0x1},
875         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE,
876          AC_AMP_SET_INPUT|AC_AMP_SET_RIGHT|AC_AMP_SET_LEFT|0x17},
877         /* SPDIF route: PCM */
878         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
879         { 0x13, AC_VERB_SET_CONNECT_SEL, 0x0 },
880         /* EAPD */
881         {0x10, AC_VERB_SET_EAPD_BTLENABLE, 0x2 }, /* default on */ 
882         { } /* end */
883 };
884
885 static const struct hda_verb cxt5045_benq_init_verbs[] = {
886         /* Internal Mic, Mic */
887         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN|AC_PINCTL_VREF_80 },
888         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN|AC_PINCTL_VREF_80 },
889         /* Line In,HP, Amp  */
890         {0x10, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
891         {0x10, AC_VERB_SET_CONNECT_SEL, 0x1},
892         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
893         {0x11, AC_VERB_SET_CONNECT_SEL, 0x1},
894         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
895         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
896         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
897         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
898         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
899         /* Record selector: Internal mic */
900         {0x1a, AC_VERB_SET_CONNECT_SEL, 0x1},
901         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE,
902          AC_AMP_SET_INPUT|AC_AMP_SET_RIGHT|AC_AMP_SET_LEFT|0x17},
903         /* SPDIF route: PCM */
904         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
905         {0x13, AC_VERB_SET_CONNECT_SEL, 0x0},
906         /* EAPD */
907         {0x10, AC_VERB_SET_EAPD_BTLENABLE, 0x2}, /* default on */
908         { } /* end */
909 };
910
911 static const struct hda_verb cxt5045_hp_sense_init_verbs[] = {
912         /* pin sensing on HP jack */
913         {0x11, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_HP_EVENT},
914         { } /* end */
915 };
916
917 static const struct hda_verb cxt5045_mic_sense_init_verbs[] = {
918         /* pin sensing on HP jack */
919         {0x12, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_MIC_EVENT},
920         { } /* end */
921 };
922
923 #ifdef CONFIG_SND_DEBUG
924 /* Test configuration for debugging, modelled after the ALC260 test
925  * configuration.
926  */
927 static const struct hda_input_mux cxt5045_test_capture_source = {
928         .num_items = 5,
929         .items = {
930                 { "MIXER", 0x0 },
931                 { "MIC1 pin", 0x1 },
932                 { "LINE1 pin", 0x2 },
933                 { "HP-OUT pin", 0x3 },
934                 { "CD pin", 0x4 },
935         },
936 };
937
938 static const struct snd_kcontrol_new cxt5045_test_mixer[] = {
939
940         /* Output controls */
941         HDA_CODEC_VOLUME("Speaker Playback Volume", 0x10, 0x0, HDA_OUTPUT),
942         HDA_CODEC_MUTE("Speaker Playback Switch", 0x10, 0x0, HDA_OUTPUT),
943         HDA_CODEC_VOLUME("HP-OUT Playback Volume", 0x11, 0x0, HDA_OUTPUT),
944         HDA_CODEC_MUTE("HP-OUT Playback Switch", 0x11, 0x0, HDA_OUTPUT),
945         HDA_CODEC_VOLUME("LINE1 Playback Volume", 0x12, 0x0, HDA_OUTPUT),
946         HDA_CODEC_MUTE("LINE1 Playback Switch", 0x12, 0x0, HDA_OUTPUT),
947         
948         /* Modes for retasking pin widgets */
949         CXT_PIN_MODE("HP-OUT pin mode", 0x11, CXT_PIN_DIR_INOUT),
950         CXT_PIN_MODE("LINE1 pin mode", 0x12, CXT_PIN_DIR_INOUT),
951
952         /* EAPD Switch Control */
953         CXT_EAPD_SWITCH("External Amplifier", 0x10, 0x0),
954
955         /* Loopback mixer controls */
956
957         HDA_CODEC_VOLUME("PCM Volume", 0x17, 0x0, HDA_INPUT),
958         HDA_CODEC_MUTE("PCM Switch", 0x17, 0x0, HDA_INPUT),
959         HDA_CODEC_VOLUME("MIC1 pin Volume", 0x17, 0x1, HDA_INPUT),
960         HDA_CODEC_MUTE("MIC1 pin Switch", 0x17, 0x1, HDA_INPUT),
961         HDA_CODEC_VOLUME("LINE1 pin Volume", 0x17, 0x2, HDA_INPUT),
962         HDA_CODEC_MUTE("LINE1 pin Switch", 0x17, 0x2, HDA_INPUT),
963         HDA_CODEC_VOLUME("HP-OUT pin Volume", 0x17, 0x3, HDA_INPUT),
964         HDA_CODEC_MUTE("HP-OUT pin Switch", 0x17, 0x3, HDA_INPUT),
965         HDA_CODEC_VOLUME("CD pin Volume", 0x17, 0x4, HDA_INPUT),
966         HDA_CODEC_MUTE("CD pin Switch", 0x17, 0x4, HDA_INPUT),
967         {
968                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
969                 .name = "Input Source",
970                 .info = conexant_mux_enum_info,
971                 .get = conexant_mux_enum_get,
972                 .put = conexant_mux_enum_put,
973         },
974         /* Audio input controls */
975         HDA_CODEC_VOLUME("Capture Volume", 0x1a, 0x0, HDA_INPUT),
976         HDA_CODEC_MUTE("Capture Switch", 0x1a, 0x0, HDA_INPUT),
977         { } /* end */
978 };
979
980 static const struct hda_verb cxt5045_test_init_verbs[] = {
981         /* Set connections */
982         { 0x10, AC_VERB_SET_CONNECT_SEL, 0x0 },
983         { 0x11, AC_VERB_SET_CONNECT_SEL, 0x0 },
984         { 0x12, AC_VERB_SET_CONNECT_SEL, 0x0 },
985         /* Enable retasking pins as output, initially without power amp */
986         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
987         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
988
989         /* Disable digital (SPDIF) pins initially, but users can enable
990          * them via a mixer switch.  In the case of SPDIF-out, this initverb
991          * payload also sets the generation to 0, output to be in "consumer"
992          * PCM format, copyright asserted, no pre-emphasis and no validity
993          * control.
994          */
995         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
996         {0x18, AC_VERB_SET_DIGI_CONVERT_1, 0},
997
998         /* Unmute retasking pin widget output buffers since the default
999          * state appears to be output.  As the pin mode is changed by the
1000          * user the pin mode control will take care of enabling the pin's
1001          * input/output buffers as needed.
1002          */
1003         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1004         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1005
1006         /* Mute capture amp left and right */
1007         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1008
1009         /* Set ADC connection select to match default mixer setting (mic1
1010          * pin)
1011          */
1012         {0x1a, AC_VERB_SET_CONNECT_SEL, 0x01},
1013         {0x17, AC_VERB_SET_CONNECT_SEL, 0x01},
1014
1015         /* Mute all inputs to mixer widget (even unconnected ones) */
1016         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)}, /* Mixer */
1017         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)}, /* Mic1 pin */
1018         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)}, /* Line pin */
1019         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)}, /* HP pin */
1020         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)}, /* CD pin */
1021
1022         { }
1023 };
1024 #endif
1025
1026
1027 /* initialize jack-sensing, too */
1028 static int cxt5045_init(struct hda_codec *codec)
1029 {
1030         conexant_init(codec);
1031         cxt5045_hp_automute(codec);
1032         return 0;
1033 }
1034
1035
1036 enum {
1037         CXT5045_LAPTOP_HPSENSE,
1038         CXT5045_LAPTOP_MICSENSE,
1039         CXT5045_LAPTOP_HPMICSENSE,
1040         CXT5045_BENQ,
1041         CXT5045_LAPTOP_HP530,
1042 #ifdef CONFIG_SND_DEBUG
1043         CXT5045_TEST,
1044 #endif
1045         CXT5045_AUTO,
1046         CXT5045_MODELS
1047 };
1048
1049 static const char * const cxt5045_models[CXT5045_MODELS] = {
1050         [CXT5045_LAPTOP_HPSENSE]        = "laptop-hpsense",
1051         [CXT5045_LAPTOP_MICSENSE]       = "laptop-micsense",
1052         [CXT5045_LAPTOP_HPMICSENSE]     = "laptop-hpmicsense",
1053         [CXT5045_BENQ]                  = "benq",
1054         [CXT5045_LAPTOP_HP530]          = "laptop-hp530",
1055 #ifdef CONFIG_SND_DEBUG
1056         [CXT5045_TEST]          = "test",
1057 #endif
1058         [CXT5045_AUTO]                  = "auto",
1059 };
1060
1061 static const struct snd_pci_quirk cxt5045_cfg_tbl[] = {
1062         SND_PCI_QUIRK(0x103c, 0x30d5, "HP 530", CXT5045_LAPTOP_HP530),
1063         SND_PCI_QUIRK(0x1179, 0xff31, "Toshiba P105", CXT5045_LAPTOP_MICSENSE),
1064         SND_PCI_QUIRK(0x152d, 0x0753, "Benq R55E", CXT5045_BENQ),
1065         SND_PCI_QUIRK(0x1734, 0x10ad, "Fujitsu Si1520", CXT5045_LAPTOP_MICSENSE),
1066         SND_PCI_QUIRK(0x1734, 0x10cb, "Fujitsu Si3515", CXT5045_LAPTOP_HPMICSENSE),
1067         SND_PCI_QUIRK(0x1734, 0x110e, "Fujitsu V5505",
1068                       CXT5045_LAPTOP_HPMICSENSE),
1069         SND_PCI_QUIRK(0x1509, 0x1e40, "FIC", CXT5045_LAPTOP_HPMICSENSE),
1070         SND_PCI_QUIRK(0x1509, 0x2f05, "FIC", CXT5045_LAPTOP_HPMICSENSE),
1071         SND_PCI_QUIRK(0x1509, 0x2f06, "FIC", CXT5045_LAPTOP_HPMICSENSE),
1072         SND_PCI_QUIRK_MASK(0x1631, 0xff00, 0xc100, "Packard Bell",
1073                            CXT5045_LAPTOP_HPMICSENSE),
1074         SND_PCI_QUIRK(0x8086, 0x2111, "Conexant Reference board", CXT5045_LAPTOP_HPSENSE),
1075         {}
1076 };
1077
1078 static int patch_cxt5045(struct hda_codec *codec)
1079 {
1080         struct conexant_spec *spec;
1081         int board_config;
1082
1083         board_config = snd_hda_check_board_config(codec, CXT5045_MODELS,
1084                                                   cxt5045_models,
1085                                                   cxt5045_cfg_tbl);
1086         if (board_config < 0)
1087                 board_config = CXT5045_AUTO; /* model=auto as default */
1088         if (board_config == CXT5045_AUTO)
1089                 return patch_conexant_auto(codec);
1090
1091         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1092         if (!spec)
1093                 return -ENOMEM;
1094         codec->spec = spec;
1095         codec->single_adc_amp = 1;
1096
1097         spec->multiout.max_channels = 2;
1098         spec->multiout.num_dacs = ARRAY_SIZE(cxt5045_dac_nids);
1099         spec->multiout.dac_nids = cxt5045_dac_nids;
1100         spec->multiout.dig_out_nid = CXT5045_SPDIF_OUT;
1101         spec->num_adc_nids = 1;
1102         spec->adc_nids = cxt5045_adc_nids;
1103         spec->capsrc_nids = cxt5045_capsrc_nids;
1104         spec->input_mux = &cxt5045_capture_source;
1105         spec->num_mixers = 1;
1106         spec->mixers[0] = cxt5045_mixers;
1107         spec->num_init_verbs = 1;
1108         spec->init_verbs[0] = cxt5045_init_verbs;
1109         spec->spdif_route = 0;
1110         spec->num_channel_mode = ARRAY_SIZE(cxt5045_modes);
1111         spec->channel_mode = cxt5045_modes;
1112
1113         set_beep_amp(spec, 0x16, 0, 1);
1114
1115         codec->patch_ops = conexant_patch_ops;
1116
1117         switch (board_config) {
1118         case CXT5045_LAPTOP_HPSENSE:
1119                 codec->patch_ops.unsol_event = cxt5045_hp_unsol_event;
1120                 spec->input_mux = &cxt5045_capture_source;
1121                 spec->num_init_verbs = 2;
1122                 spec->init_verbs[1] = cxt5045_hp_sense_init_verbs;
1123                 spec->mixers[0] = cxt5045_mixers;
1124                 codec->patch_ops.init = cxt5045_init;
1125                 break;
1126         case CXT5045_LAPTOP_MICSENSE:
1127                 codec->patch_ops.unsol_event = cxt5045_hp_unsol_event;
1128                 spec->input_mux = &cxt5045_capture_source;
1129                 spec->num_init_verbs = 2;
1130                 spec->init_verbs[1] = cxt5045_mic_sense_init_verbs;
1131                 spec->mixers[0] = cxt5045_mixers;
1132                 codec->patch_ops.init = cxt5045_init;
1133                 break;
1134         default:
1135         case CXT5045_LAPTOP_HPMICSENSE:
1136                 codec->patch_ops.unsol_event = cxt5045_hp_unsol_event;
1137                 spec->input_mux = &cxt5045_capture_source;
1138                 spec->num_init_verbs = 3;
1139                 spec->init_verbs[1] = cxt5045_hp_sense_init_verbs;
1140                 spec->init_verbs[2] = cxt5045_mic_sense_init_verbs;
1141                 spec->mixers[0] = cxt5045_mixers;
1142                 codec->patch_ops.init = cxt5045_init;
1143                 break;
1144         case CXT5045_BENQ:
1145                 codec->patch_ops.unsol_event = cxt5045_hp_unsol_event;
1146                 spec->input_mux = &cxt5045_capture_source_benq;
1147                 spec->num_init_verbs = 1;
1148                 spec->init_verbs[0] = cxt5045_benq_init_verbs;
1149                 spec->mixers[0] = cxt5045_mixers;
1150                 spec->mixers[1] = cxt5045_benq_mixers;
1151                 spec->num_mixers = 2;
1152                 codec->patch_ops.init = cxt5045_init;
1153                 break;
1154         case CXT5045_LAPTOP_HP530:
1155                 codec->patch_ops.unsol_event = cxt5045_hp_unsol_event;
1156                 spec->input_mux = &cxt5045_capture_source_hp530;
1157                 spec->num_init_verbs = 2;
1158                 spec->init_verbs[1] = cxt5045_hp_sense_init_verbs;
1159                 spec->mixers[0] = cxt5045_mixers_hp530;
1160                 codec->patch_ops.init = cxt5045_init;
1161                 break;
1162 #ifdef CONFIG_SND_DEBUG
1163         case CXT5045_TEST:
1164                 spec->input_mux = &cxt5045_test_capture_source;
1165                 spec->mixers[0] = cxt5045_test_mixer;
1166                 spec->init_verbs[0] = cxt5045_test_init_verbs;
1167                 break;
1168                 
1169 #endif  
1170         }
1171
1172         switch (codec->subsystem_id >> 16) {
1173         case 0x103c:
1174         case 0x1631:
1175         case 0x1734:
1176         case 0x17aa:
1177                 /* HP, Packard Bell, Fujitsu-Siemens & Lenovo laptops have
1178                  * really bad sound over 0dB on NID 0x17. Fix max PCM level to
1179                  * 0 dB (originally it has 0x2b steps with 0dB offset 0x14)
1180                  */
1181                 snd_hda_override_amp_caps(codec, 0x17, HDA_INPUT,
1182                                           (0x14 << AC_AMPCAP_OFFSET_SHIFT) |
1183                                           (0x14 << AC_AMPCAP_NUM_STEPS_SHIFT) |
1184                                           (0x05 << AC_AMPCAP_STEP_SIZE_SHIFT) |
1185                                           (1 << AC_AMPCAP_MUTE_SHIFT));
1186                 break;
1187         }
1188
1189         if (spec->beep_amp)
1190                 snd_hda_attach_beep_device(codec, spec->beep_amp);
1191
1192         return 0;
1193 }
1194
1195
1196 /* Conexant 5047 specific */
1197 #define CXT5047_SPDIF_OUT       0x11
1198
1199 static const hda_nid_t cxt5047_dac_nids[1] = { 0x10 }; /* 0x1c */
1200 static const hda_nid_t cxt5047_adc_nids[1] = { 0x12 };
1201 static const hda_nid_t cxt5047_capsrc_nids[1] = { 0x1a };
1202
1203 static const struct hda_channel_mode cxt5047_modes[1] = {
1204         { 2, NULL },
1205 };
1206
1207 static const struct hda_input_mux cxt5047_toshiba_capture_source = {
1208         .num_items = 2,
1209         .items = {
1210                 { "ExtMic", 0x2 },
1211                 { "Line-In", 0x1 },
1212         }
1213 };
1214
1215 /* turn on/off EAPD (+ mute HP) as a master switch */
1216 static int cxt5047_hp_master_sw_put(struct snd_kcontrol *kcontrol,
1217                                     struct snd_ctl_elem_value *ucontrol)
1218 {
1219         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1220         struct conexant_spec *spec = codec->spec;
1221         unsigned int bits;
1222
1223         if (!cxt_eapd_put(kcontrol, ucontrol))
1224                 return 0;
1225
1226         /* toggle internal speakers mute depending of presence of
1227          * the headphone jack
1228          */
1229         bits = (!spec->hp_present && spec->cur_eapd) ? 0 : HDA_AMP_MUTE;
1230         /* NOTE: Conexat codec needs the index for *OUTPUT* amp of
1231          * pin widgets unlike other codecs.  In this case, we need to
1232          * set index 0x01 for the volume from the mixer amp 0x19.
1233          */
1234         snd_hda_codec_amp_stereo(codec, 0x1d, HDA_OUTPUT, 0x01,
1235                                  HDA_AMP_MUTE, bits);
1236         bits = spec->cur_eapd ? 0 : HDA_AMP_MUTE;
1237         snd_hda_codec_amp_stereo(codec, 0x13, HDA_OUTPUT, 0,
1238                                  HDA_AMP_MUTE, bits);
1239         return 1;
1240 }
1241
1242 /* mute internal speaker if HP is plugged */
1243 static void cxt5047_hp_automute(struct hda_codec *codec)
1244 {
1245         struct conexant_spec *spec = codec->spec;
1246         unsigned int bits;
1247
1248         spec->hp_present = snd_hda_jack_detect(codec, 0x13);
1249
1250         bits = (spec->hp_present || !spec->cur_eapd) ? HDA_AMP_MUTE : 0;
1251         /* See the note in cxt5047_hp_master_sw_put */
1252         snd_hda_codec_amp_stereo(codec, 0x1d, HDA_OUTPUT, 0x01,
1253                                  HDA_AMP_MUTE, bits);
1254 }
1255
1256 /* toggle input of built-in and mic jack appropriately */
1257 static void cxt5047_hp_automic(struct hda_codec *codec)
1258 {
1259         static const struct hda_verb mic_jack_on[] = {
1260                 {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1261                 {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1262                 {}
1263         };
1264         static const struct hda_verb mic_jack_off[] = {
1265                 {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1266                 {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1267                 {}
1268         };
1269         unsigned int present;
1270
1271         present = snd_hda_jack_detect(codec, 0x15);
1272         if (present)
1273                 snd_hda_sequence_write(codec, mic_jack_on);
1274         else
1275                 snd_hda_sequence_write(codec, mic_jack_off);
1276 }
1277
1278 /* unsolicited event for HP jack sensing */
1279 static void cxt5047_hp_unsol_event(struct hda_codec *codec,
1280                                   unsigned int res)
1281 {
1282         switch (res >> 26) {
1283         case CONEXANT_HP_EVENT:
1284                 cxt5047_hp_automute(codec);
1285                 break;
1286         case CONEXANT_MIC_EVENT:
1287                 cxt5047_hp_automic(codec);
1288                 break;
1289         }
1290 }
1291
1292 static const struct snd_kcontrol_new cxt5047_base_mixers[] = {
1293         HDA_CODEC_VOLUME("Mic Playback Volume", 0x19, 0x02, HDA_INPUT),
1294         HDA_CODEC_MUTE("Mic Playback Switch", 0x19, 0x02, HDA_INPUT),
1295         HDA_CODEC_VOLUME("Mic Boost Volume", 0x1a, 0x0, HDA_OUTPUT),
1296         HDA_CODEC_VOLUME("Capture Volume", 0x12, 0x03, HDA_INPUT),
1297         HDA_CODEC_MUTE("Capture Switch", 0x12, 0x03, HDA_INPUT),
1298         HDA_CODEC_VOLUME("PCM Volume", 0x10, 0x00, HDA_OUTPUT),
1299         HDA_CODEC_MUTE("PCM Switch", 0x10, 0x00, HDA_OUTPUT),
1300         {
1301                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1302                 .name = "Master Playback Switch",
1303                 .info = cxt_eapd_info,
1304                 .get = cxt_eapd_get,
1305                 .put = cxt5047_hp_master_sw_put,
1306                 .private_value = 0x13,
1307         },
1308
1309         {}
1310 };
1311
1312 static const struct snd_kcontrol_new cxt5047_hp_spk_mixers[] = {
1313         /* See the note in cxt5047_hp_master_sw_put */
1314         HDA_CODEC_VOLUME("Speaker Playback Volume", 0x1d, 0x01, HDA_OUTPUT),
1315         HDA_CODEC_VOLUME("Headphone Playback Volume", 0x13, 0x00, HDA_OUTPUT),
1316         {}
1317 };
1318
1319 static const struct snd_kcontrol_new cxt5047_hp_only_mixers[] = {
1320         HDA_CODEC_VOLUME("Master Playback Volume", 0x13, 0x00, HDA_OUTPUT),
1321         { } /* end */
1322 };
1323
1324 static const struct hda_verb cxt5047_init_verbs[] = {
1325         /* Line in, Mic, Built-in Mic */
1326         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
1327         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN|AC_PINCTL_VREF_50 },
1328         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN|AC_PINCTL_VREF_50 },
1329         /* HP, Speaker  */
1330         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
1331         {0x13, AC_VERB_SET_CONNECT_SEL, 0x0}, /* mixer(0x19) */
1332         {0x1d, AC_VERB_SET_CONNECT_SEL, 0x1}, /* mixer(0x19) */
1333         /* Record selector: Mic */
1334         {0x12, AC_VERB_SET_CONNECT_SEL,0x03},
1335         {0x19, AC_VERB_SET_AMP_GAIN_MUTE,
1336          AC_AMP_SET_INPUT|AC_AMP_SET_RIGHT|AC_AMP_SET_LEFT|0x17},
1337         {0x1A, AC_VERB_SET_CONNECT_SEL,0x02},
1338         {0x1A, AC_VERB_SET_AMP_GAIN_MUTE,
1339          AC_AMP_SET_OUTPUT|AC_AMP_SET_RIGHT|AC_AMP_SET_LEFT|0x00},
1340         {0x1A, AC_VERB_SET_AMP_GAIN_MUTE,
1341          AC_AMP_SET_OUTPUT|AC_AMP_SET_RIGHT|AC_AMP_SET_LEFT|0x03},
1342         /* SPDIF route: PCM */
1343         { 0x18, AC_VERB_SET_CONNECT_SEL, 0x0 },
1344         /* Enable unsolicited events */
1345         {0x13, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_HP_EVENT},
1346         {0x15, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_MIC_EVENT},
1347         { } /* end */
1348 };
1349
1350 /* configuration for Toshiba Laptops */
1351 static const struct hda_verb cxt5047_toshiba_init_verbs[] = {
1352         {0x13, AC_VERB_SET_EAPD_BTLENABLE, 0x0}, /* default off */
1353         {}
1354 };
1355
1356 /* Test configuration for debugging, modelled after the ALC260 test
1357  * configuration.
1358  */
1359 #ifdef CONFIG_SND_DEBUG
1360 static const struct hda_input_mux cxt5047_test_capture_source = {
1361         .num_items = 4,
1362         .items = {
1363                 { "LINE1 pin", 0x0 },
1364                 { "MIC1 pin", 0x1 },
1365                 { "MIC2 pin", 0x2 },
1366                 { "CD pin", 0x3 },
1367         },
1368 };
1369
1370 static const struct snd_kcontrol_new cxt5047_test_mixer[] = {
1371
1372         /* Output only controls */
1373         HDA_CODEC_VOLUME("OutAmp-1 Volume", 0x10, 0x0, HDA_OUTPUT),
1374         HDA_CODEC_MUTE("OutAmp-1 Switch", 0x10,0x0, HDA_OUTPUT),
1375         HDA_CODEC_VOLUME("OutAmp-2 Volume", 0x1c, 0x0, HDA_OUTPUT),
1376         HDA_CODEC_MUTE("OutAmp-2 Switch", 0x1c, 0x0, HDA_OUTPUT),
1377         HDA_CODEC_VOLUME("Speaker Playback Volume", 0x1d, 0x0, HDA_OUTPUT),
1378         HDA_CODEC_MUTE("Speaker Playback Switch", 0x1d, 0x0, HDA_OUTPUT),
1379         HDA_CODEC_VOLUME("HeadPhone Playback Volume", 0x13, 0x0, HDA_OUTPUT),
1380         HDA_CODEC_MUTE("HeadPhone Playback Switch", 0x13, 0x0, HDA_OUTPUT),
1381         HDA_CODEC_VOLUME("Line1-Out Playback Volume", 0x14, 0x0, HDA_OUTPUT),
1382         HDA_CODEC_MUTE("Line1-Out Playback Switch", 0x14, 0x0, HDA_OUTPUT),
1383         HDA_CODEC_VOLUME("Line2-Out Playback Volume", 0x15, 0x0, HDA_OUTPUT),
1384         HDA_CODEC_MUTE("Line2-Out Playback Switch", 0x15, 0x0, HDA_OUTPUT),
1385
1386         /* Modes for retasking pin widgets */
1387         CXT_PIN_MODE("LINE1 pin mode", 0x14, CXT_PIN_DIR_INOUT),
1388         CXT_PIN_MODE("MIC1 pin mode", 0x15, CXT_PIN_DIR_INOUT),
1389
1390         /* EAPD Switch Control */
1391         CXT_EAPD_SWITCH("External Amplifier", 0x13, 0x0),
1392
1393         /* Loopback mixer controls */
1394         HDA_CODEC_VOLUME("MIC1 Playback Volume", 0x12, 0x01, HDA_INPUT),
1395         HDA_CODEC_MUTE("MIC1 Playback Switch", 0x12, 0x01, HDA_INPUT),
1396         HDA_CODEC_VOLUME("MIC2 Playback Volume", 0x12, 0x02, HDA_INPUT),
1397         HDA_CODEC_MUTE("MIC2 Playback Switch", 0x12, 0x02, HDA_INPUT),
1398         HDA_CODEC_VOLUME("LINE Playback Volume", 0x12, 0x0, HDA_INPUT),
1399         HDA_CODEC_MUTE("LINE Playback Switch", 0x12, 0x0, HDA_INPUT),
1400         HDA_CODEC_VOLUME("CD Playback Volume", 0x12, 0x04, HDA_INPUT),
1401         HDA_CODEC_MUTE("CD Playback Switch", 0x12, 0x04, HDA_INPUT),
1402
1403         HDA_CODEC_VOLUME("Capture-1 Volume", 0x19, 0x0, HDA_INPUT),
1404         HDA_CODEC_MUTE("Capture-1 Switch", 0x19, 0x0, HDA_INPUT),
1405         HDA_CODEC_VOLUME("Capture-2 Volume", 0x19, 0x1, HDA_INPUT),
1406         HDA_CODEC_MUTE("Capture-2 Switch", 0x19, 0x1, HDA_INPUT),
1407         HDA_CODEC_VOLUME("Capture-3 Volume", 0x19, 0x2, HDA_INPUT),
1408         HDA_CODEC_MUTE("Capture-3 Switch", 0x19, 0x2, HDA_INPUT),
1409         HDA_CODEC_VOLUME("Capture-4 Volume", 0x19, 0x3, HDA_INPUT),
1410         HDA_CODEC_MUTE("Capture-4 Switch", 0x19, 0x3, HDA_INPUT),
1411         {
1412                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1413                 .name = "Input Source",
1414                 .info = conexant_mux_enum_info,
1415                 .get = conexant_mux_enum_get,
1416                 .put = conexant_mux_enum_put,
1417         },
1418         HDA_CODEC_VOLUME("Mic Boost Volume", 0x1a, 0x0, HDA_OUTPUT),
1419
1420         { } /* end */
1421 };
1422
1423 static const struct hda_verb cxt5047_test_init_verbs[] = {
1424         /* Enable retasking pins as output, initially without power amp */
1425         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1426         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1427         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1428
1429         /* Disable digital (SPDIF) pins initially, but users can enable
1430          * them via a mixer switch.  In the case of SPDIF-out, this initverb
1431          * payload also sets the generation to 0, output to be in "consumer"
1432          * PCM format, copyright asserted, no pre-emphasis and no validity
1433          * control.
1434          */
1435         {0x18, AC_VERB_SET_DIGI_CONVERT_1, 0},
1436
1437         /* Ensure mic1, mic2, line1 pin widgets take input from the 
1438          * OUT1 sum bus when acting as an output.
1439          */
1440         {0x1a, AC_VERB_SET_CONNECT_SEL, 0},
1441         {0x1b, AC_VERB_SET_CONNECT_SEL, 0},
1442
1443         /* Start with output sum widgets muted and their output gains at min */
1444         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1445         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
1446
1447         /* Unmute retasking pin widget output buffers since the default
1448          * state appears to be output.  As the pin mode is changed by the
1449          * user the pin mode control will take care of enabling the pin's
1450          * input/output buffers as needed.
1451          */
1452         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1453         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1454         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1455
1456         /* Mute capture amp left and right */
1457         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1458
1459         /* Set ADC connection select to match default mixer setting (mic1
1460          * pin)
1461          */
1462         {0x12, AC_VERB_SET_CONNECT_SEL, 0x00},
1463
1464         /* Mute all inputs to mixer widget (even unconnected ones) */
1465         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)}, /* mic1 pin */
1466         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)}, /* mic2 pin */
1467         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)}, /* line1 pin */
1468         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)}, /* line2 pin */
1469         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)}, /* CD pin */
1470         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(5)}, /* Beep-gen pin */
1471         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(6)}, /* Line-out pin */
1472         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(7)}, /* HP-pin pin */
1473
1474         { }
1475 };
1476 #endif
1477
1478
1479 /* initialize jack-sensing, too */
1480 static int cxt5047_hp_init(struct hda_codec *codec)
1481 {
1482         conexant_init(codec);
1483         cxt5047_hp_automute(codec);
1484         return 0;
1485 }
1486
1487
1488 enum {
1489         CXT5047_LAPTOP,         /* Laptops w/o EAPD support */
1490         CXT5047_LAPTOP_HP,      /* Some HP laptops */
1491         CXT5047_LAPTOP_EAPD,    /* Laptops with EAPD support */
1492 #ifdef CONFIG_SND_DEBUG
1493         CXT5047_TEST,
1494 #endif
1495         CXT5047_AUTO,
1496         CXT5047_MODELS
1497 };
1498
1499 static const char * const cxt5047_models[CXT5047_MODELS] = {
1500         [CXT5047_LAPTOP]        = "laptop",
1501         [CXT5047_LAPTOP_HP]     = "laptop-hp",
1502         [CXT5047_LAPTOP_EAPD]   = "laptop-eapd",
1503 #ifdef CONFIG_SND_DEBUG
1504         [CXT5047_TEST]          = "test",
1505 #endif
1506         [CXT5047_AUTO]          = "auto",
1507 };
1508
1509 static const struct snd_pci_quirk cxt5047_cfg_tbl[] = {
1510         SND_PCI_QUIRK(0x103c, 0x30a5, "HP DV5200T/DV8000T", CXT5047_LAPTOP_HP),
1511         SND_PCI_QUIRK_MASK(0x103c, 0xff00, 0x3000, "HP DV Series",
1512                            CXT5047_LAPTOP),
1513         SND_PCI_QUIRK(0x1179, 0xff31, "Toshiba P100", CXT5047_LAPTOP_EAPD),
1514         {}
1515 };
1516
1517 static int patch_cxt5047(struct hda_codec *codec)
1518 {
1519         struct conexant_spec *spec;
1520         int board_config;
1521
1522         board_config = snd_hda_check_board_config(codec, CXT5047_MODELS,
1523                                                   cxt5047_models,
1524                                                   cxt5047_cfg_tbl);
1525         if (board_config < 0)
1526                 board_config = CXT5047_AUTO; /* model=auto as default */
1527         if (board_config == CXT5047_AUTO)
1528                 return patch_conexant_auto(codec);
1529
1530         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1531         if (!spec)
1532                 return -ENOMEM;
1533         codec->spec = spec;
1534         codec->pin_amp_workaround = 1;
1535
1536         spec->multiout.max_channels = 2;
1537         spec->multiout.num_dacs = ARRAY_SIZE(cxt5047_dac_nids);
1538         spec->multiout.dac_nids = cxt5047_dac_nids;
1539         spec->multiout.dig_out_nid = CXT5047_SPDIF_OUT;
1540         spec->num_adc_nids = 1;
1541         spec->adc_nids = cxt5047_adc_nids;
1542         spec->capsrc_nids = cxt5047_capsrc_nids;
1543         spec->num_mixers = 1;
1544         spec->mixers[0] = cxt5047_base_mixers;
1545         spec->num_init_verbs = 1;
1546         spec->init_verbs[0] = cxt5047_init_verbs;
1547         spec->spdif_route = 0;
1548         spec->num_channel_mode = ARRAY_SIZE(cxt5047_modes),
1549         spec->channel_mode = cxt5047_modes,
1550
1551         codec->patch_ops = conexant_patch_ops;
1552
1553         switch (board_config) {
1554         case CXT5047_LAPTOP:
1555                 spec->num_mixers = 2;
1556                 spec->mixers[1] = cxt5047_hp_spk_mixers;
1557                 codec->patch_ops.unsol_event = cxt5047_hp_unsol_event;
1558                 break;
1559         case CXT5047_LAPTOP_HP:
1560                 spec->num_mixers = 2;
1561                 spec->mixers[1] = cxt5047_hp_only_mixers;
1562                 codec->patch_ops.unsol_event = cxt5047_hp_unsol_event;
1563                 codec->patch_ops.init = cxt5047_hp_init;
1564                 break;
1565         case CXT5047_LAPTOP_EAPD:
1566                 spec->input_mux = &cxt5047_toshiba_capture_source;
1567                 spec->num_mixers = 2;
1568                 spec->mixers[1] = cxt5047_hp_spk_mixers;
1569                 spec->num_init_verbs = 2;
1570                 spec->init_verbs[1] = cxt5047_toshiba_init_verbs;
1571                 codec->patch_ops.unsol_event = cxt5047_hp_unsol_event;
1572                 break;
1573 #ifdef CONFIG_SND_DEBUG
1574         case CXT5047_TEST:
1575                 spec->input_mux = &cxt5047_test_capture_source;
1576                 spec->mixers[0] = cxt5047_test_mixer;
1577                 spec->init_verbs[0] = cxt5047_test_init_verbs;
1578                 codec->patch_ops.unsol_event = cxt5047_hp_unsol_event;
1579 #endif  
1580         }
1581         spec->vmaster_nid = 0x13;
1582
1583         switch (codec->subsystem_id >> 16) {
1584         case 0x103c:
1585                 /* HP laptops have really bad sound over 0 dB on NID 0x10.
1586                  * Fix max PCM level to 0 dB (originally it has 0x1e steps
1587                  * with 0 dB offset 0x17)
1588                  */
1589                 snd_hda_override_amp_caps(codec, 0x10, HDA_INPUT,
1590                                           (0x17 << AC_AMPCAP_OFFSET_SHIFT) |
1591                                           (0x17 << AC_AMPCAP_NUM_STEPS_SHIFT) |
1592                                           (0x05 << AC_AMPCAP_STEP_SIZE_SHIFT) |
1593                                           (1 << AC_AMPCAP_MUTE_SHIFT));
1594                 break;
1595         }
1596
1597         return 0;
1598 }
1599
1600 /* Conexant 5051 specific */
1601 static const hda_nid_t cxt5051_dac_nids[1] = { 0x10 };
1602 static const hda_nid_t cxt5051_adc_nids[2] = { 0x14, 0x15 };
1603
1604 static const struct hda_channel_mode cxt5051_modes[1] = {
1605         { 2, NULL },
1606 };
1607
1608 static void cxt5051_update_speaker(struct hda_codec *codec)
1609 {
1610         struct conexant_spec *spec = codec->spec;
1611         unsigned int pinctl;
1612         /* headphone pin */
1613         pinctl = (spec->hp_present && spec->cur_eapd) ? PIN_HP : 0;
1614         snd_hda_set_pin_ctl(codec, 0x16, pinctl);
1615         /* speaker pin */
1616         pinctl = (!spec->hp_present && spec->cur_eapd) ? PIN_OUT : 0;
1617         snd_hda_set_pin_ctl(codec, 0x1a, pinctl);
1618         /* on ideapad there is an additional speaker (subwoofer) to mute */
1619         if (spec->ideapad)
1620                 snd_hda_set_pin_ctl(codec, 0x1b, pinctl);
1621 }
1622
1623 /* turn on/off EAPD (+ mute HP) as a master switch */
1624 static int cxt5051_hp_master_sw_put(struct snd_kcontrol *kcontrol,
1625                                     struct snd_ctl_elem_value *ucontrol)
1626 {
1627         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1628
1629         if (!cxt_eapd_put(kcontrol, ucontrol))
1630                 return 0;
1631         cxt5051_update_speaker(codec);
1632         return 1;
1633 }
1634
1635 /* toggle input of built-in and mic jack appropriately */
1636 static void cxt5051_portb_automic(struct hda_codec *codec)
1637 {
1638         struct conexant_spec *spec = codec->spec;
1639         unsigned int present;
1640
1641         if (!(spec->auto_mic & AUTO_MIC_PORTB))
1642                 return;
1643         present = snd_hda_jack_detect(codec, 0x17);
1644         snd_hda_codec_write(codec, 0x14, 0,
1645                             AC_VERB_SET_CONNECT_SEL,
1646                             present ? 0x01 : 0x00);
1647 }
1648
1649 /* switch the current ADC according to the jack state */
1650 static void cxt5051_portc_automic(struct hda_codec *codec)
1651 {
1652         struct conexant_spec *spec = codec->spec;
1653         unsigned int present;
1654         hda_nid_t new_adc;
1655
1656         if (!(spec->auto_mic & AUTO_MIC_PORTC))
1657                 return;
1658         present = snd_hda_jack_detect(codec, 0x18);
1659         if (present)
1660                 spec->cur_adc_idx = 1;
1661         else
1662                 spec->cur_adc_idx = 0;
1663         new_adc = spec->adc_nids[spec->cur_adc_idx];
1664         if (spec->cur_adc && spec->cur_adc != new_adc) {
1665                 /* stream is running, let's swap the current ADC */
1666                 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
1667                 spec->cur_adc = new_adc;
1668                 snd_hda_codec_setup_stream(codec, new_adc,
1669                                            spec->cur_adc_stream_tag, 0,
1670                                            spec->cur_adc_format);
1671         }
1672 }
1673
1674 /* mute internal speaker if HP is plugged */
1675 static void cxt5051_hp_automute(struct hda_codec *codec)
1676 {
1677         struct conexant_spec *spec = codec->spec;
1678
1679         spec->hp_present = snd_hda_jack_detect(codec, 0x16);
1680         cxt5051_update_speaker(codec);
1681 }
1682
1683 /* unsolicited event for HP jack sensing */
1684 static void cxt5051_hp_unsol_event(struct hda_codec *codec,
1685                                    unsigned int res)
1686 {
1687         switch (res >> 26) {
1688         case CONEXANT_HP_EVENT:
1689                 cxt5051_hp_automute(codec);
1690                 break;
1691         case CXT5051_PORTB_EVENT:
1692                 cxt5051_portb_automic(codec);
1693                 break;
1694         case CXT5051_PORTC_EVENT:
1695                 cxt5051_portc_automic(codec);
1696                 break;
1697         }
1698 }
1699
1700 static const struct snd_kcontrol_new cxt5051_playback_mixers[] = {
1701         HDA_CODEC_VOLUME("Master Playback Volume", 0x10, 0x00, HDA_OUTPUT),
1702         {
1703                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1704                 .name = "Master Playback Switch",
1705                 .info = cxt_eapd_info,
1706                 .get = cxt_eapd_get,
1707                 .put = cxt5051_hp_master_sw_put,
1708                 .private_value = 0x1a,
1709         },
1710         {}
1711 };
1712
1713 static const struct snd_kcontrol_new cxt5051_capture_mixers[] = {
1714         HDA_CODEC_VOLUME("Internal Mic Volume", 0x14, 0x00, HDA_INPUT),
1715         HDA_CODEC_MUTE("Internal Mic Switch", 0x14, 0x00, HDA_INPUT),
1716         HDA_CODEC_VOLUME("Mic Volume", 0x14, 0x01, HDA_INPUT),
1717         HDA_CODEC_MUTE("Mic Switch", 0x14, 0x01, HDA_INPUT),
1718         HDA_CODEC_VOLUME("Dock Mic Volume", 0x15, 0x00, HDA_INPUT),
1719         HDA_CODEC_MUTE("Dock Mic Switch", 0x15, 0x00, HDA_INPUT),
1720         {}
1721 };
1722
1723 static const struct snd_kcontrol_new cxt5051_hp_mixers[] = {
1724         HDA_CODEC_VOLUME("Internal Mic Volume", 0x14, 0x00, HDA_INPUT),
1725         HDA_CODEC_MUTE("Internal Mic Switch", 0x14, 0x00, HDA_INPUT),
1726         HDA_CODEC_VOLUME("Mic Volume", 0x15, 0x00, HDA_INPUT),
1727         HDA_CODEC_MUTE("Mic Switch", 0x15, 0x00, HDA_INPUT),
1728         {}
1729 };
1730
1731 static const struct snd_kcontrol_new cxt5051_hp_dv6736_mixers[] = {
1732         HDA_CODEC_VOLUME("Capture Volume", 0x14, 0x00, HDA_INPUT),
1733         HDA_CODEC_MUTE("Capture Switch", 0x14, 0x00, HDA_INPUT),
1734         {}
1735 };
1736
1737 static const struct snd_kcontrol_new cxt5051_f700_mixers[] = {
1738         HDA_CODEC_VOLUME("Capture Volume", 0x14, 0x01, HDA_INPUT),
1739         HDA_CODEC_MUTE("Capture Switch", 0x14, 0x01, HDA_INPUT),
1740         {}
1741 };
1742
1743 static const struct snd_kcontrol_new cxt5051_toshiba_mixers[] = {
1744         HDA_CODEC_VOLUME("Internal Mic Volume", 0x14, 0x00, HDA_INPUT),
1745         HDA_CODEC_MUTE("Internal Mic Switch", 0x14, 0x00, HDA_INPUT),
1746         HDA_CODEC_VOLUME("Mic Volume", 0x14, 0x01, HDA_INPUT),
1747         HDA_CODEC_MUTE("Mic Switch", 0x14, 0x01, HDA_INPUT),
1748         {}
1749 };
1750
1751 static const struct hda_verb cxt5051_init_verbs[] = {
1752         /* Line in, Mic */
1753         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x03},
1754         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1755         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x03},
1756         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1757         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
1758         {0x1d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x03},
1759         /* SPK  */
1760         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1761         {0x1a, AC_VERB_SET_CONNECT_SEL, 0x00},
1762         /* HP, Amp  */
1763         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1764         {0x16, AC_VERB_SET_CONNECT_SEL, 0x00},
1765         /* DAC1 */      
1766         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1767         /* Record selector: Internal mic */
1768         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x44},
1769         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1) | 0x44},
1770         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x44},
1771         /* SPDIF route: PCM */
1772         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1773         {0x1c, AC_VERB_SET_CONNECT_SEL, 0x0},
1774         /* EAPD */
1775         {0x1a, AC_VERB_SET_EAPD_BTLENABLE, 0x2}, /* default on */ 
1776         {0x16, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN|CONEXANT_HP_EVENT},
1777         { } /* end */
1778 };
1779
1780 static const struct hda_verb cxt5051_hp_dv6736_init_verbs[] = {
1781         /* Line in, Mic */
1782         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x03},
1783         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1784         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x0},
1785         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x0},
1786         /* SPK  */
1787         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1788         {0x1a, AC_VERB_SET_CONNECT_SEL, 0x00},
1789         /* HP, Amp  */
1790         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1791         {0x16, AC_VERB_SET_CONNECT_SEL, 0x00},
1792         /* DAC1 */
1793         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1794         /* Record selector: Internal mic */
1795         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1) | 0x44},
1796         {0x14, AC_VERB_SET_CONNECT_SEL, 0x1},
1797         /* SPDIF route: PCM */
1798         {0x1c, AC_VERB_SET_CONNECT_SEL, 0x0},
1799         /* EAPD */
1800         {0x1a, AC_VERB_SET_EAPD_BTLENABLE, 0x2}, /* default on */
1801         {0x16, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN|CONEXANT_HP_EVENT},
1802         { } /* end */
1803 };
1804
1805 static const struct hda_verb cxt5051_f700_init_verbs[] = {
1806         /* Line in, Mic */
1807         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x03},
1808         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1809         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x0},
1810         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x0},
1811         /* SPK  */
1812         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1813         {0x1a, AC_VERB_SET_CONNECT_SEL, 0x00},
1814         /* HP, Amp  */
1815         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1816         {0x16, AC_VERB_SET_CONNECT_SEL, 0x00},
1817         /* DAC1 */
1818         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1819         /* Record selector: Internal mic */
1820         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1) | 0x44},
1821         {0x14, AC_VERB_SET_CONNECT_SEL, 0x1},
1822         /* SPDIF route: PCM */
1823         {0x1c, AC_VERB_SET_CONNECT_SEL, 0x0},
1824         /* EAPD */
1825         {0x1a, AC_VERB_SET_EAPD_BTLENABLE, 0x2}, /* default on */
1826         {0x16, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN|CONEXANT_HP_EVENT},
1827         { } /* end */
1828 };
1829
1830 static void cxt5051_init_mic_port(struct hda_codec *codec, hda_nid_t nid,
1831                                  unsigned int event)
1832 {
1833         snd_hda_codec_write(codec, nid, 0,
1834                             AC_VERB_SET_UNSOLICITED_ENABLE,
1835                             AC_USRSP_EN | event);
1836 }
1837
1838 static const struct hda_verb cxt5051_ideapad_init_verbs[] = {
1839         /* Subwoofer */
1840         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1841         {0x1b, AC_VERB_SET_CONNECT_SEL, 0x00},
1842         { } /* end */
1843 };
1844
1845 /* initialize jack-sensing, too */
1846 static int cxt5051_init(struct hda_codec *codec)
1847 {
1848         struct conexant_spec *spec = codec->spec;
1849
1850         conexant_init(codec);
1851
1852         if (spec->auto_mic & AUTO_MIC_PORTB)
1853                 cxt5051_init_mic_port(codec, 0x17, CXT5051_PORTB_EVENT);
1854         if (spec->auto_mic & AUTO_MIC_PORTC)
1855                 cxt5051_init_mic_port(codec, 0x18, CXT5051_PORTC_EVENT);
1856
1857         if (codec->patch_ops.unsol_event) {
1858                 cxt5051_hp_automute(codec);
1859                 cxt5051_portb_automic(codec);
1860                 cxt5051_portc_automic(codec);
1861         }
1862         return 0;
1863 }
1864
1865
1866 enum {
1867         CXT5051_LAPTOP,  /* Laptops w/ EAPD support */
1868         CXT5051_HP,     /* no docking */
1869         CXT5051_HP_DV6736,      /* HP without mic switch */
1870         CXT5051_F700,       /* HP Compaq Presario F700 */
1871         CXT5051_TOSHIBA,        /* Toshiba M300 & co */
1872         CXT5051_IDEAPAD,        /* Lenovo IdeaPad Y430 */
1873         CXT5051_AUTO,           /* auto-parser */
1874         CXT5051_MODELS
1875 };
1876
1877 static const char *const cxt5051_models[CXT5051_MODELS] = {
1878         [CXT5051_LAPTOP]        = "laptop",
1879         [CXT5051_HP]            = "hp",
1880         [CXT5051_HP_DV6736]     = "hp-dv6736",
1881         [CXT5051_F700]          = "hp-700",
1882         [CXT5051_TOSHIBA]       = "toshiba",
1883         [CXT5051_IDEAPAD]       = "ideapad",
1884         [CXT5051_AUTO]          = "auto",
1885 };
1886
1887 static const struct snd_pci_quirk cxt5051_cfg_tbl[] = {
1888         SND_PCI_QUIRK(0x103c, 0x30cf, "HP DV6736", CXT5051_HP_DV6736),
1889         SND_PCI_QUIRK(0x103c, 0x360b, "Compaq Presario CQ60", CXT5051_HP),
1890         SND_PCI_QUIRK(0x103c, 0x30ea, "Compaq Presario F700", CXT5051_F700),
1891         SND_PCI_QUIRK(0x1179, 0xff50, "Toshiba M30x", CXT5051_TOSHIBA),
1892         SND_PCI_QUIRK(0x14f1, 0x0101, "Conexant Reference board",
1893                       CXT5051_LAPTOP),
1894         SND_PCI_QUIRK(0x14f1, 0x5051, "HP Spartan 1.1", CXT5051_HP),
1895         SND_PCI_QUIRK(0x17aa, 0x3a0d, "Lenovo IdeaPad", CXT5051_IDEAPAD),
1896         {}
1897 };
1898
1899 static int patch_cxt5051(struct hda_codec *codec)
1900 {
1901         struct conexant_spec *spec;
1902         int board_config;
1903
1904         board_config = snd_hda_check_board_config(codec, CXT5051_MODELS,
1905                                                   cxt5051_models,
1906                                                   cxt5051_cfg_tbl);
1907         if (board_config < 0)
1908                 board_config = CXT5051_AUTO; /* model=auto as default */
1909         if (board_config == CXT5051_AUTO)
1910                 return patch_conexant_auto(codec);
1911
1912         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1913         if (!spec)
1914                 return -ENOMEM;
1915         codec->spec = spec;
1916         codec->pin_amp_workaround = 1;
1917
1918         codec->patch_ops = conexant_patch_ops;
1919         codec->patch_ops.init = cxt5051_init;
1920
1921         spec->multiout.max_channels = 2;
1922         spec->multiout.num_dacs = ARRAY_SIZE(cxt5051_dac_nids);
1923         spec->multiout.dac_nids = cxt5051_dac_nids;
1924         spec->multiout.dig_out_nid = CXT5051_SPDIF_OUT;
1925         spec->num_adc_nids = 1; /* not 2; via auto-mic switch */
1926         spec->adc_nids = cxt5051_adc_nids;
1927         spec->num_mixers = 2;
1928         spec->mixers[0] = cxt5051_capture_mixers;
1929         spec->mixers[1] = cxt5051_playback_mixers;
1930         spec->num_init_verbs = 1;
1931         spec->init_verbs[0] = cxt5051_init_verbs;
1932         spec->spdif_route = 0;
1933         spec->num_channel_mode = ARRAY_SIZE(cxt5051_modes);
1934         spec->channel_mode = cxt5051_modes;
1935         spec->cur_adc = 0;
1936         spec->cur_adc_idx = 0;
1937
1938         set_beep_amp(spec, 0x13, 0, HDA_OUTPUT);
1939
1940         codec->patch_ops.unsol_event = cxt5051_hp_unsol_event;
1941
1942         spec->auto_mic = AUTO_MIC_PORTB | AUTO_MIC_PORTC;
1943         switch (board_config) {
1944         case CXT5051_HP:
1945                 spec->mixers[0] = cxt5051_hp_mixers;
1946                 break;
1947         case CXT5051_HP_DV6736:
1948                 spec->init_verbs[0] = cxt5051_hp_dv6736_init_verbs;
1949                 spec->mixers[0] = cxt5051_hp_dv6736_mixers;
1950                 spec->auto_mic = 0;
1951                 break;
1952         case CXT5051_F700:
1953                 spec->init_verbs[0] = cxt5051_f700_init_verbs;
1954                 spec->mixers[0] = cxt5051_f700_mixers;
1955                 spec->auto_mic = 0;
1956                 break;
1957         case CXT5051_TOSHIBA:
1958                 spec->mixers[0] = cxt5051_toshiba_mixers;
1959                 spec->auto_mic = AUTO_MIC_PORTB;
1960                 break;
1961         case CXT5051_IDEAPAD:
1962                 spec->init_verbs[spec->num_init_verbs++] =
1963                         cxt5051_ideapad_init_verbs;
1964                 spec->ideapad = 1;
1965                 break;
1966         }
1967
1968         if (spec->beep_amp)
1969                 snd_hda_attach_beep_device(codec, spec->beep_amp);
1970
1971         return 0;
1972 }
1973
1974 /* Conexant 5066 specific */
1975
1976 static const hda_nid_t cxt5066_dac_nids[1] = { 0x10 };
1977 static const hda_nid_t cxt5066_adc_nids[3] = { 0x14, 0x15, 0x16 };
1978 static const hda_nid_t cxt5066_capsrc_nids[1] = { 0x17 };
1979 static const hda_nid_t cxt5066_digout_pin_nids[2] = { 0x20, 0x22 };
1980
1981 /* OLPC's microphone port is DC coupled for use with external sensors,
1982  * therefore we use a 50% mic bias in order to center the input signal with
1983  * the DC input range of the codec. */
1984 #define CXT5066_OLPC_EXT_MIC_BIAS PIN_VREF50
1985
1986 static const struct hda_channel_mode cxt5066_modes[1] = {
1987         { 2, NULL },
1988 };
1989
1990 #define HP_PRESENT_PORT_A       (1 << 0)
1991 #define HP_PRESENT_PORT_D       (1 << 1)
1992 #define hp_port_a_present(spec) ((spec)->hp_present & HP_PRESENT_PORT_A)
1993 #define hp_port_d_present(spec) ((spec)->hp_present & HP_PRESENT_PORT_D)
1994
1995 static void cxt5066_update_speaker(struct hda_codec *codec)
1996 {
1997         struct conexant_spec *spec = codec->spec;
1998         unsigned int pinctl;
1999
2000         snd_printdd("CXT5066: update speaker, hp_present=%d, cur_eapd=%d\n",
2001                     spec->hp_present, spec->cur_eapd);
2002
2003         /* Port A (HP) */
2004         pinctl = (hp_port_a_present(spec) && spec->cur_eapd) ? PIN_HP : 0;
2005         snd_hda_set_pin_ctl(codec, 0x19, pinctl);
2006
2007         /* Port D (HP/LO) */
2008         pinctl = spec->cur_eapd ? spec->port_d_mode : 0;
2009         if (spec->dell_automute || spec->thinkpad) {
2010                 /* Mute if Port A is connected */
2011                 if (hp_port_a_present(spec))
2012                         pinctl = 0;
2013         } else {
2014                 /* Thinkpad/Dell doesn't give pin-D status */
2015                 if (!hp_port_d_present(spec))
2016                         pinctl = 0;
2017         }
2018         snd_hda_set_pin_ctl(codec, 0x1c, pinctl);
2019
2020         /* CLASS_D AMP */
2021         pinctl = (!spec->hp_present && spec->cur_eapd) ? PIN_OUT : 0;
2022         snd_hda_set_pin_ctl(codec, 0x1f, pinctl);
2023 }
2024
2025 /* turn on/off EAPD (+ mute HP) as a master switch */
2026 static int cxt5066_hp_master_sw_put(struct snd_kcontrol *kcontrol,
2027                                     struct snd_ctl_elem_value *ucontrol)
2028 {
2029         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2030
2031         if (!cxt_eapd_put(kcontrol, ucontrol))
2032                 return 0;
2033
2034         cxt5066_update_speaker(codec);
2035         return 1;
2036 }
2037
2038 static const struct hda_input_mux cxt5066_olpc_dc_bias = {
2039         .num_items = 3,
2040         .items = {
2041                 { "Off", PIN_IN },
2042                 { "50%", PIN_VREF50 },
2043                 { "80%", PIN_VREF80 },
2044         },
2045 };
2046
2047 static int cxt5066_set_olpc_dc_bias(struct hda_codec *codec)
2048 {
2049         struct conexant_spec *spec = codec->spec;
2050         /* Even though port F is the DC input, the bias is controlled on port B.
2051          * we also leave that port as an active input (but unselected) in DC mode
2052          * just in case that is necessary to make the bias setting take effect. */
2053         return snd_hda_set_pin_ctl_cache(codec, 0x1a,
2054                 cxt5066_olpc_dc_bias.items[spec->dc_input_bias].index);
2055 }
2056
2057 /* OLPC defers mic widget control until when capture is started because the
2058  * microphone LED comes on as soon as these settings are put in place. if we
2059  * did this before recording, it would give the false indication that recording
2060  * is happening when it is not. */
2061 static void cxt5066_olpc_select_mic(struct hda_codec *codec)
2062 {
2063         struct conexant_spec *spec = codec->spec;
2064         if (!spec->recording)
2065                 return;
2066
2067         if (spec->dc_enable) {
2068                 /* in DC mode we ignore presence detection and just use the jack
2069                  * through our special DC port */
2070                 const struct hda_verb enable_dc_mode[] = {
2071                         /* disble internal mic, port C */
2072                         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2073
2074                         /* enable DC capture, port F */
2075                         {0x1e, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
2076                         {},
2077                 };
2078
2079                 snd_hda_sequence_write(codec, enable_dc_mode);
2080                 /* port B input disabled (and bias set) through the following call */
2081                 cxt5066_set_olpc_dc_bias(codec);
2082                 return;
2083         }
2084
2085         /* disable DC (port F) */
2086         snd_hda_set_pin_ctl(codec, 0x1e, 0);
2087
2088         /* external mic, port B */
2089         snd_hda_set_pin_ctl(codec, 0x1a,
2090                 spec->ext_mic_present ? CXT5066_OLPC_EXT_MIC_BIAS : 0);
2091
2092         /* internal mic, port C */
2093         snd_hda_set_pin_ctl(codec, 0x1b,
2094                 spec->ext_mic_present ? 0 : PIN_VREF80);
2095 }
2096
2097 /* toggle input of built-in and mic jack appropriately */
2098 static void cxt5066_olpc_automic(struct hda_codec *codec)
2099 {
2100         struct conexant_spec *spec = codec->spec;
2101         unsigned int present;
2102
2103         if (spec->dc_enable) /* don't do presence detection in DC mode */
2104                 return;
2105
2106         present = snd_hda_codec_read(codec, 0x1a, 0,
2107                                      AC_VERB_GET_PIN_SENSE, 0) & 0x80000000;
2108         if (present)
2109                 snd_printdd("CXT5066: external microphone detected\n");
2110         else
2111                 snd_printdd("CXT5066: external microphone absent\n");
2112
2113         snd_hda_codec_write(codec, 0x17, 0, AC_VERB_SET_CONNECT_SEL,
2114                 present ? 0 : 1);
2115         spec->ext_mic_present = !!present;
2116
2117         cxt5066_olpc_select_mic(codec);
2118 }
2119
2120 /* toggle input of built-in digital mic and mic jack appropriately */
2121 static void cxt5066_vostro_automic(struct hda_codec *codec)
2122 {
2123         unsigned int present;
2124
2125         struct hda_verb ext_mic_present[] = {
2126                 /* enable external mic, port B */
2127                 {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
2128
2129                 /* switch to external mic input */
2130                 {0x17, AC_VERB_SET_CONNECT_SEL, 0},
2131                 {0x14, AC_VERB_SET_CONNECT_SEL, 0},
2132
2133                 /* disable internal digital mic */
2134                 {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2135                 {}
2136         };
2137         static const struct hda_verb ext_mic_absent[] = {
2138                 /* enable internal mic, port C */
2139                 {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
2140
2141                 /* switch to internal mic input */
2142                 {0x14, AC_VERB_SET_CONNECT_SEL, 2},
2143
2144                 /* disable external mic, port B */
2145                 {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2146                 {}
2147         };
2148
2149         present = snd_hda_jack_detect(codec, 0x1a);
2150         if (present) {
2151                 snd_printdd("CXT5066: external microphone detected\n");
2152                 snd_hda_sequence_write(codec, ext_mic_present);
2153         } else {
2154                 snd_printdd("CXT5066: external microphone absent\n");
2155                 snd_hda_sequence_write(codec, ext_mic_absent);
2156         }
2157 }
2158
2159 /* toggle input of built-in digital mic and mic jack appropriately */
2160 static void cxt5066_ideapad_automic(struct hda_codec *codec)
2161 {
2162         unsigned int present;
2163
2164         struct hda_verb ext_mic_present[] = {
2165                 {0x14, AC_VERB_SET_CONNECT_SEL, 0},
2166                 {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
2167                 {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2168                 {}
2169         };
2170         static const struct hda_verb ext_mic_absent[] = {
2171                 {0x14, AC_VERB_SET_CONNECT_SEL, 2},
2172                 {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
2173                 {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2174                 {}
2175         };
2176
2177         present = snd_hda_jack_detect(codec, 0x1b);
2178         if (present) {
2179                 snd_printdd("CXT5066: external microphone detected\n");
2180                 snd_hda_sequence_write(codec, ext_mic_present);
2181         } else {
2182                 snd_printdd("CXT5066: external microphone absent\n");
2183                 snd_hda_sequence_write(codec, ext_mic_absent);
2184         }
2185 }
2186
2187
2188 /* toggle input of built-in digital mic and mic jack appropriately */
2189 static void cxt5066_asus_automic(struct hda_codec *codec)
2190 {
2191         unsigned int present;
2192
2193         present = snd_hda_jack_detect(codec, 0x1b);
2194         snd_printdd("CXT5066: external microphone present=%d\n", present);
2195         snd_hda_codec_write(codec, 0x17, 0, AC_VERB_SET_CONNECT_SEL,
2196                             present ? 1 : 0);
2197 }
2198
2199
2200 /* toggle input of built-in digital mic and mic jack appropriately */
2201 static void cxt5066_hp_laptop_automic(struct hda_codec *codec)
2202 {
2203         unsigned int present;
2204
2205         present = snd_hda_jack_detect(codec, 0x1b);
2206         snd_printdd("CXT5066: external microphone present=%d\n", present);
2207         snd_hda_codec_write(codec, 0x17, 0, AC_VERB_SET_CONNECT_SEL,
2208                             present ? 1 : 3);
2209 }
2210
2211
2212 /* toggle input of built-in digital mic and mic jack appropriately
2213    order is: external mic -> dock mic -> interal mic */
2214 static void cxt5066_thinkpad_automic(struct hda_codec *codec)
2215 {
2216         unsigned int ext_present, dock_present;
2217
2218         static const struct hda_verb ext_mic_present[] = {
2219                 {0x14, AC_VERB_SET_CONNECT_SEL, 0},
2220                 {0x17, AC_VERB_SET_CONNECT_SEL, 1},
2221                 {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
2222                 {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2223                 {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2224                 {}
2225         };
2226         static const struct hda_verb dock_mic_present[] = {
2227                 {0x14, AC_VERB_SET_CONNECT_SEL, 0},
2228                 {0x17, AC_VERB_SET_CONNECT_SEL, 0},
2229                 {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
2230                 {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2231                 {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2232                 {}
2233         };
2234         static const struct hda_verb ext_mic_absent[] = {
2235                 {0x14, AC_VERB_SET_CONNECT_SEL, 2},
2236                 {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
2237                 {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2238                 {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2239                 {}
2240         };
2241
2242         ext_present = snd_hda_jack_detect(codec, 0x1b);
2243         dock_present = snd_hda_jack_detect(codec, 0x1a);
2244         if (ext_present) {
2245                 snd_printdd("CXT5066: external microphone detected\n");
2246                 snd_hda_sequence_write(codec, ext_mic_present);
2247         } else if (dock_present) {
2248                 snd_printdd("CXT5066: dock microphone detected\n");
2249                 snd_hda_sequence_write(codec, dock_mic_present);
2250         } else {
2251                 snd_printdd("CXT5066: external microphone absent\n");
2252                 snd_hda_sequence_write(codec, ext_mic_absent);
2253         }
2254 }
2255
2256 /* mute internal speaker if HP is plugged */
2257 static void cxt5066_hp_automute(struct hda_codec *codec)
2258 {
2259         struct conexant_spec *spec = codec->spec;
2260         unsigned int portA, portD;
2261
2262         /* Port A */
2263         portA = snd_hda_jack_detect(codec, 0x19);
2264
2265         /* Port D */
2266         portD = snd_hda_jack_detect(codec, 0x1c);
2267
2268         spec->hp_present = portA ? HP_PRESENT_PORT_A : 0;
2269         spec->hp_present |= portD ? HP_PRESENT_PORT_D : 0;
2270         snd_printdd("CXT5066: hp automute portA=%x portD=%x present=%d\n",
2271                 portA, portD, spec->hp_present);
2272         cxt5066_update_speaker(codec);
2273 }
2274
2275 /* Dispatch the right mic autoswitch function */
2276 static void cxt5066_automic(struct hda_codec *codec)
2277 {
2278         struct conexant_spec *spec = codec->spec;
2279
2280         if (spec->dell_vostro)
2281                 cxt5066_vostro_automic(codec);
2282         else if (spec->ideapad)
2283                 cxt5066_ideapad_automic(codec);
2284         else if (spec->thinkpad)
2285                 cxt5066_thinkpad_automic(codec);
2286         else if (spec->hp_laptop)
2287                 cxt5066_hp_laptop_automic(codec);
2288         else if (spec->asus)
2289                 cxt5066_asus_automic(codec);
2290 }
2291
2292 /* unsolicited event for jack sensing */
2293 static void cxt5066_olpc_unsol_event(struct hda_codec *codec, unsigned int res)
2294 {
2295         struct conexant_spec *spec = codec->spec;
2296         snd_printdd("CXT5066: unsol event %x (%x)\n", res, res >> 26);
2297         switch (res >> 26) {
2298         case CONEXANT_HP_EVENT:
2299                 cxt5066_hp_automute(codec);
2300                 break;
2301         case CONEXANT_MIC_EVENT:
2302                 /* ignore mic events in DC mode; we're always using the jack */
2303                 if (!spec->dc_enable)
2304                         cxt5066_olpc_automic(codec);
2305                 break;
2306         }
2307 }
2308
2309 /* unsolicited event for jack sensing */
2310 static void cxt5066_unsol_event(struct hda_codec *codec, unsigned int res)
2311 {
2312         snd_printdd("CXT5066: unsol event %x (%x)\n", res, res >> 26);
2313         switch (res >> 26) {
2314         case CONEXANT_HP_EVENT:
2315                 cxt5066_hp_automute(codec);
2316                 break;
2317         case CONEXANT_MIC_EVENT:
2318                 cxt5066_automic(codec);
2319                 break;
2320         }
2321 }
2322
2323
2324 static const struct hda_input_mux cxt5066_analog_mic_boost = {
2325         .num_items = 5,
2326         .items = {
2327                 { "0dB",  0 },
2328                 { "10dB", 1 },
2329                 { "20dB", 2 },
2330                 { "30dB", 3 },
2331                 { "40dB", 4 },
2332         },
2333 };
2334
2335 static void cxt5066_set_mic_boost(struct hda_codec *codec)
2336 {
2337         struct conexant_spec *spec = codec->spec;
2338         snd_hda_codec_write_cache(codec, 0x17, 0,
2339                 AC_VERB_SET_AMP_GAIN_MUTE,
2340                 AC_AMP_SET_RIGHT | AC_AMP_SET_LEFT | AC_AMP_SET_OUTPUT |
2341                         cxt5066_analog_mic_boost.items[spec->mic_boost].index);
2342         if (spec->ideapad || spec->thinkpad) {
2343                 /* adjust the internal mic as well...it is not through 0x17 */
2344                 snd_hda_codec_write_cache(codec, 0x23, 0,
2345                         AC_VERB_SET_AMP_GAIN_MUTE,
2346                         AC_AMP_SET_RIGHT | AC_AMP_SET_LEFT | AC_AMP_SET_INPUT |
2347                                 cxt5066_analog_mic_boost.
2348                                         items[spec->mic_boost].index);
2349         }
2350 }
2351
2352 static int cxt5066_mic_boost_mux_enum_info(struct snd_kcontrol *kcontrol,
2353                                            struct snd_ctl_elem_info *uinfo)
2354 {
2355         return snd_hda_input_mux_info(&cxt5066_analog_mic_boost, uinfo);
2356 }
2357
2358 static int cxt5066_mic_boost_mux_enum_get(struct snd_kcontrol *kcontrol,
2359                                           struct snd_ctl_elem_value *ucontrol)
2360 {
2361         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2362         struct conexant_spec *spec = codec->spec;
2363         ucontrol->value.enumerated.item[0] = spec->mic_boost;
2364         return 0;
2365 }
2366
2367 static int cxt5066_mic_boost_mux_enum_put(struct snd_kcontrol *kcontrol,
2368                                           struct snd_ctl_elem_value *ucontrol)
2369 {
2370         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2371         struct conexant_spec *spec = codec->spec;
2372         const struct hda_input_mux *imux = &cxt5066_analog_mic_boost;
2373         unsigned int idx;
2374         idx = ucontrol->value.enumerated.item[0];
2375         if (idx >= imux->num_items)
2376                 idx = imux->num_items - 1;
2377
2378         spec->mic_boost = idx;
2379         if (!spec->dc_enable)
2380                 cxt5066_set_mic_boost(codec);
2381         return 1;
2382 }
2383
2384 static void cxt5066_enable_dc(struct hda_codec *codec)
2385 {
2386         const struct hda_verb enable_dc_mode[] = {
2387                 /* disable gain */
2388                 {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2389
2390                 /* switch to DC input */
2391                 {0x17, AC_VERB_SET_CONNECT_SEL, 3},
2392                 {}
2393         };
2394
2395         /* configure as input source */
2396         snd_hda_sequence_write(codec, enable_dc_mode);
2397         cxt5066_olpc_select_mic(codec); /* also sets configured bias */
2398 }
2399
2400 static void cxt5066_disable_dc(struct hda_codec *codec)
2401 {
2402         /* reconfigure input source */
2403         cxt5066_set_mic_boost(codec);
2404         /* automic also selects the right mic if we're recording */
2405         cxt5066_olpc_automic(codec);
2406 }
2407
2408 static int cxt5066_olpc_dc_get(struct snd_kcontrol *kcontrol,
2409                              struct snd_ctl_elem_value *ucontrol)
2410 {
2411         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2412         struct conexant_spec *spec = codec->spec;
2413         ucontrol->value.integer.value[0] = spec->dc_enable;
2414         return 0;
2415 }
2416
2417 static int cxt5066_olpc_dc_put(struct snd_kcontrol *kcontrol,
2418                              struct snd_ctl_elem_value *ucontrol)
2419 {
2420         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2421         struct conexant_spec *spec = codec->spec;
2422         int dc_enable = !!ucontrol->value.integer.value[0];
2423
2424         if (dc_enable == spec->dc_enable)
2425                 return 0;
2426
2427         spec->dc_enable = dc_enable;
2428         if (dc_enable)
2429                 cxt5066_enable_dc(codec);
2430         else
2431                 cxt5066_disable_dc(codec);
2432
2433         return 1;
2434 }
2435
2436 static int cxt5066_olpc_dc_bias_enum_info(struct snd_kcontrol *kcontrol,
2437                                            struct snd_ctl_elem_info *uinfo)
2438 {
2439         return snd_hda_input_mux_info(&cxt5066_olpc_dc_bias, uinfo);
2440 }
2441
2442 static int cxt5066_olpc_dc_bias_enum_get(struct snd_kcontrol *kcontrol,
2443                                           struct snd_ctl_elem_value *ucontrol)
2444 {
2445         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2446         struct conexant_spec *spec = codec->spec;
2447         ucontrol->value.enumerated.item[0] = spec->dc_input_bias;
2448         return 0;
2449 }
2450
2451 static int cxt5066_olpc_dc_bias_enum_put(struct snd_kcontrol *kcontrol,
2452                                           struct snd_ctl_elem_value *ucontrol)
2453 {
2454         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2455         struct conexant_spec *spec = codec->spec;
2456         const struct hda_input_mux *imux = &cxt5066_analog_mic_boost;
2457         unsigned int idx;
2458
2459         idx = ucontrol->value.enumerated.item[0];
2460         if (idx >= imux->num_items)
2461                 idx = imux->num_items - 1;
2462
2463         spec->dc_input_bias = idx;
2464         if (spec->dc_enable)
2465                 cxt5066_set_olpc_dc_bias(codec);
2466         return 1;
2467 }
2468
2469 static void cxt5066_olpc_capture_prepare(struct hda_codec *codec)
2470 {
2471         struct conexant_spec *spec = codec->spec;
2472         /* mark as recording and configure the microphone widget so that the
2473          * recording LED comes on. */
2474         spec->recording = 1;
2475         cxt5066_olpc_select_mic(codec);
2476 }
2477
2478 static void cxt5066_olpc_capture_cleanup(struct hda_codec *codec)
2479 {
2480         struct conexant_spec *spec = codec->spec;
2481         const struct hda_verb disable_mics[] = {
2482                 /* disable external mic, port B */
2483                 {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2484
2485                 /* disble internal mic, port C */
2486                 {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2487
2488                 /* disable DC capture, port F */
2489                 {0x1e, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2490                 {},
2491         };
2492
2493         snd_hda_sequence_write(codec, disable_mics);
2494         spec->recording = 0;
2495 }
2496
2497 static void conexant_check_dig_outs(struct hda_codec *codec,
2498                                     const hda_nid_t *dig_pins,
2499                                     int num_pins)
2500 {
2501         struct conexant_spec *spec = codec->spec;
2502         hda_nid_t *nid_loc = &spec->multiout.dig_out_nid;
2503         int i;
2504
2505         for (i = 0; i < num_pins; i++, dig_pins++) {
2506                 unsigned int cfg = snd_hda_codec_get_pincfg(codec, *dig_pins);
2507                 if (get_defcfg_connect(cfg) == AC_JACK_PORT_NONE)
2508                         continue;
2509                 if (snd_hda_get_connections(codec, *dig_pins, nid_loc, 1) != 1)
2510                         continue;
2511                 if (spec->slave_dig_outs[0])
2512                         nid_loc++;
2513                 else
2514                         nid_loc = spec->slave_dig_outs;
2515         }
2516 }
2517
2518 static const struct hda_input_mux cxt5066_capture_source = {
2519         .num_items = 4,
2520         .items = {
2521                 { "Mic B", 0 },
2522                 { "Mic C", 1 },
2523                 { "Mic E", 2 },
2524                 { "Mic F", 3 },
2525         },
2526 };
2527
2528 static const struct hda_bind_ctls cxt5066_bind_capture_vol_others = {
2529         .ops = &snd_hda_bind_vol,
2530         .values = {
2531                 HDA_COMPOSE_AMP_VAL(0x14, 3, 0, HDA_INPUT),
2532                 HDA_COMPOSE_AMP_VAL(0x14, 3, 2, HDA_INPUT),
2533                 0
2534         },
2535 };
2536
2537 static const struct hda_bind_ctls cxt5066_bind_capture_sw_others = {
2538         .ops = &snd_hda_bind_sw,
2539         .values = {
2540                 HDA_COMPOSE_AMP_VAL(0x14, 3, 0, HDA_INPUT),
2541                 HDA_COMPOSE_AMP_VAL(0x14, 3, 2, HDA_INPUT),
2542                 0
2543         },
2544 };
2545
2546 static const struct snd_kcontrol_new cxt5066_mixer_master[] = {
2547         HDA_CODEC_VOLUME("Master Playback Volume", 0x10, 0x00, HDA_OUTPUT),
2548         {}
2549 };
2550
2551 static const struct snd_kcontrol_new cxt5066_mixer_master_olpc[] = {
2552         {
2553                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2554                 .name = "Master Playback Volume",
2555                 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
2556                                   SNDRV_CTL_ELEM_ACCESS_TLV_READ |
2557                                   SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK,
2558                 .subdevice = HDA_SUBDEV_AMP_FLAG,
2559                 .info = snd_hda_mixer_amp_volume_info,
2560                 .get = snd_hda_mixer_amp_volume_get,
2561                 .put = snd_hda_mixer_amp_volume_put,
2562                 .tlv = { .c = snd_hda_mixer_amp_tlv },
2563                 /* offset by 28 volume steps to limit minimum gain to -46dB */
2564                 .private_value =
2565                         HDA_COMPOSE_AMP_VAL_OFS(0x10, 3, 0, HDA_OUTPUT, 28),
2566         },
2567         {}
2568 };
2569
2570 static const struct snd_kcontrol_new cxt5066_mixer_olpc_dc[] = {
2571         {
2572                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2573                 .name = "DC Mode Enable Switch",
2574                 .info = snd_ctl_boolean_mono_info,
2575                 .get = cxt5066_olpc_dc_get,
2576                 .put = cxt5066_olpc_dc_put,
2577         },
2578         {
2579                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2580                 .name = "DC Input Bias Enum",
2581                 .info = cxt5066_olpc_dc_bias_enum_info,
2582                 .get = cxt5066_olpc_dc_bias_enum_get,
2583                 .put = cxt5066_olpc_dc_bias_enum_put,
2584         },
2585         {}
2586 };
2587
2588 static const struct snd_kcontrol_new cxt5066_mixers[] = {
2589         {
2590                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2591                 .name = "Master Playback Switch",
2592                 .info = cxt_eapd_info,
2593                 .get = cxt_eapd_get,
2594                 .put = cxt5066_hp_master_sw_put,
2595                 .private_value = 0x1d,
2596         },
2597
2598         {
2599                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2600                 .name = "Analog Mic Boost Capture Enum",
2601                 .info = cxt5066_mic_boost_mux_enum_info,
2602                 .get = cxt5066_mic_boost_mux_enum_get,
2603                 .put = cxt5066_mic_boost_mux_enum_put,
2604         },
2605
2606         HDA_BIND_VOL("Capture Volume", &cxt5066_bind_capture_vol_others),
2607         HDA_BIND_SW("Capture Switch", &cxt5066_bind_capture_sw_others),
2608         {}
2609 };
2610
2611 static const struct snd_kcontrol_new cxt5066_vostro_mixers[] = {
2612         {
2613                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2614                 .name = "Internal Mic Boost Capture Enum",
2615                 .info = cxt5066_mic_boost_mux_enum_info,
2616                 .get = cxt5066_mic_boost_mux_enum_get,
2617                 .put = cxt5066_mic_boost_mux_enum_put,
2618                 .private_value = 0x23 | 0x100,
2619         },
2620         {}
2621 };
2622
2623 static const struct hda_verb cxt5066_init_verbs[] = {
2624         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80}, /* Port B */
2625         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80}, /* Port C */
2626         {0x1e, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN}, /* Port F */
2627         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN}, /* Port E */
2628
2629         /* Speakers  */
2630         {0x1f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2631         {0x1f, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2632
2633         /* HP, Amp  */
2634         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
2635         {0x19, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2636
2637         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
2638         {0x1c, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2639
2640         /* DAC1 */
2641         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2642
2643         /* Node 14 connections: 0x17 0x18 0x23 0x24 0x27 */
2644         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x50},
2645         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2646         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2) | 0x50},
2647         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2648         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
2649
2650         /* no digital microphone support yet */
2651         {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2652
2653         /* Audio input selector */
2654         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x3},
2655
2656         /* SPDIF route: PCM */
2657         {0x20, AC_VERB_SET_CONNECT_SEL, 0x0},
2658         {0x22, AC_VERB_SET_CONNECT_SEL, 0x0},
2659
2660         {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2661         {0x22, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2662
2663         /* EAPD */
2664         {0x1d, AC_VERB_SET_EAPD_BTLENABLE, 0x2}, /* default on */
2665
2666         /* not handling these yet */
2667         {0x19, AC_VERB_SET_UNSOLICITED_ENABLE, 0},
2668         {0x1a, AC_VERB_SET_UNSOLICITED_ENABLE, 0},
2669         {0x1b, AC_VERB_SET_UNSOLICITED_ENABLE, 0},
2670         {0x1c, AC_VERB_SET_UNSOLICITED_ENABLE, 0},
2671         {0x1d, AC_VERB_SET_UNSOLICITED_ENABLE, 0},
2672         {0x1e, AC_VERB_SET_UNSOLICITED_ENABLE, 0},
2673         {0x20, AC_VERB_SET_UNSOLICITED_ENABLE, 0},
2674         {0x22, AC_VERB_SET_UNSOLICITED_ENABLE, 0},
2675         { } /* end */
2676 };
2677
2678 static const struct hda_verb cxt5066_init_verbs_olpc[] = {
2679         /* Port A: headphones */
2680         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
2681         {0x19, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2682
2683         /* Port B: external microphone */
2684         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2685
2686         /* Port C: internal microphone */
2687         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2688
2689         /* Port D: unused */
2690         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2691
2692         /* Port E: unused, but has primary EAPD */
2693         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2694         {0x1d, AC_VERB_SET_EAPD_BTLENABLE, 0x2}, /* default on */
2695
2696         /* Port F: external DC input through microphone port */
2697         {0x1e, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2698
2699         /* Port G: internal speakers */
2700         {0x1f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2701         {0x1f, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2702
2703         /* DAC1 */
2704         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2705
2706         /* DAC2: unused */
2707         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2708
2709         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x50},
2710         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2711         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
2712         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2713         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2714         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2715         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
2716         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2717         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2718         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2719         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
2720         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2721
2722         /* Disable digital microphone port */
2723         {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2724
2725         /* Audio input selectors */
2726         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x3},
2727         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE },
2728
2729         /* Disable SPDIF */
2730         {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2731         {0x22, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2732
2733         /* enable unsolicited events for Port A and B */
2734         {0x19, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_HP_EVENT},
2735         {0x1a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_MIC_EVENT},
2736         { } /* end */
2737 };
2738
2739 static const struct hda_verb cxt5066_init_verbs_vostro[] = {
2740         /* Port A: headphones */
2741         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2742         {0x19, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2743
2744         /* Port B: external microphone */
2745         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2746
2747         /* Port C: unused */
2748         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2749
2750         /* Port D: unused */
2751         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2752
2753         /* Port E: unused, but has primary EAPD */
2754         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2755         {0x1d, AC_VERB_SET_EAPD_BTLENABLE, 0x2}, /* default on */
2756
2757         /* Port F: unused */
2758         {0x1e, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2759
2760         /* Port G: internal speakers */
2761         {0x1f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2762         {0x1f, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2763
2764         /* DAC1 */
2765         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2766
2767         /* DAC2: unused */
2768         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
2769
2770         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2771         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2772         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
2773         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2774         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2775         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2776         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
2777         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2778         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
2779         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2780         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
2781         {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2782
2783         /* Digital microphone port */
2784         {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
2785
2786         /* Audio input selectors */
2787         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x3},
2788         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE },
2789
2790         /* Disable SPDIF */
2791         {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2792         {0x22, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2793
2794         /* enable unsolicited events for Port A and B */
2795         {0x19, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_HP_EVENT},
2796         {0x1a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_MIC_EVENT},
2797         { } /* end */
2798 };
2799
2800 static const struct hda_verb cxt5066_init_verbs_ideapad[] = {
2801         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80}, /* Port B */
2802         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80}, /* Port C */
2803         {0x1e, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN}, /* Port F */
2804         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN}, /* Port E */
2805
2806         /* Speakers  */
2807         {0x1f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2808         {0x1f, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2809
2810         /* HP, Amp  */
2811         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
2812         {0x19, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2813
2814         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
2815         {0x1c, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2816
2817         /* DAC1 */
2818         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2819
2820         /* Node 14 connections: 0x17 0x18 0x23 0x24 0x27 */
2821         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x50},
2822         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2823         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2) | 0x50},
2824         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2825         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
2826         {0x14, AC_VERB_SET_CONNECT_SEL, 2},     /* default to internal mic */
2827
2828         /* Audio input selector */
2829         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x2},
2830         {0x17, AC_VERB_SET_CONNECT_SEL, 1},     /* route ext mic */
2831
2832         /* SPDIF route: PCM */
2833         {0x20, AC_VERB_SET_CONNECT_SEL, 0x0},
2834         {0x22, AC_VERB_SET_CONNECT_SEL, 0x0},
2835
2836         {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2837         {0x22, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2838
2839         /* internal microphone */
2840         {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN}, /* enable internal mic */
2841
2842         /* EAPD */
2843         {0x1d, AC_VERB_SET_EAPD_BTLENABLE, 0x2}, /* default on */
2844
2845         {0x19, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_HP_EVENT},
2846         {0x1b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_MIC_EVENT},
2847         { } /* end */
2848 };
2849
2850 static const struct hda_verb cxt5066_init_verbs_thinkpad[] = {
2851         {0x1e, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN}, /* Port F */
2852         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN}, /* Port E */
2853
2854         /* Port G: internal speakers  */
2855         {0x1f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2856         {0x1f, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2857
2858         /* Port A: HP, Amp  */
2859         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2860         {0x19, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2861
2862         /* Port B: Mic Dock */
2863         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2864
2865         /* Port C: Mic */
2866         {0x1b, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2867
2868         /* Port D: HP Dock, Amp */
2869         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, 0},
2870         {0x1c, AC_VERB_SET_CONNECT_SEL, 0x00}, /* DAC1 */
2871
2872         /* DAC1 */
2873         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
2874
2875         /* Node 14 connections: 0x17 0x18 0x23 0x24 0x27 */
2876         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x50},
2877         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
2878         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2) | 0x50},
2879         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
2880         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
2881         {0x14, AC_VERB_SET_CONNECT_SEL, 2},     /* default to internal mic */
2882
2883         /* Audio input selector */
2884         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE | 0x2},
2885         {0x17, AC_VERB_SET_CONNECT_SEL, 1},     /* route ext mic */
2886
2887         /* SPDIF route: PCM */
2888         {0x20, AC_VERB_SET_CONNECT_SEL, 0x0},
2889         {0x22, AC_VERB_SET_CONNECT_SEL, 0x0},
2890
2891         {0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2892         {0x22, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2893
2894         /* internal microphone */
2895         {0x23, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN}, /* enable internal mic */
2896
2897         /* EAPD */
2898         {0x1d, AC_VERB_SET_EAPD_BTLENABLE, 0x2}, /* default on */
2899
2900         /* enable unsolicited events for Port A, B, C and D */
2901         {0x19, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_HP_EVENT},
2902         {0x1c, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_HP_EVENT},
2903         {0x1a, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_MIC_EVENT},
2904         {0x1b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_MIC_EVENT},
2905         { } /* end */
2906 };
2907
2908 static const struct hda_verb cxt5066_init_verbs_portd_lo[] = {
2909         {0x1c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
2910         { } /* end */
2911 };
2912
2913
2914 static const struct hda_verb cxt5066_init_verbs_hp_laptop[] = {
2915         {0x14, AC_VERB_SET_CONNECT_SEL, 0x0},
2916         {0x19, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_HP_EVENT},
2917         {0x1b, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_MIC_EVENT},
2918         { } /* end */
2919 };
2920
2921 /* initialize jack-sensing, too */
2922 static int cxt5066_init(struct hda_codec *codec)
2923 {
2924         snd_printdd("CXT5066: init\n");
2925         conexant_init(codec);
2926         if (codec->patch_ops.unsol_event) {
2927                 cxt5066_hp_automute(codec);
2928                 cxt5066_automic(codec);
2929         }
2930         cxt5066_set_mic_boost(codec);
2931         return 0;
2932 }
2933
2934 static int cxt5066_olpc_init(struct hda_codec *codec)
2935 {
2936         struct conexant_spec *spec = codec->spec;
2937         snd_printdd("CXT5066: init\n");
2938         conexant_init(codec);
2939         cxt5066_hp_automute(codec);
2940         if (!spec->dc_enable) {
2941                 cxt5066_set_mic_boost(codec);
2942                 cxt5066_olpc_automic(codec);
2943         } else {
2944                 cxt5066_enable_dc(codec);
2945         }
2946         return 0;
2947 }
2948
2949 enum {
2950         CXT5066_LAPTOP,         /* Laptops w/ EAPD support */
2951         CXT5066_DELL_LAPTOP,    /* Dell Laptop */
2952         CXT5066_OLPC_XO_1_5,    /* OLPC XO 1.5 */
2953         CXT5066_DELL_VOSTRO,    /* Dell Vostro 1015i */
2954         CXT5066_IDEAPAD,        /* Lenovo IdeaPad U150 */
2955         CXT5066_THINKPAD,       /* Lenovo ThinkPad T410s, others? */
2956         CXT5066_ASUS,           /* Asus K52JU, Lenovo G560 - Int mic at 0x1a and Ext mic at 0x1b */
2957         CXT5066_HP_LAPTOP,      /* HP Laptop */
2958         CXT5066_AUTO,           /* BIOS auto-parser */
2959         CXT5066_MODELS
2960 };
2961
2962 static const char * const cxt5066_models[CXT5066_MODELS] = {
2963         [CXT5066_LAPTOP]        = "laptop",
2964         [CXT5066_DELL_LAPTOP]   = "dell-laptop",
2965         [CXT5066_OLPC_XO_1_5]   = "olpc-xo-1_5",
2966         [CXT5066_DELL_VOSTRO]   = "dell-vostro",
2967         [CXT5066_IDEAPAD]       = "ideapad",
2968         [CXT5066_THINKPAD]      = "thinkpad",
2969         [CXT5066_ASUS]          = "asus",
2970         [CXT5066_HP_LAPTOP]     = "hp-laptop",
2971         [CXT5066_AUTO]          = "auto",
2972 };
2973
2974 static const struct snd_pci_quirk cxt5066_cfg_tbl[] = {
2975         SND_PCI_QUIRK_MASK(0x1025, 0xff00, 0x0400, "Acer", CXT5066_IDEAPAD),
2976         SND_PCI_QUIRK(0x1028, 0x02d8, "Dell Vostro", CXT5066_DELL_VOSTRO),
2977         SND_PCI_QUIRK(0x1028, 0x02f5, "Dell Vostro 320", CXT5066_IDEAPAD),
2978         SND_PCI_QUIRK(0x1028, 0x0401, "Dell Vostro 1014", CXT5066_DELL_VOSTRO),
2979         SND_PCI_QUIRK(0x1028, 0x0408, "Dell Inspiron One 19T", CXT5066_IDEAPAD),
2980         SND_PCI_QUIRK(0x1028, 0x050f, "Dell Inspiron", CXT5066_IDEAPAD),
2981         SND_PCI_QUIRK(0x1028, 0x0510, "Dell Vostro", CXT5066_IDEAPAD),
2982         SND_PCI_QUIRK(0x103c, 0x360b, "HP G60", CXT5066_HP_LAPTOP),
2983         SND_PCI_QUIRK(0x1043, 0x13f3, "Asus A52J", CXT5066_ASUS),
2984         SND_PCI_QUIRK(0x1043, 0x1643, "Asus K52JU", CXT5066_ASUS),
2985         SND_PCI_QUIRK(0x1043, 0x1993, "Asus U50F", CXT5066_ASUS),
2986         SND_PCI_QUIRK(0x1179, 0xff1e, "Toshiba Satellite C650D", CXT5066_IDEAPAD),
2987         SND_PCI_QUIRK(0x1179, 0xff50, "Toshiba Satellite P500-PSPGSC-01800T", CXT5066_OLPC_XO_1_5),
2988         SND_PCI_QUIRK(0x14f1, 0x0101, "Conexant Reference board",
2989                       CXT5066_LAPTOP),
2990         SND_PCI_QUIRK(0x152d, 0x0833, "OLPC XO-1.5", CXT5066_OLPC_XO_1_5),
2991         SND_PCI_QUIRK(0x17aa, 0x20f2, "Lenovo T400s", CXT5066_THINKPAD),
2992         SND_PCI_QUIRK(0x17aa, 0x21c5, "Thinkpad Edge 13", CXT5066_THINKPAD),
2993         SND_PCI_QUIRK(0x17aa, 0x21c6, "Thinkpad Edge 13", CXT5066_ASUS),
2994         SND_PCI_QUIRK(0x17aa, 0x21da, "Lenovo X220", CXT5066_THINKPAD),
2995         SND_PCI_QUIRK(0x17aa, 0x21db, "Lenovo X220-tablet", CXT5066_THINKPAD),
2996         SND_PCI_QUIRK(0x17aa, 0x3a0d, "Lenovo U350", CXT5066_ASUS),
2997         SND_PCI_QUIRK(0x17aa, 0x38af, "Lenovo G560", CXT5066_ASUS),
2998         {}
2999 };
3000
3001 static int patch_cxt5066(struct hda_codec *codec)
3002 {
3003         struct conexant_spec *spec;
3004         int board_config;
3005
3006         board_config = snd_hda_check_board_config(codec, CXT5066_MODELS,
3007                                                   cxt5066_models, cxt5066_cfg_tbl);
3008         if (board_config < 0)
3009                 board_config = CXT5066_AUTO; /* model=auto as default */
3010         if (board_config == CXT5066_AUTO)
3011                 return patch_conexant_auto(codec);
3012
3013         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
3014         if (!spec)
3015                 return -ENOMEM;
3016         codec->spec = spec;
3017
3018         codec->patch_ops = conexant_patch_ops;
3019         codec->patch_ops.init = conexant_init;
3020
3021         spec->dell_automute = 0;
3022         spec->multiout.max_channels = 2;
3023         spec->multiout.num_dacs = ARRAY_SIZE(cxt5066_dac_nids);
3024         spec->multiout.dac_nids = cxt5066_dac_nids;
3025         conexant_check_dig_outs(codec, cxt5066_digout_pin_nids,
3026             ARRAY_SIZE(cxt5066_digout_pin_nids));
3027         spec->num_adc_nids = 1;
3028         spec->adc_nids = cxt5066_adc_nids;
3029         spec->capsrc_nids = cxt5066_capsrc_nids;
3030         spec->input_mux = &cxt5066_capture_source;
3031
3032         spec->port_d_mode = PIN_HP;
3033
3034         spec->num_init_verbs = 1;
3035         spec->init_verbs[0] = cxt5066_init_verbs;
3036         spec->num_channel_mode = ARRAY_SIZE(cxt5066_modes);
3037         spec->channel_mode = cxt5066_modes;
3038         spec->cur_adc = 0;
3039         spec->cur_adc_idx = 0;
3040
3041         set_beep_amp(spec, 0x13, 0, HDA_OUTPUT);
3042
3043         switch (board_config) {
3044         default:
3045         case CXT5066_LAPTOP:
3046                 spec->mixers[spec->num_mixers++] = cxt5066_mixer_master;
3047                 spec->mixers[spec->num_mixers++] = cxt5066_mixers;
3048                 break;
3049         case CXT5066_DELL_LAPTOP:
3050                 spec->mixers[spec->num_mixers++] = cxt5066_mixer_master;
3051                 spec->mixers[spec->num_mixers++] = cxt5066_mixers;
3052
3053                 spec->port_d_mode = PIN_OUT;
3054                 spec->init_verbs[spec->num_init_verbs] = cxt5066_init_verbs_portd_lo;
3055                 spec->num_init_verbs++;
3056                 spec->dell_automute = 1;
3057                 break;
3058         case CXT5066_ASUS:
3059         case CXT5066_HP_LAPTOP:
3060                 codec->patch_ops.init = cxt5066_init;
3061                 codec->patch_ops.unsol_event = cxt5066_unsol_event;
3062                 spec->init_verbs[spec->num_init_verbs] =
3063                         cxt5066_init_verbs_hp_laptop;
3064                 spec->num_init_verbs++;
3065                 spec->hp_laptop = board_config == CXT5066_HP_LAPTOP;
3066                 spec->asus = board_config == CXT5066_ASUS;
3067                 spec->mixers[spec->num_mixers++] = cxt5066_mixer_master;
3068                 spec->mixers[spec->num_mixers++] = cxt5066_mixers;
3069                 /* no S/PDIF out */
3070                 if (board_config == CXT5066_HP_LAPTOP)
3071                         spec->multiout.dig_out_nid = 0;
3072                 /* input source automatically selected */
3073                 spec->input_mux = NULL;
3074                 spec->port_d_mode = 0;
3075                 spec->mic_boost = 3; /* default 30dB gain */
3076                 break;
3077
3078         case CXT5066_OLPC_XO_1_5:
3079                 codec->patch_ops.init = cxt5066_olpc_init;
3080                 codec->patch_ops.unsol_event = cxt5066_olpc_unsol_event;
3081                 spec->init_verbs[0] = cxt5066_init_verbs_olpc;
3082                 spec->mixers[spec->num_mixers++] = cxt5066_mixer_master_olpc;
3083                 spec->mixers[spec->num_mixers++] = cxt5066_mixer_olpc_dc;
3084                 spec->mixers[spec->num_mixers++] = cxt5066_mixers;
3085                 spec->port_d_mode = 0;
3086                 spec->mic_boost = 3; /* default 30dB gain */
3087
3088                 /* no S/PDIF out */
3089                 spec->multiout.dig_out_nid = 0;
3090
3091                 /* input source automatically selected */
3092                 spec->input_mux = NULL;
3093
3094                 /* our capture hooks which allow us to turn on the microphone LED
3095                  * at the right time */
3096                 spec->capture_prepare = cxt5066_olpc_capture_prepare;
3097                 spec->capture_cleanup = cxt5066_olpc_capture_cleanup;
3098                 break;
3099         case CXT5066_DELL_VOSTRO:
3100                 codec->patch_ops.init = cxt5066_init;
3101                 codec->patch_ops.unsol_event = cxt5066_unsol_event;
3102                 spec->init_verbs[0] = cxt5066_init_verbs_vostro;
3103                 spec->mixers[spec->num_mixers++] = cxt5066_mixer_master_olpc;
3104                 spec->mixers[spec->num_mixers++] = cxt5066_mixers;
3105                 spec->mixers[spec->num_mixers++] = cxt5066_vostro_mixers;
3106                 spec->port_d_mode = 0;
3107                 spec->dell_vostro = 1;
3108                 spec->mic_boost = 3; /* default 30dB gain */
3109
3110                 /* no S/PDIF out */
3111                 spec->multiout.dig_out_nid = 0;
3112
3113                 /* input source automatically selected */
3114                 spec->input_mux = NULL;
3115                 break;
3116         case CXT5066_IDEAPAD:
3117                 codec->patch_ops.init = cxt5066_init;
3118                 codec->patch_ops.unsol_event = cxt5066_unsol_event;
3119                 spec->mixers[spec->num_mixers++] = cxt5066_mixer_master;
3120                 spec->mixers[spec->num_mixers++] = cxt5066_mixers;
3121                 spec->init_verbs[0] = cxt5066_init_verbs_ideapad;
3122                 spec->port_d_mode = 0;
3123                 spec->ideapad = 1;
3124                 spec->mic_boost = 2;    /* default 20dB gain */
3125
3126                 /* no S/PDIF out */
3127                 spec->multiout.dig_out_nid = 0;
3128
3129                 /* input source automatically selected */
3130                 spec->input_mux = NULL;
3131                 break;
3132         case CXT5066_THINKPAD:
3133                 codec->patch_ops.init = cxt5066_init;
3134                 codec->patch_ops.unsol_event = cxt5066_unsol_event;
3135                 spec->mixers[spec->num_mixers++] = cxt5066_mixer_master;
3136                 spec->mixers[spec->num_mixers++] = cxt5066_mixers;
3137                 spec->init_verbs[0] = cxt5066_init_verbs_thinkpad;
3138                 spec->thinkpad = 1;
3139                 spec->port_d_mode = PIN_OUT;
3140                 spec->mic_boost = 2;    /* default 20dB gain */
3141
3142                 /* no S/PDIF out */
3143                 spec->multiout.dig_out_nid = 0;
3144
3145                 /* input source automatically selected */
3146                 spec->input_mux = NULL;
3147                 break;
3148         }
3149
3150         if (spec->beep_amp)
3151                 snd_hda_attach_beep_device(codec, spec->beep_amp);
3152
3153         return 0;
3154 }
3155
3156 /*
3157  * Automatic parser for CX20641 & co
3158  */
3159
3160 static int cx_auto_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
3161                                        struct hda_codec *codec,
3162                                        unsigned int stream_tag,
3163                                        unsigned int format,
3164                                        struct snd_pcm_substream *substream)
3165 {
3166         struct conexant_spec *spec = codec->spec;
3167         hda_nid_t adc = spec->imux_info[spec->cur_mux[0]].adc;
3168         if (spec->adc_switching) {
3169                 spec->cur_adc = adc;
3170                 spec->cur_adc_stream_tag = stream_tag;
3171                 spec->cur_adc_format = format;
3172         }
3173         snd_hda_codec_setup_stream(codec, adc, stream_tag, 0, format);
3174         return 0;
3175 }
3176
3177 static int cx_auto_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
3178                                        struct hda_codec *codec,
3179                                        struct snd_pcm_substream *substream)
3180 {
3181         struct conexant_spec *spec = codec->spec;
3182         snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
3183         spec->cur_adc = 0;
3184         return 0;
3185 }
3186
3187 static const struct hda_pcm_stream cx_auto_pcm_analog_capture = {
3188         .substreams = 1,
3189         .channels_min = 2,
3190         .channels_max = 2,
3191         .nid = 0, /* fill later */
3192         .ops = {
3193                 .prepare = cx_auto_capture_pcm_prepare,
3194                 .cleanup = cx_auto_capture_pcm_cleanup
3195         },
3196 };
3197
3198 static const hda_nid_t cx_auto_adc_nids[] = { 0x14 };
3199
3200 #define get_connection_index(codec, mux, nid)\
3201         snd_hda_get_conn_index(codec, mux, nid, 0)
3202
3203 /* get an unassigned DAC from the given list.
3204  * Return the nid if found and reduce the DAC list, or return zero if
3205  * not found
3206  */
3207 static hda_nid_t get_unassigned_dac(struct hda_codec *codec, hda_nid_t pin,
3208                                     hda_nid_t *dacs, int *num_dacs)
3209 {
3210         int i, nums = *num_dacs;
3211         hda_nid_t ret = 0;
3212
3213         for (i = 0; i < nums; i++) {
3214                 if (get_connection_index(codec, pin, dacs[i]) >= 0) {
3215                         ret = dacs[i];
3216                         break;
3217                 }
3218         }
3219         if (!ret)
3220                 return 0;
3221         if (--nums > 0)
3222                 memmove(dacs, dacs + 1, nums * sizeof(hda_nid_t));
3223         *num_dacs = nums;
3224         return ret;
3225 }
3226
3227 #define MAX_AUTO_DACS   5
3228
3229 #define DAC_SLAVE_FLAG  0x8000  /* filled dac is a slave */
3230
3231 /* fill analog DAC list from the widget tree */
3232 static int fill_cx_auto_dacs(struct hda_codec *codec, hda_nid_t *dacs)
3233 {
3234         hda_nid_t nid, end_nid;
3235         int nums = 0;
3236
3237         end_nid = codec->start_nid + codec->num_nodes;
3238         for (nid = codec->start_nid; nid < end_nid; nid++) {
3239                 unsigned int wcaps = get_wcaps(codec, nid);
3240                 unsigned int type = get_wcaps_type(wcaps);
3241                 if (type == AC_WID_AUD_OUT && !(wcaps & AC_WCAP_DIGITAL)) {
3242                         dacs[nums++] = nid;
3243                         if (nums >= MAX_AUTO_DACS)
3244                                 break;
3245                 }
3246         }
3247         return nums;
3248 }
3249
3250 /* fill pin_dac_pair list from the pin and dac list */
3251 static int fill_dacs_for_pins(struct hda_codec *codec, hda_nid_t *pins,
3252                               int num_pins, hda_nid_t *dacs, int *rest,
3253                               struct pin_dac_pair *filled, int nums, 
3254                               int type)
3255 {
3256         int i, start = nums;
3257
3258         for (i = 0; i < num_pins; i++, nums++) {
3259                 filled[nums].pin = pins[i];
3260                 filled[nums].type = type;
3261                 filled[nums].dac = get_unassigned_dac(codec, pins[i], dacs, rest);
3262                 if (filled[nums].dac) 
3263                         continue;
3264                 if (filled[start].dac && get_connection_index(codec, pins[i], filled[start].dac) >= 0) {
3265                         filled[nums].dac = filled[start].dac | DAC_SLAVE_FLAG;
3266                         continue;
3267                 }
3268                 if (filled[0].dac && get_connection_index(codec, pins[i], filled[0].dac) >= 0) {
3269                         filled[nums].dac = filled[0].dac | DAC_SLAVE_FLAG;
3270                         continue;
3271                 }
3272                 snd_printdd("Failed to find a DAC for pin 0x%x", pins[i]);
3273         }
3274         return nums;
3275 }
3276
3277 /* parse analog output paths */
3278 static void cx_auto_parse_output(struct hda_codec *codec)
3279 {
3280         struct conexant_spec *spec = codec->spec;
3281         struct auto_pin_cfg *cfg = &spec->autocfg;
3282         hda_nid_t dacs[MAX_AUTO_DACS];
3283         int i, j, nums, rest;
3284
3285         rest = fill_cx_auto_dacs(codec, dacs);
3286         /* parse all analog output pins */
3287         nums = fill_dacs_for_pins(codec, cfg->line_out_pins, cfg->line_outs,
3288                           dacs, &rest, spec->dac_info, 0,
3289                           AUTO_PIN_LINE_OUT);
3290         nums = fill_dacs_for_pins(codec, cfg->hp_pins, cfg->hp_outs,
3291                           dacs, &rest, spec->dac_info, nums,
3292                           AUTO_PIN_HP_OUT);
3293         nums = fill_dacs_for_pins(codec, cfg->speaker_pins, cfg->speaker_outs,
3294                           dacs, &rest, spec->dac_info, nums,
3295                           AUTO_PIN_SPEAKER_OUT);
3296         spec->dac_info_filled = nums;
3297         /* fill multiout struct */
3298         for (i = 0; i < nums; i++) {
3299                 hda_nid_t dac = spec->dac_info[i].dac;
3300                 if (!dac || (dac & DAC_SLAVE_FLAG))
3301                         continue;
3302                 switch (spec->dac_info[i].type) {
3303                 case AUTO_PIN_LINE_OUT:
3304                         spec->private_dac_nids[spec->multiout.num_dacs] = dac;
3305                         spec->multiout.num_dacs++;
3306                         break;
3307                 case AUTO_PIN_HP_OUT:
3308                 case AUTO_PIN_SPEAKER_OUT:
3309                         if (!spec->multiout.hp_nid) {
3310                                 spec->multiout.hp_nid = dac;
3311                                 break;
3312                         }
3313                         for (j = 0; j < ARRAY_SIZE(spec->multiout.extra_out_nid); j++)
3314                                 if (!spec->multiout.extra_out_nid[j]) {
3315                                         spec->multiout.extra_out_nid[j] = dac;
3316                                         break;
3317                                 }
3318                         break;
3319                 }
3320         }
3321         spec->multiout.dac_nids = spec->private_dac_nids;
3322         spec->multiout.max_channels = spec->multiout.num_dacs * 2;
3323
3324         for (i = 0; i < cfg->hp_outs; i++) {
3325                 if (is_jack_detectable(codec, cfg->hp_pins[i])) {
3326                         spec->auto_mute = 1;
3327                         break;
3328                 }
3329         }
3330         if (spec->auto_mute &&
3331             cfg->line_out_pins[0] &&
3332             cfg->line_out_type != AUTO_PIN_SPEAKER_OUT &&
3333             cfg->line_out_pins[0] != cfg->hp_pins[0] &&
3334             cfg->line_out_pins[0] != cfg->speaker_pins[0]) {
3335                 for (i = 0; i < cfg->line_outs; i++) {
3336                         if (is_jack_detectable(codec, cfg->line_out_pins[i])) {
3337                                 spec->detect_line = 1;
3338                                 break;
3339                         }
3340                 }
3341                 spec->automute_lines = spec->detect_line;
3342         }
3343
3344         spec->vmaster_nid = spec->private_dac_nids[0];
3345 }
3346
3347 static void cx_auto_turn_eapd(struct hda_codec *codec, int num_pins,
3348                               hda_nid_t *pins, bool on);
3349
3350 static void do_automute(struct hda_codec *codec, int num_pins,
3351                         hda_nid_t *pins, bool on)
3352 {
3353         struct conexant_spec *spec = codec->spec;
3354         int i;
3355         for (i = 0; i < num_pins; i++)
3356                 snd_hda_set_pin_ctl(codec, pins[i], on ? PIN_OUT : 0);
3357         if (spec->pin_eapd_ctrls)
3358                 cx_auto_turn_eapd(codec, num_pins, pins, on);
3359 }
3360
3361 static int detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
3362 {
3363         int i, present = 0;
3364
3365         for (i = 0; i < num_pins; i++) {
3366                 hda_nid_t nid = pins[i];
3367                 if (!nid || !is_jack_detectable(codec, nid))
3368                         break;
3369                 present |= snd_hda_jack_detect(codec, nid);
3370         }
3371         return present;
3372 }
3373
3374 /* auto-mute/unmute speaker and line outs according to headphone jack */
3375 static void cx_auto_update_speakers(struct hda_codec *codec)
3376 {
3377         struct conexant_spec *spec = codec->spec;
3378         struct auto_pin_cfg *cfg = &spec->autocfg;
3379         int on = 1;
3380
3381         /* turn on HP EAPD when HP jacks are present */
3382         if (spec->pin_eapd_ctrls) {
3383                 if (spec->auto_mute)
3384                         on = spec->hp_present;
3385                 cx_auto_turn_eapd(codec, cfg->hp_outs, cfg->hp_pins, on);
3386         }
3387
3388         /* mute speakers in auto-mode if HP or LO jacks are plugged */
3389         if (spec->auto_mute)
3390                 on = !(spec->hp_present ||
3391                        (spec->detect_line && spec->line_present));
3392         do_automute(codec, cfg->speaker_outs, cfg->speaker_pins, on);
3393
3394         /* toggle line-out mutes if needed, too */
3395         /* if LO is a copy of either HP or Speaker, don't need to handle it */
3396         if (cfg->line_out_pins[0] == cfg->hp_pins[0] ||
3397             cfg->line_out_pins[0] == cfg->speaker_pins[0])
3398                 return;
3399         if (spec->auto_mute) {
3400                 /* mute LO in auto-mode when HP jack is present */
3401                 if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT ||
3402                     spec->automute_lines)
3403                         on = !spec->hp_present;
3404                 else
3405                         on = 1;
3406         }
3407         do_automute(codec, cfg->line_outs, cfg->line_out_pins, on);
3408 }
3409
3410 static void cx_auto_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
3411 {
3412         struct conexant_spec *spec = codec->spec;
3413         struct auto_pin_cfg *cfg = &spec->autocfg;
3414
3415         if (!spec->auto_mute)
3416                 return;
3417         spec->hp_present = detect_jacks(codec, cfg->hp_outs, cfg->hp_pins);
3418         cx_auto_update_speakers(codec);
3419 }
3420
3421 static void cx_auto_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
3422 {
3423         struct conexant_spec *spec = codec->spec;
3424         struct auto_pin_cfg *cfg = &spec->autocfg;
3425
3426         if (!spec->auto_mute || !spec->detect_line)
3427                 return;
3428         spec->line_present = detect_jacks(codec, cfg->line_outs,
3429                                           cfg->line_out_pins);
3430         cx_auto_update_speakers(codec);
3431 }
3432
3433 static int cx_automute_mode_info(struct snd_kcontrol *kcontrol,
3434                                  struct snd_ctl_elem_info *uinfo)
3435 {
3436         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3437         struct conexant_spec *spec = codec->spec;
3438         static const char * const texts3[] = {
3439                 "Disabled", "Speaker Only", "Line Out+Speaker"
3440         };
3441
3442         if (spec->automute_hp_lo)
3443                 return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
3444         return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
3445 }
3446
3447 static int cx_automute_mode_get(struct snd_kcontrol *kcontrol,
3448                                 struct snd_ctl_elem_value *ucontrol)
3449 {
3450         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3451         struct conexant_spec *spec = codec->spec;
3452         unsigned int val;
3453         if (!spec->auto_mute)
3454                 val = 0;
3455         else if (!spec->automute_lines)
3456                 val = 1;
3457         else
3458                 val = 2;
3459         ucontrol->value.enumerated.item[0] = val;
3460         return 0;
3461 }
3462
3463 static int cx_automute_mode_put(struct snd_kcontrol *kcontrol,
3464                                 struct snd_ctl_elem_value *ucontrol)
3465 {
3466         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3467         struct conexant_spec *spec = codec->spec;
3468
3469         switch (ucontrol->value.enumerated.item[0]) {
3470         case 0:
3471                 if (!spec->auto_mute)
3472                         return 0;
3473                 spec->auto_mute = 0;
3474                 break;
3475         case 1:
3476                 if (spec->auto_mute && !spec->automute_lines)
3477                         return 0;
3478                 spec->auto_mute = 1;
3479                 spec->automute_lines = 0;
3480                 break;
3481         case 2:
3482                 if (!spec->automute_hp_lo)
3483                         return -EINVAL;
3484                 if (spec->auto_mute && spec->automute_lines)
3485                         return 0;
3486                 spec->auto_mute = 1;
3487                 spec->automute_lines = 1;
3488                 break;
3489         default:
3490                 return -EINVAL;
3491         }
3492         cx_auto_update_speakers(codec);
3493         return 1;
3494 }
3495
3496 static const struct snd_kcontrol_new cx_automute_mode_enum[] = {
3497         {
3498                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3499                 .name = "Auto-Mute Mode",
3500                 .info = cx_automute_mode_info,
3501                 .get = cx_automute_mode_get,
3502                 .put = cx_automute_mode_put,
3503         },
3504         { }
3505 };
3506
3507 static int cx_auto_mux_enum_info(struct snd_kcontrol *kcontrol,
3508                                  struct snd_ctl_elem_info *uinfo)
3509 {
3510         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3511         struct conexant_spec *spec = codec->spec;
3512
3513         return snd_hda_input_mux_info(&spec->private_imux, uinfo);
3514 }
3515
3516 static int cx_auto_mux_enum_get(struct snd_kcontrol *kcontrol,
3517                                 struct snd_ctl_elem_value *ucontrol)
3518 {
3519         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3520         struct conexant_spec *spec = codec->spec;
3521
3522         ucontrol->value.enumerated.item[0] = spec->cur_mux[0];
3523         return 0;
3524 }
3525
3526 /* look for the route the given pin from mux and return the index;
3527  * if do_select is set, actually select the route.
3528  */
3529 static int __select_input_connection(struct hda_codec *codec, hda_nid_t mux,
3530                                      hda_nid_t pin, hda_nid_t *srcp,
3531                                      bool do_select, int depth)
3532 {
3533         struct conexant_spec *spec = codec->spec;
3534         hda_nid_t conn[HDA_MAX_NUM_INPUTS];
3535         int startidx, i, nums;
3536
3537         switch (get_wcaps_type(get_wcaps(codec, mux))) {
3538         case AC_WID_AUD_IN:
3539         case AC_WID_AUD_SEL:
3540         case AC_WID_AUD_MIX:
3541                 break;
3542         default:
3543                 return -1;
3544         }
3545
3546         nums = snd_hda_get_connections(codec, mux, conn, ARRAY_SIZE(conn));
3547         for (i = 0; i < nums; i++)
3548                 if (conn[i] == pin) {
3549                         if (do_select)
3550                                 snd_hda_codec_write(codec, mux, 0,
3551                                                     AC_VERB_SET_CONNECT_SEL, i);
3552                         if (srcp)
3553                                 *srcp = mux;
3554                         return i;
3555                 }
3556         depth++;
3557         if (depth == 2)
3558                 return -1;
3559
3560         /* Try to rotate around connections to avoid one boost controlling
3561            another input path as well */
3562         startidx = 0;
3563         for (i = 0; i < spec->private_imux.num_items; i++)
3564                 if (spec->imux_info[i].pin == pin) {
3565                         startidx = i;
3566                         break;
3567                 }
3568
3569         for (i = 0; i < nums; i++) {
3570                 int j = (i + startidx) % nums;
3571                 int ret  = __select_input_connection(codec, conn[j], pin, srcp,
3572                                                      do_select, depth);
3573                 if (ret >= 0) {
3574                         if (do_select)
3575                                 snd_hda_codec_write(codec, mux, 0,
3576                                                     AC_VERB_SET_CONNECT_SEL, j);
3577                         return j;
3578                 }
3579         }
3580         return -1;
3581 }
3582
3583 static void select_input_connection(struct hda_codec *codec, hda_nid_t mux,
3584                                    hda_nid_t pin)
3585 {
3586         __select_input_connection(codec, mux, pin, NULL, true, 0);
3587 }
3588
3589 static int get_input_connection(struct hda_codec *codec, hda_nid_t mux,
3590                                 hda_nid_t pin)
3591 {
3592         return __select_input_connection(codec, mux, pin, NULL, false, 0);
3593 }
3594
3595 static int cx_auto_mux_enum_update(struct hda_codec *codec,
3596                                    const struct hda_input_mux *imux,
3597                                    unsigned int idx)
3598 {
3599         struct conexant_spec *spec = codec->spec;
3600         hda_nid_t adc;
3601         int changed = 1;
3602
3603         if (!imux->num_items)
3604                 return 0;
3605         if (idx >= imux->num_items)
3606                 idx = imux->num_items - 1;
3607         if (spec->cur_mux[0] == idx)
3608                 changed = 0;
3609         adc = spec->imux_info[idx].adc;
3610         select_input_connection(codec, spec->imux_info[idx].adc,
3611                                 spec->imux_info[idx].pin);
3612         if (spec->cur_adc && spec->cur_adc != adc) {
3613                 /* stream is running, let's swap the current ADC */
3614                 __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
3615                 spec->cur_adc = adc;
3616                 snd_hda_codec_setup_stream(codec, adc,
3617                                            spec->cur_adc_stream_tag, 0,
3618                                            spec->cur_adc_format);
3619         }
3620         spec->cur_mux[0] = idx;
3621         return changed;
3622 }
3623
3624 static int cx_auto_mux_enum_put(struct snd_kcontrol *kcontrol,
3625                                 struct snd_ctl_elem_value *ucontrol)
3626 {
3627         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3628         struct conexant_spec *spec = codec->spec;
3629
3630         return cx_auto_mux_enum_update(codec, &spec->private_imux,
3631                                        ucontrol->value.enumerated.item[0]);
3632 }
3633
3634 static const struct snd_kcontrol_new cx_auto_capture_mixers[] = {
3635         {
3636                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3637                 .name = "Capture Source",
3638                 .info = cx_auto_mux_enum_info,
3639                 .get = cx_auto_mux_enum_get,
3640                 .put = cx_auto_mux_enum_put
3641         },
3642         {}
3643 };
3644
3645 static bool select_automic(struct hda_codec *codec, int idx, bool detect)
3646 {
3647         struct conexant_spec *spec = codec->spec;
3648         if (idx < 0)
3649                 return false;
3650         if (detect && !snd_hda_jack_detect(codec, spec->imux_info[idx].pin))
3651                 return false;
3652         cx_auto_mux_enum_update(codec, &spec->private_imux, idx);
3653         return true;
3654 }
3655
3656 /* automatic switch internal and external mic */
3657 static void cx_auto_automic(struct hda_codec *codec, struct hda_jack_tbl *jack)
3658 {
3659         struct conexant_spec *spec = codec->spec;
3660
3661         if (!spec->auto_mic)
3662                 return;
3663         if (!select_automic(codec, spec->auto_mic_ext, true))
3664                 if (!select_automic(codec, spec->auto_mic_dock, true))
3665                         select_automic(codec, spec->auto_mic_int, false);
3666 }
3667
3668 /* check whether the pin config is suitable for auto-mic switching;
3669  * auto-mic is enabled only when one int-mic and one ext- and/or
3670  * one dock-mic exist
3671  */
3672 static void cx_auto_check_auto_mic(struct hda_codec *codec)
3673 {
3674         struct conexant_spec *spec = codec->spec;
3675         int pset[INPUT_PIN_ATTR_NORMAL + 1];
3676         int i;
3677
3678         for (i = 0; i < ARRAY_SIZE(pset); i++)
3679                 pset[i] = -1;
3680         for (i = 0; i < spec->private_imux.num_items; i++) {
3681                 hda_nid_t pin = spec->imux_info[i].pin;
3682                 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, pin);
3683                 int type, attr;
3684                 attr = snd_hda_get_input_pin_attr(def_conf);
3685                 if (attr == INPUT_PIN_ATTR_UNUSED)
3686                         return; /* invalid entry */
3687                 if (attr > INPUT_PIN_ATTR_NORMAL)
3688                         attr = INPUT_PIN_ATTR_NORMAL;
3689                 if (attr != INPUT_PIN_ATTR_INT &&
3690                     !is_jack_detectable(codec, pin))
3691                         return; /* non-detectable pin */
3692                 type = get_defcfg_device(def_conf);
3693                 if (type != AC_JACK_MIC_IN &&
3694                     (attr != INPUT_PIN_ATTR_DOCK || type != AC_JACK_LINE_IN))
3695                         return; /* no valid input type */
3696                 if (pset[attr] >= 0)
3697                         return; /* already occupied */
3698                 pset[attr] = i;
3699         }
3700         if (pset[INPUT_PIN_ATTR_INT] < 0 ||
3701             (pset[INPUT_PIN_ATTR_NORMAL] < 0 && pset[INPUT_PIN_ATTR_DOCK]))
3702                 return; /* no input to switch*/
3703         spec->auto_mic = 1;
3704         spec->auto_mic_ext = pset[INPUT_PIN_ATTR_NORMAL];
3705         spec->auto_mic_dock = pset[INPUT_PIN_ATTR_DOCK];
3706         spec->auto_mic_int = pset[INPUT_PIN_ATTR_INT];
3707 }
3708
3709 static void cx_auto_parse_input(struct hda_codec *codec)
3710 {
3711         struct conexant_spec *spec = codec->spec;
3712         struct auto_pin_cfg *cfg = &spec->autocfg;
3713         struct hda_input_mux *imux;
3714         int i, j;
3715
3716         imux = &spec->private_imux;
3717         for (i = 0; i < cfg->num_inputs; i++) {
3718                 for (j = 0; j < spec->num_adc_nids; j++) {
3719                         hda_nid_t adc = spec->adc_nids[j];
3720                         int idx = get_input_connection(codec, adc,
3721                                                        cfg->inputs[i].pin);
3722                         if (idx >= 0) {
3723                                 const char *label;
3724                                 label = hda_get_autocfg_input_label(codec, cfg, i);
3725                                 spec->imux_info[imux->num_items].index = i;
3726                                 spec->imux_info[imux->num_items].boost = 0;
3727                                 spec->imux_info[imux->num_items].adc = adc;
3728                                 spec->imux_info[imux->num_items].pin =
3729                                         cfg->inputs[i].pin;
3730                                 snd_hda_add_imux_item(imux, label, idx, NULL);
3731                                 break;
3732                         }
3733                 }
3734         }
3735         if (imux->num_items >= 2 && cfg->num_inputs == imux->num_items)
3736                 cx_auto_check_auto_mic(codec);
3737         if (imux->num_items > 1) {
3738                 for (i = 1; i < imux->num_items; i++) {
3739                         if (spec->imux_info[i].adc != spec->imux_info[0].adc) {
3740                                 spec->adc_switching = 1;
3741                                 break;
3742                         }
3743                 }
3744         }
3745 }
3746
3747 /* get digital-input audio widget corresponding to the given pin */
3748 static hda_nid_t cx_auto_get_dig_in(struct hda_codec *codec, hda_nid_t pin)
3749 {
3750         hda_nid_t nid, end_nid;
3751
3752         end_nid = codec->start_nid + codec->num_nodes;
3753         for (nid = codec->start_nid; nid < end_nid; nid++) {
3754                 unsigned int wcaps = get_wcaps(codec, nid);
3755                 unsigned int type = get_wcaps_type(wcaps);
3756                 if (type == AC_WID_AUD_IN && (wcaps & AC_WCAP_DIGITAL)) {
3757                         if (get_connection_index(codec, nid, pin) >= 0)
3758                                 return nid;
3759                 }
3760         }
3761         return 0;
3762 }
3763
3764 static void cx_auto_parse_digital(struct hda_codec *codec)
3765 {
3766         struct conexant_spec *spec = codec->spec;
3767         struct auto_pin_cfg *cfg = &spec->autocfg;
3768         hda_nid_t nid;
3769
3770         if (cfg->dig_outs &&
3771             snd_hda_get_connections(codec, cfg->dig_out_pins[0], &nid, 1) == 1)
3772                 spec->multiout.dig_out_nid = nid;
3773         if (cfg->dig_in_pin)
3774                 spec->dig_in_nid = cx_auto_get_dig_in(codec, cfg->dig_in_pin);
3775 }
3776
3777 #ifdef CONFIG_SND_HDA_INPUT_BEEP
3778 static void cx_auto_parse_beep(struct hda_codec *codec)
3779 {
3780         struct conexant_spec *spec = codec->spec;
3781         hda_nid_t nid, end_nid;
3782
3783         end_nid = codec->start_nid + codec->num_nodes;
3784         for (nid = codec->start_nid; nid < end_nid; nid++)
3785                 if (get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_BEEP) {
3786                         set_beep_amp(spec, nid, 0, HDA_OUTPUT);
3787                         break;
3788                 }
3789 }
3790 #else
3791 #define cx_auto_parse_beep(codec)
3792 #endif
3793
3794 /* parse EAPDs */
3795 static void cx_auto_parse_eapd(struct hda_codec *codec)
3796 {
3797         struct conexant_spec *spec = codec->spec;
3798         hda_nid_t nid, end_nid;
3799
3800         end_nid = codec->start_nid + codec->num_nodes;
3801         for (nid = codec->start_nid; nid < end_nid; nid++) {
3802                 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
3803                         continue;
3804                 if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD))
3805                         continue;
3806                 spec->eapds[spec->num_eapds++] = nid;
3807                 if (spec->num_eapds >= ARRAY_SIZE(spec->eapds))
3808                         break;
3809         }
3810
3811         /* NOTE: below is a wild guess; if we have more than two EAPDs,
3812          * it's a new chip, where EAPDs are supposed to be associated to
3813          * pins, and we can control EAPD per pin.
3814          * OTOH, if only one or two EAPDs are found, it's an old chip,
3815          * thus it might control over all pins.
3816          */
3817         spec->pin_eapd_ctrls = spec->num_eapds > 2;
3818 }
3819
3820 static int cx_auto_parse_auto_config(struct hda_codec *codec)
3821 {
3822         struct conexant_spec *spec = codec->spec;
3823         int err;
3824
3825         err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
3826         if (err < 0)
3827                 return err;
3828
3829         cx_auto_parse_output(codec);
3830         cx_auto_parse_input(codec);
3831         cx_auto_parse_digital(codec);
3832         cx_auto_parse_beep(codec);
3833         cx_auto_parse_eapd(codec);
3834         return 0;
3835 }
3836
3837 static void cx_auto_turn_eapd(struct hda_codec *codec, int num_pins,
3838                               hda_nid_t *pins, bool on)
3839 {
3840         int i;
3841         for (i = 0; i < num_pins; i++) {
3842                 if (snd_hda_query_pin_caps(codec, pins[i]) & AC_PINCAP_EAPD)
3843                         snd_hda_codec_write(codec, pins[i], 0,
3844                                             AC_VERB_SET_EAPD_BTLENABLE,
3845                                             on ? 0x02 : 0);
3846         }
3847 }
3848
3849 static void select_connection(struct hda_codec *codec, hda_nid_t pin,
3850                               hda_nid_t src)
3851 {
3852         int idx = get_connection_index(codec, pin, src);
3853         if (idx >= 0)
3854                 snd_hda_codec_write(codec, pin, 0,
3855                                     AC_VERB_SET_CONNECT_SEL, idx);
3856 }
3857
3858 static void mute_outputs(struct hda_codec *codec, int num_nids,
3859                          const hda_nid_t *nids)
3860 {
3861         int i, val;
3862
3863         for (i = 0; i < num_nids; i++) {
3864                 hda_nid_t nid = nids[i];
3865                 if (!(get_wcaps(codec, nid) & AC_WCAP_OUT_AMP))
3866                         continue;
3867                 if (query_amp_caps(codec, nid, HDA_OUTPUT) & AC_AMPCAP_MUTE)
3868                         val = AMP_OUT_MUTE;
3869                 else
3870                         val = AMP_OUT_ZERO;
3871                 snd_hda_codec_write(codec, nid, 0,
3872                                     AC_VERB_SET_AMP_GAIN_MUTE, val);
3873         }
3874 }
3875
3876 static void enable_unsol_pins(struct hda_codec *codec, int num_pins,
3877                               hda_nid_t *pins, unsigned int action,
3878                               hda_jack_callback cb)
3879 {
3880         int i;
3881         for (i = 0; i < num_pins; i++)
3882                 snd_hda_jack_detect_enable_callback(codec, pins[i], action, cb);
3883 }
3884
3885 static bool found_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
3886 {
3887         int i;
3888         for (i = 0; i < nums; i++)
3889                 if (list[i] == nid)
3890                         return true;
3891         return false;
3892 }
3893
3894 /* is the given NID found in any of autocfg items? */
3895 static bool found_in_autocfg(struct auto_pin_cfg *cfg, hda_nid_t nid)
3896 {
3897         int i;
3898
3899         if (found_in_nid_list(nid, cfg->line_out_pins, cfg->line_outs) ||
3900             found_in_nid_list(nid, cfg->hp_pins, cfg->hp_outs) ||
3901             found_in_nid_list(nid, cfg->speaker_pins, cfg->speaker_outs) ||
3902             found_in_nid_list(nid, cfg->dig_out_pins, cfg->dig_outs))
3903                 return true;
3904         for (i = 0; i < cfg->num_inputs; i++)
3905                 if (cfg->inputs[i].pin == nid)
3906                         return true;
3907         if (cfg->dig_in_pin == nid)
3908                 return true;
3909         return false;
3910 }
3911
3912 /* clear unsol-event tags on unused pins; Conexant codecs seem to leave
3913  * invalid unsol tags by some reason
3914  */
3915 static void clear_unsol_on_unused_pins(struct hda_codec *codec)
3916 {
3917         struct conexant_spec *spec = codec->spec;
3918         struct auto_pin_cfg *cfg = &spec->autocfg;
3919         int i;
3920
3921         for (i = 0; i < codec->init_pins.used; i++) {
3922                 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
3923                 if (!found_in_autocfg(cfg, pin->nid))
3924                         snd_hda_codec_write(codec, pin->nid, 0,
3925                                             AC_VERB_SET_UNSOLICITED_ENABLE, 0);
3926         }
3927 }
3928
3929 /* turn on/off EAPD according to Master switch */
3930 static void cx_auto_vmaster_hook(void *private_data, int enabled)
3931 {
3932         struct hda_codec *codec = private_data;
3933         struct conexant_spec *spec = codec->spec;
3934
3935         if (enabled && spec->pin_eapd_ctrls) {
3936                 cx_auto_update_speakers(codec);
3937                 return;
3938         }
3939         cx_auto_turn_eapd(codec, spec->num_eapds, spec->eapds, enabled);
3940 }
3941
3942 static void cx_auto_init_output(struct hda_codec *codec)
3943 {
3944         struct conexant_spec *spec = codec->spec;
3945         struct auto_pin_cfg *cfg = &spec->autocfg;
3946         hda_nid_t nid;
3947         int i;
3948
3949         mute_outputs(codec, spec->multiout.num_dacs, spec->multiout.dac_nids);
3950         for (i = 0; i < cfg->hp_outs; i++) {
3951                 unsigned int val = PIN_OUT;
3952                 if (snd_hda_query_pin_caps(codec, cfg->hp_pins[i]) &
3953                     AC_PINCAP_HP_DRV)
3954                         val |= AC_PINCTL_HP_EN;
3955                 snd_hda_set_pin_ctl(codec, cfg->hp_pins[i], val);
3956         }
3957         mute_outputs(codec, cfg->hp_outs, cfg->hp_pins);
3958         mute_outputs(codec, cfg->line_outs, cfg->line_out_pins);
3959         mute_outputs(codec, cfg->speaker_outs, cfg->speaker_pins);
3960         for (i = 0; i < spec->dac_info_filled; i++) {
3961                 nid = spec->dac_info[i].dac;
3962                 if (!nid)
3963                         nid = spec->multiout.dac_nids[0];
3964                 else if (nid & DAC_SLAVE_FLAG)
3965                         nid &= ~DAC_SLAVE_FLAG;
3966                 select_connection(codec, spec->dac_info[i].pin, nid);
3967         }
3968         if (spec->auto_mute) {
3969                 enable_unsol_pins(codec, cfg->hp_outs, cfg->hp_pins,
3970                                   CONEXANT_HP_EVENT, cx_auto_hp_automute);
3971                 spec->hp_present = detect_jacks(codec, cfg->hp_outs,
3972                                                 cfg->hp_pins);
3973                 if (spec->detect_line) {
3974                         enable_unsol_pins(codec, cfg->line_outs,
3975                                           cfg->line_out_pins,
3976                                           CONEXANT_LINE_EVENT,
3977                                           cx_auto_line_automute);
3978                         spec->line_present =
3979                                 detect_jacks(codec, cfg->line_outs,
3980                                              cfg->line_out_pins);
3981                 }
3982         }
3983         cx_auto_update_speakers(codec);
3984         /* turn on all EAPDs if no individual EAPD control is available */
3985         if (!spec->pin_eapd_ctrls)
3986                 cx_auto_turn_eapd(codec, spec->num_eapds, spec->eapds, true);
3987         clear_unsol_on_unused_pins(codec);
3988 }
3989
3990 static void cx_auto_init_input(struct hda_codec *codec)
3991 {
3992         struct conexant_spec *spec = codec->spec;
3993         struct auto_pin_cfg *cfg = &spec->autocfg;
3994         int i, val;
3995
3996         for (i = 0; i < spec->num_adc_nids; i++) {
3997                 hda_nid_t nid = spec->adc_nids[i];
3998                 if (!(get_wcaps(codec, nid) & AC_WCAP_IN_AMP))
3999                         continue;
4000                 if (query_amp_caps(codec, nid, HDA_INPUT) & AC_AMPCAP_MUTE)
4001                         val = AMP_IN_MUTE(0);
4002                 else
4003                         val = AMP_IN_UNMUTE(0);
4004                 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
4005                                     val);
4006         }
4007
4008         for (i = 0; i < cfg->num_inputs; i++) {
4009                 hda_nid_t pin = cfg->inputs[i].pin;
4010                 unsigned int type = PIN_IN;
4011                 if (cfg->inputs[i].type == AUTO_PIN_MIC)
4012                         type |= snd_hda_get_default_vref(codec, pin);
4013                 snd_hda_set_pin_ctl(codec, pin, type);
4014         }
4015
4016         if (spec->auto_mic) {
4017                 if (spec->auto_mic_ext >= 0) {
4018                         snd_hda_jack_detect_enable_callback(codec,
4019                                 cfg->inputs[spec->auto_mic_ext].pin,
4020                                 CONEXANT_MIC_EVENT, cx_auto_automic);
4021                 }
4022                 if (spec->auto_mic_dock >= 0) {
4023                         snd_hda_jack_detect_enable_callback(codec,
4024                                 cfg->inputs[spec->auto_mic_dock].pin,
4025                                 CONEXANT_MIC_EVENT, cx_auto_automic);
4026                 }
4027                 cx_auto_automic(codec, NULL);
4028         } else {
4029                 select_input_connection(codec, spec->imux_info[0].adc,
4030                                         spec->imux_info[0].pin);
4031         }
4032 }
4033
4034 static void cx_auto_init_digital(struct hda_codec *codec)
4035 {
4036         struct conexant_spec *spec = codec->spec;
4037         struct auto_pin_cfg *cfg = &spec->autocfg;
4038
4039         if (spec->multiout.dig_out_nid)
4040                 snd_hda_set_pin_ctl(codec, cfg->dig_out_pins[0], PIN_OUT);
4041         if (spec->dig_in_nid)
4042                 snd_hda_set_pin_ctl(codec, cfg->dig_in_pin, PIN_IN);
4043 }
4044
4045 static int cx_auto_init(struct hda_codec *codec)
4046 {
4047         struct conexant_spec *spec = codec->spec;
4048         snd_hda_gen_apply_verbs(codec);
4049         cx_auto_init_output(codec);
4050         cx_auto_init_input(codec);
4051         cx_auto_init_digital(codec);
4052         snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
4053         return 0;
4054 }
4055
4056 static int cx_auto_add_volume_idx(struct hda_codec *codec, const char *basename,
4057                               const char *dir, int cidx,
4058                               hda_nid_t nid, int hda_dir, int amp_idx, int chs)
4059 {
4060         static char name[44];
4061         static struct snd_kcontrol_new knew[] = {
4062                 HDA_CODEC_VOLUME(name, 0, 0, 0),
4063                 HDA_CODEC_MUTE(name, 0, 0, 0),
4064         };
4065         static const char * const sfx[2] = { "Volume", "Switch" };
4066         int i, err;
4067
4068         for (i = 0; i < 2; i++) {
4069                 struct snd_kcontrol *kctl;
4070                 knew[i].private_value = HDA_COMPOSE_AMP_VAL(nid, chs, amp_idx,
4071                                                             hda_dir);
4072                 knew[i].subdevice = HDA_SUBDEV_AMP_FLAG;
4073                 knew[i].index = cidx;
4074                 snprintf(name, sizeof(name), "%s%s %s", basename, dir, sfx[i]);
4075                 kctl = snd_ctl_new1(&knew[i], codec);
4076                 if (!kctl)
4077                         return -ENOMEM;
4078                 err = snd_hda_ctl_add(codec, nid, kctl);
4079                 if (err < 0)
4080                         return err;
4081                 if (!(query_amp_caps(codec, nid, hda_dir) &
4082                       (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)))
4083                         break;
4084         }
4085         return 0;
4086 }
4087
4088 #define cx_auto_add_volume(codec, str, dir, cidx, nid, hda_dir)         \
4089         cx_auto_add_volume_idx(codec, str, dir, cidx, nid, hda_dir, 0, 3)
4090
4091 #define cx_auto_add_pb_volume(codec, nid, str, idx)                     \
4092         cx_auto_add_volume(codec, str, " Playback", idx, nid, HDA_OUTPUT)
4093
4094 static int try_add_pb_volume(struct hda_codec *codec, hda_nid_t dac,
4095                              hda_nid_t pin, const char *name, int idx)
4096 {
4097         unsigned int caps;
4098         if (dac && !(dac & DAC_SLAVE_FLAG)) {
4099                 caps = query_amp_caps(codec, dac, HDA_OUTPUT);
4100                 if (caps & AC_AMPCAP_NUM_STEPS)
4101                         return cx_auto_add_pb_volume(codec, dac, name, idx);
4102         }
4103         caps = query_amp_caps(codec, pin, HDA_OUTPUT);
4104         if (caps & AC_AMPCAP_NUM_STEPS)
4105                 return cx_auto_add_pb_volume(codec, pin, name, idx);
4106         return 0;
4107 }
4108
4109 static bool is_2_1_speaker(struct conexant_spec *spec)
4110 {
4111         int i, type, num_spk = 0;
4112
4113         for (i = 0; i < spec->dac_info_filled; i++) {
4114                 type = spec->dac_info[i].type;
4115                 if (type == AUTO_PIN_LINE_OUT)
4116                         type = spec->autocfg.line_out_type;
4117                 if (type == AUTO_PIN_SPEAKER_OUT)
4118                         num_spk++;
4119         }
4120         return (num_spk == 2 && spec->autocfg.line_out_type != AUTO_PIN_LINE_OUT);
4121 }
4122
4123 static int cx_auto_build_output_controls(struct hda_codec *codec)
4124 {
4125         struct conexant_spec *spec = codec->spec;
4126         int i, err;
4127         int num_line = 0, num_hp = 0, num_spk = 0;
4128         bool speaker_2_1;
4129         static const char * const texts[3] = { "Front", "Surround", "CLFE" };
4130
4131         if (spec->dac_info_filled == 1)
4132                 return try_add_pb_volume(codec, spec->dac_info[0].dac,
4133                                          spec->dac_info[0].pin,
4134                                          "Master", 0);
4135
4136         speaker_2_1 = is_2_1_speaker(spec);
4137
4138         for (i = 0; i < spec->dac_info_filled; i++) {
4139                 const char *label;
4140                 int idx, type;
4141                 hda_nid_t dac = spec->dac_info[i].dac;
4142                 type = spec->dac_info[i].type;
4143                 if (type == AUTO_PIN_LINE_OUT)
4144                         type = spec->autocfg.line_out_type;
4145                 switch (type) {
4146                 case AUTO_PIN_LINE_OUT:
4147                 default:
4148                         label = texts[num_line++];
4149                         idx = 0;
4150                         break;
4151                 case AUTO_PIN_HP_OUT:
4152                         label = "Headphone";
4153                         idx = num_hp++;
4154                         break;
4155                 case AUTO_PIN_SPEAKER_OUT:
4156                         if (speaker_2_1) {
4157                                 label = num_spk++ ? "Bass Speaker" : "Speaker";
4158                                 idx = 0;
4159                         } else {
4160                                 label = "Speaker";
4161                                 idx = num_spk++;
4162                         }
4163                         break;
4164                 }
4165                 err = try_add_pb_volume(codec, dac,
4166                                         spec->dac_info[i].pin,
4167                                         label, idx);
4168                 if (err < 0)
4169                         return err;
4170         }
4171
4172         if (spec->auto_mute) {
4173                 err = snd_hda_add_new_ctls(codec, cx_automute_mode_enum);
4174                 if (err < 0)
4175                         return err;
4176         }
4177         
4178         return 0;
4179 }
4180
4181 /* Returns zero if this is a normal stereo channel, and non-zero if it should
4182    be split in two independent channels.
4183    dest_label must be at least 44 characters. */
4184 static int cx_auto_get_rightch_label(struct hda_codec *codec, const char *label,
4185                                      char *dest_label, int nid)
4186 {
4187         struct conexant_spec *spec = codec->spec;
4188         int i;
4189
4190         if (!spec->fixup_stereo_dmic)
4191                 return 0;
4192
4193         for (i = 0; i < AUTO_CFG_MAX_INS; i++) {
4194                 int def_conf;
4195                 if (spec->autocfg.inputs[i].pin != nid)
4196                         continue;
4197
4198                 if (spec->autocfg.inputs[i].type != AUTO_PIN_MIC)
4199                         return 0;
4200                 def_conf = snd_hda_codec_get_pincfg(codec, nid);
4201                 if (snd_hda_get_input_pin_attr(def_conf) != INPUT_PIN_ATTR_INT)
4202                         return 0;
4203
4204                 /* Finally found the inverted internal mic! */
4205                 snprintf(dest_label, 44, "Inverted %s", label);
4206                 return 1;
4207         }
4208         return 0;
4209 }
4210
4211 static int cx_auto_add_capture_volume(struct hda_codec *codec, hda_nid_t nid,
4212                                       const char *label, const char *pfx,
4213                                       int cidx)
4214 {
4215         struct conexant_spec *spec = codec->spec;
4216         int i;
4217
4218         for (i = 0; i < spec->num_adc_nids; i++) {
4219                 char rightch_label[44];
4220                 hda_nid_t adc_nid = spec->adc_nids[i];
4221                 int idx = get_input_connection(codec, adc_nid, nid);
4222                 if (idx < 0)
4223                         continue;
4224                 if (codec->single_adc_amp)
4225                         idx = 0;
4226
4227                 if (cx_auto_get_rightch_label(codec, label, rightch_label, nid)) {
4228                         /* Make two independent kcontrols for left and right */
4229                         int err = cx_auto_add_volume_idx(codec, label, pfx,
4230                                               cidx, adc_nid, HDA_INPUT, idx, 1);
4231                         if (err < 0)
4232                                 return err;
4233                         return cx_auto_add_volume_idx(codec, rightch_label, pfx,
4234                                                       cidx, adc_nid, HDA_INPUT, idx, 2);
4235                 }
4236                 return cx_auto_add_volume_idx(codec, label, pfx,
4237                                               cidx, adc_nid, HDA_INPUT, idx, 3);
4238         }
4239         return 0;
4240 }
4241
4242 static int cx_auto_add_boost_volume(struct hda_codec *codec, int idx,
4243                                     const char *label, int cidx)
4244 {
4245         struct conexant_spec *spec = codec->spec;
4246         hda_nid_t mux, nid;
4247         int i, con;
4248
4249         nid = spec->imux_info[idx].pin;
4250         if (get_wcaps(codec, nid) & AC_WCAP_IN_AMP) {
4251                 char rightch_label[44];
4252                 if (cx_auto_get_rightch_label(codec, label, rightch_label, nid)) {
4253                         int err = cx_auto_add_volume_idx(codec, label, " Boost",
4254                                                          cidx, nid, HDA_INPUT, 0, 1);
4255                         if (err < 0)
4256                                 return err;
4257                         return cx_auto_add_volume_idx(codec, rightch_label, " Boost",
4258                                                       cidx, nid, HDA_INPUT, 0, 2);
4259                 }
4260                 return cx_auto_add_volume(codec, label, " Boost", cidx,
4261                                           nid, HDA_INPUT);
4262         }
4263         con = __select_input_connection(codec, spec->imux_info[idx].adc, nid,
4264                                         &mux, false, 0);
4265         if (con < 0)
4266                 return 0;
4267         for (i = 0; i < idx; i++) {
4268                 if (spec->imux_info[i].boost == mux)
4269                         return 0; /* already present */
4270         }
4271
4272         if (get_wcaps(codec, mux) & AC_WCAP_OUT_AMP) {
4273                 spec->imux_info[idx].boost = mux;
4274                 return cx_auto_add_volume(codec, label, " Boost", cidx,
4275                                           mux, HDA_OUTPUT);
4276         }
4277         return 0;
4278 }
4279
4280 static int cx_auto_build_input_controls(struct hda_codec *codec)
4281 {
4282         struct conexant_spec *spec = codec->spec;
4283         struct hda_input_mux *imux = &spec->private_imux;
4284         const char *prev_label;
4285         int input_conn[HDA_MAX_NUM_INPUTS];
4286         int i, j, err, cidx;
4287         int multi_connection;
4288
4289         if (!imux->num_items)
4290                 return 0;
4291
4292         multi_connection = 0;
4293         for (i = 0; i < imux->num_items; i++) {
4294                 cidx = get_input_connection(codec, spec->imux_info[i].adc,
4295                                             spec->imux_info[i].pin);
4296                 if (cidx < 0)
4297                         continue;
4298                 input_conn[i] = spec->imux_info[i].adc;
4299                 if (!codec->single_adc_amp)
4300                         input_conn[i] |= cidx << 8;
4301                 if (i > 0 && input_conn[i] != input_conn[0])
4302                         multi_connection = 1;
4303         }
4304
4305         prev_label = NULL;
4306         cidx = 0;
4307         for (i = 0; i < imux->num_items; i++) {
4308                 hda_nid_t nid = spec->imux_info[i].pin;
4309                 const char *label;
4310
4311                 label = hda_get_autocfg_input_label(codec, &spec->autocfg,
4312                                                     spec->imux_info[i].index);
4313                 if (label == prev_label)
4314                         cidx++;
4315                 else
4316                         cidx = 0;
4317                 prev_label = label;
4318
4319                 err = cx_auto_add_boost_volume(codec, i, label, cidx);
4320                 if (err < 0)
4321                         return err;
4322
4323                 if (!multi_connection) {
4324                         if (i > 0)
4325                                 continue;
4326                         err = cx_auto_add_capture_volume(codec, nid,
4327                                                          "Capture", "", cidx);
4328                 } else {
4329                         bool dup_found = false;
4330                         for (j = 0; j < i; j++) {
4331                                 if (input_conn[j] == input_conn[i]) {
4332                                         dup_found = true;
4333                                         break;
4334                                 }
4335                         }
4336                         if (dup_found)
4337                                 continue;
4338                         err = cx_auto_add_capture_volume(codec, nid,
4339                                                          label, " Capture", cidx);
4340                 }
4341                 if (err < 0)
4342                         return err;
4343         }
4344
4345         if (spec->private_imux.num_items > 1 && !spec->auto_mic) {
4346                 err = snd_hda_add_new_ctls(codec, cx_auto_capture_mixers);
4347                 if (err < 0)
4348                         return err;
4349         }
4350
4351         return 0;
4352 }
4353
4354 static int cx_auto_build_controls(struct hda_codec *codec)
4355 {
4356         struct conexant_spec *spec = codec->spec;
4357         int err;
4358
4359         err = cx_auto_build_output_controls(codec);
4360         if (err < 0)
4361                 return err;
4362         err = cx_auto_build_input_controls(codec);
4363         if (err < 0)
4364                 return err;
4365         err = conexant_build_controls(codec);
4366         if (err < 0)
4367                 return err;
4368         err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
4369         if (err < 0)
4370                 return err;
4371         if (spec->vmaster_mute.sw_kctl) {
4372                 spec->vmaster_mute.hook = cx_auto_vmaster_hook;
4373                 err = snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
4374                                                spec->vmaster_mute_led);
4375                 if (err < 0)
4376                         return err;
4377         }
4378         return 0;
4379 }
4380
4381 static int cx_auto_search_adcs(struct hda_codec *codec)
4382 {
4383         struct conexant_spec *spec = codec->spec;
4384         hda_nid_t nid, end_nid;
4385
4386         end_nid = codec->start_nid + codec->num_nodes;
4387         for (nid = codec->start_nid; nid < end_nid; nid++) {
4388                 unsigned int caps = get_wcaps(codec, nid);
4389                 if (get_wcaps_type(caps) != AC_WID_AUD_IN)
4390                         continue;
4391                 if (caps & AC_WCAP_DIGITAL)
4392                         continue;
4393                 if (snd_BUG_ON(spec->num_adc_nids >=
4394                                ARRAY_SIZE(spec->private_adc_nids)))
4395                         break;
4396                 spec->private_adc_nids[spec->num_adc_nids++] = nid;
4397         }
4398         spec->adc_nids = spec->private_adc_nids;
4399         return 0;
4400 }
4401
4402 static const struct hda_codec_ops cx_auto_patch_ops = {
4403         .build_controls = cx_auto_build_controls,
4404         .build_pcms = conexant_build_pcms,
4405         .init = cx_auto_init,
4406         .free = conexant_free,
4407         .unsol_event = snd_hda_jack_unsol_event,
4408 #ifdef CONFIG_PM
4409         .suspend = conexant_suspend,
4410 #endif
4411         .reboot_notify = snd_hda_shutup_pins,
4412 };
4413
4414 /*
4415  * pin fix-up
4416  */
4417 enum {
4418         CXT_PINCFG_LENOVO_X200,
4419         CXT_PINCFG_LENOVO_TP410,
4420         CXT_PINCFG_LEMOTE_A1004,
4421         CXT_PINCFG_LEMOTE_A1205,
4422         CXT_FIXUP_STEREO_DMIC,
4423         CXT_FIXUP_INC_MIC_BOOST,
4424 };
4425
4426 static void cxt_fixup_stereo_dmic(struct hda_codec *codec,
4427                                   const struct hda_fixup *fix, int action)
4428 {
4429         struct conexant_spec *spec = codec->spec;
4430         spec->fixup_stereo_dmic = 1;
4431 }
4432
4433 static void cxt5066_increase_mic_boost(struct hda_codec *codec,
4434                                    const struct hda_fixup *fix, int action)
4435 {
4436         if (action != HDA_FIXUP_ACT_PRE_PROBE)
4437                 return;
4438
4439         snd_hda_override_amp_caps(codec, 0x17, HDA_OUTPUT,
4440                                   (0x3 << AC_AMPCAP_OFFSET_SHIFT) |
4441                                   (0x4 << AC_AMPCAP_NUM_STEPS_SHIFT) |
4442                                   (0x27 << AC_AMPCAP_STEP_SIZE_SHIFT) |
4443                                   (0 << AC_AMPCAP_MUTE_SHIFT));
4444 }
4445
4446 /* ThinkPad X200 & co with cxt5051 */
4447 static const struct hda_pintbl cxt_pincfg_lenovo_x200[] = {
4448         { 0x16, 0x042140ff }, /* HP (seq# overridden) */
4449         { 0x17, 0x21a11000 }, /* dock-mic */
4450         { 0x19, 0x2121103f }, /* dock-HP */
4451         { 0x1c, 0x21440100 }, /* dock SPDIF out */
4452         {}
4453 };
4454
4455 /* ThinkPad 410/420/510/520, X201 & co with cxt5066 */
4456 static const struct hda_pintbl cxt_pincfg_lenovo_tp410[] = {
4457         { 0x19, 0x042110ff }, /* HP (seq# overridden) */
4458         { 0x1a, 0x21a190f0 }, /* dock-mic */
4459         { 0x1c, 0x212140ff }, /* dock-HP */
4460         {}
4461 };
4462
4463 /* Lemote A1004/A1205 with cxt5066 */
4464 static const struct hda_pintbl cxt_pincfg_lemote[] = {
4465         { 0x1a, 0x90a10020 }, /* Internal mic */
4466         { 0x1b, 0x03a11020 }, /* External mic */
4467         { 0x1d, 0x400101f0 }, /* Not used */
4468         { 0x1e, 0x40a701f0 }, /* Not used */
4469         { 0x20, 0x404501f0 }, /* Not used */
4470         { 0x22, 0x404401f0 }, /* Not used */
4471         { 0x23, 0x40a701f0 }, /* Not used */
4472         {}
4473 };
4474
4475 static const struct hda_fixup cxt_fixups[] = {
4476         [CXT_PINCFG_LENOVO_X200] = {
4477                 .type = HDA_FIXUP_PINS,
4478                 .v.pins = cxt_pincfg_lenovo_x200,
4479         },
4480         [CXT_PINCFG_LENOVO_TP410] = {
4481                 .type = HDA_FIXUP_PINS,
4482                 .v.pins = cxt_pincfg_lenovo_tp410,
4483         },
4484         [CXT_PINCFG_LEMOTE_A1004] = {
4485                 .type = HDA_FIXUP_PINS,
4486                 .chained = true,
4487                 .chain_id = CXT_FIXUP_INC_MIC_BOOST,
4488                 .v.pins = cxt_pincfg_lemote,
4489         },
4490         [CXT_PINCFG_LEMOTE_A1205] = {
4491                 .type = HDA_FIXUP_PINS,
4492                 .v.pins = cxt_pincfg_lemote,
4493         },
4494         [CXT_FIXUP_STEREO_DMIC] = {
4495                 .type = HDA_FIXUP_FUNC,
4496                 .v.func = cxt_fixup_stereo_dmic,
4497         },
4498         [CXT_FIXUP_INC_MIC_BOOST] = {
4499                 .type = HDA_FIXUP_FUNC,
4500                 .v.func = cxt5066_increase_mic_boost,
4501         },
4502 };
4503
4504 static const struct snd_pci_quirk cxt5051_fixups[] = {
4505         SND_PCI_QUIRK(0x17aa, 0x20f2, "Lenovo X200", CXT_PINCFG_LENOVO_X200),
4506         {}
4507 };
4508
4509 static const struct snd_pci_quirk cxt5066_fixups[] = {
4510         SND_PCI_QUIRK(0x1025, 0x0543, "Acer Aspire One 522", CXT_FIXUP_STEREO_DMIC),
4511         SND_PCI_QUIRK(0x17aa, 0x20f2, "Lenovo T400", CXT_PINCFG_LENOVO_TP410),
4512         SND_PCI_QUIRK(0x17aa, 0x215e, "Lenovo T410", CXT_PINCFG_LENOVO_TP410),
4513         SND_PCI_QUIRK(0x17aa, 0x215f, "Lenovo T510", CXT_PINCFG_LENOVO_TP410),
4514         SND_PCI_QUIRK(0x17aa, 0x21ce, "Lenovo T420", CXT_PINCFG_LENOVO_TP410),
4515         SND_PCI_QUIRK(0x17aa, 0x21cf, "Lenovo T520", CXT_PINCFG_LENOVO_TP410),
4516         SND_PCI_QUIRK(0x17aa, 0x3975, "Lenovo U300s", CXT_FIXUP_STEREO_DMIC),
4517         SND_PCI_QUIRK(0x17aa, 0x3977, "Lenovo IdeaPad U310", CXT_FIXUP_STEREO_DMIC),
4518         SND_PCI_QUIRK(0x17aa, 0x397b, "Lenovo S205", CXT_FIXUP_STEREO_DMIC),
4519         SND_PCI_QUIRK(0x1c06, 0x2011, "Lemote A1004", CXT_PINCFG_LEMOTE_A1004),
4520         SND_PCI_QUIRK(0x1c06, 0x2012, "Lemote A1205", CXT_PINCFG_LEMOTE_A1205),
4521         {}
4522 };
4523
4524 /* add "fake" mute amp-caps to DACs on cx5051 so that mixer mute switches
4525  * can be created (bko#42825)
4526  */
4527 static void add_cx5051_fake_mutes(struct hda_codec *codec)
4528 {
4529         static hda_nid_t out_nids[] = {
4530                 0x10, 0x11, 0
4531         };
4532         hda_nid_t *p;
4533
4534         for (p = out_nids; *p; p++)
4535                 snd_hda_override_amp_caps(codec, *p, HDA_OUTPUT,
4536                                           AC_AMPCAP_MIN_MUTE |
4537                                           query_amp_caps(codec, *p, HDA_OUTPUT));
4538 }
4539
4540 static int patch_conexant_auto(struct hda_codec *codec)
4541 {
4542         struct conexant_spec *spec;
4543         int err;
4544
4545         printk(KERN_INFO "hda_codec: %s: BIOS auto-probing.\n",
4546                codec->chip_name);
4547
4548         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
4549         if (!spec)
4550                 return -ENOMEM;
4551         codec->spec = spec;
4552         snd_hda_gen_init(&spec->gen);
4553
4554         switch (codec->vendor_id) {
4555         case 0x14f15045:
4556                 codec->single_adc_amp = 1;
4557                 break;
4558         case 0x14f15051:
4559                 add_cx5051_fake_mutes(codec);
4560                 codec->pin_amp_workaround = 1;
4561                 snd_hda_pick_fixup(codec, NULL, cxt5051_fixups, cxt_fixups);
4562                 break;
4563         default:
4564                 codec->pin_amp_workaround = 1;
4565                 snd_hda_pick_fixup(codec, NULL, cxt5066_fixups, cxt_fixups);
4566                 break;
4567         }
4568
4569         snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
4570
4571         /* Show mute-led control only on HP laptops
4572          * This is a sort of white-list: on HP laptops, EAPD corresponds
4573          * only to the mute-LED without actualy amp function.  Meanwhile,
4574          * others may use EAPD really as an amp switch, so it might be
4575          * not good to expose it blindly.
4576          */
4577         switch (codec->subsystem_id >> 16) {
4578         case 0x103c:
4579                 spec->vmaster_mute_led = 1;
4580                 break;
4581         }
4582
4583         err = cx_auto_search_adcs(codec);
4584         if (err < 0)
4585                 return err;
4586         err = cx_auto_parse_auto_config(codec);
4587         if (err < 0) {
4588                 kfree(codec->spec);
4589                 codec->spec = NULL;
4590                 return err;
4591         }
4592         spec->capture_stream = &cx_auto_pcm_analog_capture;
4593         codec->patch_ops = cx_auto_patch_ops;
4594         if (spec->beep_amp)
4595                 snd_hda_attach_beep_device(codec, spec->beep_amp);
4596
4597         /* Some laptops with Conexant chips show stalls in S3 resume,
4598          * which falls into the single-cmd mode.
4599          * Better to make reset, then.
4600          */
4601         if (!codec->bus->sync_write) {
4602                 snd_printd("hda_codec: "
4603                            "Enable sync_write for stable communication\n");
4604                 codec->bus->sync_write = 1;
4605                 codec->bus->allow_bus_reset = 1;
4606         }
4607
4608         return 0;
4609 }
4610
4611 /*
4612  */
4613
4614 static const struct hda_codec_preset snd_hda_preset_conexant[] = {
4615         { .id = 0x14f15045, .name = "CX20549 (Venice)",
4616           .patch = patch_cxt5045 },
4617         { .id = 0x14f15047, .name = "CX20551 (Waikiki)",
4618           .patch = patch_cxt5047 },
4619         { .id = 0x14f15051, .name = "CX20561 (Hermosa)",
4620           .patch = patch_cxt5051 },
4621         { .id = 0x14f15066, .name = "CX20582 (Pebble)",
4622           .patch = patch_cxt5066 },
4623         { .id = 0x14f15067, .name = "CX20583 (Pebble HSF)",
4624           .patch = patch_cxt5066 },
4625         { .id = 0x14f15068, .name = "CX20584",
4626           .patch = patch_cxt5066 },
4627         { .id = 0x14f15069, .name = "CX20585",
4628           .patch = patch_cxt5066 },
4629         { .id = 0x14f1506c, .name = "CX20588",
4630           .patch = patch_cxt5066 },
4631         { .id = 0x14f1506e, .name = "CX20590",
4632           .patch = patch_cxt5066 },
4633         { .id = 0x14f15097, .name = "CX20631",
4634           .patch = patch_conexant_auto },
4635         { .id = 0x14f15098, .name = "CX20632",
4636           .patch = patch_conexant_auto },
4637         { .id = 0x14f150a1, .name = "CX20641",
4638           .patch = patch_conexant_auto },
4639         { .id = 0x14f150a2, .name = "CX20642",
4640           .patch = patch_conexant_auto },
4641         { .id = 0x14f150ab, .name = "CX20651",
4642           .patch = patch_conexant_auto },
4643         { .id = 0x14f150ac, .name = "CX20652",
4644           .patch = patch_conexant_auto },
4645         { .id = 0x14f150b8, .name = "CX20664",
4646           .patch = patch_conexant_auto },
4647         { .id = 0x14f150b9, .name = "CX20665",
4648           .patch = patch_conexant_auto },
4649         { .id = 0x14f1510f, .name = "CX20751/2",
4650           .patch = patch_conexant_auto },
4651         { .id = 0x14f15110, .name = "CX20751/2",
4652           .patch = patch_conexant_auto },
4653         { .id = 0x14f15111, .name = "CX20753/4",
4654           .patch = patch_conexant_auto },
4655         {} /* terminator */
4656 };
4657
4658 MODULE_ALIAS("snd-hda-codec-id:14f15045");
4659 MODULE_ALIAS("snd-hda-codec-id:14f15047");
4660 MODULE_ALIAS("snd-hda-codec-id:14f15051");
4661 MODULE_ALIAS("snd-hda-codec-id:14f15066");
4662 MODULE_ALIAS("snd-hda-codec-id:14f15067");
4663 MODULE_ALIAS("snd-hda-codec-id:14f15068");
4664 MODULE_ALIAS("snd-hda-codec-id:14f15069");
4665 MODULE_ALIAS("snd-hda-codec-id:14f1506c");
4666 MODULE_ALIAS("snd-hda-codec-id:14f1506e");
4667 MODULE_ALIAS("snd-hda-codec-id:14f15097");
4668 MODULE_ALIAS("snd-hda-codec-id:14f15098");
4669 MODULE_ALIAS("snd-hda-codec-id:14f150a1");
4670 MODULE_ALIAS("snd-hda-codec-id:14f150a2");
4671 MODULE_ALIAS("snd-hda-codec-id:14f150ab");
4672 MODULE_ALIAS("snd-hda-codec-id:14f150ac");
4673 MODULE_ALIAS("snd-hda-codec-id:14f150b8");
4674 MODULE_ALIAS("snd-hda-codec-id:14f150b9");
4675 MODULE_ALIAS("snd-hda-codec-id:14f1510f");
4676 MODULE_ALIAS("snd-hda-codec-id:14f15110");
4677 MODULE_ALIAS("snd-hda-codec-id:14f15111");
4678
4679 MODULE_LICENSE("GPL");
4680 MODULE_DESCRIPTION("Conexant HD-audio codec");
4681
4682 static struct hda_codec_preset_list conexant_list = {
4683         .preset = snd_hda_preset_conexant,
4684         .owner = THIS_MODULE,
4685 };
4686
4687 static int __init patch_conexant_init(void)
4688 {
4689         return snd_hda_add_codec_preset(&conexant_list);
4690 }
4691
4692 static void __exit patch_conexant_exit(void)
4693 {
4694         snd_hda_delete_codec_preset(&conexant_list);
4695 }
4696
4697 module_init(patch_conexant_init)
4698 module_exit(patch_conexant_exit)