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