hda_auto_parser.c 29 KB

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