blob: 33b3ece224c6917e39a24ab033eb76782e8e3c18 [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
100/*
101 * Parse all pin widgets and store the useful pin nids to cfg
102 *
103 * The number of line-outs or any primary output is stored in line_outs,
104 * and the corresponding output pins are assigned to line_out_pins[],
105 * in the order of front, rear, CLFE, side, ...
106 *
107 * If more extra outputs (speaker and headphone) are found, the pins are
108 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
109 * is detected, one of speaker of HP pins is assigned as the primary
110 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
111 * if any analog output exists.
112 *
113 * The analog input pins are assigned to inputs array.
114 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
115 * respectively.
116 */
117int snd_hda_parse_pin_defcfg(struct hda_codec *codec,
118 struct auto_pin_cfg *cfg,
119 const hda_nid_t *ignore_nids,
120 unsigned int cond_flags)
121{
122 hda_nid_t nid, end_nid;
123 short seq, assoc_line_out;
Takashi Iwaib9030a02012-11-29 10:18:57 +0100124 struct auto_out_pin line_out[ARRAY_SIZE(cfg->line_out_pins)];
125 struct auto_out_pin speaker_out[ARRAY_SIZE(cfg->speaker_pins)];
126 struct auto_out_pin hp_out[ARRAY_SIZE(cfg->hp_pins)];
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200127 int i;
128
Takashi Iwai1c70a582013-01-11 17:48:22 +0100129 if (!snd_hda_get_int_hint(codec, "parser_flags", &i))
130 cond_flags = i;
131
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200132 memset(cfg, 0, sizeof(*cfg));
133
Takashi Iwaib9030a02012-11-29 10:18:57 +0100134 memset(line_out, 0, sizeof(line_out));
135 memset(speaker_out, 0, sizeof(speaker_out));
136 memset(hp_out, 0, sizeof(hp_out));
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200137 assoc_line_out = 0;
138
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200139 end_nid = codec->start_nid + codec->num_nodes;
140 for (nid = codec->start_nid; nid < end_nid; nid++) {
141 unsigned int wid_caps = get_wcaps(codec, nid);
142 unsigned int wid_type = get_wcaps_type(wid_caps);
143 unsigned int def_conf;
144 short assoc, loc, conn, dev;
145
146 /* read all default configuration for pin complex */
147 if (wid_type != AC_WID_PIN)
148 continue;
149 /* ignore the given nids (e.g. pc-beep returns error) */
150 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
151 continue;
152
153 def_conf = snd_hda_codec_get_pincfg(codec, nid);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200154 conn = get_defcfg_connect(def_conf);
155 if (conn == AC_JACK_PORT_NONE)
156 continue;
157 loc = get_defcfg_location(def_conf);
158 dev = get_defcfg_device(def_conf);
159
160 /* workaround for buggy BIOS setups */
161 if (dev == AC_JACK_LINE_OUT) {
Takashi Iwaifb690cf2013-01-07 18:21:47 +0100162 if (conn == AC_JACK_PORT_FIXED ||
163 conn == AC_JACK_PORT_BOTH)
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200164 dev = AC_JACK_SPEAKER;
165 }
166
167 switch (dev) {
168 case AC_JACK_LINE_OUT:
169 seq = get_defcfg_sequence(def_conf);
170 assoc = get_defcfg_association(def_conf);
171
172 if (!(wid_caps & AC_WCAP_STEREO))
173 if (!cfg->mono_out_pin)
174 cfg->mono_out_pin = nid;
175 if (!assoc)
176 continue;
177 if (!assoc_line_out)
178 assoc_line_out = assoc;
179 else if (assoc_line_out != assoc)
180 continue;
181 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
182 continue;
Takashi Iwaib9030a02012-11-29 10:18:57 +0100183 line_out[cfg->line_outs].pin = nid;
184 line_out[cfg->line_outs].seq = seq;
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200185 cfg->line_outs++;
186 break;
187 case AC_JACK_SPEAKER:
188 seq = get_defcfg_sequence(def_conf);
189 assoc = get_defcfg_association(def_conf);
190 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
191 continue;
Takashi Iwaib9030a02012-11-29 10:18:57 +0100192 speaker_out[cfg->speaker_outs].pin = nid;
193 speaker_out[cfg->speaker_outs].seq = (assoc << 4) | seq;
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200194 cfg->speaker_outs++;
195 break;
196 case AC_JACK_HP_OUT:
197 seq = get_defcfg_sequence(def_conf);
198 assoc = get_defcfg_association(def_conf);
199 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
200 continue;
Takashi Iwaib9030a02012-11-29 10:18:57 +0100201 hp_out[cfg->hp_outs].pin = nid;
202 hp_out[cfg->hp_outs].seq = (assoc << 4) | seq;
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200203 cfg->hp_outs++;
204 break;
205 case AC_JACK_MIC_IN:
206 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_MIC);
207 break;
208 case AC_JACK_LINE_IN:
209 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_LINE_IN);
210 break;
211 case AC_JACK_CD:
212 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_CD);
213 break;
214 case AC_JACK_AUX:
215 add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_AUX);
216 break;
217 case AC_JACK_SPDIF_OUT:
218 case AC_JACK_DIG_OTHER_OUT:
219 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
220 continue;
221 cfg->dig_out_pins[cfg->dig_outs] = nid;
222 cfg->dig_out_type[cfg->dig_outs] =
223 (loc == AC_JACK_LOC_HDMI) ?
224 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
225 cfg->dig_outs++;
226 break;
227 case AC_JACK_SPDIF_IN:
228 case AC_JACK_DIG_OTHER_IN:
229 cfg->dig_in_pin = nid;
230 if (loc == AC_JACK_LOC_HDMI)
231 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
232 else
233 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
234 break;
235 }
236 }
237
238 /* FIX-UP:
239 * If no line-out is defined but multiple HPs are found,
240 * some of them might be the real line-outs.
241 */
242 if (!cfg->line_outs && cfg->hp_outs > 1 &&
243 !(cond_flags & HDA_PINCFG_NO_HP_FIXUP)) {
244 int i = 0;
245 while (i < cfg->hp_outs) {
246 /* The real HPs should have the sequence 0x0f */
Takashi Iwaib9030a02012-11-29 10:18:57 +0100247 if ((hp_out[i].seq & 0x0f) == 0x0f) {
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200248 i++;
249 continue;
250 }
251 /* Move it to the line-out table */
Takashi Iwaib9030a02012-11-29 10:18:57 +0100252 line_out[cfg->line_outs++] = hp_out[i];
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200253 cfg->hp_outs--;
Takashi Iwaib9030a02012-11-29 10:18:57 +0100254 memmove(hp_out + i, hp_out + i + 1,
255 sizeof(hp_out[0]) * (cfg->hp_outs - i));
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200256 }
Takashi Iwaib9030a02012-11-29 10:18:57 +0100257 memset(hp_out + cfg->hp_outs, 0,
258 sizeof(hp_out[0]) * (AUTO_CFG_MAX_OUTS - cfg->hp_outs));
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200259 if (!cfg->hp_outs)
260 cfg->line_out_type = AUTO_PIN_HP_OUT;
261
262 }
263
264 /* sort by sequence */
Takashi Iwaib9030a02012-11-29 10:18:57 +0100265 sort_pins_by_sequence(cfg->line_out_pins, line_out, cfg->line_outs);
266 sort_pins_by_sequence(cfg->speaker_pins, speaker_out,
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200267 cfg->speaker_outs);
Takashi Iwaib9030a02012-11-29 10:18:57 +0100268 sort_pins_by_sequence(cfg->hp_pins, hp_out, cfg->hp_outs);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200269
270 /*
271 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
272 * as a primary output
273 */
274 if (!cfg->line_outs &&
275 !(cond_flags & HDA_PINCFG_NO_LO_FIXUP)) {
276 if (cfg->speaker_outs) {
277 cfg->line_outs = cfg->speaker_outs;
278 memcpy(cfg->line_out_pins, cfg->speaker_pins,
279 sizeof(cfg->speaker_pins));
280 cfg->speaker_outs = 0;
281 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
282 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
283 } else if (cfg->hp_outs) {
284 cfg->line_outs = cfg->hp_outs;
285 memcpy(cfg->line_out_pins, cfg->hp_pins,
286 sizeof(cfg->hp_pins));
287 cfg->hp_outs = 0;
288 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
289 cfg->line_out_type = AUTO_PIN_HP_OUT;
290 }
291 }
292
293 reorder_outputs(cfg->line_outs, cfg->line_out_pins);
294 reorder_outputs(cfg->hp_outs, cfg->hp_pins);
295 reorder_outputs(cfg->speaker_outs, cfg->speaker_pins);
296
Takashi Iwaib9030a02012-11-29 10:18:57 +0100297 /* sort inputs in the order of AUTO_PIN_* type */
298 sort(cfg->inputs, cfg->num_inputs, sizeof(cfg->inputs[0]),
299 compare_input_type, NULL);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200300
301 /*
302 * debug prints of the parsed results
303 */
304 snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x) type:%s\n",
305 cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
306 cfg->line_out_pins[2], cfg->line_out_pins[3],
307 cfg->line_out_pins[4],
308 cfg->line_out_type == AUTO_PIN_HP_OUT ? "hp" :
309 (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT ?
310 "speaker" : "line"));
311 snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
312 cfg->speaker_outs, cfg->speaker_pins[0],
313 cfg->speaker_pins[1], cfg->speaker_pins[2],
314 cfg->speaker_pins[3], cfg->speaker_pins[4]);
315 snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
316 cfg->hp_outs, cfg->hp_pins[0],
317 cfg->hp_pins[1], cfg->hp_pins[2],
318 cfg->hp_pins[3], cfg->hp_pins[4]);
319 snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
320 if (cfg->dig_outs)
321 snd_printd(" dig-out=0x%x/0x%x\n",
322 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
Takashi Iwai709aea62012-08-07 18:09:23 +0200323 snd_printd(" inputs:\n");
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200324 for (i = 0; i < cfg->num_inputs; i++) {
Takashi Iwai709aea62012-08-07 18:09:23 +0200325 snd_printd(" %s=0x%x\n",
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200326 hda_get_autocfg_input_label(codec, cfg, i),
327 cfg->inputs[i].pin);
328 }
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200329 if (cfg->dig_in_pin)
330 snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
331
332 return 0;
333}
334EXPORT_SYMBOL_HDA(snd_hda_parse_pin_defcfg);
335
336int snd_hda_get_input_pin_attr(unsigned int def_conf)
337{
338 unsigned int loc = get_defcfg_location(def_conf);
339 unsigned int conn = get_defcfg_connect(def_conf);
340 if (conn == AC_JACK_PORT_NONE)
341 return INPUT_PIN_ATTR_UNUSED;
342 /* Windows may claim the internal mic to be BOTH, too */
343 if (conn == AC_JACK_PORT_FIXED || conn == AC_JACK_PORT_BOTH)
344 return INPUT_PIN_ATTR_INT;
345 if ((loc & 0x30) == AC_JACK_LOC_INTERNAL)
346 return INPUT_PIN_ATTR_INT;
347 if ((loc & 0x30) == AC_JACK_LOC_SEPARATE)
348 return INPUT_PIN_ATTR_DOCK;
349 if (loc == AC_JACK_LOC_REAR)
350 return INPUT_PIN_ATTR_REAR;
351 if (loc == AC_JACK_LOC_FRONT)
352 return INPUT_PIN_ATTR_FRONT;
353 return INPUT_PIN_ATTR_NORMAL;
354}
355EXPORT_SYMBOL_HDA(snd_hda_get_input_pin_attr);
356
357/**
358 * hda_get_input_pin_label - Give a label for the given input pin
359 *
360 * When check_location is true, the function checks the pin location
361 * for mic and line-in pins, and set an appropriate prefix like "Front",
362 * "Rear", "Internal".
363 */
364
365static const char *hda_get_input_pin_label(struct hda_codec *codec,
366 hda_nid_t pin, bool check_location)
367{
368 unsigned int def_conf;
369 static const char * const mic_names[] = {
Takashi Iwai5ec16d12012-11-28 18:11:59 +0100370 "Internal Mic", "Dock Mic", "Mic", "Rear Mic", "Front Mic"
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200371 };
372 int attr;
373
374 def_conf = snd_hda_codec_get_pincfg(codec, pin);
375
376 switch (get_defcfg_device(def_conf)) {
377 case AC_JACK_MIC_IN:
378 if (!check_location)
379 return "Mic";
380 attr = snd_hda_get_input_pin_attr(def_conf);
381 if (!attr)
382 return "None";
383 return mic_names[attr - 1];
384 case AC_JACK_LINE_IN:
385 if (!check_location)
386 return "Line";
387 attr = snd_hda_get_input_pin_attr(def_conf);
388 if (!attr)
389 return "None";
390 if (attr == INPUT_PIN_ATTR_DOCK)
391 return "Dock Line";
392 return "Line";
393 case AC_JACK_AUX:
394 return "Aux";
395 case AC_JACK_CD:
396 return "CD";
397 case AC_JACK_SPDIF_IN:
398 return "SPDIF In";
399 case AC_JACK_DIG_OTHER_IN:
400 return "Digital In";
Takashi Iwai54d778b2013-01-09 08:46:34 +0100401 case AC_JACK_HP_OUT:
402 return "Headphone Mic";
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200403 default:
404 return "Misc";
405 }
406}
407
408/* Check whether the location prefix needs to be added to the label.
409 * If all mic-jacks are in the same location (e.g. rear panel), we don't
410 * have to put "Front" prefix to each label. In such a case, returns false.
411 */
412static int check_mic_location_need(struct hda_codec *codec,
413 const struct auto_pin_cfg *cfg,
414 int input)
415{
416 unsigned int defc;
417 int i, attr, attr2;
418
419 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[input].pin);
420 attr = snd_hda_get_input_pin_attr(defc);
421 /* for internal or docking mics, we need locations */
422 if (attr <= INPUT_PIN_ATTR_NORMAL)
423 return 1;
424
425 attr = 0;
426 for (i = 0; i < cfg->num_inputs; i++) {
427 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[i].pin);
428 attr2 = snd_hda_get_input_pin_attr(defc);
429 if (attr2 >= INPUT_PIN_ATTR_NORMAL) {
430 if (attr && attr != attr2)
431 return 1; /* different locations found */
432 attr = attr2;
433 }
434 }
435 return 0;
436}
437
438/**
439 * hda_get_autocfg_input_label - Get a label for the given input
440 *
441 * Get a label for the given input pin defined by the autocfg item.
442 * Unlike hda_get_input_pin_label(), this function checks all inputs
443 * defined in autocfg and avoids the redundant mic/line prefix as much as
444 * possible.
445 */
446const char *hda_get_autocfg_input_label(struct hda_codec *codec,
447 const struct auto_pin_cfg *cfg,
448 int input)
449{
450 int type = cfg->inputs[input].type;
451 int has_multiple_pins = 0;
452
453 if ((input > 0 && cfg->inputs[input - 1].type == type) ||
454 (input < cfg->num_inputs - 1 && cfg->inputs[input + 1].type == type))
455 has_multiple_pins = 1;
456 if (has_multiple_pins && type == AUTO_PIN_MIC)
457 has_multiple_pins &= check_mic_location_need(codec, cfg, input);
458 return hda_get_input_pin_label(codec, cfg->inputs[input].pin,
459 has_multiple_pins);
460}
461EXPORT_SYMBOL_HDA(hda_get_autocfg_input_label);
462
463/* return the position of NID in the list, or -1 if not found */
464static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
465{
466 int i;
467 for (i = 0; i < nums; i++)
468 if (list[i] == nid)
469 return i;
470 return -1;
471}
472
473/* get a unique suffix or an index number */
474static const char *check_output_sfx(hda_nid_t nid, const hda_nid_t *pins,
475 int num_pins, int *indexp)
476{
477 static const char * const channel_sfx[] = {
478 " Front", " Surround", " CLFE", " Side"
479 };
480 int i;
481
482 i = find_idx_in_nid_list(nid, pins, num_pins);
483 if (i < 0)
484 return NULL;
485 if (num_pins == 1)
486 return "";
487 if (num_pins > ARRAY_SIZE(channel_sfx)) {
488 if (indexp)
489 *indexp = i;
490 return "";
491 }
492 return channel_sfx[i];
493}
494
David Henningssoneee3ed42012-10-03 11:12:53 +0200495static const char *check_output_pfx(struct hda_codec *codec, hda_nid_t nid)
496{
497 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
498 int attr = snd_hda_get_input_pin_attr(def_conf);
499
500 /* check the location */
501 switch (attr) {
502 case INPUT_PIN_ATTR_DOCK:
503 return "Dock ";
504 case INPUT_PIN_ATTR_FRONT:
505 return "Front ";
506 }
507 return "";
508}
509
510static int get_hp_label_index(struct hda_codec *codec, hda_nid_t nid,
511 const hda_nid_t *pins, int num_pins)
512{
513 int i, j, idx = 0;
514
515 const char *pfx = check_output_pfx(codec, nid);
516
517 i = find_idx_in_nid_list(nid, pins, num_pins);
518 if (i < 0)
519 return -1;
520 for (j = 0; j < i; j++)
521 if (pfx == check_output_pfx(codec, pins[j]))
522 idx++;
523
524 return idx;
525}
526
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200527static int fill_audio_out_name(struct hda_codec *codec, hda_nid_t nid,
528 const struct auto_pin_cfg *cfg,
529 const char *name, char *label, int maxlen,
530 int *indexp)
531{
532 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
533 int attr = snd_hda_get_input_pin_attr(def_conf);
David Henningssoneee3ed42012-10-03 11:12:53 +0200534 const char *pfx, *sfx = "";
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200535
536 /* handle as a speaker if it's a fixed line-out */
537 if (!strcmp(name, "Line Out") && attr == INPUT_PIN_ATTR_INT)
538 name = "Speaker";
David Henningssoneee3ed42012-10-03 11:12:53 +0200539 pfx = check_output_pfx(codec, nid);
540
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200541 if (cfg) {
542 /* try to give a unique suffix if needed */
543 sfx = check_output_sfx(nid, cfg->line_out_pins, cfg->line_outs,
544 indexp);
545 if (!sfx)
546 sfx = check_output_sfx(nid, cfg->speaker_pins, cfg->speaker_outs,
547 indexp);
548 if (!sfx) {
549 /* don't add channel suffix for Headphone controls */
David Henningssoneee3ed42012-10-03 11:12:53 +0200550 int idx = get_hp_label_index(codec, nid, cfg->hp_pins,
551 cfg->hp_outs);
Takashi Iwai128bc4b2012-05-07 17:42:31 +0200552 if (idx >= 0)
553 *indexp = idx;
554 sfx = "";
555 }
556 }
557 snprintf(label, maxlen, "%s%s%s", pfx, name, sfx);
558 return 1;
559}
560
561/**
562 * snd_hda_get_pin_label - Get a label for the given I/O pin
563 *
564 * Get a label for the given pin. This function works for both input and
565 * output pins. When @cfg is given as non-NULL, the function tries to get
566 * an optimized label using hda_get_autocfg_input_label().
567 *
568 * This function tries to give a unique label string for the pin as much as
569 * possible. For example, when the multiple line-outs are present, it adds
570 * the channel suffix like "Front", "Surround", etc (only when @cfg is given).
571 * If no unique name with a suffix is available and @indexp is non-NULL, the
572 * index number is stored in the pointer.
573 */
574int snd_hda_get_pin_label(struct hda_codec *codec, hda_nid_t nid,
575 const struct auto_pin_cfg *cfg,
576 char *label, int maxlen, int *indexp)
577{
578 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
579 const char *name = NULL;
580 int i;
581
582 if (indexp)
583 *indexp = 0;
584 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
585 return 0;
586
587 switch (get_defcfg_device(def_conf)) {
588 case AC_JACK_LINE_OUT:
589 return fill_audio_out_name(codec, nid, cfg, "Line Out",
590 label, maxlen, indexp);
591 case AC_JACK_SPEAKER:
592 return fill_audio_out_name(codec, nid, cfg, "Speaker",
593 label, maxlen, indexp);
594 case AC_JACK_HP_OUT:
595 return fill_audio_out_name(codec, nid, cfg, "Headphone",
596 label, maxlen, indexp);
597 case AC_JACK_SPDIF_OUT:
598 case AC_JACK_DIG_OTHER_OUT:
599 if (get_defcfg_location(def_conf) == AC_JACK_LOC_HDMI)
600 name = "HDMI";
601 else
602 name = "SPDIF";
603 if (cfg && indexp) {
604 i = find_idx_in_nid_list(nid, cfg->dig_out_pins,
605 cfg->dig_outs);
606 if (i >= 0)
607 *indexp = i;
608 }
609 break;
610 default:
611 if (cfg) {
612 for (i = 0; i < cfg->num_inputs; i++) {
613 if (cfg->inputs[i].pin != nid)
614 continue;
615 name = hda_get_autocfg_input_label(codec, cfg, i);
616 if (name)
617 break;
618 }
619 }
620 if (!name)
621 name = hda_get_input_pin_label(codec, nid, true);
622 break;
623 }
624 if (!name)
625 return 0;
626 strlcpy(label, name, maxlen);
627 return 1;
628}
629EXPORT_SYMBOL_HDA(snd_hda_get_pin_label);
630
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100631int snd_hda_add_verbs(struct hda_codec *codec,
632 const struct hda_verb *list)
Takashi Iwai23d30f22012-05-07 17:17:32 +0200633{
634 const struct hda_verb **v;
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100635 v = snd_array_new(&codec->verbs);
Takashi Iwai23d30f22012-05-07 17:17:32 +0200636 if (!v)
637 return -ENOMEM;
638 *v = list;
639 return 0;
640}
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100641EXPORT_SYMBOL_HDA(snd_hda_add_verbs);
Takashi Iwai23d30f22012-05-07 17:17:32 +0200642
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100643void snd_hda_apply_verbs(struct hda_codec *codec)
Takashi Iwai23d30f22012-05-07 17:17:32 +0200644{
Takashi Iwai23d30f22012-05-07 17:17:32 +0200645 int i;
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100646 for (i = 0; i < codec->verbs.used; i++) {
647 struct hda_verb **v = snd_array_elem(&codec->verbs, i);
Takashi Iwai23d30f22012-05-07 17:17:32 +0200648 snd_hda_sequence_write(codec, *v);
649 }
650}
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100651EXPORT_SYMBOL_HDA(snd_hda_apply_verbs);
Takashi Iwai23d30f22012-05-07 17:17:32 +0200652
653void snd_hda_apply_pincfgs(struct hda_codec *codec,
654 const struct hda_pintbl *cfg)
655{
656 for (; cfg->nid; cfg++)
657 snd_hda_codec_set_pincfg(codec, cfg->nid, cfg->val);
658}
659EXPORT_SYMBOL_HDA(snd_hda_apply_pincfgs);
660
Takashi Iwaifd108212013-01-10 10:18:14 +0100661static void set_pin_targets(struct hda_codec *codec,
662 const struct hda_pintbl *cfg)
663{
664 for (; cfg->nid; cfg++)
665 snd_hda_set_pin_ctl_cache(codec, cfg->nid, cfg->val);
666}
667
Takashi Iwai23d30f22012-05-07 17:17:32 +0200668void snd_hda_apply_fixup(struct hda_codec *codec, int action)
669{
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100670 int id = codec->fixup_id;
Takashi Iwai23d30f22012-05-07 17:17:32 +0200671#ifdef CONFIG_SND_DEBUG_VERBOSE
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100672 const char *modelname = codec->fixup_name;
Takashi Iwai23d30f22012-05-07 17:17:32 +0200673#endif
674 int depth = 0;
675
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100676 if (!codec->fixup_list)
Takashi Iwai23d30f22012-05-07 17:17:32 +0200677 return;
678
679 while (id >= 0) {
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100680 const struct hda_fixup *fix = codec->fixup_list + id;
Takashi Iwai23d30f22012-05-07 17:17:32 +0200681
682 switch (fix->type) {
683 case HDA_FIXUP_PINS:
684 if (action != HDA_FIXUP_ACT_PRE_PROBE || !fix->v.pins)
685 break;
686 snd_printdd(KERN_INFO SFX
687 "%s: Apply pincfg for %s\n",
688 codec->chip_name, modelname);
689 snd_hda_apply_pincfgs(codec, fix->v.pins);
690 break;
691 case HDA_FIXUP_VERBS:
692 if (action != HDA_FIXUP_ACT_PROBE || !fix->v.verbs)
693 break;
694 snd_printdd(KERN_INFO SFX
695 "%s: Apply fix-verbs for %s\n",
696 codec->chip_name, modelname);
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100697 snd_hda_add_verbs(codec, fix->v.verbs);
Takashi Iwai23d30f22012-05-07 17:17:32 +0200698 break;
699 case HDA_FIXUP_FUNC:
700 if (!fix->v.func)
701 break;
702 snd_printdd(KERN_INFO SFX
703 "%s: Apply fix-func for %s\n",
704 codec->chip_name, modelname);
705 fix->v.func(codec, fix, action);
706 break;
Takashi Iwaifd108212013-01-10 10:18:14 +0100707 case HDA_FIXUP_PINCTLS:
708 if (action != HDA_FIXUP_ACT_PROBE || !fix->v.pins)
709 break;
710 snd_printdd(KERN_INFO SFX
711 "%s: Apply pinctl for %s\n",
712 codec->chip_name, modelname);
713 set_pin_targets(codec, fix->v.pins);
714 break;
Takashi Iwai23d30f22012-05-07 17:17:32 +0200715 default:
716 snd_printk(KERN_ERR SFX
717 "%s: Invalid fixup type %d\n",
718 codec->chip_name, fix->type);
719 break;
720 }
721 if (!fix->chained)
722 break;
723 if (++depth > 10)
724 break;
725 id = fix->chain_id;
726 }
727}
728EXPORT_SYMBOL_HDA(snd_hda_apply_fixup);
729
730void snd_hda_pick_fixup(struct hda_codec *codec,
731 const struct hda_model_fixup *models,
732 const struct snd_pci_quirk *quirk,
733 const struct hda_fixup *fixlist)
734{
Takashi Iwai23d30f22012-05-07 17:17:32 +0200735 const struct snd_pci_quirk *q;
736 int id = -1;
737 const char *name = NULL;
738
739 /* when model=nofixup is given, don't pick up any fixups */
740 if (codec->modelname && !strcmp(codec->modelname, "nofixup")) {
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100741 codec->fixup_list = NULL;
742 codec->fixup_id = -1;
Takashi Iwai23d30f22012-05-07 17:17:32 +0200743 return;
744 }
745
746 if (codec->modelname && models) {
747 while (models->name) {
748 if (!strcmp(codec->modelname, models->name)) {
749 id = models->id;
750 name = models->name;
751 break;
752 }
753 models++;
754 }
755 }
David Henningsson639aa4b2012-07-18 18:02:53 +0200756 if (id < 0 && quirk) {
Takashi Iwai23d30f22012-05-07 17:17:32 +0200757 q = snd_pci_quirk_lookup(codec->bus->pci, quirk);
758 if (q) {
759 id = q->value;
760#ifdef CONFIG_SND_DEBUG_VERBOSE
761 name = q->name;
762#endif
763 }
764 }
David Henningsson639aa4b2012-07-18 18:02:53 +0200765 if (id < 0 && quirk) {
Takashi Iwai23d30f22012-05-07 17:17:32 +0200766 for (q = quirk; q->subvendor; q++) {
767 unsigned int vendorid =
768 q->subdevice | (q->subvendor << 16);
Takashi Iwaia33b7b02012-09-11 16:42:18 +0200769 unsigned int mask = 0xffff0000 | q->subdevice_mask;
770 if ((codec->subsystem_id & mask) == (vendorid & mask)) {
Takashi Iwai23d30f22012-05-07 17:17:32 +0200771 id = q->value;
772#ifdef CONFIG_SND_DEBUG_VERBOSE
773 name = q->name;
774#endif
775 break;
776 }
777 }
778 }
779
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100780 codec->fixup_id = id;
Takashi Iwai23d30f22012-05-07 17:17:32 +0200781 if (id >= 0) {
Takashi Iwaic9ce6b22012-12-18 18:12:44 +0100782 codec->fixup_list = fixlist;
783 codec->fixup_name = name;
Takashi Iwai23d30f22012-05-07 17:17:32 +0200784 }
785}
786EXPORT_SYMBOL_HDA(snd_hda_pick_fixup);