hda_codec.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857
  1. /*
  2. * Universal Interface for Intel High Definition Audio Codec
  3. *
  4. * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
  5. *
  6. *
  7. * This driver is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This driver is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <sound/driver.h>
  22. #include <linux/init.h>
  23. #include <linux/delay.h>
  24. #include <linux/slab.h>
  25. #include <linux/pci.h>
  26. #include <linux/moduleparam.h>
  27. #include <sound/core.h>
  28. #include "hda_codec.h"
  29. #include <sound/asoundef.h>
  30. #include <sound/initval.h>
  31. #include "hda_local.h"
  32. MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
  33. MODULE_DESCRIPTION("Universal interface for High Definition Audio Codec");
  34. MODULE_LICENSE("GPL");
  35. /*
  36. * vendor / preset table
  37. */
  38. struct hda_vendor_id {
  39. unsigned int id;
  40. const char *name;
  41. };
  42. /* codec vendor labels */
  43. static struct hda_vendor_id hda_vendor_ids[] = {
  44. { 0x10ec, "Realtek" },
  45. { 0x13f6, "C-Media" },
  46. { 0x434d, "C-Media" },
  47. { 0x8384, "SigmaTel" },
  48. {} /* terminator */
  49. };
  50. /* codec presets */
  51. #include "hda_patch.h"
  52. /**
  53. * snd_hda_codec_read - send a command and get the response
  54. * @codec: the HDA codec
  55. * @nid: NID to send the command
  56. * @direct: direct flag
  57. * @verb: the verb to send
  58. * @parm: the parameter for the verb
  59. *
  60. * Send a single command and read the corresponding response.
  61. *
  62. * Returns the obtained response value, or -1 for an error.
  63. */
  64. unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid, int direct,
  65. unsigned int verb, unsigned int parm)
  66. {
  67. unsigned int res;
  68. down(&codec->bus->cmd_mutex);
  69. if (! codec->bus->ops.command(codec, nid, direct, verb, parm))
  70. res = codec->bus->ops.get_response(codec);
  71. else
  72. res = (unsigned int)-1;
  73. up(&codec->bus->cmd_mutex);
  74. return res;
  75. }
  76. /**
  77. * snd_hda_codec_write - send a single command without waiting for response
  78. * @codec: the HDA codec
  79. * @nid: NID to send the command
  80. * @direct: direct flag
  81. * @verb: the verb to send
  82. * @parm: the parameter for the verb
  83. *
  84. * Send a single command without waiting for response.
  85. *
  86. * Returns 0 if successful, or a negative error code.
  87. */
  88. int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
  89. unsigned int verb, unsigned int parm)
  90. {
  91. int err;
  92. down(&codec->bus->cmd_mutex);
  93. err = codec->bus->ops.command(codec, nid, direct, verb, parm);
  94. up(&codec->bus->cmd_mutex);
  95. return err;
  96. }
  97. /**
  98. * snd_hda_sequence_write - sequence writes
  99. * @codec: the HDA codec
  100. * @seq: VERB array to send
  101. *
  102. * Send the commands sequentially from the given array.
  103. * The array must be terminated with NID=0.
  104. */
  105. void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
  106. {
  107. for (; seq->nid; seq++)
  108. snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
  109. }
  110. /**
  111. * snd_hda_get_sub_nodes - get the range of sub nodes
  112. * @codec: the HDA codec
  113. * @nid: NID to parse
  114. * @start_id: the pointer to store the start NID
  115. *
  116. * Parse the NID and store the start NID of its sub-nodes.
  117. * Returns the number of sub-nodes.
  118. */
  119. int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid, hda_nid_t *start_id)
  120. {
  121. unsigned int parm;
  122. parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
  123. *start_id = (parm >> 16) & 0x7fff;
  124. return (int)(parm & 0x7fff);
  125. }
  126. /**
  127. * snd_hda_get_connections - get connection list
  128. * @codec: the HDA codec
  129. * @nid: NID to parse
  130. * @conn_list: connection list array
  131. * @max_conns: max. number of connections to store
  132. *
  133. * Parses the connection list of the given widget and stores the list
  134. * of NIDs.
  135. *
  136. * Returns the number of connections, or a negative error code.
  137. */
  138. int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
  139. hda_nid_t *conn_list, int max_conns)
  140. {
  141. unsigned int parm;
  142. int i, j, conn_len, num_tupples, conns;
  143. unsigned int shift, num_elems, mask;
  144. snd_assert(conn_list && max_conns > 0, return -EINVAL);
  145. parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
  146. if (parm & AC_CLIST_LONG) {
  147. /* long form */
  148. shift = 16;
  149. num_elems = 2;
  150. } else {
  151. /* short form */
  152. shift = 8;
  153. num_elems = 4;
  154. }
  155. conn_len = parm & AC_CLIST_LENGTH;
  156. num_tupples = num_elems / 2;
  157. mask = (1 << (shift-1)) - 1;
  158. if (! conn_len)
  159. return 0; /* no connection */
  160. if (conn_len == 1) {
  161. /* single connection */
  162. parm = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONNECT_LIST, 0);
  163. conn_list[0] = parm & mask;
  164. return 1;
  165. }
  166. /* multi connection */
  167. conns = 0;
  168. for (i = 0; i < conn_len; i += num_elems) {
  169. parm = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONNECT_LIST, i);
  170. for (j = 0; j < num_tupples; j++) {
  171. int range_val;
  172. hda_nid_t val1, val2, n;
  173. range_val = parm & (1 << (shift-1)); /* ranges */
  174. val1 = parm & mask;
  175. parm >>= shift;
  176. val2 = parm & mask;
  177. parm >>= shift;
  178. if (range_val) {
  179. /* ranges between val1 and val2 */
  180. if (val1 > val2) {
  181. snd_printk(KERN_WARNING "hda_codec: invalid dep_range_val %x:%x\n", val1, val2);
  182. continue;
  183. }
  184. for (n = val1; n <= val2; n++) {
  185. if (conns >= max_conns)
  186. return -EINVAL;
  187. conn_list[conns++] = n;
  188. }
  189. } else {
  190. if (! val1)
  191. break;
  192. if (conns >= max_conns)
  193. return -EINVAL;
  194. conn_list[conns++] = val1;
  195. if (! val2)
  196. break;
  197. if (conns >= max_conns)
  198. return -EINVAL;
  199. conn_list[conns++] = val2;
  200. }
  201. }
  202. }
  203. return conns;
  204. }
  205. /**
  206. * snd_hda_queue_unsol_event - add an unsolicited event to queue
  207. * @bus: the BUS
  208. * @res: unsolicited event (lower 32bit of RIRB entry)
  209. * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
  210. *
  211. * Adds the given event to the queue. The events are processed in
  212. * the workqueue asynchronously. Call this function in the interrupt
  213. * hanlder when RIRB receives an unsolicited event.
  214. *
  215. * Returns 0 if successful, or a negative error code.
  216. */
  217. int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
  218. {
  219. struct hda_bus_unsolicited *unsol;
  220. unsigned int wp;
  221. if ((unsol = bus->unsol) == NULL)
  222. return 0;
  223. wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
  224. unsol->wp = wp;
  225. wp <<= 1;
  226. unsol->queue[wp] = res;
  227. unsol->queue[wp + 1] = res_ex;
  228. queue_work(unsol->workq, &unsol->work);
  229. return 0;
  230. }
  231. /*
  232. * process queueud unsolicited events
  233. */
  234. static void process_unsol_events(void *data)
  235. {
  236. struct hda_bus *bus = data;
  237. struct hda_bus_unsolicited *unsol = bus->unsol;
  238. struct hda_codec *codec;
  239. unsigned int rp, caddr, res;
  240. while (unsol->rp != unsol->wp) {
  241. rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
  242. unsol->rp = rp;
  243. rp <<= 1;
  244. res = unsol->queue[rp];
  245. caddr = unsol->queue[rp + 1];
  246. if (! (caddr & (1 << 4))) /* no unsolicited event? */
  247. continue;
  248. codec = bus->caddr_tbl[caddr & 0x0f];
  249. if (codec && codec->patch_ops.unsol_event)
  250. codec->patch_ops.unsol_event(codec, res);
  251. }
  252. }
  253. /*
  254. * initialize unsolicited queue
  255. */
  256. static int init_unsol_queue(struct hda_bus *bus)
  257. {
  258. struct hda_bus_unsolicited *unsol;
  259. unsol = kcalloc(1, sizeof(*unsol), GFP_KERNEL);
  260. if (! unsol) {
  261. snd_printk(KERN_ERR "hda_codec: can't allocate unsolicited queue\n");
  262. return -ENOMEM;
  263. }
  264. unsol->workq = create_workqueue("hda_codec");
  265. if (! unsol->workq) {
  266. snd_printk(KERN_ERR "hda_codec: can't create workqueue\n");
  267. kfree(unsol);
  268. return -ENOMEM;
  269. }
  270. INIT_WORK(&unsol->work, process_unsol_events, bus);
  271. bus->unsol = unsol;
  272. return 0;
  273. }
  274. /*
  275. * destructor
  276. */
  277. static void snd_hda_codec_free(struct hda_codec *codec);
  278. static int snd_hda_bus_free(struct hda_bus *bus)
  279. {
  280. struct list_head *p, *n;
  281. if (! bus)
  282. return 0;
  283. if (bus->unsol) {
  284. destroy_workqueue(bus->unsol->workq);
  285. kfree(bus->unsol);
  286. }
  287. list_for_each_safe(p, n, &bus->codec_list) {
  288. struct hda_codec *codec = list_entry(p, struct hda_codec, list);
  289. snd_hda_codec_free(codec);
  290. }
  291. if (bus->ops.private_free)
  292. bus->ops.private_free(bus);
  293. kfree(bus);
  294. return 0;
  295. }
  296. static int snd_hda_bus_dev_free(snd_device_t *device)
  297. {
  298. struct hda_bus *bus = device->device_data;
  299. return snd_hda_bus_free(bus);
  300. }
  301. /**
  302. * snd_hda_bus_new - create a HDA bus
  303. * @card: the card entry
  304. * @temp: the template for hda_bus information
  305. * @busp: the pointer to store the created bus instance
  306. *
  307. * Returns 0 if successful, or a negative error code.
  308. */
  309. int snd_hda_bus_new(snd_card_t *card, const struct hda_bus_template *temp,
  310. struct hda_bus **busp)
  311. {
  312. struct hda_bus *bus;
  313. int err;
  314. static snd_device_ops_t dev_ops = {
  315. .dev_free = snd_hda_bus_dev_free,
  316. };
  317. snd_assert(temp, return -EINVAL);
  318. snd_assert(temp->ops.command && temp->ops.get_response, return -EINVAL);
  319. if (busp)
  320. *busp = NULL;
  321. bus = kcalloc(1, sizeof(*bus), GFP_KERNEL);
  322. if (bus == NULL) {
  323. snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
  324. return -ENOMEM;
  325. }
  326. bus->card = card;
  327. bus->private_data = temp->private_data;
  328. bus->pci = temp->pci;
  329. bus->modelname = temp->modelname;
  330. bus->ops = temp->ops;
  331. init_MUTEX(&bus->cmd_mutex);
  332. INIT_LIST_HEAD(&bus->codec_list);
  333. init_unsol_queue(bus);
  334. if ((err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops)) < 0) {
  335. snd_hda_bus_free(bus);
  336. return err;
  337. }
  338. if (busp)
  339. *busp = bus;
  340. return 0;
  341. }
  342. /*
  343. * find a matching codec preset
  344. */
  345. static const struct hda_codec_preset *find_codec_preset(struct hda_codec *codec)
  346. {
  347. const struct hda_codec_preset **tbl, *preset;
  348. for (tbl = hda_preset_tables; *tbl; tbl++) {
  349. for (preset = *tbl; preset->id; preset++) {
  350. u32 mask = preset->mask;
  351. if (! mask)
  352. mask = ~0;
  353. if (preset->id == (codec->vendor_id & mask))
  354. return preset;
  355. }
  356. }
  357. return NULL;
  358. }
  359. /*
  360. * snd_hda_get_codec_name - store the codec name
  361. */
  362. void snd_hda_get_codec_name(struct hda_codec *codec,
  363. char *name, int namelen)
  364. {
  365. const struct hda_vendor_id *c;
  366. const char *vendor = NULL;
  367. u16 vendor_id = codec->vendor_id >> 16;
  368. char tmp[16];
  369. for (c = hda_vendor_ids; c->id; c++) {
  370. if (c->id == vendor_id) {
  371. vendor = c->name;
  372. break;
  373. }
  374. }
  375. if (! vendor) {
  376. sprintf(tmp, "Generic %04x", vendor_id);
  377. vendor = tmp;
  378. }
  379. if (codec->preset && codec->preset->name)
  380. snprintf(name, namelen, "%s %s", vendor, codec->preset->name);
  381. else
  382. snprintf(name, namelen, "%s ID %x", vendor, codec->vendor_id & 0xffff);
  383. }
  384. /*
  385. * look for an AFG node
  386. *
  387. * return 0 if not found
  388. */
  389. static int look_for_afg_node(struct hda_codec *codec)
  390. {
  391. int i, total_nodes;
  392. hda_nid_t nid;
  393. total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
  394. for (i = 0; i < total_nodes; i++, nid++) {
  395. if ((snd_hda_param_read(codec, nid, AC_PAR_FUNCTION_TYPE) & 0xff) ==
  396. AC_GRP_AUDIO_FUNCTION)
  397. return nid;
  398. }
  399. return 0;
  400. }
  401. /*
  402. * codec destructor
  403. */
  404. static void snd_hda_codec_free(struct hda_codec *codec)
  405. {
  406. if (! codec)
  407. return;
  408. list_del(&codec->list);
  409. codec->bus->caddr_tbl[codec->addr] = NULL;
  410. if (codec->patch_ops.free)
  411. codec->patch_ops.free(codec);
  412. kfree(codec);
  413. }
  414. static void init_amp_hash(struct hda_codec *codec);
  415. /**
  416. * snd_hda_codec_new - create a HDA codec
  417. * @bus: the bus to assign
  418. * @codec_addr: the codec address
  419. * @codecp: the pointer to store the generated codec
  420. *
  421. * Returns 0 if successful, or a negative error code.
  422. */
  423. int snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
  424. struct hda_codec **codecp)
  425. {
  426. struct hda_codec *codec;
  427. char component[13];
  428. int err;
  429. snd_assert(bus, return -EINVAL);
  430. snd_assert(codec_addr <= HDA_MAX_CODEC_ADDRESS, return -EINVAL);
  431. if (bus->caddr_tbl[codec_addr]) {
  432. snd_printk(KERN_ERR "hda_codec: address 0x%x is already occupied\n", codec_addr);
  433. return -EBUSY;
  434. }
  435. codec = kcalloc(1, sizeof(*codec), GFP_KERNEL);
  436. if (codec == NULL) {
  437. snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
  438. return -ENOMEM;
  439. }
  440. codec->bus = bus;
  441. codec->addr = codec_addr;
  442. init_MUTEX(&codec->spdif_mutex);
  443. init_amp_hash(codec);
  444. list_add_tail(&codec->list, &bus->codec_list);
  445. bus->caddr_tbl[codec_addr] = codec;
  446. codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, AC_PAR_VENDOR_ID);
  447. codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT, AC_PAR_SUBSYSTEM_ID);
  448. codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT, AC_PAR_REV_ID);
  449. /* FIXME: support for multiple AFGs? */
  450. codec->afg = look_for_afg_node(codec);
  451. if (! codec->afg) {
  452. snd_printk(KERN_ERR "hda_codec: no AFG node found\n");
  453. snd_hda_codec_free(codec);
  454. return -ENODEV;
  455. }
  456. codec->preset = find_codec_preset(codec);
  457. if (! *bus->card->mixername)
  458. snd_hda_get_codec_name(codec, bus->card->mixername,
  459. sizeof(bus->card->mixername));
  460. if (codec->preset && codec->preset->patch)
  461. err = codec->preset->patch(codec);
  462. else
  463. err = snd_hda_parse_generic_codec(codec);
  464. if (err < 0) {
  465. snd_hda_codec_free(codec);
  466. return err;
  467. }
  468. snd_hda_codec_proc_new(codec);
  469. sprintf(component, "HDA:%08x", codec->vendor_id);
  470. snd_component_add(codec->bus->card, component);
  471. if (codecp)
  472. *codecp = codec;
  473. return 0;
  474. }
  475. /**
  476. * snd_hda_codec_setup_stream - set up the codec for streaming
  477. * @codec: the CODEC to set up
  478. * @nid: the NID to set up
  479. * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
  480. * @channel_id: channel id to pass, zero based.
  481. * @format: stream format.
  482. */
  483. void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid, u32 stream_tag,
  484. int channel_id, int format)
  485. {
  486. snd_printdd("hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
  487. nid, stream_tag, channel_id, format);
  488. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
  489. (stream_tag << 4) | channel_id);
  490. msleep(1);
  491. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
  492. }
  493. /*
  494. * amp access functions
  495. */
  496. #define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + (idx) * 32 + (dir) * 64)
  497. #define INFO_AMP_CAPS (1<<0)
  498. #define INFO_AMP_VOL (1<<1)
  499. /* initialize the hash table */
  500. static void init_amp_hash(struct hda_codec *codec)
  501. {
  502. memset(codec->amp_hash, 0xff, sizeof(codec->amp_hash));
  503. codec->num_amp_entries = 0;
  504. }
  505. /* query the hash. allocate an entry if not found. */
  506. static struct hda_amp_info *get_alloc_amp_hash(struct hda_codec *codec, u32 key)
  507. {
  508. u16 idx = key % (u16)ARRAY_SIZE(codec->amp_hash);
  509. u16 cur = codec->amp_hash[idx];
  510. struct hda_amp_info *info;
  511. while (cur != 0xffff) {
  512. info = &codec->amp_info[cur];
  513. if (info->key == key)
  514. return info;
  515. cur = info->next;
  516. }
  517. /* add a new hash entry */
  518. if (codec->num_amp_entries >= ARRAY_SIZE(codec->amp_info)) {
  519. snd_printk(KERN_ERR "hda_codec: Tooooo many amps!\n");
  520. return NULL;
  521. }
  522. cur = codec->num_amp_entries++;
  523. info = &codec->amp_info[cur];
  524. info->key = key;
  525. info->status = 0; /* not initialized yet */
  526. info->next = codec->amp_hash[idx];
  527. codec->amp_hash[idx] = cur;
  528. return info;
  529. }
  530. /*
  531. * query AMP capabilities for the given widget and direction
  532. */
  533. static u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
  534. {
  535. struct hda_amp_info *info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
  536. if (! info)
  537. return 0;
  538. if (! (info->status & INFO_AMP_CAPS)) {
  539. if (!(snd_hda_param_read(codec, nid, AC_PAR_AUDIO_WIDGET_CAP) & AC_WCAP_AMP_OVRD))
  540. nid = codec->afg;
  541. info->amp_caps = snd_hda_param_read(codec, nid, direction == HDA_OUTPUT ?
  542. AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
  543. info->status |= INFO_AMP_CAPS;
  544. }
  545. return info->amp_caps;
  546. }
  547. /*
  548. * read the current volume to info
  549. * if the cache exists, read from the cache.
  550. */
  551. static void get_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
  552. hda_nid_t nid, int ch, int direction, int index)
  553. {
  554. u32 val, parm;
  555. if (info->status & (INFO_AMP_VOL << ch))
  556. return;
  557. parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
  558. parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
  559. parm |= index;
  560. val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_AMP_GAIN_MUTE, parm);
  561. info->vol[ch] = val & 0xff;
  562. info->status |= INFO_AMP_VOL << ch;
  563. }
  564. /*
  565. * write the current volume in info to the h/w
  566. */
  567. static void put_vol_mute(struct hda_codec *codec,
  568. hda_nid_t nid, int ch, int direction, int index, int val)
  569. {
  570. u32 parm;
  571. parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
  572. parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
  573. parm |= index << AC_AMP_SET_INDEX_SHIFT;
  574. parm |= val;
  575. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
  576. }
  577. /*
  578. * read/write AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
  579. */
  580. int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch, int direction, int index)
  581. {
  582. struct hda_amp_info *info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
  583. if (! info)
  584. return 0;
  585. get_vol_mute(codec, info, nid, ch, direction, index);
  586. return info->vol[ch];
  587. }
  588. int snd_hda_codec_amp_write(struct hda_codec *codec, hda_nid_t nid, int ch, int direction, int idx, int val)
  589. {
  590. struct hda_amp_info *info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
  591. if (! info)
  592. return 0;
  593. get_vol_mute(codec, info, nid, ch, direction, idx);
  594. if (info->vol[ch] == val && ! codec->in_resume)
  595. return 0;
  596. put_vol_mute(codec, nid, ch, direction, idx, val);
  597. info->vol[ch] = val;
  598. return 1;
  599. }
  600. /*
  601. * AMP control callbacks
  602. */
  603. /* retrieve parameters from private_value */
  604. #define get_amp_nid(kc) ((kc)->private_value & 0xffff)
  605. #define get_amp_channels(kc) (((kc)->private_value >> 16) & 0x3)
  606. #define get_amp_direction(kc) (((kc)->private_value >> 18) & 0x1)
  607. #define get_amp_index(kc) (((kc)->private_value >> 19) & 0xf)
  608. /* volume */
  609. int snd_hda_mixer_amp_volume_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
  610. {
  611. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  612. u16 nid = get_amp_nid(kcontrol);
  613. u8 chs = get_amp_channels(kcontrol);
  614. int dir = get_amp_direction(kcontrol);
  615. u32 caps;
  616. caps = query_amp_caps(codec, nid, dir);
  617. caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; /* num steps */
  618. if (! caps) {
  619. printk(KERN_WARNING "hda_codec: num_steps = 0 for NID=0x%x\n", nid);
  620. return -EINVAL;
  621. }
  622. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  623. uinfo->count = chs == 3 ? 2 : 1;
  624. uinfo->value.integer.min = 0;
  625. uinfo->value.integer.max = caps;
  626. return 0;
  627. }
  628. int snd_hda_mixer_amp_volume_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  629. {
  630. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  631. hda_nid_t nid = get_amp_nid(kcontrol);
  632. int chs = get_amp_channels(kcontrol);
  633. int dir = get_amp_direction(kcontrol);
  634. int idx = get_amp_index(kcontrol);
  635. long *valp = ucontrol->value.integer.value;
  636. if (chs & 1)
  637. *valp++ = snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 0x7f;
  638. if (chs & 2)
  639. *valp = snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 0x7f;
  640. return 0;
  641. }
  642. int snd_hda_mixer_amp_volume_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  643. {
  644. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  645. hda_nid_t nid = get_amp_nid(kcontrol);
  646. int chs = get_amp_channels(kcontrol);
  647. int dir = get_amp_direction(kcontrol);
  648. int idx = get_amp_index(kcontrol);
  649. int val;
  650. long *valp = ucontrol->value.integer.value;
  651. int change = 0;
  652. if (chs & 1) {
  653. val = *valp & 0x7f;
  654. val |= snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 0x80;
  655. change = snd_hda_codec_amp_write(codec, nid, 0, dir, idx, val);
  656. valp++;
  657. }
  658. if (chs & 2) {
  659. val = *valp & 0x7f;
  660. val |= snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 0x80;
  661. change |= snd_hda_codec_amp_write(codec, nid, 1, dir, idx, val);
  662. }
  663. return change;
  664. }
  665. /* switch */
  666. int snd_hda_mixer_amp_switch_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
  667. {
  668. int chs = get_amp_channels(kcontrol);
  669. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  670. uinfo->count = chs == 3 ? 2 : 1;
  671. uinfo->value.integer.min = 0;
  672. uinfo->value.integer.max = 1;
  673. return 0;
  674. }
  675. int snd_hda_mixer_amp_switch_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  676. {
  677. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  678. hda_nid_t nid = get_amp_nid(kcontrol);
  679. int chs = get_amp_channels(kcontrol);
  680. int dir = get_amp_direction(kcontrol);
  681. int idx = get_amp_index(kcontrol);
  682. long *valp = ucontrol->value.integer.value;
  683. if (chs & 1)
  684. *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 0x80) ? 0 : 1;
  685. if (chs & 2)
  686. *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 0x80) ? 0 : 1;
  687. return 0;
  688. }
  689. int snd_hda_mixer_amp_switch_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  690. {
  691. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  692. hda_nid_t nid = get_amp_nid(kcontrol);
  693. int chs = get_amp_channels(kcontrol);
  694. int dir = get_amp_direction(kcontrol);
  695. int idx = get_amp_index(kcontrol);
  696. int val;
  697. long *valp = ucontrol->value.integer.value;
  698. int change = 0;
  699. if (chs & 1) {
  700. val = snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 0x7f;
  701. val |= *valp ? 0 : 0x80;
  702. change = snd_hda_codec_amp_write(codec, nid, 0, dir, idx, val);
  703. valp++;
  704. }
  705. if (chs & 2) {
  706. val = snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 0x7f;
  707. val |= *valp ? 0 : 0x80;
  708. change = snd_hda_codec_amp_write(codec, nid, 1, dir, idx, val);
  709. }
  710. return change;
  711. }
  712. /*
  713. * SPDIF out controls
  714. */
  715. static int snd_hda_spdif_mask_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
  716. {
  717. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  718. uinfo->count = 1;
  719. return 0;
  720. }
  721. static int snd_hda_spdif_cmask_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  722. {
  723. ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
  724. IEC958_AES0_NONAUDIO |
  725. IEC958_AES0_CON_EMPHASIS_5015 |
  726. IEC958_AES0_CON_NOT_COPYRIGHT;
  727. ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
  728. IEC958_AES1_CON_ORIGINAL;
  729. return 0;
  730. }
  731. static int snd_hda_spdif_pmask_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  732. {
  733. ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
  734. IEC958_AES0_NONAUDIO |
  735. IEC958_AES0_PRO_EMPHASIS_5015;
  736. return 0;
  737. }
  738. static int snd_hda_spdif_default_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  739. {
  740. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  741. ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
  742. ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
  743. ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
  744. ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
  745. return 0;
  746. }
  747. /* convert from SPDIF status bits to HDA SPDIF bits
  748. * bit 0 (DigEn) is always set zero (to be filled later)
  749. */
  750. static unsigned short convert_from_spdif_status(unsigned int sbits)
  751. {
  752. unsigned short val = 0;
  753. if (sbits & IEC958_AES0_PROFESSIONAL)
  754. val |= 1 << 6;
  755. if (sbits & IEC958_AES0_NONAUDIO)
  756. val |= 1 << 5;
  757. if (sbits & IEC958_AES0_PROFESSIONAL) {
  758. if ((sbits & IEC958_AES0_PRO_EMPHASIS) == IEC958_AES0_PRO_EMPHASIS_5015)
  759. val |= 1 << 3;
  760. } else {
  761. if ((sbits & IEC958_AES0_CON_EMPHASIS) == IEC958_AES0_CON_EMPHASIS_5015)
  762. val |= 1 << 3;
  763. if (! (sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
  764. val |= 1 << 4;
  765. if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
  766. val |= 1 << 7;
  767. val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
  768. }
  769. return val;
  770. }
  771. /* convert to SPDIF status bits from HDA SPDIF bits
  772. */
  773. static unsigned int convert_to_spdif_status(unsigned short val)
  774. {
  775. unsigned int sbits = 0;
  776. if (val & (1 << 5))
  777. sbits |= IEC958_AES0_NONAUDIO;
  778. if (val & (1 << 6))
  779. sbits |= IEC958_AES0_PROFESSIONAL;
  780. if (sbits & IEC958_AES0_PROFESSIONAL) {
  781. if (sbits & (1 << 3))
  782. sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
  783. } else {
  784. if (val & (1 << 3))
  785. sbits |= IEC958_AES0_CON_EMPHASIS_5015;
  786. if (! (val & (1 << 4)))
  787. sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
  788. if (val & (1 << 7))
  789. sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
  790. sbits |= val & (0x7f << 8);
  791. }
  792. return sbits;
  793. }
  794. static int snd_hda_spdif_default_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  795. {
  796. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  797. hda_nid_t nid = kcontrol->private_value;
  798. unsigned short val;
  799. int change;
  800. down(&codec->spdif_mutex);
  801. codec->spdif_status = ucontrol->value.iec958.status[0] |
  802. ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
  803. ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
  804. ((unsigned int)ucontrol->value.iec958.status[3] << 24);
  805. val = convert_from_spdif_status(codec->spdif_status);
  806. val |= codec->spdif_ctls & 1;
  807. change = codec->spdif_ctls != val;
  808. codec->spdif_ctls = val;
  809. if (change || codec->in_resume) {
  810. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, val & 0xff);
  811. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_2, val >> 8);
  812. }
  813. up(&codec->spdif_mutex);
  814. return change;
  815. }
  816. static int snd_hda_spdif_out_switch_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
  817. {
  818. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  819. uinfo->count = 1;
  820. uinfo->value.integer.min = 0;
  821. uinfo->value.integer.max = 1;
  822. return 0;
  823. }
  824. static int snd_hda_spdif_out_switch_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  825. {
  826. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  827. ucontrol->value.integer.value[0] = codec->spdif_ctls & 1;
  828. return 0;
  829. }
  830. static int snd_hda_spdif_out_switch_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  831. {
  832. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  833. hda_nid_t nid = kcontrol->private_value;
  834. unsigned short val;
  835. int change;
  836. down(&codec->spdif_mutex);
  837. val = codec->spdif_ctls & ~1;
  838. if (ucontrol->value.integer.value[0])
  839. val |= 1;
  840. change = codec->spdif_ctls != val;
  841. if (change || codec->in_resume) {
  842. codec->spdif_ctls = val;
  843. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, val & 0xff);
  844. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
  845. AC_AMP_SET_RIGHT | AC_AMP_SET_LEFT |
  846. AC_AMP_SET_OUTPUT | ((val & 1) ? 0 : 0x80));
  847. }
  848. up(&codec->spdif_mutex);
  849. return change;
  850. }
  851. static snd_kcontrol_new_t dig_mixes[] = {
  852. {
  853. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  854. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  855. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
  856. .info = snd_hda_spdif_mask_info,
  857. .get = snd_hda_spdif_cmask_get,
  858. },
  859. {
  860. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  861. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  862. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
  863. .info = snd_hda_spdif_mask_info,
  864. .get = snd_hda_spdif_pmask_get,
  865. },
  866. {
  867. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  868. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
  869. .info = snd_hda_spdif_mask_info,
  870. .get = snd_hda_spdif_default_get,
  871. .put = snd_hda_spdif_default_put,
  872. },
  873. {
  874. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  875. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
  876. .info = snd_hda_spdif_out_switch_info,
  877. .get = snd_hda_spdif_out_switch_get,
  878. .put = snd_hda_spdif_out_switch_put,
  879. },
  880. { } /* end */
  881. };
  882. /**
  883. * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
  884. * @codec: the HDA codec
  885. * @nid: audio out widget NID
  886. *
  887. * Creates controls related with the SPDIF output.
  888. * Called from each patch supporting the SPDIF out.
  889. *
  890. * Returns 0 if successful, or a negative error code.
  891. */
  892. int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
  893. {
  894. int err;
  895. snd_kcontrol_t *kctl;
  896. snd_kcontrol_new_t *dig_mix;
  897. for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
  898. kctl = snd_ctl_new1(dig_mix, codec);
  899. kctl->private_value = nid;
  900. if ((err = snd_ctl_add(codec->bus->card, kctl)) < 0)
  901. return err;
  902. }
  903. codec->spdif_ctls = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0);
  904. codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
  905. return 0;
  906. }
  907. /*
  908. * SPDIF input
  909. */
  910. #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
  911. static int snd_hda_spdif_in_switch_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  912. {
  913. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  914. ucontrol->value.integer.value[0] = codec->spdif_in_enable;
  915. return 0;
  916. }
  917. static int snd_hda_spdif_in_switch_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  918. {
  919. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  920. hda_nid_t nid = kcontrol->private_value;
  921. unsigned int val = !!ucontrol->value.integer.value[0];
  922. int change;
  923. down(&codec->spdif_mutex);
  924. change = codec->spdif_in_enable != val;
  925. if (change || codec->in_resume) {
  926. codec->spdif_in_enable = val;
  927. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, val);
  928. }
  929. up(&codec->spdif_mutex);
  930. return change;
  931. }
  932. static int snd_hda_spdif_in_status_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  933. {
  934. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  935. hda_nid_t nid = kcontrol->private_value;
  936. unsigned short val;
  937. unsigned int sbits;
  938. val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0);
  939. sbits = convert_to_spdif_status(val);
  940. ucontrol->value.iec958.status[0] = sbits;
  941. ucontrol->value.iec958.status[1] = sbits >> 8;
  942. ucontrol->value.iec958.status[2] = sbits >> 16;
  943. ucontrol->value.iec958.status[3] = sbits >> 24;
  944. return 0;
  945. }
  946. static snd_kcontrol_new_t dig_in_ctls[] = {
  947. {
  948. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  949. .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
  950. .info = snd_hda_spdif_in_switch_info,
  951. .get = snd_hda_spdif_in_switch_get,
  952. .put = snd_hda_spdif_in_switch_put,
  953. },
  954. {
  955. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  956. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  957. .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
  958. .info = snd_hda_spdif_mask_info,
  959. .get = snd_hda_spdif_in_status_get,
  960. },
  961. { } /* end */
  962. };
  963. /**
  964. * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
  965. * @codec: the HDA codec
  966. * @nid: audio in widget NID
  967. *
  968. * Creates controls related with the SPDIF input.
  969. * Called from each patch supporting the SPDIF in.
  970. *
  971. * Returns 0 if successful, or a negative error code.
  972. */
  973. int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
  974. {
  975. int err;
  976. snd_kcontrol_t *kctl;
  977. snd_kcontrol_new_t *dig_mix;
  978. for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
  979. kctl = snd_ctl_new1(dig_mix, codec);
  980. kctl->private_value = nid;
  981. if ((err = snd_ctl_add(codec->bus->card, kctl)) < 0)
  982. return err;
  983. }
  984. codec->spdif_in_enable = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0) & 1;
  985. return 0;
  986. }
  987. /**
  988. * snd_hda_build_controls - build mixer controls
  989. * @bus: the BUS
  990. *
  991. * Creates mixer controls for each codec included in the bus.
  992. *
  993. * Returns 0 if successful, otherwise a negative error code.
  994. */
  995. int snd_hda_build_controls(struct hda_bus *bus)
  996. {
  997. struct list_head *p;
  998. /* build controls */
  999. list_for_each(p, &bus->codec_list) {
  1000. struct hda_codec *codec = list_entry(p, struct hda_codec, list);
  1001. int err;
  1002. if (! codec->patch_ops.build_controls)
  1003. continue;
  1004. err = codec->patch_ops.build_controls(codec);
  1005. if (err < 0)
  1006. return err;
  1007. }
  1008. /* initialize */
  1009. list_for_each(p, &bus->codec_list) {
  1010. struct hda_codec *codec = list_entry(p, struct hda_codec, list);
  1011. int err;
  1012. if (! codec->patch_ops.init)
  1013. continue;
  1014. err = codec->patch_ops.init(codec);
  1015. if (err < 0)
  1016. return err;
  1017. }
  1018. return 0;
  1019. }
  1020. /*
  1021. * stream formats
  1022. */
  1023. static unsigned int rate_bits[][3] = {
  1024. /* rate in Hz, ALSA rate bitmask, HDA format value */
  1025. { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
  1026. { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
  1027. { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
  1028. { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
  1029. { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
  1030. { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
  1031. { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
  1032. { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
  1033. { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
  1034. { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
  1035. { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
  1036. { 0 }
  1037. };
  1038. /**
  1039. * snd_hda_calc_stream_format - calculate format bitset
  1040. * @rate: the sample rate
  1041. * @channels: the number of channels
  1042. * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
  1043. * @maxbps: the max. bps
  1044. *
  1045. * Calculate the format bitset from the given rate, channels and th PCM format.
  1046. *
  1047. * Return zero if invalid.
  1048. */
  1049. unsigned int snd_hda_calc_stream_format(unsigned int rate,
  1050. unsigned int channels,
  1051. unsigned int format,
  1052. unsigned int maxbps)
  1053. {
  1054. int i;
  1055. unsigned int val = 0;
  1056. for (i = 0; rate_bits[i][0]; i++)
  1057. if (rate_bits[i][0] == rate) {
  1058. val = rate_bits[i][2];
  1059. break;
  1060. }
  1061. if (! rate_bits[i][0]) {
  1062. snd_printdd("invalid rate %d\n", rate);
  1063. return 0;
  1064. }
  1065. if (channels == 0 || channels > 8) {
  1066. snd_printdd("invalid channels %d\n", channels);
  1067. return 0;
  1068. }
  1069. val |= channels - 1;
  1070. switch (snd_pcm_format_width(format)) {
  1071. case 8: val |= 0x00; break;
  1072. case 16: val |= 0x10; break;
  1073. case 20:
  1074. case 24:
  1075. case 32:
  1076. if (maxbps >= 32)
  1077. val |= 0x40;
  1078. else if (maxbps >= 24)
  1079. val |= 0x30;
  1080. else
  1081. val |= 0x20;
  1082. break;
  1083. default:
  1084. snd_printdd("invalid format width %d\n", snd_pcm_format_width(format));
  1085. return 0;
  1086. }
  1087. return val;
  1088. }
  1089. /**
  1090. * snd_hda_query_supported_pcm - query the supported PCM rates and formats
  1091. * @codec: the HDA codec
  1092. * @nid: NID to query
  1093. * @ratesp: the pointer to store the detected rate bitflags
  1094. * @formatsp: the pointer to store the detected formats
  1095. * @bpsp: the pointer to store the detected format widths
  1096. *
  1097. * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
  1098. * or @bsps argument is ignored.
  1099. *
  1100. * Returns 0 if successful, otherwise a negative error code.
  1101. */
  1102. int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
  1103. u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
  1104. {
  1105. int i;
  1106. unsigned int val, streams;
  1107. val = 0;
  1108. if (nid != codec->afg &&
  1109. snd_hda_param_read(codec, nid, AC_PAR_AUDIO_WIDGET_CAP) & AC_WCAP_FORMAT_OVRD) {
  1110. val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
  1111. if (val == -1)
  1112. return -EIO;
  1113. }
  1114. if (! val)
  1115. val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
  1116. if (ratesp) {
  1117. u32 rates = 0;
  1118. for (i = 0; rate_bits[i][0]; i++) {
  1119. if (val & (1 << i))
  1120. rates |= rate_bits[i][1];
  1121. }
  1122. *ratesp = rates;
  1123. }
  1124. if (formatsp || bpsp) {
  1125. u64 formats = 0;
  1126. unsigned int bps;
  1127. unsigned int wcaps;
  1128. wcaps = snd_hda_param_read(codec, nid, AC_PAR_AUDIO_WIDGET_CAP);
  1129. streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
  1130. if (streams == -1)
  1131. return -EIO;
  1132. if (! streams) {
  1133. streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
  1134. if (streams == -1)
  1135. return -EIO;
  1136. }
  1137. bps = 0;
  1138. if (streams & AC_SUPFMT_PCM) {
  1139. if (val & AC_SUPPCM_BITS_8) {
  1140. formats |= SNDRV_PCM_FMTBIT_U8;
  1141. bps = 8;
  1142. }
  1143. if (val & AC_SUPPCM_BITS_16) {
  1144. formats |= SNDRV_PCM_FMTBIT_S16_LE;
  1145. bps = 16;
  1146. }
  1147. if (wcaps & AC_WCAP_DIGITAL) {
  1148. if (val & AC_SUPPCM_BITS_32)
  1149. formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
  1150. if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
  1151. formats |= SNDRV_PCM_FMTBIT_S32_LE;
  1152. if (val & AC_SUPPCM_BITS_24)
  1153. bps = 24;
  1154. else if (val & AC_SUPPCM_BITS_20)
  1155. bps = 20;
  1156. } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|AC_SUPPCM_BITS_32)) {
  1157. formats |= SNDRV_PCM_FMTBIT_S32_LE;
  1158. if (val & AC_SUPPCM_BITS_32)
  1159. bps = 32;
  1160. else if (val & AC_SUPPCM_BITS_20)
  1161. bps = 20;
  1162. else if (val & AC_SUPPCM_BITS_24)
  1163. bps = 24;
  1164. }
  1165. }
  1166. else if (streams == AC_SUPFMT_FLOAT32) { /* should be exclusive */
  1167. formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
  1168. bps = 32;
  1169. } else if (streams == AC_SUPFMT_AC3) { /* should be exclusive */
  1170. /* temporary hack: we have still no proper support
  1171. * for the direct AC3 stream...
  1172. */
  1173. formats |= SNDRV_PCM_FMTBIT_U8;
  1174. bps = 8;
  1175. }
  1176. if (formatsp)
  1177. *formatsp = formats;
  1178. if (bpsp)
  1179. *bpsp = bps;
  1180. }
  1181. return 0;
  1182. }
  1183. /**
  1184. * snd_hda_is_supported_format - check whether the given node supports the format val
  1185. *
  1186. * Returns 1 if supported, 0 if not.
  1187. */
  1188. int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
  1189. unsigned int format)
  1190. {
  1191. int i;
  1192. unsigned int val = 0, rate, stream;
  1193. if (nid != codec->afg &&
  1194. snd_hda_param_read(codec, nid, AC_PAR_AUDIO_WIDGET_CAP) & AC_WCAP_FORMAT_OVRD) {
  1195. val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
  1196. if (val == -1)
  1197. return 0;
  1198. }
  1199. if (! val) {
  1200. val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
  1201. if (val == -1)
  1202. return 0;
  1203. }
  1204. rate = format & 0xff00;
  1205. for (i = 0; rate_bits[i][0]; i++)
  1206. if (rate_bits[i][2] == rate) {
  1207. if (val & (1 << i))
  1208. break;
  1209. return 0;
  1210. }
  1211. if (! rate_bits[i][0])
  1212. return 0;
  1213. stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
  1214. if (stream == -1)
  1215. return 0;
  1216. if (! stream && nid != codec->afg)
  1217. stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
  1218. if (! stream || stream == -1)
  1219. return 0;
  1220. if (stream & AC_SUPFMT_PCM) {
  1221. switch (format & 0xf0) {
  1222. case 0x00:
  1223. if (! (val & AC_SUPPCM_BITS_8))
  1224. return 0;
  1225. break;
  1226. case 0x10:
  1227. if (! (val & AC_SUPPCM_BITS_16))
  1228. return 0;
  1229. break;
  1230. case 0x20:
  1231. if (! (val & AC_SUPPCM_BITS_20))
  1232. return 0;
  1233. break;
  1234. case 0x30:
  1235. if (! (val & AC_SUPPCM_BITS_24))
  1236. return 0;
  1237. break;
  1238. case 0x40:
  1239. if (! (val & AC_SUPPCM_BITS_32))
  1240. return 0;
  1241. break;
  1242. default:
  1243. return 0;
  1244. }
  1245. } else {
  1246. /* FIXME: check for float32 and AC3? */
  1247. }
  1248. return 1;
  1249. }
  1250. /*
  1251. * PCM stuff
  1252. */
  1253. static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
  1254. struct hda_codec *codec,
  1255. snd_pcm_substream_t *substream)
  1256. {
  1257. return 0;
  1258. }
  1259. static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
  1260. struct hda_codec *codec,
  1261. unsigned int stream_tag,
  1262. unsigned int format,
  1263. snd_pcm_substream_t *substream)
  1264. {
  1265. snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
  1266. return 0;
  1267. }
  1268. static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
  1269. struct hda_codec *codec,
  1270. snd_pcm_substream_t *substream)
  1271. {
  1272. snd_hda_codec_setup_stream(codec, hinfo->nid, 0, 0, 0);
  1273. return 0;
  1274. }
  1275. static int set_pcm_default_values(struct hda_codec *codec, struct hda_pcm_stream *info)
  1276. {
  1277. if (info->nid) {
  1278. /* query support PCM information from the given NID */
  1279. if (! info->rates || ! info->formats)
  1280. snd_hda_query_supported_pcm(codec, info->nid,
  1281. info->rates ? NULL : &info->rates,
  1282. info->formats ? NULL : &info->formats,
  1283. info->maxbps ? NULL : &info->maxbps);
  1284. }
  1285. if (info->ops.open == NULL)
  1286. info->ops.open = hda_pcm_default_open_close;
  1287. if (info->ops.close == NULL)
  1288. info->ops.close = hda_pcm_default_open_close;
  1289. if (info->ops.prepare == NULL) {
  1290. snd_assert(info->nid, return -EINVAL);
  1291. info->ops.prepare = hda_pcm_default_prepare;
  1292. }
  1293. if (info->ops.prepare == NULL) {
  1294. snd_assert(info->nid, return -EINVAL);
  1295. info->ops.prepare = hda_pcm_default_prepare;
  1296. }
  1297. if (info->ops.cleanup == NULL) {
  1298. snd_assert(info->nid, return -EINVAL);
  1299. info->ops.cleanup = hda_pcm_default_cleanup;
  1300. }
  1301. return 0;
  1302. }
  1303. /**
  1304. * snd_hda_build_pcms - build PCM information
  1305. * @bus: the BUS
  1306. *
  1307. * Create PCM information for each codec included in the bus.
  1308. *
  1309. * The build_pcms codec patch is requested to set up codec->num_pcms and
  1310. * codec->pcm_info properly. The array is referred by the top-level driver
  1311. * to create its PCM instances.
  1312. * The allocated codec->pcm_info should be released in codec->patch_ops.free
  1313. * callback.
  1314. *
  1315. * At least, substreams, channels_min and channels_max must be filled for
  1316. * each stream. substreams = 0 indicates that the stream doesn't exist.
  1317. * When rates and/or formats are zero, the supported values are queried
  1318. * from the given nid. The nid is used also by the default ops.prepare
  1319. * and ops.cleanup callbacks.
  1320. *
  1321. * The driver needs to call ops.open in its open callback. Similarly,
  1322. * ops.close is supposed to be called in the close callback.
  1323. * ops.prepare should be called in the prepare or hw_params callback
  1324. * with the proper parameters for set up.
  1325. * ops.cleanup should be called in hw_free for clean up of streams.
  1326. *
  1327. * This function returns 0 if successfull, or a negative error code.
  1328. */
  1329. int snd_hda_build_pcms(struct hda_bus *bus)
  1330. {
  1331. struct list_head *p;
  1332. list_for_each(p, &bus->codec_list) {
  1333. struct hda_codec *codec = list_entry(p, struct hda_codec, list);
  1334. unsigned int pcm, s;
  1335. int err;
  1336. if (! codec->patch_ops.build_pcms)
  1337. continue;
  1338. err = codec->patch_ops.build_pcms(codec);
  1339. if (err < 0)
  1340. return err;
  1341. for (pcm = 0; pcm < codec->num_pcms; pcm++) {
  1342. for (s = 0; s < 2; s++) {
  1343. struct hda_pcm_stream *info;
  1344. info = &codec->pcm_info[pcm].stream[s];
  1345. if (! info->substreams)
  1346. continue;
  1347. err = set_pcm_default_values(codec, info);
  1348. if (err < 0)
  1349. return err;
  1350. }
  1351. }
  1352. }
  1353. return 0;
  1354. }
  1355. /**
  1356. * snd_hda_check_board_config - compare the current codec with the config table
  1357. * @codec: the HDA codec
  1358. * @tbl: configuration table, terminated by null entries
  1359. *
  1360. * Compares the modelname or PCI subsystem id of the current codec with the
  1361. * given configuration table. If a matching entry is found, returns its
  1362. * config value (supposed to be 0 or positive).
  1363. *
  1364. * If no entries are matching, the function returns a negative value.
  1365. */
  1366. int snd_hda_check_board_config(struct hda_codec *codec, struct hda_board_config *tbl)
  1367. {
  1368. struct hda_board_config *c;
  1369. if (codec->bus->modelname) {
  1370. for (c = tbl; c->modelname || c->pci_vendor; c++) {
  1371. if (c->modelname &&
  1372. ! strcmp(codec->bus->modelname, c->modelname)) {
  1373. snd_printd(KERN_INFO "hda_codec: model '%s' is selected\n", c->modelname);
  1374. return c->config;
  1375. }
  1376. }
  1377. }
  1378. if (codec->bus->pci) {
  1379. u16 subsystem_vendor, subsystem_device;
  1380. pci_read_config_word(codec->bus->pci, PCI_SUBSYSTEM_VENDOR_ID, &subsystem_vendor);
  1381. pci_read_config_word(codec->bus->pci, PCI_SUBSYSTEM_ID, &subsystem_device);
  1382. for (c = tbl; c->modelname || c->pci_vendor; c++) {
  1383. if (c->pci_vendor == subsystem_vendor &&
  1384. c->pci_device == subsystem_device)
  1385. return c->config;
  1386. }
  1387. }
  1388. return -1;
  1389. }
  1390. /**
  1391. * snd_hda_add_new_ctls - create controls from the array
  1392. * @codec: the HDA codec
  1393. * @knew: the array of snd_kcontrol_new_t
  1394. *
  1395. * This helper function creates and add new controls in the given array.
  1396. * The array must be terminated with an empty entry as terminator.
  1397. *
  1398. * Returns 0 if successful, or a negative error code.
  1399. */
  1400. int snd_hda_add_new_ctls(struct hda_codec *codec, snd_kcontrol_new_t *knew)
  1401. {
  1402. int err;
  1403. for (; knew->name; knew++) {
  1404. err = snd_ctl_add(codec->bus->card, snd_ctl_new1(knew, codec));
  1405. if (err < 0)
  1406. return err;
  1407. }
  1408. return 0;
  1409. }
  1410. /*
  1411. * input MUX helper
  1412. */
  1413. int snd_hda_input_mux_info(const struct hda_input_mux *imux, snd_ctl_elem_info_t *uinfo)
  1414. {
  1415. unsigned int index;
  1416. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1417. uinfo->count = 1;
  1418. uinfo->value.enumerated.items = imux->num_items;
  1419. index = uinfo->value.enumerated.item;
  1420. if (index >= imux->num_items)
  1421. index = imux->num_items - 1;
  1422. strcpy(uinfo->value.enumerated.name, imux->items[index].label);
  1423. return 0;
  1424. }
  1425. int snd_hda_input_mux_put(struct hda_codec *codec, const struct hda_input_mux *imux,
  1426. snd_ctl_elem_value_t *ucontrol, hda_nid_t nid,
  1427. unsigned int *cur_val)
  1428. {
  1429. unsigned int idx;
  1430. idx = ucontrol->value.enumerated.item[0];
  1431. if (idx >= imux->num_items)
  1432. idx = imux->num_items - 1;
  1433. if (*cur_val == idx && ! codec->in_resume)
  1434. return 0;
  1435. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
  1436. imux->items[idx].index);
  1437. *cur_val = idx;
  1438. return 1;
  1439. }
  1440. /*
  1441. * Multi-channel / digital-out PCM helper functions
  1442. */
  1443. /*
  1444. * open the digital out in the exclusive mode
  1445. */
  1446. int snd_hda_multi_out_dig_open(struct hda_codec *codec, struct hda_multi_out *mout)
  1447. {
  1448. down(&codec->spdif_mutex);
  1449. if (mout->dig_out_used) {
  1450. up(&codec->spdif_mutex);
  1451. return -EBUSY; /* already being used */
  1452. }
  1453. mout->dig_out_used = HDA_DIG_EXCLUSIVE;
  1454. up(&codec->spdif_mutex);
  1455. return 0;
  1456. }
  1457. /*
  1458. * release the digital out
  1459. */
  1460. int snd_hda_multi_out_dig_close(struct hda_codec *codec, struct hda_multi_out *mout)
  1461. {
  1462. down(&codec->spdif_mutex);
  1463. mout->dig_out_used = 0;
  1464. up(&codec->spdif_mutex);
  1465. return 0;
  1466. }
  1467. /*
  1468. * set up more restrictions for analog out
  1469. */
  1470. int snd_hda_multi_out_analog_open(struct hda_codec *codec, struct hda_multi_out *mout,
  1471. snd_pcm_substream_t *substream)
  1472. {
  1473. substream->runtime->hw.channels_max = mout->max_channels;
  1474. return snd_pcm_hw_constraint_step(substream->runtime, 0,
  1475. SNDRV_PCM_HW_PARAM_CHANNELS, 2);
  1476. }
  1477. /*
  1478. * set up the i/o for analog out
  1479. * when the digital out is available, copy the front out to digital out, too.
  1480. */
  1481. int snd_hda_multi_out_analog_prepare(struct hda_codec *codec, struct hda_multi_out *mout,
  1482. unsigned int stream_tag,
  1483. unsigned int format,
  1484. snd_pcm_substream_t *substream)
  1485. {
  1486. hda_nid_t *nids = mout->dac_nids;
  1487. int chs = substream->runtime->channels;
  1488. int i;
  1489. down(&codec->spdif_mutex);
  1490. if (mout->dig_out_nid && mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
  1491. if (chs == 2 &&
  1492. snd_hda_is_supported_format(codec, mout->dig_out_nid, format) &&
  1493. ! (codec->spdif_status & IEC958_AES0_NONAUDIO)) {
  1494. mout->dig_out_used = HDA_DIG_ANALOG_DUP;
  1495. /* setup digital receiver */
  1496. snd_hda_codec_setup_stream(codec, mout->dig_out_nid,
  1497. stream_tag, 0, format);
  1498. } else {
  1499. mout->dig_out_used = 0;
  1500. snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
  1501. }
  1502. }
  1503. up(&codec->spdif_mutex);
  1504. /* front */
  1505. snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag, 0, format);
  1506. if (mout->hp_nid)
  1507. /* headphone out will just decode front left/right (stereo) */
  1508. snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag, 0, format);
  1509. /* surrounds */
  1510. for (i = 1; i < mout->num_dacs; i++) {
  1511. if (i == HDA_REAR && chs == 2) /* copy front to rear */
  1512. snd_hda_codec_setup_stream(codec, nids[i], stream_tag, 0, format);
  1513. else if (chs >= (i + 1) * 2) /* independent out */
  1514. snd_hda_codec_setup_stream(codec, nids[i], stream_tag, i * 2,
  1515. format);
  1516. }
  1517. return 0;
  1518. }
  1519. /*
  1520. * clean up the setting for analog out
  1521. */
  1522. int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec, struct hda_multi_out *mout)
  1523. {
  1524. hda_nid_t *nids = mout->dac_nids;
  1525. int i;
  1526. for (i = 0; i < mout->num_dacs; i++)
  1527. snd_hda_codec_setup_stream(codec, nids[i], 0, 0, 0);
  1528. if (mout->hp_nid)
  1529. snd_hda_codec_setup_stream(codec, mout->hp_nid, 0, 0, 0);
  1530. down(&codec->spdif_mutex);
  1531. if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
  1532. snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
  1533. mout->dig_out_used = 0;
  1534. }
  1535. up(&codec->spdif_mutex);
  1536. return 0;
  1537. }
  1538. #ifdef CONFIG_PM
  1539. /*
  1540. * power management
  1541. */
  1542. /**
  1543. * snd_hda_suspend - suspend the codecs
  1544. * @bus: the HDA bus
  1545. * @state: suspsend state
  1546. *
  1547. * Returns 0 if successful.
  1548. */
  1549. int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
  1550. {
  1551. struct list_head *p;
  1552. /* FIXME: should handle power widget capabilities */
  1553. list_for_each(p, &bus->codec_list) {
  1554. struct hda_codec *codec = list_entry(p, struct hda_codec, list);
  1555. if (codec->patch_ops.suspend)
  1556. codec->patch_ops.suspend(codec, state);
  1557. }
  1558. return 0;
  1559. }
  1560. /**
  1561. * snd_hda_resume - resume the codecs
  1562. * @bus: the HDA bus
  1563. * @state: resume state
  1564. *
  1565. * Returns 0 if successful.
  1566. */
  1567. int snd_hda_resume(struct hda_bus *bus)
  1568. {
  1569. struct list_head *p;
  1570. list_for_each(p, &bus->codec_list) {
  1571. struct hda_codec *codec = list_entry(p, struct hda_codec, list);
  1572. if (codec->patch_ops.resume)
  1573. codec->patch_ops.resume(codec);
  1574. }
  1575. return 0;
  1576. }
  1577. /**
  1578. * snd_hda_resume_ctls - resume controls in the new control list
  1579. * @codec: the HDA codec
  1580. * @knew: the array of snd_kcontrol_new_t
  1581. *
  1582. * This function resumes the mixer controls in the snd_kcontrol_new_t array,
  1583. * originally for snd_hda_add_new_ctls().
  1584. * The array must be terminated with an empty entry as terminator.
  1585. */
  1586. int snd_hda_resume_ctls(struct hda_codec *codec, snd_kcontrol_new_t *knew)
  1587. {
  1588. snd_ctl_elem_value_t *val;
  1589. val = kmalloc(sizeof(*val), GFP_KERNEL);
  1590. if (! val)
  1591. return -ENOMEM;
  1592. codec->in_resume = 1;
  1593. for (; knew->name; knew++) {
  1594. int i, count;
  1595. count = knew->count ? knew->count : 1;
  1596. for (i = 0; i < count; i++) {
  1597. memset(val, 0, sizeof(*val));
  1598. val->id.iface = knew->iface;
  1599. val->id.device = knew->device;
  1600. val->id.subdevice = knew->subdevice;
  1601. strcpy(val->id.name, knew->name);
  1602. val->id.index = knew->index ? knew->index : i;
  1603. /* Assume that get callback reads only from cache,
  1604. * not accessing to the real hardware
  1605. */
  1606. if (snd_ctl_elem_read(codec->bus->card, val) < 0)
  1607. continue;
  1608. snd_ctl_elem_write(codec->bus->card, NULL, val);
  1609. }
  1610. }
  1611. codec->in_resume = 0;
  1612. kfree(val);
  1613. return 0;
  1614. }
  1615. /**
  1616. * snd_hda_resume_spdif_out - resume the digital out
  1617. * @codec: the HDA codec
  1618. */
  1619. int snd_hda_resume_spdif_out(struct hda_codec *codec)
  1620. {
  1621. return snd_hda_resume_ctls(codec, dig_mixes);
  1622. }
  1623. /**
  1624. * snd_hda_resume_spdif_in - resume the digital in
  1625. * @codec: the HDA codec
  1626. */
  1627. int snd_hda_resume_spdif_in(struct hda_codec *codec)
  1628. {
  1629. return snd_hda_resume_ctls(codec, dig_in_ctls);
  1630. }
  1631. #endif
  1632. /*
  1633. * symbols exported for controller modules
  1634. */
  1635. EXPORT_SYMBOL(snd_hda_codec_read);
  1636. EXPORT_SYMBOL(snd_hda_codec_write);
  1637. EXPORT_SYMBOL(snd_hda_sequence_write);
  1638. EXPORT_SYMBOL(snd_hda_get_sub_nodes);
  1639. EXPORT_SYMBOL(snd_hda_queue_unsol_event);
  1640. EXPORT_SYMBOL(snd_hda_bus_new);
  1641. EXPORT_SYMBOL(snd_hda_codec_new);
  1642. EXPORT_SYMBOL(snd_hda_codec_setup_stream);
  1643. EXPORT_SYMBOL(snd_hda_calc_stream_format);
  1644. EXPORT_SYMBOL(snd_hda_build_pcms);
  1645. EXPORT_SYMBOL(snd_hda_build_controls);
  1646. #ifdef CONFIG_PM
  1647. EXPORT_SYMBOL(snd_hda_suspend);
  1648. EXPORT_SYMBOL(snd_hda_resume);
  1649. #endif
  1650. /*
  1651. * INIT part
  1652. */
  1653. static int __init alsa_hda_init(void)
  1654. {
  1655. return 0;
  1656. }
  1657. static void __exit alsa_hda_exit(void)
  1658. {
  1659. }
  1660. module_init(alsa_hda_init)
  1661. module_exit(alsa_hda_exit)