hda_generic.c 28 KB

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