blob: 0088bb0b04decc4507ad6330c0e7ce596be2e226 [file] [log] [blame]
Takashi Iwai23d30f22012-05-07 17:17:32 +02001/*
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>
Takashi Iwaib9030a02012-11-29 10:18:57 +010014#include <linux/sort.h>
Takashi Iwai23d30f22012-05-07 17:17:32 +020015#include <sound/core.h>
16#include "hda_codec.h"
Takashi Iwai128bc4b2012-05-07 17:42:31 +020017#include "hda_local.h"
Takashi Iwai23d30f22012-05-07 17:17:32 +020018#include "hda_auto_parser.h"
19
20#define SFX "hda_codec: "
21
Takashi Iwai128bc4b2012-05-07 17:42:31 +020022/*
23 * Helper for automatic pin configuration
24 */
25
26static 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
Takashi Iwaib9030a02012-11-29 10:18:57 +010034/* a pair of input pin and its sequence */
35struct auto_out_pin {
36 hda_nid_t pin;
37 short seq;
38};
39
40static 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}
Takashi Iwai128bc4b2012-05-07 17:42:31 +020046
47/*
48 * Sort an associated group of pins according to their sequence numbers.
Takashi Iwaib9030a02012-11-29 10:18:57 +010049 * then store it to a pin array.
Takashi Iwai128bc4b2012-05-07 17:42:31 +020050 */
Takashi Iwaib9030a02012-11-29 10:18:57 +010051static void sort_pins_by_sequence(hda_nid_t *pins, struct auto_out_pin *list,
Takashi Iwai128bc4b2012-05-07 17:42:31 +020052 int num_pins)
53{
Takashi Iwaib9030a02012-11-29 10:18:57 +010054 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;
Takashi Iwai128bc4b2012-05-07 17:42:31 +020058}
59
60
61/* add the found input-pin to the cfg->inputs[] table */
62static 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
Takashi Iwaib9030a02012-11-29 10:18:57 +010072static int compare_input_type(const void *ap, const void *bp)
Takashi Iwai128bc4b2012-05-07 17:42:31 +020073{
Takashi Iwaib9030a02012-11-29 10:18:57 +010074 const struct auto_pin_cfg_item *a = ap;
75 const struct auto_pin_cfg_item *b = bp;
76 return (int)(a->type - b->type);
Takashi Iwai128bc4b2012-05-07 17:42:31 +020077}
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 */
86static 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
Takashi Iwai52fd5cb2013-01-15 08:45:33 +0100100/* check whether the given pin has a proper pin I/O capability bit */
101static bool check_pincap_validity(struct hda_codec *codec, hda_nid_t pin,
102 unsigned int dev)
103{
104 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
105
106 /* some old hardware don't return the proper pincaps */
107 if (!pincap)
108 return true;
109
110 switch (dev) {
111 case AC_JACK_LINE_OUT:
112 case AC_JACK_SPEAKER:
113 case AC_JACK_HP_OUT:
114 case AC_JACK_SPDIF_OUT:
115 case AC_JACK_DIG_OTHER_OUT:
116 return !!(pincap & AC_PINCAP_OUT);
117 default:
118 return !!(pincap & AC_PINCAP_IN);
119 }
120}
121
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200122/*
123 * Parse all pin widgets and store the useful pin nids to cfg
124 *
125 * The number of line-outs or any primary output is stored in line_outs,
126 * and the corresponding output pins are assigned to line_out_pins[],
127 * in the order of front, rear, CLFE, side, ...
128 *
129 * If more extra outputs (speaker and headphone) are found, the pins are
130 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
131 * is detected, one of speaker of HP pins is assigned as the primary
132 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
133 * if any analog output exists.
134 *
135 * The analog input pins are assigned to inputs array.
136 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
137 * respectively.
138 */
139int snd_hda_parse_pin_defcfg(struct hda_codec *codec,
140 struct auto_pin_cfg *cfg,
141 const hda_nid_t *ignore_nids,
142 unsigned int cond_flags)
143{
144 hda_nid_t nid, end_nid;
145 short seq, assoc_line_out;
Takashi Iwaib9030a02012-11-29 10:18:57 +0100146 struct auto_out_pin line_out[ARRAY_SIZE(cfg->line_out_pins)];
147 struct auto_out_pin speaker_out[ARRAY_SIZE(cfg->speaker_pins)];
148 struct auto_out_pin hp_out[ARRAY_SIZE(cfg->hp_pins)];
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200149 int i;
150
Takashi Iwai1c70a582013-01-11 17:48:22 +0100151 if (!snd_hda_get_int_hint(codec, "parser_flags", &i))
152 cond_flags = i;
153
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200154 memset(cfg, 0, sizeof(*cfg));
155
Takashi Iwaib9030a02012-11-29 10:18:57 +0100156 memset(line_out, 0, sizeof(line_out));
157 memset(speaker_out, 0, sizeof(speaker_out));
158 memset(hp_out, 0, sizeof(hp_out));
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200159 assoc_line_out = 0;
160
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200161 end_nid = codec->start_nid + codec->num_nodes;
162 for (nid = codec->start_nid; nid < end_nid; nid++) {
163 unsigned int wid_caps = get_wcaps(codec, nid);
164 unsigned int wid_type = get_wcaps_type(wid_caps);
165 unsigned int def_conf;
166 short assoc, loc, conn, dev;
167
168 /* read all default configuration for pin complex */
169 if (wid_type != AC_WID_PIN)
170 continue;
171 /* ignore the given nids (e.g. pc-beep returns error) */
172 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
173 continue;
174
175 def_conf = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200176 conn = get_defcfg_connect(def_conf);
177 if (conn == AC_JACK_PORT_NONE)
178 continue;
179 loc = get_defcfg_location(def_conf);
180 dev = get_defcfg_device(def_conf);
181
182 /* workaround for buggy BIOS setups */
183 if (dev == AC_JACK_LINE_OUT) {
Takashi Iwaifb690cf2013-01-07 18:21:47 +0100184 if (conn == AC_JACK_PORT_FIXED ||
185 conn == AC_JACK_PORT_BOTH)
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200186 dev = AC_JACK_SPEAKER;
187 }
188
Takashi Iwai52fd5cb2013-01-15 08:45:33 +0100189 if (!check_pincap_validity(codec, nid, dev))
190 continue;
191
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200192 switch (dev) {
193 case AC_JACK_LINE_OUT:
194 seq = get_defcfg_sequence(def_conf);
195 assoc = get_defcfg_association(def_conf);
196
197 if (!(wid_caps & AC_WCAP_STEREO))
198 if (!cfg->mono_out_pin)
199 cfg->mono_out_pin = nid;
200 if (!assoc)
201 continue;
202 if (!assoc_line_out)
203 assoc_line_out = assoc;
204 else if (assoc_line_out != assoc)
205 continue;
206 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
207 continue;
Takashi Iwaib9030a02012-11-29 10:18:57 +0100208 line_out[cfg->line_outs].pin = nid;
209 line_out[cfg->line_outs].seq = seq;
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200210 cfg->line_outs++;
211 break;
212 case AC_JACK_SPEAKER:
213 seq = get_defcfg_sequence(def_conf);
214 assoc = get_defcfg_association(def_conf);
215 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
216 continue;
Takashi Iwaib9030a02012-11-29 10:18:57 +0100217 speaker_out[cfg->speaker_outs].pin = nid;
218 speaker_out[cfg->speaker_outs].seq = (assoc << 4) | seq;
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200219 cfg->speaker_outs++;
220 break;
221 case AC_JACK_HP_OUT:
222 seq = get_defcfg_sequence(def_conf);
223 assoc = get_defcfg_association(def_conf);
224 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
225 continue;
Takashi Iwaib9030a02012-11-29 10:18:57 +0100226 hp_out[cfg->hp_outs].pin = nid;
227 hp_out[cfg->hp_outs].seq = (assoc << 4) | seq;
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200228 cfg->hp_outs++;
229 break;
230 case AC_JACK_MIC_IN:
231 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_MIC);
232 break;
233 case AC_JACK_LINE_IN:
234 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_LINE_IN);
235 break;
236 case AC_JACK_CD:
237 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_CD);
238 break;
239 case AC_JACK_AUX:
240 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_AUX);
241 break;
242 case AC_JACK_SPDIF_OUT:
243 case AC_JACK_DIG_OTHER_OUT:
244 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
245 continue;
246 cfg->dig_out_pins[cfg->dig_outs] = nid;
247 cfg->dig_out_type[cfg->dig_outs] =
248 (loc == AC_JACK_LOC_HDMI) ?
249 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
250 cfg->dig_outs++;
251 break;
252 case AC_JACK_SPDIF_IN:
253 case AC_JACK_DIG_OTHER_IN:
254 cfg->dig_in_pin = nid;
255 if (loc == AC_JACK_LOC_HDMI)
256 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
257 else
258 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
259 break;
260 }
261 }
262
263 /* FIX-UP:
264 * If no line-out is defined but multiple HPs are found,
265 * some of them might be the real line-outs.
266 */
267 if (!cfg->line_outs && cfg->hp_outs > 1 &&
268 !(cond_flags & HDA_PINCFG_NO_HP_FIXUP)) {
269 int i = 0;
270 while (i < cfg->hp_outs) {
271 /* The real HPs should have the sequence 0x0f */
Takashi Iwaib9030a02012-11-29 10:18:57 +0100272 if ((hp_out[i].seq & 0x0f) == 0x0f) {
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200273 i++;
274 continue;
275 }
276 /* Move it to the line-out table */
Takashi Iwaib9030a02012-11-29 10:18:57 +0100277 line_out[cfg->line_outs++] = hp_out[i];
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200278 cfg->hp_outs--;
Takashi Iwaib9030a02012-11-29 10:18:57 +0100279 memmove(hp_out + i, hp_out + i + 1,
280 sizeof(hp_out[0]) * (cfg->hp_outs - i));
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200281 }
Takashi Iwaib9030a02012-11-29 10:18:57 +0100282 memset(hp_out + cfg->hp_outs, 0,
283 sizeof(hp_out[0]) * (AUTO_CFG_MAX_OUTS - cfg->hp_outs));
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200284 if (!cfg->hp_outs)
285 cfg->line_out_type = AUTO_PIN_HP_OUT;
286
287 }
288
289 /* sort by sequence */
Takashi Iwaib9030a02012-11-29 10:18:57 +0100290 sort_pins_by_sequence(cfg->line_out_pins, line_out, cfg->line_outs);
291 sort_pins_by_sequence(cfg->speaker_pins, speaker_out,
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200292 cfg->speaker_outs);
Takashi Iwaib9030a02012-11-29 10:18:57 +0100293 sort_pins_by_sequence(cfg->hp_pins, hp_out, cfg->hp_outs);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200294
295 /*
296 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
297 * as a primary output
298 */
299 if (!cfg->line_outs &&
300 !(cond_flags & HDA_PINCFG_NO_LO_FIXUP)) {
301 if (cfg->speaker_outs) {
302 cfg->line_outs = cfg->speaker_outs;
303 memcpy(cfg->line_out_pins, cfg->speaker_pins,
304 sizeof(cfg->speaker_pins));
305 cfg->speaker_outs = 0;
306 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
307 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
308 } else if (cfg->hp_outs) {
309 cfg->line_outs = cfg->hp_outs;
310 memcpy(cfg->line_out_pins, cfg->hp_pins,
311 sizeof(cfg->hp_pins));
312 cfg->hp_outs = 0;
313 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
314 cfg->line_out_type = AUTO_PIN_HP_OUT;
315 }
316 }
317
318 reorder_outputs(cfg->line_outs, cfg->line_out_pins);
319 reorder_outputs(cfg->hp_outs, cfg->hp_pins);
320 reorder_outputs(cfg->speaker_outs, cfg->speaker_pins);
321
Takashi Iwaib9030a02012-11-29 10:18:57 +0100322 /* sort inputs in the order of AUTO_PIN_* type */
323 sort(cfg->inputs, cfg->num_inputs, sizeof(cfg->inputs[0]),
324 compare_input_type, NULL);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200325
326 /*
327 * debug prints of the parsed results
328 */
329 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x) type:%s\n",
330 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
331 cfg->line_out_pins[2], cfg->line_out_pins[3],
332 cfg->line_out_pins[4],
333 cfg->line_out_type == AUTO_PIN_HP_OUT ? "hp" :
334 (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT ?
335 "speaker" : "line"));
336 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
337 cfg->speaker_outs, cfg->speaker_pins[0],
338 cfg->speaker_pins[1], cfg->speaker_pins[2],
339 cfg->speaker_pins[3], cfg->speaker_pins[4]);
340 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
341 cfg->hp_outs, cfg->hp_pins[0],
342 cfg->hp_pins[1], cfg->hp_pins[2],
343 cfg->hp_pins[3], cfg->hp_pins[4]);
344 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
345 if (cfg->dig_outs)
346 snd_printd(" dig-out=0x%x/0x%x\n",
347 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
Takashi Iwai709aea62012-08-07 18:09:23 +0200348 snd_printd(" inputs:\n");
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200349 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwai709aea62012-08-07 18:09:23 +0200350 snd_printd(" %s=0x%x\n",
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200351 hda_get_autocfg_input_label(codec, cfg, i),
352 cfg->inputs[i].pin);
353 }
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200354 if (cfg->dig_in_pin)
355 snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
356
357 return 0;
358}
359EXPORT_SYMBOL_HDA(snd_hda_parse_pin_defcfg);
360
361int snd_hda_get_input_pin_attr(unsigned int def_conf)
362{
363 unsigned int loc = get_defcfg_location(def_conf);
364 unsigned int conn = get_defcfg_connect(def_conf);
365 if (conn == AC_JACK_PORT_NONE)
366 return INPUT_PIN_ATTR_UNUSED;
367 /* Windows may claim the internal mic to be BOTH, too */
368 if (conn == AC_JACK_PORT_FIXED || conn == AC_JACK_PORT_BOTH)
369 return INPUT_PIN_ATTR_INT;
370 if ((loc & 0x30) == AC_JACK_LOC_INTERNAL)
371 return INPUT_PIN_ATTR_INT;
372 if ((loc & 0x30) == AC_JACK_LOC_SEPARATE)
373 return INPUT_PIN_ATTR_DOCK;
374 if (loc == AC_JACK_LOC_REAR)
375 return INPUT_PIN_ATTR_REAR;
376 if (loc == AC_JACK_LOC_FRONT)
377 return INPUT_PIN_ATTR_FRONT;
378 return INPUT_PIN_ATTR_NORMAL;
379}
380EXPORT_SYMBOL_HDA(snd_hda_get_input_pin_attr);
381
382/**
383 * hda_get_input_pin_label - Give a label for the given input pin
384 *
385 * When check_location is true, the function checks the pin location
386 * for mic and line-in pins, and set an appropriate prefix like "Front",
387 * "Rear", "Internal".
388 */
389
390static const char *hda_get_input_pin_label(struct hda_codec *codec,
391 hda_nid_t pin, bool check_location)
392{
393 unsigned int def_conf;
394 static const char * const mic_names[] = {
Takashi Iwai5ec16d12012-11-28 18:11:59 +0100395 "Internal Mic", "Dock Mic", "Mic", "Rear Mic", "Front Mic"
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200396 };
397 int attr;
398
399 def_conf = snd_hda_codec_get_pincfg(codec, pin);
400
401 switch (get_defcfg_device(def_conf)) {
402 case AC_JACK_MIC_IN:
403 if (!check_location)
404 return "Mic";
405 attr = snd_hda_get_input_pin_attr(def_conf);
406 if (!attr)
407 return "None";
408 return mic_names[attr - 1];
409 case AC_JACK_LINE_IN:
410 if (!check_location)
411 return "Line";
412 attr = snd_hda_get_input_pin_attr(def_conf);
413 if (!attr)
414 return "None";
415 if (attr == INPUT_PIN_ATTR_DOCK)
416 return "Dock Line";
417 return "Line";
418 case AC_JACK_AUX:
419 return "Aux";
420 case AC_JACK_CD:
421 return "CD";
422 case AC_JACK_SPDIF_IN:
423 return "SPDIF In";
424 case AC_JACK_DIG_OTHER_IN:
425 return "Digital In";
Takashi Iwai54d778b2013-01-09 08:46:34 +0100426 case AC_JACK_HP_OUT:
427 return "Headphone Mic";
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200428 default:
429 return "Misc";
430 }
431}
432
433/* Check whether the location prefix needs to be added to the label.
434 * If all mic-jacks are in the same location (e.g. rear panel), we don't
435 * have to put "Front" prefix to each label. In such a case, returns false.
436 */
437static int check_mic_location_need(struct hda_codec *codec,
438 const struct auto_pin_cfg *cfg,
439 int input)
440{
441 unsigned int defc;
442 int i, attr, attr2;
443
444 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[input].pin);
445 attr = snd_hda_get_input_pin_attr(defc);
446 /* for internal or docking mics, we need locations */
447 if (attr <= INPUT_PIN_ATTR_NORMAL)
448 return 1;
449
450 attr = 0;
451 for (i = 0; i < cfg->num_inputs; i++) {
452 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[i].pin);
453 attr2 = snd_hda_get_input_pin_attr(defc);
454 if (attr2 >= INPUT_PIN_ATTR_NORMAL) {
455 if (attr && attr != attr2)
456 return 1; /* different locations found */
457 attr = attr2;
458 }
459 }
460 return 0;
461}
462
463/**
464 * hda_get_autocfg_input_label - Get a label for the given input
465 *
466 * Get a label for the given input pin defined by the autocfg item.
467 * Unlike hda_get_input_pin_label(), this function checks all inputs
468 * defined in autocfg and avoids the redundant mic/line prefix as much as
469 * possible.
470 */
471const char *hda_get_autocfg_input_label(struct hda_codec *codec,
472 const struct auto_pin_cfg *cfg,
473 int input)
474{
475 int type = cfg->inputs[input].type;
476 int has_multiple_pins = 0;
477
478 if ((input > 0 && cfg->inputs[input - 1].type == type) ||
479 (input < cfg->num_inputs - 1 && cfg->inputs[input + 1].type == type))
480 has_multiple_pins = 1;
481 if (has_multiple_pins && type == AUTO_PIN_MIC)
482 has_multiple_pins &= check_mic_location_need(codec, cfg, input);
483 return hda_get_input_pin_label(codec, cfg->inputs[input].pin,
484 has_multiple_pins);
485}
486EXPORT_SYMBOL_HDA(hda_get_autocfg_input_label);
487
488/* return the position of NID in the list, or -1 if not found */
489static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
490{
491 int i;
492 for (i = 0; i < nums; i++)
493 if (list[i] == nid)
494 return i;
495 return -1;
496}
497
498/* get a unique suffix or an index number */
499static const char *check_output_sfx(hda_nid_t nid, const hda_nid_t *pins,
500 int num_pins, int *indexp)
501{
502 static const char * const channel_sfx[] = {
503 " Front", " Surround", " CLFE", " Side"
504 };
505 int i;
506
507 i = find_idx_in_nid_list(nid, pins, num_pins);
508 if (i < 0)
509 return NULL;
510 if (num_pins == 1)
511 return "";
512 if (num_pins > ARRAY_SIZE(channel_sfx)) {
513 if (indexp)
514 *indexp = i;
515 return "";
516 }
517 return channel_sfx[i];
518}
519
David Henningssoneee3ed42012-10-03 11:12:53 +0200520static const char *check_output_pfx(struct hda_codec *codec, hda_nid_t nid)
521{
522 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
523 int attr = snd_hda_get_input_pin_attr(def_conf);
524
525 /* check the location */
526 switch (attr) {
527 case INPUT_PIN_ATTR_DOCK:
528 return "Dock ";
529 case INPUT_PIN_ATTR_FRONT:
530 return "Front ";
531 }
532 return "";
533}
534
535static int get_hp_label_index(struct hda_codec *codec, hda_nid_t nid,
536 const hda_nid_t *pins, int num_pins)
537{
538 int i, j, idx = 0;
539
540 const char *pfx = check_output_pfx(codec, nid);
541
542 i = find_idx_in_nid_list(nid, pins, num_pins);
543 if (i < 0)
544 return -1;
545 for (j = 0; j < i; j++)
546 if (pfx == check_output_pfx(codec, pins[j]))
547 idx++;
548
549 return idx;
550}
551
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200552static int fill_audio_out_name(struct hda_codec *codec, hda_nid_t nid,
553 const struct auto_pin_cfg *cfg,
554 const char *name, char *label, int maxlen,
555 int *indexp)
556{
557 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
558 int attr = snd_hda_get_input_pin_attr(def_conf);
David Henningssoneee3ed42012-10-03 11:12:53 +0200559 const char *pfx, *sfx = "";
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200560
561 /* handle as a speaker if it's a fixed line-out */
562 if (!strcmp(name, "Line Out") && attr == INPUT_PIN_ATTR_INT)
563 name = "Speaker";
David Henningssoneee3ed42012-10-03 11:12:53 +0200564 pfx = check_output_pfx(codec, nid);
565
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200566 if (cfg) {
567 /* try to give a unique suffix if needed */
568 sfx = check_output_sfx(nid, cfg->line_out_pins, cfg->line_outs,
569 indexp);
570 if (!sfx)
571 sfx = check_output_sfx(nid, cfg->speaker_pins, cfg->speaker_outs,
572 indexp);
573 if (!sfx) {
574 /* don't add channel suffix for Headphone controls */
David Henningssoneee3ed42012-10-03 11:12:53 +0200575 int idx = get_hp_label_index(codec, nid, cfg->hp_pins,
576 cfg->hp_outs);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200577 if (idx >= 0)
578 *indexp = idx;
579 sfx = "";
580 }
581 }
582 snprintf(label, maxlen, "%s%s%s", pfx, name, sfx);
583 return 1;
584}
585
David Henningsson3f25dcf2013-01-18 15:43:03 +0100586#define is_hdmi_cfg(conf) \
587 (get_defcfg_location(conf) == AC_JACK_LOC_HDMI)
588
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200589/**
590 * snd_hda_get_pin_label - Get a label for the given I/O pin
591 *
592 * Get a label for the given pin. This function works for both input and
593 * output pins. When @cfg is given as non-NULL, the function tries to get
594 * an optimized label using hda_get_autocfg_input_label().
595 *
596 * This function tries to give a unique label string for the pin as much as
597 * possible. For example, when the multiple line-outs are present, it adds
598 * the channel suffix like "Front", "Surround", etc (only when @cfg is given).
599 * If no unique name with a suffix is available and @indexp is non-NULL, the
600 * index number is stored in the pointer.
601 */
602int snd_hda_get_pin_label(struct hda_codec *codec, hda_nid_t nid,
603 const struct auto_pin_cfg *cfg,
604 char *label, int maxlen, int *indexp)
605{
606 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
607 const char *name = NULL;
608 int i;
David Henningsson3f25dcf2013-01-18 15:43:03 +0100609 bool hdmi;
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200610
611 if (indexp)
612 *indexp = 0;
613 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
614 return 0;
615
616 switch (get_defcfg_device(def_conf)) {
617 case AC_JACK_LINE_OUT:
618 return fill_audio_out_name(codec, nid, cfg, "Line Out",
619 label, maxlen, indexp);
620 case AC_JACK_SPEAKER:
621 return fill_audio_out_name(codec, nid, cfg, "Speaker",
622 label, maxlen, indexp);
623 case AC_JACK_HP_OUT:
624 return fill_audio_out_name(codec, nid, cfg, "Headphone",
625 label, maxlen, indexp);
626 case AC_JACK_SPDIF_OUT:
627 case AC_JACK_DIG_OTHER_OUT:
David Henningsson3f25dcf2013-01-18 15:43:03 +0100628 hdmi = is_hdmi_cfg(def_conf);
629 name = hdmi ? "HDMI" : "SPDIF";
630 if (cfg && indexp)
631 for (i = 0; i < cfg->dig_outs; i++) {
632 hda_nid_t pin = cfg->dig_out_pins[i];
633 unsigned int c;
634 if (pin == nid)
635 break;
636 c = snd_hda_codec_get_pincfg(codec, pin);
637 if (hdmi == is_hdmi_cfg(c))
638 (*indexp)++;
639 }
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200640 break;
641 default:
642 if (cfg) {
643 for (i = 0; i < cfg->num_inputs; i++) {
644 if (cfg->inputs[i].pin != nid)
645 continue;
646 name = hda_get_autocfg_input_label(codec, cfg, i);
647 if (name)
648 break;
649 }
650 }
651 if (!name)
652 name = hda_get_input_pin_label(codec, nid, true);
653 break;
654 }
655 if (!name)
656 return 0;
657 strlcpy(label, name, maxlen);
658 return 1;
659}
660EXPORT_SYMBOL_HDA(snd_hda_get_pin_label);
661
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100662int snd_hda_add_verbs(struct hda_codec *codec,
663 const struct hda_verb *list)
Takashi Iwai23d30f22012-05-07 17:17:32 +0200664{
665 const struct hda_verb **v;
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100666 v = snd_array_new(&codec->verbs);
Takashi Iwai23d30f22012-05-07 17:17:32 +0200667 if (!v)
668 return -ENOMEM;
669 *v = list;
670 return 0;
671}
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100672EXPORT_SYMBOL_HDA(snd_hda_add_verbs);
Takashi Iwai23d30f22012-05-07 17:17:32 +0200673
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100674void snd_hda_apply_verbs(struct hda_codec *codec)
Takashi Iwai23d30f22012-05-07 17:17:32 +0200675{
Takashi Iwai23d30f22012-05-07 17:17:32 +0200676 int i;
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100677 for (i = 0; i < codec->verbs.used; i++) {
678 struct hda_verb **v = snd_array_elem(&codec->verbs, i);
Takashi Iwai23d30f22012-05-07 17:17:32 +0200679 snd_hda_sequence_write(codec, *v);
680 }
681}
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100682EXPORT_SYMBOL_HDA(snd_hda_apply_verbs);
Takashi Iwai23d30f22012-05-07 17:17:32 +0200683
684void snd_hda_apply_pincfgs(struct hda_codec *codec,
685 const struct hda_pintbl *cfg)
686{
687 for (; cfg->nid; cfg++)
688 snd_hda_codec_set_pincfg(codec, cfg->nid, cfg->val);
689}
690EXPORT_SYMBOL_HDA(snd_hda_apply_pincfgs);
691
Takashi Iwaifd108212013-01-10 10:18:14 +0100692static void set_pin_targets(struct hda_codec *codec,
693 const struct hda_pintbl *cfg)
694{
695 for (; cfg->nid; cfg++)
696 snd_hda_set_pin_ctl_cache(codec, cfg->nid, cfg->val);
697}
698
Takashi Iwai23d30f22012-05-07 17:17:32 +0200699void snd_hda_apply_fixup(struct hda_codec *codec, int action)
700{
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100701 int id = codec->fixup_id;
Takashi Iwai23d30f22012-05-07 17:17:32 +0200702#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100703 const char *modelname = codec->fixup_name;
Takashi Iwai23d30f22012-05-07 17:17:32 +0200704#endif
705 int depth = 0;
706
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100707 if (!codec->fixup_list)
Takashi Iwai23d30f22012-05-07 17:17:32 +0200708 return;
709
710 while (id >= 0) {
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100711 const struct hda_fixup *fix = codec->fixup_list + id;
Takashi Iwai23d30f22012-05-07 17:17:32 +0200712
713 switch (fix->type) {
714 case HDA_FIXUP_PINS:
715 if (action != HDA_FIXUP_ACT_PRE_PROBE || !fix->v.pins)
716 break;
717 snd_printdd(KERN_INFO SFX
718 "%s: Apply pincfg for %s\n",
719 codec->chip_name, modelname);
720 snd_hda_apply_pincfgs(codec, fix->v.pins);
721 break;
722 case HDA_FIXUP_VERBS:
723 if (action != HDA_FIXUP_ACT_PROBE || !fix->v.verbs)
724 break;
725 snd_printdd(KERN_INFO SFX
726 "%s: Apply fix-verbs for %s\n",
727 codec->chip_name, modelname);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100728 snd_hda_add_verbs(codec, fix->v.verbs);
Takashi Iwai23d30f22012-05-07 17:17:32 +0200729 break;
730 case HDA_FIXUP_FUNC:
731 if (!fix->v.func)
732 break;
733 snd_printdd(KERN_INFO SFX
734 "%s: Apply fix-func for %s\n",
735 codec->chip_name, modelname);
736 fix->v.func(codec, fix, action);
737 break;
Takashi Iwaifd108212013-01-10 10:18:14 +0100738 case HDA_FIXUP_PINCTLS:
739 if (action != HDA_FIXUP_ACT_PROBE || !fix->v.pins)
740 break;
741 snd_printdd(KERN_INFO SFX
742 "%s: Apply pinctl for %s\n",
743 codec->chip_name, modelname);
744 set_pin_targets(codec, fix->v.pins);
745 break;
Takashi Iwai23d30f22012-05-07 17:17:32 +0200746 default:
747 snd_printk(KERN_ERR SFX
748 "%s: Invalid fixup type %d\n",
749 codec->chip_name, fix->type);
750 break;
751 }
752 if (!fix->chained)
753 break;
754 if (++depth > 10)
755 break;
756 id = fix->chain_id;
757 }
758}
759EXPORT_SYMBOL_HDA(snd_hda_apply_fixup);
760
761void snd_hda_pick_fixup(struct hda_codec *codec,
762 const struct hda_model_fixup *models,
763 const struct snd_pci_quirk *quirk,
764 const struct hda_fixup *fixlist)
765{
Takashi Iwai23d30f22012-05-07 17:17:32 +0200766 const struct snd_pci_quirk *q;
767 int id = -1;
768 const char *name = NULL;
769
770 /* when model=nofixup is given, don't pick up any fixups */
771 if (codec->modelname && !strcmp(codec->modelname, "nofixup")) {
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100772 codec->fixup_list = NULL;
773 codec->fixup_id = -1;
Takashi Iwai23d30f22012-05-07 17:17:32 +0200774 return;
775 }
776
777 if (codec->modelname && models) {
778 while (models->name) {
779 if (!strcmp(codec->modelname, models->name)) {
780 id = models->id;
781 name = models->name;
782 break;
783 }
784 models++;
785 }
786 }
David Henningsson639aa4b2012-07-18 18:02:53 +0200787 if (id < 0 && quirk) {
Takashi Iwai23d30f22012-05-07 17:17:32 +0200788 q = snd_pci_quirk_lookup(codec->bus->pci, quirk);
789 if (q) {
790 id = q->value;
791#ifdef CONFIG_SND_DEBUG_VERBOSE
792 name = q->name;
793#endif
794 }
795 }
David Henningsson639aa4b2012-07-18 18:02:53 +0200796 if (id < 0 && quirk) {
Takashi Iwai23d30f22012-05-07 17:17:32 +0200797 for (q = quirk; q->subvendor; q++) {
798 unsigned int vendorid =
799 q->subdevice | (q->subvendor << 16);
Takashi Iwaia33b7b02012-09-11 16:42:18 +0200800 unsigned int mask = 0xffff0000 | q->subdevice_mask;
801 if ((codec->subsystem_id & mask) == (vendorid & mask)) {
Takashi Iwai23d30f22012-05-07 17:17:32 +0200802 id = q->value;
803#ifdef CONFIG_SND_DEBUG_VERBOSE
804 name = q->name;
805#endif
806 break;
807 }
808 }
809 }
810
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100811 codec->fixup_id = id;
Takashi Iwai23d30f22012-05-07 17:17:32 +0200812 if (id >= 0) {
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100813 codec->fixup_list = fixlist;
814 codec->fixup_name = name;
Takashi Iwai23d30f22012-05-07 17:17:32 +0200815 }
816}
817EXPORT_SYMBOL_HDA(snd_hda_pick_fixup);