hda_auto_parser.c 29 KB

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