hda_codec.c 128 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788
  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 "hda_beep.h"
  33. #include <sound/hda_hwdep.h>
  34. /*
  35. * vendor / preset table
  36. */
  37. struct hda_vendor_id {
  38. unsigned int id;
  39. const char *name;
  40. };
  41. /* codec vendor labels */
  42. static struct hda_vendor_id hda_vendor_ids[] = {
  43. { 0x1002, "ATI" },
  44. { 0x1013, "Cirrus Logic" },
  45. { 0x1057, "Motorola" },
  46. { 0x1095, "Silicon Image" },
  47. { 0x10de, "Nvidia" },
  48. { 0x10ec, "Realtek" },
  49. { 0x1102, "Creative" },
  50. { 0x1106, "VIA" },
  51. { 0x111d, "IDT" },
  52. { 0x11c1, "LSI" },
  53. { 0x11d4, "Analog Devices" },
  54. { 0x13f6, "C-Media" },
  55. { 0x14f1, "Conexant" },
  56. { 0x17e8, "Chrontel" },
  57. { 0x1854, "LG" },
  58. { 0x1aec, "Wolfson Microelectronics" },
  59. { 0x434d, "C-Media" },
  60. { 0x8086, "Intel" },
  61. { 0x8384, "SigmaTel" },
  62. {} /* terminator */
  63. };
  64. static DEFINE_MUTEX(preset_mutex);
  65. static LIST_HEAD(hda_preset_tables);
  66. int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
  67. {
  68. mutex_lock(&preset_mutex);
  69. list_add_tail(&preset->list, &hda_preset_tables);
  70. mutex_unlock(&preset_mutex);
  71. return 0;
  72. }
  73. EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset);
  74. int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
  75. {
  76. mutex_lock(&preset_mutex);
  77. list_del(&preset->list);
  78. mutex_unlock(&preset_mutex);
  79. return 0;
  80. }
  81. EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset);
  82. #ifdef CONFIG_SND_HDA_POWER_SAVE
  83. static void hda_power_work(struct work_struct *work);
  84. static void hda_keep_power_on(struct hda_codec *codec);
  85. #else
  86. static inline void hda_keep_power_on(struct hda_codec *codec) {}
  87. #endif
  88. /**
  89. * snd_hda_get_jack_location - Give a location string of the jack
  90. * @cfg: pin default config value
  91. *
  92. * Parse the pin default config value and returns the string of the
  93. * jack location, e.g. "Rear", "Front", etc.
  94. */
  95. const char *snd_hda_get_jack_location(u32 cfg)
  96. {
  97. static char *bases[7] = {
  98. "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
  99. };
  100. static unsigned char specials_idx[] = {
  101. 0x07, 0x08,
  102. 0x17, 0x18, 0x19,
  103. 0x37, 0x38
  104. };
  105. static char *specials[] = {
  106. "Rear Panel", "Drive Bar",
  107. "Riser", "HDMI", "ATAPI",
  108. "Mobile-In", "Mobile-Out"
  109. };
  110. int i;
  111. cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
  112. if ((cfg & 0x0f) < 7)
  113. return bases[cfg & 0x0f];
  114. for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
  115. if (cfg == specials_idx[i])
  116. return specials[i];
  117. }
  118. return "UNKNOWN";
  119. }
  120. EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
  121. /**
  122. * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
  123. * @cfg: pin default config value
  124. *
  125. * Parse the pin default config value and returns the string of the
  126. * jack connectivity, i.e. external or internal connection.
  127. */
  128. const char *snd_hda_get_jack_connectivity(u32 cfg)
  129. {
  130. static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
  131. return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
  132. }
  133. EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
  134. /**
  135. * snd_hda_get_jack_type - Give a type string of the jack
  136. * @cfg: pin default config value
  137. *
  138. * Parse the pin default config value and returns the string of the
  139. * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
  140. */
  141. const char *snd_hda_get_jack_type(u32 cfg)
  142. {
  143. static char *jack_types[16] = {
  144. "Line Out", "Speaker", "HP Out", "CD",
  145. "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
  146. "Line In", "Aux", "Mic", "Telephony",
  147. "SPDIF In", "Digitial In", "Reserved", "Other"
  148. };
  149. return jack_types[(cfg & AC_DEFCFG_DEVICE)
  150. >> AC_DEFCFG_DEVICE_SHIFT];
  151. }
  152. EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
  153. /*
  154. * Compose a 32bit command word to be sent to the HD-audio controller
  155. */
  156. static inline unsigned int
  157. make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
  158. unsigned int verb, unsigned int parm)
  159. {
  160. u32 val;
  161. if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) ||
  162. (verb & ~0xfff) || (parm & ~0xffff)) {
  163. printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
  164. codec->addr, direct, nid, verb, parm);
  165. return ~0;
  166. }
  167. val = (u32)codec->addr << 28;
  168. val |= (u32)direct << 27;
  169. val |= (u32)nid << 20;
  170. val |= verb << 8;
  171. val |= parm;
  172. return val;
  173. }
  174. /*
  175. * Send and receive a verb
  176. */
  177. static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
  178. unsigned int *res)
  179. {
  180. struct hda_bus *bus = codec->bus;
  181. int err;
  182. if (cmd == ~0)
  183. return -1;
  184. if (res)
  185. *res = -1;
  186. again:
  187. snd_hda_power_up(codec);
  188. mutex_lock(&bus->cmd_mutex);
  189. err = bus->ops.command(bus, cmd);
  190. if (!err && res)
  191. *res = bus->ops.get_response(bus, codec->addr);
  192. mutex_unlock(&bus->cmd_mutex);
  193. snd_hda_power_down(codec);
  194. if (res && *res == -1 && bus->rirb_error) {
  195. if (bus->response_reset) {
  196. snd_printd("hda_codec: resetting BUS due to "
  197. "fatal communication error\n");
  198. bus->ops.bus_reset(bus);
  199. }
  200. goto again;
  201. }
  202. /* clear reset-flag when the communication gets recovered */
  203. if (!err)
  204. bus->response_reset = 0;
  205. return err;
  206. }
  207. /**
  208. * snd_hda_codec_read - send a command and get the response
  209. * @codec: the HDA codec
  210. * @nid: NID to send the command
  211. * @direct: direct flag
  212. * @verb: the verb to send
  213. * @parm: the parameter for the verb
  214. *
  215. * Send a single command and read the corresponding response.
  216. *
  217. * Returns the obtained response value, or -1 for an error.
  218. */
  219. unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
  220. int direct,
  221. unsigned int verb, unsigned int parm)
  222. {
  223. unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm);
  224. unsigned int res;
  225. codec_exec_verb(codec, cmd, &res);
  226. return res;
  227. }
  228. EXPORT_SYMBOL_HDA(snd_hda_codec_read);
  229. /**
  230. * snd_hda_codec_write - send a single command without waiting for response
  231. * @codec: the HDA codec
  232. * @nid: NID to send the command
  233. * @direct: direct flag
  234. * @verb: the verb to send
  235. * @parm: the parameter for the verb
  236. *
  237. * Send a single command without waiting for response.
  238. *
  239. * Returns 0 if successful, or a negative error code.
  240. */
  241. int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
  242. unsigned int verb, unsigned int parm)
  243. {
  244. unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm);
  245. unsigned int res;
  246. return codec_exec_verb(codec, cmd,
  247. codec->bus->sync_write ? &res : NULL);
  248. }
  249. EXPORT_SYMBOL_HDA(snd_hda_codec_write);
  250. /**
  251. * snd_hda_sequence_write - sequence writes
  252. * @codec: the HDA codec
  253. * @seq: VERB array to send
  254. *
  255. * Send the commands sequentially from the given array.
  256. * The array must be terminated with NID=0.
  257. */
  258. void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
  259. {
  260. for (; seq->nid; seq++)
  261. snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
  262. }
  263. EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
  264. /**
  265. * snd_hda_get_sub_nodes - get the range of sub nodes
  266. * @codec: the HDA codec
  267. * @nid: NID to parse
  268. * @start_id: the pointer to store the start NID
  269. *
  270. * Parse the NID and store the start NID of its sub-nodes.
  271. * Returns the number of sub-nodes.
  272. */
  273. int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
  274. hda_nid_t *start_id)
  275. {
  276. unsigned int parm;
  277. parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
  278. if (parm == -1)
  279. return 0;
  280. *start_id = (parm >> 16) & 0x7fff;
  281. return (int)(parm & 0x7fff);
  282. }
  283. EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
  284. /**
  285. * snd_hda_get_connections - get connection list
  286. * @codec: the HDA codec
  287. * @nid: NID to parse
  288. * @conn_list: connection list array
  289. * @max_conns: max. number of connections to store
  290. *
  291. * Parses the connection list of the given widget and stores the list
  292. * of NIDs.
  293. *
  294. * Returns the number of connections, or a negative error code.
  295. */
  296. int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
  297. hda_nid_t *conn_list, int max_conns)
  298. {
  299. unsigned int parm;
  300. int i, conn_len, conns;
  301. unsigned int shift, num_elems, mask;
  302. unsigned int wcaps;
  303. hda_nid_t prev_nid;
  304. if (snd_BUG_ON(!conn_list || max_conns <= 0))
  305. return -EINVAL;
  306. wcaps = get_wcaps(codec, nid);
  307. if (!(wcaps & AC_WCAP_CONN_LIST) &&
  308. get_wcaps_type(wcaps) != AC_WID_VOL_KNB) {
  309. snd_printk(KERN_WARNING "hda_codec: "
  310. "connection list not available for 0x%x\n", nid);
  311. return -EINVAL;
  312. }
  313. parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
  314. if (parm & AC_CLIST_LONG) {
  315. /* long form */
  316. shift = 16;
  317. num_elems = 2;
  318. } else {
  319. /* short form */
  320. shift = 8;
  321. num_elems = 4;
  322. }
  323. conn_len = parm & AC_CLIST_LENGTH;
  324. mask = (1 << (shift-1)) - 1;
  325. if (!conn_len)
  326. return 0; /* no connection */
  327. if (conn_len == 1) {
  328. /* single connection */
  329. parm = snd_hda_codec_read(codec, nid, 0,
  330. AC_VERB_GET_CONNECT_LIST, 0);
  331. if (parm == -1 && codec->bus->rirb_error)
  332. return -EIO;
  333. conn_list[0] = parm & mask;
  334. return 1;
  335. }
  336. /* multi connection */
  337. conns = 0;
  338. prev_nid = 0;
  339. for (i = 0; i < conn_len; i++) {
  340. int range_val;
  341. hda_nid_t val, n;
  342. if (i % num_elems == 0) {
  343. parm = snd_hda_codec_read(codec, nid, 0,
  344. AC_VERB_GET_CONNECT_LIST, i);
  345. if (parm == -1 && codec->bus->rirb_error)
  346. return -EIO;
  347. }
  348. range_val = !!(parm & (1 << (shift-1))); /* ranges */
  349. val = parm & mask;
  350. if (val == 0) {
  351. snd_printk(KERN_WARNING "hda_codec: "
  352. "invalid CONNECT_LIST verb %x[%i]:%x\n",
  353. nid, i, parm);
  354. return 0;
  355. }
  356. parm >>= shift;
  357. if (range_val) {
  358. /* ranges between the previous and this one */
  359. if (!prev_nid || prev_nid >= val) {
  360. snd_printk(KERN_WARNING "hda_codec: "
  361. "invalid dep_range_val %x:%x\n",
  362. prev_nid, val);
  363. continue;
  364. }
  365. for (n = prev_nid + 1; n <= val; n++) {
  366. if (conns >= max_conns) {
  367. snd_printk(KERN_ERR "hda_codec: "
  368. "Too many connections %d for NID 0x%x\n",
  369. conns, nid);
  370. return -EINVAL;
  371. }
  372. conn_list[conns++] = n;
  373. }
  374. } else {
  375. if (conns >= max_conns) {
  376. snd_printk(KERN_ERR "hda_codec: "
  377. "Too many connections %d for NID 0x%x\n",
  378. conns, nid);
  379. return -EINVAL;
  380. }
  381. conn_list[conns++] = val;
  382. }
  383. prev_nid = val;
  384. }
  385. return conns;
  386. }
  387. EXPORT_SYMBOL_HDA(snd_hda_get_connections);
  388. /**
  389. * snd_hda_queue_unsol_event - add an unsolicited event to queue
  390. * @bus: the BUS
  391. * @res: unsolicited event (lower 32bit of RIRB entry)
  392. * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
  393. *
  394. * Adds the given event to the queue. The events are processed in
  395. * the workqueue asynchronously. Call this function in the interrupt
  396. * hanlder when RIRB receives an unsolicited event.
  397. *
  398. * Returns 0 if successful, or a negative error code.
  399. */
  400. int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
  401. {
  402. struct hda_bus_unsolicited *unsol;
  403. unsigned int wp;
  404. unsol = bus->unsol;
  405. if (!unsol)
  406. return 0;
  407. wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
  408. unsol->wp = wp;
  409. wp <<= 1;
  410. unsol->queue[wp] = res;
  411. unsol->queue[wp + 1] = res_ex;
  412. queue_work(bus->workq, &unsol->work);
  413. return 0;
  414. }
  415. EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
  416. /*
  417. * process queued unsolicited events
  418. */
  419. static void process_unsol_events(struct work_struct *work)
  420. {
  421. struct hda_bus_unsolicited *unsol =
  422. container_of(work, struct hda_bus_unsolicited, work);
  423. struct hda_bus *bus = unsol->bus;
  424. struct hda_codec *codec;
  425. unsigned int rp, caddr, res;
  426. while (unsol->rp != unsol->wp) {
  427. rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
  428. unsol->rp = rp;
  429. rp <<= 1;
  430. res = unsol->queue[rp];
  431. caddr = unsol->queue[rp + 1];
  432. if (!(caddr & (1 << 4))) /* no unsolicited event? */
  433. continue;
  434. codec = bus->caddr_tbl[caddr & 0x0f];
  435. if (codec && codec->patch_ops.unsol_event)
  436. codec->patch_ops.unsol_event(codec, res);
  437. }
  438. }
  439. /*
  440. * initialize unsolicited queue
  441. */
  442. static int init_unsol_queue(struct hda_bus *bus)
  443. {
  444. struct hda_bus_unsolicited *unsol;
  445. if (bus->unsol) /* already initialized */
  446. return 0;
  447. unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
  448. if (!unsol) {
  449. snd_printk(KERN_ERR "hda_codec: "
  450. "can't allocate unsolicited queue\n");
  451. return -ENOMEM;
  452. }
  453. INIT_WORK(&unsol->work, process_unsol_events);
  454. unsol->bus = bus;
  455. bus->unsol = unsol;
  456. return 0;
  457. }
  458. /*
  459. * destructor
  460. */
  461. static void snd_hda_codec_free(struct hda_codec *codec);
  462. static int snd_hda_bus_free(struct hda_bus *bus)
  463. {
  464. struct hda_codec *codec, *n;
  465. if (!bus)
  466. return 0;
  467. if (bus->workq)
  468. flush_workqueue(bus->workq);
  469. if (bus->unsol)
  470. kfree(bus->unsol);
  471. list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
  472. snd_hda_codec_free(codec);
  473. }
  474. if (bus->ops.private_free)
  475. bus->ops.private_free(bus);
  476. if (bus->workq)
  477. destroy_workqueue(bus->workq);
  478. kfree(bus);
  479. return 0;
  480. }
  481. static int snd_hda_bus_dev_free(struct snd_device *device)
  482. {
  483. struct hda_bus *bus = device->device_data;
  484. bus->shutdown = 1;
  485. return snd_hda_bus_free(bus);
  486. }
  487. #ifdef CONFIG_SND_HDA_HWDEP
  488. static int snd_hda_bus_dev_register(struct snd_device *device)
  489. {
  490. struct hda_bus *bus = device->device_data;
  491. struct hda_codec *codec;
  492. list_for_each_entry(codec, &bus->codec_list, list) {
  493. snd_hda_hwdep_add_sysfs(codec);
  494. snd_hda_hwdep_add_power_sysfs(codec);
  495. }
  496. return 0;
  497. }
  498. #else
  499. #define snd_hda_bus_dev_register NULL
  500. #endif
  501. /**
  502. * snd_hda_bus_new - create a HDA bus
  503. * @card: the card entry
  504. * @temp: the template for hda_bus information
  505. * @busp: the pointer to store the created bus instance
  506. *
  507. * Returns 0 if successful, or a negative error code.
  508. */
  509. int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
  510. const struct hda_bus_template *temp,
  511. struct hda_bus **busp)
  512. {
  513. struct hda_bus *bus;
  514. int err;
  515. static struct snd_device_ops dev_ops = {
  516. .dev_register = snd_hda_bus_dev_register,
  517. .dev_free = snd_hda_bus_dev_free,
  518. };
  519. if (snd_BUG_ON(!temp))
  520. return -EINVAL;
  521. if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
  522. return -EINVAL;
  523. if (busp)
  524. *busp = NULL;
  525. bus = kzalloc(sizeof(*bus), GFP_KERNEL);
  526. if (bus == NULL) {
  527. snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
  528. return -ENOMEM;
  529. }
  530. bus->card = card;
  531. bus->private_data = temp->private_data;
  532. bus->pci = temp->pci;
  533. bus->modelname = temp->modelname;
  534. bus->power_save = temp->power_save;
  535. bus->ops = temp->ops;
  536. mutex_init(&bus->cmd_mutex);
  537. mutex_init(&bus->prepare_mutex);
  538. INIT_LIST_HEAD(&bus->codec_list);
  539. snprintf(bus->workq_name, sizeof(bus->workq_name),
  540. "hd-audio%d", card->number);
  541. bus->workq = create_singlethread_workqueue(bus->workq_name);
  542. if (!bus->workq) {
  543. snd_printk(KERN_ERR "cannot create workqueue %s\n",
  544. bus->workq_name);
  545. kfree(bus);
  546. return -ENOMEM;
  547. }
  548. err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
  549. if (err < 0) {
  550. snd_hda_bus_free(bus);
  551. return err;
  552. }
  553. if (busp)
  554. *busp = bus;
  555. return 0;
  556. }
  557. EXPORT_SYMBOL_HDA(snd_hda_bus_new);
  558. #ifdef CONFIG_SND_HDA_GENERIC
  559. #define is_generic_config(codec) \
  560. (codec->modelname && !strcmp(codec->modelname, "generic"))
  561. #else
  562. #define is_generic_config(codec) 0
  563. #endif
  564. #ifdef MODULE
  565. #define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
  566. #else
  567. #define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
  568. #endif
  569. /*
  570. * find a matching codec preset
  571. */
  572. static const struct hda_codec_preset *
  573. find_codec_preset(struct hda_codec *codec)
  574. {
  575. struct hda_codec_preset_list *tbl;
  576. const struct hda_codec_preset *preset;
  577. int mod_requested = 0;
  578. if (is_generic_config(codec))
  579. return NULL; /* use the generic parser */
  580. again:
  581. mutex_lock(&preset_mutex);
  582. list_for_each_entry(tbl, &hda_preset_tables, list) {
  583. if (!try_module_get(tbl->owner)) {
  584. snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
  585. continue;
  586. }
  587. for (preset = tbl->preset; preset->id; preset++) {
  588. u32 mask = preset->mask;
  589. if (preset->afg && preset->afg != codec->afg)
  590. continue;
  591. if (preset->mfg && preset->mfg != codec->mfg)
  592. continue;
  593. if (!mask)
  594. mask = ~0;
  595. if (preset->id == (codec->vendor_id & mask) &&
  596. (!preset->rev ||
  597. preset->rev == codec->revision_id)) {
  598. mutex_unlock(&preset_mutex);
  599. codec->owner = tbl->owner;
  600. return preset;
  601. }
  602. }
  603. module_put(tbl->owner);
  604. }
  605. mutex_unlock(&preset_mutex);
  606. if (mod_requested < HDA_MODREQ_MAX_COUNT) {
  607. char name[32];
  608. if (!mod_requested)
  609. snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
  610. codec->vendor_id);
  611. else
  612. snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
  613. (codec->vendor_id >> 16) & 0xffff);
  614. request_module(name);
  615. mod_requested++;
  616. goto again;
  617. }
  618. return NULL;
  619. }
  620. /*
  621. * get_codec_name - store the codec name
  622. */
  623. static int get_codec_name(struct hda_codec *codec)
  624. {
  625. const struct hda_vendor_id *c;
  626. const char *vendor = NULL;
  627. u16 vendor_id = codec->vendor_id >> 16;
  628. char tmp[16];
  629. if (codec->vendor_name)
  630. goto get_chip_name;
  631. for (c = hda_vendor_ids; c->id; c++) {
  632. if (c->id == vendor_id) {
  633. vendor = c->name;
  634. break;
  635. }
  636. }
  637. if (!vendor) {
  638. sprintf(tmp, "Generic %04x", vendor_id);
  639. vendor = tmp;
  640. }
  641. codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
  642. if (!codec->vendor_name)
  643. return -ENOMEM;
  644. get_chip_name:
  645. if (codec->chip_name)
  646. return 0;
  647. if (codec->preset && codec->preset->name)
  648. codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
  649. else {
  650. sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
  651. codec->chip_name = kstrdup(tmp, GFP_KERNEL);
  652. }
  653. if (!codec->chip_name)
  654. return -ENOMEM;
  655. return 0;
  656. }
  657. /*
  658. * look for an AFG and MFG nodes
  659. */
  660. static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
  661. {
  662. int i, total_nodes, function_id;
  663. hda_nid_t nid;
  664. total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
  665. for (i = 0; i < total_nodes; i++, nid++) {
  666. function_id = snd_hda_param_read(codec, nid,
  667. AC_PAR_FUNCTION_TYPE);
  668. switch (function_id & 0xff) {
  669. case AC_GRP_AUDIO_FUNCTION:
  670. codec->afg = nid;
  671. codec->afg_function_id = function_id & 0xff;
  672. codec->afg_unsol = (function_id >> 8) & 1;
  673. break;
  674. case AC_GRP_MODEM_FUNCTION:
  675. codec->mfg = nid;
  676. codec->mfg_function_id = function_id & 0xff;
  677. codec->mfg_unsol = (function_id >> 8) & 1;
  678. break;
  679. default:
  680. break;
  681. }
  682. }
  683. }
  684. /*
  685. * read widget caps for each widget and store in cache
  686. */
  687. static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
  688. {
  689. int i;
  690. hda_nid_t nid;
  691. codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
  692. &codec->start_nid);
  693. codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
  694. if (!codec->wcaps)
  695. return -ENOMEM;
  696. nid = codec->start_nid;
  697. for (i = 0; i < codec->num_nodes; i++, nid++)
  698. codec->wcaps[i] = snd_hda_param_read(codec, nid,
  699. AC_PAR_AUDIO_WIDGET_CAP);
  700. return 0;
  701. }
  702. /* read all pin default configurations and save codec->init_pins */
  703. static int read_pin_defaults(struct hda_codec *codec)
  704. {
  705. int i;
  706. hda_nid_t nid = codec->start_nid;
  707. for (i = 0; i < codec->num_nodes; i++, nid++) {
  708. struct hda_pincfg *pin;
  709. unsigned int wcaps = get_wcaps(codec, nid);
  710. unsigned int wid_type = get_wcaps_type(wcaps);
  711. if (wid_type != AC_WID_PIN)
  712. continue;
  713. pin = snd_array_new(&codec->init_pins);
  714. if (!pin)
  715. return -ENOMEM;
  716. pin->nid = nid;
  717. pin->cfg = snd_hda_codec_read(codec, nid, 0,
  718. AC_VERB_GET_CONFIG_DEFAULT, 0);
  719. pin->ctrl = snd_hda_codec_read(codec, nid, 0,
  720. AC_VERB_GET_PIN_WIDGET_CONTROL,
  721. 0);
  722. }
  723. return 0;
  724. }
  725. /* look up the given pin config list and return the item matching with NID */
  726. static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
  727. struct snd_array *array,
  728. hda_nid_t nid)
  729. {
  730. int i;
  731. for (i = 0; i < array->used; i++) {
  732. struct hda_pincfg *pin = snd_array_elem(array, i);
  733. if (pin->nid == nid)
  734. return pin;
  735. }
  736. return NULL;
  737. }
  738. /* write a config value for the given NID */
  739. static void set_pincfg(struct hda_codec *codec, hda_nid_t nid,
  740. unsigned int cfg)
  741. {
  742. int i;
  743. for (i = 0; i < 4; i++) {
  744. snd_hda_codec_write(codec, nid, 0,
  745. AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i,
  746. cfg & 0xff);
  747. cfg >>= 8;
  748. }
  749. }
  750. /* set the current pin config value for the given NID.
  751. * the value is cached, and read via snd_hda_codec_get_pincfg()
  752. */
  753. int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
  754. hda_nid_t nid, unsigned int cfg)
  755. {
  756. struct hda_pincfg *pin;
  757. unsigned int oldcfg;
  758. if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
  759. return -EINVAL;
  760. oldcfg = snd_hda_codec_get_pincfg(codec, nid);
  761. pin = look_up_pincfg(codec, list, nid);
  762. if (!pin) {
  763. pin = snd_array_new(list);
  764. if (!pin)
  765. return -ENOMEM;
  766. pin->nid = nid;
  767. }
  768. pin->cfg = cfg;
  769. /* change only when needed; e.g. if the pincfg is already present
  770. * in user_pins[], don't write it
  771. */
  772. cfg = snd_hda_codec_get_pincfg(codec, nid);
  773. if (oldcfg != cfg)
  774. set_pincfg(codec, nid, cfg);
  775. return 0;
  776. }
  777. /**
  778. * snd_hda_codec_set_pincfg - Override a pin default configuration
  779. * @codec: the HDA codec
  780. * @nid: NID to set the pin config
  781. * @cfg: the pin default config value
  782. *
  783. * Override a pin default configuration value in the cache.
  784. * This value can be read by snd_hda_codec_get_pincfg() in a higher
  785. * priority than the real hardware value.
  786. */
  787. int snd_hda_codec_set_pincfg(struct hda_codec *codec,
  788. hda_nid_t nid, unsigned int cfg)
  789. {
  790. return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
  791. }
  792. EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
  793. /**
  794. * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
  795. * @codec: the HDA codec
  796. * @nid: NID to get the pin config
  797. *
  798. * Get the current pin config value of the given pin NID.
  799. * If the pincfg value is cached or overridden via sysfs or driver,
  800. * returns the cached value.
  801. */
  802. unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
  803. {
  804. struct hda_pincfg *pin;
  805. #ifdef CONFIG_SND_HDA_HWDEP
  806. pin = look_up_pincfg(codec, &codec->user_pins, nid);
  807. if (pin)
  808. return pin->cfg;
  809. #endif
  810. pin = look_up_pincfg(codec, &codec->driver_pins, nid);
  811. if (pin)
  812. return pin->cfg;
  813. pin = look_up_pincfg(codec, &codec->init_pins, nid);
  814. if (pin)
  815. return pin->cfg;
  816. return 0;
  817. }
  818. EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
  819. /* restore all current pin configs */
  820. static void restore_pincfgs(struct hda_codec *codec)
  821. {
  822. int i;
  823. for (i = 0; i < codec->init_pins.used; i++) {
  824. struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
  825. set_pincfg(codec, pin->nid,
  826. snd_hda_codec_get_pincfg(codec, pin->nid));
  827. }
  828. }
  829. /**
  830. * snd_hda_shutup_pins - Shut up all pins
  831. * @codec: the HDA codec
  832. *
  833. * Clear all pin controls to shup up before suspend for avoiding click noise.
  834. * The controls aren't cached so that they can be resumed properly.
  835. */
  836. void snd_hda_shutup_pins(struct hda_codec *codec)
  837. {
  838. int i;
  839. /* don't shut up pins when unloading the driver; otherwise it breaks
  840. * the default pin setup at the next load of the driver
  841. */
  842. if (codec->bus->shutdown)
  843. return;
  844. for (i = 0; i < codec->init_pins.used; i++) {
  845. struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
  846. /* use read here for syncing after issuing each verb */
  847. snd_hda_codec_read(codec, pin->nid, 0,
  848. AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
  849. }
  850. codec->pins_shutup = 1;
  851. }
  852. EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
  853. /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
  854. static void restore_shutup_pins(struct hda_codec *codec)
  855. {
  856. int i;
  857. if (!codec->pins_shutup)
  858. return;
  859. if (codec->bus->shutdown)
  860. return;
  861. for (i = 0; i < codec->init_pins.used; i++) {
  862. struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
  863. snd_hda_codec_write(codec, pin->nid, 0,
  864. AC_VERB_SET_PIN_WIDGET_CONTROL,
  865. pin->ctrl);
  866. }
  867. codec->pins_shutup = 0;
  868. }
  869. static void init_hda_cache(struct hda_cache_rec *cache,
  870. unsigned int record_size);
  871. static void free_hda_cache(struct hda_cache_rec *cache);
  872. /* restore the initial pin cfgs and release all pincfg lists */
  873. static void restore_init_pincfgs(struct hda_codec *codec)
  874. {
  875. /* first free driver_pins and user_pins, then call restore_pincfg
  876. * so that only the values in init_pins are restored
  877. */
  878. snd_array_free(&codec->driver_pins);
  879. #ifdef CONFIG_SND_HDA_HWDEP
  880. snd_array_free(&codec->user_pins);
  881. #endif
  882. restore_pincfgs(codec);
  883. snd_array_free(&codec->init_pins);
  884. }
  885. /*
  886. * audio-converter setup caches
  887. */
  888. struct hda_cvt_setup {
  889. hda_nid_t nid;
  890. u8 stream_tag;
  891. u8 channel_id;
  892. u16 format_id;
  893. unsigned char active; /* cvt is currently used */
  894. unsigned char dirty; /* setups should be cleared */
  895. };
  896. /* get or create a cache entry for the given audio converter NID */
  897. static struct hda_cvt_setup *
  898. get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
  899. {
  900. struct hda_cvt_setup *p;
  901. int i;
  902. for (i = 0; i < codec->cvt_setups.used; i++) {
  903. p = snd_array_elem(&codec->cvt_setups, i);
  904. if (p->nid == nid)
  905. return p;
  906. }
  907. p = snd_array_new(&codec->cvt_setups);
  908. if (p)
  909. p->nid = nid;
  910. return p;
  911. }
  912. /*
  913. * codec destructor
  914. */
  915. static void snd_hda_codec_free(struct hda_codec *codec)
  916. {
  917. if (!codec)
  918. return;
  919. restore_init_pincfgs(codec);
  920. #ifdef CONFIG_SND_HDA_POWER_SAVE
  921. cancel_delayed_work(&codec->power_work);
  922. flush_workqueue(codec->bus->workq);
  923. #endif
  924. list_del(&codec->list);
  925. snd_array_free(&codec->mixers);
  926. snd_array_free(&codec->nids);
  927. codec->bus->caddr_tbl[codec->addr] = NULL;
  928. if (codec->patch_ops.free)
  929. codec->patch_ops.free(codec);
  930. module_put(codec->owner);
  931. free_hda_cache(&codec->amp_cache);
  932. free_hda_cache(&codec->cmd_cache);
  933. kfree(codec->vendor_name);
  934. kfree(codec->chip_name);
  935. kfree(codec->modelname);
  936. kfree(codec->wcaps);
  937. kfree(codec);
  938. }
  939. static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
  940. unsigned int power_state);
  941. /**
  942. * snd_hda_codec_new - create a HDA codec
  943. * @bus: the bus to assign
  944. * @codec_addr: the codec address
  945. * @codecp: the pointer to store the generated codec
  946. *
  947. * Returns 0 if successful, or a negative error code.
  948. */
  949. int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus,
  950. unsigned int codec_addr,
  951. struct hda_codec **codecp)
  952. {
  953. struct hda_codec *codec;
  954. char component[31];
  955. int err;
  956. if (snd_BUG_ON(!bus))
  957. return -EINVAL;
  958. if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
  959. return -EINVAL;
  960. if (bus->caddr_tbl[codec_addr]) {
  961. snd_printk(KERN_ERR "hda_codec: "
  962. "address 0x%x is already occupied\n", codec_addr);
  963. return -EBUSY;
  964. }
  965. codec = kzalloc(sizeof(*codec), GFP_KERNEL);
  966. if (codec == NULL) {
  967. snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
  968. return -ENOMEM;
  969. }
  970. codec->bus = bus;
  971. codec->addr = codec_addr;
  972. mutex_init(&codec->spdif_mutex);
  973. mutex_init(&codec->control_mutex);
  974. init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
  975. init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
  976. snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
  977. snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
  978. snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
  979. snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
  980. snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
  981. if (codec->bus->modelname) {
  982. codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
  983. if (!codec->modelname) {
  984. snd_hda_codec_free(codec);
  985. return -ENODEV;
  986. }
  987. }
  988. #ifdef CONFIG_SND_HDA_POWER_SAVE
  989. INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
  990. /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
  991. * the caller has to power down appropriatley after initialization
  992. * phase.
  993. */
  994. hda_keep_power_on(codec);
  995. #endif
  996. list_add_tail(&codec->list, &bus->codec_list);
  997. bus->caddr_tbl[codec_addr] = codec;
  998. codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
  999. AC_PAR_VENDOR_ID);
  1000. if (codec->vendor_id == -1)
  1001. /* read again, hopefully the access method was corrected
  1002. * in the last read...
  1003. */
  1004. codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
  1005. AC_PAR_VENDOR_ID);
  1006. codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
  1007. AC_PAR_SUBSYSTEM_ID);
  1008. codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
  1009. AC_PAR_REV_ID);
  1010. setup_fg_nodes(codec);
  1011. if (!codec->afg && !codec->mfg) {
  1012. snd_printdd("hda_codec: no AFG or MFG node found\n");
  1013. err = -ENODEV;
  1014. goto error;
  1015. }
  1016. err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg);
  1017. if (err < 0) {
  1018. snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
  1019. goto error;
  1020. }
  1021. err = read_pin_defaults(codec);
  1022. if (err < 0)
  1023. goto error;
  1024. if (!codec->subsystem_id) {
  1025. hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
  1026. codec->subsystem_id =
  1027. snd_hda_codec_read(codec, nid, 0,
  1028. AC_VERB_GET_SUBSYSTEM_ID, 0);
  1029. }
  1030. /* power-up all before initialization */
  1031. hda_set_power_state(codec,
  1032. codec->afg ? codec->afg : codec->mfg,
  1033. AC_PWRST_D0);
  1034. snd_hda_codec_proc_new(codec);
  1035. snd_hda_create_hwdep(codec);
  1036. sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
  1037. codec->subsystem_id, codec->revision_id);
  1038. snd_component_add(codec->bus->card, component);
  1039. if (codecp)
  1040. *codecp = codec;
  1041. return 0;
  1042. error:
  1043. snd_hda_codec_free(codec);
  1044. return err;
  1045. }
  1046. EXPORT_SYMBOL_HDA(snd_hda_codec_new);
  1047. /**
  1048. * snd_hda_codec_configure - (Re-)configure the HD-audio codec
  1049. * @codec: the HDA codec
  1050. *
  1051. * Start parsing of the given codec tree and (re-)initialize the whole
  1052. * patch instance.
  1053. *
  1054. * Returns 0 if successful or a negative error code.
  1055. */
  1056. int snd_hda_codec_configure(struct hda_codec *codec)
  1057. {
  1058. int err;
  1059. codec->preset = find_codec_preset(codec);
  1060. if (!codec->vendor_name || !codec->chip_name) {
  1061. err = get_codec_name(codec);
  1062. if (err < 0)
  1063. return err;
  1064. }
  1065. if (is_generic_config(codec)) {
  1066. err = snd_hda_parse_generic_codec(codec);
  1067. goto patched;
  1068. }
  1069. if (codec->preset && codec->preset->patch) {
  1070. err = codec->preset->patch(codec);
  1071. goto patched;
  1072. }
  1073. /* call the default parser */
  1074. err = snd_hda_parse_generic_codec(codec);
  1075. if (err < 0)
  1076. printk(KERN_ERR "hda-codec: No codec parser is available\n");
  1077. patched:
  1078. if (!err && codec->patch_ops.unsol_event)
  1079. err = init_unsol_queue(codec->bus);
  1080. /* audio codec should override the mixer name */
  1081. if (!err && (codec->afg || !*codec->bus->card->mixername))
  1082. snprintf(codec->bus->card->mixername,
  1083. sizeof(codec->bus->card->mixername),
  1084. "%s %s", codec->vendor_name, codec->chip_name);
  1085. return err;
  1086. }
  1087. EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
  1088. /**
  1089. * snd_hda_codec_setup_stream - set up the codec for streaming
  1090. * @codec: the CODEC to set up
  1091. * @nid: the NID to set up
  1092. * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
  1093. * @channel_id: channel id to pass, zero based.
  1094. * @format: stream format.
  1095. */
  1096. void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
  1097. u32 stream_tag,
  1098. int channel_id, int format)
  1099. {
  1100. struct hda_codec *c;
  1101. struct hda_cvt_setup *p;
  1102. unsigned int oldval, newval;
  1103. int i;
  1104. if (!nid)
  1105. return;
  1106. snd_printdd("hda_codec_setup_stream: "
  1107. "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
  1108. nid, stream_tag, channel_id, format);
  1109. p = get_hda_cvt_setup(codec, nid);
  1110. if (!p)
  1111. return;
  1112. /* update the stream-id if changed */
  1113. if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
  1114. oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
  1115. newval = (stream_tag << 4) | channel_id;
  1116. if (oldval != newval)
  1117. snd_hda_codec_write(codec, nid, 0,
  1118. AC_VERB_SET_CHANNEL_STREAMID,
  1119. newval);
  1120. p->stream_tag = stream_tag;
  1121. p->channel_id = channel_id;
  1122. }
  1123. /* update the format-id if changed */
  1124. if (p->format_id != format) {
  1125. oldval = snd_hda_codec_read(codec, nid, 0,
  1126. AC_VERB_GET_STREAM_FORMAT, 0);
  1127. if (oldval != format) {
  1128. msleep(1);
  1129. snd_hda_codec_write(codec, nid, 0,
  1130. AC_VERB_SET_STREAM_FORMAT,
  1131. format);
  1132. }
  1133. p->format_id = format;
  1134. }
  1135. p->active = 1;
  1136. p->dirty = 0;
  1137. /* make other inactive cvts with the same stream-tag dirty */
  1138. list_for_each_entry(c, &codec->bus->codec_list, list) {
  1139. for (i = 0; i < c->cvt_setups.used; i++) {
  1140. p = snd_array_elem(&c->cvt_setups, i);
  1141. if (!p->active && p->stream_tag == stream_tag)
  1142. p->dirty = 1;
  1143. }
  1144. }
  1145. }
  1146. EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
  1147. static void really_cleanup_stream(struct hda_codec *codec,
  1148. struct hda_cvt_setup *q);
  1149. /**
  1150. * __snd_hda_codec_cleanup_stream - clean up the codec for closing
  1151. * @codec: the CODEC to clean up
  1152. * @nid: the NID to clean up
  1153. * @do_now: really clean up the stream instead of clearing the active flag
  1154. */
  1155. void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
  1156. int do_now)
  1157. {
  1158. struct hda_cvt_setup *p;
  1159. if (!nid)
  1160. return;
  1161. snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
  1162. p = get_hda_cvt_setup(codec, nid);
  1163. if (p) {
  1164. /* here we just clear the active flag when do_now isn't set;
  1165. * actual clean-ups will be done later in
  1166. * purify_inactive_streams() called from snd_hda_codec_prpapre()
  1167. */
  1168. if (do_now)
  1169. really_cleanup_stream(codec, p);
  1170. else
  1171. p->active = 0;
  1172. }
  1173. }
  1174. EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
  1175. static void really_cleanup_stream(struct hda_codec *codec,
  1176. struct hda_cvt_setup *q)
  1177. {
  1178. hda_nid_t nid = q->nid;
  1179. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
  1180. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
  1181. memset(q, 0, sizeof(*q));
  1182. q->nid = nid;
  1183. }
  1184. /* clean up the all conflicting obsolete streams */
  1185. static void purify_inactive_streams(struct hda_codec *codec)
  1186. {
  1187. struct hda_codec *c;
  1188. int i;
  1189. list_for_each_entry(c, &codec->bus->codec_list, list) {
  1190. for (i = 0; i < c->cvt_setups.used; i++) {
  1191. struct hda_cvt_setup *p;
  1192. p = snd_array_elem(&c->cvt_setups, i);
  1193. if (p->dirty)
  1194. really_cleanup_stream(c, p);
  1195. }
  1196. }
  1197. }
  1198. /* clean up all streams; called from suspend */
  1199. static void hda_cleanup_all_streams(struct hda_codec *codec)
  1200. {
  1201. int i;
  1202. for (i = 0; i < codec->cvt_setups.used; i++) {
  1203. struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
  1204. if (p->stream_tag)
  1205. really_cleanup_stream(codec, p);
  1206. }
  1207. }
  1208. /*
  1209. * amp access functions
  1210. */
  1211. /* FIXME: more better hash key? */
  1212. #define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
  1213. #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
  1214. #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
  1215. #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
  1216. #define INFO_AMP_CAPS (1<<0)
  1217. #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
  1218. /* initialize the hash table */
  1219. static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
  1220. unsigned int record_size)
  1221. {
  1222. memset(cache, 0, sizeof(*cache));
  1223. memset(cache->hash, 0xff, sizeof(cache->hash));
  1224. snd_array_init(&cache->buf, record_size, 64);
  1225. }
  1226. static void free_hda_cache(struct hda_cache_rec *cache)
  1227. {
  1228. snd_array_free(&cache->buf);
  1229. }
  1230. /* query the hash. allocate an entry if not found. */
  1231. static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
  1232. {
  1233. u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
  1234. u16 cur = cache->hash[idx];
  1235. struct hda_cache_head *info;
  1236. while (cur != 0xffff) {
  1237. info = snd_array_elem(&cache->buf, cur);
  1238. if (info->key == key)
  1239. return info;
  1240. cur = info->next;
  1241. }
  1242. return NULL;
  1243. }
  1244. /* query the hash. allocate an entry if not found. */
  1245. static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
  1246. u32 key)
  1247. {
  1248. struct hda_cache_head *info = get_hash(cache, key);
  1249. if (!info) {
  1250. u16 idx, cur;
  1251. /* add a new hash entry */
  1252. info = snd_array_new(&cache->buf);
  1253. if (!info)
  1254. return NULL;
  1255. cur = snd_array_index(&cache->buf, info);
  1256. info->key = key;
  1257. info->val = 0;
  1258. idx = key % (u16)ARRAY_SIZE(cache->hash);
  1259. info->next = cache->hash[idx];
  1260. cache->hash[idx] = cur;
  1261. }
  1262. return info;
  1263. }
  1264. /* query and allocate an amp hash entry */
  1265. static inline struct hda_amp_info *
  1266. get_alloc_amp_hash(struct hda_codec *codec, u32 key)
  1267. {
  1268. return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
  1269. }
  1270. /**
  1271. * query_amp_caps - query AMP capabilities
  1272. * @codec: the HD-auio codec
  1273. * @nid: the NID to query
  1274. * @direction: either #HDA_INPUT or #HDA_OUTPUT
  1275. *
  1276. * Query AMP capabilities for the given widget and direction.
  1277. * Returns the obtained capability bits.
  1278. *
  1279. * When cap bits have been already read, this doesn't read again but
  1280. * returns the cached value.
  1281. */
  1282. u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
  1283. {
  1284. struct hda_amp_info *info;
  1285. info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
  1286. if (!info)
  1287. return 0;
  1288. if (!(info->head.val & INFO_AMP_CAPS)) {
  1289. if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
  1290. nid = codec->afg;
  1291. info->amp_caps = snd_hda_param_read(codec, nid,
  1292. direction == HDA_OUTPUT ?
  1293. AC_PAR_AMP_OUT_CAP :
  1294. AC_PAR_AMP_IN_CAP);
  1295. if (info->amp_caps)
  1296. info->head.val |= INFO_AMP_CAPS;
  1297. }
  1298. return info->amp_caps;
  1299. }
  1300. EXPORT_SYMBOL_HDA(query_amp_caps);
  1301. /**
  1302. * snd_hda_override_amp_caps - Override the AMP capabilities
  1303. * @codec: the CODEC to clean up
  1304. * @nid: the NID to clean up
  1305. * @direction: either #HDA_INPUT or #HDA_OUTPUT
  1306. * @caps: the capability bits to set
  1307. *
  1308. * Override the cached AMP caps bits value by the given one.
  1309. * This function is useful if the driver needs to adjust the AMP ranges,
  1310. * e.g. limit to 0dB, etc.
  1311. *
  1312. * Returns zero if successful or a negative error code.
  1313. */
  1314. int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
  1315. unsigned int caps)
  1316. {
  1317. struct hda_amp_info *info;
  1318. info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
  1319. if (!info)
  1320. return -EINVAL;
  1321. info->amp_caps = caps;
  1322. info->head.val |= INFO_AMP_CAPS;
  1323. return 0;
  1324. }
  1325. EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
  1326. static unsigned int
  1327. query_caps_hash(struct hda_codec *codec, hda_nid_t nid, u32 key,
  1328. unsigned int (*func)(struct hda_codec *, hda_nid_t))
  1329. {
  1330. struct hda_amp_info *info;
  1331. info = get_alloc_amp_hash(codec, key);
  1332. if (!info)
  1333. return 0;
  1334. if (!info->head.val) {
  1335. info->head.val |= INFO_AMP_CAPS;
  1336. info->amp_caps = func(codec, nid);
  1337. }
  1338. return info->amp_caps;
  1339. }
  1340. static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid)
  1341. {
  1342. return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
  1343. }
  1344. /**
  1345. * snd_hda_query_pin_caps - Query PIN capabilities
  1346. * @codec: the HD-auio codec
  1347. * @nid: the NID to query
  1348. *
  1349. * Query PIN capabilities for the given widget.
  1350. * Returns the obtained capability bits.
  1351. *
  1352. * When cap bits have been already read, this doesn't read again but
  1353. * returns the cached value.
  1354. */
  1355. u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
  1356. {
  1357. return query_caps_hash(codec, nid, HDA_HASH_PINCAP_KEY(nid),
  1358. read_pin_cap);
  1359. }
  1360. EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
  1361. /**
  1362. * snd_hda_pin_sense - execute pin sense measurement
  1363. * @codec: the CODEC to sense
  1364. * @nid: the pin NID to sense
  1365. *
  1366. * Execute necessary pin sense measurement and return its Presence Detect,
  1367. * Impedance, ELD Valid etc. status bits.
  1368. */
  1369. u32 snd_hda_pin_sense(struct hda_codec *codec, hda_nid_t nid)
  1370. {
  1371. u32 pincap;
  1372. if (!codec->no_trigger_sense) {
  1373. pincap = snd_hda_query_pin_caps(codec, nid);
  1374. if (pincap & AC_PINCAP_TRIG_REQ) /* need trigger? */
  1375. snd_hda_codec_read(codec, nid, 0,
  1376. AC_VERB_SET_PIN_SENSE, 0);
  1377. }
  1378. return snd_hda_codec_read(codec, nid, 0,
  1379. AC_VERB_GET_PIN_SENSE, 0);
  1380. }
  1381. EXPORT_SYMBOL_HDA(snd_hda_pin_sense);
  1382. /**
  1383. * snd_hda_jack_detect - query pin Presence Detect status
  1384. * @codec: the CODEC to sense
  1385. * @nid: the pin NID to sense
  1386. *
  1387. * Query and return the pin's Presence Detect status.
  1388. */
  1389. int snd_hda_jack_detect(struct hda_codec *codec, hda_nid_t nid)
  1390. {
  1391. u32 sense = snd_hda_pin_sense(codec, nid);
  1392. return !!(sense & AC_PINSENSE_PRESENCE);
  1393. }
  1394. EXPORT_SYMBOL_HDA(snd_hda_jack_detect);
  1395. /*
  1396. * read the current volume to info
  1397. * if the cache exists, read the cache value.
  1398. */
  1399. static unsigned int get_vol_mute(struct hda_codec *codec,
  1400. struct hda_amp_info *info, hda_nid_t nid,
  1401. int ch, int direction, int index)
  1402. {
  1403. u32 val, parm;
  1404. if (info->head.val & INFO_AMP_VOL(ch))
  1405. return info->vol[ch];
  1406. parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
  1407. parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
  1408. parm |= index;
  1409. val = snd_hda_codec_read(codec, nid, 0,
  1410. AC_VERB_GET_AMP_GAIN_MUTE, parm);
  1411. info->vol[ch] = val & 0xff;
  1412. info->head.val |= INFO_AMP_VOL(ch);
  1413. return info->vol[ch];
  1414. }
  1415. /*
  1416. * write the current volume in info to the h/w and update the cache
  1417. */
  1418. static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
  1419. hda_nid_t nid, int ch, int direction, int index,
  1420. int val)
  1421. {
  1422. u32 parm;
  1423. parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
  1424. parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
  1425. parm |= index << AC_AMP_SET_INDEX_SHIFT;
  1426. parm |= val;
  1427. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
  1428. info->vol[ch] = val;
  1429. }
  1430. /**
  1431. * snd_hda_codec_amp_read - Read AMP value
  1432. * @codec: HD-audio codec
  1433. * @nid: NID to read the AMP value
  1434. * @ch: channel (left=0 or right=1)
  1435. * @direction: #HDA_INPUT or #HDA_OUTPUT
  1436. * @index: the index value (only for input direction)
  1437. *
  1438. * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
  1439. */
  1440. int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
  1441. int direction, int index)
  1442. {
  1443. struct hda_amp_info *info;
  1444. info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
  1445. if (!info)
  1446. return 0;
  1447. return get_vol_mute(codec, info, nid, ch, direction, index);
  1448. }
  1449. EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
  1450. /**
  1451. * snd_hda_codec_amp_update - update the AMP value
  1452. * @codec: HD-audio codec
  1453. * @nid: NID to read the AMP value
  1454. * @ch: channel (left=0 or right=1)
  1455. * @direction: #HDA_INPUT or #HDA_OUTPUT
  1456. * @idx: the index value (only for input direction)
  1457. * @mask: bit mask to set
  1458. * @val: the bits value to set
  1459. *
  1460. * Update the AMP value with a bit mask.
  1461. * Returns 0 if the value is unchanged, 1 if changed.
  1462. */
  1463. int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
  1464. int direction, int idx, int mask, int val)
  1465. {
  1466. struct hda_amp_info *info;
  1467. info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
  1468. if (!info)
  1469. return 0;
  1470. if (snd_BUG_ON(mask & ~0xff))
  1471. mask &= 0xff;
  1472. val &= mask;
  1473. val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
  1474. if (info->vol[ch] == val)
  1475. return 0;
  1476. put_vol_mute(codec, info, nid, ch, direction, idx, val);
  1477. return 1;
  1478. }
  1479. EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
  1480. /**
  1481. * snd_hda_codec_amp_stereo - update the AMP stereo values
  1482. * @codec: HD-audio codec
  1483. * @nid: NID to read the AMP value
  1484. * @direction: #HDA_INPUT or #HDA_OUTPUT
  1485. * @idx: the index value (only for input direction)
  1486. * @mask: bit mask to set
  1487. * @val: the bits value to set
  1488. *
  1489. * Update the AMP values like snd_hda_codec_amp_update(), but for a
  1490. * stereo widget with the same mask and value.
  1491. */
  1492. int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
  1493. int direction, int idx, int mask, int val)
  1494. {
  1495. int ch, ret = 0;
  1496. if (snd_BUG_ON(mask & ~0xff))
  1497. mask &= 0xff;
  1498. for (ch = 0; ch < 2; ch++)
  1499. ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
  1500. idx, mask, val);
  1501. return ret;
  1502. }
  1503. EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
  1504. #ifdef SND_HDA_NEEDS_RESUME
  1505. /**
  1506. * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
  1507. * @codec: HD-audio codec
  1508. *
  1509. * Resume the all amp commands from the cache.
  1510. */
  1511. void snd_hda_codec_resume_amp(struct hda_codec *codec)
  1512. {
  1513. struct hda_amp_info *buffer = codec->amp_cache.buf.list;
  1514. int i;
  1515. for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
  1516. u32 key = buffer->head.key;
  1517. hda_nid_t nid;
  1518. unsigned int idx, dir, ch;
  1519. if (!key)
  1520. continue;
  1521. nid = key & 0xff;
  1522. idx = (key >> 16) & 0xff;
  1523. dir = (key >> 24) & 0xff;
  1524. for (ch = 0; ch < 2; ch++) {
  1525. if (!(buffer->head.val & INFO_AMP_VOL(ch)))
  1526. continue;
  1527. put_vol_mute(codec, buffer, nid, ch, dir, idx,
  1528. buffer->vol[ch]);
  1529. }
  1530. }
  1531. }
  1532. EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
  1533. #endif /* SND_HDA_NEEDS_RESUME */
  1534. static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
  1535. unsigned int ofs)
  1536. {
  1537. u32 caps = query_amp_caps(codec, nid, dir);
  1538. /* get num steps */
  1539. caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
  1540. if (ofs < caps)
  1541. caps -= ofs;
  1542. return caps;
  1543. }
  1544. /**
  1545. * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
  1546. *
  1547. * The control element is supposed to have the private_value field
  1548. * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
  1549. */
  1550. int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
  1551. struct snd_ctl_elem_info *uinfo)
  1552. {
  1553. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1554. u16 nid = get_amp_nid(kcontrol);
  1555. u8 chs = get_amp_channels(kcontrol);
  1556. int dir = get_amp_direction(kcontrol);
  1557. unsigned int ofs = get_amp_offset(kcontrol);
  1558. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1559. uinfo->count = chs == 3 ? 2 : 1;
  1560. uinfo->value.integer.min = 0;
  1561. uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
  1562. if (!uinfo->value.integer.max) {
  1563. printk(KERN_WARNING "hda_codec: "
  1564. "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
  1565. kcontrol->id.name);
  1566. return -EINVAL;
  1567. }
  1568. return 0;
  1569. }
  1570. EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
  1571. static inline unsigned int
  1572. read_amp_value(struct hda_codec *codec, hda_nid_t nid,
  1573. int ch, int dir, int idx, unsigned int ofs)
  1574. {
  1575. unsigned int val;
  1576. val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
  1577. val &= HDA_AMP_VOLMASK;
  1578. if (val >= ofs)
  1579. val -= ofs;
  1580. else
  1581. val = 0;
  1582. return val;
  1583. }
  1584. static inline int
  1585. update_amp_value(struct hda_codec *codec, hda_nid_t nid,
  1586. int ch, int dir, int idx, unsigned int ofs,
  1587. unsigned int val)
  1588. {
  1589. unsigned int maxval;
  1590. if (val > 0)
  1591. val += ofs;
  1592. /* ofs = 0: raw max value */
  1593. maxval = get_amp_max_value(codec, nid, dir, 0);
  1594. if (val > maxval)
  1595. val = maxval;
  1596. return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
  1597. HDA_AMP_VOLMASK, val);
  1598. }
  1599. /**
  1600. * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
  1601. *
  1602. * The control element is supposed to have the private_value field
  1603. * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
  1604. */
  1605. int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
  1606. struct snd_ctl_elem_value *ucontrol)
  1607. {
  1608. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1609. hda_nid_t nid = get_amp_nid(kcontrol);
  1610. int chs = get_amp_channels(kcontrol);
  1611. int dir = get_amp_direction(kcontrol);
  1612. int idx = get_amp_index(kcontrol);
  1613. unsigned int ofs = get_amp_offset(kcontrol);
  1614. long *valp = ucontrol->value.integer.value;
  1615. if (chs & 1)
  1616. *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
  1617. if (chs & 2)
  1618. *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
  1619. return 0;
  1620. }
  1621. EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
  1622. /**
  1623. * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
  1624. *
  1625. * The control element is supposed to have the private_value field
  1626. * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
  1627. */
  1628. int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
  1629. struct snd_ctl_elem_value *ucontrol)
  1630. {
  1631. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1632. hda_nid_t nid = get_amp_nid(kcontrol);
  1633. int chs = get_amp_channels(kcontrol);
  1634. int dir = get_amp_direction(kcontrol);
  1635. int idx = get_amp_index(kcontrol);
  1636. unsigned int ofs = get_amp_offset(kcontrol);
  1637. long *valp = ucontrol->value.integer.value;
  1638. int change = 0;
  1639. snd_hda_power_up(codec);
  1640. if (chs & 1) {
  1641. change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
  1642. valp++;
  1643. }
  1644. if (chs & 2)
  1645. change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
  1646. snd_hda_power_down(codec);
  1647. return change;
  1648. }
  1649. EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
  1650. /**
  1651. * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
  1652. *
  1653. * The control element is supposed to have the private_value field
  1654. * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
  1655. */
  1656. int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
  1657. unsigned int size, unsigned int __user *_tlv)
  1658. {
  1659. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1660. hda_nid_t nid = get_amp_nid(kcontrol);
  1661. int dir = get_amp_direction(kcontrol);
  1662. unsigned int ofs = get_amp_offset(kcontrol);
  1663. u32 caps, val1, val2;
  1664. if (size < 4 * sizeof(unsigned int))
  1665. return -ENOMEM;
  1666. caps = query_amp_caps(codec, nid, dir);
  1667. val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
  1668. val2 = (val2 + 1) * 25;
  1669. val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
  1670. val1 += ofs;
  1671. val1 = ((int)val1) * ((int)val2);
  1672. if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
  1673. return -EFAULT;
  1674. if (put_user(2 * sizeof(unsigned int), _tlv + 1))
  1675. return -EFAULT;
  1676. if (put_user(val1, _tlv + 2))
  1677. return -EFAULT;
  1678. if (put_user(val2, _tlv + 3))
  1679. return -EFAULT;
  1680. return 0;
  1681. }
  1682. EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
  1683. /**
  1684. * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
  1685. * @codec: HD-audio codec
  1686. * @nid: NID of a reference widget
  1687. * @dir: #HDA_INPUT or #HDA_OUTPUT
  1688. * @tlv: TLV data to be stored, at least 4 elements
  1689. *
  1690. * Set (static) TLV data for a virtual master volume using the AMP caps
  1691. * obtained from the reference NID.
  1692. * The volume range is recalculated as if the max volume is 0dB.
  1693. */
  1694. void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
  1695. unsigned int *tlv)
  1696. {
  1697. u32 caps;
  1698. int nums, step;
  1699. caps = query_amp_caps(codec, nid, dir);
  1700. nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
  1701. step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
  1702. step = (step + 1) * 25;
  1703. tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
  1704. tlv[1] = 2 * sizeof(unsigned int);
  1705. tlv[2] = -nums * step;
  1706. tlv[3] = step;
  1707. }
  1708. EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
  1709. /* find a mixer control element with the given name */
  1710. static struct snd_kcontrol *
  1711. _snd_hda_find_mixer_ctl(struct hda_codec *codec,
  1712. const char *name, int idx)
  1713. {
  1714. struct snd_ctl_elem_id id;
  1715. memset(&id, 0, sizeof(id));
  1716. id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  1717. id.index = idx;
  1718. if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
  1719. return NULL;
  1720. strcpy(id.name, name);
  1721. return snd_ctl_find_id(codec->bus->card, &id);
  1722. }
  1723. /**
  1724. * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
  1725. * @codec: HD-audio codec
  1726. * @name: ctl id name string
  1727. *
  1728. * Get the control element with the given id string and IFACE_MIXER.
  1729. */
  1730. struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
  1731. const char *name)
  1732. {
  1733. return _snd_hda_find_mixer_ctl(codec, name, 0);
  1734. }
  1735. EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
  1736. /**
  1737. * snd_hda_ctl_add - Add a control element and assign to the codec
  1738. * @codec: HD-audio codec
  1739. * @nid: corresponding NID (optional)
  1740. * @kctl: the control element to assign
  1741. *
  1742. * Add the given control element to an array inside the codec instance.
  1743. * All control elements belonging to a codec are supposed to be added
  1744. * by this function so that a proper clean-up works at the free or
  1745. * reconfiguration time.
  1746. *
  1747. * If non-zero @nid is passed, the NID is assigned to the control element.
  1748. * The assignment is shown in the codec proc file.
  1749. *
  1750. * snd_hda_ctl_add() checks the control subdev id field whether
  1751. * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
  1752. * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
  1753. * specifies if kctl->private_value is a HDA amplifier value.
  1754. */
  1755. int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
  1756. struct snd_kcontrol *kctl)
  1757. {
  1758. int err;
  1759. unsigned short flags = 0;
  1760. struct hda_nid_item *item;
  1761. if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
  1762. flags |= HDA_NID_ITEM_AMP;
  1763. if (nid == 0)
  1764. nid = get_amp_nid_(kctl->private_value);
  1765. }
  1766. if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
  1767. nid = kctl->id.subdevice & 0xffff;
  1768. if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
  1769. kctl->id.subdevice = 0;
  1770. err = snd_ctl_add(codec->bus->card, kctl);
  1771. if (err < 0)
  1772. return err;
  1773. item = snd_array_new(&codec->mixers);
  1774. if (!item)
  1775. return -ENOMEM;
  1776. item->kctl = kctl;
  1777. item->nid = nid;
  1778. item->flags = flags;
  1779. return 0;
  1780. }
  1781. EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
  1782. /**
  1783. * snd_hda_add_nid - Assign a NID to a control element
  1784. * @codec: HD-audio codec
  1785. * @nid: corresponding NID (optional)
  1786. * @kctl: the control element to assign
  1787. * @index: index to kctl
  1788. *
  1789. * Add the given control element to an array inside the codec instance.
  1790. * This function is used when #snd_hda_ctl_add cannot be used for 1:1
  1791. * NID:KCTL mapping - for example "Capture Source" selector.
  1792. */
  1793. int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
  1794. unsigned int index, hda_nid_t nid)
  1795. {
  1796. struct hda_nid_item *item;
  1797. if (nid > 0) {
  1798. item = snd_array_new(&codec->nids);
  1799. if (!item)
  1800. return -ENOMEM;
  1801. item->kctl = kctl;
  1802. item->index = index;
  1803. item->nid = nid;
  1804. return 0;
  1805. }
  1806. printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
  1807. kctl->id.name, kctl->id.index, index);
  1808. return -EINVAL;
  1809. }
  1810. EXPORT_SYMBOL_HDA(snd_hda_add_nid);
  1811. /**
  1812. * snd_hda_ctls_clear - Clear all controls assigned to the given codec
  1813. * @codec: HD-audio codec
  1814. */
  1815. void snd_hda_ctls_clear(struct hda_codec *codec)
  1816. {
  1817. int i;
  1818. struct hda_nid_item *items = codec->mixers.list;
  1819. for (i = 0; i < codec->mixers.used; i++)
  1820. snd_ctl_remove(codec->bus->card, items[i].kctl);
  1821. snd_array_free(&codec->mixers);
  1822. snd_array_free(&codec->nids);
  1823. }
  1824. /* pseudo device locking
  1825. * toggle card->shutdown to allow/disallow the device access (as a hack)
  1826. */
  1827. static int hda_lock_devices(struct snd_card *card)
  1828. {
  1829. spin_lock(&card->files_lock);
  1830. if (card->shutdown) {
  1831. spin_unlock(&card->files_lock);
  1832. return -EINVAL;
  1833. }
  1834. card->shutdown = 1;
  1835. spin_unlock(&card->files_lock);
  1836. return 0;
  1837. }
  1838. static void hda_unlock_devices(struct snd_card *card)
  1839. {
  1840. spin_lock(&card->files_lock);
  1841. card->shutdown = 0;
  1842. spin_unlock(&card->files_lock);
  1843. }
  1844. /**
  1845. * snd_hda_codec_reset - Clear all objects assigned to the codec
  1846. * @codec: HD-audio codec
  1847. *
  1848. * This frees the all PCM and control elements assigned to the codec, and
  1849. * clears the caches and restores the pin default configurations.
  1850. *
  1851. * When a device is being used, it returns -EBSY. If successfully freed,
  1852. * returns zero.
  1853. */
  1854. int snd_hda_codec_reset(struct hda_codec *codec)
  1855. {
  1856. struct snd_card *card = codec->bus->card;
  1857. int i, pcm;
  1858. if (hda_lock_devices(card) < 0)
  1859. return -EBUSY;
  1860. /* check whether the codec isn't used by any mixer or PCM streams */
  1861. if (!list_empty(&card->ctl_files)) {
  1862. hda_unlock_devices(card);
  1863. return -EBUSY;
  1864. }
  1865. for (pcm = 0; pcm < codec->num_pcms; pcm++) {
  1866. struct hda_pcm *cpcm = &codec->pcm_info[pcm];
  1867. if (!cpcm->pcm)
  1868. continue;
  1869. if (cpcm->pcm->streams[0].substream_opened ||
  1870. cpcm->pcm->streams[1].substream_opened) {
  1871. hda_unlock_devices(card);
  1872. return -EBUSY;
  1873. }
  1874. }
  1875. /* OK, let it free */
  1876. #ifdef CONFIG_SND_HDA_POWER_SAVE
  1877. cancel_delayed_work(&codec->power_work);
  1878. flush_workqueue(codec->bus->workq);
  1879. #endif
  1880. snd_hda_ctls_clear(codec);
  1881. /* relase PCMs */
  1882. for (i = 0; i < codec->num_pcms; i++) {
  1883. if (codec->pcm_info[i].pcm) {
  1884. snd_device_free(card, codec->pcm_info[i].pcm);
  1885. clear_bit(codec->pcm_info[i].device,
  1886. codec->bus->pcm_dev_bits);
  1887. }
  1888. }
  1889. if (codec->patch_ops.free)
  1890. codec->patch_ops.free(codec);
  1891. codec->proc_widget_hook = NULL;
  1892. codec->spec = NULL;
  1893. free_hda_cache(&codec->amp_cache);
  1894. free_hda_cache(&codec->cmd_cache);
  1895. init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
  1896. init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
  1897. /* free only driver_pins so that init_pins + user_pins are restored */
  1898. snd_array_free(&codec->driver_pins);
  1899. restore_pincfgs(codec);
  1900. codec->num_pcms = 0;
  1901. codec->pcm_info = NULL;
  1902. codec->preset = NULL;
  1903. memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
  1904. codec->slave_dig_outs = NULL;
  1905. codec->spdif_status_reset = 0;
  1906. module_put(codec->owner);
  1907. codec->owner = NULL;
  1908. /* allow device access again */
  1909. hda_unlock_devices(card);
  1910. return 0;
  1911. }
  1912. /**
  1913. * snd_hda_add_vmaster - create a virtual master control and add slaves
  1914. * @codec: HD-audio codec
  1915. * @name: vmaster control name
  1916. * @tlv: TLV data (optional)
  1917. * @slaves: slave control names (optional)
  1918. *
  1919. * Create a virtual master control with the given name. The TLV data
  1920. * must be either NULL or a valid data.
  1921. *
  1922. * @slaves is a NULL-terminated array of strings, each of which is a
  1923. * slave control name. All controls with these names are assigned to
  1924. * the new virtual master control.
  1925. *
  1926. * This function returns zero if successful or a negative error code.
  1927. */
  1928. int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
  1929. unsigned int *tlv, const char **slaves)
  1930. {
  1931. struct snd_kcontrol *kctl;
  1932. const char **s;
  1933. int err;
  1934. for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
  1935. ;
  1936. if (!*s) {
  1937. snd_printdd("No slave found for %s\n", name);
  1938. return 0;
  1939. }
  1940. kctl = snd_ctl_make_virtual_master(name, tlv);
  1941. if (!kctl)
  1942. return -ENOMEM;
  1943. err = snd_hda_ctl_add(codec, 0, kctl);
  1944. if (err < 0)
  1945. return err;
  1946. for (s = slaves; *s; s++) {
  1947. struct snd_kcontrol *sctl;
  1948. int i = 0;
  1949. for (;;) {
  1950. sctl = _snd_hda_find_mixer_ctl(codec, *s, i);
  1951. if (!sctl) {
  1952. if (!i)
  1953. snd_printdd("Cannot find slave %s, "
  1954. "skipped\n", *s);
  1955. break;
  1956. }
  1957. err = snd_ctl_add_slave(kctl, sctl);
  1958. if (err < 0)
  1959. return err;
  1960. i++;
  1961. }
  1962. }
  1963. return 0;
  1964. }
  1965. EXPORT_SYMBOL_HDA(snd_hda_add_vmaster);
  1966. /**
  1967. * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
  1968. *
  1969. * The control element is supposed to have the private_value field
  1970. * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
  1971. */
  1972. int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
  1973. struct snd_ctl_elem_info *uinfo)
  1974. {
  1975. int chs = get_amp_channels(kcontrol);
  1976. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1977. uinfo->count = chs == 3 ? 2 : 1;
  1978. uinfo->value.integer.min = 0;
  1979. uinfo->value.integer.max = 1;
  1980. return 0;
  1981. }
  1982. EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
  1983. /**
  1984. * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
  1985. *
  1986. * The control element is supposed to have the private_value field
  1987. * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
  1988. */
  1989. int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
  1990. struct snd_ctl_elem_value *ucontrol)
  1991. {
  1992. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1993. hda_nid_t nid = get_amp_nid(kcontrol);
  1994. int chs = get_amp_channels(kcontrol);
  1995. int dir = get_amp_direction(kcontrol);
  1996. int idx = get_amp_index(kcontrol);
  1997. long *valp = ucontrol->value.integer.value;
  1998. if (chs & 1)
  1999. *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
  2000. HDA_AMP_MUTE) ? 0 : 1;
  2001. if (chs & 2)
  2002. *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
  2003. HDA_AMP_MUTE) ? 0 : 1;
  2004. return 0;
  2005. }
  2006. EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
  2007. /**
  2008. * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
  2009. *
  2010. * The control element is supposed to have the private_value field
  2011. * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
  2012. */
  2013. int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
  2014. struct snd_ctl_elem_value *ucontrol)
  2015. {
  2016. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2017. hda_nid_t nid = get_amp_nid(kcontrol);
  2018. int chs = get_amp_channels(kcontrol);
  2019. int dir = get_amp_direction(kcontrol);
  2020. int idx = get_amp_index(kcontrol);
  2021. long *valp = ucontrol->value.integer.value;
  2022. int change = 0;
  2023. snd_hda_power_up(codec);
  2024. if (chs & 1) {
  2025. change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
  2026. HDA_AMP_MUTE,
  2027. *valp ? 0 : HDA_AMP_MUTE);
  2028. valp++;
  2029. }
  2030. if (chs & 2)
  2031. change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
  2032. HDA_AMP_MUTE,
  2033. *valp ? 0 : HDA_AMP_MUTE);
  2034. #ifdef CONFIG_SND_HDA_POWER_SAVE
  2035. if (codec->patch_ops.check_power_status)
  2036. codec->patch_ops.check_power_status(codec, nid);
  2037. #endif
  2038. snd_hda_power_down(codec);
  2039. return change;
  2040. }
  2041. EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
  2042. #ifdef CONFIG_SND_HDA_INPUT_BEEP
  2043. /**
  2044. * snd_hda_mixer_amp_switch_put_beep - Put callback for a beep AMP switch
  2045. *
  2046. * This function calls snd_hda_enable_beep_device(), which behaves differently
  2047. * depending on beep_mode option.
  2048. */
  2049. int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol,
  2050. struct snd_ctl_elem_value *ucontrol)
  2051. {
  2052. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2053. long *valp = ucontrol->value.integer.value;
  2054. snd_hda_enable_beep_device(codec, *valp);
  2055. return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
  2056. }
  2057. EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep);
  2058. #endif /* CONFIG_SND_HDA_INPUT_BEEP */
  2059. /*
  2060. * bound volume controls
  2061. *
  2062. * bind multiple volumes (# indices, from 0)
  2063. */
  2064. #define AMP_VAL_IDX_SHIFT 19
  2065. #define AMP_VAL_IDX_MASK (0x0f<<19)
  2066. /**
  2067. * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
  2068. *
  2069. * The control element is supposed to have the private_value field
  2070. * set up via HDA_BIND_MUTE*() macros.
  2071. */
  2072. int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
  2073. struct snd_ctl_elem_value *ucontrol)
  2074. {
  2075. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2076. unsigned long pval;
  2077. int err;
  2078. mutex_lock(&codec->control_mutex);
  2079. pval = kcontrol->private_value;
  2080. kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
  2081. err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
  2082. kcontrol->private_value = pval;
  2083. mutex_unlock(&codec->control_mutex);
  2084. return err;
  2085. }
  2086. EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
  2087. /**
  2088. * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
  2089. *
  2090. * The control element is supposed to have the private_value field
  2091. * set up via HDA_BIND_MUTE*() macros.
  2092. */
  2093. int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
  2094. struct snd_ctl_elem_value *ucontrol)
  2095. {
  2096. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2097. unsigned long pval;
  2098. int i, indices, err = 0, change = 0;
  2099. mutex_lock(&codec->control_mutex);
  2100. pval = kcontrol->private_value;
  2101. indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
  2102. for (i = 0; i < indices; i++) {
  2103. kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
  2104. (i << AMP_VAL_IDX_SHIFT);
  2105. err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
  2106. if (err < 0)
  2107. break;
  2108. change |= err;
  2109. }
  2110. kcontrol->private_value = pval;
  2111. mutex_unlock(&codec->control_mutex);
  2112. return err < 0 ? err : change;
  2113. }
  2114. EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
  2115. /**
  2116. * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
  2117. *
  2118. * The control element is supposed to have the private_value field
  2119. * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
  2120. */
  2121. int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
  2122. struct snd_ctl_elem_info *uinfo)
  2123. {
  2124. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2125. struct hda_bind_ctls *c;
  2126. int err;
  2127. mutex_lock(&codec->control_mutex);
  2128. c = (struct hda_bind_ctls *)kcontrol->private_value;
  2129. kcontrol->private_value = *c->values;
  2130. err = c->ops->info(kcontrol, uinfo);
  2131. kcontrol->private_value = (long)c;
  2132. mutex_unlock(&codec->control_mutex);
  2133. return err;
  2134. }
  2135. EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
  2136. /**
  2137. * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
  2138. *
  2139. * The control element is supposed to have the private_value field
  2140. * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
  2141. */
  2142. int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
  2143. struct snd_ctl_elem_value *ucontrol)
  2144. {
  2145. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2146. struct hda_bind_ctls *c;
  2147. int err;
  2148. mutex_lock(&codec->control_mutex);
  2149. c = (struct hda_bind_ctls *)kcontrol->private_value;
  2150. kcontrol->private_value = *c->values;
  2151. err = c->ops->get(kcontrol, ucontrol);
  2152. kcontrol->private_value = (long)c;
  2153. mutex_unlock(&codec->control_mutex);
  2154. return err;
  2155. }
  2156. EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
  2157. /**
  2158. * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
  2159. *
  2160. * The control element is supposed to have the private_value field
  2161. * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
  2162. */
  2163. int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
  2164. struct snd_ctl_elem_value *ucontrol)
  2165. {
  2166. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2167. struct hda_bind_ctls *c;
  2168. unsigned long *vals;
  2169. int err = 0, change = 0;
  2170. mutex_lock(&codec->control_mutex);
  2171. c = (struct hda_bind_ctls *)kcontrol->private_value;
  2172. for (vals = c->values; *vals; vals++) {
  2173. kcontrol->private_value = *vals;
  2174. err = c->ops->put(kcontrol, ucontrol);
  2175. if (err < 0)
  2176. break;
  2177. change |= err;
  2178. }
  2179. kcontrol->private_value = (long)c;
  2180. mutex_unlock(&codec->control_mutex);
  2181. return err < 0 ? err : change;
  2182. }
  2183. EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
  2184. /**
  2185. * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
  2186. *
  2187. * The control element is supposed to have the private_value field
  2188. * set up via HDA_BIND_VOL() macro.
  2189. */
  2190. int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
  2191. unsigned int size, unsigned int __user *tlv)
  2192. {
  2193. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2194. struct hda_bind_ctls *c;
  2195. int err;
  2196. mutex_lock(&codec->control_mutex);
  2197. c = (struct hda_bind_ctls *)kcontrol->private_value;
  2198. kcontrol->private_value = *c->values;
  2199. err = c->ops->tlv(kcontrol, op_flag, size, tlv);
  2200. kcontrol->private_value = (long)c;
  2201. mutex_unlock(&codec->control_mutex);
  2202. return err;
  2203. }
  2204. EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
  2205. struct hda_ctl_ops snd_hda_bind_vol = {
  2206. .info = snd_hda_mixer_amp_volume_info,
  2207. .get = snd_hda_mixer_amp_volume_get,
  2208. .put = snd_hda_mixer_amp_volume_put,
  2209. .tlv = snd_hda_mixer_amp_tlv
  2210. };
  2211. EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
  2212. struct hda_ctl_ops snd_hda_bind_sw = {
  2213. .info = snd_hda_mixer_amp_switch_info,
  2214. .get = snd_hda_mixer_amp_switch_get,
  2215. .put = snd_hda_mixer_amp_switch_put,
  2216. .tlv = snd_hda_mixer_amp_tlv
  2217. };
  2218. EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
  2219. /*
  2220. * SPDIF out controls
  2221. */
  2222. static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
  2223. struct snd_ctl_elem_info *uinfo)
  2224. {
  2225. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  2226. uinfo->count = 1;
  2227. return 0;
  2228. }
  2229. static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
  2230. struct snd_ctl_elem_value *ucontrol)
  2231. {
  2232. ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
  2233. IEC958_AES0_NONAUDIO |
  2234. IEC958_AES0_CON_EMPHASIS_5015 |
  2235. IEC958_AES0_CON_NOT_COPYRIGHT;
  2236. ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
  2237. IEC958_AES1_CON_ORIGINAL;
  2238. return 0;
  2239. }
  2240. static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
  2241. struct snd_ctl_elem_value *ucontrol)
  2242. {
  2243. ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
  2244. IEC958_AES0_NONAUDIO |
  2245. IEC958_AES0_PRO_EMPHASIS_5015;
  2246. return 0;
  2247. }
  2248. static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
  2249. struct snd_ctl_elem_value *ucontrol)
  2250. {
  2251. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2252. ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
  2253. ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
  2254. ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
  2255. ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
  2256. return 0;
  2257. }
  2258. /* convert from SPDIF status bits to HDA SPDIF bits
  2259. * bit 0 (DigEn) is always set zero (to be filled later)
  2260. */
  2261. static unsigned short convert_from_spdif_status(unsigned int sbits)
  2262. {
  2263. unsigned short val = 0;
  2264. if (sbits & IEC958_AES0_PROFESSIONAL)
  2265. val |= AC_DIG1_PROFESSIONAL;
  2266. if (sbits & IEC958_AES0_NONAUDIO)
  2267. val |= AC_DIG1_NONAUDIO;
  2268. if (sbits & IEC958_AES0_PROFESSIONAL) {
  2269. if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
  2270. IEC958_AES0_PRO_EMPHASIS_5015)
  2271. val |= AC_DIG1_EMPHASIS;
  2272. } else {
  2273. if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
  2274. IEC958_AES0_CON_EMPHASIS_5015)
  2275. val |= AC_DIG1_EMPHASIS;
  2276. if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
  2277. val |= AC_DIG1_COPYRIGHT;
  2278. if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
  2279. val |= AC_DIG1_LEVEL;
  2280. val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
  2281. }
  2282. return val;
  2283. }
  2284. /* convert to SPDIF status bits from HDA SPDIF bits
  2285. */
  2286. static unsigned int convert_to_spdif_status(unsigned short val)
  2287. {
  2288. unsigned int sbits = 0;
  2289. if (val & AC_DIG1_NONAUDIO)
  2290. sbits |= IEC958_AES0_NONAUDIO;
  2291. if (val & AC_DIG1_PROFESSIONAL)
  2292. sbits |= IEC958_AES0_PROFESSIONAL;
  2293. if (sbits & IEC958_AES0_PROFESSIONAL) {
  2294. if (sbits & AC_DIG1_EMPHASIS)
  2295. sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
  2296. } else {
  2297. if (val & AC_DIG1_EMPHASIS)
  2298. sbits |= IEC958_AES0_CON_EMPHASIS_5015;
  2299. if (!(val & AC_DIG1_COPYRIGHT))
  2300. sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
  2301. if (val & AC_DIG1_LEVEL)
  2302. sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
  2303. sbits |= val & (0x7f << 8);
  2304. }
  2305. return sbits;
  2306. }
  2307. /* set digital convert verbs both for the given NID and its slaves */
  2308. static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
  2309. int verb, int val)
  2310. {
  2311. hda_nid_t *d;
  2312. snd_hda_codec_write_cache(codec, nid, 0, verb, val);
  2313. d = codec->slave_dig_outs;
  2314. if (!d)
  2315. return;
  2316. for (; *d; d++)
  2317. snd_hda_codec_write_cache(codec, *d, 0, verb, val);
  2318. }
  2319. static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
  2320. int dig1, int dig2)
  2321. {
  2322. if (dig1 != -1)
  2323. set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
  2324. if (dig2 != -1)
  2325. set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
  2326. }
  2327. static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
  2328. struct snd_ctl_elem_value *ucontrol)
  2329. {
  2330. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2331. hda_nid_t nid = kcontrol->private_value;
  2332. unsigned short val;
  2333. int change;
  2334. mutex_lock(&codec->spdif_mutex);
  2335. codec->spdif_status = ucontrol->value.iec958.status[0] |
  2336. ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
  2337. ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
  2338. ((unsigned int)ucontrol->value.iec958.status[3] << 24);
  2339. val = convert_from_spdif_status(codec->spdif_status);
  2340. val |= codec->spdif_ctls & 1;
  2341. change = codec->spdif_ctls != val;
  2342. codec->spdif_ctls = val;
  2343. if (change)
  2344. set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
  2345. mutex_unlock(&codec->spdif_mutex);
  2346. return change;
  2347. }
  2348. #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
  2349. static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
  2350. struct snd_ctl_elem_value *ucontrol)
  2351. {
  2352. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2353. ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
  2354. return 0;
  2355. }
  2356. static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
  2357. struct snd_ctl_elem_value *ucontrol)
  2358. {
  2359. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2360. hda_nid_t nid = kcontrol->private_value;
  2361. unsigned short val;
  2362. int change;
  2363. mutex_lock(&codec->spdif_mutex);
  2364. val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
  2365. if (ucontrol->value.integer.value[0])
  2366. val |= AC_DIG1_ENABLE;
  2367. change = codec->spdif_ctls != val;
  2368. if (change) {
  2369. codec->spdif_ctls = val;
  2370. set_dig_out_convert(codec, nid, val & 0xff, -1);
  2371. /* unmute amp switch (if any) */
  2372. if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
  2373. (val & AC_DIG1_ENABLE))
  2374. snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
  2375. HDA_AMP_MUTE, 0);
  2376. }
  2377. mutex_unlock(&codec->spdif_mutex);
  2378. return change;
  2379. }
  2380. static struct snd_kcontrol_new dig_mixes[] = {
  2381. {
  2382. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  2383. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2384. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
  2385. .info = snd_hda_spdif_mask_info,
  2386. .get = snd_hda_spdif_cmask_get,
  2387. },
  2388. {
  2389. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  2390. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2391. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
  2392. .info = snd_hda_spdif_mask_info,
  2393. .get = snd_hda_spdif_pmask_get,
  2394. },
  2395. {
  2396. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2397. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
  2398. .info = snd_hda_spdif_mask_info,
  2399. .get = snd_hda_spdif_default_get,
  2400. .put = snd_hda_spdif_default_put,
  2401. },
  2402. {
  2403. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2404. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
  2405. .info = snd_hda_spdif_out_switch_info,
  2406. .get = snd_hda_spdif_out_switch_get,
  2407. .put = snd_hda_spdif_out_switch_put,
  2408. },
  2409. { } /* end */
  2410. };
  2411. #define SPDIF_MAX_IDX 4 /* 4 instances should be enough to probe */
  2412. /**
  2413. * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
  2414. * @codec: the HDA codec
  2415. * @nid: audio out widget NID
  2416. *
  2417. * Creates controls related with the SPDIF output.
  2418. * Called from each patch supporting the SPDIF out.
  2419. *
  2420. * Returns 0 if successful, or a negative error code.
  2421. */
  2422. int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
  2423. {
  2424. int err;
  2425. struct snd_kcontrol *kctl;
  2426. struct snd_kcontrol_new *dig_mix;
  2427. int idx;
  2428. for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
  2429. if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch",
  2430. idx))
  2431. break;
  2432. }
  2433. if (idx >= SPDIF_MAX_IDX) {
  2434. printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
  2435. return -EBUSY;
  2436. }
  2437. for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
  2438. kctl = snd_ctl_new1(dig_mix, codec);
  2439. if (!kctl)
  2440. return -ENOMEM;
  2441. kctl->id.index = idx;
  2442. kctl->private_value = nid;
  2443. err = snd_hda_ctl_add(codec, nid, kctl);
  2444. if (err < 0)
  2445. return err;
  2446. }
  2447. codec->spdif_ctls =
  2448. snd_hda_codec_read(codec, nid, 0,
  2449. AC_VERB_GET_DIGI_CONVERT_1, 0);
  2450. codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
  2451. return 0;
  2452. }
  2453. EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
  2454. /*
  2455. * SPDIF sharing with analog output
  2456. */
  2457. static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
  2458. struct snd_ctl_elem_value *ucontrol)
  2459. {
  2460. struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
  2461. ucontrol->value.integer.value[0] = mout->share_spdif;
  2462. return 0;
  2463. }
  2464. static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
  2465. struct snd_ctl_elem_value *ucontrol)
  2466. {
  2467. struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
  2468. mout->share_spdif = !!ucontrol->value.integer.value[0];
  2469. return 0;
  2470. }
  2471. static struct snd_kcontrol_new spdif_share_sw = {
  2472. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2473. .name = "IEC958 Default PCM Playback Switch",
  2474. .info = snd_ctl_boolean_mono_info,
  2475. .get = spdif_share_sw_get,
  2476. .put = spdif_share_sw_put,
  2477. };
  2478. /**
  2479. * snd_hda_create_spdif_share_sw - create Default PCM switch
  2480. * @codec: the HDA codec
  2481. * @mout: multi-out instance
  2482. */
  2483. int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
  2484. struct hda_multi_out *mout)
  2485. {
  2486. if (!mout->dig_out_nid)
  2487. return 0;
  2488. /* ATTENTION: here mout is passed as private_data, instead of codec */
  2489. return snd_hda_ctl_add(codec, mout->dig_out_nid,
  2490. snd_ctl_new1(&spdif_share_sw, mout));
  2491. }
  2492. EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
  2493. /*
  2494. * SPDIF input
  2495. */
  2496. #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
  2497. static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
  2498. struct snd_ctl_elem_value *ucontrol)
  2499. {
  2500. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2501. ucontrol->value.integer.value[0] = codec->spdif_in_enable;
  2502. return 0;
  2503. }
  2504. static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
  2505. struct snd_ctl_elem_value *ucontrol)
  2506. {
  2507. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2508. hda_nid_t nid = kcontrol->private_value;
  2509. unsigned int val = !!ucontrol->value.integer.value[0];
  2510. int change;
  2511. mutex_lock(&codec->spdif_mutex);
  2512. change = codec->spdif_in_enable != val;
  2513. if (change) {
  2514. codec->spdif_in_enable = val;
  2515. snd_hda_codec_write_cache(codec, nid, 0,
  2516. AC_VERB_SET_DIGI_CONVERT_1, val);
  2517. }
  2518. mutex_unlock(&codec->spdif_mutex);
  2519. return change;
  2520. }
  2521. static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
  2522. struct snd_ctl_elem_value *ucontrol)
  2523. {
  2524. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2525. hda_nid_t nid = kcontrol->private_value;
  2526. unsigned short val;
  2527. unsigned int sbits;
  2528. val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
  2529. sbits = convert_to_spdif_status(val);
  2530. ucontrol->value.iec958.status[0] = sbits;
  2531. ucontrol->value.iec958.status[1] = sbits >> 8;
  2532. ucontrol->value.iec958.status[2] = sbits >> 16;
  2533. ucontrol->value.iec958.status[3] = sbits >> 24;
  2534. return 0;
  2535. }
  2536. static struct snd_kcontrol_new dig_in_ctls[] = {
  2537. {
  2538. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2539. .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
  2540. .info = snd_hda_spdif_in_switch_info,
  2541. .get = snd_hda_spdif_in_switch_get,
  2542. .put = snd_hda_spdif_in_switch_put,
  2543. },
  2544. {
  2545. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  2546. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2547. .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
  2548. .info = snd_hda_spdif_mask_info,
  2549. .get = snd_hda_spdif_in_status_get,
  2550. },
  2551. { } /* end */
  2552. };
  2553. /**
  2554. * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
  2555. * @codec: the HDA codec
  2556. * @nid: audio in widget NID
  2557. *
  2558. * Creates controls related with the SPDIF input.
  2559. * Called from each patch supporting the SPDIF in.
  2560. *
  2561. * Returns 0 if successful, or a negative error code.
  2562. */
  2563. int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
  2564. {
  2565. int err;
  2566. struct snd_kcontrol *kctl;
  2567. struct snd_kcontrol_new *dig_mix;
  2568. int idx;
  2569. for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
  2570. if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Capture Switch",
  2571. idx))
  2572. break;
  2573. }
  2574. if (idx >= SPDIF_MAX_IDX) {
  2575. printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
  2576. return -EBUSY;
  2577. }
  2578. for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
  2579. kctl = snd_ctl_new1(dig_mix, codec);
  2580. if (!kctl)
  2581. return -ENOMEM;
  2582. kctl->private_value = nid;
  2583. err = snd_hda_ctl_add(codec, nid, kctl);
  2584. if (err < 0)
  2585. return err;
  2586. }
  2587. codec->spdif_in_enable =
  2588. snd_hda_codec_read(codec, nid, 0,
  2589. AC_VERB_GET_DIGI_CONVERT_1, 0) &
  2590. AC_DIG1_ENABLE;
  2591. return 0;
  2592. }
  2593. EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
  2594. #ifdef SND_HDA_NEEDS_RESUME
  2595. /*
  2596. * command cache
  2597. */
  2598. /* build a 32bit cache key with the widget id and the command parameter */
  2599. #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
  2600. #define get_cmd_cache_nid(key) ((key) & 0xff)
  2601. #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
  2602. /**
  2603. * snd_hda_codec_write_cache - send a single command with caching
  2604. * @codec: the HDA codec
  2605. * @nid: NID to send the command
  2606. * @direct: direct flag
  2607. * @verb: the verb to send
  2608. * @parm: the parameter for the verb
  2609. *
  2610. * Send a single command without waiting for response.
  2611. *
  2612. * Returns 0 if successful, or a negative error code.
  2613. */
  2614. int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
  2615. int direct, unsigned int verb, unsigned int parm)
  2616. {
  2617. int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
  2618. struct hda_cache_head *c;
  2619. u32 key;
  2620. if (err < 0)
  2621. return err;
  2622. /* parm may contain the verb stuff for get/set amp */
  2623. verb = verb | (parm >> 8);
  2624. parm &= 0xff;
  2625. key = build_cmd_cache_key(nid, verb);
  2626. mutex_lock(&codec->bus->cmd_mutex);
  2627. c = get_alloc_hash(&codec->cmd_cache, key);
  2628. if (c)
  2629. c->val = parm;
  2630. mutex_unlock(&codec->bus->cmd_mutex);
  2631. return 0;
  2632. }
  2633. EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
  2634. /**
  2635. * snd_hda_codec_update_cache - check cache and write the cmd only when needed
  2636. * @codec: the HDA codec
  2637. * @nid: NID to send the command
  2638. * @direct: direct flag
  2639. * @verb: the verb to send
  2640. * @parm: the parameter for the verb
  2641. *
  2642. * This function works like snd_hda_codec_write_cache(), but it doesn't send
  2643. * command if the parameter is already identical with the cached value.
  2644. * If not, it sends the command and refreshes the cache.
  2645. *
  2646. * Returns 0 if successful, or a negative error code.
  2647. */
  2648. int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
  2649. int direct, unsigned int verb, unsigned int parm)
  2650. {
  2651. struct hda_cache_head *c;
  2652. u32 key;
  2653. /* parm may contain the verb stuff for get/set amp */
  2654. verb = verb | (parm >> 8);
  2655. parm &= 0xff;
  2656. key = build_cmd_cache_key(nid, verb);
  2657. mutex_lock(&codec->bus->cmd_mutex);
  2658. c = get_hash(&codec->cmd_cache, key);
  2659. if (c && c->val == parm) {
  2660. mutex_unlock(&codec->bus->cmd_mutex);
  2661. return 0;
  2662. }
  2663. mutex_unlock(&codec->bus->cmd_mutex);
  2664. return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
  2665. }
  2666. EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
  2667. /**
  2668. * snd_hda_codec_resume_cache - Resume the all commands from the cache
  2669. * @codec: HD-audio codec
  2670. *
  2671. * Execute all verbs recorded in the command caches to resume.
  2672. */
  2673. void snd_hda_codec_resume_cache(struct hda_codec *codec)
  2674. {
  2675. struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
  2676. int i;
  2677. for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
  2678. u32 key = buffer->key;
  2679. if (!key)
  2680. continue;
  2681. snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
  2682. get_cmd_cache_cmd(key), buffer->val);
  2683. }
  2684. }
  2685. EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
  2686. /**
  2687. * snd_hda_sequence_write_cache - sequence writes with caching
  2688. * @codec: the HDA codec
  2689. * @seq: VERB array to send
  2690. *
  2691. * Send the commands sequentially from the given array.
  2692. * Thte commands are recorded on cache for power-save and resume.
  2693. * The array must be terminated with NID=0.
  2694. */
  2695. void snd_hda_sequence_write_cache(struct hda_codec *codec,
  2696. const struct hda_verb *seq)
  2697. {
  2698. for (; seq->nid; seq++)
  2699. snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
  2700. seq->param);
  2701. }
  2702. EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
  2703. #endif /* SND_HDA_NEEDS_RESUME */
  2704. /*
  2705. * set power state of the codec
  2706. */
  2707. static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
  2708. unsigned int power_state)
  2709. {
  2710. hda_nid_t nid;
  2711. int i;
  2712. /* this delay seems necessary to avoid click noise at power-down */
  2713. if (power_state == AC_PWRST_D3)
  2714. msleep(100);
  2715. snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
  2716. power_state);
  2717. /* partial workaround for "azx_get_response timeout" */
  2718. if (power_state == AC_PWRST_D0 &&
  2719. (codec->vendor_id & 0xffff0000) == 0x14f10000)
  2720. msleep(10);
  2721. nid = codec->start_nid;
  2722. for (i = 0; i < codec->num_nodes; i++, nid++) {
  2723. unsigned int wcaps = get_wcaps(codec, nid);
  2724. if (wcaps & AC_WCAP_POWER) {
  2725. unsigned int wid_type = get_wcaps_type(wcaps);
  2726. if (power_state == AC_PWRST_D3 &&
  2727. wid_type == AC_WID_PIN) {
  2728. unsigned int pincap;
  2729. /*
  2730. * don't power down the widget if it controls
  2731. * eapd and EAPD_BTLENABLE is set.
  2732. */
  2733. pincap = snd_hda_query_pin_caps(codec, nid);
  2734. if (pincap & AC_PINCAP_EAPD) {
  2735. int eapd = snd_hda_codec_read(codec,
  2736. nid, 0,
  2737. AC_VERB_GET_EAPD_BTLENABLE, 0);
  2738. eapd &= 0x02;
  2739. if (eapd)
  2740. continue;
  2741. }
  2742. }
  2743. snd_hda_codec_write(codec, nid, 0,
  2744. AC_VERB_SET_POWER_STATE,
  2745. power_state);
  2746. }
  2747. }
  2748. if (power_state == AC_PWRST_D0) {
  2749. unsigned long end_time;
  2750. int state;
  2751. /* wait until the codec reachs to D0 */
  2752. end_time = jiffies + msecs_to_jiffies(500);
  2753. do {
  2754. state = snd_hda_codec_read(codec, fg, 0,
  2755. AC_VERB_GET_POWER_STATE, 0);
  2756. if (state == power_state)
  2757. break;
  2758. msleep(1);
  2759. } while (time_after_eq(end_time, jiffies));
  2760. }
  2761. }
  2762. #ifdef CONFIG_SND_HDA_HWDEP
  2763. /* execute additional init verbs */
  2764. static void hda_exec_init_verbs(struct hda_codec *codec)
  2765. {
  2766. if (codec->init_verbs.list)
  2767. snd_hda_sequence_write(codec, codec->init_verbs.list);
  2768. }
  2769. #else
  2770. static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
  2771. #endif
  2772. #ifdef SND_HDA_NEEDS_RESUME
  2773. /*
  2774. * call suspend and power-down; used both from PM and power-save
  2775. */
  2776. static void hda_call_codec_suspend(struct hda_codec *codec)
  2777. {
  2778. if (codec->patch_ops.suspend)
  2779. codec->patch_ops.suspend(codec, PMSG_SUSPEND);
  2780. hda_cleanup_all_streams(codec);
  2781. hda_set_power_state(codec,
  2782. codec->afg ? codec->afg : codec->mfg,
  2783. AC_PWRST_D3);
  2784. #ifdef CONFIG_SND_HDA_POWER_SAVE
  2785. snd_hda_update_power_acct(codec);
  2786. cancel_delayed_work(&codec->power_work);
  2787. codec->power_on = 0;
  2788. codec->power_transition = 0;
  2789. codec->power_jiffies = jiffies;
  2790. #endif
  2791. }
  2792. /*
  2793. * kick up codec; used both from PM and power-save
  2794. */
  2795. static void hda_call_codec_resume(struct hda_codec *codec)
  2796. {
  2797. hda_set_power_state(codec,
  2798. codec->afg ? codec->afg : codec->mfg,
  2799. AC_PWRST_D0);
  2800. restore_pincfgs(codec); /* restore all current pin configs */
  2801. restore_shutup_pins(codec);
  2802. hda_exec_init_verbs(codec);
  2803. if (codec->patch_ops.resume)
  2804. codec->patch_ops.resume(codec);
  2805. else {
  2806. if (codec->patch_ops.init)
  2807. codec->patch_ops.init(codec);
  2808. snd_hda_codec_resume_amp(codec);
  2809. snd_hda_codec_resume_cache(codec);
  2810. }
  2811. }
  2812. #endif /* SND_HDA_NEEDS_RESUME */
  2813. /**
  2814. * snd_hda_build_controls - build mixer controls
  2815. * @bus: the BUS
  2816. *
  2817. * Creates mixer controls for each codec included in the bus.
  2818. *
  2819. * Returns 0 if successful, otherwise a negative error code.
  2820. */
  2821. int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
  2822. {
  2823. struct hda_codec *codec;
  2824. list_for_each_entry(codec, &bus->codec_list, list) {
  2825. int err = snd_hda_codec_build_controls(codec);
  2826. if (err < 0) {
  2827. printk(KERN_ERR "hda_codec: cannot build controls "
  2828. "for #%d (error %d)\n", codec->addr, err);
  2829. err = snd_hda_codec_reset(codec);
  2830. if (err < 0) {
  2831. printk(KERN_ERR
  2832. "hda_codec: cannot revert codec\n");
  2833. return err;
  2834. }
  2835. }
  2836. }
  2837. return 0;
  2838. }
  2839. EXPORT_SYMBOL_HDA(snd_hda_build_controls);
  2840. int snd_hda_codec_build_controls(struct hda_codec *codec)
  2841. {
  2842. int err = 0;
  2843. hda_exec_init_verbs(codec);
  2844. /* continue to initialize... */
  2845. if (codec->patch_ops.init)
  2846. err = codec->patch_ops.init(codec);
  2847. if (!err && codec->patch_ops.build_controls)
  2848. err = codec->patch_ops.build_controls(codec);
  2849. if (err < 0)
  2850. return err;
  2851. return 0;
  2852. }
  2853. /*
  2854. * stream formats
  2855. */
  2856. struct hda_rate_tbl {
  2857. unsigned int hz;
  2858. unsigned int alsa_bits;
  2859. unsigned int hda_fmt;
  2860. };
  2861. /* rate = base * mult / div */
  2862. #define HDA_RATE(base, mult, div) \
  2863. (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
  2864. (((div) - 1) << AC_FMT_DIV_SHIFT))
  2865. static struct hda_rate_tbl rate_bits[] = {
  2866. /* rate in Hz, ALSA rate bitmask, HDA format value */
  2867. /* autodetected value used in snd_hda_query_supported_pcm */
  2868. { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
  2869. { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
  2870. { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
  2871. { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
  2872. { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
  2873. { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
  2874. { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
  2875. { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
  2876. { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
  2877. { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
  2878. { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
  2879. #define AC_PAR_PCM_RATE_BITS 11
  2880. /* up to bits 10, 384kHZ isn't supported properly */
  2881. /* not autodetected value */
  2882. { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
  2883. { 0 } /* terminator */
  2884. };
  2885. /**
  2886. * snd_hda_calc_stream_format - calculate format bitset
  2887. * @rate: the sample rate
  2888. * @channels: the number of channels
  2889. * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
  2890. * @maxbps: the max. bps
  2891. *
  2892. * Calculate the format bitset from the given rate, channels and th PCM format.
  2893. *
  2894. * Return zero if invalid.
  2895. */
  2896. unsigned int snd_hda_calc_stream_format(unsigned int rate,
  2897. unsigned int channels,
  2898. unsigned int format,
  2899. unsigned int maxbps,
  2900. unsigned short spdif_ctls)
  2901. {
  2902. int i;
  2903. unsigned int val = 0;
  2904. for (i = 0; rate_bits[i].hz; i++)
  2905. if (rate_bits[i].hz == rate) {
  2906. val = rate_bits[i].hda_fmt;
  2907. break;
  2908. }
  2909. if (!rate_bits[i].hz) {
  2910. snd_printdd("invalid rate %d\n", rate);
  2911. return 0;
  2912. }
  2913. if (channels == 0 || channels > 8) {
  2914. snd_printdd("invalid channels %d\n", channels);
  2915. return 0;
  2916. }
  2917. val |= channels - 1;
  2918. switch (snd_pcm_format_width(format)) {
  2919. case 8:
  2920. val |= AC_FMT_BITS_8;
  2921. break;
  2922. case 16:
  2923. val |= AC_FMT_BITS_16;
  2924. break;
  2925. case 20:
  2926. case 24:
  2927. case 32:
  2928. if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
  2929. val |= AC_FMT_BITS_32;
  2930. else if (maxbps >= 24)
  2931. val |= AC_FMT_BITS_24;
  2932. else
  2933. val |= AC_FMT_BITS_20;
  2934. break;
  2935. default:
  2936. snd_printdd("invalid format width %d\n",
  2937. snd_pcm_format_width(format));
  2938. return 0;
  2939. }
  2940. if (spdif_ctls & AC_DIG1_NONAUDIO)
  2941. val |= AC_FMT_TYPE_NON_PCM;
  2942. return val;
  2943. }
  2944. EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
  2945. static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid)
  2946. {
  2947. unsigned int val = 0;
  2948. if (nid != codec->afg &&
  2949. (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
  2950. val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
  2951. if (!val || val == -1)
  2952. val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
  2953. if (!val || val == -1)
  2954. return 0;
  2955. return val;
  2956. }
  2957. static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
  2958. {
  2959. return query_caps_hash(codec, nid, HDA_HASH_PARPCM_KEY(nid),
  2960. get_pcm_param);
  2961. }
  2962. static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid)
  2963. {
  2964. unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
  2965. if (!streams || streams == -1)
  2966. streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
  2967. if (!streams || streams == -1)
  2968. return 0;
  2969. return streams;
  2970. }
  2971. static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
  2972. {
  2973. return query_caps_hash(codec, nid, HDA_HASH_PARSTR_KEY(nid),
  2974. get_stream_param);
  2975. }
  2976. /**
  2977. * snd_hda_query_supported_pcm - query the supported PCM rates and formats
  2978. * @codec: the HDA codec
  2979. * @nid: NID to query
  2980. * @ratesp: the pointer to store the detected rate bitflags
  2981. * @formatsp: the pointer to store the detected formats
  2982. * @bpsp: the pointer to store the detected format widths
  2983. *
  2984. * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
  2985. * or @bsps argument is ignored.
  2986. *
  2987. * Returns 0 if successful, otherwise a negative error code.
  2988. */
  2989. static int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
  2990. u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
  2991. {
  2992. unsigned int i, val, wcaps;
  2993. wcaps = get_wcaps(codec, nid);
  2994. val = query_pcm_param(codec, nid);
  2995. if (ratesp) {
  2996. u32 rates = 0;
  2997. for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
  2998. if (val & (1 << i))
  2999. rates |= rate_bits[i].alsa_bits;
  3000. }
  3001. if (rates == 0) {
  3002. snd_printk(KERN_ERR "hda_codec: rates == 0 "
  3003. "(nid=0x%x, val=0x%x, ovrd=%i)\n",
  3004. nid, val,
  3005. (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
  3006. return -EIO;
  3007. }
  3008. *ratesp = rates;
  3009. }
  3010. if (formatsp || bpsp) {
  3011. u64 formats = 0;
  3012. unsigned int streams, bps;
  3013. streams = query_stream_param(codec, nid);
  3014. if (!streams)
  3015. return -EIO;
  3016. bps = 0;
  3017. if (streams & AC_SUPFMT_PCM) {
  3018. if (val & AC_SUPPCM_BITS_8) {
  3019. formats |= SNDRV_PCM_FMTBIT_U8;
  3020. bps = 8;
  3021. }
  3022. if (val & AC_SUPPCM_BITS_16) {
  3023. formats |= SNDRV_PCM_FMTBIT_S16_LE;
  3024. bps = 16;
  3025. }
  3026. if (wcaps & AC_WCAP_DIGITAL) {
  3027. if (val & AC_SUPPCM_BITS_32)
  3028. formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
  3029. if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
  3030. formats |= SNDRV_PCM_FMTBIT_S32_LE;
  3031. if (val & AC_SUPPCM_BITS_24)
  3032. bps = 24;
  3033. else if (val & AC_SUPPCM_BITS_20)
  3034. bps = 20;
  3035. } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
  3036. AC_SUPPCM_BITS_32)) {
  3037. formats |= SNDRV_PCM_FMTBIT_S32_LE;
  3038. if (val & AC_SUPPCM_BITS_32)
  3039. bps = 32;
  3040. else if (val & AC_SUPPCM_BITS_24)
  3041. bps = 24;
  3042. else if (val & AC_SUPPCM_BITS_20)
  3043. bps = 20;
  3044. }
  3045. }
  3046. if (streams & AC_SUPFMT_FLOAT32) {
  3047. formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
  3048. if (!bps)
  3049. bps = 32;
  3050. }
  3051. if (streams == AC_SUPFMT_AC3) {
  3052. /* should be exclusive */
  3053. /* temporary hack: we have still no proper support
  3054. * for the direct AC3 stream...
  3055. */
  3056. formats |= SNDRV_PCM_FMTBIT_U8;
  3057. bps = 8;
  3058. }
  3059. if (formats == 0) {
  3060. snd_printk(KERN_ERR "hda_codec: formats == 0 "
  3061. "(nid=0x%x, val=0x%x, ovrd=%i, "
  3062. "streams=0x%x)\n",
  3063. nid, val,
  3064. (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
  3065. streams);
  3066. return -EIO;
  3067. }
  3068. if (formatsp)
  3069. *formatsp = formats;
  3070. if (bpsp)
  3071. *bpsp = bps;
  3072. }
  3073. return 0;
  3074. }
  3075. /**
  3076. * snd_hda_is_supported_format - Check the validity of the format
  3077. * @codec: HD-audio codec
  3078. * @nid: NID to check
  3079. * @format: the HD-audio format value to check
  3080. *
  3081. * Check whether the given node supports the format value.
  3082. *
  3083. * Returns 1 if supported, 0 if not.
  3084. */
  3085. int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
  3086. unsigned int format)
  3087. {
  3088. int i;
  3089. unsigned int val = 0, rate, stream;
  3090. val = query_pcm_param(codec, nid);
  3091. if (!val)
  3092. return 0;
  3093. rate = format & 0xff00;
  3094. for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
  3095. if (rate_bits[i].hda_fmt == rate) {
  3096. if (val & (1 << i))
  3097. break;
  3098. return 0;
  3099. }
  3100. if (i >= AC_PAR_PCM_RATE_BITS)
  3101. return 0;
  3102. stream = query_stream_param(codec, nid);
  3103. if (!stream)
  3104. return 0;
  3105. if (stream & AC_SUPFMT_PCM) {
  3106. switch (format & 0xf0) {
  3107. case 0x00:
  3108. if (!(val & AC_SUPPCM_BITS_8))
  3109. return 0;
  3110. break;
  3111. case 0x10:
  3112. if (!(val & AC_SUPPCM_BITS_16))
  3113. return 0;
  3114. break;
  3115. case 0x20:
  3116. if (!(val & AC_SUPPCM_BITS_20))
  3117. return 0;
  3118. break;
  3119. case 0x30:
  3120. if (!(val & AC_SUPPCM_BITS_24))
  3121. return 0;
  3122. break;
  3123. case 0x40:
  3124. if (!(val & AC_SUPPCM_BITS_32))
  3125. return 0;
  3126. break;
  3127. default:
  3128. return 0;
  3129. }
  3130. } else {
  3131. /* FIXME: check for float32 and AC3? */
  3132. }
  3133. return 1;
  3134. }
  3135. EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
  3136. /*
  3137. * PCM stuff
  3138. */
  3139. static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
  3140. struct hda_codec *codec,
  3141. struct snd_pcm_substream *substream)
  3142. {
  3143. return 0;
  3144. }
  3145. static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
  3146. struct hda_codec *codec,
  3147. unsigned int stream_tag,
  3148. unsigned int format,
  3149. struct snd_pcm_substream *substream)
  3150. {
  3151. snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
  3152. return 0;
  3153. }
  3154. static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
  3155. struct hda_codec *codec,
  3156. struct snd_pcm_substream *substream)
  3157. {
  3158. snd_hda_codec_cleanup_stream(codec, hinfo->nid);
  3159. return 0;
  3160. }
  3161. static int set_pcm_default_values(struct hda_codec *codec,
  3162. struct hda_pcm_stream *info)
  3163. {
  3164. int err;
  3165. /* query support PCM information from the given NID */
  3166. if (info->nid && (!info->rates || !info->formats)) {
  3167. err = snd_hda_query_supported_pcm(codec, info->nid,
  3168. info->rates ? NULL : &info->rates,
  3169. info->formats ? NULL : &info->formats,
  3170. info->maxbps ? NULL : &info->maxbps);
  3171. if (err < 0)
  3172. return err;
  3173. }
  3174. if (info->ops.open == NULL)
  3175. info->ops.open = hda_pcm_default_open_close;
  3176. if (info->ops.close == NULL)
  3177. info->ops.close = hda_pcm_default_open_close;
  3178. if (info->ops.prepare == NULL) {
  3179. if (snd_BUG_ON(!info->nid))
  3180. return -EINVAL;
  3181. info->ops.prepare = hda_pcm_default_prepare;
  3182. }
  3183. if (info->ops.cleanup == NULL) {
  3184. if (snd_BUG_ON(!info->nid))
  3185. return -EINVAL;
  3186. info->ops.cleanup = hda_pcm_default_cleanup;
  3187. }
  3188. return 0;
  3189. }
  3190. /*
  3191. * codec prepare/cleanup entries
  3192. */
  3193. int snd_hda_codec_prepare(struct hda_codec *codec,
  3194. struct hda_pcm_stream *hinfo,
  3195. unsigned int stream,
  3196. unsigned int format,
  3197. struct snd_pcm_substream *substream)
  3198. {
  3199. int ret;
  3200. mutex_lock(&codec->bus->prepare_mutex);
  3201. ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
  3202. if (ret >= 0)
  3203. purify_inactive_streams(codec);
  3204. mutex_unlock(&codec->bus->prepare_mutex);
  3205. return ret;
  3206. }
  3207. EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
  3208. void snd_hda_codec_cleanup(struct hda_codec *codec,
  3209. struct hda_pcm_stream *hinfo,
  3210. struct snd_pcm_substream *substream)
  3211. {
  3212. mutex_lock(&codec->bus->prepare_mutex);
  3213. hinfo->ops.cleanup(hinfo, codec, substream);
  3214. mutex_unlock(&codec->bus->prepare_mutex);
  3215. }
  3216. EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
  3217. /* global */
  3218. const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
  3219. "Audio", "SPDIF", "HDMI", "Modem"
  3220. };
  3221. /*
  3222. * get the empty PCM device number to assign
  3223. *
  3224. * note the max device number is limited by HDA_MAX_PCMS, currently 10
  3225. */
  3226. static int get_empty_pcm_device(struct hda_bus *bus, int type)
  3227. {
  3228. /* audio device indices; not linear to keep compatibility */
  3229. static int audio_idx[HDA_PCM_NTYPES][5] = {
  3230. [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
  3231. [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
  3232. [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
  3233. [HDA_PCM_TYPE_MODEM] = { 6, -1 },
  3234. };
  3235. int i;
  3236. if (type >= HDA_PCM_NTYPES) {
  3237. snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
  3238. return -EINVAL;
  3239. }
  3240. for (i = 0; audio_idx[type][i] >= 0 ; i++)
  3241. if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
  3242. return audio_idx[type][i];
  3243. snd_printk(KERN_WARNING "Too many %s devices\n",
  3244. snd_hda_pcm_type_name[type]);
  3245. return -EAGAIN;
  3246. }
  3247. /*
  3248. * attach a new PCM stream
  3249. */
  3250. static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
  3251. {
  3252. struct hda_bus *bus = codec->bus;
  3253. struct hda_pcm_stream *info;
  3254. int stream, err;
  3255. if (snd_BUG_ON(!pcm->name))
  3256. return -EINVAL;
  3257. for (stream = 0; stream < 2; stream++) {
  3258. info = &pcm->stream[stream];
  3259. if (info->substreams) {
  3260. err = set_pcm_default_values(codec, info);
  3261. if (err < 0)
  3262. return err;
  3263. }
  3264. }
  3265. return bus->ops.attach_pcm(bus, codec, pcm);
  3266. }
  3267. /* assign all PCMs of the given codec */
  3268. int snd_hda_codec_build_pcms(struct hda_codec *codec)
  3269. {
  3270. unsigned int pcm;
  3271. int err;
  3272. if (!codec->num_pcms) {
  3273. if (!codec->patch_ops.build_pcms)
  3274. return 0;
  3275. err = codec->patch_ops.build_pcms(codec);
  3276. if (err < 0) {
  3277. printk(KERN_ERR "hda_codec: cannot build PCMs"
  3278. "for #%d (error %d)\n", codec->addr, err);
  3279. err = snd_hda_codec_reset(codec);
  3280. if (err < 0) {
  3281. printk(KERN_ERR
  3282. "hda_codec: cannot revert codec\n");
  3283. return err;
  3284. }
  3285. }
  3286. }
  3287. for (pcm = 0; pcm < codec->num_pcms; pcm++) {
  3288. struct hda_pcm *cpcm = &codec->pcm_info[pcm];
  3289. int dev;
  3290. if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
  3291. continue; /* no substreams assigned */
  3292. if (!cpcm->pcm) {
  3293. dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
  3294. if (dev < 0)
  3295. continue; /* no fatal error */
  3296. cpcm->device = dev;
  3297. err = snd_hda_attach_pcm(codec, cpcm);
  3298. if (err < 0) {
  3299. printk(KERN_ERR "hda_codec: cannot attach "
  3300. "PCM stream %d for codec #%d\n",
  3301. dev, codec->addr);
  3302. continue; /* no fatal error */
  3303. }
  3304. }
  3305. }
  3306. return 0;
  3307. }
  3308. /**
  3309. * snd_hda_build_pcms - build PCM information
  3310. * @bus: the BUS
  3311. *
  3312. * Create PCM information for each codec included in the bus.
  3313. *
  3314. * The build_pcms codec patch is requested to set up codec->num_pcms and
  3315. * codec->pcm_info properly. The array is referred by the top-level driver
  3316. * to create its PCM instances.
  3317. * The allocated codec->pcm_info should be released in codec->patch_ops.free
  3318. * callback.
  3319. *
  3320. * At least, substreams, channels_min and channels_max must be filled for
  3321. * each stream. substreams = 0 indicates that the stream doesn't exist.
  3322. * When rates and/or formats are zero, the supported values are queried
  3323. * from the given nid. The nid is used also by the default ops.prepare
  3324. * and ops.cleanup callbacks.
  3325. *
  3326. * The driver needs to call ops.open in its open callback. Similarly,
  3327. * ops.close is supposed to be called in the close callback.
  3328. * ops.prepare should be called in the prepare or hw_params callback
  3329. * with the proper parameters for set up.
  3330. * ops.cleanup should be called in hw_free for clean up of streams.
  3331. *
  3332. * This function returns 0 if successfull, or a negative error code.
  3333. */
  3334. int __devinit snd_hda_build_pcms(struct hda_bus *bus)
  3335. {
  3336. struct hda_codec *codec;
  3337. list_for_each_entry(codec, &bus->codec_list, list) {
  3338. int err = snd_hda_codec_build_pcms(codec);
  3339. if (err < 0)
  3340. return err;
  3341. }
  3342. return 0;
  3343. }
  3344. EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
  3345. /**
  3346. * snd_hda_check_board_config - compare the current codec with the config table
  3347. * @codec: the HDA codec
  3348. * @num_configs: number of config enums
  3349. * @models: array of model name strings
  3350. * @tbl: configuration table, terminated by null entries
  3351. *
  3352. * Compares the modelname or PCI subsystem id of the current codec with the
  3353. * given configuration table. If a matching entry is found, returns its
  3354. * config value (supposed to be 0 or positive).
  3355. *
  3356. * If no entries are matching, the function returns a negative value.
  3357. */
  3358. int snd_hda_check_board_config(struct hda_codec *codec,
  3359. int num_configs, const char **models,
  3360. const struct snd_pci_quirk *tbl)
  3361. {
  3362. if (codec->modelname && models) {
  3363. int i;
  3364. for (i = 0; i < num_configs; i++) {
  3365. if (models[i] &&
  3366. !strcmp(codec->modelname, models[i])) {
  3367. snd_printd(KERN_INFO "hda_codec: model '%s' is "
  3368. "selected\n", models[i]);
  3369. return i;
  3370. }
  3371. }
  3372. }
  3373. if (!codec->bus->pci || !tbl)
  3374. return -1;
  3375. tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
  3376. if (!tbl)
  3377. return -1;
  3378. if (tbl->value >= 0 && tbl->value < num_configs) {
  3379. #ifdef CONFIG_SND_DEBUG_VERBOSE
  3380. char tmp[10];
  3381. const char *model = NULL;
  3382. if (models)
  3383. model = models[tbl->value];
  3384. if (!model) {
  3385. sprintf(tmp, "#%d", tbl->value);
  3386. model = tmp;
  3387. }
  3388. snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
  3389. "for config %x:%x (%s)\n",
  3390. model, tbl->subvendor, tbl->subdevice,
  3391. (tbl->name ? tbl->name : "Unknown device"));
  3392. #endif
  3393. return tbl->value;
  3394. }
  3395. return -1;
  3396. }
  3397. EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
  3398. /**
  3399. * snd_hda_check_board_codec_sid_config - compare the current codec
  3400. subsystem ID with the
  3401. config table
  3402. This is important for Gateway notebooks with SB450 HDA Audio
  3403. where the vendor ID of the PCI device is:
  3404. ATI Technologies Inc SB450 HDA Audio [1002:437b]
  3405. and the vendor/subvendor are found only at the codec.
  3406. * @codec: the HDA codec
  3407. * @num_configs: number of config enums
  3408. * @models: array of model name strings
  3409. * @tbl: configuration table, terminated by null entries
  3410. *
  3411. * Compares the modelname or PCI subsystem id of the current codec with the
  3412. * given configuration table. If a matching entry is found, returns its
  3413. * config value (supposed to be 0 or positive).
  3414. *
  3415. * If no entries are matching, the function returns a negative value.
  3416. */
  3417. int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
  3418. int num_configs, const char **models,
  3419. const struct snd_pci_quirk *tbl)
  3420. {
  3421. const struct snd_pci_quirk *q;
  3422. /* Search for codec ID */
  3423. for (q = tbl; q->subvendor; q++) {
  3424. unsigned long vendorid = (q->subdevice) | (q->subvendor << 16);
  3425. if (vendorid == codec->subsystem_id)
  3426. break;
  3427. }
  3428. if (!q->subvendor)
  3429. return -1;
  3430. tbl = q;
  3431. if (tbl->value >= 0 && tbl->value < num_configs) {
  3432. #ifdef CONFIG_SND_DEBUG_VERBOSE
  3433. char tmp[10];
  3434. const char *model = NULL;
  3435. if (models)
  3436. model = models[tbl->value];
  3437. if (!model) {
  3438. sprintf(tmp, "#%d", tbl->value);
  3439. model = tmp;
  3440. }
  3441. snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
  3442. "for config %x:%x (%s)\n",
  3443. model, tbl->subvendor, tbl->subdevice,
  3444. (tbl->name ? tbl->name : "Unknown device"));
  3445. #endif
  3446. return tbl->value;
  3447. }
  3448. return -1;
  3449. }
  3450. EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
  3451. /**
  3452. * snd_hda_add_new_ctls - create controls from the array
  3453. * @codec: the HDA codec
  3454. * @knew: the array of struct snd_kcontrol_new
  3455. *
  3456. * This helper function creates and add new controls in the given array.
  3457. * The array must be terminated with an empty entry as terminator.
  3458. *
  3459. * Returns 0 if successful, or a negative error code.
  3460. */
  3461. int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
  3462. {
  3463. int err;
  3464. for (; knew->name; knew++) {
  3465. struct snd_kcontrol *kctl;
  3466. if (knew->iface == -1) /* skip this codec private value */
  3467. continue;
  3468. kctl = snd_ctl_new1(knew, codec);
  3469. if (!kctl)
  3470. return -ENOMEM;
  3471. err = snd_hda_ctl_add(codec, 0, kctl);
  3472. if (err < 0) {
  3473. if (!codec->addr)
  3474. return err;
  3475. kctl = snd_ctl_new1(knew, codec);
  3476. if (!kctl)
  3477. return -ENOMEM;
  3478. kctl->id.device = codec->addr;
  3479. err = snd_hda_ctl_add(codec, 0, kctl);
  3480. if (err < 0)
  3481. return err;
  3482. }
  3483. }
  3484. return 0;
  3485. }
  3486. EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
  3487. #ifdef CONFIG_SND_HDA_POWER_SAVE
  3488. static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
  3489. unsigned int power_state);
  3490. static void hda_power_work(struct work_struct *work)
  3491. {
  3492. struct hda_codec *codec =
  3493. container_of(work, struct hda_codec, power_work.work);
  3494. struct hda_bus *bus = codec->bus;
  3495. if (!codec->power_on || codec->power_count) {
  3496. codec->power_transition = 0;
  3497. return;
  3498. }
  3499. hda_call_codec_suspend(codec);
  3500. if (bus->ops.pm_notify)
  3501. bus->ops.pm_notify(bus);
  3502. }
  3503. static void hda_keep_power_on(struct hda_codec *codec)
  3504. {
  3505. codec->power_count++;
  3506. codec->power_on = 1;
  3507. codec->power_jiffies = jiffies;
  3508. }
  3509. /* update the power on/off account with the current jiffies */
  3510. void snd_hda_update_power_acct(struct hda_codec *codec)
  3511. {
  3512. unsigned long delta = jiffies - codec->power_jiffies;
  3513. if (codec->power_on)
  3514. codec->power_on_acct += delta;
  3515. else
  3516. codec->power_off_acct += delta;
  3517. codec->power_jiffies += delta;
  3518. }
  3519. /**
  3520. * snd_hda_power_up - Power-up the codec
  3521. * @codec: HD-audio codec
  3522. *
  3523. * Increment the power-up counter and power up the hardware really when
  3524. * not turned on yet.
  3525. */
  3526. void snd_hda_power_up(struct hda_codec *codec)
  3527. {
  3528. struct hda_bus *bus = codec->bus;
  3529. codec->power_count++;
  3530. if (codec->power_on || codec->power_transition)
  3531. return;
  3532. snd_hda_update_power_acct(codec);
  3533. codec->power_on = 1;
  3534. codec->power_jiffies = jiffies;
  3535. if (bus->ops.pm_notify)
  3536. bus->ops.pm_notify(bus);
  3537. hda_call_codec_resume(codec);
  3538. cancel_delayed_work(&codec->power_work);
  3539. codec->power_transition = 0;
  3540. }
  3541. EXPORT_SYMBOL_HDA(snd_hda_power_up);
  3542. #define power_save(codec) \
  3543. ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
  3544. /**
  3545. * snd_hda_power_down - Power-down the codec
  3546. * @codec: HD-audio codec
  3547. *
  3548. * Decrement the power-up counter and schedules the power-off work if
  3549. * the counter rearches to zero.
  3550. */
  3551. void snd_hda_power_down(struct hda_codec *codec)
  3552. {
  3553. --codec->power_count;
  3554. if (!codec->power_on || codec->power_count || codec->power_transition)
  3555. return;
  3556. if (power_save(codec)) {
  3557. codec->power_transition = 1; /* avoid reentrance */
  3558. queue_delayed_work(codec->bus->workq, &codec->power_work,
  3559. msecs_to_jiffies(power_save(codec) * 1000));
  3560. }
  3561. }
  3562. EXPORT_SYMBOL_HDA(snd_hda_power_down);
  3563. /**
  3564. * snd_hda_check_amp_list_power - Check the amp list and update the power
  3565. * @codec: HD-audio codec
  3566. * @check: the object containing an AMP list and the status
  3567. * @nid: NID to check / update
  3568. *
  3569. * Check whether the given NID is in the amp list. If it's in the list,
  3570. * check the current AMP status, and update the the power-status according
  3571. * to the mute status.
  3572. *
  3573. * This function is supposed to be set or called from the check_power_status
  3574. * patch ops.
  3575. */
  3576. int snd_hda_check_amp_list_power(struct hda_codec *codec,
  3577. struct hda_loopback_check *check,
  3578. hda_nid_t nid)
  3579. {
  3580. struct hda_amp_list *p;
  3581. int ch, v;
  3582. if (!check->amplist)
  3583. return 0;
  3584. for (p = check->amplist; p->nid; p++) {
  3585. if (p->nid == nid)
  3586. break;
  3587. }
  3588. if (!p->nid)
  3589. return 0; /* nothing changed */
  3590. for (p = check->amplist; p->nid; p++) {
  3591. for (ch = 0; ch < 2; ch++) {
  3592. v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
  3593. p->idx);
  3594. if (!(v & HDA_AMP_MUTE) && v > 0) {
  3595. if (!check->power_on) {
  3596. check->power_on = 1;
  3597. snd_hda_power_up(codec);
  3598. }
  3599. return 1;
  3600. }
  3601. }
  3602. }
  3603. if (check->power_on) {
  3604. check->power_on = 0;
  3605. snd_hda_power_down(codec);
  3606. }
  3607. return 0;
  3608. }
  3609. EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
  3610. #endif
  3611. /*
  3612. * Channel mode helper
  3613. */
  3614. /**
  3615. * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
  3616. */
  3617. int snd_hda_ch_mode_info(struct hda_codec *codec,
  3618. struct snd_ctl_elem_info *uinfo,
  3619. const struct hda_channel_mode *chmode,
  3620. int num_chmodes)
  3621. {
  3622. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  3623. uinfo->count = 1;
  3624. uinfo->value.enumerated.items = num_chmodes;
  3625. if (uinfo->value.enumerated.item >= num_chmodes)
  3626. uinfo->value.enumerated.item = num_chmodes - 1;
  3627. sprintf(uinfo->value.enumerated.name, "%dch",
  3628. chmode[uinfo->value.enumerated.item].channels);
  3629. return 0;
  3630. }
  3631. EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
  3632. /**
  3633. * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
  3634. */
  3635. int snd_hda_ch_mode_get(struct hda_codec *codec,
  3636. struct snd_ctl_elem_value *ucontrol,
  3637. const struct hda_channel_mode *chmode,
  3638. int num_chmodes,
  3639. int max_channels)
  3640. {
  3641. int i;
  3642. for (i = 0; i < num_chmodes; i++) {
  3643. if (max_channels == chmode[i].channels) {
  3644. ucontrol->value.enumerated.item[0] = i;
  3645. break;
  3646. }
  3647. }
  3648. return 0;
  3649. }
  3650. EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
  3651. /**
  3652. * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
  3653. */
  3654. int snd_hda_ch_mode_put(struct hda_codec *codec,
  3655. struct snd_ctl_elem_value *ucontrol,
  3656. const struct hda_channel_mode *chmode,
  3657. int num_chmodes,
  3658. int *max_channelsp)
  3659. {
  3660. unsigned int mode;
  3661. mode = ucontrol->value.enumerated.item[0];
  3662. if (mode >= num_chmodes)
  3663. return -EINVAL;
  3664. if (*max_channelsp == chmode[mode].channels)
  3665. return 0;
  3666. /* change the current channel setting */
  3667. *max_channelsp = chmode[mode].channels;
  3668. if (chmode[mode].sequence)
  3669. snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
  3670. return 1;
  3671. }
  3672. EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
  3673. /*
  3674. * input MUX helper
  3675. */
  3676. /**
  3677. * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
  3678. */
  3679. int snd_hda_input_mux_info(const struct hda_input_mux *imux,
  3680. struct snd_ctl_elem_info *uinfo)
  3681. {
  3682. unsigned int index;
  3683. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  3684. uinfo->count = 1;
  3685. uinfo->value.enumerated.items = imux->num_items;
  3686. if (!imux->num_items)
  3687. return 0;
  3688. index = uinfo->value.enumerated.item;
  3689. if (index >= imux->num_items)
  3690. index = imux->num_items - 1;
  3691. strcpy(uinfo->value.enumerated.name, imux->items[index].label);
  3692. return 0;
  3693. }
  3694. EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
  3695. /**
  3696. * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
  3697. */
  3698. int snd_hda_input_mux_put(struct hda_codec *codec,
  3699. const struct hda_input_mux *imux,
  3700. struct snd_ctl_elem_value *ucontrol,
  3701. hda_nid_t nid,
  3702. unsigned int *cur_val)
  3703. {
  3704. unsigned int idx;
  3705. if (!imux->num_items)
  3706. return 0;
  3707. idx = ucontrol->value.enumerated.item[0];
  3708. if (idx >= imux->num_items)
  3709. idx = imux->num_items - 1;
  3710. if (*cur_val == idx)
  3711. return 0;
  3712. snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
  3713. imux->items[idx].index);
  3714. *cur_val = idx;
  3715. return 1;
  3716. }
  3717. EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
  3718. /*
  3719. * Multi-channel / digital-out PCM helper functions
  3720. */
  3721. /* setup SPDIF output stream */
  3722. static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
  3723. unsigned int stream_tag, unsigned int format)
  3724. {
  3725. /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
  3726. if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
  3727. set_dig_out_convert(codec, nid,
  3728. codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff,
  3729. -1);
  3730. snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
  3731. if (codec->slave_dig_outs) {
  3732. hda_nid_t *d;
  3733. for (d = codec->slave_dig_outs; *d; d++)
  3734. snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
  3735. format);
  3736. }
  3737. /* turn on again (if needed) */
  3738. if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
  3739. set_dig_out_convert(codec, nid,
  3740. codec->spdif_ctls & 0xff, -1);
  3741. }
  3742. static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
  3743. {
  3744. snd_hda_codec_cleanup_stream(codec, nid);
  3745. if (codec->slave_dig_outs) {
  3746. hda_nid_t *d;
  3747. for (d = codec->slave_dig_outs; *d; d++)
  3748. snd_hda_codec_cleanup_stream(codec, *d);
  3749. }
  3750. }
  3751. /**
  3752. * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
  3753. * @bus: HD-audio bus
  3754. */
  3755. void snd_hda_bus_reboot_notify(struct hda_bus *bus)
  3756. {
  3757. struct hda_codec *codec;
  3758. if (!bus)
  3759. return;
  3760. list_for_each_entry(codec, &bus->codec_list, list) {
  3761. #ifdef CONFIG_SND_HDA_POWER_SAVE
  3762. if (!codec->power_on)
  3763. continue;
  3764. #endif
  3765. if (codec->patch_ops.reboot_notify)
  3766. codec->patch_ops.reboot_notify(codec);
  3767. }
  3768. }
  3769. EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
  3770. /**
  3771. * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
  3772. */
  3773. int snd_hda_multi_out_dig_open(struct hda_codec *codec,
  3774. struct hda_multi_out *mout)
  3775. {
  3776. mutex_lock(&codec->spdif_mutex);
  3777. if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
  3778. /* already opened as analog dup; reset it once */
  3779. cleanup_dig_out_stream(codec, mout->dig_out_nid);
  3780. mout->dig_out_used = HDA_DIG_EXCLUSIVE;
  3781. mutex_unlock(&codec->spdif_mutex);
  3782. return 0;
  3783. }
  3784. EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
  3785. /**
  3786. * snd_hda_multi_out_dig_prepare - prepare the digital out stream
  3787. */
  3788. int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
  3789. struct hda_multi_out *mout,
  3790. unsigned int stream_tag,
  3791. unsigned int format,
  3792. struct snd_pcm_substream *substream)
  3793. {
  3794. mutex_lock(&codec->spdif_mutex);
  3795. setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
  3796. mutex_unlock(&codec->spdif_mutex);
  3797. return 0;
  3798. }
  3799. EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
  3800. /**
  3801. * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
  3802. */
  3803. int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
  3804. struct hda_multi_out *mout)
  3805. {
  3806. mutex_lock(&codec->spdif_mutex);
  3807. cleanup_dig_out_stream(codec, mout->dig_out_nid);
  3808. mutex_unlock(&codec->spdif_mutex);
  3809. return 0;
  3810. }
  3811. EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
  3812. /**
  3813. * snd_hda_multi_out_dig_close - release the digital out stream
  3814. */
  3815. int snd_hda_multi_out_dig_close(struct hda_codec *codec,
  3816. struct hda_multi_out *mout)
  3817. {
  3818. mutex_lock(&codec->spdif_mutex);
  3819. mout->dig_out_used = 0;
  3820. mutex_unlock(&codec->spdif_mutex);
  3821. return 0;
  3822. }
  3823. EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
  3824. /**
  3825. * snd_hda_multi_out_analog_open - open analog outputs
  3826. *
  3827. * Open analog outputs and set up the hw-constraints.
  3828. * If the digital outputs can be opened as slave, open the digital
  3829. * outputs, too.
  3830. */
  3831. int snd_hda_multi_out_analog_open(struct hda_codec *codec,
  3832. struct hda_multi_out *mout,
  3833. struct snd_pcm_substream *substream,
  3834. struct hda_pcm_stream *hinfo)
  3835. {
  3836. struct snd_pcm_runtime *runtime = substream->runtime;
  3837. runtime->hw.channels_max = mout->max_channels;
  3838. if (mout->dig_out_nid) {
  3839. if (!mout->analog_rates) {
  3840. mout->analog_rates = hinfo->rates;
  3841. mout->analog_formats = hinfo->formats;
  3842. mout->analog_maxbps = hinfo->maxbps;
  3843. } else {
  3844. runtime->hw.rates = mout->analog_rates;
  3845. runtime->hw.formats = mout->analog_formats;
  3846. hinfo->maxbps = mout->analog_maxbps;
  3847. }
  3848. if (!mout->spdif_rates) {
  3849. snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
  3850. &mout->spdif_rates,
  3851. &mout->spdif_formats,
  3852. &mout->spdif_maxbps);
  3853. }
  3854. mutex_lock(&codec->spdif_mutex);
  3855. if (mout->share_spdif) {
  3856. if ((runtime->hw.rates & mout->spdif_rates) &&
  3857. (runtime->hw.formats & mout->spdif_formats)) {
  3858. runtime->hw.rates &= mout->spdif_rates;
  3859. runtime->hw.formats &= mout->spdif_formats;
  3860. if (mout->spdif_maxbps < hinfo->maxbps)
  3861. hinfo->maxbps = mout->spdif_maxbps;
  3862. } else {
  3863. mout->share_spdif = 0;
  3864. /* FIXME: need notify? */
  3865. }
  3866. }
  3867. mutex_unlock(&codec->spdif_mutex);
  3868. }
  3869. return snd_pcm_hw_constraint_step(substream->runtime, 0,
  3870. SNDRV_PCM_HW_PARAM_CHANNELS, 2);
  3871. }
  3872. EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
  3873. /**
  3874. * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
  3875. *
  3876. * Set up the i/o for analog out.
  3877. * When the digital out is available, copy the front out to digital out, too.
  3878. */
  3879. int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
  3880. struct hda_multi_out *mout,
  3881. unsigned int stream_tag,
  3882. unsigned int format,
  3883. struct snd_pcm_substream *substream)
  3884. {
  3885. hda_nid_t *nids = mout->dac_nids;
  3886. int chs = substream->runtime->channels;
  3887. int i;
  3888. mutex_lock(&codec->spdif_mutex);
  3889. if (mout->dig_out_nid && mout->share_spdif &&
  3890. mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
  3891. if (chs == 2 &&
  3892. snd_hda_is_supported_format(codec, mout->dig_out_nid,
  3893. format) &&
  3894. !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
  3895. mout->dig_out_used = HDA_DIG_ANALOG_DUP;
  3896. setup_dig_out_stream(codec, mout->dig_out_nid,
  3897. stream_tag, format);
  3898. } else {
  3899. mout->dig_out_used = 0;
  3900. cleanup_dig_out_stream(codec, mout->dig_out_nid);
  3901. }
  3902. }
  3903. mutex_unlock(&codec->spdif_mutex);
  3904. /* front */
  3905. snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
  3906. 0, format);
  3907. if (!mout->no_share_stream &&
  3908. mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
  3909. /* headphone out will just decode front left/right (stereo) */
  3910. snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
  3911. 0, format);
  3912. /* extra outputs copied from front */
  3913. for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
  3914. if (!mout->no_share_stream && mout->extra_out_nid[i])
  3915. snd_hda_codec_setup_stream(codec,
  3916. mout->extra_out_nid[i],
  3917. stream_tag, 0, format);
  3918. /* surrounds */
  3919. for (i = 1; i < mout->num_dacs; i++) {
  3920. if (chs >= (i + 1) * 2) /* independent out */
  3921. snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
  3922. i * 2, format);
  3923. else if (!mout->no_share_stream) /* copy front */
  3924. snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
  3925. 0, format);
  3926. }
  3927. return 0;
  3928. }
  3929. EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
  3930. /**
  3931. * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
  3932. */
  3933. int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
  3934. struct hda_multi_out *mout)
  3935. {
  3936. hda_nid_t *nids = mout->dac_nids;
  3937. int i;
  3938. for (i = 0; i < mout->num_dacs; i++)
  3939. snd_hda_codec_cleanup_stream(codec, nids[i]);
  3940. if (mout->hp_nid)
  3941. snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
  3942. for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
  3943. if (mout->extra_out_nid[i])
  3944. snd_hda_codec_cleanup_stream(codec,
  3945. mout->extra_out_nid[i]);
  3946. mutex_lock(&codec->spdif_mutex);
  3947. if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
  3948. cleanup_dig_out_stream(codec, mout->dig_out_nid);
  3949. mout->dig_out_used = 0;
  3950. }
  3951. mutex_unlock(&codec->spdif_mutex);
  3952. return 0;
  3953. }
  3954. EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
  3955. /*
  3956. * Helper for automatic pin configuration
  3957. */
  3958. static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
  3959. {
  3960. for (; *list; list++)
  3961. if (*list == nid)
  3962. return 1;
  3963. return 0;
  3964. }
  3965. /*
  3966. * Sort an associated group of pins according to their sequence numbers.
  3967. */
  3968. static void sort_pins_by_sequence(hda_nid_t *pins, short *sequences,
  3969. int num_pins)
  3970. {
  3971. int i, j;
  3972. short seq;
  3973. hda_nid_t nid;
  3974. for (i = 0; i < num_pins; i++) {
  3975. for (j = i + 1; j < num_pins; j++) {
  3976. if (sequences[i] > sequences[j]) {
  3977. seq = sequences[i];
  3978. sequences[i] = sequences[j];
  3979. sequences[j] = seq;
  3980. nid = pins[i];
  3981. pins[i] = pins[j];
  3982. pins[j] = nid;
  3983. }
  3984. }
  3985. }
  3986. }
  3987. /*
  3988. * Parse all pin widgets and store the useful pin nids to cfg
  3989. *
  3990. * The number of line-outs or any primary output is stored in line_outs,
  3991. * and the corresponding output pins are assigned to line_out_pins[],
  3992. * in the order of front, rear, CLFE, side, ...
  3993. *
  3994. * If more extra outputs (speaker and headphone) are found, the pins are
  3995. * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
  3996. * is detected, one of speaker of HP pins is assigned as the primary
  3997. * output, i.e. to line_out_pins[0]. So, line_outs is always positive
  3998. * if any analog output exists.
  3999. *
  4000. * The analog input pins are assigned to input_pins array.
  4001. * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
  4002. * respectively.
  4003. */
  4004. int snd_hda_parse_pin_def_config(struct hda_codec *codec,
  4005. struct auto_pin_cfg *cfg,
  4006. hda_nid_t *ignore_nids)
  4007. {
  4008. hda_nid_t nid, end_nid;
  4009. short seq, assoc_line_out, assoc_speaker;
  4010. short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
  4011. short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
  4012. short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
  4013. memset(cfg, 0, sizeof(*cfg));
  4014. memset(sequences_line_out, 0, sizeof(sequences_line_out));
  4015. memset(sequences_speaker, 0, sizeof(sequences_speaker));
  4016. memset(sequences_hp, 0, sizeof(sequences_hp));
  4017. assoc_line_out = assoc_speaker = 0;
  4018. end_nid = codec->start_nid + codec->num_nodes;
  4019. for (nid = codec->start_nid; nid < end_nid; nid++) {
  4020. unsigned int wid_caps = get_wcaps(codec, nid);
  4021. unsigned int wid_type = get_wcaps_type(wid_caps);
  4022. unsigned int def_conf;
  4023. short assoc, loc;
  4024. /* read all default configuration for pin complex */
  4025. if (wid_type != AC_WID_PIN)
  4026. continue;
  4027. /* ignore the given nids (e.g. pc-beep returns error) */
  4028. if (ignore_nids && is_in_nid_list(nid, ignore_nids))
  4029. continue;
  4030. def_conf = snd_hda_codec_get_pincfg(codec, nid);
  4031. if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
  4032. continue;
  4033. loc = get_defcfg_location(def_conf);
  4034. switch (get_defcfg_device(def_conf)) {
  4035. case AC_JACK_LINE_OUT:
  4036. seq = get_defcfg_sequence(def_conf);
  4037. assoc = get_defcfg_association(def_conf);
  4038. if (!(wid_caps & AC_WCAP_STEREO))
  4039. if (!cfg->mono_out_pin)
  4040. cfg->mono_out_pin = nid;
  4041. if (!assoc)
  4042. continue;
  4043. if (!assoc_line_out)
  4044. assoc_line_out = assoc;
  4045. else if (assoc_line_out != assoc)
  4046. continue;
  4047. if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
  4048. continue;
  4049. cfg->line_out_pins[cfg->line_outs] = nid;
  4050. sequences_line_out[cfg->line_outs] = seq;
  4051. cfg->line_outs++;
  4052. break;
  4053. case AC_JACK_SPEAKER:
  4054. seq = get_defcfg_sequence(def_conf);
  4055. assoc = get_defcfg_association(def_conf);
  4056. if (!assoc)
  4057. continue;
  4058. if (!assoc_speaker)
  4059. assoc_speaker = assoc;
  4060. else if (assoc_speaker != assoc)
  4061. continue;
  4062. if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
  4063. continue;
  4064. cfg->speaker_pins[cfg->speaker_outs] = nid;
  4065. sequences_speaker[cfg->speaker_outs] = seq;
  4066. cfg->speaker_outs++;
  4067. break;
  4068. case AC_JACK_HP_OUT:
  4069. seq = get_defcfg_sequence(def_conf);
  4070. assoc = get_defcfg_association(def_conf);
  4071. if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
  4072. continue;
  4073. cfg->hp_pins[cfg->hp_outs] = nid;
  4074. sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
  4075. cfg->hp_outs++;
  4076. break;
  4077. case AC_JACK_MIC_IN: {
  4078. int preferred, alt;
  4079. if (loc == AC_JACK_LOC_FRONT ||
  4080. (loc & 0x30) == AC_JACK_LOC_INTERNAL) {
  4081. preferred = AUTO_PIN_FRONT_MIC;
  4082. alt = AUTO_PIN_MIC;
  4083. } else {
  4084. preferred = AUTO_PIN_MIC;
  4085. alt = AUTO_PIN_FRONT_MIC;
  4086. }
  4087. if (!cfg->input_pins[preferred])
  4088. cfg->input_pins[preferred] = nid;
  4089. else if (!cfg->input_pins[alt])
  4090. cfg->input_pins[alt] = nid;
  4091. break;
  4092. }
  4093. case AC_JACK_LINE_IN:
  4094. if (loc == AC_JACK_LOC_FRONT)
  4095. cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
  4096. else
  4097. cfg->input_pins[AUTO_PIN_LINE] = nid;
  4098. break;
  4099. case AC_JACK_CD:
  4100. cfg->input_pins[AUTO_PIN_CD] = nid;
  4101. break;
  4102. case AC_JACK_AUX:
  4103. cfg->input_pins[AUTO_PIN_AUX] = nid;
  4104. break;
  4105. case AC_JACK_SPDIF_OUT:
  4106. case AC_JACK_DIG_OTHER_OUT:
  4107. if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
  4108. continue;
  4109. cfg->dig_out_pins[cfg->dig_outs] = nid;
  4110. cfg->dig_out_type[cfg->dig_outs] =
  4111. (loc == AC_JACK_LOC_HDMI) ?
  4112. HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
  4113. cfg->dig_outs++;
  4114. break;
  4115. case AC_JACK_SPDIF_IN:
  4116. case AC_JACK_DIG_OTHER_IN:
  4117. cfg->dig_in_pin = nid;
  4118. if (loc == AC_JACK_LOC_HDMI)
  4119. cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
  4120. else
  4121. cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
  4122. break;
  4123. }
  4124. }
  4125. /* FIX-UP:
  4126. * If no line-out is defined but multiple HPs are found,
  4127. * some of them might be the real line-outs.
  4128. */
  4129. if (!cfg->line_outs && cfg->hp_outs > 1) {
  4130. int i = 0;
  4131. while (i < cfg->hp_outs) {
  4132. /* The real HPs should have the sequence 0x0f */
  4133. if ((sequences_hp[i] & 0x0f) == 0x0f) {
  4134. i++;
  4135. continue;
  4136. }
  4137. /* Move it to the line-out table */
  4138. cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
  4139. sequences_line_out[cfg->line_outs] = sequences_hp[i];
  4140. cfg->line_outs++;
  4141. cfg->hp_outs--;
  4142. memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
  4143. sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
  4144. memmove(sequences_hp + i, sequences_hp + i + 1,
  4145. sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
  4146. }
  4147. }
  4148. /* sort by sequence */
  4149. sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
  4150. cfg->line_outs);
  4151. sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
  4152. cfg->speaker_outs);
  4153. sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
  4154. cfg->hp_outs);
  4155. /* if we have only one mic, make it AUTO_PIN_MIC */
  4156. if (!cfg->input_pins[AUTO_PIN_MIC] &&
  4157. cfg->input_pins[AUTO_PIN_FRONT_MIC]) {
  4158. cfg->input_pins[AUTO_PIN_MIC] =
  4159. cfg->input_pins[AUTO_PIN_FRONT_MIC];
  4160. cfg->input_pins[AUTO_PIN_FRONT_MIC] = 0;
  4161. }
  4162. /* ditto for line-in */
  4163. if (!cfg->input_pins[AUTO_PIN_LINE] &&
  4164. cfg->input_pins[AUTO_PIN_FRONT_LINE]) {
  4165. cfg->input_pins[AUTO_PIN_LINE] =
  4166. cfg->input_pins[AUTO_PIN_FRONT_LINE];
  4167. cfg->input_pins[AUTO_PIN_FRONT_LINE] = 0;
  4168. }
  4169. /*
  4170. * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
  4171. * as a primary output
  4172. */
  4173. if (!cfg->line_outs) {
  4174. if (cfg->speaker_outs) {
  4175. cfg->line_outs = cfg->speaker_outs;
  4176. memcpy(cfg->line_out_pins, cfg->speaker_pins,
  4177. sizeof(cfg->speaker_pins));
  4178. cfg->speaker_outs = 0;
  4179. memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
  4180. cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
  4181. } else if (cfg->hp_outs) {
  4182. cfg->line_outs = cfg->hp_outs;
  4183. memcpy(cfg->line_out_pins, cfg->hp_pins,
  4184. sizeof(cfg->hp_pins));
  4185. cfg->hp_outs = 0;
  4186. memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
  4187. cfg->line_out_type = AUTO_PIN_HP_OUT;
  4188. }
  4189. }
  4190. /* Reorder the surround channels
  4191. * ALSA sequence is front/surr/clfe/side
  4192. * HDA sequence is:
  4193. * 4-ch: front/surr => OK as it is
  4194. * 6-ch: front/clfe/surr
  4195. * 8-ch: front/clfe/rear/side|fc
  4196. */
  4197. switch (cfg->line_outs) {
  4198. case 3:
  4199. case 4:
  4200. nid = cfg->line_out_pins[1];
  4201. cfg->line_out_pins[1] = cfg->line_out_pins[2];
  4202. cfg->line_out_pins[2] = nid;
  4203. break;
  4204. }
  4205. /*
  4206. * debug prints of the parsed results
  4207. */
  4208. snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
  4209. cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
  4210. cfg->line_out_pins[2], cfg->line_out_pins[3],
  4211. cfg->line_out_pins[4]);
  4212. snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
  4213. cfg->speaker_outs, cfg->speaker_pins[0],
  4214. cfg->speaker_pins[1], cfg->speaker_pins[2],
  4215. cfg->speaker_pins[3], cfg->speaker_pins[4]);
  4216. snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
  4217. cfg->hp_outs, cfg->hp_pins[0],
  4218. cfg->hp_pins[1], cfg->hp_pins[2],
  4219. cfg->hp_pins[3], cfg->hp_pins[4]);
  4220. snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
  4221. if (cfg->dig_outs)
  4222. snd_printd(" dig-out=0x%x/0x%x\n",
  4223. cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
  4224. snd_printd(" inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
  4225. " cd=0x%x, aux=0x%x\n",
  4226. cfg->input_pins[AUTO_PIN_MIC],
  4227. cfg->input_pins[AUTO_PIN_FRONT_MIC],
  4228. cfg->input_pins[AUTO_PIN_LINE],
  4229. cfg->input_pins[AUTO_PIN_FRONT_LINE],
  4230. cfg->input_pins[AUTO_PIN_CD],
  4231. cfg->input_pins[AUTO_PIN_AUX]);
  4232. if (cfg->dig_in_pin)
  4233. snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
  4234. return 0;
  4235. }
  4236. EXPORT_SYMBOL_HDA(snd_hda_parse_pin_def_config);
  4237. /* labels for input pins */
  4238. const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
  4239. "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
  4240. };
  4241. EXPORT_SYMBOL_HDA(auto_pin_cfg_labels);
  4242. #ifdef CONFIG_PM
  4243. /*
  4244. * power management
  4245. */
  4246. /**
  4247. * snd_hda_suspend - suspend the codecs
  4248. * @bus: the HDA bus
  4249. *
  4250. * Returns 0 if successful.
  4251. */
  4252. int snd_hda_suspend(struct hda_bus *bus)
  4253. {
  4254. struct hda_codec *codec;
  4255. list_for_each_entry(codec, &bus->codec_list, list) {
  4256. #ifdef CONFIG_SND_HDA_POWER_SAVE
  4257. if (!codec->power_on)
  4258. continue;
  4259. #endif
  4260. hda_call_codec_suspend(codec);
  4261. }
  4262. return 0;
  4263. }
  4264. EXPORT_SYMBOL_HDA(snd_hda_suspend);
  4265. /**
  4266. * snd_hda_resume - resume the codecs
  4267. * @bus: the HDA bus
  4268. *
  4269. * Returns 0 if successful.
  4270. *
  4271. * This fucntion is defined only when POWER_SAVE isn't set.
  4272. * In the power-save mode, the codec is resumed dynamically.
  4273. */
  4274. int snd_hda_resume(struct hda_bus *bus)
  4275. {
  4276. struct hda_codec *codec;
  4277. list_for_each_entry(codec, &bus->codec_list, list) {
  4278. if (snd_hda_codec_needs_resume(codec))
  4279. hda_call_codec_resume(codec);
  4280. }
  4281. return 0;
  4282. }
  4283. EXPORT_SYMBOL_HDA(snd_hda_resume);
  4284. #endif /* CONFIG_PM */
  4285. /*
  4286. * generic arrays
  4287. */
  4288. /**
  4289. * snd_array_new - get a new element from the given array
  4290. * @array: the array object
  4291. *
  4292. * Get a new element from the given array. If it exceeds the
  4293. * pre-allocated array size, re-allocate the array.
  4294. *
  4295. * Returns NULL if allocation failed.
  4296. */
  4297. void *snd_array_new(struct snd_array *array)
  4298. {
  4299. if (array->used >= array->alloced) {
  4300. int num = array->alloced + array->alloc_align;
  4301. void *nlist;
  4302. if (snd_BUG_ON(num >= 4096))
  4303. return NULL;
  4304. nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL);
  4305. if (!nlist)
  4306. return NULL;
  4307. if (array->list) {
  4308. memcpy(nlist, array->list,
  4309. array->elem_size * array->alloced);
  4310. kfree(array->list);
  4311. }
  4312. array->list = nlist;
  4313. array->alloced = num;
  4314. }
  4315. return snd_array_elem(array, array->used++);
  4316. }
  4317. EXPORT_SYMBOL_HDA(snd_array_new);
  4318. /**
  4319. * snd_array_free - free the given array elements
  4320. * @array: the array object
  4321. */
  4322. void snd_array_free(struct snd_array *array)
  4323. {
  4324. kfree(array->list);
  4325. array->used = 0;
  4326. array->alloced = 0;
  4327. array->list = NULL;
  4328. }
  4329. EXPORT_SYMBOL_HDA(snd_array_free);
  4330. /**
  4331. * snd_print_pcm_rates - Print the supported PCM rates to the string buffer
  4332. * @pcm: PCM caps bits
  4333. * @buf: the string buffer to write
  4334. * @buflen: the max buffer length
  4335. *
  4336. * used by hda_proc.c and hda_eld.c
  4337. */
  4338. void snd_print_pcm_rates(int pcm, char *buf, int buflen)
  4339. {
  4340. static unsigned int rates[] = {
  4341. 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
  4342. 96000, 176400, 192000, 384000
  4343. };
  4344. int i, j;
  4345. for (i = 0, j = 0; i < ARRAY_SIZE(rates); i++)
  4346. if (pcm & (1 << i))
  4347. j += snprintf(buf + j, buflen - j, " %d", rates[i]);
  4348. buf[j] = '\0'; /* necessary when j == 0 */
  4349. }
  4350. EXPORT_SYMBOL_HDA(snd_print_pcm_rates);
  4351. /**
  4352. * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
  4353. * @pcm: PCM caps bits
  4354. * @buf: the string buffer to write
  4355. * @buflen: the max buffer length
  4356. *
  4357. * used by hda_proc.c and hda_eld.c
  4358. */
  4359. void snd_print_pcm_bits(int pcm, char *buf, int buflen)
  4360. {
  4361. static unsigned int bits[] = { 8, 16, 20, 24, 32 };
  4362. int i, j;
  4363. for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
  4364. if (pcm & (AC_SUPPCM_BITS_8 << i))
  4365. j += snprintf(buf + j, buflen - j, " %d", bits[i]);
  4366. buf[j] = '\0'; /* necessary when j == 0 */
  4367. }
  4368. EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
  4369. MODULE_DESCRIPTION("HDA codec core");
  4370. MODULE_LICENSE("GPL");