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