2 * BIOS auto-parser helper functions for HD-audio
4 * Copyright (c) 2012 Takashi Iwai <tiwai@suse.de>
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.
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"
21 * Helper for automatic pin configuration
24 static int is_in_nid_list(hda_nid_t nid, const hda_nid_t *list)
32 /* a pair of input pin and its sequence */
38 static int compare_seq(const void *ap, const void *bp)
40 const struct auto_out_pin *a = ap;
41 const struct auto_out_pin *b = bp;
42 return (int)(a->seq - b->seq);
46 * Sort an associated group of pins according to their sequence numbers.
47 * then store it to a pin array.
49 static void sort_pins_by_sequence(hda_nid_t *pins, struct auto_out_pin *list,
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;
59 /* add the found input-pin to the cfg->inputs[] table */
60 static void add_auto_cfg_input_pin(struct hda_codec *codec, struct auto_pin_cfg *cfg,
61 hda_nid_t nid, int type)
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);
72 static int compare_input_type(const void *ap, const void *bp)
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);
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)
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);
90 /* Reorder the surround channels
91 * ALSA sequence is front/surr/clfe/side
93 * 4-ch: front/surr => OK as it is
94 * 6-ch: front/clfe/surr
95 * 8-ch: front/clfe/rear/side|fc
97 static void reorder_outputs(unsigned int nums, hda_nid_t *pins)
111 /* check whether the given pin has a proper pin I/O capability bit */
112 static bool check_pincap_validity(struct hda_codec *codec, hda_nid_t pin,
115 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
117 /* some old hardware don't return the proper pincaps */
122 case AC_JACK_LINE_OUT:
123 case AC_JACK_SPEAKER:
125 case AC_JACK_SPDIF_OUT:
126 case AC_JACK_DIG_OTHER_OUT:
127 return !!(pincap & AC_PINCAP_OUT);
129 return !!(pincap & AC_PINCAP_IN);
133 static bool can_be_headset_mic(struct hda_codec *codec,
134 struct auto_pin_cfg_item *item,
138 unsigned int def_conf;
139 if (item->type != AUTO_PIN_MIC)
142 if (item->is_headset_mic || item->is_headphone_mic)
143 return false; /* Already assigned */
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)
150 if (seq_number >= 0) {
151 int seq = get_defcfg_sequence(def_conf);
152 if (seq != seq_number)
160 * Parse all pin widgets and store the useful pin nids to cfg
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, ...
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.
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,
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)
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)];
188 if (!snd_hda_get_int_hint(codec, "parser_flags", &i))
191 memset(cfg, 0, sizeof(*cfg));
193 memset(line_out, 0, sizeof(line_out));
194 memset(speaker_out, 0, sizeof(speaker_out));
195 memset(hp_out, 0, sizeof(hp_out));
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;
204 /* read all default configuration for pin complex */
205 if (wid_type != AC_WID_PIN)
207 /* ignore the given nids (e.g. pc-beep returns error) */
208 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
211 def_conf = snd_hda_codec_get_pincfg(codec, nid);
212 conn = get_defcfg_connect(def_conf);
213 if (conn == AC_JACK_PORT_NONE)
215 loc = get_defcfg_location(def_conf);
216 dev = get_defcfg_device(def_conf);
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;
225 if (!check_pincap_validity(codec, nid, dev))
229 case AC_JACK_LINE_OUT:
230 seq = get_defcfg_sequence(def_conf);
231 assoc = get_defcfg_association(def_conf);
233 if (!(wid_caps & AC_WCAP_STEREO))
234 if (!cfg->mono_out_pin)
235 cfg->mono_out_pin = nid;
239 assoc_line_out = assoc;
240 else if (assoc_line_out != assoc) {
242 "ignore pin 0x%x with mismatching assoc# 0x%x vs 0x%x\n",
243 nid, assoc, assoc_line_out);
246 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins)) {
248 "ignore pin 0x%x, too many assigned pins\n",
252 line_out[cfg->line_outs].pin = nid;
253 line_out[cfg->line_outs].seq = seq;
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)) {
261 "ignore pin 0x%x, too many assigned pins\n",
265 speaker_out[cfg->speaker_outs].pin = nid;
266 speaker_out[cfg->speaker_outs].seq = (assoc << 4) | seq;
270 seq = get_defcfg_sequence(def_conf);
271 assoc = get_defcfg_association(def_conf);
272 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins)) {
274 "ignore pin 0x%x, too many assigned pins\n",
278 hp_out[cfg->hp_outs].pin = nid;
279 hp_out[cfg->hp_outs].seq = (assoc << 4) | seq;
283 add_auto_cfg_input_pin(codec, cfg, nid, AUTO_PIN_MIC);
285 case AC_JACK_LINE_IN:
286 add_auto_cfg_input_pin(codec, cfg, nid, AUTO_PIN_LINE_IN);
289 add_auto_cfg_input_pin(codec, cfg, nid, AUTO_PIN_CD);
292 add_auto_cfg_input_pin(codec, cfg, nid, AUTO_PIN_AUX);
294 case AC_JACK_SPDIF_OUT:
295 case AC_JACK_DIG_OTHER_OUT:
296 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins)) {
298 "ignore pin 0x%x, too many assigned pins\n",
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;
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;
314 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
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;
327 } else if (hpmic && can_be_headset_mic(codec, &cfg->inputs[i], 0xd)) {
328 cfg->inputs[i].is_headphone_mic = 1;
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))
337 cfg->inputs[i].is_headset_mic = 1;
340 cfg->inputs[i].is_headphone_mic = 1;
346 codec_dbg(codec, "Told to look for a headset mic, but didn't find any.\n");
348 codec_dbg(codec, "Told to look for a headphone mic, but didn't find any.\n");
352 * If no line-out is defined but multiple HPs are found,
353 * some of them might be the real line-outs.
355 if (!cfg->line_outs && cfg->hp_outs > 1 &&
356 !(cond_flags & HDA_PINCFG_NO_HP_FIXUP)) {
358 while (i < cfg->hp_outs) {
359 /* The real HPs should have the sequence 0x0f */
360 if ((hp_out[i].seq & 0x0f) == 0x0f) {
364 /* Move it to the line-out table */
365 line_out[cfg->line_outs++] = hp_out[i];
367 memmove(hp_out + i, hp_out + i + 1,
368 sizeof(hp_out[0]) * (cfg->hp_outs - i));
370 memset(hp_out + cfg->hp_outs, 0,
371 sizeof(hp_out[0]) * (AUTO_CFG_MAX_OUTS - cfg->hp_outs));
373 cfg->line_out_type = AUTO_PIN_HP_OUT;
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,
381 sort_pins_by_sequence(cfg->hp_pins, hp_out, cfg->hp_outs);
384 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
385 * as a primary output
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));
401 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
402 cfg->line_out_type = AUTO_PIN_HP_OUT;
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);
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);
415 * debug prints of the parsed results
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);
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),
443 codec_info(codec, " dig-in=0x%x\n", cfg->dig_in_pin);
447 EXPORT_SYMBOL_GPL(snd_hda_parse_pin_defcfg);
450 * snd_hda_get_input_pin_attr - Get the input pin attribute from pin config
451 * @def_conf: pin configuration value
453 * Guess the input pin attribute (INPUT_PIN_ATTR_XXX) from the given
454 * default pin configuration value.
456 int snd_hda_get_input_pin_attr(unsigned int def_conf)
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;
475 EXPORT_SYMBOL_GPL(snd_hda_get_input_pin_attr);
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
482 * @check_location: flag to add the jack location prefix
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".
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)
492 unsigned int def_conf;
493 static const char * const mic_names[] = {
494 "Internal Mic", "Dock Mic", "Mic", "Rear Mic", "Front Mic"
498 def_conf = snd_hda_codec_get_pincfg(codec, pin);
500 switch (get_defcfg_device(def_conf)) {
502 if (item && item->is_headset_mic)
503 return "Headset Mic";
504 if (item && item->is_headphone_mic)
505 return "Headphone Mic";
508 attr = snd_hda_get_input_pin_attr(def_conf);
511 return mic_names[attr - 1];
512 case AC_JACK_LINE_IN:
515 attr = snd_hda_get_input_pin_attr(def_conf);
518 if (attr == INPUT_PIN_ATTR_DOCK)
525 case AC_JACK_SPDIF_IN:
527 case AC_JACK_DIG_OTHER_IN:
530 return "Headphone Mic";
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.
540 static int check_mic_location_need(struct hda_codec *codec,
541 const struct auto_pin_cfg *cfg,
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)
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 */
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
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
577 const char *hda_get_autocfg_input_label(struct hda_codec *codec,
578 const struct auto_pin_cfg *cfg,
581 int type = cfg->inputs[input].type;
582 int has_multiple_pins = 0;
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 return hda_get_input_pin_label(codec, &cfg->inputs[input],
590 cfg->inputs[input].pin,
593 EXPORT_SYMBOL_GPL(hda_get_autocfg_input_label);
595 /* return the position of NID in the list, or -1 if not found */
596 static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
599 for (i = 0; i < nums; i++)
605 /* get a unique suffix or an index number */
606 static const char *check_output_sfx(hda_nid_t nid, const hda_nid_t *pins,
607 int num_pins, int *indexp)
609 static const char * const channel_sfx[] = {
610 " Front", " Surround", " CLFE", " Side"
614 i = find_idx_in_nid_list(nid, pins, num_pins);
619 if (num_pins > ARRAY_SIZE(channel_sfx)) {
624 return channel_sfx[i];
627 static const char *check_output_pfx(struct hda_codec *codec, hda_nid_t nid)
629 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
630 int attr = snd_hda_get_input_pin_attr(def_conf);
632 /* check the location */
634 case INPUT_PIN_ATTR_DOCK:
636 case INPUT_PIN_ATTR_FRONT:
642 static int get_hp_label_index(struct hda_codec *codec, hda_nid_t nid,
643 const hda_nid_t *pins, int num_pins)
647 const char *pfx = check_output_pfx(codec, nid);
649 i = find_idx_in_nid_list(nid, pins, num_pins);
652 for (j = 0; j < i; j++)
653 if (pfx == check_output_pfx(codec, pins[j]))
659 static int fill_audio_out_name(struct hda_codec *codec, hda_nid_t nid,
660 const struct auto_pin_cfg *cfg,
661 const char *name, char *label, int maxlen,
664 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
665 int attr = snd_hda_get_input_pin_attr(def_conf);
666 const char *pfx, *sfx = "";
668 /* handle as a speaker if it's a fixed line-out */
669 if (!strcmp(name, "Line Out") && attr == INPUT_PIN_ATTR_INT)
671 pfx = check_output_pfx(codec, nid);
674 /* try to give a unique suffix if needed */
675 sfx = check_output_sfx(nid, cfg->line_out_pins, cfg->line_outs,
678 sfx = check_output_sfx(nid, cfg->speaker_pins, cfg->speaker_outs,
681 /* don't add channel suffix for Headphone controls */
682 int idx = get_hp_label_index(codec, nid, cfg->hp_pins,
684 if (idx >= 0 && indexp)
689 snprintf(label, maxlen, "%s%s%s", pfx, name, sfx);
693 #define is_hdmi_cfg(conf) \
694 (get_defcfg_location(conf) == AC_JACK_LOC_HDMI)
697 * snd_hda_get_pin_label - Get a label for the given I/O pin
698 * @codec: the HDA codec
700 * @cfg: the parsed pin configuration
701 * @label: the string buffer to store
702 * @maxlen: the max length of string buffer (including termination)
703 * @indexp: the pointer to return the index number (for multiple ctls)
705 * Get a label for the given pin. This function works for both input and
706 * output pins. When @cfg is given as non-NULL, the function tries to get
707 * an optimized label using hda_get_autocfg_input_label().
709 * This function tries to give a unique label string for the pin as much as
710 * possible. For example, when the multiple line-outs are present, it adds
711 * the channel suffix like "Front", "Surround", etc (only when @cfg is given).
712 * If no unique name with a suffix is available and @indexp is non-NULL, the
713 * index number is stored in the pointer.
715 int snd_hda_get_pin_label(struct hda_codec *codec, hda_nid_t nid,
716 const struct auto_pin_cfg *cfg,
717 char *label, int maxlen, int *indexp)
719 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
720 const char *name = NULL;
726 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
729 switch (get_defcfg_device(def_conf)) {
730 case AC_JACK_LINE_OUT:
731 return fill_audio_out_name(codec, nid, cfg, "Line Out",
732 label, maxlen, indexp);
733 case AC_JACK_SPEAKER:
734 return fill_audio_out_name(codec, nid, cfg, "Speaker",
735 label, maxlen, indexp);
737 return fill_audio_out_name(codec, nid, cfg, "Headphone",
738 label, maxlen, indexp);
739 case AC_JACK_SPDIF_OUT:
740 case AC_JACK_DIG_OTHER_OUT:
741 hdmi = is_hdmi_cfg(def_conf);
742 name = hdmi ? "HDMI" : "SPDIF";
744 for (i = 0; i < cfg->dig_outs; i++) {
745 hda_nid_t pin = cfg->dig_out_pins[i];
749 c = snd_hda_codec_get_pincfg(codec, pin);
750 if (hdmi == is_hdmi_cfg(c))
756 for (i = 0; i < cfg->num_inputs; i++) {
757 if (cfg->inputs[i].pin != nid)
759 name = hda_get_autocfg_input_label(codec, cfg, i);
765 name = hda_get_input_pin_label(codec, NULL, nid, true);
770 strlcpy(label, name, maxlen);
773 EXPORT_SYMBOL_GPL(snd_hda_get_pin_label);
776 * snd_hda_add_verbs - Add verbs to the init list
777 * @codec: the HDA codec
778 * @list: zero-terminated verb list to add
780 * Append the given verb list to the execution list. The verbs will be
781 * performed at init and resume time via snd_hda_apply_verbs().
783 int snd_hda_add_verbs(struct hda_codec *codec,
784 const struct hda_verb *list)
786 const struct hda_verb **v;
787 v = snd_array_new(&codec->verbs);
793 EXPORT_SYMBOL_GPL(snd_hda_add_verbs);
796 * snd_hda_apply_verbs - Execute the init verb lists
797 * @codec: the HDA codec
799 void snd_hda_apply_verbs(struct hda_codec *codec)
802 for (i = 0; i < codec->verbs.used; i++) {
803 struct hda_verb **v = snd_array_elem(&codec->verbs, i);
804 snd_hda_sequence_write(codec, *v);
807 EXPORT_SYMBOL_GPL(snd_hda_apply_verbs);
810 * snd_hda_apply_pincfgs - Set each pin config in the given list
811 * @codec: the HDA codec
812 * @cfg: NULL-terminated pin config table
814 void snd_hda_apply_pincfgs(struct hda_codec *codec,
815 const struct hda_pintbl *cfg)
817 for (; cfg->nid; cfg++)
818 snd_hda_codec_set_pincfg(codec, cfg->nid, cfg->val);
820 EXPORT_SYMBOL_GPL(snd_hda_apply_pincfgs);
822 static void set_pin_targets(struct hda_codec *codec,
823 const struct hda_pintbl *cfg)
825 for (; cfg->nid; cfg++)
826 snd_hda_set_pin_ctl_cache(codec, cfg->nid, cfg->val);
829 static void apply_fixup(struct hda_codec *codec, int id, int action, int depth)
831 const char *modelname = codec->fixup_name;
834 const struct hda_fixup *fix = codec->fixup_list + id;
838 if (fix->chained_before)
839 apply_fixup(codec, fix->chain_id, action, depth + 1);
843 if (action != HDA_FIXUP_ACT_PRE_PROBE || !fix->v.pins)
845 codec_dbg(codec, "%s: Apply pincfg for %s\n",
846 codec->core.chip_name, modelname);
847 snd_hda_apply_pincfgs(codec, fix->v.pins);
849 case HDA_FIXUP_VERBS:
850 if (action != HDA_FIXUP_ACT_PROBE || !fix->v.verbs)
852 codec_dbg(codec, "%s: Apply fix-verbs for %s\n",
853 codec->core.chip_name, modelname);
854 snd_hda_add_verbs(codec, fix->v.verbs);
859 codec_dbg(codec, "%s: Apply fix-func for %s\n",
860 codec->core.chip_name, modelname);
861 fix->v.func(codec, fix, action);
863 case HDA_FIXUP_PINCTLS:
864 if (action != HDA_FIXUP_ACT_PROBE || !fix->v.pins)
866 codec_dbg(codec, "%s: Apply pinctl for %s\n",
867 codec->core.chip_name, modelname);
868 set_pin_targets(codec, fix->v.pins);
871 codec_err(codec, "%s: Invalid fixup type %d\n",
872 codec->core.chip_name, fix->type);
875 if (!fix->chained || fix->chained_before)
882 * snd_hda_apply_fixup - Apply the fixup chain with the given action
883 * @codec: the HDA codec
884 * @action: fixup action (HDA_FIXUP_ACT_XXX)
886 void snd_hda_apply_fixup(struct hda_codec *codec, int action)
888 if (codec->fixup_list)
889 apply_fixup(codec, codec->fixup_id, action, 0);
891 EXPORT_SYMBOL_GPL(snd_hda_apply_fixup);
893 #define IGNORE_SEQ_ASSOC (~(AC_DEFCFG_SEQUENCE | AC_DEFCFG_DEF_ASSOC))
895 static bool pin_config_match(struct hda_codec *codec,
896 const struct hda_pintbl *pins)
900 for (i = 0; i < codec->init_pins.used; i++) {
901 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
902 hda_nid_t nid = pin->nid;
904 const struct hda_pintbl *t_pins;
909 for (; t_pins->nid; t_pins++) {
910 if (t_pins->nid == nid) {
912 if ((t_pins->val & IGNORE_SEQ_ASSOC) == (cfg & IGNORE_SEQ_ASSOC))
914 else if ((cfg & 0xf0000000) == 0x40000000 && (t_pins->val & 0xf0000000) == 0x40000000)
920 if (!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
933 void snd_hda_pick_pin_fixup(struct hda_codec *codec,
934 const struct snd_hda_pin_quirk *pin_quirk,
935 const struct hda_fixup *fixlist)
937 const struct snd_hda_pin_quirk *pq;
939 if (codec->fixup_id != HDA_FIXUP_ID_NOT_SET)
942 for (pq = pin_quirk; pq->subvendor; pq++) {
943 if ((codec->core.subsystem_id & 0xffff0000) != (pq->subvendor << 16))
945 if (codec->core.vendor_id != pq->codec)
947 if (pin_config_match(codec, pq->pins)) {
948 codec->fixup_id = pq->value;
949 #ifdef CONFIG_SND_DEBUG_VERBOSE
950 codec->fixup_name = pq->name;
951 codec_dbg(codec, "%s: picked fixup %s (pin match)\n",
952 codec->core.chip_name, codec->fixup_name);
954 codec->fixup_list = fixlist;
959 EXPORT_SYMBOL_GPL(snd_hda_pick_pin_fixup);
962 * snd_hda_pick_fixup - Pick up a fixup matching with PCI/codec SSID or model string
963 * @codec: the HDA codec
964 * @models: NULL-terminated model string list
965 * @quirk: zero-terminated PCI/codec SSID quirk list
966 * @fixlist: the fixup list
968 * Pick up a fixup entry matching with the given model string or SSID.
969 * If a fixup was already set beforehand, the function doesn't do anything.
970 * When a special model string "nofixup" is given, also no fixup is applied.
972 * The function tries to find the matching model name at first, if 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;
985 if (codec->fixup_id != HDA_FIXUP_ID_NOT_SET)
988 /* when model=nofixup is given, don't pick up any fixups */
989 if (codec->modelname && !strcmp(codec->modelname, "nofixup")) {
990 codec->fixup_list = NULL;
991 codec->fixup_name = NULL;
992 codec->fixup_id = HDA_FIXUP_ID_NO_FIXUP;
993 codec_dbg(codec, "%s: picked no fixup (nofixup specified)\n",
994 codec->core.chip_name);
998 if (codec->modelname && models) {
999 while (models->name) {
1000 if (!strcmp(codec->modelname, models->name)) {
1001 codec->fixup_id = models->id;
1002 codec->fixup_name = models->name;
1003 codec->fixup_list = fixlist;
1004 codec_dbg(codec, "%s: picked fixup %s (model specified)\n",
1005 codec->core.chip_name, codec->fixup_name);
1012 q = snd_pci_quirk_lookup(codec->bus->pci, quirk);
1015 #ifdef CONFIG_SND_DEBUG_VERBOSE
1017 codec_dbg(codec, "%s: picked fixup %s (PCI SSID%s)\n",
1018 codec->core.chip_name, name, q->subdevice_mask ? "" : " - vendor generic");
1022 if (id < 0 && quirk) {
1023 for (q = quirk; q->subvendor || q->subdevice; q++) {
1024 unsigned int vendorid =
1025 q->subdevice | (q->subvendor << 16);
1026 unsigned int mask = 0xffff0000 | q->subdevice_mask;
1027 if ((codec->core.subsystem_id & mask) == (vendorid & mask)) {
1029 #ifdef CONFIG_SND_DEBUG_VERBOSE
1031 codec_dbg(codec, "%s: picked fixup %s (codec SSID)\n",
1032 codec->core.chip_name, name);
1039 codec->fixup_id = id;
1041 codec->fixup_list = fixlist;
1042 codec->fixup_name = name;
1045 EXPORT_SYMBOL_GPL(snd_hda_pick_fixup);