hda_generic.c 28 KB

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