• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * BIOS auto-parser helper functions for HD-audio
3  *
4  * Copyright (c) 2012 Takashi Iwai <tiwai@suse.de>
5  *
6  * This driver is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  */
11 
12 #include <linux/slab.h>
13 #include <linux/export.h>
14 #include <linux/sort.h>
15 #include <sound/core.h>
16 #include "hda_codec.h"
17 #include "hda_local.h"
18 #include "hda_auto_parser.h"
19 
20 /*
21  * Helper for automatic pin configuration
22  */
23 
is_in_nid_list(hda_nid_t nid,const hda_nid_t * list)24 static int is_in_nid_list(hda_nid_t nid, const hda_nid_t *list)
25 {
26 	for (; *list; list++)
27 		if (*list == nid)
28 			return 1;
29 	return 0;
30 }
31 
32 /* a pair of input pin and its sequence */
33 struct auto_out_pin {
34 	hda_nid_t pin;
35 	short seq;
36 };
37 
compare_seq(const void * ap,const void * bp)38 static int compare_seq(const void *ap, const void *bp)
39 {
40 	const struct auto_out_pin *a = ap;
41 	const struct auto_out_pin *b = bp;
42 	return (int)(a->seq - b->seq);
43 }
44 
45 /*
46  * Sort an associated group of pins according to their sequence numbers.
47  * then store it to a pin array.
48  */
sort_pins_by_sequence(hda_nid_t * pins,struct auto_out_pin * list,int num_pins)49 static void sort_pins_by_sequence(hda_nid_t *pins, struct auto_out_pin *list,
50 				  int num_pins)
51 {
52 	int i;
53 	sort(list, num_pins, sizeof(list[0]), compare_seq, NULL);
54 	for (i = 0; i < num_pins; i++)
55 		pins[i] = list[i].pin;
56 }
57 
58 
59 /* add the found input-pin to the cfg->inputs[] table */
add_auto_cfg_input_pin(struct hda_codec * codec,struct auto_pin_cfg * cfg,hda_nid_t nid,int type)60 static void add_auto_cfg_input_pin(struct hda_codec *codec, struct auto_pin_cfg *cfg,
61 				   hda_nid_t nid, int type)
62 {
63 	if (cfg->num_inputs < AUTO_CFG_MAX_INS) {
64 		cfg->inputs[cfg->num_inputs].pin = nid;
65 		cfg->inputs[cfg->num_inputs].type = type;
66 		cfg->inputs[cfg->num_inputs].has_boost_on_pin =
67 			nid_has_volume(codec, nid, HDA_INPUT);
68 		cfg->num_inputs++;
69 	}
70 }
71 
compare_input_type(const void * ap,const void * bp)72 static int compare_input_type(const void *ap, const void *bp)
73 {
74 	const struct auto_pin_cfg_item *a = ap;
75 	const struct auto_pin_cfg_item *b = bp;
76 	if (a->type != b->type)
77 		return (int)(a->type - b->type);
78 
79 	/* If has both hs_mic and hp_mic, pick the hs_mic ahead of hp_mic. */
80 	if (a->is_headset_mic && b->is_headphone_mic)
81 		return -1; /* don't swap */
82 	else if (a->is_headphone_mic && b->is_headset_mic)
83 		return 1; /* swap */
84 
85 	/* In case one has boost and the other one has not,
86 	   pick the one with boost first. */
87 	return (int)(b->has_boost_on_pin - a->has_boost_on_pin);
88 }
89 
90 /* Reorder the surround channels
91  * ALSA sequence is front/surr/clfe/side
92  * HDA sequence is:
93  *    4-ch: front/surr  =>  OK as it is
94  *    6-ch: front/clfe/surr
95  *    8-ch: front/clfe/rear/side|fc
96  */
reorder_outputs(unsigned int nums,hda_nid_t * pins)97 static void reorder_outputs(unsigned int nums, hda_nid_t *pins)
98 {
99 	hda_nid_t nid;
100 
101 	switch (nums) {
102 	case 3:
103 	case 4:
104 		nid = pins[1];
105 		pins[1] = pins[2];
106 		pins[2] = nid;
107 		break;
108 	}
109 }
110 
111 /* check whether the given pin has a proper pin I/O capability bit */
check_pincap_validity(struct hda_codec * codec,hda_nid_t pin,unsigned int dev)112 static bool check_pincap_validity(struct hda_codec *codec, hda_nid_t pin,
113 				  unsigned int dev)
114 {
115 	unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
116 
117 	/* some old hardware don't return the proper pincaps */
118 	if (!pincap)
119 		return true;
120 
121 	switch (dev) {
122 	case AC_JACK_LINE_OUT:
123 	case AC_JACK_SPEAKER:
124 	case AC_JACK_HP_OUT:
125 	case AC_JACK_SPDIF_OUT:
126 	case AC_JACK_DIG_OTHER_OUT:
127 		return !!(pincap & AC_PINCAP_OUT);
128 	default:
129 		return !!(pincap & AC_PINCAP_IN);
130 	}
131 }
132 
can_be_headset_mic(struct hda_codec * codec,struct auto_pin_cfg_item * item,int seq_number)133 static bool can_be_headset_mic(struct hda_codec *codec,
134 			       struct auto_pin_cfg_item *item,
135 			       int seq_number)
136 {
137 	int attr;
138 	unsigned int def_conf;
139 	if (item->type != AUTO_PIN_MIC)
140 		return false;
141 
142 	if (item->is_headset_mic || item->is_headphone_mic)
143 		return false; /* Already assigned */
144 
145 	def_conf = snd_hda_codec_get_pincfg(codec, item->pin);
146 	attr = snd_hda_get_input_pin_attr(def_conf);
147 	if (attr <= INPUT_PIN_ATTR_DOCK)
148 		return false;
149 
150 	if (seq_number >= 0) {
151 		int seq = get_defcfg_sequence(def_conf);
152 		if (seq != seq_number)
153 			return false;
154 	}
155 
156 	return true;
157 }
158 
159 /*
160  * Parse all pin widgets and store the useful pin nids to cfg
161  *
162  * The number of line-outs or any primary output is stored in line_outs,
163  * and the corresponding output pins are assigned to line_out_pins[],
164  * in the order of front, rear, CLFE, side, ...
165  *
166  * If more extra outputs (speaker and headphone) are found, the pins are
167  * assisnged to hp_pins[] and speaker_pins[], respectively.  If no line-out jack
168  * is detected, one of speaker of HP pins is assigned as the primary
169  * output, i.e. to line_out_pins[0].  So, line_outs is always positive
170  * if any analog output exists.
171  *
172  * The analog input pins are assigned to inputs array.
173  * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
174  * respectively.
175  */
snd_hda_parse_pin_defcfg(struct hda_codec * codec,struct auto_pin_cfg * cfg,const hda_nid_t * ignore_nids,unsigned int cond_flags)176 int snd_hda_parse_pin_defcfg(struct hda_codec *codec,
177 			     struct auto_pin_cfg *cfg,
178 			     const hda_nid_t *ignore_nids,
179 			     unsigned int cond_flags)
180 {
181 	hda_nid_t nid;
182 	short seq, assoc_line_out;
183 	struct auto_out_pin line_out[ARRAY_SIZE(cfg->line_out_pins)];
184 	struct auto_out_pin speaker_out[ARRAY_SIZE(cfg->speaker_pins)];
185 	struct auto_out_pin hp_out[ARRAY_SIZE(cfg->hp_pins)];
186 	int i;
187 
188 	if (!snd_hda_get_int_hint(codec, "parser_flags", &i))
189 		cond_flags = i;
190 
191 	memset(cfg, 0, sizeof(*cfg));
192 
193 	memset(line_out, 0, sizeof(line_out));
194 	memset(speaker_out, 0, sizeof(speaker_out));
195 	memset(hp_out, 0, sizeof(hp_out));
196 	assoc_line_out = 0;
197 
198 	for_each_hda_codec_node(nid, codec) {
199 		unsigned int wid_caps = get_wcaps(codec, nid);
200 		unsigned int wid_type = get_wcaps_type(wid_caps);
201 		unsigned int def_conf;
202 		short assoc, loc, conn, dev;
203 
204 		/* read all default configuration for pin complex */
205 		if (wid_type != AC_WID_PIN)
206 			continue;
207 		/* ignore the given nids (e.g. pc-beep returns error) */
208 		if (ignore_nids && is_in_nid_list(nid, ignore_nids))
209 			continue;
210 
211 		def_conf = snd_hda_codec_get_pincfg(codec, nid);
212 		conn = get_defcfg_connect(def_conf);
213 		if (conn == AC_JACK_PORT_NONE)
214 			continue;
215 		loc = get_defcfg_location(def_conf);
216 		dev = get_defcfg_device(def_conf);
217 
218 		/* workaround for buggy BIOS setups */
219 		if (dev == AC_JACK_LINE_OUT) {
220 			if (conn == AC_JACK_PORT_FIXED ||
221 			    conn == AC_JACK_PORT_BOTH)
222 				dev = AC_JACK_SPEAKER;
223 		}
224 
225 		if (!check_pincap_validity(codec, nid, dev))
226 			continue;
227 
228 		switch (dev) {
229 		case AC_JACK_LINE_OUT:
230 			seq = get_defcfg_sequence(def_conf);
231 			assoc = get_defcfg_association(def_conf);
232 
233 			if (!(wid_caps & AC_WCAP_STEREO))
234 				if (!cfg->mono_out_pin)
235 					cfg->mono_out_pin = nid;
236 			if (!assoc)
237 				continue;
238 			if (!assoc_line_out)
239 				assoc_line_out = assoc;
240 			else if (assoc_line_out != assoc) {
241 				codec_info(codec,
242 					   "ignore pin 0x%x with mismatching assoc# 0x%x vs 0x%x\n",
243 					   nid, assoc, assoc_line_out);
244 				continue;
245 			}
246 			if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins)) {
247 				codec_info(codec,
248 					   "ignore pin 0x%x, too many assigned pins\n",
249 					   nid);
250 				continue;
251 			}
252 			line_out[cfg->line_outs].pin = nid;
253 			line_out[cfg->line_outs].seq = seq;
254 			cfg->line_outs++;
255 			break;
256 		case AC_JACK_SPEAKER:
257 			seq = get_defcfg_sequence(def_conf);
258 			assoc = get_defcfg_association(def_conf);
259 			if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins)) {
260 				codec_info(codec,
261 					   "ignore pin 0x%x, too many assigned pins\n",
262 					   nid);
263 				continue;
264 			}
265 			speaker_out[cfg->speaker_outs].pin = nid;
266 			speaker_out[cfg->speaker_outs].seq = (assoc << 4) | seq;
267 			cfg->speaker_outs++;
268 			break;
269 		case AC_JACK_HP_OUT:
270 			seq = get_defcfg_sequence(def_conf);
271 			assoc = get_defcfg_association(def_conf);
272 			if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins)) {
273 				codec_info(codec,
274 					   "ignore pin 0x%x, too many assigned pins\n",
275 					   nid);
276 				continue;
277 			}
278 			hp_out[cfg->hp_outs].pin = nid;
279 			hp_out[cfg->hp_outs].seq = (assoc << 4) | seq;
280 			cfg->hp_outs++;
281 			break;
282 		case AC_JACK_MIC_IN:
283 			add_auto_cfg_input_pin(codec, cfg, nid, AUTO_PIN_MIC);
284 			break;
285 		case AC_JACK_LINE_IN:
286 			add_auto_cfg_input_pin(codec, cfg, nid, AUTO_PIN_LINE_IN);
287 			break;
288 		case AC_JACK_CD:
289 			add_auto_cfg_input_pin(codec, cfg, nid, AUTO_PIN_CD);
290 			break;
291 		case AC_JACK_AUX:
292 			add_auto_cfg_input_pin(codec, cfg, nid, AUTO_PIN_AUX);
293 			break;
294 		case AC_JACK_SPDIF_OUT:
295 		case AC_JACK_DIG_OTHER_OUT:
296 			if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins)) {
297 				codec_info(codec,
298 					   "ignore pin 0x%x, too many assigned pins\n",
299 					   nid);
300 				continue;
301 			}
302 			cfg->dig_out_pins[cfg->dig_outs] = nid;
303 			cfg->dig_out_type[cfg->dig_outs] =
304 				(loc == AC_JACK_LOC_HDMI) ?
305 				HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
306 			cfg->dig_outs++;
307 			break;
308 		case AC_JACK_SPDIF_IN:
309 		case AC_JACK_DIG_OTHER_IN:
310 			cfg->dig_in_pin = nid;
311 			if (loc == AC_JACK_LOC_HDMI)
312 				cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
313 			else
314 				cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
315 			break;
316 		}
317 	}
318 
319 	/* Find a pin that could be a headset or headphone mic */
320 	if (cond_flags & HDA_PINCFG_HEADSET_MIC || cond_flags & HDA_PINCFG_HEADPHONE_MIC) {
321 		bool hsmic = !!(cond_flags & HDA_PINCFG_HEADSET_MIC);
322 		bool hpmic = !!(cond_flags & HDA_PINCFG_HEADPHONE_MIC);
323 		for (i = 0; (hsmic || hpmic) && (i < cfg->num_inputs); i++)
324 			if (hsmic && can_be_headset_mic(codec, &cfg->inputs[i], 0xc)) {
325 				cfg->inputs[i].is_headset_mic = 1;
326 				hsmic = false;
327 			} else if (hpmic && can_be_headset_mic(codec, &cfg->inputs[i], 0xd)) {
328 				cfg->inputs[i].is_headphone_mic = 1;
329 				hpmic = false;
330 			}
331 
332 		/* If we didn't find our sequence number mark, fall back to any sequence number */
333 		for (i = 0; (hsmic || hpmic) && (i < cfg->num_inputs); i++) {
334 			if (!can_be_headset_mic(codec, &cfg->inputs[i], -1))
335 				continue;
336 			if (hsmic) {
337 				cfg->inputs[i].is_headset_mic = 1;
338 				hsmic = false;
339 			} else if (hpmic) {
340 				cfg->inputs[i].is_headphone_mic = 1;
341 				hpmic = false;
342 			}
343 		}
344 
345 		if (hsmic)
346 			codec_dbg(codec, "Told to look for a headset mic, but didn't find any.\n");
347 		if (hpmic)
348 			codec_dbg(codec, "Told to look for a headphone mic, but didn't find any.\n");
349 	}
350 
351 	/* FIX-UP:
352 	 * If no line-out is defined but multiple HPs are found,
353 	 * some of them might be the real line-outs.
354 	 */
355 	if (!cfg->line_outs && cfg->hp_outs > 1 &&
356 	    !(cond_flags & HDA_PINCFG_NO_HP_FIXUP)) {
357 		int i = 0;
358 		while (i < cfg->hp_outs) {
359 			/* The real HPs should have the sequence 0x0f */
360 			if ((hp_out[i].seq & 0x0f) == 0x0f) {
361 				i++;
362 				continue;
363 			}
364 			/* Move it to the line-out table */
365 			line_out[cfg->line_outs++] = hp_out[i];
366 			cfg->hp_outs--;
367 			memmove(hp_out + i, hp_out + i + 1,
368 				sizeof(hp_out[0]) * (cfg->hp_outs - i));
369 		}
370 		memset(hp_out + cfg->hp_outs, 0,
371 		       sizeof(hp_out[0]) * (AUTO_CFG_MAX_OUTS - cfg->hp_outs));
372 		if (!cfg->hp_outs)
373 			cfg->line_out_type = AUTO_PIN_HP_OUT;
374 
375 	}
376 
377 	/* sort by sequence */
378 	sort_pins_by_sequence(cfg->line_out_pins, line_out, cfg->line_outs);
379 	sort_pins_by_sequence(cfg->speaker_pins, speaker_out,
380 			      cfg->speaker_outs);
381 	sort_pins_by_sequence(cfg->hp_pins, hp_out, cfg->hp_outs);
382 
383 	/*
384 	 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
385 	 * as a primary output
386 	 */
387 	if (!cfg->line_outs &&
388 	    !(cond_flags & HDA_PINCFG_NO_LO_FIXUP)) {
389 		if (cfg->speaker_outs) {
390 			cfg->line_outs = cfg->speaker_outs;
391 			memcpy(cfg->line_out_pins, cfg->speaker_pins,
392 			       sizeof(cfg->speaker_pins));
393 			cfg->speaker_outs = 0;
394 			memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
395 			cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
396 		} else if (cfg->hp_outs) {
397 			cfg->line_outs = cfg->hp_outs;
398 			memcpy(cfg->line_out_pins, cfg->hp_pins,
399 			       sizeof(cfg->hp_pins));
400 			cfg->hp_outs = 0;
401 			memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
402 			cfg->line_out_type = AUTO_PIN_HP_OUT;
403 		}
404 	}
405 
406 	reorder_outputs(cfg->line_outs, cfg->line_out_pins);
407 	reorder_outputs(cfg->hp_outs, cfg->hp_pins);
408 	reorder_outputs(cfg->speaker_outs, cfg->speaker_pins);
409 
410 	/* sort inputs in the order of AUTO_PIN_* type */
411 	sort(cfg->inputs, cfg->num_inputs, sizeof(cfg->inputs[0]),
412 	     compare_input_type, NULL);
413 
414 	/*
415 	 * debug prints of the parsed results
416 	 */
417 	codec_info(codec, "autoconfig for %s: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x) type:%s\n",
418 		   codec->core.chip_name, cfg->line_outs, cfg->line_out_pins[0],
419 		   cfg->line_out_pins[1], cfg->line_out_pins[2],
420 		   cfg->line_out_pins[3], cfg->line_out_pins[4],
421 		   cfg->line_out_type == AUTO_PIN_HP_OUT ? "hp" :
422 		   (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT ?
423 		    "speaker" : "line"));
424 	codec_info(codec, "   speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
425 		   cfg->speaker_outs, cfg->speaker_pins[0],
426 		   cfg->speaker_pins[1], cfg->speaker_pins[2],
427 		   cfg->speaker_pins[3], cfg->speaker_pins[4]);
428 	codec_info(codec, "   hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
429 		   cfg->hp_outs, cfg->hp_pins[0],
430 		   cfg->hp_pins[1], cfg->hp_pins[2],
431 		   cfg->hp_pins[3], cfg->hp_pins[4]);
432 	codec_info(codec, "   mono: mono_out=0x%x\n", cfg->mono_out_pin);
433 	if (cfg->dig_outs)
434 		codec_info(codec, "   dig-out=0x%x/0x%x\n",
435 			   cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
436 	codec_info(codec, "   inputs:\n");
437 	for (i = 0; i < cfg->num_inputs; i++) {
438 		codec_info(codec, "     %s=0x%x\n",
439 			    hda_get_autocfg_input_label(codec, cfg, i),
440 			    cfg->inputs[i].pin);
441 	}
442 	if (cfg->dig_in_pin)
443 		codec_info(codec, "   dig-in=0x%x\n", cfg->dig_in_pin);
444 
445 	return 0;
446 }
447 EXPORT_SYMBOL_GPL(snd_hda_parse_pin_defcfg);
448 
449 /**
450  * snd_hda_get_input_pin_attr - Get the input pin attribute from pin config
451  * @def_conf: pin configuration value
452  *
453  * Guess the input pin attribute (INPUT_PIN_ATTR_XXX) from the given
454  * default pin configuration value.
455  */
snd_hda_get_input_pin_attr(unsigned int def_conf)456 int snd_hda_get_input_pin_attr(unsigned int def_conf)
457 {
458 	unsigned int loc = get_defcfg_location(def_conf);
459 	unsigned int conn = get_defcfg_connect(def_conf);
460 	if (conn == AC_JACK_PORT_NONE)
461 		return INPUT_PIN_ATTR_UNUSED;
462 	/* Windows may claim the internal mic to be BOTH, too */
463 	if (conn == AC_JACK_PORT_FIXED || conn == AC_JACK_PORT_BOTH)
464 		return INPUT_PIN_ATTR_INT;
465 	if ((loc & 0x30) == AC_JACK_LOC_INTERNAL)
466 		return INPUT_PIN_ATTR_INT;
467 	if ((loc & 0x30) == AC_JACK_LOC_SEPARATE)
468 		return INPUT_PIN_ATTR_DOCK;
469 	if (loc == AC_JACK_LOC_REAR)
470 		return INPUT_PIN_ATTR_REAR;
471 	if (loc == AC_JACK_LOC_FRONT)
472 		return INPUT_PIN_ATTR_FRONT;
473 	return INPUT_PIN_ATTR_NORMAL;
474 }
475 EXPORT_SYMBOL_GPL(snd_hda_get_input_pin_attr);
476 
477 /**
478  * hda_get_input_pin_label - Give a label for the given input pin
479  * @codec: the HDA codec
480  * @item: ping config item to refer
481  * @pin: the pin NID
482  * @check_location: flag to add the jack location prefix
483  *
484  * When @check_location is true, the function checks the pin location
485  * for mic and line-in pins, and set an appropriate prefix like "Front",
486  * "Rear", "Internal".
487  */
hda_get_input_pin_label(struct hda_codec * codec,const struct auto_pin_cfg_item * item,hda_nid_t pin,bool check_location)488 static const char *hda_get_input_pin_label(struct hda_codec *codec,
489 					   const struct auto_pin_cfg_item *item,
490 					   hda_nid_t pin, bool check_location)
491 {
492 	unsigned int def_conf;
493 	static const char * const mic_names[] = {
494 		"Internal Mic", "Dock Mic", "Mic", "Rear Mic", "Front Mic"
495 	};
496 	int attr;
497 
498 	def_conf = snd_hda_codec_get_pincfg(codec, pin);
499 
500 	switch (get_defcfg_device(def_conf)) {
501 	case AC_JACK_MIC_IN:
502 		if (item && item->is_headset_mic)
503 			return "Headset Mic";
504 		if (item && item->is_headphone_mic)
505 			return "Headphone Mic";
506 		if (!check_location)
507 			return "Mic";
508 		attr = snd_hda_get_input_pin_attr(def_conf);
509 		if (!attr)
510 			return "None";
511 		return mic_names[attr - 1];
512 	case AC_JACK_LINE_IN:
513 		if (!check_location)
514 			return "Line";
515 		attr = snd_hda_get_input_pin_attr(def_conf);
516 		if (!attr)
517 			return "None";
518 		if (attr == INPUT_PIN_ATTR_DOCK)
519 			return "Dock Line";
520 		return "Line";
521 	case AC_JACK_AUX:
522 		return "Aux";
523 	case AC_JACK_CD:
524 		return "CD";
525 	case AC_JACK_SPDIF_IN:
526 		return "SPDIF In";
527 	case AC_JACK_DIG_OTHER_IN:
528 		return "Digital In";
529 	case AC_JACK_HP_OUT:
530 		return "Headphone Mic";
531 	default:
532 		return "Misc";
533 	}
534 }
535 
536 /* Check whether the location prefix needs to be added to the label.
537  * If all mic-jacks are in the same location (e.g. rear panel), we don't
538  * have to put "Front" prefix to each label.  In such a case, returns false.
539  */
check_mic_location_need(struct hda_codec * codec,const struct auto_pin_cfg * cfg,int input)540 static int check_mic_location_need(struct hda_codec *codec,
541 				   const struct auto_pin_cfg *cfg,
542 				   int input)
543 {
544 	unsigned int defc;
545 	int i, attr, attr2;
546 
547 	defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[input].pin);
548 	attr = snd_hda_get_input_pin_attr(defc);
549 	/* for internal or docking mics, we need locations */
550 	if (attr <= INPUT_PIN_ATTR_NORMAL)
551 		return 1;
552 
553 	attr = 0;
554 	for (i = 0; i < cfg->num_inputs; i++) {
555 		defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[i].pin);
556 		attr2 = snd_hda_get_input_pin_attr(defc);
557 		if (attr2 >= INPUT_PIN_ATTR_NORMAL) {
558 			if (attr && attr != attr2)
559 				return 1; /* different locations found */
560 			attr = attr2;
561 		}
562 	}
563 	return 0;
564 }
565 
566 /**
567  * hda_get_autocfg_input_label - Get a label for the given input
568  * @codec: the HDA codec
569  * @cfg: the parsed pin configuration
570  * @input: the input index number
571  *
572  * Get a label for the given input pin defined by the autocfg item.
573  * Unlike hda_get_input_pin_label(), this function checks all inputs
574  * defined in autocfg and avoids the redundant mic/line prefix as much as
575  * possible.
576  */
hda_get_autocfg_input_label(struct hda_codec * codec,const struct auto_pin_cfg * cfg,int input)577 const char *hda_get_autocfg_input_label(struct hda_codec *codec,
578 					const struct auto_pin_cfg *cfg,
579 					int input)
580 {
581 	int type = cfg->inputs[input].type;
582 	int has_multiple_pins = 0;
583 
584 	if ((input > 0 && cfg->inputs[input - 1].type == type) ||
585 	    (input < cfg->num_inputs - 1 && cfg->inputs[input + 1].type == type))
586 		has_multiple_pins = 1;
587 	if (has_multiple_pins && type == AUTO_PIN_MIC)
588 		has_multiple_pins &= check_mic_location_need(codec, cfg, input);
589 	has_multiple_pins |= codec->force_pin_prefix;
590 	return hda_get_input_pin_label(codec, &cfg->inputs[input],
591 				       cfg->inputs[input].pin,
592 				       has_multiple_pins);
593 }
594 EXPORT_SYMBOL_GPL(hda_get_autocfg_input_label);
595 
596 /* 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)597 static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
598 {
599 	int i;
600 	for (i = 0; i < nums; i++)
601 		if (list[i] == nid)
602 			return i;
603 	return -1;
604 }
605 
606 /* get a unique suffix or an index number */
check_output_sfx(hda_nid_t nid,const hda_nid_t * pins,int num_pins,int * indexp)607 static const char *check_output_sfx(hda_nid_t nid, const hda_nid_t *pins,
608 				    int num_pins, int *indexp)
609 {
610 	static const char * const channel_sfx[] = {
611 		" Front", " Surround", " CLFE", " Side"
612 	};
613 	int i;
614 
615 	i = find_idx_in_nid_list(nid, pins, num_pins);
616 	if (i < 0)
617 		return NULL;
618 	if (num_pins == 1)
619 		return "";
620 	if (num_pins > ARRAY_SIZE(channel_sfx)) {
621 		if (indexp)
622 			*indexp = i;
623 		return "";
624 	}
625 	return channel_sfx[i];
626 }
627 
check_output_pfx(struct hda_codec * codec,hda_nid_t nid)628 static const char *check_output_pfx(struct hda_codec *codec, hda_nid_t nid)
629 {
630 	unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
631 	int attr = snd_hda_get_input_pin_attr(def_conf);
632 
633 	/* check the location */
634 	switch (attr) {
635 	case INPUT_PIN_ATTR_DOCK:
636 		return "Dock ";
637 	case INPUT_PIN_ATTR_FRONT:
638 		return "Front ";
639 	}
640 	return "";
641 }
642 
get_hp_label_index(struct hda_codec * codec,hda_nid_t nid,const hda_nid_t * pins,int num_pins)643 static int get_hp_label_index(struct hda_codec *codec, hda_nid_t nid,
644 			      const hda_nid_t *pins, int num_pins)
645 {
646 	int i, j, idx = 0;
647 
648 	const char *pfx = check_output_pfx(codec, nid);
649 
650 	i = find_idx_in_nid_list(nid, pins, num_pins);
651 	if (i < 0)
652 		return -1;
653 	for (j = 0; j < i; j++)
654 		if (pfx == check_output_pfx(codec, pins[j]))
655 			idx++;
656 
657 	return idx;
658 }
659 
fill_audio_out_name(struct hda_codec * codec,hda_nid_t nid,const struct auto_pin_cfg * cfg,const char * name,char * label,int maxlen,int * indexp)660 static int fill_audio_out_name(struct hda_codec *codec, hda_nid_t nid,
661 			       const struct auto_pin_cfg *cfg,
662 			       const char *name, char *label, int maxlen,
663 			       int *indexp)
664 {
665 	unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
666 	int attr = snd_hda_get_input_pin_attr(def_conf);
667 	const char *pfx, *sfx = "";
668 
669 	/* handle as a speaker if it's a fixed line-out */
670 	if (!strcmp(name, "Line Out") && attr == INPUT_PIN_ATTR_INT)
671 		name = "Speaker";
672 	pfx = check_output_pfx(codec, nid);
673 
674 	if (cfg) {
675 		/* try to give a unique suffix if needed */
676 		sfx = check_output_sfx(nid, cfg->line_out_pins, cfg->line_outs,
677 				       indexp);
678 		if (!sfx)
679 			sfx = check_output_sfx(nid, cfg->speaker_pins, cfg->speaker_outs,
680 					       indexp);
681 		if (!sfx) {
682 			/* don't add channel suffix for Headphone controls */
683 			int idx = get_hp_label_index(codec, nid, cfg->hp_pins,
684 						     cfg->hp_outs);
685 			if (idx >= 0 && indexp)
686 				*indexp = idx;
687 			sfx = "";
688 		}
689 	}
690 	snprintf(label, maxlen, "%s%s%s", pfx, name, sfx);
691 	return 1;
692 }
693 
694 #define is_hdmi_cfg(conf) \
695 	(get_defcfg_location(conf) == AC_JACK_LOC_HDMI)
696 
697 /**
698  * snd_hda_get_pin_label - Get a label for the given I/O pin
699  * @codec: the HDA codec
700  * @nid: pin NID
701  * @cfg: the parsed pin configuration
702  * @label: the string buffer to store
703  * @maxlen: the max length of string buffer (including termination)
704  * @indexp: the pointer to return the index number (for multiple ctls)
705  *
706  * Get a label for the given pin.  This function works for both input and
707  * output pins.  When @cfg is given as non-NULL, the function tries to get
708  * an optimized label using hda_get_autocfg_input_label().
709  *
710  * This function tries to give a unique label string for the pin as much as
711  * possible.  For example, when the multiple line-outs are present, it adds
712  * the channel suffix like "Front", "Surround", etc (only when @cfg is given).
713  * If no unique name with a suffix is available and @indexp is non-NULL, the
714  * index number is stored in the pointer.
715  */
snd_hda_get_pin_label(struct hda_codec * codec,hda_nid_t nid,const struct auto_pin_cfg * cfg,char * label,int maxlen,int * indexp)716 int snd_hda_get_pin_label(struct hda_codec *codec, hda_nid_t nid,
717 			  const struct auto_pin_cfg *cfg,
718 			  char *label, int maxlen, int *indexp)
719 {
720 	unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
721 	const char *name = NULL;
722 	int i;
723 	bool hdmi;
724 
725 	if (indexp)
726 		*indexp = 0;
727 	if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
728 		return 0;
729 
730 	switch (get_defcfg_device(def_conf)) {
731 	case AC_JACK_LINE_OUT:
732 		return fill_audio_out_name(codec, nid, cfg, "Line Out",
733 					   label, maxlen, indexp);
734 	case AC_JACK_SPEAKER:
735 		return fill_audio_out_name(codec, nid, cfg, "Speaker",
736 					   label, maxlen, indexp);
737 	case AC_JACK_HP_OUT:
738 		return fill_audio_out_name(codec, nid, cfg, "Headphone",
739 					   label, maxlen, indexp);
740 	case AC_JACK_SPDIF_OUT:
741 	case AC_JACK_DIG_OTHER_OUT:
742 		hdmi = is_hdmi_cfg(def_conf);
743 		name = hdmi ? "HDMI" : "SPDIF";
744 		if (cfg && indexp)
745 			for (i = 0; i < cfg->dig_outs; i++) {
746 				hda_nid_t pin = cfg->dig_out_pins[i];
747 				unsigned int c;
748 				if (pin == nid)
749 					break;
750 				c = snd_hda_codec_get_pincfg(codec, pin);
751 				if (hdmi == is_hdmi_cfg(c))
752 					(*indexp)++;
753 			}
754 		break;
755 	default:
756 		if (cfg) {
757 			for (i = 0; i < cfg->num_inputs; i++) {
758 				if (cfg->inputs[i].pin != nid)
759 					continue;
760 				name = hda_get_autocfg_input_label(codec, cfg, i);
761 				if (name)
762 					break;
763 			}
764 		}
765 		if (!name)
766 			name = hda_get_input_pin_label(codec, NULL, nid, true);
767 		break;
768 	}
769 	if (!name)
770 		return 0;
771 	strlcpy(label, name, maxlen);
772 	return 1;
773 }
774 EXPORT_SYMBOL_GPL(snd_hda_get_pin_label);
775 
776 /**
777  * snd_hda_add_verbs - Add verbs to the init list
778  * @codec: the HDA codec
779  * @list: zero-terminated verb list to add
780  *
781  * Append the given verb list to the execution list.  The verbs will be
782  * performed at init and resume time via snd_hda_apply_verbs().
783  */
snd_hda_add_verbs(struct hda_codec * codec,const struct hda_verb * list)784 int snd_hda_add_verbs(struct hda_codec *codec,
785 		      const struct hda_verb *list)
786 {
787 	const struct hda_verb **v;
788 	v = snd_array_new(&codec->verbs);
789 	if (!v)
790 		return -ENOMEM;
791 	*v = list;
792 	return 0;
793 }
794 EXPORT_SYMBOL_GPL(snd_hda_add_verbs);
795 
796 /**
797  * snd_hda_apply_verbs - Execute the init verb lists
798  * @codec: the HDA codec
799  */
snd_hda_apply_verbs(struct hda_codec * codec)800 void snd_hda_apply_verbs(struct hda_codec *codec)
801 {
802 	const struct hda_verb **v;
803 	int i;
804 
805 	snd_array_for_each(&codec->verbs, i, v)
806 		snd_hda_sequence_write(codec, *v);
807 }
808 EXPORT_SYMBOL_GPL(snd_hda_apply_verbs);
809 
810 /**
811  * snd_hda_apply_pincfgs - Set each pin config in the given list
812  * @codec: the HDA codec
813  * @cfg: NULL-terminated pin config table
814  */
snd_hda_apply_pincfgs(struct hda_codec * codec,const struct hda_pintbl * cfg)815 void snd_hda_apply_pincfgs(struct hda_codec *codec,
816 			   const struct hda_pintbl *cfg)
817 {
818 	for (; cfg->nid; cfg++)
819 		snd_hda_codec_set_pincfg(codec, cfg->nid, cfg->val);
820 }
821 EXPORT_SYMBOL_GPL(snd_hda_apply_pincfgs);
822 
set_pin_targets(struct hda_codec * codec,const struct hda_pintbl * cfg)823 static void set_pin_targets(struct hda_codec *codec,
824 			    const struct hda_pintbl *cfg)
825 {
826 	for (; cfg->nid; cfg++)
827 		snd_hda_set_pin_ctl_cache(codec, cfg->nid, cfg->val);
828 }
829 
apply_fixup(struct hda_codec * codec,int id,int action,int depth)830 static void apply_fixup(struct hda_codec *codec, int id, int action, int depth)
831 {
832 	const char *modelname = codec->fixup_name;
833 
834 	while (id >= 0) {
835 		const struct hda_fixup *fix = codec->fixup_list + id;
836 
837 		if (++depth > 10)
838 			break;
839 		if (fix->chained_before)
840 			apply_fixup(codec, fix->chain_id, action, depth + 1);
841 
842 		switch (fix->type) {
843 		case HDA_FIXUP_PINS:
844 			if (action != HDA_FIXUP_ACT_PRE_PROBE || !fix->v.pins)
845 				break;
846 			codec_dbg(codec, "%s: Apply pincfg for %s\n",
847 				    codec->core.chip_name, modelname);
848 			snd_hda_apply_pincfgs(codec, fix->v.pins);
849 			break;
850 		case HDA_FIXUP_VERBS:
851 			if (action != HDA_FIXUP_ACT_PROBE || !fix->v.verbs)
852 				break;
853 			codec_dbg(codec, "%s: Apply fix-verbs for %s\n",
854 				    codec->core.chip_name, modelname);
855 			snd_hda_add_verbs(codec, fix->v.verbs);
856 			break;
857 		case HDA_FIXUP_FUNC:
858 			if (!fix->v.func)
859 				break;
860 			codec_dbg(codec, "%s: Apply fix-func for %s\n",
861 				    codec->core.chip_name, modelname);
862 			fix->v.func(codec, fix, action);
863 			break;
864 		case HDA_FIXUP_PINCTLS:
865 			if (action != HDA_FIXUP_ACT_PROBE || !fix->v.pins)
866 				break;
867 			codec_dbg(codec, "%s: Apply pinctl for %s\n",
868 				    codec->core.chip_name, modelname);
869 			set_pin_targets(codec, fix->v.pins);
870 			break;
871 		default:
872 			codec_err(codec, "%s: Invalid fixup type %d\n",
873 				   codec->core.chip_name, fix->type);
874 			break;
875 		}
876 		if (!fix->chained || fix->chained_before)
877 			break;
878 		id = fix->chain_id;
879 	}
880 }
881 
882 /**
883  * snd_hda_apply_fixup - Apply the fixup chain with the given action
884  * @codec: the HDA codec
885  * @action: fixup action (HDA_FIXUP_ACT_XXX)
886  */
snd_hda_apply_fixup(struct hda_codec * codec,int action)887 void snd_hda_apply_fixup(struct hda_codec *codec, int action)
888 {
889 	if (codec->fixup_list)
890 		apply_fixup(codec, codec->fixup_id, action, 0);
891 }
892 EXPORT_SYMBOL_GPL(snd_hda_apply_fixup);
893 
894 #define IGNORE_SEQ_ASSOC (~(AC_DEFCFG_SEQUENCE | AC_DEFCFG_DEF_ASSOC))
895 
pin_config_match(struct hda_codec * codec,const struct hda_pintbl * pins)896 static bool pin_config_match(struct hda_codec *codec,
897 			     const struct hda_pintbl *pins)
898 {
899 	const struct hda_pincfg *pin;
900 	int i;
901 
902 	snd_array_for_each(&codec->init_pins, i, pin) {
903 		hda_nid_t nid = pin->nid;
904 		u32 cfg = pin->cfg;
905 		const struct hda_pintbl *t_pins;
906 		int found;
907 
908 		t_pins = pins;
909 		found = 0;
910 		for (; t_pins->nid; t_pins++) {
911 			if (t_pins->nid == nid) {
912 				found = 1;
913 				if ((t_pins->val & IGNORE_SEQ_ASSOC) == (cfg & IGNORE_SEQ_ASSOC))
914 					break;
915 				else if ((cfg & 0xf0000000) == 0x40000000 && (t_pins->val & 0xf0000000) == 0x40000000)
916 					break;
917 				else
918 					return false;
919 			}
920 		}
921 		if (!found && (cfg & 0xf0000000) != 0x40000000)
922 			return false;
923 	}
924 
925 	return true;
926 }
927 
928 /**
929  * snd_hda_pick_pin_fixup - Pick up a fixup matching with the pin quirk list
930  * @codec: the HDA codec
931  * @pin_quirk: zero-terminated pin quirk list
932  * @fixlist: the fixup list
933  */
snd_hda_pick_pin_fixup(struct hda_codec * codec,const struct snd_hda_pin_quirk * pin_quirk,const struct hda_fixup * fixlist)934 void snd_hda_pick_pin_fixup(struct hda_codec *codec,
935 			    const struct snd_hda_pin_quirk *pin_quirk,
936 			    const struct hda_fixup *fixlist)
937 {
938 	const struct snd_hda_pin_quirk *pq;
939 
940 	if (codec->fixup_id != HDA_FIXUP_ID_NOT_SET)
941 		return;
942 
943 	for (pq = pin_quirk; pq->subvendor; pq++) {
944 		if ((codec->core.subsystem_id & 0xffff0000) != (pq->subvendor << 16))
945 			continue;
946 		if (codec->core.vendor_id != pq->codec)
947 			continue;
948 		if (pin_config_match(codec, pq->pins)) {
949 			codec->fixup_id = pq->value;
950 #ifdef CONFIG_SND_DEBUG_VERBOSE
951 			codec->fixup_name = pq->name;
952 			codec_dbg(codec, "%s: picked fixup %s (pin match)\n",
953 				  codec->core.chip_name, codec->fixup_name);
954 #endif
955 			codec->fixup_list = fixlist;
956 			return;
957 		}
958 	}
959 }
960 EXPORT_SYMBOL_GPL(snd_hda_pick_pin_fixup);
961 
962 /**
963  * snd_hda_pick_fixup - Pick up a fixup matching with PCI/codec SSID or model string
964  * @codec: the HDA codec
965  * @models: NULL-terminated model string list
966  * @quirk: zero-terminated PCI/codec SSID quirk list
967  * @fixlist: the fixup list
968  *
969  * Pick up a fixup entry matching with the given model string or SSID.
970  * If a fixup was already set beforehand, the function doesn't do anything.
971  * When a special model string "nofixup" is given, also no fixup is applied.
972  *
973  * The function tries to find the matching model name at first, if given.
974  * If nothing matched, try to look up the PCI SSID.
975  * If still nothing matched, try to look up the codec SSID.
976  */
snd_hda_pick_fixup(struct hda_codec * codec,const struct hda_model_fixup * models,const struct snd_pci_quirk * quirk,const struct hda_fixup * fixlist)977 void snd_hda_pick_fixup(struct hda_codec *codec,
978 			const struct hda_model_fixup *models,
979 			const struct snd_pci_quirk *quirk,
980 			const struct hda_fixup *fixlist)
981 {
982 	const struct snd_pci_quirk *q;
983 	int id = HDA_FIXUP_ID_NOT_SET;
984 	const char *name = NULL;
985 
986 	if (codec->fixup_id != HDA_FIXUP_ID_NOT_SET)
987 		return;
988 
989 	/* when model=nofixup is given, don't pick up any fixups */
990 	if (codec->modelname && !strcmp(codec->modelname, "nofixup")) {
991 		codec->fixup_list = NULL;
992 		codec->fixup_name = NULL;
993 		codec->fixup_id = HDA_FIXUP_ID_NO_FIXUP;
994 		codec_dbg(codec, "%s: picked no fixup (nofixup specified)\n",
995 			  codec->core.chip_name);
996 		return;
997 	}
998 
999 	if (codec->modelname && models) {
1000 		while (models->name) {
1001 			if (!strcmp(codec->modelname, models->name)) {
1002 				codec->fixup_id = models->id;
1003 				codec->fixup_name = models->name;
1004 				codec->fixup_list = fixlist;
1005 				codec_dbg(codec, "%s: picked fixup %s (model specified)\n",
1006 					  codec->core.chip_name, codec->fixup_name);
1007 				return;
1008 			}
1009 			models++;
1010 		}
1011 	}
1012 	if (quirk) {
1013 		q = snd_pci_quirk_lookup(codec->bus->pci, quirk);
1014 		if (q) {
1015 			id = q->value;
1016 #ifdef CONFIG_SND_DEBUG_VERBOSE
1017 			name = q->name;
1018 			codec_dbg(codec, "%s: picked fixup %s (PCI SSID%s)\n",
1019 				  codec->core.chip_name, name, q->subdevice_mask ? "" : " - vendor generic");
1020 #endif
1021 		}
1022 	}
1023 	if (id < 0 && quirk) {
1024 		for (q = quirk; q->subvendor || q->subdevice; q++) {
1025 			unsigned int vendorid =
1026 				q->subdevice | (q->subvendor << 16);
1027 			unsigned int mask = 0xffff0000 | q->subdevice_mask;
1028 			if ((codec->core.subsystem_id & mask) == (vendorid & mask)) {
1029 				id = q->value;
1030 #ifdef CONFIG_SND_DEBUG_VERBOSE
1031 				name = q->name;
1032 				codec_dbg(codec, "%s: picked fixup %s (codec SSID)\n",
1033 					  codec->core.chip_name, name);
1034 #endif
1035 				break;
1036 			}
1037 		}
1038 	}
1039 
1040 	codec->fixup_id = id;
1041 	if (id >= 0) {
1042 		codec->fixup_list = fixlist;
1043 		codec->fixup_name = name;
1044 	}
1045 }
1046 EXPORT_SYMBOL_GPL(snd_hda_pick_fixup);
1047