blob: 03b7399bb7f00a131fb677bcec2768e0aa09cb71 [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
Takashi Iwai128bc4b2012-05-07 17:42:31 +020020/*
21 * Helper for automatic pin configuration
22 */
23
24static int is_in_nid_list(hda_nid_t nid, const hda_nid_t *list)
25{
26 for (; *list; list++)
27 if (*list == nid)
28 return 1;
29 return 0;
30}
31
Takashi Iwaib9030a02012-11-29 10:18:57 +010032/* a pair of input pin and its sequence */
33struct auto_out_pin {
34 hda_nid_t pin;
35 short seq;
36};
37
38static int compare_seq(const void *ap, const void *bp)
39{
40 const struct auto_out_pin *a = ap;
41 const struct auto_out_pin *b = bp;
42 return (int)(a->seq - b->seq);
43}
Takashi Iwai128bc4b2012-05-07 17:42:31 +020044
45/*
46 * Sort an associated group of pins according to their sequence numbers.
Takashi Iwaib9030a02012-11-29 10:18:57 +010047 * then store it to a pin array.
Takashi Iwai128bc4b2012-05-07 17:42:31 +020048 */
Takashi Iwaib9030a02012-11-29 10:18:57 +010049static void sort_pins_by_sequence(hda_nid_t *pins, struct auto_out_pin *list,
Takashi Iwai128bc4b2012-05-07 17:42:31 +020050 int num_pins)
51{
Takashi Iwaib9030a02012-11-29 10:18:57 +010052 int i;
53 sort(list, num_pins, sizeof(list[0]), compare_seq, NULL);
54 for (i = 0; i < num_pins; i++)
55 pins[i] = list[i].pin;
Takashi Iwai128bc4b2012-05-07 17:42:31 +020056}
57
58
59/* add the found input-pin to the cfg->inputs[] table */
David Henningsson95f72cf2014-09-23 10:38:18 +020060static void add_auto_cfg_input_pin(struct hda_codec *codec, struct auto_pin_cfg *cfg,
61 hda_nid_t nid, int type)
Takashi Iwai128bc4b2012-05-07 17:42:31 +020062{
63 if (cfg->num_inputs < AUTO_CFG_MAX_INS) {
64 cfg->inputs[cfg->num_inputs].pin = nid;
65 cfg->inputs[cfg->num_inputs].type = type;
David Henningsson95f72cf2014-09-23 10:38:18 +020066 cfg->inputs[cfg->num_inputs].has_boost_on_pin =
67 nid_has_volume(codec, nid, HDA_INPUT);
Takashi Iwai128bc4b2012-05-07 17:42:31 +020068 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;
David Henningsson95f72cf2014-09-23 10:38:18 +020076 if (a->type != b->type)
77 return (int)(a->type - b->type);
78
79 /* In case one has boost and the other one has not,
80 pick the one with boost first. */
81 return (int)(b->has_boost_on_pin - a->has_boost_on_pin);
Takashi Iwai128bc4b2012-05-07 17:42:31 +020082}
83
84/* Reorder the surround channels
85 * ALSA sequence is front/surr/clfe/side
86 * HDA sequence is:
87 * 4-ch: front/surr => OK as it is
88 * 6-ch: front/clfe/surr
89 * 8-ch: front/clfe/rear/side|fc
90 */
91static void reorder_outputs(unsigned int nums, hda_nid_t *pins)
92{
93 hda_nid_t nid;
94
95 switch (nums) {
96 case 3:
97 case 4:
98 nid = pins[1];
99 pins[1] = pins[2];
100 pins[2] = nid;
101 break;
102 }
103}
104
Takashi Iwai52fd5cb2013-01-15 08:45:33 +0100105/* check whether the given pin has a proper pin I/O capability bit */
106static bool check_pincap_validity(struct hda_codec *codec, hda_nid_t pin,
107 unsigned int dev)
108{
109 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
110
111 /* some old hardware don't return the proper pincaps */
112 if (!pincap)
113 return true;
114
115 switch (dev) {
116 case AC_JACK_LINE_OUT:
117 case AC_JACK_SPEAKER:
118 case AC_JACK_HP_OUT:
119 case AC_JACK_SPDIF_OUT:
120 case AC_JACK_DIG_OTHER_OUT:
121 return !!(pincap & AC_PINCAP_OUT);
122 default:
123 return !!(pincap & AC_PINCAP_IN);
124 }
125}
126
David Henningssoncb420b12013-04-11 11:30:28 +0200127static bool can_be_headset_mic(struct hda_codec *codec,
128 struct auto_pin_cfg_item *item,
129 int seq_number)
130{
131 int attr;
132 unsigned int def_conf;
133 if (item->type != AUTO_PIN_MIC)
134 return false;
135
136 if (item->is_headset_mic || item->is_headphone_mic)
137 return false; /* Already assigned */
138
139 def_conf = snd_hda_codec_get_pincfg(codec, item->pin);
140 attr = snd_hda_get_input_pin_attr(def_conf);
141 if (attr <= INPUT_PIN_ATTR_DOCK)
142 return false;
143
144 if (seq_number >= 0) {
145 int seq = get_defcfg_sequence(def_conf);
146 if (seq != seq_number)
147 return false;
148 }
149
150 return true;
151}
152
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200153/*
154 * Parse all pin widgets and store the useful pin nids to cfg
155 *
156 * The number of line-outs or any primary output is stored in line_outs,
157 * and the corresponding output pins are assigned to line_out_pins[],
158 * in the order of front, rear, CLFE, side, ...
159 *
160 * If more extra outputs (speaker and headphone) are found, the pins are
161 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
162 * is detected, one of speaker of HP pins is assigned as the primary
163 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
164 * if any analog output exists.
165 *
166 * The analog input pins are assigned to inputs array.
167 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
168 * respectively.
169 */
170int snd_hda_parse_pin_defcfg(struct hda_codec *codec,
171 struct auto_pin_cfg *cfg,
172 const hda_nid_t *ignore_nids,
173 unsigned int cond_flags)
174{
Takashi Iwai7639a062015-03-03 10:07:24 +0100175 hda_nid_t nid;
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200176 short seq, assoc_line_out;
Takashi Iwaib9030a02012-11-29 10:18:57 +0100177 struct auto_out_pin line_out[ARRAY_SIZE(cfg->line_out_pins)];
178 struct auto_out_pin speaker_out[ARRAY_SIZE(cfg->speaker_pins)];
179 struct auto_out_pin hp_out[ARRAY_SIZE(cfg->hp_pins)];
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200180 int i;
181
Takashi Iwai1c70a582013-01-11 17:48:22 +0100182 if (!snd_hda_get_int_hint(codec, "parser_flags", &i))
183 cond_flags = i;
184
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200185 memset(cfg, 0, sizeof(*cfg));
186
Takashi Iwaib9030a02012-11-29 10:18:57 +0100187 memset(line_out, 0, sizeof(line_out));
188 memset(speaker_out, 0, sizeof(speaker_out));
189 memset(hp_out, 0, sizeof(hp_out));
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200190 assoc_line_out = 0;
191
Takashi Iwai7639a062015-03-03 10:07:24 +0100192 for_each_hda_codec_node(nid, codec) {
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200193 unsigned int wid_caps = get_wcaps(codec, nid);
194 unsigned int wid_type = get_wcaps_type(wid_caps);
195 unsigned int def_conf;
196 short assoc, loc, conn, dev;
197
198 /* read all default configuration for pin complex */
199 if (wid_type != AC_WID_PIN)
200 continue;
201 /* ignore the given nids (e.g. pc-beep returns error) */
202 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
203 continue;
204
205 def_conf = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200206 conn = get_defcfg_connect(def_conf);
207 if (conn == AC_JACK_PORT_NONE)
208 continue;
209 loc = get_defcfg_location(def_conf);
210 dev = get_defcfg_device(def_conf);
211
212 /* workaround for buggy BIOS setups */
213 if (dev == AC_JACK_LINE_OUT) {
Takashi Iwaifb690cf2013-01-07 18:21:47 +0100214 if (conn == AC_JACK_PORT_FIXED ||
215 conn == AC_JACK_PORT_BOTH)
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200216 dev = AC_JACK_SPEAKER;
217 }
218
Takashi Iwai52fd5cb2013-01-15 08:45:33 +0100219 if (!check_pincap_validity(codec, nid, dev))
220 continue;
221
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200222 switch (dev) {
223 case AC_JACK_LINE_OUT:
224 seq = get_defcfg_sequence(def_conf);
225 assoc = get_defcfg_association(def_conf);
226
227 if (!(wid_caps & AC_WCAP_STEREO))
228 if (!cfg->mono_out_pin)
229 cfg->mono_out_pin = nid;
230 if (!assoc)
231 continue;
232 if (!assoc_line_out)
233 assoc_line_out = assoc;
Takashi Iwai9b7564a2014-03-25 10:02:01 +0100234 else if (assoc_line_out != assoc) {
235 codec_info(codec,
236 "ignore pin 0x%x with mismatching assoc# 0x%x vs 0x%x\n",
237 nid, assoc, assoc_line_out);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200238 continue;
Takashi Iwai9b7564a2014-03-25 10:02:01 +0100239 }
240 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins)) {
241 codec_info(codec,
242 "ignore pin 0x%x, too many assigned pins\n",
243 nid);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200244 continue;
Takashi Iwai9b7564a2014-03-25 10:02:01 +0100245 }
Takashi Iwaib9030a02012-11-29 10:18:57 +0100246 line_out[cfg->line_outs].pin = nid;
247 line_out[cfg->line_outs].seq = seq;
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200248 cfg->line_outs++;
249 break;
250 case AC_JACK_SPEAKER:
251 seq = get_defcfg_sequence(def_conf);
252 assoc = get_defcfg_association(def_conf);
Takashi Iwai9b7564a2014-03-25 10:02:01 +0100253 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins)) {
254 codec_info(codec,
255 "ignore pin 0x%x, too many assigned pins\n",
256 nid);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200257 continue;
Takashi Iwai9b7564a2014-03-25 10:02:01 +0100258 }
Takashi Iwaib9030a02012-11-29 10:18:57 +0100259 speaker_out[cfg->speaker_outs].pin = nid;
260 speaker_out[cfg->speaker_outs].seq = (assoc << 4) | seq;
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200261 cfg->speaker_outs++;
262 break;
263 case AC_JACK_HP_OUT:
264 seq = get_defcfg_sequence(def_conf);
265 assoc = get_defcfg_association(def_conf);
Takashi Iwai9b7564a2014-03-25 10:02:01 +0100266 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins)) {
267 codec_info(codec,
268 "ignore pin 0x%x, too many assigned pins\n",
269 nid);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200270 continue;
Takashi Iwai9b7564a2014-03-25 10:02:01 +0100271 }
Takashi Iwaib9030a02012-11-29 10:18:57 +0100272 hp_out[cfg->hp_outs].pin = nid;
273 hp_out[cfg->hp_outs].seq = (assoc << 4) | seq;
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200274 cfg->hp_outs++;
275 break;
276 case AC_JACK_MIC_IN:
David Henningsson95f72cf2014-09-23 10:38:18 +0200277 add_auto_cfg_input_pin(codec, cfg, nid, AUTO_PIN_MIC);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200278 break;
279 case AC_JACK_LINE_IN:
David Henningsson95f72cf2014-09-23 10:38:18 +0200280 add_auto_cfg_input_pin(codec, cfg, nid, AUTO_PIN_LINE_IN);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200281 break;
282 case AC_JACK_CD:
David Henningsson95f72cf2014-09-23 10:38:18 +0200283 add_auto_cfg_input_pin(codec, cfg, nid, AUTO_PIN_CD);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200284 break;
285 case AC_JACK_AUX:
David Henningsson95f72cf2014-09-23 10:38:18 +0200286 add_auto_cfg_input_pin(codec, cfg, nid, AUTO_PIN_AUX);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200287 break;
288 case AC_JACK_SPDIF_OUT:
289 case AC_JACK_DIG_OTHER_OUT:
Takashi Iwai9b7564a2014-03-25 10:02:01 +0100290 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins)) {
291 codec_info(codec,
292 "ignore pin 0x%x, too many assigned pins\n",
293 nid);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200294 continue;
Takashi Iwai9b7564a2014-03-25 10:02:01 +0100295 }
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200296 cfg->dig_out_pins[cfg->dig_outs] = nid;
297 cfg->dig_out_type[cfg->dig_outs] =
298 (loc == AC_JACK_LOC_HDMI) ?
299 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
300 cfg->dig_outs++;
301 break;
302 case AC_JACK_SPDIF_IN:
303 case AC_JACK_DIG_OTHER_IN:
304 cfg->dig_in_pin = nid;
305 if (loc == AC_JACK_LOC_HDMI)
306 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
307 else
308 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
309 break;
310 }
311 }
312
David Henningssoncb420b12013-04-11 11:30:28 +0200313 /* Find a pin that could be a headset or headphone mic */
314 if (cond_flags & HDA_PINCFG_HEADSET_MIC || cond_flags & HDA_PINCFG_HEADPHONE_MIC) {
315 bool hsmic = !!(cond_flags & HDA_PINCFG_HEADSET_MIC);
316 bool hpmic = !!(cond_flags & HDA_PINCFG_HEADPHONE_MIC);
317 for (i = 0; (hsmic || hpmic) && (i < cfg->num_inputs); i++)
318 if (hsmic && can_be_headset_mic(codec, &cfg->inputs[i], 0xc)) {
319 cfg->inputs[i].is_headset_mic = 1;
320 hsmic = false;
321 } else if (hpmic && can_be_headset_mic(codec, &cfg->inputs[i], 0xd)) {
322 cfg->inputs[i].is_headphone_mic = 1;
323 hpmic = false;
324 }
325
326 /* If we didn't find our sequence number mark, fall back to any sequence number */
327 for (i = 0; (hsmic || hpmic) && (i < cfg->num_inputs); i++) {
328 if (!can_be_headset_mic(codec, &cfg->inputs[i], -1))
David Henningssona385d972013-03-21 12:16:29 +0100329 continue;
David Henningssoncb420b12013-04-11 11:30:28 +0200330 if (hsmic) {
331 cfg->inputs[i].is_headset_mic = 1;
332 hsmic = false;
333 } else if (hpmic) {
334 cfg->inputs[i].is_headphone_mic = 1;
335 hpmic = false;
336 }
David Henningssona385d972013-03-21 12:16:29 +0100337 }
David Henningssoncb420b12013-04-11 11:30:28 +0200338
339 if (hsmic)
Takashi Iwai4e76a882014-02-25 12:21:03 +0100340 codec_dbg(codec, "Told to look for a headset mic, but didn't find any.\n");
David Henningssoncb420b12013-04-11 11:30:28 +0200341 if (hpmic)
Takashi Iwai4e76a882014-02-25 12:21:03 +0100342 codec_dbg(codec, "Told to look for a headphone mic, but didn't find any.\n");
David Henningssona385d972013-03-21 12:16:29 +0100343 }
344
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200345 /* FIX-UP:
346 * If no line-out is defined but multiple HPs are found,
347 * some of them might be the real line-outs.
348 */
349 if (!cfg->line_outs && cfg->hp_outs > 1 &&
350 !(cond_flags & HDA_PINCFG_NO_HP_FIXUP)) {
351 int i = 0;
352 while (i < cfg->hp_outs) {
353 /* The real HPs should have the sequence 0x0f */
Takashi Iwaib9030a02012-11-29 10:18:57 +0100354 if ((hp_out[i].seq & 0x0f) == 0x0f) {
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200355 i++;
356 continue;
357 }
358 /* Move it to the line-out table */
Takashi Iwaib9030a02012-11-29 10:18:57 +0100359 line_out[cfg->line_outs++] = hp_out[i];
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200360 cfg->hp_outs--;
Takashi Iwaib9030a02012-11-29 10:18:57 +0100361 memmove(hp_out + i, hp_out + i + 1,
362 sizeof(hp_out[0]) * (cfg->hp_outs - i));
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200363 }
Takashi Iwaib9030a02012-11-29 10:18:57 +0100364 memset(hp_out + cfg->hp_outs, 0,
365 sizeof(hp_out[0]) * (AUTO_CFG_MAX_OUTS - cfg->hp_outs));
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200366 if (!cfg->hp_outs)
367 cfg->line_out_type = AUTO_PIN_HP_OUT;
368
369 }
370
371 /* sort by sequence */
Takashi Iwaib9030a02012-11-29 10:18:57 +0100372 sort_pins_by_sequence(cfg->line_out_pins, line_out, cfg->line_outs);
373 sort_pins_by_sequence(cfg->speaker_pins, speaker_out,
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200374 cfg->speaker_outs);
Takashi Iwaib9030a02012-11-29 10:18:57 +0100375 sort_pins_by_sequence(cfg->hp_pins, hp_out, cfg->hp_outs);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200376
377 /*
378 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
379 * as a primary output
380 */
381 if (!cfg->line_outs &&
382 !(cond_flags & HDA_PINCFG_NO_LO_FIXUP)) {
383 if (cfg->speaker_outs) {
384 cfg->line_outs = cfg->speaker_outs;
385 memcpy(cfg->line_out_pins, cfg->speaker_pins,
386 sizeof(cfg->speaker_pins));
387 cfg->speaker_outs = 0;
388 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
389 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
390 } else if (cfg->hp_outs) {
391 cfg->line_outs = cfg->hp_outs;
392 memcpy(cfg->line_out_pins, cfg->hp_pins,
393 sizeof(cfg->hp_pins));
394 cfg->hp_outs = 0;
395 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
396 cfg->line_out_type = AUTO_PIN_HP_OUT;
397 }
398 }
399
400 reorder_outputs(cfg->line_outs, cfg->line_out_pins);
401 reorder_outputs(cfg->hp_outs, cfg->hp_pins);
402 reorder_outputs(cfg->speaker_outs, cfg->speaker_pins);
403
Takashi Iwaib9030a02012-11-29 10:18:57 +0100404 /* sort inputs in the order of AUTO_PIN_* type */
405 sort(cfg->inputs, cfg->num_inputs, sizeof(cfg->inputs[0]),
406 compare_input_type, NULL);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200407
408 /*
409 * debug prints of the parsed results
410 */
David Henningsson322b8af12015-01-07 14:41:58 +0100411 codec_info(codec, "autoconfig for %s: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x) type:%s\n",
Takashi Iwai7639a062015-03-03 10:07:24 +0100412 codec->core.chip_name, cfg->line_outs, cfg->line_out_pins[0],
David Henningsson322b8af12015-01-07 14:41:58 +0100413 cfg->line_out_pins[1], cfg->line_out_pins[2],
414 cfg->line_out_pins[3], cfg->line_out_pins[4],
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200415 cfg->line_out_type == AUTO_PIN_HP_OUT ? "hp" :
416 (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT ?
417 "speaker" : "line"));
Takashi Iwai4e76a882014-02-25 12:21:03 +0100418 codec_info(codec, " speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200419 cfg->speaker_outs, cfg->speaker_pins[0],
420 cfg->speaker_pins[1], cfg->speaker_pins[2],
421 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwai4e76a882014-02-25 12:21:03 +0100422 codec_info(codec, " hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200423 cfg->hp_outs, cfg->hp_pins[0],
424 cfg->hp_pins[1], cfg->hp_pins[2],
425 cfg->hp_pins[3], cfg->hp_pins[4]);
Takashi Iwai4e76a882014-02-25 12:21:03 +0100426 codec_info(codec, " mono: mono_out=0x%x\n", cfg->mono_out_pin);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200427 if (cfg->dig_outs)
Takashi Iwai4e76a882014-02-25 12:21:03 +0100428 codec_info(codec, " dig-out=0x%x/0x%x\n",
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200429 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
Takashi Iwai4e76a882014-02-25 12:21:03 +0100430 codec_info(codec, " inputs:\n");
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200431 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100432 codec_info(codec, " %s=0x%x\n",
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200433 hda_get_autocfg_input_label(codec, cfg, i),
434 cfg->inputs[i].pin);
435 }
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200436 if (cfg->dig_in_pin)
Takashi Iwai4e76a882014-02-25 12:21:03 +0100437 codec_info(codec, " dig-in=0x%x\n", cfg->dig_in_pin);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200438
439 return 0;
440}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100441EXPORT_SYMBOL_GPL(snd_hda_parse_pin_defcfg);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200442
Takashi Iwai95a962c2014-10-29 16:03:58 +0100443/**
444 * snd_hda_get_input_pin_attr - Get the input pin attribute from pin config
445 * @def_conf: pin configuration value
446 *
447 * Guess the input pin attribute (INPUT_PIN_ATTR_XXX) from the given
448 * default pin configuration value.
449 */
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200450int snd_hda_get_input_pin_attr(unsigned int def_conf)
451{
452 unsigned int loc = get_defcfg_location(def_conf);
453 unsigned int conn = get_defcfg_connect(def_conf);
454 if (conn == AC_JACK_PORT_NONE)
455 return INPUT_PIN_ATTR_UNUSED;
456 /* Windows may claim the internal mic to be BOTH, too */
457 if (conn == AC_JACK_PORT_FIXED || conn == AC_JACK_PORT_BOTH)
458 return INPUT_PIN_ATTR_INT;
459 if ((loc & 0x30) == AC_JACK_LOC_INTERNAL)
460 return INPUT_PIN_ATTR_INT;
461 if ((loc & 0x30) == AC_JACK_LOC_SEPARATE)
462 return INPUT_PIN_ATTR_DOCK;
463 if (loc == AC_JACK_LOC_REAR)
464 return INPUT_PIN_ATTR_REAR;
465 if (loc == AC_JACK_LOC_FRONT)
466 return INPUT_PIN_ATTR_FRONT;
467 return INPUT_PIN_ATTR_NORMAL;
468}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100469EXPORT_SYMBOL_GPL(snd_hda_get_input_pin_attr);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200470
471/**
472 * hda_get_input_pin_label - Give a label for the given input pin
Takashi Iwaia11e9b12014-10-29 15:06:01 +0100473 * @codec: the HDA codec
474 * @item: ping config item to refer
475 * @pin: the pin NID
476 * @check_location: flag to add the jack location prefix
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200477 *
Takashi Iwaia11e9b12014-10-29 15:06:01 +0100478 * When @check_location is true, the function checks the pin location
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200479 * for mic and line-in pins, and set an appropriate prefix like "Front",
480 * "Rear", "Internal".
481 */
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200482static const char *hda_get_input_pin_label(struct hda_codec *codec,
David Henningssona385d972013-03-21 12:16:29 +0100483 const struct auto_pin_cfg_item *item,
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200484 hda_nid_t pin, bool check_location)
485{
486 unsigned int def_conf;
487 static const char * const mic_names[] = {
Takashi Iwai5ec16d12012-11-28 18:11:59 +0100488 "Internal Mic", "Dock Mic", "Mic", "Rear Mic", "Front Mic"
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200489 };
490 int attr;
491
492 def_conf = snd_hda_codec_get_pincfg(codec, pin);
493
494 switch (get_defcfg_device(def_conf)) {
495 case AC_JACK_MIC_IN:
David Henningssona385d972013-03-21 12:16:29 +0100496 if (item && item->is_headset_mic)
497 return "Headset Mic";
David Henningssoncb420b12013-04-11 11:30:28 +0200498 if (item && item->is_headphone_mic)
499 return "Headphone Mic";
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200500 if (!check_location)
501 return "Mic";
502 attr = snd_hda_get_input_pin_attr(def_conf);
503 if (!attr)
504 return "None";
505 return mic_names[attr - 1];
506 case AC_JACK_LINE_IN:
507 if (!check_location)
508 return "Line";
509 attr = snd_hda_get_input_pin_attr(def_conf);
510 if (!attr)
511 return "None";
512 if (attr == INPUT_PIN_ATTR_DOCK)
513 return "Dock Line";
514 return "Line";
515 case AC_JACK_AUX:
516 return "Aux";
517 case AC_JACK_CD:
518 return "CD";
519 case AC_JACK_SPDIF_IN:
520 return "SPDIF In";
521 case AC_JACK_DIG_OTHER_IN:
522 return "Digital In";
Takashi Iwai54d778b2013-01-09 08:46:34 +0100523 case AC_JACK_HP_OUT:
524 return "Headphone Mic";
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200525 default:
526 return "Misc";
527 }
528}
529
530/* Check whether the location prefix needs to be added to the label.
531 * If all mic-jacks are in the same location (e.g. rear panel), we don't
532 * have to put "Front" prefix to each label. In such a case, returns false.
533 */
534static int check_mic_location_need(struct hda_codec *codec,
535 const struct auto_pin_cfg *cfg,
536 int input)
537{
538 unsigned int defc;
539 int i, attr, attr2;
540
541 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[input].pin);
542 attr = snd_hda_get_input_pin_attr(defc);
543 /* for internal or docking mics, we need locations */
544 if (attr <= INPUT_PIN_ATTR_NORMAL)
545 return 1;
546
547 attr = 0;
548 for (i = 0; i < cfg->num_inputs; i++) {
549 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[i].pin);
550 attr2 = snd_hda_get_input_pin_attr(defc);
551 if (attr2 >= INPUT_PIN_ATTR_NORMAL) {
552 if (attr && attr != attr2)
553 return 1; /* different locations found */
554 attr = attr2;
555 }
556 }
557 return 0;
558}
559
560/**
561 * hda_get_autocfg_input_label - Get a label for the given input
Takashi Iwaia11e9b12014-10-29 15:06:01 +0100562 * @codec: the HDA codec
563 * @cfg: the parsed pin configuration
564 * @input: the input index number
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200565 *
566 * Get a label for the given input pin defined by the autocfg item.
567 * Unlike hda_get_input_pin_label(), this function checks all inputs
568 * defined in autocfg and avoids the redundant mic/line prefix as much as
569 * possible.
570 */
571const char *hda_get_autocfg_input_label(struct hda_codec *codec,
572 const struct auto_pin_cfg *cfg,
573 int input)
574{
575 int type = cfg->inputs[input].type;
576 int has_multiple_pins = 0;
577
578 if ((input > 0 && cfg->inputs[input - 1].type == type) ||
579 (input < cfg->num_inputs - 1 && cfg->inputs[input + 1].type == type))
580 has_multiple_pins = 1;
581 if (has_multiple_pins && type == AUTO_PIN_MIC)
582 has_multiple_pins &= check_mic_location_need(codec, cfg, input);
David Henningssona385d972013-03-21 12:16:29 +0100583 return hda_get_input_pin_label(codec, &cfg->inputs[input],
584 cfg->inputs[input].pin,
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200585 has_multiple_pins);
586}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100587EXPORT_SYMBOL_GPL(hda_get_autocfg_input_label);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200588
589/* return the position of NID in the list, or -1 if not found */
590static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
591{
592 int i;
593 for (i = 0; i < nums; i++)
594 if (list[i] == nid)
595 return i;
596 return -1;
597}
598
599/* get a unique suffix or an index number */
600static const char *check_output_sfx(hda_nid_t nid, const hda_nid_t *pins,
601 int num_pins, int *indexp)
602{
603 static const char * const channel_sfx[] = {
604 " Front", " Surround", " CLFE", " Side"
605 };
606 int i;
607
608 i = find_idx_in_nid_list(nid, pins, num_pins);
609 if (i < 0)
610 return NULL;
611 if (num_pins == 1)
612 return "";
613 if (num_pins > ARRAY_SIZE(channel_sfx)) {
614 if (indexp)
615 *indexp = i;
616 return "";
617 }
618 return channel_sfx[i];
619}
620
David Henningssoneee3ed42012-10-03 11:12:53 +0200621static const char *check_output_pfx(struct hda_codec *codec, hda_nid_t nid)
622{
623 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
624 int attr = snd_hda_get_input_pin_attr(def_conf);
625
626 /* check the location */
627 switch (attr) {
628 case INPUT_PIN_ATTR_DOCK:
629 return "Dock ";
630 case INPUT_PIN_ATTR_FRONT:
631 return "Front ";
632 }
633 return "";
634}
635
636static int get_hp_label_index(struct hda_codec *codec, hda_nid_t nid,
637 const hda_nid_t *pins, int num_pins)
638{
639 int i, j, idx = 0;
640
641 const char *pfx = check_output_pfx(codec, nid);
642
643 i = find_idx_in_nid_list(nid, pins, num_pins);
644 if (i < 0)
645 return -1;
646 for (j = 0; j < i; j++)
647 if (pfx == check_output_pfx(codec, pins[j]))
648 idx++;
649
650 return idx;
651}
652
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200653static int fill_audio_out_name(struct hda_codec *codec, hda_nid_t nid,
654 const struct auto_pin_cfg *cfg,
655 const char *name, char *label, int maxlen,
656 int *indexp)
657{
658 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
659 int attr = snd_hda_get_input_pin_attr(def_conf);
David Henningssoneee3ed42012-10-03 11:12:53 +0200660 const char *pfx, *sfx = "";
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200661
662 /* handle as a speaker if it's a fixed line-out */
663 if (!strcmp(name, "Line Out") && attr == INPUT_PIN_ATTR_INT)
664 name = "Speaker";
David Henningssoneee3ed42012-10-03 11:12:53 +0200665 pfx = check_output_pfx(codec, nid);
666
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200667 if (cfg) {
668 /* try to give a unique suffix if needed */
669 sfx = check_output_sfx(nid, cfg->line_out_pins, cfg->line_outs,
670 indexp);
671 if (!sfx)
672 sfx = check_output_sfx(nid, cfg->speaker_pins, cfg->speaker_outs,
673 indexp);
674 if (!sfx) {
675 /* don't add channel suffix for Headphone controls */
David Henningssoneee3ed42012-10-03 11:12:53 +0200676 int idx = get_hp_label_index(codec, nid, cfg->hp_pins,
677 cfg->hp_outs);
Takashi Iwai728deec2013-10-28 11:39:23 +0100678 if (idx >= 0 && indexp)
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200679 *indexp = idx;
680 sfx = "";
681 }
682 }
683 snprintf(label, maxlen, "%s%s%s", pfx, name, sfx);
684 return 1;
685}
686
David Henningsson3f25dcf2013-01-18 15:43:03 +0100687#define is_hdmi_cfg(conf) \
688 (get_defcfg_location(conf) == AC_JACK_LOC_HDMI)
689
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200690/**
691 * snd_hda_get_pin_label - Get a label for the given I/O pin
Takashi Iwaia11e9b12014-10-29 15:06:01 +0100692 * @codec: the HDA codec
693 * @nid: pin NID
694 * @cfg: the parsed pin configuration
695 * @label: the string buffer to store
696 * @maxlen: the max length of string buffer (including termination)
697 * @indexp: the pointer to return the index number (for multiple ctls)
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200698 *
699 * Get a label for the given pin. This function works for both input and
700 * output pins. When @cfg is given as non-NULL, the function tries to get
701 * an optimized label using hda_get_autocfg_input_label().
702 *
703 * This function tries to give a unique label string for the pin as much as
704 * possible. For example, when the multiple line-outs are present, it adds
705 * the channel suffix like "Front", "Surround", etc (only when @cfg is given).
706 * If no unique name with a suffix is available and @indexp is non-NULL, the
707 * index number is stored in the pointer.
708 */
709int snd_hda_get_pin_label(struct hda_codec *codec, hda_nid_t nid,
710 const struct auto_pin_cfg *cfg,
711 char *label, int maxlen, int *indexp)
712{
713 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
714 const char *name = NULL;
715 int i;
David Henningsson3f25dcf2013-01-18 15:43:03 +0100716 bool hdmi;
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200717
718 if (indexp)
719 *indexp = 0;
720 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
721 return 0;
722
723 switch (get_defcfg_device(def_conf)) {
724 case AC_JACK_LINE_OUT:
725 return fill_audio_out_name(codec, nid, cfg, "Line Out",
726 label, maxlen, indexp);
727 case AC_JACK_SPEAKER:
728 return fill_audio_out_name(codec, nid, cfg, "Speaker",
729 label, maxlen, indexp);
730 case AC_JACK_HP_OUT:
731 return fill_audio_out_name(codec, nid, cfg, "Headphone",
732 label, maxlen, indexp);
733 case AC_JACK_SPDIF_OUT:
734 case AC_JACK_DIG_OTHER_OUT:
David Henningsson3f25dcf2013-01-18 15:43:03 +0100735 hdmi = is_hdmi_cfg(def_conf);
736 name = hdmi ? "HDMI" : "SPDIF";
737 if (cfg && indexp)
738 for (i = 0; i < cfg->dig_outs; i++) {
739 hda_nid_t pin = cfg->dig_out_pins[i];
740 unsigned int c;
741 if (pin == nid)
742 break;
743 c = snd_hda_codec_get_pincfg(codec, pin);
744 if (hdmi == is_hdmi_cfg(c))
745 (*indexp)++;
746 }
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200747 break;
748 default:
749 if (cfg) {
750 for (i = 0; i < cfg->num_inputs; i++) {
751 if (cfg->inputs[i].pin != nid)
752 continue;
753 name = hda_get_autocfg_input_label(codec, cfg, i);
754 if (name)
755 break;
756 }
757 }
758 if (!name)
David Henningssona385d972013-03-21 12:16:29 +0100759 name = hda_get_input_pin_label(codec, NULL, nid, true);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200760 break;
761 }
762 if (!name)
763 return 0;
764 strlcpy(label, name, maxlen);
765 return 1;
766}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100767EXPORT_SYMBOL_GPL(snd_hda_get_pin_label);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200768
Takashi Iwai95a962c2014-10-29 16:03:58 +0100769/**
770 * snd_hda_add_verbs - Add verbs to the init list
771 * @codec: the HDA codec
772 * @list: zero-terminated verb list to add
773 *
774 * Append the given verb list to the execution list. The verbs will be
775 * performed at init and resume time via snd_hda_apply_verbs().
776 */
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100777int snd_hda_add_verbs(struct hda_codec *codec,
778 const struct hda_verb *list)
Takashi Iwai23d30f22012-05-07 17:17:32 +0200779{
780 const struct hda_verb **v;
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100781 v = snd_array_new(&codec->verbs);
Takashi Iwai23d30f22012-05-07 17:17:32 +0200782 if (!v)
783 return -ENOMEM;
784 *v = list;
785 return 0;
786}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100787EXPORT_SYMBOL_GPL(snd_hda_add_verbs);
Takashi Iwai23d30f22012-05-07 17:17:32 +0200788
Takashi Iwai95a962c2014-10-29 16:03:58 +0100789/**
790 * snd_hda_apply_verbs - Execute the init verb lists
791 * @codec: the HDA codec
792 */
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100793void snd_hda_apply_verbs(struct hda_codec *codec)
Takashi Iwai23d30f22012-05-07 17:17:32 +0200794{
Takashi Iwai23d30f22012-05-07 17:17:32 +0200795 int i;
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100796 for (i = 0; i < codec->verbs.used; i++) {
797 struct hda_verb **v = snd_array_elem(&codec->verbs, i);
Takashi Iwai23d30f22012-05-07 17:17:32 +0200798 snd_hda_sequence_write(codec, *v);
799 }
800}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100801EXPORT_SYMBOL_GPL(snd_hda_apply_verbs);
Takashi Iwai23d30f22012-05-07 17:17:32 +0200802
Takashi Iwai95a962c2014-10-29 16:03:58 +0100803/**
804 * snd_hda_apply_pincfgs - Set each pin config in the given list
805 * @codec: the HDA codec
806 * @cfg: NULL-terminated pin config table
807 */
Takashi Iwai23d30f22012-05-07 17:17:32 +0200808void snd_hda_apply_pincfgs(struct hda_codec *codec,
809 const struct hda_pintbl *cfg)
810{
811 for (; cfg->nid; cfg++)
812 snd_hda_codec_set_pincfg(codec, cfg->nid, cfg->val);
813}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100814EXPORT_SYMBOL_GPL(snd_hda_apply_pincfgs);
Takashi Iwai23d30f22012-05-07 17:17:32 +0200815
Takashi Iwaifd108212013-01-10 10:18:14 +0100816static void set_pin_targets(struct hda_codec *codec,
817 const struct hda_pintbl *cfg)
818{
819 for (; cfg->nid; cfg++)
820 snd_hda_set_pin_ctl_cache(codec, cfg->nid, cfg->val);
821}
822
Takashi Iwai1f578252013-01-23 18:10:10 +0100823static void apply_fixup(struct hda_codec *codec, int id, int action, int depth)
Takashi Iwai23d30f22012-05-07 17:17:32 +0200824{
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100825 const char *modelname = codec->fixup_name;
Takashi Iwai23d30f22012-05-07 17:17:32 +0200826
827 while (id >= 0) {
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100828 const struct hda_fixup *fix = codec->fixup_list + id;
Takashi Iwai23d30f22012-05-07 17:17:32 +0200829
Takashi Iwai1f578252013-01-23 18:10:10 +0100830 if (fix->chained_before)
831 apply_fixup(codec, fix->chain_id, action, depth + 1);
832
Takashi Iwai23d30f22012-05-07 17:17:32 +0200833 switch (fix->type) {
834 case HDA_FIXUP_PINS:
835 if (action != HDA_FIXUP_ACT_PRE_PROBE || !fix->v.pins)
836 break;
Takashi Iwai4e76a882014-02-25 12:21:03 +0100837 codec_dbg(codec, "%s: Apply pincfg for %s\n",
Takashi Iwai7639a062015-03-03 10:07:24 +0100838 codec->core.chip_name, modelname);
Takashi Iwai23d30f22012-05-07 17:17:32 +0200839 snd_hda_apply_pincfgs(codec, fix->v.pins);
840 break;
841 case HDA_FIXUP_VERBS:
842 if (action != HDA_FIXUP_ACT_PROBE || !fix->v.verbs)
843 break;
Takashi Iwai4e76a882014-02-25 12:21:03 +0100844 codec_dbg(codec, "%s: Apply fix-verbs for %s\n",
Takashi Iwai7639a062015-03-03 10:07:24 +0100845 codec->core.chip_name, modelname);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100846 snd_hda_add_verbs(codec, fix->v.verbs);
Takashi Iwai23d30f22012-05-07 17:17:32 +0200847 break;
848 case HDA_FIXUP_FUNC:
849 if (!fix->v.func)
850 break;
Takashi Iwai4e76a882014-02-25 12:21:03 +0100851 codec_dbg(codec, "%s: Apply fix-func for %s\n",
Takashi Iwai7639a062015-03-03 10:07:24 +0100852 codec->core.chip_name, modelname);
Takashi Iwai23d30f22012-05-07 17:17:32 +0200853 fix->v.func(codec, fix, action);
854 break;
Takashi Iwaifd108212013-01-10 10:18:14 +0100855 case HDA_FIXUP_PINCTLS:
856 if (action != HDA_FIXUP_ACT_PROBE || !fix->v.pins)
857 break;
Takashi Iwai4e76a882014-02-25 12:21:03 +0100858 codec_dbg(codec, "%s: Apply pinctl for %s\n",
Takashi Iwai7639a062015-03-03 10:07:24 +0100859 codec->core.chip_name, modelname);
Takashi Iwaifd108212013-01-10 10:18:14 +0100860 set_pin_targets(codec, fix->v.pins);
861 break;
Takashi Iwai23d30f22012-05-07 17:17:32 +0200862 default:
Takashi Iwai4e76a882014-02-25 12:21:03 +0100863 codec_err(codec, "%s: Invalid fixup type %d\n",
Takashi Iwai7639a062015-03-03 10:07:24 +0100864 codec->core.chip_name, fix->type);
Takashi Iwai23d30f22012-05-07 17:17:32 +0200865 break;
866 }
Takashi Iwai1f578252013-01-23 18:10:10 +0100867 if (!fix->chained || fix->chained_before)
Takashi Iwai23d30f22012-05-07 17:17:32 +0200868 break;
869 if (++depth > 10)
870 break;
871 id = fix->chain_id;
872 }
873}
Takashi Iwai1f578252013-01-23 18:10:10 +0100874
Takashi Iwai95a962c2014-10-29 16:03:58 +0100875/**
876 * snd_hda_apply_fixup - Apply the fixup chain with the given action
877 * @codec: the HDA codec
878 * @action: fixup action (HDA_FIXUP_ACT_XXX)
879 */
Takashi Iwai1f578252013-01-23 18:10:10 +0100880void snd_hda_apply_fixup(struct hda_codec *codec, int action)
881{
882 if (codec->fixup_list)
883 apply_fixup(codec, codec->fixup_id, action, 0);
884}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100885EXPORT_SYMBOL_GPL(snd_hda_apply_fixup);
Takashi Iwai23d30f22012-05-07 17:17:32 +0200886
David Henningsson20531412014-05-26 16:22:41 +0800887static bool pin_config_match(struct hda_codec *codec,
888 const struct hda_pintbl *pins)
889{
890 for (; pins->nid; pins++) {
891 u32 def_conf = snd_hda_codec_get_pincfg(codec, pins->nid);
Hui Wang37df0942014-05-29 15:59:17 +0800892 if (pins->val != def_conf)
David Henningsson20531412014-05-26 16:22:41 +0800893 return false;
894 }
895 return true;
896}
897
Takashi Iwai95a962c2014-10-29 16:03:58 +0100898/**
899 * snd_hda_pick_pin_fixup - Pick up a fixup matching with the pin quirk list
900 * @codec: the HDA codec
901 * @pin_quirk: zero-terminated pin quirk list
902 * @fixlist: the fixup list
903 */
David Henningsson20531412014-05-26 16:22:41 +0800904void snd_hda_pick_pin_fixup(struct hda_codec *codec,
905 const struct snd_hda_pin_quirk *pin_quirk,
906 const struct hda_fixup *fixlist)
907{
908 const struct snd_hda_pin_quirk *pq;
909
David Henningssonf5662e12014-07-22 14:09:34 +0200910 if (codec->fixup_id != HDA_FIXUP_ID_NOT_SET)
David Henningsson20531412014-05-26 16:22:41 +0800911 return;
912
913 for (pq = pin_quirk; pq->subvendor; pq++) {
Takashi Iwai7639a062015-03-03 10:07:24 +0100914 if ((codec->core.subsystem_id & 0xffff0000) != (pq->subvendor << 16))
David Henningsson20531412014-05-26 16:22:41 +0800915 continue;
Takashi Iwai7639a062015-03-03 10:07:24 +0100916 if (codec->core.vendor_id != pq->codec)
David Henningsson20531412014-05-26 16:22:41 +0800917 continue;
918 if (pin_config_match(codec, pq->pins)) {
919 codec->fixup_id = pq->value;
920#ifdef CONFIG_SND_DEBUG_VERBOSE
921 codec->fixup_name = pq->name;
David Henningsson4f7946e2015-01-07 14:41:59 +0100922 codec_dbg(codec, "%s: picked fixup %s (pin match)\n",
Takashi Iwai7639a062015-03-03 10:07:24 +0100923 codec->core.chip_name, codec->fixup_name);
David Henningsson20531412014-05-26 16:22:41 +0800924#endif
925 codec->fixup_list = fixlist;
926 return;
927 }
928 }
929}
930EXPORT_SYMBOL_GPL(snd_hda_pick_pin_fixup);
931
Takashi Iwai95a962c2014-10-29 16:03:58 +0100932/**
933 * snd_hda_pick_fixup - Pick up a fixup matching with PCI/codec SSID or model string
934 * @codec: the HDA codec
935 * @models: NULL-terminated model string list
936 * @quirk: zero-terminated PCI/codec SSID quirk list
937 * @fixlist: the fixup list
938 *
939 * Pick up a fixup entry matching with the given model string or SSID.
940 * If a fixup was already set beforehand, the function doesn't do anything.
941 * When a special model string "nofixup" is given, also no fixup is applied.
942 *
943 * The function tries to find the matching model name at first, if given.
944 * If nothing matched, try to look up the PCI SSID.
945 * If still nothing matched, try to look up the codec SSID.
946 */
Takashi Iwai23d30f22012-05-07 17:17:32 +0200947void snd_hda_pick_fixup(struct hda_codec *codec,
948 const struct hda_model_fixup *models,
949 const struct snd_pci_quirk *quirk,
950 const struct hda_fixup *fixlist)
951{
Takashi Iwai23d30f22012-05-07 17:17:32 +0200952 const struct snd_pci_quirk *q;
David Henningssonf5662e12014-07-22 14:09:34 +0200953 int id = HDA_FIXUP_ID_NOT_SET;
Takashi Iwai23d30f22012-05-07 17:17:32 +0200954 const char *name = NULL;
955
David Henningssonf5662e12014-07-22 14:09:34 +0200956 if (codec->fixup_id != HDA_FIXUP_ID_NOT_SET)
957 return;
958
Takashi Iwai23d30f22012-05-07 17:17:32 +0200959 /* when model=nofixup is given, don't pick up any fixups */
960 if (codec->modelname && !strcmp(codec->modelname, "nofixup")) {
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100961 codec->fixup_list = NULL;
David Henningssonf5662e12014-07-22 14:09:34 +0200962 codec->fixup_name = NULL;
963 codec->fixup_id = HDA_FIXUP_ID_NO_FIXUP;
David Henningsson4f7946e2015-01-07 14:41:59 +0100964 codec_dbg(codec, "%s: picked no fixup (nofixup specified)\n",
Takashi Iwai7639a062015-03-03 10:07:24 +0100965 codec->core.chip_name);
Takashi Iwai23d30f22012-05-07 17:17:32 +0200966 return;
967 }
968
969 if (codec->modelname && models) {
970 while (models->name) {
971 if (!strcmp(codec->modelname, models->name)) {
David Henningssonc21c8cf2014-05-26 16:22:40 +0800972 codec->fixup_id = models->id;
973 codec->fixup_name = models->name;
David Henningsson8fffe7d2014-06-19 22:07:19 +0200974 codec->fixup_list = fixlist;
David Henningsson4f7946e2015-01-07 14:41:59 +0100975 codec_dbg(codec, "%s: picked fixup %s (model specified)\n",
Takashi Iwai7639a062015-03-03 10:07:24 +0100976 codec->core.chip_name, codec->fixup_name);
David Henningssonc21c8cf2014-05-26 16:22:40 +0800977 return;
Takashi Iwai23d30f22012-05-07 17:17:32 +0200978 }
979 models++;
980 }
981 }
David Henningssonf5662e12014-07-22 14:09:34 +0200982 if (quirk) {
Takashi Iwai23d30f22012-05-07 17:17:32 +0200983 q = snd_pci_quirk_lookup(codec->bus->pci, quirk);
984 if (q) {
985 id = q->value;
986#ifdef CONFIG_SND_DEBUG_VERBOSE
987 name = q->name;
David Henningsson4f7946e2015-01-07 14:41:59 +0100988 codec_dbg(codec, "%s: picked fixup %s (PCI SSID%s)\n",
Takashi Iwai7639a062015-03-03 10:07:24 +0100989 codec->core.chip_name, name, q->subdevice_mask ? "" : " - vendor generic");
Takashi Iwai23d30f22012-05-07 17:17:32 +0200990#endif
991 }
992 }
David Henningsson639aa4b2012-07-18 18:02:53 +0200993 if (id < 0 && quirk) {
Takashi Iwai697aeba2013-08-01 08:38:27 +0200994 for (q = quirk; q->subvendor || q->subdevice; q++) {
Takashi Iwai23d30f22012-05-07 17:17:32 +0200995 unsigned int vendorid =
996 q->subdevice | (q->subvendor << 16);
Takashi Iwaia33b7b02012-09-11 16:42:18 +0200997 unsigned int mask = 0xffff0000 | q->subdevice_mask;
Takashi Iwai7639a062015-03-03 10:07:24 +0100998 if ((codec->core.subsystem_id & mask) == (vendorid & mask)) {
Takashi Iwai23d30f22012-05-07 17:17:32 +0200999 id = q->value;
1000#ifdef CONFIG_SND_DEBUG_VERBOSE
1001 name = q->name;
David Henningsson4f7946e2015-01-07 14:41:59 +01001002 codec_dbg(codec, "%s: picked fixup %s (codec SSID)\n",
Takashi Iwai7639a062015-03-03 10:07:24 +01001003 codec->core.chip_name, name);
Takashi Iwai23d30f22012-05-07 17:17:32 +02001004#endif
1005 break;
1006 }
1007 }
1008 }
1009
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01001010 codec->fixup_id = id;
Takashi Iwai23d30f22012-05-07 17:17:32 +02001011 if (id >= 0) {
Takashi Iwaic9ce6b22012-12-18 18:12:44 +01001012 codec->fixup_list = fixlist;
1013 codec->fixup_name = name;
Takashi Iwai23d30f22012-05-07 17:17:32 +02001014 }
1015}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001016EXPORT_SYMBOL_GPL(snd_hda_pick_fixup);