blob: 6b173b34369776d5378632fd92d365003acbd797 [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
David Henningssona385d972013-03-21 12:16:29 +0100263 /* Take first mic to be a headset mic pin */
264 if (cond_flags & HDA_PINCFG_HEADSET_MIC) {
265 for (i = 0; i < cfg->num_inputs; i++) {
266 int attr;
267 unsigned int def_conf;
268 if (cfg->inputs[i].type != AUTO_PIN_MIC)
269 continue;
270 def_conf = snd_hda_codec_get_pincfg(codec, cfg->inputs[i].pin);
271 attr = snd_hda_get_input_pin_attr(def_conf);
272 if (attr <= INPUT_PIN_ATTR_DOCK)
273 continue;
274 cfg->inputs[i].is_headset_mic = 1;
275 break;
276 }
277 }
278
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200279 /* FIX-UP:
280 * If no line-out is defined but multiple HPs are found,
281 * some of them might be the real line-outs.
282 */
283 if (!cfg->line_outs && cfg->hp_outs > 1 &&
284 !(cond_flags & HDA_PINCFG_NO_HP_FIXUP)) {
285 int i = 0;
286 while (i < cfg->hp_outs) {
287 /* The real HPs should have the sequence 0x0f */
Takashi Iwaib9030a02012-11-29 10:18:57 +0100288 if ((hp_out[i].seq & 0x0f) == 0x0f) {
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200289 i++;
290 continue;
291 }
292 /* Move it to the line-out table */
Takashi Iwaib9030a02012-11-29 10:18:57 +0100293 line_out[cfg->line_outs++] = hp_out[i];
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200294 cfg->hp_outs--;
Takashi Iwaib9030a02012-11-29 10:18:57 +0100295 memmove(hp_out + i, hp_out + i + 1,
296 sizeof(hp_out[0]) * (cfg->hp_outs - i));
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200297 }
Takashi Iwaib9030a02012-11-29 10:18:57 +0100298 memset(hp_out + cfg->hp_outs, 0,
299 sizeof(hp_out[0]) * (AUTO_CFG_MAX_OUTS - cfg->hp_outs));
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200300 if (!cfg->hp_outs)
301 cfg->line_out_type = AUTO_PIN_HP_OUT;
302
303 }
304
305 /* sort by sequence */
Takashi Iwaib9030a02012-11-29 10:18:57 +0100306 sort_pins_by_sequence(cfg->line_out_pins, line_out, cfg->line_outs);
307 sort_pins_by_sequence(cfg->speaker_pins, speaker_out,
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200308 cfg->speaker_outs);
Takashi Iwaib9030a02012-11-29 10:18:57 +0100309 sort_pins_by_sequence(cfg->hp_pins, hp_out, cfg->hp_outs);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200310
311 /*
312 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
313 * as a primary output
314 */
315 if (!cfg->line_outs &&
316 !(cond_flags & HDA_PINCFG_NO_LO_FIXUP)) {
317 if (cfg->speaker_outs) {
318 cfg->line_outs = cfg->speaker_outs;
319 memcpy(cfg->line_out_pins, cfg->speaker_pins,
320 sizeof(cfg->speaker_pins));
321 cfg->speaker_outs = 0;
322 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
323 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
324 } else if (cfg->hp_outs) {
325 cfg->line_outs = cfg->hp_outs;
326 memcpy(cfg->line_out_pins, cfg->hp_pins,
327 sizeof(cfg->hp_pins));
328 cfg->hp_outs = 0;
329 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
330 cfg->line_out_type = AUTO_PIN_HP_OUT;
331 }
332 }
333
334 reorder_outputs(cfg->line_outs, cfg->line_out_pins);
335 reorder_outputs(cfg->hp_outs, cfg->hp_pins);
336 reorder_outputs(cfg->speaker_outs, cfg->speaker_pins);
337
Takashi Iwaib9030a02012-11-29 10:18:57 +0100338 /* sort inputs in the order of AUTO_PIN_* type */
339 sort(cfg->inputs, cfg->num_inputs, sizeof(cfg->inputs[0]),
340 compare_input_type, NULL);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200341
342 /*
343 * debug prints of the parsed results
344 */
345 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x) type:%s\n",
346 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
347 cfg->line_out_pins[2], cfg->line_out_pins[3],
348 cfg->line_out_pins[4],
349 cfg->line_out_type == AUTO_PIN_HP_OUT ? "hp" :
350 (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT ?
351 "speaker" : "line"));
352 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
353 cfg->speaker_outs, cfg->speaker_pins[0],
354 cfg->speaker_pins[1], cfg->speaker_pins[2],
355 cfg->speaker_pins[3], cfg->speaker_pins[4]);
356 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
357 cfg->hp_outs, cfg->hp_pins[0],
358 cfg->hp_pins[1], cfg->hp_pins[2],
359 cfg->hp_pins[3], cfg->hp_pins[4]);
360 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
361 if (cfg->dig_outs)
362 snd_printd(" dig-out=0x%x/0x%x\n",
363 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
Takashi Iwai709aea62012-08-07 18:09:23 +0200364 snd_printd(" inputs:\n");
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200365 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwai709aea62012-08-07 18:09:23 +0200366 snd_printd(" %s=0x%x\n",
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200367 hda_get_autocfg_input_label(codec, cfg, i),
368 cfg->inputs[i].pin);
369 }
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200370 if (cfg->dig_in_pin)
371 snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
372
373 return 0;
374}
375EXPORT_SYMBOL_HDA(snd_hda_parse_pin_defcfg);
376
377int snd_hda_get_input_pin_attr(unsigned int def_conf)
378{
379 unsigned int loc = get_defcfg_location(def_conf);
380 unsigned int conn = get_defcfg_connect(def_conf);
381 if (conn == AC_JACK_PORT_NONE)
382 return INPUT_PIN_ATTR_UNUSED;
383 /* Windows may claim the internal mic to be BOTH, too */
384 if (conn == AC_JACK_PORT_FIXED || conn == AC_JACK_PORT_BOTH)
385 return INPUT_PIN_ATTR_INT;
386 if ((loc & 0x30) == AC_JACK_LOC_INTERNAL)
387 return INPUT_PIN_ATTR_INT;
388 if ((loc & 0x30) == AC_JACK_LOC_SEPARATE)
389 return INPUT_PIN_ATTR_DOCK;
390 if (loc == AC_JACK_LOC_REAR)
391 return INPUT_PIN_ATTR_REAR;
392 if (loc == AC_JACK_LOC_FRONT)
393 return INPUT_PIN_ATTR_FRONT;
394 return INPUT_PIN_ATTR_NORMAL;
395}
396EXPORT_SYMBOL_HDA(snd_hda_get_input_pin_attr);
397
398/**
399 * hda_get_input_pin_label - Give a label for the given input pin
400 *
401 * When check_location is true, the function checks the pin location
402 * for mic and line-in pins, and set an appropriate prefix like "Front",
403 * "Rear", "Internal".
404 */
405
406static const char *hda_get_input_pin_label(struct hda_codec *codec,
David Henningssona385d972013-03-21 12:16:29 +0100407 const struct auto_pin_cfg_item *item,
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200408 hda_nid_t pin, bool check_location)
409{
410 unsigned int def_conf;
411 static const char * const mic_names[] = {
Takashi Iwai5ec16d12012-11-28 18:11:59 +0100412 "Internal Mic", "Dock Mic", "Mic", "Rear Mic", "Front Mic"
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200413 };
414 int attr;
415
416 def_conf = snd_hda_codec_get_pincfg(codec, pin);
417
418 switch (get_defcfg_device(def_conf)) {
419 case AC_JACK_MIC_IN:
David Henningssona385d972013-03-21 12:16:29 +0100420 if (item && item->is_headset_mic)
421 return "Headset Mic";
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200422 if (!check_location)
423 return "Mic";
424 attr = snd_hda_get_input_pin_attr(def_conf);
425 if (!attr)
426 return "None";
427 return mic_names[attr - 1];
428 case AC_JACK_LINE_IN:
429 if (!check_location)
430 return "Line";
431 attr = snd_hda_get_input_pin_attr(def_conf);
432 if (!attr)
433 return "None";
434 if (attr == INPUT_PIN_ATTR_DOCK)
435 return "Dock Line";
436 return "Line";
437 case AC_JACK_AUX:
438 return "Aux";
439 case AC_JACK_CD:
440 return "CD";
441 case AC_JACK_SPDIF_IN:
442 return "SPDIF In";
443 case AC_JACK_DIG_OTHER_IN:
444 return "Digital In";
Takashi Iwai54d778b2013-01-09 08:46:34 +0100445 case AC_JACK_HP_OUT:
446 return "Headphone Mic";
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200447 default:
448 return "Misc";
449 }
450}
451
452/* Check whether the location prefix needs to be added to the label.
453 * If all mic-jacks are in the same location (e.g. rear panel), we don't
454 * have to put "Front" prefix to each label. In such a case, returns false.
455 */
456static int check_mic_location_need(struct hda_codec *codec,
457 const struct auto_pin_cfg *cfg,
458 int input)
459{
460 unsigned int defc;
461 int i, attr, attr2;
462
463 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[input].pin);
464 attr = snd_hda_get_input_pin_attr(defc);
465 /* for internal or docking mics, we need locations */
466 if (attr <= INPUT_PIN_ATTR_NORMAL)
467 return 1;
468
469 attr = 0;
470 for (i = 0; i < cfg->num_inputs; i++) {
471 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[i].pin);
472 attr2 = snd_hda_get_input_pin_attr(defc);
473 if (attr2 >= INPUT_PIN_ATTR_NORMAL) {
474 if (attr && attr != attr2)
475 return 1; /* different locations found */
476 attr = attr2;
477 }
478 }
479 return 0;
480}
481
482/**
483 * hda_get_autocfg_input_label - Get a label for the given input
484 *
485 * Get a label for the given input pin defined by the autocfg item.
486 * Unlike hda_get_input_pin_label(), this function checks all inputs
487 * defined in autocfg and avoids the redundant mic/line prefix as much as
488 * possible.
489 */
490const char *hda_get_autocfg_input_label(struct hda_codec *codec,
491 const struct auto_pin_cfg *cfg,
492 int input)
493{
494 int type = cfg->inputs[input].type;
495 int has_multiple_pins = 0;
496
497 if ((input > 0 && cfg->inputs[input - 1].type == type) ||
498 (input < cfg->num_inputs - 1 && cfg->inputs[input + 1].type == type))
499 has_multiple_pins = 1;
500 if (has_multiple_pins && type == AUTO_PIN_MIC)
501 has_multiple_pins &= check_mic_location_need(codec, cfg, input);
David Henningssona385d972013-03-21 12:16:29 +0100502 return hda_get_input_pin_label(codec, &cfg->inputs[input],
503 cfg->inputs[input].pin,
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200504 has_multiple_pins);
505}
506EXPORT_SYMBOL_HDA(hda_get_autocfg_input_label);
507
508/* return the position of NID in the list, or -1 if not found */
509static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
510{
511 int i;
512 for (i = 0; i < nums; i++)
513 if (list[i] == nid)
514 return i;
515 return -1;
516}
517
518/* get a unique suffix or an index number */
519static const char *check_output_sfx(hda_nid_t nid, const hda_nid_t *pins,
520 int num_pins, int *indexp)
521{
522 static const char * const channel_sfx[] = {
523 " Front", " Surround", " CLFE", " Side"
524 };
525 int i;
526
527 i = find_idx_in_nid_list(nid, pins, num_pins);
528 if (i < 0)
529 return NULL;
530 if (num_pins == 1)
531 return "";
532 if (num_pins > ARRAY_SIZE(channel_sfx)) {
533 if (indexp)
534 *indexp = i;
535 return "";
536 }
537 return channel_sfx[i];
538}
539
David Henningssoneee3ed42012-10-03 11:12:53 +0200540static const char *check_output_pfx(struct hda_codec *codec, hda_nid_t nid)
541{
542 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
543 int attr = snd_hda_get_input_pin_attr(def_conf);
544
545 /* check the location */
546 switch (attr) {
547 case INPUT_PIN_ATTR_DOCK:
548 return "Dock ";
549 case INPUT_PIN_ATTR_FRONT:
550 return "Front ";
551 }
552 return "";
553}
554
555static int get_hp_label_index(struct hda_codec *codec, hda_nid_t nid,
556 const hda_nid_t *pins, int num_pins)
557{
558 int i, j, idx = 0;
559
560 const char *pfx = check_output_pfx(codec, nid);
561
562 i = find_idx_in_nid_list(nid, pins, num_pins);
563 if (i < 0)
564 return -1;
565 for (j = 0; j < i; j++)
566 if (pfx == check_output_pfx(codec, pins[j]))
567 idx++;
568
569 return idx;
570}
571
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200572static int fill_audio_out_name(struct hda_codec *codec, hda_nid_t nid,
573 const struct auto_pin_cfg *cfg,
574 const char *name, char *label, int maxlen,
575 int *indexp)
576{
577 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
578 int attr = snd_hda_get_input_pin_attr(def_conf);
David Henningssoneee3ed42012-10-03 11:12:53 +0200579 const char *pfx, *sfx = "";
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200580
581 /* handle as a speaker if it's a fixed line-out */
582 if (!strcmp(name, "Line Out") && attr == INPUT_PIN_ATTR_INT)
583 name = "Speaker";
David Henningssoneee3ed42012-10-03 11:12:53 +0200584 pfx = check_output_pfx(codec, nid);
585
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200586 if (cfg) {
587 /* try to give a unique suffix if needed */
588 sfx = check_output_sfx(nid, cfg->line_out_pins, cfg->line_outs,
589 indexp);
590 if (!sfx)
591 sfx = check_output_sfx(nid, cfg->speaker_pins, cfg->speaker_outs,
592 indexp);
593 if (!sfx) {
594 /* don't add channel suffix for Headphone controls */
David Henningssoneee3ed42012-10-03 11:12:53 +0200595 int idx = get_hp_label_index(codec, nid, cfg->hp_pins,
596 cfg->hp_outs);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200597 if (idx >= 0)
598 *indexp = idx;
599 sfx = "";
600 }
601 }
602 snprintf(label, maxlen, "%s%s%s", pfx, name, sfx);
603 return 1;
604}
605
David Henningsson3f25dcf2013-01-18 15:43:03 +0100606#define is_hdmi_cfg(conf) \
607 (get_defcfg_location(conf) == AC_JACK_LOC_HDMI)
608
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200609/**
610 * snd_hda_get_pin_label - Get a label for the given I/O pin
611 *
612 * Get a label for the given pin. This function works for both input and
613 * output pins. When @cfg is given as non-NULL, the function tries to get
614 * an optimized label using hda_get_autocfg_input_label().
615 *
616 * This function tries to give a unique label string for the pin as much as
617 * possible. For example, when the multiple line-outs are present, it adds
618 * the channel suffix like "Front", "Surround", etc (only when @cfg is given).
619 * If no unique name with a suffix is available and @indexp is non-NULL, the
620 * index number is stored in the pointer.
621 */
622int snd_hda_get_pin_label(struct hda_codec *codec, hda_nid_t nid,
623 const struct auto_pin_cfg *cfg,
624 char *label, int maxlen, int *indexp)
625{
626 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
627 const char *name = NULL;
628 int i;
David Henningsson3f25dcf2013-01-18 15:43:03 +0100629 bool hdmi;
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200630
631 if (indexp)
632 *indexp = 0;
633 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
634 return 0;
635
636 switch (get_defcfg_device(def_conf)) {
637 case AC_JACK_LINE_OUT:
638 return fill_audio_out_name(codec, nid, cfg, "Line Out",
639 label, maxlen, indexp);
640 case AC_JACK_SPEAKER:
641 return fill_audio_out_name(codec, nid, cfg, "Speaker",
642 label, maxlen, indexp);
643 case AC_JACK_HP_OUT:
644 return fill_audio_out_name(codec, nid, cfg, "Headphone",
645 label, maxlen, indexp);
646 case AC_JACK_SPDIF_OUT:
647 case AC_JACK_DIG_OTHER_OUT:
David Henningsson3f25dcf2013-01-18 15:43:03 +0100648 hdmi = is_hdmi_cfg(def_conf);
649 name = hdmi ? "HDMI" : "SPDIF";
650 if (cfg && indexp)
651 for (i = 0; i < cfg->dig_outs; i++) {
652 hda_nid_t pin = cfg->dig_out_pins[i];
653 unsigned int c;
654 if (pin == nid)
655 break;
656 c = snd_hda_codec_get_pincfg(codec, pin);
657 if (hdmi == is_hdmi_cfg(c))
658 (*indexp)++;
659 }
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200660 break;
661 default:
662 if (cfg) {
663 for (i = 0; i < cfg->num_inputs; i++) {
664 if (cfg->inputs[i].pin != nid)
665 continue;
666 name = hda_get_autocfg_input_label(codec, cfg, i);
667 if (name)
668 break;
669 }
670 }
671 if (!name)
David Henningssona385d972013-03-21 12:16:29 +0100672 name = hda_get_input_pin_label(codec, NULL, nid, true);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200673 break;
674 }
675 if (!name)
676 return 0;
677 strlcpy(label, name, maxlen);
678 return 1;
679}
680EXPORT_SYMBOL_HDA(snd_hda_get_pin_label);
681
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100682int snd_hda_add_verbs(struct hda_codec *codec,
683 const struct hda_verb *list)
Takashi Iwai23d30f22012-05-07 17:17:32 +0200684{
685 const struct hda_verb **v;
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100686 v = snd_array_new(&codec->verbs);
Takashi Iwai23d30f22012-05-07 17:17:32 +0200687 if (!v)
688 return -ENOMEM;
689 *v = list;
690 return 0;
691}
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100692EXPORT_SYMBOL_HDA(snd_hda_add_verbs);
Takashi Iwai23d30f22012-05-07 17:17:32 +0200693
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100694void snd_hda_apply_verbs(struct hda_codec *codec)
Takashi Iwai23d30f22012-05-07 17:17:32 +0200695{
Takashi Iwai23d30f22012-05-07 17:17:32 +0200696 int i;
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100697 for (i = 0; i < codec->verbs.used; i++) {
698 struct hda_verb **v = snd_array_elem(&codec->verbs, i);
Takashi Iwai23d30f22012-05-07 17:17:32 +0200699 snd_hda_sequence_write(codec, *v);
700 }
701}
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100702EXPORT_SYMBOL_HDA(snd_hda_apply_verbs);
Takashi Iwai23d30f22012-05-07 17:17:32 +0200703
704void snd_hda_apply_pincfgs(struct hda_codec *codec,
705 const struct hda_pintbl *cfg)
706{
707 for (; cfg->nid; cfg++)
708 snd_hda_codec_set_pincfg(codec, cfg->nid, cfg->val);
709}
710EXPORT_SYMBOL_HDA(snd_hda_apply_pincfgs);
711
Takashi Iwaifd108212013-01-10 10:18:14 +0100712static void set_pin_targets(struct hda_codec *codec,
713 const struct hda_pintbl *cfg)
714{
715 for (; cfg->nid; cfg++)
716 snd_hda_set_pin_ctl_cache(codec, cfg->nid, cfg->val);
717}
718
Takashi Iwai1f578252013-01-23 18:10:10 +0100719static void apply_fixup(struct hda_codec *codec, int id, int action, int depth)
Takashi Iwai23d30f22012-05-07 17:17:32 +0200720{
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100721 const char *modelname = codec->fixup_name;
Takashi Iwai23d30f22012-05-07 17:17:32 +0200722
723 while (id >= 0) {
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100724 const struct hda_fixup *fix = codec->fixup_list + id;
Takashi Iwai23d30f22012-05-07 17:17:32 +0200725
Takashi Iwai1f578252013-01-23 18:10:10 +0100726 if (fix->chained_before)
727 apply_fixup(codec, fix->chain_id, action, depth + 1);
728
Takashi Iwai23d30f22012-05-07 17:17:32 +0200729 switch (fix->type) {
730 case HDA_FIXUP_PINS:
731 if (action != HDA_FIXUP_ACT_PRE_PROBE || !fix->v.pins)
732 break;
733 snd_printdd(KERN_INFO SFX
734 "%s: Apply pincfg for %s\n",
735 codec->chip_name, modelname);
736 snd_hda_apply_pincfgs(codec, fix->v.pins);
737 break;
738 case HDA_FIXUP_VERBS:
739 if (action != HDA_FIXUP_ACT_PROBE || !fix->v.verbs)
740 break;
741 snd_printdd(KERN_INFO SFX
742 "%s: Apply fix-verbs for %s\n",
743 codec->chip_name, modelname);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100744 snd_hda_add_verbs(codec, fix->v.verbs);
Takashi Iwai23d30f22012-05-07 17:17:32 +0200745 break;
746 case HDA_FIXUP_FUNC:
747 if (!fix->v.func)
748 break;
749 snd_printdd(KERN_INFO SFX
750 "%s: Apply fix-func for %s\n",
751 codec->chip_name, modelname);
752 fix->v.func(codec, fix, action);
753 break;
Takashi Iwaifd108212013-01-10 10:18:14 +0100754 case HDA_FIXUP_PINCTLS:
755 if (action != HDA_FIXUP_ACT_PROBE || !fix->v.pins)
756 break;
757 snd_printdd(KERN_INFO SFX
758 "%s: Apply pinctl for %s\n",
759 codec->chip_name, modelname);
760 set_pin_targets(codec, fix->v.pins);
761 break;
Takashi Iwai23d30f22012-05-07 17:17:32 +0200762 default:
763 snd_printk(KERN_ERR SFX
764 "%s: Invalid fixup type %d\n",
765 codec->chip_name, fix->type);
766 break;
767 }
Takashi Iwai1f578252013-01-23 18:10:10 +0100768 if (!fix->chained || fix->chained_before)
Takashi Iwai23d30f22012-05-07 17:17:32 +0200769 break;
770 if (++depth > 10)
771 break;
772 id = fix->chain_id;
773 }
774}
Takashi Iwai1f578252013-01-23 18:10:10 +0100775
776void snd_hda_apply_fixup(struct hda_codec *codec, int action)
777{
778 if (codec->fixup_list)
779 apply_fixup(codec, codec->fixup_id, action, 0);
780}
Takashi Iwai23d30f22012-05-07 17:17:32 +0200781EXPORT_SYMBOL_HDA(snd_hda_apply_fixup);
782
783void snd_hda_pick_fixup(struct hda_codec *codec,
784 const struct hda_model_fixup *models,
785 const struct snd_pci_quirk *quirk,
786 const struct hda_fixup *fixlist)
787{
Takashi Iwai23d30f22012-05-07 17:17:32 +0200788 const struct snd_pci_quirk *q;
789 int id = -1;
790 const char *name = NULL;
791
792 /* when model=nofixup is given, don't pick up any fixups */
793 if (codec->modelname && !strcmp(codec->modelname, "nofixup")) {
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100794 codec->fixup_list = NULL;
795 codec->fixup_id = -1;
Takashi Iwai23d30f22012-05-07 17:17:32 +0200796 return;
797 }
798
799 if (codec->modelname && models) {
800 while (models->name) {
801 if (!strcmp(codec->modelname, models->name)) {
802 id = models->id;
803 name = models->name;
804 break;
805 }
806 models++;
807 }
808 }
David Henningsson639aa4b2012-07-18 18:02:53 +0200809 if (id < 0 && quirk) {
Takashi Iwai23d30f22012-05-07 17:17:32 +0200810 q = snd_pci_quirk_lookup(codec->bus->pci, quirk);
811 if (q) {
812 id = q->value;
813#ifdef CONFIG_SND_DEBUG_VERBOSE
814 name = q->name;
815#endif
816 }
817 }
David Henningsson639aa4b2012-07-18 18:02:53 +0200818 if (id < 0 && quirk) {
Takashi Iwai23d30f22012-05-07 17:17:32 +0200819 for (q = quirk; q->subvendor; q++) {
820 unsigned int vendorid =
821 q->subdevice | (q->subvendor << 16);
Takashi Iwaia33b7b02012-09-11 16:42:18 +0200822 unsigned int mask = 0xffff0000 | q->subdevice_mask;
823 if ((codec->subsystem_id & mask) == (vendorid & mask)) {
Takashi Iwai23d30f22012-05-07 17:17:32 +0200824 id = q->value;
825#ifdef CONFIG_SND_DEBUG_VERBOSE
826 name = q->name;
827#endif
828 break;
829 }
830 }
831 }
832
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100833 codec->fixup_id = id;
Takashi Iwai23d30f22012-05-07 17:17:32 +0200834 if (id >= 0) {
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100835 codec->fixup_list = fixlist;
836 codec->fixup_name = name;
Takashi Iwai23d30f22012-05-07 17:17:32 +0200837 }
838}
839EXPORT_SYMBOL_HDA(snd_hda_pick_fixup);