Merge branch 'timers-for-linus-migration' of git://git.kernel.org/pub/scm/linux/kerne...
[linux-2.6] / sound / pci / hda / patch_conexant.c
1 /*
2  * HD audio interface patch for Conexant HDA audio codec
3  *
4  * Copyright (c) 2006 Pototskiy Akex <alex.pototskiy@gmail.com>
5  *                    Takashi Iwai <tiwai@suse.de>
6  *                    Tobin Davis  <tdavis@dsl-only.net>
7  *
8  *  This driver is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This driver is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21  */
22
23 #include <linux/init.h>
24 #include <linux/delay.h>
25 #include <linux/slab.h>
26 #include <linux/pci.h>
27 #include <sound/core.h>
28 #include <sound/jack.h>
29
30 #include "hda_codec.h"
31 #include "hda_local.h"
32
33 #define CXT_PIN_DIR_IN              0x00
34 #define CXT_PIN_DIR_OUT             0x01
35 #define CXT_PIN_DIR_INOUT           0x02
36 #define CXT_PIN_DIR_IN_NOMICBIAS    0x03
37 #define CXT_PIN_DIR_INOUT_NOMICBIAS 0x04
38
39 #define CONEXANT_HP_EVENT       0x37
40 #define CONEXANT_MIC_EVENT      0x38
41
42 /* Conexant 5051 specific */
43
44 #define CXT5051_SPDIF_OUT       0x1C
45 #define CXT5051_PORTB_EVENT     0x38
46 #define CXT5051_PORTC_EVENT     0x39
47
48
49 struct conexant_jack {
50
51         hda_nid_t nid;
52         int type;
53         struct snd_jack *jack;
54
55 };
56
57 struct conexant_spec {
58
59         struct snd_kcontrol_new *mixers[5];
60         int num_mixers;
61         hda_nid_t vmaster_nid;
62
63         const struct hda_verb *init_verbs[5];   /* initialization verbs
64                                                  * don't forget NULL
65                                                  * termination!
66                                                  */
67         unsigned int num_init_verbs;
68
69         /* playback */
70         struct hda_multi_out multiout;  /* playback set-up
71                                          * max_channels, dacs must be set
72                                          * dig_out_nid and hp_nid are optional
73                                          */
74         unsigned int cur_eapd;
75         unsigned int hp_present;
76         unsigned int no_auto_mic;
77         unsigned int need_dac_fix;
78
79         /* capture */
80         unsigned int num_adc_nids;
81         hda_nid_t *adc_nids;
82         hda_nid_t dig_in_nid;           /* digital-in NID; optional */
83
84         unsigned int cur_adc_idx;
85         hda_nid_t cur_adc;
86         unsigned int cur_adc_stream_tag;
87         unsigned int cur_adc_format;
88
89         /* capture source */
90         const struct hda_input_mux *input_mux;
91         hda_nid_t *capsrc_nids;
92         unsigned int cur_mux[3];
93
94         /* channel model */
95         const struct hda_channel_mode *channel_mode;
96         int num_channel_mode;
97
98         /* PCM information */
99         struct hda_pcm pcm_rec[2];      /* used in build_pcms() */
100
101         unsigned int spdif_route;
102
103         /* jack detection */
104         struct snd_array jacks;
105
106         /* dynamic controls, init_verbs and input_mux */
107         struct auto_pin_cfg autocfg;
108         struct hda_input_mux private_imux;
109         hda_nid_t private_dac_nids[AUTO_CFG_MAX_OUTS];
110
111 };
112
113 static int conexant_playback_pcm_open(struct hda_pcm_stream *hinfo,
114                                       struct hda_codec *codec,
115                                       struct snd_pcm_substream *substream)
116 {
117         struct conexant_spec *spec = codec->spec;
118         return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
119                                              hinfo);
120 }
121
122 static int conexant_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
123                                          struct hda_codec *codec,
124                                          unsigned int stream_tag,
125                                          unsigned int format,
126                                          struct snd_pcm_substream *substream)
127 {
128         struct conexant_spec *spec = codec->spec;
129         return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
130                                                 stream_tag,
131                                                 format, substream);
132 }
133
134 static int conexant_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
135                                          struct hda_codec *codec,
136                                          struct snd_pcm_substream *substream)
137 {
138         struct conexant_spec *spec = codec->spec;
139         return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
140 }
141
142 /*
143  * Digital out
144  */
145 static int conexant_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
146                                           struct hda_codec *codec,
147                                           struct snd_pcm_substream *substream)
148 {
149         struct conexant_spec *spec = codec->spec;
150         return snd_hda_multi_out_dig_open(codec, &spec->multiout);
151 }
152
153 static int conexant_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
154                                          struct hda_codec *codec,
155                                          struct snd_pcm_substream *substream)
156 {
157         struct conexant_spec *spec = codec->spec;
158         return snd_hda_multi_out_dig_close(codec, &spec->multiout);
159 }
160
161 static int conexant_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
162                                          struct hda_codec *codec,
163                                          unsigned int stream_tag,
164                                          unsigned int format,
165                                          struct snd_pcm_substream *substream)
166 {
167         struct conexant_spec *spec = codec->spec;
168         return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
169                                              stream_tag,
170                                              format, substream);
171 }
172
173 /*
174  * Analog capture
175  */
176 static int conexant_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
177                                       struct hda_codec *codec,
178                                       unsigned int stream_tag,
179                                       unsigned int format,
180                                       struct snd_pcm_substream *substream)
181 {
182         struct conexant_spec *spec = codec->spec;
183         snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
184                                    stream_tag, 0, format);
185         return 0;
186 }
187
188 static int conexant_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
189                                       struct hda_codec *codec,
190                                       struct snd_pcm_substream *substream)
191 {
192         struct conexant_spec *spec = codec->spec;
193         snd_hda_codec_cleanup_stream(codec, spec->adc_nids[substream->number]);
194         return 0;
195 }
196
197
198
199 static struct hda_pcm_stream conexant_pcm_analog_playback = {
200         .substreams = 1,
201         .channels_min = 2,
202         .channels_max = 2,
203         .nid = 0, /* fill later */
204         .ops = {
205                 .open = conexant_playback_pcm_open,
206                 .prepare = conexant_playback_pcm_prepare,
207                 .cleanup = conexant_playback_pcm_cleanup
208         },
209 };
210
211 static struct hda_pcm_stream conexant_pcm_analog_capture = {
212         .substreams = 1,
213         .channels_min = 2,
214         .channels_max = 2,
215         .nid = 0, /* fill later */
216         .ops = {
217                 .prepare = conexant_capture_pcm_prepare,
218                 .cleanup = conexant_capture_pcm_cleanup
219         },
220 };
221
222
223 static struct hda_pcm_stream conexant_pcm_digital_playback = {
224         .substreams = 1,
225         .channels_min = 2,
226         .channels_max = 2,
227         .nid = 0, /* fill later */
228         .ops = {
229                 .open = conexant_dig_playback_pcm_open,
230                 .close = conexant_dig_playback_pcm_close,
231                 .prepare = conexant_dig_playback_pcm_prepare
232         },
233 };
234
235 static struct hda_pcm_stream conexant_pcm_digital_capture = {
236         .substreams = 1,
237         .channels_min = 2,
238         .channels_max = 2,
239         /* NID is set in alc_build_pcms */
240 };
241
242 static int cx5051_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
243                                       struct hda_codec *codec,
244                                       unsigned int stream_tag,
245                                       unsigned int format,
246                                       struct snd_pcm_substream *substream)
247 {
248         struct conexant_spec *spec = codec->spec;
249         spec->cur_adc = spec->adc_nids[spec->cur_adc_idx];
250         spec->cur_adc_stream_tag = stream_tag;
251         spec->cur_adc_format = format;
252         snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
253         return 0;
254 }
255
256 static int cx5051_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
257                                       struct hda_codec *codec,
258                                       struct snd_pcm_substream *substream)
259 {
260         struct conexant_spec *spec = codec->spec;
261         snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
262         spec->cur_adc = 0;
263         return 0;
264 }
265
266 static struct hda_pcm_stream cx5051_pcm_analog_capture = {
267         .substreams = 1,
268         .channels_min = 2,
269         .channels_max = 2,
270         .nid = 0, /* fill later */
271         .ops = {
272                 .prepare = cx5051_capture_pcm_prepare,
273                 .cleanup = cx5051_capture_pcm_cleanup
274         },
275 };
276
277 static int conexant_build_pcms(struct hda_codec *codec)
278 {
279         struct conexant_spec *spec = codec->spec;
280         struct hda_pcm *info = spec->pcm_rec;
281
282         codec->num_pcms = 1;
283         codec->pcm_info = info;
284
285         info->name = "CONEXANT Analog";
286         info->stream[SNDRV_PCM_STREAM_PLAYBACK] = conexant_pcm_analog_playback;
287         info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
288                 spec->multiout.max_channels;
289         info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
290                 spec->multiout.dac_nids[0];
291         if (codec->vendor_id == 0x14f15051)
292                 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
293                         cx5051_pcm_analog_capture;
294         else
295                 info->stream[SNDRV_PCM_STREAM_CAPTURE] =
296                         conexant_pcm_analog_capture;
297         info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = spec->num_adc_nids;
298         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
299
300         if (spec->multiout.dig_out_nid) {
301                 info++;
302                 codec->num_pcms++;
303                 info->name = "Conexant Digital";
304                 info->pcm_type = HDA_PCM_TYPE_SPDIF;
305                 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
306                         conexant_pcm_digital_playback;
307                 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
308                         spec->multiout.dig_out_nid;
309                 if (spec->dig_in_nid) {
310                         info->stream[SNDRV_PCM_STREAM_CAPTURE] =
311                                 conexant_pcm_digital_capture;
312                         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
313                                 spec->dig_in_nid;
314                 }
315         }
316
317         return 0;
318 }
319
320 static int conexant_mux_enum_info(struct snd_kcontrol *kcontrol,
321                                   struct snd_ctl_elem_info *uinfo)
322 {
323         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
324         struct conexant_spec *spec = codec->spec;
325
326         return snd_hda_input_mux_info(spec->input_mux, uinfo);
327 }
328
329 static int conexant_mux_enum_get(struct snd_kcontrol *kcontrol,
330                                  struct snd_ctl_elem_value *ucontrol)
331 {
332         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
333         struct conexant_spec *spec = codec->spec;
334         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
335
336         ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
337         return 0;
338 }
339
340 static int conexant_mux_enum_put(struct snd_kcontrol *kcontrol,
341                                  struct snd_ctl_elem_value *ucontrol)
342 {
343         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
344         struct conexant_spec *spec = codec->spec;
345         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
346
347         return snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
348                                      spec->capsrc_nids[adc_idx],
349                                      &spec->cur_mux[adc_idx]);
350 }
351
352 #ifdef CONFIG_SND_JACK
353 static void conexant_free_jack_priv(struct snd_jack *jack)
354 {
355         struct conexant_jack *jacks = jack->private_data;
356         jacks->nid = 0;
357         jacks->jack = NULL;
358 }
359
360 static int conexant_add_jack(struct hda_codec *codec,
361                 hda_nid_t nid, int type)
362 {
363         struct conexant_spec *spec;
364         struct conexant_jack *jack;
365         const char *name;
366         int err;
367
368         spec = codec->spec;
369         snd_array_init(&spec->jacks, sizeof(*jack), 32);
370         jack = snd_array_new(&spec->jacks);
371         name = (type == SND_JACK_HEADPHONE) ? "Headphone" : "Mic" ;
372
373         if (!jack)
374                 return -ENOMEM;
375
376         jack->nid = nid;
377         jack->type = type;
378
379         err = snd_jack_new(codec->bus->card, name, type, &jack->jack);
380         if (err < 0)
381                 return err;
382         jack->jack->private_data = jack;
383         jack->jack->private_free = conexant_free_jack_priv;
384         return 0;
385 }
386
387 static void conexant_report_jack(struct hda_codec *codec, hda_nid_t nid)
388 {
389         struct conexant_spec *spec = codec->spec;
390         struct conexant_jack *jacks = spec->jacks.list;
391
392         if (jacks) {
393                 int i;
394                 for (i = 0; i < spec->jacks.used; i++) {
395                         if (jacks->nid == nid) {
396                                 unsigned int present;
397                                 present = snd_hda_codec_read(codec, nid, 0,
398                                                 AC_VERB_GET_PIN_SENSE, 0) &
399                                         AC_PINSENSE_PRESENCE;
400
401                                 present = (present) ? jacks->type : 0 ;
402
403                                 snd_jack_report(jacks->jack,
404                                                 present);
405                         }
406                         jacks++;
407                 }
408         }
409 }
410
411 static int conexant_init_jacks(struct hda_codec *codec)
412 {
413         struct conexant_spec *spec = codec->spec;
414         int i;
415
416         for (i = 0; i < spec->num_init_verbs; i++) {
417                 const struct hda_verb *hv;
418
419                 hv = spec->init_verbs[i];
420                 while (hv->nid) {
421                         int err = 0;
422                         switch (hv->param ^ AC_USRSP_EN) {
423                         case CONEXANT_HP_EVENT:
424                                 err = conexant_add_jack(codec, hv->nid,
425                                                 SND_JACK_HEADPHONE);
426                                 conexant_report_jack(codec, hv->nid);
427                                 break;
428                         case CXT5051_PORTC_EVENT:
429                         case CONEXANT_MIC_EVENT:
430                                 err = conexant_add_jack(codec, hv->nid,
431                                                 SND_JACK_MICROPHONE);
432                                 conexant_report_jack(codec, hv->nid);
433                                 break;
434                         }
435                         if (err < 0)
436                                 return err;
437                         ++hv;
438                 }
439         }
440         return 0;
441
442 }
443 #else
444 static inline void conexant_report_jack(struct hda_codec *codec, hda_nid_t nid)
445 {
446 }
447
448 static inline int conexant_init_jacks(struct hda_codec *codec)
449 {
450         return 0;
451 }
452 #endif
453
454 static int conexant_init(struct hda_codec *codec)
455 {
456         struct conexant_spec *spec = codec->spec;
457         int i;
458
459         for (i = 0; i < spec->num_init_verbs; i++)
460                 snd_hda_sequence_write(codec, spec->init_verbs[i]);
461         return 0;
462 }
463
464 static void conexant_free(struct hda_codec *codec)
465 {
466 #ifdef CONFIG_SND_JACK
467         struct conexant_spec *spec = codec->spec;
468         if (spec->jacks.list) {
469                 struct conexant_jack *jacks = spec->jacks.list;
470                 int i;
471                 for (i = 0; i < spec->jacks.used; i++, jacks++) {
472                         if (jacks->jack)
473                                 snd_device_free(codec->bus->card, jacks->jack);
474                 }
475                 snd_array_free(&spec->jacks);
476         }
477 #endif
478         kfree(codec->spec);
479 }
480
481 static struct snd_kcontrol_new cxt_capture_mixers[] = {
482         {
483                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
484                 .name = "Capture Source",
485                 .info = conexant_mux_enum_info,
486                 .get = conexant_mux_enum_get,
487                 .put = conexant_mux_enum_put
488         },
489         {}
490 };
491
492 static const char *slave_vols[] = {
493         "Headphone Playback Volume",
494         "Speaker Playback Volume",
495         NULL
496 };
497
498 static const char *slave_sws[] = {
499         "Headphone Playback Switch",
500         "Speaker Playback Switch",
501         NULL
502 };
503
504 static int conexant_build_controls(struct hda_codec *codec)
505 {
506         struct conexant_spec *spec = codec->spec;
507         unsigned int i;
508         int err;
509
510         for (i = 0; i < spec->num_mixers; i++) {
511                 err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
512                 if (err < 0)
513                         return err;
514         }
515         if (spec->multiout.dig_out_nid) {
516                 err = snd_hda_create_spdif_out_ctls(codec,
517                                                     spec->multiout.dig_out_nid);
518                 if (err < 0)
519                         return err;
520                 err = snd_hda_create_spdif_share_sw(codec,
521                                                     &spec->multiout);
522                 if (err < 0)
523                         return err;
524                 spec->multiout.share_spdif = 1;
525         } 
526         if (spec->dig_in_nid) {
527                 err = snd_hda_create_spdif_in_ctls(codec,spec->dig_in_nid);
528                 if (err < 0)
529                         return err;
530         }
531
532         /* if we have no master control, let's create it */
533         if (spec->vmaster_nid &&
534             !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
535                 unsigned int vmaster_tlv[4];
536                 snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
537                                         HDA_OUTPUT, vmaster_tlv);
538                 err = snd_hda_add_vmaster(codec, "Master Playback Volume",
539                                           vmaster_tlv, slave_vols);
540                 if (err < 0)
541                         return err;
542         }
543         if (spec->vmaster_nid &&
544             !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
545                 err = snd_hda_add_vmaster(codec, "Master Playback Switch",
546                                           NULL, slave_sws);
547                 if (err < 0)
548                         return err;
549         }
550
551         if (spec->input_mux) {
552                 err = snd_hda_add_new_ctls(codec, cxt_capture_mixers);
553                 if (err < 0)
554                         return err;
555         }
556
557         return 0;
558 }
559
560 static struct hda_codec_ops conexant_patch_ops = {
561         .build_controls = conexant_build_controls,
562         .build_pcms = conexant_build_pcms,
563         .init = conexant_init,
564         .free = conexant_free,
565 };
566
567 /*
568  * EAPD control
569  * the private value = nid | (invert << 8)
570  */
571
572 #define cxt_eapd_info           snd_ctl_boolean_mono_info
573
574 static int cxt_eapd_get(struct snd_kcontrol *kcontrol,
575                              struct snd_ctl_elem_value *ucontrol)
576 {
577         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
578         struct conexant_spec *spec = codec->spec;
579         int invert = (kcontrol->private_value >> 8) & 1;
580         if (invert)
581                 ucontrol->value.integer.value[0] = !spec->cur_eapd;
582         else
583                 ucontrol->value.integer.value[0] = spec->cur_eapd;
584         return 0;
585
586 }
587
588 static int cxt_eapd_put(struct snd_kcontrol *kcontrol,
589                              struct snd_ctl_elem_value *ucontrol)
590 {
591         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
592         struct conexant_spec *spec = codec->spec;
593         int invert = (kcontrol->private_value >> 8) & 1;
594         hda_nid_t nid = kcontrol->private_value & 0xff;
595         unsigned int eapd;
596
597         eapd = !!ucontrol->value.integer.value[0];
598         if (invert)
599                 eapd = !eapd;
600         if (eapd == spec->cur_eapd)
601                 return 0;
602         
603         spec->cur_eapd = eapd;
604         snd_hda_codec_write_cache(codec, nid,
605                                   0, AC_VERB_SET_EAPD_BTLENABLE,
606                                   eapd ? 0x02 : 0x00);
607         return 1;
608 }
609
610 /* controls for test mode */
611 #ifdef CONFIG_SND_DEBUG
612
613 #define CXT_EAPD_SWITCH(xname, nid, mask) \
614         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = 0,  \
615           .info = cxt_eapd_info, \
616           .get = cxt_eapd_get, \
617           .put = cxt_eapd_put, \
618           .private_value = nid | (mask<<16) }
619
620
621
622 static int conexant_ch_mode_info(struct snd_kcontrol *kcontrol,
623                                  struct snd_ctl_elem_info *uinfo)
624 {
625         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
626         struct conexant_spec *spec = codec->spec;
627         return snd_hda_ch_mode_info(codec, uinfo, spec->channel_mode,
628                                     spec->num_channel_mode);
629 }
630
631 static int conexant_ch_mode_get(struct snd_kcontrol *kcontrol,
632                                 struct snd_ctl_elem_value *ucontrol)
633 {
634         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
635         struct conexant_spec *spec = codec->spec;
636         return snd_hda_ch_mode_get(codec, ucontrol, spec->channel_mode,
637                                    spec->num_channel_mode,
638                                    spec->multiout.max_channels);
639 }
640
641 static int conexant_ch_mode_put(struct snd_kcontrol *kcontrol,
642                                 struct snd_ctl_elem_value *ucontrol)
643 {
644         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
645         struct conexant_spec *spec = codec->spec;
646         int err = snd_hda_ch_mode_put(codec, ucontrol, spec->channel_mode,
647                                       spec->num_channel_mode,
648                                       &spec->multiout.max_channels);
649         if (err >= 0 && spec->need_dac_fix)
650                 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
651         return err;
652 }
653
654 #define CXT_PIN_MODE(xname, nid, dir) \
655         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = 0,  \
656           .info = conexant_ch_mode_info, \
657           .get = conexant_ch_mode_get, \
658           .put = conexant_ch_mode_put, \
659           .private_value = nid | (dir<<16) }
660
661 #endif /* CONFIG_SND_DEBUG */
662
663 /* Conexant 5045 specific */
664
665 static hda_nid_t cxt5045_dac_nids[1] = { 0x19 };
666 static hda_nid_t cxt5045_adc_nids[1] = { 0x1a };
667 static hda_nid_t cxt5045_capsrc_nids[1] = { 0x1a };
668 #define CXT5045_SPDIF_OUT       0x18
669
670 static struct hda_channel_mode cxt5045_modes[1] = {
671         { 2, NULL },
672 };
673
674 static struct hda_input_mux cxt5045_capture_source = {
675         .num_items = 2,
676         .items = {
677                 { "IntMic", 0x1 },
678                 { "ExtMic", 0x2 },
679         }
680 };
681
682 static struct hda_input_mux cxt5045_capture_source_benq = {
683         .num_items = 3,
684         .items = {
685                 { "IntMic", 0x1 },
686                 { "ExtMic", 0x2 },
687                 { "LineIn", 0x3 },
688         }
689 };
690
691 static struct hda_input_mux cxt5045_capture_source_hp530 = {
692         .num_items = 2,
693         .items = {
694                 { "ExtMic", 0x1 },
695                 { "IntMic", 0x2 },
696         }
697 };
698
699 /* turn on/off EAPD (+ mute HP) as a master switch */
700 static int cxt5045_hp_master_sw_put(struct snd_kcontrol *kcontrol,
701                                     struct snd_ctl_elem_value *ucontrol)
702 {
703         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
704         struct conexant_spec *spec = codec->spec;
705         unsigned int bits;
706
707         if (!cxt_eapd_put(kcontrol, ucontrol))
708                 return 0;
709
710         /* toggle internal speakers mute depending of presence of
711          * the headphone jack
712          */
713         bits = (!spec->hp_present && spec->cur_eapd) ? 0 : HDA_AMP_MUTE;
714         snd_hda_codec_amp_stereo(codec, 0x10, HDA_OUTPUT, 0,
715                                  HDA_AMP_MUTE, bits);
716
717         bits = spec->cur_eapd ? 0 : HDA_AMP_MUTE;
718         snd_hda_codec_amp_stereo(codec, 0x11, HDA_OUTPUT, 0,
719                                  HDA_AMP_MUTE, bits);
720         return 1;
721 }
722
723 /* bind volumes of both NID 0x10 and 0x11 */
724 static struct hda_bind_ctls cxt5045_hp_bind_master_vol = {
725         .ops = &snd_hda_bind_vol,
726         .values = {
727                 HDA_COMPOSE_AMP_VAL(0x10, 3, 0, HDA_OUTPUT),
728                 HDA_COMPOSE_AMP_VAL(0x11, 3, 0, HDA_OUTPUT),
729                 0
730         },
731 };
732
733 /* toggle input of built-in and mic jack appropriately */
734 static void cxt5045_hp_automic(struct hda_codec *codec)
735 {
736         static struct hda_verb mic_jack_on[] = {
737                 {0x14, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
738                 {0x12, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
739                 {}
740         };
741         static struct hda_verb mic_jack_off[] = {
742                 {0x12, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
743                 {0x14, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
744                 {}
745         };
746         unsigned int present;
747
748         present = snd_hda_codec_read(codec, 0x12, 0,
749                                      AC_VERB_GET_PIN_SENSE, 0) & 0x80000000;
750         if (present)
751                 snd_hda_sequence_write(codec, mic_jack_on);
752         else
753                 snd_hda_sequence_write(codec, mic_jack_off);
754 }
755
756
757 /* mute internal speaker if HP is plugged */
758 static void cxt5045_hp_automute(struct hda_codec *codec)
759 {
760         struct conexant_spec *spec = codec->spec;
761         unsigned int bits;
762
763         spec->hp_present = snd_hda_codec_read(codec, 0x11, 0,
764                                      AC_VERB_GET_PIN_SENSE, 0) & 0x80000000;
765
766         bits = (spec->hp_present || !spec->cur_eapd) ? HDA_AMP_MUTE : 0; 
767         snd_hda_codec_amp_stereo(codec, 0x10, HDA_OUTPUT, 0,
768                                  HDA_AMP_MUTE, bits);
769 }
770
771 /* unsolicited event for HP jack sensing */
772 static void cxt5045_hp_unsol_event(struct hda_codec *codec,
773                                    unsigned int res)
774 {
775         res >>= 26;
776         switch (res) {
777         case CONEXANT_HP_EVENT:
778                 cxt5045_hp_automute(codec);
779                 break;
780         case CONEXANT_MIC_EVENT:
781                 cxt5045_hp_automic(codec);
782                 break;
783
784         }
785 }
786
787 static struct snd_kcontrol_new cxt5045_mixers[] = {
788         HDA_CODEC_VOLUME("Int Mic Capture Volume", 0x1a, 0x01, HDA_INPUT),
789         HDA_CODEC_MUTE("Int Mic Capture Switch", 0x1a, 0x01, HDA_INPUT),
790         HDA_CODEC_VOLUME("Ext Mic Capture Volume", 0x1a, 0x02, HDA_INPUT),
791         HDA_CODEC_MUTE("Ext Mic Capture Switch", 0x1a, 0x02, HDA_INPUT),
792         HDA_CODEC_VOLUME("PCM Playback Volume", 0x17, 0x0, HDA_INPUT),
793         HDA_CODEC_MUTE("PCM Playback Switch", 0x17, 0x0, HDA_INPUT),
794         HDA_CODEC_VOLUME("Int Mic Playback Volume", 0x17, 0x1, HDA_INPUT),
795         HDA_CODEC_MUTE("Int Mic Playback Switch", 0x17, 0x1, HDA_INPUT),
796         HDA_CODEC_VOLUME("Ext Mic Playback Volume", 0x17, 0x2, HDA_INPUT),
797         HDA_CODEC_MUTE("Ext Mic Playback Switch", 0x17, 0x2, HDA_INPUT),
798         HDA_BIND_VOL("Master Playback Volume", &cxt5045_hp_bind_master_vol),
799         {
800                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
801                 .name = "Master Playback Switch",
802                 .info = cxt_eapd_info,
803                 .get = cxt_eapd_get,
804                 .put = cxt5045_hp_master_sw_put,
805                 .private_value = 0x10,
806         },
807
808         {}
809 };
810
811 static struct snd_kcontrol_new cxt5045_benq_mixers[] = {
812         HDA_CODEC_VOLUME("Line In Capture Volume", 0x1a, 0x03, HDA_INPUT),
813         HDA_CODEC_MUTE("Line In Capture Switch", 0x1a, 0x03, HDA_INPUT),
814         HDA_CODEC_VOLUME("Line In Playback Volume", 0x17, 0x3, HDA_INPUT),
815         HDA_CODEC_MUTE("Line In Playback Switch", 0x17, 0x3, HDA_INPUT),
816
817         {}
818 };
819
820 static struct snd_kcontrol_new cxt5045_mixers_hp530[] = {
821         HDA_CODEC_VOLUME("Int Mic Capture Volume", 0x1a, 0x02, HDA_INPUT),
822         HDA_CODEC_MUTE("Int Mic Capture Switch", 0x1a, 0x02, HDA_INPUT),
823         HDA_CODEC_VOLUME("Ext Mic Capture Volume", 0x1a, 0x01, HDA_INPUT),
824         HDA_CODEC_MUTE("Ext Mic Capture Switch", 0x1a, 0x01, HDA_INPUT),
825         HDA_CODEC_VOLUME("PCM Playback Volume", 0x17, 0x0, HDA_INPUT),
826         HDA_CODEC_MUTE("PCM Playback Switch", 0x17, 0x0, HDA_INPUT),
827         HDA_CODEC_VOLUME("Int Mic Playback Volume", 0x17, 0x2, HDA_INPUT),
828         HDA_CODEC_MUTE("Int Mic Playback Switch", 0x17, 0x2, HDA_INPUT),
829         HDA_CODEC_VOLUME("Ext Mic Playback Volume", 0x17, 0x1, HDA_INPUT),
830         HDA_CODEC_MUTE("Ext Mic Playback Switch", 0x17, 0x1, HDA_INPUT),
831         HDA_BIND_VOL("Master Playback Volume", &cxt5045_hp_bind_master_vol),
832         {
833                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
834                 .name = "Master Playback Switch",
835                 .info = cxt_eapd_info,
836                 .get = cxt_eapd_get,
837                 .put = cxt5045_hp_master_sw_put,
838                 .private_value = 0x10,
839         },
840
841         {}
842 };
843
844 static struct hda_verb cxt5045_init_verbs[] = {
845         /* Line in, Mic */
846         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN|AC_PINCTL_VREF_80 },
847         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN|AC_PINCTL_VREF_80 },
848         /* HP, Amp  */
849         {0x10, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
850         {0x10, AC_VERB_SET_CONNECT_SEL, 0x1},
851         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
852         {0x11, AC_VERB_SET_CONNECT_SEL, 0x1},
853         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
854         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
855         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
856         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
857         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
858         /* Record selector: Int mic */
859         {0x1a, AC_VERB_SET_CONNECT_SEL,0x1},
860         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE,
861          AC_AMP_SET_INPUT|AC_AMP_SET_RIGHT|AC_AMP_SET_LEFT|0x17},
862         /* SPDIF route: PCM */
863         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
864         { 0x13, AC_VERB_SET_CONNECT_SEL, 0x0 },
865         /* EAPD */
866         {0x10, AC_VERB_SET_EAPD_BTLENABLE, 0x2 }, /* default on */ 
867         { } /* end */
868 };
869
870 static struct hda_verb cxt5045_benq_init_verbs[] = {
871         /* Int Mic, Mic */
872         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN|AC_PINCTL_VREF_80 },
873         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN|AC_PINCTL_VREF_80 },
874         /* Line In,HP, Amp  */
875         {0x10, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
876         {0x10, AC_VERB_SET_CONNECT_SEL, 0x1},
877         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
878         {0x11, AC_VERB_SET_CONNECT_SEL, 0x1},
879         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
880         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
881         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)},
882         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)},
883         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)},
884         /* Record selector: Int mic */
885         {0x1a, AC_VERB_SET_CONNECT_SEL, 0x1},
886         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE,
887          AC_AMP_SET_INPUT|AC_AMP_SET_RIGHT|AC_AMP_SET_LEFT|0x17},
888         /* SPDIF route: PCM */
889         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
890         {0x13, AC_VERB_SET_CONNECT_SEL, 0x0},
891         /* EAPD */
892         {0x10, AC_VERB_SET_EAPD_BTLENABLE, 0x2}, /* default on */
893         { } /* end */
894 };
895
896 static struct hda_verb cxt5045_hp_sense_init_verbs[] = {
897         /* pin sensing on HP jack */
898         {0x11, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_HP_EVENT},
899         { } /* end */
900 };
901
902 static struct hda_verb cxt5045_mic_sense_init_verbs[] = {
903         /* pin sensing on HP jack */
904         {0x12, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_MIC_EVENT},
905         { } /* end */
906 };
907
908 #ifdef CONFIG_SND_DEBUG
909 /* Test configuration for debugging, modelled after the ALC260 test
910  * configuration.
911  */
912 static struct hda_input_mux cxt5045_test_capture_source = {
913         .num_items = 5,
914         .items = {
915                 { "MIXER", 0x0 },
916                 { "MIC1 pin", 0x1 },
917                 { "LINE1 pin", 0x2 },
918                 { "HP-OUT pin", 0x3 },
919                 { "CD pin", 0x4 },
920         },
921 };
922
923 static struct snd_kcontrol_new cxt5045_test_mixer[] = {
924
925         /* Output controls */
926         HDA_CODEC_VOLUME("Speaker Playback Volume", 0x10, 0x0, HDA_OUTPUT),
927         HDA_CODEC_MUTE("Speaker Playback Switch", 0x10, 0x0, HDA_OUTPUT),
928         HDA_CODEC_VOLUME("Node 11 Playback Volume", 0x11, 0x0, HDA_OUTPUT),
929         HDA_CODEC_MUTE("Node 11 Playback Switch", 0x11, 0x0, HDA_OUTPUT),
930         HDA_CODEC_VOLUME("Node 12 Playback Volume", 0x12, 0x0, HDA_OUTPUT),
931         HDA_CODEC_MUTE("Node 12 Playback Switch", 0x12, 0x0, HDA_OUTPUT),
932         
933         /* Modes for retasking pin widgets */
934         CXT_PIN_MODE("HP-OUT pin mode", 0x11, CXT_PIN_DIR_INOUT),
935         CXT_PIN_MODE("LINE1 pin mode", 0x12, CXT_PIN_DIR_INOUT),
936
937         /* EAPD Switch Control */
938         CXT_EAPD_SWITCH("External Amplifier", 0x10, 0x0),
939
940         /* Loopback mixer controls */
941
942         HDA_CODEC_VOLUME("Mixer-1 Volume", 0x17, 0x0, HDA_INPUT),
943         HDA_CODEC_MUTE("Mixer-1 Switch", 0x17, 0x0, HDA_INPUT),
944         HDA_CODEC_VOLUME("Mixer-2 Volume", 0x17, 0x1, HDA_INPUT),
945         HDA_CODEC_MUTE("Mixer-2 Switch", 0x17, 0x1, HDA_INPUT),
946         HDA_CODEC_VOLUME("Mixer-3 Volume", 0x17, 0x2, HDA_INPUT),
947         HDA_CODEC_MUTE("Mixer-3 Switch", 0x17, 0x2, HDA_INPUT),
948         HDA_CODEC_VOLUME("Mixer-4 Volume", 0x17, 0x3, HDA_INPUT),
949         HDA_CODEC_MUTE("Mixer-4 Switch", 0x17, 0x3, HDA_INPUT),
950         HDA_CODEC_VOLUME("Mixer-5 Volume", 0x17, 0x4, HDA_INPUT),
951         HDA_CODEC_MUTE("Mixer-5 Switch", 0x17, 0x4, HDA_INPUT),
952         {
953                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
954                 .name = "Input Source",
955                 .info = conexant_mux_enum_info,
956                 .get = conexant_mux_enum_get,
957                 .put = conexant_mux_enum_put,
958         },
959         /* Audio input controls */
960         HDA_CODEC_VOLUME("Input-1 Volume", 0x1a, 0x0, HDA_INPUT),
961         HDA_CODEC_MUTE("Input-1 Switch", 0x1a, 0x0, HDA_INPUT),
962         HDA_CODEC_VOLUME("Input-2 Volume", 0x1a, 0x1, HDA_INPUT),
963         HDA_CODEC_MUTE("Input-2 Switch", 0x1a, 0x1, HDA_INPUT),
964         HDA_CODEC_VOLUME("Input-3 Volume", 0x1a, 0x2, HDA_INPUT),
965         HDA_CODEC_MUTE("Input-3 Switch", 0x1a, 0x2, HDA_INPUT),
966         HDA_CODEC_VOLUME("Input-4 Volume", 0x1a, 0x3, HDA_INPUT),
967         HDA_CODEC_MUTE("Input-4 Switch", 0x1a, 0x3, HDA_INPUT),
968         HDA_CODEC_VOLUME("Input-5 Volume", 0x1a, 0x4, HDA_INPUT),
969         HDA_CODEC_MUTE("Input-5 Switch", 0x1a, 0x4, HDA_INPUT),
970         { } /* end */
971 };
972
973 static struct hda_verb cxt5045_test_init_verbs[] = {
974         /* Set connections */
975         { 0x10, AC_VERB_SET_CONNECT_SEL, 0x0 },
976         { 0x11, AC_VERB_SET_CONNECT_SEL, 0x0 },
977         { 0x12, AC_VERB_SET_CONNECT_SEL, 0x0 },
978         /* Enable retasking pins as output, initially without power amp */
979         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
980         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
981
982         /* Disable digital (SPDIF) pins initially, but users can enable
983          * them via a mixer switch.  In the case of SPDIF-out, this initverb
984          * payload also sets the generation to 0, output to be in "consumer"
985          * PCM format, copyright asserted, no pre-emphasis and no validity
986          * control.
987          */
988         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
989         {0x18, AC_VERB_SET_DIGI_CONVERT_1, 0},
990
991         /* Start with output sum widgets muted and their output gains at min */
992         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
993         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
994
995         /* Unmute retasking pin widget output buffers since the default
996          * state appears to be output.  As the pin mode is changed by the
997          * user the pin mode control will take care of enabling the pin's
998          * input/output buffers as needed.
999          */
1000         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1001         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1002
1003         /* Mute capture amp left and right */
1004         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1005
1006         /* Set ADC connection select to match default mixer setting (mic1
1007          * pin)
1008          */
1009         {0x1a, AC_VERB_SET_CONNECT_SEL, 0x00},
1010         {0x17, AC_VERB_SET_CONNECT_SEL, 0x00},
1011
1012         /* Mute all inputs to mixer widget (even unconnected ones) */
1013         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)}, /* Mixer pin */
1014         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)}, /* Mic1 pin */
1015         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)}, /* Line pin */
1016         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)}, /* HP pin */
1017         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)}, /* CD pin */
1018
1019         { }
1020 };
1021 #endif
1022
1023
1024 /* initialize jack-sensing, too */
1025 static int cxt5045_init(struct hda_codec *codec)
1026 {
1027         conexant_init(codec);
1028         cxt5045_hp_automute(codec);
1029         return 0;
1030 }
1031
1032
1033 enum {
1034         CXT5045_LAPTOP_HPSENSE,
1035         CXT5045_LAPTOP_MICSENSE,
1036         CXT5045_LAPTOP_HPMICSENSE,
1037         CXT5045_BENQ,
1038         CXT5045_LAPTOP_HP530,
1039 #ifdef CONFIG_SND_DEBUG
1040         CXT5045_TEST,
1041 #endif
1042         CXT5045_MODELS
1043 };
1044
1045 static const char *cxt5045_models[CXT5045_MODELS] = {
1046         [CXT5045_LAPTOP_HPSENSE]        = "laptop-hpsense",
1047         [CXT5045_LAPTOP_MICSENSE]       = "laptop-micsense",
1048         [CXT5045_LAPTOP_HPMICSENSE]     = "laptop-hpmicsense",
1049         [CXT5045_BENQ]                  = "benq",
1050         [CXT5045_LAPTOP_HP530]          = "laptop-hp530",
1051 #ifdef CONFIG_SND_DEBUG
1052         [CXT5045_TEST]          = "test",
1053 #endif
1054 };
1055
1056 static struct snd_pci_quirk cxt5045_cfg_tbl[] = {
1057         SND_PCI_QUIRK(0x103c, 0x30d5, "HP 530", CXT5045_LAPTOP_HP530),
1058         SND_PCI_QUIRK_MASK(0x103c, 0xff00, 0x3000, "HP DV Series",
1059                            CXT5045_LAPTOP_HPSENSE),
1060         SND_PCI_QUIRK(0x1179, 0xff31, "Toshiba P105", CXT5045_LAPTOP_MICSENSE),
1061         SND_PCI_QUIRK(0x152d, 0x0753, "Benq R55E", CXT5045_BENQ),
1062         SND_PCI_QUIRK(0x1734, 0x10ad, "Fujitsu Si1520", CXT5045_LAPTOP_MICSENSE),
1063         SND_PCI_QUIRK(0x1734, 0x10cb, "Fujitsu Si3515", CXT5045_LAPTOP_HPMICSENSE),
1064         SND_PCI_QUIRK(0x1734, 0x110e, "Fujitsu V5505",
1065                       CXT5045_LAPTOP_HPMICSENSE),
1066         SND_PCI_QUIRK(0x1509, 0x1e40, "FIC", CXT5045_LAPTOP_HPMICSENSE),
1067         SND_PCI_QUIRK(0x1509, 0x2f05, "FIC", CXT5045_LAPTOP_HPMICSENSE),
1068         SND_PCI_QUIRK(0x1509, 0x2f06, "FIC", CXT5045_LAPTOP_HPMICSENSE),
1069         SND_PCI_QUIRK_MASK(0x1631, 0xff00, 0xc100, "Packard Bell",
1070                            CXT5045_LAPTOP_HPMICSENSE),
1071         SND_PCI_QUIRK(0x8086, 0x2111, "Conexant Reference board", CXT5045_LAPTOP_HPSENSE),
1072         {}
1073 };
1074
1075 static int patch_cxt5045(struct hda_codec *codec)
1076 {
1077         struct conexant_spec *spec;
1078         int board_config;
1079
1080         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1081         if (!spec)
1082                 return -ENOMEM;
1083         codec->spec = spec;
1084         codec->pin_amp_workaround = 1;
1085
1086         spec->multiout.max_channels = 2;
1087         spec->multiout.num_dacs = ARRAY_SIZE(cxt5045_dac_nids);
1088         spec->multiout.dac_nids = cxt5045_dac_nids;
1089         spec->multiout.dig_out_nid = CXT5045_SPDIF_OUT;
1090         spec->num_adc_nids = 1;
1091         spec->adc_nids = cxt5045_adc_nids;
1092         spec->capsrc_nids = cxt5045_capsrc_nids;
1093         spec->input_mux = &cxt5045_capture_source;
1094         spec->num_mixers = 1;
1095         spec->mixers[0] = cxt5045_mixers;
1096         spec->num_init_verbs = 1;
1097         spec->init_verbs[0] = cxt5045_init_verbs;
1098         spec->spdif_route = 0;
1099         spec->num_channel_mode = ARRAY_SIZE(cxt5045_modes),
1100         spec->channel_mode = cxt5045_modes,
1101
1102
1103         codec->patch_ops = conexant_patch_ops;
1104
1105         board_config = snd_hda_check_board_config(codec, CXT5045_MODELS,
1106                                                   cxt5045_models,
1107                                                   cxt5045_cfg_tbl);
1108         switch (board_config) {
1109         case CXT5045_LAPTOP_HPSENSE:
1110                 codec->patch_ops.unsol_event = cxt5045_hp_unsol_event;
1111                 spec->input_mux = &cxt5045_capture_source;
1112                 spec->num_init_verbs = 2;
1113                 spec->init_verbs[1] = cxt5045_hp_sense_init_verbs;
1114                 spec->mixers[0] = cxt5045_mixers;
1115                 codec->patch_ops.init = cxt5045_init;
1116                 break;
1117         case CXT5045_LAPTOP_MICSENSE:
1118                 codec->patch_ops.unsol_event = cxt5045_hp_unsol_event;
1119                 spec->input_mux = &cxt5045_capture_source;
1120                 spec->num_init_verbs = 2;
1121                 spec->init_verbs[1] = cxt5045_mic_sense_init_verbs;
1122                 spec->mixers[0] = cxt5045_mixers;
1123                 codec->patch_ops.init = cxt5045_init;
1124                 break;
1125         default:
1126         case CXT5045_LAPTOP_HPMICSENSE:
1127                 codec->patch_ops.unsol_event = cxt5045_hp_unsol_event;
1128                 spec->input_mux = &cxt5045_capture_source;
1129                 spec->num_init_verbs = 3;
1130                 spec->init_verbs[1] = cxt5045_hp_sense_init_verbs;
1131                 spec->init_verbs[2] = cxt5045_mic_sense_init_verbs;
1132                 spec->mixers[0] = cxt5045_mixers;
1133                 codec->patch_ops.init = cxt5045_init;
1134                 break;
1135         case CXT5045_BENQ:
1136                 codec->patch_ops.unsol_event = cxt5045_hp_unsol_event;
1137                 spec->input_mux = &cxt5045_capture_source_benq;
1138                 spec->num_init_verbs = 1;
1139                 spec->init_verbs[0] = cxt5045_benq_init_verbs;
1140                 spec->mixers[0] = cxt5045_mixers;
1141                 spec->mixers[1] = cxt5045_benq_mixers;
1142                 spec->num_mixers = 2;
1143                 codec->patch_ops.init = cxt5045_init;
1144                 break;
1145         case CXT5045_LAPTOP_HP530:
1146                 codec->patch_ops.unsol_event = cxt5045_hp_unsol_event;
1147                 spec->input_mux = &cxt5045_capture_source_hp530;
1148                 spec->num_init_verbs = 2;
1149                 spec->init_verbs[1] = cxt5045_hp_sense_init_verbs;
1150                 spec->mixers[0] = cxt5045_mixers_hp530;
1151                 codec->patch_ops.init = cxt5045_init;
1152                 break;
1153 #ifdef CONFIG_SND_DEBUG
1154         case CXT5045_TEST:
1155                 spec->input_mux = &cxt5045_test_capture_source;
1156                 spec->mixers[0] = cxt5045_test_mixer;
1157                 spec->init_verbs[0] = cxt5045_test_init_verbs;
1158                 break;
1159                 
1160 #endif  
1161         }
1162
1163         switch (codec->subsystem_id >> 16) {
1164         case 0x103c:
1165                 /* HP laptop has a really bad sound over 0dB on NID 0x17.
1166                  * Fix max PCM level to 0 dB
1167                  * (originall it has 0x2b steps with 0dB offset 0x14)
1168                  */
1169                 snd_hda_override_amp_caps(codec, 0x17, HDA_INPUT,
1170                                           (0x14 << AC_AMPCAP_OFFSET_SHIFT) |
1171                                           (0x14 << AC_AMPCAP_NUM_STEPS_SHIFT) |
1172                                           (0x05 << AC_AMPCAP_STEP_SIZE_SHIFT) |
1173                                           (1 << AC_AMPCAP_MUTE_SHIFT));
1174                 break;
1175         }
1176
1177         return 0;
1178 }
1179
1180
1181 /* Conexant 5047 specific */
1182 #define CXT5047_SPDIF_OUT       0x11
1183
1184 static hda_nid_t cxt5047_dac_nids[1] = { 0x10 }; /* 0x1c */
1185 static hda_nid_t cxt5047_adc_nids[1] = { 0x12 };
1186 static hda_nid_t cxt5047_capsrc_nids[1] = { 0x1a };
1187
1188 static struct hda_channel_mode cxt5047_modes[1] = {
1189         { 2, NULL },
1190 };
1191
1192 static struct hda_input_mux cxt5047_toshiba_capture_source = {
1193         .num_items = 2,
1194         .items = {
1195                 { "ExtMic", 0x2 },
1196                 { "Line-In", 0x1 },
1197         }
1198 };
1199
1200 /* turn on/off EAPD (+ mute HP) as a master switch */
1201 static int cxt5047_hp_master_sw_put(struct snd_kcontrol *kcontrol,
1202                                     struct snd_ctl_elem_value *ucontrol)
1203 {
1204         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1205         struct conexant_spec *spec = codec->spec;
1206         unsigned int bits;
1207
1208         if (!cxt_eapd_put(kcontrol, ucontrol))
1209                 return 0;
1210
1211         /* toggle internal speakers mute depending of presence of
1212          * the headphone jack
1213          */
1214         bits = (!spec->hp_present && spec->cur_eapd) ? 0 : HDA_AMP_MUTE;
1215         /* NOTE: Conexat codec needs the index for *OUTPUT* amp of
1216          * pin widgets unlike other codecs.  In this case, we need to
1217          * set index 0x01 for the volume from the mixer amp 0x19.
1218          */
1219         snd_hda_codec_amp_stereo(codec, 0x1d, HDA_OUTPUT, 0x01,
1220                                  HDA_AMP_MUTE, bits);
1221         bits = spec->cur_eapd ? 0 : HDA_AMP_MUTE;
1222         snd_hda_codec_amp_stereo(codec, 0x13, HDA_OUTPUT, 0,
1223                                  HDA_AMP_MUTE, bits);
1224         return 1;
1225 }
1226
1227 /* mute internal speaker if HP is plugged */
1228 static void cxt5047_hp_automute(struct hda_codec *codec)
1229 {
1230         struct conexant_spec *spec = codec->spec;
1231         unsigned int bits;
1232
1233         spec->hp_present = snd_hda_codec_read(codec, 0x13, 0,
1234                                      AC_VERB_GET_PIN_SENSE, 0) & 0x80000000;
1235
1236         bits = (spec->hp_present || !spec->cur_eapd) ? HDA_AMP_MUTE : 0;
1237         /* See the note in cxt5047_hp_master_sw_put */
1238         snd_hda_codec_amp_stereo(codec, 0x1d, HDA_OUTPUT, 0x01,
1239                                  HDA_AMP_MUTE, bits);
1240 }
1241
1242 /* toggle input of built-in and mic jack appropriately */
1243 static void cxt5047_hp_automic(struct hda_codec *codec)
1244 {
1245         static struct hda_verb mic_jack_on[] = {
1246                 {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1247                 {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1248                 {}
1249         };
1250         static struct hda_verb mic_jack_off[] = {
1251                 {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE},
1252                 {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1253                 {}
1254         };
1255         unsigned int present;
1256
1257         present = snd_hda_codec_read(codec, 0x15, 0,
1258                                      AC_VERB_GET_PIN_SENSE, 0) & 0x80000000;
1259         if (present)
1260                 snd_hda_sequence_write(codec, mic_jack_on);
1261         else
1262                 snd_hda_sequence_write(codec, mic_jack_off);
1263 }
1264
1265 /* unsolicited event for HP jack sensing */
1266 static void cxt5047_hp_unsol_event(struct hda_codec *codec,
1267                                   unsigned int res)
1268 {
1269         switch (res >> 26) {
1270         case CONEXANT_HP_EVENT:
1271                 cxt5047_hp_automute(codec);
1272                 break;
1273         case CONEXANT_MIC_EVENT:
1274                 cxt5047_hp_automic(codec);
1275                 break;
1276         }
1277 }
1278
1279 static struct snd_kcontrol_new cxt5047_base_mixers[] = {
1280         HDA_CODEC_VOLUME("Mic Playback Volume", 0x19, 0x02, HDA_INPUT),
1281         HDA_CODEC_MUTE("Mic Playback Switch", 0x19, 0x02, HDA_INPUT),
1282         HDA_CODEC_VOLUME("Mic Boost", 0x1a, 0x0, HDA_OUTPUT),
1283         HDA_CODEC_VOLUME("Capture Volume", 0x12, 0x03, HDA_INPUT),
1284         HDA_CODEC_MUTE("Capture Switch", 0x12, 0x03, HDA_INPUT),
1285         HDA_CODEC_VOLUME("PCM Volume", 0x10, 0x00, HDA_OUTPUT),
1286         HDA_CODEC_MUTE("PCM Switch", 0x10, 0x00, HDA_OUTPUT),
1287         {
1288                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1289                 .name = "Master Playback Switch",
1290                 .info = cxt_eapd_info,
1291                 .get = cxt_eapd_get,
1292                 .put = cxt5047_hp_master_sw_put,
1293                 .private_value = 0x13,
1294         },
1295
1296         {}
1297 };
1298
1299 static struct snd_kcontrol_new cxt5047_hp_spk_mixers[] = {
1300         /* See the note in cxt5047_hp_master_sw_put */
1301         HDA_CODEC_VOLUME("Speaker Playback Volume", 0x1d, 0x01, HDA_OUTPUT),
1302         HDA_CODEC_VOLUME("Headphone Playback Volume", 0x13, 0x00, HDA_OUTPUT),
1303         {}
1304 };
1305
1306 static struct snd_kcontrol_new cxt5047_hp_only_mixers[] = {
1307         HDA_CODEC_VOLUME("Master Playback Volume", 0x13, 0x00, HDA_OUTPUT),
1308         { } /* end */
1309 };
1310
1311 static struct hda_verb cxt5047_init_verbs[] = {
1312         /* Line in, Mic, Built-in Mic */
1313         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
1314         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN|AC_PINCTL_VREF_50 },
1315         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN|AC_PINCTL_VREF_50 },
1316         /* HP, Speaker  */
1317         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
1318         {0x13, AC_VERB_SET_CONNECT_SEL, 0x0}, /* mixer(0x19) */
1319         {0x1d, AC_VERB_SET_CONNECT_SEL, 0x1}, /* mixer(0x19) */
1320         /* Record selector: Mic */
1321         {0x12, AC_VERB_SET_CONNECT_SEL,0x03},
1322         {0x19, AC_VERB_SET_AMP_GAIN_MUTE,
1323          AC_AMP_SET_INPUT|AC_AMP_SET_RIGHT|AC_AMP_SET_LEFT|0x17},
1324         {0x1A, AC_VERB_SET_CONNECT_SEL,0x02},
1325         {0x1A, AC_VERB_SET_AMP_GAIN_MUTE,
1326          AC_AMP_SET_OUTPUT|AC_AMP_SET_RIGHT|AC_AMP_SET_LEFT|0x00},
1327         {0x1A, AC_VERB_SET_AMP_GAIN_MUTE,
1328          AC_AMP_SET_OUTPUT|AC_AMP_SET_RIGHT|AC_AMP_SET_LEFT|0x03},
1329         /* SPDIF route: PCM */
1330         { 0x18, AC_VERB_SET_CONNECT_SEL, 0x0 },
1331         /* Enable unsolicited events */
1332         {0x13, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_HP_EVENT},
1333         {0x15, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_MIC_EVENT},
1334         { } /* end */
1335 };
1336
1337 /* configuration for Toshiba Laptops */
1338 static struct hda_verb cxt5047_toshiba_init_verbs[] = {
1339         {0x13, AC_VERB_SET_EAPD_BTLENABLE, 0x0}, /* default off */
1340         {}
1341 };
1342
1343 /* Test configuration for debugging, modelled after the ALC260 test
1344  * configuration.
1345  */
1346 #ifdef CONFIG_SND_DEBUG
1347 static struct hda_input_mux cxt5047_test_capture_source = {
1348         .num_items = 4,
1349         .items = {
1350                 { "LINE1 pin", 0x0 },
1351                 { "MIC1 pin", 0x1 },
1352                 { "MIC2 pin", 0x2 },
1353                 { "CD pin", 0x3 },
1354         },
1355 };
1356
1357 static struct snd_kcontrol_new cxt5047_test_mixer[] = {
1358
1359         /* Output only controls */
1360         HDA_CODEC_VOLUME("OutAmp-1 Volume", 0x10, 0x0, HDA_OUTPUT),
1361         HDA_CODEC_MUTE("OutAmp-1 Switch", 0x10,0x0, HDA_OUTPUT),
1362         HDA_CODEC_VOLUME("OutAmp-2 Volume", 0x1c, 0x0, HDA_OUTPUT),
1363         HDA_CODEC_MUTE("OutAmp-2 Switch", 0x1c, 0x0, HDA_OUTPUT),
1364         HDA_CODEC_VOLUME("Speaker Playback Volume", 0x1d, 0x0, HDA_OUTPUT),
1365         HDA_CODEC_MUTE("Speaker Playback Switch", 0x1d, 0x0, HDA_OUTPUT),
1366         HDA_CODEC_VOLUME("HeadPhone Playback Volume", 0x13, 0x0, HDA_OUTPUT),
1367         HDA_CODEC_MUTE("HeadPhone Playback Switch", 0x13, 0x0, HDA_OUTPUT),
1368         HDA_CODEC_VOLUME("Line1-Out Playback Volume", 0x14, 0x0, HDA_OUTPUT),
1369         HDA_CODEC_MUTE("Line1-Out Playback Switch", 0x14, 0x0, HDA_OUTPUT),
1370         HDA_CODEC_VOLUME("Line2-Out Playback Volume", 0x15, 0x0, HDA_OUTPUT),
1371         HDA_CODEC_MUTE("Line2-Out Playback Switch", 0x15, 0x0, HDA_OUTPUT),
1372
1373         /* Modes for retasking pin widgets */
1374         CXT_PIN_MODE("LINE1 pin mode", 0x14, CXT_PIN_DIR_INOUT),
1375         CXT_PIN_MODE("MIC1 pin mode", 0x15, CXT_PIN_DIR_INOUT),
1376
1377         /* EAPD Switch Control */
1378         CXT_EAPD_SWITCH("External Amplifier", 0x13, 0x0),
1379
1380         /* Loopback mixer controls */
1381         HDA_CODEC_VOLUME("MIC1 Playback Volume", 0x12, 0x01, HDA_INPUT),
1382         HDA_CODEC_MUTE("MIC1 Playback Switch", 0x12, 0x01, HDA_INPUT),
1383         HDA_CODEC_VOLUME("MIC2 Playback Volume", 0x12, 0x02, HDA_INPUT),
1384         HDA_CODEC_MUTE("MIC2 Playback Switch", 0x12, 0x02, HDA_INPUT),
1385         HDA_CODEC_VOLUME("LINE Playback Volume", 0x12, 0x0, HDA_INPUT),
1386         HDA_CODEC_MUTE("LINE Playback Switch", 0x12, 0x0, HDA_INPUT),
1387         HDA_CODEC_VOLUME("CD Playback Volume", 0x12, 0x04, HDA_INPUT),
1388         HDA_CODEC_MUTE("CD Playback Switch", 0x12, 0x04, HDA_INPUT),
1389
1390         HDA_CODEC_VOLUME("Capture-1 Volume", 0x19, 0x0, HDA_INPUT),
1391         HDA_CODEC_MUTE("Capture-1 Switch", 0x19, 0x0, HDA_INPUT),
1392         HDA_CODEC_VOLUME("Capture-2 Volume", 0x19, 0x1, HDA_INPUT),
1393         HDA_CODEC_MUTE("Capture-2 Switch", 0x19, 0x1, HDA_INPUT),
1394         HDA_CODEC_VOLUME("Capture-3 Volume", 0x19, 0x2, HDA_INPUT),
1395         HDA_CODEC_MUTE("Capture-3 Switch", 0x19, 0x2, HDA_INPUT),
1396         HDA_CODEC_VOLUME("Capture-4 Volume", 0x19, 0x3, HDA_INPUT),
1397         HDA_CODEC_MUTE("Capture-4 Switch", 0x19, 0x3, HDA_INPUT),
1398         {
1399                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1400                 .name = "Input Source",
1401                 .info = conexant_mux_enum_info,
1402                 .get = conexant_mux_enum_get,
1403                 .put = conexant_mux_enum_put,
1404         },
1405         HDA_CODEC_VOLUME("Input-1 Volume", 0x1a, 0x0, HDA_INPUT),
1406         HDA_CODEC_MUTE("Input-1 Switch", 0x1a, 0x0, HDA_INPUT),
1407         HDA_CODEC_VOLUME("Input-2 Volume", 0x1a, 0x1, HDA_INPUT),
1408         HDA_CODEC_MUTE("Input-2 Switch", 0x1a, 0x1, HDA_INPUT),
1409         HDA_CODEC_VOLUME("Input-3 Volume", 0x1a, 0x2, HDA_INPUT),
1410         HDA_CODEC_MUTE("Input-3 Switch", 0x1a, 0x2, HDA_INPUT),
1411         HDA_CODEC_VOLUME("Input-4 Volume", 0x1a, 0x3, HDA_INPUT),
1412         HDA_CODEC_MUTE("Input-4 Switch", 0x1a, 0x3, HDA_INPUT),
1413         HDA_CODEC_VOLUME("Input-5 Volume", 0x1a, 0x4, HDA_INPUT),
1414         HDA_CODEC_MUTE("Input-5 Switch", 0x1a, 0x4, HDA_INPUT),
1415
1416         { } /* end */
1417 };
1418
1419 static struct hda_verb cxt5047_test_init_verbs[] = {
1420         /* Enable retasking pins as output, initially without power amp */
1421         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1422         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1423         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1424
1425         /* Disable digital (SPDIF) pins initially, but users can enable
1426          * them via a mixer switch.  In the case of SPDIF-out, this initverb
1427          * payload also sets the generation to 0, output to be in "consumer"
1428          * PCM format, copyright asserted, no pre-emphasis and no validity
1429          * control.
1430          */
1431         {0x18, AC_VERB_SET_DIGI_CONVERT_1, 0},
1432
1433         /* Ensure mic1, mic2, line1 pin widgets take input from the 
1434          * OUT1 sum bus when acting as an output.
1435          */
1436         {0x1a, AC_VERB_SET_CONNECT_SEL, 0},
1437         {0x1b, AC_VERB_SET_CONNECT_SEL, 0},
1438
1439         /* Start with output sum widgets muted and their output gains at min */
1440         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1441         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
1442
1443         /* Unmute retasking pin widget output buffers since the default
1444          * state appears to be output.  As the pin mode is changed by the
1445          * user the pin mode control will take care of enabling the pin's
1446          * input/output buffers as needed.
1447          */
1448         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1449         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1450         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1451
1452         /* Mute capture amp left and right */
1453         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1454
1455         /* Set ADC connection select to match default mixer setting (mic1
1456          * pin)
1457          */
1458         {0x12, AC_VERB_SET_CONNECT_SEL, 0x00},
1459
1460         /* Mute all inputs to mixer widget (even unconnected ones) */
1461         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)}, /* mic1 pin */
1462         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)}, /* mic2 pin */
1463         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)}, /* line1 pin */
1464         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)}, /* line2 pin */
1465         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)}, /* CD pin */
1466         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(5)}, /* Beep-gen pin */
1467         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(6)}, /* Line-out pin */
1468         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(7)}, /* HP-pin pin */
1469
1470         { }
1471 };
1472 #endif
1473
1474
1475 /* initialize jack-sensing, too */
1476 static int cxt5047_hp_init(struct hda_codec *codec)
1477 {
1478         conexant_init(codec);
1479         cxt5047_hp_automute(codec);
1480         return 0;
1481 }
1482
1483
1484 enum {
1485         CXT5047_LAPTOP,         /* Laptops w/o EAPD support */
1486         CXT5047_LAPTOP_HP,      /* Some HP laptops */
1487         CXT5047_LAPTOP_EAPD,    /* Laptops with EAPD support */
1488 #ifdef CONFIG_SND_DEBUG
1489         CXT5047_TEST,
1490 #endif
1491         CXT5047_MODELS
1492 };
1493
1494 static const char *cxt5047_models[CXT5047_MODELS] = {
1495         [CXT5047_LAPTOP]        = "laptop",
1496         [CXT5047_LAPTOP_HP]     = "laptop-hp",
1497         [CXT5047_LAPTOP_EAPD]   = "laptop-eapd",
1498 #ifdef CONFIG_SND_DEBUG
1499         [CXT5047_TEST]          = "test",
1500 #endif
1501 };
1502
1503 static struct snd_pci_quirk cxt5047_cfg_tbl[] = {
1504         SND_PCI_QUIRK(0x103c, 0x30a5, "HP DV5200T/DV8000T", CXT5047_LAPTOP_HP),
1505         SND_PCI_QUIRK_MASK(0x103c, 0xff00, 0x3000, "HP DV Series",
1506                            CXT5047_LAPTOP),
1507         SND_PCI_QUIRK(0x1179, 0xff31, "Toshiba P100", CXT5047_LAPTOP_EAPD),
1508         {}
1509 };
1510
1511 static int patch_cxt5047(struct hda_codec *codec)
1512 {
1513         struct conexant_spec *spec;
1514         int board_config;
1515
1516         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1517         if (!spec)
1518                 return -ENOMEM;
1519         codec->spec = spec;
1520         codec->pin_amp_workaround = 1;
1521
1522         spec->multiout.max_channels = 2;
1523         spec->multiout.num_dacs = ARRAY_SIZE(cxt5047_dac_nids);
1524         spec->multiout.dac_nids = cxt5047_dac_nids;
1525         spec->multiout.dig_out_nid = CXT5047_SPDIF_OUT;
1526         spec->num_adc_nids = 1;
1527         spec->adc_nids = cxt5047_adc_nids;
1528         spec->capsrc_nids = cxt5047_capsrc_nids;
1529         spec->num_mixers = 1;
1530         spec->mixers[0] = cxt5047_base_mixers;
1531         spec->num_init_verbs = 1;
1532         spec->init_verbs[0] = cxt5047_init_verbs;
1533         spec->spdif_route = 0;
1534         spec->num_channel_mode = ARRAY_SIZE(cxt5047_modes),
1535         spec->channel_mode = cxt5047_modes,
1536
1537         codec->patch_ops = conexant_patch_ops;
1538
1539         board_config = snd_hda_check_board_config(codec, CXT5047_MODELS,
1540                                                   cxt5047_models,
1541                                                   cxt5047_cfg_tbl);
1542         switch (board_config) {
1543         case CXT5047_LAPTOP:
1544                 spec->num_mixers = 2;
1545                 spec->mixers[1] = cxt5047_hp_spk_mixers;
1546                 codec->patch_ops.unsol_event = cxt5047_hp_unsol_event;
1547                 break;
1548         case CXT5047_LAPTOP_HP:
1549                 spec->num_mixers = 2;
1550                 spec->mixers[1] = cxt5047_hp_only_mixers;
1551                 codec->patch_ops.unsol_event = cxt5047_hp_unsol_event;
1552                 codec->patch_ops.init = cxt5047_hp_init;
1553                 break;
1554         case CXT5047_LAPTOP_EAPD:
1555                 spec->input_mux = &cxt5047_toshiba_capture_source;
1556                 spec->num_mixers = 2;
1557                 spec->mixers[1] = cxt5047_hp_spk_mixers;
1558                 spec->num_init_verbs = 2;
1559                 spec->init_verbs[1] = cxt5047_toshiba_init_verbs;
1560                 codec->patch_ops.unsol_event = cxt5047_hp_unsol_event;
1561                 break;
1562 #ifdef CONFIG_SND_DEBUG
1563         case CXT5047_TEST:
1564                 spec->input_mux = &cxt5047_test_capture_source;
1565                 spec->mixers[0] = cxt5047_test_mixer;
1566                 spec->init_verbs[0] = cxt5047_test_init_verbs;
1567                 codec->patch_ops.unsol_event = cxt5047_hp_unsol_event;
1568 #endif  
1569         }
1570         spec->vmaster_nid = 0x13;
1571         return 0;
1572 }
1573
1574 /* Conexant 5051 specific */
1575 static hda_nid_t cxt5051_dac_nids[1] = { 0x10 };
1576 static hda_nid_t cxt5051_adc_nids[2] = { 0x14, 0x15 };
1577
1578 static struct hda_channel_mode cxt5051_modes[1] = {
1579         { 2, NULL },
1580 };
1581
1582 static void cxt5051_update_speaker(struct hda_codec *codec)
1583 {
1584         struct conexant_spec *spec = codec->spec;
1585         unsigned int pinctl;
1586         pinctl = (!spec->hp_present && spec->cur_eapd) ? PIN_OUT : 0;
1587         snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
1588                             pinctl);
1589 }
1590
1591 /* turn on/off EAPD (+ mute HP) as a master switch */
1592 static int cxt5051_hp_master_sw_put(struct snd_kcontrol *kcontrol,
1593                                     struct snd_ctl_elem_value *ucontrol)
1594 {
1595         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1596
1597         if (!cxt_eapd_put(kcontrol, ucontrol))
1598                 return 0;
1599         cxt5051_update_speaker(codec);
1600         return 1;
1601 }
1602
1603 /* toggle input of built-in and mic jack appropriately */
1604 static void cxt5051_portb_automic(struct hda_codec *codec)
1605 {
1606         struct conexant_spec *spec = codec->spec;
1607         unsigned int present;
1608
1609         if (spec->no_auto_mic)
1610                 return;
1611         present = snd_hda_codec_read(codec, 0x17, 0,
1612                                      AC_VERB_GET_PIN_SENSE, 0) &
1613                 AC_PINSENSE_PRESENCE;
1614         snd_hda_codec_write(codec, 0x14, 0,
1615                             AC_VERB_SET_CONNECT_SEL,
1616                             present ? 0x01 : 0x00);
1617 }
1618
1619 /* switch the current ADC according to the jack state */
1620 static void cxt5051_portc_automic(struct hda_codec *codec)
1621 {
1622         struct conexant_spec *spec = codec->spec;
1623         unsigned int present;
1624         hda_nid_t new_adc;
1625
1626         if (spec->no_auto_mic)
1627                 return;
1628         present = snd_hda_codec_read(codec, 0x18, 0,
1629                                      AC_VERB_GET_PIN_SENSE, 0) &
1630                 AC_PINSENSE_PRESENCE;
1631         if (present)
1632                 spec->cur_adc_idx = 1;
1633         else
1634                 spec->cur_adc_idx = 0;
1635         new_adc = spec->adc_nids[spec->cur_adc_idx];
1636         if (spec->cur_adc && spec->cur_adc != new_adc) {
1637                 /* stream is running, let's swap the current ADC */
1638                 snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
1639                 spec->cur_adc = new_adc;
1640                 snd_hda_codec_setup_stream(codec, new_adc,
1641                                            spec->cur_adc_stream_tag, 0,
1642                                            spec->cur_adc_format);
1643         }
1644 }
1645
1646 /* mute internal speaker if HP is plugged */
1647 static void cxt5051_hp_automute(struct hda_codec *codec)
1648 {
1649         struct conexant_spec *spec = codec->spec;
1650
1651         spec->hp_present = snd_hda_codec_read(codec, 0x16, 0,
1652                                      AC_VERB_GET_PIN_SENSE, 0) &
1653                 AC_PINSENSE_PRESENCE;
1654         cxt5051_update_speaker(codec);
1655 }
1656
1657 /* unsolicited event for HP jack sensing */
1658 static void cxt5051_hp_unsol_event(struct hda_codec *codec,
1659                                    unsigned int res)
1660 {
1661         int nid = (res & AC_UNSOL_RES_SUBTAG) >> 20;
1662         switch (res >> 26) {
1663         case CONEXANT_HP_EVENT:
1664                 cxt5051_hp_automute(codec);
1665                 break;
1666         case CXT5051_PORTB_EVENT:
1667                 cxt5051_portb_automic(codec);
1668                 break;
1669         case CXT5051_PORTC_EVENT:
1670                 cxt5051_portc_automic(codec);
1671                 break;
1672         }
1673         conexant_report_jack(codec, nid);
1674 }
1675
1676 static struct snd_kcontrol_new cxt5051_mixers[] = {
1677         HDA_CODEC_VOLUME("Internal Mic Volume", 0x14, 0x00, HDA_INPUT),
1678         HDA_CODEC_MUTE("Internal Mic Switch", 0x14, 0x00, HDA_INPUT),
1679         HDA_CODEC_VOLUME("External Mic Volume", 0x14, 0x01, HDA_INPUT),
1680         HDA_CODEC_MUTE("External Mic Switch", 0x14, 0x01, HDA_INPUT),
1681         HDA_CODEC_VOLUME("Docking Mic Volume", 0x15, 0x00, HDA_INPUT),
1682         HDA_CODEC_MUTE("Docking Mic Switch", 0x15, 0x00, HDA_INPUT),
1683         HDA_CODEC_VOLUME("Master Playback Volume", 0x10, 0x00, HDA_OUTPUT),
1684         {
1685                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1686                 .name = "Master Playback Switch",
1687                 .info = cxt_eapd_info,
1688                 .get = cxt_eapd_get,
1689                 .put = cxt5051_hp_master_sw_put,
1690                 .private_value = 0x1a,
1691         },
1692
1693         {}
1694 };
1695
1696 static struct snd_kcontrol_new cxt5051_hp_mixers[] = {
1697         HDA_CODEC_VOLUME("Internal Mic Volume", 0x14, 0x00, HDA_INPUT),
1698         HDA_CODEC_MUTE("Internal Mic Switch", 0x14, 0x00, HDA_INPUT),
1699         HDA_CODEC_VOLUME("External Mic Volume", 0x15, 0x00, HDA_INPUT),
1700         HDA_CODEC_MUTE("External Mic Switch", 0x15, 0x00, HDA_INPUT),
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
1714 static struct snd_kcontrol_new cxt5051_hp_dv6736_mixers[] = {
1715         HDA_CODEC_VOLUME("Mic Volume", 0x14, 0x00, HDA_INPUT),
1716         HDA_CODEC_MUTE("Mic Switch", 0x14, 0x00, HDA_INPUT),
1717         HDA_CODEC_VOLUME("Master Playback Volume", 0x10, 0x00, HDA_OUTPUT),
1718         {
1719                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1720                 .name = "Master Playback Switch",
1721                 .info = cxt_eapd_info,
1722                 .get = cxt_eapd_get,
1723                 .put = cxt5051_hp_master_sw_put,
1724                 .private_value = 0x1a,
1725         },
1726
1727         {}
1728 };
1729
1730 static struct hda_verb cxt5051_init_verbs[] = {
1731         /* Line in, Mic */
1732         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x03},
1733         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1734         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x03},
1735         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1736         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
1737         {0x1d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x03},
1738         /* SPK  */
1739         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1740         {0x1a, AC_VERB_SET_CONNECT_SEL, 0x00},
1741         /* HP, Amp  */
1742         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1743         {0x16, AC_VERB_SET_CONNECT_SEL, 0x00},
1744         /* DAC1 */      
1745         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1746         /* Record selector: Int mic */
1747         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x44},
1748         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1) | 0x44},
1749         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x44},
1750         /* SPDIF route: PCM */
1751         {0x1c, AC_VERB_SET_CONNECT_SEL, 0x0},
1752         /* EAPD */
1753         {0x1a, AC_VERB_SET_EAPD_BTLENABLE, 0x2}, /* default on */ 
1754         {0x16, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN|CONEXANT_HP_EVENT},
1755         {0x17, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN|CXT5051_PORTB_EVENT},
1756         {0x18, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN|CXT5051_PORTC_EVENT},
1757         { } /* end */
1758 };
1759
1760 static struct hda_verb cxt5051_hp_dv6736_init_verbs[] = {
1761         /* Line in, Mic */
1762         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x03},
1763         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1764         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x0},
1765         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x0},
1766         /* SPK  */
1767         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1768         {0x1a, AC_VERB_SET_CONNECT_SEL, 0x00},
1769         /* HP, Amp  */
1770         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1771         {0x16, AC_VERB_SET_CONNECT_SEL, 0x00},
1772         /* DAC1 */
1773         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1774         /* Record selector: Int mic */
1775         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1) | 0x44},
1776         {0x14, AC_VERB_SET_CONNECT_SEL, 0x1},
1777         /* SPDIF route: PCM */
1778         {0x1c, AC_VERB_SET_CONNECT_SEL, 0x0},
1779         /* EAPD */
1780         {0x1a, AC_VERB_SET_EAPD_BTLENABLE, 0x2}, /* default on */
1781         {0x16, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN|CONEXANT_HP_EVENT},
1782         {0x17, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN|CXT5051_PORTB_EVENT},
1783         { } /* end */
1784 };
1785
1786 static struct hda_verb cxt5051_lenovo_x200_init_verbs[] = {
1787         /* Line in, Mic */
1788         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x03},
1789         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1790         {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x03},
1791         {0x18, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80},
1792         {0x1d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN},
1793         {0x1d, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x03},
1794         /* SPK  */
1795         {0x1a, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1796         {0x1a, AC_VERB_SET_CONNECT_SEL, 0x00},
1797         /* HP, Amp  */
1798         {0x16, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1799         {0x16, AC_VERB_SET_CONNECT_SEL, 0x00},
1800         /* Docking HP */
1801         {0x19, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP},
1802         {0x19, AC_VERB_SET_CONNECT_SEL, 0x00},
1803         /* DAC1 */
1804         {0x10, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1805         /* Record selector: Int mic */
1806         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x44},
1807         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1) | 0x44},
1808         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0) | 0x44},
1809         /* SPDIF route: PCM */
1810         {0x1c, AC_VERB_SET_CONNECT_SEL, 0x0},
1811         /* EAPD */
1812         {0x1a, AC_VERB_SET_EAPD_BTLENABLE, 0x2}, /* default on */
1813         {0x16, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN|CONEXANT_HP_EVENT},
1814         {0x17, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN|CXT5051_PORTB_EVENT},
1815         {0x18, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN|CXT5051_PORTC_EVENT},
1816         {0x19, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN|CONEXANT_HP_EVENT},
1817         { } /* end */
1818 };
1819
1820 /* initialize jack-sensing, too */
1821 static int cxt5051_init(struct hda_codec *codec)
1822 {
1823         conexant_init(codec);
1824         conexant_init_jacks(codec);
1825         if (codec->patch_ops.unsol_event) {
1826                 cxt5051_hp_automute(codec);
1827                 cxt5051_portb_automic(codec);
1828                 cxt5051_portc_automic(codec);
1829         }
1830         return 0;
1831 }
1832
1833
1834 enum {
1835         CXT5051_LAPTOP,  /* Laptops w/ EAPD support */
1836         CXT5051_HP,     /* no docking */
1837         CXT5051_HP_DV6736,      /* HP without mic switch */
1838         CXT5051_LENOVO_X200,    /* Lenovo X200 laptop */
1839         CXT5051_MODELS
1840 };
1841
1842 static const char *cxt5051_models[CXT5051_MODELS] = {
1843         [CXT5051_LAPTOP]        = "laptop",
1844         [CXT5051_HP]            = "hp",
1845         [CXT5051_HP_DV6736]     = "hp-dv6736",
1846         [CXT5051_LENOVO_X200]   = "lenovo-x200",
1847 };
1848
1849 static struct snd_pci_quirk cxt5051_cfg_tbl[] = {
1850         SND_PCI_QUIRK(0x103c, 0x30cf, "HP DV6736", CXT5051_HP_DV6736),
1851         SND_PCI_QUIRK(0x103c, 0x360b, "Compaq Presario CQ60", CXT5051_HP),
1852         SND_PCI_QUIRK(0x14f1, 0x0101, "Conexant Reference board",
1853                       CXT5051_LAPTOP),
1854         SND_PCI_QUIRK(0x14f1, 0x5051, "HP Spartan 1.1", CXT5051_HP),
1855         SND_PCI_QUIRK(0x17aa, 0x20f2, "Lenovo X200", CXT5051_LENOVO_X200),
1856         {}
1857 };
1858
1859 static int patch_cxt5051(struct hda_codec *codec)
1860 {
1861         struct conexant_spec *spec;
1862         int board_config;
1863
1864         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1865         if (!spec)
1866                 return -ENOMEM;
1867         codec->spec = spec;
1868         codec->pin_amp_workaround = 1;
1869
1870         codec->patch_ops = conexant_patch_ops;
1871         codec->patch_ops.init = cxt5051_init;
1872
1873         spec->multiout.max_channels = 2;
1874         spec->multiout.num_dacs = ARRAY_SIZE(cxt5051_dac_nids);
1875         spec->multiout.dac_nids = cxt5051_dac_nids;
1876         spec->multiout.dig_out_nid = CXT5051_SPDIF_OUT;
1877         spec->num_adc_nids = 1; /* not 2; via auto-mic switch */
1878         spec->adc_nids = cxt5051_adc_nids;
1879         spec->num_mixers = 1;
1880         spec->mixers[0] = cxt5051_mixers;
1881         spec->num_init_verbs = 1;
1882         spec->init_verbs[0] = cxt5051_init_verbs;
1883         spec->spdif_route = 0;
1884         spec->num_channel_mode = ARRAY_SIZE(cxt5051_modes);
1885         spec->channel_mode = cxt5051_modes;
1886         spec->cur_adc = 0;
1887         spec->cur_adc_idx = 0;
1888
1889         codec->patch_ops.unsol_event = cxt5051_hp_unsol_event;
1890
1891         board_config = snd_hda_check_board_config(codec, CXT5051_MODELS,
1892                                                   cxt5051_models,
1893                                                   cxt5051_cfg_tbl);
1894         switch (board_config) {
1895         case CXT5051_HP:
1896                 spec->mixers[0] = cxt5051_hp_mixers;
1897                 break;
1898         case CXT5051_HP_DV6736:
1899                 spec->init_verbs[0] = cxt5051_hp_dv6736_init_verbs;
1900                 spec->mixers[0] = cxt5051_hp_dv6736_mixers;
1901                 spec->no_auto_mic = 1;
1902                 break;
1903         case CXT5051_LENOVO_X200:
1904                 spec->init_verbs[0] = cxt5051_lenovo_x200_init_verbs;
1905                 break;
1906         }
1907
1908         return 0;
1909 }
1910
1911
1912 /*
1913  */
1914
1915 static struct hda_codec_preset snd_hda_preset_conexant[] = {
1916         { .id = 0x14f15045, .name = "CX20549 (Venice)",
1917           .patch = patch_cxt5045 },
1918         { .id = 0x14f15047, .name = "CX20551 (Waikiki)",
1919           .patch = patch_cxt5047 },
1920         { .id = 0x14f15051, .name = "CX20561 (Hermosa)",
1921           .patch = patch_cxt5051 },
1922         {} /* terminator */
1923 };
1924
1925 MODULE_ALIAS("snd-hda-codec-id:14f15045");
1926 MODULE_ALIAS("snd-hda-codec-id:14f15047");
1927 MODULE_ALIAS("snd-hda-codec-id:14f15051");
1928
1929 MODULE_LICENSE("GPL");
1930 MODULE_DESCRIPTION("Conexant HD-audio codec");
1931
1932 static struct hda_codec_preset_list conexant_list = {
1933         .preset = snd_hda_preset_conexant,
1934         .owner = THIS_MODULE,
1935 };
1936
1937 static int __init patch_conexant_init(void)
1938 {
1939         return snd_hda_add_codec_preset(&conexant_list);
1940 }
1941
1942 static void __exit patch_conexant_exit(void)
1943 {
1944         snd_hda_delete_codec_preset(&conexant_list);
1945 }
1946
1947 module_init(patch_conexant_init)
1948 module_exit(patch_conexant_exit)