hda_codec.c 73 KB

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