Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ieee1394...
[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 <sound/driver.h>
24 #include <linux/init.h>
25 #include <linux/delay.h>
26 #include <linux/slab.h>
27 #include <linux/pci.h>
28 #include <sound/core.h>
29 #include "hda_codec.h"
30 #include "hda_local.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 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         /* capture source */
68         const struct hda_input_mux *input_mux;
69         hda_nid_t *capsrc_nids;
70         unsigned int cur_mux[3];
71
72         /* channel model */
73         const struct hda_channel_mode *channel_mode;
74         int num_channel_mode;
75
76         /* PCM information */
77         struct hda_pcm pcm_rec[2];      /* used in build_pcms() */
78
79         struct mutex amp_mutex; /* PCM volume/mute control mutex */
80         unsigned int spdif_route;
81
82         /* dynamic controls, init_verbs and input_mux */
83         struct auto_pin_cfg autocfg;
84         unsigned int num_kctl_alloc, num_kctl_used;
85         struct snd_kcontrol_new *kctl_alloc;
86         struct hda_input_mux private_imux;
87         hda_nid_t private_dac_nids[4];
88
89 };
90
91 static int conexant_playback_pcm_open(struct hda_pcm_stream *hinfo,
92                                       struct hda_codec *codec,
93                                       struct snd_pcm_substream *substream)
94 {
95         struct conexant_spec *spec = codec->spec;
96         return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream);
97 }
98
99 static int conexant_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
100                                          struct hda_codec *codec,
101                                          unsigned int stream_tag,
102                                          unsigned int format,
103                                          struct snd_pcm_substream *substream)
104 {
105         struct conexant_spec *spec = codec->spec;
106         return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
107                                                 stream_tag,
108                                                 format, substream);
109 }
110
111 static int conexant_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
112                                          struct hda_codec *codec,
113                                          struct snd_pcm_substream *substream)
114 {
115         struct conexant_spec *spec = codec->spec;
116         return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
117 }
118
119 /*
120  * Digital out
121  */
122 static int conexant_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
123                                           struct hda_codec *codec,
124                                           struct snd_pcm_substream *substream)
125 {
126         struct conexant_spec *spec = codec->spec;
127         return snd_hda_multi_out_dig_open(codec, &spec->multiout);
128 }
129
130 static int conexant_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
131                                          struct hda_codec *codec,
132                                          struct snd_pcm_substream *substream)
133 {
134         struct conexant_spec *spec = codec->spec;
135         return snd_hda_multi_out_dig_close(codec, &spec->multiout);
136 }
137
138 /*
139  * Analog capture
140  */
141 static int conexant_capture_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         snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
149                                    stream_tag, 0, format);
150         return 0;
151 }
152
153 static int conexant_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
154                                       struct hda_codec *codec,
155                                       struct snd_pcm_substream *substream)
156 {
157         struct conexant_spec *spec = codec->spec;
158         snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
159                                    0, 0, 0);
160         return 0;
161 }
162
163
164
165 static struct hda_pcm_stream conexant_pcm_analog_playback = {
166         .substreams = 1,
167         .channels_min = 2,
168         .channels_max = 2,
169         .nid = 0, /* fill later */
170         .ops = {
171                 .open = conexant_playback_pcm_open,
172                 .prepare = conexant_playback_pcm_prepare,
173                 .cleanup = conexant_playback_pcm_cleanup
174         },
175 };
176
177 static struct hda_pcm_stream conexant_pcm_analog_capture = {
178         .substreams = 1,
179         .channels_min = 2,
180         .channels_max = 2,
181         .nid = 0, /* fill later */
182         .ops = {
183                 .prepare = conexant_capture_pcm_prepare,
184                 .cleanup = conexant_capture_pcm_cleanup
185         },
186 };
187
188
189 static struct hda_pcm_stream conexant_pcm_digital_playback = {
190         .substreams = 1,
191         .channels_min = 2,
192         .channels_max = 2,
193         .nid = 0, /* fill later */
194         .ops = {
195                 .open = conexant_dig_playback_pcm_open,
196                 .close = conexant_dig_playback_pcm_close
197         },
198 };
199
200 static struct hda_pcm_stream conexant_pcm_digital_capture = {
201         .substreams = 1,
202         .channels_min = 2,
203         .channels_max = 2,
204         /* NID is set in alc_build_pcms */
205 };
206
207 static int conexant_build_pcms(struct hda_codec *codec)
208 {
209         struct conexant_spec *spec = codec->spec;
210         struct hda_pcm *info = spec->pcm_rec;
211
212         codec->num_pcms = 1;
213         codec->pcm_info = info;
214
215         info->name = "CONEXANT Analog";
216         info->stream[SNDRV_PCM_STREAM_PLAYBACK] = conexant_pcm_analog_playback;
217         info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
218                 spec->multiout.max_channels;
219         info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
220                 spec->multiout.dac_nids[0];
221         info->stream[SNDRV_PCM_STREAM_CAPTURE] = conexant_pcm_analog_capture;
222         info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = spec->num_adc_nids;
223         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
224
225         if (spec->multiout.dig_out_nid) {
226                 info++;
227                 codec->num_pcms++;
228                 info->name = "Conexant Digital";
229                 info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
230                         conexant_pcm_digital_playback;
231                 info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
232                         spec->multiout.dig_out_nid;
233                 if (spec->dig_in_nid) {
234                         info->stream[SNDRV_PCM_STREAM_CAPTURE] =
235                                 conexant_pcm_digital_capture;
236                         info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
237                                 spec->dig_in_nid;
238                 }
239         }
240
241         return 0;
242 }
243
244 static int conexant_mux_enum_info(struct snd_kcontrol *kcontrol,
245                                   struct snd_ctl_elem_info *uinfo)
246 {
247         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
248         struct conexant_spec *spec = codec->spec;
249
250         return snd_hda_input_mux_info(spec->input_mux, uinfo);
251 }
252
253 static int conexant_mux_enum_get(struct snd_kcontrol *kcontrol,
254                                  struct snd_ctl_elem_value *ucontrol)
255 {
256         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
257         struct conexant_spec *spec = codec->spec;
258         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
259
260         ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
261         return 0;
262 }
263
264 static int conexant_mux_enum_put(struct snd_kcontrol *kcontrol,
265                                  struct snd_ctl_elem_value *ucontrol)
266 {
267         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
268         struct conexant_spec *spec = codec->spec;
269         unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
270
271         return snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
272                                      spec->capsrc_nids[adc_idx],
273                                      &spec->cur_mux[adc_idx]);
274 }
275
276 static int conexant_init(struct hda_codec *codec)
277 {
278         struct conexant_spec *spec = codec->spec;
279         int i;
280
281         for (i = 0; i < spec->num_init_verbs; i++)
282                 snd_hda_sequence_write(codec, spec->init_verbs[i]);
283         return 0;
284 }
285
286 static void conexant_free(struct hda_codec *codec)
287 {
288         struct conexant_spec *spec = codec->spec;
289         unsigned int i;
290
291         if (spec->kctl_alloc) {
292                 for (i = 0; i < spec->num_kctl_used; i++)
293                         kfree(spec->kctl_alloc[i].name);
294                 kfree(spec->kctl_alloc);
295         }
296
297         kfree(codec->spec);
298 }
299
300 #ifdef CONFIG_PM
301 static int conexant_resume(struct hda_codec *codec)
302 {
303         struct conexant_spec *spec = codec->spec;
304         int i;
305
306         codec->patch_ops.init(codec);
307         for (i = 0; i < spec->num_mixers; i++)
308                 snd_hda_resume_ctls(codec, spec->mixers[i]);
309         if (spec->multiout.dig_out_nid)
310                 snd_hda_resume_spdif_out(codec);
311         if (spec->dig_in_nid)
312                 snd_hda_resume_spdif_in(codec);
313         return 0;
314 }
315 #endif
316
317 static int conexant_build_controls(struct hda_codec *codec)
318 {
319         struct conexant_spec *spec = codec->spec;
320         unsigned int i;
321         int err;
322
323         for (i = 0; i < spec->num_mixers; i++) {
324                 err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
325                 if (err < 0)
326                         return err;
327         }
328         if (spec->multiout.dig_out_nid) {
329                 err = snd_hda_create_spdif_out_ctls(codec,
330                                                     spec->multiout.dig_out_nid);
331                 if (err < 0)
332                         return err;
333         } 
334         if (spec->dig_in_nid) {
335                 err = snd_hda_create_spdif_in_ctls(codec,spec->dig_in_nid);
336                 if (err < 0)
337                         return err;
338         }
339         return 0;
340 }
341
342 static struct hda_codec_ops conexant_patch_ops = {
343         .build_controls = conexant_build_controls,
344         .build_pcms = conexant_build_pcms,
345         .init = conexant_init,
346         .free = conexant_free,
347 #ifdef CONFIG_PM
348         .resume = conexant_resume,
349 #endif
350 };
351
352 /*
353  * EAPD control
354  * the private value = nid | (invert << 8)
355  */
356
357 static int conexant_eapd_info(struct snd_kcontrol *kcontrol,
358                               struct snd_ctl_elem_info *uinfo)
359 {
360         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
361         uinfo->count = 1;
362         uinfo->value.integer.min = 0;
363         uinfo->value.integer.max = 1;
364         return 0;
365 }
366
367 static int conexant_eapd_get(struct snd_kcontrol *kcontrol,
368                              struct snd_ctl_elem_value *ucontrol)
369 {
370         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
371         struct conexant_spec *spec = codec->spec;
372         int invert = (kcontrol->private_value >> 8) & 1;
373         if (invert)
374                 ucontrol->value.integer.value[0] = !spec->cur_eapd;
375         else
376                 ucontrol->value.integer.value[0] = spec->cur_eapd;
377         return 0;
378 }
379
380 static int conexant_eapd_put(struct snd_kcontrol *kcontrol,
381                              struct snd_ctl_elem_value *ucontrol)
382 {
383         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
384         struct conexant_spec *spec = codec->spec;
385         int invert = (kcontrol->private_value >> 8) & 1;
386         hda_nid_t nid = kcontrol->private_value & 0xff;
387         unsigned int eapd;
388         eapd = ucontrol->value.integer.value[0];
389         if (invert)
390                 eapd = !eapd;
391         if (eapd == spec->cur_eapd && !codec->in_resume)
392                 return 0;
393         spec->cur_eapd = eapd;
394         snd_hda_codec_write(codec, nid,
395                             0, AC_VERB_SET_EAPD_BTLENABLE,
396                             eapd ? 0x02 : 0x00);
397         return 1;
398 }
399
400 /* controls for test mode */
401 #ifdef CONFIG_SND_DEBUG
402
403 static int conexant_ch_mode_info(struct snd_kcontrol *kcontrol,
404                                  struct snd_ctl_elem_info *uinfo)
405 {
406         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
407         struct conexant_spec *spec = codec->spec;
408         return snd_hda_ch_mode_info(codec, uinfo, spec->channel_mode,
409                                     spec->num_channel_mode);
410 }
411
412 static int conexant_ch_mode_get(struct snd_kcontrol *kcontrol,
413                                 struct snd_ctl_elem_value *ucontrol)
414 {
415         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
416         struct conexant_spec *spec = codec->spec;
417         return snd_hda_ch_mode_get(codec, ucontrol, spec->channel_mode,
418                                    spec->num_channel_mode,
419                                    spec->multiout.max_channels);
420 }
421
422 static int conexant_ch_mode_put(struct snd_kcontrol *kcontrol,
423                                 struct snd_ctl_elem_value *ucontrol)
424 {
425         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
426         struct conexant_spec *spec = codec->spec;
427         int err = snd_hda_ch_mode_put(codec, ucontrol, spec->channel_mode,
428                                       spec->num_channel_mode,
429                                       &spec->multiout.max_channels);
430         if (err >= 0 && spec->need_dac_fix)
431                 spec->multiout.num_dacs = spec->multiout.max_channels / 2;
432         return err;
433 }
434
435 #define CXT_PIN_MODE(xname, nid, dir) \
436         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = 0,  \
437           .info = conexant_ch_mode_info, \
438           .get = conexant_ch_mode_get, \
439           .put = conexant_ch_mode_put, \
440           .private_value = nid | (dir<<16) }
441
442 static int cxt_gpio_data_info(struct snd_kcontrol *kcontrol,
443                               struct snd_ctl_elem_info *uinfo)
444 {
445         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
446         uinfo->count = 1;
447         uinfo->value.integer.min = 0;
448         uinfo->value.integer.max = 1;
449         return 0;
450 }                                
451
452 static int cxt_gpio_data_get(struct snd_kcontrol *kcontrol,
453                              struct snd_ctl_elem_value *ucontrol)
454 {
455         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
456         hda_nid_t nid = kcontrol->private_value & 0xffff;
457         unsigned char mask = (kcontrol->private_value >> 16) & 0xff;
458         long *valp = ucontrol->value.integer.value;
459         unsigned int val = snd_hda_codec_read(codec, nid, 0,
460                                               AC_VERB_GET_GPIO_DATA, 0x00);
461
462         *valp = (val & mask) != 0;
463         return 0;
464 }
465
466 static int cxt_gpio_data_put(struct snd_kcontrol *kcontrol,
467                              struct snd_ctl_elem_value *ucontrol)
468 {
469         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
470         hda_nid_t nid = kcontrol->private_value & 0xffff;
471         unsigned char mask = (kcontrol->private_value >> 16) & 0xff;
472         long val = *ucontrol->value.integer.value;
473         unsigned int gpio_data = snd_hda_codec_read(codec, nid, 0,
474                                                     AC_VERB_GET_GPIO_DATA,
475                                                     0x00);
476         unsigned int old_data = gpio_data;
477
478         /* Set/unset the masked GPIO bit(s) as needed */
479         if (val == 0)
480                 gpio_data &= ~mask;
481         else
482                 gpio_data |= mask;
483         if (gpio_data == old_data && !codec->in_resume)
484                 return 0;
485         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_GPIO_DATA, gpio_data);
486         return 1;
487 }
488
489 #define CXT_GPIO_DATA_SWITCH(xname, nid, mask) \
490         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = 0,  \
491           .info = cxt_gpio_data_info, \
492           .get = cxt_gpio_data_get, \
493           .put = cxt_gpio_data_put, \
494           .private_value = nid | (mask<<16) }
495
496 static int cxt_spdif_ctrl_info(struct snd_kcontrol *kcontrol,
497                                struct snd_ctl_elem_info *uinfo)
498 {
499         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
500         uinfo->count = 1;
501         uinfo->value.integer.min = 0;
502         uinfo->value.integer.max = 1;
503         return 0;
504 }                                
505
506 static int cxt_spdif_ctrl_get(struct snd_kcontrol *kcontrol,
507                               struct snd_ctl_elem_value *ucontrol)
508 {
509         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
510         hda_nid_t nid = kcontrol->private_value & 0xffff;
511         unsigned char mask = (kcontrol->private_value >> 16) & 0xff;
512         long *valp = ucontrol->value.integer.value;
513         unsigned int val = snd_hda_codec_read(codec, nid, 0,
514                                               AC_VERB_GET_DIGI_CONVERT, 0x00);
515
516         *valp = (val & mask) != 0;
517         return 0;
518 }
519
520 static int cxt_spdif_ctrl_put(struct snd_kcontrol *kcontrol,
521                               struct snd_ctl_elem_value *ucontrol)
522 {
523         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
524         hda_nid_t nid = kcontrol->private_value & 0xffff;
525         unsigned char mask = (kcontrol->private_value >> 16) & 0xff;
526         long val = *ucontrol->value.integer.value;
527         unsigned int ctrl_data = snd_hda_codec_read(codec, nid, 0,
528                                                     AC_VERB_GET_DIGI_CONVERT,
529                                                     0x00);
530         unsigned int old_data = ctrl_data;
531
532         /* Set/unset the masked control bit(s) as needed */
533         if (val == 0)
534                 ctrl_data &= ~mask;
535         else
536                 ctrl_data |= mask;
537         if (ctrl_data == old_data && !codec->in_resume)
538                 return 0;
539         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1,
540                             ctrl_data);
541         return 1;
542 }
543
544 #define CXT_SPDIF_CTRL_SWITCH(xname, nid, mask) \
545         { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = 0,  \
546           .info = cxt_spdif_ctrl_info, \
547           .get = cxt_spdif_ctrl_get, \
548           .put = cxt_spdif_ctrl_put, \
549           .private_value = nid | (mask<<16) }
550
551 #endif /* CONFIG_SND_DEBUG */
552
553 /* Conexant 5045 specific */
554
555 static hda_nid_t cxt5045_dac_nids[1] = { 0x19 };
556 static hda_nid_t cxt5045_adc_nids[1] = { 0x1a };
557 static hda_nid_t cxt5045_capsrc_nids[1] = { 0x1a };
558 #define CXT5045_SPDIF_OUT       0x13
559
560 static struct hda_channel_mode cxt5045_modes[1] = {
561         { 2, NULL },
562 };
563
564 static struct hda_input_mux cxt5045_capture_source = {
565         .num_items = 2,
566         .items = {
567                 { "ExtMic", 0x1 },
568                 { "LineIn", 0x2 },
569         }
570 };
571
572 /* turn on/off EAPD (+ mute HP) as a master switch */
573 static int cxt5045_hp_master_sw_put(struct snd_kcontrol *kcontrol,
574                                     struct snd_ctl_elem_value *ucontrol)
575 {
576         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
577         struct conexant_spec *spec = codec->spec;
578
579         if (!conexant_eapd_put(kcontrol, ucontrol))
580                 return 0;
581
582         /* toggle HP mute appropriately */
583         snd_hda_codec_amp_update(codec, 0x11, 0, HDA_OUTPUT, 0,
584                                  0x80, spec->cur_eapd ? 0 : 0x80);
585         snd_hda_codec_amp_update(codec, 0x11, 1, HDA_OUTPUT, 0,
586                                  0x80, spec->cur_eapd ? 0 : 0x80);
587         return 1;
588 }
589
590 /* bind volumes of both NID 0x10 and 0x11 */
591 static int cxt5045_hp_master_vol_put(struct snd_kcontrol *kcontrol,
592                                      struct snd_ctl_elem_value *ucontrol)
593 {
594         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
595         long *valp = ucontrol->value.integer.value;
596         int change;
597
598         change = snd_hda_codec_amp_update(codec, 0x10, 0, HDA_OUTPUT, 0,
599                                           0x7f, valp[0] & 0x7f);
600         change |= snd_hda_codec_amp_update(codec, 0x10, 1, HDA_OUTPUT, 0,
601                                            0x7f, valp[1] & 0x7f);
602         snd_hda_codec_amp_update(codec, 0x11, 0, HDA_OUTPUT, 0,
603                                  0x7f, valp[0] & 0x7f);
604         snd_hda_codec_amp_update(codec, 0x11, 1, HDA_OUTPUT, 0,
605                                  0x7f, valp[1] & 0x7f);
606         return change;
607 }
608
609
610 /* mute internal speaker if HP is plugged */
611 static void cxt5045_hp_automute(struct hda_codec *codec)
612 {
613         unsigned int present;
614
615         present = snd_hda_codec_read(codec, 0x11, 0,
616                                      AC_VERB_GET_PIN_SENSE, 0) & 0x80000000;
617         snd_hda_codec_amp_update(codec, 0x10, 0, HDA_OUTPUT, 0,
618                                  0x80, present ? 0x80 : 0);
619         snd_hda_codec_amp_update(codec, 0x10, 1, HDA_OUTPUT, 0,
620                                  0x80, present ? 0x80 : 0);
621 }
622
623 /* unsolicited event for HP jack sensing */
624 static void cxt5045_hp_unsol_event(struct hda_codec *codec,
625                                    unsigned int res)
626 {
627         res >>= 26;
628         switch (res) {
629         case CONEXANT_HP_EVENT:
630                 cxt5045_hp_automute(codec);
631                 break;
632         }
633 }
634
635 static struct snd_kcontrol_new cxt5045_mixers[] = {
636         {
637                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
638                 .name = "Capture Source",
639                 .info = conexant_mux_enum_info,
640                 .get = conexant_mux_enum_get,
641                 .put = conexant_mux_enum_put
642         },
643         HDA_CODEC_VOLUME("Mic Bypass Capture Volume", 0x17, 0x02, HDA_INPUT),
644         HDA_CODEC_MUTE("Mic Bypass Capture Switch", 0x17, 0x02, HDA_INPUT),
645         HDA_CODEC_VOLUME("Capture Volume", 0x1a, 0x02, HDA_INPUT),
646         HDA_CODEC_MUTE("Capture Switch", 0x1a, 0x02, HDA_INPUT),
647         {
648                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
649                 .name = "Master Playback Volume",
650                 .info = snd_hda_mixer_amp_volume_info,
651                 .get = snd_hda_mixer_amp_volume_get,
652                 .put = cxt5045_hp_master_vol_put,
653                 .private_value = HDA_COMPOSE_AMP_VAL(0x11, 3, 0, HDA_OUTPUT),
654         },
655         {
656                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
657                 .name = "Master Playback Switch",
658                 .info = conexant_eapd_info,
659                 .get = conexant_eapd_get,
660                 .put = cxt5045_hp_master_sw_put,
661                 .private_value = 0x11,
662         },
663
664         {}
665 };
666
667 static struct hda_verb cxt5045_init_verbs[] = {
668         /* Line in, Mic */
669         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
670         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN|AC_PINCTL_VREF_50 },
671         /* HP, Amp  */
672         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
673         {0x1A, AC_VERB_SET_CONNECT_SEL,0x01},
674         {0x1A, AC_VERB_SET_AMP_GAIN_MUTE,
675          AC_AMP_SET_OUTPUT|AC_AMP_SET_RIGHT|AC_AMP_SET_LEFT|0x00},
676         {0x1A, AC_VERB_SET_AMP_GAIN_MUTE,
677          AC_AMP_SET_OUTPUT|AC_AMP_SET_RIGHT|AC_AMP_SET_LEFT|0x03},
678         /* Record selector: Front mic */
679         {0x14, AC_VERB_SET_CONNECT_SEL,0x03},
680         {0x17, AC_VERB_SET_AMP_GAIN_MUTE,
681          AC_AMP_SET_INPUT|AC_AMP_SET_RIGHT|AC_AMP_SET_LEFT|0x17},
682         /* SPDIF route: PCM */
683         { 0x13, AC_VERB_SET_CONNECT_SEL, 0x0 },
684         /* pin sensing on HP and Mic jacks */
685         {0x11, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_HP_EVENT},
686         /* EAPD */
687         {0x10, AC_VERB_SET_EAPD_BTLENABLE, 0x0 }, /* default on */
688         { } /* end */
689 };
690
691 #ifdef CONFIG_SND_DEBUG
692 /* Test configuration for debugging, modelled after the ALC260 test
693  * configuration.
694  */
695 static struct hda_input_mux cxt5045_test_capture_source = {
696         .num_items = 5,
697         .items = {
698                 { "MIXER", 0x0 },
699                 { "MIC1 pin", 0x1 },
700                 { "LINE1 pin", 0x2 },
701                 { "HP-OUT pin", 0x3 },
702                 { "CD pin", 0x4 },
703         },
704 };
705
706 static struct snd_kcontrol_new cxt5045_test_mixer[] = {
707
708         /* Output controls */
709         HDA_CODEC_VOLUME("OutAmp-1 Volume", 0x19, 0x00, HDA_OUTPUT),
710         HDA_CODEC_MUTE("OutAmp-1 Switch", 0x19,0x00, HDA_OUTPUT),
711         HDA_CODEC_VOLUME("Speaker Playback Volume", 0x10, 0x0, HDA_OUTPUT),
712         HDA_CODEC_MUTE("Speaker Playback Switch", 0x10, 0x0, HDA_OUTPUT),
713         
714         /* Modes for retasking pin widgets */
715         CXT_PIN_MODE("HP-OUT pin mode", 0x11, CXT_PIN_DIR_INOUT),
716         CXT_PIN_MODE("LINE1 pin mode", 0x12, CXT_PIN_DIR_INOUT),
717
718         /* Loopback mixer controls */
719         HDA_CODEC_VOLUME("MIC1 Playback Volume", 0x17, 0x01, HDA_INPUT),
720         HDA_CODEC_MUTE("MIC1 Playback Switch", 0x17, 0x01, HDA_INPUT),
721         HDA_CODEC_VOLUME("LINE loopback Playback Volume", 0x17, 0x02, HDA_INPUT),
722         HDA_CODEC_MUTE("LINE loopback Playback Switch", 0x17, 0x02, HDA_INPUT),
723         HDA_CODEC_VOLUME("HP-OUT loopback Playback Volume", 0x17, 0x03, HDA_INPUT),
724         HDA_CODEC_MUTE("HP-OUT loopback Playback Switch", 0x17, 0x03, HDA_INPUT),
725         HDA_CODEC_VOLUME("CD Playback Volume", 0x17, 0x04, HDA_INPUT),
726         HDA_CODEC_MUTE("CD Playback Switch", 0x17, 0x04, HDA_INPUT),
727
728         /* Controls for GPIO pins, assuming they exist and are configured as outputs */
729         CXT_GPIO_DATA_SWITCH("GPIO pin 0", 0x01, 0x01),
730 #if 0   /* limit this to one GPIO pin for now */        
731         CXT_GPIO_DATA_SWITCH("GPIO pin 1", 0x01, 0x02),
732         CXT_GPIO_DATA_SWITCH("GPIO pin 2", 0x01, 0x04),
733         CXT_GPIO_DATA_SWITCH("GPIO pin 3", 0x01, 0x08),
734 #endif
735         CXT_SPDIF_CTRL_SWITCH("SPDIF Playback Switch", 0x13, 0x01),
736
737         HDA_CODEC_VOLUME("Capture Volume", 0x17, 0x0, HDA_OUTPUT),
738         HDA_CODEC_MUTE("Capture Switch", 0x17, 0x0, HDA_OUTPUT),
739         {
740                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
741                 .name = "Input Source",
742                 .info = conexant_mux_enum_info,
743                 .get = conexant_mux_enum_get,
744                 .put = conexant_mux_enum_put,
745         },
746
747         { } /* end */
748 };
749
750 static struct hda_verb cxt5045_test_init_verbs[] = {
751         /* Enable all GPIOs as outputs with an initial value of 0 */
752         {0x01, AC_VERB_SET_GPIO_DIRECTION, 0x0f},
753         {0x01, AC_VERB_SET_GPIO_DATA, 0x00},
754         {0x01, AC_VERB_SET_GPIO_MASK, 0x0f},
755
756         /* Enable retasking pins as output, initially without power amp */
757         {0x12, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
758         {0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
759
760         /* Disable digital (SPDIF) pins initially, but users can enable
761          * them via a mixer switch.  In the case of SPDIF-out, this initverb
762          * payload also sets the generation to 0, output to be in "consumer"
763          * PCM format, copyright asserted, no pre-emphasis and no validity
764          * control.
765          */
766         {0x13, AC_VERB_SET_DIGI_CONVERT_1, 0},
767
768         /* Start with output sum widgets muted and their output gains at min */
769         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
770         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
771
772         /* Unmute retasking pin widget output buffers since the default
773          * state appears to be output.  As the pin mode is changed by the
774          * user the pin mode control will take care of enabling the pin's
775          * input/output buffers as needed.
776          */
777         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
778         {0x11, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
779
780         /* Mute capture amp left and right */
781         {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
782
783         /* Set ADC connection select to match default mixer setting (mic1
784          * pin)
785          */
786         {0x1a, AC_VERB_SET_CONNECT_SEL, 0x00},
787
788         /* Mute all inputs to mixer widget (even unconnected ones) */
789         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)}, /* Mixer pin */
790         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)}, /* Mic1 pin */
791         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)}, /* Line pin */
792         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)}, /* HP pin */
793         {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)}, /* CD pin */
794
795         { }
796 };
797 #endif
798
799
800 /* initialize jack-sensing, too */
801 static int cxt5045_init(struct hda_codec *codec)
802 {
803         conexant_init(codec);
804         cxt5045_hp_automute(codec);
805         return 0;
806 }
807
808
809 enum {
810         CXT5045_LAPTOP, /* Laptops w/ EAPD support */
811 #ifdef CONFIG_SND_DEBUG
812         CXT5045_TEST,
813 #endif
814         CXT5045_MODELS
815 };
816
817 static const char *cxt5045_models[CXT5045_MODELS] = {
818         [CXT5045_LAPTOP]        = "laptop",
819 #ifdef CONFIG_SND_DEBUG
820         [CXT5045_TEST]          = "test",
821 #endif
822 };
823
824 static struct snd_pci_quirk cxt5045_cfg_tbl[] = {
825         SND_PCI_QUIRK(0x103c, 0x30b7, "HP DV6000Z", CXT5045_LAPTOP),
826         {}
827 };
828
829 static int patch_cxt5045(struct hda_codec *codec)
830 {
831         struct conexant_spec *spec;
832         int board_config;
833
834         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
835         if (!spec)
836                 return -ENOMEM;
837         mutex_init(&spec->amp_mutex);
838         codec->spec = spec;
839
840         spec->multiout.max_channels = 2;
841         spec->multiout.num_dacs = ARRAY_SIZE(cxt5045_dac_nids);
842         spec->multiout.dac_nids = cxt5045_dac_nids;
843         spec->multiout.dig_out_nid = CXT5045_SPDIF_OUT;
844         spec->num_adc_nids = 1;
845         spec->adc_nids = cxt5045_adc_nids;
846         spec->capsrc_nids = cxt5045_capsrc_nids;
847         spec->input_mux = &cxt5045_capture_source;
848         spec->num_mixers = 1;
849         spec->mixers[0] = cxt5045_mixers;
850         spec->num_init_verbs = 1;
851         spec->init_verbs[0] = cxt5045_init_verbs;
852         spec->spdif_route = 0;
853         spec->num_channel_mode = ARRAY_SIZE(cxt5045_modes),
854         spec->channel_mode = cxt5045_modes,
855
856
857         codec->patch_ops = conexant_patch_ops;
858         codec->patch_ops.unsol_event = cxt5045_hp_unsol_event;
859
860         board_config = snd_hda_check_board_config(codec, CXT5045_MODELS,
861                                                   cxt5045_models,
862                                                   cxt5045_cfg_tbl);
863         switch (board_config) {
864         case CXT5045_LAPTOP:
865                 spec->input_mux = &cxt5045_capture_source;
866                 spec->num_init_verbs = 2;
867                 spec->init_verbs[1] = cxt5045_init_verbs;
868                 spec->mixers[0] = cxt5045_mixers;
869                 codec->patch_ops.init = cxt5045_init;
870                 break;
871 #ifdef CONFIG_SND_DEBUG
872         case CXT5045_TEST:
873                 spec->input_mux = &cxt5045_test_capture_source;
874                 spec->mixers[0] = cxt5045_test_mixer;
875                 spec->init_verbs[0] = cxt5045_test_init_verbs;
876 #endif  
877         }
878         return 0;
879 }
880
881
882 /* Conexant 5047 specific */
883
884 static hda_nid_t cxt5047_dac_nids[1] = { 0x10 };
885 static hda_nid_t cxt5047_adc_nids[1] = { 0x12 };
886 static hda_nid_t cxt5047_capsrc_nids[1] = { 0x1a };
887 #define CXT5047_SPDIF_OUT       0x11
888
889 static struct hda_channel_mode cxt5047_modes[1] = {
890         { 2, NULL },
891 };
892
893 static struct hda_input_mux cxt5047_capture_source = {
894         .num_items = 2,
895         .items = {
896                 { "ExtMic", 0x1 },
897                 { "IntMic", 0x2 },
898         }
899 };
900
901 static struct hda_input_mux cxt5047_hp_capture_source = {
902         .num_items = 1,
903         .items = {
904                 { "ExtMic", 0x1 },
905         }
906 };
907
908 /* turn on/off EAPD (+ mute HP) as a master switch */
909 static int cxt5047_hp_master_sw_put(struct snd_kcontrol *kcontrol,
910                                     struct snd_ctl_elem_value *ucontrol)
911 {
912         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
913         struct conexant_spec *spec = codec->spec;
914
915         if (!conexant_eapd_put(kcontrol, ucontrol))
916                 return 0;
917
918         /* toggle HP mute appropriately */
919         snd_hda_codec_amp_update(codec, 0x13, 0, HDA_OUTPUT, 0,
920                                  0x80, spec->cur_eapd ? 0 : 0x80);
921         snd_hda_codec_amp_update(codec, 0x13, 1, HDA_OUTPUT, 0,
922                                  0x80, spec->cur_eapd ? 0 : 0x80);
923         return 1;
924 }
925
926 #if 0
927 /* bind volumes of both NID 0x13 and 0x1d */
928 static int cxt5047_hp_master_vol_put(struct snd_kcontrol *kcontrol,
929                                      struct snd_ctl_elem_value *ucontrol)
930 {
931         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
932         long *valp = ucontrol->value.integer.value;
933         int change;
934
935         change = snd_hda_codec_amp_update(codec, 0x1c, 0, HDA_OUTPUT, 0,
936                                           0x7f, valp[0] & 0x7f);
937         change |= snd_hda_codec_amp_update(codec, 0x1c, 1, HDA_OUTPUT, 0,
938                                            0x7f, valp[1] & 0x7f);
939         snd_hda_codec_amp_update(codec, 0x13, 0, HDA_OUTPUT, 0,
940                                  0x7f, valp[0] & 0x7f);
941         snd_hda_codec_amp_update(codec, 0x13, 1, HDA_OUTPUT, 0,
942                                  0x7f, valp[1] & 0x7f);
943         return change;
944 }
945 #endif
946
947 /* mute internal speaker if HP is plugged */
948 static void cxt5047_hp_automute(struct hda_codec *codec)
949 {
950         unsigned int present;
951
952         present = snd_hda_codec_read(codec, 0x13, 0,
953                                      AC_VERB_GET_PIN_SENSE, 0) & 0x80000000;
954         snd_hda_codec_amp_update(codec, 0x1c, 0, HDA_OUTPUT, 0,
955                                  0x80, present ? 0x80 : 0);
956         snd_hda_codec_amp_update(codec, 0x1c, 1, HDA_OUTPUT, 0,
957                                  0x80, present ? 0x80 : 0);
958 }
959
960 /* toggle input of built-in and mic jack appropriately */
961 static void cxt5047_hp_automic(struct hda_codec *codec)
962 {
963         static struct hda_verb mic_jack_on[] = {
964                 {0x15, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
965                 {0x17, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
966                 {}
967         };
968         static struct hda_verb mic_jack_off[] = {
969                 {0x17, AC_VERB_SET_AMP_GAIN_MUTE, 0xb080},
970                 {0x15, AC_VERB_SET_AMP_GAIN_MUTE, 0xb000},
971                 {}
972         };
973         unsigned int present;
974
975         present = snd_hda_codec_read(codec, 0x08, 0,
976                                      AC_VERB_GET_PIN_SENSE, 0) & 0x80000000;
977         if (present)
978                 snd_hda_sequence_write(codec, mic_jack_on);
979         else
980                 snd_hda_sequence_write(codec, mic_jack_off);
981 }
982
983 /* unsolicited event for HP jack sensing */
984 static void cxt5047_hp_unsol_event(struct hda_codec *codec,
985                                   unsigned int res)
986 {
987         res >>= 26;
988         switch (res) {
989         case CONEXANT_HP_EVENT:
990                 cxt5047_hp_automute(codec);
991                 break;
992         case CONEXANT_MIC_EVENT:
993                 cxt5047_hp_automic(codec);
994                 break;
995         }
996 }
997
998 static struct snd_kcontrol_new cxt5047_mixers[] = {
999         {
1000                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1001                 .name = "Capture Source",
1002                 .info = conexant_mux_enum_info,
1003                 .get = conexant_mux_enum_get,
1004                 .put = conexant_mux_enum_put
1005         },
1006         HDA_CODEC_VOLUME("Mic Bypass Capture Volume", 0x19, 0x02, HDA_INPUT),
1007         HDA_CODEC_MUTE("Mic Bypass Capture Switch", 0x19, 0x02, HDA_INPUT),
1008         HDA_CODEC_VOLUME("Capture Volume", 0x12, 0x03, HDA_INPUT),
1009         HDA_CODEC_MUTE("Capture Switch", 0x12, 0x03, HDA_INPUT),
1010         HDA_CODEC_VOLUME("PCM Volume", 0x10, 0x00, HDA_OUTPUT),
1011         HDA_CODEC_MUTE("PCM Switch", 0x10, 0x00, HDA_OUTPUT),
1012         HDA_CODEC_VOLUME("Master Playback Volume", 0x13, 0x00, HDA_OUTPUT),
1013         {
1014                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1015                 .name = "Master Playback Switch",
1016                 .info = conexant_eapd_info,
1017                 .get = conexant_eapd_get,
1018                 .put = cxt5047_hp_master_sw_put,
1019                 .private_value = 0x13,
1020         },
1021
1022         {}
1023 };
1024
1025 static struct snd_kcontrol_new cxt5047_hp_mixers[] = {
1026         {
1027                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1028                 .name = "Capture Source",
1029                 .info = conexant_mux_enum_info,
1030                 .get = conexant_mux_enum_get,
1031                 .put = conexant_mux_enum_put
1032         },
1033         HDA_CODEC_VOLUME("Mic Bypass Capture Volume", 0x19, 0x02, HDA_INPUT),
1034         HDA_CODEC_MUTE("Mic Bypass Capture Switch", 0x19,0x02,HDA_INPUT),
1035         HDA_CODEC_VOLUME("Capture Volume", 0x12, 0x03, HDA_INPUT),
1036         HDA_CODEC_MUTE("Capture Switch", 0x12, 0x03, HDA_INPUT),
1037         HDA_CODEC_VOLUME("PCM Volume", 0x10, 0x00, HDA_OUTPUT),
1038         HDA_CODEC_MUTE("PCM Switch", 0x10, 0x00, HDA_OUTPUT),
1039         HDA_CODEC_VOLUME("Master Playback Volume", 0x13, 0x00, HDA_OUTPUT),
1040         {
1041                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1042                 .name = "Master Playback Switch",
1043                 .info = conexant_eapd_info,
1044                 .get = conexant_eapd_get,
1045                 .put = cxt5047_hp_master_sw_put,
1046                 .private_value = 0x13,
1047         },
1048         { } /* end */
1049 };
1050
1051 static struct hda_verb cxt5047_init_verbs[] = {
1052         /* Line in, Mic, Built-in Mic */
1053         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
1054         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN|AC_PINCTL_VREF_50 },
1055         {0x17, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN|AC_PINCTL_VREF_50 },
1056         /* HP, Amp  */
1057         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
1058         {0x1A, AC_VERB_SET_CONNECT_SEL,0x03},
1059         {0x1A, AC_VERB_SET_AMP_GAIN_MUTE,
1060          AC_AMP_SET_OUTPUT|AC_AMP_SET_RIGHT|AC_AMP_SET_LEFT|0x00},
1061         {0x1A, AC_VERB_SET_AMP_GAIN_MUTE,
1062          AC_AMP_SET_OUTPUT|AC_AMP_SET_RIGHT|AC_AMP_SET_LEFT|0x03},
1063         /* Record selector: Front mic */
1064         {0x12, AC_VERB_SET_CONNECT_SEL,0x03},
1065         {0x19, AC_VERB_SET_AMP_GAIN_MUTE,
1066          AC_AMP_SET_INPUT|AC_AMP_SET_RIGHT|AC_AMP_SET_LEFT|0x17},
1067         /* SPDIF route: PCM */
1068         { 0x18, AC_VERB_SET_CONNECT_SEL, 0x0 },
1069         { } /* end */
1070 };
1071
1072 /* configuration for Toshiba Laptops */
1073 static struct hda_verb cxt5047_toshiba_init_verbs[] = {
1074         {0x13, AC_VERB_SET_EAPD_BTLENABLE, 0x0 }, /* default on */
1075         /* pin sensing on HP and Mic jacks */
1076         {0x13, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_HP_EVENT},
1077         {0x15, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_MIC_EVENT},
1078         {}
1079 };
1080
1081 /* configuration for HP Laptops */
1082 static struct hda_verb cxt5047_hp_init_verbs[] = {
1083         /* pin sensing on HP and Mic jacks */
1084         {0x13, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_HP_EVENT},
1085         {0x15, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN | CONEXANT_MIC_EVENT},
1086         {}
1087 };
1088
1089 /* Test configuration for debugging, modelled after the ALC260 test
1090  * configuration.
1091  */
1092 #ifdef CONFIG_SND_DEBUG
1093 static struct hda_input_mux cxt5047_test_capture_source = {
1094         .num_items = 5,
1095         .items = {
1096                 { "MIXER", 0x0 },
1097                 { "LINE1 pin", 0x1 },
1098                 { "MIC1 pin", 0x2 },
1099                 { "MIC2 pin", 0x3 },
1100                 { "CD pin", 0x4 },
1101         },
1102 };
1103
1104 static struct snd_kcontrol_new cxt5047_test_mixer[] = {
1105
1106         /* Output only controls */
1107         HDA_CODEC_VOLUME("OutAmp-1 Volume", 0x10, 0x00, HDA_OUTPUT),
1108         HDA_CODEC_MUTE("OutAmp-1 Switch", 0x10,0x00, HDA_OUTPUT),
1109         HDA_CODEC_VOLUME("OutAmp-2 Volume", 0x1c, 0x00, HDA_OUTPUT),
1110         HDA_CODEC_MUTE("OutAmp-2 Switch", 0x1c, 0x00, HDA_OUTPUT),
1111         HDA_CODEC_VOLUME("Speaker Playback Volume", 0x1d, 0x0, HDA_OUTPUT),
1112         HDA_CODEC_MUTE("Speaker Playback Switch", 0x1d, 0x0, HDA_OUTPUT),
1113         HDA_CODEC_VOLUME("HeadPhone Playback Volume", 0x13, 0x0, HDA_OUTPUT),
1114         HDA_CODEC_MUTE("HeadPhone Playback Switch", 0x13, 0x0, HDA_OUTPUT),
1115
1116         /* Modes for retasking pin widgets */
1117         CXT_PIN_MODE("LINE1 pin mode", 0x14, CXT_PIN_DIR_INOUT),
1118         CXT_PIN_MODE("MIC1 pin mode", 0x15, CXT_PIN_DIR_INOUT),
1119
1120         /* Loopback mixer controls */
1121         HDA_CODEC_VOLUME("MIC1 Playback Volume", 0x19, 0x02, HDA_INPUT),
1122         HDA_CODEC_MUTE("MIC1 Playback Switch", 0x19, 0x02, HDA_INPUT),
1123         HDA_CODEC_VOLUME("MIC2 Playback Volume", 0x19, 0x03, HDA_INPUT),
1124         HDA_CODEC_MUTE("MIC2 Playback Switch", 0x19, 0x03, HDA_INPUT),
1125         HDA_CODEC_VOLUME("LINE Playback Volume", 0x19, 0x01, HDA_INPUT),
1126         HDA_CODEC_MUTE("LINE Playback Switch", 0x19, 0x01, HDA_INPUT),
1127         HDA_CODEC_VOLUME("CD Playback Volume", 0x19, 0x04, HDA_INPUT),
1128         HDA_CODEC_MUTE("CD Playback Switch", 0x19, 0x04, HDA_INPUT),
1129
1130 #if 0   
1131         /* Controls for GPIO pins, assuming they exist and are configured as outputs */
1132         CXT_GPIO_DATA_SWITCH("GPIO pin 0", 0x01, 0x01),
1133         CXT_GPIO_DATA_SWITCH("GPIO pin 1", 0x01, 0x02),
1134         CXT_GPIO_DATA_SWITCH("GPIO pin 2", 0x01, 0x04),
1135         CXT_GPIO_DATA_SWITCH("GPIO pin 3", 0x01, 0x08),
1136 #endif
1137         CXT_SPDIF_CTRL_SWITCH("SPDIF Playback Switch", 0x18, 0x01),
1138
1139         HDA_CODEC_VOLUME("Capture Volume", 0x19, 0x0, HDA_OUTPUT),
1140         HDA_CODEC_MUTE("Capture Switch", 0x19, 0x0, HDA_OUTPUT),
1141         {
1142                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1143                 .name = "Input Source",
1144                 .info = conexant_mux_enum_info,
1145                 .get = conexant_mux_enum_get,
1146                 .put = conexant_mux_enum_put,
1147         },
1148
1149         { } /* end */
1150 };
1151
1152 static struct hda_verb cxt5047_test_init_verbs[] = {
1153         /* Enable all GPIOs as outputs with an initial value of 0 */
1154         {0x01, AC_VERB_SET_GPIO_DIRECTION, 0x0f},
1155         {0x01, AC_VERB_SET_GPIO_DATA, 0x00},
1156         {0x01, AC_VERB_SET_GPIO_MASK, 0x0f},
1157
1158         /* Enable retasking pins as output, initially without power amp */
1159         {0x15, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1160         {0x14, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1161         {0x13, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
1162
1163         /* Disable digital (SPDIF) pins initially, but users can enable
1164          * them via a mixer switch.  In the case of SPDIF-out, this initverb
1165          * payload also sets the generation to 0, output to be in "consumer"
1166          * PCM format, copyright asserted, no pre-emphasis and no validity
1167          * control.
1168          */
1169         {0x18, AC_VERB_SET_DIGI_CONVERT_1, 0},
1170
1171         /* Ensure mic1, mic2, line1 pin widgets take input from the 
1172          * OUT1 sum bus when acting as an output.
1173          */
1174         {0x1a, AC_VERB_SET_CONNECT_SEL, 0},
1175         {0x1b, AC_VERB_SET_CONNECT_SEL, 0},
1176
1177         /* Start with output sum widgets muted and their output gains at min */
1178         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1179         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)},
1180
1181         /* Unmute retasking pin widget output buffers since the default
1182          * state appears to be output.  As the pin mode is changed by the
1183          * user the pin mode control will take care of enabling the pin's
1184          * input/output buffers as needed.
1185          */
1186         {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1187         {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1188         {0x13, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE},
1189
1190         /* Mute capture amp left and right */
1191         {0x12, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)},
1192
1193         /* Set ADC connection select to match default mixer setting (mic1
1194          * pin)
1195          */
1196         {0x12, AC_VERB_SET_CONNECT_SEL, 0x00},
1197
1198         /* Mute all inputs to mixer widget (even unconnected ones) */
1199         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(0)}, /* mic1 pin */
1200         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(1)}, /* mic2 pin */
1201         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(2)}, /* line1 pin */
1202         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(3)}, /* line2 pin */
1203         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(4)}, /* CD pin */
1204         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(5)}, /* Beep-gen pin */
1205         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(6)}, /* Line-out pin */
1206         {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_MUTE(7)}, /* HP-pin pin */
1207
1208         { }
1209 };
1210 #endif
1211
1212
1213 /* initialize jack-sensing, too */
1214 static int cxt5047_hp_init(struct hda_codec *codec)
1215 {
1216         conexant_init(codec);
1217         cxt5047_hp_automute(codec);
1218         cxt5047_hp_automic(codec);
1219         return 0;
1220 }
1221
1222
1223 enum {
1224         CXT5047_LAPTOP,         /* Laptops w/o EAPD support */
1225         CXT5047_LAPTOP_HP,      /* Some HP laptops */
1226         CXT5047_LAPTOP_EAPD,    /* Laptops with EAPD support */
1227 #ifdef CONFIG_SND_DEBUG
1228         CXT5047_TEST,
1229 #endif
1230         CXT5047_MODELS
1231 };
1232
1233 static const char *cxt5047_models[CXT5047_MODELS] = {
1234         [CXT5047_LAPTOP]        = "laptop",
1235         [CXT5047_LAPTOP_HP]     = "laptop-hp",
1236         [CXT5047_LAPTOP_EAPD]   = "laptop-eapd",
1237 #ifdef CONFIG_SND_DEBUG
1238         [CXT5047_TEST]          = "test",
1239 #endif
1240 };
1241
1242 static struct snd_pci_quirk cxt5047_cfg_tbl[] = {
1243         SND_PCI_QUIRK(0x103c, 0x30a0, "HP DV1000", CXT5047_LAPTOP),
1244         SND_PCI_QUIRK(0x103c, 0x30b2, "HP DV2000T/DV3000T", CXT5047_LAPTOP),
1245         SND_PCI_QUIRK(0x103c, 0x30a5, "HP DV5200T/DV8000T", CXT5047_LAPTOP_HP),
1246         SND_PCI_QUIRK(0x1179, 0xff31, "Toshiba P100", CXT5047_LAPTOP_EAPD),
1247         {}
1248 };
1249
1250 static int patch_cxt5047(struct hda_codec *codec)
1251 {
1252         struct conexant_spec *spec;
1253         int board_config;
1254
1255         spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1256         if (!spec)
1257                 return -ENOMEM;
1258         mutex_init(&spec->amp_mutex);
1259         codec->spec = spec;
1260
1261         spec->multiout.max_channels = 2;
1262         spec->multiout.num_dacs = ARRAY_SIZE(cxt5047_dac_nids);
1263         spec->multiout.dac_nids = cxt5047_dac_nids;
1264         spec->multiout.dig_out_nid = CXT5047_SPDIF_OUT;
1265         spec->num_adc_nids = 1;
1266         spec->adc_nids = cxt5047_adc_nids;
1267         spec->capsrc_nids = cxt5047_capsrc_nids;
1268         spec->input_mux = &cxt5047_capture_source;
1269         spec->num_mixers = 1;
1270         spec->mixers[0] = cxt5047_mixers;
1271         spec->num_init_verbs = 1;
1272         spec->init_verbs[0] = cxt5047_init_verbs;
1273         spec->spdif_route = 0;
1274         spec->num_channel_mode = ARRAY_SIZE(cxt5047_modes),
1275         spec->channel_mode = cxt5047_modes,
1276
1277         codec->patch_ops = conexant_patch_ops;
1278         codec->patch_ops.unsol_event = cxt5047_hp_unsol_event;
1279
1280         board_config = snd_hda_check_board_config(codec, CXT5047_MODELS,
1281                                                   cxt5047_models,
1282                                                   cxt5047_cfg_tbl);
1283         switch (board_config) {
1284         case CXT5047_LAPTOP:
1285                 break;
1286         case CXT5047_LAPTOP_HP:
1287                 spec->input_mux = &cxt5047_hp_capture_source;
1288                 spec->num_init_verbs = 2;
1289                 spec->init_verbs[1] = cxt5047_hp_init_verbs;
1290                 spec->mixers[0] = cxt5047_hp_mixers;
1291                 codec->patch_ops.init = cxt5047_hp_init;
1292                 break;
1293         case CXT5047_LAPTOP_EAPD:
1294                 spec->num_init_verbs = 2;
1295                 spec->init_verbs[1] = cxt5047_toshiba_init_verbs;
1296                 break;
1297 #ifdef CONFIG_SND_DEBUG
1298         case CXT5047_TEST:
1299                 spec->input_mux = &cxt5047_test_capture_source;
1300                 spec->mixers[0] = cxt5047_test_mixer;
1301                 spec->init_verbs[0] = cxt5047_test_init_verbs;
1302 #endif  
1303         }
1304         return 0;
1305 }
1306
1307 struct hda_codec_preset snd_hda_preset_conexant[] = {
1308         { .id = 0x14f15045, .name = "CXT5045", .patch = patch_cxt5045 },
1309         { .id = 0x14f15047, .name = "CXT5047", .patch = patch_cxt5047 },
1310         {} /* terminator */
1311 };