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