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