hda_generic.c 28 KB

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