hda_generic.c 28 KB

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