hda_codec.c 55 KB

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