1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * BIOS auto-parser helper functions for HD-audio
5 * Copyright (c) 2012 Takashi Iwai <tiwai@suse.de>
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"
17 * Helper for automatic pin configuration
20 static int is_in_nid_list(hda_nid_t nid, const hda_nid_t *list)
28 /* a pair of input pin and its sequence */
34 static int compare_seq(const void *ap, const void *bp)
36 const struct auto_out_pin *a = ap;
37 const struct auto_out_pin *b = bp;
38 return (int)(a->seq - b->seq);
42 * Sort an associated group of pins according to their sequence numbers.
43 * then store it to a pin array.
45 static void sort_pins_by_sequence(hda_nid_t *pins, struct auto_out_pin *list,
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;
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)
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);
68 static int compare_input_type(const void *ap, const void *bp)
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);
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)
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);
86 /* Reorder the surround channels
87 * ALSA sequence is front/surr/clfe/side
89 * 4-ch: front/surr => OK as it is
90 * 6-ch: front/clfe/surr
91 * 8-ch: front/clfe/rear/side|fc
93 static void reorder_outputs(unsigned int nums, hda_nid_t *pins)
107 /* check whether the given pin has a proper pin I/O capability bit */
108 static bool check_pincap_validity(struct hda_codec *codec, hda_nid_t pin,
111 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
113 /* some old hardware don't return the proper pincaps */
118 case AC_JACK_LINE_OUT:
119 case AC_JACK_SPEAKER:
121 case AC_JACK_SPDIF_OUT:
122 case AC_JACK_DIG_OTHER_OUT:
123 return !!(pincap & AC_PINCAP_OUT);
125 return !!(pincap & AC_PINCAP_IN);
129 static bool can_be_headset_mic(struct hda_codec *codec,
130 struct auto_pin_cfg_item *item,
134 unsigned int def_conf;
135 if (item->type != AUTO_PIN_MIC)
138 if (item->is_headset_mic || item->is_headphone_mic)
139 return false; /* Already assigned */
141 def_conf = snd_hda_codec_get_pincfg(codec, item->pin);
142 attr = snd_hda_get_input_pin_attr(def_conf);
143 if (attr <= INPUT_PIN_ATTR_DOCK)
146 if (seq_number >= 0) {
147 int seq = get_defcfg_sequence(def_conf);
148 if (seq != seq_number)
156 * Parse all pin widgets and store the useful pin nids to cfg
158 * The number of line-outs or any primary output is stored in line_outs,
159 * and the corresponding output pins are assigned to line_out_pins[],
160 * in the order of front, rear, CLFE, side, ...
162 * If more extra outputs (speaker and headphone) are found, the pins are
163 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
164 * is detected, one of speaker of HP pins is assigned as the primary
165 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
166 * if any analog output exists.
168 * The analog input pins are assigned to inputs array.
169 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
172 int snd_hda_parse_pin_defcfg(struct hda_codec *codec,
173 struct auto_pin_cfg *cfg,
174 const hda_nid_t *ignore_nids,
175 unsigned int cond_flags)
178 short seq, assoc_line_out;
179 struct auto_out_pin line_out[ARRAY_SIZE(cfg->line_out_pins)];
180 struct auto_out_pin speaker_out[ARRAY_SIZE(cfg->speaker_pins)];
181 struct auto_out_pin hp_out[ARRAY_SIZE(cfg->hp_pins)];
184 if (!snd_hda_get_int_hint(codec, "parser_flags", &i))
187 memset(cfg, 0, sizeof(*cfg));
189 memset(line_out, 0, sizeof(line_out));
190 memset(speaker_out, 0, sizeof(speaker_out));
191 memset(hp_out, 0, sizeof(hp_out));
194 for_each_hda_codec_node(nid, codec) {
195 unsigned int wid_caps = get_wcaps(codec, nid);
196 unsigned int wid_type = get_wcaps_type(wid_caps);
197 unsigned int def_conf;
198 short assoc, loc, conn, dev;
200 /* read all default configuration for pin complex */
201 if (wid_type != AC_WID_PIN)
203 /* ignore the given nids (e.g. pc-beep returns error) */
204 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
207 def_conf = snd_hda_codec_get_pincfg(codec, nid);
208 conn = get_defcfg_connect(def_conf);
209 if (conn == AC_JACK_PORT_NONE)
211 loc = get_defcfg_location(def_conf);
212 dev = get_defcfg_device(def_conf);
214 /* workaround for buggy BIOS setups */
215 if (dev == AC_JACK_LINE_OUT) {
216 if (conn == AC_JACK_PORT_FIXED ||
217 conn == AC_JACK_PORT_BOTH)
218 dev = AC_JACK_SPEAKER;
221 if (!check_pincap_validity(codec, nid, dev))
225 case AC_JACK_LINE_OUT:
226 seq = get_defcfg_sequence(def_conf);
227 assoc = get_defcfg_association(def_conf);
229 if (!(wid_caps & AC_WCAP_STEREO))
230 if (!cfg->mono_out_pin)
231 cfg->mono_out_pin = nid;
235 assoc_line_out = assoc;
236 else if (assoc_line_out != assoc) {
238 "ignore pin 0x%x with mismatching assoc# 0x%x vs 0x%x\n",
239 nid, assoc, assoc_line_out);
242 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins)) {
244 "ignore pin 0x%x, too many assigned pins\n",
248 line_out[cfg->line_outs].pin = nid;
249 line_out[cfg->line_outs].seq = seq;
252 case AC_JACK_SPEAKER:
253 seq = get_defcfg_sequence(def_conf);
254 assoc = get_defcfg_association(def_conf);
255 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins)) {
257 "ignore pin 0x%x, too many assigned pins\n",
261 speaker_out[cfg->speaker_outs].pin = nid;
262 speaker_out[cfg->speaker_outs].seq = (assoc << 4) | seq;
266 seq = get_defcfg_sequence(def_conf);
267 assoc = get_defcfg_association(def_conf);
268 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins)) {
270 "ignore pin 0x%x, too many assigned pins\n",
274 hp_out[cfg->hp_outs].pin = nid;
275 hp_out[cfg->hp_outs].seq = (assoc << 4) | seq;
279 add_auto_cfg_input_pin(codec, cfg, nid, AUTO_PIN_MIC);
281 case AC_JACK_LINE_IN:
282 add_auto_cfg_input_pin(codec, cfg, nid, AUTO_PIN_LINE_IN);
285 add_auto_cfg_input_pin(codec, cfg, nid, AUTO_PIN_CD);
288 add_auto_cfg_input_pin(codec, cfg, nid, AUTO_PIN_AUX);
290 case AC_JACK_SPDIF_OUT:
291 case AC_JACK_DIG_OTHER_OUT:
292 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins)) {
294 "ignore pin 0x%x, too many assigned pins\n",
298 cfg->dig_out_pins[cfg->dig_outs] = nid;
299 cfg->dig_out_type[cfg->dig_outs] =
300 (loc == AC_JACK_LOC_HDMI) ?
301 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
304 case AC_JACK_SPDIF_IN:
305 case AC_JACK_DIG_OTHER_IN:
306 cfg->dig_in_pin = nid;
307 if (loc == AC_JACK_LOC_HDMI)
308 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
310 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
315 /* Find a pin that could be a headset or headphone mic */
316 if (cond_flags & HDA_PINCFG_HEADSET_MIC || cond_flags & HDA_PINCFG_HEADPHONE_MIC) {
317 bool hsmic = !!(cond_flags & HDA_PINCFG_HEADSET_MIC);
318 bool hpmic = !!(cond_flags & HDA_PINCFG_HEADPHONE_MIC);
319 for (i = 0; (hsmic || hpmic) && (i < cfg->num_inputs); i++)
320 if (hsmic && can_be_headset_mic(codec, &cfg->inputs[i], 0xc)) {
321 cfg->inputs[i].is_headset_mic = 1;
323 } else if (hpmic && can_be_headset_mic(codec, &cfg->inputs[i], 0xd)) {
324 cfg->inputs[i].is_headphone_mic = 1;
328 /* If we didn't find our sequence number mark, fall back to any sequence number */
329 for (i = 0; (hsmic || hpmic) && (i < cfg->num_inputs); i++) {
330 if (!can_be_headset_mic(codec, &cfg->inputs[i], -1))
333 cfg->inputs[i].is_headset_mic = 1;
336 cfg->inputs[i].is_headphone_mic = 1;
342 codec_dbg(codec, "Told to look for a headset mic, but didn't find any.\n");
344 codec_dbg(codec, "Told to look for a headphone mic, but didn't find any.\n");
348 * If no line-out is defined but multiple HPs are found,
349 * some of them might be the real line-outs.
351 if (!cfg->line_outs && cfg->hp_outs > 1 &&
352 !(cond_flags & HDA_PINCFG_NO_HP_FIXUP)) {
354 while (i < cfg->hp_outs) {
355 /* The real HPs should have the sequence 0x0f */
356 if ((hp_out[i].seq & 0x0f) == 0x0f) {
360 /* Move it to the line-out table */
361 line_out[cfg->line_outs++] = hp_out[i];
363 memmove(hp_out + i, hp_out + i + 1,
364 sizeof(hp_out[0]) * (cfg->hp_outs - i));
366 memset(hp_out + cfg->hp_outs, 0,
367 sizeof(hp_out[0]) * (AUTO_CFG_MAX_OUTS - cfg->hp_outs));
369 cfg->line_out_type = AUTO_PIN_HP_OUT;
373 /* sort by sequence */
374 sort_pins_by_sequence(cfg->line_out_pins, line_out, cfg->line_outs);
375 sort_pins_by_sequence(cfg->speaker_pins, speaker_out,
377 sort_pins_by_sequence(cfg->hp_pins, hp_out, cfg->hp_outs);
380 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
381 * as a primary output
383 if (!cfg->line_outs &&
384 !(cond_flags & HDA_PINCFG_NO_LO_FIXUP)) {
385 if (cfg->speaker_outs) {
386 cfg->line_outs = cfg->speaker_outs;
387 memcpy(cfg->line_out_pins, cfg->speaker_pins,
388 sizeof(cfg->speaker_pins));
389 cfg->speaker_outs = 0;
390 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
391 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
392 } else if (cfg->hp_outs) {
393 cfg->line_outs = cfg->hp_outs;
394 memcpy(cfg->line_out_pins, cfg->hp_pins,
395 sizeof(cfg->hp_pins));
397 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
398 cfg->line_out_type = AUTO_PIN_HP_OUT;
402 reorder_outputs(cfg->line_outs, cfg->line_out_pins);
403 reorder_outputs(cfg->hp_outs, cfg->hp_pins);
404 reorder_outputs(cfg->speaker_outs, cfg->speaker_pins);
406 /* sort inputs in the order of AUTO_PIN_* type */
407 sort(cfg->inputs, cfg->num_inputs, sizeof(cfg->inputs[0]),
408 compare_input_type, NULL);
411 * debug prints of the parsed results
413 codec_info(codec, "autoconfig for %s: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x) type:%s\n",
414 codec->core.chip_name, cfg->line_outs, cfg->line_out_pins[0],
415 cfg->line_out_pins[1], cfg->line_out_pins[2],
416 cfg->line_out_pins[3], cfg->line_out_pins[4],
417 cfg->line_out_type == AUTO_PIN_HP_OUT ? "hp" :
418 (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT ?
419 "speaker" : "line"));
420 codec_info(codec, " speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
421 cfg->speaker_outs, cfg->speaker_pins[0],
422 cfg->speaker_pins[1], cfg->speaker_pins[2],
423 cfg->speaker_pins[3], cfg->speaker_pins[4]);
424 codec_info(codec, " hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
425 cfg->hp_outs, cfg->hp_pins[0],
426 cfg->hp_pins[1], cfg->hp_pins[2],
427 cfg->hp_pins[3], cfg->hp_pins[4]);
428 codec_info(codec, " mono: mono_out=0x%x\n", cfg->mono_out_pin);
430 codec_info(codec, " dig-out=0x%x/0x%x\n",
431 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
432 codec_info(codec, " inputs:\n");
433 for (i = 0; i < cfg->num_inputs; i++) {
434 codec_info(codec, " %s=0x%x\n",
435 hda_get_autocfg_input_label(codec, cfg, i),
439 codec_info(codec, " dig-in=0x%x\n", cfg->dig_in_pin);
443 EXPORT_SYMBOL_GPL(snd_hda_parse_pin_defcfg);
446 * snd_hda_get_input_pin_attr - Get the input pin attribute from pin config
447 * @def_conf: pin configuration value
449 * Guess the input pin attribute (INPUT_PIN_ATTR_XXX) from the given
450 * default pin configuration value.
452 int snd_hda_get_input_pin_attr(unsigned int def_conf)
454 unsigned int loc = get_defcfg_location(def_conf);
455 unsigned int conn = get_defcfg_connect(def_conf);
456 if (conn == AC_JACK_PORT_NONE)
457 return INPUT_PIN_ATTR_UNUSED;
458 /* Windows may claim the internal mic to be BOTH, too */
459 if (conn == AC_JACK_PORT_FIXED || conn == AC_JACK_PORT_BOTH)
460 return INPUT_PIN_ATTR_INT;
461 if ((loc & 0x30) == AC_JACK_LOC_INTERNAL)
462 return INPUT_PIN_ATTR_INT;
463 if ((loc & 0x30) == AC_JACK_LOC_SEPARATE)
464 return INPUT_PIN_ATTR_DOCK;
465 if (loc == AC_JACK_LOC_REAR)
466 return INPUT_PIN_ATTR_REAR;
467 if (loc == AC_JACK_LOC_FRONT)
468 return INPUT_PIN_ATTR_FRONT;
469 return INPUT_PIN_ATTR_NORMAL;
471 EXPORT_SYMBOL_GPL(snd_hda_get_input_pin_attr);
474 * hda_get_input_pin_label - Give a label for the given input pin
475 * @codec: the HDA codec
476 * @item: ping config item to refer
478 * @check_location: flag to add the jack location prefix
480 * When @check_location is true, the function checks the pin location
481 * for mic and line-in pins, and set an appropriate prefix like "Front",
482 * "Rear", "Internal".
484 static const char *hda_get_input_pin_label(struct hda_codec *codec,
485 const struct auto_pin_cfg_item *item,
486 hda_nid_t pin, bool check_location)
488 unsigned int def_conf;
489 static const char * const mic_names[] = {
490 "Internal Mic", "Dock Mic", "Mic", "Rear Mic", "Front Mic"
494 def_conf = snd_hda_codec_get_pincfg(codec, pin);
496 switch (get_defcfg_device(def_conf)) {
498 if (item && item->is_headset_mic)
499 return "Headset Mic";
500 if (item && item->is_headphone_mic)
501 return "Headphone Mic";
504 attr = snd_hda_get_input_pin_attr(def_conf);
507 return mic_names[attr - 1];
508 case AC_JACK_LINE_IN:
511 attr = snd_hda_get_input_pin_attr(def_conf);
514 if (attr == INPUT_PIN_ATTR_DOCK)
521 case AC_JACK_SPDIF_IN:
523 case AC_JACK_DIG_OTHER_IN:
526 return "Headphone Mic";
532 /* Check whether the location prefix needs to be added to the label.
533 * If all mic-jacks are in the same location (e.g. rear panel), we don't
534 * have to put "Front" prefix to each label. In such a case, returns false.
536 static int check_mic_location_need(struct hda_codec *codec,
537 const struct auto_pin_cfg *cfg,
543 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[input].pin);
544 attr = snd_hda_get_input_pin_attr(defc);
545 /* for internal or docking mics, we need locations */
546 if (attr <= INPUT_PIN_ATTR_NORMAL)
550 for (i = 0; i < cfg->num_inputs; i++) {
551 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[i].pin);
552 attr2 = snd_hda_get_input_pin_attr(defc);
553 if (attr2 >= INPUT_PIN_ATTR_NORMAL) {
554 if (attr && attr != attr2)
555 return 1; /* different locations found */
563 * hda_get_autocfg_input_label - Get a label for the given input
564 * @codec: the HDA codec
565 * @cfg: the parsed pin configuration
566 * @input: the input index number
568 * Get a label for the given input pin defined by the autocfg item.
569 * Unlike hda_get_input_pin_label(), this function checks all inputs
570 * defined in autocfg and avoids the redundant mic/line prefix as much as
573 const char *hda_get_autocfg_input_label(struct hda_codec *codec,
574 const struct auto_pin_cfg *cfg,
577 int type = cfg->inputs[input].type;
578 int has_multiple_pins = 0;
580 if ((input > 0 && cfg->inputs[input - 1].type == type) ||
581 (input < cfg->num_inputs - 1 && cfg->inputs[input + 1].type == type))
582 has_multiple_pins = 1;
583 if (has_multiple_pins && type == AUTO_PIN_MIC)
584 has_multiple_pins &= check_mic_location_need(codec, cfg, input);
585 has_multiple_pins |= codec->force_pin_prefix;
586 return hda_get_input_pin_label(codec, &cfg->inputs[input],
587 cfg->inputs[input].pin,
590 EXPORT_SYMBOL_GPL(hda_get_autocfg_input_label);
592 /* return the position of NID in the list, or -1 if not found */
593 static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
596 for (i = 0; i < nums; i++)
602 /* get a unique suffix or an index number */
603 static const char *check_output_sfx(hda_nid_t nid, const hda_nid_t *pins,
604 int num_pins, int *indexp)
606 static const char * const channel_sfx[] = {
607 " Front", " Surround", " CLFE", " Side"
611 i = find_idx_in_nid_list(nid, pins, num_pins);
616 if (num_pins > ARRAY_SIZE(channel_sfx)) {
621 return channel_sfx[i];
624 static const char *check_output_pfx(struct hda_codec *codec, hda_nid_t nid)
626 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
627 int attr = snd_hda_get_input_pin_attr(def_conf);
629 /* check the location */
631 case INPUT_PIN_ATTR_DOCK:
633 case INPUT_PIN_ATTR_FRONT:
639 static int get_hp_label_index(struct hda_codec *codec, hda_nid_t nid,
640 const hda_nid_t *pins, int num_pins)
644 const char *pfx = check_output_pfx(codec, nid);
646 i = find_idx_in_nid_list(nid, pins, num_pins);
649 for (j = 0; j < i; j++)
650 if (pfx == check_output_pfx(codec, pins[j]))
656 static int fill_audio_out_name(struct hda_codec *codec, hda_nid_t nid,
657 const struct auto_pin_cfg *cfg,
658 const char *name, char *label, int maxlen,
661 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
662 int attr = snd_hda_get_input_pin_attr(def_conf);
663 const char *pfx, *sfx = "";
665 /* handle as a speaker if it's a fixed line-out */
666 if (!strcmp(name, "Line Out") && attr == INPUT_PIN_ATTR_INT)
668 pfx = check_output_pfx(codec, nid);
671 /* try to give a unique suffix if needed */
672 sfx = check_output_sfx(nid, cfg->line_out_pins, cfg->line_outs,
675 sfx = check_output_sfx(nid, cfg->speaker_pins, cfg->speaker_outs,
678 /* don't add channel suffix for Headphone controls */
679 int idx = get_hp_label_index(codec, nid, cfg->hp_pins,
681 if (idx >= 0 && indexp)
686 snprintf(label, maxlen, "%s%s%s", pfx, name, sfx);
690 #define is_hdmi_cfg(conf) \
691 (get_defcfg_location(conf) == AC_JACK_LOC_HDMI)
694 * snd_hda_get_pin_label - Get a label for the given I/O pin
695 * @codec: the HDA codec
697 * @cfg: the parsed pin configuration
698 * @label: the string buffer to store
699 * @maxlen: the max length of string buffer (including termination)
700 * @indexp: the pointer to return the index number (for multiple ctls)
702 * Get a label for the given pin. This function works for both input and
703 * output pins. When @cfg is given as non-NULL, the function tries to get
704 * an optimized label using hda_get_autocfg_input_label().
706 * This function tries to give a unique label string for the pin as much as
707 * possible. For example, when the multiple line-outs are present, it adds
708 * the channel suffix like "Front", "Surround", etc (only when @cfg is given).
709 * If no unique name with a suffix is available and @indexp is non-NULL, the
710 * index number is stored in the pointer.
712 int snd_hda_get_pin_label(struct hda_codec *codec, hda_nid_t nid,
713 const struct auto_pin_cfg *cfg,
714 char *label, int maxlen, int *indexp)
716 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
717 const char *name = NULL;
723 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
726 switch (get_defcfg_device(def_conf)) {
727 case AC_JACK_LINE_OUT:
728 return fill_audio_out_name(codec, nid, cfg, "Line Out",
729 label, maxlen, indexp);
730 case AC_JACK_SPEAKER:
731 return fill_audio_out_name(codec, nid, cfg, "Speaker",
732 label, maxlen, indexp);
734 return fill_audio_out_name(codec, nid, cfg, "Headphone",
735 label, maxlen, indexp);
736 case AC_JACK_SPDIF_OUT:
737 case AC_JACK_DIG_OTHER_OUT:
738 hdmi = is_hdmi_cfg(def_conf);
739 name = hdmi ? "HDMI" : "SPDIF";
741 for (i = 0; i < cfg->dig_outs; i++) {
742 hda_nid_t pin = cfg->dig_out_pins[i];
746 c = snd_hda_codec_get_pincfg(codec, pin);
747 if (hdmi == is_hdmi_cfg(c))
753 for (i = 0; i < cfg->num_inputs; i++) {
754 if (cfg->inputs[i].pin != nid)
756 name = hda_get_autocfg_input_label(codec, cfg, i);
762 name = hda_get_input_pin_label(codec, NULL, nid, true);
767 strscpy(label, name, maxlen);
770 EXPORT_SYMBOL_GPL(snd_hda_get_pin_label);
773 * snd_hda_add_verbs - Add verbs to the init list
774 * @codec: the HDA codec
775 * @list: zero-terminated verb list to add
777 * Append the given verb list to the execution list. The verbs will be
778 * performed at init and resume time via snd_hda_apply_verbs().
780 int snd_hda_add_verbs(struct hda_codec *codec,
781 const struct hda_verb *list)
783 const struct hda_verb **v;
784 v = snd_array_new(&codec->verbs);
790 EXPORT_SYMBOL_GPL(snd_hda_add_verbs);
793 * snd_hda_apply_verbs - Execute the init verb lists
794 * @codec: the HDA codec
796 void snd_hda_apply_verbs(struct hda_codec *codec)
798 const struct hda_verb **v;
801 snd_array_for_each(&codec->verbs, i, v)
802 snd_hda_sequence_write(codec, *v);
804 EXPORT_SYMBOL_GPL(snd_hda_apply_verbs);
807 * snd_hda_apply_pincfgs - Set each pin config in the given list
808 * @codec: the HDA codec
809 * @cfg: NULL-terminated pin config table
811 void snd_hda_apply_pincfgs(struct hda_codec *codec,
812 const struct hda_pintbl *cfg)
814 for (; cfg->nid; cfg++)
815 snd_hda_codec_set_pincfg(codec, cfg->nid, cfg->val);
817 EXPORT_SYMBOL_GPL(snd_hda_apply_pincfgs);
819 static void set_pin_targets(struct hda_codec *codec,
820 const struct hda_pintbl *cfg)
822 for (; cfg->nid; cfg++)
823 snd_hda_set_pin_ctl_cache(codec, cfg->nid, cfg->val);
826 void __snd_hda_apply_fixup(struct hda_codec *codec, int id, int action, int depth)
828 const char *modelname = codec->fixup_name;
831 const struct hda_fixup *fix = codec->fixup_list + id;
835 if (fix->chained_before)
836 __snd_hda_apply_fixup(codec, fix->chain_id, action, depth + 1);
840 if (action != HDA_FIXUP_ACT_PRE_PROBE || !fix->v.pins)
842 codec_dbg(codec, "%s: Apply pincfg for %s\n",
843 codec->core.chip_name, modelname);
844 snd_hda_apply_pincfgs(codec, fix->v.pins);
846 case HDA_FIXUP_VERBS:
847 if (action != HDA_FIXUP_ACT_PROBE || !fix->v.verbs)
849 codec_dbg(codec, "%s: Apply fix-verbs for %s\n",
850 codec->core.chip_name, modelname);
851 snd_hda_add_verbs(codec, fix->v.verbs);
856 codec_dbg(codec, "%s: Apply fix-func for %s\n",
857 codec->core.chip_name, modelname);
858 fix->v.func(codec, fix, action);
860 case HDA_FIXUP_PINCTLS:
861 if (action != HDA_FIXUP_ACT_PROBE || !fix->v.pins)
863 codec_dbg(codec, "%s: Apply pinctl for %s\n",
864 codec->core.chip_name, modelname);
865 set_pin_targets(codec, fix->v.pins);
868 codec_err(codec, "%s: Invalid fixup type %d\n",
869 codec->core.chip_name, fix->type);
872 if (!fix->chained || fix->chained_before)
877 EXPORT_SYMBOL_GPL(__snd_hda_apply_fixup);
880 * snd_hda_apply_fixup - Apply the fixup chain with the given action
881 * @codec: the HDA codec
882 * @action: fixup action (HDA_FIXUP_ACT_XXX)
884 void snd_hda_apply_fixup(struct hda_codec *codec, int action)
886 if (codec->fixup_list)
887 __snd_hda_apply_fixup(codec, codec->fixup_id, action, 0);
889 EXPORT_SYMBOL_GPL(snd_hda_apply_fixup);
891 #define IGNORE_SEQ_ASSOC (~(AC_DEFCFG_SEQUENCE | AC_DEFCFG_DEF_ASSOC))
893 static bool pin_config_match(struct hda_codec *codec,
894 const struct hda_pintbl *pins,
897 const struct hda_pincfg *pin;
900 snd_array_for_each(&codec->init_pins, i, pin) {
901 hda_nid_t nid = pin->nid;
903 const struct hda_pintbl *t_pins;
908 for (; t_pins->nid; t_pins++) {
909 if (t_pins->nid == nid) {
911 if ((t_pins->val & IGNORE_SEQ_ASSOC) == (cfg & IGNORE_SEQ_ASSOC))
913 else if ((cfg & 0xf0000000) == 0x40000000 && (t_pins->val & 0xf0000000) == 0x40000000)
919 if (match_all_pins &&
920 !found && (cfg & 0xf0000000) != 0x40000000)
928 * snd_hda_pick_pin_fixup - Pick up a fixup matching with the pin quirk list
929 * @codec: the HDA codec
930 * @pin_quirk: zero-terminated pin quirk list
931 * @fixlist: the fixup list
932 * @match_all_pins: all valid pins must match with the table entries
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,
939 const struct snd_hda_pin_quirk *pq;
941 if (codec->fixup_id != HDA_FIXUP_ID_NOT_SET)
944 for (pq = pin_quirk; pq->subvendor; pq++) {
945 if ((codec->core.subsystem_id & 0xffff0000) != (pq->subvendor << 16))
947 if (codec->core.vendor_id != pq->codec)
949 if (pin_config_match(codec, pq->pins, match_all_pins)) {
950 codec->fixup_id = pq->value;
951 #ifdef CONFIG_SND_DEBUG_VERBOSE
952 codec->fixup_name = pq->name;
953 codec_dbg(codec, "%s: picked fixup %s (pin match)\n",
954 codec->core.chip_name, codec->fixup_name);
956 codec->fixup_list = fixlist;
961 EXPORT_SYMBOL_GPL(snd_hda_pick_pin_fixup);
964 * snd_hda_pick_fixup - Pick up a fixup matching with PCI/codec SSID or model string
965 * @codec: the HDA codec
966 * @models: NULL-terminated model string list
967 * @quirk: zero-terminated PCI/codec SSID quirk list
968 * @fixlist: the fixup list
970 * Pick up a fixup entry matching with the given model string or SSID.
971 * If a fixup was already set beforehand, the function doesn't do anything.
972 * When a special model string "nofixup" is given, also no fixup is applied.
974 * The function tries to find the matching model name at first, if given.
975 * If the model string contains the SSID alias, try to look up with the given
977 * If nothing matched, try to look up the PCI SSID.
978 * If still nothing matched, try to look up the codec SSID.
980 void snd_hda_pick_fixup(struct hda_codec *codec,
981 const struct hda_model_fixup *models,
982 const struct snd_pci_quirk *quirk,
983 const struct hda_fixup *fixlist)
985 const struct snd_pci_quirk *q;
986 int id = HDA_FIXUP_ID_NOT_SET;
987 const char *name = NULL;
988 const char *type = NULL;
989 unsigned int vendor, device;
991 if (codec->fixup_id != HDA_FIXUP_ID_NOT_SET)
994 /* when model=nofixup is given, don't pick up any fixups */
995 if (codec->modelname && !strcmp(codec->modelname, "nofixup")) {
996 id = HDA_FIXUP_ID_NO_FIXUP;
998 codec_dbg(codec, "%s: picked no fixup (nofixup specified)\n",
999 codec->core.chip_name);
1003 /* match with the model name string */
1004 if (codec->modelname && models) {
1005 while (models->name) {
1006 if (!strcmp(codec->modelname, models->name)) {
1008 name = models->name;
1009 codec_dbg(codec, "%s: picked fixup %s (model specified)\n",
1010 codec->core.chip_name, codec->fixup_name);
1020 /* match with the SSID alias given by the model string "XXXX:YYYY" */
1021 if (codec->modelname &&
1022 sscanf(codec->modelname, "%04x:%04x", &vendor, &device) == 2) {
1023 q = snd_pci_quirk_lookup_id(vendor, device, quirk);
1025 type = "alias SSID";
1030 /* match with the PCI SSID */
1031 q = snd_pci_quirk_lookup(codec->bus->pci, quirk);
1037 /* match with the codec SSID */
1038 q = snd_pci_quirk_lookup_id(codec->core.subsystem_id >> 16,
1039 codec->core.subsystem_id & 0xffff,
1042 type = "codec SSID";
1046 return; /* no matching */
1050 #ifdef CONFIG_SND_DEBUG_VERBOSE
1053 codec_dbg(codec, "%s: picked fixup %s for %s %04x:%04x\n",
1054 codec->core.chip_name, name ? name : "",
1055 type, q->subvendor, q->subdevice);
1057 codec->fixup_id = id;
1058 codec->fixup_list = fixlist;
1059 codec->fixup_name = name;
1061 EXPORT_SYMBOL_GPL(snd_hda_pick_fixup);