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:\n");
336 for (i = 0; i < cfg->num_inputs; i++) {
337 snd_printd(" %s=0x%x\n",
338 hda_get_autocfg_input_label(codec, cfg, i),
342 snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
346 EXPORT_SYMBOL_HDA(snd_hda_parse_pin_defcfg);
348 int snd_hda_get_input_pin_attr(unsigned int def_conf)
350 unsigned int loc = get_defcfg_location(def_conf);
351 unsigned int conn = get_defcfg_connect(def_conf);
352 if (conn == AC_JACK_PORT_NONE)
353 return INPUT_PIN_ATTR_UNUSED;
354 /* Windows may claim the internal mic to be BOTH, too */
355 if (conn == AC_JACK_PORT_FIXED || conn == AC_JACK_PORT_BOTH)
356 return INPUT_PIN_ATTR_INT;
357 if ((loc & 0x30) == AC_JACK_LOC_INTERNAL)
358 return INPUT_PIN_ATTR_INT;
359 if ((loc & 0x30) == AC_JACK_LOC_SEPARATE)
360 return INPUT_PIN_ATTR_DOCK;
361 if (loc == AC_JACK_LOC_REAR)
362 return INPUT_PIN_ATTR_REAR;
363 if (loc == AC_JACK_LOC_FRONT)
364 return INPUT_PIN_ATTR_FRONT;
365 return INPUT_PIN_ATTR_NORMAL;
367 EXPORT_SYMBOL_HDA(snd_hda_get_input_pin_attr);
370 * hda_get_input_pin_label - Give a label for the given input pin
372 * When check_location is true, the function checks the pin location
373 * for mic and line-in pins, and set an appropriate prefix like "Front",
374 * "Rear", "Internal".
377 static const char *hda_get_input_pin_label(struct hda_codec *codec,
378 hda_nid_t pin, bool check_location)
380 unsigned int def_conf;
381 static const char * const mic_names[] = {
382 "Internal Mic", "Dock Mic", "Mic", "Front Mic", "Rear Mic",
386 def_conf = snd_hda_codec_get_pincfg(codec, pin);
388 switch (get_defcfg_device(def_conf)) {
392 attr = snd_hda_get_input_pin_attr(def_conf);
395 return mic_names[attr - 1];
396 case AC_JACK_LINE_IN:
399 attr = snd_hda_get_input_pin_attr(def_conf);
402 if (attr == INPUT_PIN_ATTR_DOCK)
409 case AC_JACK_SPDIF_IN:
411 case AC_JACK_DIG_OTHER_IN:
418 /* Check whether the location prefix needs to be added to the label.
419 * If all mic-jacks are in the same location (e.g. rear panel), we don't
420 * have to put "Front" prefix to each label. In such a case, returns false.
422 static int check_mic_location_need(struct hda_codec *codec,
423 const struct auto_pin_cfg *cfg,
429 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[input].pin);
430 attr = snd_hda_get_input_pin_attr(defc);
431 /* for internal or docking mics, we need locations */
432 if (attr <= INPUT_PIN_ATTR_NORMAL)
436 for (i = 0; i < cfg->num_inputs; i++) {
437 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[i].pin);
438 attr2 = snd_hda_get_input_pin_attr(defc);
439 if (attr2 >= INPUT_PIN_ATTR_NORMAL) {
440 if (attr && attr != attr2)
441 return 1; /* different locations found */
449 * hda_get_autocfg_input_label - Get a label for the given input
451 * Get a label for the given input pin defined by the autocfg item.
452 * Unlike hda_get_input_pin_label(), this function checks all inputs
453 * defined in autocfg and avoids the redundant mic/line prefix as much as
456 const char *hda_get_autocfg_input_label(struct hda_codec *codec,
457 const struct auto_pin_cfg *cfg,
460 int type = cfg->inputs[input].type;
461 int has_multiple_pins = 0;
463 if ((input > 0 && cfg->inputs[input - 1].type == type) ||
464 (input < cfg->num_inputs - 1 && cfg->inputs[input + 1].type == type))
465 has_multiple_pins = 1;
466 if (has_multiple_pins && type == AUTO_PIN_MIC)
467 has_multiple_pins &= check_mic_location_need(codec, cfg, input);
468 return hda_get_input_pin_label(codec, cfg->inputs[input].pin,
471 EXPORT_SYMBOL_HDA(hda_get_autocfg_input_label);
473 /* return the position of NID in the list, or -1 if not found */
474 static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
477 for (i = 0; i < nums; i++)
483 /* get a unique suffix or an index number */
484 static const char *check_output_sfx(hda_nid_t nid, const hda_nid_t *pins,
485 int num_pins, int *indexp)
487 static const char * const channel_sfx[] = {
488 " Front", " Surround", " CLFE", " Side"
492 i = find_idx_in_nid_list(nid, pins, num_pins);
497 if (num_pins > ARRAY_SIZE(channel_sfx)) {
502 return channel_sfx[i];
505 static int fill_audio_out_name(struct hda_codec *codec, hda_nid_t nid,
506 const struct auto_pin_cfg *cfg,
507 const char *name, char *label, int maxlen,
510 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
511 int attr = snd_hda_get_input_pin_attr(def_conf);
512 const char *pfx = "", *sfx = "";
514 /* handle as a speaker if it's a fixed line-out */
515 if (!strcmp(name, "Line Out") && attr == INPUT_PIN_ATTR_INT)
517 /* check the location */
519 case INPUT_PIN_ATTR_DOCK:
522 case INPUT_PIN_ATTR_FRONT:
527 /* try to give a unique suffix if needed */
528 sfx = check_output_sfx(nid, cfg->line_out_pins, cfg->line_outs,
531 sfx = check_output_sfx(nid, cfg->speaker_pins, cfg->speaker_outs,
534 /* don't add channel suffix for Headphone controls */
535 int idx = find_idx_in_nid_list(nid, cfg->hp_pins,
542 snprintf(label, maxlen, "%s%s%s", pfx, name, sfx);
547 * snd_hda_get_pin_label - Get a label for the given I/O pin
549 * Get a label for the given pin. This function works for both input and
550 * output pins. When @cfg is given as non-NULL, the function tries to get
551 * an optimized label using hda_get_autocfg_input_label().
553 * This function tries to give a unique label string for the pin as much as
554 * possible. For example, when the multiple line-outs are present, it adds
555 * the channel suffix like "Front", "Surround", etc (only when @cfg is given).
556 * If no unique name with a suffix is available and @indexp is non-NULL, the
557 * index number is stored in the pointer.
559 int snd_hda_get_pin_label(struct hda_codec *codec, hda_nid_t nid,
560 const struct auto_pin_cfg *cfg,
561 char *label, int maxlen, int *indexp)
563 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
564 const char *name = NULL;
569 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
572 switch (get_defcfg_device(def_conf)) {
573 case AC_JACK_LINE_OUT:
574 return fill_audio_out_name(codec, nid, cfg, "Line Out",
575 label, maxlen, indexp);
576 case AC_JACK_SPEAKER:
577 return fill_audio_out_name(codec, nid, cfg, "Speaker",
578 label, maxlen, indexp);
580 return fill_audio_out_name(codec, nid, cfg, "Headphone",
581 label, maxlen, indexp);
582 case AC_JACK_SPDIF_OUT:
583 case AC_JACK_DIG_OTHER_OUT:
584 if (get_defcfg_location(def_conf) == AC_JACK_LOC_HDMI)
589 i = find_idx_in_nid_list(nid, cfg->dig_out_pins,
597 for (i = 0; i < cfg->num_inputs; i++) {
598 if (cfg->inputs[i].pin != nid)
600 name = hda_get_autocfg_input_label(codec, cfg, i);
606 name = hda_get_input_pin_label(codec, nid, true);
611 strlcpy(label, name, maxlen);
614 EXPORT_SYMBOL_HDA(snd_hda_get_pin_label);
616 int snd_hda_gen_add_verbs(struct hda_gen_spec *spec,
617 const struct hda_verb *list)
619 const struct hda_verb **v;
620 v = snd_array_new(&spec->verbs);
626 EXPORT_SYMBOL_HDA(snd_hda_gen_add_verbs);
628 void snd_hda_gen_apply_verbs(struct hda_codec *codec)
630 struct hda_gen_spec *spec = codec->spec;
632 for (i = 0; i < spec->verbs.used; i++) {
633 struct hda_verb **v = snd_array_elem(&spec->verbs, i);
634 snd_hda_sequence_write(codec, *v);
637 EXPORT_SYMBOL_HDA(snd_hda_gen_apply_verbs);
639 void snd_hda_apply_pincfgs(struct hda_codec *codec,
640 const struct hda_pintbl *cfg)
642 for (; cfg->nid; cfg++)
643 snd_hda_codec_set_pincfg(codec, cfg->nid, cfg->val);
645 EXPORT_SYMBOL_HDA(snd_hda_apply_pincfgs);
647 void snd_hda_apply_fixup(struct hda_codec *codec, int action)
649 struct hda_gen_spec *spec = codec->spec;
650 int id = spec->fixup_id;
651 #ifdef CONFIG_SND_DEBUG_VERBOSE
652 const char *modelname = spec->fixup_name;
656 if (!spec->fixup_list)
660 const struct hda_fixup *fix = spec->fixup_list + id;
664 if (action != HDA_FIXUP_ACT_PRE_PROBE || !fix->v.pins)
666 snd_printdd(KERN_INFO SFX
667 "%s: Apply pincfg for %s\n",
668 codec->chip_name, modelname);
669 snd_hda_apply_pincfgs(codec, fix->v.pins);
671 case HDA_FIXUP_VERBS:
672 if (action != HDA_FIXUP_ACT_PROBE || !fix->v.verbs)
674 snd_printdd(KERN_INFO SFX
675 "%s: Apply fix-verbs for %s\n",
676 codec->chip_name, modelname);
677 snd_hda_gen_add_verbs(codec->spec, fix->v.verbs);
682 snd_printdd(KERN_INFO SFX
683 "%s: Apply fix-func for %s\n",
684 codec->chip_name, modelname);
685 fix->v.func(codec, fix, action);
688 snd_printk(KERN_ERR SFX
689 "%s: Invalid fixup type %d\n",
690 codec->chip_name, fix->type);
700 EXPORT_SYMBOL_HDA(snd_hda_apply_fixup);
702 void snd_hda_pick_fixup(struct hda_codec *codec,
703 const struct hda_model_fixup *models,
704 const struct snd_pci_quirk *quirk,
705 const struct hda_fixup *fixlist)
707 struct hda_gen_spec *spec = codec->spec;
708 const struct snd_pci_quirk *q;
710 const char *name = NULL;
712 /* when model=nofixup is given, don't pick up any fixups */
713 if (codec->modelname && !strcmp(codec->modelname, "nofixup")) {
714 spec->fixup_list = NULL;
719 if (codec->modelname && models) {
720 while (models->name) {
721 if (!strcmp(codec->modelname, models->name)) {
729 if (id < 0 && quirk) {
730 q = snd_pci_quirk_lookup(codec->bus->pci, quirk);
733 #ifdef CONFIG_SND_DEBUG_VERBOSE
738 if (id < 0 && quirk) {
739 for (q = quirk; q->subvendor; q++) {
740 unsigned int vendorid =
741 q->subdevice | (q->subvendor << 16);
742 if (vendorid == codec->subsystem_id) {
744 #ifdef CONFIG_SND_DEBUG_VERBOSE
754 spec->fixup_list = fixlist;
755 spec->fixup_name = name;
758 EXPORT_SYMBOL_HDA(snd_hda_pick_fixup);