hda_codec.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856
  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_printdd("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. if (! nid)
  487. return;
  488. snd_printdd("hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
  489. nid, stream_tag, channel_id, format);
  490. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
  491. (stream_tag << 4) | channel_id);
  492. msleep(1);
  493. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
  494. }
  495. /*
  496. * amp access functions
  497. */
  498. #define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + (idx) * 32 + (dir) * 64)
  499. #define INFO_AMP_CAPS (1<<0)
  500. #define INFO_AMP_VOL (1<<1)
  501. /* initialize the hash table */
  502. static void init_amp_hash(struct hda_codec *codec)
  503. {
  504. memset(codec->amp_hash, 0xff, sizeof(codec->amp_hash));
  505. codec->num_amp_entries = 0;
  506. }
  507. /* query the hash. allocate an entry if not found. */
  508. static struct hda_amp_info *get_alloc_amp_hash(struct hda_codec *codec, u32 key)
  509. {
  510. u16 idx = key % (u16)ARRAY_SIZE(codec->amp_hash);
  511. u16 cur = codec->amp_hash[idx];
  512. struct hda_amp_info *info;
  513. while (cur != 0xffff) {
  514. info = &codec->amp_info[cur];
  515. if (info->key == key)
  516. return info;
  517. cur = info->next;
  518. }
  519. /* add a new hash entry */
  520. if (codec->num_amp_entries >= ARRAY_SIZE(codec->amp_info)) {
  521. snd_printk(KERN_ERR "hda_codec: Tooooo many amps!\n");
  522. return NULL;
  523. }
  524. cur = codec->num_amp_entries++;
  525. info = &codec->amp_info[cur];
  526. info->key = key;
  527. info->status = 0; /* not initialized yet */
  528. info->next = codec->amp_hash[idx];
  529. codec->amp_hash[idx] = cur;
  530. return info;
  531. }
  532. /*
  533. * query AMP capabilities for the given widget and direction
  534. */
  535. static u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
  536. {
  537. struct hda_amp_info *info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
  538. if (! info)
  539. return 0;
  540. if (! (info->status & INFO_AMP_CAPS)) {
  541. if (!(snd_hda_param_read(codec, nid, AC_PAR_AUDIO_WIDGET_CAP) & AC_WCAP_AMP_OVRD))
  542. nid = codec->afg;
  543. info->amp_caps = snd_hda_param_read(codec, nid, direction == HDA_OUTPUT ?
  544. AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
  545. info->status |= INFO_AMP_CAPS;
  546. }
  547. return info->amp_caps;
  548. }
  549. /*
  550. * read the current volume to info
  551. * if the cache exists, read from the cache.
  552. */
  553. static void get_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
  554. hda_nid_t nid, int ch, int direction, int index)
  555. {
  556. u32 val, parm;
  557. if (info->status & (INFO_AMP_VOL << ch))
  558. return;
  559. parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
  560. parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
  561. parm |= index;
  562. val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_AMP_GAIN_MUTE, parm);
  563. info->vol[ch] = val & 0xff;
  564. info->status |= INFO_AMP_VOL << ch;
  565. }
  566. /*
  567. * write the current volume in info to the h/w
  568. */
  569. static void put_vol_mute(struct hda_codec *codec,
  570. hda_nid_t nid, int ch, int direction, int index, int val)
  571. {
  572. u32 parm;
  573. parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
  574. parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
  575. parm |= index << AC_AMP_SET_INDEX_SHIFT;
  576. parm |= val;
  577. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
  578. }
  579. /*
  580. * read/write AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
  581. */
  582. static int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch, int direction, int index)
  583. {
  584. struct hda_amp_info *info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
  585. if (! info)
  586. return 0;
  587. get_vol_mute(codec, info, nid, ch, direction, index);
  588. return info->vol[ch];
  589. }
  590. static int snd_hda_codec_amp_write(struct hda_codec *codec, hda_nid_t nid, int ch, int direction, int idx, int val)
  591. {
  592. struct hda_amp_info *info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
  593. if (! info)
  594. return 0;
  595. get_vol_mute(codec, info, nid, ch, direction, idx);
  596. if (info->vol[ch] == val && ! codec->in_resume)
  597. return 0;
  598. put_vol_mute(codec, nid, ch, direction, idx, val);
  599. info->vol[ch] = val;
  600. return 1;
  601. }
  602. /*
  603. * AMP control callbacks
  604. */
  605. /* retrieve parameters from private_value */
  606. #define get_amp_nid(kc) ((kc)->private_value & 0xffff)
  607. #define get_amp_channels(kc) (((kc)->private_value >> 16) & 0x3)
  608. #define get_amp_direction(kc) (((kc)->private_value >> 18) & 0x1)
  609. #define get_amp_index(kc) (((kc)->private_value >> 19) & 0xf)
  610. /* volume */
  611. int snd_hda_mixer_amp_volume_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
  612. {
  613. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  614. u16 nid = get_amp_nid(kcontrol);
  615. u8 chs = get_amp_channels(kcontrol);
  616. int dir = get_amp_direction(kcontrol);
  617. u32 caps;
  618. caps = query_amp_caps(codec, nid, dir);
  619. caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; /* num steps */
  620. if (! caps) {
  621. printk(KERN_WARNING "hda_codec: num_steps = 0 for NID=0x%x\n", nid);
  622. return -EINVAL;
  623. }
  624. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  625. uinfo->count = chs == 3 ? 2 : 1;
  626. uinfo->value.integer.min = 0;
  627. uinfo->value.integer.max = caps;
  628. return 0;
  629. }
  630. int snd_hda_mixer_amp_volume_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  631. {
  632. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  633. hda_nid_t nid = get_amp_nid(kcontrol);
  634. int chs = get_amp_channels(kcontrol);
  635. int dir = get_amp_direction(kcontrol);
  636. int idx = get_amp_index(kcontrol);
  637. long *valp = ucontrol->value.integer.value;
  638. if (chs & 1)
  639. *valp++ = snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 0x7f;
  640. if (chs & 2)
  641. *valp = snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 0x7f;
  642. return 0;
  643. }
  644. int snd_hda_mixer_amp_volume_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  645. {
  646. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  647. hda_nid_t nid = get_amp_nid(kcontrol);
  648. int chs = get_amp_channels(kcontrol);
  649. int dir = get_amp_direction(kcontrol);
  650. int idx = get_amp_index(kcontrol);
  651. int val;
  652. long *valp = ucontrol->value.integer.value;
  653. int change = 0;
  654. if (chs & 1) {
  655. val = *valp & 0x7f;
  656. val |= snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 0x80;
  657. change = snd_hda_codec_amp_write(codec, nid, 0, dir, idx, val);
  658. valp++;
  659. }
  660. if (chs & 2) {
  661. val = *valp & 0x7f;
  662. val |= snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 0x80;
  663. change |= snd_hda_codec_amp_write(codec, nid, 1, dir, idx, val);
  664. }
  665. return change;
  666. }
  667. /* switch */
  668. int snd_hda_mixer_amp_switch_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
  669. {
  670. int chs = get_amp_channels(kcontrol);
  671. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  672. uinfo->count = chs == 3 ? 2 : 1;
  673. uinfo->value.integer.min = 0;
  674. uinfo->value.integer.max = 1;
  675. return 0;
  676. }
  677. int snd_hda_mixer_amp_switch_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  678. {
  679. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  680. hda_nid_t nid = get_amp_nid(kcontrol);
  681. int chs = get_amp_channels(kcontrol);
  682. int dir = get_amp_direction(kcontrol);
  683. int idx = get_amp_index(kcontrol);
  684. long *valp = ucontrol->value.integer.value;
  685. if (chs & 1)
  686. *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 0x80) ? 0 : 1;
  687. if (chs & 2)
  688. *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 0x80) ? 0 : 1;
  689. return 0;
  690. }
  691. int snd_hda_mixer_amp_switch_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  692. {
  693. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  694. hda_nid_t nid = get_amp_nid(kcontrol);
  695. int chs = get_amp_channels(kcontrol);
  696. int dir = get_amp_direction(kcontrol);
  697. int idx = get_amp_index(kcontrol);
  698. int val;
  699. long *valp = ucontrol->value.integer.value;
  700. int change = 0;
  701. if (chs & 1) {
  702. val = snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 0x7f;
  703. val |= *valp ? 0 : 0x80;
  704. change = snd_hda_codec_amp_write(codec, nid, 0, dir, idx, val);
  705. valp++;
  706. }
  707. if (chs & 2) {
  708. val = snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 0x7f;
  709. val |= *valp ? 0 : 0x80;
  710. change = snd_hda_codec_amp_write(codec, nid, 1, dir, idx, val);
  711. }
  712. return change;
  713. }
  714. /*
  715. * SPDIF out controls
  716. */
  717. static int snd_hda_spdif_mask_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
  718. {
  719. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  720. uinfo->count = 1;
  721. return 0;
  722. }
  723. static int snd_hda_spdif_cmask_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  724. {
  725. ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
  726. IEC958_AES0_NONAUDIO |
  727. IEC958_AES0_CON_EMPHASIS_5015 |
  728. IEC958_AES0_CON_NOT_COPYRIGHT;
  729. ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
  730. IEC958_AES1_CON_ORIGINAL;
  731. return 0;
  732. }
  733. static int snd_hda_spdif_pmask_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  734. {
  735. ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
  736. IEC958_AES0_NONAUDIO |
  737. IEC958_AES0_PRO_EMPHASIS_5015;
  738. return 0;
  739. }
  740. static int snd_hda_spdif_default_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  741. {
  742. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  743. ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
  744. ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
  745. ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
  746. ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
  747. return 0;
  748. }
  749. /* convert from SPDIF status bits to HDA SPDIF bits
  750. * bit 0 (DigEn) is always set zero (to be filled later)
  751. */
  752. static unsigned short convert_from_spdif_status(unsigned int sbits)
  753. {
  754. unsigned short val = 0;
  755. if (sbits & IEC958_AES0_PROFESSIONAL)
  756. val |= 1 << 6;
  757. if (sbits & IEC958_AES0_NONAUDIO)
  758. val |= 1 << 5;
  759. if (sbits & IEC958_AES0_PROFESSIONAL) {
  760. if ((sbits & IEC958_AES0_PRO_EMPHASIS) == IEC958_AES0_PRO_EMPHASIS_5015)
  761. val |= 1 << 3;
  762. } else {
  763. if ((sbits & IEC958_AES0_CON_EMPHASIS) == IEC958_AES0_CON_EMPHASIS_5015)
  764. val |= 1 << 3;
  765. if (! (sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
  766. val |= 1 << 4;
  767. if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
  768. val |= 1 << 7;
  769. val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
  770. }
  771. return val;
  772. }
  773. /* convert to SPDIF status bits from HDA SPDIF bits
  774. */
  775. static unsigned int convert_to_spdif_status(unsigned short val)
  776. {
  777. unsigned int sbits = 0;
  778. if (val & (1 << 5))
  779. sbits |= IEC958_AES0_NONAUDIO;
  780. if (val & (1 << 6))
  781. sbits |= IEC958_AES0_PROFESSIONAL;
  782. if (sbits & IEC958_AES0_PROFESSIONAL) {
  783. if (sbits & (1 << 3))
  784. sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
  785. } else {
  786. if (val & (1 << 3))
  787. sbits |= IEC958_AES0_CON_EMPHASIS_5015;
  788. if (! (val & (1 << 4)))
  789. sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
  790. if (val & (1 << 7))
  791. sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
  792. sbits |= val & (0x7f << 8);
  793. }
  794. return sbits;
  795. }
  796. static int snd_hda_spdif_default_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  797. {
  798. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  799. hda_nid_t nid = kcontrol->private_value;
  800. unsigned short val;
  801. int change;
  802. down(&codec->spdif_mutex);
  803. codec->spdif_status = ucontrol->value.iec958.status[0] |
  804. ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
  805. ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
  806. ((unsigned int)ucontrol->value.iec958.status[3] << 24);
  807. val = convert_from_spdif_status(codec->spdif_status);
  808. val |= codec->spdif_ctls & 1;
  809. change = codec->spdif_ctls != val;
  810. codec->spdif_ctls = val;
  811. if (change || codec->in_resume) {
  812. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, val & 0xff);
  813. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_2, val >> 8);
  814. }
  815. up(&codec->spdif_mutex);
  816. return change;
  817. }
  818. static int snd_hda_spdif_out_switch_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
  819. {
  820. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  821. uinfo->count = 1;
  822. uinfo->value.integer.min = 0;
  823. uinfo->value.integer.max = 1;
  824. return 0;
  825. }
  826. static int snd_hda_spdif_out_switch_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  827. {
  828. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  829. ucontrol->value.integer.value[0] = codec->spdif_ctls & 1;
  830. return 0;
  831. }
  832. static int snd_hda_spdif_out_switch_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  833. {
  834. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  835. hda_nid_t nid = kcontrol->private_value;
  836. unsigned short val;
  837. int change;
  838. down(&codec->spdif_mutex);
  839. val = codec->spdif_ctls & ~1;
  840. if (ucontrol->value.integer.value[0])
  841. val |= 1;
  842. change = codec->spdif_ctls != val;
  843. if (change || codec->in_resume) {
  844. codec->spdif_ctls = val;
  845. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, val & 0xff);
  846. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
  847. AC_AMP_SET_RIGHT | AC_AMP_SET_LEFT |
  848. AC_AMP_SET_OUTPUT | ((val & 1) ? 0 : 0x80));
  849. }
  850. up(&codec->spdif_mutex);
  851. return change;
  852. }
  853. static snd_kcontrol_new_t dig_mixes[] = {
  854. {
  855. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  856. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  857. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
  858. .info = snd_hda_spdif_mask_info,
  859. .get = snd_hda_spdif_cmask_get,
  860. },
  861. {
  862. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  863. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  864. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
  865. .info = snd_hda_spdif_mask_info,
  866. .get = snd_hda_spdif_pmask_get,
  867. },
  868. {
  869. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  870. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
  871. .info = snd_hda_spdif_mask_info,
  872. .get = snd_hda_spdif_default_get,
  873. .put = snd_hda_spdif_default_put,
  874. },
  875. {
  876. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  877. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
  878. .info = snd_hda_spdif_out_switch_info,
  879. .get = snd_hda_spdif_out_switch_get,
  880. .put = snd_hda_spdif_out_switch_put,
  881. },
  882. { } /* end */
  883. };
  884. /**
  885. * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
  886. * @codec: the HDA codec
  887. * @nid: audio out widget NID
  888. *
  889. * Creates controls related with the SPDIF output.
  890. * Called from each patch supporting the SPDIF out.
  891. *
  892. * Returns 0 if successful, or a negative error code.
  893. */
  894. int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
  895. {
  896. int err;
  897. snd_kcontrol_t *kctl;
  898. snd_kcontrol_new_t *dig_mix;
  899. for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
  900. kctl = snd_ctl_new1(dig_mix, codec);
  901. kctl->private_value = nid;
  902. if ((err = snd_ctl_add(codec->bus->card, kctl)) < 0)
  903. return err;
  904. }
  905. codec->spdif_ctls = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0);
  906. codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
  907. return 0;
  908. }
  909. /*
  910. * SPDIF input
  911. */
  912. #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
  913. static int snd_hda_spdif_in_switch_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  914. {
  915. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  916. ucontrol->value.integer.value[0] = codec->spdif_in_enable;
  917. return 0;
  918. }
  919. static int snd_hda_spdif_in_switch_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  920. {
  921. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  922. hda_nid_t nid = kcontrol->private_value;
  923. unsigned int val = !!ucontrol->value.integer.value[0];
  924. int change;
  925. down(&codec->spdif_mutex);
  926. change = codec->spdif_in_enable != val;
  927. if (change || codec->in_resume) {
  928. codec->spdif_in_enable = val;
  929. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, val);
  930. }
  931. up(&codec->spdif_mutex);
  932. return change;
  933. }
  934. static int snd_hda_spdif_in_status_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  935. {
  936. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  937. hda_nid_t nid = kcontrol->private_value;
  938. unsigned short val;
  939. unsigned int sbits;
  940. val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0);
  941. sbits = convert_to_spdif_status(val);
  942. ucontrol->value.iec958.status[0] = sbits;
  943. ucontrol->value.iec958.status[1] = sbits >> 8;
  944. ucontrol->value.iec958.status[2] = sbits >> 16;
  945. ucontrol->value.iec958.status[3] = sbits >> 24;
  946. return 0;
  947. }
  948. static snd_kcontrol_new_t dig_in_ctls[] = {
  949. {
  950. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  951. .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
  952. .info = snd_hda_spdif_in_switch_info,
  953. .get = snd_hda_spdif_in_switch_get,
  954. .put = snd_hda_spdif_in_switch_put,
  955. },
  956. {
  957. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  958. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  959. .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
  960. .info = snd_hda_spdif_mask_info,
  961. .get = snd_hda_spdif_in_status_get,
  962. },
  963. { } /* end */
  964. };
  965. /**
  966. * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
  967. * @codec: the HDA codec
  968. * @nid: audio in widget NID
  969. *
  970. * Creates controls related with the SPDIF input.
  971. * Called from each patch supporting the SPDIF in.
  972. *
  973. * Returns 0 if successful, or a negative error code.
  974. */
  975. int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
  976. {
  977. int err;
  978. snd_kcontrol_t *kctl;
  979. snd_kcontrol_new_t *dig_mix;
  980. for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
  981. kctl = snd_ctl_new1(dig_mix, codec);
  982. kctl->private_value = nid;
  983. if ((err = snd_ctl_add(codec->bus->card, kctl)) < 0)
  984. return err;
  985. }
  986. codec->spdif_in_enable = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0) & 1;
  987. return 0;
  988. }
  989. /**
  990. * snd_hda_build_controls - build mixer controls
  991. * @bus: the BUS
  992. *
  993. * Creates mixer controls for each codec included in the bus.
  994. *
  995. * Returns 0 if successful, otherwise a negative error code.
  996. */
  997. int snd_hda_build_controls(struct hda_bus *bus)
  998. {
  999. struct list_head *p;
  1000. /* build controls */
  1001. list_for_each(p, &bus->codec_list) {
  1002. struct hda_codec *codec = list_entry(p, struct hda_codec, list);
  1003. int err;
  1004. if (! codec->patch_ops.build_controls)
  1005. continue;
  1006. err = codec->patch_ops.build_controls(codec);
  1007. if (err < 0)
  1008. return err;
  1009. }
  1010. /* initialize */
  1011. list_for_each(p, &bus->codec_list) {
  1012. struct hda_codec *codec = list_entry(p, struct hda_codec, list);
  1013. int err;
  1014. if (! codec->patch_ops.init)
  1015. continue;
  1016. err = codec->patch_ops.init(codec);
  1017. if (err < 0)
  1018. return err;
  1019. }
  1020. return 0;
  1021. }
  1022. /*
  1023. * stream formats
  1024. */
  1025. static unsigned int rate_bits[][3] = {
  1026. /* rate in Hz, ALSA rate bitmask, HDA format value */
  1027. { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
  1028. { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
  1029. { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
  1030. { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
  1031. { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
  1032. { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
  1033. { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
  1034. { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
  1035. { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
  1036. { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
  1037. { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
  1038. { 0 }
  1039. };
  1040. /**
  1041. * snd_hda_calc_stream_format - calculate format bitset
  1042. * @rate: the sample rate
  1043. * @channels: the number of channels
  1044. * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
  1045. * @maxbps: the max. bps
  1046. *
  1047. * Calculate the format bitset from the given rate, channels and th PCM format.
  1048. *
  1049. * Return zero if invalid.
  1050. */
  1051. unsigned int snd_hda_calc_stream_format(unsigned int rate,
  1052. unsigned int channels,
  1053. unsigned int format,
  1054. unsigned int maxbps)
  1055. {
  1056. int i;
  1057. unsigned int val = 0;
  1058. for (i = 0; rate_bits[i][0]; i++)
  1059. if (rate_bits[i][0] == rate) {
  1060. val = rate_bits[i][2];
  1061. break;
  1062. }
  1063. if (! rate_bits[i][0]) {
  1064. snd_printdd("invalid rate %d\n", rate);
  1065. return 0;
  1066. }
  1067. if (channels == 0 || channels > 8) {
  1068. snd_printdd("invalid channels %d\n", channels);
  1069. return 0;
  1070. }
  1071. val |= channels - 1;
  1072. switch (snd_pcm_format_width(format)) {
  1073. case 8: val |= 0x00; break;
  1074. case 16: val |= 0x10; break;
  1075. case 20:
  1076. case 24:
  1077. case 32:
  1078. if (maxbps >= 32)
  1079. val |= 0x40;
  1080. else if (maxbps >= 24)
  1081. val |= 0x30;
  1082. else
  1083. val |= 0x20;
  1084. break;
  1085. default:
  1086. snd_printdd("invalid format width %d\n", snd_pcm_format_width(format));
  1087. return 0;
  1088. }
  1089. return val;
  1090. }
  1091. /**
  1092. * snd_hda_query_supported_pcm - query the supported PCM rates and formats
  1093. * @codec: the HDA codec
  1094. * @nid: NID to query
  1095. * @ratesp: the pointer to store the detected rate bitflags
  1096. * @formatsp: the pointer to store the detected formats
  1097. * @bpsp: the pointer to store the detected format widths
  1098. *
  1099. * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
  1100. * or @bsps argument is ignored.
  1101. *
  1102. * Returns 0 if successful, otherwise a negative error code.
  1103. */
  1104. int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
  1105. u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
  1106. {
  1107. int i;
  1108. unsigned int val, streams;
  1109. val = 0;
  1110. if (nid != codec->afg &&
  1111. snd_hda_param_read(codec, nid, AC_PAR_AUDIO_WIDGET_CAP) & AC_WCAP_FORMAT_OVRD) {
  1112. val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
  1113. if (val == -1)
  1114. return -EIO;
  1115. }
  1116. if (! val)
  1117. val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
  1118. if (ratesp) {
  1119. u32 rates = 0;
  1120. for (i = 0; rate_bits[i][0]; i++) {
  1121. if (val & (1 << i))
  1122. rates |= rate_bits[i][1];
  1123. }
  1124. *ratesp = rates;
  1125. }
  1126. if (formatsp || bpsp) {
  1127. u64 formats = 0;
  1128. unsigned int bps;
  1129. unsigned int wcaps;
  1130. wcaps = snd_hda_param_read(codec, nid, AC_PAR_AUDIO_WIDGET_CAP);
  1131. streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
  1132. if (streams == -1)
  1133. return -EIO;
  1134. if (! streams) {
  1135. streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
  1136. if (streams == -1)
  1137. return -EIO;
  1138. }
  1139. bps = 0;
  1140. if (streams & AC_SUPFMT_PCM) {
  1141. if (val & AC_SUPPCM_BITS_8) {
  1142. formats |= SNDRV_PCM_FMTBIT_U8;
  1143. bps = 8;
  1144. }
  1145. if (val & AC_SUPPCM_BITS_16) {
  1146. formats |= SNDRV_PCM_FMTBIT_S16_LE;
  1147. bps = 16;
  1148. }
  1149. if (wcaps & AC_WCAP_DIGITAL) {
  1150. if (val & AC_SUPPCM_BITS_32)
  1151. formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
  1152. if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
  1153. formats |= SNDRV_PCM_FMTBIT_S32_LE;
  1154. if (val & AC_SUPPCM_BITS_24)
  1155. bps = 24;
  1156. else if (val & AC_SUPPCM_BITS_20)
  1157. bps = 20;
  1158. } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|AC_SUPPCM_BITS_32)) {
  1159. formats |= SNDRV_PCM_FMTBIT_S32_LE;
  1160. if (val & AC_SUPPCM_BITS_32)
  1161. bps = 32;
  1162. else if (val & AC_SUPPCM_BITS_20)
  1163. bps = 20;
  1164. else if (val & AC_SUPPCM_BITS_24)
  1165. bps = 24;
  1166. }
  1167. }
  1168. else if (streams == AC_SUPFMT_FLOAT32) { /* should be exclusive */
  1169. formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
  1170. bps = 32;
  1171. } else if (streams == AC_SUPFMT_AC3) { /* should be exclusive */
  1172. /* temporary hack: we have still no proper support
  1173. * for the direct AC3 stream...
  1174. */
  1175. formats |= SNDRV_PCM_FMTBIT_U8;
  1176. bps = 8;
  1177. }
  1178. if (formatsp)
  1179. *formatsp = formats;
  1180. if (bpsp)
  1181. *bpsp = bps;
  1182. }
  1183. return 0;
  1184. }
  1185. /**
  1186. * snd_hda_is_supported_format - check whether the given node supports the format val
  1187. *
  1188. * Returns 1 if supported, 0 if not.
  1189. */
  1190. int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
  1191. unsigned int format)
  1192. {
  1193. int i;
  1194. unsigned int val = 0, rate, stream;
  1195. if (nid != codec->afg &&
  1196. snd_hda_param_read(codec, nid, AC_PAR_AUDIO_WIDGET_CAP) & AC_WCAP_FORMAT_OVRD) {
  1197. val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
  1198. if (val == -1)
  1199. return 0;
  1200. }
  1201. if (! val) {
  1202. val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
  1203. if (val == -1)
  1204. return 0;
  1205. }
  1206. rate = format & 0xff00;
  1207. for (i = 0; rate_bits[i][0]; i++)
  1208. if (rate_bits[i][2] == rate) {
  1209. if (val & (1 << i))
  1210. break;
  1211. return 0;
  1212. }
  1213. if (! rate_bits[i][0])
  1214. return 0;
  1215. stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
  1216. if (stream == -1)
  1217. return 0;
  1218. if (! stream && nid != codec->afg)
  1219. stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
  1220. if (! stream || stream == -1)
  1221. return 0;
  1222. if (stream & AC_SUPFMT_PCM) {
  1223. switch (format & 0xf0) {
  1224. case 0x00:
  1225. if (! (val & AC_SUPPCM_BITS_8))
  1226. return 0;
  1227. break;
  1228. case 0x10:
  1229. if (! (val & AC_SUPPCM_BITS_16))
  1230. return 0;
  1231. break;
  1232. case 0x20:
  1233. if (! (val & AC_SUPPCM_BITS_20))
  1234. return 0;
  1235. break;
  1236. case 0x30:
  1237. if (! (val & AC_SUPPCM_BITS_24))
  1238. return 0;
  1239. break;
  1240. case 0x40:
  1241. if (! (val & AC_SUPPCM_BITS_32))
  1242. return 0;
  1243. break;
  1244. default:
  1245. return 0;
  1246. }
  1247. } else {
  1248. /* FIXME: check for float32 and AC3? */
  1249. }
  1250. return 1;
  1251. }
  1252. /*
  1253. * PCM stuff
  1254. */
  1255. static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
  1256. struct hda_codec *codec,
  1257. snd_pcm_substream_t *substream)
  1258. {
  1259. return 0;
  1260. }
  1261. static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
  1262. struct hda_codec *codec,
  1263. unsigned int stream_tag,
  1264. unsigned int format,
  1265. snd_pcm_substream_t *substream)
  1266. {
  1267. snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
  1268. return 0;
  1269. }
  1270. static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
  1271. struct hda_codec *codec,
  1272. snd_pcm_substream_t *substream)
  1273. {
  1274. snd_hda_codec_setup_stream(codec, hinfo->nid, 0, 0, 0);
  1275. return 0;
  1276. }
  1277. static int set_pcm_default_values(struct hda_codec *codec, struct hda_pcm_stream *info)
  1278. {
  1279. if (info->nid) {
  1280. /* query support PCM information from the given NID */
  1281. if (! info->rates || ! info->formats)
  1282. snd_hda_query_supported_pcm(codec, info->nid,
  1283. info->rates ? NULL : &info->rates,
  1284. info->formats ? NULL : &info->formats,
  1285. info->maxbps ? NULL : &info->maxbps);
  1286. }
  1287. if (info->ops.open == NULL)
  1288. info->ops.open = hda_pcm_default_open_close;
  1289. if (info->ops.close == NULL)
  1290. info->ops.close = hda_pcm_default_open_close;
  1291. if (info->ops.prepare == NULL) {
  1292. snd_assert(info->nid, return -EINVAL);
  1293. info->ops.prepare = hda_pcm_default_prepare;
  1294. }
  1295. if (info->ops.cleanup == NULL) {
  1296. snd_assert(info->nid, return -EINVAL);
  1297. info->ops.cleanup = hda_pcm_default_cleanup;
  1298. }
  1299. return 0;
  1300. }
  1301. /**
  1302. * snd_hda_build_pcms - build PCM information
  1303. * @bus: the BUS
  1304. *
  1305. * Create PCM information for each codec included in the bus.
  1306. *
  1307. * The build_pcms codec patch is requested to set up codec->num_pcms and
  1308. * codec->pcm_info properly. The array is referred by the top-level driver
  1309. * to create its PCM instances.
  1310. * The allocated codec->pcm_info should be released in codec->patch_ops.free
  1311. * callback.
  1312. *
  1313. * At least, substreams, channels_min and channels_max must be filled for
  1314. * each stream. substreams = 0 indicates that the stream doesn't exist.
  1315. * When rates and/or formats are zero, the supported values are queried
  1316. * from the given nid. The nid is used also by the default ops.prepare
  1317. * and ops.cleanup callbacks.
  1318. *
  1319. * The driver needs to call ops.open in its open callback. Similarly,
  1320. * ops.close is supposed to be called in the close callback.
  1321. * ops.prepare should be called in the prepare or hw_params callback
  1322. * with the proper parameters for set up.
  1323. * ops.cleanup should be called in hw_free for clean up of streams.
  1324. *
  1325. * This function returns 0 if successfull, or a negative error code.
  1326. */
  1327. int snd_hda_build_pcms(struct hda_bus *bus)
  1328. {
  1329. struct list_head *p;
  1330. list_for_each(p, &bus->codec_list) {
  1331. struct hda_codec *codec = list_entry(p, struct hda_codec, list);
  1332. unsigned int pcm, s;
  1333. int err;
  1334. if (! codec->patch_ops.build_pcms)
  1335. continue;
  1336. err = codec->patch_ops.build_pcms(codec);
  1337. if (err < 0)
  1338. return err;
  1339. for (pcm = 0; pcm < codec->num_pcms; pcm++) {
  1340. for (s = 0; s < 2; s++) {
  1341. struct hda_pcm_stream *info;
  1342. info = &codec->pcm_info[pcm].stream[s];
  1343. if (! info->substreams)
  1344. continue;
  1345. err = set_pcm_default_values(codec, info);
  1346. if (err < 0)
  1347. return err;
  1348. }
  1349. }
  1350. }
  1351. return 0;
  1352. }
  1353. /**
  1354. * snd_hda_check_board_config - compare the current codec with the config table
  1355. * @codec: the HDA codec
  1356. * @tbl: configuration table, terminated by null entries
  1357. *
  1358. * Compares the modelname or PCI subsystem id of the current codec with the
  1359. * given configuration table. If a matching entry is found, returns its
  1360. * config value (supposed to be 0 or positive).
  1361. *
  1362. * If no entries are matching, the function returns a negative value.
  1363. */
  1364. int snd_hda_check_board_config(struct hda_codec *codec, struct hda_board_config *tbl)
  1365. {
  1366. struct hda_board_config *c;
  1367. if (codec->bus->modelname) {
  1368. for (c = tbl; c->modelname || c->pci_subvendor; c++) {
  1369. if (c->modelname &&
  1370. ! strcmp(codec->bus->modelname, c->modelname)) {
  1371. snd_printd(KERN_INFO "hda_codec: model '%s' is selected\n", c->modelname);
  1372. return c->config;
  1373. }
  1374. }
  1375. }
  1376. if (codec->bus->pci) {
  1377. u16 subsystem_vendor, subsystem_device;
  1378. pci_read_config_word(codec->bus->pci, PCI_SUBSYSTEM_VENDOR_ID, &subsystem_vendor);
  1379. pci_read_config_word(codec->bus->pci, PCI_SUBSYSTEM_ID, &subsystem_device);
  1380. for (c = tbl; c->modelname || c->pci_subvendor; c++) {
  1381. if (c->pci_subvendor == subsystem_vendor &&
  1382. c->pci_subdevice == subsystem_device)
  1383. return c->config;
  1384. }
  1385. }
  1386. return -1;
  1387. }
  1388. /**
  1389. * snd_hda_add_new_ctls - create controls from the array
  1390. * @codec: the HDA codec
  1391. * @knew: the array of snd_kcontrol_new_t
  1392. *
  1393. * This helper function creates and add new controls in the given array.
  1394. * The array must be terminated with an empty entry as terminator.
  1395. *
  1396. * Returns 0 if successful, or a negative error code.
  1397. */
  1398. int snd_hda_add_new_ctls(struct hda_codec *codec, snd_kcontrol_new_t *knew)
  1399. {
  1400. int err;
  1401. for (; knew->name; knew++) {
  1402. err = snd_ctl_add(codec->bus->card, snd_ctl_new1(knew, codec));
  1403. if (err < 0)
  1404. return err;
  1405. }
  1406. return 0;
  1407. }
  1408. /*
  1409. * input MUX helper
  1410. */
  1411. int snd_hda_input_mux_info(const struct hda_input_mux *imux, snd_ctl_elem_info_t *uinfo)
  1412. {
  1413. unsigned int index;
  1414. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1415. uinfo->count = 1;
  1416. uinfo->value.enumerated.items = imux->num_items;
  1417. index = uinfo->value.enumerated.item;
  1418. if (index >= imux->num_items)
  1419. index = imux->num_items - 1;
  1420. strcpy(uinfo->value.enumerated.name, imux->items[index].label);
  1421. return 0;
  1422. }
  1423. int snd_hda_input_mux_put(struct hda_codec *codec, const struct hda_input_mux *imux,
  1424. snd_ctl_elem_value_t *ucontrol, hda_nid_t nid,
  1425. unsigned int *cur_val)
  1426. {
  1427. unsigned int idx;
  1428. idx = ucontrol->value.enumerated.item[0];
  1429. if (idx >= imux->num_items)
  1430. idx = imux->num_items - 1;
  1431. if (*cur_val == idx && ! codec->in_resume)
  1432. return 0;
  1433. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
  1434. imux->items[idx].index);
  1435. *cur_val = idx;
  1436. return 1;
  1437. }
  1438. /*
  1439. * Multi-channel / digital-out PCM helper functions
  1440. */
  1441. /*
  1442. * open the digital out in the exclusive mode
  1443. */
  1444. int snd_hda_multi_out_dig_open(struct hda_codec *codec, struct hda_multi_out *mout)
  1445. {
  1446. down(&codec->spdif_mutex);
  1447. if (mout->dig_out_used) {
  1448. up(&codec->spdif_mutex);
  1449. return -EBUSY; /* already being used */
  1450. }
  1451. mout->dig_out_used = HDA_DIG_EXCLUSIVE;
  1452. up(&codec->spdif_mutex);
  1453. return 0;
  1454. }
  1455. /*
  1456. * release the digital out
  1457. */
  1458. int snd_hda_multi_out_dig_close(struct hda_codec *codec, struct hda_multi_out *mout)
  1459. {
  1460. down(&codec->spdif_mutex);
  1461. mout->dig_out_used = 0;
  1462. up(&codec->spdif_mutex);
  1463. return 0;
  1464. }
  1465. /*
  1466. * set up more restrictions for analog out
  1467. */
  1468. int snd_hda_multi_out_analog_open(struct hda_codec *codec, struct hda_multi_out *mout,
  1469. snd_pcm_substream_t *substream)
  1470. {
  1471. substream->runtime->hw.channels_max = mout->max_channels;
  1472. return snd_pcm_hw_constraint_step(substream->runtime, 0,
  1473. SNDRV_PCM_HW_PARAM_CHANNELS, 2);
  1474. }
  1475. /*
  1476. * set up the i/o for analog out
  1477. * when the digital out is available, copy the front out to digital out, too.
  1478. */
  1479. int snd_hda_multi_out_analog_prepare(struct hda_codec *codec, struct hda_multi_out *mout,
  1480. unsigned int stream_tag,
  1481. unsigned int format,
  1482. snd_pcm_substream_t *substream)
  1483. {
  1484. hda_nid_t *nids = mout->dac_nids;
  1485. int chs = substream->runtime->channels;
  1486. int i;
  1487. down(&codec->spdif_mutex);
  1488. if (mout->dig_out_nid && mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
  1489. if (chs == 2 &&
  1490. snd_hda_is_supported_format(codec, mout->dig_out_nid, format) &&
  1491. ! (codec->spdif_status & IEC958_AES0_NONAUDIO)) {
  1492. mout->dig_out_used = HDA_DIG_ANALOG_DUP;
  1493. /* setup digital receiver */
  1494. snd_hda_codec_setup_stream(codec, mout->dig_out_nid,
  1495. stream_tag, 0, format);
  1496. } else {
  1497. mout->dig_out_used = 0;
  1498. snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
  1499. }
  1500. }
  1501. up(&codec->spdif_mutex);
  1502. /* front */
  1503. snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag, 0, format);
  1504. if (mout->hp_nid)
  1505. /* headphone out will just decode front left/right (stereo) */
  1506. snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag, 0, format);
  1507. /* surrounds */
  1508. for (i = 1; i < mout->num_dacs; i++) {
  1509. if (i == HDA_REAR && chs == 2) /* copy front to rear */
  1510. snd_hda_codec_setup_stream(codec, nids[i], stream_tag, 0, format);
  1511. else if (chs >= (i + 1) * 2) /* independent out */
  1512. snd_hda_codec_setup_stream(codec, nids[i], stream_tag, i * 2,
  1513. format);
  1514. }
  1515. return 0;
  1516. }
  1517. /*
  1518. * clean up the setting for analog out
  1519. */
  1520. int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec, struct hda_multi_out *mout)
  1521. {
  1522. hda_nid_t *nids = mout->dac_nids;
  1523. int i;
  1524. for (i = 0; i < mout->num_dacs; i++)
  1525. snd_hda_codec_setup_stream(codec, nids[i], 0, 0, 0);
  1526. if (mout->hp_nid)
  1527. snd_hda_codec_setup_stream(codec, mout->hp_nid, 0, 0, 0);
  1528. down(&codec->spdif_mutex);
  1529. if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
  1530. snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
  1531. mout->dig_out_used = 0;
  1532. }
  1533. up(&codec->spdif_mutex);
  1534. return 0;
  1535. }
  1536. #ifdef CONFIG_PM
  1537. /*
  1538. * power management
  1539. */
  1540. /**
  1541. * snd_hda_suspend - suspend the codecs
  1542. * @bus: the HDA bus
  1543. * @state: suspsend state
  1544. *
  1545. * Returns 0 if successful.
  1546. */
  1547. int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
  1548. {
  1549. struct list_head *p;
  1550. /* FIXME: should handle power widget capabilities */
  1551. list_for_each(p, &bus->codec_list) {
  1552. struct hda_codec *codec = list_entry(p, struct hda_codec, list);
  1553. if (codec->patch_ops.suspend)
  1554. codec->patch_ops.suspend(codec, state);
  1555. }
  1556. return 0;
  1557. }
  1558. /**
  1559. * snd_hda_resume - resume the codecs
  1560. * @bus: the HDA bus
  1561. * @state: resume state
  1562. *
  1563. * Returns 0 if successful.
  1564. */
  1565. int snd_hda_resume(struct hda_bus *bus)
  1566. {
  1567. struct list_head *p;
  1568. list_for_each(p, &bus->codec_list) {
  1569. struct hda_codec *codec = list_entry(p, struct hda_codec, list);
  1570. if (codec->patch_ops.resume)
  1571. codec->patch_ops.resume(codec);
  1572. }
  1573. return 0;
  1574. }
  1575. /**
  1576. * snd_hda_resume_ctls - resume controls in the new control list
  1577. * @codec: the HDA codec
  1578. * @knew: the array of snd_kcontrol_new_t
  1579. *
  1580. * This function resumes the mixer controls in the snd_kcontrol_new_t array,
  1581. * originally for snd_hda_add_new_ctls().
  1582. * The array must be terminated with an empty entry as terminator.
  1583. */
  1584. int snd_hda_resume_ctls(struct hda_codec *codec, snd_kcontrol_new_t *knew)
  1585. {
  1586. snd_ctl_elem_value_t *val;
  1587. val = kmalloc(sizeof(*val), GFP_KERNEL);
  1588. if (! val)
  1589. return -ENOMEM;
  1590. codec->in_resume = 1;
  1591. for (; knew->name; knew++) {
  1592. int i, count;
  1593. count = knew->count ? knew->count : 1;
  1594. for (i = 0; i < count; i++) {
  1595. memset(val, 0, sizeof(*val));
  1596. val->id.iface = knew->iface;
  1597. val->id.device = knew->device;
  1598. val->id.subdevice = knew->subdevice;
  1599. strcpy(val->id.name, knew->name);
  1600. val->id.index = knew->index ? knew->index : i;
  1601. /* Assume that get callback reads only from cache,
  1602. * not accessing to the real hardware
  1603. */
  1604. if (snd_ctl_elem_read(codec->bus->card, val) < 0)
  1605. continue;
  1606. snd_ctl_elem_write(codec->bus->card, NULL, val);
  1607. }
  1608. }
  1609. codec->in_resume = 0;
  1610. kfree(val);
  1611. return 0;
  1612. }
  1613. /**
  1614. * snd_hda_resume_spdif_out - resume the digital out
  1615. * @codec: the HDA codec
  1616. */
  1617. int snd_hda_resume_spdif_out(struct hda_codec *codec)
  1618. {
  1619. return snd_hda_resume_ctls(codec, dig_mixes);
  1620. }
  1621. /**
  1622. * snd_hda_resume_spdif_in - resume the digital in
  1623. * @codec: the HDA codec
  1624. */
  1625. int snd_hda_resume_spdif_in(struct hda_codec *codec)
  1626. {
  1627. return snd_hda_resume_ctls(codec, dig_in_ctls);
  1628. }
  1629. #endif
  1630. /*
  1631. * symbols exported for controller modules
  1632. */
  1633. EXPORT_SYMBOL(snd_hda_codec_read);
  1634. EXPORT_SYMBOL(snd_hda_codec_write);
  1635. EXPORT_SYMBOL(snd_hda_sequence_write);
  1636. EXPORT_SYMBOL(snd_hda_get_sub_nodes);
  1637. EXPORT_SYMBOL(snd_hda_queue_unsol_event);
  1638. EXPORT_SYMBOL(snd_hda_bus_new);
  1639. EXPORT_SYMBOL(snd_hda_codec_new);
  1640. EXPORT_SYMBOL(snd_hda_codec_setup_stream);
  1641. EXPORT_SYMBOL(snd_hda_calc_stream_format);
  1642. EXPORT_SYMBOL(snd_hda_build_pcms);
  1643. EXPORT_SYMBOL(snd_hda_build_controls);
  1644. #ifdef CONFIG_PM
  1645. EXPORT_SYMBOL(snd_hda_suspend);
  1646. EXPORT_SYMBOL(snd_hda_resume);
  1647. #endif
  1648. /*
  1649. * INIT part
  1650. */
  1651. static int __init alsa_hda_init(void)
  1652. {
  1653. return 0;
  1654. }
  1655. static void __exit alsa_hda_exit(void)
  1656. {
  1657. }
  1658. module_init(alsa_hda_init)
  1659. module_exit(alsa_hda_exit)