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