hda_auto_parser.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * BIOS auto-parser helper functions for HD-audio
  4. *
  5. * Copyright (c) 2012 Takashi Iwai <tiwai@suse.de>
  6. */
  7. #include <linux/slab.h>
  8. #include <linux/export.h>
  9. #include <linux/sort.h>
  10. #include <sound/core.h>
  11. #include <sound/hda_codec.h>
  12. #include "hda_local.h"
  13. #include "hda_auto_parser.h"
  14. /*
  15. * Helper for automatic pin configuration
  16. */
  17. static int is_in_nid_list(hda_nid_t nid, const hda_nid_t *list)
  18. {
  19. for (; *list; list++)
  20. if (*list == nid)
  21. return 1;
  22. return 0;
  23. }
  24. /* a pair of input pin and its sequence */
  25. struct auto_out_pin {
  26. hda_nid_t pin;
  27. short seq;
  28. };
  29. static int compare_seq(const void *ap, const void *bp)
  30. {
  31. const struct auto_out_pin *a = ap;
  32. const struct auto_out_pin *b = bp;
  33. return (int)(a->seq - b->seq);
  34. }
  35. /*
  36. * Sort an associated group of pins according to their sequence numbers.
  37. * then store it to a pin array.
  38. */
  39. static void sort_pins_by_sequence(hda_nid_t *pins, struct auto_out_pin *list,
  40. int num_pins)
  41. {
  42. int i;
  43. sort(list, num_pins, sizeof(list[0]), compare_seq, NULL);
  44. for (i = 0; i < num_pins; i++)
  45. pins[i] = list[i].pin;
  46. }
  47. /* add the found input-pin to the cfg->inputs[] table */
  48. static void add_auto_cfg_input_pin(struct hda_codec *codec, struct auto_pin_cfg *cfg,
  49. hda_nid_t nid, int type)
  50. {
  51. if (cfg->num_inputs < AUTO_CFG_MAX_INS) {
  52. cfg->inputs[cfg->num_inputs].pin = nid;
  53. cfg->inputs[cfg->num_inputs].type = type;
  54. cfg->inputs[cfg->num_inputs].has_boost_on_pin =
  55. nid_has_volume(codec, nid, HDA_INPUT);
  56. cfg->num_inputs++;
  57. }
  58. }
  59. static int compare_input_type(const void *ap, const void *bp)
  60. {
  61. const struct auto_pin_cfg_item *a = ap;
  62. const struct auto_pin_cfg_item *b = bp;
  63. if (a->type != b->type)
  64. return (int)(a->type - b->type);
  65. /* If has both hs_mic and hp_mic, pick the hs_mic ahead of hp_mic. */
  66. if (a->is_headset_mic && b->is_headphone_mic)
  67. return -1; /* don't swap */
  68. else if (a->is_headphone_mic && b->is_headset_mic)
  69. return 1; /* swap */
  70. /* In case one has boost and the other one has not,
  71. pick the one with boost first. */
  72. if (a->has_boost_on_pin != b->has_boost_on_pin)
  73. return (int)(b->has_boost_on_pin - a->has_boost_on_pin);
  74. /* Keep the original order */
  75. return a->order - b->order;
  76. }
  77. /* Reorder the surround channels
  78. * ALSA sequence is front/surr/clfe/side
  79. * HDA sequence is:
  80. * 4-ch: front/surr => OK as it is
  81. * 6-ch: front/clfe/surr
  82. * 8-ch: front/clfe/rear/side|fc
  83. */
  84. static void reorder_outputs(unsigned int nums, hda_nid_t *pins)
  85. {
  86. switch (nums) {
  87. case 3:
  88. case 4:
  89. swap(pins[1], pins[2]);
  90. break;
  91. }
  92. }
  93. /* check whether the given pin has a proper pin I/O capability bit */
  94. static bool check_pincap_validity(struct hda_codec *codec, hda_nid_t pin,
  95. unsigned int dev)
  96. {
  97. unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
  98. /* some old hardware don't return the proper pincaps */
  99. if (!pincap)
  100. return true;
  101. switch (dev) {
  102. case AC_JACK_LINE_OUT:
  103. case AC_JACK_SPEAKER:
  104. case AC_JACK_HP_OUT:
  105. case AC_JACK_SPDIF_OUT:
  106. case AC_JACK_DIG_OTHER_OUT:
  107. return !!(pincap & AC_PINCAP_OUT);
  108. default:
  109. return !!(pincap & AC_PINCAP_IN);
  110. }
  111. }
  112. static bool can_be_headset_mic(struct hda_codec *codec,
  113. struct auto_pin_cfg_item *item,
  114. int seq_number)
  115. {
  116. int attr;
  117. unsigned int def_conf;
  118. if (item->type != AUTO_PIN_MIC)
  119. return false;
  120. if (item->is_headset_mic || item->is_headphone_mic)
  121. return false; /* Already assigned */
  122. def_conf = snd_hda_codec_get_pincfg(codec, item->pin);
  123. attr = snd_hda_get_input_pin_attr(def_conf);
  124. if (attr <= INPUT_PIN_ATTR_DOCK)
  125. return false;
  126. if (seq_number >= 0) {
  127. int seq = get_defcfg_sequence(def_conf);
  128. if (seq != seq_number)
  129. return false;
  130. }
  131. return true;
  132. }
  133. /*
  134. * Parse all pin widgets and store the useful pin nids to cfg
  135. *
  136. * The number of line-outs or any primary output is stored in line_outs,
  137. * and the corresponding output pins are assigned to line_out_pins[],
  138. * in the order of front, rear, CLFE, side, ...
  139. *
  140. * If more extra outputs (speaker and headphone) are found, the pins are
  141. * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
  142. * is detected, one of speaker of HP pins is assigned as the primary
  143. * output, i.e. to line_out_pins[0]. So, line_outs is always positive
  144. * if any analog output exists.
  145. *
  146. * The analog input pins are assigned to inputs array.
  147. * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
  148. * respectively.
  149. */
  150. int snd_hda_parse_pin_defcfg(struct hda_codec *codec,
  151. struct auto_pin_cfg *cfg,
  152. const hda_nid_t *ignore_nids,
  153. unsigned int cond_flags)
  154. {
  155. hda_nid_t nid;
  156. short seq, assoc_line_out;
  157. struct auto_out_pin line_out[ARRAY_SIZE(cfg->line_out_pins)];
  158. struct auto_out_pin speaker_out[ARRAY_SIZE(cfg->speaker_pins)];
  159. struct auto_out_pin hp_out[ARRAY_SIZE(cfg->hp_pins)];
  160. int i;
  161. if (!snd_hda_get_int_hint(codec, "parser_flags", &i))
  162. cond_flags = i;
  163. memset(cfg, 0, sizeof(*cfg));
  164. memset(line_out, 0, sizeof(line_out));
  165. memset(speaker_out, 0, sizeof(speaker_out));
  166. memset(hp_out, 0, sizeof(hp_out));
  167. assoc_line_out = 0;
  168. for_each_hda_codec_node(nid, codec) {
  169. unsigned int wid_caps = get_wcaps(codec, nid);
  170. unsigned int wid_type = get_wcaps_type(wid_caps);
  171. unsigned int def_conf;
  172. short assoc, loc, conn, dev;
  173. /* read all default configuration for pin complex */
  174. if (wid_type != AC_WID_PIN)
  175. continue;
  176. /* ignore the given nids (e.g. pc-beep returns error) */
  177. if (ignore_nids && is_in_nid_list(nid, ignore_nids))
  178. continue;
  179. def_conf = snd_hda_codec_get_pincfg(codec, nid);
  180. conn = get_defcfg_connect(def_conf);
  181. if (conn == AC_JACK_PORT_NONE)
  182. continue;
  183. loc = get_defcfg_location(def_conf);
  184. dev = get_defcfg_device(def_conf);
  185. /* workaround for buggy BIOS setups */
  186. if (dev == AC_JACK_LINE_OUT) {
  187. if (conn == AC_JACK_PORT_FIXED ||
  188. conn == AC_JACK_PORT_BOTH)
  189. dev = AC_JACK_SPEAKER;
  190. }
  191. if (!check_pincap_validity(codec, nid, dev))
  192. continue;
  193. switch (dev) {
  194. case AC_JACK_LINE_OUT:
  195. seq = get_defcfg_sequence(def_conf);
  196. assoc = get_defcfg_association(def_conf);
  197. if (!(wid_caps & AC_WCAP_STEREO))
  198. if (!cfg->mono_out_pin)
  199. cfg->mono_out_pin = nid;
  200. if (!assoc)
  201. continue;
  202. if (!assoc_line_out)
  203. assoc_line_out = assoc;
  204. else if (assoc_line_out != assoc) {
  205. codec_info(codec,
  206. "ignore pin 0x%x with mismatching assoc# 0x%x vs 0x%x\n",
  207. nid, assoc, assoc_line_out);
  208. continue;
  209. }
  210. if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins)) {
  211. codec_info(codec,
  212. "ignore pin 0x%x, too many assigned pins\n",
  213. nid);
  214. continue;
  215. }
  216. line_out[cfg->line_outs].pin = nid;
  217. line_out[cfg->line_outs].seq = seq;
  218. cfg->line_outs++;
  219. break;
  220. case AC_JACK_SPEAKER:
  221. seq = get_defcfg_sequence(def_conf);
  222. assoc = get_defcfg_association(def_conf);
  223. if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins)) {
  224. codec_info(codec,
  225. "ignore pin 0x%x, too many assigned pins\n",
  226. nid);
  227. continue;
  228. }
  229. speaker_out[cfg->speaker_outs].pin = nid;
  230. speaker_out[cfg->speaker_outs].seq = (assoc << 4) | seq;
  231. cfg->speaker_outs++;
  232. break;
  233. case AC_JACK_HP_OUT:
  234. seq = get_defcfg_sequence(def_conf);
  235. assoc = get_defcfg_association(def_conf);
  236. if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins)) {
  237. codec_info(codec,
  238. "ignore pin 0x%x, too many assigned pins\n",
  239. nid);
  240. continue;
  241. }
  242. hp_out[cfg->hp_outs].pin = nid;
  243. hp_out[cfg->hp_outs].seq = (assoc << 4) | seq;
  244. cfg->hp_outs++;
  245. break;
  246. case AC_JACK_MIC_IN:
  247. add_auto_cfg_input_pin(codec, cfg, nid, AUTO_PIN_MIC);
  248. break;
  249. case AC_JACK_LINE_IN:
  250. add_auto_cfg_input_pin(codec, cfg, nid, AUTO_PIN_LINE_IN);
  251. break;
  252. case AC_JACK_CD:
  253. add_auto_cfg_input_pin(codec, cfg, nid, AUTO_PIN_CD);
  254. break;
  255. case AC_JACK_AUX:
  256. add_auto_cfg_input_pin(codec, cfg, nid, AUTO_PIN_AUX);
  257. break;
  258. case AC_JACK_SPDIF_OUT:
  259. case AC_JACK_DIG_OTHER_OUT:
  260. if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins)) {
  261. codec_info(codec,
  262. "ignore pin 0x%x, too many assigned pins\n",
  263. nid);
  264. continue;
  265. }
  266. cfg->dig_out_pins[cfg->dig_outs] = nid;
  267. cfg->dig_out_type[cfg->dig_outs] =
  268. (loc == AC_JACK_LOC_HDMI) ?
  269. HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
  270. cfg->dig_outs++;
  271. break;
  272. case AC_JACK_SPDIF_IN:
  273. case AC_JACK_DIG_OTHER_IN:
  274. cfg->dig_in_pin = nid;
  275. if (loc == AC_JACK_LOC_HDMI)
  276. cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
  277. else
  278. cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
  279. break;
  280. }
  281. }
  282. /* Find a pin that could be a headset or headphone mic */
  283. if (cond_flags & HDA_PINCFG_HEADSET_MIC || cond_flags & HDA_PINCFG_HEADPHONE_MIC) {
  284. bool hsmic = !!(cond_flags & HDA_PINCFG_HEADSET_MIC);
  285. bool hpmic = !!(cond_flags & HDA_PINCFG_HEADPHONE_MIC);
  286. for (i = 0; (hsmic || hpmic) && (i < cfg->num_inputs); i++)
  287. if (hsmic && can_be_headset_mic(codec, &cfg->inputs[i], 0xc)) {
  288. cfg->inputs[i].is_headset_mic = 1;
  289. hsmic = false;
  290. } else if (hpmic && can_be_headset_mic(codec, &cfg->inputs[i], 0xd)) {
  291. cfg->inputs[i].is_headphone_mic = 1;
  292. hpmic = false;
  293. }
  294. /* If we didn't find our sequence number mark, fall back to any sequence number */
  295. for (i = 0; (hsmic || hpmic) && (i < cfg->num_inputs); i++) {
  296. if (!can_be_headset_mic(codec, &cfg->inputs[i], -1))
  297. continue;
  298. if (hsmic) {
  299. cfg->inputs[i].is_headset_mic = 1;
  300. hsmic = false;
  301. } else if (hpmic) {
  302. cfg->inputs[i].is_headphone_mic = 1;
  303. hpmic = false;
  304. }
  305. }
  306. if (hsmic)
  307. codec_dbg(codec, "Told to look for a headset mic, but didn't find any.\n");
  308. if (hpmic)
  309. codec_dbg(codec, "Told to look for a headphone mic, but didn't find any.\n");
  310. }
  311. /* FIX-UP:
  312. * If no line-out is defined but multiple HPs are found,
  313. * some of them might be the real line-outs.
  314. */
  315. if (!cfg->line_outs && cfg->hp_outs > 1 &&
  316. !(cond_flags & HDA_PINCFG_NO_HP_FIXUP)) {
  317. i = 0;
  318. while (i < cfg->hp_outs) {
  319. /* The real HPs should have the sequence 0x0f */
  320. if ((hp_out[i].seq & 0x0f) == 0x0f) {
  321. i++;
  322. continue;
  323. }
  324. /* Move it to the line-out table */
  325. line_out[cfg->line_outs++] = hp_out[i];
  326. cfg->hp_outs--;
  327. memmove(hp_out + i, hp_out + i + 1,
  328. sizeof(hp_out[0]) * (cfg->hp_outs - i));
  329. }
  330. memset(hp_out + cfg->hp_outs, 0,
  331. sizeof(hp_out[0]) * (AUTO_CFG_MAX_OUTS - cfg->hp_outs));
  332. if (!cfg->hp_outs)
  333. cfg->line_out_type = AUTO_PIN_HP_OUT;
  334. }
  335. /* sort by sequence */
  336. sort_pins_by_sequence(cfg->line_out_pins, line_out, cfg->line_outs);
  337. sort_pins_by_sequence(cfg->speaker_pins, speaker_out,
  338. cfg->speaker_outs);
  339. sort_pins_by_sequence(cfg->hp_pins, hp_out, cfg->hp_outs);
  340. /*
  341. * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
  342. * as a primary output
  343. */
  344. if (!cfg->line_outs &&
  345. !(cond_flags & HDA_PINCFG_NO_LO_FIXUP)) {
  346. if (cfg->speaker_outs) {
  347. cfg->line_outs = cfg->speaker_outs;
  348. memcpy(cfg->line_out_pins, cfg->speaker_pins,
  349. sizeof(cfg->speaker_pins));
  350. cfg->speaker_outs = 0;
  351. memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
  352. cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
  353. } else if (cfg->hp_outs) {
  354. cfg->line_outs = cfg->hp_outs;
  355. memcpy(cfg->line_out_pins, cfg->hp_pins,
  356. sizeof(cfg->hp_pins));
  357. cfg->hp_outs = 0;
  358. memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
  359. cfg->line_out_type = AUTO_PIN_HP_OUT;
  360. }
  361. }
  362. reorder_outputs(cfg->line_outs, cfg->line_out_pins);
  363. reorder_outputs(cfg->hp_outs, cfg->hp_pins);
  364. reorder_outputs(cfg->speaker_outs, cfg->speaker_pins);
  365. /* sort inputs in the order of AUTO_PIN_* type */
  366. for (i = 0; i < cfg->num_inputs; i++)
  367. cfg->inputs[i].order = i;
  368. sort(cfg->inputs, cfg->num_inputs, sizeof(cfg->inputs[0]),
  369. compare_input_type, NULL);
  370. /*
  371. * debug prints of the parsed results
  372. */
  373. codec_info(codec, "autoconfig for %s: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x) type:%s\n",
  374. codec->core.chip_name, cfg->line_outs, cfg->line_out_pins[0],
  375. cfg->line_out_pins[1], cfg->line_out_pins[2],
  376. cfg->line_out_pins[3], cfg->line_out_pins[4],
  377. cfg->line_out_type == AUTO_PIN_HP_OUT ? "hp" :
  378. (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT ?
  379. "speaker" : "line"));
  380. codec_info(codec, " speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
  381. cfg->speaker_outs, cfg->speaker_pins[0],
  382. cfg->speaker_pins[1], cfg->speaker_pins[2],
  383. cfg->speaker_pins[3], cfg->speaker_pins[4]);
  384. codec_info(codec, " hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
  385. cfg->hp_outs, cfg->hp_pins[0],
  386. cfg->hp_pins[1], cfg->hp_pins[2],
  387. cfg->hp_pins[3], cfg->hp_pins[4]);
  388. codec_info(codec, " mono: mono_out=0x%x\n", cfg->mono_out_pin);
  389. if (cfg->dig_outs)
  390. codec_info(codec, " dig-out=0x%x/0x%x\n",
  391. cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
  392. codec_info(codec, " inputs:\n");
  393. for (i = 0; i < cfg->num_inputs; i++) {
  394. codec_info(codec, " %s=0x%x\n",
  395. hda_get_autocfg_input_label(codec, cfg, i),
  396. cfg->inputs[i].pin);
  397. }
  398. if (cfg->dig_in_pin)
  399. codec_info(codec, " dig-in=0x%x\n", cfg->dig_in_pin);
  400. return 0;
  401. }
  402. EXPORT_SYMBOL_GPL(snd_hda_parse_pin_defcfg);
  403. /**
  404. * snd_hda_get_input_pin_attr - Get the input pin attribute from pin config
  405. * @def_conf: pin configuration value
  406. *
  407. * Guess the input pin attribute (INPUT_PIN_ATTR_XXX) from the given
  408. * default pin configuration value.
  409. */
  410. int snd_hda_get_input_pin_attr(unsigned int def_conf)
  411. {
  412. unsigned int loc = get_defcfg_location(def_conf);
  413. unsigned int conn = get_defcfg_connect(def_conf);
  414. if (conn == AC_JACK_PORT_NONE)
  415. return INPUT_PIN_ATTR_UNUSED;
  416. /* Windows may claim the internal mic to be BOTH, too */
  417. if (conn == AC_JACK_PORT_FIXED || conn == AC_JACK_PORT_BOTH)
  418. return INPUT_PIN_ATTR_INT;
  419. if ((loc & 0x30) == AC_JACK_LOC_INTERNAL)
  420. return INPUT_PIN_ATTR_INT;
  421. if ((loc & 0x30) == AC_JACK_LOC_SEPARATE)
  422. return INPUT_PIN_ATTR_DOCK;
  423. if (loc == AC_JACK_LOC_REAR)
  424. return INPUT_PIN_ATTR_REAR;
  425. if (loc == AC_JACK_LOC_FRONT)
  426. return INPUT_PIN_ATTR_FRONT;
  427. return INPUT_PIN_ATTR_NORMAL;
  428. }
  429. EXPORT_SYMBOL_GPL(snd_hda_get_input_pin_attr);
  430. /**
  431. * hda_get_input_pin_label - Give a label for the given input pin
  432. * @codec: the HDA codec
  433. * @item: ping config item to refer
  434. * @pin: the pin NID
  435. * @check_location: flag to add the jack location prefix
  436. *
  437. * When @check_location is true, the function checks the pin location
  438. * for mic and line-in pins, and set an appropriate prefix like "Front",
  439. * "Rear", "Internal".
  440. */
  441. static const char *hda_get_input_pin_label(struct hda_codec *codec,
  442. const struct auto_pin_cfg_item *item,
  443. hda_nid_t pin, bool check_location)
  444. {
  445. unsigned int def_conf;
  446. static const char * const mic_names[] = {
  447. "Internal Mic", "Dock Mic", "Mic", "Rear Mic", "Front Mic"
  448. };
  449. int attr;
  450. def_conf = snd_hda_codec_get_pincfg(codec, pin);
  451. switch (get_defcfg_device(def_conf)) {
  452. case AC_JACK_MIC_IN:
  453. if (item && item->is_headset_mic)
  454. return "Headset Mic";
  455. if (item && item->is_headphone_mic)
  456. return "Headphone Mic";
  457. if (!check_location)
  458. return "Mic";
  459. attr = snd_hda_get_input_pin_attr(def_conf);
  460. if (!attr)
  461. return "None";
  462. return mic_names[attr - 1];
  463. case AC_JACK_LINE_IN:
  464. if (!check_location)
  465. return "Line";
  466. attr = snd_hda_get_input_pin_attr(def_conf);
  467. if (!attr)
  468. return "None";
  469. if (attr == INPUT_PIN_ATTR_DOCK)
  470. return "Dock Line";
  471. return "Line";
  472. case AC_JACK_AUX:
  473. return "Aux";
  474. case AC_JACK_CD:
  475. return "CD";
  476. case AC_JACK_SPDIF_IN:
  477. return "SPDIF In";
  478. case AC_JACK_DIG_OTHER_IN:
  479. return "Digital In";
  480. case AC_JACK_HP_OUT:
  481. return "Headphone Mic";
  482. default:
  483. return "Misc";
  484. }
  485. }
  486. /* Check whether the location prefix needs to be added to the label.
  487. * If all mic-jacks are in the same location (e.g. rear panel), we don't
  488. * have to put "Front" prefix to each label. In such a case, returns false.
  489. */
  490. static int check_mic_location_need(struct hda_codec *codec,
  491. const struct auto_pin_cfg *cfg,
  492. int input)
  493. {
  494. unsigned int defc;
  495. int i, attr, attr2;
  496. defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[input].pin);
  497. attr = snd_hda_get_input_pin_attr(defc);
  498. /* for internal or docking mics, we need locations */
  499. if (attr <= INPUT_PIN_ATTR_NORMAL)
  500. return 1;
  501. attr = 0;
  502. for (i = 0; i < cfg->num_inputs; i++) {
  503. defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[i].pin);
  504. attr2 = snd_hda_get_input_pin_attr(defc);
  505. if (attr2 >= INPUT_PIN_ATTR_NORMAL) {
  506. if (attr && attr != attr2)
  507. return 1; /* different locations found */
  508. attr = attr2;
  509. }
  510. }
  511. return 0;
  512. }
  513. /**
  514. * hda_get_autocfg_input_label - Get a label for the given input
  515. * @codec: the HDA codec
  516. * @cfg: the parsed pin configuration
  517. * @input: the input index number
  518. *
  519. * Get a label for the given input pin defined by the autocfg item.
  520. * Unlike hda_get_input_pin_label(), this function checks all inputs
  521. * defined in autocfg and avoids the redundant mic/line prefix as much as
  522. * possible.
  523. */
  524. const char *hda_get_autocfg_input_label(struct hda_codec *codec,
  525. const struct auto_pin_cfg *cfg,
  526. int input)
  527. {
  528. int type = cfg->inputs[input].type;
  529. int has_multiple_pins = 0;
  530. if ((input > 0 && cfg->inputs[input - 1].type == type) ||
  531. (input < cfg->num_inputs - 1 && cfg->inputs[input + 1].type == type))
  532. has_multiple_pins = 1;
  533. if (has_multiple_pins && type == AUTO_PIN_MIC)
  534. has_multiple_pins &= check_mic_location_need(codec, cfg, input);
  535. has_multiple_pins |= codec->force_pin_prefix;
  536. return hda_get_input_pin_label(codec, &cfg->inputs[input],
  537. cfg->inputs[input].pin,
  538. has_multiple_pins);
  539. }
  540. EXPORT_SYMBOL_GPL(hda_get_autocfg_input_label);
  541. /* return the position of NID in the list, or -1 if not found */
  542. static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
  543. {
  544. int i;
  545. for (i = 0; i < nums; i++)
  546. if (list[i] == nid)
  547. return i;
  548. return -1;
  549. }
  550. /* get a unique suffix or an index number */
  551. static const char *check_output_sfx(hda_nid_t nid, const hda_nid_t *pins,
  552. int num_pins, int *indexp)
  553. {
  554. static const char * const channel_sfx[] = {
  555. " Front", " Surround", " CLFE", " Side"
  556. };
  557. int i;
  558. i = find_idx_in_nid_list(nid, pins, num_pins);
  559. if (i < 0)
  560. return NULL;
  561. if (num_pins == 1)
  562. return "";
  563. if (num_pins > ARRAY_SIZE(channel_sfx)) {
  564. if (indexp)
  565. *indexp = i;
  566. return "";
  567. }
  568. return channel_sfx[i];
  569. }
  570. static const char *check_output_pfx(struct hda_codec *codec, hda_nid_t nid)
  571. {
  572. unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
  573. int attr = snd_hda_get_input_pin_attr(def_conf);
  574. /* check the location */
  575. switch (attr) {
  576. case INPUT_PIN_ATTR_DOCK:
  577. return "Dock ";
  578. case INPUT_PIN_ATTR_FRONT:
  579. return "Front ";
  580. }
  581. return "";
  582. }
  583. static int get_hp_label_index(struct hda_codec *codec, hda_nid_t nid,
  584. const hda_nid_t *pins, int num_pins)
  585. {
  586. int i, j, idx = 0;
  587. const char *pfx = check_output_pfx(codec, nid);
  588. i = find_idx_in_nid_list(nid, pins, num_pins);
  589. if (i < 0)
  590. return -1;
  591. for (j = 0; j < i; j++)
  592. if (pfx == check_output_pfx(codec, pins[j]))
  593. idx++;
  594. return idx;
  595. }
  596. static int fill_audio_out_name(struct hda_codec *codec, hda_nid_t nid,
  597. const struct auto_pin_cfg *cfg,
  598. const char *name, char *label, int maxlen,
  599. int *indexp)
  600. {
  601. unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
  602. int attr = snd_hda_get_input_pin_attr(def_conf);
  603. const char *pfx, *sfx = "";
  604. /* handle as a speaker if it's a fixed line-out */
  605. if (!strcmp(name, "Line Out") && attr == INPUT_PIN_ATTR_INT)
  606. name = "Speaker";
  607. pfx = check_output_pfx(codec, nid);
  608. if (cfg) {
  609. /* try to give a unique suffix if needed */
  610. sfx = check_output_sfx(nid, cfg->line_out_pins, cfg->line_outs,
  611. indexp);
  612. if (!sfx)
  613. sfx = check_output_sfx(nid, cfg->speaker_pins, cfg->speaker_outs,
  614. indexp);
  615. if (!sfx) {
  616. /* don't add channel suffix for Headphone controls */
  617. int idx = get_hp_label_index(codec, nid, cfg->hp_pins,
  618. cfg->hp_outs);
  619. if (idx >= 0 && indexp)
  620. *indexp = idx;
  621. sfx = "";
  622. }
  623. }
  624. snprintf(label, maxlen, "%s%s%s", pfx, name, sfx);
  625. return 1;
  626. }
  627. #define is_hdmi_cfg(conf) \
  628. (get_defcfg_location(conf) == AC_JACK_LOC_HDMI)
  629. /**
  630. * snd_hda_get_pin_label - Get a label for the given I/O pin
  631. * @codec: the HDA codec
  632. * @nid: pin NID
  633. * @cfg: the parsed pin configuration
  634. * @label: the string buffer to store
  635. * @maxlen: the max length of string buffer (including termination)
  636. * @indexp: the pointer to return the index number (for multiple ctls)
  637. *
  638. * Get a label for the given pin. This function works for both input and
  639. * output pins. When @cfg is given as non-NULL, the function tries to get
  640. * an optimized label using hda_get_autocfg_input_label().
  641. *
  642. * This function tries to give a unique label string for the pin as much as
  643. * possible. For example, when the multiple line-outs are present, it adds
  644. * the channel suffix like "Front", "Surround", etc (only when @cfg is given).
  645. * If no unique name with a suffix is available and @indexp is non-NULL, the
  646. * index number is stored in the pointer.
  647. */
  648. int snd_hda_get_pin_label(struct hda_codec *codec, hda_nid_t nid,
  649. const struct auto_pin_cfg *cfg,
  650. char *label, int maxlen, int *indexp)
  651. {
  652. unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
  653. const char *name = NULL;
  654. int i;
  655. bool hdmi;
  656. if (indexp)
  657. *indexp = 0;
  658. if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
  659. return 0;
  660. switch (get_defcfg_device(def_conf)) {
  661. case AC_JACK_LINE_OUT:
  662. return fill_audio_out_name(codec, nid, cfg, "Line Out",
  663. label, maxlen, indexp);
  664. case AC_JACK_SPEAKER:
  665. return fill_audio_out_name(codec, nid, cfg, "Speaker",
  666. label, maxlen, indexp);
  667. case AC_JACK_HP_OUT:
  668. return fill_audio_out_name(codec, nid, cfg, "Headphone",
  669. label, maxlen, indexp);
  670. case AC_JACK_SPDIF_OUT:
  671. case AC_JACK_DIG_OTHER_OUT:
  672. hdmi = is_hdmi_cfg(def_conf);
  673. name = hdmi ? "HDMI" : "SPDIF";
  674. if (cfg && indexp)
  675. for (i = 0; i < cfg->dig_outs; i++) {
  676. hda_nid_t pin = cfg->dig_out_pins[i];
  677. unsigned int c;
  678. if (pin == nid)
  679. break;
  680. c = snd_hda_codec_get_pincfg(codec, pin);
  681. if (hdmi == is_hdmi_cfg(c))
  682. (*indexp)++;
  683. }
  684. break;
  685. default:
  686. if (cfg) {
  687. for (i = 0; i < cfg->num_inputs; i++) {
  688. if (cfg->inputs[i].pin != nid)
  689. continue;
  690. name = hda_get_autocfg_input_label(codec, cfg, i);
  691. if (name)
  692. break;
  693. }
  694. }
  695. if (!name)
  696. name = hda_get_input_pin_label(codec, NULL, nid, true);
  697. break;
  698. }
  699. if (!name)
  700. return 0;
  701. strscpy(label, name, maxlen);
  702. return 1;
  703. }
  704. EXPORT_SYMBOL_GPL(snd_hda_get_pin_label);
  705. /**
  706. * snd_hda_add_verbs - Add verbs to the init list
  707. * @codec: the HDA codec
  708. * @list: zero-terminated verb list to add
  709. *
  710. * Append the given verb list to the execution list. The verbs will be
  711. * performed at init and resume time via snd_hda_apply_verbs().
  712. */
  713. int snd_hda_add_verbs(struct hda_codec *codec,
  714. const struct hda_verb *list)
  715. {
  716. const struct hda_verb **v;
  717. v = snd_array_new(&codec->verbs);
  718. if (!v)
  719. return -ENOMEM;
  720. *v = list;
  721. return 0;
  722. }
  723. EXPORT_SYMBOL_GPL(snd_hda_add_verbs);
  724. /**
  725. * snd_hda_apply_verbs - Execute the init verb lists
  726. * @codec: the HDA codec
  727. */
  728. void snd_hda_apply_verbs(struct hda_codec *codec)
  729. {
  730. const struct hda_verb **v;
  731. int i;
  732. snd_array_for_each(&codec->verbs, i, v)
  733. snd_hda_sequence_write(codec, *v);
  734. }
  735. EXPORT_SYMBOL_GPL(snd_hda_apply_verbs);
  736. /**
  737. * snd_hda_apply_pincfgs - Set each pin config in the given list
  738. * @codec: the HDA codec
  739. * @cfg: NULL-terminated pin config table
  740. */
  741. void snd_hda_apply_pincfgs(struct hda_codec *codec,
  742. const struct hda_pintbl *cfg)
  743. {
  744. for (; cfg->nid; cfg++)
  745. snd_hda_codec_set_pincfg(codec, cfg->nid, cfg->val);
  746. }
  747. EXPORT_SYMBOL_GPL(snd_hda_apply_pincfgs);
  748. static void set_pin_targets(struct hda_codec *codec,
  749. const struct hda_pintbl *cfg)
  750. {
  751. for (; cfg->nid; cfg++)
  752. snd_hda_set_pin_ctl_cache(codec, cfg->nid, cfg->val);
  753. }
  754. void __snd_hda_apply_fixup(struct hda_codec *codec, int id, int action, int depth)
  755. {
  756. const char *modelname = codec->fixup_name;
  757. while (id >= 0) {
  758. const struct hda_fixup *fix = codec->fixup_list + id;
  759. if (++depth > 10)
  760. break;
  761. if (fix->chained_before)
  762. __snd_hda_apply_fixup(codec, fix->chain_id, action, depth + 1);
  763. switch (fix->type) {
  764. case HDA_FIXUP_PINS:
  765. if (action != HDA_FIXUP_ACT_PRE_PROBE || !fix->v.pins)
  766. break;
  767. codec_dbg(codec, "%s: Apply pincfg for %s\n",
  768. codec->core.chip_name, modelname);
  769. snd_hda_apply_pincfgs(codec, fix->v.pins);
  770. break;
  771. case HDA_FIXUP_VERBS:
  772. if (action != HDA_FIXUP_ACT_PROBE || !fix->v.verbs)
  773. break;
  774. codec_dbg(codec, "%s: Apply fix-verbs for %s\n",
  775. codec->core.chip_name, modelname);
  776. snd_hda_add_verbs(codec, fix->v.verbs);
  777. break;
  778. case HDA_FIXUP_FUNC:
  779. if (!fix->v.func)
  780. break;
  781. codec_dbg(codec, "%s: Apply fix-func for %s\n",
  782. codec->core.chip_name, modelname);
  783. fix->v.func(codec, fix, action);
  784. break;
  785. case HDA_FIXUP_PINCTLS:
  786. if (action != HDA_FIXUP_ACT_PROBE || !fix->v.pins)
  787. break;
  788. codec_dbg(codec, "%s: Apply pinctl for %s\n",
  789. codec->core.chip_name, modelname);
  790. set_pin_targets(codec, fix->v.pins);
  791. break;
  792. default:
  793. codec_err(codec, "%s: Invalid fixup type %d\n",
  794. codec->core.chip_name, fix->type);
  795. break;
  796. }
  797. if (!fix->chained || fix->chained_before)
  798. break;
  799. id = fix->chain_id;
  800. }
  801. }
  802. EXPORT_SYMBOL_GPL(__snd_hda_apply_fixup);
  803. /**
  804. * snd_hda_apply_fixup - Apply the fixup chain with the given action
  805. * @codec: the HDA codec
  806. * @action: fixup action (HDA_FIXUP_ACT_XXX)
  807. */
  808. void snd_hda_apply_fixup(struct hda_codec *codec, int action)
  809. {
  810. if (codec->fixup_list)
  811. __snd_hda_apply_fixup(codec, codec->fixup_id, action, 0);
  812. }
  813. EXPORT_SYMBOL_GPL(snd_hda_apply_fixup);
  814. #define IGNORE_SEQ_ASSOC (~(AC_DEFCFG_SEQUENCE | AC_DEFCFG_DEF_ASSOC))
  815. static bool pin_config_match(struct hda_codec *codec,
  816. const struct hda_pintbl *pins,
  817. bool match_all_pins)
  818. {
  819. const struct hda_pincfg *pin;
  820. int i;
  821. snd_array_for_each(&codec->init_pins, i, pin) {
  822. hda_nid_t nid = pin->nid;
  823. u32 cfg = pin->cfg;
  824. const struct hda_pintbl *t_pins;
  825. int found;
  826. t_pins = pins;
  827. found = 0;
  828. for (; t_pins->nid; t_pins++) {
  829. if (t_pins->nid == nid) {
  830. found = 1;
  831. if ((t_pins->val & IGNORE_SEQ_ASSOC) == (cfg & IGNORE_SEQ_ASSOC))
  832. break;
  833. else if ((cfg & 0xf0000000) == 0x40000000 && (t_pins->val & 0xf0000000) == 0x40000000)
  834. break;
  835. else
  836. return false;
  837. }
  838. }
  839. if (match_all_pins &&
  840. !found && (cfg & 0xf0000000) != 0x40000000)
  841. return false;
  842. }
  843. return true;
  844. }
  845. /**
  846. * snd_hda_pick_pin_fixup - Pick up a fixup matching with the pin quirk list
  847. * @codec: the HDA codec
  848. * @pin_quirk: zero-terminated pin quirk list
  849. * @fixlist: the fixup list
  850. * @match_all_pins: all valid pins must match with the table entries
  851. */
  852. void snd_hda_pick_pin_fixup(struct hda_codec *codec,
  853. const struct snd_hda_pin_quirk *pin_quirk,
  854. const struct hda_fixup *fixlist,
  855. bool match_all_pins)
  856. {
  857. const struct snd_hda_pin_quirk *pq;
  858. if (codec->fixup_id != HDA_FIXUP_ID_NOT_SET)
  859. return;
  860. for (pq = pin_quirk; pq->subvendor; pq++) {
  861. if ((codec->core.subsystem_id & 0xffff0000) != (pq->subvendor << 16))
  862. continue;
  863. if (codec->core.vendor_id != pq->codec)
  864. continue;
  865. if (pin_config_match(codec, pq->pins, match_all_pins)) {
  866. codec->fixup_id = pq->value;
  867. #ifdef CONFIG_SND_DEBUG_VERBOSE
  868. codec->fixup_name = pq->name;
  869. codec_dbg(codec, "%s: picked fixup %s (pin match)\n",
  870. codec->core.chip_name, codec->fixup_name);
  871. #endif
  872. codec->fixup_list = fixlist;
  873. return;
  874. }
  875. }
  876. }
  877. EXPORT_SYMBOL_GPL(snd_hda_pick_pin_fixup);
  878. /* check whether the given quirk entry matches with vendor/device pair */
  879. static bool hda_quirk_match(u16 vendor, u16 device, const struct hda_quirk *q)
  880. {
  881. if (q->subvendor != vendor)
  882. return false;
  883. return !q->subdevice ||
  884. (device & q->subdevice_mask) == q->subdevice;
  885. }
  886. /* look through the quirk list and return the matching entry */
  887. static const struct hda_quirk *
  888. hda_quirk_lookup_id(u16 vendor, u16 device, const struct hda_quirk *list)
  889. {
  890. const struct hda_quirk *q;
  891. for (q = list; q->subvendor || q->subdevice; q++) {
  892. if (hda_quirk_match(vendor, device, q))
  893. return q;
  894. }
  895. return NULL;
  896. }
  897. /**
  898. * snd_hda_pick_fixup - Pick up a fixup matching with PCI/codec SSID or model string
  899. * @codec: the HDA codec
  900. * @models: NULL-terminated model string list
  901. * @quirk: zero-terminated PCI/codec SSID quirk list
  902. * @fixlist: the fixup list
  903. *
  904. * Pick up a fixup entry matching with the given model string or SSID.
  905. * If a fixup was already set beforehand, the function doesn't do anything.
  906. * When a special model string "nofixup" is given, also no fixup is applied.
  907. *
  908. * The function tries to find the matching model name at first, if given.
  909. * If the model string contains the SSID alias, try to look up with the given
  910. * alias ID.
  911. * If nothing matched, try to look up the PCI SSID.
  912. * If still nothing matched, try to look up the codec SSID.
  913. */
  914. void snd_hda_pick_fixup(struct hda_codec *codec,
  915. const struct hda_model_fixup *models,
  916. const struct hda_quirk *quirk,
  917. const struct hda_fixup *fixlist)
  918. {
  919. const struct hda_quirk *q;
  920. int id = HDA_FIXUP_ID_NOT_SET;
  921. const char *name = NULL;
  922. const char *type = NULL;
  923. unsigned int vendor, device;
  924. u16 pci_vendor, pci_device;
  925. u16 codec_vendor, codec_device;
  926. if (codec->fixup_id != HDA_FIXUP_ID_NOT_SET)
  927. return;
  928. /* when model=nofixup is given, don't pick up any fixups */
  929. if (codec->modelname && !strcmp(codec->modelname, "nofixup")) {
  930. id = HDA_FIXUP_ID_NO_FIXUP;
  931. fixlist = NULL;
  932. codec_dbg(codec, "%s: picked no fixup (nofixup specified)\n",
  933. codec->core.chip_name);
  934. goto found;
  935. }
  936. /* match with the model name string */
  937. if (codec->modelname && models) {
  938. while (models->name) {
  939. if (!strcmp(codec->modelname, models->name)) {
  940. id = models->id;
  941. name = models->name;
  942. codec_dbg(codec, "%s: picked fixup %s (model specified)\n",
  943. codec->core.chip_name, codec->fixup_name);
  944. goto found;
  945. }
  946. models++;
  947. }
  948. }
  949. if (!quirk)
  950. return;
  951. if (codec->bus->pci) {
  952. pci_vendor = codec->bus->pci->subsystem_vendor;
  953. pci_device = codec->bus->pci->subsystem_device;
  954. }
  955. codec_vendor = codec->core.subsystem_id >> 16;
  956. codec_device = codec->core.subsystem_id & 0xffff;
  957. /* match with the SSID alias given by the model string "XXXX:YYYY" */
  958. if (codec->modelname &&
  959. sscanf(codec->modelname, "%04x:%04x", &vendor, &device) == 2) {
  960. q = hda_quirk_lookup_id(vendor, device, quirk);
  961. if (q) {
  962. type = "alias SSID";
  963. goto found_device;
  964. }
  965. }
  966. /* match primarily with the PCI SSID */
  967. for (q = quirk; q->subvendor || q->subdevice; q++) {
  968. /* if the entry is specific to codec SSID, check with it */
  969. if (!codec->bus->pci || q->match_codec_ssid) {
  970. if (hda_quirk_match(codec_vendor, codec_device, q)) {
  971. type = "codec SSID";
  972. goto found_device;
  973. }
  974. } else {
  975. if (hda_quirk_match(pci_vendor, pci_device, q)) {
  976. type = "PCI SSID";
  977. goto found_device;
  978. }
  979. }
  980. }
  981. /* match with the codec SSID */
  982. q = hda_quirk_lookup_id(codec_vendor, codec_device, quirk);
  983. if (q) {
  984. type = "codec SSID";
  985. goto found_device;
  986. }
  987. return; /* no matching */
  988. found_device:
  989. id = q->value;
  990. #ifdef CONFIG_SND_DEBUG_VERBOSE
  991. name = q->name;
  992. #endif
  993. codec_dbg(codec, "%s: picked fixup %s for %s %04x:%04x\n",
  994. codec->core.chip_name, name ? name : "",
  995. type, q->subvendor, q->subdevice);
  996. found:
  997. codec->fixup_id = id;
  998. codec->fixup_list = fixlist;
  999. codec->fixup_name = name;
  1000. }
  1001. EXPORT_SYMBOL_GPL(snd_hda_pick_fixup);