hda_codec.c 83 KB

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