hda_generic.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  1. /*
  2. * Universal Interface for Intel High Definition Audio Codec
  3. *
  4. * Generic widget tree parser
  5. *
  6. * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
  7. *
  8. * This driver is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This driver is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/init.h>
  23. #include <linux/slab.h>
  24. #include <sound/core.h>
  25. #include "hda_codec.h"
  26. #include "hda_local.h"
  27. /* widget node for parsing */
  28. struct hda_gnode {
  29. hda_nid_t nid; /* NID of this widget */
  30. unsigned short nconns; /* number of input connections */
  31. hda_nid_t *conn_list;
  32. hda_nid_t slist[2]; /* temporay list */
  33. unsigned int wid_caps; /* widget capabilities */
  34. unsigned char type; /* widget type */
  35. unsigned char pin_ctl; /* pin controls */
  36. unsigned char checked; /* the flag indicates that the node is already parsed */
  37. unsigned int pin_caps; /* pin widget capabilities */
  38. unsigned int def_cfg; /* default configuration */
  39. unsigned int amp_out_caps; /* AMP out capabilities */
  40. unsigned int amp_in_caps; /* AMP in capabilities */
  41. struct list_head list;
  42. };
  43. /* patch-specific record */
  44. #define MAX_PCM_VOLS 2
  45. struct pcm_vol {
  46. struct hda_gnode *node; /* Node for PCM volume */
  47. unsigned int index; /* connection of PCM volume */
  48. };
  49. struct hda_gspec {
  50. struct hda_gnode *dac_node[2]; /* DAC node */
  51. struct hda_gnode *out_pin_node[2]; /* Output pin (Line-Out) node */
  52. struct pcm_vol pcm_vol[MAX_PCM_VOLS]; /* PCM volumes */
  53. unsigned int pcm_vol_nodes; /* number of PCM volumes */
  54. struct hda_gnode *adc_node; /* ADC node */
  55. struct hda_gnode *cap_vol_node; /* Node for capture volume */
  56. unsigned int cur_cap_src; /* current capture source */
  57. struct hda_input_mux input_mux;
  58. unsigned int def_amp_in_caps;
  59. unsigned int def_amp_out_caps;
  60. struct hda_pcm pcm_rec; /* PCM information */
  61. struct list_head nid_list; /* list of widgets */
  62. #ifdef CONFIG_SND_HDA_POWER_SAVE
  63. #define MAX_LOOPBACK_AMPS 7
  64. struct hda_loopback_check loopback;
  65. int num_loopbacks;
  66. struct hda_amp_list loopback_list[MAX_LOOPBACK_AMPS + 1];
  67. #endif
  68. };
  69. /*
  70. * retrieve the default device type from the default config value
  71. */
  72. #define defcfg_type(node) (((node)->def_cfg & AC_DEFCFG_DEVICE) >> \
  73. AC_DEFCFG_DEVICE_SHIFT)
  74. #define defcfg_location(node) (((node)->def_cfg & AC_DEFCFG_LOCATION) >> \
  75. AC_DEFCFG_LOCATION_SHIFT)
  76. #define defcfg_port_conn(node) (((node)->def_cfg & AC_DEFCFG_PORT_CONN) >> \
  77. AC_DEFCFG_PORT_CONN_SHIFT)
  78. /*
  79. * destructor
  80. */
  81. static void snd_hda_generic_free(struct hda_codec *codec)
  82. {
  83. struct hda_gspec *spec = codec->spec;
  84. struct hda_gnode *node, *n;
  85. if (! spec)
  86. return;
  87. /* free all widgets */
  88. list_for_each_entry_safe(node, n, &spec->nid_list, list) {
  89. if (node->conn_list != node->slist)
  90. kfree(node->conn_list);
  91. kfree(node);
  92. }
  93. kfree(spec);
  94. }
  95. /*
  96. * add a new widget node and read its attributes
  97. */
  98. static int add_new_node(struct hda_codec *codec, struct hda_gspec *spec, hda_nid_t nid)
  99. {
  100. struct hda_gnode *node;
  101. int nconns;
  102. hda_nid_t conn_list[HDA_MAX_CONNECTIONS];
  103. node = kzalloc(sizeof(*node), GFP_KERNEL);
  104. if (node == NULL)
  105. return -ENOMEM;
  106. node->nid = nid;
  107. node->wid_caps = get_wcaps(codec, nid);
  108. node->type = get_wcaps_type(node->wid_caps);
  109. if (node->wid_caps & AC_WCAP_CONN_LIST) {
  110. nconns = snd_hda_get_connections(codec, nid, conn_list,
  111. HDA_MAX_CONNECTIONS);
  112. if (nconns < 0) {
  113. kfree(node);
  114. return nconns;
  115. }
  116. } else {
  117. nconns = 0;
  118. }
  119. if (nconns <= ARRAY_SIZE(node->slist))
  120. node->conn_list = node->slist;
  121. else {
  122. node->conn_list = kmalloc(sizeof(hda_nid_t) * nconns,
  123. GFP_KERNEL);
  124. if (! node->conn_list) {
  125. snd_printk(KERN_ERR "hda-generic: cannot malloc\n");
  126. kfree(node);
  127. return -ENOMEM;
  128. }
  129. }
  130. memcpy(node->conn_list, conn_list, nconns * sizeof(hda_nid_t));
  131. node->nconns = nconns;
  132. if (node->type == AC_WID_PIN) {
  133. node->pin_caps = snd_hda_query_pin_caps(codec, node->nid);
  134. node->pin_ctl = snd_hda_codec_read(codec, node->nid, 0, AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  135. node->def_cfg = snd_hda_codec_get_pincfg(codec, node->nid);
  136. }
  137. if (node->wid_caps & AC_WCAP_OUT_AMP) {
  138. if (node->wid_caps & AC_WCAP_AMP_OVRD)
  139. node->amp_out_caps = snd_hda_param_read(codec, node->nid, AC_PAR_AMP_OUT_CAP);
  140. if (! node->amp_out_caps)
  141. node->amp_out_caps = spec->def_amp_out_caps;
  142. }
  143. if (node->wid_caps & AC_WCAP_IN_AMP) {
  144. if (node->wid_caps & AC_WCAP_AMP_OVRD)
  145. node->amp_in_caps = snd_hda_param_read(codec, node->nid, AC_PAR_AMP_IN_CAP);
  146. if (! node->amp_in_caps)
  147. node->amp_in_caps = spec->def_amp_in_caps;
  148. }
  149. list_add_tail(&node->list, &spec->nid_list);
  150. return 0;
  151. }
  152. /*
  153. * build the AFG subtree
  154. */
  155. static int build_afg_tree(struct hda_codec *codec)
  156. {
  157. struct hda_gspec *spec = codec->spec;
  158. int i, nodes, err;
  159. hda_nid_t nid;
  160. if (snd_BUG_ON(!spec))
  161. return -EINVAL;
  162. spec->def_amp_out_caps = snd_hda_param_read(codec, codec->afg, AC_PAR_AMP_OUT_CAP);
  163. spec->def_amp_in_caps = snd_hda_param_read(codec, codec->afg, AC_PAR_AMP_IN_CAP);
  164. nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid);
  165. if (! nid || nodes < 0) {
  166. printk(KERN_ERR "Invalid AFG subtree\n");
  167. return -EINVAL;
  168. }
  169. /* parse all nodes belonging to the AFG */
  170. for (i = 0; i < nodes; i++, nid++) {
  171. if ((err = add_new_node(codec, spec, nid)) < 0)
  172. return err;
  173. }
  174. return 0;
  175. }
  176. /*
  177. * look for the node record for the given NID
  178. */
  179. /* FIXME: should avoid the braindead linear search */
  180. static struct hda_gnode *hda_get_node(struct hda_gspec *spec, hda_nid_t nid)
  181. {
  182. struct hda_gnode *node;
  183. list_for_each_entry(node, &spec->nid_list, list) {
  184. if (node->nid == nid)
  185. return node;
  186. }
  187. return NULL;
  188. }
  189. /*
  190. * unmute (and set max vol) the output amplifier
  191. */
  192. static int unmute_output(struct hda_codec *codec, struct hda_gnode *node)
  193. {
  194. unsigned int val, ofs;
  195. snd_printdd("UNMUTE OUT: NID=0x%x\n", node->nid);
  196. val = (node->amp_out_caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
  197. ofs = (node->amp_out_caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
  198. if (val >= ofs)
  199. val -= ofs;
  200. snd_hda_codec_amp_stereo(codec, node->nid, HDA_OUTPUT, 0, 0xff, val);
  201. return 0;
  202. }
  203. /*
  204. * unmute (and set max vol) the input amplifier
  205. */
  206. static int unmute_input(struct hda_codec *codec, struct hda_gnode *node, unsigned int index)
  207. {
  208. unsigned int val, ofs;
  209. snd_printdd("UNMUTE IN: NID=0x%x IDX=0x%x\n", node->nid, index);
  210. val = (node->amp_in_caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
  211. ofs = (node->amp_in_caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
  212. if (val >= ofs)
  213. val -= ofs;
  214. snd_hda_codec_amp_stereo(codec, node->nid, HDA_INPUT, index, 0xff, val);
  215. return 0;
  216. }
  217. /*
  218. * select the input connection of the given node.
  219. */
  220. static int select_input_connection(struct hda_codec *codec, struct hda_gnode *node,
  221. unsigned int index)
  222. {
  223. snd_printdd("CONNECT: NID=0x%x IDX=0x%x\n", node->nid, index);
  224. return snd_hda_codec_write_cache(codec, node->nid, 0,
  225. AC_VERB_SET_CONNECT_SEL, index);
  226. }
  227. /*
  228. * clear checked flag of each node in the node list
  229. */
  230. static void clear_check_flags(struct hda_gspec *spec)
  231. {
  232. struct hda_gnode *node;
  233. list_for_each_entry(node, &spec->nid_list, list) {
  234. node->checked = 0;
  235. }
  236. }
  237. /*
  238. * parse the output path recursively until reach to an audio output widget
  239. *
  240. * returns 0 if not found, 1 if found, or a negative error code.
  241. */
  242. static int parse_output_path(struct hda_codec *codec, struct hda_gspec *spec,
  243. struct hda_gnode *node, int dac_idx)
  244. {
  245. int i, err;
  246. struct hda_gnode *child;
  247. if (node->checked)
  248. return 0;
  249. node->checked = 1;
  250. if (node->type == AC_WID_AUD_OUT) {
  251. if (node->wid_caps & AC_WCAP_DIGITAL) {
  252. snd_printdd("Skip Digital OUT node %x\n", node->nid);
  253. return 0;
  254. }
  255. snd_printdd("AUD_OUT found %x\n", node->nid);
  256. if (spec->dac_node[dac_idx]) {
  257. /* already DAC node is assigned, just unmute & connect */
  258. return node == spec->dac_node[dac_idx];
  259. }
  260. spec->dac_node[dac_idx] = node;
  261. if ((node->wid_caps & AC_WCAP_OUT_AMP) &&
  262. spec->pcm_vol_nodes < MAX_PCM_VOLS) {
  263. spec->pcm_vol[spec->pcm_vol_nodes].node = node;
  264. spec->pcm_vol[spec->pcm_vol_nodes].index = 0;
  265. spec->pcm_vol_nodes++;
  266. }
  267. return 1; /* found */
  268. }
  269. for (i = 0; i < node->nconns; i++) {
  270. child = hda_get_node(spec, node->conn_list[i]);
  271. if (! child)
  272. continue;
  273. err = parse_output_path(codec, spec, child, dac_idx);
  274. if (err < 0)
  275. return err;
  276. else if (err > 0) {
  277. /* found one,
  278. * select the path, unmute both input and output
  279. */
  280. if (node->nconns > 1)
  281. select_input_connection(codec, node, i);
  282. unmute_input(codec, node, i);
  283. unmute_output(codec, node);
  284. if (spec->dac_node[dac_idx] &&
  285. spec->pcm_vol_nodes < MAX_PCM_VOLS &&
  286. !(spec->dac_node[dac_idx]->wid_caps &
  287. AC_WCAP_OUT_AMP)) {
  288. if ((node->wid_caps & AC_WCAP_IN_AMP) ||
  289. (node->wid_caps & AC_WCAP_OUT_AMP)) {
  290. int n = spec->pcm_vol_nodes;
  291. spec->pcm_vol[n].node = node;
  292. spec->pcm_vol[n].index = i;
  293. spec->pcm_vol_nodes++;
  294. }
  295. }
  296. return 1;
  297. }
  298. }
  299. return 0;
  300. }
  301. /*
  302. * Look for the output PIN widget with the given jack type
  303. * and parse the output path to that PIN.
  304. *
  305. * Returns the PIN node when the path to DAC is established.
  306. */
  307. static struct hda_gnode *parse_output_jack(struct hda_codec *codec,
  308. struct hda_gspec *spec,
  309. int jack_type)
  310. {
  311. struct hda_gnode *node;
  312. int err;
  313. list_for_each_entry(node, &spec->nid_list, list) {
  314. if (node->type != AC_WID_PIN)
  315. continue;
  316. /* output capable? */
  317. if (! (node->pin_caps & AC_PINCAP_OUT))
  318. continue;
  319. if (defcfg_port_conn(node) == AC_JACK_PORT_NONE)
  320. continue; /* unconnected */
  321. if (jack_type >= 0) {
  322. if (jack_type != defcfg_type(node))
  323. continue;
  324. if (node->wid_caps & AC_WCAP_DIGITAL)
  325. continue; /* skip SPDIF */
  326. } else {
  327. /* output as default? */
  328. if (! (node->pin_ctl & AC_PINCTL_OUT_EN))
  329. continue;
  330. }
  331. clear_check_flags(spec);
  332. err = parse_output_path(codec, spec, node, 0);
  333. if (err < 0)
  334. return NULL;
  335. if (! err && spec->out_pin_node[0]) {
  336. err = parse_output_path(codec, spec, node, 1);
  337. if (err < 0)
  338. return NULL;
  339. }
  340. if (err > 0) {
  341. /* unmute the PIN output */
  342. unmute_output(codec, node);
  343. /* set PIN-Out enable */
  344. snd_hda_codec_write_cache(codec, node->nid, 0,
  345. AC_VERB_SET_PIN_WIDGET_CONTROL,
  346. AC_PINCTL_OUT_EN |
  347. ((node->pin_caps & AC_PINCAP_HP_DRV) ?
  348. AC_PINCTL_HP_EN : 0));
  349. return node;
  350. }
  351. }
  352. return NULL;
  353. }
  354. /*
  355. * parse outputs
  356. */
  357. static int parse_output(struct hda_codec *codec)
  358. {
  359. struct hda_gspec *spec = codec->spec;
  360. struct hda_gnode *node;
  361. /*
  362. * Look for the output PIN widget
  363. */
  364. /* first, look for the line-out pin */
  365. node = parse_output_jack(codec, spec, AC_JACK_LINE_OUT);
  366. if (node) /* found, remember the PIN node */
  367. spec->out_pin_node[0] = node;
  368. else {
  369. /* if no line-out is found, try speaker out */
  370. node = parse_output_jack(codec, spec, AC_JACK_SPEAKER);
  371. if (node)
  372. spec->out_pin_node[0] = node;
  373. }
  374. /* look for the HP-out pin */
  375. node = parse_output_jack(codec, spec, AC_JACK_HP_OUT);
  376. if (node) {
  377. if (! spec->out_pin_node[0])
  378. spec->out_pin_node[0] = node;
  379. else
  380. spec->out_pin_node[1] = node;
  381. }
  382. if (! spec->out_pin_node[0]) {
  383. /* no line-out or HP pins found,
  384. * then choose for the first output pin
  385. */
  386. spec->out_pin_node[0] = parse_output_jack(codec, spec, -1);
  387. if (! spec->out_pin_node[0])
  388. snd_printd("hda_generic: no proper output path found\n");
  389. }
  390. return 0;
  391. }
  392. /*
  393. * input MUX
  394. */
  395. /* control callbacks */
  396. static int capture_source_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  397. {
  398. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  399. struct hda_gspec *spec = codec->spec;
  400. return snd_hda_input_mux_info(&spec->input_mux, uinfo);
  401. }
  402. static int capture_source_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  403. {
  404. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  405. struct hda_gspec *spec = codec->spec;
  406. ucontrol->value.enumerated.item[0] = spec->cur_cap_src;
  407. return 0;
  408. }
  409. static int capture_source_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  410. {
  411. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  412. struct hda_gspec *spec = codec->spec;
  413. return snd_hda_input_mux_put(codec, &spec->input_mux, ucontrol,
  414. spec->adc_node->nid, &spec->cur_cap_src);
  415. }
  416. /*
  417. * return the string name of the given input PIN widget
  418. */
  419. static const char *get_input_type(struct hda_gnode *node, unsigned int *pinctl)
  420. {
  421. unsigned int location = defcfg_location(node);
  422. switch (defcfg_type(node)) {
  423. case AC_JACK_LINE_IN:
  424. if ((location & 0x0f) == AC_JACK_LOC_FRONT)
  425. return "Front Line";
  426. return "Line";
  427. case AC_JACK_CD:
  428. #if 0
  429. if (pinctl)
  430. *pinctl |= AC_PINCTL_VREF_GRD;
  431. #endif
  432. return "CD";
  433. case AC_JACK_AUX:
  434. if ((location & 0x0f) == AC_JACK_LOC_FRONT)
  435. return "Front Aux";
  436. return "Aux";
  437. case AC_JACK_MIC_IN:
  438. if (pinctl &&
  439. (node->pin_caps &
  440. (AC_PINCAP_VREF_80 << AC_PINCAP_VREF_SHIFT)))
  441. *pinctl |= AC_PINCTL_VREF_80;
  442. if ((location & 0x0f) == AC_JACK_LOC_FRONT)
  443. return "Front Mic";
  444. return "Mic";
  445. case AC_JACK_SPDIF_IN:
  446. return "SPDIF";
  447. case AC_JACK_DIG_OTHER_IN:
  448. return "Digital";
  449. }
  450. return NULL;
  451. }
  452. /*
  453. * parse the nodes recursively until reach to the input PIN
  454. *
  455. * returns 0 if not found, 1 if found, or a negative error code.
  456. */
  457. static int parse_adc_sub_nodes(struct hda_codec *codec, struct hda_gspec *spec,
  458. struct hda_gnode *node, int idx)
  459. {
  460. int i, err;
  461. unsigned int pinctl;
  462. const char *type;
  463. if (node->checked)
  464. return 0;
  465. node->checked = 1;
  466. if (node->type != AC_WID_PIN) {
  467. for (i = 0; i < node->nconns; i++) {
  468. struct hda_gnode *child;
  469. child = hda_get_node(spec, node->conn_list[i]);
  470. if (! child)
  471. continue;
  472. err = parse_adc_sub_nodes(codec, spec, child, idx);
  473. if (err < 0)
  474. return err;
  475. if (err > 0) {
  476. /* found one,
  477. * select the path, unmute both input and output
  478. */
  479. if (node->nconns > 1)
  480. select_input_connection(codec, node, i);
  481. unmute_input(codec, node, i);
  482. unmute_output(codec, node);
  483. return err;
  484. }
  485. }
  486. return 0;
  487. }
  488. /* input capable? */
  489. if (! (node->pin_caps & AC_PINCAP_IN))
  490. return 0;
  491. if (defcfg_port_conn(node) == AC_JACK_PORT_NONE)
  492. return 0; /* unconnected */
  493. if (node->wid_caps & AC_WCAP_DIGITAL)
  494. return 0; /* skip SPDIF */
  495. if (spec->input_mux.num_items >= HDA_MAX_NUM_INPUTS) {
  496. snd_printk(KERN_ERR "hda_generic: Too many items for capture\n");
  497. return -EINVAL;
  498. }
  499. pinctl = AC_PINCTL_IN_EN;
  500. /* create a proper capture source label */
  501. type = get_input_type(node, &pinctl);
  502. if (! type) {
  503. /* input as default? */
  504. if (! (node->pin_ctl & AC_PINCTL_IN_EN))
  505. return 0;
  506. type = "Input";
  507. }
  508. snd_hda_add_imux_item(&spec->input_mux, type, idx, NULL);
  509. /* unmute the PIN external input */
  510. unmute_input(codec, node, 0); /* index = 0? */
  511. /* set PIN-In enable */
  512. snd_hda_codec_write_cache(codec, node->nid, 0,
  513. AC_VERB_SET_PIN_WIDGET_CONTROL, pinctl);
  514. return 1; /* found */
  515. }
  516. /*
  517. * parse input
  518. */
  519. static int parse_input_path(struct hda_codec *codec, struct hda_gnode *adc_node)
  520. {
  521. struct hda_gspec *spec = codec->spec;
  522. struct hda_gnode *node;
  523. int i, err;
  524. snd_printdd("AUD_IN = %x\n", adc_node->nid);
  525. clear_check_flags(spec);
  526. // awk added - fixed no recording due to muted widget
  527. unmute_input(codec, adc_node, 0);
  528. /*
  529. * check each connection of the ADC
  530. * if it reaches to a proper input PIN, add the path as the
  531. * input path.
  532. */
  533. /* first, check the direct connections to PIN widgets */
  534. for (i = 0; i < adc_node->nconns; i++) {
  535. node = hda_get_node(spec, adc_node->conn_list[i]);
  536. if (node && node->type == AC_WID_PIN) {
  537. err = parse_adc_sub_nodes(codec, spec, node, i);
  538. if (err < 0)
  539. return err;
  540. }
  541. }
  542. /* ... then check the rests, more complicated connections */
  543. for (i = 0; i < adc_node->nconns; i++) {
  544. node = hda_get_node(spec, adc_node->conn_list[i]);
  545. if (node && node->type != AC_WID_PIN) {
  546. err = parse_adc_sub_nodes(codec, spec, node, i);
  547. if (err < 0)
  548. return err;
  549. }
  550. }
  551. if (! spec->input_mux.num_items)
  552. return 0; /* no input path found... */
  553. snd_printdd("[Capture Source] NID=0x%x, #SRC=%d\n", adc_node->nid, spec->input_mux.num_items);
  554. for (i = 0; i < spec->input_mux.num_items; i++)
  555. snd_printdd(" [%s] IDX=0x%x\n", spec->input_mux.items[i].label,
  556. spec->input_mux.items[i].index);
  557. spec->adc_node = adc_node;
  558. return 1;
  559. }
  560. /*
  561. * parse input
  562. */
  563. static int parse_input(struct hda_codec *codec)
  564. {
  565. struct hda_gspec *spec = codec->spec;
  566. struct hda_gnode *node;
  567. int err;
  568. /*
  569. * At first we look for an audio input widget.
  570. * If it reaches to certain input PINs, we take it as the
  571. * input path.
  572. */
  573. list_for_each_entry(node, &spec->nid_list, list) {
  574. if (node->wid_caps & AC_WCAP_DIGITAL)
  575. continue; /* skip SPDIF */
  576. if (node->type == AC_WID_AUD_IN) {
  577. err = parse_input_path(codec, node);
  578. if (err < 0)
  579. return err;
  580. else if (err > 0)
  581. return 0;
  582. }
  583. }
  584. snd_printd("hda_generic: no proper input path found\n");
  585. return 0;
  586. }
  587. #ifdef CONFIG_SND_HDA_POWER_SAVE
  588. static void add_input_loopback(struct hda_codec *codec, hda_nid_t nid,
  589. int dir, int idx)
  590. {
  591. struct hda_gspec *spec = codec->spec;
  592. struct hda_amp_list *p;
  593. if (spec->num_loopbacks >= MAX_LOOPBACK_AMPS) {
  594. snd_printk(KERN_ERR "hda_generic: Too many loopback ctls\n");
  595. return;
  596. }
  597. p = &spec->loopback_list[spec->num_loopbacks++];
  598. p->nid = nid;
  599. p->dir = dir;
  600. p->idx = idx;
  601. spec->loopback.amplist = spec->loopback_list;
  602. }
  603. #else
  604. #define add_input_loopback(codec,nid,dir,idx)
  605. #endif
  606. /*
  607. * create mixer controls if possible
  608. */
  609. static int create_mixer(struct hda_codec *codec, struct hda_gnode *node,
  610. unsigned int index, const char *type,
  611. const char *dir_sfx, int is_loopback)
  612. {
  613. char name[32];
  614. int err;
  615. int created = 0;
  616. struct snd_kcontrol_new knew;
  617. if (type)
  618. sprintf(name, "%s %s Switch", type, dir_sfx);
  619. else
  620. sprintf(name, "%s Switch", dir_sfx);
  621. if ((node->wid_caps & AC_WCAP_IN_AMP) &&
  622. (node->amp_in_caps & AC_AMPCAP_MUTE)) {
  623. knew = (struct snd_kcontrol_new)HDA_CODEC_MUTE(name, node->nid, index, HDA_INPUT);
  624. if (is_loopback)
  625. add_input_loopback(codec, node->nid, HDA_INPUT, index);
  626. snd_printdd("[%s] NID=0x%x, DIR=IN, IDX=0x%x\n", name, node->nid, index);
  627. err = snd_hda_ctl_add(codec, node->nid,
  628. snd_ctl_new1(&knew, codec));
  629. if (err < 0)
  630. return err;
  631. created = 1;
  632. } else if ((node->wid_caps & AC_WCAP_OUT_AMP) &&
  633. (node->amp_out_caps & AC_AMPCAP_MUTE)) {
  634. knew = (struct snd_kcontrol_new)HDA_CODEC_MUTE(name, node->nid, 0, HDA_OUTPUT);
  635. if (is_loopback)
  636. add_input_loopback(codec, node->nid, HDA_OUTPUT, 0);
  637. snd_printdd("[%s] NID=0x%x, DIR=OUT\n", name, node->nid);
  638. err = snd_hda_ctl_add(codec, node->nid,
  639. snd_ctl_new1(&knew, codec));
  640. if (err < 0)
  641. return err;
  642. created = 1;
  643. }
  644. if (type)
  645. sprintf(name, "%s %s Volume", type, dir_sfx);
  646. else
  647. sprintf(name, "%s Volume", dir_sfx);
  648. if ((node->wid_caps & AC_WCAP_IN_AMP) &&
  649. (node->amp_in_caps & AC_AMPCAP_NUM_STEPS)) {
  650. knew = (struct snd_kcontrol_new)HDA_CODEC_VOLUME(name, node->nid, index, HDA_INPUT);
  651. snd_printdd("[%s] NID=0x%x, DIR=IN, IDX=0x%x\n", name, node->nid, index);
  652. err = snd_hda_ctl_add(codec, node->nid,
  653. snd_ctl_new1(&knew, codec));
  654. if (err < 0)
  655. return err;
  656. created = 1;
  657. } else if ((node->wid_caps & AC_WCAP_OUT_AMP) &&
  658. (node->amp_out_caps & AC_AMPCAP_NUM_STEPS)) {
  659. knew = (struct snd_kcontrol_new)HDA_CODEC_VOLUME(name, node->nid, 0, HDA_OUTPUT);
  660. snd_printdd("[%s] NID=0x%x, DIR=OUT\n", name, node->nid);
  661. err = snd_hda_ctl_add(codec, node->nid,
  662. snd_ctl_new1(&knew, codec));
  663. if (err < 0)
  664. return err;
  665. created = 1;
  666. }
  667. return created;
  668. }
  669. /*
  670. * check whether the controls with the given name and direction suffix already exist
  671. */
  672. static int check_existing_control(struct hda_codec *codec, const char *type, const char *dir)
  673. {
  674. struct snd_ctl_elem_id id;
  675. memset(&id, 0, sizeof(id));
  676. sprintf(id.name, "%s %s Volume", type, dir);
  677. id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  678. if (snd_ctl_find_id(codec->bus->card, &id))
  679. return 1;
  680. sprintf(id.name, "%s %s Switch", type, dir);
  681. id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  682. if (snd_ctl_find_id(codec->bus->card, &id))
  683. return 1;
  684. return 0;
  685. }
  686. /*
  687. * build output mixer controls
  688. */
  689. static int create_output_mixers(struct hda_codec *codec,
  690. const char * const *names)
  691. {
  692. struct hda_gspec *spec = codec->spec;
  693. int i, err;
  694. for (i = 0; i < spec->pcm_vol_nodes; i++) {
  695. err = create_mixer(codec, spec->pcm_vol[i].node,
  696. spec->pcm_vol[i].index,
  697. names[i], "Playback", 0);
  698. if (err < 0)
  699. return err;
  700. }
  701. return 0;
  702. }
  703. static int build_output_controls(struct hda_codec *codec)
  704. {
  705. struct hda_gspec *spec = codec->spec;
  706. static const char * const types_speaker[] = { "Speaker", "Headphone" };
  707. static const char * const types_line[] = { "Front", "Headphone" };
  708. switch (spec->pcm_vol_nodes) {
  709. case 1:
  710. return create_mixer(codec, spec->pcm_vol[0].node,
  711. spec->pcm_vol[0].index,
  712. "Master", "Playback", 0);
  713. case 2:
  714. if (defcfg_type(spec->out_pin_node[0]) == AC_JACK_SPEAKER)
  715. return create_output_mixers(codec, types_speaker);
  716. else
  717. return create_output_mixers(codec, types_line);
  718. }
  719. return 0;
  720. }
  721. /* create capture volume/switch */
  722. static int build_input_controls(struct hda_codec *codec)
  723. {
  724. struct hda_gspec *spec = codec->spec;
  725. struct hda_gnode *adc_node = spec->adc_node;
  726. int i, err;
  727. static struct snd_kcontrol_new cap_sel = {
  728. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  729. .name = "Capture Source",
  730. .info = capture_source_info,
  731. .get = capture_source_get,
  732. .put = capture_source_put,
  733. };
  734. if (! adc_node || ! spec->input_mux.num_items)
  735. return 0; /* not found */
  736. spec->cur_cap_src = 0;
  737. select_input_connection(codec, adc_node,
  738. spec->input_mux.items[0].index);
  739. /* create capture volume and switch controls if the ADC has an amp */
  740. /* do we have only a single item? */
  741. if (spec->input_mux.num_items == 1) {
  742. err = create_mixer(codec, adc_node,
  743. spec->input_mux.items[0].index,
  744. NULL, "Capture", 0);
  745. if (err < 0)
  746. return err;
  747. return 0;
  748. }
  749. /* create input MUX if multiple sources are available */
  750. err = snd_hda_ctl_add(codec, spec->adc_node->nid,
  751. snd_ctl_new1(&cap_sel, codec));
  752. if (err < 0)
  753. return err;
  754. /* no volume control? */
  755. if (! (adc_node->wid_caps & AC_WCAP_IN_AMP) ||
  756. ! (adc_node->amp_in_caps & AC_AMPCAP_NUM_STEPS))
  757. return 0;
  758. for (i = 0; i < spec->input_mux.num_items; i++) {
  759. struct snd_kcontrol_new knew;
  760. char name[32];
  761. sprintf(name, "%s Capture Volume",
  762. spec->input_mux.items[i].label);
  763. knew = (struct snd_kcontrol_new)
  764. HDA_CODEC_VOLUME(name, adc_node->nid,
  765. spec->input_mux.items[i].index,
  766. HDA_INPUT);
  767. err = snd_hda_ctl_add(codec, adc_node->nid,
  768. snd_ctl_new1(&knew, codec));
  769. if (err < 0)
  770. return err;
  771. }
  772. return 0;
  773. }
  774. /*
  775. * parse the nodes recursively until reach to the output PIN.
  776. *
  777. * returns 0 - if not found,
  778. * 1 - if found, but no mixer is created
  779. * 2 - if found and mixer was already created, (just skip)
  780. * a negative error code
  781. */
  782. static int parse_loopback_path(struct hda_codec *codec, struct hda_gspec *spec,
  783. struct hda_gnode *node, struct hda_gnode *dest_node,
  784. const char *type)
  785. {
  786. int i, err;
  787. if (node->checked)
  788. return 0;
  789. node->checked = 1;
  790. if (node == dest_node) {
  791. /* loopback connection found */
  792. return 1;
  793. }
  794. for (i = 0; i < node->nconns; i++) {
  795. struct hda_gnode *child = hda_get_node(spec, node->conn_list[i]);
  796. if (! child)
  797. continue;
  798. err = parse_loopback_path(codec, spec, child, dest_node, type);
  799. if (err < 0)
  800. return err;
  801. else if (err >= 1) {
  802. if (err == 1) {
  803. err = create_mixer(codec, node, i, type,
  804. "Playback", 1);
  805. if (err < 0)
  806. return err;
  807. if (err > 0)
  808. return 2; /* ok, created */
  809. /* not created, maybe in the lower path */
  810. err = 1;
  811. }
  812. /* connect and unmute */
  813. if (node->nconns > 1)
  814. select_input_connection(codec, node, i);
  815. unmute_input(codec, node, i);
  816. unmute_output(codec, node);
  817. return err;
  818. }
  819. }
  820. return 0;
  821. }
  822. /*
  823. * parse the tree and build the loopback controls
  824. */
  825. static int build_loopback_controls(struct hda_codec *codec)
  826. {
  827. struct hda_gspec *spec = codec->spec;
  828. struct hda_gnode *node;
  829. int err;
  830. const char *type;
  831. if (! spec->out_pin_node[0])
  832. return 0;
  833. list_for_each_entry(node, &spec->nid_list, list) {
  834. if (node->type != AC_WID_PIN)
  835. continue;
  836. /* input capable? */
  837. if (! (node->pin_caps & AC_PINCAP_IN))
  838. return 0;
  839. type = get_input_type(node, NULL);
  840. if (type) {
  841. if (check_existing_control(codec, type, "Playback"))
  842. continue;
  843. clear_check_flags(spec);
  844. err = parse_loopback_path(codec, spec,
  845. spec->out_pin_node[0],
  846. node, type);
  847. if (err < 0)
  848. return err;
  849. if (! err)
  850. continue;
  851. }
  852. }
  853. return 0;
  854. }
  855. /*
  856. * build mixer controls
  857. */
  858. static int build_generic_controls(struct hda_codec *codec)
  859. {
  860. int err;
  861. if ((err = build_input_controls(codec)) < 0 ||
  862. (err = build_output_controls(codec)) < 0 ||
  863. (err = build_loopback_controls(codec)) < 0)
  864. return err;
  865. return 0;
  866. }
  867. /*
  868. * PCM
  869. */
  870. static struct hda_pcm_stream generic_pcm_playback = {
  871. .substreams = 1,
  872. .channels_min = 2,
  873. .channels_max = 2,
  874. };
  875. static int generic_pcm2_prepare(struct hda_pcm_stream *hinfo,
  876. struct hda_codec *codec,
  877. unsigned int stream_tag,
  878. unsigned int format,
  879. struct snd_pcm_substream *substream)
  880. {
  881. struct hda_gspec *spec = codec->spec;
  882. snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
  883. snd_hda_codec_setup_stream(codec, spec->dac_node[1]->nid,
  884. stream_tag, 0, format);
  885. return 0;
  886. }
  887. static int generic_pcm2_cleanup(struct hda_pcm_stream *hinfo,
  888. struct hda_codec *codec,
  889. struct snd_pcm_substream *substream)
  890. {
  891. struct hda_gspec *spec = codec->spec;
  892. snd_hda_codec_cleanup_stream(codec, hinfo->nid);
  893. snd_hda_codec_cleanup_stream(codec, spec->dac_node[1]->nid);
  894. return 0;
  895. }
  896. static int build_generic_pcms(struct hda_codec *codec)
  897. {
  898. struct hda_gspec *spec = codec->spec;
  899. struct hda_pcm *info = &spec->pcm_rec;
  900. if (! spec->dac_node[0] && ! spec->adc_node) {
  901. snd_printd("hda_generic: no PCM found\n");
  902. return 0;
  903. }
  904. codec->num_pcms = 1;
  905. codec->pcm_info = info;
  906. info->name = "HDA Generic";
  907. if (spec->dac_node[0]) {
  908. info->stream[0] = generic_pcm_playback;
  909. info->stream[0].nid = spec->dac_node[0]->nid;
  910. if (spec->dac_node[1]) {
  911. info->stream[0].ops.prepare = generic_pcm2_prepare;
  912. info->stream[0].ops.cleanup = generic_pcm2_cleanup;
  913. }
  914. }
  915. if (spec->adc_node) {
  916. info->stream[1] = generic_pcm_playback;
  917. info->stream[1].nid = spec->adc_node->nid;
  918. }
  919. return 0;
  920. }
  921. #ifdef CONFIG_SND_HDA_POWER_SAVE
  922. static int generic_check_power_status(struct hda_codec *codec, hda_nid_t nid)
  923. {
  924. struct hda_gspec *spec = codec->spec;
  925. return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
  926. }
  927. #endif
  928. /*
  929. */
  930. static struct hda_codec_ops generic_patch_ops = {
  931. .build_controls = build_generic_controls,
  932. .build_pcms = build_generic_pcms,
  933. .free = snd_hda_generic_free,
  934. #ifdef CONFIG_SND_HDA_POWER_SAVE
  935. .check_power_status = generic_check_power_status,
  936. #endif
  937. };
  938. /*
  939. * the generic parser
  940. */
  941. int snd_hda_parse_generic_codec(struct hda_codec *codec)
  942. {
  943. struct hda_gspec *spec;
  944. int err;
  945. if(!codec->afg)
  946. return 0;
  947. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  948. if (spec == NULL) {
  949. printk(KERN_ERR "hda_generic: can't allocate spec\n");
  950. return -ENOMEM;
  951. }
  952. codec->spec = spec;
  953. INIT_LIST_HEAD(&spec->nid_list);
  954. if ((err = build_afg_tree(codec)) < 0)
  955. goto error;
  956. if ((err = parse_input(codec)) < 0 ||
  957. (err = parse_output(codec)) < 0)
  958. goto error;
  959. codec->patch_ops = generic_patch_ops;
  960. return 0;
  961. error:
  962. snd_hda_generic_free(codec);
  963. return err;
  964. }
  965. EXPORT_SYMBOL(snd_hda_parse_generic_codec);