blob: 47ad31c6aa702c1e0783b75f7b5f9ac8ed5bc5dc [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
David Henningssoncb420b12013-04-11 11:30:28 +0200122static bool can_be_headset_mic(struct hda_codec *codec,
123 struct auto_pin_cfg_item *item,
124 int seq_number)
125{
126 int attr;
127 unsigned int def_conf;
128 if (item->type != AUTO_PIN_MIC)
129 return false;
130
131 if (item->is_headset_mic || item->is_headphone_mic)
132 return false; /* Already assigned */
133
134 def_conf = snd_hda_codec_get_pincfg(codec, item->pin);
135 attr = snd_hda_get_input_pin_attr(def_conf);
136 if (attr <= INPUT_PIN_ATTR_DOCK)
137 return false;
138
139 if (seq_number >= 0) {
140 int seq = get_defcfg_sequence(def_conf);
141 if (seq != seq_number)
142 return false;
143 }
144
145 return true;
146}
147
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200148/*
149 * Parse all pin widgets and store the useful pin nids to cfg
150 *
151 * The number of line-outs or any primary output is stored in line_outs,
152 * and the corresponding output pins are assigned to line_out_pins[],
153 * in the order of front, rear, CLFE, side, ...
154 *
155 * If more extra outputs (speaker and headphone) are found, the pins are
156 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
157 * is detected, one of speaker of HP pins is assigned as the primary
158 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
159 * if any analog output exists.
160 *
161 * The analog input pins are assigned to inputs array.
162 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
163 * respectively.
164 */
165int snd_hda_parse_pin_defcfg(struct hda_codec *codec,
166 struct auto_pin_cfg *cfg,
167 const hda_nid_t *ignore_nids,
168 unsigned int cond_flags)
169{
170 hda_nid_t nid, end_nid;
171 short seq, assoc_line_out;
Takashi Iwaib9030a02012-11-29 10:18:57 +0100172 struct auto_out_pin line_out[ARRAY_SIZE(cfg->line_out_pins)];
173 struct auto_out_pin speaker_out[ARRAY_SIZE(cfg->speaker_pins)];
174 struct auto_out_pin hp_out[ARRAY_SIZE(cfg->hp_pins)];
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200175 int i;
176
Takashi Iwai1c70a582013-01-11 17:48:22 +0100177 if (!snd_hda_get_int_hint(codec, "parser_flags", &i))
178 cond_flags = i;
179
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200180 memset(cfg, 0, sizeof(*cfg));
181
Takashi Iwaib9030a02012-11-29 10:18:57 +0100182 memset(line_out, 0, sizeof(line_out));
183 memset(speaker_out, 0, sizeof(speaker_out));
184 memset(hp_out, 0, sizeof(hp_out));
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200185 assoc_line_out = 0;
186
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200187 end_nid = codec->start_nid + codec->num_nodes;
188 for (nid = codec->start_nid; nid < end_nid; nid++) {
189 unsigned int wid_caps = get_wcaps(codec, nid);
190 unsigned int wid_type = get_wcaps_type(wid_caps);
191 unsigned int def_conf;
192 short assoc, loc, conn, dev;
193
194 /* read all default configuration for pin complex */
195 if (wid_type != AC_WID_PIN)
196 continue;
197 /* ignore the given nids (e.g. pc-beep returns error) */
198 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
199 continue;
200
201 def_conf = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200202 conn = get_defcfg_connect(def_conf);
203 if (conn == AC_JACK_PORT_NONE)
204 continue;
205 loc = get_defcfg_location(def_conf);
206 dev = get_defcfg_device(def_conf);
207
208 /* workaround for buggy BIOS setups */
209 if (dev == AC_JACK_LINE_OUT) {
Takashi Iwaifb690cf2013-01-07 18:21:47 +0100210 if (conn == AC_JACK_PORT_FIXED ||
211 conn == AC_JACK_PORT_BOTH)
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200212 dev = AC_JACK_SPEAKER;
213 }
214
Takashi Iwai52fd5cb2013-01-15 08:45:33 +0100215 if (!check_pincap_validity(codec, nid, dev))
216 continue;
217
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200218 switch (dev) {
219 case AC_JACK_LINE_OUT:
220 seq = get_defcfg_sequence(def_conf);
221 assoc = get_defcfg_association(def_conf);
222
223 if (!(wid_caps & AC_WCAP_STEREO))
224 if (!cfg->mono_out_pin)
225 cfg->mono_out_pin = nid;
226 if (!assoc)
227 continue;
228 if (!assoc_line_out)
229 assoc_line_out = assoc;
230 else if (assoc_line_out != assoc)
231 continue;
232 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
233 continue;
Takashi Iwaib9030a02012-11-29 10:18:57 +0100234 line_out[cfg->line_outs].pin = nid;
235 line_out[cfg->line_outs].seq = seq;
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200236 cfg->line_outs++;
237 break;
238 case AC_JACK_SPEAKER:
239 seq = get_defcfg_sequence(def_conf);
240 assoc = get_defcfg_association(def_conf);
241 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
242 continue;
Takashi Iwaib9030a02012-11-29 10:18:57 +0100243 speaker_out[cfg->speaker_outs].pin = nid;
244 speaker_out[cfg->speaker_outs].seq = (assoc << 4) | seq;
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200245 cfg->speaker_outs++;
246 break;
247 case AC_JACK_HP_OUT:
248 seq = get_defcfg_sequence(def_conf);
249 assoc = get_defcfg_association(def_conf);
250 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
251 continue;
Takashi Iwaib9030a02012-11-29 10:18:57 +0100252 hp_out[cfg->hp_outs].pin = nid;
253 hp_out[cfg->hp_outs].seq = (assoc << 4) | seq;
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200254 cfg->hp_outs++;
255 break;
256 case AC_JACK_MIC_IN:
257 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_MIC);
258 break;
259 case AC_JACK_LINE_IN:
260 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_LINE_IN);
261 break;
262 case AC_JACK_CD:
263 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_CD);
264 break;
265 case AC_JACK_AUX:
266 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_AUX);
267 break;
268 case AC_JACK_SPDIF_OUT:
269 case AC_JACK_DIG_OTHER_OUT:
270 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
271 continue;
272 cfg->dig_out_pins[cfg->dig_outs] = nid;
273 cfg->dig_out_type[cfg->dig_outs] =
274 (loc == AC_JACK_LOC_HDMI) ?
275 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
276 cfg->dig_outs++;
277 break;
278 case AC_JACK_SPDIF_IN:
279 case AC_JACK_DIG_OTHER_IN:
280 cfg->dig_in_pin = nid;
281 if (loc == AC_JACK_LOC_HDMI)
282 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
283 else
284 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
285 break;
286 }
287 }
288
David Henningssoncb420b12013-04-11 11:30:28 +0200289 /* Find a pin that could be a headset or headphone mic */
290 if (cond_flags & HDA_PINCFG_HEADSET_MIC || cond_flags & HDA_PINCFG_HEADPHONE_MIC) {
291 bool hsmic = !!(cond_flags & HDA_PINCFG_HEADSET_MIC);
292 bool hpmic = !!(cond_flags & HDA_PINCFG_HEADPHONE_MIC);
293 for (i = 0; (hsmic || hpmic) && (i < cfg->num_inputs); i++)
294 if (hsmic && can_be_headset_mic(codec, &cfg->inputs[i], 0xc)) {
295 cfg->inputs[i].is_headset_mic = 1;
296 hsmic = false;
297 } else if (hpmic && can_be_headset_mic(codec, &cfg->inputs[i], 0xd)) {
298 cfg->inputs[i].is_headphone_mic = 1;
299 hpmic = false;
300 }
301
302 /* If we didn't find our sequence number mark, fall back to any sequence number */
303 for (i = 0; (hsmic || hpmic) && (i < cfg->num_inputs); i++) {
304 if (!can_be_headset_mic(codec, &cfg->inputs[i], -1))
David Henningssona385d972013-03-21 12:16:29 +0100305 continue;
David Henningssoncb420b12013-04-11 11:30:28 +0200306 if (hsmic) {
307 cfg->inputs[i].is_headset_mic = 1;
308 hsmic = false;
309 } else if (hpmic) {
310 cfg->inputs[i].is_headphone_mic = 1;
311 hpmic = false;
312 }
David Henningssona385d972013-03-21 12:16:29 +0100313 }
David Henningssoncb420b12013-04-11 11:30:28 +0200314
315 if (hsmic)
316 snd_printdd("Told to look for a headset mic, but didn't find any.\n");
317 if (hpmic)
318 snd_printdd("Told to look for a headphone mic, but didn't find any.\n");
David Henningssona385d972013-03-21 12:16:29 +0100319 }
320
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200321 /* FIX-UP:
322 * If no line-out is defined but multiple HPs are found,
323 * some of them might be the real line-outs.
324 */
325 if (!cfg->line_outs && cfg->hp_outs > 1 &&
326 !(cond_flags & HDA_PINCFG_NO_HP_FIXUP)) {
327 int i = 0;
328 while (i < cfg->hp_outs) {
329 /* The real HPs should have the sequence 0x0f */
Takashi Iwaib9030a02012-11-29 10:18:57 +0100330 if ((hp_out[i].seq & 0x0f) == 0x0f) {
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200331 i++;
332 continue;
333 }
334 /* Move it to the line-out table */
Takashi Iwaib9030a02012-11-29 10:18:57 +0100335 line_out[cfg->line_outs++] = hp_out[i];
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200336 cfg->hp_outs--;
Takashi Iwaib9030a02012-11-29 10:18:57 +0100337 memmove(hp_out + i, hp_out + i + 1,
338 sizeof(hp_out[0]) * (cfg->hp_outs - i));
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200339 }
Takashi Iwaib9030a02012-11-29 10:18:57 +0100340 memset(hp_out + cfg->hp_outs, 0,
341 sizeof(hp_out[0]) * (AUTO_CFG_MAX_OUTS - cfg->hp_outs));
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200342 if (!cfg->hp_outs)
343 cfg->line_out_type = AUTO_PIN_HP_OUT;
344
345 }
346
347 /* sort by sequence */
Takashi Iwaib9030a02012-11-29 10:18:57 +0100348 sort_pins_by_sequence(cfg->line_out_pins, line_out, cfg->line_outs);
349 sort_pins_by_sequence(cfg->speaker_pins, speaker_out,
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200350 cfg->speaker_outs);
Takashi Iwaib9030a02012-11-29 10:18:57 +0100351 sort_pins_by_sequence(cfg->hp_pins, hp_out, cfg->hp_outs);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200352
353 /*
354 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
355 * as a primary output
356 */
357 if (!cfg->line_outs &&
358 !(cond_flags & HDA_PINCFG_NO_LO_FIXUP)) {
359 if (cfg->speaker_outs) {
360 cfg->line_outs = cfg->speaker_outs;
361 memcpy(cfg->line_out_pins, cfg->speaker_pins,
362 sizeof(cfg->speaker_pins));
363 cfg->speaker_outs = 0;
364 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
365 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
366 } else if (cfg->hp_outs) {
367 cfg->line_outs = cfg->hp_outs;
368 memcpy(cfg->line_out_pins, cfg->hp_pins,
369 sizeof(cfg->hp_pins));
370 cfg->hp_outs = 0;
371 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
372 cfg->line_out_type = AUTO_PIN_HP_OUT;
373 }
374 }
375
376 reorder_outputs(cfg->line_outs, cfg->line_out_pins);
377 reorder_outputs(cfg->hp_outs, cfg->hp_pins);
378 reorder_outputs(cfg->speaker_outs, cfg->speaker_pins);
379
Takashi Iwaib9030a02012-11-29 10:18:57 +0100380 /* sort inputs in the order of AUTO_PIN_* type */
381 sort(cfg->inputs, cfg->num_inputs, sizeof(cfg->inputs[0]),
382 compare_input_type, NULL);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200383
384 /*
385 * debug prints of the parsed results
386 */
387 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x) type:%s\n",
388 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
389 cfg->line_out_pins[2], cfg->line_out_pins[3],
390 cfg->line_out_pins[4],
391 cfg->line_out_type == AUTO_PIN_HP_OUT ? "hp" :
392 (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT ?
393 "speaker" : "line"));
394 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
395 cfg->speaker_outs, cfg->speaker_pins[0],
396 cfg->speaker_pins[1], cfg->speaker_pins[2],
397 cfg->speaker_pins[3], cfg->speaker_pins[4]);
398 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
399 cfg->hp_outs, cfg->hp_pins[0],
400 cfg->hp_pins[1], cfg->hp_pins[2],
401 cfg->hp_pins[3], cfg->hp_pins[4]);
402 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
403 if (cfg->dig_outs)
404 snd_printd(" dig-out=0x%x/0x%x\n",
405 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
Takashi Iwai709aea62012-08-07 18:09:23 +0200406 snd_printd(" inputs:\n");
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200407 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwai709aea62012-08-07 18:09:23 +0200408 snd_printd(" %s=0x%x\n",
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200409 hda_get_autocfg_input_label(codec, cfg, i),
410 cfg->inputs[i].pin);
411 }
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200412 if (cfg->dig_in_pin)
413 snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
414
415 return 0;
416}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100417EXPORT_SYMBOL_GPL(snd_hda_parse_pin_defcfg);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200418
419int snd_hda_get_input_pin_attr(unsigned int def_conf)
420{
421 unsigned int loc = get_defcfg_location(def_conf);
422 unsigned int conn = get_defcfg_connect(def_conf);
423 if (conn == AC_JACK_PORT_NONE)
424 return INPUT_PIN_ATTR_UNUSED;
425 /* Windows may claim the internal mic to be BOTH, too */
426 if (conn == AC_JACK_PORT_FIXED || conn == AC_JACK_PORT_BOTH)
427 return INPUT_PIN_ATTR_INT;
428 if ((loc & 0x30) == AC_JACK_LOC_INTERNAL)
429 return INPUT_PIN_ATTR_INT;
430 if ((loc & 0x30) == AC_JACK_LOC_SEPARATE)
431 return INPUT_PIN_ATTR_DOCK;
432 if (loc == AC_JACK_LOC_REAR)
433 return INPUT_PIN_ATTR_REAR;
434 if (loc == AC_JACK_LOC_FRONT)
435 return INPUT_PIN_ATTR_FRONT;
436 return INPUT_PIN_ATTR_NORMAL;
437}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100438EXPORT_SYMBOL_GPL(snd_hda_get_input_pin_attr);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200439
440/**
441 * hda_get_input_pin_label - Give a label for the given input pin
442 *
443 * When check_location is true, the function checks the pin location
444 * for mic and line-in pins, and set an appropriate prefix like "Front",
445 * "Rear", "Internal".
446 */
447
448static const char *hda_get_input_pin_label(struct hda_codec *codec,
David Henningssona385d972013-03-21 12:16:29 +0100449 const struct auto_pin_cfg_item *item,
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200450 hda_nid_t pin, bool check_location)
451{
452 unsigned int def_conf;
453 static const char * const mic_names[] = {
Takashi Iwai5ec16d12012-11-28 18:11:59 +0100454 "Internal Mic", "Dock Mic", "Mic", "Rear Mic", "Front Mic"
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200455 };
456 int attr;
457
458 def_conf = snd_hda_codec_get_pincfg(codec, pin);
459
460 switch (get_defcfg_device(def_conf)) {
461 case AC_JACK_MIC_IN:
David Henningssona385d972013-03-21 12:16:29 +0100462 if (item && item->is_headset_mic)
463 return "Headset Mic";
David Henningssoncb420b12013-04-11 11:30:28 +0200464 if (item && item->is_headphone_mic)
465 return "Headphone Mic";
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200466 if (!check_location)
467 return "Mic";
468 attr = snd_hda_get_input_pin_attr(def_conf);
469 if (!attr)
470 return "None";
471 return mic_names[attr - 1];
472 case AC_JACK_LINE_IN:
473 if (!check_location)
474 return "Line";
475 attr = snd_hda_get_input_pin_attr(def_conf);
476 if (!attr)
477 return "None";
478 if (attr == INPUT_PIN_ATTR_DOCK)
479 return "Dock Line";
480 return "Line";
481 case AC_JACK_AUX:
482 return "Aux";
483 case AC_JACK_CD:
484 return "CD";
485 case AC_JACK_SPDIF_IN:
486 return "SPDIF In";
487 case AC_JACK_DIG_OTHER_IN:
488 return "Digital In";
Takashi Iwai54d778b2013-01-09 08:46:34 +0100489 case AC_JACK_HP_OUT:
490 return "Headphone Mic";
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200491 default:
492 return "Misc";
493 }
494}
495
496/* Check whether the location prefix needs to be added to the label.
497 * If all mic-jacks are in the same location (e.g. rear panel), we don't
498 * have to put "Front" prefix to each label. In such a case, returns false.
499 */
500static int check_mic_location_need(struct hda_codec *codec,
501 const struct auto_pin_cfg *cfg,
502 int input)
503{
504 unsigned int defc;
505 int i, attr, attr2;
506
507 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[input].pin);
508 attr = snd_hda_get_input_pin_attr(defc);
509 /* for internal or docking mics, we need locations */
510 if (attr <= INPUT_PIN_ATTR_NORMAL)
511 return 1;
512
513 attr = 0;
514 for (i = 0; i < cfg->num_inputs; i++) {
515 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[i].pin);
516 attr2 = snd_hda_get_input_pin_attr(defc);
517 if (attr2 >= INPUT_PIN_ATTR_NORMAL) {
518 if (attr && attr != attr2)
519 return 1; /* different locations found */
520 attr = attr2;
521 }
522 }
523 return 0;
524}
525
526/**
527 * hda_get_autocfg_input_label - Get a label for the given input
528 *
529 * Get a label for the given input pin defined by the autocfg item.
530 * Unlike hda_get_input_pin_label(), this function checks all inputs
531 * defined in autocfg and avoids the redundant mic/line prefix as much as
532 * possible.
533 */
534const char *hda_get_autocfg_input_label(struct hda_codec *codec,
535 const struct auto_pin_cfg *cfg,
536 int input)
537{
538 int type = cfg->inputs[input].type;
539 int has_multiple_pins = 0;
540
541 if ((input > 0 && cfg->inputs[input - 1].type == type) ||
542 (input < cfg->num_inputs - 1 && cfg->inputs[input + 1].type == type))
543 has_multiple_pins = 1;
544 if (has_multiple_pins && type == AUTO_PIN_MIC)
545 has_multiple_pins &= check_mic_location_need(codec, cfg, input);
David Henningssona385d972013-03-21 12:16:29 +0100546 return hda_get_input_pin_label(codec, &cfg->inputs[input],
547 cfg->inputs[input].pin,
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200548 has_multiple_pins);
549}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100550EXPORT_SYMBOL_GPL(hda_get_autocfg_input_label);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200551
552/* return the position of NID in the list, or -1 if not found */
553static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
554{
555 int i;
556 for (i = 0; i < nums; i++)
557 if (list[i] == nid)
558 return i;
559 return -1;
560}
561
562/* get a unique suffix or an index number */
563static const char *check_output_sfx(hda_nid_t nid, const hda_nid_t *pins,
564 int num_pins, int *indexp)
565{
566 static const char * const channel_sfx[] = {
567 " Front", " Surround", " CLFE", " Side"
568 };
569 int i;
570
571 i = find_idx_in_nid_list(nid, pins, num_pins);
572 if (i < 0)
573 return NULL;
574 if (num_pins == 1)
575 return "";
576 if (num_pins > ARRAY_SIZE(channel_sfx)) {
577 if (indexp)
578 *indexp = i;
579 return "";
580 }
581 return channel_sfx[i];
582}
583
David Henningssoneee3ed42012-10-03 11:12:53 +0200584static const char *check_output_pfx(struct hda_codec *codec, hda_nid_t nid)
585{
586 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
587 int attr = snd_hda_get_input_pin_attr(def_conf);
588
589 /* check the location */
590 switch (attr) {
591 case INPUT_PIN_ATTR_DOCK:
592 return "Dock ";
593 case INPUT_PIN_ATTR_FRONT:
594 return "Front ";
595 }
596 return "";
597}
598
599static int get_hp_label_index(struct hda_codec *codec, hda_nid_t nid,
600 const hda_nid_t *pins, int num_pins)
601{
602 int i, j, idx = 0;
603
604 const char *pfx = check_output_pfx(codec, nid);
605
606 i = find_idx_in_nid_list(nid, pins, num_pins);
607 if (i < 0)
608 return -1;
609 for (j = 0; j < i; j++)
610 if (pfx == check_output_pfx(codec, pins[j]))
611 idx++;
612
613 return idx;
614}
615
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200616static int fill_audio_out_name(struct hda_codec *codec, hda_nid_t nid,
617 const struct auto_pin_cfg *cfg,
618 const char *name, char *label, int maxlen,
619 int *indexp)
620{
621 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
622 int attr = snd_hda_get_input_pin_attr(def_conf);
David Henningssoneee3ed42012-10-03 11:12:53 +0200623 const char *pfx, *sfx = "";
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200624
625 /* handle as a speaker if it's a fixed line-out */
626 if (!strcmp(name, "Line Out") && attr == INPUT_PIN_ATTR_INT)
627 name = "Speaker";
David Henningssoneee3ed42012-10-03 11:12:53 +0200628 pfx = check_output_pfx(codec, nid);
629
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200630 if (cfg) {
631 /* try to give a unique suffix if needed */
632 sfx = check_output_sfx(nid, cfg->line_out_pins, cfg->line_outs,
633 indexp);
634 if (!sfx)
635 sfx = check_output_sfx(nid, cfg->speaker_pins, cfg->speaker_outs,
636 indexp);
637 if (!sfx) {
638 /* don't add channel suffix for Headphone controls */
David Henningssoneee3ed42012-10-03 11:12:53 +0200639 int idx = get_hp_label_index(codec, nid, cfg->hp_pins,
640 cfg->hp_outs);
Takashi Iwai728deec2013-10-28 11:39:23 +0100641 if (idx >= 0 && indexp)
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200642 *indexp = idx;
643 sfx = "";
644 }
645 }
646 snprintf(label, maxlen, "%s%s%s", pfx, name, sfx);
647 return 1;
648}
649
David Henningsson3f25dcf2013-01-18 15:43:03 +0100650#define is_hdmi_cfg(conf) \
651 (get_defcfg_location(conf) == AC_JACK_LOC_HDMI)
652
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200653/**
654 * snd_hda_get_pin_label - Get a label for the given I/O pin
655 *
656 * Get a label for the given pin. This function works for both input and
657 * output pins. When @cfg is given as non-NULL, the function tries to get
658 * an optimized label using hda_get_autocfg_input_label().
659 *
660 * This function tries to give a unique label string for the pin as much as
661 * possible. For example, when the multiple line-outs are present, it adds
662 * the channel suffix like "Front", "Surround", etc (only when @cfg is given).
663 * If no unique name with a suffix is available and @indexp is non-NULL, the
664 * index number is stored in the pointer.
665 */
666int snd_hda_get_pin_label(struct hda_codec *codec, hda_nid_t nid,
667 const struct auto_pin_cfg *cfg,
668 char *label, int maxlen, int *indexp)
669{
670 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
671 const char *name = NULL;
672 int i;
David Henningsson3f25dcf2013-01-18 15:43:03 +0100673 bool hdmi;
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200674
675 if (indexp)
676 *indexp = 0;
677 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
678 return 0;
679
680 switch (get_defcfg_device(def_conf)) {
681 case AC_JACK_LINE_OUT:
682 return fill_audio_out_name(codec, nid, cfg, "Line Out",
683 label, maxlen, indexp);
684 case AC_JACK_SPEAKER:
685 return fill_audio_out_name(codec, nid, cfg, "Speaker",
686 label, maxlen, indexp);
687 case AC_JACK_HP_OUT:
688 return fill_audio_out_name(codec, nid, cfg, "Headphone",
689 label, maxlen, indexp);
690 case AC_JACK_SPDIF_OUT:
691 case AC_JACK_DIG_OTHER_OUT:
David Henningsson3f25dcf2013-01-18 15:43:03 +0100692 hdmi = is_hdmi_cfg(def_conf);
693 name = hdmi ? "HDMI" : "SPDIF";
694 if (cfg && indexp)
695 for (i = 0; i < cfg->dig_outs; i++) {
696 hda_nid_t pin = cfg->dig_out_pins[i];
697 unsigned int c;
698 if (pin == nid)
699 break;
700 c = snd_hda_codec_get_pincfg(codec, pin);
701 if (hdmi == is_hdmi_cfg(c))
702 (*indexp)++;
703 }
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200704 break;
705 default:
706 if (cfg) {
707 for (i = 0; i < cfg->num_inputs; i++) {
708 if (cfg->inputs[i].pin != nid)
709 continue;
710 name = hda_get_autocfg_input_label(codec, cfg, i);
711 if (name)
712 break;
713 }
714 }
715 if (!name)
David Henningssona385d972013-03-21 12:16:29 +0100716 name = hda_get_input_pin_label(codec, NULL, nid, true);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200717 break;
718 }
719 if (!name)
720 return 0;
721 strlcpy(label, name, maxlen);
722 return 1;
723}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100724EXPORT_SYMBOL_GPL(snd_hda_get_pin_label);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200725
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100726int snd_hda_add_verbs(struct hda_codec *codec,
727 const struct hda_verb *list)
Takashi Iwai23d30f22012-05-07 17:17:32 +0200728{
729 const struct hda_verb **v;
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100730 v = snd_array_new(&codec->verbs);
Takashi Iwai23d30f22012-05-07 17:17:32 +0200731 if (!v)
732 return -ENOMEM;
733 *v = list;
734 return 0;
735}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100736EXPORT_SYMBOL_GPL(snd_hda_add_verbs);
Takashi Iwai23d30f22012-05-07 17:17:32 +0200737
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100738void snd_hda_apply_verbs(struct hda_codec *codec)
Takashi Iwai23d30f22012-05-07 17:17:32 +0200739{
Takashi Iwai23d30f22012-05-07 17:17:32 +0200740 int i;
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100741 for (i = 0; i < codec->verbs.used; i++) {
742 struct hda_verb **v = snd_array_elem(&codec->verbs, i);
Takashi Iwai23d30f22012-05-07 17:17:32 +0200743 snd_hda_sequence_write(codec, *v);
744 }
745}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100746EXPORT_SYMBOL_GPL(snd_hda_apply_verbs);
Takashi Iwai23d30f22012-05-07 17:17:32 +0200747
748void snd_hda_apply_pincfgs(struct hda_codec *codec,
749 const struct hda_pintbl *cfg)
750{
751 for (; cfg->nid; cfg++)
752 snd_hda_codec_set_pincfg(codec, cfg->nid, cfg->val);
753}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100754EXPORT_SYMBOL_GPL(snd_hda_apply_pincfgs);
Takashi Iwai23d30f22012-05-07 17:17:32 +0200755
Takashi Iwaifd108212013-01-10 10:18:14 +0100756static void set_pin_targets(struct hda_codec *codec,
757 const struct hda_pintbl *cfg)
758{
759 for (; cfg->nid; cfg++)
760 snd_hda_set_pin_ctl_cache(codec, cfg->nid, cfg->val);
761}
762
Takashi Iwai1f578252013-01-23 18:10:10 +0100763static void apply_fixup(struct hda_codec *codec, int id, int action, int depth)
Takashi Iwai23d30f22012-05-07 17:17:32 +0200764{
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100765 const char *modelname = codec->fixup_name;
Takashi Iwai23d30f22012-05-07 17:17:32 +0200766
767 while (id >= 0) {
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100768 const struct hda_fixup *fix = codec->fixup_list + id;
Takashi Iwai23d30f22012-05-07 17:17:32 +0200769
Takashi Iwai1f578252013-01-23 18:10:10 +0100770 if (fix->chained_before)
771 apply_fixup(codec, fix->chain_id, action, depth + 1);
772
Takashi Iwai23d30f22012-05-07 17:17:32 +0200773 switch (fix->type) {
774 case HDA_FIXUP_PINS:
775 if (action != HDA_FIXUP_ACT_PRE_PROBE || !fix->v.pins)
776 break;
777 snd_printdd(KERN_INFO SFX
778 "%s: Apply pincfg for %s\n",
779 codec->chip_name, modelname);
780 snd_hda_apply_pincfgs(codec, fix->v.pins);
781 break;
782 case HDA_FIXUP_VERBS:
783 if (action != HDA_FIXUP_ACT_PROBE || !fix->v.verbs)
784 break;
785 snd_printdd(KERN_INFO SFX
786 "%s: Apply fix-verbs for %s\n",
787 codec->chip_name, modelname);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100788 snd_hda_add_verbs(codec, fix->v.verbs);
Takashi Iwai23d30f22012-05-07 17:17:32 +0200789 break;
790 case HDA_FIXUP_FUNC:
791 if (!fix->v.func)
792 break;
793 snd_printdd(KERN_INFO SFX
794 "%s: Apply fix-func for %s\n",
795 codec->chip_name, modelname);
796 fix->v.func(codec, fix, action);
797 break;
Takashi Iwaifd108212013-01-10 10:18:14 +0100798 case HDA_FIXUP_PINCTLS:
799 if (action != HDA_FIXUP_ACT_PROBE || !fix->v.pins)
800 break;
801 snd_printdd(KERN_INFO SFX
802 "%s: Apply pinctl for %s\n",
803 codec->chip_name, modelname);
804 set_pin_targets(codec, fix->v.pins);
805 break;
Takashi Iwai23d30f22012-05-07 17:17:32 +0200806 default:
807 snd_printk(KERN_ERR SFX
808 "%s: Invalid fixup type %d\n",
809 codec->chip_name, fix->type);
810 break;
811 }
Takashi Iwai1f578252013-01-23 18:10:10 +0100812 if (!fix->chained || fix->chained_before)
Takashi Iwai23d30f22012-05-07 17:17:32 +0200813 break;
814 if (++depth > 10)
815 break;
816 id = fix->chain_id;
817 }
818}
Takashi Iwai1f578252013-01-23 18:10:10 +0100819
820void snd_hda_apply_fixup(struct hda_codec *codec, int action)
821{
822 if (codec->fixup_list)
823 apply_fixup(codec, codec->fixup_id, action, 0);
824}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100825EXPORT_SYMBOL_GPL(snd_hda_apply_fixup);
Takashi Iwai23d30f22012-05-07 17:17:32 +0200826
827void snd_hda_pick_fixup(struct hda_codec *codec,
828 const struct hda_model_fixup *models,
829 const struct snd_pci_quirk *quirk,
830 const struct hda_fixup *fixlist)
831{
Takashi Iwai23d30f22012-05-07 17:17:32 +0200832 const struct snd_pci_quirk *q;
833 int id = -1;
834 const char *name = NULL;
835
836 /* when model=nofixup is given, don't pick up any fixups */
837 if (codec->modelname && !strcmp(codec->modelname, "nofixup")) {
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100838 codec->fixup_list = NULL;
839 codec->fixup_id = -1;
Takashi Iwai23d30f22012-05-07 17:17:32 +0200840 return;
841 }
842
843 if (codec->modelname && models) {
844 while (models->name) {
845 if (!strcmp(codec->modelname, models->name)) {
846 id = models->id;
847 name = models->name;
848 break;
849 }
850 models++;
851 }
852 }
David Henningsson639aa4b2012-07-18 18:02:53 +0200853 if (id < 0 && quirk) {
Takashi Iwai23d30f22012-05-07 17:17:32 +0200854 q = snd_pci_quirk_lookup(codec->bus->pci, quirk);
855 if (q) {
856 id = q->value;
857#ifdef CONFIG_SND_DEBUG_VERBOSE
858 name = q->name;
859#endif
860 }
861 }
David Henningsson639aa4b2012-07-18 18:02:53 +0200862 if (id < 0 && quirk) {
Takashi Iwai697aeba2013-08-01 08:38:27 +0200863 for (q = quirk; q->subvendor || q->subdevice; q++) {
Takashi Iwai23d30f22012-05-07 17:17:32 +0200864 unsigned int vendorid =
865 q->subdevice | (q->subvendor << 16);
Takashi Iwaia33b7b02012-09-11 16:42:18 +0200866 unsigned int mask = 0xffff0000 | q->subdevice_mask;
867 if ((codec->subsystem_id & mask) == (vendorid & mask)) {
Takashi Iwai23d30f22012-05-07 17:17:32 +0200868 id = q->value;
869#ifdef CONFIG_SND_DEBUG_VERBOSE
870 name = q->name;
871#endif
872 break;
873 }
874 }
875 }
876
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100877 codec->fixup_id = id;
Takashi Iwai23d30f22012-05-07 17:17:32 +0200878 if (id >= 0) {
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100879 codec->fixup_list = fixlist;
880 codec->fixup_name = name;
Takashi Iwai23d30f22012-05-07 17:17:32 +0200881 }
882}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100883EXPORT_SYMBOL_GPL(snd_hda_pick_fixup);