hda_codec.c 64 KB

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