hda_codec.c 69 KB

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