xref: /linux/sound/pci/hda/patch_conexant.c (revision 5c94bdab3a32e6225b30df6650337ad21ac42551)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * HD audio interface patch for Conexant HDA audio codec
4  *
5  * Copyright (c) 2006 Pototskiy Akex <alex.pototskiy@gmail.com>
6  * 		      Takashi Iwai <tiwai@suse.de>
7  * 		      Tobin Davis  <tdavis@dsl-only.net>
8  */
9 
10 #include <linux/init.h>
11 #include <linux/delay.h>
12 #include <linux/slab.h>
13 #include <linux/module.h>
14 #include <sound/core.h>
15 #include <sound/jack.h>
16 
17 #include <sound/hda_codec.h>
18 #include "hda_local.h"
19 #include "hda_auto_parser.h"
20 #include "hda_beep.h"
21 #include "hda_jack.h"
22 #include "hda_generic.h"
23 
24 struct conexant_spec {
25 	struct hda_gen_spec gen;
26 
27 	/* extra EAPD pins */
28 	unsigned int num_eapds;
29 	hda_nid_t eapds[4];
30 	bool dynamic_eapd;
31 	hda_nid_t mute_led_eapd;
32 
33 	unsigned int parse_flags; /* flag for snd_hda_parse_pin_defcfg() */
34 
35 	/* OPLC XO specific */
36 	bool recording;
37 	bool dc_enable;
38 	unsigned int dc_input_bias; /* offset into olpc_xo_dc_bias */
39 	struct nid_path *dc_mode_path;
40 
41 	int mute_led_polarity;
42 	unsigned int gpio_led;
43 	unsigned int gpio_mute_led_mask;
44 	unsigned int gpio_mic_led_mask;
45 	bool is_cx8070_sn6140;
46 };
47 
48 
49 #ifdef CONFIG_SND_HDA_INPUT_BEEP
50 /* additional beep mixers; private_value will be overwritten */
51 static const struct snd_kcontrol_new cxt_beep_mixer[] = {
52 	HDA_CODEC_VOLUME_MONO("Beep Playback Volume", 0, 1, 0, HDA_OUTPUT),
53 	HDA_CODEC_MUTE_BEEP_MONO("Beep Playback Switch", 0, 1, 0, HDA_OUTPUT),
54 };
55 
set_beep_amp(struct conexant_spec * spec,hda_nid_t nid,int idx,int dir)56 static int set_beep_amp(struct conexant_spec *spec, hda_nid_t nid,
57 			int idx, int dir)
58 {
59 	struct snd_kcontrol_new *knew;
60 	unsigned int beep_amp = HDA_COMPOSE_AMP_VAL(nid, 1, idx, dir);
61 	int i;
62 
63 	spec->gen.beep_nid = nid;
64 	for (i = 0; i < ARRAY_SIZE(cxt_beep_mixer); i++) {
65 		knew = snd_hda_gen_add_kctl(&spec->gen, NULL,
66 					    &cxt_beep_mixer[i]);
67 		if (!knew)
68 			return -ENOMEM;
69 		knew->private_value = beep_amp;
70 	}
71 	return 0;
72 }
73 
cx_auto_parse_beep(struct hda_codec * codec)74 static int cx_auto_parse_beep(struct hda_codec *codec)
75 {
76 	struct conexant_spec *spec = codec->spec;
77 	hda_nid_t nid;
78 
79 	for_each_hda_codec_node(nid, codec)
80 		if (get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_BEEP)
81 			return set_beep_amp(spec, nid, 0, HDA_OUTPUT);
82 	return 0;
83 }
84 #else
85 #define cx_auto_parse_beep(codec)	0
86 #endif
87 
88 /*
89  * Automatic parser for CX20641 & co
90  */
91 
92 /* parse EAPDs */
cx_auto_parse_eapd(struct hda_codec * codec)93 static void cx_auto_parse_eapd(struct hda_codec *codec)
94 {
95 	struct conexant_spec *spec = codec->spec;
96 	hda_nid_t nid;
97 
98 	for_each_hda_codec_node(nid, codec) {
99 		if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
100 			continue;
101 		if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD))
102 			continue;
103 		spec->eapds[spec->num_eapds++] = nid;
104 		if (spec->num_eapds >= ARRAY_SIZE(spec->eapds))
105 			break;
106 	}
107 
108 	/* NOTE: below is a wild guess; if we have more than two EAPDs,
109 	 * it's a new chip, where EAPDs are supposed to be associated to
110 	 * pins, and we can control EAPD per pin.
111 	 * OTOH, if only one or two EAPDs are found, it's an old chip,
112 	 * thus it might control over all pins.
113 	 */
114 	if (spec->num_eapds > 2)
115 		spec->dynamic_eapd = 1;
116 }
117 
cx_auto_turn_eapd(struct hda_codec * codec,int num_pins,const hda_nid_t * pins,bool on)118 static void cx_auto_turn_eapd(struct hda_codec *codec, int num_pins,
119 			      const hda_nid_t *pins, bool on)
120 {
121 	int i;
122 	for (i = 0; i < num_pins; i++) {
123 		if (snd_hda_query_pin_caps(codec, pins[i]) & AC_PINCAP_EAPD)
124 			snd_hda_codec_write(codec, pins[i], 0,
125 					    AC_VERB_SET_EAPD_BTLENABLE,
126 					    on ? 0x02 : 0);
127 	}
128 }
129 
130 /* turn on/off EAPD according to Master switch */
cx_auto_vmaster_hook(void * private_data,int enabled)131 static void cx_auto_vmaster_hook(void *private_data, int enabled)
132 {
133 	struct hda_codec *codec = private_data;
134 	struct conexant_spec *spec = codec->spec;
135 
136 	cx_auto_turn_eapd(codec, spec->num_eapds, spec->eapds, enabled);
137 }
138 
139 /* turn on/off EAPD according to Master switch (inversely!) for mute LED */
cx_auto_vmaster_mute_led(struct led_classdev * led_cdev,enum led_brightness brightness)140 static int cx_auto_vmaster_mute_led(struct led_classdev *led_cdev,
141 				    enum led_brightness brightness)
142 {
143 	struct hda_codec *codec = dev_to_hda_codec(led_cdev->dev->parent);
144 	struct conexant_spec *spec = codec->spec;
145 
146 	snd_hda_codec_write(codec, spec->mute_led_eapd, 0,
147 			    AC_VERB_SET_EAPD_BTLENABLE,
148 			    brightness ? 0x02 : 0x00);
149 	return 0;
150 }
151 
cxt_init_gpio_led(struct hda_codec * codec)152 static void cxt_init_gpio_led(struct hda_codec *codec)
153 {
154 	struct conexant_spec *spec = codec->spec;
155 	unsigned int mask = spec->gpio_mute_led_mask | spec->gpio_mic_led_mask;
156 
157 	if (mask) {
158 		snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_MASK,
159 				    mask);
160 		snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DIRECTION,
161 				    mask);
162 		snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
163 				    spec->gpio_led);
164 	}
165 }
166 
cx_fixup_headset_recog(struct hda_codec * codec)167 static void cx_fixup_headset_recog(struct hda_codec *codec)
168 {
169 	unsigned int mic_present;
170 
171 	/* fix some headset type recognize fail issue, such as EDIFIER headset */
172 	/* set micbias output current comparator threshold from 66% to 55%. */
173 	snd_hda_codec_write(codec, 0x1c, 0, 0x320, 0x010);
174 	/* set OFF voltage for DFET from -1.2V to -0.8V, set headset micbias register
175 	 * value adjustment trim from 2.2K ohms to 2.0K ohms.
176 	 */
177 	snd_hda_codec_write(codec, 0x1c, 0, 0x3b0, 0xe10);
178 	/* fix reboot headset type recognize fail issue */
179 	mic_present = snd_hda_codec_read(codec, 0x19, 0, AC_VERB_GET_PIN_SENSE, 0x0);
180 	if (mic_present & AC_PINSENSE_PRESENCE)
181 		/* enable headset mic VREF */
182 		snd_hda_codec_write(codec, 0x19, 0, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x24);
183 	else
184 		/* disable headset mic VREF */
185 		snd_hda_codec_write(codec, 0x19, 0, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20);
186 }
187 
cx_auto_init(struct hda_codec * codec)188 static int cx_auto_init(struct hda_codec *codec)
189 {
190 	struct conexant_spec *spec = codec->spec;
191 	snd_hda_gen_init(codec);
192 	if (!spec->dynamic_eapd)
193 		cx_auto_turn_eapd(codec, spec->num_eapds, spec->eapds, true);
194 
195 	cxt_init_gpio_led(codec);
196 	snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_INIT);
197 
198 	if (spec->is_cx8070_sn6140)
199 		cx_fixup_headset_recog(codec);
200 
201 	return 0;
202 }
203 
cx_auto_shutdown(struct hda_codec * codec)204 static void cx_auto_shutdown(struct hda_codec *codec)
205 {
206 	struct conexant_spec *spec = codec->spec;
207 
208 	snd_hda_gen_shutup_speakers(codec);
209 
210 	/* Turn the problematic codec into D3 to avoid spurious noises
211 	   from the internal speaker during (and after) reboot */
212 	cx_auto_turn_eapd(codec, spec->num_eapds, spec->eapds, false);
213 }
214 
cx_auto_free(struct hda_codec * codec)215 static void cx_auto_free(struct hda_codec *codec)
216 {
217 	cx_auto_shutdown(codec);
218 	snd_hda_gen_free(codec);
219 }
220 
cx_process_headset_plugin(struct hda_codec * codec)221 static void cx_process_headset_plugin(struct hda_codec *codec)
222 {
223 	unsigned int val;
224 	unsigned int count = 0;
225 
226 	/* Wait headset detect done. */
227 	do {
228 		val = snd_hda_codec_read(codec, 0x1c, 0, 0xca0, 0x0);
229 		if (val & 0x080) {
230 			codec_dbg(codec, "headset type detect done!\n");
231 			break;
232 		}
233 		msleep(20);
234 		count++;
235 	} while (count < 3);
236 	val = snd_hda_codec_read(codec, 0x1c, 0, 0xcb0, 0x0);
237 	if (val & 0x800) {
238 		codec_dbg(codec, "headset plugin, type is CTIA\n");
239 		snd_hda_codec_write(codec, 0x19, 0, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x24);
240 	} else if (val & 0x400) {
241 		codec_dbg(codec, "headset plugin, type is OMTP\n");
242 		snd_hda_codec_write(codec, 0x19, 0, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x24);
243 	} else {
244 		codec_dbg(codec, "headphone plugin\n");
245 	}
246 }
247 
cx_update_headset_mic_vref(struct hda_codec * codec,struct hda_jack_callback * event)248 static void cx_update_headset_mic_vref(struct hda_codec *codec, struct hda_jack_callback *event)
249 {
250 	unsigned int mic_present;
251 
252 	/* In cx8070 and sn6140, the node 16 can only be configured to headphone or disabled,
253 	 * the node 19 can only be configured to microphone or disabled.
254 	 * Check hp&mic tag to process headset plugin & plugout.
255 	 */
256 	mic_present = snd_hda_codec_read(codec, 0x19, 0, AC_VERB_GET_PIN_SENSE, 0x0);
257 	if (!(mic_present & AC_PINSENSE_PRESENCE)) /* mic plugout */
258 		snd_hda_codec_write(codec, 0x19, 0, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x20);
259 	else
260 		cx_process_headset_plugin(codec);
261 }
262 
cx_auto_suspend(struct hda_codec * codec)263 static int cx_auto_suspend(struct hda_codec *codec)
264 {
265 	cx_auto_shutdown(codec);
266 	return 0;
267 }
268 
269 static const struct hda_codec_ops cx_auto_patch_ops = {
270 	.build_controls = snd_hda_gen_build_controls,
271 	.build_pcms = snd_hda_gen_build_pcms,
272 	.init = cx_auto_init,
273 	.free = cx_auto_free,
274 	.unsol_event = snd_hda_jack_unsol_event,
275 	.suspend = cx_auto_suspend,
276 	.check_power_status = snd_hda_gen_check_power_status,
277 };
278 
279 /*
280  * pin fix-up
281  */
282 enum {
283 	CXT_PINCFG_LENOVO_X200,
284 	CXT_PINCFG_LENOVO_TP410,
285 	CXT_PINCFG_LEMOTE_A1004,
286 	CXT_PINCFG_LEMOTE_A1205,
287 	CXT_PINCFG_COMPAQ_CQ60,
288 	CXT_FIXUP_STEREO_DMIC,
289 	CXT_PINCFG_LENOVO_NOTEBOOK,
290 	CXT_FIXUP_INC_MIC_BOOST,
291 	CXT_FIXUP_HEADPHONE_MIC_PIN,
292 	CXT_FIXUP_HEADPHONE_MIC,
293 	CXT_FIXUP_GPIO1,
294 	CXT_FIXUP_ASPIRE_DMIC,
295 	CXT_FIXUP_THINKPAD_ACPI,
296 	CXT_FIXUP_OLPC_XO,
297 	CXT_FIXUP_CAP_MIX_AMP,
298 	CXT_FIXUP_TOSHIBA_P105,
299 	CXT_FIXUP_HP_530,
300 	CXT_FIXUP_CAP_MIX_AMP_5047,
301 	CXT_FIXUP_MUTE_LED_EAPD,
302 	CXT_FIXUP_HP_DOCK,
303 	CXT_FIXUP_HP_SPECTRE,
304 	CXT_FIXUP_HP_GATE_MIC,
305 	CXT_FIXUP_MUTE_LED_GPIO,
306 	CXT_FIXUP_HP_ELITEONE_OUT_DIS,
307 	CXT_FIXUP_HP_ZBOOK_MUTE_LED,
308 	CXT_FIXUP_HEADSET_MIC,
309 	CXT_FIXUP_HP_MIC_NO_PRESENCE,
310 	CXT_PINCFG_SWS_JS201D,
311 	CXT_PINCFG_TOP_SPEAKER,
312 };
313 
314 /* for hda_fixup_thinkpad_acpi() */
315 #include "thinkpad_helper.c"
316 
cxt_fixup_stereo_dmic(struct hda_codec * codec,const struct hda_fixup * fix,int action)317 static void cxt_fixup_stereo_dmic(struct hda_codec *codec,
318 				  const struct hda_fixup *fix, int action)
319 {
320 	struct conexant_spec *spec = codec->spec;
321 	spec->gen.inv_dmic_split = 1;
322 }
323 
324 /* fix widget control pin settings */
cxt_fixup_update_pinctl(struct hda_codec * codec,const struct hda_fixup * fix,int action)325 static void cxt_fixup_update_pinctl(struct hda_codec *codec,
326 				   const struct hda_fixup *fix, int action)
327 {
328 	if (action == HDA_FIXUP_ACT_PROBE) {
329 		/* Unset OUT_EN for this Node pin, leaving only HP_EN.
330 		 * This is the value stored in the codec register after
331 		 * the correct initialization of the previous windows boot.
332 		 */
333 		snd_hda_set_pin_ctl_cache(codec, 0x1d, AC_PINCTL_HP_EN);
334 	}
335 }
336 
cxt5066_increase_mic_boost(struct hda_codec * codec,const struct hda_fixup * fix,int action)337 static void cxt5066_increase_mic_boost(struct hda_codec *codec,
338 				   const struct hda_fixup *fix, int action)
339 {
340 	if (action != HDA_FIXUP_ACT_PRE_PROBE)
341 		return;
342 
343 	snd_hda_override_amp_caps(codec, 0x17, HDA_OUTPUT,
344 				  (0x3 << AC_AMPCAP_OFFSET_SHIFT) |
345 				  (0x4 << AC_AMPCAP_NUM_STEPS_SHIFT) |
346 				  (0x27 << AC_AMPCAP_STEP_SIZE_SHIFT) |
347 				  (0 << AC_AMPCAP_MUTE_SHIFT));
348 }
349 
cxt_update_headset_mode(struct hda_codec * codec)350 static void cxt_update_headset_mode(struct hda_codec *codec)
351 {
352 	/* The verbs used in this function were tested on a Conexant CX20751/2 codec. */
353 	int i;
354 	bool mic_mode = false;
355 	struct conexant_spec *spec = codec->spec;
356 	struct auto_pin_cfg *cfg = &spec->gen.autocfg;
357 
358 	hda_nid_t mux_pin = spec->gen.imux_pins[spec->gen.cur_mux[0]];
359 
360 	for (i = 0; i < cfg->num_inputs; i++)
361 		if (cfg->inputs[i].pin == mux_pin) {
362 			mic_mode = !!cfg->inputs[i].is_headphone_mic;
363 			break;
364 		}
365 
366 	if (mic_mode) {
367 		snd_hda_codec_write_cache(codec, 0x1c, 0, 0x410, 0x7c); /* enable merged mode for analog int-mic */
368 		spec->gen.hp_jack_present = false;
369 	} else {
370 		snd_hda_codec_write_cache(codec, 0x1c, 0, 0x410, 0x54); /* disable merged mode for analog int-mic */
371 		spec->gen.hp_jack_present = snd_hda_jack_detect(codec, spec->gen.autocfg.hp_pins[0]);
372 	}
373 
374 	snd_hda_gen_update_outputs(codec);
375 }
376 
cxt_update_headset_mode_hook(struct hda_codec * codec,struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)377 static void cxt_update_headset_mode_hook(struct hda_codec *codec,
378 					 struct snd_kcontrol *kcontrol,
379 					 struct snd_ctl_elem_value *ucontrol)
380 {
381 	cxt_update_headset_mode(codec);
382 }
383 
cxt_fixup_headphone_mic(struct hda_codec * codec,const struct hda_fixup * fix,int action)384 static void cxt_fixup_headphone_mic(struct hda_codec *codec,
385 				    const struct hda_fixup *fix, int action)
386 {
387 	struct conexant_spec *spec = codec->spec;
388 
389 	switch (action) {
390 	case HDA_FIXUP_ACT_PRE_PROBE:
391 		spec->parse_flags |= HDA_PINCFG_HEADPHONE_MIC;
392 		snd_hdac_regmap_add_vendor_verb(&codec->core, 0x410);
393 		break;
394 	case HDA_FIXUP_ACT_PROBE:
395 		WARN_ON(spec->gen.cap_sync_hook);
396 		spec->gen.cap_sync_hook = cxt_update_headset_mode_hook;
397 		spec->gen.automute_hook = cxt_update_headset_mode;
398 		break;
399 	case HDA_FIXUP_ACT_INIT:
400 		cxt_update_headset_mode(codec);
401 		break;
402 	}
403 }
404 
cxt_fixup_headset_mic(struct hda_codec * codec,const struct hda_fixup * fix,int action)405 static void cxt_fixup_headset_mic(struct hda_codec *codec,
406 				    const struct hda_fixup *fix, int action)
407 {
408 	struct conexant_spec *spec = codec->spec;
409 
410 	switch (action) {
411 	case HDA_FIXUP_ACT_PRE_PROBE:
412 		spec->parse_flags |= HDA_PINCFG_HEADSET_MIC;
413 		break;
414 	}
415 }
416 
417 /* OPLC XO 1.5 fixup */
418 
419 /* OLPC XO-1.5 supports DC input mode (e.g. for use with analog sensors)
420  * through the microphone jack.
421  * When the user enables this through a mixer switch, both internal and
422  * external microphones are disabled. Gain is fixed at 0dB. In this mode,
423  * we also allow the bias to be configured through a separate mixer
424  * control. */
425 
426 #define update_mic_pin(codec, nid, val)					\
427 	snd_hda_codec_write_cache(codec, nid, 0,			\
428 				   AC_VERB_SET_PIN_WIDGET_CONTROL, val)
429 
430 static const struct hda_input_mux olpc_xo_dc_bias = {
431 	.num_items = 3,
432 	.items = {
433 		{ "Off", PIN_IN },
434 		{ "50%", PIN_VREF50 },
435 		{ "80%", PIN_VREF80 },
436 	},
437 };
438 
olpc_xo_update_mic_boost(struct hda_codec * codec)439 static void olpc_xo_update_mic_boost(struct hda_codec *codec)
440 {
441 	struct conexant_spec *spec = codec->spec;
442 	int ch, val;
443 
444 	for (ch = 0; ch < 2; ch++) {
445 		val = AC_AMP_SET_OUTPUT |
446 			(ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT);
447 		if (!spec->dc_enable)
448 			val |= snd_hda_codec_amp_read(codec, 0x17, ch, HDA_OUTPUT, 0);
449 		snd_hda_codec_write(codec, 0x17, 0,
450 				    AC_VERB_SET_AMP_GAIN_MUTE, val);
451 	}
452 }
453 
olpc_xo_update_mic_pins(struct hda_codec * codec)454 static void olpc_xo_update_mic_pins(struct hda_codec *codec)
455 {
456 	struct conexant_spec *spec = codec->spec;
457 	int cur_input, val;
458 	struct nid_path *path;
459 
460 	cur_input = spec->gen.input_paths[0][spec->gen.cur_mux[0]];
461 
462 	/* Set up mic pins for port-B, C and F dynamically as the recording
463 	 * LED is turned on/off by these pin controls
464 	 */
465 	if (!spec->dc_enable) {
466 		/* disable DC bias path and pin for port F */
467 		update_mic_pin(codec, 0x1e, 0);
468 		snd_hda_activate_path(codec, spec->dc_mode_path, false, false);
469 
470 		/* update port B (ext mic) and C (int mic) */
471 		/* OLPC defers mic widget control until when capture is
472 		 * started because the microphone LED comes on as soon as
473 		 * these settings are put in place. if we did this before
474 		 * recording, it would give the false indication that
475 		 * recording is happening when it is not.
476 		 */
477 		update_mic_pin(codec, 0x1a, spec->recording ?
478 			       snd_hda_codec_get_pin_target(codec, 0x1a) : 0);
479 		update_mic_pin(codec, 0x1b, spec->recording ?
480 			       snd_hda_codec_get_pin_target(codec, 0x1b) : 0);
481 		/* enable normal mic path */
482 		path = snd_hda_get_path_from_idx(codec, cur_input);
483 		if (path)
484 			snd_hda_activate_path(codec, path, true, false);
485 	} else {
486 		/* disable normal mic path */
487 		path = snd_hda_get_path_from_idx(codec, cur_input);
488 		if (path)
489 			snd_hda_activate_path(codec, path, false, false);
490 
491 		/* Even though port F is the DC input, the bias is controlled
492 		 * on port B.  We also leave that port as an active input (but
493 		 * unselected) in DC mode just in case that is necessary to
494 		 * make the bias setting take effect.
495 		 */
496 		if (spec->recording)
497 			val = olpc_xo_dc_bias.items[spec->dc_input_bias].index;
498 		else
499 			val = 0;
500 		update_mic_pin(codec, 0x1a, val);
501 		update_mic_pin(codec, 0x1b, 0);
502 		/* enable DC bias path and pin */
503 		update_mic_pin(codec, 0x1e, spec->recording ? PIN_IN : 0);
504 		snd_hda_activate_path(codec, spec->dc_mode_path, true, false);
505 	}
506 }
507 
508 /* mic_autoswitch hook */
olpc_xo_automic(struct hda_codec * codec,struct hda_jack_callback * jack)509 static void olpc_xo_automic(struct hda_codec *codec,
510 			    struct hda_jack_callback *jack)
511 {
512 	struct conexant_spec *spec = codec->spec;
513 
514 	/* in DC mode, we don't handle automic */
515 	if (!spec->dc_enable)
516 		snd_hda_gen_mic_autoswitch(codec, jack);
517 	olpc_xo_update_mic_pins(codec);
518 	if (spec->dc_enable)
519 		olpc_xo_update_mic_boost(codec);
520 }
521 
522 /* pcm_capture hook */
olpc_xo_capture_hook(struct hda_pcm_stream * hinfo,struct hda_codec * codec,struct snd_pcm_substream * substream,int action)523 static void olpc_xo_capture_hook(struct hda_pcm_stream *hinfo,
524 				 struct hda_codec *codec,
525 				 struct snd_pcm_substream *substream,
526 				 int action)
527 {
528 	struct conexant_spec *spec = codec->spec;
529 
530 	/* toggle spec->recording flag and update mic pins accordingly
531 	 * for turning on/off LED
532 	 */
533 	switch (action) {
534 	case HDA_GEN_PCM_ACT_PREPARE:
535 		spec->recording = 1;
536 		olpc_xo_update_mic_pins(codec);
537 		break;
538 	case HDA_GEN_PCM_ACT_CLEANUP:
539 		spec->recording = 0;
540 		olpc_xo_update_mic_pins(codec);
541 		break;
542 	}
543 }
544 
olpc_xo_dc_mode_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)545 static int olpc_xo_dc_mode_get(struct snd_kcontrol *kcontrol,
546 			       struct snd_ctl_elem_value *ucontrol)
547 {
548 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
549 	struct conexant_spec *spec = codec->spec;
550 	ucontrol->value.integer.value[0] = spec->dc_enable;
551 	return 0;
552 }
553 
olpc_xo_dc_mode_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)554 static int olpc_xo_dc_mode_put(struct snd_kcontrol *kcontrol,
555 			       struct snd_ctl_elem_value *ucontrol)
556 {
557 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
558 	struct conexant_spec *spec = codec->spec;
559 	int dc_enable = !!ucontrol->value.integer.value[0];
560 
561 	if (dc_enable == spec->dc_enable)
562 		return 0;
563 
564 	spec->dc_enable = dc_enable;
565 	olpc_xo_update_mic_pins(codec);
566 	olpc_xo_update_mic_boost(codec);
567 	return 1;
568 }
569 
olpc_xo_dc_bias_enum_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)570 static int olpc_xo_dc_bias_enum_get(struct snd_kcontrol *kcontrol,
571 				    struct snd_ctl_elem_value *ucontrol)
572 {
573 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
574 	struct conexant_spec *spec = codec->spec;
575 	ucontrol->value.enumerated.item[0] = spec->dc_input_bias;
576 	return 0;
577 }
578 
olpc_xo_dc_bias_enum_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)579 static int olpc_xo_dc_bias_enum_info(struct snd_kcontrol *kcontrol,
580 				     struct snd_ctl_elem_info *uinfo)
581 {
582 	return snd_hda_input_mux_info(&olpc_xo_dc_bias, uinfo);
583 }
584 
olpc_xo_dc_bias_enum_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)585 static int olpc_xo_dc_bias_enum_put(struct snd_kcontrol *kcontrol,
586 				    struct snd_ctl_elem_value *ucontrol)
587 {
588 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
589 	struct conexant_spec *spec = codec->spec;
590 	const struct hda_input_mux *imux = &olpc_xo_dc_bias;
591 	unsigned int idx;
592 
593 	idx = ucontrol->value.enumerated.item[0];
594 	if (idx >= imux->num_items)
595 		idx = imux->num_items - 1;
596 	if (spec->dc_input_bias == idx)
597 		return 0;
598 
599 	spec->dc_input_bias = idx;
600 	if (spec->dc_enable)
601 		olpc_xo_update_mic_pins(codec);
602 	return 1;
603 }
604 
605 static const struct snd_kcontrol_new olpc_xo_mixers[] = {
606 	{
607 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
608 		.name = "DC Mode Enable Switch",
609 		.info = snd_ctl_boolean_mono_info,
610 		.get = olpc_xo_dc_mode_get,
611 		.put = olpc_xo_dc_mode_put,
612 	},
613 	{
614 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
615 		.name = "DC Input Bias Enum",
616 		.info = olpc_xo_dc_bias_enum_info,
617 		.get = olpc_xo_dc_bias_enum_get,
618 		.put = olpc_xo_dc_bias_enum_put,
619 	},
620 	{}
621 };
622 
623 /* overriding mic boost put callback; update mic boost volume only when
624  * DC mode is disabled
625  */
olpc_xo_mic_boost_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)626 static int olpc_xo_mic_boost_put(struct snd_kcontrol *kcontrol,
627 				 struct snd_ctl_elem_value *ucontrol)
628 {
629 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
630 	struct conexant_spec *spec = codec->spec;
631 	int ret = snd_hda_mixer_amp_volume_put(kcontrol, ucontrol);
632 	if (ret > 0 && spec->dc_enable)
633 		olpc_xo_update_mic_boost(codec);
634 	return ret;
635 }
636 
cxt_fixup_olpc_xo(struct hda_codec * codec,const struct hda_fixup * fix,int action)637 static void cxt_fixup_olpc_xo(struct hda_codec *codec,
638 				    const struct hda_fixup *fix, int action)
639 {
640 	struct conexant_spec *spec = codec->spec;
641 	struct snd_kcontrol_new *kctl;
642 	int i;
643 
644 	if (action != HDA_FIXUP_ACT_PROBE)
645 		return;
646 
647 	spec->gen.mic_autoswitch_hook = olpc_xo_automic;
648 	spec->gen.pcm_capture_hook = olpc_xo_capture_hook;
649 	spec->dc_mode_path = snd_hda_add_new_path(codec, 0x1e, 0x14, 0);
650 
651 	snd_hda_add_new_ctls(codec, olpc_xo_mixers);
652 
653 	/* OLPC's microphone port is DC coupled for use with external sensors,
654 	 * therefore we use a 50% mic bias in order to center the input signal
655 	 * with the DC input range of the codec.
656 	 */
657 	snd_hda_codec_set_pin_target(codec, 0x1a, PIN_VREF50);
658 
659 	/* override mic boost control */
660 	snd_array_for_each(&spec->gen.kctls, i, kctl) {
661 		if (!strcmp(kctl->name, "Mic Boost Volume")) {
662 			kctl->put = olpc_xo_mic_boost_put;
663 			break;
664 		}
665 	}
666 }
667 
cxt_fixup_mute_led_eapd(struct hda_codec * codec,const struct hda_fixup * fix,int action)668 static void cxt_fixup_mute_led_eapd(struct hda_codec *codec,
669 				    const struct hda_fixup *fix, int action)
670 {
671 	struct conexant_spec *spec = codec->spec;
672 
673 	if (action == HDA_FIXUP_ACT_PRE_PROBE) {
674 		spec->mute_led_eapd = 0x1b;
675 		spec->dynamic_eapd = true;
676 		snd_hda_gen_add_mute_led_cdev(codec, cx_auto_vmaster_mute_led);
677 	}
678 }
679 
680 /*
681  * Fix max input level on mixer widget to 0dB
682  * (originally it has 0x2b steps with 0dB offset 0x14)
683  */
cxt_fixup_cap_mix_amp(struct hda_codec * codec,const struct hda_fixup * fix,int action)684 static void cxt_fixup_cap_mix_amp(struct hda_codec *codec,
685 				  const struct hda_fixup *fix, int action)
686 {
687 	snd_hda_override_amp_caps(codec, 0x17, HDA_INPUT,
688 				  (0x14 << AC_AMPCAP_OFFSET_SHIFT) |
689 				  (0x14 << AC_AMPCAP_NUM_STEPS_SHIFT) |
690 				  (0x05 << AC_AMPCAP_STEP_SIZE_SHIFT) |
691 				  (1 << AC_AMPCAP_MUTE_SHIFT));
692 }
693 
694 /*
695  * Fix max input level on mixer widget to 0dB
696  * (originally it has 0x1e steps with 0 dB offset 0x17)
697  */
cxt_fixup_cap_mix_amp_5047(struct hda_codec * codec,const struct hda_fixup * fix,int action)698 static void cxt_fixup_cap_mix_amp_5047(struct hda_codec *codec,
699 				  const struct hda_fixup *fix, int action)
700 {
701 	snd_hda_override_amp_caps(codec, 0x10, HDA_INPUT,
702 				  (0x17 << AC_AMPCAP_OFFSET_SHIFT) |
703 				  (0x17 << AC_AMPCAP_NUM_STEPS_SHIFT) |
704 				  (0x05 << AC_AMPCAP_STEP_SIZE_SHIFT) |
705 				  (1 << AC_AMPCAP_MUTE_SHIFT));
706 }
707 
cxt_fixup_hp_gate_mic_jack(struct hda_codec * codec,const struct hda_fixup * fix,int action)708 static void cxt_fixup_hp_gate_mic_jack(struct hda_codec *codec,
709 				       const struct hda_fixup *fix,
710 				       int action)
711 {
712 	/* the mic pin (0x19) doesn't give an unsolicited event;
713 	 * probe the mic pin together with the headphone pin (0x16)
714 	 */
715 	if (action == HDA_FIXUP_ACT_PROBE)
716 		snd_hda_jack_set_gating_jack(codec, 0x19, 0x16);
717 }
718 
719 /* update LED status via GPIO */
cxt_update_gpio_led(struct hda_codec * codec,unsigned int mask,bool led_on)720 static void cxt_update_gpio_led(struct hda_codec *codec, unsigned int mask,
721 				bool led_on)
722 {
723 	struct conexant_spec *spec = codec->spec;
724 	unsigned int oldval = spec->gpio_led;
725 
726 	if (spec->mute_led_polarity)
727 		led_on = !led_on;
728 
729 	if (led_on)
730 		spec->gpio_led |= mask;
731 	else
732 		spec->gpio_led &= ~mask;
733 	codec_dbg(codec, "mask:%d enabled:%d gpio_led:%d\n",
734 			mask, led_on, spec->gpio_led);
735 	if (spec->gpio_led != oldval)
736 		snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
737 				    spec->gpio_led);
738 }
739 
740 /* turn on/off mute LED via GPIO per vmaster hook */
cxt_gpio_mute_update(struct led_classdev * led_cdev,enum led_brightness brightness)741 static int cxt_gpio_mute_update(struct led_classdev *led_cdev,
742 				enum led_brightness brightness)
743 {
744 	struct hda_codec *codec = dev_to_hda_codec(led_cdev->dev->parent);
745 	struct conexant_spec *spec = codec->spec;
746 
747 	cxt_update_gpio_led(codec, spec->gpio_mute_led_mask, brightness);
748 	return 0;
749 }
750 
751 /* turn on/off mic-mute LED via GPIO per capture hook */
cxt_gpio_micmute_update(struct led_classdev * led_cdev,enum led_brightness brightness)752 static int cxt_gpio_micmute_update(struct led_classdev *led_cdev,
753 				   enum led_brightness brightness)
754 {
755 	struct hda_codec *codec = dev_to_hda_codec(led_cdev->dev->parent);
756 	struct conexant_spec *spec = codec->spec;
757 
758 	cxt_update_gpio_led(codec, spec->gpio_mic_led_mask, brightness);
759 	return 0;
760 }
761 
cxt_setup_mute_led(struct hda_codec * codec,unsigned int mute,unsigned int mic_mute)762 static void cxt_setup_mute_led(struct hda_codec *codec,
763 			       unsigned int mute, unsigned int mic_mute)
764 {
765 	struct conexant_spec *spec = codec->spec;
766 
767 	spec->gpio_led = 0;
768 	spec->mute_led_polarity = 0;
769 	if (mute) {
770 		snd_hda_gen_add_mute_led_cdev(codec, cxt_gpio_mute_update);
771 		spec->gpio_mute_led_mask = mute;
772 	}
773 	if (mic_mute) {
774 		snd_hda_gen_add_micmute_led_cdev(codec, cxt_gpio_micmute_update);
775 		spec->gpio_mic_led_mask = mic_mute;
776 	}
777 }
778 
cxt_fixup_mute_led_gpio(struct hda_codec * codec,const struct hda_fixup * fix,int action)779 static void cxt_fixup_mute_led_gpio(struct hda_codec *codec,
780 				const struct hda_fixup *fix, int action)
781 {
782 	if (action == HDA_FIXUP_ACT_PRE_PROBE)
783 		cxt_setup_mute_led(codec, 0x01, 0x02);
784 }
785 
cxt_fixup_hp_zbook_mute_led(struct hda_codec * codec,const struct hda_fixup * fix,int action)786 static void cxt_fixup_hp_zbook_mute_led(struct hda_codec *codec,
787 					const struct hda_fixup *fix, int action)
788 {
789 	if (action == HDA_FIXUP_ACT_PRE_PROBE)
790 		cxt_setup_mute_led(codec, 0x10, 0x20);
791 }
792 
793 /* ThinkPad X200 & co with cxt5051 */
794 static const struct hda_pintbl cxt_pincfg_lenovo_x200[] = {
795 	{ 0x16, 0x042140ff }, /* HP (seq# overridden) */
796 	{ 0x17, 0x21a11000 }, /* dock-mic */
797 	{ 0x19, 0x2121103f }, /* dock-HP */
798 	{ 0x1c, 0x21440100 }, /* dock SPDIF out */
799 	{}
800 };
801 
802 /* ThinkPad 410/420/510/520, X201 & co with cxt5066 */
803 static const struct hda_pintbl cxt_pincfg_lenovo_tp410[] = {
804 	{ 0x19, 0x042110ff }, /* HP (seq# overridden) */
805 	{ 0x1a, 0x21a190f0 }, /* dock-mic */
806 	{ 0x1c, 0x212140ff }, /* dock-HP */
807 	{}
808 };
809 
810 /* Lemote A1004/A1205 with cxt5066 */
811 static const struct hda_pintbl cxt_pincfg_lemote[] = {
812 	{ 0x1a, 0x90a10020 }, /* Internal mic */
813 	{ 0x1b, 0x03a11020 }, /* External mic */
814 	{ 0x1d, 0x400101f0 }, /* Not used */
815 	{ 0x1e, 0x40a701f0 }, /* Not used */
816 	{ 0x20, 0x404501f0 }, /* Not used */
817 	{ 0x22, 0x404401f0 }, /* Not used */
818 	{ 0x23, 0x40a701f0 }, /* Not used */
819 	{}
820 };
821 
822 /* SuoWoSi/South-holding JS201D with sn6140 */
823 static const struct hda_pintbl cxt_pincfg_sws_js201d[] = {
824 	{ 0x16, 0x03211040 }, /* hp out */
825 	{ 0x17, 0x91170110 }, /* SPK/Class_D */
826 	{ 0x18, 0x95a70130 }, /* Internal mic */
827 	{ 0x19, 0x03a11020 }, /* Headset Mic */
828 	{ 0x1a, 0x40f001f0 }, /* Not used */
829 	{ 0x21, 0x40f001f0 }, /* Not used */
830 	{}
831 };
832 
833 /* pincfg quirk for Tuxedo Sirius;
834  * unfortunately the (PCI) SSID conflicts with System76 Pangolin pang14,
835  * which has incompatible pin setup, so we check the codec SSID (luckily
836  * different one!) and conditionally apply the quirk here
837  */
cxt_fixup_sirius_top_speaker(struct hda_codec * codec,const struct hda_fixup * fix,int action)838 static void cxt_fixup_sirius_top_speaker(struct hda_codec *codec,
839 					 const struct hda_fixup *fix,
840 					 int action)
841 {
842 	/* ignore for incorrectly picked-up pang14 */
843 	if (codec->core.subsystem_id == 0x278212b3)
844 		return;
845 	/* set up the top speaker pin */
846 	if (action == HDA_FIXUP_ACT_PRE_PROBE)
847 		snd_hda_codec_set_pincfg(codec, 0x1d, 0x82170111);
848 }
849 
850 static const struct hda_fixup cxt_fixups[] = {
851 	[CXT_PINCFG_LENOVO_X200] = {
852 		.type = HDA_FIXUP_PINS,
853 		.v.pins = cxt_pincfg_lenovo_x200,
854 	},
855 	[CXT_PINCFG_LENOVO_TP410] = {
856 		.type = HDA_FIXUP_PINS,
857 		.v.pins = cxt_pincfg_lenovo_tp410,
858 		.chained = true,
859 		.chain_id = CXT_FIXUP_THINKPAD_ACPI,
860 	},
861 	[CXT_PINCFG_LEMOTE_A1004] = {
862 		.type = HDA_FIXUP_PINS,
863 		.chained = true,
864 		.chain_id = CXT_FIXUP_INC_MIC_BOOST,
865 		.v.pins = cxt_pincfg_lemote,
866 	},
867 	[CXT_PINCFG_LEMOTE_A1205] = {
868 		.type = HDA_FIXUP_PINS,
869 		.v.pins = cxt_pincfg_lemote,
870 	},
871 	[CXT_PINCFG_COMPAQ_CQ60] = {
872 		.type = HDA_FIXUP_PINS,
873 		.v.pins = (const struct hda_pintbl[]) {
874 			/* 0x17 was falsely set up as a mic, it should 0x1d */
875 			{ 0x17, 0x400001f0 },
876 			{ 0x1d, 0x97a70120 },
877 			{ }
878 		}
879 	},
880 	[CXT_FIXUP_STEREO_DMIC] = {
881 		.type = HDA_FIXUP_FUNC,
882 		.v.func = cxt_fixup_stereo_dmic,
883 	},
884 	[CXT_PINCFG_LENOVO_NOTEBOOK] = {
885 		.type = HDA_FIXUP_PINS,
886 		.v.pins = (const struct hda_pintbl[]) {
887 			{ 0x1a, 0x05d71030 },
888 			{ }
889 		},
890 		.chain_id = CXT_FIXUP_STEREO_DMIC,
891 	},
892 	[CXT_FIXUP_INC_MIC_BOOST] = {
893 		.type = HDA_FIXUP_FUNC,
894 		.v.func = cxt5066_increase_mic_boost,
895 	},
896 	[CXT_FIXUP_HEADPHONE_MIC_PIN] = {
897 		.type = HDA_FIXUP_PINS,
898 		.chained = true,
899 		.chain_id = CXT_FIXUP_HEADPHONE_MIC,
900 		.v.pins = (const struct hda_pintbl[]) {
901 			{ 0x18, 0x03a1913d }, /* use as headphone mic, without its own jack detect */
902 			{ }
903 		}
904 	},
905 	[CXT_FIXUP_HEADPHONE_MIC] = {
906 		.type = HDA_FIXUP_FUNC,
907 		.v.func = cxt_fixup_headphone_mic,
908 	},
909 	[CXT_FIXUP_GPIO1] = {
910 		.type = HDA_FIXUP_VERBS,
911 		.v.verbs = (const struct hda_verb[]) {
912 			{ 0x01, AC_VERB_SET_GPIO_MASK, 0x01 },
913 			{ 0x01, AC_VERB_SET_GPIO_DIRECTION, 0x01 },
914 			{ 0x01, AC_VERB_SET_GPIO_DATA, 0x01 },
915 			{ }
916 		},
917 	},
918 	[CXT_FIXUP_ASPIRE_DMIC] = {
919 		.type = HDA_FIXUP_FUNC,
920 		.v.func = cxt_fixup_stereo_dmic,
921 		.chained = true,
922 		.chain_id = CXT_FIXUP_GPIO1,
923 	},
924 	[CXT_FIXUP_THINKPAD_ACPI] = {
925 		.type = HDA_FIXUP_FUNC,
926 		.v.func = hda_fixup_thinkpad_acpi,
927 	},
928 	[CXT_FIXUP_OLPC_XO] = {
929 		.type = HDA_FIXUP_FUNC,
930 		.v.func = cxt_fixup_olpc_xo,
931 	},
932 	[CXT_FIXUP_CAP_MIX_AMP] = {
933 		.type = HDA_FIXUP_FUNC,
934 		.v.func = cxt_fixup_cap_mix_amp,
935 	},
936 	[CXT_FIXUP_TOSHIBA_P105] = {
937 		.type = HDA_FIXUP_PINS,
938 		.v.pins = (const struct hda_pintbl[]) {
939 			{ 0x10, 0x961701f0 }, /* speaker/hp */
940 			{ 0x12, 0x02a1901e }, /* ext mic */
941 			{ 0x14, 0x95a70110 }, /* int mic */
942 			{}
943 		},
944 	},
945 	[CXT_FIXUP_HP_530] = {
946 		.type = HDA_FIXUP_PINS,
947 		.v.pins = (const struct hda_pintbl[]) {
948 			{ 0x12, 0x90a60160 }, /* int mic */
949 			{}
950 		},
951 		.chained = true,
952 		.chain_id = CXT_FIXUP_CAP_MIX_AMP,
953 	},
954 	[CXT_FIXUP_CAP_MIX_AMP_5047] = {
955 		.type = HDA_FIXUP_FUNC,
956 		.v.func = cxt_fixup_cap_mix_amp_5047,
957 	},
958 	[CXT_FIXUP_MUTE_LED_EAPD] = {
959 		.type = HDA_FIXUP_FUNC,
960 		.v.func = cxt_fixup_mute_led_eapd,
961 	},
962 	[CXT_FIXUP_HP_DOCK] = {
963 		.type = HDA_FIXUP_PINS,
964 		.v.pins = (const struct hda_pintbl[]) {
965 			{ 0x16, 0x21011020 }, /* line-out */
966 			{ 0x18, 0x2181103f }, /* line-in */
967 			{ }
968 		},
969 		.chained = true,
970 		.chain_id = CXT_FIXUP_MUTE_LED_GPIO,
971 	},
972 	[CXT_FIXUP_HP_SPECTRE] = {
973 		.type = HDA_FIXUP_PINS,
974 		.v.pins = (const struct hda_pintbl[]) {
975 			/* enable NID 0x1d for the speaker on top */
976 			{ 0x1d, 0x91170111 },
977 			{ }
978 		}
979 	},
980 	[CXT_FIXUP_HP_GATE_MIC] = {
981 		.type = HDA_FIXUP_FUNC,
982 		.v.func = cxt_fixup_hp_gate_mic_jack,
983 	},
984 	[CXT_FIXUP_MUTE_LED_GPIO] = {
985 		.type = HDA_FIXUP_FUNC,
986 		.v.func = cxt_fixup_mute_led_gpio,
987 	},
988 	[CXT_FIXUP_HP_ELITEONE_OUT_DIS] = {
989 		.type = HDA_FIXUP_FUNC,
990 		.v.func = cxt_fixup_update_pinctl,
991 	},
992 	[CXT_FIXUP_HP_ZBOOK_MUTE_LED] = {
993 		.type = HDA_FIXUP_FUNC,
994 		.v.func = cxt_fixup_hp_zbook_mute_led,
995 	},
996 	[CXT_FIXUP_HEADSET_MIC] = {
997 		.type = HDA_FIXUP_FUNC,
998 		.v.func = cxt_fixup_headset_mic,
999 	},
1000 	[CXT_FIXUP_HP_MIC_NO_PRESENCE] = {
1001 		.type = HDA_FIXUP_PINS,
1002 		.v.pins = (const struct hda_pintbl[]) {
1003 			{ 0x1a, 0x02a1113c },
1004 			{ }
1005 		},
1006 		.chained = true,
1007 		.chain_id = CXT_FIXUP_HEADSET_MIC,
1008 	},
1009 	[CXT_PINCFG_SWS_JS201D] = {
1010 		.type = HDA_FIXUP_PINS,
1011 		.v.pins = cxt_pincfg_sws_js201d,
1012 	},
1013 	[CXT_PINCFG_TOP_SPEAKER] = {
1014 		.type = HDA_FIXUP_FUNC,
1015 		.v.func = cxt_fixup_sirius_top_speaker,
1016 	},
1017 };
1018 
1019 static const struct snd_pci_quirk cxt5045_fixups[] = {
1020 	SND_PCI_QUIRK(0x103c, 0x30d5, "HP 530", CXT_FIXUP_HP_530),
1021 	SND_PCI_QUIRK(0x1179, 0xff31, "Toshiba P105", CXT_FIXUP_TOSHIBA_P105),
1022 	/* HP, Packard Bell, Fujitsu-Siemens & Lenovo laptops have
1023 	 * really bad sound over 0dB on NID 0x17.
1024 	 */
1025 	SND_PCI_QUIRK_VENDOR(0x103c, "HP", CXT_FIXUP_CAP_MIX_AMP),
1026 	SND_PCI_QUIRK_VENDOR(0x1631, "Packard Bell", CXT_FIXUP_CAP_MIX_AMP),
1027 	SND_PCI_QUIRK_VENDOR(0x1734, "Fujitsu", CXT_FIXUP_CAP_MIX_AMP),
1028 	SND_PCI_QUIRK_VENDOR(0x17aa, "Lenovo", CXT_FIXUP_CAP_MIX_AMP),
1029 	{}
1030 };
1031 
1032 static const struct hda_model_fixup cxt5045_fixup_models[] = {
1033 	{ .id = CXT_FIXUP_CAP_MIX_AMP, .name = "cap-mix-amp" },
1034 	{ .id = CXT_FIXUP_TOSHIBA_P105, .name = "toshiba-p105" },
1035 	{ .id = CXT_FIXUP_HP_530, .name = "hp-530" },
1036 	{}
1037 };
1038 
1039 static const struct snd_pci_quirk cxt5047_fixups[] = {
1040 	/* HP laptops have really bad sound over 0 dB on NID 0x10.
1041 	 */
1042 	SND_PCI_QUIRK_VENDOR(0x103c, "HP", CXT_FIXUP_CAP_MIX_AMP_5047),
1043 	{}
1044 };
1045 
1046 static const struct hda_model_fixup cxt5047_fixup_models[] = {
1047 	{ .id = CXT_FIXUP_CAP_MIX_AMP_5047, .name = "cap-mix-amp" },
1048 	{}
1049 };
1050 
1051 static const struct snd_pci_quirk cxt5051_fixups[] = {
1052 	SND_PCI_QUIRK(0x103c, 0x360b, "Compaq CQ60", CXT_PINCFG_COMPAQ_CQ60),
1053 	SND_PCI_QUIRK(0x17aa, 0x20f2, "Lenovo X200", CXT_PINCFG_LENOVO_X200),
1054 	{}
1055 };
1056 
1057 static const struct hda_model_fixup cxt5051_fixup_models[] = {
1058 	{ .id = CXT_PINCFG_LENOVO_X200, .name = "lenovo-x200" },
1059 	{}
1060 };
1061 
1062 static const struct snd_pci_quirk cxt5066_fixups[] = {
1063 	SND_PCI_QUIRK(0x1025, 0x0543, "Acer Aspire One 522", CXT_FIXUP_STEREO_DMIC),
1064 	SND_PCI_QUIRK(0x1025, 0x054c, "Acer Aspire 3830TG", CXT_FIXUP_ASPIRE_DMIC),
1065 	SND_PCI_QUIRK(0x1025, 0x054f, "Acer Aspire 4830T", CXT_FIXUP_ASPIRE_DMIC),
1066 	SND_PCI_QUIRK(0x103c, 0x8079, "HP EliteBook 840 G3", CXT_FIXUP_HP_DOCK),
1067 	SND_PCI_QUIRK(0x103c, 0x807C, "HP EliteBook 820 G3", CXT_FIXUP_HP_DOCK),
1068 	SND_PCI_QUIRK(0x103c, 0x80FD, "HP ProBook 640 G2", CXT_FIXUP_HP_DOCK),
1069 	SND_PCI_QUIRK(0x103c, 0x8115, "HP Z1 Gen3", CXT_FIXUP_HP_GATE_MIC),
1070 	SND_PCI_QUIRK(0x103c, 0x814f, "HP ZBook 15u G3", CXT_FIXUP_MUTE_LED_GPIO),
1071 	SND_PCI_QUIRK(0x103c, 0x8174, "HP Spectre x360", CXT_FIXUP_HP_SPECTRE),
1072 	SND_PCI_QUIRK(0x103c, 0x822e, "HP ProBook 440 G4", CXT_FIXUP_MUTE_LED_GPIO),
1073 	SND_PCI_QUIRK(0x103c, 0x828c, "HP EliteBook 840 G4", CXT_FIXUP_HP_DOCK),
1074 	SND_PCI_QUIRK(0x103c, 0x8299, "HP 800 G3 SFF", CXT_FIXUP_HP_MIC_NO_PRESENCE),
1075 	SND_PCI_QUIRK(0x103c, 0x829a, "HP 800 G3 DM", CXT_FIXUP_HP_MIC_NO_PRESENCE),
1076 	SND_PCI_QUIRK(0x103c, 0x82b4, "HP ProDesk 600 G3", CXT_FIXUP_HP_MIC_NO_PRESENCE),
1077 	SND_PCI_QUIRK(0x103c, 0x836e, "HP ProBook 455 G5", CXT_FIXUP_MUTE_LED_GPIO),
1078 	SND_PCI_QUIRK(0x103c, 0x837f, "HP ProBook 470 G5", CXT_FIXUP_MUTE_LED_GPIO),
1079 	SND_PCI_QUIRK(0x103c, 0x83b2, "HP EliteBook 840 G5", CXT_FIXUP_HP_DOCK),
1080 	SND_PCI_QUIRK(0x103c, 0x83b3, "HP EliteBook 830 G5", CXT_FIXUP_HP_DOCK),
1081 	SND_PCI_QUIRK(0x103c, 0x83d3, "HP ProBook 640 G4", CXT_FIXUP_HP_DOCK),
1082 	SND_PCI_QUIRK(0x103c, 0x83e5, "HP EliteOne 1000 G2", CXT_FIXUP_HP_ELITEONE_OUT_DIS),
1083 	SND_PCI_QUIRK(0x103c, 0x8402, "HP ProBook 645 G4", CXT_FIXUP_MUTE_LED_GPIO),
1084 	SND_PCI_QUIRK(0x103c, 0x8427, "HP ZBook Studio G5", CXT_FIXUP_HP_ZBOOK_MUTE_LED),
1085 	SND_PCI_QUIRK(0x103c, 0x844f, "HP ZBook Studio G5", CXT_FIXUP_HP_ZBOOK_MUTE_LED),
1086 	SND_PCI_QUIRK(0x103c, 0x8455, "HP Z2 G4", CXT_FIXUP_HP_MIC_NO_PRESENCE),
1087 	SND_PCI_QUIRK(0x103c, 0x8456, "HP Z2 G4 SFF", CXT_FIXUP_HP_MIC_NO_PRESENCE),
1088 	SND_PCI_QUIRK(0x103c, 0x8457, "HP Z2 G4 mini", CXT_FIXUP_HP_MIC_NO_PRESENCE),
1089 	SND_PCI_QUIRK(0x103c, 0x8458, "HP Z2 G4 mini premium", CXT_FIXUP_HP_MIC_NO_PRESENCE),
1090 	SND_PCI_QUIRK(0x1043, 0x138d, "Asus", CXT_FIXUP_HEADPHONE_MIC_PIN),
1091 	SND_PCI_QUIRK(0x14f1, 0x0265, "SWS JS201D", CXT_PINCFG_SWS_JS201D),
1092 	SND_PCI_QUIRK(0x152d, 0x0833, "OLPC XO-1.5", CXT_FIXUP_OLPC_XO),
1093 	SND_PCI_QUIRK(0x17aa, 0x20f2, "Lenovo T400", CXT_PINCFG_LENOVO_TP410),
1094 	SND_PCI_QUIRK(0x17aa, 0x215e, "Lenovo T410", CXT_PINCFG_LENOVO_TP410),
1095 	SND_PCI_QUIRK(0x17aa, 0x215f, "Lenovo T510", CXT_PINCFG_LENOVO_TP410),
1096 	SND_PCI_QUIRK(0x17aa, 0x21ce, "Lenovo T420", CXT_PINCFG_LENOVO_TP410),
1097 	SND_PCI_QUIRK(0x17aa, 0x21cf, "Lenovo T520", CXT_PINCFG_LENOVO_TP410),
1098 	SND_PCI_QUIRK(0x17aa, 0x21d2, "Lenovo T420s", CXT_PINCFG_LENOVO_TP410),
1099 	SND_PCI_QUIRK(0x17aa, 0x21da, "Lenovo X220", CXT_PINCFG_LENOVO_TP410),
1100 	SND_PCI_QUIRK(0x17aa, 0x21db, "Lenovo X220-tablet", CXT_PINCFG_LENOVO_TP410),
1101 	SND_PCI_QUIRK(0x17aa, 0x38af, "Lenovo IdeaPad Z560", CXT_FIXUP_MUTE_LED_EAPD),
1102 	SND_PCI_QUIRK(0x17aa, 0x3905, "Lenovo G50-30", CXT_FIXUP_STEREO_DMIC),
1103 	SND_PCI_QUIRK(0x17aa, 0x390b, "Lenovo G50-80", CXT_FIXUP_STEREO_DMIC),
1104 	SND_PCI_QUIRK(0x17aa, 0x3975, "Lenovo U300s", CXT_FIXUP_STEREO_DMIC),
1105 	/* NOTE: we'd need to extend the quirk for 17aa:3977 as the same
1106 	 * PCI SSID is used on multiple Lenovo models
1107 	 */
1108 	SND_PCI_QUIRK(0x17aa, 0x3977, "Lenovo IdeaPad U310", CXT_FIXUP_STEREO_DMIC),
1109 	SND_PCI_QUIRK(0x17aa, 0x3978, "Lenovo G50-70", CXT_FIXUP_STEREO_DMIC),
1110 	SND_PCI_QUIRK(0x17aa, 0x397b, "Lenovo S205", CXT_FIXUP_STEREO_DMIC),
1111 	SND_PCI_QUIRK_VENDOR(0x17aa, "Thinkpad", CXT_FIXUP_THINKPAD_ACPI),
1112 	SND_PCI_QUIRK(0x1c06, 0x2011, "Lemote A1004", CXT_PINCFG_LEMOTE_A1004),
1113 	SND_PCI_QUIRK(0x1c06, 0x2012, "Lemote A1205", CXT_PINCFG_LEMOTE_A1205),
1114 	SND_PCI_QUIRK(0x2782, 0x12c3, "Sirius Gen1", CXT_PINCFG_TOP_SPEAKER),
1115 	SND_PCI_QUIRK(0x2782, 0x12c5, "Sirius Gen2", CXT_PINCFG_TOP_SPEAKER),
1116 	{}
1117 };
1118 
1119 static const struct hda_model_fixup cxt5066_fixup_models[] = {
1120 	{ .id = CXT_FIXUP_STEREO_DMIC, .name = "stereo-dmic" },
1121 	{ .id = CXT_FIXUP_GPIO1, .name = "gpio1" },
1122 	{ .id = CXT_FIXUP_HEADPHONE_MIC_PIN, .name = "headphone-mic-pin" },
1123 	{ .id = CXT_PINCFG_LENOVO_TP410, .name = "tp410" },
1124 	{ .id = CXT_FIXUP_THINKPAD_ACPI, .name = "thinkpad" },
1125 	{ .id = CXT_PINCFG_LEMOTE_A1004, .name = "lemote-a1004" },
1126 	{ .id = CXT_PINCFG_LEMOTE_A1205, .name = "lemote-a1205" },
1127 	{ .id = CXT_FIXUP_OLPC_XO, .name = "olpc-xo" },
1128 	{ .id = CXT_FIXUP_MUTE_LED_EAPD, .name = "mute-led-eapd" },
1129 	{ .id = CXT_FIXUP_HP_DOCK, .name = "hp-dock" },
1130 	{ .id = CXT_FIXUP_MUTE_LED_GPIO, .name = "mute-led-gpio" },
1131 	{ .id = CXT_FIXUP_HP_ZBOOK_MUTE_LED, .name = "hp-zbook-mute-led" },
1132 	{ .id = CXT_FIXUP_HP_MIC_NO_PRESENCE, .name = "hp-mic-fix" },
1133 	{ .id = CXT_PINCFG_LENOVO_NOTEBOOK, .name = "lenovo-20149" },
1134 	{ .id = CXT_PINCFG_SWS_JS201D, .name = "sws-js201d" },
1135 	{ .id = CXT_PINCFG_TOP_SPEAKER, .name = "sirius-top-speaker" },
1136 	{}
1137 };
1138 
1139 /* add "fake" mute amp-caps to DACs on cx5051 so that mixer mute switches
1140  * can be created (bko#42825)
1141  */
add_cx5051_fake_mutes(struct hda_codec * codec)1142 static void add_cx5051_fake_mutes(struct hda_codec *codec)
1143 {
1144 	struct conexant_spec *spec = codec->spec;
1145 	static const hda_nid_t out_nids[] = {
1146 		0x10, 0x11, 0
1147 	};
1148 	const hda_nid_t *p;
1149 
1150 	for (p = out_nids; *p; p++)
1151 		snd_hda_override_amp_caps(codec, *p, HDA_OUTPUT,
1152 					  AC_AMPCAP_MIN_MUTE |
1153 					  query_amp_caps(codec, *p, HDA_OUTPUT));
1154 	spec->gen.dac_min_mute = true;
1155 }
1156 
patch_conexant_auto(struct hda_codec * codec)1157 static int patch_conexant_auto(struct hda_codec *codec)
1158 {
1159 	struct conexant_spec *spec;
1160 	int err;
1161 
1162 	codec_info(codec, "%s: BIOS auto-probing.\n", codec->core.chip_name);
1163 
1164 	spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1165 	if (!spec)
1166 		return -ENOMEM;
1167 	snd_hda_gen_spec_init(&spec->gen);
1168 	codec->spec = spec;
1169 	codec->patch_ops = cx_auto_patch_ops;
1170 
1171 	/* init cx8070/sn6140 flag and reset headset_present_flag */
1172 	switch (codec->core.vendor_id) {
1173 	case 0x14f11f86:
1174 	case 0x14f11f87:
1175 		spec->is_cx8070_sn6140 = true;
1176 		snd_hda_jack_detect_enable_callback(codec, 0x19, cx_update_headset_mic_vref);
1177 		break;
1178 	}
1179 
1180 	cx_auto_parse_eapd(codec);
1181 	spec->gen.own_eapd_ctl = 1;
1182 
1183 	switch (codec->core.vendor_id) {
1184 	case 0x14f15045:
1185 		codec->single_adc_amp = 1;
1186 		spec->gen.mixer_nid = 0x17;
1187 		spec->gen.add_stereo_mix_input = HDA_HINT_STEREO_MIX_AUTO;
1188 		snd_hda_pick_fixup(codec, cxt5045_fixup_models,
1189 				   cxt5045_fixups, cxt_fixups);
1190 		break;
1191 	case 0x14f15047:
1192 		codec->pin_amp_workaround = 1;
1193 		spec->gen.mixer_nid = 0x19;
1194 		spec->gen.add_stereo_mix_input = HDA_HINT_STEREO_MIX_AUTO;
1195 		snd_hda_pick_fixup(codec, cxt5047_fixup_models,
1196 				   cxt5047_fixups, cxt_fixups);
1197 		break;
1198 	case 0x14f15051:
1199 		add_cx5051_fake_mutes(codec);
1200 		codec->pin_amp_workaround = 1;
1201 		snd_hda_pick_fixup(codec, cxt5051_fixup_models,
1202 				   cxt5051_fixups, cxt_fixups);
1203 		break;
1204 	case 0x14f15098:
1205 		codec->pin_amp_workaround = 1;
1206 		spec->gen.mixer_nid = 0x22;
1207 		spec->gen.add_stereo_mix_input = HDA_HINT_STEREO_MIX_AUTO;
1208 		snd_hda_pick_fixup(codec, cxt5066_fixup_models,
1209 				   cxt5066_fixups, cxt_fixups);
1210 		break;
1211 	case 0x14f150f2:
1212 		codec->power_save_node = 1;
1213 		fallthrough;
1214 	default:
1215 		codec->pin_amp_workaround = 1;
1216 		snd_hda_pick_fixup(codec, cxt5066_fixup_models,
1217 				   cxt5066_fixups, cxt_fixups);
1218 		break;
1219 	}
1220 
1221 	if (!spec->gen.vmaster_mute.hook && spec->dynamic_eapd)
1222 		spec->gen.vmaster_mute.hook = cx_auto_vmaster_hook;
1223 
1224 	snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
1225 
1226 	err = snd_hda_parse_pin_defcfg(codec, &spec->gen.autocfg, NULL,
1227 				       spec->parse_flags);
1228 	if (err < 0)
1229 		goto error;
1230 
1231 	err = cx_auto_parse_beep(codec);
1232 	if (err < 0)
1233 		goto error;
1234 
1235 	err = snd_hda_gen_parse_auto_config(codec, &spec->gen.autocfg);
1236 	if (err < 0)
1237 		goto error;
1238 
1239 	/* Some laptops with Conexant chips show stalls in S3 resume,
1240 	 * which falls into the single-cmd mode.
1241 	 * Better to make reset, then.
1242 	 */
1243 	if (!codec->bus->core.sync_write) {
1244 		codec_info(codec,
1245 			   "Enable sync_write for stable communication\n");
1246 		codec->bus->core.sync_write = 1;
1247 		codec->bus->allow_bus_reset = 1;
1248 	}
1249 
1250 	snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PROBE);
1251 
1252 	return 0;
1253 
1254  error:
1255 	cx_auto_free(codec);
1256 	return err;
1257 }
1258 
1259 /*
1260  */
1261 
1262 static const struct hda_device_id snd_hda_id_conexant[] = {
1263 	HDA_CODEC_ENTRY(0x14f11f86, "CX8070", patch_conexant_auto),
1264 	HDA_CODEC_ENTRY(0x14f11f87, "SN6140", patch_conexant_auto),
1265 	HDA_CODEC_ENTRY(0x14f12008, "CX8200", patch_conexant_auto),
1266 	HDA_CODEC_ENTRY(0x14f120d0, "CX11970", patch_conexant_auto),
1267 	HDA_CODEC_ENTRY(0x14f120d1, "SN6180", patch_conexant_auto),
1268 	HDA_CODEC_ENTRY(0x14f15045, "CX20549 (Venice)", patch_conexant_auto),
1269 	HDA_CODEC_ENTRY(0x14f15047, "CX20551 (Waikiki)", patch_conexant_auto),
1270 	HDA_CODEC_ENTRY(0x14f15051, "CX20561 (Hermosa)", patch_conexant_auto),
1271 	HDA_CODEC_ENTRY(0x14f15066, "CX20582 (Pebble)", patch_conexant_auto),
1272 	HDA_CODEC_ENTRY(0x14f15067, "CX20583 (Pebble HSF)", patch_conexant_auto),
1273 	HDA_CODEC_ENTRY(0x14f15068, "CX20584", patch_conexant_auto),
1274 	HDA_CODEC_ENTRY(0x14f15069, "CX20585", patch_conexant_auto),
1275 	HDA_CODEC_ENTRY(0x14f1506c, "CX20588", patch_conexant_auto),
1276 	HDA_CODEC_ENTRY(0x14f1506e, "CX20590", patch_conexant_auto),
1277 	HDA_CODEC_ENTRY(0x14f15097, "CX20631", patch_conexant_auto),
1278 	HDA_CODEC_ENTRY(0x14f15098, "CX20632", patch_conexant_auto),
1279 	HDA_CODEC_ENTRY(0x14f150a1, "CX20641", patch_conexant_auto),
1280 	HDA_CODEC_ENTRY(0x14f150a2, "CX20642", patch_conexant_auto),
1281 	HDA_CODEC_ENTRY(0x14f150ab, "CX20651", patch_conexant_auto),
1282 	HDA_CODEC_ENTRY(0x14f150ac, "CX20652", patch_conexant_auto),
1283 	HDA_CODEC_ENTRY(0x14f150b8, "CX20664", patch_conexant_auto),
1284 	HDA_CODEC_ENTRY(0x14f150b9, "CX20665", patch_conexant_auto),
1285 	HDA_CODEC_ENTRY(0x14f150f1, "CX21722", patch_conexant_auto),
1286 	HDA_CODEC_ENTRY(0x14f150f2, "CX20722", patch_conexant_auto),
1287 	HDA_CODEC_ENTRY(0x14f150f3, "CX21724", patch_conexant_auto),
1288 	HDA_CODEC_ENTRY(0x14f150f4, "CX20724", patch_conexant_auto),
1289 	HDA_CODEC_ENTRY(0x14f1510f, "CX20751/2", patch_conexant_auto),
1290 	HDA_CODEC_ENTRY(0x14f15110, "CX20751/2", patch_conexant_auto),
1291 	HDA_CODEC_ENTRY(0x14f15111, "CX20753/4", patch_conexant_auto),
1292 	HDA_CODEC_ENTRY(0x14f15113, "CX20755", patch_conexant_auto),
1293 	HDA_CODEC_ENTRY(0x14f15114, "CX20756", patch_conexant_auto),
1294 	HDA_CODEC_ENTRY(0x14f15115, "CX20757", patch_conexant_auto),
1295 	HDA_CODEC_ENTRY(0x14f151d7, "CX20952", patch_conexant_auto),
1296 	{} /* terminator */
1297 };
1298 MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_conexant);
1299 
1300 MODULE_LICENSE("GPL");
1301 MODULE_DESCRIPTION("Conexant HD-audio codec");
1302 
1303 static struct hda_codec_driver conexant_driver = {
1304 	.id = snd_hda_id_conexant,
1305 };
1306 
1307 module_hda_codec_driver(conexant_driver);
1308