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>
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"
19
20 /*
21 * Helper for automatic pin configuration
22 */
23
is_in_nid_list(hda_nid_t nid,const hda_nid_t * list)24 static 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
32 /* a pair of input pin and its sequence */
33 struct auto_out_pin {
34 hda_nid_t pin;
35 short seq;
36 };
37
compare_seq(const void * ap,const void * bp)38 static 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 }
44
45 /*
46 * Sort an associated group of pins according to their sequence numbers.
47 * then store it to a pin array.
48 */
sort_pins_by_sequence(hda_nid_t * pins,struct auto_out_pin * list,int num_pins)49 static void sort_pins_by_sequence(hda_nid_t *pins, struct auto_out_pin *list,
50 int num_pins)
51 {
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;
56 }
57
58
59 /* add the found input-pin to the cfg->inputs[] table */
add_auto_cfg_input_pin(struct hda_codec * codec,struct auto_pin_cfg * cfg,hda_nid_t nid,int type)60 static void add_auto_cfg_input_pin(struct hda_codec *codec, struct auto_pin_cfg *cfg,
61 hda_nid_t nid, int type)
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;
66 cfg->inputs[cfg->num_inputs].has_boost_on_pin =
67 nid_has_volume(codec, nid, HDA_INPUT);
68 cfg->num_inputs++;
69 }
70 }
71
compare_input_type(const void * ap,const void * bp)72 static int compare_input_type(const void *ap, const void *bp)
73 {
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);
78
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)
83 return 1; /* swap */
84
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);
88 }
89
90 /* Reorder the surround channels
91 * ALSA sequence is front/surr/clfe/side
92 * HDA sequence is:
93 * 4-ch: front/surr => OK as it is
94 * 6-ch: front/clfe/surr
95 * 8-ch: front/clfe/rear/side|fc
96 */
reorder_outputs(unsigned int nums,hda_nid_t * pins)97 static void reorder_outputs(unsigned int nums, hda_nid_t *pins)
98 {
99 hda_nid_t nid;
100
101 switch (nums) {
102 case 3:
103 case 4:
104 nid = pins[1];
105 pins[1] = pins[2];
106 pins[2] = nid;
107 break;
108 }
109 }
110
111 /* check whether the given pin has a proper pin I/O capability bit */
check_pincap_validity(struct hda_codec * codec,hda_nid_t pin,unsigned int dev)112 static bool check_pincap_validity(struct hda_codec *codec, hda_nid_t pin,
113 unsigned int dev)
114 {
115 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
116
117 /* some old hardware don't return the proper pincaps */
118 if (!pincap)
119 return true;
120
121 switch (dev) {
122 case AC_JACK_LINE_OUT:
123 case AC_JACK_SPEAKER:
124 case AC_JACK_HP_OUT:
125 case AC_JACK_SPDIF_OUT:
126 case AC_JACK_DIG_OTHER_OUT:
127 return !!(pincap & AC_PINCAP_OUT);
128 default:
129 return !!(pincap & AC_PINCAP_IN);
130 }
131 }
132
can_be_headset_mic(struct hda_codec * codec,struct auto_pin_cfg_item * item,int seq_number)133 static bool can_be_headset_mic(struct hda_codec *codec,
134 struct auto_pin_cfg_item *item,
135 int seq_number)
136 {
137 int attr;
138 unsigned int def_conf;
139 if (item->type != AUTO_PIN_MIC)
140 return false;
141
142 if (item->is_headset_mic || item->is_headphone_mic)
143 return false; /* Already assigned */
144
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)
148 return false;
149
150 if (seq_number >= 0) {
151 int seq = get_defcfg_sequence(def_conf);
152 if (seq != seq_number)
153 return false;
154 }
155
156 return true;
157 }
158
159 /*
160 * Parse all pin widgets and store the useful pin nids to cfg
161 *
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, ...
165 *
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.
171 *
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,
174 * respectively.
175 */
snd_hda_parse_pin_defcfg(struct hda_codec * codec,struct auto_pin_cfg * cfg,const hda_nid_t * ignore_nids,unsigned int cond_flags)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)
180 {
181 hda_nid_t nid;
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)];
186 int i;
187
188 if (!snd_hda_get_int_hint(codec, "parser_flags", &i))
189 cond_flags = i;
190
191 memset(cfg, 0, sizeof(*cfg));
192
193 memset(line_out, 0, sizeof(line_out));
194 memset(speaker_out, 0, sizeof(speaker_out));
195 memset(hp_out, 0, sizeof(hp_out));
196 assoc_line_out = 0;
197
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;
203
204 /* read all default configuration for pin complex */
205 if (wid_type != AC_WID_PIN)
206 continue;
207 /* ignore the given nids (e.g. pc-beep returns error) */
208 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
209 continue;
210
211 def_conf = snd_hda_codec_get_pincfg(codec, nid);
212 conn = get_defcfg_connect(def_conf);
213 if (conn == AC_JACK_PORT_NONE)
214 continue;
215 loc = get_defcfg_location(def_conf);
216 dev = get_defcfg_device(def_conf);
217
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;
223 }
224
225 if (!check_pincap_validity(codec, nid, dev))
226 continue;
227
228 switch (dev) {
229 case AC_JACK_LINE_OUT:
230 seq = get_defcfg_sequence(def_conf);
231 assoc = get_defcfg_association(def_conf);
232
233 if (!(wid_caps & AC_WCAP_STEREO))
234 if (!cfg->mono_out_pin)
235 cfg->mono_out_pin = nid;
236 if (!assoc)
237 continue;
238 if (!assoc_line_out)
239 assoc_line_out = assoc;
240 else if (assoc_line_out != assoc) {
241 codec_info(codec,
242 "ignore pin 0x%x with mismatching assoc# 0x%x vs 0x%x\n",
243 nid, assoc, assoc_line_out);
244 continue;
245 }
246 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins)) {
247 codec_info(codec,
248 "ignore pin 0x%x, too many assigned pins\n",
249 nid);
250 continue;
251 }
252 line_out[cfg->line_outs].pin = nid;
253 line_out[cfg->line_outs].seq = seq;
254 cfg->line_outs++;
255 break;
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)) {
260 codec_info(codec,
261 "ignore pin 0x%x, too many assigned pins\n",
262 nid);
263 continue;
264 }
265 speaker_out[cfg->speaker_outs].pin = nid;
266 speaker_out[cfg->speaker_outs].seq = (assoc << 4) | seq;
267 cfg->speaker_outs++;
268 break;
269 case AC_JACK_HP_OUT:
270 seq = get_defcfg_sequence(def_conf);
271 assoc = get_defcfg_association(def_conf);
272 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins)) {
273 codec_info(codec,
274 "ignore pin 0x%x, too many assigned pins\n",
275 nid);
276 continue;
277 }
278 hp_out[cfg->hp_outs].pin = nid;
279 hp_out[cfg->hp_outs].seq = (assoc << 4) | seq;
280 cfg->hp_outs++;
281 break;
282 case AC_JACK_MIC_IN:
283 add_auto_cfg_input_pin(codec, cfg, nid, AUTO_PIN_MIC);
284 break;
285 case AC_JACK_LINE_IN:
286 add_auto_cfg_input_pin(codec, cfg, nid, AUTO_PIN_LINE_IN);
287 break;
288 case AC_JACK_CD:
289 add_auto_cfg_input_pin(codec, cfg, nid, AUTO_PIN_CD);
290 break;
291 case AC_JACK_AUX:
292 add_auto_cfg_input_pin(codec, cfg, nid, AUTO_PIN_AUX);
293 break;
294 case AC_JACK_SPDIF_OUT:
295 case AC_JACK_DIG_OTHER_OUT:
296 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins)) {
297 codec_info(codec,
298 "ignore pin 0x%x, too many assigned pins\n",
299 nid);
300 continue;
301 }
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;
306 cfg->dig_outs++;
307 break;
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;
313 else
314 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
315 break;
316 }
317 }
318
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;
326 hsmic = false;
327 } else if (hpmic && can_be_headset_mic(codec, &cfg->inputs[i], 0xd)) {
328 cfg->inputs[i].is_headphone_mic = 1;
329 hpmic = false;
330 }
331
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))
335 continue;
336 if (hsmic) {
337 cfg->inputs[i].is_headset_mic = 1;
338 hsmic = false;
339 } else if (hpmic) {
340 cfg->inputs[i].is_headphone_mic = 1;
341 hpmic = false;
342 }
343 }
344
345 if (hsmic)
346 codec_dbg(codec, "Told to look for a headset mic, but didn't find any.\n");
347 if (hpmic)
348 codec_dbg(codec, "Told to look for a headphone mic, but didn't find any.\n");
349 }
350
351 /* FIX-UP:
352 * If no line-out is defined but multiple HPs are found,
353 * some of them might be the real line-outs.
354 */
355 if (!cfg->line_outs && cfg->hp_outs > 1 &&
356 !(cond_flags & HDA_PINCFG_NO_HP_FIXUP)) {
357 int i = 0;
358 while (i < cfg->hp_outs) {
359 /* The real HPs should have the sequence 0x0f */
360 if ((hp_out[i].seq & 0x0f) == 0x0f) {
361 i++;
362 continue;
363 }
364 /* Move it to the line-out table */
365 line_out[cfg->line_outs++] = hp_out[i];
366 cfg->hp_outs--;
367 memmove(hp_out + i, hp_out + i + 1,
368 sizeof(hp_out[0]) * (cfg->hp_outs - i));
369 }
370 memset(hp_out + cfg->hp_outs, 0,
371 sizeof(hp_out[0]) * (AUTO_CFG_MAX_OUTS - cfg->hp_outs));
372 if (!cfg->hp_outs)
373 cfg->line_out_type = AUTO_PIN_HP_OUT;
374
375 }
376
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,
380 cfg->speaker_outs);
381 sort_pins_by_sequence(cfg->hp_pins, hp_out, cfg->hp_outs);
382
383 /*
384 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
385 * as a primary output
386 */
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));
400 cfg->hp_outs = 0;
401 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
402 cfg->line_out_type = AUTO_PIN_HP_OUT;
403 }
404 }
405
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);
409
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);
413
414 /*
415 * debug prints of the parsed results
416 */
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);
433 if (cfg->dig_outs)
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),
440 cfg->inputs[i].pin);
441 }
442 if (cfg->dig_in_pin)
443 codec_info(codec, " dig-in=0x%x\n", cfg->dig_in_pin);
444
445 return 0;
446 }
447 EXPORT_SYMBOL_GPL(snd_hda_parse_pin_defcfg);
448
449 /**
450 * snd_hda_get_input_pin_attr - Get the input pin attribute from pin config
451 * @def_conf: pin configuration value
452 *
453 * Guess the input pin attribute (INPUT_PIN_ATTR_XXX) from the given
454 * default pin configuration value.
455 */
snd_hda_get_input_pin_attr(unsigned int def_conf)456 int snd_hda_get_input_pin_attr(unsigned int def_conf)
457 {
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;
474 }
475 EXPORT_SYMBOL_GPL(snd_hda_get_input_pin_attr);
476
477 /**
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
481 * @pin: the pin NID
482 * @check_location: flag to add the jack location prefix
483 *
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".
487 */
hda_get_input_pin_label(struct hda_codec * codec,const struct auto_pin_cfg_item * item,hda_nid_t pin,bool check_location)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)
491 {
492 unsigned int def_conf;
493 static const char * const mic_names[] = {
494 "Internal Mic", "Dock Mic", "Mic", "Rear Mic", "Front Mic"
495 };
496 int attr;
497
498 def_conf = snd_hda_codec_get_pincfg(codec, pin);
499
500 switch (get_defcfg_device(def_conf)) {
501 case AC_JACK_MIC_IN:
502 if (item && item->is_headset_mic)
503 return "Headset Mic";
504 if (item && item->is_headphone_mic)
505 return "Headphone Mic";
506 if (!check_location)
507 return "Mic";
508 attr = snd_hda_get_input_pin_attr(def_conf);
509 if (!attr)
510 return "None";
511 return mic_names[attr - 1];
512 case AC_JACK_LINE_IN:
513 if (!check_location)
514 return "Line";
515 attr = snd_hda_get_input_pin_attr(def_conf);
516 if (!attr)
517 return "None";
518 if (attr == INPUT_PIN_ATTR_DOCK)
519 return "Dock Line";
520 return "Line";
521 case AC_JACK_AUX:
522 return "Aux";
523 case AC_JACK_CD:
524 return "CD";
525 case AC_JACK_SPDIF_IN:
526 return "SPDIF In";
527 case AC_JACK_DIG_OTHER_IN:
528 return "Digital In";
529 case AC_JACK_HP_OUT:
530 return "Headphone Mic";
531 default:
532 return "Misc";
533 }
534 }
535
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.
539 */
check_mic_location_need(struct hda_codec * codec,const struct auto_pin_cfg * cfg,int input)540 static int check_mic_location_need(struct hda_codec *codec,
541 const struct auto_pin_cfg *cfg,
542 int input)
543 {
544 unsigned int defc;
545 int i, attr, attr2;
546
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)
551 return 1;
552
553 attr = 0;
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 */
560 attr = attr2;
561 }
562 }
563 return 0;
564 }
565
566 /**
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
571 *
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
575 * possible.
576 */
hda_get_autocfg_input_label(struct hda_codec * codec,const struct auto_pin_cfg * cfg,int input)577 const char *hda_get_autocfg_input_label(struct hda_codec *codec,
578 const struct auto_pin_cfg *cfg,
579 int input)
580 {
581 int type = cfg->inputs[input].type;
582 int has_multiple_pins = 0;
583
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,
591 has_multiple_pins);
592 }
593 EXPORT_SYMBOL_GPL(hda_get_autocfg_input_label);
594
595 /* return the position of NID in the list, or -1 if not found */
find_idx_in_nid_list(hda_nid_t nid,const hda_nid_t * list,int nums)596 static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
597 {
598 int i;
599 for (i = 0; i < nums; i++)
600 if (list[i] == nid)
601 return i;
602 return -1;
603 }
604
605 /* get a unique suffix or an index number */
check_output_sfx(hda_nid_t nid,const hda_nid_t * pins,int num_pins,int * indexp)606 static const char *check_output_sfx(hda_nid_t nid, const hda_nid_t *pins,
607 int num_pins, int *indexp)
608 {
609 static const char * const channel_sfx[] = {
610 " Front", " Surround", " CLFE", " Side"
611 };
612 int i;
613
614 i = find_idx_in_nid_list(nid, pins, num_pins);
615 if (i < 0)
616 return NULL;
617 if (num_pins == 1)
618 return "";
619 if (num_pins > ARRAY_SIZE(channel_sfx)) {
620 if (indexp)
621 *indexp = i;
622 return "";
623 }
624 return channel_sfx[i];
625 }
626
check_output_pfx(struct hda_codec * codec,hda_nid_t nid)627 static const char *check_output_pfx(struct hda_codec *codec, hda_nid_t nid)
628 {
629 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
630 int attr = snd_hda_get_input_pin_attr(def_conf);
631
632 /* check the location */
633 switch (attr) {
634 case INPUT_PIN_ATTR_DOCK:
635 return "Dock ";
636 case INPUT_PIN_ATTR_FRONT:
637 return "Front ";
638 }
639 return "";
640 }
641
get_hp_label_index(struct hda_codec * codec,hda_nid_t nid,const hda_nid_t * pins,int num_pins)642 static int get_hp_label_index(struct hda_codec *codec, hda_nid_t nid,
643 const hda_nid_t *pins, int num_pins)
644 {
645 int i, j, idx = 0;
646
647 const char *pfx = check_output_pfx(codec, nid);
648
649 i = find_idx_in_nid_list(nid, pins, num_pins);
650 if (i < 0)
651 return -1;
652 for (j = 0; j < i; j++)
653 if (pfx == check_output_pfx(codec, pins[j]))
654 idx++;
655
656 return idx;
657 }
658
fill_audio_out_name(struct hda_codec * codec,hda_nid_t nid,const struct auto_pin_cfg * cfg,const char * name,char * label,int maxlen,int * indexp)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,
662 int *indexp)
663 {
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 = "";
667
668 /* handle as a speaker if it's a fixed line-out */
669 if (!strcmp(name, "Line Out") && attr == INPUT_PIN_ATTR_INT)
670 name = "Speaker";
671 pfx = check_output_pfx(codec, nid);
672
673 if (cfg) {
674 /* try to give a unique suffix if needed */
675 sfx = check_output_sfx(nid, cfg->line_out_pins, cfg->line_outs,
676 indexp);
677 if (!sfx)
678 sfx = check_output_sfx(nid, cfg->speaker_pins, cfg->speaker_outs,
679 indexp);
680 if (!sfx) {
681 /* don't add channel suffix for Headphone controls */
682 int idx = get_hp_label_index(codec, nid, cfg->hp_pins,
683 cfg->hp_outs);
684 if (idx >= 0 && indexp)
685 *indexp = idx;
686 sfx = "";
687 }
688 }
689 snprintf(label, maxlen, "%s%s%s", pfx, name, sfx);
690 return 1;
691 }
692
693 #define is_hdmi_cfg(conf) \
694 (get_defcfg_location(conf) == AC_JACK_LOC_HDMI)
695
696 /**
697 * snd_hda_get_pin_label - Get a label for the given I/O pin
698 * @codec: the HDA codec
699 * @nid: pin NID
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)
704 *
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().
708 *
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.
714 */
snd_hda_get_pin_label(struct hda_codec * codec,hda_nid_t nid,const struct auto_pin_cfg * cfg,char * label,int maxlen,int * indexp)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)
718 {
719 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
720 const char *name = NULL;
721 int i;
722 bool hdmi;
723
724 if (indexp)
725 *indexp = 0;
726 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
727 return 0;
728
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);
736 case AC_JACK_HP_OUT:
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";
743 if (cfg && indexp)
744 for (i = 0; i < cfg->dig_outs; i++) {
745 hda_nid_t pin = cfg->dig_out_pins[i];
746 unsigned int c;
747 if (pin == nid)
748 break;
749 c = snd_hda_codec_get_pincfg(codec, pin);
750 if (hdmi == is_hdmi_cfg(c))
751 (*indexp)++;
752 }
753 break;
754 default:
755 if (cfg) {
756 for (i = 0; i < cfg->num_inputs; i++) {
757 if (cfg->inputs[i].pin != nid)
758 continue;
759 name = hda_get_autocfg_input_label(codec, cfg, i);
760 if (name)
761 break;
762 }
763 }
764 if (!name)
765 name = hda_get_input_pin_label(codec, NULL, nid, true);
766 break;
767 }
768 if (!name)
769 return 0;
770 strlcpy(label, name, maxlen);
771 return 1;
772 }
773 EXPORT_SYMBOL_GPL(snd_hda_get_pin_label);
774
775 /**
776 * snd_hda_add_verbs - Add verbs to the init list
777 * @codec: the HDA codec
778 * @list: zero-terminated verb list to add
779 *
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().
782 */
snd_hda_add_verbs(struct hda_codec * codec,const struct hda_verb * list)783 int snd_hda_add_verbs(struct hda_codec *codec,
784 const struct hda_verb *list)
785 {
786 const struct hda_verb **v;
787 v = snd_array_new(&codec->verbs);
788 if (!v)
789 return -ENOMEM;
790 *v = list;
791 return 0;
792 }
793 EXPORT_SYMBOL_GPL(snd_hda_add_verbs);
794
795 /**
796 * snd_hda_apply_verbs - Execute the init verb lists
797 * @codec: the HDA codec
798 */
snd_hda_apply_verbs(struct hda_codec * codec)799 void snd_hda_apply_verbs(struct hda_codec *codec)
800 {
801 int i;
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);
805 }
806 }
807 EXPORT_SYMBOL_GPL(snd_hda_apply_verbs);
808
809 /**
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
813 */
snd_hda_apply_pincfgs(struct hda_codec * codec,const struct hda_pintbl * cfg)814 void snd_hda_apply_pincfgs(struct hda_codec *codec,
815 const struct hda_pintbl *cfg)
816 {
817 for (; cfg->nid; cfg++)
818 snd_hda_codec_set_pincfg(codec, cfg->nid, cfg->val);
819 }
820 EXPORT_SYMBOL_GPL(snd_hda_apply_pincfgs);
821
set_pin_targets(struct hda_codec * codec,const struct hda_pintbl * cfg)822 static void set_pin_targets(struct hda_codec *codec,
823 const struct hda_pintbl *cfg)
824 {
825 for (; cfg->nid; cfg++)
826 snd_hda_set_pin_ctl_cache(codec, cfg->nid, cfg->val);
827 }
828
apply_fixup(struct hda_codec * codec,int id,int action,int depth)829 static void apply_fixup(struct hda_codec *codec, int id, int action, int depth)
830 {
831 const char *modelname = codec->fixup_name;
832
833 while (id >= 0) {
834 const struct hda_fixup *fix = codec->fixup_list + id;
835
836 if (++depth > 10)
837 break;
838 if (fix->chained_before)
839 apply_fixup(codec, fix->chain_id, action, depth + 1);
840
841 switch (fix->type) {
842 case HDA_FIXUP_PINS:
843 if (action != HDA_FIXUP_ACT_PRE_PROBE || !fix->v.pins)
844 break;
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);
848 break;
849 case HDA_FIXUP_VERBS:
850 if (action != HDA_FIXUP_ACT_PROBE || !fix->v.verbs)
851 break;
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);
855 break;
856 case HDA_FIXUP_FUNC:
857 if (!fix->v.func)
858 break;
859 codec_dbg(codec, "%s: Apply fix-func for %s\n",
860 codec->core.chip_name, modelname);
861 fix->v.func(codec, fix, action);
862 break;
863 case HDA_FIXUP_PINCTLS:
864 if (action != HDA_FIXUP_ACT_PROBE || !fix->v.pins)
865 break;
866 codec_dbg(codec, "%s: Apply pinctl for %s\n",
867 codec->core.chip_name, modelname);
868 set_pin_targets(codec, fix->v.pins);
869 break;
870 default:
871 codec_err(codec, "%s: Invalid fixup type %d\n",
872 codec->core.chip_name, fix->type);
873 break;
874 }
875 if (!fix->chained || fix->chained_before)
876 break;
877 id = fix->chain_id;
878 }
879 }
880
881 /**
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)
885 */
snd_hda_apply_fixup(struct hda_codec * codec,int action)886 void snd_hda_apply_fixup(struct hda_codec *codec, int action)
887 {
888 if (codec->fixup_list)
889 apply_fixup(codec, codec->fixup_id, action, 0);
890 }
891 EXPORT_SYMBOL_GPL(snd_hda_apply_fixup);
892
893 #define IGNORE_SEQ_ASSOC (~(AC_DEFCFG_SEQUENCE | AC_DEFCFG_DEF_ASSOC))
894
pin_config_match(struct hda_codec * codec,const struct hda_pintbl * pins)895 static bool pin_config_match(struct hda_codec *codec,
896 const struct hda_pintbl *pins)
897 {
898 int i;
899
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;
903 u32 cfg = pin->cfg;
904 const struct hda_pintbl *t_pins;
905 int found;
906
907 t_pins = pins;
908 found = 0;
909 for (; t_pins->nid; t_pins++) {
910 if (t_pins->nid == nid) {
911 found = 1;
912 if ((t_pins->val & IGNORE_SEQ_ASSOC) == (cfg & IGNORE_SEQ_ASSOC))
913 break;
914 else if ((cfg & 0xf0000000) == 0x40000000 && (t_pins->val & 0xf0000000) == 0x40000000)
915 break;
916 else
917 return false;
918 }
919 }
920 if (!found && (cfg & 0xf0000000) != 0x40000000)
921 return false;
922 }
923
924 return true;
925 }
926
927 /**
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
932 */
snd_hda_pick_pin_fixup(struct hda_codec * codec,const struct snd_hda_pin_quirk * pin_quirk,const struct hda_fixup * fixlist)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)
936 {
937 const struct snd_hda_pin_quirk *pq;
938
939 if (codec->fixup_id != HDA_FIXUP_ID_NOT_SET)
940 return;
941
942 for (pq = pin_quirk; pq->subvendor; pq++) {
943 if ((codec->core.subsystem_id & 0xffff0000) != (pq->subvendor << 16))
944 continue;
945 if (codec->core.vendor_id != pq->codec)
946 continue;
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);
953 #endif
954 codec->fixup_list = fixlist;
955 return;
956 }
957 }
958 }
959 EXPORT_SYMBOL_GPL(snd_hda_pick_pin_fixup);
960
961 /**
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
967 *
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.
971 *
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.
975 */
snd_hda_pick_fixup(struct hda_codec * codec,const struct hda_model_fixup * models,const struct snd_pci_quirk * quirk,const struct hda_fixup * fixlist)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)
980 {
981 const struct snd_pci_quirk *q;
982 int id = HDA_FIXUP_ID_NOT_SET;
983 const char *name = NULL;
984
985 if (codec->fixup_id != HDA_FIXUP_ID_NOT_SET)
986 return;
987
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);
995 return;
996 }
997
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);
1006 return;
1007 }
1008 models++;
1009 }
1010 }
1011 if (quirk) {
1012 q = snd_pci_quirk_lookup(codec->bus->pci, quirk);
1013 if (q) {
1014 id = q->value;
1015 #ifdef CONFIG_SND_DEBUG_VERBOSE
1016 name = q->name;
1017 codec_dbg(codec, "%s: picked fixup %s (PCI SSID%s)\n",
1018 codec->core.chip_name, name, q->subdevice_mask ? "" : " - vendor generic");
1019 #endif
1020 }
1021 }
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)) {
1028 id = q->value;
1029 #ifdef CONFIG_SND_DEBUG_VERBOSE
1030 name = q->name;
1031 codec_dbg(codec, "%s: picked fixup %s (codec SSID)\n",
1032 codec->core.chip_name, name);
1033 #endif
1034 break;
1035 }
1036 }
1037 }
1038
1039 codec->fixup_id = id;
1040 if (id >= 0) {
1041 codec->fixup_list = fixlist;
1042 codec->fixup_name = name;
1043 }
1044 }
1045 EXPORT_SYMBOL_GPL(snd_hda_pick_fixup);
1046