hda_codec.c 64 KB

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