hda_generic.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  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, const char **names)
  690. {
  691. struct hda_gspec *spec = codec->spec;
  692. int i, err;
  693. for (i = 0; i < spec->pcm_vol_nodes; i++) {
  694. err = create_mixer(codec, spec->pcm_vol[i].node,
  695. spec->pcm_vol[i].index,
  696. names[i], "Playback", 0);
  697. if (err < 0)
  698. return err;
  699. }
  700. return 0;
  701. }
  702. static int build_output_controls(struct hda_codec *codec)
  703. {
  704. struct hda_gspec *spec = codec->spec;
  705. static const char *types_speaker[] = { "Speaker", "Headphone" };
  706. static const char *types_line[] = { "Front", "Headphone" };
  707. switch (spec->pcm_vol_nodes) {
  708. case 1:
  709. return create_mixer(codec, spec->pcm_vol[0].node,
  710. spec->pcm_vol[0].index,
  711. "Master", "Playback", 0);
  712. case 2:
  713. if (defcfg_type(spec->out_pin_node[0]) == AC_JACK_SPEAKER)
  714. return create_output_mixers(codec, types_speaker);
  715. else
  716. return create_output_mixers(codec, types_line);
  717. }
  718. return 0;
  719. }
  720. /* create capture volume/switch */
  721. static int build_input_controls(struct hda_codec *codec)
  722. {
  723. struct hda_gspec *spec = codec->spec;
  724. struct hda_gnode *adc_node = spec->adc_node;
  725. int i, err;
  726. static struct snd_kcontrol_new cap_sel = {
  727. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  728. .name = "Capture Source",
  729. .info = capture_source_info,
  730. .get = capture_source_get,
  731. .put = capture_source_put,
  732. };
  733. if (! adc_node || ! spec->input_mux.num_items)
  734. return 0; /* not found */
  735. spec->cur_cap_src = 0;
  736. select_input_connection(codec, adc_node,
  737. spec->input_mux.items[0].index);
  738. /* create capture volume and switch controls if the ADC has an amp */
  739. /* do we have only a single item? */
  740. if (spec->input_mux.num_items == 1) {
  741. err = create_mixer(codec, adc_node,
  742. spec->input_mux.items[0].index,
  743. NULL, "Capture", 0);
  744. if (err < 0)
  745. return err;
  746. return 0;
  747. }
  748. /* create input MUX if multiple sources are available */
  749. err = snd_hda_ctl_add(codec, spec->adc_node->nid,
  750. snd_ctl_new1(&cap_sel, codec));
  751. if (err < 0)
  752. return err;
  753. /* no volume control? */
  754. if (! (adc_node->wid_caps & AC_WCAP_IN_AMP) ||
  755. ! (adc_node->amp_in_caps & AC_AMPCAP_NUM_STEPS))
  756. return 0;
  757. for (i = 0; i < spec->input_mux.num_items; i++) {
  758. struct snd_kcontrol_new knew;
  759. char name[32];
  760. sprintf(name, "%s Capture Volume",
  761. spec->input_mux.items[i].label);
  762. knew = (struct snd_kcontrol_new)
  763. HDA_CODEC_VOLUME(name, adc_node->nid,
  764. spec->input_mux.items[i].index,
  765. HDA_INPUT);
  766. err = snd_hda_ctl_add(codec, adc_node->nid,
  767. snd_ctl_new1(&knew, codec));
  768. if (err < 0)
  769. return err;
  770. }
  771. return 0;
  772. }
  773. /*
  774. * parse the nodes recursively until reach to the output PIN.
  775. *
  776. * returns 0 - if not found,
  777. * 1 - if found, but no mixer is created
  778. * 2 - if found and mixer was already created, (just skip)
  779. * a negative error code
  780. */
  781. static int parse_loopback_path(struct hda_codec *codec, struct hda_gspec *spec,
  782. struct hda_gnode *node, struct hda_gnode *dest_node,
  783. const char *type)
  784. {
  785. int i, err;
  786. if (node->checked)
  787. return 0;
  788. node->checked = 1;
  789. if (node == dest_node) {
  790. /* loopback connection found */
  791. return 1;
  792. }
  793. for (i = 0; i < node->nconns; i++) {
  794. struct hda_gnode *child = hda_get_node(spec, node->conn_list[i]);
  795. if (! child)
  796. continue;
  797. err = parse_loopback_path(codec, spec, child, dest_node, type);
  798. if (err < 0)
  799. return err;
  800. else if (err >= 1) {
  801. if (err == 1) {
  802. err = create_mixer(codec, node, i, type,
  803. "Playback", 1);
  804. if (err < 0)
  805. return err;
  806. if (err > 0)
  807. return 2; /* ok, created */
  808. /* not created, maybe in the lower path */
  809. err = 1;
  810. }
  811. /* connect and unmute */
  812. if (node->nconns > 1)
  813. select_input_connection(codec, node, i);
  814. unmute_input(codec, node, i);
  815. unmute_output(codec, node);
  816. return err;
  817. }
  818. }
  819. return 0;
  820. }
  821. /*
  822. * parse the tree and build the loopback controls
  823. */
  824. static int build_loopback_controls(struct hda_codec *codec)
  825. {
  826. struct hda_gspec *spec = codec->spec;
  827. struct hda_gnode *node;
  828. int err;
  829. const char *type;
  830. if (! spec->out_pin_node[0])
  831. return 0;
  832. list_for_each_entry(node, &spec->nid_list, list) {
  833. if (node->type != AC_WID_PIN)
  834. continue;
  835. /* input capable? */
  836. if (! (node->pin_caps & AC_PINCAP_IN))
  837. return 0;
  838. type = get_input_type(node, NULL);
  839. if (type) {
  840. if (check_existing_control(codec, type, "Playback"))
  841. continue;
  842. clear_check_flags(spec);
  843. err = parse_loopback_path(codec, spec,
  844. spec->out_pin_node[0],
  845. node, type);
  846. if (err < 0)
  847. return err;
  848. if (! err)
  849. continue;
  850. }
  851. }
  852. return 0;
  853. }
  854. /*
  855. * build mixer controls
  856. */
  857. static int build_generic_controls(struct hda_codec *codec)
  858. {
  859. int err;
  860. if ((err = build_input_controls(codec)) < 0 ||
  861. (err = build_output_controls(codec)) < 0 ||
  862. (err = build_loopback_controls(codec)) < 0)
  863. return err;
  864. return 0;
  865. }
  866. /*
  867. * PCM
  868. */
  869. static struct hda_pcm_stream generic_pcm_playback = {
  870. .substreams = 1,
  871. .channels_min = 2,
  872. .channels_max = 2,
  873. };
  874. static int generic_pcm2_prepare(struct hda_pcm_stream *hinfo,
  875. struct hda_codec *codec,
  876. unsigned int stream_tag,
  877. unsigned int format,
  878. struct snd_pcm_substream *substream)
  879. {
  880. struct hda_gspec *spec = codec->spec;
  881. snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
  882. snd_hda_codec_setup_stream(codec, spec->dac_node[1]->nid,
  883. stream_tag, 0, format);
  884. return 0;
  885. }
  886. static int generic_pcm2_cleanup(struct hda_pcm_stream *hinfo,
  887. struct hda_codec *codec,
  888. struct snd_pcm_substream *substream)
  889. {
  890. struct hda_gspec *spec = codec->spec;
  891. snd_hda_codec_cleanup_stream(codec, hinfo->nid);
  892. snd_hda_codec_cleanup_stream(codec, spec->dac_node[1]->nid);
  893. return 0;
  894. }
  895. static int build_generic_pcms(struct hda_codec *codec)
  896. {
  897. struct hda_gspec *spec = codec->spec;
  898. struct hda_pcm *info = &spec->pcm_rec;
  899. if (! spec->dac_node[0] && ! spec->adc_node) {
  900. snd_printd("hda_generic: no PCM found\n");
  901. return 0;
  902. }
  903. codec->num_pcms = 1;
  904. codec->pcm_info = info;
  905. info->name = "HDA Generic";
  906. if (spec->dac_node[0]) {
  907. info->stream[0] = generic_pcm_playback;
  908. info->stream[0].nid = spec->dac_node[0]->nid;
  909. if (spec->dac_node[1]) {
  910. info->stream[0].ops.prepare = generic_pcm2_prepare;
  911. info->stream[0].ops.cleanup = generic_pcm2_cleanup;
  912. }
  913. }
  914. if (spec->adc_node) {
  915. info->stream[1] = generic_pcm_playback;
  916. info->stream[1].nid = spec->adc_node->nid;
  917. }
  918. return 0;
  919. }
  920. #ifdef CONFIG_SND_HDA_POWER_SAVE
  921. static int generic_check_power_status(struct hda_codec *codec, hda_nid_t nid)
  922. {
  923. struct hda_gspec *spec = codec->spec;
  924. return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
  925. }
  926. #endif
  927. /*
  928. */
  929. static struct hda_codec_ops generic_patch_ops = {
  930. .build_controls = build_generic_controls,
  931. .build_pcms = build_generic_pcms,
  932. .free = snd_hda_generic_free,
  933. #ifdef CONFIG_SND_HDA_POWER_SAVE
  934. .check_power_status = generic_check_power_status,
  935. #endif
  936. };
  937. /*
  938. * the generic parser
  939. */
  940. int snd_hda_parse_generic_codec(struct hda_codec *codec)
  941. {
  942. struct hda_gspec *spec;
  943. int err;
  944. if(!codec->afg)
  945. return 0;
  946. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  947. if (spec == NULL) {
  948. printk(KERN_ERR "hda_generic: can't allocate spec\n");
  949. return -ENOMEM;
  950. }
  951. codec->spec = spec;
  952. INIT_LIST_HEAD(&spec->nid_list);
  953. if ((err = build_afg_tree(codec)) < 0)
  954. goto error;
  955. if ((err = parse_input(codec)) < 0 ||
  956. (err = parse_output(codec)) < 0)
  957. goto error;
  958. codec->patch_ops = generic_patch_ops;
  959. return 0;
  960. error:
  961. snd_hda_generic_free(codec);
  962. return err;
  963. }
  964. EXPORT_SYMBOL(snd_hda_parse_generic_codec);