hda_codec.c 49 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. {} /* terminator */
  48. };
  49. /* codec presets */
  50. #include "hda_patch.h"
  51. /**
  52. * snd_hda_codec_read - send a command and get the response
  53. * @codec: the HDA codec
  54. * @nid: NID to send the command
  55. * @direct: direct flag
  56. * @verb: the verb to send
  57. * @parm: the parameter for the verb
  58. *
  59. * Send a single command and read the corresponding response.
  60. *
  61. * Returns the obtained response value, or -1 for an error.
  62. */
  63. unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid, int direct,
  64. unsigned int verb, unsigned int parm)
  65. {
  66. unsigned int res;
  67. down(&codec->bus->cmd_mutex);
  68. if (! codec->bus->ops.command(codec, nid, direct, verb, parm))
  69. res = codec->bus->ops.get_response(codec);
  70. else
  71. res = (unsigned int)-1;
  72. up(&codec->bus->cmd_mutex);
  73. return res;
  74. }
  75. /**
  76. * snd_hda_codec_write - send a single command without waiting for response
  77. * @codec: the HDA codec
  78. * @nid: NID to send the command
  79. * @direct: direct flag
  80. * @verb: the verb to send
  81. * @parm: the parameter for the verb
  82. *
  83. * Send a single command without waiting for response.
  84. *
  85. * Returns 0 if successful, or a negative error code.
  86. */
  87. int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
  88. unsigned int verb, unsigned int parm)
  89. {
  90. int err;
  91. down(&codec->bus->cmd_mutex);
  92. err = codec->bus->ops.command(codec, nid, direct, verb, parm);
  93. up(&codec->bus->cmd_mutex);
  94. return err;
  95. }
  96. /**
  97. * snd_hda_sequence_write - sequence writes
  98. * @codec: the HDA codec
  99. * @seq: VERB array to send
  100. *
  101. * Send the commands sequentially from the given array.
  102. * The array must be terminated with NID=0.
  103. */
  104. void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
  105. {
  106. for (; seq->nid; seq++)
  107. snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
  108. }
  109. /**
  110. * snd_hda_get_sub_nodes - get the range of sub nodes
  111. * @codec: the HDA codec
  112. * @nid: NID to parse
  113. * @start_id: the pointer to store the start NID
  114. *
  115. * Parse the NID and store the start NID of its sub-nodes.
  116. * Returns the number of sub-nodes.
  117. */
  118. int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid, hda_nid_t *start_id)
  119. {
  120. unsigned int parm;
  121. parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
  122. *start_id = (parm >> 16) & 0x7fff;
  123. return (int)(parm & 0x7fff);
  124. }
  125. /**
  126. * snd_hda_get_connections - get connection list
  127. * @codec: the HDA codec
  128. * @nid: NID to parse
  129. * @conn_list: connection list array
  130. * @max_conns: max. number of connections to store
  131. *
  132. * Parses the connection list of the given widget and stores the list
  133. * of NIDs.
  134. *
  135. * Returns the number of connections, or a negative error code.
  136. */
  137. int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
  138. hda_nid_t *conn_list, int max_conns)
  139. {
  140. unsigned int parm;
  141. int i, j, conn_len, num_tupples, conns;
  142. unsigned int shift, num_elems, mask;
  143. snd_assert(conn_list && max_conns > 0, return -EINVAL);
  144. parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
  145. if (parm & AC_CLIST_LONG) {
  146. /* long form */
  147. shift = 16;
  148. num_elems = 2;
  149. } else {
  150. /* short form */
  151. shift = 8;
  152. num_elems = 4;
  153. }
  154. conn_len = parm & AC_CLIST_LENGTH;
  155. num_tupples = num_elems / 2;
  156. mask = (1 << (shift-1)) - 1;
  157. if (! conn_len)
  158. return 0; /* no connection */
  159. if (conn_len == 1) {
  160. /* single connection */
  161. parm = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONNECT_LIST, 0);
  162. conn_list[0] = parm & mask;
  163. return 1;
  164. }
  165. /* multi connection */
  166. conns = 0;
  167. for (i = 0; i < conn_len; i += num_elems) {
  168. parm = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONNECT_LIST, i);
  169. for (j = 0; j < num_tupples; j++) {
  170. int range_val;
  171. hda_nid_t val1, val2, n;
  172. range_val = parm & (1 << (shift-1)); /* ranges */
  173. val1 = parm & mask;
  174. parm >>= shift;
  175. val2 = parm & mask;
  176. parm >>= shift;
  177. if (range_val) {
  178. /* ranges between val1 and val2 */
  179. if (val1 > val2) {
  180. snd_printk(KERN_WARNING "hda_codec: invalid dep_range_val %x:%x\n", val1, val2);
  181. continue;
  182. }
  183. for (n = val1; n <= val2; n++) {
  184. if (conns >= max_conns)
  185. return -EINVAL;
  186. conn_list[conns++] = n;
  187. }
  188. } else {
  189. if (! val1)
  190. break;
  191. if (conns >= max_conns)
  192. return -EINVAL;
  193. conn_list[conns++] = val1;
  194. if (! val2)
  195. break;
  196. if (conns >= max_conns)
  197. return -EINVAL;
  198. conn_list[conns++] = val2;
  199. }
  200. }
  201. }
  202. return conns;
  203. }
  204. /**
  205. * snd_hda_queue_unsol_event - add an unsolicited event to queue
  206. * @bus: the BUS
  207. * @res: unsolicited event (lower 32bit of RIRB entry)
  208. * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
  209. *
  210. * Adds the given event to the queue. The events are processed in
  211. * the workqueue asynchronously. Call this function in the interrupt
  212. * hanlder when RIRB receives an unsolicited event.
  213. *
  214. * Returns 0 if successful, or a negative error code.
  215. */
  216. int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
  217. {
  218. struct hda_bus_unsolicited *unsol;
  219. unsigned int wp;
  220. if ((unsol = bus->unsol) == NULL)
  221. return 0;
  222. wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
  223. unsol->wp = wp;
  224. wp <<= 1;
  225. unsol->queue[wp] = res;
  226. unsol->queue[wp + 1] = res_ex;
  227. queue_work(unsol->workq, &unsol->work);
  228. return 0;
  229. }
  230. /*
  231. * process queueud unsolicited events
  232. */
  233. static void process_unsol_events(void *data)
  234. {
  235. struct hda_bus *bus = data;
  236. struct hda_bus_unsolicited *unsol = bus->unsol;
  237. struct hda_codec *codec;
  238. unsigned int rp, caddr, res;
  239. while (unsol->rp != unsol->wp) {
  240. rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
  241. unsol->rp = rp;
  242. rp <<= 1;
  243. res = unsol->queue[rp];
  244. caddr = unsol->queue[rp + 1];
  245. if (! (caddr & (1 << 4))) /* no unsolicited event? */
  246. continue;
  247. codec = bus->caddr_tbl[caddr & 0x0f];
  248. if (codec && codec->patch_ops.unsol_event)
  249. codec->patch_ops.unsol_event(codec, res);
  250. }
  251. }
  252. /*
  253. * initialize unsolicited queue
  254. */
  255. static int init_unsol_queue(struct hda_bus *bus)
  256. {
  257. struct hda_bus_unsolicited *unsol;
  258. unsol = kcalloc(1, sizeof(*unsol), GFP_KERNEL);
  259. if (! unsol) {
  260. snd_printk(KERN_ERR "hda_codec: can't allocate unsolicited queue\n");
  261. return -ENOMEM;
  262. }
  263. unsol->workq = create_workqueue("hda_codec");
  264. if (! unsol->workq) {
  265. snd_printk(KERN_ERR "hda_codec: can't create workqueue\n");
  266. kfree(unsol);
  267. return -ENOMEM;
  268. }
  269. INIT_WORK(&unsol->work, process_unsol_events, bus);
  270. bus->unsol = unsol;
  271. return 0;
  272. }
  273. /*
  274. * destructor
  275. */
  276. static void snd_hda_codec_free(struct hda_codec *codec);
  277. static int snd_hda_bus_free(struct hda_bus *bus)
  278. {
  279. struct list_head *p, *n;
  280. if (! bus)
  281. return 0;
  282. if (bus->unsol) {
  283. destroy_workqueue(bus->unsol->workq);
  284. kfree(bus->unsol);
  285. }
  286. list_for_each_safe(p, n, &bus->codec_list) {
  287. struct hda_codec *codec = list_entry(p, struct hda_codec, list);
  288. snd_hda_codec_free(codec);
  289. }
  290. if (bus->ops.private_free)
  291. bus->ops.private_free(bus);
  292. kfree(bus);
  293. return 0;
  294. }
  295. static int snd_hda_bus_dev_free(snd_device_t *device)
  296. {
  297. struct hda_bus *bus = device->device_data;
  298. return snd_hda_bus_free(bus);
  299. }
  300. /**
  301. * snd_hda_bus_new - create a HDA bus
  302. * @card: the card entry
  303. * @temp: the template for hda_bus information
  304. * @busp: the pointer to store the created bus instance
  305. *
  306. * Returns 0 if successful, or a negative error code.
  307. */
  308. int snd_hda_bus_new(snd_card_t *card, const struct hda_bus_template *temp,
  309. struct hda_bus **busp)
  310. {
  311. struct hda_bus *bus;
  312. int err;
  313. static snd_device_ops_t dev_ops = {
  314. .dev_free = snd_hda_bus_dev_free,
  315. };
  316. snd_assert(temp, return -EINVAL);
  317. snd_assert(temp->ops.command && temp->ops.get_response, return -EINVAL);
  318. if (busp)
  319. *busp = NULL;
  320. bus = kcalloc(1, sizeof(*bus), GFP_KERNEL);
  321. if (bus == NULL) {
  322. snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
  323. return -ENOMEM;
  324. }
  325. bus->card = card;
  326. bus->private_data = temp->private_data;
  327. bus->pci = temp->pci;
  328. bus->modelname = temp->modelname;
  329. bus->ops = temp->ops;
  330. init_MUTEX(&bus->cmd_mutex);
  331. INIT_LIST_HEAD(&bus->codec_list);
  332. init_unsol_queue(bus);
  333. if ((err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops)) < 0) {
  334. snd_hda_bus_free(bus);
  335. return err;
  336. }
  337. if (busp)
  338. *busp = bus;
  339. return 0;
  340. }
  341. /*
  342. * find a matching codec preset
  343. */
  344. static const struct hda_codec_preset *find_codec_preset(struct hda_codec *codec)
  345. {
  346. const struct hda_codec_preset **tbl, *preset;
  347. for (tbl = hda_preset_tables; *tbl; tbl++) {
  348. for (preset = *tbl; preset->id; preset++) {
  349. u32 mask = preset->mask;
  350. if (! mask)
  351. mask = ~0;
  352. if (preset->id == (codec->vendor_id & mask))
  353. return preset;
  354. }
  355. }
  356. return NULL;
  357. }
  358. /*
  359. * snd_hda_get_codec_name - store the codec name
  360. */
  361. void snd_hda_get_codec_name(struct hda_codec *codec,
  362. char *name, int namelen)
  363. {
  364. const struct hda_vendor_id *c;
  365. const char *vendor = NULL;
  366. u16 vendor_id = codec->vendor_id >> 16;
  367. char tmp[16];
  368. for (c = hda_vendor_ids; c->id; c++) {
  369. if (c->id == vendor_id) {
  370. vendor = c->name;
  371. break;
  372. }
  373. }
  374. if (! vendor) {
  375. sprintf(tmp, "Generic %04x", vendor_id);
  376. vendor = tmp;
  377. }
  378. if (codec->preset && codec->preset->name)
  379. snprintf(name, namelen, "%s %s", vendor, codec->preset->name);
  380. else
  381. snprintf(name, namelen, "%s ID %x", vendor, codec->vendor_id & 0xffff);
  382. }
  383. /*
  384. * look for an AFG node
  385. *
  386. * return 0 if not found
  387. */
  388. static int look_for_afg_node(struct hda_codec *codec)
  389. {
  390. int i, total_nodes;
  391. hda_nid_t nid;
  392. total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
  393. for (i = 0; i < total_nodes; i++, nid++) {
  394. if ((snd_hda_param_read(codec, nid, AC_PAR_FUNCTION_TYPE) & 0xff) ==
  395. AC_GRP_AUDIO_FUNCTION)
  396. return nid;
  397. }
  398. return 0;
  399. }
  400. /*
  401. * codec destructor
  402. */
  403. static void snd_hda_codec_free(struct hda_codec *codec)
  404. {
  405. if (! codec)
  406. return;
  407. list_del(&codec->list);
  408. codec->bus->caddr_tbl[codec->addr] = NULL;
  409. if (codec->patch_ops.free)
  410. codec->patch_ops.free(codec);
  411. kfree(codec);
  412. }
  413. static void init_amp_hash(struct hda_codec *codec);
  414. /**
  415. * snd_hda_codec_new - create a HDA codec
  416. * @bus: the bus to assign
  417. * @codec_addr: the codec address
  418. * @codecp: the pointer to store the generated codec
  419. *
  420. * Returns 0 if successful, or a negative error code.
  421. */
  422. int snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
  423. struct hda_codec **codecp)
  424. {
  425. struct hda_codec *codec;
  426. char component[13];
  427. int err;
  428. snd_assert(bus, return -EINVAL);
  429. snd_assert(codec_addr <= HDA_MAX_CODEC_ADDRESS, return -EINVAL);
  430. if (bus->caddr_tbl[codec_addr]) {
  431. snd_printk(KERN_ERR "hda_codec: address 0x%x is already occupied\n", codec_addr);
  432. return -EBUSY;
  433. }
  434. codec = kcalloc(1, sizeof(*codec), GFP_KERNEL);
  435. if (codec == NULL) {
  436. snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
  437. return -ENOMEM;
  438. }
  439. codec->bus = bus;
  440. codec->addr = codec_addr;
  441. init_MUTEX(&codec->spdif_mutex);
  442. init_amp_hash(codec);
  443. list_add_tail(&codec->list, &bus->codec_list);
  444. bus->caddr_tbl[codec_addr] = codec;
  445. codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, AC_PAR_VENDOR_ID);
  446. codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT, AC_PAR_SUBSYSTEM_ID);
  447. codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT, AC_PAR_REV_ID);
  448. /* FIXME: support for multiple AFGs? */
  449. codec->afg = look_for_afg_node(codec);
  450. if (! codec->afg) {
  451. snd_printk(KERN_ERR "hda_codec: no AFG node found\n");
  452. snd_hda_codec_free(codec);
  453. return -ENODEV;
  454. }
  455. codec->preset = find_codec_preset(codec);
  456. if (! *bus->card->mixername)
  457. snd_hda_get_codec_name(codec, bus->card->mixername,
  458. sizeof(bus->card->mixername));
  459. if (codec->preset && codec->preset->patch)
  460. err = codec->preset->patch(codec);
  461. else
  462. err = snd_hda_parse_generic_codec(codec);
  463. if (err < 0) {
  464. snd_hda_codec_free(codec);
  465. return err;
  466. }
  467. snd_hda_codec_proc_new(codec);
  468. sprintf(component, "HDA:%08x", codec->vendor_id);
  469. snd_component_add(codec->bus->card, component);
  470. if (codecp)
  471. *codecp = codec;
  472. return 0;
  473. }
  474. /**
  475. * snd_hda_codec_setup_stream - set up the codec for streaming
  476. * @codec: the CODEC to set up
  477. * @nid: the NID to set up
  478. * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
  479. * @channel_id: channel id to pass, zero based.
  480. * @format: stream format.
  481. */
  482. void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid, u32 stream_tag,
  483. int channel_id, int format)
  484. {
  485. snd_printdd("hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
  486. nid, stream_tag, channel_id, format);
  487. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
  488. (stream_tag << 4) | channel_id);
  489. msleep(1);
  490. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
  491. }
  492. /*
  493. * amp access functions
  494. */
  495. #define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + (idx) * 32 + (dir) * 64)
  496. #define INFO_AMP_CAPS (1<<0)
  497. #define INFO_AMP_VOL (1<<1)
  498. /* initialize the hash table */
  499. static void init_amp_hash(struct hda_codec *codec)
  500. {
  501. memset(codec->amp_hash, 0xff, sizeof(codec->amp_hash));
  502. codec->num_amp_entries = 0;
  503. }
  504. /* query the hash. allocate an entry if not found. */
  505. static struct hda_amp_info *get_alloc_amp_hash(struct hda_codec *codec, u32 key)
  506. {
  507. u16 idx = key % (u16)ARRAY_SIZE(codec->amp_hash);
  508. u16 cur = codec->amp_hash[idx];
  509. struct hda_amp_info *info;
  510. while (cur != 0xffff) {
  511. info = &codec->amp_info[cur];
  512. if (info->key == key)
  513. return info;
  514. cur = info->next;
  515. }
  516. /* add a new hash entry */
  517. if (codec->num_amp_entries >= ARRAY_SIZE(codec->amp_info)) {
  518. snd_printk(KERN_ERR "hda_codec: Tooooo many amps!\n");
  519. return NULL;
  520. }
  521. cur = codec->num_amp_entries++;
  522. info = &codec->amp_info[cur];
  523. info->key = key;
  524. info->status = 0; /* not initialized yet */
  525. info->next = codec->amp_hash[idx];
  526. codec->amp_hash[idx] = cur;
  527. return info;
  528. }
  529. /*
  530. * query AMP capabilities for the given widget and direction
  531. */
  532. static u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
  533. {
  534. struct hda_amp_info *info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
  535. if (! info)
  536. return 0;
  537. if (! (info->status & INFO_AMP_CAPS)) {
  538. if (!(snd_hda_param_read(codec, nid, AC_PAR_AUDIO_WIDGET_CAP) & AC_WCAP_AMP_OVRD))
  539. nid = codec->afg;
  540. info->amp_caps = snd_hda_param_read(codec, nid, direction == HDA_OUTPUT ?
  541. AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
  542. info->status |= INFO_AMP_CAPS;
  543. }
  544. return info->amp_caps;
  545. }
  546. /*
  547. * read the current volume to info
  548. * if the cache exists, read from the cache.
  549. */
  550. static void get_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
  551. hda_nid_t nid, int ch, int direction, int index)
  552. {
  553. u32 val, parm;
  554. if (info->status & (INFO_AMP_VOL << ch))
  555. return;
  556. parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
  557. parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
  558. parm |= index;
  559. val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_AMP_GAIN_MUTE, parm);
  560. info->vol[ch] = val & 0xff;
  561. info->status |= INFO_AMP_VOL << ch;
  562. }
  563. /*
  564. * write the current volume in info to the h/w
  565. */
  566. static void put_vol_mute(struct hda_codec *codec,
  567. hda_nid_t nid, int ch, int direction, int index, int val)
  568. {
  569. u32 parm;
  570. parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
  571. parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
  572. parm |= index << AC_AMP_SET_INDEX_SHIFT;
  573. parm |= val;
  574. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
  575. }
  576. /*
  577. * read/write AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
  578. */
  579. int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch, int direction, int index)
  580. {
  581. struct hda_amp_info *info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
  582. if (! info)
  583. return 0;
  584. get_vol_mute(codec, info, nid, ch, direction, index);
  585. return info->vol[ch];
  586. }
  587. int snd_hda_codec_amp_write(struct hda_codec *codec, hda_nid_t nid, int ch, int direction, int idx, int val)
  588. {
  589. struct hda_amp_info *info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
  590. if (! info)
  591. return 0;
  592. get_vol_mute(codec, info, nid, ch, direction, idx);
  593. if (info->vol[ch] == val && ! codec->in_resume)
  594. return 0;
  595. put_vol_mute(codec, nid, ch, direction, idx, val);
  596. info->vol[ch] = val;
  597. return 1;
  598. }
  599. /*
  600. * AMP control callbacks
  601. */
  602. /* retrieve parameters from private_value */
  603. #define get_amp_nid(kc) ((kc)->private_value & 0xffff)
  604. #define get_amp_channels(kc) (((kc)->private_value >> 16) & 0x3)
  605. #define get_amp_direction(kc) (((kc)->private_value >> 18) & 0x1)
  606. #define get_amp_index(kc) (((kc)->private_value >> 19) & 0xf)
  607. /* volume */
  608. int snd_hda_mixer_amp_volume_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
  609. {
  610. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  611. u16 nid = get_amp_nid(kcontrol);
  612. u8 chs = get_amp_channels(kcontrol);
  613. int dir = get_amp_direction(kcontrol);
  614. u32 caps;
  615. caps = query_amp_caps(codec, nid, dir);
  616. caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; /* num steps */
  617. if (! caps) {
  618. printk(KERN_WARNING "hda_codec: num_steps = 0 for NID=0x%x\n", nid);
  619. return -EINVAL;
  620. }
  621. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  622. uinfo->count = chs == 3 ? 2 : 1;
  623. uinfo->value.integer.min = 0;
  624. uinfo->value.integer.max = caps;
  625. return 0;
  626. }
  627. int snd_hda_mixer_amp_volume_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  628. {
  629. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  630. hda_nid_t nid = get_amp_nid(kcontrol);
  631. int chs = get_amp_channels(kcontrol);
  632. int dir = get_amp_direction(kcontrol);
  633. int idx = get_amp_index(kcontrol);
  634. long *valp = ucontrol->value.integer.value;
  635. if (chs & 1)
  636. *valp++ = snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 0x7f;
  637. if (chs & 2)
  638. *valp = snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 0x7f;
  639. return 0;
  640. }
  641. int snd_hda_mixer_amp_volume_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  642. {
  643. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  644. hda_nid_t nid = get_amp_nid(kcontrol);
  645. int chs = get_amp_channels(kcontrol);
  646. int dir = get_amp_direction(kcontrol);
  647. int idx = get_amp_index(kcontrol);
  648. int val;
  649. long *valp = ucontrol->value.integer.value;
  650. int change = 0;
  651. if (chs & 1) {
  652. val = *valp & 0x7f;
  653. val |= snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 0x80;
  654. change = snd_hda_codec_amp_write(codec, nid, 0, dir, idx, val);
  655. valp++;
  656. }
  657. if (chs & 2) {
  658. val = *valp & 0x7f;
  659. val |= snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 0x80;
  660. change |= snd_hda_codec_amp_write(codec, nid, 1, dir, idx, val);
  661. }
  662. return change;
  663. }
  664. /* switch */
  665. int snd_hda_mixer_amp_switch_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
  666. {
  667. int chs = get_amp_channels(kcontrol);
  668. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  669. uinfo->count = chs == 3 ? 2 : 1;
  670. uinfo->value.integer.min = 0;
  671. uinfo->value.integer.max = 1;
  672. return 0;
  673. }
  674. int snd_hda_mixer_amp_switch_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  675. {
  676. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  677. hda_nid_t nid = get_amp_nid(kcontrol);
  678. int chs = get_amp_channels(kcontrol);
  679. int dir = get_amp_direction(kcontrol);
  680. int idx = get_amp_index(kcontrol);
  681. long *valp = ucontrol->value.integer.value;
  682. if (chs & 1)
  683. *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 0x80) ? 0 : 1;
  684. if (chs & 2)
  685. *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 0x80) ? 0 : 1;
  686. return 0;
  687. }
  688. int snd_hda_mixer_amp_switch_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  689. {
  690. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  691. hda_nid_t nid = get_amp_nid(kcontrol);
  692. int chs = get_amp_channels(kcontrol);
  693. int dir = get_amp_direction(kcontrol);
  694. int idx = get_amp_index(kcontrol);
  695. int val;
  696. long *valp = ucontrol->value.integer.value;
  697. int change = 0;
  698. if (chs & 1) {
  699. val = snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 0x7f;
  700. val |= *valp ? 0 : 0x80;
  701. change = snd_hda_codec_amp_write(codec, nid, 0, dir, idx, val);
  702. valp++;
  703. }
  704. if (chs & 2) {
  705. val = snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 0x7f;
  706. val |= *valp ? 0 : 0x80;
  707. change = snd_hda_codec_amp_write(codec, nid, 1, dir, idx, val);
  708. }
  709. return change;
  710. }
  711. /*
  712. * SPDIF out controls
  713. */
  714. static int snd_hda_spdif_mask_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
  715. {
  716. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  717. uinfo->count = 1;
  718. return 0;
  719. }
  720. static int snd_hda_spdif_cmask_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  721. {
  722. ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
  723. IEC958_AES0_NONAUDIO |
  724. IEC958_AES0_CON_EMPHASIS_5015 |
  725. IEC958_AES0_CON_NOT_COPYRIGHT;
  726. ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
  727. IEC958_AES1_CON_ORIGINAL;
  728. return 0;
  729. }
  730. static int snd_hda_spdif_pmask_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  731. {
  732. ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
  733. IEC958_AES0_NONAUDIO |
  734. IEC958_AES0_PRO_EMPHASIS_5015;
  735. return 0;
  736. }
  737. static int snd_hda_spdif_default_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  738. {
  739. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  740. ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
  741. ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
  742. ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
  743. ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
  744. return 0;
  745. }
  746. /* convert from SPDIF status bits to HDA SPDIF bits
  747. * bit 0 (DigEn) is always set zero (to be filled later)
  748. */
  749. static unsigned short convert_from_spdif_status(unsigned int sbits)
  750. {
  751. unsigned short val = 0;
  752. if (sbits & IEC958_AES0_PROFESSIONAL)
  753. val |= 1 << 6;
  754. if (sbits & IEC958_AES0_NONAUDIO)
  755. val |= 1 << 5;
  756. if (sbits & IEC958_AES0_PROFESSIONAL) {
  757. if ((sbits & IEC958_AES0_PRO_EMPHASIS) == IEC958_AES0_PRO_EMPHASIS_5015)
  758. val |= 1 << 3;
  759. } else {
  760. if ((sbits & IEC958_AES0_CON_EMPHASIS) == IEC958_AES0_CON_EMPHASIS_5015)
  761. val |= 1 << 3;
  762. if (! (sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
  763. val |= 1 << 4;
  764. if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
  765. val |= 1 << 7;
  766. val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
  767. }
  768. return val;
  769. }
  770. /* convert to SPDIF status bits from HDA SPDIF bits
  771. */
  772. static unsigned int convert_to_spdif_status(unsigned short val)
  773. {
  774. unsigned int sbits = 0;
  775. if (val & (1 << 5))
  776. sbits |= IEC958_AES0_NONAUDIO;
  777. if (val & (1 << 6))
  778. sbits |= IEC958_AES0_PROFESSIONAL;
  779. if (sbits & IEC958_AES0_PROFESSIONAL) {
  780. if (sbits & (1 << 3))
  781. sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
  782. } else {
  783. if (val & (1 << 3))
  784. sbits |= IEC958_AES0_CON_EMPHASIS_5015;
  785. if (! (val & (1 << 4)))
  786. sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
  787. if (val & (1 << 7))
  788. sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
  789. sbits |= val & (0x7f << 8);
  790. }
  791. return sbits;
  792. }
  793. static int snd_hda_spdif_default_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  794. {
  795. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  796. hda_nid_t nid = kcontrol->private_value;
  797. unsigned short val;
  798. int change;
  799. down(&codec->spdif_mutex);
  800. codec->spdif_status = ucontrol->value.iec958.status[0] |
  801. ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
  802. ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
  803. ((unsigned int)ucontrol->value.iec958.status[3] << 24);
  804. val = convert_from_spdif_status(codec->spdif_status);
  805. val |= codec->spdif_ctls & 1;
  806. change = codec->spdif_ctls != val;
  807. codec->spdif_ctls = val;
  808. if (change || codec->in_resume) {
  809. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, val & 0xff);
  810. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_2, val >> 8);
  811. }
  812. up(&codec->spdif_mutex);
  813. return change;
  814. }
  815. static int snd_hda_spdif_out_switch_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
  816. {
  817. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  818. uinfo->count = 1;
  819. uinfo->value.integer.min = 0;
  820. uinfo->value.integer.max = 1;
  821. return 0;
  822. }
  823. static int snd_hda_spdif_out_switch_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  824. {
  825. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  826. ucontrol->value.integer.value[0] = codec->spdif_ctls & 1;
  827. return 0;
  828. }
  829. static int snd_hda_spdif_out_switch_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  830. {
  831. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  832. hda_nid_t nid = kcontrol->private_value;
  833. unsigned short val;
  834. int change;
  835. down(&codec->spdif_mutex);
  836. val = codec->spdif_ctls & ~1;
  837. if (ucontrol->value.integer.value[0])
  838. val |= 1;
  839. change = codec->spdif_ctls != val;
  840. if (change || codec->in_resume) {
  841. codec->spdif_ctls = val;
  842. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, val & 0xff);
  843. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
  844. AC_AMP_SET_RIGHT | AC_AMP_SET_LEFT |
  845. AC_AMP_SET_OUTPUT | ((val & 1) ? 0 : 0x80));
  846. }
  847. up(&codec->spdif_mutex);
  848. return change;
  849. }
  850. static snd_kcontrol_new_t dig_mixes[] = {
  851. {
  852. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  853. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  854. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
  855. .info = snd_hda_spdif_mask_info,
  856. .get = snd_hda_spdif_cmask_get,
  857. },
  858. {
  859. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  860. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  861. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
  862. .info = snd_hda_spdif_mask_info,
  863. .get = snd_hda_spdif_pmask_get,
  864. },
  865. {
  866. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  867. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
  868. .info = snd_hda_spdif_mask_info,
  869. .get = snd_hda_spdif_default_get,
  870. .put = snd_hda_spdif_default_put,
  871. },
  872. {
  873. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  874. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
  875. .info = snd_hda_spdif_out_switch_info,
  876. .get = snd_hda_spdif_out_switch_get,
  877. .put = snd_hda_spdif_out_switch_put,
  878. },
  879. { } /* end */
  880. };
  881. /**
  882. * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
  883. * @codec: the HDA codec
  884. * @nid: audio out widget NID
  885. *
  886. * Creates controls related with the SPDIF output.
  887. * Called from each patch supporting the SPDIF out.
  888. *
  889. * Returns 0 if successful, or a negative error code.
  890. */
  891. int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
  892. {
  893. int err;
  894. snd_kcontrol_t *kctl;
  895. snd_kcontrol_new_t *dig_mix;
  896. for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
  897. kctl = snd_ctl_new1(dig_mix, codec);
  898. kctl->private_value = nid;
  899. if ((err = snd_ctl_add(codec->bus->card, kctl)) < 0)
  900. return err;
  901. }
  902. codec->spdif_ctls = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0);
  903. codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
  904. return 0;
  905. }
  906. /*
  907. * SPDIF input
  908. */
  909. #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
  910. static int snd_hda_spdif_in_switch_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  911. {
  912. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  913. ucontrol->value.integer.value[0] = codec->spdif_in_enable;
  914. return 0;
  915. }
  916. static int snd_hda_spdif_in_switch_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  917. {
  918. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  919. hda_nid_t nid = kcontrol->private_value;
  920. unsigned int val = !!ucontrol->value.integer.value[0];
  921. int change;
  922. down(&codec->spdif_mutex);
  923. change = codec->spdif_in_enable != val;
  924. if (change || codec->in_resume) {
  925. codec->spdif_in_enable = val;
  926. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, val);
  927. }
  928. up(&codec->spdif_mutex);
  929. return change;
  930. }
  931. static int snd_hda_spdif_in_status_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  932. {
  933. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  934. hda_nid_t nid = kcontrol->private_value;
  935. unsigned short val;
  936. unsigned int sbits;
  937. val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0);
  938. sbits = convert_to_spdif_status(val);
  939. ucontrol->value.iec958.status[0] = sbits;
  940. ucontrol->value.iec958.status[1] = sbits >> 8;
  941. ucontrol->value.iec958.status[2] = sbits >> 16;
  942. ucontrol->value.iec958.status[3] = sbits >> 24;
  943. return 0;
  944. }
  945. static snd_kcontrol_new_t dig_in_ctls[] = {
  946. {
  947. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  948. .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
  949. .info = snd_hda_spdif_in_switch_info,
  950. .get = snd_hda_spdif_in_switch_get,
  951. .put = snd_hda_spdif_in_switch_put,
  952. },
  953. {
  954. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  955. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  956. .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
  957. .info = snd_hda_spdif_mask_info,
  958. .get = snd_hda_spdif_in_status_get,
  959. },
  960. { } /* end */
  961. };
  962. /**
  963. * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
  964. * @codec: the HDA codec
  965. * @nid: audio in widget NID
  966. *
  967. * Creates controls related with the SPDIF input.
  968. * Called from each patch supporting the SPDIF in.
  969. *
  970. * Returns 0 if successful, or a negative error code.
  971. */
  972. int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
  973. {
  974. int err;
  975. snd_kcontrol_t *kctl;
  976. snd_kcontrol_new_t *dig_mix;
  977. for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
  978. kctl = snd_ctl_new1(dig_mix, codec);
  979. kctl->private_value = nid;
  980. if ((err = snd_ctl_add(codec->bus->card, kctl)) < 0)
  981. return err;
  982. }
  983. codec->spdif_in_enable = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0) & 1;
  984. return 0;
  985. }
  986. /**
  987. * snd_hda_build_controls - build mixer controls
  988. * @bus: the BUS
  989. *
  990. * Creates mixer controls for each codec included in the bus.
  991. *
  992. * Returns 0 if successful, otherwise a negative error code.
  993. */
  994. int snd_hda_build_controls(struct hda_bus *bus)
  995. {
  996. struct list_head *p;
  997. /* build controls */
  998. list_for_each(p, &bus->codec_list) {
  999. struct hda_codec *codec = list_entry(p, struct hda_codec, list);
  1000. int err;
  1001. if (! codec->patch_ops.build_controls)
  1002. continue;
  1003. err = codec->patch_ops.build_controls(codec);
  1004. if (err < 0)
  1005. return err;
  1006. }
  1007. /* initialize */
  1008. list_for_each(p, &bus->codec_list) {
  1009. struct hda_codec *codec = list_entry(p, struct hda_codec, list);
  1010. int err;
  1011. if (! codec->patch_ops.init)
  1012. continue;
  1013. err = codec->patch_ops.init(codec);
  1014. if (err < 0)
  1015. return err;
  1016. }
  1017. return 0;
  1018. }
  1019. /*
  1020. * stream formats
  1021. */
  1022. static unsigned int rate_bits[][3] = {
  1023. /* rate in Hz, ALSA rate bitmask, HDA format value */
  1024. { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
  1025. { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
  1026. { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
  1027. { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
  1028. { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
  1029. { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
  1030. { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
  1031. { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
  1032. { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
  1033. { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
  1034. { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
  1035. { 0 }
  1036. };
  1037. /**
  1038. * snd_hda_calc_stream_format - calculate format bitset
  1039. * @rate: the sample rate
  1040. * @channels: the number of channels
  1041. * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
  1042. * @maxbps: the max. bps
  1043. *
  1044. * Calculate the format bitset from the given rate, channels and th PCM format.
  1045. *
  1046. * Return zero if invalid.
  1047. */
  1048. unsigned int snd_hda_calc_stream_format(unsigned int rate,
  1049. unsigned int channels,
  1050. unsigned int format,
  1051. unsigned int maxbps)
  1052. {
  1053. int i;
  1054. unsigned int val = 0;
  1055. for (i = 0; rate_bits[i][0]; i++)
  1056. if (rate_bits[i][0] == rate) {
  1057. val = rate_bits[i][2];
  1058. break;
  1059. }
  1060. if (! rate_bits[i][0]) {
  1061. snd_printdd("invalid rate %d\n", rate);
  1062. return 0;
  1063. }
  1064. if (channels == 0 || channels > 8) {
  1065. snd_printdd("invalid channels %d\n", channels);
  1066. return 0;
  1067. }
  1068. val |= channels - 1;
  1069. switch (snd_pcm_format_width(format)) {
  1070. case 8: val |= 0x00; break;
  1071. case 16: val |= 0x10; break;
  1072. case 20:
  1073. case 24:
  1074. case 32:
  1075. if (maxbps >= 32)
  1076. val |= 0x40;
  1077. else if (maxbps >= 24)
  1078. val |= 0x30;
  1079. else
  1080. val |= 0x20;
  1081. break;
  1082. default:
  1083. snd_printdd("invalid format width %d\n", snd_pcm_format_width(format));
  1084. return 0;
  1085. }
  1086. return val;
  1087. }
  1088. /**
  1089. * snd_hda_query_supported_pcm - query the supported PCM rates and formats
  1090. * @codec: the HDA codec
  1091. * @nid: NID to query
  1092. * @ratesp: the pointer to store the detected rate bitflags
  1093. * @formatsp: the pointer to store the detected formats
  1094. * @bpsp: the pointer to store the detected format widths
  1095. *
  1096. * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
  1097. * or @bsps argument is ignored.
  1098. *
  1099. * Returns 0 if successful, otherwise a negative error code.
  1100. */
  1101. int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
  1102. u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
  1103. {
  1104. int i;
  1105. unsigned int val, streams;
  1106. val = 0;
  1107. if (nid != codec->afg &&
  1108. snd_hda_param_read(codec, nid, AC_PAR_AUDIO_WIDGET_CAP) & AC_WCAP_FORMAT_OVRD) {
  1109. val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
  1110. if (val == -1)
  1111. return -EIO;
  1112. }
  1113. if (! val)
  1114. val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
  1115. if (ratesp) {
  1116. u32 rates = 0;
  1117. for (i = 0; rate_bits[i][0]; i++) {
  1118. if (val & (1 << i))
  1119. rates |= rate_bits[i][1];
  1120. }
  1121. *ratesp = rates;
  1122. }
  1123. if (formatsp || bpsp) {
  1124. u64 formats = 0;
  1125. unsigned int bps;
  1126. unsigned int wcaps;
  1127. wcaps = snd_hda_param_read(codec, nid, AC_PAR_AUDIO_WIDGET_CAP);
  1128. streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
  1129. if (streams == -1)
  1130. return -EIO;
  1131. if (! streams) {
  1132. streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
  1133. if (streams == -1)
  1134. return -EIO;
  1135. }
  1136. bps = 0;
  1137. if (streams & AC_SUPFMT_PCM) {
  1138. if (val & AC_SUPPCM_BITS_8) {
  1139. formats |= SNDRV_PCM_FMTBIT_U8;
  1140. bps = 8;
  1141. }
  1142. if (val & AC_SUPPCM_BITS_16) {
  1143. formats |= SNDRV_PCM_FMTBIT_S16_LE;
  1144. bps = 16;
  1145. }
  1146. if (wcaps & AC_WCAP_DIGITAL) {
  1147. if (val & AC_SUPPCM_BITS_32)
  1148. formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
  1149. if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
  1150. formats |= SNDRV_PCM_FMTBIT_S32_LE;
  1151. if (val & AC_SUPPCM_BITS_24)
  1152. bps = 24;
  1153. else if (val & AC_SUPPCM_BITS_20)
  1154. bps = 20;
  1155. } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|AC_SUPPCM_BITS_32)) {
  1156. formats |= SNDRV_PCM_FMTBIT_S32_LE;
  1157. if (val & AC_SUPPCM_BITS_32)
  1158. bps = 32;
  1159. else if (val & AC_SUPPCM_BITS_20)
  1160. bps = 20;
  1161. else if (val & AC_SUPPCM_BITS_24)
  1162. bps = 24;
  1163. }
  1164. }
  1165. else if (streams == AC_SUPFMT_FLOAT32) { /* should be exclusive */
  1166. formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
  1167. bps = 32;
  1168. } else if (streams == AC_SUPFMT_AC3) { /* should be exclusive */
  1169. /* temporary hack: we have still no proper support
  1170. * for the direct AC3 stream...
  1171. */
  1172. formats |= SNDRV_PCM_FMTBIT_U8;
  1173. bps = 8;
  1174. }
  1175. if (formatsp)
  1176. *formatsp = formats;
  1177. if (bpsp)
  1178. *bpsp = bps;
  1179. }
  1180. return 0;
  1181. }
  1182. /**
  1183. * snd_hda_is_supported_format - check whether the given node supports the format val
  1184. *
  1185. * Returns 1 if supported, 0 if not.
  1186. */
  1187. int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
  1188. unsigned int format)
  1189. {
  1190. int i;
  1191. unsigned int val = 0, rate, stream;
  1192. if (nid != codec->afg &&
  1193. snd_hda_param_read(codec, nid, AC_PAR_AUDIO_WIDGET_CAP) & AC_WCAP_FORMAT_OVRD) {
  1194. val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
  1195. if (val == -1)
  1196. return 0;
  1197. }
  1198. if (! val) {
  1199. val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
  1200. if (val == -1)
  1201. return 0;
  1202. }
  1203. rate = format & 0xff00;
  1204. for (i = 0; rate_bits[i][0]; i++)
  1205. if (rate_bits[i][2] == rate) {
  1206. if (val & (1 << i))
  1207. break;
  1208. return 0;
  1209. }
  1210. if (! rate_bits[i][0])
  1211. return 0;
  1212. stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
  1213. if (stream == -1)
  1214. return 0;
  1215. if (! stream && nid != codec->afg)
  1216. stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
  1217. if (! stream || stream == -1)
  1218. return 0;
  1219. if (stream & AC_SUPFMT_PCM) {
  1220. switch (format & 0xf0) {
  1221. case 0x00:
  1222. if (! (val & AC_SUPPCM_BITS_8))
  1223. return 0;
  1224. break;
  1225. case 0x10:
  1226. if (! (val & AC_SUPPCM_BITS_16))
  1227. return 0;
  1228. break;
  1229. case 0x20:
  1230. if (! (val & AC_SUPPCM_BITS_20))
  1231. return 0;
  1232. break;
  1233. case 0x30:
  1234. if (! (val & AC_SUPPCM_BITS_24))
  1235. return 0;
  1236. break;
  1237. case 0x40:
  1238. if (! (val & AC_SUPPCM_BITS_32))
  1239. return 0;
  1240. break;
  1241. default:
  1242. return 0;
  1243. }
  1244. } else {
  1245. /* FIXME: check for float32 and AC3? */
  1246. }
  1247. return 1;
  1248. }
  1249. /*
  1250. * PCM stuff
  1251. */
  1252. static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
  1253. struct hda_codec *codec,
  1254. snd_pcm_substream_t *substream)
  1255. {
  1256. return 0;
  1257. }
  1258. static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
  1259. struct hda_codec *codec,
  1260. unsigned int stream_tag,
  1261. unsigned int format,
  1262. snd_pcm_substream_t *substream)
  1263. {
  1264. snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
  1265. return 0;
  1266. }
  1267. static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
  1268. struct hda_codec *codec,
  1269. snd_pcm_substream_t *substream)
  1270. {
  1271. snd_hda_codec_setup_stream(codec, hinfo->nid, 0, 0, 0);
  1272. return 0;
  1273. }
  1274. static int set_pcm_default_values(struct hda_codec *codec, struct hda_pcm_stream *info)
  1275. {
  1276. if (info->nid) {
  1277. /* query support PCM information from the given NID */
  1278. if (! info->rates || ! info->formats)
  1279. snd_hda_query_supported_pcm(codec, info->nid,
  1280. info->rates ? NULL : &info->rates,
  1281. info->formats ? NULL : &info->formats,
  1282. info->maxbps ? NULL : &info->maxbps);
  1283. }
  1284. if (info->ops.open == NULL)
  1285. info->ops.open = hda_pcm_default_open_close;
  1286. if (info->ops.close == NULL)
  1287. info->ops.close = hda_pcm_default_open_close;
  1288. if (info->ops.prepare == NULL) {
  1289. snd_assert(info->nid, return -EINVAL);
  1290. info->ops.prepare = hda_pcm_default_prepare;
  1291. }
  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_vendor; 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_vendor; c++) {
  1382. if (c->pci_vendor == subsystem_vendor &&
  1383. c->pci_device == 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)