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)
98 swap(pins[1], pins[2]);
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,
107 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
109 /* some old hardware don't return the proper pincaps */
114 case AC_JACK_LINE_OUT:
115 case AC_JACK_SPEAKER:
117 case AC_JACK_SPDIF_OUT:
118 case AC_JACK_DIG_OTHER_OUT:
119 return !!(pincap & AC_PINCAP_OUT);
121 return !!(pincap & AC_PINCAP_IN);
125 static bool can_be_headset_mic(struct hda_codec *codec,
126 struct auto_pin_cfg_item *item,
130 unsigned int def_conf;
131 if (item->type != AUTO_PIN_MIC)
134 if (item->is_headset_mic || item->is_headphone_mic)
135 return false; /* Already assigned */
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)
142 if (seq_number >= 0) {
143 int seq = get_defcfg_sequence(def_conf);
144 if (seq != seq_number)
152 * Parse all pin widgets and store the useful pin nids to cfg
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, ...
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.
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,
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)
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)];
180 if (!snd_hda_get_int_hint(codec, "parser_flags", &i))
183 memset(cfg, 0, sizeof(*cfg));
185 memset(line_out, 0, sizeof(line_out));
186 memset(speaker_out, 0, sizeof(speaker_out));
187 memset(hp_out, 0, sizeof(hp_out));
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;
196 /* read all default configuration for pin complex */
197 if (wid_type != AC_WID_PIN)
199 /* ignore the given nids (e.g. pc-beep returns error) */
200 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
203 def_conf = snd_hda_codec_get_pincfg(codec, nid);
204 conn = get_defcfg_connect(def_conf);
205 if (conn == AC_JACK_PORT_NONE)
207 loc = get_defcfg_location(def_conf);
208 dev = get_defcfg_device(def_conf);
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;
217 if (!check_pincap_validity(codec, nid, dev))
221 case AC_JACK_LINE_OUT:
222 seq = get_defcfg_sequence(def_conf);
223 assoc = get_defcfg_association(def_conf);
225 if (!(wid_caps & AC_WCAP_STEREO))
226 if (!cfg->mono_out_pin)
227 cfg->mono_out_pin = nid;
231 assoc_line_out = assoc;
232 else if (assoc_line_out != assoc) {
234 "ignore pin 0x%x with mismatching assoc# 0x%x vs 0x%x\n",
235 nid, assoc, assoc_line_out);
238 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins)) {
240 "ignore pin 0x%x, too many assigned pins\n",
244 line_out[cfg->line_outs].pin = nid;
245 line_out[cfg->line_outs].seq = seq;
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)) {
253 "ignore pin 0x%x, too many assigned pins\n",
257 speaker_out[cfg->speaker_outs].pin = nid;
258 speaker_out[cfg->speaker_outs].seq = (assoc << 4) | seq;
262 seq = get_defcfg_sequence(def_conf);
263 assoc = get_defcfg_association(def_conf);
264 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins)) {
266 "ignore pin 0x%x, too many assigned pins\n",
270 hp_out[cfg->hp_outs].pin = nid;
271 hp_out[cfg->hp_outs].seq = (assoc << 4) | seq;
275 add_auto_cfg_input_pin(codec, cfg, nid, AUTO_PIN_MIC);
277 case AC_JACK_LINE_IN:
278 add_auto_cfg_input_pin(codec, cfg, nid, AUTO_PIN_LINE_IN);
281 add_auto_cfg_input_pin(codec, cfg, nid, AUTO_PIN_CD);
284 add_auto_cfg_input_pin(codec, cfg, nid, AUTO_PIN_AUX);
286 case AC_JACK_SPDIF_OUT:
287 case AC_JACK_DIG_OTHER_OUT:
288 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins)) {
290 "ignore pin 0x%x, too many assigned pins\n",
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;
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;
306 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
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;
319 } else if (hpmic && can_be_headset_mic(codec, &cfg->inputs[i], 0xd)) {
320 cfg->inputs[i].is_headphone_mic = 1;
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))
329 cfg->inputs[i].is_headset_mic = 1;
332 cfg->inputs[i].is_headphone_mic = 1;
338 codec_dbg(codec, "Told to look for a headset mic, but didn't find any.\n");
340 codec_dbg(codec, "Told to look for a headphone mic, but didn't find any.\n");
344 * If no line-out is defined but multiple HPs are found,
345 * some of them might be the real line-outs.
347 if (!cfg->line_outs && cfg->hp_outs > 1 &&
348 !(cond_flags & HDA_PINCFG_NO_HP_FIXUP)) {
350 while (i < cfg->hp_outs) {
351 /* The real HPs should have the sequence 0x0f */
352 if ((hp_out[i].seq & 0x0f) == 0x0f) {
356 /* Move it to the line-out table */
357 line_out[cfg->line_outs++] = hp_out[i];
359 memmove(hp_out + i, hp_out + i + 1,
360 sizeof(hp_out[0]) * (cfg->hp_outs - i));
362 memset(hp_out + cfg->hp_outs, 0,
363 sizeof(hp_out[0]) * (AUTO_CFG_MAX_OUTS - cfg->hp_outs));
365 cfg->line_out_type = AUTO_PIN_HP_OUT;
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,
373 sort_pins_by_sequence(cfg->hp_pins, hp_out, cfg->hp_outs);
376 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
377 * as a primary output
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));
393 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
394 cfg->line_out_type = AUTO_PIN_HP_OUT;
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);
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);
407 * debug prints of the parsed results
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);
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),
435 codec_info(codec, " dig-in=0x%x\n", cfg->dig_in_pin);
439 EXPORT_SYMBOL_GPL(snd_hda_parse_pin_defcfg);
442 * snd_hda_get_input_pin_attr - Get the input pin attribute from pin config
443 * @def_conf: pin configuration value
445 * Guess the input pin attribute (INPUT_PIN_ATTR_XXX) from the given
446 * default pin configuration value.
448 int snd_hda_get_input_pin_attr(unsigned int def_conf)
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;
467 EXPORT_SYMBOL_GPL(snd_hda_get_input_pin_attr);
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
474 * @check_location: flag to add the jack location prefix
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".
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)
484 unsigned int def_conf;
485 static const char * const mic_names[] = {
486 "Internal Mic", "Dock Mic", "Mic", "Rear Mic", "Front Mic"
490 def_conf = snd_hda_codec_get_pincfg(codec, pin);
492 switch (get_defcfg_device(def_conf)) {
494 if (item && item->is_headset_mic)
495 return "Headset Mic";
496 if (item && item->is_headphone_mic)
497 return "Headphone Mic";
500 attr = snd_hda_get_input_pin_attr(def_conf);
503 return mic_names[attr - 1];
504 case AC_JACK_LINE_IN:
507 attr = snd_hda_get_input_pin_attr(def_conf);
510 if (attr == INPUT_PIN_ATTR_DOCK)
517 case AC_JACK_SPDIF_IN:
519 case AC_JACK_DIG_OTHER_IN:
522 return "Headphone Mic";
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.
532 static int check_mic_location_need(struct hda_codec *codec,
533 const struct auto_pin_cfg *cfg,
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)
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 */
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
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
569 const char *hda_get_autocfg_input_label(struct hda_codec *codec,
570 const struct auto_pin_cfg *cfg,
573 int type = cfg->inputs[input].type;
574 int has_multiple_pins = 0;
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,
586 EXPORT_SYMBOL_GPL(hda_get_autocfg_input_label);
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)
592 for (i = 0; i < nums; i++)
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)
602 static const char * const channel_sfx[] = {
603 " Front", " Surround", " CLFE", " Side"
607 i = find_idx_in_nid_list(nid, pins, num_pins);
612 if (num_pins > ARRAY_SIZE(channel_sfx)) {
617 return channel_sfx[i];
620 static const char *check_output_pfx(struct hda_codec *codec, hda_nid_t nid)
622 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
623 int attr = snd_hda_get_input_pin_attr(def_conf);
625 /* check the location */
627 case INPUT_PIN_ATTR_DOCK:
629 case INPUT_PIN_ATTR_FRONT:
635 static int get_hp_label_index(struct hda_codec *codec, hda_nid_t nid,
636 const hda_nid_t *pins, int num_pins)
640 const char *pfx = check_output_pfx(codec, nid);
642 i = find_idx_in_nid_list(nid, pins, num_pins);
645 for (j = 0; j < i; j++)
646 if (pfx == check_output_pfx(codec, pins[j]))
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,
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 = "";
661 /* handle as a speaker if it's a fixed line-out */
662 if (!strcmp(name, "Line Out") && attr == INPUT_PIN_ATTR_INT)
664 pfx = check_output_pfx(codec, nid);
667 /* try to give a unique suffix if needed */
668 sfx = check_output_sfx(nid, cfg->line_out_pins, cfg->line_outs,
671 sfx = check_output_sfx(nid, cfg->speaker_pins, cfg->speaker_outs,
674 /* don't add channel suffix for Headphone controls */
675 int idx = get_hp_label_index(codec, nid, cfg->hp_pins,
677 if (idx >= 0 && indexp)
682 snprintf(label, maxlen, "%s%s%s", pfx, name, sfx);
686 #define is_hdmi_cfg(conf) \
687 (get_defcfg_location(conf) == AC_JACK_LOC_HDMI)
690 * snd_hda_get_pin_label - Get a label for the given I/O pin
691 * @codec: the HDA codec
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)
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().
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.
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)
712 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
713 const char *name = NULL;
719 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
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);
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";
737 for (i = 0; i < cfg->dig_outs; i++) {
738 hda_nid_t pin = cfg->dig_out_pins[i];
742 c = snd_hda_codec_get_pincfg(codec, pin);
743 if (hdmi == is_hdmi_cfg(c))
749 for (i = 0; i < cfg->num_inputs; i++) {
750 if (cfg->inputs[i].pin != nid)
752 name = hda_get_autocfg_input_label(codec, cfg, i);
758 name = hda_get_input_pin_label(codec, NULL, nid, true);
763 strscpy(label, name, maxlen);
766 EXPORT_SYMBOL_GPL(snd_hda_get_pin_label);
769 * snd_hda_add_verbs - Add verbs to the init list
770 * @codec: the HDA codec
771 * @list: zero-terminated verb list to add
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().
776 int snd_hda_add_verbs(struct hda_codec *codec,
777 const struct hda_verb *list)
779 const struct hda_verb **v;
780 v = snd_array_new(&codec->verbs);
786 EXPORT_SYMBOL_GPL(snd_hda_add_verbs);
789 * snd_hda_apply_verbs - Execute the init verb lists
790 * @codec: the HDA codec
792 void snd_hda_apply_verbs(struct hda_codec *codec)
794 const struct hda_verb **v;
797 snd_array_for_each(&codec->verbs, i, v)
798 snd_hda_sequence_write(codec, *v);
800 EXPORT_SYMBOL_GPL(snd_hda_apply_verbs);
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
807 void snd_hda_apply_pincfgs(struct hda_codec *codec,
808 const struct hda_pintbl *cfg)
810 for (; cfg->nid; cfg++)
811 snd_hda_codec_set_pincfg(codec, cfg->nid, cfg->val);
813 EXPORT_SYMBOL_GPL(snd_hda_apply_pincfgs);
815 static void set_pin_targets(struct hda_codec *codec,
816 const struct hda_pintbl *cfg)
818 for (; cfg->nid; cfg++)
819 snd_hda_set_pin_ctl_cache(codec, cfg->nid, cfg->val);
822 void __snd_hda_apply_fixup(struct hda_codec *codec, int id, int action, int depth)
824 const char *modelname = codec->fixup_name;
827 const struct hda_fixup *fix = codec->fixup_list + id;
831 if (fix->chained_before)
832 __snd_hda_apply_fixup(codec, fix->chain_id, action, depth + 1);
836 if (action != HDA_FIXUP_ACT_PRE_PROBE || !fix->v.pins)
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);
842 case HDA_FIXUP_VERBS:
843 if (action != HDA_FIXUP_ACT_PROBE || !fix->v.verbs)
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);
852 codec_dbg(codec, "%s: Apply fix-func for %s\n",
853 codec->core.chip_name, modelname);
854 fix->v.func(codec, fix, action);
856 case HDA_FIXUP_PINCTLS:
857 if (action != HDA_FIXUP_ACT_PROBE || !fix->v.pins)
859 codec_dbg(codec, "%s: Apply pinctl for %s\n",
860 codec->core.chip_name, modelname);
861 set_pin_targets(codec, fix->v.pins);
864 codec_err(codec, "%s: Invalid fixup type %d\n",
865 codec->core.chip_name, fix->type);
868 if (!fix->chained || fix->chained_before)
873 EXPORT_SYMBOL_GPL(__snd_hda_apply_fixup);
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)
880 void snd_hda_apply_fixup(struct hda_codec *codec, int action)
882 if (codec->fixup_list)
883 __snd_hda_apply_fixup(codec, codec->fixup_id, action, 0);
885 EXPORT_SYMBOL_GPL(snd_hda_apply_fixup);
887 #define IGNORE_SEQ_ASSOC (~(AC_DEFCFG_SEQUENCE | AC_DEFCFG_DEF_ASSOC))
889 static bool pin_config_match(struct hda_codec *codec,
890 const struct hda_pintbl *pins,
893 const struct hda_pincfg *pin;
896 snd_array_for_each(&codec->init_pins, i, pin) {
897 hda_nid_t nid = pin->nid;
899 const struct hda_pintbl *t_pins;
904 for (; t_pins->nid; t_pins++) {
905 if (t_pins->nid == nid) {
907 if ((t_pins->val & IGNORE_SEQ_ASSOC) == (cfg & IGNORE_SEQ_ASSOC))
909 else if ((cfg & 0xf0000000) == 0x40000000 && (t_pins->val & 0xf0000000) == 0x40000000)
915 if (match_all_pins &&
916 !found && (cfg & 0xf0000000) != 0x40000000)
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
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,
935 const struct snd_hda_pin_quirk *pq;
937 if (codec->fixup_id != HDA_FIXUP_ID_NOT_SET)
940 for (pq = pin_quirk; pq->subvendor; pq++) {
941 if ((codec->core.subsystem_id & 0xffff0000) != (pq->subvendor << 16))
943 if (codec->core.vendor_id != pq->codec)
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);
952 codec->fixup_list = fixlist;
957 EXPORT_SYMBOL_GPL(snd_hda_pick_pin_fixup);
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
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.
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
973 * If nothing matched, try to look up the PCI SSID.
974 * If still nothing matched, try to look up the codec SSID.
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)
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;
987 if (codec->fixup_id != HDA_FIXUP_ID_NOT_SET)
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;
994 codec_dbg(codec, "%s: picked no fixup (nofixup specified)\n",
995 codec->core.chip_name);
999 /* match with the model name string */
1000 if (codec->modelname && models) {
1001 while (models->name) {
1002 if (!strcmp(codec->modelname, models->name)) {
1004 name = models->name;
1005 codec_dbg(codec, "%s: picked fixup %s (model specified)\n",
1006 codec->core.chip_name, codec->fixup_name);
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);
1021 type = "alias SSID";
1026 /* match with the PCI SSID */
1027 q = snd_pci_quirk_lookup(codec->bus->pci, quirk);
1033 /* match with the codec SSID */
1034 q = snd_pci_quirk_lookup_id(codec->core.subsystem_id >> 16,
1035 codec->core.subsystem_id & 0xffff,
1038 type = "codec SSID";
1042 return; /* no matching */
1046 #ifdef CONFIG_SND_DEBUG_VERBOSE
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);
1053 codec->fixup_id = id;
1054 codec->fixup_list = fixlist;
1055 codec->fixup_name = name;
1057 EXPORT_SYMBOL_GPL(snd_hda_pick_fixup);