xref: /linux/sound/hda/codecs/realtek/realtek.c (revision 177bf8620cf4ed290ee170a6c5966adc0924b336)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 //
3 // Realtek HD-audio codec support code
4 //
5 
6 #include <linux/init.h>
7 #include <linux/module.h>
8 #include "realtek.h"
9 
10 /*
11  * COEF access helper functions
12  */
13 
coef_mutex_lock(struct hda_codec * codec)14 static void coef_mutex_lock(struct hda_codec *codec)
15 {
16 	struct alc_spec *spec = codec->spec;
17 
18 	snd_hda_power_up_pm(codec);
19 	mutex_lock(&spec->coef_mutex);
20 }
21 
coef_mutex_unlock(struct hda_codec * codec)22 static void coef_mutex_unlock(struct hda_codec *codec)
23 {
24 	struct alc_spec *spec = codec->spec;
25 
26 	mutex_unlock(&spec->coef_mutex);
27 	snd_hda_power_down_pm(codec);
28 }
29 
__alc_read_coefex_idx(struct hda_codec * codec,hda_nid_t nid,unsigned int coef_idx)30 static int __alc_read_coefex_idx(struct hda_codec *codec, hda_nid_t nid,
31 				 unsigned int coef_idx)
32 {
33 	unsigned int val;
34 
35 	snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_COEF_INDEX, coef_idx);
36 	val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_PROC_COEF, 0);
37 	return val;
38 }
39 
alc_read_coefex_idx(struct hda_codec * codec,hda_nid_t nid,unsigned int coef_idx)40 int alc_read_coefex_idx(struct hda_codec *codec, hda_nid_t nid,
41 			unsigned int coef_idx)
42 {
43 	unsigned int val;
44 
45 	coef_mutex_lock(codec);
46 	val = __alc_read_coefex_idx(codec, nid, coef_idx);
47 	coef_mutex_unlock(codec);
48 	return val;
49 }
50 EXPORT_SYMBOL_NS_GPL(alc_read_coefex_idx, "SND_HDA_CODEC_REALTEK");
51 
__alc_write_coefex_idx(struct hda_codec * codec,hda_nid_t nid,unsigned int coef_idx,unsigned int coef_val)52 static void __alc_write_coefex_idx(struct hda_codec *codec, hda_nid_t nid,
53 				   unsigned int coef_idx, unsigned int coef_val)
54 {
55 	snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_COEF_INDEX, coef_idx);
56 	snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PROC_COEF, coef_val);
57 }
58 
alc_write_coefex_idx(struct hda_codec * codec,hda_nid_t nid,unsigned int coef_idx,unsigned int coef_val)59 void alc_write_coefex_idx(struct hda_codec *codec, hda_nid_t nid,
60 			  unsigned int coef_idx, unsigned int coef_val)
61 {
62 	coef_mutex_lock(codec);
63 	__alc_write_coefex_idx(codec, nid, coef_idx, coef_val);
64 	coef_mutex_unlock(codec);
65 }
66 EXPORT_SYMBOL_NS_GPL(alc_write_coefex_idx, "SND_HDA_CODEC_REALTEK");
67 
__alc_update_coefex_idx(struct hda_codec * codec,hda_nid_t nid,unsigned int coef_idx,unsigned int mask,unsigned int bits_set)68 static void __alc_update_coefex_idx(struct hda_codec *codec, hda_nid_t nid,
69 				    unsigned int coef_idx, unsigned int mask,
70 				    unsigned int bits_set)
71 {
72 	unsigned int val = __alc_read_coefex_idx(codec, nid, coef_idx);
73 
74 	if (val != -1)
75 		__alc_write_coefex_idx(codec, nid, coef_idx,
76 				       (val & ~mask) | bits_set);
77 }
78 
alc_update_coefex_idx(struct hda_codec * codec,hda_nid_t nid,unsigned int coef_idx,unsigned int mask,unsigned int bits_set)79 void alc_update_coefex_idx(struct hda_codec *codec, hda_nid_t nid,
80 			   unsigned int coef_idx, unsigned int mask,
81 			   unsigned int bits_set)
82 {
83 	coef_mutex_lock(codec);
84 	__alc_update_coefex_idx(codec, nid, coef_idx, mask, bits_set);
85 	coef_mutex_unlock(codec);
86 }
87 EXPORT_SYMBOL_NS_GPL(alc_update_coefex_idx, "SND_HDA_CODEC_REALTEK");
88 
89 /* a special bypass for COEF 0; read the cached value at the second time */
alc_get_coef0(struct hda_codec * codec)90 unsigned int alc_get_coef0(struct hda_codec *codec)
91 {
92 	struct alc_spec *spec = codec->spec;
93 
94 	if (!spec->coef0)
95 		spec->coef0 = alc_read_coef_idx(codec, 0);
96 	return spec->coef0;
97 }
98 EXPORT_SYMBOL_NS_GPL(alc_get_coef0, "SND_HDA_CODEC_REALTEK");
99 
alc_process_coef_fw(struct hda_codec * codec,const struct coef_fw * fw)100 void alc_process_coef_fw(struct hda_codec *codec, const struct coef_fw *fw)
101 {
102 	coef_mutex_lock(codec);
103 	for (; fw->nid; fw++) {
104 		if (fw->mask == (unsigned short)-1)
105 			__alc_write_coefex_idx(codec, fw->nid, fw->idx, fw->val);
106 		else
107 			__alc_update_coefex_idx(codec, fw->nid, fw->idx,
108 						fw->mask, fw->val);
109 	}
110 	coef_mutex_unlock(codec);
111 }
112 EXPORT_SYMBOL_NS_GPL(alc_process_coef_fw, "SND_HDA_CODEC_REALTEK");
113 
114 /*
115  * GPIO setup tables, used in initialization
116  */
117 
118 /* Enable GPIO mask and set output */
alc_setup_gpio(struct hda_codec * codec,unsigned int mask)119 void alc_setup_gpio(struct hda_codec *codec, unsigned int mask)
120 {
121 	struct alc_spec *spec = codec->spec;
122 
123 	spec->gpio_mask |= mask;
124 	spec->gpio_dir |= mask;
125 	spec->gpio_data |= mask;
126 }
127 EXPORT_SYMBOL_NS_GPL(alc_setup_gpio, "SND_HDA_CODEC_REALTEK");
128 
alc_write_gpio_data(struct hda_codec * codec)129 void alc_write_gpio_data(struct hda_codec *codec)
130 {
131 	struct alc_spec *spec = codec->spec;
132 
133 	snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA,
134 			    spec->gpio_data);
135 }
136 EXPORT_SYMBOL_NS_GPL(alc_write_gpio_data, "SND_HDA_CODEC_REALTEK");
137 
alc_update_gpio_data(struct hda_codec * codec,unsigned int mask,bool on)138 void alc_update_gpio_data(struct hda_codec *codec, unsigned int mask,
139 			  bool on)
140 {
141 	struct alc_spec *spec = codec->spec;
142 	unsigned int oldval = spec->gpio_data;
143 
144 	if (on)
145 		spec->gpio_data |= mask;
146 	else
147 		spec->gpio_data &= ~mask;
148 	if (oldval != spec->gpio_data)
149 		alc_write_gpio_data(codec);
150 }
151 EXPORT_SYMBOL_NS_GPL(alc_update_gpio_data, "SND_HDA_CODEC_REALTEK");
152 
alc_write_gpio(struct hda_codec * codec)153 void alc_write_gpio(struct hda_codec *codec)
154 {
155 	struct alc_spec *spec = codec->spec;
156 
157 	if (!spec->gpio_mask)
158 		return;
159 
160 	snd_hda_codec_write(codec, codec->core.afg, 0,
161 			    AC_VERB_SET_GPIO_MASK, spec->gpio_mask);
162 	snd_hda_codec_write(codec, codec->core.afg, 0,
163 			    AC_VERB_SET_GPIO_DIRECTION, spec->gpio_dir);
164 	if (spec->gpio_write_delay)
165 		msleep(1);
166 	alc_write_gpio_data(codec);
167 }
168 EXPORT_SYMBOL_NS_GPL(alc_write_gpio, "SND_HDA_CODEC_REALTEK");
169 
alc_fixup_gpio(struct hda_codec * codec,int action,unsigned int mask)170 void alc_fixup_gpio(struct hda_codec *codec, int action, unsigned int mask)
171 {
172 	if (action == HDA_FIXUP_ACT_PRE_PROBE)
173 		alc_setup_gpio(codec, mask);
174 }
175 EXPORT_SYMBOL_NS_GPL(alc_fixup_gpio, "SND_HDA_CODEC_REALTEK");
176 
alc_fixup_gpio1(struct hda_codec * codec,const struct hda_fixup * fix,int action)177 void alc_fixup_gpio1(struct hda_codec *codec,
178 		     const struct hda_fixup *fix, int action)
179 {
180 	alc_fixup_gpio(codec, action, 0x01);
181 }
182 EXPORT_SYMBOL_NS_GPL(alc_fixup_gpio1, "SND_HDA_CODEC_REALTEK");
183 
alc_fixup_gpio2(struct hda_codec * codec,const struct hda_fixup * fix,int action)184 void alc_fixup_gpio2(struct hda_codec *codec,
185 		     const struct hda_fixup *fix, int action)
186 {
187 	alc_fixup_gpio(codec, action, 0x02);
188 }
189 EXPORT_SYMBOL_NS_GPL(alc_fixup_gpio2, "SND_HDA_CODEC_REALTEK");
190 
alc_fixup_gpio3(struct hda_codec * codec,const struct hda_fixup * fix,int action)191 void alc_fixup_gpio3(struct hda_codec *codec,
192 		     const struct hda_fixup *fix, int action)
193 {
194 	alc_fixup_gpio(codec, action, 0x03);
195 }
196 EXPORT_SYMBOL_NS_GPL(alc_fixup_gpio3, "SND_HDA_CODEC_REALTEK");
197 
alc_fixup_gpio4(struct hda_codec * codec,const struct hda_fixup * fix,int action)198 void alc_fixup_gpio4(struct hda_codec *codec,
199 		     const struct hda_fixup *fix, int action)
200 {
201 	alc_fixup_gpio(codec, action, 0x04);
202 }
203 EXPORT_SYMBOL_NS_GPL(alc_fixup_gpio4, "SND_HDA_CODEC_REALTEK");
204 
alc_fixup_micmute_led(struct hda_codec * codec,const struct hda_fixup * fix,int action)205 void alc_fixup_micmute_led(struct hda_codec *codec,
206 			   const struct hda_fixup *fix, int action)
207 {
208 	if (action == HDA_FIXUP_ACT_PRE_PROBE)
209 		snd_hda_gen_add_micmute_led_cdev(codec, NULL);
210 }
211 EXPORT_SYMBOL_NS_GPL(alc_fixup_micmute_led, "SND_HDA_CODEC_REALTEK");
212 
213 /*
214  * Fix hardware PLL issue
215  * On some codecs, the analog PLL gating control must be off while
216  * the default value is 1.
217  */
alc_fix_pll(struct hda_codec * codec)218 void alc_fix_pll(struct hda_codec *codec)
219 {
220 	struct alc_spec *spec = codec->spec;
221 
222 	if (spec->pll_nid)
223 		alc_update_coefex_idx(codec, spec->pll_nid, spec->pll_coef_idx,
224 				      1 << spec->pll_coef_bit, 0);
225 }
226 EXPORT_SYMBOL_NS_GPL(alc_fix_pll, "SND_HDA_CODEC_REALTEK");
227 
alc_fix_pll_init(struct hda_codec * codec,hda_nid_t nid,unsigned int coef_idx,unsigned int coef_bit)228 void alc_fix_pll_init(struct hda_codec *codec, hda_nid_t nid,
229 		      unsigned int coef_idx, unsigned int coef_bit)
230 {
231 	struct alc_spec *spec = codec->spec;
232 	spec->pll_nid = nid;
233 	spec->pll_coef_idx = coef_idx;
234 	spec->pll_coef_bit = coef_bit;
235 	alc_fix_pll(codec);
236 }
237 EXPORT_SYMBOL_NS_GPL(alc_fix_pll_init, "SND_HDA_CODEC_REALTEK");
238 
239 /* update the master volume per volume-knob's unsol event */
alc_update_knob_master(struct hda_codec * codec,struct hda_jack_callback * jack)240 void alc_update_knob_master(struct hda_codec *codec,
241 			    struct hda_jack_callback *jack)
242 {
243 	unsigned int val;
244 	struct snd_kcontrol *kctl;
245 	struct snd_ctl_elem_value *uctl;
246 
247 	kctl = snd_hda_find_mixer_ctl(codec, "Master Playback Volume");
248 	if (!kctl)
249 		return;
250 	uctl = kzalloc(sizeof(*uctl), GFP_KERNEL);
251 	if (!uctl)
252 		return;
253 	val = snd_hda_codec_read(codec, jack->nid, 0,
254 				 AC_VERB_GET_VOLUME_KNOB_CONTROL, 0);
255 	val &= HDA_AMP_VOLMASK;
256 	uctl->value.integer.value[0] = val;
257 	uctl->value.integer.value[1] = val;
258 	kctl->put(kctl, uctl);
259 	kfree(uctl);
260 }
261 EXPORT_SYMBOL_NS_GPL(alc_update_knob_master, "SND_HDA_CODEC_REALTEK");
262 
263 /* Change EAPD to verb control */
alc_fill_eapd_coef(struct hda_codec * codec)264 void alc_fill_eapd_coef(struct hda_codec *codec)
265 {
266 	int coef;
267 
268 	coef = alc_get_coef0(codec);
269 
270 	switch (codec->core.vendor_id) {
271 	case 0x10ec0262:
272 		alc_update_coef_idx(codec, 0x7, 0, 1<<5);
273 		break;
274 	case 0x10ec0267:
275 	case 0x10ec0268:
276 		alc_update_coef_idx(codec, 0x7, 0, 1<<13);
277 		break;
278 	case 0x10ec0269:
279 		if ((coef & 0x00f0) == 0x0010)
280 			alc_update_coef_idx(codec, 0xd, 0, 1<<14);
281 		if ((coef & 0x00f0) == 0x0020)
282 			alc_update_coef_idx(codec, 0x4, 1<<15, 0);
283 		if ((coef & 0x00f0) == 0x0030)
284 			alc_update_coef_idx(codec, 0x10, 1<<9, 0);
285 		break;
286 	case 0x10ec0280:
287 	case 0x10ec0284:
288 	case 0x10ec0290:
289 	case 0x10ec0292:
290 		alc_update_coef_idx(codec, 0x4, 1<<15, 0);
291 		break;
292 	case 0x10ec0225:
293 	case 0x10ec0295:
294 	case 0x10ec0299:
295 		alc_update_coef_idx(codec, 0x67, 0xf000, 0x3000);
296 		fallthrough;
297 	case 0x10ec0215:
298 	case 0x10ec0236:
299 	case 0x10ec0245:
300 	case 0x10ec0256:
301 	case 0x10ec0257:
302 	case 0x10ec0285:
303 	case 0x10ec0289:
304 		alc_update_coef_idx(codec, 0x36, 1<<13, 0);
305 		fallthrough;
306 	case 0x10ec0230:
307 	case 0x10ec0233:
308 	case 0x10ec0235:
309 	case 0x10ec0255:
310 	case 0x19e58326:
311 	case 0x10ec0282:
312 	case 0x10ec0283:
313 	case 0x10ec0286:
314 	case 0x10ec0288:
315 	case 0x10ec0298:
316 	case 0x10ec0300:
317 		alc_update_coef_idx(codec, 0x10, 1<<9, 0);
318 		break;
319 	case 0x10ec0275:
320 		alc_update_coef_idx(codec, 0xe, 0, 1<<0);
321 		break;
322 	case 0x10ec0287:
323 		alc_update_coef_idx(codec, 0x10, 1<<9, 0);
324 		alc_write_coef_idx(codec, 0x8, 0x4ab7);
325 		break;
326 	case 0x10ec0293:
327 		alc_update_coef_idx(codec, 0xa, 1<<13, 0);
328 		break;
329 	case 0x10ec0234:
330 	case 0x10ec0274:
331 		alc_write_coef_idx(codec, 0x6e, 0x0c25);
332 		fallthrough;
333 	case 0x10ec0294:
334 	case 0x10ec0700:
335 	case 0x10ec0701:
336 	case 0x10ec0703:
337 	case 0x10ec0711:
338 		alc_update_coef_idx(codec, 0x10, 1<<15, 0);
339 		break;
340 	case 0x10ec0662:
341 		if ((coef & 0x00f0) == 0x0030)
342 			alc_update_coef_idx(codec, 0x4, 1<<10, 0); /* EAPD Ctrl */
343 		break;
344 	case 0x10ec0272:
345 	case 0x10ec0273:
346 	case 0x10ec0663:
347 	case 0x10ec0665:
348 	case 0x10ec0670:
349 	case 0x10ec0671:
350 	case 0x10ec0672:
351 		alc_update_coef_idx(codec, 0xd, 0, 1<<14); /* EAPD Ctrl */
352 		break;
353 	case 0x10ec0222:
354 	case 0x10ec0623:
355 		alc_update_coef_idx(codec, 0x19, 1<<13, 0);
356 		break;
357 	case 0x10ec0668:
358 		alc_update_coef_idx(codec, 0x7, 3<<13, 0);
359 		break;
360 	case 0x10ec0867:
361 		alc_update_coef_idx(codec, 0x4, 1<<10, 0);
362 		break;
363 	case 0x10ec0888:
364 		if ((coef & 0x00f0) == 0x0020 || (coef & 0x00f0) == 0x0030)
365 			alc_update_coef_idx(codec, 0x7, 1<<5, 0);
366 		break;
367 	case 0x10ec0892:
368 	case 0x10ec0897:
369 		alc_update_coef_idx(codec, 0x7, 1<<5, 0);
370 		break;
371 	case 0x10ec0899:
372 	case 0x10ec0900:
373 	case 0x10ec0b00:
374 	case 0x10ec1168:
375 	case 0x10ec1220:
376 		alc_update_coef_idx(codec, 0x7, 1<<1, 0);
377 		break;
378 	}
379 }
380 EXPORT_SYMBOL_NS_GPL(alc_fill_eapd_coef, "SND_HDA_CODEC_REALTEK");
381 
382 /* turn on/off EAPD control (only if available) */
set_eapd(struct hda_codec * codec,hda_nid_t nid,int on)383 static void set_eapd(struct hda_codec *codec, hda_nid_t nid, int on)
384 {
385 	if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
386 		return;
387 	if (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)
388 		snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_EAPD_BTLENABLE,
389 				    on ? 2 : 0);
390 }
391 
392 /* turn on/off EAPD controls of the codec */
alc_auto_setup_eapd(struct hda_codec * codec,bool on)393 void alc_auto_setup_eapd(struct hda_codec *codec, bool on)
394 {
395 	/* We currently only handle front, HP */
396 	static const hda_nid_t pins[] = {
397 		0x0f, 0x10, 0x14, 0x15, 0x17, 0
398 	};
399 	const hda_nid_t *p;
400 	for (p = pins; *p; p++)
401 		set_eapd(codec, *p, on);
402 }
403 EXPORT_SYMBOL_NS_GPL(alc_auto_setup_eapd, "SND_HDA_CODEC_REALTEK");
404 
405 /* Returns the nid of the external mic input pin, or 0 if it cannot be found. */
alc_find_ext_mic_pin(struct hda_codec * codec)406 int alc_find_ext_mic_pin(struct hda_codec *codec)
407 {
408 	struct alc_spec *spec = codec->spec;
409 	struct auto_pin_cfg *cfg = &spec->gen.autocfg;
410 	hda_nid_t nid;
411 	unsigned int defcfg;
412 	int i;
413 
414 	for (i = 0; i < cfg->num_inputs; i++) {
415 		if (cfg->inputs[i].type != AUTO_PIN_MIC)
416 			continue;
417 		nid = cfg->inputs[i].pin;
418 		defcfg = snd_hda_codec_get_pincfg(codec, nid);
419 		if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
420 			continue;
421 		return nid;
422 	}
423 
424 	return 0;
425 }
426 EXPORT_SYMBOL_NS_GPL(alc_find_ext_mic_pin, "SND_HDA_CODEC_REALTEK");
427 
alc_headset_mic_no_shutup(struct hda_codec * codec)428 void alc_headset_mic_no_shutup(struct hda_codec *codec)
429 {
430 	const struct hda_pincfg *pin;
431 	int mic_pin = alc_find_ext_mic_pin(codec);
432 	int i;
433 
434 	/* don't shut up pins when unloading the driver; otherwise it breaks
435 	 * the default pin setup at the next load of the driver
436 	 */
437 	if (codec->bus->shutdown)
438 		return;
439 
440 	snd_array_for_each(&codec->init_pins, i, pin) {
441 		/* use read here for syncing after issuing each verb */
442 		if (pin->nid != mic_pin)
443 			snd_hda_codec_read(codec, pin->nid, 0,
444 					AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
445 	}
446 
447 	codec->pins_shutup = 1;
448 }
449 EXPORT_SYMBOL_NS_GPL(alc_headset_mic_no_shutup, "SND_HDA_CODEC_REALTEK");
450 
alc_shutup_pins(struct hda_codec * codec)451 void alc_shutup_pins(struct hda_codec *codec)
452 {
453 	struct alc_spec *spec = codec->spec;
454 
455 	if (spec->no_shutup_pins)
456 		return;
457 
458 	switch (codec->core.vendor_id) {
459 	case 0x10ec0236:
460 	case 0x10ec0256:
461 	case 0x10ec0257:
462 	case 0x19e58326:
463 	case 0x10ec0283:
464 	case 0x10ec0285:
465 	case 0x10ec0286:
466 	case 0x10ec0287:
467 	case 0x10ec0288:
468 	case 0x10ec0295:
469 	case 0x10ec0298:
470 		alc_headset_mic_no_shutup(codec);
471 		break;
472 	default:
473 		snd_hda_shutup_pins(codec);
474 		break;
475 	}
476 }
477 EXPORT_SYMBOL_NS_GPL(alc_shutup_pins, "SND_HDA_CODEC_REALTEK");
478 
479 /* generic shutup callback;
480  * just turning off EAPD and a little pause for avoiding pop-noise
481  */
alc_eapd_shutup(struct hda_codec * codec)482 void alc_eapd_shutup(struct hda_codec *codec)
483 {
484 	struct alc_spec *spec = codec->spec;
485 
486 	alc_auto_setup_eapd(codec, false);
487 	if (!spec->no_depop_delay)
488 		msleep(200);
489 	alc_shutup_pins(codec);
490 }
491 EXPORT_SYMBOL_NS_GPL(alc_eapd_shutup, "SND_HDA_CODEC_REALTEK");
492 
493 /* additional initialization for ALC888 variants */
alc888_coef_init(struct hda_codec * codec)494 static void alc888_coef_init(struct hda_codec *codec)
495 {
496 	switch (alc_get_coef0(codec) & 0x00f0) {
497 	/* alc888-VA */
498 	case 0x00:
499 	/* alc888-VB */
500 	case 0x10:
501 		alc_update_coef_idx(codec, 7, 0, 0x2030); /* Turn EAPD to High */
502 		break;
503 	}
504 }
505 
506 /* generic EAPD initialization */
alc_auto_init_amp(struct hda_codec * codec,int type)507 void alc_auto_init_amp(struct hda_codec *codec, int type)
508 {
509 	alc_auto_setup_eapd(codec, true);
510 	alc_write_gpio(codec);
511 	switch (type) {
512 	case ALC_INIT_DEFAULT:
513 		switch (codec->core.vendor_id) {
514 		case 0x10ec0260:
515 			alc_update_coefex_idx(codec, 0x1a, 7, 0, 0x2010);
516 			break;
517 		case 0x10ec0880:
518 		case 0x10ec0882:
519 		case 0x10ec0883:
520 		case 0x10ec0885:
521 			alc_update_coef_idx(codec, 7, 0, 0x2030);
522 			break;
523 		case 0x10ec0888:
524 			alc888_coef_init(codec);
525 			break;
526 		}
527 		break;
528 	}
529 }
530 EXPORT_SYMBOL_NS_GPL(alc_auto_init_amp, "SND_HDA_CODEC_REALTEK");
531 
532 /* get a primary headphone pin if available */
alc_get_hp_pin(struct alc_spec * spec)533 hda_nid_t alc_get_hp_pin(struct alc_spec *spec)
534 {
535 	if (spec->gen.autocfg.hp_pins[0])
536 		return spec->gen.autocfg.hp_pins[0];
537 	if (spec->gen.autocfg.line_out_type == AC_JACK_HP_OUT)
538 		return spec->gen.autocfg.line_out_pins[0];
539 	return 0;
540 }
541 EXPORT_SYMBOL_NS_GPL(alc_get_hp_pin, "SND_HDA_CODEC_REALTEK");
542 
543 /*
544  * Realtek SSID verification
545  */
546 
547 /* Could be any non-zero and even value. When used as fixup, tells
548  * the driver to ignore any present sku defines.
549  */
550 #define ALC_FIXUP_SKU_IGNORE (2)
551 
alc_fixup_sku_ignore(struct hda_codec * codec,const struct hda_fixup * fix,int action)552 void alc_fixup_sku_ignore(struct hda_codec *codec,
553 			  const struct hda_fixup *fix, int action)
554 {
555 	struct alc_spec *spec = codec->spec;
556 	if (action == HDA_FIXUP_ACT_PRE_PROBE) {
557 		spec->cdefine.fixup = 1;
558 		spec->cdefine.sku_cfg = ALC_FIXUP_SKU_IGNORE;
559 	}
560 }
561 EXPORT_SYMBOL_NS_GPL(alc_fixup_sku_ignore, "SND_HDA_CODEC_REALTEK");
562 
alc_fixup_no_depop_delay(struct hda_codec * codec,const struct hda_fixup * fix,int action)563 void alc_fixup_no_depop_delay(struct hda_codec *codec,
564 			      const struct hda_fixup *fix, int action)
565 {
566 	struct alc_spec *spec = codec->spec;
567 
568 	if (action == HDA_FIXUP_ACT_PROBE) {
569 		spec->no_depop_delay = 1;
570 		codec->depop_delay = 0;
571 	}
572 }
573 EXPORT_SYMBOL_NS_GPL(alc_fixup_no_depop_delay, "SND_HDA_CODEC_REALTEK");
574 
alc_auto_parse_customize_define(struct hda_codec * codec)575 int alc_auto_parse_customize_define(struct hda_codec *codec)
576 {
577 	unsigned int ass, tmp, i;
578 	unsigned nid = 0;
579 	struct alc_spec *spec = codec->spec;
580 
581 	spec->cdefine.enable_pcbeep = 1; /* assume always enabled */
582 
583 	if (spec->cdefine.fixup) {
584 		ass = spec->cdefine.sku_cfg;
585 		if (ass == ALC_FIXUP_SKU_IGNORE)
586 			return -1;
587 		goto do_sku;
588 	}
589 
590 	if (!codec->bus->pci)
591 		return -1;
592 	ass = codec->core.subsystem_id & 0xffff;
593 	if (ass != codec->bus->pci->subsystem_device && (ass & 1))
594 		goto do_sku;
595 
596 	nid = 0x1d;
597 	if (codec->core.vendor_id == 0x10ec0260)
598 		nid = 0x17;
599 	ass = snd_hda_codec_get_pincfg(codec, nid);
600 
601 	if (!(ass & 1)) {
602 		codec_info(codec, "%s: SKU not ready 0x%08x\n",
603 			   codec->core.chip_name, ass);
604 		return -1;
605 	}
606 
607 	/* check sum */
608 	tmp = 0;
609 	for (i = 1; i < 16; i++) {
610 		if ((ass >> i) & 1)
611 			tmp++;
612 	}
613 	if (((ass >> 16) & 0xf) != tmp)
614 		return -1;
615 
616 	spec->cdefine.port_connectivity = ass >> 30;
617 	spec->cdefine.enable_pcbeep = (ass & 0x100000) >> 20;
618 	spec->cdefine.check_sum = (ass >> 16) & 0xf;
619 	spec->cdefine.customization = ass >> 8;
620 do_sku:
621 	spec->cdefine.sku_cfg = ass;
622 	spec->cdefine.external_amp = (ass & 0x38) >> 3;
623 	spec->cdefine.platform_type = (ass & 0x4) >> 2;
624 	spec->cdefine.swap = (ass & 0x2) >> 1;
625 	spec->cdefine.override = ass & 0x1;
626 
627 	codec_dbg(codec, "SKU: Nid=0x%x sku_cfg=0x%08x\n",
628 		   nid, spec->cdefine.sku_cfg);
629 	codec_dbg(codec, "SKU: port_connectivity=0x%x\n",
630 		   spec->cdefine.port_connectivity);
631 	codec_dbg(codec, "SKU: enable_pcbeep=0x%x\n", spec->cdefine.enable_pcbeep);
632 	codec_dbg(codec, "SKU: check_sum=0x%08x\n", spec->cdefine.check_sum);
633 	codec_dbg(codec, "SKU: customization=0x%08x\n", spec->cdefine.customization);
634 	codec_dbg(codec, "SKU: external_amp=0x%x\n", spec->cdefine.external_amp);
635 	codec_dbg(codec, "SKU: platform_type=0x%x\n", spec->cdefine.platform_type);
636 	codec_dbg(codec, "SKU: swap=0x%x\n", spec->cdefine.swap);
637 	codec_dbg(codec, "SKU: override=0x%x\n", spec->cdefine.override);
638 
639 	return 0;
640 }
641 EXPORT_SYMBOL_NS_GPL(alc_auto_parse_customize_define, "SND_HDA_CODEC_REALTEK");
642 
643 /* return the position of NID in the list, or -1 if not found */
find_idx_in_nid_list(hda_nid_t nid,const hda_nid_t * list,int nums)644 static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
645 {
646 	int i;
647 	for (i = 0; i < nums; i++)
648 		if (list[i] == nid)
649 			return i;
650 	return -1;
651 }
652 /* return true if the given NID is found in the list */
found_in_nid_list(hda_nid_t nid,const hda_nid_t * list,int nums)653 static bool found_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
654 {
655 	return find_idx_in_nid_list(nid, list, nums) >= 0;
656 }
657 
658 /* check subsystem ID and set up device-specific initialization;
659  * return 1 if initialized, 0 if invalid SSID
660  */
661 /* 32-bit subsystem ID for BIOS loading in HD Audio codec.
662  *	31 ~ 16 :	Manufacture ID
663  *	15 ~ 8	:	SKU ID
664  *	7  ~ 0	:	Assembly ID
665  *	port-A --> pin 39/41, port-E --> pin 14/15, port-D --> pin 35/36
666  */
alc_subsystem_id(struct hda_codec * codec,const hda_nid_t * ports)667 int alc_subsystem_id(struct hda_codec *codec, const hda_nid_t *ports)
668 {
669 	unsigned int ass, tmp, i;
670 	unsigned nid;
671 	struct alc_spec *spec = codec->spec;
672 
673 	if (spec->cdefine.fixup) {
674 		ass = spec->cdefine.sku_cfg;
675 		if (ass == ALC_FIXUP_SKU_IGNORE)
676 			return 0;
677 		goto do_sku;
678 	}
679 
680 	ass = codec->core.subsystem_id & 0xffff;
681 	if (codec->bus->pci &&
682 	    ass != codec->bus->pci->subsystem_device && (ass & 1))
683 		goto do_sku;
684 
685 	/* invalid SSID, check the special NID pin defcfg instead */
686 	/*
687 	 * 31~30	: port connectivity
688 	 * 29~21	: reserve
689 	 * 20		: PCBEEP input
690 	 * 19~16	: Check sum (15:1)
691 	 * 15~1		: Custom
692 	 * 0		: override
693 	*/
694 	nid = 0x1d;
695 	if (codec->core.vendor_id == 0x10ec0260)
696 		nid = 0x17;
697 	ass = snd_hda_codec_get_pincfg(codec, nid);
698 	codec_dbg(codec,
699 		  "realtek: No valid SSID, checking pincfg 0x%08x for NID 0x%x\n",
700 		   ass, nid);
701 	if (!(ass & 1))
702 		return 0;
703 	if ((ass >> 30) != 1)	/* no physical connection */
704 		return 0;
705 
706 	/* check sum */
707 	tmp = 0;
708 	for (i = 1; i < 16; i++) {
709 		if ((ass >> i) & 1)
710 			tmp++;
711 	}
712 	if (((ass >> 16) & 0xf) != tmp)
713 		return 0;
714 do_sku:
715 	codec_dbg(codec, "realtek: Enabling init ASM_ID=0x%04x CODEC_ID=%08x\n",
716 		   ass & 0xffff, codec->core.vendor_id);
717 	/*
718 	 * 0 : override
719 	 * 1 :	Swap Jack
720 	 * 2 : 0 --> Desktop, 1 --> Laptop
721 	 * 3~5 : External Amplifier control
722 	 * 7~6 : Reserved
723 	*/
724 	tmp = (ass & 0x38) >> 3;	/* external Amp control */
725 	if (spec->init_amp == ALC_INIT_UNDEFINED) {
726 		switch (tmp) {
727 		case 1:
728 			alc_setup_gpio(codec, 0x01);
729 			break;
730 		case 3:
731 			alc_setup_gpio(codec, 0x02);
732 			break;
733 		case 7:
734 			alc_setup_gpio(codec, 0x04);
735 			break;
736 		case 5:
737 		default:
738 			spec->init_amp = ALC_INIT_DEFAULT;
739 			break;
740 		}
741 	}
742 
743 	/* is laptop or Desktop and enable the function "Mute internal speaker
744 	 * when the external headphone out jack is plugged"
745 	 */
746 	if (!(ass & 0x8000))
747 		return 1;
748 	/*
749 	 * 10~8 : Jack location
750 	 * 12~11: Headphone out -> 00: PortA, 01: PortE, 02: PortD, 03: Resvered
751 	 * 14~13: Resvered
752 	 * 15   : 1 --> enable the function "Mute internal speaker
753 	 *	        when the external headphone out jack is plugged"
754 	 */
755 	if (!alc_get_hp_pin(spec)) {
756 		hda_nid_t nid;
757 		tmp = (ass >> 11) & 0x3;	/* HP to chassis */
758 		nid = ports[tmp];
759 		if (found_in_nid_list(nid, spec->gen.autocfg.line_out_pins,
760 				      spec->gen.autocfg.line_outs))
761 			return 1;
762 		spec->gen.autocfg.hp_pins[0] = nid;
763 	}
764 	return 1;
765 }
766 EXPORT_SYMBOL_NS_GPL(alc_subsystem_id, "SND_HDA_CODEC_REALTEK");
767 
768 /* Check the validity of ALC subsystem-id
769  * ports contains an array of 4 pin NIDs for port-A, E, D and I */
alc_ssid_check(struct hda_codec * codec,const hda_nid_t * ports)770 void alc_ssid_check(struct hda_codec *codec, const hda_nid_t *ports)
771 {
772 	if (!alc_subsystem_id(codec, ports)) {
773 		struct alc_spec *spec = codec->spec;
774 		if (spec->init_amp == ALC_INIT_UNDEFINED) {
775 			codec_dbg(codec,
776 				  "realtek: Enable default setup for auto mode as fallback\n");
777 			spec->init_amp = ALC_INIT_DEFAULT;
778 		}
779 	}
780 }
781 EXPORT_SYMBOL_NS_GPL(alc_ssid_check, "SND_HDA_CODEC_REALTEK");
782 
783 /* inverted digital-mic */
alc_fixup_inv_dmic(struct hda_codec * codec,const struct hda_fixup * fix,int action)784 void alc_fixup_inv_dmic(struct hda_codec *codec,
785 			const struct hda_fixup *fix, int action)
786 {
787 	struct alc_spec *spec = codec->spec;
788 
789 	spec->gen.inv_dmic_split = 1;
790 }
791 EXPORT_SYMBOL_NS_GPL(alc_fixup_inv_dmic, "SND_HDA_CODEC_REALTEK");
792 
alc_build_controls(struct hda_codec * codec)793 int alc_build_controls(struct hda_codec *codec)
794 {
795 	int err;
796 
797 	err = snd_hda_gen_build_controls(codec);
798 	if (err < 0)
799 		return err;
800 
801 	snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_BUILD);
802 	return 0;
803 }
804 EXPORT_SYMBOL_NS_GPL(alc_build_controls, "SND_HDA_CODEC_REALTEK");
805 
alc_init(struct hda_codec * codec)806 int alc_init(struct hda_codec *codec)
807 {
808 	struct alc_spec *spec = codec->spec;
809 
810 	/* hibernation resume needs the full chip initialization */
811 	if (is_s4_resume(codec))
812 		alc_pre_init(codec);
813 
814 	if (spec->init_hook)
815 		spec->init_hook(codec);
816 
817 	spec->gen.skip_verbs = 1; /* applied in below */
818 	snd_hda_gen_init(codec);
819 	alc_fix_pll(codec);
820 	alc_auto_init_amp(codec, spec->init_amp);
821 	snd_hda_apply_verbs(codec); /* apply verbs here after own init */
822 
823 	snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_INIT);
824 
825 	return 0;
826 }
827 EXPORT_SYMBOL_NS_GPL(alc_init, "SND_HDA_CODEC_REALTEK");
828 
alc_shutup(struct hda_codec * codec)829 void alc_shutup(struct hda_codec *codec)
830 {
831 	struct alc_spec *spec = codec->spec;
832 
833 	if (!snd_hda_get_bool_hint(codec, "shutup"))
834 		return; /* disabled explicitly by hints */
835 
836 	if (spec && spec->shutup)
837 		spec->shutup(codec);
838 	else
839 		alc_shutup_pins(codec);
840 }
841 EXPORT_SYMBOL_NS_GPL(alc_shutup, "SND_HDA_CODEC_REALTEK");
842 
alc_power_eapd(struct hda_codec * codec)843 void alc_power_eapd(struct hda_codec *codec)
844 {
845 	alc_auto_setup_eapd(codec, false);
846 }
847 EXPORT_SYMBOL_NS_GPL(alc_power_eapd, "SND_HDA_CODEC_REALTEK");
848 
alc_suspend(struct hda_codec * codec)849 int alc_suspend(struct hda_codec *codec)
850 {
851 	struct alc_spec *spec = codec->spec;
852 	alc_shutup(codec);
853 	if (spec && spec->power_hook)
854 		spec->power_hook(codec);
855 	return 0;
856 }
857 EXPORT_SYMBOL_NS_GPL(alc_suspend, "SND_HDA_CODEC_REALTEK");
858 
alc_resume(struct hda_codec * codec)859 int alc_resume(struct hda_codec *codec)
860 {
861 	struct alc_spec *spec = codec->spec;
862 
863 	if (!spec->no_depop_delay)
864 		msleep(150); /* to avoid pop noise */
865 	snd_hda_codec_init(codec);
866 	snd_hda_regmap_sync(codec);
867 	hda_call_check_power_status(codec, 0x01);
868 	return 0;
869 }
870 EXPORT_SYMBOL_NS_GPL(alc_resume, "SND_HDA_CODEC_REALTEK");
871 
872 /*
873  * Rename codecs appropriately from COEF value or subvendor id
874  */
875 struct alc_codec_rename_table {
876 	unsigned int vendor_id;
877 	unsigned short coef_mask;
878 	unsigned short coef_bits;
879 	const char *name;
880 };
881 
882 struct alc_codec_rename_pci_table {
883 	unsigned int codec_vendor_id;
884 	unsigned short pci_subvendor;
885 	unsigned short pci_subdevice;
886 	const char *name;
887 };
888 
889 static const struct alc_codec_rename_table rename_tbl[] = {
890 	{ 0x10ec0221, 0xf00f, 0x1003, "ALC231" },
891 	{ 0x10ec0269, 0xfff0, 0x3010, "ALC277" },
892 	{ 0x10ec0269, 0xf0f0, 0x2010, "ALC259" },
893 	{ 0x10ec0269, 0xf0f0, 0x3010, "ALC258" },
894 	{ 0x10ec0269, 0x00f0, 0x0010, "ALC269VB" },
895 	{ 0x10ec0269, 0xffff, 0xa023, "ALC259" },
896 	{ 0x10ec0269, 0xffff, 0x6023, "ALC281X" },
897 	{ 0x10ec0269, 0x00f0, 0x0020, "ALC269VC" },
898 	{ 0x10ec0269, 0x00f0, 0x0030, "ALC269VD" },
899 	{ 0x10ec0662, 0xffff, 0x4020, "ALC656" },
900 	{ 0x10ec0887, 0x00f0, 0x0030, "ALC887-VD" },
901 	{ 0x10ec0888, 0x00f0, 0x0030, "ALC888-VD" },
902 	{ 0x10ec0888, 0xf0f0, 0x3020, "ALC886" },
903 	{ 0x10ec0899, 0x2000, 0x2000, "ALC899" },
904 	{ 0x10ec0892, 0xffff, 0x8020, "ALC661" },
905 	{ 0x10ec0892, 0xffff, 0x8011, "ALC661" },
906 	{ 0x10ec0892, 0xffff, 0x4011, "ALC656" },
907 	{ } /* terminator */
908 };
909 
910 static const struct alc_codec_rename_pci_table rename_pci_tbl[] = {
911 	{ 0x10ec0280, 0x1028, 0, "ALC3220" },
912 	{ 0x10ec0282, 0x1028, 0, "ALC3221" },
913 	{ 0x10ec0283, 0x1028, 0, "ALC3223" },
914 	{ 0x10ec0288, 0x1028, 0, "ALC3263" },
915 	{ 0x10ec0292, 0x1028, 0, "ALC3226" },
916 	{ 0x10ec0293, 0x1028, 0, "ALC3235" },
917 	{ 0x10ec0255, 0x1028, 0, "ALC3234" },
918 	{ 0x10ec0668, 0x1028, 0, "ALC3661" },
919 	{ 0x10ec0275, 0x1028, 0, "ALC3260" },
920 	{ 0x10ec0899, 0x1028, 0, "ALC3861" },
921 	{ 0x10ec0298, 0x1028, 0, "ALC3266" },
922 	{ 0x10ec0236, 0x1028, 0, "ALC3204" },
923 	{ 0x10ec0256, 0x1028, 0, "ALC3246" },
924 	{ 0x10ec0225, 0x1028, 0, "ALC3253" },
925 	{ 0x10ec0295, 0x1028, 0, "ALC3254" },
926 	{ 0x10ec0299, 0x1028, 0, "ALC3271" },
927 	{ 0x10ec0670, 0x1025, 0, "ALC669X" },
928 	{ 0x10ec0676, 0x1025, 0, "ALC679X" },
929 	{ 0x10ec0282, 0x1043, 0, "ALC3229" },
930 	{ 0x10ec0233, 0x1043, 0, "ALC3236" },
931 	{ 0x10ec0280, 0x103c, 0, "ALC3228" },
932 	{ 0x10ec0282, 0x103c, 0, "ALC3227" },
933 	{ 0x10ec0286, 0x103c, 0, "ALC3242" },
934 	{ 0x10ec0290, 0x103c, 0, "ALC3241" },
935 	{ 0x10ec0668, 0x103c, 0, "ALC3662" },
936 	{ 0x10ec0283, 0x17aa, 0, "ALC3239" },
937 	{ 0x10ec0292, 0x17aa, 0, "ALC3232" },
938 	{ 0x10ec0257, 0x12f0, 0, "ALC3328" },
939 	{ } /* terminator */
940 };
941 
alc_codec_rename_from_preset(struct hda_codec * codec)942 static int alc_codec_rename_from_preset(struct hda_codec *codec)
943 {
944 	const struct alc_codec_rename_table *p;
945 	const struct alc_codec_rename_pci_table *q;
946 
947 	for (p = rename_tbl; p->vendor_id; p++) {
948 		if (p->vendor_id != codec->core.vendor_id)
949 			continue;
950 		if ((alc_get_coef0(codec) & p->coef_mask) == p->coef_bits)
951 			return alc_codec_rename(codec, p->name);
952 	}
953 
954 	if (!codec->bus->pci)
955 		return 0;
956 	for (q = rename_pci_tbl; q->codec_vendor_id; q++) {
957 		if (q->codec_vendor_id != codec->core.vendor_id)
958 			continue;
959 		if (q->pci_subvendor != codec->bus->pci->subsystem_vendor)
960 			continue;
961 		if (!q->pci_subdevice ||
962 		    q->pci_subdevice == codec->bus->pci->subsystem_device)
963 			return alc_codec_rename(codec, q->name);
964 	}
965 
966 	return 0;
967 }
968 
969 /*
970  * Digital-beep handlers
971  */
972 #ifdef CONFIG_SND_HDA_INPUT_BEEP
973 
974 /* additional beep mixers; private_value will be overwritten */
975 static const struct snd_kcontrol_new alc_beep_mixer[] = {
976 	HDA_CODEC_VOLUME("Beep Playback Volume", 0, 0, HDA_INPUT),
977 	HDA_CODEC_MUTE_BEEP("Beep Playback Switch", 0, 0, HDA_INPUT),
978 };
979 
980 /* set up and create beep controls */
alc_set_beep_amp(struct alc_spec * spec,hda_nid_t nid,int idx,int dir)981 int alc_set_beep_amp(struct alc_spec *spec, hda_nid_t nid, int idx, int dir)
982 {
983 	struct snd_kcontrol_new *knew;
984 	unsigned int beep_amp = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
985 	int i;
986 
987 	for (i = 0; i < ARRAY_SIZE(alc_beep_mixer); i++) {
988 		knew = snd_hda_gen_add_kctl(&spec->gen, NULL,
989 					    &alc_beep_mixer[i]);
990 		if (!knew)
991 			return -ENOMEM;
992 		knew->private_value = beep_amp;
993 	}
994 	return 0;
995 }
996 EXPORT_SYMBOL_NS_GPL(alc_set_beep_amp, "SND_HDA_CODEC_REALTEK");
997 
998 static const struct snd_pci_quirk beep_allow_list[] = {
999 	SND_PCI_QUIRK(0x1043, 0x103c, "ASUS", 1),
1000 	SND_PCI_QUIRK(0x1043, 0x115d, "ASUS", 1),
1001 	SND_PCI_QUIRK(0x1043, 0x829f, "ASUS", 1),
1002 	SND_PCI_QUIRK(0x1043, 0x8376, "EeePC", 1),
1003 	SND_PCI_QUIRK(0x1043, 0x83ce, "EeePC", 1),
1004 	SND_PCI_QUIRK(0x1043, 0x831a, "EeePC", 1),
1005 	SND_PCI_QUIRK(0x1043, 0x834a, "EeePC", 1),
1006 	SND_PCI_QUIRK(0x1458, 0xa002, "GA-MA790X", 1),
1007 	SND_PCI_QUIRK(0x8086, 0xd613, "Intel", 1),
1008 	/* denylist -- no beep available */
1009 	SND_PCI_QUIRK(0x17aa, 0x309e, "Lenovo ThinkCentre M73", 0),
1010 	SND_PCI_QUIRK(0x17aa, 0x30a3, "Lenovo ThinkCentre M93", 0),
1011 	{}
1012 };
1013 
alc_has_cdefine_beep(struct hda_codec * codec)1014 int alc_has_cdefine_beep(struct hda_codec *codec)
1015 {
1016 	struct alc_spec *spec = codec->spec;
1017 	const struct snd_pci_quirk *q;
1018 	q = snd_pci_quirk_lookup(codec->bus->pci, beep_allow_list);
1019 	if (q)
1020 		return q->value;
1021 	return spec->cdefine.enable_pcbeep;
1022 }
1023 EXPORT_SYMBOL_NS_GPL(alc_has_cdefine_beep, "SND_HDA_CODEC_REALTEK");
1024 
1025 #endif /* CONFIG_SND_HDA_INPUT_BEEP */
1026 
1027 /* parse the BIOS configuration and set up the alc_spec */
1028 /* return 1 if successful, 0 if the proper config is not found,
1029  * or a negative error code
1030  */
alc_parse_auto_config(struct hda_codec * codec,const hda_nid_t * ignore_nids,const hda_nid_t * ssid_nids)1031 int alc_parse_auto_config(struct hda_codec *codec,
1032 			  const hda_nid_t *ignore_nids,
1033 			  const hda_nid_t *ssid_nids)
1034 {
1035 	struct alc_spec *spec = codec->spec;
1036 	struct auto_pin_cfg *cfg = &spec->gen.autocfg;
1037 	int err;
1038 
1039 	err = snd_hda_parse_pin_defcfg(codec, cfg, ignore_nids,
1040 				       spec->parse_flags);
1041 	if (err < 0)
1042 		return err;
1043 
1044 	if (ssid_nids)
1045 		alc_ssid_check(codec, ssid_nids);
1046 
1047 	err = snd_hda_gen_parse_auto_config(codec, cfg);
1048 	if (err < 0)
1049 		return err;
1050 
1051 	return 1;
1052 }
1053 EXPORT_SYMBOL_NS_GPL(alc_parse_auto_config, "SND_HDA_CODEC_REALTEK");
1054 
1055 /* common preparation job for alc_spec */
alc_alloc_spec(struct hda_codec * codec,hda_nid_t mixer_nid)1056 int alc_alloc_spec(struct hda_codec *codec, hda_nid_t mixer_nid)
1057 {
1058 	struct alc_spec *spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1059 	int err;
1060 
1061 	if (!spec)
1062 		return -ENOMEM;
1063 	codec->spec = spec;
1064 	snd_hda_gen_spec_init(&spec->gen);
1065 	spec->gen.mixer_nid = mixer_nid;
1066 	spec->gen.own_eapd_ctl = 1;
1067 	codec->single_adc_amp = 1;
1068 	/* FIXME: do we need this for all Realtek codec models? */
1069 	codec->spdif_status_reset = 1;
1070 	codec->forced_resume = 1;
1071 	mutex_init(&spec->coef_mutex);
1072 
1073 	err = alc_codec_rename_from_preset(codec);
1074 	if (err < 0) {
1075 		kfree(spec);
1076 		return err;
1077 	}
1078 	return 0;
1079 }
1080 EXPORT_SYMBOL_NS_GPL(alc_alloc_spec, "SND_HDA_CODEC_REALTEK");
1081 
1082 /* For dual-codec configuration, we need to disable some features to avoid
1083  * conflicts of kctls and PCM streams
1084  */
alc_fixup_dual_codecs(struct hda_codec * codec,const struct hda_fixup * fix,int action)1085 void alc_fixup_dual_codecs(struct hda_codec *codec,
1086 			   const struct hda_fixup *fix, int action)
1087 {
1088 	struct alc_spec *spec = codec->spec;
1089 
1090 	if (action != HDA_FIXUP_ACT_PRE_PROBE)
1091 		return;
1092 	/* disable vmaster */
1093 	spec->gen.suppress_vmaster = 1;
1094 	/* auto-mute and auto-mic switch don't work with multiple codecs */
1095 	spec->gen.suppress_auto_mute = 1;
1096 	spec->gen.suppress_auto_mic = 1;
1097 	/* disable aamix as well */
1098 	spec->gen.mixer_nid = 0;
1099 	/* add location prefix to avoid conflicts */
1100 	codec->force_pin_prefix = 1;
1101 }
1102 EXPORT_SYMBOL_NS_GPL(alc_fixup_dual_codecs, "SND_HDA_CODEC_REALTEK");
1103 
1104 static const struct snd_pcm_chmap_elem asus_pcm_2_1_chmaps[] = {
1105 	{ .channels = 2,
1106 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
1107 	{ .channels = 4,
1108 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
1109 		   SNDRV_CHMAP_NA, SNDRV_CHMAP_LFE } }, /* LFE only on right */
1110 	{ }
1111 };
1112 
1113 /* override the 2.1 chmap */
alc_fixup_bass_chmap(struct hda_codec * codec,const struct hda_fixup * fix,int action)1114 void alc_fixup_bass_chmap(struct hda_codec *codec,
1115 			  const struct hda_fixup *fix, int action)
1116 {
1117 	if (action == HDA_FIXUP_ACT_BUILD) {
1118 		struct alc_spec *spec = codec->spec;
1119 		spec->gen.pcm_rec[0]->stream[0].chmap = asus_pcm_2_1_chmaps;
1120 	}
1121 }
1122 EXPORT_SYMBOL_NS_GPL(alc_fixup_bass_chmap, "SND_HDA_CODEC_REALTEK");
1123 
1124 /* exported as it's used by multiple codecs */
alc1220_fixup_gb_dual_codecs(struct hda_codec * codec,const struct hda_fixup * fix,int action)1125 void alc1220_fixup_gb_dual_codecs(struct hda_codec *codec,
1126 				  const struct hda_fixup *fix,
1127 				  int action)
1128 {
1129 	alc_fixup_dual_codecs(codec, fix, action);
1130 	switch (action) {
1131 	case HDA_FIXUP_ACT_PRE_PROBE:
1132 		/* override card longname to provide a unique UCM profile */
1133 		strscpy(codec->card->longname, "HDAudio-Gigabyte-ALC1220DualCodecs");
1134 		break;
1135 	case HDA_FIXUP_ACT_BUILD:
1136 		/* rename Capture controls depending on the codec */
1137 		rename_ctl(codec, "Capture Volume",
1138 			   codec->addr == 0 ?
1139 			   "Rear-Panel Capture Volume" :
1140 			   "Front-Panel Capture Volume");
1141 		rename_ctl(codec, "Capture Switch",
1142 			   codec->addr == 0 ?
1143 			   "Rear-Panel Capture Switch" :
1144 			   "Front-Panel Capture Switch");
1145 		break;
1146 	}
1147 }
1148 EXPORT_SYMBOL_NS_GPL(alc1220_fixup_gb_dual_codecs, "SND_HDA_CODEC_REALTEK");
1149 
alc233_alc662_fixup_lenovo_dual_codecs(struct hda_codec * codec,const struct hda_fixup * fix,int action)1150 void alc233_alc662_fixup_lenovo_dual_codecs(struct hda_codec *codec,
1151 					    const struct hda_fixup *fix,
1152 					    int action)
1153 {
1154 	alc_fixup_dual_codecs(codec, fix, action);
1155 	switch (action) {
1156 	case HDA_FIXUP_ACT_PRE_PROBE:
1157 		/* override card longname to provide a unique UCM profile */
1158 		strscpy(codec->card->longname, "HDAudio-Lenovo-DualCodecs");
1159 		break;
1160 	case HDA_FIXUP_ACT_BUILD:
1161 		/* rename Capture controls depending on the codec */
1162 		rename_ctl(codec, "Capture Volume",
1163 			   codec->addr == 0 ?
1164 			   "Rear-Panel Capture Volume" :
1165 			   "Front-Panel Capture Volume");
1166 		rename_ctl(codec, "Capture Switch",
1167 			   codec->addr == 0 ?
1168 			   "Rear-Panel Capture Switch" :
1169 			   "Front-Panel Capture Switch");
1170 		break;
1171 	}
1172 }
1173 EXPORT_SYMBOL_NS_GPL(alc233_alc662_fixup_lenovo_dual_codecs, "SND_HDA_CODEC_REALTEK");
1174 
alc_shutup_dell_xps13(struct hda_codec * codec)1175 static void alc_shutup_dell_xps13(struct hda_codec *codec)
1176 {
1177 	struct alc_spec *spec = codec->spec;
1178 	int hp_pin = alc_get_hp_pin(spec);
1179 
1180 	/* Prevent pop noises when headphones are plugged in */
1181 	snd_hda_codec_write(codec, hp_pin, 0,
1182 			    AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1183 	msleep(20);
1184 }
1185 
alc_fixup_dell_xps13(struct hda_codec * codec,const struct hda_fixup * fix,int action)1186 void alc_fixup_dell_xps13(struct hda_codec *codec,
1187 			  const struct hda_fixup *fix, int action)
1188 {
1189 	struct alc_spec *spec = codec->spec;
1190 	struct hda_input_mux *imux = &spec->gen.input_mux;
1191 	int i;
1192 
1193 	switch (action) {
1194 	case HDA_FIXUP_ACT_PRE_PROBE:
1195 		/* mic pin 0x19 must be initialized with Vref Hi-Z, otherwise
1196 		 * it causes a click noise at start up
1197 		 */
1198 		snd_hda_codec_set_pin_target(codec, 0x19, PIN_VREFHIZ);
1199 		spec->shutup = alc_shutup_dell_xps13;
1200 		break;
1201 	case HDA_FIXUP_ACT_PROBE:
1202 		/* Make the internal mic the default input source. */
1203 		for (i = 0; i < imux->num_items; i++) {
1204 			if (spec->gen.imux_pins[i] == 0x12) {
1205 				spec->gen.cur_mux[0] = i;
1206 				break;
1207 			}
1208 		}
1209 		break;
1210 	}
1211 }
1212 EXPORT_SYMBOL_NS_GPL(alc_fixup_dell_xps13, "SND_HDA_CODEC_REALTEK");
1213 
1214 /*
1215  * headset handling
1216  */
1217 
alc_hp_mute_disable(struct hda_codec * codec,unsigned int delay)1218 static void alc_hp_mute_disable(struct hda_codec *codec, unsigned int delay)
1219 {
1220 	if (delay <= 0)
1221 		delay = 75;
1222 	snd_hda_codec_write(codec, 0x21, 0,
1223 		    AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1224 	msleep(delay);
1225 	snd_hda_codec_write(codec, 0x21, 0,
1226 		    AC_VERB_SET_PIN_WIDGET_CONTROL, 0x0);
1227 	msleep(delay);
1228 }
1229 
alc_hp_enable_unmute(struct hda_codec * codec,unsigned int delay)1230 static void alc_hp_enable_unmute(struct hda_codec *codec, unsigned int delay)
1231 {
1232 	if (delay <= 0)
1233 		delay = 75;
1234 	snd_hda_codec_write(codec, 0x21, 0,
1235 		    AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
1236 	msleep(delay);
1237 	snd_hda_codec_write(codec, 0x21, 0,
1238 		    AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
1239 	msleep(delay);
1240 }
1241 
1242 static const struct coef_fw alc225_pre_hsmode[] = {
1243 	UPDATE_COEF(0x4a, 1<<8, 0),
1244 	UPDATE_COEFEX(0x57, 0x05, 1<<14, 0),
1245 	UPDATE_COEF(0x63, 3<<14, 3<<14),
1246 	UPDATE_COEF(0x4a, 3<<4, 2<<4),
1247 	UPDATE_COEF(0x4a, 3<<10, 3<<10),
1248 	UPDATE_COEF(0x45, 0x3f<<10, 0x34<<10),
1249 	UPDATE_COEF(0x4a, 3<<10, 0),
1250 	{}
1251 };
1252 
alc_headset_mode_unplugged(struct hda_codec * codec)1253 static void alc_headset_mode_unplugged(struct hda_codec *codec)
1254 {
1255 	struct alc_spec *spec = codec->spec;
1256 	static const struct coef_fw coef0255[] = {
1257 		WRITE_COEF(0x1b, 0x0c0b), /* LDO and MISC control */
1258 		WRITE_COEF(0x45, 0xd089), /* UAJ function set to menual mode */
1259 		UPDATE_COEFEX(0x57, 0x05, 1<<14, 0), /* Direct Drive HP Amp control(Set to verb control)*/
1260 		WRITE_COEF(0x06, 0x6104), /* Set MIC2 Vref gate with HP */
1261 		WRITE_COEFEX(0x57, 0x03, 0x8aa6), /* Direct Drive HP Amp control */
1262 		{}
1263 	};
1264 	static const struct coef_fw coef0256[] = {
1265 		WRITE_COEF(0x1b, 0x0c4b), /* LDO and MISC control */
1266 		WRITE_COEF(0x45, 0xd089), /* UAJ function set to menual mode */
1267 		WRITE_COEF(0x06, 0x6104), /* Set MIC2 Vref gate with HP */
1268 		WRITE_COEFEX(0x57, 0x03, 0x09a3), /* Direct Drive HP Amp control */
1269 		UPDATE_COEFEX(0x57, 0x05, 1<<14, 0), /* Direct Drive HP Amp control(Set to verb control)*/
1270 		{}
1271 	};
1272 	static const struct coef_fw coef0233[] = {
1273 		WRITE_COEF(0x1b, 0x0c0b),
1274 		WRITE_COEF(0x45, 0xc429),
1275 		UPDATE_COEF(0x35, 0x4000, 0),
1276 		WRITE_COEF(0x06, 0x2104),
1277 		WRITE_COEF(0x1a, 0x0001),
1278 		WRITE_COEF(0x26, 0x0004),
1279 		WRITE_COEF(0x32, 0x42a3),
1280 		{}
1281 	};
1282 	static const struct coef_fw coef0288[] = {
1283 		UPDATE_COEF(0x4f, 0xfcc0, 0xc400),
1284 		UPDATE_COEF(0x50, 0x2000, 0x2000),
1285 		UPDATE_COEF(0x56, 0x0006, 0x0006),
1286 		UPDATE_COEF(0x66, 0x0008, 0),
1287 		UPDATE_COEF(0x67, 0x2000, 0),
1288 		{}
1289 	};
1290 	static const struct coef_fw coef0298[] = {
1291 		UPDATE_COEF(0x19, 0x1300, 0x0300),
1292 		{}
1293 	};
1294 	static const struct coef_fw coef0292[] = {
1295 		WRITE_COEF(0x76, 0x000e),
1296 		WRITE_COEF(0x6c, 0x2400),
1297 		WRITE_COEF(0x18, 0x7308),
1298 		WRITE_COEF(0x6b, 0xc429),
1299 		{}
1300 	};
1301 	static const struct coef_fw coef0293[] = {
1302 		UPDATE_COEF(0x10, 7<<8, 6<<8), /* SET Line1 JD to 0 */
1303 		UPDATE_COEFEX(0x57, 0x05, 1<<15|1<<13, 0x0), /* SET charge pump by verb */
1304 		UPDATE_COEFEX(0x57, 0x03, 1<<10, 1<<10), /* SET EN_OSW to 1 */
1305 		UPDATE_COEF(0x1a, 1<<3, 1<<3), /* Combo JD gating with LINE1-VREFO */
1306 		WRITE_COEF(0x45, 0xc429), /* Set to TRS type */
1307 		UPDATE_COEF(0x4a, 0x000f, 0x000e), /* Combo Jack auto detect */
1308 		{}
1309 	};
1310 	static const struct coef_fw coef0668[] = {
1311 		WRITE_COEF(0x15, 0x0d40),
1312 		WRITE_COEF(0xb7, 0x802b),
1313 		{}
1314 	};
1315 	static const struct coef_fw coef0225[] = {
1316 		UPDATE_COEF(0x63, 3<<14, 0),
1317 		{}
1318 	};
1319 	static const struct coef_fw coef0274[] = {
1320 		UPDATE_COEF(0x4a, 0x0100, 0),
1321 		UPDATE_COEFEX(0x57, 0x05, 0x4000, 0),
1322 		UPDATE_COEF(0x6b, 0xf000, 0x5000),
1323 		UPDATE_COEF(0x4a, 0x0010, 0),
1324 		UPDATE_COEF(0x4a, 0x0c00, 0x0c00),
1325 		WRITE_COEF(0x45, 0x5289),
1326 		UPDATE_COEF(0x4a, 0x0c00, 0),
1327 		{}
1328 	};
1329 
1330 	if (spec->no_internal_mic_pin) {
1331 		alc_update_coef_idx(codec, 0x45, 0xf<<12 | 1<<10, 5<<12);
1332 		return;
1333 	}
1334 
1335 	switch (codec->core.vendor_id) {
1336 	case 0x10ec0255:
1337 		alc_process_coef_fw(codec, coef0255);
1338 		break;
1339 	case 0x10ec0230:
1340 	case 0x10ec0236:
1341 	case 0x10ec0256:
1342 	case 0x19e58326:
1343 		alc_hp_mute_disable(codec, 75);
1344 		alc_process_coef_fw(codec, coef0256);
1345 		break;
1346 	case 0x10ec0234:
1347 	case 0x10ec0274:
1348 	case 0x10ec0294:
1349 		alc_process_coef_fw(codec, coef0274);
1350 		break;
1351 	case 0x10ec0233:
1352 	case 0x10ec0283:
1353 		alc_process_coef_fw(codec, coef0233);
1354 		break;
1355 	case 0x10ec0286:
1356 	case 0x10ec0288:
1357 		alc_process_coef_fw(codec, coef0288);
1358 		break;
1359 	case 0x10ec0298:
1360 		alc_process_coef_fw(codec, coef0298);
1361 		alc_process_coef_fw(codec, coef0288);
1362 		break;
1363 	case 0x10ec0292:
1364 		alc_process_coef_fw(codec, coef0292);
1365 		break;
1366 	case 0x10ec0293:
1367 		alc_process_coef_fw(codec, coef0293);
1368 		break;
1369 	case 0x10ec0668:
1370 		alc_process_coef_fw(codec, coef0668);
1371 		break;
1372 	case 0x10ec0215:
1373 	case 0x10ec0225:
1374 	case 0x10ec0285:
1375 	case 0x10ec0295:
1376 	case 0x10ec0289:
1377 	case 0x10ec0299:
1378 		alc_hp_mute_disable(codec, 75);
1379 		alc_process_coef_fw(codec, alc225_pre_hsmode);
1380 		alc_process_coef_fw(codec, coef0225);
1381 		break;
1382 	case 0x10ec0867:
1383 		alc_update_coefex_idx(codec, 0x57, 0x5, 1<<14, 0);
1384 		break;
1385 	}
1386 	codec_dbg(codec, "Headset jack set to unplugged mode.\n");
1387 }
1388 
1389 
alc_headset_mode_mic_in(struct hda_codec * codec,hda_nid_t hp_pin,hda_nid_t mic_pin)1390 static void alc_headset_mode_mic_in(struct hda_codec *codec, hda_nid_t hp_pin,
1391 				    hda_nid_t mic_pin)
1392 {
1393 	static const struct coef_fw coef0255[] = {
1394 		WRITE_COEFEX(0x57, 0x03, 0x8aa6),
1395 		WRITE_COEF(0x06, 0x6100), /* Set MIC2 Vref gate to normal */
1396 		{}
1397 	};
1398 	static const struct coef_fw coef0256[] = {
1399 		UPDATE_COEFEX(0x57, 0x05, 1<<14, 1<<14), /* Direct Drive HP Amp control(Set to verb control)*/
1400 		WRITE_COEFEX(0x57, 0x03, 0x09a3),
1401 		WRITE_COEF(0x06, 0x6100), /* Set MIC2 Vref gate to normal */
1402 		{}
1403 	};
1404 	static const struct coef_fw coef0233[] = {
1405 		UPDATE_COEF(0x35, 0, 1<<14),
1406 		WRITE_COEF(0x06, 0x2100),
1407 		WRITE_COEF(0x1a, 0x0021),
1408 		WRITE_COEF(0x26, 0x008c),
1409 		{}
1410 	};
1411 	static const struct coef_fw coef0288[] = {
1412 		UPDATE_COEF(0x4f, 0x00c0, 0),
1413 		UPDATE_COEF(0x50, 0x2000, 0),
1414 		UPDATE_COEF(0x56, 0x0006, 0),
1415 		UPDATE_COEF(0x4f, 0xfcc0, 0xc400),
1416 		UPDATE_COEF(0x66, 0x0008, 0x0008),
1417 		UPDATE_COEF(0x67, 0x2000, 0x2000),
1418 		{}
1419 	};
1420 	static const struct coef_fw coef0292[] = {
1421 		WRITE_COEF(0x19, 0xa208),
1422 		WRITE_COEF(0x2e, 0xacf0),
1423 		{}
1424 	};
1425 	static const struct coef_fw coef0293[] = {
1426 		UPDATE_COEFEX(0x57, 0x05, 0, 1<<15|1<<13), /* SET charge pump by verb */
1427 		UPDATE_COEFEX(0x57, 0x03, 1<<10, 0), /* SET EN_OSW to 0 */
1428 		UPDATE_COEF(0x1a, 1<<3, 0), /* Combo JD gating without LINE1-VREFO */
1429 		{}
1430 	};
1431 	static const struct coef_fw coef0688[] = {
1432 		WRITE_COEF(0xb7, 0x802b),
1433 		WRITE_COEF(0xb5, 0x1040),
1434 		UPDATE_COEF(0xc3, 0, 1<<12),
1435 		{}
1436 	};
1437 	static const struct coef_fw coef0225[] = {
1438 		UPDATE_COEFEX(0x57, 0x05, 1<<14, 1<<14),
1439 		UPDATE_COEF(0x4a, 3<<4, 2<<4),
1440 		UPDATE_COEF(0x63, 3<<14, 0),
1441 		{}
1442 	};
1443 	static const struct coef_fw coef0274[] = {
1444 		UPDATE_COEFEX(0x57, 0x05, 0x4000, 0x4000),
1445 		UPDATE_COEF(0x4a, 0x0010, 0),
1446 		UPDATE_COEF(0x6b, 0xf000, 0),
1447 		{}
1448 	};
1449 
1450 	switch (codec->core.vendor_id) {
1451 	case 0x10ec0255:
1452 		alc_write_coef_idx(codec, 0x45, 0xc489);
1453 		snd_hda_set_pin_ctl_cache(codec, hp_pin, 0);
1454 		alc_process_coef_fw(codec, coef0255);
1455 		snd_hda_set_pin_ctl_cache(codec, mic_pin, PIN_VREF50);
1456 		break;
1457 	case 0x10ec0230:
1458 	case 0x10ec0236:
1459 	case 0x10ec0256:
1460 	case 0x19e58326:
1461 		alc_write_coef_idx(codec, 0x45, 0xc489);
1462 		snd_hda_set_pin_ctl_cache(codec, hp_pin, 0);
1463 		alc_process_coef_fw(codec, coef0256);
1464 		snd_hda_set_pin_ctl_cache(codec, mic_pin, PIN_VREF50);
1465 		break;
1466 	case 0x10ec0234:
1467 	case 0x10ec0274:
1468 	case 0x10ec0294:
1469 		alc_write_coef_idx(codec, 0x45, 0x4689);
1470 		snd_hda_set_pin_ctl_cache(codec, hp_pin, 0);
1471 		alc_process_coef_fw(codec, coef0274);
1472 		snd_hda_set_pin_ctl_cache(codec, mic_pin, PIN_VREF50);
1473 		break;
1474 	case 0x10ec0233:
1475 	case 0x10ec0283:
1476 		alc_write_coef_idx(codec, 0x45, 0xc429);
1477 		snd_hda_set_pin_ctl_cache(codec, hp_pin, 0);
1478 		alc_process_coef_fw(codec, coef0233);
1479 		snd_hda_set_pin_ctl_cache(codec, mic_pin, PIN_VREF50);
1480 		break;
1481 	case 0x10ec0286:
1482 	case 0x10ec0288:
1483 	case 0x10ec0298:
1484 		snd_hda_set_pin_ctl_cache(codec, hp_pin, 0);
1485 		alc_process_coef_fw(codec, coef0288);
1486 		snd_hda_set_pin_ctl_cache(codec, mic_pin, PIN_VREF50);
1487 		break;
1488 	case 0x10ec0292:
1489 		snd_hda_set_pin_ctl_cache(codec, hp_pin, 0);
1490 		alc_process_coef_fw(codec, coef0292);
1491 		break;
1492 	case 0x10ec0293:
1493 		/* Set to TRS mode */
1494 		alc_write_coef_idx(codec, 0x45, 0xc429);
1495 		snd_hda_set_pin_ctl_cache(codec, hp_pin, 0);
1496 		alc_process_coef_fw(codec, coef0293);
1497 		snd_hda_set_pin_ctl_cache(codec, mic_pin, PIN_VREF50);
1498 		break;
1499 	case 0x10ec0867:
1500 		alc_update_coefex_idx(codec, 0x57, 0x5, 0, 1<<14);
1501 		fallthrough;
1502 	case 0x10ec0221:
1503 	case 0x10ec0662:
1504 		snd_hda_set_pin_ctl_cache(codec, hp_pin, 0);
1505 		snd_hda_set_pin_ctl_cache(codec, mic_pin, PIN_VREF50);
1506 		break;
1507 	case 0x10ec0668:
1508 		alc_write_coef_idx(codec, 0x11, 0x0001);
1509 		snd_hda_set_pin_ctl_cache(codec, hp_pin, 0);
1510 		alc_process_coef_fw(codec, coef0688);
1511 		snd_hda_set_pin_ctl_cache(codec, mic_pin, PIN_VREF50);
1512 		break;
1513 	case 0x10ec0215:
1514 	case 0x10ec0225:
1515 	case 0x10ec0285:
1516 	case 0x10ec0295:
1517 	case 0x10ec0289:
1518 	case 0x10ec0299:
1519 		alc_process_coef_fw(codec, alc225_pre_hsmode);
1520 		alc_update_coef_idx(codec, 0x45, 0x3f<<10, 0x31<<10);
1521 		snd_hda_set_pin_ctl_cache(codec, hp_pin, 0);
1522 		alc_process_coef_fw(codec, coef0225);
1523 		snd_hda_set_pin_ctl_cache(codec, mic_pin, PIN_VREF50);
1524 		break;
1525 	}
1526 	codec_dbg(codec, "Headset jack set to mic-in mode.\n");
1527 }
1528 
alc_headset_mode_default(struct hda_codec * codec)1529 static void alc_headset_mode_default(struct hda_codec *codec)
1530 {
1531 	static const struct coef_fw coef0225[] = {
1532 		UPDATE_COEF(0x45, 0x3f<<10, 0x30<<10),
1533 		UPDATE_COEF(0x45, 0x3f<<10, 0x31<<10),
1534 		UPDATE_COEF(0x49, 3<<8, 0<<8),
1535 		UPDATE_COEF(0x4a, 3<<4, 3<<4),
1536 		UPDATE_COEF(0x63, 3<<14, 0),
1537 		UPDATE_COEF(0x67, 0xf000, 0x3000),
1538 		{}
1539 	};
1540 	static const struct coef_fw coef0255[] = {
1541 		WRITE_COEF(0x45, 0xc089),
1542 		WRITE_COEF(0x45, 0xc489),
1543 		WRITE_COEFEX(0x57, 0x03, 0x8ea6),
1544 		WRITE_COEF(0x49, 0x0049),
1545 		{}
1546 	};
1547 	static const struct coef_fw coef0256[] = {
1548 		WRITE_COEF(0x45, 0xc489),
1549 		WRITE_COEFEX(0x57, 0x03, 0x0da3),
1550 		WRITE_COEF(0x49, 0x0049),
1551 		UPDATE_COEFEX(0x57, 0x05, 1<<14, 0), /* Direct Drive HP Amp control(Set to verb control)*/
1552 		WRITE_COEF(0x06, 0x6100),
1553 		{}
1554 	};
1555 	static const struct coef_fw coef0233[] = {
1556 		WRITE_COEF(0x06, 0x2100),
1557 		WRITE_COEF(0x32, 0x4ea3),
1558 		{}
1559 	};
1560 	static const struct coef_fw coef0288[] = {
1561 		UPDATE_COEF(0x4f, 0xfcc0, 0xc400), /* Set to TRS type */
1562 		UPDATE_COEF(0x50, 0x2000, 0x2000),
1563 		UPDATE_COEF(0x56, 0x0006, 0x0006),
1564 		UPDATE_COEF(0x66, 0x0008, 0),
1565 		UPDATE_COEF(0x67, 0x2000, 0),
1566 		{}
1567 	};
1568 	static const struct coef_fw coef0292[] = {
1569 		WRITE_COEF(0x76, 0x000e),
1570 		WRITE_COEF(0x6c, 0x2400),
1571 		WRITE_COEF(0x6b, 0xc429),
1572 		WRITE_COEF(0x18, 0x7308),
1573 		{}
1574 	};
1575 	static const struct coef_fw coef0293[] = {
1576 		UPDATE_COEF(0x4a, 0x000f, 0x000e), /* Combo Jack auto detect */
1577 		WRITE_COEF(0x45, 0xC429), /* Set to TRS type */
1578 		UPDATE_COEF(0x1a, 1<<3, 0), /* Combo JD gating without LINE1-VREFO */
1579 		{}
1580 	};
1581 	static const struct coef_fw coef0688[] = {
1582 		WRITE_COEF(0x11, 0x0041),
1583 		WRITE_COEF(0x15, 0x0d40),
1584 		WRITE_COEF(0xb7, 0x802b),
1585 		{}
1586 	};
1587 	static const struct coef_fw coef0274[] = {
1588 		WRITE_COEF(0x45, 0x4289),
1589 		UPDATE_COEF(0x4a, 0x0010, 0x0010),
1590 		UPDATE_COEF(0x6b, 0x0f00, 0),
1591 		UPDATE_COEF(0x49, 0x0300, 0x0300),
1592 		{}
1593 	};
1594 
1595 	switch (codec->core.vendor_id) {
1596 	case 0x10ec0215:
1597 	case 0x10ec0225:
1598 	case 0x10ec0285:
1599 	case 0x10ec0295:
1600 	case 0x10ec0289:
1601 	case 0x10ec0299:
1602 		alc_process_coef_fw(codec, alc225_pre_hsmode);
1603 		alc_process_coef_fw(codec, coef0225);
1604 		alc_hp_enable_unmute(codec, 75);
1605 		break;
1606 	case 0x10ec0255:
1607 		alc_process_coef_fw(codec, coef0255);
1608 		break;
1609 	case 0x10ec0230:
1610 	case 0x10ec0236:
1611 	case 0x10ec0256:
1612 	case 0x19e58326:
1613 		alc_write_coef_idx(codec, 0x1b, 0x0e4b);
1614 		alc_write_coef_idx(codec, 0x45, 0xc089);
1615 		msleep(50);
1616 		alc_process_coef_fw(codec, coef0256);
1617 		alc_hp_enable_unmute(codec, 75);
1618 		break;
1619 	case 0x10ec0234:
1620 	case 0x10ec0274:
1621 	case 0x10ec0294:
1622 		alc_process_coef_fw(codec, coef0274);
1623 		break;
1624 	case 0x10ec0233:
1625 	case 0x10ec0283:
1626 		alc_process_coef_fw(codec, coef0233);
1627 		break;
1628 	case 0x10ec0286:
1629 	case 0x10ec0288:
1630 	case 0x10ec0298:
1631 		alc_process_coef_fw(codec, coef0288);
1632 		break;
1633 	case 0x10ec0292:
1634 		alc_process_coef_fw(codec, coef0292);
1635 		break;
1636 	case 0x10ec0293:
1637 		alc_process_coef_fw(codec, coef0293);
1638 		break;
1639 	case 0x10ec0668:
1640 		alc_process_coef_fw(codec, coef0688);
1641 		break;
1642 	case 0x10ec0867:
1643 		alc_update_coefex_idx(codec, 0x57, 0x5, 1<<14, 0);
1644 		break;
1645 	}
1646 	codec_dbg(codec, "Headset jack set to headphone (default) mode.\n");
1647 }
1648 
1649 /* Iphone type */
alc_headset_mode_ctia(struct hda_codec * codec)1650 static void alc_headset_mode_ctia(struct hda_codec *codec)
1651 {
1652 	int val;
1653 
1654 	static const struct coef_fw coef0255[] = {
1655 		WRITE_COEF(0x45, 0xd489), /* Set to CTIA type */
1656 		WRITE_COEF(0x1b, 0x0c2b),
1657 		WRITE_COEFEX(0x57, 0x03, 0x8ea6),
1658 		{}
1659 	};
1660 	static const struct coef_fw coef0256[] = {
1661 		WRITE_COEF(0x45, 0xd489), /* Set to CTIA type */
1662 		WRITE_COEF(0x1b, 0x0e6b),
1663 		{}
1664 	};
1665 	static const struct coef_fw coef0233[] = {
1666 		WRITE_COEF(0x45, 0xd429),
1667 		WRITE_COEF(0x1b, 0x0c2b),
1668 		WRITE_COEF(0x32, 0x4ea3),
1669 		{}
1670 	};
1671 	static const struct coef_fw coef0288[] = {
1672 		UPDATE_COEF(0x50, 0x2000, 0x2000),
1673 		UPDATE_COEF(0x56, 0x0006, 0x0006),
1674 		UPDATE_COEF(0x66, 0x0008, 0),
1675 		UPDATE_COEF(0x67, 0x2000, 0),
1676 		{}
1677 	};
1678 	static const struct coef_fw coef0292[] = {
1679 		WRITE_COEF(0x6b, 0xd429),
1680 		WRITE_COEF(0x76, 0x0008),
1681 		WRITE_COEF(0x18, 0x7388),
1682 		{}
1683 	};
1684 	static const struct coef_fw coef0293[] = {
1685 		WRITE_COEF(0x45, 0xd429), /* Set to ctia type */
1686 		UPDATE_COEF(0x10, 7<<8, 7<<8), /* SET Line1 JD to 1 */
1687 		{}
1688 	};
1689 	static const struct coef_fw coef0688[] = {
1690 		WRITE_COEF(0x11, 0x0001),
1691 		WRITE_COEF(0x15, 0x0d60),
1692 		WRITE_COEF(0xc3, 0x0000),
1693 		{}
1694 	};
1695 	static const struct coef_fw coef0225_1[] = {
1696 		UPDATE_COEF(0x45, 0x3f<<10, 0x35<<10),
1697 		UPDATE_COEF(0x63, 3<<14, 2<<14),
1698 		{}
1699 	};
1700 	static const struct coef_fw coef0225_2[] = {
1701 		UPDATE_COEF(0x45, 0x3f<<10, 0x35<<10),
1702 		UPDATE_COEF(0x63, 3<<14, 1<<14),
1703 		{}
1704 	};
1705 
1706 	switch (codec->core.vendor_id) {
1707 	case 0x10ec0255:
1708 		alc_process_coef_fw(codec, coef0255);
1709 		break;
1710 	case 0x10ec0230:
1711 	case 0x10ec0236:
1712 	case 0x10ec0256:
1713 	case 0x19e58326:
1714 		alc_process_coef_fw(codec, coef0256);
1715 		alc_hp_enable_unmute(codec, 75);
1716 		break;
1717 	case 0x10ec0234:
1718 	case 0x10ec0274:
1719 	case 0x10ec0294:
1720 		alc_write_coef_idx(codec, 0x45, 0xd689);
1721 		break;
1722 	case 0x10ec0233:
1723 	case 0x10ec0283:
1724 		alc_process_coef_fw(codec, coef0233);
1725 		break;
1726 	case 0x10ec0298:
1727 		val = alc_read_coef_idx(codec, 0x50);
1728 		if (val & (1 << 12)) {
1729 			alc_update_coef_idx(codec, 0x8e, 0x0070, 0x0020);
1730 			alc_update_coef_idx(codec, 0x4f, 0xfcc0, 0xd400);
1731 			msleep(300);
1732 		} else {
1733 			alc_update_coef_idx(codec, 0x8e, 0x0070, 0x0010);
1734 			alc_update_coef_idx(codec, 0x4f, 0xfcc0, 0xd400);
1735 			msleep(300);
1736 		}
1737 		break;
1738 	case 0x10ec0286:
1739 	case 0x10ec0288:
1740 		alc_update_coef_idx(codec, 0x4f, 0xfcc0, 0xd400);
1741 		msleep(300);
1742 		alc_process_coef_fw(codec, coef0288);
1743 		break;
1744 	case 0x10ec0292:
1745 		alc_process_coef_fw(codec, coef0292);
1746 		break;
1747 	case 0x10ec0293:
1748 		alc_process_coef_fw(codec, coef0293);
1749 		break;
1750 	case 0x10ec0668:
1751 		alc_process_coef_fw(codec, coef0688);
1752 		break;
1753 	case 0x10ec0215:
1754 	case 0x10ec0225:
1755 	case 0x10ec0285:
1756 	case 0x10ec0295:
1757 	case 0x10ec0289:
1758 	case 0x10ec0299:
1759 		val = alc_read_coef_idx(codec, 0x45);
1760 		if (val & (1 << 9))
1761 			alc_process_coef_fw(codec, coef0225_2);
1762 		else
1763 			alc_process_coef_fw(codec, coef0225_1);
1764 		alc_hp_enable_unmute(codec, 75);
1765 		break;
1766 	case 0x10ec0867:
1767 		alc_update_coefex_idx(codec, 0x57, 0x5, 1<<14, 0);
1768 		break;
1769 	}
1770 	codec_dbg(codec, "Headset jack set to iPhone-style headset mode.\n");
1771 }
1772 
1773 /* Nokia type */
alc_headset_mode_omtp(struct hda_codec * codec)1774 static void alc_headset_mode_omtp(struct hda_codec *codec)
1775 {
1776 	static const struct coef_fw coef0255[] = {
1777 		WRITE_COEF(0x45, 0xe489), /* Set to OMTP Type */
1778 		WRITE_COEF(0x1b, 0x0c2b),
1779 		WRITE_COEFEX(0x57, 0x03, 0x8ea6),
1780 		{}
1781 	};
1782 	static const struct coef_fw coef0256[] = {
1783 		WRITE_COEF(0x45, 0xe489), /* Set to OMTP Type */
1784 		WRITE_COEF(0x1b, 0x0e6b),
1785 		{}
1786 	};
1787 	static const struct coef_fw coef0233[] = {
1788 		WRITE_COEF(0x45, 0xe429),
1789 		WRITE_COEF(0x1b, 0x0c2b),
1790 		WRITE_COEF(0x32, 0x4ea3),
1791 		{}
1792 	};
1793 	static const struct coef_fw coef0288[] = {
1794 		UPDATE_COEF(0x50, 0x2000, 0x2000),
1795 		UPDATE_COEF(0x56, 0x0006, 0x0006),
1796 		UPDATE_COEF(0x66, 0x0008, 0),
1797 		UPDATE_COEF(0x67, 0x2000, 0),
1798 		{}
1799 	};
1800 	static const struct coef_fw coef0292[] = {
1801 		WRITE_COEF(0x6b, 0xe429),
1802 		WRITE_COEF(0x76, 0x0008),
1803 		WRITE_COEF(0x18, 0x7388),
1804 		{}
1805 	};
1806 	static const struct coef_fw coef0293[] = {
1807 		WRITE_COEF(0x45, 0xe429), /* Set to omtp type */
1808 		UPDATE_COEF(0x10, 7<<8, 7<<8), /* SET Line1 JD to 1 */
1809 		{}
1810 	};
1811 	static const struct coef_fw coef0688[] = {
1812 		WRITE_COEF(0x11, 0x0001),
1813 		WRITE_COEF(0x15, 0x0d50),
1814 		WRITE_COEF(0xc3, 0x0000),
1815 		{}
1816 	};
1817 	static const struct coef_fw coef0225[] = {
1818 		UPDATE_COEF(0x45, 0x3f<<10, 0x39<<10),
1819 		UPDATE_COEF(0x63, 3<<14, 2<<14),
1820 		{}
1821 	};
1822 
1823 	switch (codec->core.vendor_id) {
1824 	case 0x10ec0255:
1825 		alc_process_coef_fw(codec, coef0255);
1826 		break;
1827 	case 0x10ec0230:
1828 	case 0x10ec0236:
1829 	case 0x10ec0256:
1830 	case 0x19e58326:
1831 		alc_process_coef_fw(codec, coef0256);
1832 		alc_hp_enable_unmute(codec, 75);
1833 		break;
1834 	case 0x10ec0234:
1835 	case 0x10ec0274:
1836 	case 0x10ec0294:
1837 		alc_write_coef_idx(codec, 0x45, 0xe689);
1838 		break;
1839 	case 0x10ec0233:
1840 	case 0x10ec0283:
1841 		alc_process_coef_fw(codec, coef0233);
1842 		break;
1843 	case 0x10ec0298:
1844 		alc_update_coef_idx(codec, 0x8e, 0x0070, 0x0010);/* Headset output enable */
1845 		alc_update_coef_idx(codec, 0x4f, 0xfcc0, 0xe400);
1846 		msleep(300);
1847 		break;
1848 	case 0x10ec0286:
1849 	case 0x10ec0288:
1850 		alc_update_coef_idx(codec, 0x4f, 0xfcc0, 0xe400);
1851 		msleep(300);
1852 		alc_process_coef_fw(codec, coef0288);
1853 		break;
1854 	case 0x10ec0292:
1855 		alc_process_coef_fw(codec, coef0292);
1856 		break;
1857 	case 0x10ec0293:
1858 		alc_process_coef_fw(codec, coef0293);
1859 		break;
1860 	case 0x10ec0668:
1861 		alc_process_coef_fw(codec, coef0688);
1862 		break;
1863 	case 0x10ec0215:
1864 	case 0x10ec0225:
1865 	case 0x10ec0285:
1866 	case 0x10ec0295:
1867 	case 0x10ec0289:
1868 	case 0x10ec0299:
1869 		alc_process_coef_fw(codec, coef0225);
1870 		alc_hp_enable_unmute(codec, 75);
1871 		break;
1872 	}
1873 	codec_dbg(codec, "Headset jack set to Nokia-style headset mode.\n");
1874 }
1875 
alc_determine_headset_type(struct hda_codec * codec)1876 static void alc_determine_headset_type(struct hda_codec *codec)
1877 {
1878 	int val;
1879 	bool is_ctia = false;
1880 	struct alc_spec *spec = codec->spec;
1881 	static const struct coef_fw coef0255[] = {
1882 		WRITE_COEF(0x45, 0xd089), /* combo jack auto switch control(Check type)*/
1883 		WRITE_COEF(0x49, 0x0149), /* combo jack auto switch control(Vref
1884  conteol) */
1885 		{}
1886 	};
1887 	static const struct coef_fw coef0288[] = {
1888 		UPDATE_COEF(0x4f, 0xfcc0, 0xd400), /* Check Type */
1889 		{}
1890 	};
1891 	static const struct coef_fw coef0298[] = {
1892 		UPDATE_COEF(0x50, 0x2000, 0x2000),
1893 		UPDATE_COEF(0x56, 0x0006, 0x0006),
1894 		UPDATE_COEF(0x66, 0x0008, 0),
1895 		UPDATE_COEF(0x67, 0x2000, 0),
1896 		UPDATE_COEF(0x19, 0x1300, 0x1300),
1897 		{}
1898 	};
1899 	static const struct coef_fw coef0293[] = {
1900 		UPDATE_COEF(0x4a, 0x000f, 0x0008), /* Combo Jack auto detect */
1901 		WRITE_COEF(0x45, 0xD429), /* Set to ctia type */
1902 		{}
1903 	};
1904 	static const struct coef_fw coef0688[] = {
1905 		WRITE_COEF(0x11, 0x0001),
1906 		WRITE_COEF(0xb7, 0x802b),
1907 		WRITE_COEF(0x15, 0x0d60),
1908 		WRITE_COEF(0xc3, 0x0c00),
1909 		{}
1910 	};
1911 	static const struct coef_fw coef0274[] = {
1912 		UPDATE_COEF(0x4a, 0x0010, 0),
1913 		UPDATE_COEF(0x4a, 0x8000, 0),
1914 		WRITE_COEF(0x45, 0xd289),
1915 		UPDATE_COEF(0x49, 0x0300, 0x0300),
1916 		{}
1917 	};
1918 
1919 	if (spec->no_internal_mic_pin) {
1920 		alc_update_coef_idx(codec, 0x45, 0xf<<12 | 1<<10, 5<<12);
1921 		return;
1922 	}
1923 
1924 	switch (codec->core.vendor_id) {
1925 	case 0x10ec0255:
1926 		alc_process_coef_fw(codec, coef0255);
1927 		msleep(300);
1928 		val = alc_read_coef_idx(codec, 0x46);
1929 		is_ctia = (val & 0x0070) == 0x0070;
1930 		break;
1931 	case 0x10ec0230:
1932 	case 0x10ec0236:
1933 	case 0x10ec0256:
1934 	case 0x19e58326:
1935 		alc_write_coef_idx(codec, 0x1b, 0x0e4b);
1936 		alc_write_coef_idx(codec, 0x06, 0x6104);
1937 		alc_write_coefex_idx(codec, 0x57, 0x3, 0x09a3);
1938 
1939 		alc_process_coef_fw(codec, coef0255);
1940 		msleep(300);
1941 		val = alc_read_coef_idx(codec, 0x46);
1942 		is_ctia = (val & 0x0070) == 0x0070;
1943 		if (!is_ctia) {
1944 			alc_write_coef_idx(codec, 0x45, 0xe089);
1945 			msleep(100);
1946 			val = alc_read_coef_idx(codec, 0x46);
1947 			if ((val & 0x0070) == 0x0070)
1948 				is_ctia = false;
1949 			else
1950 				is_ctia = true;
1951 		}
1952 		alc_write_coefex_idx(codec, 0x57, 0x3, 0x0da3);
1953 		alc_update_coefex_idx(codec, 0x57, 0x5, 1<<14, 0);
1954 		break;
1955 	case 0x10ec0234:
1956 	case 0x10ec0274:
1957 	case 0x10ec0294:
1958 		alc_process_coef_fw(codec, coef0274);
1959 		msleep(850);
1960 		val = alc_read_coef_idx(codec, 0x46);
1961 		is_ctia = (val & 0x00f0) == 0x00f0;
1962 		break;
1963 	case 0x10ec0233:
1964 	case 0x10ec0283:
1965 		alc_write_coef_idx(codec, 0x45, 0xd029);
1966 		msleep(300);
1967 		val = alc_read_coef_idx(codec, 0x46);
1968 		is_ctia = (val & 0x0070) == 0x0070;
1969 		break;
1970 	case 0x10ec0298:
1971 		snd_hda_codec_write(codec, 0x21, 0,
1972 			    AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
1973 		msleep(100);
1974 		snd_hda_codec_write(codec, 0x21, 0,
1975 			    AC_VERB_SET_PIN_WIDGET_CONTROL, 0x0);
1976 		msleep(200);
1977 
1978 		val = alc_read_coef_idx(codec, 0x50);
1979 		if (val & (1 << 12)) {
1980 			alc_update_coef_idx(codec, 0x8e, 0x0070, 0x0020);
1981 			alc_process_coef_fw(codec, coef0288);
1982 			msleep(350);
1983 			val = alc_read_coef_idx(codec, 0x50);
1984 			is_ctia = (val & 0x0070) == 0x0070;
1985 		} else {
1986 			alc_update_coef_idx(codec, 0x8e, 0x0070, 0x0010);
1987 			alc_process_coef_fw(codec, coef0288);
1988 			msleep(350);
1989 			val = alc_read_coef_idx(codec, 0x50);
1990 			is_ctia = (val & 0x0070) == 0x0070;
1991 		}
1992 		alc_process_coef_fw(codec, coef0298);
1993 		snd_hda_codec_write(codec, 0x21, 0,
1994 			    AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP);
1995 		msleep(75);
1996 		snd_hda_codec_write(codec, 0x21, 0,
1997 			    AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
1998 		break;
1999 	case 0x10ec0286:
2000 	case 0x10ec0288:
2001 		alc_process_coef_fw(codec, coef0288);
2002 		msleep(350);
2003 		val = alc_read_coef_idx(codec, 0x50);
2004 		is_ctia = (val & 0x0070) == 0x0070;
2005 		break;
2006 	case 0x10ec0292:
2007 		alc_write_coef_idx(codec, 0x6b, 0xd429);
2008 		msleep(300);
2009 		val = alc_read_coef_idx(codec, 0x6c);
2010 		is_ctia = (val & 0x001c) == 0x001c;
2011 		break;
2012 	case 0x10ec0293:
2013 		alc_process_coef_fw(codec, coef0293);
2014 		msleep(300);
2015 		val = alc_read_coef_idx(codec, 0x46);
2016 		is_ctia = (val & 0x0070) == 0x0070;
2017 		break;
2018 	case 0x10ec0668:
2019 		alc_process_coef_fw(codec, coef0688);
2020 		msleep(300);
2021 		val = alc_read_coef_idx(codec, 0xbe);
2022 		is_ctia = (val & 0x1c02) == 0x1c02;
2023 		break;
2024 	case 0x10ec0215:
2025 	case 0x10ec0225:
2026 	case 0x10ec0285:
2027 	case 0x10ec0295:
2028 	case 0x10ec0289:
2029 	case 0x10ec0299:
2030 		alc_process_coef_fw(codec, alc225_pre_hsmode);
2031 		alc_update_coef_idx(codec, 0x67, 0xf000, 0x1000);
2032 		val = alc_read_coef_idx(codec, 0x45);
2033 		if (val & (1 << 9)) {
2034 			alc_update_coef_idx(codec, 0x45, 0x3f<<10, 0x34<<10);
2035 			alc_update_coef_idx(codec, 0x49, 3<<8, 2<<8);
2036 			msleep(800);
2037 			val = alc_read_coef_idx(codec, 0x46);
2038 			is_ctia = (val & 0x00f0) == 0x00f0;
2039 		} else {
2040 			alc_update_coef_idx(codec, 0x45, 0x3f<<10, 0x34<<10);
2041 			alc_update_coef_idx(codec, 0x49, 3<<8, 1<<8);
2042 			msleep(800);
2043 			val = alc_read_coef_idx(codec, 0x46);
2044 			is_ctia = (val & 0x00f0) == 0x00f0;
2045 		}
2046 		if (!is_ctia) {
2047 			alc_update_coef_idx(codec, 0x45, 0x3f<<10, 0x38<<10);
2048 			alc_update_coef_idx(codec, 0x49, 3<<8, 1<<8);
2049 			msleep(100);
2050 			val = alc_read_coef_idx(codec, 0x46);
2051 			if ((val & 0x00f0) == 0x00f0)
2052 				is_ctia = false;
2053 			else
2054 				is_ctia = true;
2055 		}
2056 		alc_update_coef_idx(codec, 0x4a, 7<<6, 7<<6);
2057 		alc_update_coef_idx(codec, 0x4a, 3<<4, 3<<4);
2058 		alc_update_coef_idx(codec, 0x67, 0xf000, 0x3000);
2059 		break;
2060 	case 0x10ec0867:
2061 		is_ctia = true;
2062 		break;
2063 	}
2064 
2065 	codec_dbg(codec, "Headset jack detected iPhone-style headset: %s\n",
2066 		  str_yes_no(is_ctia));
2067 	spec->current_headset_type = is_ctia ? ALC_HEADSET_TYPE_CTIA : ALC_HEADSET_TYPE_OMTP;
2068 }
2069 
alc_update_headset_mode(struct hda_codec * codec)2070 static void alc_update_headset_mode(struct hda_codec *codec)
2071 {
2072 	struct alc_spec *spec = codec->spec;
2073 
2074 	hda_nid_t mux_pin = spec->gen.imux_pins[spec->gen.cur_mux[0]];
2075 	hda_nid_t hp_pin = alc_get_hp_pin(spec);
2076 
2077 	int new_headset_mode;
2078 
2079 	if (!snd_hda_jack_detect(codec, hp_pin))
2080 		new_headset_mode = ALC_HEADSET_MODE_UNPLUGGED;
2081 	else if (mux_pin == spec->headset_mic_pin)
2082 		new_headset_mode = ALC_HEADSET_MODE_HEADSET;
2083 	else if (mux_pin == spec->headphone_mic_pin)
2084 		new_headset_mode = ALC_HEADSET_MODE_MIC;
2085 	else
2086 		new_headset_mode = ALC_HEADSET_MODE_HEADPHONE;
2087 
2088 	if (new_headset_mode == spec->current_headset_mode) {
2089 		snd_hda_gen_update_outputs(codec);
2090 		return;
2091 	}
2092 
2093 	switch (new_headset_mode) {
2094 	case ALC_HEADSET_MODE_UNPLUGGED:
2095 		alc_headset_mode_unplugged(codec);
2096 		spec->current_headset_mode = ALC_HEADSET_MODE_UNKNOWN;
2097 		spec->current_headset_type = ALC_HEADSET_TYPE_UNKNOWN;
2098 		spec->gen.hp_jack_present = false;
2099 		break;
2100 	case ALC_HEADSET_MODE_HEADSET:
2101 		if (spec->current_headset_type == ALC_HEADSET_TYPE_UNKNOWN)
2102 			alc_determine_headset_type(codec);
2103 		if (spec->current_headset_type == ALC_HEADSET_TYPE_CTIA)
2104 			alc_headset_mode_ctia(codec);
2105 		else if (spec->current_headset_type == ALC_HEADSET_TYPE_OMTP)
2106 			alc_headset_mode_omtp(codec);
2107 		spec->gen.hp_jack_present = true;
2108 		break;
2109 	case ALC_HEADSET_MODE_MIC:
2110 		alc_headset_mode_mic_in(codec, hp_pin, spec->headphone_mic_pin);
2111 		spec->gen.hp_jack_present = false;
2112 		break;
2113 	case ALC_HEADSET_MODE_HEADPHONE:
2114 		alc_headset_mode_default(codec);
2115 		spec->gen.hp_jack_present = true;
2116 		break;
2117 	}
2118 	if (new_headset_mode != ALC_HEADSET_MODE_MIC) {
2119 		snd_hda_set_pin_ctl_cache(codec, hp_pin,
2120 					  AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
2121 		if (spec->headphone_mic_pin && spec->headphone_mic_pin != hp_pin)
2122 			snd_hda_set_pin_ctl_cache(codec, spec->headphone_mic_pin,
2123 						  PIN_VREFHIZ);
2124 	}
2125 	spec->current_headset_mode = new_headset_mode;
2126 
2127 	snd_hda_gen_update_outputs(codec);
2128 }
2129 
alc_update_headset_mode_hook(struct hda_codec * codec,struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2130 static void alc_update_headset_mode_hook(struct hda_codec *codec,
2131 					 struct snd_kcontrol *kcontrol,
2132 					 struct snd_ctl_elem_value *ucontrol)
2133 {
2134 	alc_update_headset_mode(codec);
2135 }
2136 
alc_update_headset_jack_cb(struct hda_codec * codec,struct hda_jack_callback * jack)2137 void alc_update_headset_jack_cb(struct hda_codec *codec,
2138 				struct hda_jack_callback *jack)
2139 {
2140 	snd_hda_gen_hp_automute(codec, jack);
2141 	alc_update_headset_mode(codec);
2142 }
2143 EXPORT_SYMBOL_NS_GPL(alc_update_headset_jack_cb, "SND_HDA_CODEC_REALTEK");
2144 
alc_probe_headset_mode(struct hda_codec * codec)2145 static void alc_probe_headset_mode(struct hda_codec *codec)
2146 {
2147 	int i;
2148 	struct alc_spec *spec = codec->spec;
2149 	struct auto_pin_cfg *cfg = &spec->gen.autocfg;
2150 
2151 	/* Find mic pins */
2152 	for (i = 0; i < cfg->num_inputs; i++) {
2153 		if (cfg->inputs[i].is_headset_mic && !spec->headset_mic_pin)
2154 			spec->headset_mic_pin = cfg->inputs[i].pin;
2155 		if (cfg->inputs[i].is_headphone_mic && !spec->headphone_mic_pin)
2156 			spec->headphone_mic_pin = cfg->inputs[i].pin;
2157 	}
2158 
2159 	WARN_ON(spec->gen.cap_sync_hook);
2160 	spec->gen.cap_sync_hook = alc_update_headset_mode_hook;
2161 	spec->gen.automute_hook = alc_update_headset_mode;
2162 	spec->gen.hp_automute_hook = alc_update_headset_jack_cb;
2163 }
2164 
alc_fixup_headset_mode(struct hda_codec * codec,const struct hda_fixup * fix,int action)2165 void alc_fixup_headset_mode(struct hda_codec *codec,
2166 			    const struct hda_fixup *fix, int action)
2167 {
2168 	struct alc_spec *spec = codec->spec;
2169 
2170 	switch (action) {
2171 	case HDA_FIXUP_ACT_PRE_PROBE:
2172 		spec->parse_flags |= HDA_PINCFG_HEADSET_MIC | HDA_PINCFG_HEADPHONE_MIC;
2173 		break;
2174 	case HDA_FIXUP_ACT_PROBE:
2175 		alc_probe_headset_mode(codec);
2176 		break;
2177 	case HDA_FIXUP_ACT_INIT:
2178 		if (is_s3_resume(codec) || is_s4_resume(codec)) {
2179 			spec->current_headset_mode = ALC_HEADSET_MODE_UNKNOWN;
2180 			spec->current_headset_type = ALC_HEADSET_TYPE_UNKNOWN;
2181 		}
2182 		alc_update_headset_mode(codec);
2183 		break;
2184 	}
2185 }
2186 EXPORT_SYMBOL_NS_GPL(alc_fixup_headset_mode, "SND_HDA_CODEC_REALTEK");
2187 
alc_fixup_headset_mode_no_hp_mic(struct hda_codec * codec,const struct hda_fixup * fix,int action)2188 void alc_fixup_headset_mode_no_hp_mic(struct hda_codec *codec,
2189 				      const struct hda_fixup *fix, int action)
2190 {
2191 	if (action == HDA_FIXUP_ACT_PRE_PROBE) {
2192 		struct alc_spec *spec = codec->spec;
2193 		spec->parse_flags |= HDA_PINCFG_HEADSET_MIC;
2194 	}
2195 	else
2196 		alc_fixup_headset_mode(codec, fix, action);
2197 }
2198 EXPORT_SYMBOL_NS_GPL(alc_fixup_headset_mode_no_hp_mic, "SND_HDA_CODEC_REALTEK");
2199 
alc_fixup_headset_mic(struct hda_codec * codec,const struct hda_fixup * fix,int action)2200 void alc_fixup_headset_mic(struct hda_codec *codec,
2201 			   const struct hda_fixup *fix, int action)
2202 {
2203 	struct alc_spec *spec = codec->spec;
2204 
2205 	if (action == HDA_FIXUP_ACT_PRE_PROBE)
2206 		spec->parse_flags |= HDA_PINCFG_HEADSET_MIC;
2207 }
2208 EXPORT_SYMBOL_NS_GPL(alc_fixup_headset_mic, "SND_HDA_CODEC_REALTEK");
2209 
2210 /* update LED status via GPIO */
alc_update_gpio_led(struct hda_codec * codec,unsigned int mask,int polarity,bool enabled)2211 void alc_update_gpio_led(struct hda_codec *codec, unsigned int mask,
2212 			 int polarity, bool enabled)
2213 {
2214 	if (polarity)
2215 		enabled = !enabled;
2216 	alc_update_gpio_data(codec, mask, !enabled); /* muted -> LED on */
2217 }
2218 EXPORT_SYMBOL_NS_GPL(alc_update_gpio_led, "SND_HDA_CODEC_REALTEK");
2219 
2220 /* turn on/off mic-mute LED via GPIO per capture hook */
micmute_led_set(struct led_classdev * led_cdev,enum led_brightness brightness)2221 static int micmute_led_set(struct led_classdev *led_cdev,
2222 			   enum led_brightness brightness)
2223 {
2224 	struct hda_codec *codec = dev_to_hda_codec(led_cdev->dev->parent);
2225 	struct alc_spec *spec = codec->spec;
2226 
2227 	alc_update_gpio_led(codec, spec->gpio_mic_led_mask,
2228 			    spec->micmute_led_polarity, !brightness);
2229 	return 0;
2230 }
2231 
2232 /* turn on/off mute LED via GPIO per vmaster hook */
gpio_mute_led_set(struct led_classdev * led_cdev,enum led_brightness brightness)2233 static int gpio_mute_led_set(struct led_classdev *led_cdev,
2234 			     enum led_brightness brightness)
2235 {
2236 	struct hda_codec *codec = dev_to_hda_codec(led_cdev->dev->parent);
2237 	struct alc_spec *spec = codec->spec;
2238 
2239 	alc_update_gpio_led(codec, spec->gpio_mute_led_mask,
2240 			    spec->mute_led_polarity, !brightness);
2241 	return 0;
2242 }
2243 
2244 /* setup mute and mic-mute GPIO bits, add hooks appropriately */
alc_fixup_hp_gpio_led(struct hda_codec * codec,int action,unsigned int mute_mask,unsigned int micmute_mask)2245 void alc_fixup_hp_gpio_led(struct hda_codec *codec,
2246 			   int action,
2247 			   unsigned int mute_mask,
2248 			   unsigned int micmute_mask)
2249 {
2250 	struct alc_spec *spec = codec->spec;
2251 
2252 	alc_fixup_gpio(codec, action, mute_mask | micmute_mask);
2253 
2254 	if (action != HDA_FIXUP_ACT_PRE_PROBE)
2255 		return;
2256 	if (mute_mask) {
2257 		spec->gpio_mute_led_mask = mute_mask;
2258 		snd_hda_gen_add_mute_led_cdev(codec, gpio_mute_led_set);
2259 	}
2260 	if (micmute_mask) {
2261 		spec->gpio_mic_led_mask = micmute_mask;
2262 		snd_hda_gen_add_micmute_led_cdev(codec, micmute_led_set);
2263 	}
2264 }
2265 EXPORT_SYMBOL_NS_GPL(alc_fixup_hp_gpio_led, "SND_HDA_CODEC_REALTEK");
2266 
2267 /* suppress the jack-detection */
alc_fixup_no_jack_detect(struct hda_codec * codec,const struct hda_fixup * fix,int action)2268 void alc_fixup_no_jack_detect(struct hda_codec *codec,
2269 			      const struct hda_fixup *fix, int action)
2270 {
2271 	if (action == HDA_FIXUP_ACT_PRE_PROBE)
2272 		codec->no_jack_detect = 1;
2273 }
2274 EXPORT_SYMBOL_NS_GPL(alc_fixup_no_jack_detect, "SND_HDA_CODEC_REALTEK");
2275 
alc_fixup_disable_aamix(struct hda_codec * codec,const struct hda_fixup * fix,int action)2276 void alc_fixup_disable_aamix(struct hda_codec *codec,
2277 			     const struct hda_fixup *fix, int action)
2278 {
2279 	if (action == HDA_FIXUP_ACT_PRE_PROBE) {
2280 		struct alc_spec *spec = codec->spec;
2281 		/* Disable AA-loopback as it causes white noise */
2282 		spec->gen.mixer_nid = 0;
2283 	}
2284 }
2285 EXPORT_SYMBOL_NS_GPL(alc_fixup_disable_aamix, "SND_HDA_CODEC_REALTEK");
2286 
alc_fixup_auto_mute_via_amp(struct hda_codec * codec,const struct hda_fixup * fix,int action)2287 void alc_fixup_auto_mute_via_amp(struct hda_codec *codec,
2288 				 const struct hda_fixup *fix, int action)
2289 {
2290 	if (action == HDA_FIXUP_ACT_PRE_PROBE) {
2291 		struct alc_spec *spec = codec->spec;
2292 		spec->gen.auto_mute_via_amp = 1;
2293 	}
2294 }
2295 EXPORT_SYMBOL_NS_GPL(alc_fixup_auto_mute_via_amp, "SND_HDA_CODEC_REALTEK");
2296 
2297 MODULE_IMPORT_NS("SND_HDA_SCODEC_COMPONENT");
2298 MODULE_LICENSE("GPL");
2299 MODULE_DESCRIPTION("Realtek HD-audio codec helper");
2300