blob: 90d2fda6c8f99935b810425e64d98b6840e333a4 [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;
Takashi Iwai9b7564a2014-03-25 10:02:01 +0100230 else if (assoc_line_out != assoc) {
231 codec_info(codec,
232 "ignore pin 0x%x with mismatching assoc# 0x%x vs 0x%x\n",
233 nid, assoc, assoc_line_out);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200234 continue;
Takashi Iwai9b7564a2014-03-25 10:02:01 +0100235 }
236 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins)) {
237 codec_info(codec,
238 "ignore pin 0x%x, too many assigned pins\n",
239 nid);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200240 continue;
Takashi Iwai9b7564a2014-03-25 10:02:01 +0100241 }
Takashi Iwaib9030a02012-11-29 10:18:57 +0100242 line_out[cfg->line_outs].pin = nid;
243 line_out[cfg->line_outs].seq = seq;
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200244 cfg->line_outs++;
245 break;
246 case AC_JACK_SPEAKER:
247 seq = get_defcfg_sequence(def_conf);
248 assoc = get_defcfg_association(def_conf);
Takashi Iwai9b7564a2014-03-25 10:02:01 +0100249 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins)) {
250 codec_info(codec,
251 "ignore pin 0x%x, too many assigned pins\n",
252 nid);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200253 continue;
Takashi Iwai9b7564a2014-03-25 10:02:01 +0100254 }
Takashi Iwaib9030a02012-11-29 10:18:57 +0100255 speaker_out[cfg->speaker_outs].pin = nid;
256 speaker_out[cfg->speaker_outs].seq = (assoc << 4) | seq;
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200257 cfg->speaker_outs++;
258 break;
259 case AC_JACK_HP_OUT:
260 seq = get_defcfg_sequence(def_conf);
261 assoc = get_defcfg_association(def_conf);
Takashi Iwai9b7564a2014-03-25 10:02:01 +0100262 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins)) {
263 codec_info(codec,
264 "ignore pin 0x%x, too many assigned pins\n",
265 nid);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200266 continue;
Takashi Iwai9b7564a2014-03-25 10:02:01 +0100267 }
Takashi Iwaib9030a02012-11-29 10:18:57 +0100268 hp_out[cfg->hp_outs].pin = nid;
269 hp_out[cfg->hp_outs].seq = (assoc << 4) | seq;
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200270 cfg->hp_outs++;
271 break;
272 case AC_JACK_MIC_IN:
273 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_MIC);
274 break;
275 case AC_JACK_LINE_IN:
276 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_LINE_IN);
277 break;
278 case AC_JACK_CD:
279 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_CD);
280 break;
281 case AC_JACK_AUX:
282 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_AUX);
283 break;
284 case AC_JACK_SPDIF_OUT:
285 case AC_JACK_DIG_OTHER_OUT:
Takashi Iwai9b7564a2014-03-25 10:02:01 +0100286 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins)) {
287 codec_info(codec,
288 "ignore pin 0x%x, too many assigned pins\n",
289 nid);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200290 continue;
Takashi Iwai9b7564a2014-03-25 10:02:01 +0100291 }
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200292 cfg->dig_out_pins[cfg->dig_outs] = nid;
293 cfg->dig_out_type[cfg->dig_outs] =
294 (loc == AC_JACK_LOC_HDMI) ?
295 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
296 cfg->dig_outs++;
297 break;
298 case AC_JACK_SPDIF_IN:
299 case AC_JACK_DIG_OTHER_IN:
300 cfg->dig_in_pin = nid;
301 if (loc == AC_JACK_LOC_HDMI)
302 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
303 else
304 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
305 break;
306 }
307 }
308
David Henningssoncb420b12013-04-11 11:30:28 +0200309 /* Find a pin that could be a headset or headphone mic */
310 if (cond_flags & HDA_PINCFG_HEADSET_MIC || cond_flags & HDA_PINCFG_HEADPHONE_MIC) {
311 bool hsmic = !!(cond_flags & HDA_PINCFG_HEADSET_MIC);
312 bool hpmic = !!(cond_flags & HDA_PINCFG_HEADPHONE_MIC);
313 for (i = 0; (hsmic || hpmic) && (i < cfg->num_inputs); i++)
314 if (hsmic && can_be_headset_mic(codec, &cfg->inputs[i], 0xc)) {
315 cfg->inputs[i].is_headset_mic = 1;
316 hsmic = false;
317 } else if (hpmic && can_be_headset_mic(codec, &cfg->inputs[i], 0xd)) {
318 cfg->inputs[i].is_headphone_mic = 1;
319 hpmic = false;
320 }
321
322 /* If we didn't find our sequence number mark, fall back to any sequence number */
323 for (i = 0; (hsmic || hpmic) && (i < cfg->num_inputs); i++) {
324 if (!can_be_headset_mic(codec, &cfg->inputs[i], -1))
David Henningssona385d972013-03-21 12:16:29 +0100325 continue;
David Henningssoncb420b12013-04-11 11:30:28 +0200326 if (hsmic) {
327 cfg->inputs[i].is_headset_mic = 1;
328 hsmic = false;
329 } else if (hpmic) {
330 cfg->inputs[i].is_headphone_mic = 1;
331 hpmic = false;
332 }
David Henningssona385d972013-03-21 12:16:29 +0100333 }
David Henningssoncb420b12013-04-11 11:30:28 +0200334
335 if (hsmic)
Takashi Iwai4e76a882014-02-25 12:21:03 +0100336 codec_dbg(codec, "Told to look for a headset mic, but didn't find any.\n");
David Henningssoncb420b12013-04-11 11:30:28 +0200337 if (hpmic)
Takashi Iwai4e76a882014-02-25 12:21:03 +0100338 codec_dbg(codec, "Told to look for a headphone mic, but didn't find any.\n");
David Henningssona385d972013-03-21 12:16:29 +0100339 }
340
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200341 /* FIX-UP:
342 * If no line-out is defined but multiple HPs are found,
343 * some of them might be the real line-outs.
344 */
345 if (!cfg->line_outs && cfg->hp_outs > 1 &&
346 !(cond_flags & HDA_PINCFG_NO_HP_FIXUP)) {
347 int i = 0;
348 while (i < cfg->hp_outs) {
349 /* The real HPs should have the sequence 0x0f */
Takashi Iwaib9030a02012-11-29 10:18:57 +0100350 if ((hp_out[i].seq & 0x0f) == 0x0f) {
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200351 i++;
352 continue;
353 }
354 /* Move it to the line-out table */
Takashi Iwaib9030a02012-11-29 10:18:57 +0100355 line_out[cfg->line_outs++] = hp_out[i];
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200356 cfg->hp_outs--;
Takashi Iwaib9030a02012-11-29 10:18:57 +0100357 memmove(hp_out + i, hp_out + i + 1,
358 sizeof(hp_out[0]) * (cfg->hp_outs - i));
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200359 }
Takashi Iwaib9030a02012-11-29 10:18:57 +0100360 memset(hp_out + cfg->hp_outs, 0,
361 sizeof(hp_out[0]) * (AUTO_CFG_MAX_OUTS - cfg->hp_outs));
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200362 if (!cfg->hp_outs)
363 cfg->line_out_type = AUTO_PIN_HP_OUT;
364
365 }
366
367 /* sort by sequence */
Takashi Iwaib9030a02012-11-29 10:18:57 +0100368 sort_pins_by_sequence(cfg->line_out_pins, line_out, cfg->line_outs);
369 sort_pins_by_sequence(cfg->speaker_pins, speaker_out,
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200370 cfg->speaker_outs);
Takashi Iwaib9030a02012-11-29 10:18:57 +0100371 sort_pins_by_sequence(cfg->hp_pins, hp_out, cfg->hp_outs);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200372
373 /*
374 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
375 * as a primary output
376 */
377 if (!cfg->line_outs &&
378 !(cond_flags & HDA_PINCFG_NO_LO_FIXUP)) {
379 if (cfg->speaker_outs) {
380 cfg->line_outs = cfg->speaker_outs;
381 memcpy(cfg->line_out_pins, cfg->speaker_pins,
382 sizeof(cfg->speaker_pins));
383 cfg->speaker_outs = 0;
384 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
385 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
386 } else if (cfg->hp_outs) {
387 cfg->line_outs = cfg->hp_outs;
388 memcpy(cfg->line_out_pins, cfg->hp_pins,
389 sizeof(cfg->hp_pins));
390 cfg->hp_outs = 0;
391 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
392 cfg->line_out_type = AUTO_PIN_HP_OUT;
393 }
394 }
395
396 reorder_outputs(cfg->line_outs, cfg->line_out_pins);
397 reorder_outputs(cfg->hp_outs, cfg->hp_pins);
398 reorder_outputs(cfg->speaker_outs, cfg->speaker_pins);
399
Takashi Iwaib9030a02012-11-29 10:18:57 +0100400 /* sort inputs in the order of AUTO_PIN_* type */
401 sort(cfg->inputs, cfg->num_inputs, sizeof(cfg->inputs[0]),
402 compare_input_type, NULL);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200403
404 /*
405 * debug prints of the parsed results
406 */
Takashi Iwai4e76a882014-02-25 12:21:03 +0100407 codec_info(codec, "autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x) type:%s\n",
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200408 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
409 cfg->line_out_pins[2], cfg->line_out_pins[3],
410 cfg->line_out_pins[4],
411 cfg->line_out_type == AUTO_PIN_HP_OUT ? "hp" :
412 (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT ?
413 "speaker" : "line"));
Takashi Iwai4e76a882014-02-25 12:21:03 +0100414 codec_info(codec, " speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200415 cfg->speaker_outs, cfg->speaker_pins[0],
416 cfg->speaker_pins[1], cfg->speaker_pins[2],
417 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwai4e76a882014-02-25 12:21:03 +0100418 codec_info(codec, " hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200419 cfg->hp_outs, cfg->hp_pins[0],
420 cfg->hp_pins[1], cfg->hp_pins[2],
421 cfg->hp_pins[3], cfg->hp_pins[4]);
Takashi Iwai4e76a882014-02-25 12:21:03 +0100422 codec_info(codec, " mono: mono_out=0x%x\n", cfg->mono_out_pin);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200423 if (cfg->dig_outs)
Takashi Iwai4e76a882014-02-25 12:21:03 +0100424 codec_info(codec, " dig-out=0x%x/0x%x\n",
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200425 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
Takashi Iwai4e76a882014-02-25 12:21:03 +0100426 codec_info(codec, " inputs:\n");
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200427 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100428 codec_info(codec, " %s=0x%x\n",
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200429 hda_get_autocfg_input_label(codec, cfg, i),
430 cfg->inputs[i].pin);
431 }
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200432 if (cfg->dig_in_pin)
Takashi Iwai4e76a882014-02-25 12:21:03 +0100433 codec_info(codec, " dig-in=0x%x\n", cfg->dig_in_pin);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200434
435 return 0;
436}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100437EXPORT_SYMBOL_GPL(snd_hda_parse_pin_defcfg);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200438
439int snd_hda_get_input_pin_attr(unsigned int def_conf)
440{
441 unsigned int loc = get_defcfg_location(def_conf);
442 unsigned int conn = get_defcfg_connect(def_conf);
443 if (conn == AC_JACK_PORT_NONE)
444 return INPUT_PIN_ATTR_UNUSED;
445 /* Windows may claim the internal mic to be BOTH, too */
446 if (conn == AC_JACK_PORT_FIXED || conn == AC_JACK_PORT_BOTH)
447 return INPUT_PIN_ATTR_INT;
448 if ((loc & 0x30) == AC_JACK_LOC_INTERNAL)
449 return INPUT_PIN_ATTR_INT;
450 if ((loc & 0x30) == AC_JACK_LOC_SEPARATE)
451 return INPUT_PIN_ATTR_DOCK;
452 if (loc == AC_JACK_LOC_REAR)
453 return INPUT_PIN_ATTR_REAR;
454 if (loc == AC_JACK_LOC_FRONT)
455 return INPUT_PIN_ATTR_FRONT;
456 return INPUT_PIN_ATTR_NORMAL;
457}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100458EXPORT_SYMBOL_GPL(snd_hda_get_input_pin_attr);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200459
460/**
461 * hda_get_input_pin_label - Give a label for the given input pin
462 *
463 * When check_location is true, the function checks the pin location
464 * for mic and line-in pins, and set an appropriate prefix like "Front",
465 * "Rear", "Internal".
466 */
467
468static const char *hda_get_input_pin_label(struct hda_codec *codec,
David Henningssona385d972013-03-21 12:16:29 +0100469 const struct auto_pin_cfg_item *item,
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200470 hda_nid_t pin, bool check_location)
471{
472 unsigned int def_conf;
473 static const char * const mic_names[] = {
Takashi Iwai5ec16d12012-11-28 18:11:59 +0100474 "Internal Mic", "Dock Mic", "Mic", "Rear Mic", "Front Mic"
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200475 };
476 int attr;
477
478 def_conf = snd_hda_codec_get_pincfg(codec, pin);
479
480 switch (get_defcfg_device(def_conf)) {
481 case AC_JACK_MIC_IN:
David Henningssona385d972013-03-21 12:16:29 +0100482 if (item && item->is_headset_mic)
483 return "Headset Mic";
David Henningssoncb420b12013-04-11 11:30:28 +0200484 if (item && item->is_headphone_mic)
485 return "Headphone Mic";
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200486 if (!check_location)
487 return "Mic";
488 attr = snd_hda_get_input_pin_attr(def_conf);
489 if (!attr)
490 return "None";
491 return mic_names[attr - 1];
492 case AC_JACK_LINE_IN:
493 if (!check_location)
494 return "Line";
495 attr = snd_hda_get_input_pin_attr(def_conf);
496 if (!attr)
497 return "None";
498 if (attr == INPUT_PIN_ATTR_DOCK)
499 return "Dock Line";
500 return "Line";
501 case AC_JACK_AUX:
502 return "Aux";
503 case AC_JACK_CD:
504 return "CD";
505 case AC_JACK_SPDIF_IN:
506 return "SPDIF In";
507 case AC_JACK_DIG_OTHER_IN:
508 return "Digital In";
Takashi Iwai54d778b2013-01-09 08:46:34 +0100509 case AC_JACK_HP_OUT:
510 return "Headphone Mic";
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200511 default:
512 return "Misc";
513 }
514}
515
516/* Check whether the location prefix needs to be added to the label.
517 * If all mic-jacks are in the same location (e.g. rear panel), we don't
518 * have to put "Front" prefix to each label. In such a case, returns false.
519 */
520static int check_mic_location_need(struct hda_codec *codec,
521 const struct auto_pin_cfg *cfg,
522 int input)
523{
524 unsigned int defc;
525 int i, attr, attr2;
526
527 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[input].pin);
528 attr = snd_hda_get_input_pin_attr(defc);
529 /* for internal or docking mics, we need locations */
530 if (attr <= INPUT_PIN_ATTR_NORMAL)
531 return 1;
532
533 attr = 0;
534 for (i = 0; i < cfg->num_inputs; i++) {
535 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[i].pin);
536 attr2 = snd_hda_get_input_pin_attr(defc);
537 if (attr2 >= INPUT_PIN_ATTR_NORMAL) {
538 if (attr && attr != attr2)
539 return 1; /* different locations found */
540 attr = attr2;
541 }
542 }
543 return 0;
544}
545
546/**
547 * hda_get_autocfg_input_label - Get a label for the given input
548 *
549 * Get a label for the given input pin defined by the autocfg item.
550 * Unlike hda_get_input_pin_label(), this function checks all inputs
551 * defined in autocfg and avoids the redundant mic/line prefix as much as
552 * possible.
553 */
554const char *hda_get_autocfg_input_label(struct hda_codec *codec,
555 const struct auto_pin_cfg *cfg,
556 int input)
557{
558 int type = cfg->inputs[input].type;
559 int has_multiple_pins = 0;
560
561 if ((input > 0 && cfg->inputs[input - 1].type == type) ||
562 (input < cfg->num_inputs - 1 && cfg->inputs[input + 1].type == type))
563 has_multiple_pins = 1;
564 if (has_multiple_pins && type == AUTO_PIN_MIC)
565 has_multiple_pins &= check_mic_location_need(codec, cfg, input);
David Henningssona385d972013-03-21 12:16:29 +0100566 return hda_get_input_pin_label(codec, &cfg->inputs[input],
567 cfg->inputs[input].pin,
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200568 has_multiple_pins);
569}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100570EXPORT_SYMBOL_GPL(hda_get_autocfg_input_label);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200571
572/* return the position of NID in the list, or -1 if not found */
573static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
574{
575 int i;
576 for (i = 0; i < nums; i++)
577 if (list[i] == nid)
578 return i;
579 return -1;
580}
581
582/* get a unique suffix or an index number */
583static const char *check_output_sfx(hda_nid_t nid, const hda_nid_t *pins,
584 int num_pins, int *indexp)
585{
586 static const char * const channel_sfx[] = {
587 " Front", " Surround", " CLFE", " Side"
588 };
589 int i;
590
591 i = find_idx_in_nid_list(nid, pins, num_pins);
592 if (i < 0)
593 return NULL;
594 if (num_pins == 1)
595 return "";
596 if (num_pins > ARRAY_SIZE(channel_sfx)) {
597 if (indexp)
598 *indexp = i;
599 return "";
600 }
601 return channel_sfx[i];
602}
603
David Henningssoneee3ed42012-10-03 11:12:53 +0200604static const char *check_output_pfx(struct hda_codec *codec, hda_nid_t nid)
605{
606 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
607 int attr = snd_hda_get_input_pin_attr(def_conf);
608
609 /* check the location */
610 switch (attr) {
611 case INPUT_PIN_ATTR_DOCK:
612 return "Dock ";
613 case INPUT_PIN_ATTR_FRONT:
614 return "Front ";
615 }
616 return "";
617}
618
619static int get_hp_label_index(struct hda_codec *codec, hda_nid_t nid,
620 const hda_nid_t *pins, int num_pins)
621{
622 int i, j, idx = 0;
623
624 const char *pfx = check_output_pfx(codec, nid);
625
626 i = find_idx_in_nid_list(nid, pins, num_pins);
627 if (i < 0)
628 return -1;
629 for (j = 0; j < i; j++)
630 if (pfx == check_output_pfx(codec, pins[j]))
631 idx++;
632
633 return idx;
634}
635
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200636static int fill_audio_out_name(struct hda_codec *codec, hda_nid_t nid,
637 const struct auto_pin_cfg *cfg,
638 const char *name, char *label, int maxlen,
639 int *indexp)
640{
641 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
642 int attr = snd_hda_get_input_pin_attr(def_conf);
David Henningssoneee3ed42012-10-03 11:12:53 +0200643 const char *pfx, *sfx = "";
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200644
645 /* handle as a speaker if it's a fixed line-out */
646 if (!strcmp(name, "Line Out") && attr == INPUT_PIN_ATTR_INT)
647 name = "Speaker";
David Henningssoneee3ed42012-10-03 11:12:53 +0200648 pfx = check_output_pfx(codec, nid);
649
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200650 if (cfg) {
651 /* try to give a unique suffix if needed */
652 sfx = check_output_sfx(nid, cfg->line_out_pins, cfg->line_outs,
653 indexp);
654 if (!sfx)
655 sfx = check_output_sfx(nid, cfg->speaker_pins, cfg->speaker_outs,
656 indexp);
657 if (!sfx) {
658 /* don't add channel suffix for Headphone controls */
David Henningssoneee3ed42012-10-03 11:12:53 +0200659 int idx = get_hp_label_index(codec, nid, cfg->hp_pins,
660 cfg->hp_outs);
Takashi Iwai728deec2013-10-28 11:39:23 +0100661 if (idx >= 0 && indexp)
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200662 *indexp = idx;
663 sfx = "";
664 }
665 }
666 snprintf(label, maxlen, "%s%s%s", pfx, name, sfx);
667 return 1;
668}
669
David Henningsson3f25dcf2013-01-18 15:43:03 +0100670#define is_hdmi_cfg(conf) \
671 (get_defcfg_location(conf) == AC_JACK_LOC_HDMI)
672
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200673/**
674 * snd_hda_get_pin_label - Get a label for the given I/O pin
675 *
676 * Get a label for the given pin. This function works for both input and
677 * output pins. When @cfg is given as non-NULL, the function tries to get
678 * an optimized label using hda_get_autocfg_input_label().
679 *
680 * This function tries to give a unique label string for the pin as much as
681 * possible. For example, when the multiple line-outs are present, it adds
682 * the channel suffix like "Front", "Surround", etc (only when @cfg is given).
683 * If no unique name with a suffix is available and @indexp is non-NULL, the
684 * index number is stored in the pointer.
685 */
686int snd_hda_get_pin_label(struct hda_codec *codec, hda_nid_t nid,
687 const struct auto_pin_cfg *cfg,
688 char *label, int maxlen, int *indexp)
689{
690 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
691 const char *name = NULL;
692 int i;
David Henningsson3f25dcf2013-01-18 15:43:03 +0100693 bool hdmi;
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200694
695 if (indexp)
696 *indexp = 0;
697 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
698 return 0;
699
700 switch (get_defcfg_device(def_conf)) {
701 case AC_JACK_LINE_OUT:
702 return fill_audio_out_name(codec, nid, cfg, "Line Out",
703 label, maxlen, indexp);
704 case AC_JACK_SPEAKER:
705 return fill_audio_out_name(codec, nid, cfg, "Speaker",
706 label, maxlen, indexp);
707 case AC_JACK_HP_OUT:
708 return fill_audio_out_name(codec, nid, cfg, "Headphone",
709 label, maxlen, indexp);
710 case AC_JACK_SPDIF_OUT:
711 case AC_JACK_DIG_OTHER_OUT:
David Henningsson3f25dcf2013-01-18 15:43:03 +0100712 hdmi = is_hdmi_cfg(def_conf);
713 name = hdmi ? "HDMI" : "SPDIF";
714 if (cfg && indexp)
715 for (i = 0; i < cfg->dig_outs; i++) {
716 hda_nid_t pin = cfg->dig_out_pins[i];
717 unsigned int c;
718 if (pin == nid)
719 break;
720 c = snd_hda_codec_get_pincfg(codec, pin);
721 if (hdmi == is_hdmi_cfg(c))
722 (*indexp)++;
723 }
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200724 break;
725 default:
726 if (cfg) {
727 for (i = 0; i < cfg->num_inputs; i++) {
728 if (cfg->inputs[i].pin != nid)
729 continue;
730 name = hda_get_autocfg_input_label(codec, cfg, i);
731 if (name)
732 break;
733 }
734 }
735 if (!name)
David Henningssona385d972013-03-21 12:16:29 +0100736 name = hda_get_input_pin_label(codec, NULL, nid, true);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200737 break;
738 }
739 if (!name)
740 return 0;
741 strlcpy(label, name, maxlen);
742 return 1;
743}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100744EXPORT_SYMBOL_GPL(snd_hda_get_pin_label);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200745
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100746int snd_hda_add_verbs(struct hda_codec *codec,
747 const struct hda_verb *list)
Takashi Iwai23d30f22012-05-07 17:17:32 +0200748{
749 const struct hda_verb **v;
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100750 v = snd_array_new(&codec->verbs);
Takashi Iwai23d30f22012-05-07 17:17:32 +0200751 if (!v)
752 return -ENOMEM;
753 *v = list;
754 return 0;
755}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100756EXPORT_SYMBOL_GPL(snd_hda_add_verbs);
Takashi Iwai23d30f22012-05-07 17:17:32 +0200757
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100758void snd_hda_apply_verbs(struct hda_codec *codec)
Takashi Iwai23d30f22012-05-07 17:17:32 +0200759{
Takashi Iwai23d30f22012-05-07 17:17:32 +0200760 int i;
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100761 for (i = 0; i < codec->verbs.used; i++) {
762 struct hda_verb **v = snd_array_elem(&codec->verbs, i);
Takashi Iwai23d30f22012-05-07 17:17:32 +0200763 snd_hda_sequence_write(codec, *v);
764 }
765}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100766EXPORT_SYMBOL_GPL(snd_hda_apply_verbs);
Takashi Iwai23d30f22012-05-07 17:17:32 +0200767
768void snd_hda_apply_pincfgs(struct hda_codec *codec,
769 const struct hda_pintbl *cfg)
770{
771 for (; cfg->nid; cfg++)
772 snd_hda_codec_set_pincfg(codec, cfg->nid, cfg->val);
773}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100774EXPORT_SYMBOL_GPL(snd_hda_apply_pincfgs);
Takashi Iwai23d30f22012-05-07 17:17:32 +0200775
Takashi Iwaifd108212013-01-10 10:18:14 +0100776static void set_pin_targets(struct hda_codec *codec,
777 const struct hda_pintbl *cfg)
778{
779 for (; cfg->nid; cfg++)
780 snd_hda_set_pin_ctl_cache(codec, cfg->nid, cfg->val);
781}
782
Takashi Iwai1f578252013-01-23 18:10:10 +0100783static void apply_fixup(struct hda_codec *codec, int id, int action, int depth)
Takashi Iwai23d30f22012-05-07 17:17:32 +0200784{
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100785 const char *modelname = codec->fixup_name;
Takashi Iwai23d30f22012-05-07 17:17:32 +0200786
787 while (id >= 0) {
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100788 const struct hda_fixup *fix = codec->fixup_list + id;
Takashi Iwai23d30f22012-05-07 17:17:32 +0200789
Takashi Iwai1f578252013-01-23 18:10:10 +0100790 if (fix->chained_before)
791 apply_fixup(codec, fix->chain_id, action, depth + 1);
792
Takashi Iwai23d30f22012-05-07 17:17:32 +0200793 switch (fix->type) {
794 case HDA_FIXUP_PINS:
795 if (action != HDA_FIXUP_ACT_PRE_PROBE || !fix->v.pins)
796 break;
Takashi Iwai4e76a882014-02-25 12:21:03 +0100797 codec_dbg(codec, "%s: Apply pincfg for %s\n",
Takashi Iwai23d30f22012-05-07 17:17:32 +0200798 codec->chip_name, modelname);
799 snd_hda_apply_pincfgs(codec, fix->v.pins);
800 break;
801 case HDA_FIXUP_VERBS:
802 if (action != HDA_FIXUP_ACT_PROBE || !fix->v.verbs)
803 break;
Takashi Iwai4e76a882014-02-25 12:21:03 +0100804 codec_dbg(codec, "%s: Apply fix-verbs for %s\n",
Takashi Iwai23d30f22012-05-07 17:17:32 +0200805 codec->chip_name, modelname);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100806 snd_hda_add_verbs(codec, fix->v.verbs);
Takashi Iwai23d30f22012-05-07 17:17:32 +0200807 break;
808 case HDA_FIXUP_FUNC:
809 if (!fix->v.func)
810 break;
Takashi Iwai4e76a882014-02-25 12:21:03 +0100811 codec_dbg(codec, "%s: Apply fix-func for %s\n",
Takashi Iwai23d30f22012-05-07 17:17:32 +0200812 codec->chip_name, modelname);
813 fix->v.func(codec, fix, action);
814 break;
Takashi Iwaifd108212013-01-10 10:18:14 +0100815 case HDA_FIXUP_PINCTLS:
816 if (action != HDA_FIXUP_ACT_PROBE || !fix->v.pins)
817 break;
Takashi Iwai4e76a882014-02-25 12:21:03 +0100818 codec_dbg(codec, "%s: Apply pinctl for %s\n",
Takashi Iwaifd108212013-01-10 10:18:14 +0100819 codec->chip_name, modelname);
820 set_pin_targets(codec, fix->v.pins);
821 break;
Takashi Iwai23d30f22012-05-07 17:17:32 +0200822 default:
Takashi Iwai4e76a882014-02-25 12:21:03 +0100823 codec_err(codec, "%s: Invalid fixup type %d\n",
Takashi Iwai23d30f22012-05-07 17:17:32 +0200824 codec->chip_name, fix->type);
825 break;
826 }
Takashi Iwai1f578252013-01-23 18:10:10 +0100827 if (!fix->chained || fix->chained_before)
Takashi Iwai23d30f22012-05-07 17:17:32 +0200828 break;
829 if (++depth > 10)
830 break;
831 id = fix->chain_id;
832 }
833}
Takashi Iwai1f578252013-01-23 18:10:10 +0100834
835void snd_hda_apply_fixup(struct hda_codec *codec, int action)
836{
837 if (codec->fixup_list)
838 apply_fixup(codec, codec->fixup_id, action, 0);
839}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100840EXPORT_SYMBOL_GPL(snd_hda_apply_fixup);
Takashi Iwai23d30f22012-05-07 17:17:32 +0200841
842void snd_hda_pick_fixup(struct hda_codec *codec,
843 const struct hda_model_fixup *models,
844 const struct snd_pci_quirk *quirk,
845 const struct hda_fixup *fixlist)
846{
Takashi Iwai23d30f22012-05-07 17:17:32 +0200847 const struct snd_pci_quirk *q;
848 int id = -1;
849 const char *name = NULL;
850
851 /* when model=nofixup is given, don't pick up any fixups */
852 if (codec->modelname && !strcmp(codec->modelname, "nofixup")) {
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100853 codec->fixup_list = NULL;
854 codec->fixup_id = -1;
Takashi Iwai23d30f22012-05-07 17:17:32 +0200855 return;
856 }
857
858 if (codec->modelname && models) {
859 while (models->name) {
860 if (!strcmp(codec->modelname, models->name)) {
861 id = models->id;
862 name = models->name;
863 break;
864 }
865 models++;
866 }
867 }
David Henningsson639aa4b2012-07-18 18:02:53 +0200868 if (id < 0 && quirk) {
Takashi Iwai23d30f22012-05-07 17:17:32 +0200869 q = snd_pci_quirk_lookup(codec->bus->pci, quirk);
870 if (q) {
871 id = q->value;
872#ifdef CONFIG_SND_DEBUG_VERBOSE
873 name = q->name;
874#endif
875 }
876 }
David Henningsson639aa4b2012-07-18 18:02:53 +0200877 if (id < 0 && quirk) {
Takashi Iwai697aeba2013-08-01 08:38:27 +0200878 for (q = quirk; q->subvendor || q->subdevice; q++) {
Takashi Iwai23d30f22012-05-07 17:17:32 +0200879 unsigned int vendorid =
880 q->subdevice | (q->subvendor << 16);
Takashi Iwaia33b7b02012-09-11 16:42:18 +0200881 unsigned int mask = 0xffff0000 | q->subdevice_mask;
882 if ((codec->subsystem_id & mask) == (vendorid & mask)) {
Takashi Iwai23d30f22012-05-07 17:17:32 +0200883 id = q->value;
884#ifdef CONFIG_SND_DEBUG_VERBOSE
885 name = q->name;
886#endif
887 break;
888 }
889 }
890 }
891
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100892 codec->fixup_id = id;
Takashi Iwai23d30f22012-05-07 17:17:32 +0200893 if (id >= 0) {
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100894 codec->fixup_list = fixlist;
895 codec->fixup_name = name;
Takashi Iwai23d30f22012-05-07 17:17:32 +0200896 }
897}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100898EXPORT_SYMBOL_GPL(snd_hda_pick_fixup);