hda_generic.c 28 KB

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