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 <sound/core.h>
15 #include "hda_codec.h"
16 #include "hda_local.h"
17 #include "hda_auto_parser.h"
19 #define SFX "hda_codec: "
22 * Helper for automatic pin configuration
25 static int is_in_nid_list(hda_nid_t nid, const hda_nid_t *list)
35 * Sort an associated group of pins according to their sequence numbers.
37 static void sort_pins_by_sequence(hda_nid_t *pins, short *sequences,
44 for (i = 0; i < num_pins; i++) {
45 for (j = i + 1; j < num_pins; j++) {
46 if (sequences[i] > sequences[j]) {
48 sequences[i] = sequences[j];
59 /* add the found input-pin to the cfg->inputs[] table */
60 static void add_auto_cfg_input_pin(struct auto_pin_cfg *cfg, hda_nid_t nid,
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;
70 /* sort inputs in the order of AUTO_PIN_* type */
71 static void sort_autocfg_input_pins(struct auto_pin_cfg *cfg)
75 for (i = 0; i < cfg->num_inputs; i++) {
76 for (j = i + 1; j < cfg->num_inputs; j++) {
77 if (cfg->inputs[i].type > cfg->inputs[j].type) {
78 struct auto_pin_cfg_item tmp;
80 cfg->inputs[i] = cfg->inputs[j];
87 /* Reorder the surround channels
88 * ALSA sequence is front/surr/clfe/side
90 * 4-ch: front/surr => OK as it is
91 * 6-ch: front/clfe/surr
92 * 8-ch: front/clfe/rear/side|fc
94 static void reorder_outputs(unsigned int nums, hda_nid_t *pins)
109 * Parse all pin widgets and store the useful pin nids to cfg
111 * The number of line-outs or any primary output is stored in line_outs,
112 * and the corresponding output pins are assigned to line_out_pins[],
113 * in the order of front, rear, CLFE, side, ...
115 * If more extra outputs (speaker and headphone) are found, the pins are
116 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
117 * is detected, one of speaker of HP pins is assigned as the primary
118 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
119 * if any analog output exists.
121 * The analog input pins are assigned to inputs array.
122 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
125 int snd_hda_parse_pin_defcfg(struct hda_codec *codec,
126 struct auto_pin_cfg *cfg,
127 const hda_nid_t *ignore_nids,
128 unsigned int cond_flags)
130 hda_nid_t nid, end_nid;
131 short seq, assoc_line_out;
132 short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
133 short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
134 short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
137 memset(cfg, 0, sizeof(*cfg));
139 memset(sequences_line_out, 0, sizeof(sequences_line_out));
140 memset(sequences_speaker, 0, sizeof(sequences_speaker));
141 memset(sequences_hp, 0, sizeof(sequences_hp));
144 end_nid = codec->start_nid + codec->num_nodes;
145 for (nid = codec->start_nid; nid < end_nid; nid++) {
146 unsigned int wid_caps = get_wcaps(codec, nid);
147 unsigned int wid_type = get_wcaps_type(wid_caps);
148 unsigned int def_conf;
149 short assoc, loc, conn, dev;
151 /* read all default configuration for pin complex */
152 if (wid_type != AC_WID_PIN)
154 /* ignore the given nids (e.g. pc-beep returns error) */
155 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
158 def_conf = snd_hda_codec_get_pincfg(codec, nid);
159 conn = get_defcfg_connect(def_conf);
160 if (conn == AC_JACK_PORT_NONE)
162 loc = get_defcfg_location(def_conf);
163 dev = get_defcfg_device(def_conf);
165 /* workaround for buggy BIOS setups */
166 if (dev == AC_JACK_LINE_OUT) {
167 if (conn == AC_JACK_PORT_FIXED)
168 dev = AC_JACK_SPEAKER;
172 case AC_JACK_LINE_OUT:
173 seq = get_defcfg_sequence(def_conf);
174 assoc = get_defcfg_association(def_conf);
176 if (!(wid_caps & AC_WCAP_STEREO))
177 if (!cfg->mono_out_pin)
178 cfg->mono_out_pin = nid;
182 assoc_line_out = assoc;
183 else if (assoc_line_out != assoc)
185 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
187 cfg->line_out_pins[cfg->line_outs] = nid;
188 sequences_line_out[cfg->line_outs] = seq;
191 case AC_JACK_SPEAKER:
192 seq = get_defcfg_sequence(def_conf);
193 assoc = get_defcfg_association(def_conf);
194 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
196 cfg->speaker_pins[cfg->speaker_outs] = nid;
197 sequences_speaker[cfg->speaker_outs] = (assoc << 4) | seq;
201 seq = get_defcfg_sequence(def_conf);
202 assoc = get_defcfg_association(def_conf);
203 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
205 cfg->hp_pins[cfg->hp_outs] = nid;
206 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
210 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_MIC);
212 case AC_JACK_LINE_IN:
213 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_LINE_IN);
216 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_CD);
219 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_AUX);
221 case AC_JACK_SPDIF_OUT:
222 case AC_JACK_DIG_OTHER_OUT:
223 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
225 cfg->dig_out_pins[cfg->dig_outs] = nid;
226 cfg->dig_out_type[cfg->dig_outs] =
227 (loc == AC_JACK_LOC_HDMI) ?
228 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
231 case AC_JACK_SPDIF_IN:
232 case AC_JACK_DIG_OTHER_IN:
233 cfg->dig_in_pin = nid;
234 if (loc == AC_JACK_LOC_HDMI)
235 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
237 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
243 * If no line-out is defined but multiple HPs are found,
244 * some of them might be the real line-outs.
246 if (!cfg->line_outs && cfg->hp_outs > 1 &&
247 !(cond_flags & HDA_PINCFG_NO_HP_FIXUP)) {
249 while (i < cfg->hp_outs) {
250 /* The real HPs should have the sequence 0x0f */
251 if ((sequences_hp[i] & 0x0f) == 0x0f) {
255 /* Move it to the line-out table */
256 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
257 sequences_line_out[cfg->line_outs] = sequences_hp[i];
260 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
261 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
262 memmove(sequences_hp + i, sequences_hp + i + 1,
263 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
265 memset(cfg->hp_pins + cfg->hp_outs, 0,
266 sizeof(hda_nid_t) * (AUTO_CFG_MAX_OUTS - cfg->hp_outs));
268 cfg->line_out_type = AUTO_PIN_HP_OUT;
272 /* sort by sequence */
273 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
275 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
277 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
281 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
282 * as a primary output
284 if (!cfg->line_outs &&
285 !(cond_flags & HDA_PINCFG_NO_LO_FIXUP)) {
286 if (cfg->speaker_outs) {
287 cfg->line_outs = cfg->speaker_outs;
288 memcpy(cfg->line_out_pins, cfg->speaker_pins,
289 sizeof(cfg->speaker_pins));
290 cfg->speaker_outs = 0;
291 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
292 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
293 } else if (cfg->hp_outs) {
294 cfg->line_outs = cfg->hp_outs;
295 memcpy(cfg->line_out_pins, cfg->hp_pins,
296 sizeof(cfg->hp_pins));
298 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
299 cfg->line_out_type = AUTO_PIN_HP_OUT;
303 reorder_outputs(cfg->line_outs, cfg->line_out_pins);
304 reorder_outputs(cfg->hp_outs, cfg->hp_pins);
305 reorder_outputs(cfg->speaker_outs, cfg->speaker_pins);
307 sort_autocfg_input_pins(cfg);
310 * debug prints of the parsed results
312 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x) type:%s\n",
313 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
314 cfg->line_out_pins[2], cfg->line_out_pins[3],
315 cfg->line_out_pins[4],
316 cfg->line_out_type == AUTO_PIN_HP_OUT ? "hp" :
317 (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT ?
318 "speaker" : "line"));
319 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
320 cfg->speaker_outs, cfg->speaker_pins[0],
321 cfg->speaker_pins[1], cfg->speaker_pins[2],
322 cfg->speaker_pins[3], cfg->speaker_pins[4]);
323 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
324 cfg->hp_outs, cfg->hp_pins[0],
325 cfg->hp_pins[1], cfg->hp_pins[2],
326 cfg->hp_pins[3], cfg->hp_pins[4]);
327 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
329 snd_printd(" dig-out=0x%x/0x%x\n",
330 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
331 snd_printd(" inputs:\n");
332 for (i = 0; i < cfg->num_inputs; i++) {
333 snd_printd(" %s=0x%x\n",
334 hda_get_autocfg_input_label(codec, cfg, i),
338 snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
342 EXPORT_SYMBOL_HDA(snd_hda_parse_pin_defcfg);
344 int snd_hda_get_input_pin_attr(unsigned int def_conf)
346 unsigned int loc = get_defcfg_location(def_conf);
347 unsigned int conn = get_defcfg_connect(def_conf);
348 if (conn == AC_JACK_PORT_NONE)
349 return INPUT_PIN_ATTR_UNUSED;
350 /* Windows may claim the internal mic to be BOTH, too */
351 if (conn == AC_JACK_PORT_FIXED || conn == AC_JACK_PORT_BOTH)
352 return INPUT_PIN_ATTR_INT;
353 if ((loc & 0x30) == AC_JACK_LOC_INTERNAL)
354 return INPUT_PIN_ATTR_INT;
355 if ((loc & 0x30) == AC_JACK_LOC_SEPARATE)
356 return INPUT_PIN_ATTR_DOCK;
357 if (loc == AC_JACK_LOC_REAR)
358 return INPUT_PIN_ATTR_REAR;
359 if (loc == AC_JACK_LOC_FRONT)
360 return INPUT_PIN_ATTR_FRONT;
361 return INPUT_PIN_ATTR_NORMAL;
363 EXPORT_SYMBOL_HDA(snd_hda_get_input_pin_attr);
366 * hda_get_input_pin_label - Give a label for the given input pin
368 * When check_location is true, the function checks the pin location
369 * for mic and line-in pins, and set an appropriate prefix like "Front",
370 * "Rear", "Internal".
373 static const char *hda_get_input_pin_label(struct hda_codec *codec,
374 hda_nid_t pin, bool check_location)
376 unsigned int def_conf;
377 static const char * const mic_names[] = {
378 "Internal Mic", "Dock Mic", "Mic", "Front Mic", "Rear Mic",
382 def_conf = snd_hda_codec_get_pincfg(codec, pin);
384 switch (get_defcfg_device(def_conf)) {
388 attr = snd_hda_get_input_pin_attr(def_conf);
391 return mic_names[attr - 1];
392 case AC_JACK_LINE_IN:
395 attr = snd_hda_get_input_pin_attr(def_conf);
398 if (attr == INPUT_PIN_ATTR_DOCK)
405 case AC_JACK_SPDIF_IN:
407 case AC_JACK_DIG_OTHER_IN:
414 /* Check whether the location prefix needs to be added to the label.
415 * If all mic-jacks are in the same location (e.g. rear panel), we don't
416 * have to put "Front" prefix to each label. In such a case, returns false.
418 static int check_mic_location_need(struct hda_codec *codec,
419 const struct auto_pin_cfg *cfg,
425 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[input].pin);
426 attr = snd_hda_get_input_pin_attr(defc);
427 /* for internal or docking mics, we need locations */
428 if (attr <= INPUT_PIN_ATTR_NORMAL)
432 for (i = 0; i < cfg->num_inputs; i++) {
433 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[i].pin);
434 attr2 = snd_hda_get_input_pin_attr(defc);
435 if (attr2 >= INPUT_PIN_ATTR_NORMAL) {
436 if (attr && attr != attr2)
437 return 1; /* different locations found */
445 * hda_get_autocfg_input_label - Get a label for the given input
447 * Get a label for the given input pin defined by the autocfg item.
448 * Unlike hda_get_input_pin_label(), this function checks all inputs
449 * defined in autocfg and avoids the redundant mic/line prefix as much as
452 const char *hda_get_autocfg_input_label(struct hda_codec *codec,
453 const struct auto_pin_cfg *cfg,
456 int type = cfg->inputs[input].type;
457 int has_multiple_pins = 0;
459 if ((input > 0 && cfg->inputs[input - 1].type == type) ||
460 (input < cfg->num_inputs - 1 && cfg->inputs[input + 1].type == type))
461 has_multiple_pins = 1;
462 if (has_multiple_pins && type == AUTO_PIN_MIC)
463 has_multiple_pins &= check_mic_location_need(codec, cfg, input);
464 return hda_get_input_pin_label(codec, cfg->inputs[input].pin,
467 EXPORT_SYMBOL_HDA(hda_get_autocfg_input_label);
469 /* return the position of NID in the list, or -1 if not found */
470 static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
473 for (i = 0; i < nums; i++)
479 /* get a unique suffix or an index number */
480 static const char *check_output_sfx(hda_nid_t nid, const hda_nid_t *pins,
481 int num_pins, int *indexp)
483 static const char * const channel_sfx[] = {
484 " Front", " Surround", " CLFE", " Side"
488 i = find_idx_in_nid_list(nid, pins, num_pins);
493 if (num_pins > ARRAY_SIZE(channel_sfx)) {
498 return channel_sfx[i];
501 static const char *check_output_pfx(struct hda_codec *codec, hda_nid_t nid)
503 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
504 int attr = snd_hda_get_input_pin_attr(def_conf);
506 /* check the location */
508 case INPUT_PIN_ATTR_DOCK:
510 case INPUT_PIN_ATTR_FRONT:
516 static int get_hp_label_index(struct hda_codec *codec, hda_nid_t nid,
517 const hda_nid_t *pins, int num_pins)
521 const char *pfx = check_output_pfx(codec, nid);
523 i = find_idx_in_nid_list(nid, pins, num_pins);
526 for (j = 0; j < i; j++)
527 if (pfx == check_output_pfx(codec, pins[j]))
533 static int fill_audio_out_name(struct hda_codec *codec, hda_nid_t nid,
534 const struct auto_pin_cfg *cfg,
535 const char *name, char *label, int maxlen,
538 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
539 int attr = snd_hda_get_input_pin_attr(def_conf);
540 const char *pfx, *sfx = "";
542 /* handle as a speaker if it's a fixed line-out */
543 if (!strcmp(name, "Line Out") && attr == INPUT_PIN_ATTR_INT)
545 pfx = check_output_pfx(codec, nid);
548 /* try to give a unique suffix if needed */
549 sfx = check_output_sfx(nid, cfg->line_out_pins, cfg->line_outs,
552 sfx = check_output_sfx(nid, cfg->speaker_pins, cfg->speaker_outs,
555 /* don't add channel suffix for Headphone controls */
556 int idx = get_hp_label_index(codec, nid, cfg->hp_pins,
563 snprintf(label, maxlen, "%s%s%s", pfx, name, sfx);
568 * snd_hda_get_pin_label - Get a label for the given I/O pin
570 * Get a label for the given pin. This function works for both input and
571 * output pins. When @cfg is given as non-NULL, the function tries to get
572 * an optimized label using hda_get_autocfg_input_label().
574 * This function tries to give a unique label string for the pin as much as
575 * possible. For example, when the multiple line-outs are present, it adds
576 * the channel suffix like "Front", "Surround", etc (only when @cfg is given).
577 * If no unique name with a suffix is available and @indexp is non-NULL, the
578 * index number is stored in the pointer.
580 int snd_hda_get_pin_label(struct hda_codec *codec, hda_nid_t nid,
581 const struct auto_pin_cfg *cfg,
582 char *label, int maxlen, int *indexp)
584 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
585 const char *name = NULL;
590 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
593 switch (get_defcfg_device(def_conf)) {
594 case AC_JACK_LINE_OUT:
595 return fill_audio_out_name(codec, nid, cfg, "Line Out",
596 label, maxlen, indexp);
597 case AC_JACK_SPEAKER:
598 return fill_audio_out_name(codec, nid, cfg, "Speaker",
599 label, maxlen, indexp);
601 return fill_audio_out_name(codec, nid, cfg, "Headphone",
602 label, maxlen, indexp);
603 case AC_JACK_SPDIF_OUT:
604 case AC_JACK_DIG_OTHER_OUT:
605 if (get_defcfg_location(def_conf) == AC_JACK_LOC_HDMI)
610 i = find_idx_in_nid_list(nid, cfg->dig_out_pins,
618 for (i = 0; i < cfg->num_inputs; i++) {
619 if (cfg->inputs[i].pin != nid)
621 name = hda_get_autocfg_input_label(codec, cfg, i);
627 name = hda_get_input_pin_label(codec, nid, true);
632 strlcpy(label, name, maxlen);
635 EXPORT_SYMBOL_HDA(snd_hda_get_pin_label);
637 int snd_hda_gen_add_verbs(struct hda_gen_spec *spec,
638 const struct hda_verb *list)
640 const struct hda_verb **v;
641 v = snd_array_new(&spec->verbs);
647 EXPORT_SYMBOL_HDA(snd_hda_gen_add_verbs);
649 void snd_hda_gen_apply_verbs(struct hda_codec *codec)
651 struct hda_gen_spec *spec = codec->spec;
653 for (i = 0; i < spec->verbs.used; i++) {
654 struct hda_verb **v = snd_array_elem(&spec->verbs, i);
655 snd_hda_sequence_write(codec, *v);
658 EXPORT_SYMBOL_HDA(snd_hda_gen_apply_verbs);
660 void snd_hda_apply_pincfgs(struct hda_codec *codec,
661 const struct hda_pintbl *cfg)
663 for (; cfg->nid; cfg++)
664 snd_hda_codec_set_pincfg(codec, cfg->nid, cfg->val);
666 EXPORT_SYMBOL_HDA(snd_hda_apply_pincfgs);
668 void snd_hda_apply_fixup(struct hda_codec *codec, int action)
670 struct hda_gen_spec *spec = codec->spec;
671 int id = spec->fixup_id;
672 #ifdef CONFIG_SND_DEBUG_VERBOSE
673 const char *modelname = spec->fixup_name;
677 if (!spec->fixup_list)
681 const struct hda_fixup *fix = spec->fixup_list + id;
685 if (action != HDA_FIXUP_ACT_PRE_PROBE || !fix->v.pins)
687 snd_printdd(KERN_INFO SFX
688 "%s: Apply pincfg for %s\n",
689 codec->chip_name, modelname);
690 snd_hda_apply_pincfgs(codec, fix->v.pins);
692 case HDA_FIXUP_VERBS:
693 if (action != HDA_FIXUP_ACT_PROBE || !fix->v.verbs)
695 snd_printdd(KERN_INFO SFX
696 "%s: Apply fix-verbs for %s\n",
697 codec->chip_name, modelname);
698 snd_hda_gen_add_verbs(codec->spec, fix->v.verbs);
703 snd_printdd(KERN_INFO SFX
704 "%s: Apply fix-func for %s\n",
705 codec->chip_name, modelname);
706 fix->v.func(codec, fix, action);
709 snd_printk(KERN_ERR SFX
710 "%s: Invalid fixup type %d\n",
711 codec->chip_name, fix->type);
721 EXPORT_SYMBOL_HDA(snd_hda_apply_fixup);
723 void snd_hda_pick_fixup(struct hda_codec *codec,
724 const struct hda_model_fixup *models,
725 const struct snd_pci_quirk *quirk,
726 const struct hda_fixup *fixlist)
728 struct hda_gen_spec *spec = codec->spec;
729 const struct snd_pci_quirk *q;
731 const char *name = NULL;
733 /* when model=nofixup is given, don't pick up any fixups */
734 if (codec->modelname && !strcmp(codec->modelname, "nofixup")) {
735 spec->fixup_list = NULL;
740 if (codec->modelname && models) {
741 while (models->name) {
742 if (!strcmp(codec->modelname, models->name)) {
750 if (id < 0 && quirk) {
751 q = snd_pci_quirk_lookup(codec->bus->pci, quirk);
754 #ifdef CONFIG_SND_DEBUG_VERBOSE
759 if (id < 0 && quirk) {
760 for (q = quirk; q->subvendor; q++) {
761 unsigned int vendorid =
762 q->subdevice | (q->subvendor << 16);
763 unsigned int mask = 0xffff0000 | q->subdevice_mask;
764 if ((codec->subsystem_id & mask) == (vendorid & mask)) {
766 #ifdef CONFIG_SND_DEBUG_VERBOSE
776 spec->fixup_list = fixlist;
777 spec->fixup_name = name;
780 EXPORT_SYMBOL_HDA(snd_hda_pick_fixup);