hda_generic.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086
  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 <linux/export.h>
  25. #include <sound/core.h>
  26. #include "hda_codec.h"
  27. #include "hda_local.h"
  28. /* widget node for parsing */
  29. struct hda_gnode {
  30. hda_nid_t nid; /* NID of this widget */
  31. unsigned short nconns; /* number of input connections */
  32. hda_nid_t *conn_list;
  33. hda_nid_t slist[2]; /* temporay list */
  34. unsigned int wid_caps; /* widget capabilities */
  35. unsigned char type; /* widget type */
  36. unsigned char pin_ctl; /* pin controls */
  37. unsigned char checked; /* the flag indicates that the node is already parsed */
  38. unsigned int pin_caps; /* pin widget capabilities */
  39. unsigned int def_cfg; /* default configuration */
  40. unsigned int amp_out_caps; /* AMP out capabilities */
  41. unsigned int amp_in_caps; /* AMP in capabilities */
  42. struct list_head list;
  43. };
  44. /* patch-specific record */
  45. #define MAX_PCM_VOLS 2
  46. struct pcm_vol {
  47. struct hda_gnode *node; /* Node for PCM volume */
  48. unsigned int index; /* connection of PCM volume */
  49. };
  50. struct hda_gspec {
  51. struct hda_gnode *dac_node[2]; /* DAC node */
  52. struct hda_gnode *out_pin_node[2]; /* Output pin (Line-Out) node */
  53. struct pcm_vol pcm_vol[MAX_PCM_VOLS]; /* PCM volumes */
  54. unsigned int pcm_vol_nodes; /* number of PCM volumes */
  55. struct hda_gnode *adc_node; /* ADC node */
  56. struct hda_gnode *cap_vol_node; /* Node for capture volume */
  57. unsigned int cur_cap_src; /* current capture source */
  58. struct hda_input_mux input_mux;
  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, int idx)
  460. {
  461. int i, err;
  462. unsigned int pinctl;
  463. const char *type;
  464. if (node->checked)
  465. return 0;
  466. node->checked = 1;
  467. if (node->type != AC_WID_PIN) {
  468. for (i = 0; i < node->nconns; i++) {
  469. struct hda_gnode *child;
  470. child = hda_get_node(spec, node->conn_list[i]);
  471. if (! child)
  472. continue;
  473. err = parse_adc_sub_nodes(codec, spec, child, idx);
  474. if (err < 0)
  475. return err;
  476. if (err > 0) {
  477. /* found one,
  478. * select the path, unmute both input and output
  479. */
  480. if (node->nconns > 1)
  481. select_input_connection(codec, node, i);
  482. unmute_input(codec, node, i);
  483. unmute_output(codec, node);
  484. return err;
  485. }
  486. }
  487. return 0;
  488. }
  489. /* input capable? */
  490. if (! (node->pin_caps & AC_PINCAP_IN))
  491. return 0;
  492. if (defcfg_port_conn(node) == AC_JACK_PORT_NONE)
  493. return 0; /* unconnected */
  494. if (node->wid_caps & AC_WCAP_DIGITAL)
  495. return 0; /* skip SPDIF */
  496. if (spec->input_mux.num_items >= HDA_MAX_NUM_INPUTS) {
  497. snd_printk(KERN_ERR "hda_generic: Too many items for capture\n");
  498. return -EINVAL;
  499. }
  500. pinctl = AC_PINCTL_IN_EN;
  501. /* create a proper capture source label */
  502. type = get_input_type(node, &pinctl);
  503. if (! type) {
  504. /* input as default? */
  505. if (! (node->pin_ctl & AC_PINCTL_IN_EN))
  506. return 0;
  507. type = "Input";
  508. }
  509. snd_hda_add_imux_item(&spec->input_mux, type, idx, NULL);
  510. /* unmute the PIN external input */
  511. unmute_input(codec, node, 0); /* index = 0? */
  512. /* set PIN-In enable */
  513. snd_hda_codec_write_cache(codec, node->nid, 0,
  514. AC_VERB_SET_PIN_WIDGET_CONTROL, pinctl);
  515. return 1; /* found */
  516. }
  517. /*
  518. * parse input
  519. */
  520. static int parse_input_path(struct hda_codec *codec, struct hda_gnode *adc_node)
  521. {
  522. struct hda_gspec *spec = codec->spec;
  523. struct hda_gnode *node;
  524. int i, err;
  525. snd_printdd("AUD_IN = %x\n", adc_node->nid);
  526. clear_check_flags(spec);
  527. // awk added - fixed no recording due to muted widget
  528. unmute_input(codec, adc_node, 0);
  529. /*
  530. * check each connection of the ADC
  531. * if it reaches to a proper input PIN, add the path as the
  532. * input path.
  533. */
  534. /* first, check the direct connections to PIN widgets */
  535. for (i = 0; i < adc_node->nconns; i++) {
  536. node = hda_get_node(spec, adc_node->conn_list[i]);
  537. if (node && node->type == AC_WID_PIN) {
  538. err = parse_adc_sub_nodes(codec, spec, node, i);
  539. if (err < 0)
  540. return err;
  541. }
  542. }
  543. /* ... then check the rests, more complicated connections */
  544. for (i = 0; i < adc_node->nconns; i++) {
  545. node = hda_get_node(spec, adc_node->conn_list[i]);
  546. if (node && node->type != AC_WID_PIN) {
  547. err = parse_adc_sub_nodes(codec, spec, node, i);
  548. if (err < 0)
  549. return err;
  550. }
  551. }
  552. if (! spec->input_mux.num_items)
  553. return 0; /* no input path found... */
  554. snd_printdd("[Capture Source] NID=0x%x, #SRC=%d\n", adc_node->nid, spec->input_mux.num_items);
  555. for (i = 0; i < spec->input_mux.num_items; i++)
  556. snd_printdd(" [%s] IDX=0x%x\n", spec->input_mux.items[i].label,
  557. spec->input_mux.items[i].index);
  558. spec->adc_node = adc_node;
  559. return 1;
  560. }
  561. /*
  562. * parse input
  563. */
  564. static int parse_input(struct hda_codec *codec)
  565. {
  566. struct hda_gspec *spec = codec->spec;
  567. struct hda_gnode *node;
  568. int err;
  569. /*
  570. * At first we look for an audio input widget.
  571. * If it reaches to certain input PINs, we take it as the
  572. * input path.
  573. */
  574. list_for_each_entry(node, &spec->nid_list, list) {
  575. if (node->wid_caps & AC_WCAP_DIGITAL)
  576. continue; /* skip SPDIF */
  577. if (node->type == AC_WID_AUD_IN) {
  578. err = parse_input_path(codec, node);
  579. if (err < 0)
  580. return err;
  581. else if (err > 0)
  582. return 0;
  583. }
  584. }
  585. snd_printd("hda_generic: no proper input path found\n");
  586. return 0;
  587. }
  588. #ifdef CONFIG_SND_HDA_POWER_SAVE
  589. static void add_input_loopback(struct hda_codec *codec, hda_nid_t nid,
  590. int dir, int idx)
  591. {
  592. struct hda_gspec *spec = codec->spec;
  593. struct hda_amp_list *p;
  594. if (spec->num_loopbacks >= MAX_LOOPBACK_AMPS) {
  595. snd_printk(KERN_ERR "hda_generic: Too many loopback ctls\n");
  596. return;
  597. }
  598. p = &spec->loopback_list[spec->num_loopbacks++];
  599. p->nid = nid;
  600. p->dir = dir;
  601. p->idx = idx;
  602. spec->loopback.amplist = spec->loopback_list;
  603. }
  604. #else
  605. #define add_input_loopback(codec,nid,dir,idx)
  606. #endif
  607. /*
  608. * create mixer controls if possible
  609. */
  610. static int create_mixer(struct hda_codec *codec, struct hda_gnode *node,
  611. unsigned int index, const char *type,
  612. const char *dir_sfx, int is_loopback)
  613. {
  614. char name[32];
  615. int err;
  616. int created = 0;
  617. struct snd_kcontrol_new knew;
  618. if (type)
  619. sprintf(name, "%s %s Switch", type, dir_sfx);
  620. else
  621. sprintf(name, "%s Switch", dir_sfx);
  622. if ((node->wid_caps & AC_WCAP_IN_AMP) &&
  623. (node->amp_in_caps & AC_AMPCAP_MUTE)) {
  624. knew = (struct snd_kcontrol_new)HDA_CODEC_MUTE(name, node->nid, index, HDA_INPUT);
  625. if (is_loopback)
  626. add_input_loopback(codec, node->nid, HDA_INPUT, index);
  627. snd_printdd("[%s] NID=0x%x, DIR=IN, IDX=0x%x\n", name, node->nid, index);
  628. err = snd_hda_ctl_add(codec, node->nid,
  629. snd_ctl_new1(&knew, codec));
  630. if (err < 0)
  631. return err;
  632. created = 1;
  633. } else if ((node->wid_caps & AC_WCAP_OUT_AMP) &&
  634. (node->amp_out_caps & AC_AMPCAP_MUTE)) {
  635. knew = (struct snd_kcontrol_new)HDA_CODEC_MUTE(name, node->nid, 0, HDA_OUTPUT);
  636. if (is_loopback)
  637. add_input_loopback(codec, node->nid, HDA_OUTPUT, 0);
  638. snd_printdd("[%s] NID=0x%x, DIR=OUT\n", name, node->nid);
  639. err = snd_hda_ctl_add(codec, node->nid,
  640. snd_ctl_new1(&knew, codec));
  641. if (err < 0)
  642. return err;
  643. created = 1;
  644. }
  645. if (type)
  646. sprintf(name, "%s %s Volume", type, dir_sfx);
  647. else
  648. sprintf(name, "%s Volume", dir_sfx);
  649. if ((node->wid_caps & AC_WCAP_IN_AMP) &&
  650. (node->amp_in_caps & AC_AMPCAP_NUM_STEPS)) {
  651. knew = (struct snd_kcontrol_new)HDA_CODEC_VOLUME(name, node->nid, index, HDA_INPUT);
  652. snd_printdd("[%s] NID=0x%x, DIR=IN, IDX=0x%x\n", name, node->nid, index);
  653. err = snd_hda_ctl_add(codec, node->nid,
  654. snd_ctl_new1(&knew, codec));
  655. if (err < 0)
  656. return err;
  657. created = 1;
  658. } else if ((node->wid_caps & AC_WCAP_OUT_AMP) &&
  659. (node->amp_out_caps & AC_AMPCAP_NUM_STEPS)) {
  660. knew = (struct snd_kcontrol_new)HDA_CODEC_VOLUME(name, node->nid, 0, HDA_OUTPUT);
  661. snd_printdd("[%s] NID=0x%x, DIR=OUT\n", name, node->nid);
  662. err = snd_hda_ctl_add(codec, node->nid,
  663. snd_ctl_new1(&knew, codec));
  664. if (err < 0)
  665. return err;
  666. created = 1;
  667. }
  668. return created;
  669. }
  670. /*
  671. * check whether the controls with the given name and direction suffix already exist
  672. */
  673. static int check_existing_control(struct hda_codec *codec, const char *type, const char *dir)
  674. {
  675. struct snd_ctl_elem_id id;
  676. memset(&id, 0, sizeof(id));
  677. sprintf(id.name, "%s %s Volume", type, dir);
  678. id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  679. if (snd_ctl_find_id(codec->bus->card, &id))
  680. return 1;
  681. sprintf(id.name, "%s %s Switch", type, dir);
  682. id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  683. if (snd_ctl_find_id(codec->bus->card, &id))
  684. return 1;
  685. return 0;
  686. }
  687. /*
  688. * build output mixer controls
  689. */
  690. static int create_output_mixers(struct hda_codec *codec,
  691. const char * const *names)
  692. {
  693. struct hda_gspec *spec = codec->spec;
  694. int i, err;
  695. for (i = 0; i < spec->pcm_vol_nodes; i++) {
  696. err = create_mixer(codec, spec->pcm_vol[i].node,
  697. spec->pcm_vol[i].index,
  698. names[i], "Playback", 0);
  699. if (err < 0)
  700. return err;
  701. }
  702. return 0;
  703. }
  704. static int build_output_controls(struct hda_codec *codec)
  705. {
  706. struct hda_gspec *spec = codec->spec;
  707. static const char * const types_speaker[] = { "Speaker", "Headphone" };
  708. static const char * const types_line[] = { "Front", "Headphone" };
  709. switch (spec->pcm_vol_nodes) {
  710. case 1:
  711. return create_mixer(codec, spec->pcm_vol[0].node,
  712. spec->pcm_vol[0].index,
  713. "Master", "Playback", 0);
  714. case 2:
  715. if (defcfg_type(spec->out_pin_node[0]) == AC_JACK_SPEAKER)
  716. return create_output_mixers(codec, types_speaker);
  717. else
  718. return create_output_mixers(codec, types_line);
  719. }
  720. return 0;
  721. }
  722. /* create capture volume/switch */
  723. static int build_input_controls(struct hda_codec *codec)
  724. {
  725. struct hda_gspec *spec = codec->spec;
  726. struct hda_gnode *adc_node = spec->adc_node;
  727. int i, err;
  728. static struct snd_kcontrol_new cap_sel = {
  729. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  730. .name = "Capture Source",
  731. .info = capture_source_info,
  732. .get = capture_source_get,
  733. .put = capture_source_put,
  734. };
  735. if (! adc_node || ! spec->input_mux.num_items)
  736. return 0; /* not found */
  737. spec->cur_cap_src = 0;
  738. select_input_connection(codec, adc_node,
  739. spec->input_mux.items[0].index);
  740. /* create capture volume and switch controls if the ADC has an amp */
  741. /* do we have only a single item? */
  742. if (spec->input_mux.num_items == 1) {
  743. err = create_mixer(codec, adc_node,
  744. spec->input_mux.items[0].index,
  745. NULL, "Capture", 0);
  746. if (err < 0)
  747. return err;
  748. return 0;
  749. }
  750. /* create input MUX if multiple sources are available */
  751. err = snd_hda_ctl_add(codec, spec->adc_node->nid,
  752. snd_ctl_new1(&cap_sel, codec));
  753. if (err < 0)
  754. return err;
  755. /* no volume control? */
  756. if (! (adc_node->wid_caps & AC_WCAP_IN_AMP) ||
  757. ! (adc_node->amp_in_caps & AC_AMPCAP_NUM_STEPS))
  758. return 0;
  759. for (i = 0; i < spec->input_mux.num_items; i++) {
  760. struct snd_kcontrol_new knew;
  761. char name[32];
  762. sprintf(name, "%s Capture Volume",
  763. spec->input_mux.items[i].label);
  764. knew = (struct snd_kcontrol_new)
  765. HDA_CODEC_VOLUME(name, adc_node->nid,
  766. spec->input_mux.items[i].index,
  767. HDA_INPUT);
  768. err = snd_hda_ctl_add(codec, adc_node->nid,
  769. snd_ctl_new1(&knew, codec));
  770. if (err < 0)
  771. return err;
  772. }
  773. return 0;
  774. }
  775. /*
  776. * parse the nodes recursively until reach to the output PIN.
  777. *
  778. * returns 0 - if not found,
  779. * 1 - if found, but no mixer is created
  780. * 2 - if found and mixer was already created, (just skip)
  781. * a negative error code
  782. */
  783. static int parse_loopback_path(struct hda_codec *codec, struct hda_gspec *spec,
  784. struct hda_gnode *node, struct hda_gnode *dest_node,
  785. const char *type)
  786. {
  787. int i, err;
  788. if (node->checked)
  789. return 0;
  790. node->checked = 1;
  791. if (node == dest_node) {
  792. /* loopback connection found */
  793. return 1;
  794. }
  795. for (i = 0; i < node->nconns; i++) {
  796. struct hda_gnode *child = hda_get_node(spec, node->conn_list[i]);
  797. if (! child)
  798. continue;
  799. err = parse_loopback_path(codec, spec, child, dest_node, type);
  800. if (err < 0)
  801. return err;
  802. else if (err >= 1) {
  803. if (err == 1) {
  804. err = create_mixer(codec, node, i, type,
  805. "Playback", 1);
  806. if (err < 0)
  807. return err;
  808. if (err > 0)
  809. return 2; /* ok, created */
  810. /* not created, maybe in the lower path */
  811. err = 1;
  812. }
  813. /* connect and unmute */
  814. if (node->nconns > 1)
  815. select_input_connection(codec, node, i);
  816. unmute_input(codec, node, i);
  817. unmute_output(codec, node);
  818. return err;
  819. }
  820. }
  821. return 0;
  822. }
  823. /*
  824. * parse the tree and build the loopback controls
  825. */
  826. static int build_loopback_controls(struct hda_codec *codec)
  827. {
  828. struct hda_gspec *spec = codec->spec;
  829. struct hda_gnode *node;
  830. int err;
  831. const char *type;
  832. if (! spec->out_pin_node[0])
  833. return 0;
  834. list_for_each_entry(node, &spec->nid_list, list) {
  835. if (node->type != AC_WID_PIN)
  836. continue;
  837. /* input capable? */
  838. if (! (node->pin_caps & AC_PINCAP_IN))
  839. return 0;
  840. type = get_input_type(node, NULL);
  841. if (type) {
  842. if (check_existing_control(codec, type, "Playback"))
  843. continue;
  844. clear_check_flags(spec);
  845. err = parse_loopback_path(codec, spec,
  846. spec->out_pin_node[0],
  847. node, type);
  848. if (err < 0)
  849. return err;
  850. if (! err)
  851. continue;
  852. }
  853. }
  854. return 0;
  855. }
  856. /*
  857. * build mixer controls
  858. */
  859. static int build_generic_controls(struct hda_codec *codec)
  860. {
  861. int err;
  862. if ((err = build_input_controls(codec)) < 0 ||
  863. (err = build_output_controls(codec)) < 0 ||
  864. (err = build_loopback_controls(codec)) < 0)
  865. return err;
  866. return 0;
  867. }
  868. /*
  869. * PCM
  870. */
  871. static struct hda_pcm_stream generic_pcm_playback = {
  872. .substreams = 1,
  873. .channels_min = 2,
  874. .channels_max = 2,
  875. };
  876. static int generic_pcm2_prepare(struct hda_pcm_stream *hinfo,
  877. struct hda_codec *codec,
  878. unsigned int stream_tag,
  879. unsigned int format,
  880. struct snd_pcm_substream *substream)
  881. {
  882. struct hda_gspec *spec = codec->spec;
  883. snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
  884. snd_hda_codec_setup_stream(codec, spec->dac_node[1]->nid,
  885. stream_tag, 0, format);
  886. return 0;
  887. }
  888. static int generic_pcm2_cleanup(struct hda_pcm_stream *hinfo,
  889. struct hda_codec *codec,
  890. struct snd_pcm_substream *substream)
  891. {
  892. struct hda_gspec *spec = codec->spec;
  893. snd_hda_codec_cleanup_stream(codec, hinfo->nid);
  894. snd_hda_codec_cleanup_stream(codec, spec->dac_node[1]->nid);
  895. return 0;
  896. }
  897. static int build_generic_pcms(struct hda_codec *codec)
  898. {
  899. struct hda_gspec *spec = codec->spec;
  900. struct hda_pcm *info = &spec->pcm_rec;
  901. if (! spec->dac_node[0] && ! spec->adc_node) {
  902. snd_printd("hda_generic: no PCM found\n");
  903. return 0;
  904. }
  905. codec->num_pcms = 1;
  906. codec->pcm_info = info;
  907. info->name = "HDA Generic";
  908. if (spec->dac_node[0]) {
  909. info->stream[0] = generic_pcm_playback;
  910. info->stream[0].nid = spec->dac_node[0]->nid;
  911. if (spec->dac_node[1]) {
  912. info->stream[0].ops.prepare = generic_pcm2_prepare;
  913. info->stream[0].ops.cleanup = generic_pcm2_cleanup;
  914. }
  915. }
  916. if (spec->adc_node) {
  917. info->stream[1] = generic_pcm_playback;
  918. info->stream[1].nid = spec->adc_node->nid;
  919. }
  920. return 0;
  921. }
  922. #ifdef CONFIG_SND_HDA_POWER_SAVE
  923. static int generic_check_power_status(struct hda_codec *codec, hda_nid_t nid)
  924. {
  925. struct hda_gspec *spec = codec->spec;
  926. return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
  927. }
  928. #endif
  929. /*
  930. */
  931. static struct hda_codec_ops generic_patch_ops = {
  932. .build_controls = build_generic_controls,
  933. .build_pcms = build_generic_pcms,
  934. .free = snd_hda_generic_free,
  935. #ifdef CONFIG_SND_HDA_POWER_SAVE
  936. .check_power_status = generic_check_power_status,
  937. #endif
  938. };
  939. /*
  940. * the generic parser
  941. */
  942. int snd_hda_parse_generic_codec(struct hda_codec *codec)
  943. {
  944. struct hda_gspec *spec;
  945. int err;
  946. if(!codec->afg)
  947. return 0;
  948. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  949. if (spec == NULL) {
  950. printk(KERN_ERR "hda_generic: can't allocate spec\n");
  951. return -ENOMEM;
  952. }
  953. codec->spec = spec;
  954. INIT_LIST_HEAD(&spec->nid_list);
  955. if ((err = build_afg_tree(codec)) < 0)
  956. goto error;
  957. if ((err = parse_input(codec)) < 0 ||
  958. (err = parse_output(codec)) < 0)
  959. goto error;
  960. codec->patch_ops = generic_patch_ops;
  961. return 0;
  962. error:
  963. snd_hda_generic_free(codec);
  964. return err;
  965. }
  966. EXPORT_SYMBOL(snd_hda_parse_generic_codec);