blob: cd1db943b7e0726587784ffaa82471638eb6bf79 [file] [log] [blame]
Thomas Gleixner62810db2019-05-20 19:08:11 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Takashi Iwai23d30f22012-05-07 17:17:32 +02002/*
3 * BIOS auto-parser helper functions for HD-audio
4 *
5 * Copyright (c) 2012 Takashi Iwai <tiwai@suse.de>
Takashi Iwai23d30f22012-05-07 17:17:32 +02006 */
7
8#include <linux/slab.h>
9#include <linux/export.h>
Takashi Iwaib9030a02012-11-29 10:18:57 +010010#include <linux/sort.h>
Takashi Iwai23d30f22012-05-07 17:17:32 +020011#include <sound/core.h>
Pierre-Louis Bossartbe57bff2018-08-22 15:24:57 -050012#include <sound/hda_codec.h>
Takashi Iwai128bc4b2012-05-07 17:42:31 +020013#include "hda_local.h"
Takashi Iwai23d30f22012-05-07 17:17:32 +020014#include "hda_auto_parser.h"
15
Takashi Iwai128bc4b2012-05-07 17:42:31 +020016/*
17 * Helper for automatic pin configuration
18 */
19
20static int is_in_nid_list(hda_nid_t nid, const hda_nid_t *list)
21{
22 for (; *list; list++)
23 if (*list == nid)
24 return 1;
25 return 0;
26}
27
Takashi Iwaib9030a02012-11-29 10:18:57 +010028/* a pair of input pin and its sequence */
29struct auto_out_pin {
30 hda_nid_t pin;
31 short seq;
32};
33
34static int compare_seq(const void *ap, const void *bp)
35{
36 const struct auto_out_pin *a = ap;
37 const struct auto_out_pin *b = bp;
38 return (int)(a->seq - b->seq);
39}
Takashi Iwai128bc4b2012-05-07 17:42:31 +020040
41/*
42 * Sort an associated group of pins according to their sequence numbers.
Takashi Iwaib9030a02012-11-29 10:18:57 +010043 * then store it to a pin array.
Takashi Iwai128bc4b2012-05-07 17:42:31 +020044 */
Takashi Iwaib9030a02012-11-29 10:18:57 +010045static void sort_pins_by_sequence(hda_nid_t *pins, struct auto_out_pin *list,
Takashi Iwai128bc4b2012-05-07 17:42:31 +020046 int num_pins)
47{
Takashi Iwaib9030a02012-11-29 10:18:57 +010048 int i;
49 sort(list, num_pins, sizeof(list[0]), compare_seq, NULL);
50 for (i = 0; i < num_pins; i++)
51 pins[i] = list[i].pin;
Takashi Iwai128bc4b2012-05-07 17:42:31 +020052}
53
54
55/* add the found input-pin to the cfg->inputs[] table */
David Henningsson95f72cf2014-09-23 10:38:18 +020056static void add_auto_cfg_input_pin(struct hda_codec *codec, struct auto_pin_cfg *cfg,
57 hda_nid_t nid, int type)
Takashi Iwai128bc4b2012-05-07 17:42:31 +020058{
59 if (cfg->num_inputs < AUTO_CFG_MAX_INS) {
60 cfg->inputs[cfg->num_inputs].pin = nid;
61 cfg->inputs[cfg->num_inputs].type = type;
David Henningsson95f72cf2014-09-23 10:38:18 +020062 cfg->inputs[cfg->num_inputs].has_boost_on_pin =
63 nid_has_volume(codec, nid, HDA_INPUT);
Takashi Iwai128bc4b2012-05-07 17:42:31 +020064 cfg->num_inputs++;
65 }
66}
67
Takashi Iwaib9030a02012-11-29 10:18:57 +010068static int compare_input_type(const void *ap, const void *bp)
Takashi Iwai128bc4b2012-05-07 17:42:31 +020069{
Takashi Iwaib9030a02012-11-29 10:18:57 +010070 const struct auto_pin_cfg_item *a = ap;
71 const struct auto_pin_cfg_item *b = bp;
David Henningsson95f72cf2014-09-23 10:38:18 +020072 if (a->type != b->type)
73 return (int)(a->type - b->type);
74
Hui Wang6a6ca782020-06-25 16:38:33 +080075 /* If has both hs_mic and hp_mic, pick the hs_mic ahead of hp_mic. */
76 if (a->is_headset_mic && b->is_headphone_mic)
77 return -1; /* don't swap */
78 else if (a->is_headphone_mic && b->is_headset_mic)
79 return 1; /* swap */
80
David Henningsson95f72cf2014-09-23 10:38:18 +020081 /* In case one has boost and the other one has not,
82 pick the one with boost first. */
83 return (int)(b->has_boost_on_pin - a->has_boost_on_pin);
Takashi Iwai128bc4b2012-05-07 17:42:31 +020084}
85
86/* Reorder the surround channels
87 * ALSA sequence is front/surr/clfe/side
88 * HDA sequence is:
89 * 4-ch: front/surr => OK as it is
90 * 6-ch: front/clfe/surr
91 * 8-ch: front/clfe/rear/side|fc
92 */
93static void reorder_outputs(unsigned int nums, hda_nid_t *pins)
94{
Takashi Iwai128bc4b2012-05-07 17:42:31 +020095 switch (nums) {
96 case 3:
97 case 4:
Yang Guang5dcdc462021-12-18 09:54:16 +080098 swap(pins[1], pins[2]);
Takashi Iwai128bc4b2012-05-07 17:42:31 +020099 break;
100 }
101}
102
Takashi Iwai52fd5cb2013-01-15 08:45:33 +0100103/* check whether the given pin has a proper pin I/O capability bit */
104static bool check_pincap_validity(struct hda_codec *codec, hda_nid_t pin,
105 unsigned int dev)
106{
107 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
108
109 /* some old hardware don't return the proper pincaps */
110 if (!pincap)
111 return true;
112
113 switch (dev) {
114 case AC_JACK_LINE_OUT:
115 case AC_JACK_SPEAKER:
116 case AC_JACK_HP_OUT:
117 case AC_JACK_SPDIF_OUT:
118 case AC_JACK_DIG_OTHER_OUT:
119 return !!(pincap & AC_PINCAP_OUT);
120 default:
121 return !!(pincap & AC_PINCAP_IN);
122 }
123}
124
David Henningssoncb420b12013-04-11 11:30:28 +0200125static bool can_be_headset_mic(struct hda_codec *codec,
126 struct auto_pin_cfg_item *item,
127 int seq_number)
128{
129 int attr;
130 unsigned int def_conf;
131 if (item->type != AUTO_PIN_MIC)
132 return false;
133
134 if (item->is_headset_mic || item->is_headphone_mic)
135 return false; /* Already assigned */
136
137 def_conf = snd_hda_codec_get_pincfg(codec, item->pin);
138 attr = snd_hda_get_input_pin_attr(def_conf);
139 if (attr <= INPUT_PIN_ATTR_DOCK)
140 return false;
141
142 if (seq_number >= 0) {
143 int seq = get_defcfg_sequence(def_conf);
144 if (seq != seq_number)
145 return false;
146 }
147
148 return true;
149}
150
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200151/*
152 * Parse all pin widgets and store the useful pin nids to cfg
153 *
154 * The number of line-outs or any primary output is stored in line_outs,
155 * and the corresponding output pins are assigned to line_out_pins[],
156 * in the order of front, rear, CLFE, side, ...
157 *
158 * If more extra outputs (speaker and headphone) are found, the pins are
159 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
160 * is detected, one of speaker of HP pins is assigned as the primary
161 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
162 * if any analog output exists.
163 *
164 * The analog input pins are assigned to inputs array.
165 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
166 * respectively.
167 */
168int snd_hda_parse_pin_defcfg(struct hda_codec *codec,
169 struct auto_pin_cfg *cfg,
170 const hda_nid_t *ignore_nids,
171 unsigned int cond_flags)
172{
Takashi Iwai7639a062015-03-03 10:07:24 +0100173 hda_nid_t nid;
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200174 short seq, assoc_line_out;
Takashi Iwaib9030a02012-11-29 10:18:57 +0100175 struct auto_out_pin line_out[ARRAY_SIZE(cfg->line_out_pins)];
176 struct auto_out_pin speaker_out[ARRAY_SIZE(cfg->speaker_pins)];
177 struct auto_out_pin hp_out[ARRAY_SIZE(cfg->hp_pins)];
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200178 int i;
179
Takashi Iwai1c70a582013-01-11 17:48:22 +0100180 if (!snd_hda_get_int_hint(codec, "parser_flags", &i))
181 cond_flags = i;
182
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200183 memset(cfg, 0, sizeof(*cfg));
184
Takashi Iwaib9030a02012-11-29 10:18:57 +0100185 memset(line_out, 0, sizeof(line_out));
186 memset(speaker_out, 0, sizeof(speaker_out));
187 memset(hp_out, 0, sizeof(hp_out));
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200188 assoc_line_out = 0;
189
Takashi Iwai7639a062015-03-03 10:07:24 +0100190 for_each_hda_codec_node(nid, codec) {
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200191 unsigned int wid_caps = get_wcaps(codec, nid);
192 unsigned int wid_type = get_wcaps_type(wid_caps);
193 unsigned int def_conf;
194 short assoc, loc, conn, dev;
195
196 /* read all default configuration for pin complex */
197 if (wid_type != AC_WID_PIN)
198 continue;
199 /* ignore the given nids (e.g. pc-beep returns error) */
200 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
201 continue;
202
203 def_conf = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200204 conn = get_defcfg_connect(def_conf);
205 if (conn == AC_JACK_PORT_NONE)
206 continue;
207 loc = get_defcfg_location(def_conf);
208 dev = get_defcfg_device(def_conf);
209
210 /* workaround for buggy BIOS setups */
211 if (dev == AC_JACK_LINE_OUT) {
Takashi Iwaifb690cf2013-01-07 18:21:47 +0100212 if (conn == AC_JACK_PORT_FIXED ||
213 conn == AC_JACK_PORT_BOTH)
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200214 dev = AC_JACK_SPEAKER;
215 }
216
Takashi Iwai52fd5cb2013-01-15 08:45:33 +0100217 if (!check_pincap_validity(codec, nid, dev))
218 continue;
219
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200220 switch (dev) {
221 case AC_JACK_LINE_OUT:
222 seq = get_defcfg_sequence(def_conf);
223 assoc = get_defcfg_association(def_conf);
224
225 if (!(wid_caps & AC_WCAP_STEREO))
226 if (!cfg->mono_out_pin)
227 cfg->mono_out_pin = nid;
228 if (!assoc)
229 continue;
230 if (!assoc_line_out)
231 assoc_line_out = assoc;
Takashi Iwai9b7564a2014-03-25 10:02:01 +0100232 else if (assoc_line_out != assoc) {
233 codec_info(codec,
234 "ignore pin 0x%x with mismatching assoc# 0x%x vs 0x%x\n",
235 nid, assoc, assoc_line_out);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200236 continue;
Takashi Iwai9b7564a2014-03-25 10:02:01 +0100237 }
238 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins)) {
239 codec_info(codec,
240 "ignore pin 0x%x, too many assigned pins\n",
241 nid);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200242 continue;
Takashi Iwai9b7564a2014-03-25 10:02:01 +0100243 }
Takashi Iwaib9030a02012-11-29 10:18:57 +0100244 line_out[cfg->line_outs].pin = nid;
245 line_out[cfg->line_outs].seq = seq;
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200246 cfg->line_outs++;
247 break;
248 case AC_JACK_SPEAKER:
249 seq = get_defcfg_sequence(def_conf);
250 assoc = get_defcfg_association(def_conf);
Takashi Iwai9b7564a2014-03-25 10:02:01 +0100251 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins)) {
252 codec_info(codec,
253 "ignore pin 0x%x, too many assigned pins\n",
254 nid);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200255 continue;
Takashi Iwai9b7564a2014-03-25 10:02:01 +0100256 }
Takashi Iwaib9030a02012-11-29 10:18:57 +0100257 speaker_out[cfg->speaker_outs].pin = nid;
258 speaker_out[cfg->speaker_outs].seq = (assoc << 4) | seq;
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200259 cfg->speaker_outs++;
260 break;
261 case AC_JACK_HP_OUT:
262 seq = get_defcfg_sequence(def_conf);
263 assoc = get_defcfg_association(def_conf);
Takashi Iwai9b7564a2014-03-25 10:02:01 +0100264 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins)) {
265 codec_info(codec,
266 "ignore pin 0x%x, too many assigned pins\n",
267 nid);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200268 continue;
Takashi Iwai9b7564a2014-03-25 10:02:01 +0100269 }
Takashi Iwaib9030a02012-11-29 10:18:57 +0100270 hp_out[cfg->hp_outs].pin = nid;
271 hp_out[cfg->hp_outs].seq = (assoc << 4) | seq;
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200272 cfg->hp_outs++;
273 break;
274 case AC_JACK_MIC_IN:
David Henningsson95f72cf2014-09-23 10:38:18 +0200275 add_auto_cfg_input_pin(codec, cfg, nid, AUTO_PIN_MIC);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200276 break;
277 case AC_JACK_LINE_IN:
David Henningsson95f72cf2014-09-23 10:38:18 +0200278 add_auto_cfg_input_pin(codec, cfg, nid, AUTO_PIN_LINE_IN);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200279 break;
280 case AC_JACK_CD:
David Henningsson95f72cf2014-09-23 10:38:18 +0200281 add_auto_cfg_input_pin(codec, cfg, nid, AUTO_PIN_CD);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200282 break;
283 case AC_JACK_AUX:
David Henningsson95f72cf2014-09-23 10:38:18 +0200284 add_auto_cfg_input_pin(codec, cfg, nid, AUTO_PIN_AUX);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200285 break;
286 case AC_JACK_SPDIF_OUT:
287 case AC_JACK_DIG_OTHER_OUT:
Takashi Iwai9b7564a2014-03-25 10:02:01 +0100288 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins)) {
289 codec_info(codec,
290 "ignore pin 0x%x, too many assigned pins\n",
291 nid);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200292 continue;
Takashi Iwai9b7564a2014-03-25 10:02:01 +0100293 }
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200294 cfg->dig_out_pins[cfg->dig_outs] = nid;
295 cfg->dig_out_type[cfg->dig_outs] =
296 (loc == AC_JACK_LOC_HDMI) ?
297 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
298 cfg->dig_outs++;
299 break;
300 case AC_JACK_SPDIF_IN:
301 case AC_JACK_DIG_OTHER_IN:
302 cfg->dig_in_pin = nid;
303 if (loc == AC_JACK_LOC_HDMI)
304 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
305 else
306 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
307 break;
308 }
309 }
310
David Henningssoncb420b12013-04-11 11:30:28 +0200311 /* Find a pin that could be a headset or headphone mic */
312 if (cond_flags & HDA_PINCFG_HEADSET_MIC || cond_flags & HDA_PINCFG_HEADPHONE_MIC) {
313 bool hsmic = !!(cond_flags & HDA_PINCFG_HEADSET_MIC);
314 bool hpmic = !!(cond_flags & HDA_PINCFG_HEADPHONE_MIC);
315 for (i = 0; (hsmic || hpmic) && (i < cfg->num_inputs); i++)
316 if (hsmic && can_be_headset_mic(codec, &cfg->inputs[i], 0xc)) {
317 cfg->inputs[i].is_headset_mic = 1;
318 hsmic = false;
319 } else if (hpmic && can_be_headset_mic(codec, &cfg->inputs[i], 0xd)) {
320 cfg->inputs[i].is_headphone_mic = 1;
321 hpmic = false;
322 }
323
324 /* If we didn't find our sequence number mark, fall back to any sequence number */
325 for (i = 0; (hsmic || hpmic) && (i < cfg->num_inputs); i++) {
326 if (!can_be_headset_mic(codec, &cfg->inputs[i], -1))
David Henningssona385d972013-03-21 12:16:29 +0100327 continue;
David Henningssoncb420b12013-04-11 11:30:28 +0200328 if (hsmic) {
329 cfg->inputs[i].is_headset_mic = 1;
330 hsmic = false;
331 } else if (hpmic) {
332 cfg->inputs[i].is_headphone_mic = 1;
333 hpmic = false;
334 }
David Henningssona385d972013-03-21 12:16:29 +0100335 }
David Henningssoncb420b12013-04-11 11:30:28 +0200336
337 if (hsmic)
Takashi Iwai4e76a882014-02-25 12:21:03 +0100338 codec_dbg(codec, "Told to look for a headset mic, but didn't find any.\n");
David Henningssoncb420b12013-04-11 11:30:28 +0200339 if (hpmic)
Takashi Iwai4e76a882014-02-25 12:21:03 +0100340 codec_dbg(codec, "Told to look for a headphone mic, but didn't find any.\n");
David Henningssona385d972013-03-21 12:16:29 +0100341 }
342
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200343 /* FIX-UP:
344 * If no line-out is defined but multiple HPs are found,
345 * some of them might be the real line-outs.
346 */
347 if (!cfg->line_outs && cfg->hp_outs > 1 &&
348 !(cond_flags & HDA_PINCFG_NO_HP_FIXUP)) {
Pierre-Louis Bossarte9bd2582020-09-02 16:21:25 -0500349 i = 0;
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200350 while (i < cfg->hp_outs) {
351 /* The real HPs should have the sequence 0x0f */
Takashi Iwaib9030a02012-11-29 10:18:57 +0100352 if ((hp_out[i].seq & 0x0f) == 0x0f) {
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200353 i++;
354 continue;
355 }
356 /* Move it to the line-out table */
Takashi Iwaib9030a02012-11-29 10:18:57 +0100357 line_out[cfg->line_outs++] = hp_out[i];
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200358 cfg->hp_outs--;
Takashi Iwaib9030a02012-11-29 10:18:57 +0100359 memmove(hp_out + i, hp_out + i + 1,
360 sizeof(hp_out[0]) * (cfg->hp_outs - i));
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200361 }
Takashi Iwaib9030a02012-11-29 10:18:57 +0100362 memset(hp_out + cfg->hp_outs, 0,
363 sizeof(hp_out[0]) * (AUTO_CFG_MAX_OUTS - cfg->hp_outs));
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200364 if (!cfg->hp_outs)
365 cfg->line_out_type = AUTO_PIN_HP_OUT;
366
367 }
368
369 /* sort by sequence */
Takashi Iwaib9030a02012-11-29 10:18:57 +0100370 sort_pins_by_sequence(cfg->line_out_pins, line_out, cfg->line_outs);
371 sort_pins_by_sequence(cfg->speaker_pins, speaker_out,
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200372 cfg->speaker_outs);
Takashi Iwaib9030a02012-11-29 10:18:57 +0100373 sort_pins_by_sequence(cfg->hp_pins, hp_out, cfg->hp_outs);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200374
375 /*
376 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
377 * as a primary output
378 */
379 if (!cfg->line_outs &&
380 !(cond_flags & HDA_PINCFG_NO_LO_FIXUP)) {
381 if (cfg->speaker_outs) {
382 cfg->line_outs = cfg->speaker_outs;
383 memcpy(cfg->line_out_pins, cfg->speaker_pins,
384 sizeof(cfg->speaker_pins));
385 cfg->speaker_outs = 0;
386 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
387 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
388 } else if (cfg->hp_outs) {
389 cfg->line_outs = cfg->hp_outs;
390 memcpy(cfg->line_out_pins, cfg->hp_pins,
391 sizeof(cfg->hp_pins));
392 cfg->hp_outs = 0;
393 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
394 cfg->line_out_type = AUTO_PIN_HP_OUT;
395 }
396 }
397
398 reorder_outputs(cfg->line_outs, cfg->line_out_pins);
399 reorder_outputs(cfg->hp_outs, cfg->hp_pins);
400 reorder_outputs(cfg->speaker_outs, cfg->speaker_pins);
401
Takashi Iwaib9030a02012-11-29 10:18:57 +0100402 /* sort inputs in the order of AUTO_PIN_* type */
403 sort(cfg->inputs, cfg->num_inputs, sizeof(cfg->inputs[0]),
404 compare_input_type, NULL);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200405
406 /*
407 * debug prints of the parsed results
408 */
David Henningsson322b8af12015-01-07 14:41:58 +0100409 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 +0100410 codec->core.chip_name, cfg->line_outs, cfg->line_out_pins[0],
David Henningsson322b8af12015-01-07 14:41:58 +0100411 cfg->line_out_pins[1], cfg->line_out_pins[2],
412 cfg->line_out_pins[3], cfg->line_out_pins[4],
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200413 cfg->line_out_type == AUTO_PIN_HP_OUT ? "hp" :
414 (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT ?
415 "speaker" : "line"));
Takashi Iwai4e76a882014-02-25 12:21:03 +0100416 codec_info(codec, " speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200417 cfg->speaker_outs, cfg->speaker_pins[0],
418 cfg->speaker_pins[1], cfg->speaker_pins[2],
419 cfg->speaker_pins[3], cfg->speaker_pins[4]);
Takashi Iwai4e76a882014-02-25 12:21:03 +0100420 codec_info(codec, " hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200421 cfg->hp_outs, cfg->hp_pins[0],
422 cfg->hp_pins[1], cfg->hp_pins[2],
423 cfg->hp_pins[3], cfg->hp_pins[4]);
Takashi Iwai4e76a882014-02-25 12:21:03 +0100424 codec_info(codec, " mono: mono_out=0x%x\n", cfg->mono_out_pin);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200425 if (cfg->dig_outs)
Takashi Iwai4e76a882014-02-25 12:21:03 +0100426 codec_info(codec, " dig-out=0x%x/0x%x\n",
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200427 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
Takashi Iwai4e76a882014-02-25 12:21:03 +0100428 codec_info(codec, " inputs:\n");
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200429 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwai4e76a882014-02-25 12:21:03 +0100430 codec_info(codec, " %s=0x%x\n",
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200431 hda_get_autocfg_input_label(codec, cfg, i),
432 cfg->inputs[i].pin);
433 }
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200434 if (cfg->dig_in_pin)
Takashi Iwai4e76a882014-02-25 12:21:03 +0100435 codec_info(codec, " dig-in=0x%x\n", cfg->dig_in_pin);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200436
437 return 0;
438}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100439EXPORT_SYMBOL_GPL(snd_hda_parse_pin_defcfg);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200440
Takashi Iwai95a962c2014-10-29 16:03:58 +0100441/**
442 * snd_hda_get_input_pin_attr - Get the input pin attribute from pin config
443 * @def_conf: pin configuration value
444 *
445 * Guess the input pin attribute (INPUT_PIN_ATTR_XXX) from the given
446 * default pin configuration value.
447 */
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200448int snd_hda_get_input_pin_attr(unsigned int def_conf)
449{
450 unsigned int loc = get_defcfg_location(def_conf);
451 unsigned int conn = get_defcfg_connect(def_conf);
452 if (conn == AC_JACK_PORT_NONE)
453 return INPUT_PIN_ATTR_UNUSED;
454 /* Windows may claim the internal mic to be BOTH, too */
455 if (conn == AC_JACK_PORT_FIXED || conn == AC_JACK_PORT_BOTH)
456 return INPUT_PIN_ATTR_INT;
457 if ((loc & 0x30) == AC_JACK_LOC_INTERNAL)
458 return INPUT_PIN_ATTR_INT;
459 if ((loc & 0x30) == AC_JACK_LOC_SEPARATE)
460 return INPUT_PIN_ATTR_DOCK;
461 if (loc == AC_JACK_LOC_REAR)
462 return INPUT_PIN_ATTR_REAR;
463 if (loc == AC_JACK_LOC_FRONT)
464 return INPUT_PIN_ATTR_FRONT;
465 return INPUT_PIN_ATTR_NORMAL;
466}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100467EXPORT_SYMBOL_GPL(snd_hda_get_input_pin_attr);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200468
469/**
470 * hda_get_input_pin_label - Give a label for the given input pin
Takashi Iwaia11e9b12014-10-29 15:06:01 +0100471 * @codec: the HDA codec
472 * @item: ping config item to refer
473 * @pin: the pin NID
474 * @check_location: flag to add the jack location prefix
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200475 *
Takashi Iwaia11e9b12014-10-29 15:06:01 +0100476 * When @check_location is true, the function checks the pin location
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200477 * for mic and line-in pins, and set an appropriate prefix like "Front",
478 * "Rear", "Internal".
479 */
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200480static const char *hda_get_input_pin_label(struct hda_codec *codec,
David Henningssona385d972013-03-21 12:16:29 +0100481 const struct auto_pin_cfg_item *item,
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200482 hda_nid_t pin, bool check_location)
483{
484 unsigned int def_conf;
485 static const char * const mic_names[] = {
Takashi Iwai5ec16d12012-11-28 18:11:59 +0100486 "Internal Mic", "Dock Mic", "Mic", "Rear Mic", "Front Mic"
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200487 };
488 int attr;
489
490 def_conf = snd_hda_codec_get_pincfg(codec, pin);
491
492 switch (get_defcfg_device(def_conf)) {
493 case AC_JACK_MIC_IN:
David Henningssona385d972013-03-21 12:16:29 +0100494 if (item && item->is_headset_mic)
495 return "Headset Mic";
David Henningssoncb420b12013-04-11 11:30:28 +0200496 if (item && item->is_headphone_mic)
497 return "Headphone Mic";
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200498 if (!check_location)
499 return "Mic";
500 attr = snd_hda_get_input_pin_attr(def_conf);
501 if (!attr)
502 return "None";
503 return mic_names[attr - 1];
504 case AC_JACK_LINE_IN:
505 if (!check_location)
506 return "Line";
507 attr = snd_hda_get_input_pin_attr(def_conf);
508 if (!attr)
509 return "None";
510 if (attr == INPUT_PIN_ATTR_DOCK)
511 return "Dock Line";
512 return "Line";
513 case AC_JACK_AUX:
514 return "Aux";
515 case AC_JACK_CD:
516 return "CD";
517 case AC_JACK_SPDIF_IN:
518 return "SPDIF In";
519 case AC_JACK_DIG_OTHER_IN:
520 return "Digital In";
Takashi Iwai54d778b2013-01-09 08:46:34 +0100521 case AC_JACK_HP_OUT:
522 return "Headphone Mic";
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200523 default:
524 return "Misc";
525 }
526}
527
528/* Check whether the location prefix needs to be added to the label.
529 * If all mic-jacks are in the same location (e.g. rear panel), we don't
530 * have to put "Front" prefix to each label. In such a case, returns false.
531 */
532static int check_mic_location_need(struct hda_codec *codec,
533 const struct auto_pin_cfg *cfg,
534 int input)
535{
536 unsigned int defc;
537 int i, attr, attr2;
538
539 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[input].pin);
540 attr = snd_hda_get_input_pin_attr(defc);
541 /* for internal or docking mics, we need locations */
542 if (attr <= INPUT_PIN_ATTR_NORMAL)
543 return 1;
544
545 attr = 0;
546 for (i = 0; i < cfg->num_inputs; i++) {
547 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[i].pin);
548 attr2 = snd_hda_get_input_pin_attr(defc);
549 if (attr2 >= INPUT_PIN_ATTR_NORMAL) {
550 if (attr && attr != attr2)
551 return 1; /* different locations found */
552 attr = attr2;
553 }
554 }
555 return 0;
556}
557
558/**
559 * hda_get_autocfg_input_label - Get a label for the given input
Takashi Iwaia11e9b12014-10-29 15:06:01 +0100560 * @codec: the HDA codec
561 * @cfg: the parsed pin configuration
562 * @input: the input index number
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200563 *
564 * Get a label for the given input pin defined by the autocfg item.
565 * Unlike hda_get_input_pin_label(), this function checks all inputs
566 * defined in autocfg and avoids the redundant mic/line prefix as much as
567 * possible.
568 */
569const char *hda_get_autocfg_input_label(struct hda_codec *codec,
570 const struct auto_pin_cfg *cfg,
571 int input)
572{
573 int type = cfg->inputs[input].type;
574 int has_multiple_pins = 0;
575
576 if ((input > 0 && cfg->inputs[input - 1].type == type) ||
577 (input < cfg->num_inputs - 1 && cfg->inputs[input + 1].type == type))
578 has_multiple_pins = 1;
579 if (has_multiple_pins && type == AUTO_PIN_MIC)
580 has_multiple_pins &= check_mic_location_need(codec, cfg, input);
Takashi Iwai9f3dadb2017-04-10 17:12:33 +0200581 has_multiple_pins |= codec->force_pin_prefix;
David Henningssona385d972013-03-21 12:16:29 +0100582 return hda_get_input_pin_label(codec, &cfg->inputs[input],
583 cfg->inputs[input].pin,
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200584 has_multiple_pins);
585}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100586EXPORT_SYMBOL_GPL(hda_get_autocfg_input_label);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200587
588/* return the position of NID in the list, or -1 if not found */
589static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
590{
591 int i;
592 for (i = 0; i < nums; i++)
593 if (list[i] == nid)
594 return i;
595 return -1;
596}
597
598/* get a unique suffix or an index number */
599static const char *check_output_sfx(hda_nid_t nid, const hda_nid_t *pins,
600 int num_pins, int *indexp)
601{
602 static const char * const channel_sfx[] = {
603 " Front", " Surround", " CLFE", " Side"
604 };
605 int i;
606
607 i = find_idx_in_nid_list(nid, pins, num_pins);
608 if (i < 0)
609 return NULL;
610 if (num_pins == 1)
611 return "";
612 if (num_pins > ARRAY_SIZE(channel_sfx)) {
613 if (indexp)
614 *indexp = i;
615 return "";
616 }
617 return channel_sfx[i];
618}
619
David Henningssoneee3ed432012-10-03 11:12:53 +0200620static const char *check_output_pfx(struct hda_codec *codec, hda_nid_t nid)
621{
622 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
623 int attr = snd_hda_get_input_pin_attr(def_conf);
624
625 /* check the location */
626 switch (attr) {
627 case INPUT_PIN_ATTR_DOCK:
628 return "Dock ";
629 case INPUT_PIN_ATTR_FRONT:
630 return "Front ";
631 }
632 return "";
633}
634
635static int get_hp_label_index(struct hda_codec *codec, hda_nid_t nid,
636 const hda_nid_t *pins, int num_pins)
637{
638 int i, j, idx = 0;
639
640 const char *pfx = check_output_pfx(codec, nid);
641
642 i = find_idx_in_nid_list(nid, pins, num_pins);
643 if (i < 0)
644 return -1;
645 for (j = 0; j < i; j++)
646 if (pfx == check_output_pfx(codec, pins[j]))
647 idx++;
648
649 return idx;
650}
651
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200652static int fill_audio_out_name(struct hda_codec *codec, hda_nid_t nid,
653 const struct auto_pin_cfg *cfg,
654 const char *name, char *label, int maxlen,
655 int *indexp)
656{
657 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
658 int attr = snd_hda_get_input_pin_attr(def_conf);
David Henningssoneee3ed432012-10-03 11:12:53 +0200659 const char *pfx, *sfx = "";
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200660
661 /* handle as a speaker if it's a fixed line-out */
662 if (!strcmp(name, "Line Out") && attr == INPUT_PIN_ATTR_INT)
663 name = "Speaker";
David Henningssoneee3ed432012-10-03 11:12:53 +0200664 pfx = check_output_pfx(codec, nid);
665
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200666 if (cfg) {
667 /* try to give a unique suffix if needed */
668 sfx = check_output_sfx(nid, cfg->line_out_pins, cfg->line_outs,
669 indexp);
670 if (!sfx)
671 sfx = check_output_sfx(nid, cfg->speaker_pins, cfg->speaker_outs,
672 indexp);
673 if (!sfx) {
674 /* don't add channel suffix for Headphone controls */
David Henningssoneee3ed432012-10-03 11:12:53 +0200675 int idx = get_hp_label_index(codec, nid, cfg->hp_pins,
676 cfg->hp_outs);
Takashi Iwai728deec2013-10-28 11:39:23 +0100677 if (idx >= 0 && indexp)
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200678 *indexp = idx;
679 sfx = "";
680 }
681 }
682 snprintf(label, maxlen, "%s%s%s", pfx, name, sfx);
683 return 1;
684}
685
David Henningsson3f25dcf2013-01-18 15:43:03 +0100686#define is_hdmi_cfg(conf) \
687 (get_defcfg_location(conf) == AC_JACK_LOC_HDMI)
688
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200689/**
690 * snd_hda_get_pin_label - Get a label for the given I/O pin
Takashi Iwaia11e9b12014-10-29 15:06:01 +0100691 * @codec: the HDA codec
692 * @nid: pin NID
693 * @cfg: the parsed pin configuration
694 * @label: the string buffer to store
695 * @maxlen: the max length of string buffer (including termination)
696 * @indexp: the pointer to return the index number (for multiple ctls)
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200697 *
698 * Get a label for the given pin. This function works for both input and
699 * output pins. When @cfg is given as non-NULL, the function tries to get
700 * an optimized label using hda_get_autocfg_input_label().
701 *
702 * This function tries to give a unique label string for the pin as much as
703 * possible. For example, when the multiple line-outs are present, it adds
704 * the channel suffix like "Front", "Surround", etc (only when @cfg is given).
705 * If no unique name with a suffix is available and @indexp is non-NULL, the
706 * index number is stored in the pointer.
707 */
708int snd_hda_get_pin_label(struct hda_codec *codec, hda_nid_t nid,
709 const struct auto_pin_cfg *cfg,
710 char *label, int maxlen, int *indexp)
711{
712 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
713 const char *name = NULL;
714 int i;
David Henningsson3f25dcf2013-01-18 15:43:03 +0100715 bool hdmi;
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200716
717 if (indexp)
718 *indexp = 0;
719 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
720 return 0;
721
722 switch (get_defcfg_device(def_conf)) {
723 case AC_JACK_LINE_OUT:
724 return fill_audio_out_name(codec, nid, cfg, "Line Out",
725 label, maxlen, indexp);
726 case AC_JACK_SPEAKER:
727 return fill_audio_out_name(codec, nid, cfg, "Speaker",
728 label, maxlen, indexp);
729 case AC_JACK_HP_OUT:
730 return fill_audio_out_name(codec, nid, cfg, "Headphone",
731 label, maxlen, indexp);
732 case AC_JACK_SPDIF_OUT:
733 case AC_JACK_DIG_OTHER_OUT:
David Henningsson3f25dcf2013-01-18 15:43:03 +0100734 hdmi = is_hdmi_cfg(def_conf);
735 name = hdmi ? "HDMI" : "SPDIF";
736 if (cfg && indexp)
737 for (i = 0; i < cfg->dig_outs; i++) {
738 hda_nid_t pin = cfg->dig_out_pins[i];
739 unsigned int c;
740 if (pin == nid)
741 break;
742 c = snd_hda_codec_get_pincfg(codec, pin);
743 if (hdmi == is_hdmi_cfg(c))
744 (*indexp)++;
745 }
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200746 break;
747 default:
748 if (cfg) {
749 for (i = 0; i < cfg->num_inputs; i++) {
750 if (cfg->inputs[i].pin != nid)
751 continue;
752 name = hda_get_autocfg_input_label(codec, cfg, i);
753 if (name)
754 break;
755 }
756 }
757 if (!name)
David Henningssona385d972013-03-21 12:16:29 +0100758 name = hda_get_input_pin_label(codec, NULL, nid, true);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200759 break;
760 }
761 if (!name)
762 return 0;
Joe Perches75b1a8f2021-01-04 09:17:34 -0800763 strscpy(label, name, maxlen);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200764 return 1;
765}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100766EXPORT_SYMBOL_GPL(snd_hda_get_pin_label);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200767
Takashi Iwai95a962c2014-10-29 16:03:58 +0100768/**
769 * snd_hda_add_verbs - Add verbs to the init list
770 * @codec: the HDA codec
771 * @list: zero-terminated verb list to add
772 *
773 * Append the given verb list to the execution list. The verbs will be
774 * performed at init and resume time via snd_hda_apply_verbs().
775 */
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100776int snd_hda_add_verbs(struct hda_codec *codec,
777 const struct hda_verb *list)
Takashi Iwai23d30f22012-05-07 17:17:32 +0200778{
779 const struct hda_verb **v;
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100780 v = snd_array_new(&codec->verbs);
Takashi Iwai23d30f22012-05-07 17:17:32 +0200781 if (!v)
782 return -ENOMEM;
783 *v = list;
784 return 0;
785}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100786EXPORT_SYMBOL_GPL(snd_hda_add_verbs);
Takashi Iwai23d30f22012-05-07 17:17:32 +0200787
Takashi Iwai95a962c2014-10-29 16:03:58 +0100788/**
789 * snd_hda_apply_verbs - Execute the init verb lists
790 * @codec: the HDA codec
791 */
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100792void snd_hda_apply_verbs(struct hda_codec *codec)
Takashi Iwai23d30f22012-05-07 17:17:32 +0200793{
Takashi Iwaia9c2dfc2018-04-23 17:24:56 +0200794 const struct hda_verb **v;
Takashi Iwai23d30f22012-05-07 17:17:32 +0200795 int i;
Takashi Iwaia9c2dfc2018-04-23 17:24:56 +0200796
797 snd_array_for_each(&codec->verbs, i, v)
Takashi Iwai23d30f22012-05-07 17:17:32 +0200798 snd_hda_sequence_write(codec, *v);
Takashi Iwai23d30f22012-05-07 17:17:32 +0200799}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100800EXPORT_SYMBOL_GPL(snd_hda_apply_verbs);
Takashi Iwai23d30f22012-05-07 17:17:32 +0200801
Takashi Iwai95a962c2014-10-29 16:03:58 +0100802/**
803 * snd_hda_apply_pincfgs - Set each pin config in the given list
804 * @codec: the HDA codec
805 * @cfg: NULL-terminated pin config table
806 */
Takashi Iwai23d30f22012-05-07 17:17:32 +0200807void snd_hda_apply_pincfgs(struct hda_codec *codec,
808 const struct hda_pintbl *cfg)
809{
810 for (; cfg->nid; cfg++)
811 snd_hda_codec_set_pincfg(codec, cfg->nid, cfg->val);
812}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100813EXPORT_SYMBOL_GPL(snd_hda_apply_pincfgs);
Takashi Iwai23d30f22012-05-07 17:17:32 +0200814
Takashi Iwaifd108212013-01-10 10:18:14 +0100815static void set_pin_targets(struct hda_codec *codec,
816 const struct hda_pintbl *cfg)
817{
818 for (; cfg->nid; cfg++)
819 snd_hda_set_pin_ctl_cache(codec, cfg->nid, cfg->val);
820}
821
Takashi Iwai1f578252013-01-23 18:10:10 +0100822static void apply_fixup(struct hda_codec *codec, int id, int action, int depth)
Takashi Iwai23d30f22012-05-07 17:17:32 +0200823{
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100824 const char *modelname = codec->fixup_name;
Takashi Iwai23d30f22012-05-07 17:17:32 +0200825
826 while (id >= 0) {
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100827 const struct hda_fixup *fix = codec->fixup_list + id;
Takashi Iwai23d30f22012-05-07 17:17:32 +0200828
Takashi Iwai333f3142019-08-29 09:52:02 +0200829 if (++depth > 10)
830 break;
Takashi Iwai1f578252013-01-23 18:10:10 +0100831 if (fix->chained_before)
832 apply_fixup(codec, fix->chain_id, action, depth + 1);
833
Takashi Iwai23d30f22012-05-07 17:17:32 +0200834 switch (fix->type) {
835 case HDA_FIXUP_PINS:
836 if (action != HDA_FIXUP_ACT_PRE_PROBE || !fix->v.pins)
837 break;
Takashi Iwai4e76a882014-02-25 12:21:03 +0100838 codec_dbg(codec, "%s: Apply pincfg for %s\n",
Takashi Iwai7639a062015-03-03 10:07:24 +0100839 codec->core.chip_name, modelname);
Takashi Iwai23d30f22012-05-07 17:17:32 +0200840 snd_hda_apply_pincfgs(codec, fix->v.pins);
841 break;
842 case HDA_FIXUP_VERBS:
843 if (action != HDA_FIXUP_ACT_PROBE || !fix->v.verbs)
844 break;
Takashi Iwai4e76a882014-02-25 12:21:03 +0100845 codec_dbg(codec, "%s: Apply fix-verbs for %s\n",
Takashi Iwai7639a062015-03-03 10:07:24 +0100846 codec->core.chip_name, modelname);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100847 snd_hda_add_verbs(codec, fix->v.verbs);
Takashi Iwai23d30f22012-05-07 17:17:32 +0200848 break;
849 case HDA_FIXUP_FUNC:
850 if (!fix->v.func)
851 break;
Takashi Iwai4e76a882014-02-25 12:21:03 +0100852 codec_dbg(codec, "%s: Apply fix-func for %s\n",
Takashi Iwai7639a062015-03-03 10:07:24 +0100853 codec->core.chip_name, modelname);
Takashi Iwai23d30f22012-05-07 17:17:32 +0200854 fix->v.func(codec, fix, action);
855 break;
Takashi Iwaifd108212013-01-10 10:18:14 +0100856 case HDA_FIXUP_PINCTLS:
857 if (action != HDA_FIXUP_ACT_PROBE || !fix->v.pins)
858 break;
Takashi Iwai4e76a882014-02-25 12:21:03 +0100859 codec_dbg(codec, "%s: Apply pinctl for %s\n",
Takashi Iwai7639a062015-03-03 10:07:24 +0100860 codec->core.chip_name, modelname);
Takashi Iwaifd108212013-01-10 10:18:14 +0100861 set_pin_targets(codec, fix->v.pins);
862 break;
Takashi Iwai23d30f22012-05-07 17:17:32 +0200863 default:
Takashi Iwai4e76a882014-02-25 12:21:03 +0100864 codec_err(codec, "%s: Invalid fixup type %d\n",
Takashi Iwai7639a062015-03-03 10:07:24 +0100865 codec->core.chip_name, fix->type);
Takashi Iwai23d30f22012-05-07 17:17:32 +0200866 break;
867 }
Takashi Iwai1f578252013-01-23 18:10:10 +0100868 if (!fix->chained || fix->chained_before)
Takashi Iwai23d30f22012-05-07 17:17:32 +0200869 break;
Takashi Iwai23d30f22012-05-07 17:17:32 +0200870 id = fix->chain_id;
871 }
872}
Takashi Iwai1f578252013-01-23 18:10:10 +0100873
Takashi Iwai95a962c2014-10-29 16:03:58 +0100874/**
875 * snd_hda_apply_fixup - Apply the fixup chain with the given action
876 * @codec: the HDA codec
877 * @action: fixup action (HDA_FIXUP_ACT_XXX)
878 */
Takashi Iwai1f578252013-01-23 18:10:10 +0100879void snd_hda_apply_fixup(struct hda_codec *codec, int action)
880{
881 if (codec->fixup_list)
882 apply_fixup(codec, codec->fixup_id, action, 0);
883}
Takashi Iwai2698ea92013-12-18 07:45:52 +0100884EXPORT_SYMBOL_GPL(snd_hda_apply_fixup);
Takashi Iwai23d30f22012-05-07 17:17:32 +0200885
Kai-Heng Feng5e0ad0d2016-12-06 16:56:27 +0800886#define IGNORE_SEQ_ASSOC (~(AC_DEFCFG_SEQUENCE | AC_DEFCFG_DEF_ASSOC))
887
David Henningsson20531412014-05-26 16:22:41 +0800888static bool pin_config_match(struct hda_codec *codec,
Hui Wang0fc1e442019-08-16 14:27:39 +0800889 const struct hda_pintbl *pins,
890 bool match_all_pins)
David Henningsson20531412014-05-26 16:22:41 +0800891{
Takashi Iwaia9c2dfc2018-04-23 17:24:56 +0200892 const struct hda_pincfg *pin;
Hui Wang11580292015-08-03 11:03:49 +0800893 int i;
894
Takashi Iwaia9c2dfc2018-04-23 17:24:56 +0200895 snd_array_for_each(&codec->init_pins, i, pin) {
Hui Wang11580292015-08-03 11:03:49 +0800896 hda_nid_t nid = pin->nid;
897 u32 cfg = pin->cfg;
898 const struct hda_pintbl *t_pins;
899 int found;
900
901 t_pins = pins;
902 found = 0;
903 for (; t_pins->nid; t_pins++) {
904 if (t_pins->nid == nid) {
905 found = 1;
Kai-Heng Feng5e0ad0d2016-12-06 16:56:27 +0800906 if ((t_pins->val & IGNORE_SEQ_ASSOC) == (cfg & IGNORE_SEQ_ASSOC))
Hui Wang11580292015-08-03 11:03:49 +0800907 break;
908 else if ((cfg & 0xf0000000) == 0x40000000 && (t_pins->val & 0xf0000000) == 0x40000000)
909 break;
910 else
911 return false;
912 }
913 }
Hui Wang0fc1e442019-08-16 14:27:39 +0800914 if (match_all_pins &&
915 !found && (cfg & 0xf0000000) != 0x40000000)
David Henningsson20531412014-05-26 16:22:41 +0800916 return false;
917 }
Hui Wang11580292015-08-03 11:03:49 +0800918
David Henningsson20531412014-05-26 16:22:41 +0800919 return true;
920}
921
Takashi Iwai95a962c2014-10-29 16:03:58 +0100922/**
923 * snd_hda_pick_pin_fixup - Pick up a fixup matching with the pin quirk list
924 * @codec: the HDA codec
925 * @pin_quirk: zero-terminated pin quirk list
926 * @fixlist: the fixup list
Hui Wang0fc1e442019-08-16 14:27:39 +0800927 * @match_all_pins: all valid pins must match with the table entries
Takashi Iwai95a962c2014-10-29 16:03:58 +0100928 */
David Henningsson20531412014-05-26 16:22:41 +0800929void snd_hda_pick_pin_fixup(struct hda_codec *codec,
930 const struct snd_hda_pin_quirk *pin_quirk,
Hui Wang0fc1e442019-08-16 14:27:39 +0800931 const struct hda_fixup *fixlist,
932 bool match_all_pins)
David Henningsson20531412014-05-26 16:22:41 +0800933{
934 const struct snd_hda_pin_quirk *pq;
935
David Henningssonf5662e12014-07-22 14:09:34 +0200936 if (codec->fixup_id != HDA_FIXUP_ID_NOT_SET)
David Henningsson20531412014-05-26 16:22:41 +0800937 return;
938
939 for (pq = pin_quirk; pq->subvendor; pq++) {
Takashi Iwai7639a062015-03-03 10:07:24 +0100940 if ((codec->core.subsystem_id & 0xffff0000) != (pq->subvendor << 16))
David Henningsson20531412014-05-26 16:22:41 +0800941 continue;
Takashi Iwai7639a062015-03-03 10:07:24 +0100942 if (codec->core.vendor_id != pq->codec)
David Henningsson20531412014-05-26 16:22:41 +0800943 continue;
Hui Wang0fc1e442019-08-16 14:27:39 +0800944 if (pin_config_match(codec, pq->pins, match_all_pins)) {
David Henningsson20531412014-05-26 16:22:41 +0800945 codec->fixup_id = pq->value;
946#ifdef CONFIG_SND_DEBUG_VERBOSE
947 codec->fixup_name = pq->name;
David Henningsson4f7946e2015-01-07 14:41:59 +0100948 codec_dbg(codec, "%s: picked fixup %s (pin match)\n",
Takashi Iwai7639a062015-03-03 10:07:24 +0100949 codec->core.chip_name, codec->fixup_name);
David Henningsson20531412014-05-26 16:22:41 +0800950#endif
951 codec->fixup_list = fixlist;
952 return;
953 }
954 }
955}
956EXPORT_SYMBOL_GPL(snd_hda_pick_pin_fixup);
957
Takashi Iwai95a962c2014-10-29 16:03:58 +0100958/**
959 * snd_hda_pick_fixup - Pick up a fixup matching with PCI/codec SSID or model string
960 * @codec: the HDA codec
961 * @models: NULL-terminated model string list
962 * @quirk: zero-terminated PCI/codec SSID quirk list
963 * @fixlist: the fixup list
964 *
965 * Pick up a fixup entry matching with the given model string or SSID.
966 * If a fixup was already set beforehand, the function doesn't do anything.
967 * When a special model string "nofixup" is given, also no fixup is applied.
968 *
969 * The function tries to find the matching model name at first, if given.
Takashi Iwaia235d5b2021-08-23 09:37:21 +0200970 * If the model string contains the SSID alias, try to look up with the given
971 * alias ID.
Takashi Iwai95a962c2014-10-29 16:03:58 +0100972 * If nothing matched, try to look up the PCI SSID.
973 * If still nothing matched, try to look up the codec SSID.
974 */
Takashi Iwai23d30f22012-05-07 17:17:32 +0200975void snd_hda_pick_fixup(struct hda_codec *codec,
976 const struct hda_model_fixup *models,
977 const struct snd_pci_quirk *quirk,
978 const struct hda_fixup *fixlist)
979{
Takashi Iwai23d30f22012-05-07 17:17:32 +0200980 const struct snd_pci_quirk *q;
David Henningssonf5662e12014-07-22 14:09:34 +0200981 int id = HDA_FIXUP_ID_NOT_SET;
Takashi Iwai23d30f22012-05-07 17:17:32 +0200982 const char *name = NULL;
Takashi Iwai73355ddd2021-08-23 09:37:20 +0200983 const char *type = NULL;
Takashi Iwai0444f822022-01-27 14:57:17 +0100984 unsigned int vendor, device;
Takashi Iwai23d30f22012-05-07 17:17:32 +0200985
David Henningssonf5662e12014-07-22 14:09:34 +0200986 if (codec->fixup_id != HDA_FIXUP_ID_NOT_SET)
987 return;
988
Takashi Iwai23d30f22012-05-07 17:17:32 +0200989 /* when model=nofixup is given, don't pick up any fixups */
990 if (codec->modelname && !strcmp(codec->modelname, "nofixup")) {
Takashi Iwai73355ddd2021-08-23 09:37:20 +0200991 id = HDA_FIXUP_ID_NO_FIXUP;
992 fixlist = NULL;
David Henningsson4f7946e2015-01-07 14:41:59 +0100993 codec_dbg(codec, "%s: picked no fixup (nofixup specified)\n",
Takashi Iwai7639a062015-03-03 10:07:24 +0100994 codec->core.chip_name);
Takashi Iwai73355ddd2021-08-23 09:37:20 +0200995 goto found;
Takashi Iwai23d30f22012-05-07 17:17:32 +0200996 }
997
Takashi Iwai73355ddd2021-08-23 09:37:20 +0200998 /* match with the model name string */
Takashi Iwai23d30f22012-05-07 17:17:32 +0200999 if (codec->modelname && models) {
1000 while (models->name) {
1001 if (!strcmp(codec->modelname, models->name)) {
Takashi Iwai73355ddd2021-08-23 09:37:20 +02001002 id = models->id;
1003 name = models->name;
David Henningsson4f7946e2015-01-07 14:41:59 +01001004 codec_dbg(codec, "%s: picked fixup %s (model specified)\n",
Takashi Iwai7639a062015-03-03 10:07:24 +01001005 codec->core.chip_name, codec->fixup_name);
Takashi Iwai73355ddd2021-08-23 09:37:20 +02001006 goto found;
Takashi Iwai23d30f22012-05-07 17:17:32 +02001007 }
1008 models++;
1009 }
1010 }
Takashi Iwai73355ddd2021-08-23 09:37:20 +02001011
1012 if (!quirk)
1013 return;
1014
Takashi Iwaia235d5b2021-08-23 09:37:21 +02001015 /* match with the SSID alias given by the model string "XXXX:YYYY" */
1016 if (codec->modelname &&
1017 sscanf(codec->modelname, "%04x:%04x", &vendor, &device) == 2) {
1018 q = snd_pci_quirk_lookup_id(vendor, device, quirk);
1019 if (q) {
1020 type = "alias SSID";
1021 goto found_device;
1022 }
1023 }
1024
Takashi Iwai73355ddd2021-08-23 09:37:20 +02001025 /* match with the PCI SSID */
1026 q = snd_pci_quirk_lookup(codec->bus->pci, quirk);
1027 if (q) {
1028 type = "PCI SSID";
1029 goto found_device;
Takashi Iwai23d30f22012-05-07 17:17:32 +02001030 }
1031
Takashi Iwai73355ddd2021-08-23 09:37:20 +02001032 /* match with the codec SSID */
1033 q = snd_pci_quirk_lookup_id(codec->core.subsystem_id >> 16,
1034 codec->core.subsystem_id & 0xffff,
1035 quirk);
1036 if (q) {
1037 type = "codec SSID";
1038 goto found_device;
Takashi Iwai23d30f22012-05-07 17:17:32 +02001039 }
Takashi Iwai73355ddd2021-08-23 09:37:20 +02001040
1041 return; /* no matching */
1042
1043 found_device:
1044 id = q->value;
1045#ifdef CONFIG_SND_DEBUG_VERBOSE
1046 name = q->name;
1047#endif
1048 codec_dbg(codec, "%s: picked fixup %s for %s %04x:%04x\n",
1049 codec->core.chip_name, name ? name : "",
1050 type, q->subvendor, q->subdevice);
1051 found:
1052 codec->fixup_id = id;
1053 codec->fixup_list = fixlist;
1054 codec->fixup_name = name;
Takashi Iwai23d30f22012-05-07 17:17:32 +02001055}
Takashi Iwai2698ea92013-12-18 07:45:52 +01001056EXPORT_SYMBOL_GPL(snd_hda_pick_fixup);