hda_codec.c 62 KB

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