hda_auto_parser.c 30 KB

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