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 codec->ignore_misc_bit = true;
145 end_nid = codec->start_nid + codec->num_nodes;
146 for (nid = codec->start_nid; nid < end_nid; nid++) {
147 unsigned int wid_caps = get_wcaps(codec, nid);
148 unsigned int wid_type = get_wcaps_type(wid_caps);
149 unsigned int def_conf;
150 short assoc, loc, conn, dev;
152 /* read all default configuration for pin complex */
153 if (wid_type != AC_WID_PIN)
155 /* ignore the given nids (e.g. pc-beep returns error) */
156 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
159 def_conf = snd_hda_codec_get_pincfg(codec, nid);
160 if (!(get_defcfg_misc(snd_hda_codec_get_pincfg(codec, nid)) &
161 AC_DEFCFG_MISC_NO_PRESENCE))
162 codec->ignore_misc_bit = false;
163 conn = get_defcfg_connect(def_conf);
164 if (conn == AC_JACK_PORT_NONE)
166 loc = get_defcfg_location(def_conf);
167 dev = get_defcfg_device(def_conf);
169 /* workaround for buggy BIOS setups */
170 if (dev == AC_JACK_LINE_OUT) {
171 if (conn == AC_JACK_PORT_FIXED)
172 dev = AC_JACK_SPEAKER;
176 case AC_JACK_LINE_OUT:
177 seq = get_defcfg_sequence(def_conf);
178 assoc = get_defcfg_association(def_conf);
180 if (!(wid_caps & AC_WCAP_STEREO))
181 if (!cfg->mono_out_pin)
182 cfg->mono_out_pin = nid;
186 assoc_line_out = assoc;
187 else if (assoc_line_out != assoc)
189 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
191 cfg->line_out_pins[cfg->line_outs] = nid;
192 sequences_line_out[cfg->line_outs] = seq;
195 case AC_JACK_SPEAKER:
196 seq = get_defcfg_sequence(def_conf);
197 assoc = get_defcfg_association(def_conf);
198 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
200 cfg->speaker_pins[cfg->speaker_outs] = nid;
201 sequences_speaker[cfg->speaker_outs] = (assoc << 4) | seq;
205 seq = get_defcfg_sequence(def_conf);
206 assoc = get_defcfg_association(def_conf);
207 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
209 cfg->hp_pins[cfg->hp_outs] = nid;
210 sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
214 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_MIC);
216 case AC_JACK_LINE_IN:
217 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_LINE_IN);
220 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_CD);
223 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_AUX);
225 case AC_JACK_SPDIF_OUT:
226 case AC_JACK_DIG_OTHER_OUT:
227 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
229 cfg->dig_out_pins[cfg->dig_outs] = nid;
230 cfg->dig_out_type[cfg->dig_outs] =
231 (loc == AC_JACK_LOC_HDMI) ?
232 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
235 case AC_JACK_SPDIF_IN:
236 case AC_JACK_DIG_OTHER_IN:
237 cfg->dig_in_pin = nid;
238 if (loc == AC_JACK_LOC_HDMI)
239 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
241 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
247 * If no line-out is defined but multiple HPs are found,
248 * some of them might be the real line-outs.
250 if (!cfg->line_outs && cfg->hp_outs > 1 &&
251 !(cond_flags & HDA_PINCFG_NO_HP_FIXUP)) {
253 while (i < cfg->hp_outs) {
254 /* The real HPs should have the sequence 0x0f */
255 if ((sequences_hp[i] & 0x0f) == 0x0f) {
259 /* Move it to the line-out table */
260 cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
261 sequences_line_out[cfg->line_outs] = sequences_hp[i];
264 memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
265 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
266 memmove(sequences_hp + i, sequences_hp + i + 1,
267 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
269 memset(cfg->hp_pins + cfg->hp_outs, 0,
270 sizeof(hda_nid_t) * (AUTO_CFG_MAX_OUTS - cfg->hp_outs));
272 cfg->line_out_type = AUTO_PIN_HP_OUT;
276 /* sort by sequence */
277 sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
279 sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
281 sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
285 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
286 * as a primary output
288 if (!cfg->line_outs &&
289 !(cond_flags & HDA_PINCFG_NO_LO_FIXUP)) {
290 if (cfg->speaker_outs) {
291 cfg->line_outs = cfg->speaker_outs;
292 memcpy(cfg->line_out_pins, cfg->speaker_pins,
293 sizeof(cfg->speaker_pins));
294 cfg->speaker_outs = 0;
295 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
296 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
297 } else if (cfg->hp_outs) {
298 cfg->line_outs = cfg->hp_outs;
299 memcpy(cfg->line_out_pins, cfg->hp_pins,
300 sizeof(cfg->hp_pins));
302 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
303 cfg->line_out_type = AUTO_PIN_HP_OUT;
307 reorder_outputs(cfg->line_outs, cfg->line_out_pins);
308 reorder_outputs(cfg->hp_outs, cfg->hp_pins);
309 reorder_outputs(cfg->speaker_outs, cfg->speaker_pins);
311 sort_autocfg_input_pins(cfg);
314 * debug prints of the parsed results
316 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x) type:%s\n",
317 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
318 cfg->line_out_pins[2], cfg->line_out_pins[3],
319 cfg->line_out_pins[4],
320 cfg->line_out_type == AUTO_PIN_HP_OUT ? "hp" :
321 (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT ?
322 "speaker" : "line"));
323 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
324 cfg->speaker_outs, cfg->speaker_pins[0],
325 cfg->speaker_pins[1], cfg->speaker_pins[2],
326 cfg->speaker_pins[3], cfg->speaker_pins[4]);
327 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
328 cfg->hp_outs, cfg->hp_pins[0],
329 cfg->hp_pins[1], cfg->hp_pins[2],
330 cfg->hp_pins[3], cfg->hp_pins[4]);
331 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
333 snd_printd(" dig-out=0x%x/0x%x\n",
334 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
335 snd_printd(" inputs:");
336 for (i = 0; i < cfg->num_inputs; i++) {
337 snd_printd(" %s=0x%x",
338 hda_get_autocfg_input_label(codec, cfg, i),
343 snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
347 EXPORT_SYMBOL_HDA(snd_hda_parse_pin_defcfg);
349 int snd_hda_get_input_pin_attr(unsigned int def_conf)
351 unsigned int loc = get_defcfg_location(def_conf);
352 unsigned int conn = get_defcfg_connect(def_conf);
353 if (conn == AC_JACK_PORT_NONE)
354 return INPUT_PIN_ATTR_UNUSED;
355 /* Windows may claim the internal mic to be BOTH, too */
356 if (conn == AC_JACK_PORT_FIXED || conn == AC_JACK_PORT_BOTH)
357 return INPUT_PIN_ATTR_INT;
358 if ((loc & 0x30) == AC_JACK_LOC_INTERNAL)
359 return INPUT_PIN_ATTR_INT;
360 if ((loc & 0x30) == AC_JACK_LOC_SEPARATE)
361 return INPUT_PIN_ATTR_DOCK;
362 if (loc == AC_JACK_LOC_REAR)
363 return INPUT_PIN_ATTR_REAR;
364 if (loc == AC_JACK_LOC_FRONT)
365 return INPUT_PIN_ATTR_FRONT;
366 return INPUT_PIN_ATTR_NORMAL;
368 EXPORT_SYMBOL_HDA(snd_hda_get_input_pin_attr);
371 * hda_get_input_pin_label - Give a label for the given input pin
373 * When check_location is true, the function checks the pin location
374 * for mic and line-in pins, and set an appropriate prefix like "Front",
375 * "Rear", "Internal".
378 static const char *hda_get_input_pin_label(struct hda_codec *codec,
379 hda_nid_t pin, bool check_location)
381 unsigned int def_conf;
382 static const char * const mic_names[] = {
383 "Internal Mic", "Dock Mic", "Mic", "Front Mic", "Rear Mic",
387 def_conf = snd_hda_codec_get_pincfg(codec, pin);
389 switch (get_defcfg_device(def_conf)) {
393 attr = snd_hda_get_input_pin_attr(def_conf);
396 return mic_names[attr - 1];
397 case AC_JACK_LINE_IN:
400 attr = snd_hda_get_input_pin_attr(def_conf);
403 if (attr == INPUT_PIN_ATTR_DOCK)
410 case AC_JACK_SPDIF_IN:
412 case AC_JACK_DIG_OTHER_IN:
419 /* Check whether the location prefix needs to be added to the label.
420 * If all mic-jacks are in the same location (e.g. rear panel), we don't
421 * have to put "Front" prefix to each label. In such a case, returns false.
423 static int check_mic_location_need(struct hda_codec *codec,
424 const struct auto_pin_cfg *cfg,
430 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[input].pin);
431 attr = snd_hda_get_input_pin_attr(defc);
432 /* for internal or docking mics, we need locations */
433 if (attr <= INPUT_PIN_ATTR_NORMAL)
437 for (i = 0; i < cfg->num_inputs; i++) {
438 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[i].pin);
439 attr2 = snd_hda_get_input_pin_attr(defc);
440 if (attr2 >= INPUT_PIN_ATTR_NORMAL) {
441 if (attr && attr != attr2)
442 return 1; /* different locations found */
450 * hda_get_autocfg_input_label - Get a label for the given input
452 * Get a label for the given input pin defined by the autocfg item.
453 * Unlike hda_get_input_pin_label(), this function checks all inputs
454 * defined in autocfg and avoids the redundant mic/line prefix as much as
457 const char *hda_get_autocfg_input_label(struct hda_codec *codec,
458 const struct auto_pin_cfg *cfg,
461 int type = cfg->inputs[input].type;
462 int has_multiple_pins = 0;
464 if ((input > 0 && cfg->inputs[input - 1].type == type) ||
465 (input < cfg->num_inputs - 1 && cfg->inputs[input + 1].type == type))
466 has_multiple_pins = 1;
467 if (has_multiple_pins && type == AUTO_PIN_MIC)
468 has_multiple_pins &= check_mic_location_need(codec, cfg, input);
469 return hda_get_input_pin_label(codec, cfg->inputs[input].pin,
472 EXPORT_SYMBOL_HDA(hda_get_autocfg_input_label);
474 /* return the position of NID in the list, or -1 if not found */
475 static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
478 for (i = 0; i < nums; i++)
484 /* get a unique suffix or an index number */
485 static const char *check_output_sfx(hda_nid_t nid, const hda_nid_t *pins,
486 int num_pins, int *indexp)
488 static const char * const channel_sfx[] = {
489 " Front", " Surround", " CLFE", " Side"
493 i = find_idx_in_nid_list(nid, pins, num_pins);
498 if (num_pins > ARRAY_SIZE(channel_sfx)) {
503 return channel_sfx[i];
506 static int fill_audio_out_name(struct hda_codec *codec, hda_nid_t nid,
507 const struct auto_pin_cfg *cfg,
508 const char *name, char *label, int maxlen,
511 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
512 int attr = snd_hda_get_input_pin_attr(def_conf);
513 const char *pfx = "", *sfx = "";
515 /* handle as a speaker if it's a fixed line-out */
516 if (!strcmp(name, "Line Out") && attr == INPUT_PIN_ATTR_INT)
518 /* check the location */
520 case INPUT_PIN_ATTR_DOCK:
523 case INPUT_PIN_ATTR_FRONT:
528 /* try to give a unique suffix if needed */
529 sfx = check_output_sfx(nid, cfg->line_out_pins, cfg->line_outs,
532 sfx = check_output_sfx(nid, cfg->speaker_pins, cfg->speaker_outs,
535 /* don't add channel suffix for Headphone controls */
536 int idx = find_idx_in_nid_list(nid, cfg->hp_pins,
543 snprintf(label, maxlen, "%s%s%s", pfx, name, sfx);
548 * snd_hda_get_pin_label - Get a label for the given I/O pin
550 * Get a label for the given pin. This function works for both input and
551 * output pins. When @cfg is given as non-NULL, the function tries to get
552 * an optimized label using hda_get_autocfg_input_label().
554 * This function tries to give a unique label string for the pin as much as
555 * possible. For example, when the multiple line-outs are present, it adds
556 * the channel suffix like "Front", "Surround", etc (only when @cfg is given).
557 * If no unique name with a suffix is available and @indexp is non-NULL, the
558 * index number is stored in the pointer.
560 int snd_hda_get_pin_label(struct hda_codec *codec, hda_nid_t nid,
561 const struct auto_pin_cfg *cfg,
562 char *label, int maxlen, int *indexp)
564 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
565 const char *name = NULL;
570 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
573 switch (get_defcfg_device(def_conf)) {
574 case AC_JACK_LINE_OUT:
575 return fill_audio_out_name(codec, nid, cfg, "Line Out",
576 label, maxlen, indexp);
577 case AC_JACK_SPEAKER:
578 return fill_audio_out_name(codec, nid, cfg, "Speaker",
579 label, maxlen, indexp);
581 return fill_audio_out_name(codec, nid, cfg, "Headphone",
582 label, maxlen, indexp);
583 case AC_JACK_SPDIF_OUT:
584 case AC_JACK_DIG_OTHER_OUT:
585 if (get_defcfg_location(def_conf) == AC_JACK_LOC_HDMI)
590 i = find_idx_in_nid_list(nid, cfg->dig_out_pins,
598 for (i = 0; i < cfg->num_inputs; i++) {
599 if (cfg->inputs[i].pin != nid)
601 name = hda_get_autocfg_input_label(codec, cfg, i);
607 name = hda_get_input_pin_label(codec, nid, true);
612 strlcpy(label, name, maxlen);
615 EXPORT_SYMBOL_HDA(snd_hda_get_pin_label);
617 int snd_hda_gen_add_verbs(struct hda_gen_spec *spec,
618 const struct hda_verb *list)
620 const struct hda_verb **v;
621 snd_array_init(&spec->verbs, sizeof(struct hda_verb *), 8);
622 v = snd_array_new(&spec->verbs);
628 EXPORT_SYMBOL_HDA(snd_hda_gen_add_verbs);
630 void snd_hda_gen_apply_verbs(struct hda_codec *codec)
632 struct hda_gen_spec *spec = codec->spec;
634 for (i = 0; i < spec->verbs.used; i++) {
635 struct hda_verb **v = snd_array_elem(&spec->verbs, i);
636 snd_hda_sequence_write(codec, *v);
639 EXPORT_SYMBOL_HDA(snd_hda_gen_apply_verbs);
641 void snd_hda_apply_pincfgs(struct hda_codec *codec,
642 const struct hda_pintbl *cfg)
644 for (; cfg->nid; cfg++)
645 snd_hda_codec_set_pincfg(codec, cfg->nid, cfg->val);
647 EXPORT_SYMBOL_HDA(snd_hda_apply_pincfgs);
649 void snd_hda_apply_fixup(struct hda_codec *codec, int action)
651 struct hda_gen_spec *spec = codec->spec;
652 int id = spec->fixup_id;
653 #ifdef CONFIG_SND_DEBUG_VERBOSE
654 const char *modelname = spec->fixup_name;
658 if (!spec->fixup_list)
662 const struct hda_fixup *fix = spec->fixup_list + id;
666 if (action != HDA_FIXUP_ACT_PRE_PROBE || !fix->v.pins)
668 snd_printdd(KERN_INFO SFX
669 "%s: Apply pincfg for %s\n",
670 codec->chip_name, modelname);
671 snd_hda_apply_pincfgs(codec, fix->v.pins);
673 case HDA_FIXUP_VERBS:
674 if (action != HDA_FIXUP_ACT_PROBE || !fix->v.verbs)
676 snd_printdd(KERN_INFO SFX
677 "%s: Apply fix-verbs for %s\n",
678 codec->chip_name, modelname);
679 snd_hda_gen_add_verbs(codec->spec, fix->v.verbs);
684 snd_printdd(KERN_INFO SFX
685 "%s: Apply fix-func for %s\n",
686 codec->chip_name, modelname);
687 fix->v.func(codec, fix, action);
690 snd_printk(KERN_ERR SFX
691 "%s: Invalid fixup type %d\n",
692 codec->chip_name, fix->type);
702 EXPORT_SYMBOL_HDA(snd_hda_apply_fixup);
704 void snd_hda_pick_fixup(struct hda_codec *codec,
705 const struct hda_model_fixup *models,
706 const struct snd_pci_quirk *quirk,
707 const struct hda_fixup *fixlist)
709 struct hda_gen_spec *spec = codec->spec;
710 const struct snd_pci_quirk *q;
712 const char *name = NULL;
714 /* when model=nofixup is given, don't pick up any fixups */
715 if (codec->modelname && !strcmp(codec->modelname, "nofixup")) {
716 spec->fixup_list = NULL;
721 if (codec->modelname && models) {
722 while (models->name) {
723 if (!strcmp(codec->modelname, models->name)) {
732 q = snd_pci_quirk_lookup(codec->bus->pci, quirk);
735 #ifdef CONFIG_SND_DEBUG_VERBOSE
741 for (q = quirk; q->subvendor; q++) {
742 unsigned int vendorid =
743 q->subdevice | (q->subvendor << 16);
744 if (vendorid == codec->subsystem_id) {
746 #ifdef CONFIG_SND_DEBUG_VERBOSE
756 spec->fixup_list = fixlist;
757 spec->fixup_name = name;
760 EXPORT_SYMBOL_HDA(snd_hda_pick_fixup);