]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - sound/pci/hda/hda_auto_parser.c
UBUNTU: Ubuntu-4.15.0-96.97
[mirror_ubuntu-bionic-kernel.git] / sound / pci / hda / hda_auto_parser.c
CommitLineData
23d30f28
TI
1/*
2 * BIOS auto-parser helper functions for HD-audio
3 *
4 * Copyright (c) 2012 Takashi Iwai <tiwai@suse.de>
5 *
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.
10 */
11
12#include <linux/slab.h>
13#include <linux/export.h>
b9030a00 14#include <linux/sort.h>
23d30f28
TI
15#include <sound/core.h>
16#include "hda_codec.h"
128bc4ba 17#include "hda_local.h"
23d30f28
TI
18#include "hda_auto_parser.h"
19
128bc4ba
TI
20/*
21 * Helper for automatic pin configuration
22 */
23
24static int is_in_nid_list(hda_nid_t nid, const hda_nid_t *list)
25{
26 for (; *list; list++)
27 if (*list == nid)
28 return 1;
29 return 0;
30}
31
b9030a00
TI
32/* a pair of input pin and its sequence */
33struct auto_out_pin {
34 hda_nid_t pin;
35 short seq;
36};
37
38static int compare_seq(const void *ap, const void *bp)
39{
40 const struct auto_out_pin *a = ap;
41 const struct auto_out_pin *b = bp;
42 return (int)(a->seq - b->seq);
43}
128bc4ba
TI
44
45/*
46 * Sort an associated group of pins according to their sequence numbers.
b9030a00 47 * then store it to a pin array.
128bc4ba 48 */
b9030a00 49static void sort_pins_by_sequence(hda_nid_t *pins, struct auto_out_pin *list,
128bc4ba
TI
50 int num_pins)
51{
b9030a00
TI
52 int i;
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;
128bc4ba
TI
56}
57
58
59/* add the found input-pin to the cfg->inputs[] table */
95f72cf2
DH
60static void add_auto_cfg_input_pin(struct hda_codec *codec, struct auto_pin_cfg *cfg,
61 hda_nid_t nid, int type)
128bc4ba
TI
62{
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;
95f72cf2
DH
66 cfg->inputs[cfg->num_inputs].has_boost_on_pin =
67 nid_has_volume(codec, nid, HDA_INPUT);
128bc4ba
TI
68 cfg->num_inputs++;
69 }
70}
71
b9030a00 72static int compare_input_type(const void *ap, const void *bp)
128bc4ba 73{
b9030a00
TI
74 const struct auto_pin_cfg_item *a = ap;
75 const struct auto_pin_cfg_item *b = bp;
95f72cf2
DH
76 if (a->type != b->type)
77 return (int)(a->type - b->type);
78
79 /* In case one has boost and the other one has not,
80 pick the one with boost first. */
81 return (int)(b->has_boost_on_pin - a->has_boost_on_pin);
128bc4ba
TI
82}
83
84/* Reorder the surround channels
85 * ALSA sequence is front/surr/clfe/side
86 * HDA sequence is:
87 * 4-ch: front/surr => OK as it is
88 * 6-ch: front/clfe/surr
89 * 8-ch: front/clfe/rear/side|fc
90 */
91static void reorder_outputs(unsigned int nums, hda_nid_t *pins)
92{
93 hda_nid_t nid;
94
95 switch (nums) {
96 case 3:
97 case 4:
98 nid = pins[1];
99 pins[1] = pins[2];
100 pins[2] = nid;
101 break;
102 }
103}
104
52fd5cbc
TI
105/* check whether the given pin has a proper pin I/O capability bit */
106static bool check_pincap_validity(struct hda_codec *codec, hda_nid_t pin,
107 unsigned int dev)
108{
109 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
110
111 /* some old hardware don't return the proper pincaps */
112 if (!pincap)
113 return true;
114
115 switch (dev) {
116 case AC_JACK_LINE_OUT:
117 case AC_JACK_SPEAKER:
118 case AC_JACK_HP_OUT:
119 case AC_JACK_SPDIF_OUT:
120 case AC_JACK_DIG_OTHER_OUT:
121 return !!(pincap & AC_PINCAP_OUT);
122 default:
123 return !!(pincap & AC_PINCAP_IN);
124 }
125}
126
cb420b11
DH
127static bool can_be_headset_mic(struct hda_codec *codec,
128 struct auto_pin_cfg_item *item,
129 int seq_number)
130{
131 int attr;
132 unsigned int def_conf;
133 if (item->type != AUTO_PIN_MIC)
134 return false;
135
136 if (item->is_headset_mic || item->is_headphone_mic)
137 return false; /* Already assigned */
138
139 def_conf = snd_hda_codec_get_pincfg(codec, item->pin);
140 attr = snd_hda_get_input_pin_attr(def_conf);
141 if (attr <= INPUT_PIN_ATTR_DOCK)
142 return false;
143
144 if (seq_number >= 0) {
145 int seq = get_defcfg_sequence(def_conf);
146 if (seq != seq_number)
147 return false;
148 }
149
150 return true;
151}
152
128bc4ba
TI
153/*
154 * Parse all pin widgets and store the useful pin nids to cfg
155 *
156 * The number of line-outs or any primary output is stored in line_outs,
157 * and the corresponding output pins are assigned to line_out_pins[],
158 * in the order of front, rear, CLFE, side, ...
159 *
160 * If more extra outputs (speaker and headphone) are found, the pins are
161 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
162 * is detected, one of speaker of HP pins is assigned as the primary
163 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
164 * if any analog output exists.
165 *
166 * The analog input pins are assigned to inputs array.
167 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
168 * respectively.
169 */
170int snd_hda_parse_pin_defcfg(struct hda_codec *codec,
171 struct auto_pin_cfg *cfg,
172 const hda_nid_t *ignore_nids,
173 unsigned int cond_flags)
174{
7639a06c 175 hda_nid_t nid;
128bc4ba 176 short seq, assoc_line_out;
b9030a00
TI
177 struct auto_out_pin line_out[ARRAY_SIZE(cfg->line_out_pins)];
178 struct auto_out_pin speaker_out[ARRAY_SIZE(cfg->speaker_pins)];
179 struct auto_out_pin hp_out[ARRAY_SIZE(cfg->hp_pins)];
128bc4ba
TI
180 int i;
181
1c70a583
TI
182 if (!snd_hda_get_int_hint(codec, "parser_flags", &i))
183 cond_flags = i;
184
128bc4ba
TI
185 memset(cfg, 0, sizeof(*cfg));
186
b9030a00
TI
187 memset(line_out, 0, sizeof(line_out));
188 memset(speaker_out, 0, sizeof(speaker_out));
189 memset(hp_out, 0, sizeof(hp_out));
128bc4ba
TI
190 assoc_line_out = 0;
191
7639a06c 192 for_each_hda_codec_node(nid, codec) {
128bc4ba
TI
193 unsigned int wid_caps = get_wcaps(codec, nid);
194 unsigned int wid_type = get_wcaps_type(wid_caps);
195 unsigned int def_conf;
196 short assoc, loc, conn, dev;
197
198 /* read all default configuration for pin complex */
199 if (wid_type != AC_WID_PIN)
200 continue;
201 /* ignore the given nids (e.g. pc-beep returns error) */
202 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
203 continue;
204
205 def_conf = snd_hda_codec_get_pincfg(codec, nid);
128bc4ba
TI
206 conn = get_defcfg_connect(def_conf);
207 if (conn == AC_JACK_PORT_NONE)
208 continue;
209 loc = get_defcfg_location(def_conf);
210 dev = get_defcfg_device(def_conf);
211
212 /* workaround for buggy BIOS setups */
213 if (dev == AC_JACK_LINE_OUT) {
fb690cf5
TI
214 if (conn == AC_JACK_PORT_FIXED ||
215 conn == AC_JACK_PORT_BOTH)
128bc4ba
TI
216 dev = AC_JACK_SPEAKER;
217 }
218
52fd5cbc
TI
219 if (!check_pincap_validity(codec, nid, dev))
220 continue;
221
128bc4ba
TI
222 switch (dev) {
223 case AC_JACK_LINE_OUT:
224 seq = get_defcfg_sequence(def_conf);
225 assoc = get_defcfg_association(def_conf);
226
227 if (!(wid_caps & AC_WCAP_STEREO))
228 if (!cfg->mono_out_pin)
229 cfg->mono_out_pin = nid;
230 if (!assoc)
231 continue;
232 if (!assoc_line_out)
233 assoc_line_out = assoc;
9b7564a6
TI
234 else if (assoc_line_out != assoc) {
235 codec_info(codec,
236 "ignore pin 0x%x with mismatching assoc# 0x%x vs 0x%x\n",
237 nid, assoc, assoc_line_out);
128bc4ba 238 continue;
9b7564a6
TI
239 }
240 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins)) {
241 codec_info(codec,
242 "ignore pin 0x%x, too many assigned pins\n",
243 nid);
128bc4ba 244 continue;
9b7564a6 245 }
b9030a00
TI
246 line_out[cfg->line_outs].pin = nid;
247 line_out[cfg->line_outs].seq = seq;
128bc4ba
TI
248 cfg->line_outs++;
249 break;
250 case AC_JACK_SPEAKER:
251 seq = get_defcfg_sequence(def_conf);
252 assoc = get_defcfg_association(def_conf);
9b7564a6
TI
253 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins)) {
254 codec_info(codec,
255 "ignore pin 0x%x, too many assigned pins\n",
256 nid);
128bc4ba 257 continue;
9b7564a6 258 }
b9030a00
TI
259 speaker_out[cfg->speaker_outs].pin = nid;
260 speaker_out[cfg->speaker_outs].seq = (assoc << 4) | seq;
128bc4ba
TI
261 cfg->speaker_outs++;
262 break;
263 case AC_JACK_HP_OUT:
264 seq = get_defcfg_sequence(def_conf);
265 assoc = get_defcfg_association(def_conf);
9b7564a6
TI
266 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins)) {
267 codec_info(codec,
268 "ignore pin 0x%x, too many assigned pins\n",
269 nid);
128bc4ba 270 continue;
9b7564a6 271 }
b9030a00
TI
272 hp_out[cfg->hp_outs].pin = nid;
273 hp_out[cfg->hp_outs].seq = (assoc << 4) | seq;
128bc4ba
TI
274 cfg->hp_outs++;
275 break;
276 case AC_JACK_MIC_IN:
95f72cf2 277 add_auto_cfg_input_pin(codec, cfg, nid, AUTO_PIN_MIC);
128bc4ba
TI
278 break;
279 case AC_JACK_LINE_IN:
95f72cf2 280 add_auto_cfg_input_pin(codec, cfg, nid, AUTO_PIN_LINE_IN);
128bc4ba
TI
281 break;
282 case AC_JACK_CD:
95f72cf2 283 add_auto_cfg_input_pin(codec, cfg, nid, AUTO_PIN_CD);
128bc4ba
TI
284 break;
285 case AC_JACK_AUX:
95f72cf2 286 add_auto_cfg_input_pin(codec, cfg, nid, AUTO_PIN_AUX);
128bc4ba
TI
287 break;
288 case AC_JACK_SPDIF_OUT:
289 case AC_JACK_DIG_OTHER_OUT:
9b7564a6
TI
290 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins)) {
291 codec_info(codec,
292 "ignore pin 0x%x, too many assigned pins\n",
293 nid);
128bc4ba 294 continue;
9b7564a6 295 }
128bc4ba
TI
296 cfg->dig_out_pins[cfg->dig_outs] = nid;
297 cfg->dig_out_type[cfg->dig_outs] =
298 (loc == AC_JACK_LOC_HDMI) ?
299 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
300 cfg->dig_outs++;
301 break;
302 case AC_JACK_SPDIF_IN:
303 case AC_JACK_DIG_OTHER_IN:
304 cfg->dig_in_pin = nid;
305 if (loc == AC_JACK_LOC_HDMI)
306 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
307 else
308 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
309 break;
310 }
311 }
312
cb420b11
DH
313 /* Find a pin that could be a headset or headphone mic */
314 if (cond_flags & HDA_PINCFG_HEADSET_MIC || cond_flags & HDA_PINCFG_HEADPHONE_MIC) {
315 bool hsmic = !!(cond_flags & HDA_PINCFG_HEADSET_MIC);
316 bool hpmic = !!(cond_flags & HDA_PINCFG_HEADPHONE_MIC);
317 for (i = 0; (hsmic || hpmic) && (i < cfg->num_inputs); i++)
318 if (hsmic && can_be_headset_mic(codec, &cfg->inputs[i], 0xc)) {
319 cfg->inputs[i].is_headset_mic = 1;
320 hsmic = false;
321 } else if (hpmic && can_be_headset_mic(codec, &cfg->inputs[i], 0xd)) {
322 cfg->inputs[i].is_headphone_mic = 1;
323 hpmic = false;
324 }
325
326 /* If we didn't find our sequence number mark, fall back to any sequence number */
327 for (i = 0; (hsmic || hpmic) && (i < cfg->num_inputs); i++) {
328 if (!can_be_headset_mic(codec, &cfg->inputs[i], -1))
a385d97b 329 continue;
cb420b11
DH
330 if (hsmic) {
331 cfg->inputs[i].is_headset_mic = 1;
332 hsmic = false;
333 } else if (hpmic) {
334 cfg->inputs[i].is_headphone_mic = 1;
335 hpmic = false;
336 }
a385d97b 337 }
cb420b11
DH
338
339 if (hsmic)
4e76a883 340 codec_dbg(codec, "Told to look for a headset mic, but didn't find any.\n");
cb420b11 341 if (hpmic)
4e76a883 342 codec_dbg(codec, "Told to look for a headphone mic, but didn't find any.\n");
a385d97b
DH
343 }
344
128bc4ba
TI
345 /* FIX-UP:
346 * If no line-out is defined but multiple HPs are found,
347 * some of them might be the real line-outs.
348 */
349 if (!cfg->line_outs && cfg->hp_outs > 1 &&
350 !(cond_flags & HDA_PINCFG_NO_HP_FIXUP)) {
351 int i = 0;
352 while (i < cfg->hp_outs) {
353 /* The real HPs should have the sequence 0x0f */
b9030a00 354 if ((hp_out[i].seq & 0x0f) == 0x0f) {
128bc4ba
TI
355 i++;
356 continue;
357 }
358 /* Move it to the line-out table */
b9030a00 359 line_out[cfg->line_outs++] = hp_out[i];
128bc4ba 360 cfg->hp_outs--;
b9030a00
TI
361 memmove(hp_out + i, hp_out + i + 1,
362 sizeof(hp_out[0]) * (cfg->hp_outs - i));
128bc4ba 363 }
b9030a00
TI
364 memset(hp_out + cfg->hp_outs, 0,
365 sizeof(hp_out[0]) * (AUTO_CFG_MAX_OUTS - cfg->hp_outs));
128bc4ba
TI
366 if (!cfg->hp_outs)
367 cfg->line_out_type = AUTO_PIN_HP_OUT;
368
369 }
370
371 /* sort by sequence */
b9030a00
TI
372 sort_pins_by_sequence(cfg->line_out_pins, line_out, cfg->line_outs);
373 sort_pins_by_sequence(cfg->speaker_pins, speaker_out,
128bc4ba 374 cfg->speaker_outs);
b9030a00 375 sort_pins_by_sequence(cfg->hp_pins, hp_out, cfg->hp_outs);
128bc4ba
TI
376
377 /*
378 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
379 * as a primary output
380 */
381 if (!cfg->line_outs &&
382 !(cond_flags & HDA_PINCFG_NO_LO_FIXUP)) {
383 if (cfg->speaker_outs) {
384 cfg->line_outs = cfg->speaker_outs;
385 memcpy(cfg->line_out_pins, cfg->speaker_pins,
386 sizeof(cfg->speaker_pins));
387 cfg->speaker_outs = 0;
388 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
389 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
390 } else if (cfg->hp_outs) {
391 cfg->line_outs = cfg->hp_outs;
392 memcpy(cfg->line_out_pins, cfg->hp_pins,
393 sizeof(cfg->hp_pins));
394 cfg->hp_outs = 0;
395 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
396 cfg->line_out_type = AUTO_PIN_HP_OUT;
397 }
398 }
399
400 reorder_outputs(cfg->line_outs, cfg->line_out_pins);
401 reorder_outputs(cfg->hp_outs, cfg->hp_pins);
402 reorder_outputs(cfg->speaker_outs, cfg->speaker_pins);
403
b9030a00
TI
404 /* sort inputs in the order of AUTO_PIN_* type */
405 sort(cfg->inputs, cfg->num_inputs, sizeof(cfg->inputs[0]),
406 compare_input_type, NULL);
128bc4ba
TI
407
408 /*
409 * debug prints of the parsed results
410 */
322b8af1 411 codec_info(codec, "autoconfig for %s: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x) type:%s\n",
7639a06c 412 codec->core.chip_name, cfg->line_outs, cfg->line_out_pins[0],
322b8af1
DH
413 cfg->line_out_pins[1], cfg->line_out_pins[2],
414 cfg->line_out_pins[3], cfg->line_out_pins[4],
128bc4ba
TI
415 cfg->line_out_type == AUTO_PIN_HP_OUT ? "hp" :
416 (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT ?
417 "speaker" : "line"));
4e76a883 418 codec_info(codec, " speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
128bc4ba
TI
419 cfg->speaker_outs, cfg->speaker_pins[0],
420 cfg->speaker_pins[1], cfg->speaker_pins[2],
421 cfg->speaker_pins[3], cfg->speaker_pins[4]);
4e76a883 422 codec_info(codec, " hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
128bc4ba
TI
423 cfg->hp_outs, cfg->hp_pins[0],
424 cfg->hp_pins[1], cfg->hp_pins[2],
425 cfg->hp_pins[3], cfg->hp_pins[4]);
4e76a883 426 codec_info(codec, " mono: mono_out=0x%x\n", cfg->mono_out_pin);
128bc4ba 427 if (cfg->dig_outs)
4e76a883 428 codec_info(codec, " dig-out=0x%x/0x%x\n",
128bc4ba 429 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
4e76a883 430 codec_info(codec, " inputs:\n");
128bc4ba 431 for (i = 0; i < cfg->num_inputs; i++) {
4e76a883 432 codec_info(codec, " %s=0x%x\n",
128bc4ba
TI
433 hda_get_autocfg_input_label(codec, cfg, i),
434 cfg->inputs[i].pin);
435 }
128bc4ba 436 if (cfg->dig_in_pin)
4e76a883 437 codec_info(codec, " dig-in=0x%x\n", cfg->dig_in_pin);
128bc4ba
TI
438
439 return 0;
440}
2698ea98 441EXPORT_SYMBOL_GPL(snd_hda_parse_pin_defcfg);
128bc4ba 442
95a962c3
TI
443/**
444 * snd_hda_get_input_pin_attr - Get the input pin attribute from pin config
445 * @def_conf: pin configuration value
446 *
447 * Guess the input pin attribute (INPUT_PIN_ATTR_XXX) from the given
448 * default pin configuration value.
449 */
128bc4ba
TI
450int snd_hda_get_input_pin_attr(unsigned int def_conf)
451{
452 unsigned int loc = get_defcfg_location(def_conf);
453 unsigned int conn = get_defcfg_connect(def_conf);
454 if (conn == AC_JACK_PORT_NONE)
455 return INPUT_PIN_ATTR_UNUSED;
456 /* Windows may claim the internal mic to be BOTH, too */
457 if (conn == AC_JACK_PORT_FIXED || conn == AC_JACK_PORT_BOTH)
458 return INPUT_PIN_ATTR_INT;
459 if ((loc & 0x30) == AC_JACK_LOC_INTERNAL)
460 return INPUT_PIN_ATTR_INT;
461 if ((loc & 0x30) == AC_JACK_LOC_SEPARATE)
462 return INPUT_PIN_ATTR_DOCK;
463 if (loc == AC_JACK_LOC_REAR)
464 return INPUT_PIN_ATTR_REAR;
465 if (loc == AC_JACK_LOC_FRONT)
466 return INPUT_PIN_ATTR_FRONT;
467 return INPUT_PIN_ATTR_NORMAL;
468}
2698ea98 469EXPORT_SYMBOL_GPL(snd_hda_get_input_pin_attr);
128bc4ba
TI
470
471/**
472 * hda_get_input_pin_label - Give a label for the given input pin
a11e9b16
TI
473 * @codec: the HDA codec
474 * @item: ping config item to refer
475 * @pin: the pin NID
476 * @check_location: flag to add the jack location prefix
128bc4ba 477 *
a11e9b16 478 * When @check_location is true, the function checks the pin location
128bc4ba
TI
479 * for mic and line-in pins, and set an appropriate prefix like "Front",
480 * "Rear", "Internal".
481 */
128bc4ba 482static const char *hda_get_input_pin_label(struct hda_codec *codec,
a385d97b 483 const struct auto_pin_cfg_item *item,
128bc4ba
TI
484 hda_nid_t pin, bool check_location)
485{
486 unsigned int def_conf;
487 static const char * const mic_names[] = {
5ec16d12 488 "Internal Mic", "Dock Mic", "Mic", "Rear Mic", "Front Mic"
128bc4ba
TI
489 };
490 int attr;
491
492 def_conf = snd_hda_codec_get_pincfg(codec, pin);
493
494 switch (get_defcfg_device(def_conf)) {
495 case AC_JACK_MIC_IN:
a385d97b
DH
496 if (item && item->is_headset_mic)
497 return "Headset Mic";
cb420b11
DH
498 if (item && item->is_headphone_mic)
499 return "Headphone Mic";
128bc4ba
TI
500 if (!check_location)
501 return "Mic";
502 attr = snd_hda_get_input_pin_attr(def_conf);
503 if (!attr)
504 return "None";
505 return mic_names[attr - 1];
506 case AC_JACK_LINE_IN:
507 if (!check_location)
508 return "Line";
509 attr = snd_hda_get_input_pin_attr(def_conf);
510 if (!attr)
511 return "None";
512 if (attr == INPUT_PIN_ATTR_DOCK)
513 return "Dock Line";
514 return "Line";
515 case AC_JACK_AUX:
516 return "Aux";
517 case AC_JACK_CD:
518 return "CD";
519 case AC_JACK_SPDIF_IN:
520 return "SPDIF In";
521 case AC_JACK_DIG_OTHER_IN:
522 return "Digital In";
54d778b3
TI
523 case AC_JACK_HP_OUT:
524 return "Headphone Mic";
128bc4ba
TI
525 default:
526 return "Misc";
527 }
528}
529
530/* Check whether the location prefix needs to be added to the label.
531 * If all mic-jacks are in the same location (e.g. rear panel), we don't
532 * have to put "Front" prefix to each label. In such a case, returns false.
533 */
534static int check_mic_location_need(struct hda_codec *codec,
535 const struct auto_pin_cfg *cfg,
536 int input)
537{
538 unsigned int defc;
539 int i, attr, attr2;
540
541 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[input].pin);
542 attr = snd_hda_get_input_pin_attr(defc);
543 /* for internal or docking mics, we need locations */
544 if (attr <= INPUT_PIN_ATTR_NORMAL)
545 return 1;
546
547 attr = 0;
548 for (i = 0; i < cfg->num_inputs; i++) {
549 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[i].pin);
550 attr2 = snd_hda_get_input_pin_attr(defc);
551 if (attr2 >= INPUT_PIN_ATTR_NORMAL) {
552 if (attr && attr != attr2)
553 return 1; /* different locations found */
554 attr = attr2;
555 }
556 }
557 return 0;
558}
559
560/**
561 * hda_get_autocfg_input_label - Get a label for the given input
a11e9b16
TI
562 * @codec: the HDA codec
563 * @cfg: the parsed pin configuration
564 * @input: the input index number
128bc4ba
TI
565 *
566 * Get a label for the given input pin defined by the autocfg item.
567 * Unlike hda_get_input_pin_label(), this function checks all inputs
568 * defined in autocfg and avoids the redundant mic/line prefix as much as
569 * possible.
570 */
571const char *hda_get_autocfg_input_label(struct hda_codec *codec,
572 const struct auto_pin_cfg *cfg,
573 int input)
574{
575 int type = cfg->inputs[input].type;
576 int has_multiple_pins = 0;
577
578 if ((input > 0 && cfg->inputs[input - 1].type == type) ||
579 (input < cfg->num_inputs - 1 && cfg->inputs[input + 1].type == type))
580 has_multiple_pins = 1;
581 if (has_multiple_pins && type == AUTO_PIN_MIC)
582 has_multiple_pins &= check_mic_location_need(codec, cfg, input);
9f3dadb1 583 has_multiple_pins |= codec->force_pin_prefix;
a385d97b
DH
584 return hda_get_input_pin_label(codec, &cfg->inputs[input],
585 cfg->inputs[input].pin,
128bc4ba
TI
586 has_multiple_pins);
587}
2698ea98 588EXPORT_SYMBOL_GPL(hda_get_autocfg_input_label);
128bc4ba
TI
589
590/* return the position of NID in the list, or -1 if not found */
591static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
592{
593 int i;
594 for (i = 0; i < nums; i++)
595 if (list[i] == nid)
596 return i;
597 return -1;
598}
599
600/* get a unique suffix or an index number */
601static const char *check_output_sfx(hda_nid_t nid, const hda_nid_t *pins,
602 int num_pins, int *indexp)
603{
604 static const char * const channel_sfx[] = {
605 " Front", " Surround", " CLFE", " Side"
606 };
607 int i;
608
609 i = find_idx_in_nid_list(nid, pins, num_pins);
610 if (i < 0)
611 return NULL;
612 if (num_pins == 1)
613 return "";
614 if (num_pins > ARRAY_SIZE(channel_sfx)) {
615 if (indexp)
616 *indexp = i;
617 return "";
618 }
619 return channel_sfx[i];
620}
621
eee3ed43
DH
622static const char *check_output_pfx(struct hda_codec *codec, hda_nid_t nid)
623{
624 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
625 int attr = snd_hda_get_input_pin_attr(def_conf);
626
627 /* check the location */
628 switch (attr) {
629 case INPUT_PIN_ATTR_DOCK:
630 return "Dock ";
631 case INPUT_PIN_ATTR_FRONT:
632 return "Front ";
633 }
634 return "";
635}
636
637static int get_hp_label_index(struct hda_codec *codec, hda_nid_t nid,
638 const hda_nid_t *pins, int num_pins)
639{
640 int i, j, idx = 0;
641
642 const char *pfx = check_output_pfx(codec, nid);
643
644 i = find_idx_in_nid_list(nid, pins, num_pins);
645 if (i < 0)
646 return -1;
647 for (j = 0; j < i; j++)
648 if (pfx == check_output_pfx(codec, pins[j]))
649 idx++;
650
651 return idx;
652}
653
128bc4ba
TI
654static int fill_audio_out_name(struct hda_codec *codec, hda_nid_t nid,
655 const struct auto_pin_cfg *cfg,
656 const char *name, char *label, int maxlen,
657 int *indexp)
658{
659 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
660 int attr = snd_hda_get_input_pin_attr(def_conf);
eee3ed43 661 const char *pfx, *sfx = "";
128bc4ba
TI
662
663 /* handle as a speaker if it's a fixed line-out */
664 if (!strcmp(name, "Line Out") && attr == INPUT_PIN_ATTR_INT)
665 name = "Speaker";
eee3ed43
DH
666 pfx = check_output_pfx(codec, nid);
667
128bc4ba
TI
668 if (cfg) {
669 /* try to give a unique suffix if needed */
670 sfx = check_output_sfx(nid, cfg->line_out_pins, cfg->line_outs,
671 indexp);
672 if (!sfx)
673 sfx = check_output_sfx(nid, cfg->speaker_pins, cfg->speaker_outs,
674 indexp);
675 if (!sfx) {
676 /* don't add channel suffix for Headphone controls */
eee3ed43
DH
677 int idx = get_hp_label_index(codec, nid, cfg->hp_pins,
678 cfg->hp_outs);
728deecd 679 if (idx >= 0 && indexp)
128bc4ba
TI
680 *indexp = idx;
681 sfx = "";
682 }
683 }
684 snprintf(label, maxlen, "%s%s%s", pfx, name, sfx);
685 return 1;
686}
687
3f25dcf6
DH
688#define is_hdmi_cfg(conf) \
689 (get_defcfg_location(conf) == AC_JACK_LOC_HDMI)
690
128bc4ba
TI
691/**
692 * snd_hda_get_pin_label - Get a label for the given I/O pin
a11e9b16
TI
693 * @codec: the HDA codec
694 * @nid: pin NID
695 * @cfg: the parsed pin configuration
696 * @label: the string buffer to store
697 * @maxlen: the max length of string buffer (including termination)
698 * @indexp: the pointer to return the index number (for multiple ctls)
128bc4ba
TI
699 *
700 * Get a label for the given pin. This function works for both input and
701 * output pins. When @cfg is given as non-NULL, the function tries to get
702 * an optimized label using hda_get_autocfg_input_label().
703 *
704 * This function tries to give a unique label string for the pin as much as
705 * possible. For example, when the multiple line-outs are present, it adds
706 * the channel suffix like "Front", "Surround", etc (only when @cfg is given).
707 * If no unique name with a suffix is available and @indexp is non-NULL, the
708 * index number is stored in the pointer.
709 */
710int snd_hda_get_pin_label(struct hda_codec *codec, hda_nid_t nid,
711 const struct auto_pin_cfg *cfg,
712 char *label, int maxlen, int *indexp)
713{
714 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
715 const char *name = NULL;
716 int i;
3f25dcf6 717 bool hdmi;
128bc4ba
TI
718
719 if (indexp)
720 *indexp = 0;
721 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
722 return 0;
723
724 switch (get_defcfg_device(def_conf)) {
725 case AC_JACK_LINE_OUT:
726 return fill_audio_out_name(codec, nid, cfg, "Line Out",
727 label, maxlen, indexp);
728 case AC_JACK_SPEAKER:
729 return fill_audio_out_name(codec, nid, cfg, "Speaker",
730 label, maxlen, indexp);
731 case AC_JACK_HP_OUT:
732 return fill_audio_out_name(codec, nid, cfg, "Headphone",
733 label, maxlen, indexp);
734 case AC_JACK_SPDIF_OUT:
735 case AC_JACK_DIG_OTHER_OUT:
3f25dcf6
DH
736 hdmi = is_hdmi_cfg(def_conf);
737 name = hdmi ? "HDMI" : "SPDIF";
738 if (cfg && indexp)
739 for (i = 0; i < cfg->dig_outs; i++) {
740 hda_nid_t pin = cfg->dig_out_pins[i];
741 unsigned int c;
742 if (pin == nid)
743 break;
744 c = snd_hda_codec_get_pincfg(codec, pin);
745 if (hdmi == is_hdmi_cfg(c))
746 (*indexp)++;
747 }
128bc4ba
TI
748 break;
749 default:
750 if (cfg) {
751 for (i = 0; i < cfg->num_inputs; i++) {
752 if (cfg->inputs[i].pin != nid)
753 continue;
754 name = hda_get_autocfg_input_label(codec, cfg, i);
755 if (name)
756 break;
757 }
758 }
759 if (!name)
a385d97b 760 name = hda_get_input_pin_label(codec, NULL, nid, true);
128bc4ba
TI
761 break;
762 }
763 if (!name)
764 return 0;
765 strlcpy(label, name, maxlen);
766 return 1;
767}
2698ea98 768EXPORT_SYMBOL_GPL(snd_hda_get_pin_label);
128bc4ba 769
95a962c3
TI
770/**
771 * snd_hda_add_verbs - Add verbs to the init list
772 * @codec: the HDA codec
773 * @list: zero-terminated verb list to add
774 *
775 * Append the given verb list to the execution list. The verbs will be
776 * performed at init and resume time via snd_hda_apply_verbs().
777 */
c9ce6b26
TI
778int snd_hda_add_verbs(struct hda_codec *codec,
779 const struct hda_verb *list)
23d30f28
TI
780{
781 const struct hda_verb **v;
c9ce6b26 782 v = snd_array_new(&codec->verbs);
23d30f28
TI
783 if (!v)
784 return -ENOMEM;
785 *v = list;
786 return 0;
787}
2698ea98 788EXPORT_SYMBOL_GPL(snd_hda_add_verbs);
23d30f28 789
95a962c3
TI
790/**
791 * snd_hda_apply_verbs - Execute the init verb lists
792 * @codec: the HDA codec
793 */
c9ce6b26 794void snd_hda_apply_verbs(struct hda_codec *codec)
23d30f28 795{
23d30f28 796 int i;
c9ce6b26
TI
797 for (i = 0; i < codec->verbs.used; i++) {
798 struct hda_verb **v = snd_array_elem(&codec->verbs, i);
23d30f28
TI
799 snd_hda_sequence_write(codec, *v);
800 }
801}
2698ea98 802EXPORT_SYMBOL_GPL(snd_hda_apply_verbs);
23d30f28 803
95a962c3
TI
804/**
805 * snd_hda_apply_pincfgs - Set each pin config in the given list
806 * @codec: the HDA codec
807 * @cfg: NULL-terminated pin config table
808 */
23d30f28
TI
809void snd_hda_apply_pincfgs(struct hda_codec *codec,
810 const struct hda_pintbl *cfg)
811{
812 for (; cfg->nid; cfg++)
813 snd_hda_codec_set_pincfg(codec, cfg->nid, cfg->val);
814}
2698ea98 815EXPORT_SYMBOL_GPL(snd_hda_apply_pincfgs);
23d30f28 816
fd108215
TI
817static void set_pin_targets(struct hda_codec *codec,
818 const struct hda_pintbl *cfg)
819{
820 for (; cfg->nid; cfg++)
821 snd_hda_set_pin_ctl_cache(codec, cfg->nid, cfg->val);
822}
823
1f578250 824static void apply_fixup(struct hda_codec *codec, int id, int action, int depth)
23d30f28 825{
c9ce6b26 826 const char *modelname = codec->fixup_name;
23d30f28
TI
827
828 while (id >= 0) {
c9ce6b26 829 const struct hda_fixup *fix = codec->fixup_list + id;
23d30f28 830
0ea48dab
TI
831 if (++depth > 10)
832 break;
1f578250
TI
833 if (fix->chained_before)
834 apply_fixup(codec, fix->chain_id, action, depth + 1);
835
23d30f28
TI
836 switch (fix->type) {
837 case HDA_FIXUP_PINS:
838 if (action != HDA_FIXUP_ACT_PRE_PROBE || !fix->v.pins)
839 break;
4e76a883 840 codec_dbg(codec, "%s: Apply pincfg for %s\n",
7639a06c 841 codec->core.chip_name, modelname);
23d30f28
TI
842 snd_hda_apply_pincfgs(codec, fix->v.pins);
843 break;
844 case HDA_FIXUP_VERBS:
845 if (action != HDA_FIXUP_ACT_PROBE || !fix->v.verbs)
846 break;
4e76a883 847 codec_dbg(codec, "%s: Apply fix-verbs for %s\n",
7639a06c 848 codec->core.chip_name, modelname);
c9ce6b26 849 snd_hda_add_verbs(codec, fix->v.verbs);
23d30f28
TI
850 break;
851 case HDA_FIXUP_FUNC:
852 if (!fix->v.func)
853 break;
4e76a883 854 codec_dbg(codec, "%s: Apply fix-func for %s\n",
7639a06c 855 codec->core.chip_name, modelname);
23d30f28
TI
856 fix->v.func(codec, fix, action);
857 break;
fd108215
TI
858 case HDA_FIXUP_PINCTLS:
859 if (action != HDA_FIXUP_ACT_PROBE || !fix->v.pins)
860 break;
4e76a883 861 codec_dbg(codec, "%s: Apply pinctl for %s\n",
7639a06c 862 codec->core.chip_name, modelname);
fd108215
TI
863 set_pin_targets(codec, fix->v.pins);
864 break;
23d30f28 865 default:
4e76a883 866 codec_err(codec, "%s: Invalid fixup type %d\n",
7639a06c 867 codec->core.chip_name, fix->type);
23d30f28
TI
868 break;
869 }
1f578250 870 if (!fix->chained || fix->chained_before)
23d30f28 871 break;
23d30f28
TI
872 id = fix->chain_id;
873 }
874}
1f578250 875
95a962c3
TI
876/**
877 * snd_hda_apply_fixup - Apply the fixup chain with the given action
878 * @codec: the HDA codec
879 * @action: fixup action (HDA_FIXUP_ACT_XXX)
880 */
1f578250
TI
881void snd_hda_apply_fixup(struct hda_codec *codec, int action)
882{
883 if (codec->fixup_list)
884 apply_fixup(codec, codec->fixup_id, action, 0);
885}
2698ea98 886EXPORT_SYMBOL_GPL(snd_hda_apply_fixup);
23d30f28 887
5e0ad0d8
KHF
888#define IGNORE_SEQ_ASSOC (~(AC_DEFCFG_SEQUENCE | AC_DEFCFG_DEF_ASSOC))
889
20531415 890static bool pin_config_match(struct hda_codec *codec,
31d7a6e3
HW
891 const struct hda_pintbl *pins,
892 bool match_all_pins)
20531415 893{
11580297
HW
894 int i;
895
896 for (i = 0; i < codec->init_pins.used; i++) {
897 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
898 hda_nid_t nid = pin->nid;
899 u32 cfg = pin->cfg;
900 const struct hda_pintbl *t_pins;
901 int found;
902
903 t_pins = pins;
904 found = 0;
905 for (; t_pins->nid; t_pins++) {
906 if (t_pins->nid == nid) {
907 found = 1;
5e0ad0d8 908 if ((t_pins->val & IGNORE_SEQ_ASSOC) == (cfg & IGNORE_SEQ_ASSOC))
11580297
HW
909 break;
910 else if ((cfg & 0xf0000000) == 0x40000000 && (t_pins->val & 0xf0000000) == 0x40000000)
911 break;
912 else
913 return false;
914 }
915 }
31d7a6e3
HW
916 if (match_all_pins &&
917 !found && (cfg & 0xf0000000) != 0x40000000)
20531415
DH
918 return false;
919 }
11580297 920
20531415
DH
921 return true;
922}
923
95a962c3
TI
924/**
925 * snd_hda_pick_pin_fixup - Pick up a fixup matching with the pin quirk list
926 * @codec: the HDA codec
927 * @pin_quirk: zero-terminated pin quirk list
928 * @fixlist: the fixup list
31d7a6e3 929 * @match_all_pins: all valid pins must match with the table entries
95a962c3 930 */
20531415
DH
931void snd_hda_pick_pin_fixup(struct hda_codec *codec,
932 const struct snd_hda_pin_quirk *pin_quirk,
31d7a6e3
HW
933 const struct hda_fixup *fixlist,
934 bool match_all_pins)
20531415
DH
935{
936 const struct snd_hda_pin_quirk *pq;
937
f5662e1c 938 if (codec->fixup_id != HDA_FIXUP_ID_NOT_SET)
20531415
DH
939 return;
940
941 for (pq = pin_quirk; pq->subvendor; pq++) {
7639a06c 942 if ((codec->core.subsystem_id & 0xffff0000) != (pq->subvendor << 16))
20531415 943 continue;
7639a06c 944 if (codec->core.vendor_id != pq->codec)
20531415 945 continue;
31d7a6e3 946 if (pin_config_match(codec, pq->pins, match_all_pins)) {
20531415
DH
947 codec->fixup_id = pq->value;
948#ifdef CONFIG_SND_DEBUG_VERBOSE
949 codec->fixup_name = pq->name;
4f7946ec 950 codec_dbg(codec, "%s: picked fixup %s (pin match)\n",
7639a06c 951 codec->core.chip_name, codec->fixup_name);
20531415
DH
952#endif
953 codec->fixup_list = fixlist;
954 return;
955 }
956 }
957}
958EXPORT_SYMBOL_GPL(snd_hda_pick_pin_fixup);
959
95a962c3
TI
960/**
961 * snd_hda_pick_fixup - Pick up a fixup matching with PCI/codec SSID or model string
962 * @codec: the HDA codec
963 * @models: NULL-terminated model string list
964 * @quirk: zero-terminated PCI/codec SSID quirk list
965 * @fixlist: the fixup list
966 *
967 * Pick up a fixup entry matching with the given model string or SSID.
968 * If a fixup was already set beforehand, the function doesn't do anything.
969 * When a special model string "nofixup" is given, also no fixup is applied.
970 *
971 * The function tries to find the matching model name at first, if given.
972 * If nothing matched, try to look up the PCI SSID.
973 * If still nothing matched, try to look up the codec SSID.
974 */
23d30f28
TI
975void snd_hda_pick_fixup(struct hda_codec *codec,
976 const struct hda_model_fixup *models,
977 const struct snd_pci_quirk *quirk,
978 const struct hda_fixup *fixlist)
979{
23d30f28 980 const struct snd_pci_quirk *q;
f5662e1c 981 int id = HDA_FIXUP_ID_NOT_SET;
23d30f28
TI
982 const char *name = NULL;
983
f5662e1c
DH
984 if (codec->fixup_id != HDA_FIXUP_ID_NOT_SET)
985 return;
986
23d30f28
TI
987 /* when model=nofixup is given, don't pick up any fixups */
988 if (codec->modelname && !strcmp(codec->modelname, "nofixup")) {
c9ce6b26 989 codec->fixup_list = NULL;
f5662e1c
DH
990 codec->fixup_name = NULL;
991 codec->fixup_id = HDA_FIXUP_ID_NO_FIXUP;
4f7946ec 992 codec_dbg(codec, "%s: picked no fixup (nofixup specified)\n",
7639a06c 993 codec->core.chip_name);
23d30f28
TI
994 return;
995 }
996
997 if (codec->modelname && models) {
998 while (models->name) {
999 if (!strcmp(codec->modelname, models->name)) {
c21c8cf7
DH
1000 codec->fixup_id = models->id;
1001 codec->fixup_name = models->name;
8fffe7d1 1002 codec->fixup_list = fixlist;
4f7946ec 1003 codec_dbg(codec, "%s: picked fixup %s (model specified)\n",
7639a06c 1004 codec->core.chip_name, codec->fixup_name);
c21c8cf7 1005 return;
23d30f28
TI
1006 }
1007 models++;
1008 }
1009 }
f5662e1c 1010 if (quirk) {
23d30f28
TI
1011 q = snd_pci_quirk_lookup(codec->bus->pci, quirk);
1012 if (q) {
1013 id = q->value;
1014#ifdef CONFIG_SND_DEBUG_VERBOSE
1015 name = q->name;
4f7946ec 1016 codec_dbg(codec, "%s: picked fixup %s (PCI SSID%s)\n",
7639a06c 1017 codec->core.chip_name, name, q->subdevice_mask ? "" : " - vendor generic");
23d30f28
TI
1018#endif
1019 }
1020 }
639aa4bd 1021 if (id < 0 && quirk) {
697aebab 1022 for (q = quirk; q->subvendor || q->subdevice; q++) {
23d30f28
TI
1023 unsigned int vendorid =
1024 q->subdevice | (q->subvendor << 16);
a33b7b0a 1025 unsigned int mask = 0xffff0000 | q->subdevice_mask;
7639a06c 1026 if ((codec->core.subsystem_id & mask) == (vendorid & mask)) {
23d30f28
TI
1027 id = q->value;
1028#ifdef CONFIG_SND_DEBUG_VERBOSE
1029 name = q->name;
4f7946ec 1030 codec_dbg(codec, "%s: picked fixup %s (codec SSID)\n",
7639a06c 1031 codec->core.chip_name, name);
23d30f28
TI
1032#endif
1033 break;
1034 }
1035 }
1036 }
1037
c9ce6b26 1038 codec->fixup_id = id;
23d30f28 1039 if (id >= 0) {
c9ce6b26
TI
1040 codec->fixup_list = fixlist;
1041 codec->fixup_name = name;
23d30f28
TI
1042 }
1043}
2698ea98 1044EXPORT_SYMBOL_GPL(snd_hda_pick_fixup);