]>
Commit | Line | Data |
---|---|---|
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 | ||
20 | #define SFX "hda_codec: " | |
21 | ||
128bc4ba TI |
22 | /* |
23 | * Helper for automatic pin configuration | |
24 | */ | |
25 | ||
26 | static int is_in_nid_list(hda_nid_t nid, const hda_nid_t *list) | |
27 | { | |
28 | for (; *list; list++) | |
29 | if (*list == nid) | |
30 | return 1; | |
31 | return 0; | |
32 | } | |
33 | ||
b9030a00 TI |
34 | /* a pair of input pin and its sequence */ |
35 | struct auto_out_pin { | |
36 | hda_nid_t pin; | |
37 | short seq; | |
38 | }; | |
39 | ||
40 | static int compare_seq(const void *ap, const void *bp) | |
41 | { | |
42 | const struct auto_out_pin *a = ap; | |
43 | const struct auto_out_pin *b = bp; | |
44 | return (int)(a->seq - b->seq); | |
45 | } | |
128bc4ba TI |
46 | |
47 | /* | |
48 | * Sort an associated group of pins according to their sequence numbers. | |
b9030a00 | 49 | * then store it to a pin array. |
128bc4ba | 50 | */ |
b9030a00 | 51 | static void sort_pins_by_sequence(hda_nid_t *pins, struct auto_out_pin *list, |
128bc4ba TI |
52 | int num_pins) |
53 | { | |
b9030a00 TI |
54 | int i; |
55 | sort(list, num_pins, sizeof(list[0]), compare_seq, NULL); | |
56 | for (i = 0; i < num_pins; i++) | |
57 | pins[i] = list[i].pin; | |
128bc4ba TI |
58 | } |
59 | ||
60 | ||
61 | /* add the found input-pin to the cfg->inputs[] table */ | |
62 | static void add_auto_cfg_input_pin(struct auto_pin_cfg *cfg, hda_nid_t nid, | |
63 | int type) | |
64 | { | |
65 | if (cfg->num_inputs < AUTO_CFG_MAX_INS) { | |
66 | cfg->inputs[cfg->num_inputs].pin = nid; | |
67 | cfg->inputs[cfg->num_inputs].type = type; | |
68 | cfg->num_inputs++; | |
69 | } | |
70 | } | |
71 | ||
b9030a00 | 72 | static 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; | |
76 | return (int)(a->type - b->type); | |
128bc4ba TI |
77 | } |
78 | ||
79 | /* Reorder the surround channels | |
80 | * ALSA sequence is front/surr/clfe/side | |
81 | * HDA sequence is: | |
82 | * 4-ch: front/surr => OK as it is | |
83 | * 6-ch: front/clfe/surr | |
84 | * 8-ch: front/clfe/rear/side|fc | |
85 | */ | |
86 | static void reorder_outputs(unsigned int nums, hda_nid_t *pins) | |
87 | { | |
88 | hda_nid_t nid; | |
89 | ||
90 | switch (nums) { | |
91 | case 3: | |
92 | case 4: | |
93 | nid = pins[1]; | |
94 | pins[1] = pins[2]; | |
95 | pins[2] = nid; | |
96 | break; | |
97 | } | |
98 | } | |
99 | ||
100 | /* | |
101 | * Parse all pin widgets and store the useful pin nids to cfg | |
102 | * | |
103 | * The number of line-outs or any primary output is stored in line_outs, | |
104 | * and the corresponding output pins are assigned to line_out_pins[], | |
105 | * in the order of front, rear, CLFE, side, ... | |
106 | * | |
107 | * If more extra outputs (speaker and headphone) are found, the pins are | |
108 | * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack | |
109 | * is detected, one of speaker of HP pins is assigned as the primary | |
110 | * output, i.e. to line_out_pins[0]. So, line_outs is always positive | |
111 | * if any analog output exists. | |
112 | * | |
113 | * The analog input pins are assigned to inputs array. | |
114 | * The digital input/output pins are assigned to dig_in_pin and dig_out_pin, | |
115 | * respectively. | |
116 | */ | |
117 | int snd_hda_parse_pin_defcfg(struct hda_codec *codec, | |
118 | struct auto_pin_cfg *cfg, | |
119 | const hda_nid_t *ignore_nids, | |
120 | unsigned int cond_flags) | |
121 | { | |
122 | hda_nid_t nid, end_nid; | |
123 | short seq, assoc_line_out; | |
b9030a00 TI |
124 | struct auto_out_pin line_out[ARRAY_SIZE(cfg->line_out_pins)]; |
125 | struct auto_out_pin speaker_out[ARRAY_SIZE(cfg->speaker_pins)]; | |
126 | struct auto_out_pin hp_out[ARRAY_SIZE(cfg->hp_pins)]; | |
128bc4ba TI |
127 | int i; |
128 | ||
129 | memset(cfg, 0, sizeof(*cfg)); | |
130 | ||
b9030a00 TI |
131 | memset(line_out, 0, sizeof(line_out)); |
132 | memset(speaker_out, 0, sizeof(speaker_out)); | |
133 | memset(hp_out, 0, sizeof(hp_out)); | |
128bc4ba TI |
134 | assoc_line_out = 0; |
135 | ||
128bc4ba TI |
136 | end_nid = codec->start_nid + codec->num_nodes; |
137 | for (nid = codec->start_nid; nid < end_nid; nid++) { | |
138 | unsigned int wid_caps = get_wcaps(codec, nid); | |
139 | unsigned int wid_type = get_wcaps_type(wid_caps); | |
140 | unsigned int def_conf; | |
141 | short assoc, loc, conn, dev; | |
142 | ||
143 | /* read all default configuration for pin complex */ | |
144 | if (wid_type != AC_WID_PIN) | |
145 | continue; | |
146 | /* ignore the given nids (e.g. pc-beep returns error) */ | |
147 | if (ignore_nids && is_in_nid_list(nid, ignore_nids)) | |
148 | continue; | |
149 | ||
150 | def_conf = snd_hda_codec_get_pincfg(codec, nid); | |
128bc4ba TI |
151 | conn = get_defcfg_connect(def_conf); |
152 | if (conn == AC_JACK_PORT_NONE) | |
153 | continue; | |
154 | loc = get_defcfg_location(def_conf); | |
155 | dev = get_defcfg_device(def_conf); | |
156 | ||
157 | /* workaround for buggy BIOS setups */ | |
158 | if (dev == AC_JACK_LINE_OUT) { | |
fb690cf5 TI |
159 | if (conn == AC_JACK_PORT_FIXED || |
160 | conn == AC_JACK_PORT_BOTH) | |
128bc4ba TI |
161 | dev = AC_JACK_SPEAKER; |
162 | } | |
163 | ||
164 | switch (dev) { | |
165 | case AC_JACK_LINE_OUT: | |
166 | seq = get_defcfg_sequence(def_conf); | |
167 | assoc = get_defcfg_association(def_conf); | |
168 | ||
169 | if (!(wid_caps & AC_WCAP_STEREO)) | |
170 | if (!cfg->mono_out_pin) | |
171 | cfg->mono_out_pin = nid; | |
172 | if (!assoc) | |
173 | continue; | |
174 | if (!assoc_line_out) | |
175 | assoc_line_out = assoc; | |
176 | else if (assoc_line_out != assoc) | |
177 | continue; | |
178 | if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins)) | |
179 | continue; | |
b9030a00 TI |
180 | line_out[cfg->line_outs].pin = nid; |
181 | line_out[cfg->line_outs].seq = seq; | |
128bc4ba TI |
182 | cfg->line_outs++; |
183 | break; | |
184 | case AC_JACK_SPEAKER: | |
185 | seq = get_defcfg_sequence(def_conf); | |
186 | assoc = get_defcfg_association(def_conf); | |
187 | if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins)) | |
188 | continue; | |
b9030a00 TI |
189 | speaker_out[cfg->speaker_outs].pin = nid; |
190 | speaker_out[cfg->speaker_outs].seq = (assoc << 4) | seq; | |
128bc4ba TI |
191 | cfg->speaker_outs++; |
192 | break; | |
193 | case AC_JACK_HP_OUT: | |
194 | seq = get_defcfg_sequence(def_conf); | |
195 | assoc = get_defcfg_association(def_conf); | |
196 | if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins)) | |
197 | continue; | |
b9030a00 TI |
198 | hp_out[cfg->hp_outs].pin = nid; |
199 | hp_out[cfg->hp_outs].seq = (assoc << 4) | seq; | |
128bc4ba TI |
200 | cfg->hp_outs++; |
201 | break; | |
202 | case AC_JACK_MIC_IN: | |
203 | add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_MIC); | |
204 | break; | |
205 | case AC_JACK_LINE_IN: | |
206 | add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_LINE_IN); | |
207 | break; | |
208 | case AC_JACK_CD: | |
209 | add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_CD); | |
210 | break; | |
211 | case AC_JACK_AUX: | |
212 | add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_AUX); | |
213 | break; | |
214 | case AC_JACK_SPDIF_OUT: | |
215 | case AC_JACK_DIG_OTHER_OUT: | |
216 | if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins)) | |
217 | continue; | |
218 | cfg->dig_out_pins[cfg->dig_outs] = nid; | |
219 | cfg->dig_out_type[cfg->dig_outs] = | |
220 | (loc == AC_JACK_LOC_HDMI) ? | |
221 | HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF; | |
222 | cfg->dig_outs++; | |
223 | break; | |
224 | case AC_JACK_SPDIF_IN: | |
225 | case AC_JACK_DIG_OTHER_IN: | |
226 | cfg->dig_in_pin = nid; | |
227 | if (loc == AC_JACK_LOC_HDMI) | |
228 | cfg->dig_in_type = HDA_PCM_TYPE_HDMI; | |
229 | else | |
230 | cfg->dig_in_type = HDA_PCM_TYPE_SPDIF; | |
231 | break; | |
232 | } | |
233 | } | |
234 | ||
235 | /* FIX-UP: | |
236 | * If no line-out is defined but multiple HPs are found, | |
237 | * some of them might be the real line-outs. | |
238 | */ | |
239 | if (!cfg->line_outs && cfg->hp_outs > 1 && | |
240 | !(cond_flags & HDA_PINCFG_NO_HP_FIXUP)) { | |
241 | int i = 0; | |
242 | while (i < cfg->hp_outs) { | |
243 | /* The real HPs should have the sequence 0x0f */ | |
b9030a00 | 244 | if ((hp_out[i].seq & 0x0f) == 0x0f) { |
128bc4ba TI |
245 | i++; |
246 | continue; | |
247 | } | |
248 | /* Move it to the line-out table */ | |
b9030a00 | 249 | line_out[cfg->line_outs++] = hp_out[i]; |
128bc4ba | 250 | cfg->hp_outs--; |
b9030a00 TI |
251 | memmove(hp_out + i, hp_out + i + 1, |
252 | sizeof(hp_out[0]) * (cfg->hp_outs - i)); | |
128bc4ba | 253 | } |
b9030a00 TI |
254 | memset(hp_out + cfg->hp_outs, 0, |
255 | sizeof(hp_out[0]) * (AUTO_CFG_MAX_OUTS - cfg->hp_outs)); | |
128bc4ba TI |
256 | if (!cfg->hp_outs) |
257 | cfg->line_out_type = AUTO_PIN_HP_OUT; | |
258 | ||
259 | } | |
260 | ||
261 | /* sort by sequence */ | |
b9030a00 TI |
262 | sort_pins_by_sequence(cfg->line_out_pins, line_out, cfg->line_outs); |
263 | sort_pins_by_sequence(cfg->speaker_pins, speaker_out, | |
128bc4ba | 264 | cfg->speaker_outs); |
b9030a00 | 265 | sort_pins_by_sequence(cfg->hp_pins, hp_out, cfg->hp_outs); |
128bc4ba TI |
266 | |
267 | /* | |
268 | * FIX-UP: if no line-outs are detected, try to use speaker or HP pin | |
269 | * as a primary output | |
270 | */ | |
271 | if (!cfg->line_outs && | |
272 | !(cond_flags & HDA_PINCFG_NO_LO_FIXUP)) { | |
273 | if (cfg->speaker_outs) { | |
274 | cfg->line_outs = cfg->speaker_outs; | |
275 | memcpy(cfg->line_out_pins, cfg->speaker_pins, | |
276 | sizeof(cfg->speaker_pins)); | |
277 | cfg->speaker_outs = 0; | |
278 | memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins)); | |
279 | cfg->line_out_type = AUTO_PIN_SPEAKER_OUT; | |
280 | } else if (cfg->hp_outs) { | |
281 | cfg->line_outs = cfg->hp_outs; | |
282 | memcpy(cfg->line_out_pins, cfg->hp_pins, | |
283 | sizeof(cfg->hp_pins)); | |
284 | cfg->hp_outs = 0; | |
285 | memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins)); | |
286 | cfg->line_out_type = AUTO_PIN_HP_OUT; | |
287 | } | |
288 | } | |
289 | ||
290 | reorder_outputs(cfg->line_outs, cfg->line_out_pins); | |
291 | reorder_outputs(cfg->hp_outs, cfg->hp_pins); | |
292 | reorder_outputs(cfg->speaker_outs, cfg->speaker_pins); | |
293 | ||
b9030a00 TI |
294 | /* sort inputs in the order of AUTO_PIN_* type */ |
295 | sort(cfg->inputs, cfg->num_inputs, sizeof(cfg->inputs[0]), | |
296 | compare_input_type, NULL); | |
128bc4ba TI |
297 | |
298 | /* | |
299 | * debug prints of the parsed results | |
300 | */ | |
301 | snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x) type:%s\n", | |
302 | cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1], | |
303 | cfg->line_out_pins[2], cfg->line_out_pins[3], | |
304 | cfg->line_out_pins[4], | |
305 | cfg->line_out_type == AUTO_PIN_HP_OUT ? "hp" : | |
306 | (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT ? | |
307 | "speaker" : "line")); | |
308 | snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n", | |
309 | cfg->speaker_outs, cfg->speaker_pins[0], | |
310 | cfg->speaker_pins[1], cfg->speaker_pins[2], | |
311 | cfg->speaker_pins[3], cfg->speaker_pins[4]); | |
312 | snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n", | |
313 | cfg->hp_outs, cfg->hp_pins[0], | |
314 | cfg->hp_pins[1], cfg->hp_pins[2], | |
315 | cfg->hp_pins[3], cfg->hp_pins[4]); | |
316 | snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin); | |
317 | if (cfg->dig_outs) | |
318 | snd_printd(" dig-out=0x%x/0x%x\n", | |
319 | cfg->dig_out_pins[0], cfg->dig_out_pins[1]); | |
709aea6b | 320 | snd_printd(" inputs:\n"); |
128bc4ba | 321 | for (i = 0; i < cfg->num_inputs; i++) { |
709aea6b | 322 | snd_printd(" %s=0x%x\n", |
128bc4ba TI |
323 | hda_get_autocfg_input_label(codec, cfg, i), |
324 | cfg->inputs[i].pin); | |
325 | } | |
128bc4ba TI |
326 | if (cfg->dig_in_pin) |
327 | snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin); | |
328 | ||
329 | return 0; | |
330 | } | |
331 | EXPORT_SYMBOL_HDA(snd_hda_parse_pin_defcfg); | |
332 | ||
333 | int snd_hda_get_input_pin_attr(unsigned int def_conf) | |
334 | { | |
335 | unsigned int loc = get_defcfg_location(def_conf); | |
336 | unsigned int conn = get_defcfg_connect(def_conf); | |
337 | if (conn == AC_JACK_PORT_NONE) | |
338 | return INPUT_PIN_ATTR_UNUSED; | |
339 | /* Windows may claim the internal mic to be BOTH, too */ | |
340 | if (conn == AC_JACK_PORT_FIXED || conn == AC_JACK_PORT_BOTH) | |
341 | return INPUT_PIN_ATTR_INT; | |
342 | if ((loc & 0x30) == AC_JACK_LOC_INTERNAL) | |
343 | return INPUT_PIN_ATTR_INT; | |
344 | if ((loc & 0x30) == AC_JACK_LOC_SEPARATE) | |
345 | return INPUT_PIN_ATTR_DOCK; | |
346 | if (loc == AC_JACK_LOC_REAR) | |
347 | return INPUT_PIN_ATTR_REAR; | |
348 | if (loc == AC_JACK_LOC_FRONT) | |
349 | return INPUT_PIN_ATTR_FRONT; | |
350 | return INPUT_PIN_ATTR_NORMAL; | |
351 | } | |
352 | EXPORT_SYMBOL_HDA(snd_hda_get_input_pin_attr); | |
353 | ||
354 | /** | |
355 | * hda_get_input_pin_label - Give a label for the given input pin | |
356 | * | |
357 | * When check_location is true, the function checks the pin location | |
358 | * for mic and line-in pins, and set an appropriate prefix like "Front", | |
359 | * "Rear", "Internal". | |
360 | */ | |
361 | ||
362 | static const char *hda_get_input_pin_label(struct hda_codec *codec, | |
363 | hda_nid_t pin, bool check_location) | |
364 | { | |
365 | unsigned int def_conf; | |
366 | static const char * const mic_names[] = { | |
5ec16d12 | 367 | "Internal Mic", "Dock Mic", "Mic", "Rear Mic", "Front Mic" |
128bc4ba TI |
368 | }; |
369 | int attr; | |
370 | ||
371 | def_conf = snd_hda_codec_get_pincfg(codec, pin); | |
372 | ||
373 | switch (get_defcfg_device(def_conf)) { | |
374 | case AC_JACK_MIC_IN: | |
375 | if (!check_location) | |
376 | return "Mic"; | |
377 | attr = snd_hda_get_input_pin_attr(def_conf); | |
378 | if (!attr) | |
379 | return "None"; | |
380 | return mic_names[attr - 1]; | |
381 | case AC_JACK_LINE_IN: | |
382 | if (!check_location) | |
383 | return "Line"; | |
384 | attr = snd_hda_get_input_pin_attr(def_conf); | |
385 | if (!attr) | |
386 | return "None"; | |
387 | if (attr == INPUT_PIN_ATTR_DOCK) | |
388 | return "Dock Line"; | |
389 | return "Line"; | |
390 | case AC_JACK_AUX: | |
391 | return "Aux"; | |
392 | case AC_JACK_CD: | |
393 | return "CD"; | |
394 | case AC_JACK_SPDIF_IN: | |
395 | return "SPDIF In"; | |
396 | case AC_JACK_DIG_OTHER_IN: | |
397 | return "Digital In"; | |
398 | default: | |
399 | return "Misc"; | |
400 | } | |
401 | } | |
402 | ||
403 | /* Check whether the location prefix needs to be added to the label. | |
404 | * If all mic-jacks are in the same location (e.g. rear panel), we don't | |
405 | * have to put "Front" prefix to each label. In such a case, returns false. | |
406 | */ | |
407 | static int check_mic_location_need(struct hda_codec *codec, | |
408 | const struct auto_pin_cfg *cfg, | |
409 | int input) | |
410 | { | |
411 | unsigned int defc; | |
412 | int i, attr, attr2; | |
413 | ||
414 | defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[input].pin); | |
415 | attr = snd_hda_get_input_pin_attr(defc); | |
416 | /* for internal or docking mics, we need locations */ | |
417 | if (attr <= INPUT_PIN_ATTR_NORMAL) | |
418 | return 1; | |
419 | ||
420 | attr = 0; | |
421 | for (i = 0; i < cfg->num_inputs; i++) { | |
422 | defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[i].pin); | |
423 | attr2 = snd_hda_get_input_pin_attr(defc); | |
424 | if (attr2 >= INPUT_PIN_ATTR_NORMAL) { | |
425 | if (attr && attr != attr2) | |
426 | return 1; /* different locations found */ | |
427 | attr = attr2; | |
428 | } | |
429 | } | |
430 | return 0; | |
431 | } | |
432 | ||
433 | /** | |
434 | * hda_get_autocfg_input_label - Get a label for the given input | |
435 | * | |
436 | * Get a label for the given input pin defined by the autocfg item. | |
437 | * Unlike hda_get_input_pin_label(), this function checks all inputs | |
438 | * defined in autocfg and avoids the redundant mic/line prefix as much as | |
439 | * possible. | |
440 | */ | |
441 | const char *hda_get_autocfg_input_label(struct hda_codec *codec, | |
442 | const struct auto_pin_cfg *cfg, | |
443 | int input) | |
444 | { | |
445 | int type = cfg->inputs[input].type; | |
446 | int has_multiple_pins = 0; | |
447 | ||
448 | if ((input > 0 && cfg->inputs[input - 1].type == type) || | |
449 | (input < cfg->num_inputs - 1 && cfg->inputs[input + 1].type == type)) | |
450 | has_multiple_pins = 1; | |
451 | if (has_multiple_pins && type == AUTO_PIN_MIC) | |
452 | has_multiple_pins &= check_mic_location_need(codec, cfg, input); | |
453 | return hda_get_input_pin_label(codec, cfg->inputs[input].pin, | |
454 | has_multiple_pins); | |
455 | } | |
456 | EXPORT_SYMBOL_HDA(hda_get_autocfg_input_label); | |
457 | ||
458 | /* return the position of NID in the list, or -1 if not found */ | |
459 | static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums) | |
460 | { | |
461 | int i; | |
462 | for (i = 0; i < nums; i++) | |
463 | if (list[i] == nid) | |
464 | return i; | |
465 | return -1; | |
466 | } | |
467 | ||
468 | /* get a unique suffix or an index number */ | |
469 | static const char *check_output_sfx(hda_nid_t nid, const hda_nid_t *pins, | |
470 | int num_pins, int *indexp) | |
471 | { | |
472 | static const char * const channel_sfx[] = { | |
473 | " Front", " Surround", " CLFE", " Side" | |
474 | }; | |
475 | int i; | |
476 | ||
477 | i = find_idx_in_nid_list(nid, pins, num_pins); | |
478 | if (i < 0) | |
479 | return NULL; | |
480 | if (num_pins == 1) | |
481 | return ""; | |
482 | if (num_pins > ARRAY_SIZE(channel_sfx)) { | |
483 | if (indexp) | |
484 | *indexp = i; | |
485 | return ""; | |
486 | } | |
487 | return channel_sfx[i]; | |
488 | } | |
489 | ||
eee3ed43 DH |
490 | static const char *check_output_pfx(struct hda_codec *codec, hda_nid_t nid) |
491 | { | |
492 | unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid); | |
493 | int attr = snd_hda_get_input_pin_attr(def_conf); | |
494 | ||
495 | /* check the location */ | |
496 | switch (attr) { | |
497 | case INPUT_PIN_ATTR_DOCK: | |
498 | return "Dock "; | |
499 | case INPUT_PIN_ATTR_FRONT: | |
500 | return "Front "; | |
501 | } | |
502 | return ""; | |
503 | } | |
504 | ||
505 | static int get_hp_label_index(struct hda_codec *codec, hda_nid_t nid, | |
506 | const hda_nid_t *pins, int num_pins) | |
507 | { | |
508 | int i, j, idx = 0; | |
509 | ||
510 | const char *pfx = check_output_pfx(codec, nid); | |
511 | ||
512 | i = find_idx_in_nid_list(nid, pins, num_pins); | |
513 | if (i < 0) | |
514 | return -1; | |
515 | for (j = 0; j < i; j++) | |
516 | if (pfx == check_output_pfx(codec, pins[j])) | |
517 | idx++; | |
518 | ||
519 | return idx; | |
520 | } | |
521 | ||
128bc4ba TI |
522 | static int fill_audio_out_name(struct hda_codec *codec, hda_nid_t nid, |
523 | const struct auto_pin_cfg *cfg, | |
524 | const char *name, char *label, int maxlen, | |
525 | int *indexp) | |
526 | { | |
527 | unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid); | |
528 | int attr = snd_hda_get_input_pin_attr(def_conf); | |
eee3ed43 | 529 | const char *pfx, *sfx = ""; |
128bc4ba TI |
530 | |
531 | /* handle as a speaker if it's a fixed line-out */ | |
532 | if (!strcmp(name, "Line Out") && attr == INPUT_PIN_ATTR_INT) | |
533 | name = "Speaker"; | |
eee3ed43 DH |
534 | pfx = check_output_pfx(codec, nid); |
535 | ||
128bc4ba TI |
536 | if (cfg) { |
537 | /* try to give a unique suffix if needed */ | |
538 | sfx = check_output_sfx(nid, cfg->line_out_pins, cfg->line_outs, | |
539 | indexp); | |
540 | if (!sfx) | |
541 | sfx = check_output_sfx(nid, cfg->speaker_pins, cfg->speaker_outs, | |
542 | indexp); | |
543 | if (!sfx) { | |
544 | /* don't add channel suffix for Headphone controls */ | |
eee3ed43 DH |
545 | int idx = get_hp_label_index(codec, nid, cfg->hp_pins, |
546 | cfg->hp_outs); | |
128bc4ba TI |
547 | if (idx >= 0) |
548 | *indexp = idx; | |
549 | sfx = ""; | |
550 | } | |
551 | } | |
552 | snprintf(label, maxlen, "%s%s%s", pfx, name, sfx); | |
553 | return 1; | |
554 | } | |
555 | ||
556 | /** | |
557 | * snd_hda_get_pin_label - Get a label for the given I/O pin | |
558 | * | |
559 | * Get a label for the given pin. This function works for both input and | |
560 | * output pins. When @cfg is given as non-NULL, the function tries to get | |
561 | * an optimized label using hda_get_autocfg_input_label(). | |
562 | * | |
563 | * This function tries to give a unique label string for the pin as much as | |
564 | * possible. For example, when the multiple line-outs are present, it adds | |
565 | * the channel suffix like "Front", "Surround", etc (only when @cfg is given). | |
566 | * If no unique name with a suffix is available and @indexp is non-NULL, the | |
567 | * index number is stored in the pointer. | |
568 | */ | |
569 | int snd_hda_get_pin_label(struct hda_codec *codec, hda_nid_t nid, | |
570 | const struct auto_pin_cfg *cfg, | |
571 | char *label, int maxlen, int *indexp) | |
572 | { | |
573 | unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid); | |
574 | const char *name = NULL; | |
575 | int i; | |
576 | ||
577 | if (indexp) | |
578 | *indexp = 0; | |
579 | if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE) | |
580 | return 0; | |
581 | ||
582 | switch (get_defcfg_device(def_conf)) { | |
583 | case AC_JACK_LINE_OUT: | |
584 | return fill_audio_out_name(codec, nid, cfg, "Line Out", | |
585 | label, maxlen, indexp); | |
586 | case AC_JACK_SPEAKER: | |
587 | return fill_audio_out_name(codec, nid, cfg, "Speaker", | |
588 | label, maxlen, indexp); | |
589 | case AC_JACK_HP_OUT: | |
590 | return fill_audio_out_name(codec, nid, cfg, "Headphone", | |
591 | label, maxlen, indexp); | |
592 | case AC_JACK_SPDIF_OUT: | |
593 | case AC_JACK_DIG_OTHER_OUT: | |
594 | if (get_defcfg_location(def_conf) == AC_JACK_LOC_HDMI) | |
595 | name = "HDMI"; | |
596 | else | |
597 | name = "SPDIF"; | |
598 | if (cfg && indexp) { | |
599 | i = find_idx_in_nid_list(nid, cfg->dig_out_pins, | |
600 | cfg->dig_outs); | |
601 | if (i >= 0) | |
602 | *indexp = i; | |
603 | } | |
604 | break; | |
605 | default: | |
606 | if (cfg) { | |
607 | for (i = 0; i < cfg->num_inputs; i++) { | |
608 | if (cfg->inputs[i].pin != nid) | |
609 | continue; | |
610 | name = hda_get_autocfg_input_label(codec, cfg, i); | |
611 | if (name) | |
612 | break; | |
613 | } | |
614 | } | |
615 | if (!name) | |
616 | name = hda_get_input_pin_label(codec, nid, true); | |
617 | break; | |
618 | } | |
619 | if (!name) | |
620 | return 0; | |
621 | strlcpy(label, name, maxlen); | |
622 | return 1; | |
623 | } | |
624 | EXPORT_SYMBOL_HDA(snd_hda_get_pin_label); | |
625 | ||
c9ce6b26 TI |
626 | int snd_hda_add_verbs(struct hda_codec *codec, |
627 | const struct hda_verb *list) | |
23d30f28 TI |
628 | { |
629 | const struct hda_verb **v; | |
c9ce6b26 | 630 | v = snd_array_new(&codec->verbs); |
23d30f28 TI |
631 | if (!v) |
632 | return -ENOMEM; | |
633 | *v = list; | |
634 | return 0; | |
635 | } | |
c9ce6b26 | 636 | EXPORT_SYMBOL_HDA(snd_hda_add_verbs); |
23d30f28 | 637 | |
c9ce6b26 | 638 | void snd_hda_apply_verbs(struct hda_codec *codec) |
23d30f28 | 639 | { |
23d30f28 | 640 | int i; |
c9ce6b26 TI |
641 | for (i = 0; i < codec->verbs.used; i++) { |
642 | struct hda_verb **v = snd_array_elem(&codec->verbs, i); | |
23d30f28 TI |
643 | snd_hda_sequence_write(codec, *v); |
644 | } | |
645 | } | |
c9ce6b26 | 646 | EXPORT_SYMBOL_HDA(snd_hda_apply_verbs); |
23d30f28 TI |
647 | |
648 | void snd_hda_apply_pincfgs(struct hda_codec *codec, | |
649 | const struct hda_pintbl *cfg) | |
650 | { | |
651 | for (; cfg->nid; cfg++) | |
652 | snd_hda_codec_set_pincfg(codec, cfg->nid, cfg->val); | |
653 | } | |
654 | EXPORT_SYMBOL_HDA(snd_hda_apply_pincfgs); | |
655 | ||
656 | void snd_hda_apply_fixup(struct hda_codec *codec, int action) | |
657 | { | |
c9ce6b26 | 658 | int id = codec->fixup_id; |
23d30f28 | 659 | #ifdef CONFIG_SND_DEBUG_VERBOSE |
c9ce6b26 | 660 | const char *modelname = codec->fixup_name; |
23d30f28 TI |
661 | #endif |
662 | int depth = 0; | |
663 | ||
c9ce6b26 | 664 | if (!codec->fixup_list) |
23d30f28 TI |
665 | return; |
666 | ||
667 | while (id >= 0) { | |
c9ce6b26 | 668 | const struct hda_fixup *fix = codec->fixup_list + id; |
23d30f28 TI |
669 | |
670 | switch (fix->type) { | |
671 | case HDA_FIXUP_PINS: | |
672 | if (action != HDA_FIXUP_ACT_PRE_PROBE || !fix->v.pins) | |
673 | break; | |
674 | snd_printdd(KERN_INFO SFX | |
675 | "%s: Apply pincfg for %s\n", | |
676 | codec->chip_name, modelname); | |
677 | snd_hda_apply_pincfgs(codec, fix->v.pins); | |
678 | break; | |
679 | case HDA_FIXUP_VERBS: | |
680 | if (action != HDA_FIXUP_ACT_PROBE || !fix->v.verbs) | |
681 | break; | |
682 | snd_printdd(KERN_INFO SFX | |
683 | "%s: Apply fix-verbs for %s\n", | |
684 | codec->chip_name, modelname); | |
c9ce6b26 | 685 | snd_hda_add_verbs(codec, fix->v.verbs); |
23d30f28 TI |
686 | break; |
687 | case HDA_FIXUP_FUNC: | |
688 | if (!fix->v.func) | |
689 | break; | |
690 | snd_printdd(KERN_INFO SFX | |
691 | "%s: Apply fix-func for %s\n", | |
692 | codec->chip_name, modelname); | |
693 | fix->v.func(codec, fix, action); | |
694 | break; | |
695 | default: | |
696 | snd_printk(KERN_ERR SFX | |
697 | "%s: Invalid fixup type %d\n", | |
698 | codec->chip_name, fix->type); | |
699 | break; | |
700 | } | |
701 | if (!fix->chained) | |
702 | break; | |
703 | if (++depth > 10) | |
704 | break; | |
705 | id = fix->chain_id; | |
706 | } | |
707 | } | |
708 | EXPORT_SYMBOL_HDA(snd_hda_apply_fixup); | |
709 | ||
710 | void snd_hda_pick_fixup(struct hda_codec *codec, | |
711 | const struct hda_model_fixup *models, | |
712 | const struct snd_pci_quirk *quirk, | |
713 | const struct hda_fixup *fixlist) | |
714 | { | |
23d30f28 TI |
715 | const struct snd_pci_quirk *q; |
716 | int id = -1; | |
717 | const char *name = NULL; | |
718 | ||
719 | /* when model=nofixup is given, don't pick up any fixups */ | |
720 | if (codec->modelname && !strcmp(codec->modelname, "nofixup")) { | |
c9ce6b26 TI |
721 | codec->fixup_list = NULL; |
722 | codec->fixup_id = -1; | |
23d30f28 TI |
723 | return; |
724 | } | |
725 | ||
726 | if (codec->modelname && models) { | |
727 | while (models->name) { | |
728 | if (!strcmp(codec->modelname, models->name)) { | |
729 | id = models->id; | |
730 | name = models->name; | |
731 | break; | |
732 | } | |
733 | models++; | |
734 | } | |
735 | } | |
639aa4bd | 736 | if (id < 0 && quirk) { |
23d30f28 TI |
737 | q = snd_pci_quirk_lookup(codec->bus->pci, quirk); |
738 | if (q) { | |
739 | id = q->value; | |
740 | #ifdef CONFIG_SND_DEBUG_VERBOSE | |
741 | name = q->name; | |
742 | #endif | |
743 | } | |
744 | } | |
639aa4bd | 745 | if (id < 0 && quirk) { |
23d30f28 TI |
746 | for (q = quirk; q->subvendor; q++) { |
747 | unsigned int vendorid = | |
748 | q->subdevice | (q->subvendor << 16); | |
a33b7b0a TI |
749 | unsigned int mask = 0xffff0000 | q->subdevice_mask; |
750 | if ((codec->subsystem_id & mask) == (vendorid & mask)) { | |
23d30f28 TI |
751 | id = q->value; |
752 | #ifdef CONFIG_SND_DEBUG_VERBOSE | |
753 | name = q->name; | |
754 | #endif | |
755 | break; | |
756 | } | |
757 | } | |
758 | } | |
759 | ||
c9ce6b26 | 760 | codec->fixup_id = id; |
23d30f28 | 761 | if (id >= 0) { |
c9ce6b26 TI |
762 | codec->fixup_list = fixlist; |
763 | codec->fixup_name = name; | |
23d30f28 TI |
764 | } |
765 | } | |
766 | EXPORT_SYMBOL_HDA(snd_hda_pick_fixup); |