hda_codec.c 48 KB

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