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