hda_codec.c 154 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853
  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/mm.h>
  22. #include <linux/init.h>
  23. #include <linux/delay.h>
  24. #include <linux/slab.h>
  25. #include <linux/mutex.h>
  26. #include <linux/module.h>
  27. #include <linux/async.h>
  28. #include <sound/core.h>
  29. #include "hda_codec.h"
  30. #include <sound/asoundef.h>
  31. #include <sound/tlv.h>
  32. #include <sound/initval.h>
  33. #include <sound/jack.h>
  34. #include "hda_local.h"
  35. #include "hda_beep.h"
  36. #include "hda_jack.h"
  37. #include <sound/hda_hwdep.h>
  38. #define CREATE_TRACE_POINTS
  39. #include "hda_trace.h"
  40. /*
  41. * vendor / preset table
  42. */
  43. struct hda_vendor_id {
  44. unsigned int id;
  45. const char *name;
  46. };
  47. /* codec vendor labels */
  48. static struct hda_vendor_id hda_vendor_ids[] = {
  49. { 0x1002, "ATI" },
  50. { 0x1013, "Cirrus Logic" },
  51. { 0x1057, "Motorola" },
  52. { 0x1095, "Silicon Image" },
  53. { 0x10de, "Nvidia" },
  54. { 0x10ec, "Realtek" },
  55. { 0x1102, "Creative" },
  56. { 0x1106, "VIA" },
  57. { 0x111d, "IDT" },
  58. { 0x11c1, "LSI" },
  59. { 0x11d4, "Analog Devices" },
  60. { 0x13f6, "C-Media" },
  61. { 0x14f1, "Conexant" },
  62. { 0x17e8, "Chrontel" },
  63. { 0x1854, "LG" },
  64. { 0x1aec, "Wolfson Microelectronics" },
  65. { 0x1af4, "QEMU" },
  66. { 0x434d, "C-Media" },
  67. { 0x8086, "Intel" },
  68. { 0x8384, "SigmaTel" },
  69. {} /* terminator */
  70. };
  71. static DEFINE_MUTEX(preset_mutex);
  72. static LIST_HEAD(hda_preset_tables);
  73. int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
  74. {
  75. mutex_lock(&preset_mutex);
  76. list_add_tail(&preset->list, &hda_preset_tables);
  77. mutex_unlock(&preset_mutex);
  78. return 0;
  79. }
  80. EXPORT_SYMBOL_GPL(snd_hda_add_codec_preset);
  81. int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
  82. {
  83. mutex_lock(&preset_mutex);
  84. list_del(&preset->list);
  85. mutex_unlock(&preset_mutex);
  86. return 0;
  87. }
  88. EXPORT_SYMBOL_GPL(snd_hda_delete_codec_preset);
  89. #ifdef CONFIG_PM
  90. #define codec_in_pm(codec) ((codec)->in_pm)
  91. static void hda_power_work(struct work_struct *work);
  92. static void hda_keep_power_on(struct hda_codec *codec);
  93. #define hda_codec_is_power_on(codec) ((codec)->power_on)
  94. static void hda_call_pm_notify(struct hda_codec *codec, bool power_up)
  95. {
  96. struct hda_bus *bus = codec->bus;
  97. if ((power_up && codec->pm_up_notified) ||
  98. (!power_up && !codec->pm_up_notified))
  99. return;
  100. if (bus->ops.pm_notify)
  101. bus->ops.pm_notify(bus, power_up);
  102. codec->pm_up_notified = power_up;
  103. }
  104. #else
  105. #define codec_in_pm(codec) 0
  106. static inline void hda_keep_power_on(struct hda_codec *codec) {}
  107. #define hda_codec_is_power_on(codec) 1
  108. #define hda_call_pm_notify(codec, state) {}
  109. #endif
  110. /**
  111. * snd_hda_get_jack_location - Give a location string of the jack
  112. * @cfg: pin default config value
  113. *
  114. * Parse the pin default config value and returns the string of the
  115. * jack location, e.g. "Rear", "Front", etc.
  116. */
  117. const char *snd_hda_get_jack_location(u32 cfg)
  118. {
  119. static char *bases[7] = {
  120. "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
  121. };
  122. static unsigned char specials_idx[] = {
  123. 0x07, 0x08,
  124. 0x17, 0x18, 0x19,
  125. 0x37, 0x38
  126. };
  127. static char *specials[] = {
  128. "Rear Panel", "Drive Bar",
  129. "Riser", "HDMI", "ATAPI",
  130. "Mobile-In", "Mobile-Out"
  131. };
  132. int i;
  133. cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
  134. if ((cfg & 0x0f) < 7)
  135. return bases[cfg & 0x0f];
  136. for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
  137. if (cfg == specials_idx[i])
  138. return specials[i];
  139. }
  140. return "UNKNOWN";
  141. }
  142. EXPORT_SYMBOL_GPL(snd_hda_get_jack_location);
  143. /**
  144. * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
  145. * @cfg: pin default config value
  146. *
  147. * Parse the pin default config value and returns the string of the
  148. * jack connectivity, i.e. external or internal connection.
  149. */
  150. const char *snd_hda_get_jack_connectivity(u32 cfg)
  151. {
  152. static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
  153. return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
  154. }
  155. EXPORT_SYMBOL_GPL(snd_hda_get_jack_connectivity);
  156. /**
  157. * snd_hda_get_jack_type - Give a type string of the jack
  158. * @cfg: pin default config value
  159. *
  160. * Parse the pin default config value and returns the string of the
  161. * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
  162. */
  163. const char *snd_hda_get_jack_type(u32 cfg)
  164. {
  165. static char *jack_types[16] = {
  166. "Line Out", "Speaker", "HP Out", "CD",
  167. "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
  168. "Line In", "Aux", "Mic", "Telephony",
  169. "SPDIF In", "Digital In", "Reserved", "Other"
  170. };
  171. return jack_types[(cfg & AC_DEFCFG_DEVICE)
  172. >> AC_DEFCFG_DEVICE_SHIFT];
  173. }
  174. EXPORT_SYMBOL_GPL(snd_hda_get_jack_type);
  175. /*
  176. * Compose a 32bit command word to be sent to the HD-audio controller
  177. */
  178. static inline unsigned int
  179. make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int flags,
  180. unsigned int verb, unsigned int parm)
  181. {
  182. u32 val;
  183. if ((codec->addr & ~0xf) || (nid & ~0x7f) ||
  184. (verb & ~0xfff) || (parm & ~0xffff)) {
  185. codec_err(codec, "hda-codec: out of range cmd %x:%x:%x:%x\n",
  186. codec->addr, nid, verb, parm);
  187. return ~0;
  188. }
  189. val = (u32)codec->addr << 28;
  190. val |= (u32)nid << 20;
  191. val |= verb << 8;
  192. val |= parm;
  193. return val;
  194. }
  195. /*
  196. * Send and receive a verb
  197. */
  198. static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
  199. int flags, unsigned int *res)
  200. {
  201. struct hda_bus *bus = codec->bus;
  202. int err;
  203. if (cmd == ~0)
  204. return -1;
  205. if (res)
  206. *res = -1;
  207. again:
  208. snd_hda_power_up(codec);
  209. mutex_lock(&bus->cmd_mutex);
  210. if (flags & HDA_RW_NO_RESPONSE_FALLBACK)
  211. bus->no_response_fallback = 1;
  212. for (;;) {
  213. trace_hda_send_cmd(codec, cmd);
  214. err = bus->ops.command(bus, cmd);
  215. if (err != -EAGAIN)
  216. break;
  217. /* process pending verbs */
  218. bus->ops.get_response(bus, codec->addr);
  219. }
  220. if (!err && res) {
  221. *res = bus->ops.get_response(bus, codec->addr);
  222. trace_hda_get_response(codec, *res);
  223. }
  224. bus->no_response_fallback = 0;
  225. mutex_unlock(&bus->cmd_mutex);
  226. snd_hda_power_down(codec);
  227. if (!codec_in_pm(codec) && res && *res == -1 && bus->rirb_error) {
  228. if (bus->response_reset) {
  229. codec_dbg(codec,
  230. "resetting BUS due to fatal communication error\n");
  231. trace_hda_bus_reset(bus);
  232. bus->ops.bus_reset(bus);
  233. }
  234. goto again;
  235. }
  236. /* clear reset-flag when the communication gets recovered */
  237. if (!err || codec_in_pm(codec))
  238. bus->response_reset = 0;
  239. return err;
  240. }
  241. /**
  242. * snd_hda_codec_read - send a command and get the response
  243. * @codec: the HDA codec
  244. * @nid: NID to send the command
  245. * @flags: optional bit flags
  246. * @verb: the verb to send
  247. * @parm: the parameter for the verb
  248. *
  249. * Send a single command and read the corresponding response.
  250. *
  251. * Returns the obtained response value, or -1 for an error.
  252. */
  253. unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
  254. int flags,
  255. unsigned int verb, unsigned int parm)
  256. {
  257. unsigned cmd = make_codec_cmd(codec, nid, flags, verb, parm);
  258. unsigned int res;
  259. if (codec_exec_verb(codec, cmd, flags, &res))
  260. return -1;
  261. return res;
  262. }
  263. EXPORT_SYMBOL_GPL(snd_hda_codec_read);
  264. /**
  265. * snd_hda_codec_write - send a single command without waiting for response
  266. * @codec: the HDA codec
  267. * @nid: NID to send the command
  268. * @flags: optional bit flags
  269. * @verb: the verb to send
  270. * @parm: the parameter for the verb
  271. *
  272. * Send a single command without waiting for response.
  273. *
  274. * Returns 0 if successful, or a negative error code.
  275. */
  276. int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int flags,
  277. unsigned int verb, unsigned int parm)
  278. {
  279. unsigned int cmd = make_codec_cmd(codec, nid, flags, verb, parm);
  280. unsigned int res;
  281. return codec_exec_verb(codec, cmd, flags,
  282. codec->bus->sync_write ? &res : NULL);
  283. }
  284. EXPORT_SYMBOL_GPL(snd_hda_codec_write);
  285. /**
  286. * snd_hda_sequence_write - sequence writes
  287. * @codec: the HDA codec
  288. * @seq: VERB array to send
  289. *
  290. * Send the commands sequentially from the given array.
  291. * The array must be terminated with NID=0.
  292. */
  293. void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
  294. {
  295. for (; seq->nid; seq++)
  296. snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
  297. }
  298. EXPORT_SYMBOL_GPL(snd_hda_sequence_write);
  299. /**
  300. * snd_hda_get_sub_nodes - get the range of sub nodes
  301. * @codec: the HDA codec
  302. * @nid: NID to parse
  303. * @start_id: the pointer to store the start NID
  304. *
  305. * Parse the NID and store the start NID of its sub-nodes.
  306. * Returns the number of sub-nodes.
  307. */
  308. int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
  309. hda_nid_t *start_id)
  310. {
  311. unsigned int parm;
  312. parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
  313. if (parm == -1)
  314. return 0;
  315. *start_id = (parm >> 16) & 0x7fff;
  316. return (int)(parm & 0x7fff);
  317. }
  318. EXPORT_SYMBOL_GPL(snd_hda_get_sub_nodes);
  319. /* connection list element */
  320. struct hda_conn_list {
  321. struct list_head list;
  322. int len;
  323. hda_nid_t nid;
  324. hda_nid_t conns[0];
  325. };
  326. /* look up the cached results */
  327. static struct hda_conn_list *
  328. lookup_conn_list(struct hda_codec *codec, hda_nid_t nid)
  329. {
  330. struct hda_conn_list *p;
  331. list_for_each_entry(p, &codec->conn_list, list) {
  332. if (p->nid == nid)
  333. return p;
  334. }
  335. return NULL;
  336. }
  337. static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
  338. const hda_nid_t *list)
  339. {
  340. struct hda_conn_list *p;
  341. p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL);
  342. if (!p)
  343. return -ENOMEM;
  344. p->len = len;
  345. p->nid = nid;
  346. memcpy(p->conns, list, len * sizeof(hda_nid_t));
  347. list_add(&p->list, &codec->conn_list);
  348. return 0;
  349. }
  350. static void remove_conn_list(struct hda_codec *codec)
  351. {
  352. while (!list_empty(&codec->conn_list)) {
  353. struct hda_conn_list *p;
  354. p = list_first_entry(&codec->conn_list, typeof(*p), list);
  355. list_del(&p->list);
  356. kfree(p);
  357. }
  358. }
  359. /* read the connection and add to the cache */
  360. static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
  361. {
  362. hda_nid_t list[32];
  363. hda_nid_t *result = list;
  364. int len;
  365. len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
  366. if (len == -ENOSPC) {
  367. len = snd_hda_get_num_raw_conns(codec, nid);
  368. result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL);
  369. if (!result)
  370. return -ENOMEM;
  371. len = snd_hda_get_raw_connections(codec, nid, result, len);
  372. }
  373. if (len >= 0)
  374. len = snd_hda_override_conn_list(codec, nid, len, result);
  375. if (result != list)
  376. kfree(result);
  377. return len;
  378. }
  379. /**
  380. * snd_hda_get_conn_list - get connection list
  381. * @codec: the HDA codec
  382. * @nid: NID to parse
  383. * @len: number of connection list entries
  384. * @listp: the pointer to store NID list
  385. *
  386. * Parses the connection list of the given widget and stores the pointer
  387. * to the list of NIDs.
  388. *
  389. * Returns the number of connections, or a negative error code.
  390. *
  391. * Note that the returned pointer isn't protected against the list
  392. * modification. If snd_hda_override_conn_list() might be called
  393. * concurrently, protect with a mutex appropriately.
  394. */
  395. int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
  396. const hda_nid_t **listp)
  397. {
  398. bool added = false;
  399. for (;;) {
  400. int err;
  401. const struct hda_conn_list *p;
  402. /* if the connection-list is already cached, read it */
  403. p = lookup_conn_list(codec, nid);
  404. if (p) {
  405. if (listp)
  406. *listp = p->conns;
  407. return p->len;
  408. }
  409. if (snd_BUG_ON(added))
  410. return -EINVAL;
  411. err = read_and_add_raw_conns(codec, nid);
  412. if (err < 0)
  413. return err;
  414. added = true;
  415. }
  416. }
  417. EXPORT_SYMBOL_GPL(snd_hda_get_conn_list);
  418. /**
  419. * snd_hda_get_connections - copy connection list
  420. * @codec: the HDA codec
  421. * @nid: NID to parse
  422. * @conn_list: connection list array; when NULL, checks only the size
  423. * @max_conns: max. number of connections to store
  424. *
  425. * Parses the connection list of the given widget and stores the list
  426. * of NIDs.
  427. *
  428. * Returns the number of connections, or a negative error code.
  429. */
  430. int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
  431. hda_nid_t *conn_list, int max_conns)
  432. {
  433. const hda_nid_t *list;
  434. int len = snd_hda_get_conn_list(codec, nid, &list);
  435. if (len > 0 && conn_list) {
  436. if (len > max_conns) {
  437. codec_err(codec, "Too many connections %d for NID 0x%x\n",
  438. len, nid);
  439. return -EINVAL;
  440. }
  441. memcpy(conn_list, list, len * sizeof(hda_nid_t));
  442. }
  443. return len;
  444. }
  445. EXPORT_SYMBOL_GPL(snd_hda_get_connections);
  446. /* return CONNLIST_LEN parameter of the given widget */
  447. static unsigned int get_num_conns(struct hda_codec *codec, hda_nid_t nid)
  448. {
  449. unsigned int wcaps = get_wcaps(codec, nid);
  450. unsigned int parm;
  451. if (!(wcaps & AC_WCAP_CONN_LIST) &&
  452. get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
  453. return 0;
  454. parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
  455. if (parm == -1)
  456. parm = 0;
  457. return parm;
  458. }
  459. int snd_hda_get_num_raw_conns(struct hda_codec *codec, hda_nid_t nid)
  460. {
  461. return snd_hda_get_raw_connections(codec, nid, NULL, 0);
  462. }
  463. /**
  464. * snd_hda_get_raw_connections - copy connection list without cache
  465. * @codec: the HDA codec
  466. * @nid: NID to parse
  467. * @conn_list: connection list array
  468. * @max_conns: max. number of connections to store
  469. *
  470. * Like snd_hda_get_connections(), copy the connection list but without
  471. * checking through the connection-list cache.
  472. * Currently called only from hda_proc.c, so not exported.
  473. */
  474. int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
  475. hda_nid_t *conn_list, int max_conns)
  476. {
  477. unsigned int parm;
  478. int i, conn_len, conns;
  479. unsigned int shift, num_elems, mask;
  480. hda_nid_t prev_nid;
  481. int null_count = 0;
  482. parm = get_num_conns(codec, nid);
  483. if (!parm)
  484. return 0;
  485. if (parm & AC_CLIST_LONG) {
  486. /* long form */
  487. shift = 16;
  488. num_elems = 2;
  489. } else {
  490. /* short form */
  491. shift = 8;
  492. num_elems = 4;
  493. }
  494. conn_len = parm & AC_CLIST_LENGTH;
  495. mask = (1 << (shift-1)) - 1;
  496. if (!conn_len)
  497. return 0; /* no connection */
  498. if (conn_len == 1) {
  499. /* single connection */
  500. parm = snd_hda_codec_read(codec, nid, 0,
  501. AC_VERB_GET_CONNECT_LIST, 0);
  502. if (parm == -1 && codec->bus->rirb_error)
  503. return -EIO;
  504. if (conn_list)
  505. conn_list[0] = parm & mask;
  506. return 1;
  507. }
  508. /* multi connection */
  509. conns = 0;
  510. prev_nid = 0;
  511. for (i = 0; i < conn_len; i++) {
  512. int range_val;
  513. hda_nid_t val, n;
  514. if (i % num_elems == 0) {
  515. parm = snd_hda_codec_read(codec, nid, 0,
  516. AC_VERB_GET_CONNECT_LIST, i);
  517. if (parm == -1 && codec->bus->rirb_error)
  518. return -EIO;
  519. }
  520. range_val = !!(parm & (1 << (shift-1))); /* ranges */
  521. val = parm & mask;
  522. if (val == 0 && null_count++) { /* no second chance */
  523. codec_dbg(codec,
  524. "invalid CONNECT_LIST verb %x[%i]:%x\n",
  525. nid, i, parm);
  526. return 0;
  527. }
  528. parm >>= shift;
  529. if (range_val) {
  530. /* ranges between the previous and this one */
  531. if (!prev_nid || prev_nid >= val) {
  532. codec_warn(codec,
  533. "invalid dep_range_val %x:%x\n",
  534. prev_nid, val);
  535. continue;
  536. }
  537. for (n = prev_nid + 1; n <= val; n++) {
  538. if (conn_list) {
  539. if (conns >= max_conns)
  540. return -ENOSPC;
  541. conn_list[conns] = n;
  542. }
  543. conns++;
  544. }
  545. } else {
  546. if (conn_list) {
  547. if (conns >= max_conns)
  548. return -ENOSPC;
  549. conn_list[conns] = val;
  550. }
  551. conns++;
  552. }
  553. prev_nid = val;
  554. }
  555. return conns;
  556. }
  557. /**
  558. * snd_hda_override_conn_list - add/modify the connection-list to cache
  559. * @codec: the HDA codec
  560. * @nid: NID to parse
  561. * @len: number of connection list entries
  562. * @list: the list of connection entries
  563. *
  564. * Add or modify the given connection-list to the cache. If the corresponding
  565. * cache already exists, invalidate it and append a new one.
  566. *
  567. * Returns zero or a negative error code.
  568. */
  569. int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
  570. const hda_nid_t *list)
  571. {
  572. struct hda_conn_list *p;
  573. p = lookup_conn_list(codec, nid);
  574. if (p) {
  575. list_del(&p->list);
  576. kfree(p);
  577. }
  578. return add_conn_list(codec, nid, len, list);
  579. }
  580. EXPORT_SYMBOL_GPL(snd_hda_override_conn_list);
  581. /**
  582. * snd_hda_get_conn_index - get the connection index of the given NID
  583. * @codec: the HDA codec
  584. * @mux: NID containing the list
  585. * @nid: NID to select
  586. * @recursive: 1 when searching NID recursively, otherwise 0
  587. *
  588. * Parses the connection list of the widget @mux and checks whether the
  589. * widget @nid is present. If it is, return the connection index.
  590. * Otherwise it returns -1.
  591. */
  592. int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
  593. hda_nid_t nid, int recursive)
  594. {
  595. const hda_nid_t *conn;
  596. int i, nums;
  597. nums = snd_hda_get_conn_list(codec, mux, &conn);
  598. for (i = 0; i < nums; i++)
  599. if (conn[i] == nid)
  600. return i;
  601. if (!recursive)
  602. return -1;
  603. if (recursive > 10) {
  604. codec_dbg(codec, "too deep connection for 0x%x\n", nid);
  605. return -1;
  606. }
  607. recursive++;
  608. for (i = 0; i < nums; i++) {
  609. unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
  610. if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
  611. continue;
  612. if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
  613. return i;
  614. }
  615. return -1;
  616. }
  617. EXPORT_SYMBOL_GPL(snd_hda_get_conn_index);
  618. /* return DEVLIST_LEN parameter of the given widget */
  619. static unsigned int get_num_devices(struct hda_codec *codec, hda_nid_t nid)
  620. {
  621. unsigned int wcaps = get_wcaps(codec, nid);
  622. unsigned int parm;
  623. if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) ||
  624. get_wcaps_type(wcaps) != AC_WID_PIN)
  625. return 0;
  626. parm = snd_hda_param_read(codec, nid, AC_PAR_DEVLIST_LEN);
  627. if (parm == -1 && codec->bus->rirb_error)
  628. parm = 0;
  629. return parm & AC_DEV_LIST_LEN_MASK;
  630. }
  631. /**
  632. * snd_hda_get_devices - copy device list without cache
  633. * @codec: the HDA codec
  634. * @nid: NID of the pin to parse
  635. * @dev_list: device list array
  636. * @max_devices: max. number of devices to store
  637. *
  638. * Copy the device list. This info is dynamic and so not cached.
  639. * Currently called only from hda_proc.c, so not exported.
  640. */
  641. int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid,
  642. u8 *dev_list, int max_devices)
  643. {
  644. unsigned int parm;
  645. int i, dev_len, devices;
  646. parm = get_num_devices(codec, nid);
  647. if (!parm) /* not multi-stream capable */
  648. return 0;
  649. dev_len = parm + 1;
  650. dev_len = dev_len < max_devices ? dev_len : max_devices;
  651. devices = 0;
  652. while (devices < dev_len) {
  653. parm = snd_hda_codec_read(codec, nid, 0,
  654. AC_VERB_GET_DEVICE_LIST, devices);
  655. if (parm == -1 && codec->bus->rirb_error)
  656. break;
  657. for (i = 0; i < 8; i++) {
  658. dev_list[devices] = (u8)parm;
  659. parm >>= 4;
  660. devices++;
  661. if (devices >= dev_len)
  662. break;
  663. }
  664. }
  665. return devices;
  666. }
  667. /**
  668. * snd_hda_queue_unsol_event - add an unsolicited event to queue
  669. * @bus: the BUS
  670. * @res: unsolicited event (lower 32bit of RIRB entry)
  671. * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
  672. *
  673. * Adds the given event to the queue. The events are processed in
  674. * the workqueue asynchronously. Call this function in the interrupt
  675. * hanlder when RIRB receives an unsolicited event.
  676. *
  677. * Returns 0 if successful, or a negative error code.
  678. */
  679. int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
  680. {
  681. struct hda_bus_unsolicited *unsol;
  682. unsigned int wp;
  683. if (!bus || !bus->workq)
  684. return 0;
  685. trace_hda_unsol_event(bus, res, res_ex);
  686. unsol = bus->unsol;
  687. if (!unsol)
  688. return 0;
  689. wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
  690. unsol->wp = wp;
  691. wp <<= 1;
  692. unsol->queue[wp] = res;
  693. unsol->queue[wp + 1] = res_ex;
  694. queue_work(bus->workq, &unsol->work);
  695. return 0;
  696. }
  697. EXPORT_SYMBOL_GPL(snd_hda_queue_unsol_event);
  698. /*
  699. * process queued unsolicited events
  700. */
  701. static void process_unsol_events(struct work_struct *work)
  702. {
  703. struct hda_bus_unsolicited *unsol =
  704. container_of(work, struct hda_bus_unsolicited, work);
  705. struct hda_bus *bus = unsol->bus;
  706. struct hda_codec *codec;
  707. unsigned int rp, caddr, res;
  708. while (unsol->rp != unsol->wp) {
  709. rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
  710. unsol->rp = rp;
  711. rp <<= 1;
  712. res = unsol->queue[rp];
  713. caddr = unsol->queue[rp + 1];
  714. if (!(caddr & (1 << 4))) /* no unsolicited event? */
  715. continue;
  716. codec = bus->caddr_tbl[caddr & 0x0f];
  717. if (codec && codec->patch_ops.unsol_event)
  718. codec->patch_ops.unsol_event(codec, res);
  719. }
  720. }
  721. /*
  722. * initialize unsolicited queue
  723. */
  724. static int init_unsol_queue(struct hda_bus *bus)
  725. {
  726. struct hda_bus_unsolicited *unsol;
  727. if (bus->unsol) /* already initialized */
  728. return 0;
  729. unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
  730. if (!unsol) {
  731. dev_err(bus->card->dev, "can't allocate unsolicited queue\n");
  732. return -ENOMEM;
  733. }
  734. INIT_WORK(&unsol->work, process_unsol_events);
  735. unsol->bus = bus;
  736. bus->unsol = unsol;
  737. return 0;
  738. }
  739. /*
  740. * destructor
  741. */
  742. static void snd_hda_bus_free(struct hda_bus *bus)
  743. {
  744. if (!bus)
  745. return;
  746. WARN_ON(!list_empty(&bus->codec_list));
  747. if (bus->workq)
  748. flush_workqueue(bus->workq);
  749. if (bus->unsol)
  750. kfree(bus->unsol);
  751. if (bus->ops.private_free)
  752. bus->ops.private_free(bus);
  753. if (bus->workq)
  754. destroy_workqueue(bus->workq);
  755. kfree(bus);
  756. }
  757. static int snd_hda_bus_dev_free(struct snd_device *device)
  758. {
  759. snd_hda_bus_free(device->device_data);
  760. return 0;
  761. }
  762. static int snd_hda_bus_dev_disconnect(struct snd_device *device)
  763. {
  764. struct hda_bus *bus = device->device_data;
  765. bus->shutdown = 1;
  766. return 0;
  767. }
  768. /**
  769. * snd_hda_bus_new - create a HDA bus
  770. * @card: the card entry
  771. * @temp: the template for hda_bus information
  772. * @busp: the pointer to store the created bus instance
  773. *
  774. * Returns 0 if successful, or a negative error code.
  775. */
  776. int snd_hda_bus_new(struct snd_card *card,
  777. const struct hda_bus_template *temp,
  778. struct hda_bus **busp)
  779. {
  780. struct hda_bus *bus;
  781. int err;
  782. static struct snd_device_ops dev_ops = {
  783. .dev_disconnect = snd_hda_bus_dev_disconnect,
  784. .dev_free = snd_hda_bus_dev_free,
  785. };
  786. if (snd_BUG_ON(!temp))
  787. return -EINVAL;
  788. if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
  789. return -EINVAL;
  790. if (busp)
  791. *busp = NULL;
  792. bus = kzalloc(sizeof(*bus), GFP_KERNEL);
  793. if (bus == NULL) {
  794. dev_err(card->dev, "can't allocate struct hda_bus\n");
  795. return -ENOMEM;
  796. }
  797. bus->card = card;
  798. bus->private_data = temp->private_data;
  799. bus->pci = temp->pci;
  800. bus->modelname = temp->modelname;
  801. bus->power_save = temp->power_save;
  802. bus->ops = temp->ops;
  803. mutex_init(&bus->cmd_mutex);
  804. mutex_init(&bus->prepare_mutex);
  805. INIT_LIST_HEAD(&bus->codec_list);
  806. snprintf(bus->workq_name, sizeof(bus->workq_name),
  807. "hd-audio%d", card->number);
  808. bus->workq = create_singlethread_workqueue(bus->workq_name);
  809. if (!bus->workq) {
  810. dev_err(card->dev, "cannot create workqueue %s\n",
  811. bus->workq_name);
  812. kfree(bus);
  813. return -ENOMEM;
  814. }
  815. err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
  816. if (err < 0) {
  817. snd_hda_bus_free(bus);
  818. return err;
  819. }
  820. if (busp)
  821. *busp = bus;
  822. return 0;
  823. }
  824. EXPORT_SYMBOL_GPL(snd_hda_bus_new);
  825. #if IS_ENABLED(CONFIG_SND_HDA_GENERIC)
  826. #define is_generic_config(codec) \
  827. (codec->modelname && !strcmp(codec->modelname, "generic"))
  828. #else
  829. #define is_generic_config(codec) 0
  830. #endif
  831. #ifdef MODULE
  832. #define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
  833. #else
  834. #define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
  835. #endif
  836. /*
  837. * find a matching codec preset
  838. */
  839. static const struct hda_codec_preset *
  840. find_codec_preset(struct hda_codec *codec)
  841. {
  842. struct hda_codec_preset_list *tbl;
  843. const struct hda_codec_preset *preset;
  844. unsigned int mod_requested = 0;
  845. again:
  846. mutex_lock(&preset_mutex);
  847. list_for_each_entry(tbl, &hda_preset_tables, list) {
  848. if (!try_module_get(tbl->owner)) {
  849. codec_err(codec, "cannot module_get\n");
  850. continue;
  851. }
  852. for (preset = tbl->preset; preset->id; preset++) {
  853. u32 mask = preset->mask;
  854. if (preset->afg && preset->afg != codec->afg)
  855. continue;
  856. if (preset->mfg && preset->mfg != codec->mfg)
  857. continue;
  858. if (!mask)
  859. mask = ~0;
  860. if (preset->id == (codec->vendor_id & mask) &&
  861. (!preset->rev ||
  862. preset->rev == codec->revision_id)) {
  863. mutex_unlock(&preset_mutex);
  864. codec->owner = tbl->owner;
  865. return preset;
  866. }
  867. }
  868. module_put(tbl->owner);
  869. }
  870. mutex_unlock(&preset_mutex);
  871. if (mod_requested < HDA_MODREQ_MAX_COUNT) {
  872. char name[32];
  873. if (!mod_requested)
  874. snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
  875. codec->vendor_id);
  876. else
  877. snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
  878. (codec->vendor_id >> 16) & 0xffff);
  879. request_module(name);
  880. mod_requested++;
  881. goto again;
  882. }
  883. return NULL;
  884. }
  885. /*
  886. * get_codec_name - store the codec name
  887. */
  888. static int get_codec_name(struct hda_codec *codec)
  889. {
  890. const struct hda_vendor_id *c;
  891. const char *vendor = NULL;
  892. u16 vendor_id = codec->vendor_id >> 16;
  893. char tmp[16];
  894. if (codec->vendor_name)
  895. goto get_chip_name;
  896. for (c = hda_vendor_ids; c->id; c++) {
  897. if (c->id == vendor_id) {
  898. vendor = c->name;
  899. break;
  900. }
  901. }
  902. if (!vendor) {
  903. sprintf(tmp, "Generic %04x", vendor_id);
  904. vendor = tmp;
  905. }
  906. codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
  907. if (!codec->vendor_name)
  908. return -ENOMEM;
  909. get_chip_name:
  910. if (codec->chip_name)
  911. return 0;
  912. if (codec->preset && codec->preset->name)
  913. codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
  914. else {
  915. sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
  916. codec->chip_name = kstrdup(tmp, GFP_KERNEL);
  917. }
  918. if (!codec->chip_name)
  919. return -ENOMEM;
  920. return 0;
  921. }
  922. /*
  923. * look for an AFG and MFG nodes
  924. */
  925. static void setup_fg_nodes(struct hda_codec *codec)
  926. {
  927. int i, total_nodes, function_id;
  928. hda_nid_t nid;
  929. total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
  930. for (i = 0; i < total_nodes; i++, nid++) {
  931. function_id = snd_hda_param_read(codec, nid,
  932. AC_PAR_FUNCTION_TYPE);
  933. switch (function_id & 0xff) {
  934. case AC_GRP_AUDIO_FUNCTION:
  935. codec->afg = nid;
  936. codec->afg_function_id = function_id & 0xff;
  937. codec->afg_unsol = (function_id >> 8) & 1;
  938. break;
  939. case AC_GRP_MODEM_FUNCTION:
  940. codec->mfg = nid;
  941. codec->mfg_function_id = function_id & 0xff;
  942. codec->mfg_unsol = (function_id >> 8) & 1;
  943. break;
  944. default:
  945. break;
  946. }
  947. }
  948. }
  949. /*
  950. * read widget caps for each widget and store in cache
  951. */
  952. static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
  953. {
  954. int i;
  955. hda_nid_t nid;
  956. codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
  957. &codec->start_nid);
  958. codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
  959. if (!codec->wcaps)
  960. return -ENOMEM;
  961. nid = codec->start_nid;
  962. for (i = 0; i < codec->num_nodes; i++, nid++)
  963. codec->wcaps[i] = snd_hda_param_read(codec, nid,
  964. AC_PAR_AUDIO_WIDGET_CAP);
  965. return 0;
  966. }
  967. /* read all pin default configurations and save codec->init_pins */
  968. static int read_pin_defaults(struct hda_codec *codec)
  969. {
  970. int i;
  971. hda_nid_t nid = codec->start_nid;
  972. for (i = 0; i < codec->num_nodes; i++, nid++) {
  973. struct hda_pincfg *pin;
  974. unsigned int wcaps = get_wcaps(codec, nid);
  975. unsigned int wid_type = get_wcaps_type(wcaps);
  976. if (wid_type != AC_WID_PIN)
  977. continue;
  978. pin = snd_array_new(&codec->init_pins);
  979. if (!pin)
  980. return -ENOMEM;
  981. pin->nid = nid;
  982. pin->cfg = snd_hda_codec_read(codec, nid, 0,
  983. AC_VERB_GET_CONFIG_DEFAULT, 0);
  984. pin->ctrl = snd_hda_codec_read(codec, nid, 0,
  985. AC_VERB_GET_PIN_WIDGET_CONTROL,
  986. 0);
  987. }
  988. return 0;
  989. }
  990. /* look up the given pin config list and return the item matching with NID */
  991. static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
  992. struct snd_array *array,
  993. hda_nid_t nid)
  994. {
  995. int i;
  996. for (i = 0; i < array->used; i++) {
  997. struct hda_pincfg *pin = snd_array_elem(array, i);
  998. if (pin->nid == nid)
  999. return pin;
  1000. }
  1001. return NULL;
  1002. }
  1003. /* set the current pin config value for the given NID.
  1004. * the value is cached, and read via snd_hda_codec_get_pincfg()
  1005. */
  1006. int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
  1007. hda_nid_t nid, unsigned int cfg)
  1008. {
  1009. struct hda_pincfg *pin;
  1010. /* the check below may be invalid when pins are added by a fixup
  1011. * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
  1012. * for now
  1013. */
  1014. /*
  1015. if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
  1016. return -EINVAL;
  1017. */
  1018. pin = look_up_pincfg(codec, list, nid);
  1019. if (!pin) {
  1020. pin = snd_array_new(list);
  1021. if (!pin)
  1022. return -ENOMEM;
  1023. pin->nid = nid;
  1024. }
  1025. pin->cfg = cfg;
  1026. return 0;
  1027. }
  1028. /**
  1029. * snd_hda_codec_set_pincfg - Override a pin default configuration
  1030. * @codec: the HDA codec
  1031. * @nid: NID to set the pin config
  1032. * @cfg: the pin default config value
  1033. *
  1034. * Override a pin default configuration value in the cache.
  1035. * This value can be read by snd_hda_codec_get_pincfg() in a higher
  1036. * priority than the real hardware value.
  1037. */
  1038. int snd_hda_codec_set_pincfg(struct hda_codec *codec,
  1039. hda_nid_t nid, unsigned int cfg)
  1040. {
  1041. return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
  1042. }
  1043. EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg);
  1044. /**
  1045. * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
  1046. * @codec: the HDA codec
  1047. * @nid: NID to get the pin config
  1048. *
  1049. * Get the current pin config value of the given pin NID.
  1050. * If the pincfg value is cached or overridden via sysfs or driver,
  1051. * returns the cached value.
  1052. */
  1053. unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
  1054. {
  1055. struct hda_pincfg *pin;
  1056. #ifdef CONFIG_SND_HDA_RECONFIG
  1057. {
  1058. unsigned int cfg = 0;
  1059. mutex_lock(&codec->user_mutex);
  1060. pin = look_up_pincfg(codec, &codec->user_pins, nid);
  1061. if (pin)
  1062. cfg = pin->cfg;
  1063. mutex_unlock(&codec->user_mutex);
  1064. if (cfg)
  1065. return cfg;
  1066. }
  1067. #endif
  1068. pin = look_up_pincfg(codec, &codec->driver_pins, nid);
  1069. if (pin)
  1070. return pin->cfg;
  1071. pin = look_up_pincfg(codec, &codec->init_pins, nid);
  1072. if (pin)
  1073. return pin->cfg;
  1074. return 0;
  1075. }
  1076. EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg);
  1077. /* remember the current pinctl target value */
  1078. int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
  1079. unsigned int val)
  1080. {
  1081. struct hda_pincfg *pin;
  1082. pin = look_up_pincfg(codec, &codec->init_pins, nid);
  1083. if (!pin)
  1084. return -EINVAL;
  1085. pin->target = val;
  1086. return 0;
  1087. }
  1088. EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target);
  1089. /* return the current pinctl target value */
  1090. int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
  1091. {
  1092. struct hda_pincfg *pin;
  1093. pin = look_up_pincfg(codec, &codec->init_pins, nid);
  1094. if (!pin)
  1095. return 0;
  1096. return pin->target;
  1097. }
  1098. EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target);
  1099. /**
  1100. * snd_hda_shutup_pins - Shut up all pins
  1101. * @codec: the HDA codec
  1102. *
  1103. * Clear all pin controls to shup up before suspend for avoiding click noise.
  1104. * The controls aren't cached so that they can be resumed properly.
  1105. */
  1106. void snd_hda_shutup_pins(struct hda_codec *codec)
  1107. {
  1108. int i;
  1109. /* don't shut up pins when unloading the driver; otherwise it breaks
  1110. * the default pin setup at the next load of the driver
  1111. */
  1112. if (codec->bus->shutdown)
  1113. return;
  1114. for (i = 0; i < codec->init_pins.used; i++) {
  1115. struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
  1116. /* use read here for syncing after issuing each verb */
  1117. snd_hda_codec_read(codec, pin->nid, 0,
  1118. AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
  1119. }
  1120. codec->pins_shutup = 1;
  1121. }
  1122. EXPORT_SYMBOL_GPL(snd_hda_shutup_pins);
  1123. #ifdef CONFIG_PM
  1124. /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
  1125. static void restore_shutup_pins(struct hda_codec *codec)
  1126. {
  1127. int i;
  1128. if (!codec->pins_shutup)
  1129. return;
  1130. if (codec->bus->shutdown)
  1131. return;
  1132. for (i = 0; i < codec->init_pins.used; i++) {
  1133. struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
  1134. snd_hda_codec_write(codec, pin->nid, 0,
  1135. AC_VERB_SET_PIN_WIDGET_CONTROL,
  1136. pin->ctrl);
  1137. }
  1138. codec->pins_shutup = 0;
  1139. }
  1140. #endif
  1141. static void hda_jackpoll_work(struct work_struct *work)
  1142. {
  1143. struct hda_codec *codec =
  1144. container_of(work, struct hda_codec, jackpoll_work.work);
  1145. snd_hda_jack_set_dirty_all(codec);
  1146. snd_hda_jack_poll_all(codec);
  1147. if (!codec->jackpoll_interval)
  1148. return;
  1149. queue_delayed_work(codec->bus->workq, &codec->jackpoll_work,
  1150. codec->jackpoll_interval);
  1151. }
  1152. static void init_hda_cache(struct hda_cache_rec *cache,
  1153. unsigned int record_size);
  1154. static void free_hda_cache(struct hda_cache_rec *cache);
  1155. /* release all pincfg lists */
  1156. static void free_init_pincfgs(struct hda_codec *codec)
  1157. {
  1158. snd_array_free(&codec->driver_pins);
  1159. #ifdef CONFIG_SND_HDA_RECONFIG
  1160. snd_array_free(&codec->user_pins);
  1161. #endif
  1162. snd_array_free(&codec->init_pins);
  1163. }
  1164. /*
  1165. * audio-converter setup caches
  1166. */
  1167. struct hda_cvt_setup {
  1168. hda_nid_t nid;
  1169. u8 stream_tag;
  1170. u8 channel_id;
  1171. u16 format_id;
  1172. unsigned char active; /* cvt is currently used */
  1173. unsigned char dirty; /* setups should be cleared */
  1174. };
  1175. /* get or create a cache entry for the given audio converter NID */
  1176. static struct hda_cvt_setup *
  1177. get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
  1178. {
  1179. struct hda_cvt_setup *p;
  1180. int i;
  1181. for (i = 0; i < codec->cvt_setups.used; i++) {
  1182. p = snd_array_elem(&codec->cvt_setups, i);
  1183. if (p->nid == nid)
  1184. return p;
  1185. }
  1186. p = snd_array_new(&codec->cvt_setups);
  1187. if (p)
  1188. p->nid = nid;
  1189. return p;
  1190. }
  1191. /*
  1192. * Dynamic symbol binding for the codec parsers
  1193. */
  1194. #define load_parser(codec, sym) \
  1195. ((codec)->parser = (int (*)(struct hda_codec *))symbol_request(sym))
  1196. static void unload_parser(struct hda_codec *codec)
  1197. {
  1198. if (codec->parser)
  1199. symbol_put_addr(codec->parser);
  1200. codec->parser = NULL;
  1201. }
  1202. /*
  1203. * codec destructor
  1204. */
  1205. static void snd_hda_codec_free(struct hda_codec *codec)
  1206. {
  1207. if (!codec)
  1208. return;
  1209. cancel_delayed_work_sync(&codec->jackpoll_work);
  1210. snd_hda_jack_tbl_clear(codec);
  1211. free_init_pincfgs(codec);
  1212. #ifdef CONFIG_PM
  1213. cancel_delayed_work(&codec->power_work);
  1214. flush_workqueue(codec->bus->workq);
  1215. #endif
  1216. list_del(&codec->list);
  1217. snd_array_free(&codec->mixers);
  1218. snd_array_free(&codec->nids);
  1219. snd_array_free(&codec->cvt_setups);
  1220. snd_array_free(&codec->spdif_out);
  1221. remove_conn_list(codec);
  1222. codec->bus->caddr_tbl[codec->addr] = NULL;
  1223. if (codec->patch_ops.free)
  1224. codec->patch_ops.free(codec);
  1225. hda_call_pm_notify(codec, false); /* cancel leftover refcounts */
  1226. snd_hda_sysfs_clear(codec);
  1227. unload_parser(codec);
  1228. module_put(codec->owner);
  1229. free_hda_cache(&codec->amp_cache);
  1230. free_hda_cache(&codec->cmd_cache);
  1231. kfree(codec->vendor_name);
  1232. kfree(codec->chip_name);
  1233. kfree(codec->modelname);
  1234. kfree(codec->wcaps);
  1235. codec->bus->num_codecs--;
  1236. put_device(&codec->dev);
  1237. }
  1238. static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
  1239. hda_nid_t fg, unsigned int power_state);
  1240. static unsigned int hda_set_power_state(struct hda_codec *codec,
  1241. unsigned int power_state);
  1242. static int snd_hda_codec_dev_register(struct snd_device *device)
  1243. {
  1244. struct hda_codec *codec = device->device_data;
  1245. int err = device_add(&codec->dev);
  1246. if (err < 0)
  1247. return err;
  1248. snd_hda_register_beep_device(codec);
  1249. return 0;
  1250. }
  1251. static int snd_hda_codec_dev_disconnect(struct snd_device *device)
  1252. {
  1253. struct hda_codec *codec = device->device_data;
  1254. snd_hda_detach_beep_device(codec);
  1255. device_del(&codec->dev);
  1256. return 0;
  1257. }
  1258. static int snd_hda_codec_dev_free(struct snd_device *device)
  1259. {
  1260. snd_hda_codec_free(device->device_data);
  1261. return 0;
  1262. }
  1263. /* just free the container */
  1264. static void snd_hda_codec_dev_release(struct device *dev)
  1265. {
  1266. kfree(container_of(dev, struct hda_codec, dev));
  1267. }
  1268. /**
  1269. * snd_hda_codec_new - create a HDA codec
  1270. * @bus: the bus to assign
  1271. * @codec_addr: the codec address
  1272. * @codecp: the pointer to store the generated codec
  1273. *
  1274. * Returns 0 if successful, or a negative error code.
  1275. */
  1276. int snd_hda_codec_new(struct hda_bus *bus,
  1277. unsigned int codec_addr,
  1278. struct hda_codec **codecp)
  1279. {
  1280. struct hda_codec *codec;
  1281. char component[31];
  1282. hda_nid_t fg;
  1283. int err;
  1284. static struct snd_device_ops dev_ops = {
  1285. .dev_register = snd_hda_codec_dev_register,
  1286. .dev_disconnect = snd_hda_codec_dev_disconnect,
  1287. .dev_free = snd_hda_codec_dev_free,
  1288. };
  1289. if (snd_BUG_ON(!bus))
  1290. return -EINVAL;
  1291. if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
  1292. return -EINVAL;
  1293. if (bus->caddr_tbl[codec_addr]) {
  1294. dev_err(bus->card->dev,
  1295. "address 0x%x is already occupied\n",
  1296. codec_addr);
  1297. return -EBUSY;
  1298. }
  1299. codec = kzalloc(sizeof(*codec), GFP_KERNEL);
  1300. if (codec == NULL) {
  1301. dev_err(bus->card->dev, "can't allocate struct hda_codec\n");
  1302. return -ENOMEM;
  1303. }
  1304. device_initialize(&codec->dev);
  1305. codec->dev.parent = &bus->card->card_dev;
  1306. codec->dev.class = sound_class;
  1307. codec->dev.release = snd_hda_codec_dev_release;
  1308. codec->dev.groups = snd_hda_dev_attr_groups;
  1309. dev_set_name(&codec->dev, "hdaudioC%dD%d", bus->card->number,
  1310. codec_addr);
  1311. dev_set_drvdata(&codec->dev, codec); /* for sysfs */
  1312. codec->bus = bus;
  1313. codec->addr = codec_addr;
  1314. mutex_init(&codec->spdif_mutex);
  1315. mutex_init(&codec->control_mutex);
  1316. mutex_init(&codec->hash_mutex);
  1317. init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
  1318. init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
  1319. snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
  1320. snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
  1321. snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
  1322. snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
  1323. snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
  1324. snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
  1325. snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
  1326. snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
  1327. INIT_LIST_HEAD(&codec->conn_list);
  1328. INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
  1329. codec->depop_delay = -1;
  1330. codec->fixup_id = HDA_FIXUP_ID_NOT_SET;
  1331. #ifdef CONFIG_PM
  1332. spin_lock_init(&codec->power_lock);
  1333. INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
  1334. /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
  1335. * the caller has to power down appropriatley after initialization
  1336. * phase.
  1337. */
  1338. hda_keep_power_on(codec);
  1339. #endif
  1340. snd_hda_sysfs_init(codec);
  1341. if (codec->bus->modelname) {
  1342. codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
  1343. if (!codec->modelname) {
  1344. err = -ENODEV;
  1345. goto error;
  1346. }
  1347. }
  1348. list_add_tail(&codec->list, &bus->codec_list);
  1349. bus->num_codecs++;
  1350. bus->caddr_tbl[codec_addr] = codec;
  1351. codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
  1352. AC_PAR_VENDOR_ID);
  1353. if (codec->vendor_id == -1)
  1354. /* read again, hopefully the access method was corrected
  1355. * in the last read...
  1356. */
  1357. codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
  1358. AC_PAR_VENDOR_ID);
  1359. codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
  1360. AC_PAR_SUBSYSTEM_ID);
  1361. codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
  1362. AC_PAR_REV_ID);
  1363. setup_fg_nodes(codec);
  1364. if (!codec->afg && !codec->mfg) {
  1365. dev_err(bus->card->dev, "no AFG or MFG node found\n");
  1366. err = -ENODEV;
  1367. goto error;
  1368. }
  1369. fg = codec->afg ? codec->afg : codec->mfg;
  1370. err = read_widget_caps(codec, fg);
  1371. if (err < 0) {
  1372. dev_err(bus->card->dev, "cannot malloc\n");
  1373. goto error;
  1374. }
  1375. err = read_pin_defaults(codec);
  1376. if (err < 0)
  1377. goto error;
  1378. if (!codec->subsystem_id) {
  1379. codec->subsystem_id =
  1380. snd_hda_codec_read(codec, fg, 0,
  1381. AC_VERB_GET_SUBSYSTEM_ID, 0);
  1382. }
  1383. #ifdef CONFIG_PM
  1384. codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
  1385. AC_PWRST_CLKSTOP);
  1386. #endif
  1387. codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
  1388. AC_PWRST_EPSS);
  1389. #ifdef CONFIG_PM
  1390. if (!codec->d3_stop_clk || !codec->epss)
  1391. bus->power_keep_link_on = 1;
  1392. #endif
  1393. /* power-up all before initialization */
  1394. hda_set_power_state(codec, AC_PWRST_D0);
  1395. snd_hda_codec_proc_new(codec);
  1396. snd_hda_create_hwdep(codec);
  1397. sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
  1398. codec->subsystem_id, codec->revision_id);
  1399. snd_component_add(codec->bus->card, component);
  1400. err = snd_device_new(bus->card, SNDRV_DEV_CODEC, codec, &dev_ops);
  1401. if (err < 0)
  1402. goto error;
  1403. if (codecp)
  1404. *codecp = codec;
  1405. return 0;
  1406. error:
  1407. snd_hda_codec_free(codec);
  1408. return err;
  1409. }
  1410. EXPORT_SYMBOL_GPL(snd_hda_codec_new);
  1411. int snd_hda_codec_update_widgets(struct hda_codec *codec)
  1412. {
  1413. hda_nid_t fg;
  1414. int err;
  1415. /* Assume the function group node does not change,
  1416. * only the widget nodes may change.
  1417. */
  1418. kfree(codec->wcaps);
  1419. fg = codec->afg ? codec->afg : codec->mfg;
  1420. err = read_widget_caps(codec, fg);
  1421. if (err < 0) {
  1422. codec_err(codec, "cannot malloc\n");
  1423. return err;
  1424. }
  1425. snd_array_free(&codec->init_pins);
  1426. err = read_pin_defaults(codec);
  1427. return err;
  1428. }
  1429. EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets);
  1430. #if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI)
  1431. /* if all audio out widgets are digital, let's assume the codec as a HDMI/DP */
  1432. static bool is_likely_hdmi_codec(struct hda_codec *codec)
  1433. {
  1434. hda_nid_t nid = codec->start_nid;
  1435. int i;
  1436. for (i = 0; i < codec->num_nodes; i++, nid++) {
  1437. unsigned int wcaps = get_wcaps(codec, nid);
  1438. switch (get_wcaps_type(wcaps)) {
  1439. case AC_WID_AUD_IN:
  1440. return false; /* HDMI parser supports only HDMI out */
  1441. case AC_WID_AUD_OUT:
  1442. if (!(wcaps & AC_WCAP_DIGITAL))
  1443. return false;
  1444. break;
  1445. }
  1446. }
  1447. return true;
  1448. }
  1449. #else
  1450. /* no HDMI codec parser support */
  1451. #define is_likely_hdmi_codec(codec) false
  1452. #endif /* CONFIG_SND_HDA_CODEC_HDMI */
  1453. /**
  1454. * snd_hda_codec_configure - (Re-)configure the HD-audio codec
  1455. * @codec: the HDA codec
  1456. *
  1457. * Start parsing of the given codec tree and (re-)initialize the whole
  1458. * patch instance.
  1459. *
  1460. * Returns 0 if successful or a negative error code.
  1461. */
  1462. int snd_hda_codec_configure(struct hda_codec *codec)
  1463. {
  1464. int (*patch)(struct hda_codec *) = NULL;
  1465. int err;
  1466. codec->preset = find_codec_preset(codec);
  1467. if (!codec->vendor_name || !codec->chip_name) {
  1468. err = get_codec_name(codec);
  1469. if (err < 0)
  1470. return err;
  1471. }
  1472. if (!is_generic_config(codec) && codec->preset)
  1473. patch = codec->preset->patch;
  1474. if (!patch) {
  1475. unload_parser(codec); /* to be sure */
  1476. if (is_likely_hdmi_codec(codec)) {
  1477. #if IS_MODULE(CONFIG_SND_HDA_CODEC_HDMI)
  1478. patch = load_parser(codec, snd_hda_parse_hdmi_codec);
  1479. #elif IS_BUILTIN(CONFIG_SND_HDA_CODEC_HDMI)
  1480. patch = snd_hda_parse_hdmi_codec;
  1481. #endif
  1482. }
  1483. if (!patch) {
  1484. #if IS_MODULE(CONFIG_SND_HDA_GENERIC)
  1485. patch = load_parser(codec, snd_hda_parse_generic_codec);
  1486. #elif IS_BUILTIN(CONFIG_SND_HDA_GENERIC)
  1487. patch = snd_hda_parse_generic_codec;
  1488. #endif
  1489. }
  1490. if (!patch) {
  1491. codec_err(codec, "No codec parser is available\n");
  1492. return -ENODEV;
  1493. }
  1494. }
  1495. err = patch(codec);
  1496. if (err < 0) {
  1497. unload_parser(codec);
  1498. return err;
  1499. }
  1500. if (codec->patch_ops.unsol_event) {
  1501. err = init_unsol_queue(codec->bus);
  1502. if (err < 0)
  1503. return err;
  1504. }
  1505. /* audio codec should override the mixer name */
  1506. if (codec->afg || !*codec->bus->card->mixername)
  1507. snprintf(codec->bus->card->mixername,
  1508. sizeof(codec->bus->card->mixername),
  1509. "%s %s", codec->vendor_name, codec->chip_name);
  1510. return 0;
  1511. }
  1512. EXPORT_SYMBOL_GPL(snd_hda_codec_configure);
  1513. /* update the stream-id if changed */
  1514. static void update_pcm_stream_id(struct hda_codec *codec,
  1515. struct hda_cvt_setup *p, hda_nid_t nid,
  1516. u32 stream_tag, int channel_id)
  1517. {
  1518. unsigned int oldval, newval;
  1519. if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
  1520. oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
  1521. newval = (stream_tag << 4) | channel_id;
  1522. if (oldval != newval)
  1523. snd_hda_codec_write(codec, nid, 0,
  1524. AC_VERB_SET_CHANNEL_STREAMID,
  1525. newval);
  1526. p->stream_tag = stream_tag;
  1527. p->channel_id = channel_id;
  1528. }
  1529. }
  1530. /* update the format-id if changed */
  1531. static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
  1532. hda_nid_t nid, int format)
  1533. {
  1534. unsigned int oldval;
  1535. if (p->format_id != format) {
  1536. oldval = snd_hda_codec_read(codec, nid, 0,
  1537. AC_VERB_GET_STREAM_FORMAT, 0);
  1538. if (oldval != format) {
  1539. msleep(1);
  1540. snd_hda_codec_write(codec, nid, 0,
  1541. AC_VERB_SET_STREAM_FORMAT,
  1542. format);
  1543. }
  1544. p->format_id = format;
  1545. }
  1546. }
  1547. /**
  1548. * snd_hda_codec_setup_stream - set up the codec for streaming
  1549. * @codec: the CODEC to set up
  1550. * @nid: the NID to set up
  1551. * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
  1552. * @channel_id: channel id to pass, zero based.
  1553. * @format: stream format.
  1554. */
  1555. void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
  1556. u32 stream_tag,
  1557. int channel_id, int format)
  1558. {
  1559. struct hda_codec *c;
  1560. struct hda_cvt_setup *p;
  1561. int type;
  1562. int i;
  1563. if (!nid)
  1564. return;
  1565. codec_dbg(codec,
  1566. "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
  1567. nid, stream_tag, channel_id, format);
  1568. p = get_hda_cvt_setup(codec, nid);
  1569. if (!p)
  1570. return;
  1571. if (codec->pcm_format_first)
  1572. update_pcm_format(codec, p, nid, format);
  1573. update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
  1574. if (!codec->pcm_format_first)
  1575. update_pcm_format(codec, p, nid, format);
  1576. p->active = 1;
  1577. p->dirty = 0;
  1578. /* make other inactive cvts with the same stream-tag dirty */
  1579. type = get_wcaps_type(get_wcaps(codec, nid));
  1580. list_for_each_entry(c, &codec->bus->codec_list, list) {
  1581. for (i = 0; i < c->cvt_setups.used; i++) {
  1582. p = snd_array_elem(&c->cvt_setups, i);
  1583. if (!p->active && p->stream_tag == stream_tag &&
  1584. get_wcaps_type(get_wcaps(c, p->nid)) == type)
  1585. p->dirty = 1;
  1586. }
  1587. }
  1588. }
  1589. EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream);
  1590. static void really_cleanup_stream(struct hda_codec *codec,
  1591. struct hda_cvt_setup *q);
  1592. /**
  1593. * __snd_hda_codec_cleanup_stream - clean up the codec for closing
  1594. * @codec: the CODEC to clean up
  1595. * @nid: the NID to clean up
  1596. * @do_now: really clean up the stream instead of clearing the active flag
  1597. */
  1598. void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
  1599. int do_now)
  1600. {
  1601. struct hda_cvt_setup *p;
  1602. if (!nid)
  1603. return;
  1604. if (codec->no_sticky_stream)
  1605. do_now = 1;
  1606. codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid);
  1607. p = get_hda_cvt_setup(codec, nid);
  1608. if (p) {
  1609. /* here we just clear the active flag when do_now isn't set;
  1610. * actual clean-ups will be done later in
  1611. * purify_inactive_streams() called from snd_hda_codec_prpapre()
  1612. */
  1613. if (do_now)
  1614. really_cleanup_stream(codec, p);
  1615. else
  1616. p->active = 0;
  1617. }
  1618. }
  1619. EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream);
  1620. static void really_cleanup_stream(struct hda_codec *codec,
  1621. struct hda_cvt_setup *q)
  1622. {
  1623. hda_nid_t nid = q->nid;
  1624. if (q->stream_tag || q->channel_id)
  1625. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
  1626. if (q->format_id)
  1627. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
  1628. );
  1629. memset(q, 0, sizeof(*q));
  1630. q->nid = nid;
  1631. }
  1632. /* clean up the all conflicting obsolete streams */
  1633. static void purify_inactive_streams(struct hda_codec *codec)
  1634. {
  1635. struct hda_codec *c;
  1636. int i;
  1637. list_for_each_entry(c, &codec->bus->codec_list, list) {
  1638. for (i = 0; i < c->cvt_setups.used; i++) {
  1639. struct hda_cvt_setup *p;
  1640. p = snd_array_elem(&c->cvt_setups, i);
  1641. if (p->dirty)
  1642. really_cleanup_stream(c, p);
  1643. }
  1644. }
  1645. }
  1646. #ifdef CONFIG_PM
  1647. /* clean up all streams; called from suspend */
  1648. static void hda_cleanup_all_streams(struct hda_codec *codec)
  1649. {
  1650. int i;
  1651. for (i = 0; i < codec->cvt_setups.used; i++) {
  1652. struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
  1653. if (p->stream_tag)
  1654. really_cleanup_stream(codec, p);
  1655. }
  1656. }
  1657. #endif
  1658. /*
  1659. * amp access functions
  1660. */
  1661. /* FIXME: more better hash key? */
  1662. #define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
  1663. #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
  1664. #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
  1665. #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
  1666. #define INFO_AMP_CAPS (1<<0)
  1667. #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
  1668. /* initialize the hash table */
  1669. static void init_hda_cache(struct hda_cache_rec *cache,
  1670. unsigned int record_size)
  1671. {
  1672. memset(cache, 0, sizeof(*cache));
  1673. memset(cache->hash, 0xff, sizeof(cache->hash));
  1674. snd_array_init(&cache->buf, record_size, 64);
  1675. }
  1676. static void free_hda_cache(struct hda_cache_rec *cache)
  1677. {
  1678. snd_array_free(&cache->buf);
  1679. }
  1680. /* query the hash. allocate an entry if not found. */
  1681. static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
  1682. {
  1683. u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
  1684. u16 cur = cache->hash[idx];
  1685. struct hda_cache_head *info;
  1686. while (cur != 0xffff) {
  1687. info = snd_array_elem(&cache->buf, cur);
  1688. if (info->key == key)
  1689. return info;
  1690. cur = info->next;
  1691. }
  1692. return NULL;
  1693. }
  1694. /* query the hash. allocate an entry if not found. */
  1695. static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
  1696. u32 key)
  1697. {
  1698. struct hda_cache_head *info = get_hash(cache, key);
  1699. if (!info) {
  1700. u16 idx, cur;
  1701. /* add a new hash entry */
  1702. info = snd_array_new(&cache->buf);
  1703. if (!info)
  1704. return NULL;
  1705. cur = snd_array_index(&cache->buf, info);
  1706. info->key = key;
  1707. info->val = 0;
  1708. info->dirty = 0;
  1709. idx = key % (u16)ARRAY_SIZE(cache->hash);
  1710. info->next = cache->hash[idx];
  1711. cache->hash[idx] = cur;
  1712. }
  1713. return info;
  1714. }
  1715. /* query and allocate an amp hash entry */
  1716. static inline struct hda_amp_info *
  1717. get_alloc_amp_hash(struct hda_codec *codec, u32 key)
  1718. {
  1719. return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
  1720. }
  1721. /* overwrite the value with the key in the caps hash */
  1722. static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
  1723. {
  1724. struct hda_amp_info *info;
  1725. mutex_lock(&codec->hash_mutex);
  1726. info = get_alloc_amp_hash(codec, key);
  1727. if (!info) {
  1728. mutex_unlock(&codec->hash_mutex);
  1729. return -EINVAL;
  1730. }
  1731. info->amp_caps = val;
  1732. info->head.val |= INFO_AMP_CAPS;
  1733. mutex_unlock(&codec->hash_mutex);
  1734. return 0;
  1735. }
  1736. /* query the value from the caps hash; if not found, fetch the current
  1737. * value from the given function and store in the hash
  1738. */
  1739. static unsigned int
  1740. query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
  1741. unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
  1742. {
  1743. struct hda_amp_info *info;
  1744. unsigned int val;
  1745. mutex_lock(&codec->hash_mutex);
  1746. info = get_alloc_amp_hash(codec, key);
  1747. if (!info) {
  1748. mutex_unlock(&codec->hash_mutex);
  1749. return 0;
  1750. }
  1751. if (!(info->head.val & INFO_AMP_CAPS)) {
  1752. mutex_unlock(&codec->hash_mutex); /* for reentrance */
  1753. val = func(codec, nid, dir);
  1754. write_caps_hash(codec, key, val);
  1755. } else {
  1756. val = info->amp_caps;
  1757. mutex_unlock(&codec->hash_mutex);
  1758. }
  1759. return val;
  1760. }
  1761. static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
  1762. int direction)
  1763. {
  1764. if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
  1765. nid = codec->afg;
  1766. return snd_hda_param_read(codec, nid,
  1767. direction == HDA_OUTPUT ?
  1768. AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
  1769. }
  1770. /**
  1771. * query_amp_caps - query AMP capabilities
  1772. * @codec: the HD-auio codec
  1773. * @nid: the NID to query
  1774. * @direction: either #HDA_INPUT or #HDA_OUTPUT
  1775. *
  1776. * Query AMP capabilities for the given widget and direction.
  1777. * Returns the obtained capability bits.
  1778. *
  1779. * When cap bits have been already read, this doesn't read again but
  1780. * returns the cached value.
  1781. */
  1782. u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
  1783. {
  1784. return query_caps_hash(codec, nid, direction,
  1785. HDA_HASH_KEY(nid, direction, 0),
  1786. read_amp_cap);
  1787. }
  1788. EXPORT_SYMBOL_GPL(query_amp_caps);
  1789. /**
  1790. * snd_hda_override_amp_caps - Override the AMP capabilities
  1791. * @codec: the CODEC to clean up
  1792. * @nid: the NID to clean up
  1793. * @direction: either #HDA_INPUT or #HDA_OUTPUT
  1794. * @caps: the capability bits to set
  1795. *
  1796. * Override the cached AMP caps bits value by the given one.
  1797. * This function is useful if the driver needs to adjust the AMP ranges,
  1798. * e.g. limit to 0dB, etc.
  1799. *
  1800. * Returns zero if successful or a negative error code.
  1801. */
  1802. int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
  1803. unsigned int caps)
  1804. {
  1805. return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
  1806. }
  1807. EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps);
  1808. static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
  1809. int dir)
  1810. {
  1811. return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
  1812. }
  1813. /**
  1814. * snd_hda_query_pin_caps - Query PIN capabilities
  1815. * @codec: the HD-auio codec
  1816. * @nid: the NID to query
  1817. *
  1818. * Query PIN capabilities for the given widget.
  1819. * Returns the obtained capability bits.
  1820. *
  1821. * When cap bits have been already read, this doesn't read again but
  1822. * returns the cached value.
  1823. */
  1824. u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
  1825. {
  1826. return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
  1827. read_pin_cap);
  1828. }
  1829. EXPORT_SYMBOL_GPL(snd_hda_query_pin_caps);
  1830. /**
  1831. * snd_hda_override_pin_caps - Override the pin capabilities
  1832. * @codec: the CODEC
  1833. * @nid: the NID to override
  1834. * @caps: the capability bits to set
  1835. *
  1836. * Override the cached PIN capabilitiy bits value by the given one.
  1837. *
  1838. * Returns zero if successful or a negative error code.
  1839. */
  1840. int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
  1841. unsigned int caps)
  1842. {
  1843. return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
  1844. }
  1845. EXPORT_SYMBOL_GPL(snd_hda_override_pin_caps);
  1846. /* read or sync the hash value with the current value;
  1847. * call within hash_mutex
  1848. */
  1849. static struct hda_amp_info *
  1850. update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
  1851. int direction, int index, bool init_only)
  1852. {
  1853. struct hda_amp_info *info;
  1854. unsigned int parm, val = 0;
  1855. bool val_read = false;
  1856. retry:
  1857. info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
  1858. if (!info)
  1859. return NULL;
  1860. if (!(info->head.val & INFO_AMP_VOL(ch))) {
  1861. if (!val_read) {
  1862. mutex_unlock(&codec->hash_mutex);
  1863. parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
  1864. parm |= direction == HDA_OUTPUT ?
  1865. AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
  1866. parm |= index;
  1867. val = snd_hda_codec_read(codec, nid, 0,
  1868. AC_VERB_GET_AMP_GAIN_MUTE, parm);
  1869. val &= 0xff;
  1870. val_read = true;
  1871. mutex_lock(&codec->hash_mutex);
  1872. goto retry;
  1873. }
  1874. info->vol[ch] = val;
  1875. info->head.val |= INFO_AMP_VOL(ch);
  1876. } else if (init_only)
  1877. return NULL;
  1878. return info;
  1879. }
  1880. /*
  1881. * write the current volume in info to the h/w
  1882. */
  1883. static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps,
  1884. hda_nid_t nid, int ch, int direction, int index,
  1885. int val)
  1886. {
  1887. u32 parm;
  1888. parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
  1889. parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
  1890. parm |= index << AC_AMP_SET_INDEX_SHIFT;
  1891. if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) &&
  1892. (amp_caps & AC_AMPCAP_MIN_MUTE))
  1893. ; /* set the zero value as a fake mute */
  1894. else
  1895. parm |= val;
  1896. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
  1897. }
  1898. /**
  1899. * snd_hda_codec_amp_read - Read AMP value
  1900. * @codec: HD-audio codec
  1901. * @nid: NID to read the AMP value
  1902. * @ch: channel (left=0 or right=1)
  1903. * @direction: #HDA_INPUT or #HDA_OUTPUT
  1904. * @index: the index value (only for input direction)
  1905. *
  1906. * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
  1907. */
  1908. int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
  1909. int direction, int index)
  1910. {
  1911. struct hda_amp_info *info;
  1912. unsigned int val = 0;
  1913. mutex_lock(&codec->hash_mutex);
  1914. info = update_amp_hash(codec, nid, ch, direction, index, false);
  1915. if (info)
  1916. val = info->vol[ch];
  1917. mutex_unlock(&codec->hash_mutex);
  1918. return val;
  1919. }
  1920. EXPORT_SYMBOL_GPL(snd_hda_codec_amp_read);
  1921. static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
  1922. int direction, int idx, int mask, int val,
  1923. bool init_only)
  1924. {
  1925. struct hda_amp_info *info;
  1926. unsigned int caps;
  1927. unsigned int cache_only;
  1928. if (snd_BUG_ON(mask & ~0xff))
  1929. mask &= 0xff;
  1930. val &= mask;
  1931. mutex_lock(&codec->hash_mutex);
  1932. info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
  1933. if (!info) {
  1934. mutex_unlock(&codec->hash_mutex);
  1935. return 0;
  1936. }
  1937. val |= info->vol[ch] & ~mask;
  1938. if (info->vol[ch] == val) {
  1939. mutex_unlock(&codec->hash_mutex);
  1940. return 0;
  1941. }
  1942. info->vol[ch] = val;
  1943. cache_only = info->head.dirty = codec->cached_write;
  1944. caps = info->amp_caps;
  1945. mutex_unlock(&codec->hash_mutex);
  1946. if (!cache_only)
  1947. put_vol_mute(codec, caps, nid, ch, direction, idx, val);
  1948. return 1;
  1949. }
  1950. /**
  1951. * snd_hda_codec_amp_update - update the AMP value
  1952. * @codec: HD-audio codec
  1953. * @nid: NID to read the AMP value
  1954. * @ch: channel (left=0 or right=1)
  1955. * @direction: #HDA_INPUT or #HDA_OUTPUT
  1956. * @idx: the index value (only for input direction)
  1957. * @mask: bit mask to set
  1958. * @val: the bits value to set
  1959. *
  1960. * Update the AMP value with a bit mask.
  1961. * Returns 0 if the value is unchanged, 1 if changed.
  1962. */
  1963. int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
  1964. int direction, int idx, int mask, int val)
  1965. {
  1966. return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false);
  1967. }
  1968. EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update);
  1969. /**
  1970. * snd_hda_codec_amp_stereo - update the AMP stereo values
  1971. * @codec: HD-audio codec
  1972. * @nid: NID to read the AMP value
  1973. * @direction: #HDA_INPUT or #HDA_OUTPUT
  1974. * @idx: the index value (only for input direction)
  1975. * @mask: bit mask to set
  1976. * @val: the bits value to set
  1977. *
  1978. * Update the AMP values like snd_hda_codec_amp_update(), but for a
  1979. * stereo widget with the same mask and value.
  1980. */
  1981. int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
  1982. int direction, int idx, int mask, int val)
  1983. {
  1984. int ch, ret = 0;
  1985. if (snd_BUG_ON(mask & ~0xff))
  1986. mask &= 0xff;
  1987. for (ch = 0; ch < 2; ch++)
  1988. ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
  1989. idx, mask, val);
  1990. return ret;
  1991. }
  1992. EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo);
  1993. /* Works like snd_hda_codec_amp_update() but it writes the value only at
  1994. * the first access. If the amp was already initialized / updated beforehand,
  1995. * this does nothing.
  1996. */
  1997. int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
  1998. int dir, int idx, int mask, int val)
  1999. {
  2000. return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true);
  2001. }
  2002. EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init);
  2003. int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
  2004. int dir, int idx, int mask, int val)
  2005. {
  2006. int ch, ret = 0;
  2007. if (snd_BUG_ON(mask & ~0xff))
  2008. mask &= 0xff;
  2009. for (ch = 0; ch < 2; ch++)
  2010. ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
  2011. idx, mask, val);
  2012. return ret;
  2013. }
  2014. EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo);
  2015. /**
  2016. * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
  2017. * @codec: HD-audio codec
  2018. *
  2019. * Resume the all amp commands from the cache.
  2020. */
  2021. void snd_hda_codec_resume_amp(struct hda_codec *codec)
  2022. {
  2023. int i;
  2024. mutex_lock(&codec->hash_mutex);
  2025. codec->cached_write = 0;
  2026. for (i = 0; i < codec->amp_cache.buf.used; i++) {
  2027. struct hda_amp_info *buffer;
  2028. u32 key;
  2029. hda_nid_t nid;
  2030. unsigned int idx, dir, ch;
  2031. struct hda_amp_info info;
  2032. buffer = snd_array_elem(&codec->amp_cache.buf, i);
  2033. if (!buffer->head.dirty)
  2034. continue;
  2035. buffer->head.dirty = 0;
  2036. info = *buffer;
  2037. key = info.head.key;
  2038. if (!key)
  2039. continue;
  2040. nid = key & 0xff;
  2041. idx = (key >> 16) & 0xff;
  2042. dir = (key >> 24) & 0xff;
  2043. for (ch = 0; ch < 2; ch++) {
  2044. if (!(info.head.val & INFO_AMP_VOL(ch)))
  2045. continue;
  2046. mutex_unlock(&codec->hash_mutex);
  2047. put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
  2048. info.vol[ch]);
  2049. mutex_lock(&codec->hash_mutex);
  2050. }
  2051. }
  2052. mutex_unlock(&codec->hash_mutex);
  2053. }
  2054. EXPORT_SYMBOL_GPL(snd_hda_codec_resume_amp);
  2055. static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
  2056. unsigned int ofs)
  2057. {
  2058. u32 caps = query_amp_caps(codec, nid, dir);
  2059. /* get num steps */
  2060. caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
  2061. if (ofs < caps)
  2062. caps -= ofs;
  2063. return caps;
  2064. }
  2065. /**
  2066. * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
  2067. *
  2068. * The control element is supposed to have the private_value field
  2069. * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
  2070. */
  2071. int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
  2072. struct snd_ctl_elem_info *uinfo)
  2073. {
  2074. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2075. u16 nid = get_amp_nid(kcontrol);
  2076. u8 chs = get_amp_channels(kcontrol);
  2077. int dir = get_amp_direction(kcontrol);
  2078. unsigned int ofs = get_amp_offset(kcontrol);
  2079. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2080. uinfo->count = chs == 3 ? 2 : 1;
  2081. uinfo->value.integer.min = 0;
  2082. uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
  2083. if (!uinfo->value.integer.max) {
  2084. codec_warn(codec,
  2085. "num_steps = 0 for NID=0x%x (ctl = %s)\n",
  2086. nid, kcontrol->id.name);
  2087. return -EINVAL;
  2088. }
  2089. return 0;
  2090. }
  2091. EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info);
  2092. static inline unsigned int
  2093. read_amp_value(struct hda_codec *codec, hda_nid_t nid,
  2094. int ch, int dir, int idx, unsigned int ofs)
  2095. {
  2096. unsigned int val;
  2097. val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
  2098. val &= HDA_AMP_VOLMASK;
  2099. if (val >= ofs)
  2100. val -= ofs;
  2101. else
  2102. val = 0;
  2103. return val;
  2104. }
  2105. static inline int
  2106. update_amp_value(struct hda_codec *codec, hda_nid_t nid,
  2107. int ch, int dir, int idx, unsigned int ofs,
  2108. unsigned int val)
  2109. {
  2110. unsigned int maxval;
  2111. if (val > 0)
  2112. val += ofs;
  2113. /* ofs = 0: raw max value */
  2114. maxval = get_amp_max_value(codec, nid, dir, 0);
  2115. if (val > maxval)
  2116. val = maxval;
  2117. return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
  2118. HDA_AMP_VOLMASK, val);
  2119. }
  2120. /**
  2121. * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
  2122. *
  2123. * The control element is supposed to have the private_value field
  2124. * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
  2125. */
  2126. int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
  2127. struct snd_ctl_elem_value *ucontrol)
  2128. {
  2129. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2130. hda_nid_t nid = get_amp_nid(kcontrol);
  2131. int chs = get_amp_channels(kcontrol);
  2132. int dir = get_amp_direction(kcontrol);
  2133. int idx = get_amp_index(kcontrol);
  2134. unsigned int ofs = get_amp_offset(kcontrol);
  2135. long *valp = ucontrol->value.integer.value;
  2136. if (chs & 1)
  2137. *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
  2138. if (chs & 2)
  2139. *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
  2140. return 0;
  2141. }
  2142. EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get);
  2143. /**
  2144. * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
  2145. *
  2146. * The control element is supposed to have the private_value field
  2147. * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
  2148. */
  2149. int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
  2150. struct snd_ctl_elem_value *ucontrol)
  2151. {
  2152. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2153. hda_nid_t nid = get_amp_nid(kcontrol);
  2154. int chs = get_amp_channels(kcontrol);
  2155. int dir = get_amp_direction(kcontrol);
  2156. int idx = get_amp_index(kcontrol);
  2157. unsigned int ofs = get_amp_offset(kcontrol);
  2158. long *valp = ucontrol->value.integer.value;
  2159. int change = 0;
  2160. snd_hda_power_up(codec);
  2161. if (chs & 1) {
  2162. change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
  2163. valp++;
  2164. }
  2165. if (chs & 2)
  2166. change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
  2167. snd_hda_power_down(codec);
  2168. return change;
  2169. }
  2170. EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put);
  2171. /**
  2172. * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
  2173. *
  2174. * The control element is supposed to have the private_value field
  2175. * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
  2176. */
  2177. int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
  2178. unsigned int size, unsigned int __user *_tlv)
  2179. {
  2180. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2181. hda_nid_t nid = get_amp_nid(kcontrol);
  2182. int dir = get_amp_direction(kcontrol);
  2183. unsigned int ofs = get_amp_offset(kcontrol);
  2184. bool min_mute = get_amp_min_mute(kcontrol);
  2185. u32 caps, val1, val2;
  2186. if (size < 4 * sizeof(unsigned int))
  2187. return -ENOMEM;
  2188. caps = query_amp_caps(codec, nid, dir);
  2189. val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
  2190. val2 = (val2 + 1) * 25;
  2191. val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
  2192. val1 += ofs;
  2193. val1 = ((int)val1) * ((int)val2);
  2194. if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
  2195. val2 |= TLV_DB_SCALE_MUTE;
  2196. if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
  2197. return -EFAULT;
  2198. if (put_user(2 * sizeof(unsigned int), _tlv + 1))
  2199. return -EFAULT;
  2200. if (put_user(val1, _tlv + 2))
  2201. return -EFAULT;
  2202. if (put_user(val2, _tlv + 3))
  2203. return -EFAULT;
  2204. return 0;
  2205. }
  2206. EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv);
  2207. /**
  2208. * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
  2209. * @codec: HD-audio codec
  2210. * @nid: NID of a reference widget
  2211. * @dir: #HDA_INPUT or #HDA_OUTPUT
  2212. * @tlv: TLV data to be stored, at least 4 elements
  2213. *
  2214. * Set (static) TLV data for a virtual master volume using the AMP caps
  2215. * obtained from the reference NID.
  2216. * The volume range is recalculated as if the max volume is 0dB.
  2217. */
  2218. void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
  2219. unsigned int *tlv)
  2220. {
  2221. u32 caps;
  2222. int nums, step;
  2223. caps = query_amp_caps(codec, nid, dir);
  2224. nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
  2225. step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
  2226. step = (step + 1) * 25;
  2227. tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
  2228. tlv[1] = 2 * sizeof(unsigned int);
  2229. tlv[2] = -nums * step;
  2230. tlv[3] = step;
  2231. }
  2232. EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv);
  2233. /* find a mixer control element with the given name */
  2234. static struct snd_kcontrol *
  2235. find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
  2236. {
  2237. struct snd_ctl_elem_id id;
  2238. memset(&id, 0, sizeof(id));
  2239. id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  2240. id.device = dev;
  2241. id.index = idx;
  2242. if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
  2243. return NULL;
  2244. strcpy(id.name, name);
  2245. return snd_ctl_find_id(codec->bus->card, &id);
  2246. }
  2247. /**
  2248. * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
  2249. * @codec: HD-audio codec
  2250. * @name: ctl id name string
  2251. *
  2252. * Get the control element with the given id string and IFACE_MIXER.
  2253. */
  2254. struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
  2255. const char *name)
  2256. {
  2257. return find_mixer_ctl(codec, name, 0, 0);
  2258. }
  2259. EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl);
  2260. static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
  2261. int start_idx)
  2262. {
  2263. int i, idx;
  2264. /* 16 ctlrs should be large enough */
  2265. for (i = 0, idx = start_idx; i < 16; i++, idx++) {
  2266. if (!find_mixer_ctl(codec, name, 0, idx))
  2267. return idx;
  2268. }
  2269. return -EBUSY;
  2270. }
  2271. /**
  2272. * snd_hda_ctl_add - Add a control element and assign to the codec
  2273. * @codec: HD-audio codec
  2274. * @nid: corresponding NID (optional)
  2275. * @kctl: the control element to assign
  2276. *
  2277. * Add the given control element to an array inside the codec instance.
  2278. * All control elements belonging to a codec are supposed to be added
  2279. * by this function so that a proper clean-up works at the free or
  2280. * reconfiguration time.
  2281. *
  2282. * If non-zero @nid is passed, the NID is assigned to the control element.
  2283. * The assignment is shown in the codec proc file.
  2284. *
  2285. * snd_hda_ctl_add() checks the control subdev id field whether
  2286. * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
  2287. * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
  2288. * specifies if kctl->private_value is a HDA amplifier value.
  2289. */
  2290. int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
  2291. struct snd_kcontrol *kctl)
  2292. {
  2293. int err;
  2294. unsigned short flags = 0;
  2295. struct hda_nid_item *item;
  2296. if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
  2297. flags |= HDA_NID_ITEM_AMP;
  2298. if (nid == 0)
  2299. nid = get_amp_nid_(kctl->private_value);
  2300. }
  2301. if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
  2302. nid = kctl->id.subdevice & 0xffff;
  2303. if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
  2304. kctl->id.subdevice = 0;
  2305. err = snd_ctl_add(codec->bus->card, kctl);
  2306. if (err < 0)
  2307. return err;
  2308. item = snd_array_new(&codec->mixers);
  2309. if (!item)
  2310. return -ENOMEM;
  2311. item->kctl = kctl;
  2312. item->nid = nid;
  2313. item->flags = flags;
  2314. return 0;
  2315. }
  2316. EXPORT_SYMBOL_GPL(snd_hda_ctl_add);
  2317. /**
  2318. * snd_hda_add_nid - Assign a NID to a control element
  2319. * @codec: HD-audio codec
  2320. * @nid: corresponding NID (optional)
  2321. * @kctl: the control element to assign
  2322. * @index: index to kctl
  2323. *
  2324. * Add the given control element to an array inside the codec instance.
  2325. * This function is used when #snd_hda_ctl_add cannot be used for 1:1
  2326. * NID:KCTL mapping - for example "Capture Source" selector.
  2327. */
  2328. int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
  2329. unsigned int index, hda_nid_t nid)
  2330. {
  2331. struct hda_nid_item *item;
  2332. if (nid > 0) {
  2333. item = snd_array_new(&codec->nids);
  2334. if (!item)
  2335. return -ENOMEM;
  2336. item->kctl = kctl;
  2337. item->index = index;
  2338. item->nid = nid;
  2339. return 0;
  2340. }
  2341. codec_err(codec, "no NID for mapping control %s:%d:%d\n",
  2342. kctl->id.name, kctl->id.index, index);
  2343. return -EINVAL;
  2344. }
  2345. EXPORT_SYMBOL_GPL(snd_hda_add_nid);
  2346. /**
  2347. * snd_hda_ctls_clear - Clear all controls assigned to the given codec
  2348. * @codec: HD-audio codec
  2349. */
  2350. void snd_hda_ctls_clear(struct hda_codec *codec)
  2351. {
  2352. int i;
  2353. struct hda_nid_item *items = codec->mixers.list;
  2354. for (i = 0; i < codec->mixers.used; i++)
  2355. snd_ctl_remove(codec->bus->card, items[i].kctl);
  2356. snd_array_free(&codec->mixers);
  2357. snd_array_free(&codec->nids);
  2358. }
  2359. /* pseudo device locking
  2360. * toggle card->shutdown to allow/disallow the device access (as a hack)
  2361. */
  2362. int snd_hda_lock_devices(struct hda_bus *bus)
  2363. {
  2364. struct snd_card *card = bus->card;
  2365. struct hda_codec *codec;
  2366. spin_lock(&card->files_lock);
  2367. if (card->shutdown)
  2368. goto err_unlock;
  2369. card->shutdown = 1;
  2370. if (!list_empty(&card->ctl_files))
  2371. goto err_clear;
  2372. list_for_each_entry(codec, &bus->codec_list, list) {
  2373. int pcm;
  2374. for (pcm = 0; pcm < codec->num_pcms; pcm++) {
  2375. struct hda_pcm *cpcm = &codec->pcm_info[pcm];
  2376. if (!cpcm->pcm)
  2377. continue;
  2378. if (cpcm->pcm->streams[0].substream_opened ||
  2379. cpcm->pcm->streams[1].substream_opened)
  2380. goto err_clear;
  2381. }
  2382. }
  2383. spin_unlock(&card->files_lock);
  2384. return 0;
  2385. err_clear:
  2386. card->shutdown = 0;
  2387. err_unlock:
  2388. spin_unlock(&card->files_lock);
  2389. return -EINVAL;
  2390. }
  2391. EXPORT_SYMBOL_GPL(snd_hda_lock_devices);
  2392. void snd_hda_unlock_devices(struct hda_bus *bus)
  2393. {
  2394. struct snd_card *card = bus->card;
  2395. card = bus->card;
  2396. spin_lock(&card->files_lock);
  2397. card->shutdown = 0;
  2398. spin_unlock(&card->files_lock);
  2399. }
  2400. EXPORT_SYMBOL_GPL(snd_hda_unlock_devices);
  2401. /**
  2402. * snd_hda_codec_reset - Clear all objects assigned to the codec
  2403. * @codec: HD-audio codec
  2404. *
  2405. * This frees the all PCM and control elements assigned to the codec, and
  2406. * clears the caches and restores the pin default configurations.
  2407. *
  2408. * When a device is being used, it returns -EBSY. If successfully freed,
  2409. * returns zero.
  2410. */
  2411. int snd_hda_codec_reset(struct hda_codec *codec)
  2412. {
  2413. struct hda_bus *bus = codec->bus;
  2414. struct snd_card *card = bus->card;
  2415. int i;
  2416. if (snd_hda_lock_devices(bus) < 0)
  2417. return -EBUSY;
  2418. /* OK, let it free */
  2419. cancel_delayed_work_sync(&codec->jackpoll_work);
  2420. #ifdef CONFIG_PM
  2421. cancel_delayed_work_sync(&codec->power_work);
  2422. flush_workqueue(bus->workq);
  2423. #endif
  2424. snd_hda_ctls_clear(codec);
  2425. /* release PCMs */
  2426. for (i = 0; i < codec->num_pcms; i++) {
  2427. if (codec->pcm_info[i].pcm) {
  2428. snd_device_free(card, codec->pcm_info[i].pcm);
  2429. clear_bit(codec->pcm_info[i].device,
  2430. bus->pcm_dev_bits);
  2431. }
  2432. }
  2433. snd_hda_detach_beep_device(codec);
  2434. if (codec->patch_ops.free)
  2435. codec->patch_ops.free(codec);
  2436. memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
  2437. snd_hda_jack_tbl_clear(codec);
  2438. codec->proc_widget_hook = NULL;
  2439. codec->spec = NULL;
  2440. free_hda_cache(&codec->amp_cache);
  2441. free_hda_cache(&codec->cmd_cache);
  2442. init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
  2443. init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
  2444. /* free only driver_pins so that init_pins + user_pins are restored */
  2445. snd_array_free(&codec->driver_pins);
  2446. snd_array_free(&codec->cvt_setups);
  2447. snd_array_free(&codec->spdif_out);
  2448. snd_array_free(&codec->verbs);
  2449. codec->num_pcms = 0;
  2450. codec->pcm_info = NULL;
  2451. codec->preset = NULL;
  2452. codec->slave_dig_outs = NULL;
  2453. codec->spdif_status_reset = 0;
  2454. unload_parser(codec);
  2455. module_put(codec->owner);
  2456. codec->owner = NULL;
  2457. /* allow device access again */
  2458. snd_hda_unlock_devices(bus);
  2459. return 0;
  2460. }
  2461. typedef int (*map_slave_func_t)(struct hda_codec *, void *, struct snd_kcontrol *);
  2462. /* apply the function to all matching slave ctls in the mixer list */
  2463. static int map_slaves(struct hda_codec *codec, const char * const *slaves,
  2464. const char *suffix, map_slave_func_t func, void *data)
  2465. {
  2466. struct hda_nid_item *items;
  2467. const char * const *s;
  2468. int i, err;
  2469. items = codec->mixers.list;
  2470. for (i = 0; i < codec->mixers.used; i++) {
  2471. struct snd_kcontrol *sctl = items[i].kctl;
  2472. if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
  2473. continue;
  2474. for (s = slaves; *s; s++) {
  2475. char tmpname[sizeof(sctl->id.name)];
  2476. const char *name = *s;
  2477. if (suffix) {
  2478. snprintf(tmpname, sizeof(tmpname), "%s %s",
  2479. name, suffix);
  2480. name = tmpname;
  2481. }
  2482. if (!strcmp(sctl->id.name, name)) {
  2483. err = func(codec, data, sctl);
  2484. if (err)
  2485. return err;
  2486. break;
  2487. }
  2488. }
  2489. }
  2490. return 0;
  2491. }
  2492. static int check_slave_present(struct hda_codec *codec,
  2493. void *data, struct snd_kcontrol *sctl)
  2494. {
  2495. return 1;
  2496. }
  2497. /* guess the value corresponding to 0dB */
  2498. static int get_kctl_0dB_offset(struct hda_codec *codec,
  2499. struct snd_kcontrol *kctl, int *step_to_check)
  2500. {
  2501. int _tlv[4];
  2502. const int *tlv = NULL;
  2503. int val = -1;
  2504. if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
  2505. /* FIXME: set_fs() hack for obtaining user-space TLV data */
  2506. mm_segment_t fs = get_fs();
  2507. set_fs(get_ds());
  2508. if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
  2509. tlv = _tlv;
  2510. set_fs(fs);
  2511. } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
  2512. tlv = kctl->tlv.p;
  2513. if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) {
  2514. int step = tlv[3];
  2515. step &= ~TLV_DB_SCALE_MUTE;
  2516. if (!step)
  2517. return -1;
  2518. if (*step_to_check && *step_to_check != step) {
  2519. codec_err(codec, "Mismatching dB step for vmaster slave (%d!=%d)\n",
  2520. - *step_to_check, step);
  2521. return -1;
  2522. }
  2523. *step_to_check = step;
  2524. val = -tlv[2] / step;
  2525. }
  2526. return val;
  2527. }
  2528. /* call kctl->put with the given value(s) */
  2529. static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
  2530. {
  2531. struct snd_ctl_elem_value *ucontrol;
  2532. ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
  2533. if (!ucontrol)
  2534. return -ENOMEM;
  2535. ucontrol->value.integer.value[0] = val;
  2536. ucontrol->value.integer.value[1] = val;
  2537. kctl->put(kctl, ucontrol);
  2538. kfree(ucontrol);
  2539. return 0;
  2540. }
  2541. /* initialize the slave volume with 0dB */
  2542. static int init_slave_0dB(struct hda_codec *codec,
  2543. void *data, struct snd_kcontrol *slave)
  2544. {
  2545. int offset = get_kctl_0dB_offset(codec, slave, data);
  2546. if (offset > 0)
  2547. put_kctl_with_value(slave, offset);
  2548. return 0;
  2549. }
  2550. /* unmute the slave */
  2551. static int init_slave_unmute(struct hda_codec *codec,
  2552. void *data, struct snd_kcontrol *slave)
  2553. {
  2554. return put_kctl_with_value(slave, 1);
  2555. }
  2556. static int add_slave(struct hda_codec *codec,
  2557. void *data, struct snd_kcontrol *slave)
  2558. {
  2559. return snd_ctl_add_slave(data, slave);
  2560. }
  2561. /**
  2562. * snd_hda_add_vmaster - create a virtual master control and add slaves
  2563. * @codec: HD-audio codec
  2564. * @name: vmaster control name
  2565. * @tlv: TLV data (optional)
  2566. * @slaves: slave control names (optional)
  2567. * @suffix: suffix string to each slave name (optional)
  2568. * @init_slave_vol: initialize slaves to unmute/0dB
  2569. * @ctl_ret: store the vmaster kcontrol in return
  2570. *
  2571. * Create a virtual master control with the given name. The TLV data
  2572. * must be either NULL or a valid data.
  2573. *
  2574. * @slaves is a NULL-terminated array of strings, each of which is a
  2575. * slave control name. All controls with these names are assigned to
  2576. * the new virtual master control.
  2577. *
  2578. * This function returns zero if successful or a negative error code.
  2579. */
  2580. int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
  2581. unsigned int *tlv, const char * const *slaves,
  2582. const char *suffix, bool init_slave_vol,
  2583. struct snd_kcontrol **ctl_ret)
  2584. {
  2585. struct snd_kcontrol *kctl;
  2586. int err;
  2587. if (ctl_ret)
  2588. *ctl_ret = NULL;
  2589. err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
  2590. if (err != 1) {
  2591. codec_dbg(codec, "No slave found for %s\n", name);
  2592. return 0;
  2593. }
  2594. kctl = snd_ctl_make_virtual_master(name, tlv);
  2595. if (!kctl)
  2596. return -ENOMEM;
  2597. err = snd_hda_ctl_add(codec, 0, kctl);
  2598. if (err < 0)
  2599. return err;
  2600. err = map_slaves(codec, slaves, suffix, add_slave, kctl);
  2601. if (err < 0)
  2602. return err;
  2603. /* init with master mute & zero volume */
  2604. put_kctl_with_value(kctl, 0);
  2605. if (init_slave_vol) {
  2606. int step = 0;
  2607. map_slaves(codec, slaves, suffix,
  2608. tlv ? init_slave_0dB : init_slave_unmute, &step);
  2609. }
  2610. if (ctl_ret)
  2611. *ctl_ret = kctl;
  2612. return 0;
  2613. }
  2614. EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster);
  2615. /*
  2616. * mute-LED control using vmaster
  2617. */
  2618. static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
  2619. struct snd_ctl_elem_info *uinfo)
  2620. {
  2621. static const char * const texts[] = {
  2622. "On", "Off", "Follow Master"
  2623. };
  2624. unsigned int index;
  2625. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  2626. uinfo->count = 1;
  2627. uinfo->value.enumerated.items = 3;
  2628. index = uinfo->value.enumerated.item;
  2629. if (index >= 3)
  2630. index = 2;
  2631. strcpy(uinfo->value.enumerated.name, texts[index]);
  2632. return 0;
  2633. }
  2634. static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
  2635. struct snd_ctl_elem_value *ucontrol)
  2636. {
  2637. struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
  2638. ucontrol->value.enumerated.item[0] = hook->mute_mode;
  2639. return 0;
  2640. }
  2641. static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
  2642. struct snd_ctl_elem_value *ucontrol)
  2643. {
  2644. struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
  2645. unsigned int old_mode = hook->mute_mode;
  2646. hook->mute_mode = ucontrol->value.enumerated.item[0];
  2647. if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
  2648. hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
  2649. if (old_mode == hook->mute_mode)
  2650. return 0;
  2651. snd_hda_sync_vmaster_hook(hook);
  2652. return 1;
  2653. }
  2654. static struct snd_kcontrol_new vmaster_mute_mode = {
  2655. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2656. .name = "Mute-LED Mode",
  2657. .info = vmaster_mute_mode_info,
  2658. .get = vmaster_mute_mode_get,
  2659. .put = vmaster_mute_mode_put,
  2660. };
  2661. /*
  2662. * Add a mute-LED hook with the given vmaster switch kctl
  2663. * "Mute-LED Mode" control is automatically created and associated with
  2664. * the given hook.
  2665. */
  2666. int snd_hda_add_vmaster_hook(struct hda_codec *codec,
  2667. struct hda_vmaster_mute_hook *hook,
  2668. bool expose_enum_ctl)
  2669. {
  2670. struct snd_kcontrol *kctl;
  2671. if (!hook->hook || !hook->sw_kctl)
  2672. return 0;
  2673. snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
  2674. hook->codec = codec;
  2675. hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
  2676. if (!expose_enum_ctl)
  2677. return 0;
  2678. kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
  2679. if (!kctl)
  2680. return -ENOMEM;
  2681. return snd_hda_ctl_add(codec, 0, kctl);
  2682. }
  2683. EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook);
  2684. /*
  2685. * Call the hook with the current value for synchronization
  2686. * Should be called in init callback
  2687. */
  2688. void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
  2689. {
  2690. if (!hook->hook || !hook->codec)
  2691. return;
  2692. /* don't call vmaster hook in the destructor since it might have
  2693. * been already destroyed
  2694. */
  2695. if (hook->codec->bus->shutdown)
  2696. return;
  2697. switch (hook->mute_mode) {
  2698. case HDA_VMUTE_FOLLOW_MASTER:
  2699. snd_ctl_sync_vmaster_hook(hook->sw_kctl);
  2700. break;
  2701. default:
  2702. hook->hook(hook->codec, hook->mute_mode);
  2703. break;
  2704. }
  2705. }
  2706. EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook);
  2707. /**
  2708. * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
  2709. *
  2710. * The control element is supposed to have the private_value field
  2711. * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
  2712. */
  2713. int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
  2714. struct snd_ctl_elem_info *uinfo)
  2715. {
  2716. int chs = get_amp_channels(kcontrol);
  2717. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  2718. uinfo->count = chs == 3 ? 2 : 1;
  2719. uinfo->value.integer.min = 0;
  2720. uinfo->value.integer.max = 1;
  2721. return 0;
  2722. }
  2723. EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info);
  2724. /**
  2725. * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
  2726. *
  2727. * The control element is supposed to have the private_value field
  2728. * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
  2729. */
  2730. int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
  2731. struct snd_ctl_elem_value *ucontrol)
  2732. {
  2733. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2734. hda_nid_t nid = get_amp_nid(kcontrol);
  2735. int chs = get_amp_channels(kcontrol);
  2736. int dir = get_amp_direction(kcontrol);
  2737. int idx = get_amp_index(kcontrol);
  2738. long *valp = ucontrol->value.integer.value;
  2739. if (chs & 1)
  2740. *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
  2741. HDA_AMP_MUTE) ? 0 : 1;
  2742. if (chs & 2)
  2743. *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
  2744. HDA_AMP_MUTE) ? 0 : 1;
  2745. return 0;
  2746. }
  2747. EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get);
  2748. /**
  2749. * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
  2750. *
  2751. * The control element is supposed to have the private_value field
  2752. * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
  2753. */
  2754. int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
  2755. struct snd_ctl_elem_value *ucontrol)
  2756. {
  2757. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2758. hda_nid_t nid = get_amp_nid(kcontrol);
  2759. int chs = get_amp_channels(kcontrol);
  2760. int dir = get_amp_direction(kcontrol);
  2761. int idx = get_amp_index(kcontrol);
  2762. long *valp = ucontrol->value.integer.value;
  2763. int change = 0;
  2764. snd_hda_power_up(codec);
  2765. if (chs & 1) {
  2766. change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
  2767. HDA_AMP_MUTE,
  2768. *valp ? 0 : HDA_AMP_MUTE);
  2769. valp++;
  2770. }
  2771. if (chs & 2)
  2772. change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
  2773. HDA_AMP_MUTE,
  2774. *valp ? 0 : HDA_AMP_MUTE);
  2775. hda_call_check_power_status(codec, nid);
  2776. snd_hda_power_down(codec);
  2777. return change;
  2778. }
  2779. EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put);
  2780. /*
  2781. * bound volume controls
  2782. *
  2783. * bind multiple volumes (# indices, from 0)
  2784. */
  2785. #define AMP_VAL_IDX_SHIFT 19
  2786. #define AMP_VAL_IDX_MASK (0x0f<<19)
  2787. /**
  2788. * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
  2789. *
  2790. * The control element is supposed to have the private_value field
  2791. * set up via HDA_BIND_MUTE*() macros.
  2792. */
  2793. int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
  2794. struct snd_ctl_elem_value *ucontrol)
  2795. {
  2796. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2797. unsigned long pval;
  2798. int err;
  2799. mutex_lock(&codec->control_mutex);
  2800. pval = kcontrol->private_value;
  2801. kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
  2802. err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
  2803. kcontrol->private_value = pval;
  2804. mutex_unlock(&codec->control_mutex);
  2805. return err;
  2806. }
  2807. EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get);
  2808. /**
  2809. * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
  2810. *
  2811. * The control element is supposed to have the private_value field
  2812. * set up via HDA_BIND_MUTE*() macros.
  2813. */
  2814. int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
  2815. struct snd_ctl_elem_value *ucontrol)
  2816. {
  2817. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2818. unsigned long pval;
  2819. int i, indices, err = 0, change = 0;
  2820. mutex_lock(&codec->control_mutex);
  2821. pval = kcontrol->private_value;
  2822. indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
  2823. for (i = 0; i < indices; i++) {
  2824. kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
  2825. (i << AMP_VAL_IDX_SHIFT);
  2826. err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
  2827. if (err < 0)
  2828. break;
  2829. change |= err;
  2830. }
  2831. kcontrol->private_value = pval;
  2832. mutex_unlock(&codec->control_mutex);
  2833. return err < 0 ? err : change;
  2834. }
  2835. EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put);
  2836. /**
  2837. * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
  2838. *
  2839. * The control element is supposed to have the private_value field
  2840. * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
  2841. */
  2842. int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
  2843. struct snd_ctl_elem_info *uinfo)
  2844. {
  2845. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2846. struct hda_bind_ctls *c;
  2847. int err;
  2848. mutex_lock(&codec->control_mutex);
  2849. c = (struct hda_bind_ctls *)kcontrol->private_value;
  2850. kcontrol->private_value = *c->values;
  2851. err = c->ops->info(kcontrol, uinfo);
  2852. kcontrol->private_value = (long)c;
  2853. mutex_unlock(&codec->control_mutex);
  2854. return err;
  2855. }
  2856. EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info);
  2857. /**
  2858. * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
  2859. *
  2860. * The control element is supposed to have the private_value field
  2861. * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
  2862. */
  2863. int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
  2864. struct snd_ctl_elem_value *ucontrol)
  2865. {
  2866. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2867. struct hda_bind_ctls *c;
  2868. int err;
  2869. mutex_lock(&codec->control_mutex);
  2870. c = (struct hda_bind_ctls *)kcontrol->private_value;
  2871. kcontrol->private_value = *c->values;
  2872. err = c->ops->get(kcontrol, ucontrol);
  2873. kcontrol->private_value = (long)c;
  2874. mutex_unlock(&codec->control_mutex);
  2875. return err;
  2876. }
  2877. EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get);
  2878. /**
  2879. * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
  2880. *
  2881. * The control element is supposed to have the private_value field
  2882. * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
  2883. */
  2884. int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
  2885. struct snd_ctl_elem_value *ucontrol)
  2886. {
  2887. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2888. struct hda_bind_ctls *c;
  2889. unsigned long *vals;
  2890. int err = 0, change = 0;
  2891. mutex_lock(&codec->control_mutex);
  2892. c = (struct hda_bind_ctls *)kcontrol->private_value;
  2893. for (vals = c->values; *vals; vals++) {
  2894. kcontrol->private_value = *vals;
  2895. err = c->ops->put(kcontrol, ucontrol);
  2896. if (err < 0)
  2897. break;
  2898. change |= err;
  2899. }
  2900. kcontrol->private_value = (long)c;
  2901. mutex_unlock(&codec->control_mutex);
  2902. return err < 0 ? err : change;
  2903. }
  2904. EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put);
  2905. /**
  2906. * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
  2907. *
  2908. * The control element is supposed to have the private_value field
  2909. * set up via HDA_BIND_VOL() macro.
  2910. */
  2911. int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
  2912. unsigned int size, unsigned int __user *tlv)
  2913. {
  2914. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2915. struct hda_bind_ctls *c;
  2916. int err;
  2917. mutex_lock(&codec->control_mutex);
  2918. c = (struct hda_bind_ctls *)kcontrol->private_value;
  2919. kcontrol->private_value = *c->values;
  2920. err = c->ops->tlv(kcontrol, op_flag, size, tlv);
  2921. kcontrol->private_value = (long)c;
  2922. mutex_unlock(&codec->control_mutex);
  2923. return err;
  2924. }
  2925. EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv);
  2926. struct hda_ctl_ops snd_hda_bind_vol = {
  2927. .info = snd_hda_mixer_amp_volume_info,
  2928. .get = snd_hda_mixer_amp_volume_get,
  2929. .put = snd_hda_mixer_amp_volume_put,
  2930. .tlv = snd_hda_mixer_amp_tlv
  2931. };
  2932. EXPORT_SYMBOL_GPL(snd_hda_bind_vol);
  2933. struct hda_ctl_ops snd_hda_bind_sw = {
  2934. .info = snd_hda_mixer_amp_switch_info,
  2935. .get = snd_hda_mixer_amp_switch_get,
  2936. .put = snd_hda_mixer_amp_switch_put,
  2937. .tlv = snd_hda_mixer_amp_tlv
  2938. };
  2939. EXPORT_SYMBOL_GPL(snd_hda_bind_sw);
  2940. /*
  2941. * SPDIF out controls
  2942. */
  2943. static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
  2944. struct snd_ctl_elem_info *uinfo)
  2945. {
  2946. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  2947. uinfo->count = 1;
  2948. return 0;
  2949. }
  2950. static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
  2951. struct snd_ctl_elem_value *ucontrol)
  2952. {
  2953. ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
  2954. IEC958_AES0_NONAUDIO |
  2955. IEC958_AES0_CON_EMPHASIS_5015 |
  2956. IEC958_AES0_CON_NOT_COPYRIGHT;
  2957. ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
  2958. IEC958_AES1_CON_ORIGINAL;
  2959. return 0;
  2960. }
  2961. static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
  2962. struct snd_ctl_elem_value *ucontrol)
  2963. {
  2964. ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
  2965. IEC958_AES0_NONAUDIO |
  2966. IEC958_AES0_PRO_EMPHASIS_5015;
  2967. return 0;
  2968. }
  2969. static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
  2970. struct snd_ctl_elem_value *ucontrol)
  2971. {
  2972. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2973. int idx = kcontrol->private_value;
  2974. struct hda_spdif_out *spdif;
  2975. mutex_lock(&codec->spdif_mutex);
  2976. spdif = snd_array_elem(&codec->spdif_out, idx);
  2977. ucontrol->value.iec958.status[0] = spdif->status & 0xff;
  2978. ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
  2979. ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
  2980. ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
  2981. mutex_unlock(&codec->spdif_mutex);
  2982. return 0;
  2983. }
  2984. /* convert from SPDIF status bits to HDA SPDIF bits
  2985. * bit 0 (DigEn) is always set zero (to be filled later)
  2986. */
  2987. static unsigned short convert_from_spdif_status(unsigned int sbits)
  2988. {
  2989. unsigned short val = 0;
  2990. if (sbits & IEC958_AES0_PROFESSIONAL)
  2991. val |= AC_DIG1_PROFESSIONAL;
  2992. if (sbits & IEC958_AES0_NONAUDIO)
  2993. val |= AC_DIG1_NONAUDIO;
  2994. if (sbits & IEC958_AES0_PROFESSIONAL) {
  2995. if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
  2996. IEC958_AES0_PRO_EMPHASIS_5015)
  2997. val |= AC_DIG1_EMPHASIS;
  2998. } else {
  2999. if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
  3000. IEC958_AES0_CON_EMPHASIS_5015)
  3001. val |= AC_DIG1_EMPHASIS;
  3002. if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
  3003. val |= AC_DIG1_COPYRIGHT;
  3004. if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
  3005. val |= AC_DIG1_LEVEL;
  3006. val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
  3007. }
  3008. return val;
  3009. }
  3010. /* convert to SPDIF status bits from HDA SPDIF bits
  3011. */
  3012. static unsigned int convert_to_spdif_status(unsigned short val)
  3013. {
  3014. unsigned int sbits = 0;
  3015. if (val & AC_DIG1_NONAUDIO)
  3016. sbits |= IEC958_AES0_NONAUDIO;
  3017. if (val & AC_DIG1_PROFESSIONAL)
  3018. sbits |= IEC958_AES0_PROFESSIONAL;
  3019. if (sbits & IEC958_AES0_PROFESSIONAL) {
  3020. if (val & AC_DIG1_EMPHASIS)
  3021. sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
  3022. } else {
  3023. if (val & AC_DIG1_EMPHASIS)
  3024. sbits |= IEC958_AES0_CON_EMPHASIS_5015;
  3025. if (!(val & AC_DIG1_COPYRIGHT))
  3026. sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
  3027. if (val & AC_DIG1_LEVEL)
  3028. sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
  3029. sbits |= val & (0x7f << 8);
  3030. }
  3031. return sbits;
  3032. }
  3033. /* set digital convert verbs both for the given NID and its slaves */
  3034. static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
  3035. int verb, int val)
  3036. {
  3037. const hda_nid_t *d;
  3038. snd_hda_codec_write_cache(codec, nid, 0, verb, val);
  3039. d = codec->slave_dig_outs;
  3040. if (!d)
  3041. return;
  3042. for (; *d; d++)
  3043. snd_hda_codec_write_cache(codec, *d, 0, verb, val);
  3044. }
  3045. static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
  3046. int dig1, int dig2)
  3047. {
  3048. if (dig1 != -1)
  3049. set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
  3050. if (dig2 != -1)
  3051. set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
  3052. }
  3053. static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
  3054. struct snd_ctl_elem_value *ucontrol)
  3055. {
  3056. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  3057. int idx = kcontrol->private_value;
  3058. struct hda_spdif_out *spdif;
  3059. hda_nid_t nid;
  3060. unsigned short val;
  3061. int change;
  3062. mutex_lock(&codec->spdif_mutex);
  3063. spdif = snd_array_elem(&codec->spdif_out, idx);
  3064. nid = spdif->nid;
  3065. spdif->status = ucontrol->value.iec958.status[0] |
  3066. ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
  3067. ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
  3068. ((unsigned int)ucontrol->value.iec958.status[3] << 24);
  3069. val = convert_from_spdif_status(spdif->status);
  3070. val |= spdif->ctls & 1;
  3071. change = spdif->ctls != val;
  3072. spdif->ctls = val;
  3073. if (change && nid != (u16)-1)
  3074. set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
  3075. mutex_unlock(&codec->spdif_mutex);
  3076. return change;
  3077. }
  3078. #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
  3079. static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
  3080. struct snd_ctl_elem_value *ucontrol)
  3081. {
  3082. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  3083. int idx = kcontrol->private_value;
  3084. struct hda_spdif_out *spdif;
  3085. mutex_lock(&codec->spdif_mutex);
  3086. spdif = snd_array_elem(&codec->spdif_out, idx);
  3087. ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
  3088. mutex_unlock(&codec->spdif_mutex);
  3089. return 0;
  3090. }
  3091. static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
  3092. int dig1, int dig2)
  3093. {
  3094. set_dig_out_convert(codec, nid, dig1, dig2);
  3095. /* unmute amp switch (if any) */
  3096. if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
  3097. (dig1 & AC_DIG1_ENABLE))
  3098. snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
  3099. HDA_AMP_MUTE, 0);
  3100. }
  3101. static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
  3102. struct snd_ctl_elem_value *ucontrol)
  3103. {
  3104. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  3105. int idx = kcontrol->private_value;
  3106. struct hda_spdif_out *spdif;
  3107. hda_nid_t nid;
  3108. unsigned short val;
  3109. int change;
  3110. mutex_lock(&codec->spdif_mutex);
  3111. spdif = snd_array_elem(&codec->spdif_out, idx);
  3112. nid = spdif->nid;
  3113. val = spdif->ctls & ~AC_DIG1_ENABLE;
  3114. if (ucontrol->value.integer.value[0])
  3115. val |= AC_DIG1_ENABLE;
  3116. change = spdif->ctls != val;
  3117. spdif->ctls = val;
  3118. if (change && nid != (u16)-1)
  3119. set_spdif_ctls(codec, nid, val & 0xff, -1);
  3120. mutex_unlock(&codec->spdif_mutex);
  3121. return change;
  3122. }
  3123. static struct snd_kcontrol_new dig_mixes[] = {
  3124. {
  3125. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  3126. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  3127. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
  3128. .info = snd_hda_spdif_mask_info,
  3129. .get = snd_hda_spdif_cmask_get,
  3130. },
  3131. {
  3132. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  3133. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  3134. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
  3135. .info = snd_hda_spdif_mask_info,
  3136. .get = snd_hda_spdif_pmask_get,
  3137. },
  3138. {
  3139. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  3140. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
  3141. .info = snd_hda_spdif_mask_info,
  3142. .get = snd_hda_spdif_default_get,
  3143. .put = snd_hda_spdif_default_put,
  3144. },
  3145. {
  3146. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  3147. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
  3148. .info = snd_hda_spdif_out_switch_info,
  3149. .get = snd_hda_spdif_out_switch_get,
  3150. .put = snd_hda_spdif_out_switch_put,
  3151. },
  3152. { } /* end */
  3153. };
  3154. /**
  3155. * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
  3156. * @codec: the HDA codec
  3157. * @associated_nid: NID that new ctls associated with
  3158. * @cvt_nid: converter NID
  3159. * @type: HDA_PCM_TYPE_*
  3160. * Creates controls related with the digital output.
  3161. * Called from each patch supporting the digital out.
  3162. *
  3163. * Returns 0 if successful, or a negative error code.
  3164. */
  3165. int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
  3166. hda_nid_t associated_nid,
  3167. hda_nid_t cvt_nid,
  3168. int type)
  3169. {
  3170. int err;
  3171. struct snd_kcontrol *kctl;
  3172. struct snd_kcontrol_new *dig_mix;
  3173. int idx = 0;
  3174. const int spdif_index = 16;
  3175. struct hda_spdif_out *spdif;
  3176. struct hda_bus *bus = codec->bus;
  3177. if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
  3178. type == HDA_PCM_TYPE_SPDIF) {
  3179. idx = spdif_index;
  3180. } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
  3181. type == HDA_PCM_TYPE_HDMI) {
  3182. /* suppose a single SPDIF device */
  3183. for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
  3184. kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
  3185. if (!kctl)
  3186. break;
  3187. kctl->id.index = spdif_index;
  3188. }
  3189. bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
  3190. }
  3191. if (!bus->primary_dig_out_type)
  3192. bus->primary_dig_out_type = type;
  3193. idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
  3194. if (idx < 0) {
  3195. codec_err(codec, "too many IEC958 outputs\n");
  3196. return -EBUSY;
  3197. }
  3198. spdif = snd_array_new(&codec->spdif_out);
  3199. if (!spdif)
  3200. return -ENOMEM;
  3201. for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
  3202. kctl = snd_ctl_new1(dig_mix, codec);
  3203. if (!kctl)
  3204. return -ENOMEM;
  3205. kctl->id.index = idx;
  3206. kctl->private_value = codec->spdif_out.used - 1;
  3207. err = snd_hda_ctl_add(codec, associated_nid, kctl);
  3208. if (err < 0)
  3209. return err;
  3210. }
  3211. spdif->nid = cvt_nid;
  3212. spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
  3213. AC_VERB_GET_DIGI_CONVERT_1, 0);
  3214. spdif->status = convert_to_spdif_status(spdif->ctls);
  3215. return 0;
  3216. }
  3217. EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls);
  3218. /* get the hda_spdif_out entry from the given NID
  3219. * call within spdif_mutex lock
  3220. */
  3221. struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
  3222. hda_nid_t nid)
  3223. {
  3224. int i;
  3225. for (i = 0; i < codec->spdif_out.used; i++) {
  3226. struct hda_spdif_out *spdif =
  3227. snd_array_elem(&codec->spdif_out, i);
  3228. if (spdif->nid == nid)
  3229. return spdif;
  3230. }
  3231. return NULL;
  3232. }
  3233. EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid);
  3234. void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
  3235. {
  3236. struct hda_spdif_out *spdif;
  3237. mutex_lock(&codec->spdif_mutex);
  3238. spdif = snd_array_elem(&codec->spdif_out, idx);
  3239. spdif->nid = (u16)-1;
  3240. mutex_unlock(&codec->spdif_mutex);
  3241. }
  3242. EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign);
  3243. void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
  3244. {
  3245. struct hda_spdif_out *spdif;
  3246. unsigned short val;
  3247. mutex_lock(&codec->spdif_mutex);
  3248. spdif = snd_array_elem(&codec->spdif_out, idx);
  3249. if (spdif->nid != nid) {
  3250. spdif->nid = nid;
  3251. val = spdif->ctls;
  3252. set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
  3253. }
  3254. mutex_unlock(&codec->spdif_mutex);
  3255. }
  3256. EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign);
  3257. /*
  3258. * SPDIF sharing with analog output
  3259. */
  3260. static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
  3261. struct snd_ctl_elem_value *ucontrol)
  3262. {
  3263. struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
  3264. ucontrol->value.integer.value[0] = mout->share_spdif;
  3265. return 0;
  3266. }
  3267. static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
  3268. struct snd_ctl_elem_value *ucontrol)
  3269. {
  3270. struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
  3271. mout->share_spdif = !!ucontrol->value.integer.value[0];
  3272. return 0;
  3273. }
  3274. static struct snd_kcontrol_new spdif_share_sw = {
  3275. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  3276. .name = "IEC958 Default PCM Playback Switch",
  3277. .info = snd_ctl_boolean_mono_info,
  3278. .get = spdif_share_sw_get,
  3279. .put = spdif_share_sw_put,
  3280. };
  3281. /**
  3282. * snd_hda_create_spdif_share_sw - create Default PCM switch
  3283. * @codec: the HDA codec
  3284. * @mout: multi-out instance
  3285. */
  3286. int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
  3287. struct hda_multi_out *mout)
  3288. {
  3289. struct snd_kcontrol *kctl;
  3290. if (!mout->dig_out_nid)
  3291. return 0;
  3292. kctl = snd_ctl_new1(&spdif_share_sw, mout);
  3293. if (!kctl)
  3294. return -ENOMEM;
  3295. /* ATTENTION: here mout is passed as private_data, instead of codec */
  3296. return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
  3297. }
  3298. EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw);
  3299. /*
  3300. * SPDIF input
  3301. */
  3302. #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
  3303. static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
  3304. struct snd_ctl_elem_value *ucontrol)
  3305. {
  3306. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  3307. ucontrol->value.integer.value[0] = codec->spdif_in_enable;
  3308. return 0;
  3309. }
  3310. static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
  3311. struct snd_ctl_elem_value *ucontrol)
  3312. {
  3313. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  3314. hda_nid_t nid = kcontrol->private_value;
  3315. unsigned int val = !!ucontrol->value.integer.value[0];
  3316. int change;
  3317. mutex_lock(&codec->spdif_mutex);
  3318. change = codec->spdif_in_enable != val;
  3319. if (change) {
  3320. codec->spdif_in_enable = val;
  3321. snd_hda_codec_write_cache(codec, nid, 0,
  3322. AC_VERB_SET_DIGI_CONVERT_1, val);
  3323. }
  3324. mutex_unlock(&codec->spdif_mutex);
  3325. return change;
  3326. }
  3327. static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
  3328. struct snd_ctl_elem_value *ucontrol)
  3329. {
  3330. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  3331. hda_nid_t nid = kcontrol->private_value;
  3332. unsigned short val;
  3333. unsigned int sbits;
  3334. val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
  3335. sbits = convert_to_spdif_status(val);
  3336. ucontrol->value.iec958.status[0] = sbits;
  3337. ucontrol->value.iec958.status[1] = sbits >> 8;
  3338. ucontrol->value.iec958.status[2] = sbits >> 16;
  3339. ucontrol->value.iec958.status[3] = sbits >> 24;
  3340. return 0;
  3341. }
  3342. static struct snd_kcontrol_new dig_in_ctls[] = {
  3343. {
  3344. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  3345. .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
  3346. .info = snd_hda_spdif_in_switch_info,
  3347. .get = snd_hda_spdif_in_switch_get,
  3348. .put = snd_hda_spdif_in_switch_put,
  3349. },
  3350. {
  3351. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  3352. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  3353. .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
  3354. .info = snd_hda_spdif_mask_info,
  3355. .get = snd_hda_spdif_in_status_get,
  3356. },
  3357. { } /* end */
  3358. };
  3359. /**
  3360. * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
  3361. * @codec: the HDA codec
  3362. * @nid: audio in widget NID
  3363. *
  3364. * Creates controls related with the SPDIF input.
  3365. * Called from each patch supporting the SPDIF in.
  3366. *
  3367. * Returns 0 if successful, or a negative error code.
  3368. */
  3369. int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
  3370. {
  3371. int err;
  3372. struct snd_kcontrol *kctl;
  3373. struct snd_kcontrol_new *dig_mix;
  3374. int idx;
  3375. idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
  3376. if (idx < 0) {
  3377. codec_err(codec, "too many IEC958 inputs\n");
  3378. return -EBUSY;
  3379. }
  3380. for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
  3381. kctl = snd_ctl_new1(dig_mix, codec);
  3382. if (!kctl)
  3383. return -ENOMEM;
  3384. kctl->private_value = nid;
  3385. err = snd_hda_ctl_add(codec, nid, kctl);
  3386. if (err < 0)
  3387. return err;
  3388. }
  3389. codec->spdif_in_enable =
  3390. snd_hda_codec_read(codec, nid, 0,
  3391. AC_VERB_GET_DIGI_CONVERT_1, 0) &
  3392. AC_DIG1_ENABLE;
  3393. return 0;
  3394. }
  3395. EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls);
  3396. /*
  3397. * command cache
  3398. */
  3399. /* build a 31bit cache key with the widget id and the command parameter */
  3400. #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
  3401. #define get_cmd_cache_nid(key) ((key) & 0xff)
  3402. #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
  3403. /**
  3404. * snd_hda_codec_write_cache - send a single command with caching
  3405. * @codec: the HDA codec
  3406. * @nid: NID to send the command
  3407. * @flags: optional bit flags
  3408. * @verb: the verb to send
  3409. * @parm: the parameter for the verb
  3410. *
  3411. * Send a single command without waiting for response.
  3412. *
  3413. * Returns 0 if successful, or a negative error code.
  3414. */
  3415. int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
  3416. int flags, unsigned int verb, unsigned int parm)
  3417. {
  3418. int err;
  3419. struct hda_cache_head *c;
  3420. u32 key;
  3421. unsigned int cache_only;
  3422. cache_only = codec->cached_write;
  3423. if (!cache_only) {
  3424. err = snd_hda_codec_write(codec, nid, flags, verb, parm);
  3425. if (err < 0)
  3426. return err;
  3427. }
  3428. /* parm may contain the verb stuff for get/set amp */
  3429. verb = verb | (parm >> 8);
  3430. parm &= 0xff;
  3431. key = build_cmd_cache_key(nid, verb);
  3432. mutex_lock(&codec->bus->cmd_mutex);
  3433. c = get_alloc_hash(&codec->cmd_cache, key);
  3434. if (c) {
  3435. c->val = parm;
  3436. c->dirty = cache_only;
  3437. }
  3438. mutex_unlock(&codec->bus->cmd_mutex);
  3439. return 0;
  3440. }
  3441. EXPORT_SYMBOL_GPL(snd_hda_codec_write_cache);
  3442. /**
  3443. * snd_hda_codec_update_cache - check cache and write the cmd only when needed
  3444. * @codec: the HDA codec
  3445. * @nid: NID to send the command
  3446. * @flags: optional bit flags
  3447. * @verb: the verb to send
  3448. * @parm: the parameter for the verb
  3449. *
  3450. * This function works like snd_hda_codec_write_cache(), but it doesn't send
  3451. * command if the parameter is already identical with the cached value.
  3452. * If not, it sends the command and refreshes the cache.
  3453. *
  3454. * Returns 0 if successful, or a negative error code.
  3455. */
  3456. int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
  3457. int flags, unsigned int verb, unsigned int parm)
  3458. {
  3459. struct hda_cache_head *c;
  3460. u32 key;
  3461. /* parm may contain the verb stuff for get/set amp */
  3462. verb = verb | (parm >> 8);
  3463. parm &= 0xff;
  3464. key = build_cmd_cache_key(nid, verb);
  3465. mutex_lock(&codec->bus->cmd_mutex);
  3466. c = get_hash(&codec->cmd_cache, key);
  3467. if (c && c->val == parm) {
  3468. mutex_unlock(&codec->bus->cmd_mutex);
  3469. return 0;
  3470. }
  3471. mutex_unlock(&codec->bus->cmd_mutex);
  3472. return snd_hda_codec_write_cache(codec, nid, flags, verb, parm);
  3473. }
  3474. EXPORT_SYMBOL_GPL(snd_hda_codec_update_cache);
  3475. /**
  3476. * snd_hda_codec_resume_cache - Resume the all commands from the cache
  3477. * @codec: HD-audio codec
  3478. *
  3479. * Execute all verbs recorded in the command caches to resume.
  3480. */
  3481. void snd_hda_codec_resume_cache(struct hda_codec *codec)
  3482. {
  3483. int i;
  3484. mutex_lock(&codec->hash_mutex);
  3485. codec->cached_write = 0;
  3486. for (i = 0; i < codec->cmd_cache.buf.used; i++) {
  3487. struct hda_cache_head *buffer;
  3488. u32 key;
  3489. buffer = snd_array_elem(&codec->cmd_cache.buf, i);
  3490. key = buffer->key;
  3491. if (!key)
  3492. continue;
  3493. if (!buffer->dirty)
  3494. continue;
  3495. buffer->dirty = 0;
  3496. mutex_unlock(&codec->hash_mutex);
  3497. snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
  3498. get_cmd_cache_cmd(key), buffer->val);
  3499. mutex_lock(&codec->hash_mutex);
  3500. }
  3501. mutex_unlock(&codec->hash_mutex);
  3502. }
  3503. EXPORT_SYMBOL_GPL(snd_hda_codec_resume_cache);
  3504. /**
  3505. * snd_hda_sequence_write_cache - sequence writes with caching
  3506. * @codec: the HDA codec
  3507. * @seq: VERB array to send
  3508. *
  3509. * Send the commands sequentially from the given array.
  3510. * Thte commands are recorded on cache for power-save and resume.
  3511. * The array must be terminated with NID=0.
  3512. */
  3513. void snd_hda_sequence_write_cache(struct hda_codec *codec,
  3514. const struct hda_verb *seq)
  3515. {
  3516. for (; seq->nid; seq++)
  3517. snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
  3518. seq->param);
  3519. }
  3520. EXPORT_SYMBOL_GPL(snd_hda_sequence_write_cache);
  3521. /**
  3522. * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
  3523. * @codec: HD-audio codec
  3524. */
  3525. void snd_hda_codec_flush_cache(struct hda_codec *codec)
  3526. {
  3527. snd_hda_codec_resume_amp(codec);
  3528. snd_hda_codec_resume_cache(codec);
  3529. }
  3530. EXPORT_SYMBOL_GPL(snd_hda_codec_flush_cache);
  3531. void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
  3532. unsigned int power_state)
  3533. {
  3534. hda_nid_t nid = codec->start_nid;
  3535. int i;
  3536. for (i = 0; i < codec->num_nodes; i++, nid++) {
  3537. unsigned int wcaps = get_wcaps(codec, nid);
  3538. unsigned int state = power_state;
  3539. if (!(wcaps & AC_WCAP_POWER))
  3540. continue;
  3541. if (codec->power_filter) {
  3542. state = codec->power_filter(codec, nid, power_state);
  3543. if (state != power_state && power_state == AC_PWRST_D3)
  3544. continue;
  3545. }
  3546. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
  3547. state);
  3548. }
  3549. }
  3550. EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all);
  3551. /*
  3552. * supported power states check
  3553. */
  3554. static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
  3555. unsigned int power_state)
  3556. {
  3557. int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
  3558. if (sup == -1)
  3559. return false;
  3560. if (sup & power_state)
  3561. return true;
  3562. else
  3563. return false;
  3564. }
  3565. /*
  3566. * wait until the state is reached, returns the current state
  3567. */
  3568. static unsigned int hda_sync_power_state(struct hda_codec *codec,
  3569. hda_nid_t fg,
  3570. unsigned int power_state)
  3571. {
  3572. unsigned long end_time = jiffies + msecs_to_jiffies(500);
  3573. unsigned int state, actual_state;
  3574. for (;;) {
  3575. state = snd_hda_codec_read(codec, fg, 0,
  3576. AC_VERB_GET_POWER_STATE, 0);
  3577. if (state & AC_PWRST_ERROR)
  3578. break;
  3579. actual_state = (state >> 4) & 0x0f;
  3580. if (actual_state == power_state)
  3581. break;
  3582. if (time_after_eq(jiffies, end_time))
  3583. break;
  3584. /* wait until the codec reachs to the target state */
  3585. msleep(1);
  3586. }
  3587. return state;
  3588. }
  3589. /* don't power down the widget if it controls eapd and EAPD_BTLENABLE is set */
  3590. unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
  3591. hda_nid_t nid,
  3592. unsigned int power_state)
  3593. {
  3594. if (nid == codec->afg || nid == codec->mfg)
  3595. return power_state;
  3596. if (power_state == AC_PWRST_D3 &&
  3597. get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
  3598. (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
  3599. int eapd = snd_hda_codec_read(codec, nid, 0,
  3600. AC_VERB_GET_EAPD_BTLENABLE, 0);
  3601. if (eapd & 0x02)
  3602. return AC_PWRST_D0;
  3603. }
  3604. return power_state;
  3605. }
  3606. EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter);
  3607. /*
  3608. * set power state of the codec, and return the power state
  3609. */
  3610. static unsigned int hda_set_power_state(struct hda_codec *codec,
  3611. unsigned int power_state)
  3612. {
  3613. hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
  3614. int count;
  3615. unsigned int state;
  3616. int flags = 0;
  3617. /* this delay seems necessary to avoid click noise at power-down */
  3618. if (power_state == AC_PWRST_D3) {
  3619. if (codec->depop_delay < 0)
  3620. msleep(codec->epss ? 10 : 100);
  3621. else if (codec->depop_delay > 0)
  3622. msleep(codec->depop_delay);
  3623. flags = HDA_RW_NO_RESPONSE_FALLBACK;
  3624. }
  3625. /* repeat power states setting at most 10 times*/
  3626. for (count = 0; count < 10; count++) {
  3627. if (codec->patch_ops.set_power_state)
  3628. codec->patch_ops.set_power_state(codec, fg,
  3629. power_state);
  3630. else {
  3631. state = power_state;
  3632. if (codec->power_filter)
  3633. state = codec->power_filter(codec, fg, state);
  3634. if (state == power_state || power_state != AC_PWRST_D3)
  3635. snd_hda_codec_read(codec, fg, flags,
  3636. AC_VERB_SET_POWER_STATE,
  3637. state);
  3638. snd_hda_codec_set_power_to_all(codec, fg, power_state);
  3639. }
  3640. state = hda_sync_power_state(codec, fg, power_state);
  3641. if (!(state & AC_PWRST_ERROR))
  3642. break;
  3643. }
  3644. return state;
  3645. }
  3646. /* sync power states of all widgets;
  3647. * this is called at the end of codec parsing
  3648. */
  3649. static void sync_power_up_states(struct hda_codec *codec)
  3650. {
  3651. hda_nid_t nid = codec->start_nid;
  3652. int i;
  3653. /* don't care if no filter is used */
  3654. if (!codec->power_filter)
  3655. return;
  3656. for (i = 0; i < codec->num_nodes; i++, nid++) {
  3657. unsigned int wcaps = get_wcaps(codec, nid);
  3658. unsigned int target;
  3659. if (!(wcaps & AC_WCAP_POWER))
  3660. continue;
  3661. target = codec->power_filter(codec, nid, AC_PWRST_D0);
  3662. if (target == AC_PWRST_D0)
  3663. continue;
  3664. if (!snd_hda_check_power_state(codec, nid, target))
  3665. snd_hda_codec_write(codec, nid, 0,
  3666. AC_VERB_SET_POWER_STATE, target);
  3667. }
  3668. }
  3669. #ifdef CONFIG_SND_HDA_RECONFIG
  3670. /* execute additional init verbs */
  3671. static void hda_exec_init_verbs(struct hda_codec *codec)
  3672. {
  3673. if (codec->init_verbs.list)
  3674. snd_hda_sequence_write(codec, codec->init_verbs.list);
  3675. }
  3676. #else
  3677. static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
  3678. #endif
  3679. #ifdef CONFIG_PM
  3680. /*
  3681. * call suspend and power-down; used both from PM and power-save
  3682. * this function returns the power state in the end
  3683. */
  3684. static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
  3685. {
  3686. unsigned int state;
  3687. codec->in_pm = 1;
  3688. if (codec->patch_ops.suspend)
  3689. codec->patch_ops.suspend(codec);
  3690. hda_cleanup_all_streams(codec);
  3691. state = hda_set_power_state(codec, AC_PWRST_D3);
  3692. /* Cancel delayed work if we aren't currently running from it. */
  3693. if (!in_wq)
  3694. cancel_delayed_work_sync(&codec->power_work);
  3695. spin_lock(&codec->power_lock);
  3696. snd_hda_update_power_acct(codec);
  3697. trace_hda_power_down(codec);
  3698. codec->power_on = 0;
  3699. codec->power_transition = 0;
  3700. codec->power_jiffies = jiffies;
  3701. spin_unlock(&codec->power_lock);
  3702. codec->in_pm = 0;
  3703. return state;
  3704. }
  3705. /* mark all entries of cmd and amp caches dirty */
  3706. static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
  3707. {
  3708. int i;
  3709. for (i = 0; i < codec->cmd_cache.buf.used; i++) {
  3710. struct hda_cache_head *cmd;
  3711. cmd = snd_array_elem(&codec->cmd_cache.buf, i);
  3712. cmd->dirty = 1;
  3713. }
  3714. for (i = 0; i < codec->amp_cache.buf.used; i++) {
  3715. struct hda_amp_info *amp;
  3716. amp = snd_array_elem(&codec->amp_cache.buf, i);
  3717. amp->head.dirty = 1;
  3718. }
  3719. }
  3720. /*
  3721. * kick up codec; used both from PM and power-save
  3722. */
  3723. static void hda_call_codec_resume(struct hda_codec *codec)
  3724. {
  3725. codec->in_pm = 1;
  3726. hda_mark_cmd_cache_dirty(codec);
  3727. /* set as if powered on for avoiding re-entering the resume
  3728. * in the resume / power-save sequence
  3729. */
  3730. hda_keep_power_on(codec);
  3731. hda_set_power_state(codec, AC_PWRST_D0);
  3732. restore_shutup_pins(codec);
  3733. hda_exec_init_verbs(codec);
  3734. snd_hda_jack_set_dirty_all(codec);
  3735. if (codec->patch_ops.resume)
  3736. codec->patch_ops.resume(codec);
  3737. else {
  3738. if (codec->patch_ops.init)
  3739. codec->patch_ops.init(codec);
  3740. snd_hda_codec_resume_amp(codec);
  3741. snd_hda_codec_resume_cache(codec);
  3742. }
  3743. if (codec->jackpoll_interval)
  3744. hda_jackpoll_work(&codec->jackpoll_work.work);
  3745. else
  3746. snd_hda_jack_report_sync(codec);
  3747. codec->in_pm = 0;
  3748. snd_hda_power_down(codec); /* flag down before returning */
  3749. }
  3750. #endif /* CONFIG_PM */
  3751. /**
  3752. * snd_hda_build_controls - build mixer controls
  3753. * @bus: the BUS
  3754. *
  3755. * Creates mixer controls for each codec included in the bus.
  3756. *
  3757. * Returns 0 if successful, otherwise a negative error code.
  3758. */
  3759. int snd_hda_build_controls(struct hda_bus *bus)
  3760. {
  3761. struct hda_codec *codec;
  3762. list_for_each_entry(codec, &bus->codec_list, list) {
  3763. int err = snd_hda_codec_build_controls(codec);
  3764. if (err < 0) {
  3765. codec_err(codec,
  3766. "cannot build controls for #%d (error %d)\n",
  3767. codec->addr, err);
  3768. err = snd_hda_codec_reset(codec);
  3769. if (err < 0) {
  3770. codec_err(codec,
  3771. "cannot revert codec\n");
  3772. return err;
  3773. }
  3774. }
  3775. }
  3776. return 0;
  3777. }
  3778. EXPORT_SYMBOL_GPL(snd_hda_build_controls);
  3779. /*
  3780. * add standard channel maps if not specified
  3781. */
  3782. static int add_std_chmaps(struct hda_codec *codec)
  3783. {
  3784. int i, str, err;
  3785. for (i = 0; i < codec->num_pcms; i++) {
  3786. for (str = 0; str < 2; str++) {
  3787. struct snd_pcm *pcm = codec->pcm_info[i].pcm;
  3788. struct hda_pcm_stream *hinfo =
  3789. &codec->pcm_info[i].stream[str];
  3790. struct snd_pcm_chmap *chmap;
  3791. const struct snd_pcm_chmap_elem *elem;
  3792. if (codec->pcm_info[i].own_chmap)
  3793. continue;
  3794. if (!pcm || !hinfo->substreams)
  3795. continue;
  3796. elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
  3797. err = snd_pcm_add_chmap_ctls(pcm, str, elem,
  3798. hinfo->channels_max,
  3799. 0, &chmap);
  3800. if (err < 0)
  3801. return err;
  3802. chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
  3803. }
  3804. }
  3805. return 0;
  3806. }
  3807. /* default channel maps for 2.1 speakers;
  3808. * since HD-audio supports only stereo, odd number channels are omitted
  3809. */
  3810. const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
  3811. { .channels = 2,
  3812. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
  3813. { .channels = 4,
  3814. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
  3815. SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
  3816. { }
  3817. };
  3818. EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
  3819. int snd_hda_codec_build_controls(struct hda_codec *codec)
  3820. {
  3821. int err = 0;
  3822. hda_exec_init_verbs(codec);
  3823. /* continue to initialize... */
  3824. if (codec->patch_ops.init)
  3825. err = codec->patch_ops.init(codec);
  3826. if (!err && codec->patch_ops.build_controls)
  3827. err = codec->patch_ops.build_controls(codec);
  3828. if (err < 0)
  3829. return err;
  3830. /* we create chmaps here instead of build_pcms */
  3831. err = add_std_chmaps(codec);
  3832. if (err < 0)
  3833. return err;
  3834. if (codec->jackpoll_interval)
  3835. hda_jackpoll_work(&codec->jackpoll_work.work);
  3836. else
  3837. snd_hda_jack_report_sync(codec); /* call at the last init point */
  3838. sync_power_up_states(codec);
  3839. return 0;
  3840. }
  3841. /*
  3842. * stream formats
  3843. */
  3844. struct hda_rate_tbl {
  3845. unsigned int hz;
  3846. unsigned int alsa_bits;
  3847. unsigned int hda_fmt;
  3848. };
  3849. /* rate = base * mult / div */
  3850. #define HDA_RATE(base, mult, div) \
  3851. (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
  3852. (((div) - 1) << AC_FMT_DIV_SHIFT))
  3853. static struct hda_rate_tbl rate_bits[] = {
  3854. /* rate in Hz, ALSA rate bitmask, HDA format value */
  3855. /* autodetected value used in snd_hda_query_supported_pcm */
  3856. { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
  3857. { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
  3858. { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
  3859. { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
  3860. { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
  3861. { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
  3862. { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
  3863. { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
  3864. { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
  3865. { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
  3866. { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
  3867. #define AC_PAR_PCM_RATE_BITS 11
  3868. /* up to bits 10, 384kHZ isn't supported properly */
  3869. /* not autodetected value */
  3870. { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
  3871. { 0 } /* terminator */
  3872. };
  3873. /**
  3874. * snd_hda_calc_stream_format - calculate format bitset
  3875. * @codec: HD-audio codec
  3876. * @rate: the sample rate
  3877. * @channels: the number of channels
  3878. * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
  3879. * @maxbps: the max. bps
  3880. *
  3881. * Calculate the format bitset from the given rate, channels and th PCM format.
  3882. *
  3883. * Return zero if invalid.
  3884. */
  3885. unsigned int snd_hda_calc_stream_format(struct hda_codec *codec,
  3886. unsigned int rate,
  3887. unsigned int channels,
  3888. unsigned int format,
  3889. unsigned int maxbps,
  3890. unsigned short spdif_ctls)
  3891. {
  3892. int i;
  3893. unsigned int val = 0;
  3894. for (i = 0; rate_bits[i].hz; i++)
  3895. if (rate_bits[i].hz == rate) {
  3896. val = rate_bits[i].hda_fmt;
  3897. break;
  3898. }
  3899. if (!rate_bits[i].hz) {
  3900. codec_dbg(codec, "invalid rate %d\n", rate);
  3901. return 0;
  3902. }
  3903. if (channels == 0 || channels > 8) {
  3904. codec_dbg(codec, "invalid channels %d\n", channels);
  3905. return 0;
  3906. }
  3907. val |= channels - 1;
  3908. switch (snd_pcm_format_width(format)) {
  3909. case 8:
  3910. val |= AC_FMT_BITS_8;
  3911. break;
  3912. case 16:
  3913. val |= AC_FMT_BITS_16;
  3914. break;
  3915. case 20:
  3916. case 24:
  3917. case 32:
  3918. if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
  3919. val |= AC_FMT_BITS_32;
  3920. else if (maxbps >= 24)
  3921. val |= AC_FMT_BITS_24;
  3922. else
  3923. val |= AC_FMT_BITS_20;
  3924. break;
  3925. default:
  3926. codec_dbg(codec, "invalid format width %d\n",
  3927. snd_pcm_format_width(format));
  3928. return 0;
  3929. }
  3930. if (spdif_ctls & AC_DIG1_NONAUDIO)
  3931. val |= AC_FMT_TYPE_NON_PCM;
  3932. return val;
  3933. }
  3934. EXPORT_SYMBOL_GPL(snd_hda_calc_stream_format);
  3935. static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
  3936. int dir)
  3937. {
  3938. unsigned int val = 0;
  3939. if (nid != codec->afg &&
  3940. (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
  3941. val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
  3942. if (!val || val == -1)
  3943. val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
  3944. if (!val || val == -1)
  3945. return 0;
  3946. return val;
  3947. }
  3948. static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
  3949. {
  3950. return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
  3951. get_pcm_param);
  3952. }
  3953. static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
  3954. int dir)
  3955. {
  3956. unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
  3957. if (!streams || streams == -1)
  3958. streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
  3959. if (!streams || streams == -1)
  3960. return 0;
  3961. return streams;
  3962. }
  3963. static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
  3964. {
  3965. return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
  3966. get_stream_param);
  3967. }
  3968. /**
  3969. * snd_hda_query_supported_pcm - query the supported PCM rates and formats
  3970. * @codec: the HDA codec
  3971. * @nid: NID to query
  3972. * @ratesp: the pointer to store the detected rate bitflags
  3973. * @formatsp: the pointer to store the detected formats
  3974. * @bpsp: the pointer to store the detected format widths
  3975. *
  3976. * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
  3977. * or @bsps argument is ignored.
  3978. *
  3979. * Returns 0 if successful, otherwise a negative error code.
  3980. */
  3981. int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
  3982. u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
  3983. {
  3984. unsigned int i, val, wcaps;
  3985. wcaps = get_wcaps(codec, nid);
  3986. val = query_pcm_param(codec, nid);
  3987. if (ratesp) {
  3988. u32 rates = 0;
  3989. for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
  3990. if (val & (1 << i))
  3991. rates |= rate_bits[i].alsa_bits;
  3992. }
  3993. if (rates == 0) {
  3994. codec_err(codec,
  3995. "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n",
  3996. nid, val,
  3997. (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
  3998. return -EIO;
  3999. }
  4000. *ratesp = rates;
  4001. }
  4002. if (formatsp || bpsp) {
  4003. u64 formats = 0;
  4004. unsigned int streams, bps;
  4005. streams = query_stream_param(codec, nid);
  4006. if (!streams)
  4007. return -EIO;
  4008. bps = 0;
  4009. if (streams & AC_SUPFMT_PCM) {
  4010. if (val & AC_SUPPCM_BITS_8) {
  4011. formats |= SNDRV_PCM_FMTBIT_U8;
  4012. bps = 8;
  4013. }
  4014. if (val & AC_SUPPCM_BITS_16) {
  4015. formats |= SNDRV_PCM_FMTBIT_S16_LE;
  4016. bps = 16;
  4017. }
  4018. if (wcaps & AC_WCAP_DIGITAL) {
  4019. if (val & AC_SUPPCM_BITS_32)
  4020. formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
  4021. if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
  4022. formats |= SNDRV_PCM_FMTBIT_S32_LE;
  4023. if (val & AC_SUPPCM_BITS_24)
  4024. bps = 24;
  4025. else if (val & AC_SUPPCM_BITS_20)
  4026. bps = 20;
  4027. } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
  4028. AC_SUPPCM_BITS_32)) {
  4029. formats |= SNDRV_PCM_FMTBIT_S32_LE;
  4030. if (val & AC_SUPPCM_BITS_32)
  4031. bps = 32;
  4032. else if (val & AC_SUPPCM_BITS_24)
  4033. bps = 24;
  4034. else if (val & AC_SUPPCM_BITS_20)
  4035. bps = 20;
  4036. }
  4037. }
  4038. #if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
  4039. if (streams & AC_SUPFMT_FLOAT32) {
  4040. formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
  4041. if (!bps)
  4042. bps = 32;
  4043. }
  4044. #endif
  4045. if (streams == AC_SUPFMT_AC3) {
  4046. /* should be exclusive */
  4047. /* temporary hack: we have still no proper support
  4048. * for the direct AC3 stream...
  4049. */
  4050. formats |= SNDRV_PCM_FMTBIT_U8;
  4051. bps = 8;
  4052. }
  4053. if (formats == 0) {
  4054. codec_err(codec,
  4055. "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n",
  4056. nid, val,
  4057. (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
  4058. streams);
  4059. return -EIO;
  4060. }
  4061. if (formatsp)
  4062. *formatsp = formats;
  4063. if (bpsp)
  4064. *bpsp = bps;
  4065. }
  4066. return 0;
  4067. }
  4068. EXPORT_SYMBOL_GPL(snd_hda_query_supported_pcm);
  4069. /**
  4070. * snd_hda_is_supported_format - Check the validity of the format
  4071. * @codec: HD-audio codec
  4072. * @nid: NID to check
  4073. * @format: the HD-audio format value to check
  4074. *
  4075. * Check whether the given node supports the format value.
  4076. *
  4077. * Returns 1 if supported, 0 if not.
  4078. */
  4079. int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
  4080. unsigned int format)
  4081. {
  4082. int i;
  4083. unsigned int val = 0, rate, stream;
  4084. val = query_pcm_param(codec, nid);
  4085. if (!val)
  4086. return 0;
  4087. rate = format & 0xff00;
  4088. for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
  4089. if (rate_bits[i].hda_fmt == rate) {
  4090. if (val & (1 << i))
  4091. break;
  4092. return 0;
  4093. }
  4094. if (i >= AC_PAR_PCM_RATE_BITS)
  4095. return 0;
  4096. stream = query_stream_param(codec, nid);
  4097. if (!stream)
  4098. return 0;
  4099. if (stream & AC_SUPFMT_PCM) {
  4100. switch (format & 0xf0) {
  4101. case 0x00:
  4102. if (!(val & AC_SUPPCM_BITS_8))
  4103. return 0;
  4104. break;
  4105. case 0x10:
  4106. if (!(val & AC_SUPPCM_BITS_16))
  4107. return 0;
  4108. break;
  4109. case 0x20:
  4110. if (!(val & AC_SUPPCM_BITS_20))
  4111. return 0;
  4112. break;
  4113. case 0x30:
  4114. if (!(val & AC_SUPPCM_BITS_24))
  4115. return 0;
  4116. break;
  4117. case 0x40:
  4118. if (!(val & AC_SUPPCM_BITS_32))
  4119. return 0;
  4120. break;
  4121. default:
  4122. return 0;
  4123. }
  4124. } else {
  4125. /* FIXME: check for float32 and AC3? */
  4126. }
  4127. return 1;
  4128. }
  4129. EXPORT_SYMBOL_GPL(snd_hda_is_supported_format);
  4130. /*
  4131. * PCM stuff
  4132. */
  4133. static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
  4134. struct hda_codec *codec,
  4135. struct snd_pcm_substream *substream)
  4136. {
  4137. return 0;
  4138. }
  4139. static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
  4140. struct hda_codec *codec,
  4141. unsigned int stream_tag,
  4142. unsigned int format,
  4143. struct snd_pcm_substream *substream)
  4144. {
  4145. snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
  4146. return 0;
  4147. }
  4148. static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
  4149. struct hda_codec *codec,
  4150. struct snd_pcm_substream *substream)
  4151. {
  4152. snd_hda_codec_cleanup_stream(codec, hinfo->nid);
  4153. return 0;
  4154. }
  4155. static int set_pcm_default_values(struct hda_codec *codec,
  4156. struct hda_pcm_stream *info)
  4157. {
  4158. int err;
  4159. /* query support PCM information from the given NID */
  4160. if (info->nid && (!info->rates || !info->formats)) {
  4161. err = snd_hda_query_supported_pcm(codec, info->nid,
  4162. info->rates ? NULL : &info->rates,
  4163. info->formats ? NULL : &info->formats,
  4164. info->maxbps ? NULL : &info->maxbps);
  4165. if (err < 0)
  4166. return err;
  4167. }
  4168. if (info->ops.open == NULL)
  4169. info->ops.open = hda_pcm_default_open_close;
  4170. if (info->ops.close == NULL)
  4171. info->ops.close = hda_pcm_default_open_close;
  4172. if (info->ops.prepare == NULL) {
  4173. if (snd_BUG_ON(!info->nid))
  4174. return -EINVAL;
  4175. info->ops.prepare = hda_pcm_default_prepare;
  4176. }
  4177. if (info->ops.cleanup == NULL) {
  4178. if (snd_BUG_ON(!info->nid))
  4179. return -EINVAL;
  4180. info->ops.cleanup = hda_pcm_default_cleanup;
  4181. }
  4182. return 0;
  4183. }
  4184. /*
  4185. * codec prepare/cleanup entries
  4186. */
  4187. int snd_hda_codec_prepare(struct hda_codec *codec,
  4188. struct hda_pcm_stream *hinfo,
  4189. unsigned int stream,
  4190. unsigned int format,
  4191. struct snd_pcm_substream *substream)
  4192. {
  4193. int ret;
  4194. mutex_lock(&codec->bus->prepare_mutex);
  4195. ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
  4196. if (ret >= 0)
  4197. purify_inactive_streams(codec);
  4198. mutex_unlock(&codec->bus->prepare_mutex);
  4199. return ret;
  4200. }
  4201. EXPORT_SYMBOL_GPL(snd_hda_codec_prepare);
  4202. void snd_hda_codec_cleanup(struct hda_codec *codec,
  4203. struct hda_pcm_stream *hinfo,
  4204. struct snd_pcm_substream *substream)
  4205. {
  4206. mutex_lock(&codec->bus->prepare_mutex);
  4207. hinfo->ops.cleanup(hinfo, codec, substream);
  4208. mutex_unlock(&codec->bus->prepare_mutex);
  4209. }
  4210. EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup);
  4211. /* global */
  4212. const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
  4213. "Audio", "SPDIF", "HDMI", "Modem"
  4214. };
  4215. /*
  4216. * get the empty PCM device number to assign
  4217. */
  4218. static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
  4219. {
  4220. /* audio device indices; not linear to keep compatibility */
  4221. /* assigned to static slots up to dev#10; if more needed, assign
  4222. * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
  4223. */
  4224. static int audio_idx[HDA_PCM_NTYPES][5] = {
  4225. [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
  4226. [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
  4227. [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
  4228. [HDA_PCM_TYPE_MODEM] = { 6, -1 },
  4229. };
  4230. int i;
  4231. if (type >= HDA_PCM_NTYPES) {
  4232. dev_err(bus->card->dev, "Invalid PCM type %d\n", type);
  4233. return -EINVAL;
  4234. }
  4235. for (i = 0; audio_idx[type][i] >= 0; i++) {
  4236. #ifndef CONFIG_SND_DYNAMIC_MINORS
  4237. if (audio_idx[type][i] >= 8)
  4238. break;
  4239. #endif
  4240. if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
  4241. return audio_idx[type][i];
  4242. }
  4243. #ifdef CONFIG_SND_DYNAMIC_MINORS
  4244. /* non-fixed slots starting from 10 */
  4245. for (i = 10; i < 32; i++) {
  4246. if (!test_and_set_bit(i, bus->pcm_dev_bits))
  4247. return i;
  4248. }
  4249. #endif
  4250. dev_warn(bus->card->dev, "Too many %s devices\n",
  4251. snd_hda_pcm_type_name[type]);
  4252. #ifndef CONFIG_SND_DYNAMIC_MINORS
  4253. dev_warn(bus->card->dev,
  4254. "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
  4255. #endif
  4256. return -EAGAIN;
  4257. }
  4258. /*
  4259. * attach a new PCM stream
  4260. */
  4261. static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
  4262. {
  4263. struct hda_bus *bus = codec->bus;
  4264. struct hda_pcm_stream *info;
  4265. int stream, err;
  4266. if (snd_BUG_ON(!pcm->name))
  4267. return -EINVAL;
  4268. for (stream = 0; stream < 2; stream++) {
  4269. info = &pcm->stream[stream];
  4270. if (info->substreams) {
  4271. err = set_pcm_default_values(codec, info);
  4272. if (err < 0)
  4273. return err;
  4274. }
  4275. }
  4276. return bus->ops.attach_pcm(bus, codec, pcm);
  4277. }
  4278. /* assign all PCMs of the given codec */
  4279. int snd_hda_codec_build_pcms(struct hda_codec *codec)
  4280. {
  4281. unsigned int pcm;
  4282. int err;
  4283. if (!codec->num_pcms) {
  4284. if (!codec->patch_ops.build_pcms)
  4285. return 0;
  4286. err = codec->patch_ops.build_pcms(codec);
  4287. if (err < 0) {
  4288. codec_err(codec,
  4289. "cannot build PCMs for #%d (error %d)\n",
  4290. codec->addr, err);
  4291. err = snd_hda_codec_reset(codec);
  4292. if (err < 0) {
  4293. codec_err(codec,
  4294. "cannot revert codec\n");
  4295. return err;
  4296. }
  4297. }
  4298. }
  4299. for (pcm = 0; pcm < codec->num_pcms; pcm++) {
  4300. struct hda_pcm *cpcm = &codec->pcm_info[pcm];
  4301. int dev;
  4302. if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
  4303. continue; /* no substreams assigned */
  4304. if (!cpcm->pcm) {
  4305. dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
  4306. if (dev < 0)
  4307. continue; /* no fatal error */
  4308. cpcm->device = dev;
  4309. err = snd_hda_attach_pcm(codec, cpcm);
  4310. if (err < 0) {
  4311. codec_err(codec,
  4312. "cannot attach PCM stream %d for codec #%d\n",
  4313. dev, codec->addr);
  4314. continue; /* no fatal error */
  4315. }
  4316. }
  4317. }
  4318. return 0;
  4319. }
  4320. /**
  4321. * snd_hda_build_pcms - build PCM information
  4322. * @bus: the BUS
  4323. *
  4324. * Create PCM information for each codec included in the bus.
  4325. *
  4326. * The build_pcms codec patch is requested to set up codec->num_pcms and
  4327. * codec->pcm_info properly. The array is referred by the top-level driver
  4328. * to create its PCM instances.
  4329. * The allocated codec->pcm_info should be released in codec->patch_ops.free
  4330. * callback.
  4331. *
  4332. * At least, substreams, channels_min and channels_max must be filled for
  4333. * each stream. substreams = 0 indicates that the stream doesn't exist.
  4334. * When rates and/or formats are zero, the supported values are queried
  4335. * from the given nid. The nid is used also by the default ops.prepare
  4336. * and ops.cleanup callbacks.
  4337. *
  4338. * The driver needs to call ops.open in its open callback. Similarly,
  4339. * ops.close is supposed to be called in the close callback.
  4340. * ops.prepare should be called in the prepare or hw_params callback
  4341. * with the proper parameters for set up.
  4342. * ops.cleanup should be called in hw_free for clean up of streams.
  4343. *
  4344. * This function returns 0 if successful, or a negative error code.
  4345. */
  4346. int snd_hda_build_pcms(struct hda_bus *bus)
  4347. {
  4348. struct hda_codec *codec;
  4349. list_for_each_entry(codec, &bus->codec_list, list) {
  4350. int err = snd_hda_codec_build_pcms(codec);
  4351. if (err < 0)
  4352. return err;
  4353. }
  4354. return 0;
  4355. }
  4356. EXPORT_SYMBOL_GPL(snd_hda_build_pcms);
  4357. /**
  4358. * snd_hda_check_board_config - compare the current codec with the config table
  4359. * @codec: the HDA codec
  4360. * @num_configs: number of config enums
  4361. * @models: array of model name strings
  4362. * @tbl: configuration table, terminated by null entries
  4363. *
  4364. * Compares the modelname or PCI subsystem id of the current codec with the
  4365. * given configuration table. If a matching entry is found, returns its
  4366. * config value (supposed to be 0 or positive).
  4367. *
  4368. * If no entries are matching, the function returns a negative value.
  4369. */
  4370. int snd_hda_check_board_config(struct hda_codec *codec,
  4371. int num_configs, const char * const *models,
  4372. const struct snd_pci_quirk *tbl)
  4373. {
  4374. if (codec->modelname && models) {
  4375. int i;
  4376. for (i = 0; i < num_configs; i++) {
  4377. if (models[i] &&
  4378. !strcmp(codec->modelname, models[i])) {
  4379. codec_info(codec, "model '%s' is selected\n",
  4380. models[i]);
  4381. return i;
  4382. }
  4383. }
  4384. }
  4385. if (!codec->bus->pci || !tbl)
  4386. return -1;
  4387. tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
  4388. if (!tbl)
  4389. return -1;
  4390. if (tbl->value >= 0 && tbl->value < num_configs) {
  4391. #ifdef CONFIG_SND_DEBUG_VERBOSE
  4392. char tmp[10];
  4393. const char *model = NULL;
  4394. if (models)
  4395. model = models[tbl->value];
  4396. if (!model) {
  4397. sprintf(tmp, "#%d", tbl->value);
  4398. model = tmp;
  4399. }
  4400. codec_info(codec, "model '%s' is selected for config %x:%x (%s)\n",
  4401. model, tbl->subvendor, tbl->subdevice,
  4402. (tbl->name ? tbl->name : "Unknown device"));
  4403. #endif
  4404. return tbl->value;
  4405. }
  4406. return -1;
  4407. }
  4408. EXPORT_SYMBOL_GPL(snd_hda_check_board_config);
  4409. /**
  4410. * snd_hda_check_board_codec_sid_config - compare the current codec
  4411. subsystem ID with the
  4412. config table
  4413. This is important for Gateway notebooks with SB450 HDA Audio
  4414. where the vendor ID of the PCI device is:
  4415. ATI Technologies Inc SB450 HDA Audio [1002:437b]
  4416. and the vendor/subvendor are found only at the codec.
  4417. * @codec: the HDA codec
  4418. * @num_configs: number of config enums
  4419. * @models: array of model name strings
  4420. * @tbl: configuration table, terminated by null entries
  4421. *
  4422. * Compares the modelname or PCI subsystem id of the current codec with the
  4423. * given configuration table. If a matching entry is found, returns its
  4424. * config value (supposed to be 0 or positive).
  4425. *
  4426. * If no entries are matching, the function returns a negative value.
  4427. */
  4428. int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
  4429. int num_configs, const char * const *models,
  4430. const struct snd_pci_quirk *tbl)
  4431. {
  4432. const struct snd_pci_quirk *q;
  4433. /* Search for codec ID */
  4434. for (q = tbl; q->subvendor; q++) {
  4435. unsigned int mask = 0xffff0000 | q->subdevice_mask;
  4436. unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
  4437. if ((codec->subsystem_id & mask) == id)
  4438. break;
  4439. }
  4440. if (!q->subvendor)
  4441. return -1;
  4442. tbl = q;
  4443. if (tbl->value >= 0 && tbl->value < num_configs) {
  4444. #ifdef CONFIG_SND_DEBUG_VERBOSE
  4445. char tmp[10];
  4446. const char *model = NULL;
  4447. if (models)
  4448. model = models[tbl->value];
  4449. if (!model) {
  4450. sprintf(tmp, "#%d", tbl->value);
  4451. model = tmp;
  4452. }
  4453. codec_info(codec, "model '%s' is selected for config %x:%x (%s)\n",
  4454. model, tbl->subvendor, tbl->subdevice,
  4455. (tbl->name ? tbl->name : "Unknown device"));
  4456. #endif
  4457. return tbl->value;
  4458. }
  4459. return -1;
  4460. }
  4461. EXPORT_SYMBOL_GPL(snd_hda_check_board_codec_sid_config);
  4462. /**
  4463. * snd_hda_add_new_ctls - create controls from the array
  4464. * @codec: the HDA codec
  4465. * @knew: the array of struct snd_kcontrol_new
  4466. *
  4467. * This helper function creates and add new controls in the given array.
  4468. * The array must be terminated with an empty entry as terminator.
  4469. *
  4470. * Returns 0 if successful, or a negative error code.
  4471. */
  4472. int snd_hda_add_new_ctls(struct hda_codec *codec,
  4473. const struct snd_kcontrol_new *knew)
  4474. {
  4475. int err;
  4476. for (; knew->name; knew++) {
  4477. struct snd_kcontrol *kctl;
  4478. int addr = 0, idx = 0;
  4479. if (knew->iface == -1) /* skip this codec private value */
  4480. continue;
  4481. for (;;) {
  4482. kctl = snd_ctl_new1(knew, codec);
  4483. if (!kctl)
  4484. return -ENOMEM;
  4485. if (addr > 0)
  4486. kctl->id.device = addr;
  4487. if (idx > 0)
  4488. kctl->id.index = idx;
  4489. err = snd_hda_ctl_add(codec, 0, kctl);
  4490. if (!err)
  4491. break;
  4492. /* try first with another device index corresponding to
  4493. * the codec addr; if it still fails (or it's the
  4494. * primary codec), then try another control index
  4495. */
  4496. if (!addr && codec->addr)
  4497. addr = codec->addr;
  4498. else if (!idx && !knew->index) {
  4499. idx = find_empty_mixer_ctl_idx(codec,
  4500. knew->name, 0);
  4501. if (idx <= 0)
  4502. return err;
  4503. } else
  4504. return err;
  4505. }
  4506. }
  4507. return 0;
  4508. }
  4509. EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls);
  4510. #ifdef CONFIG_PM
  4511. static void hda_power_work(struct work_struct *work)
  4512. {
  4513. struct hda_codec *codec =
  4514. container_of(work, struct hda_codec, power_work.work);
  4515. struct hda_bus *bus = codec->bus;
  4516. unsigned int state;
  4517. spin_lock(&codec->power_lock);
  4518. if (codec->power_transition > 0) { /* during power-up sequence? */
  4519. spin_unlock(&codec->power_lock);
  4520. return;
  4521. }
  4522. if (!codec->power_on || codec->power_count) {
  4523. codec->power_transition = 0;
  4524. spin_unlock(&codec->power_lock);
  4525. return;
  4526. }
  4527. spin_unlock(&codec->power_lock);
  4528. state = hda_call_codec_suspend(codec, true);
  4529. if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK))
  4530. hda_call_pm_notify(codec, false);
  4531. }
  4532. static void hda_keep_power_on(struct hda_codec *codec)
  4533. {
  4534. spin_lock(&codec->power_lock);
  4535. codec->power_count++;
  4536. codec->power_on = 1;
  4537. codec->power_jiffies = jiffies;
  4538. spin_unlock(&codec->power_lock);
  4539. hda_call_pm_notify(codec, true);
  4540. }
  4541. /* update the power on/off account with the current jiffies */
  4542. void snd_hda_update_power_acct(struct hda_codec *codec)
  4543. {
  4544. unsigned long delta = jiffies - codec->power_jiffies;
  4545. if (codec->power_on)
  4546. codec->power_on_acct += delta;
  4547. else
  4548. codec->power_off_acct += delta;
  4549. codec->power_jiffies += delta;
  4550. }
  4551. /* Transition to powered up, if wait_power_down then wait for a pending
  4552. * transition to D3 to complete. A pending D3 transition is indicated
  4553. * with power_transition == -1. */
  4554. /* call this with codec->power_lock held! */
  4555. static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
  4556. {
  4557. /* Return if power_on or transitioning to power_on, unless currently
  4558. * powering down. */
  4559. if ((codec->power_on || codec->power_transition > 0) &&
  4560. !(wait_power_down && codec->power_transition < 0))
  4561. return;
  4562. spin_unlock(&codec->power_lock);
  4563. cancel_delayed_work_sync(&codec->power_work);
  4564. spin_lock(&codec->power_lock);
  4565. /* If the power down delayed work was cancelled above before starting,
  4566. * then there is no need to go through power up here.
  4567. */
  4568. if (codec->power_on) {
  4569. if (codec->power_transition < 0)
  4570. codec->power_transition = 0;
  4571. return;
  4572. }
  4573. trace_hda_power_up(codec);
  4574. snd_hda_update_power_acct(codec);
  4575. codec->power_on = 1;
  4576. codec->power_jiffies = jiffies;
  4577. codec->power_transition = 1; /* avoid reentrance */
  4578. spin_unlock(&codec->power_lock);
  4579. hda_call_codec_resume(codec);
  4580. spin_lock(&codec->power_lock);
  4581. codec->power_transition = 0;
  4582. }
  4583. #define power_save(codec) \
  4584. ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
  4585. /* Transition to powered down */
  4586. static void __snd_hda_power_down(struct hda_codec *codec)
  4587. {
  4588. if (!codec->power_on || codec->power_count || codec->power_transition)
  4589. return;
  4590. if (power_save(codec)) {
  4591. codec->power_transition = -1; /* avoid reentrance */
  4592. queue_delayed_work(codec->bus->workq, &codec->power_work,
  4593. msecs_to_jiffies(power_save(codec) * 1000));
  4594. }
  4595. }
  4596. /**
  4597. * snd_hda_power_save - Power-up/down/sync the codec
  4598. * @codec: HD-audio codec
  4599. * @delta: the counter delta to change
  4600. *
  4601. * Change the power-up counter via @delta, and power up or down the hardware
  4602. * appropriately. For the power-down, queue to the delayed action.
  4603. * Passing zero to @delta means to synchronize the power state.
  4604. */
  4605. void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
  4606. {
  4607. spin_lock(&codec->power_lock);
  4608. codec->power_count += delta;
  4609. trace_hda_power_count(codec);
  4610. if (delta > 0)
  4611. __snd_hda_power_up(codec, d3wait);
  4612. else
  4613. __snd_hda_power_down(codec);
  4614. spin_unlock(&codec->power_lock);
  4615. }
  4616. EXPORT_SYMBOL_GPL(snd_hda_power_save);
  4617. /**
  4618. * snd_hda_check_amp_list_power - Check the amp list and update the power
  4619. * @codec: HD-audio codec
  4620. * @check: the object containing an AMP list and the status
  4621. * @nid: NID to check / update
  4622. *
  4623. * Check whether the given NID is in the amp list. If it's in the list,
  4624. * check the current AMP status, and update the the power-status according
  4625. * to the mute status.
  4626. *
  4627. * This function is supposed to be set or called from the check_power_status
  4628. * patch ops.
  4629. */
  4630. int snd_hda_check_amp_list_power(struct hda_codec *codec,
  4631. struct hda_loopback_check *check,
  4632. hda_nid_t nid)
  4633. {
  4634. const struct hda_amp_list *p;
  4635. int ch, v;
  4636. if (!check->amplist)
  4637. return 0;
  4638. for (p = check->amplist; p->nid; p++) {
  4639. if (p->nid == nid)
  4640. break;
  4641. }
  4642. if (!p->nid)
  4643. return 0; /* nothing changed */
  4644. for (p = check->amplist; p->nid; p++) {
  4645. for (ch = 0; ch < 2; ch++) {
  4646. v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
  4647. p->idx);
  4648. if (!(v & HDA_AMP_MUTE) && v > 0) {
  4649. if (!check->power_on) {
  4650. check->power_on = 1;
  4651. snd_hda_power_up(codec);
  4652. }
  4653. return 1;
  4654. }
  4655. }
  4656. }
  4657. if (check->power_on) {
  4658. check->power_on = 0;
  4659. snd_hda_power_down(codec);
  4660. }
  4661. return 0;
  4662. }
  4663. EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power);
  4664. #endif
  4665. /*
  4666. * Channel mode helper
  4667. */
  4668. /**
  4669. * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
  4670. */
  4671. int snd_hda_ch_mode_info(struct hda_codec *codec,
  4672. struct snd_ctl_elem_info *uinfo,
  4673. const struct hda_channel_mode *chmode,
  4674. int num_chmodes)
  4675. {
  4676. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  4677. uinfo->count = 1;
  4678. uinfo->value.enumerated.items = num_chmodes;
  4679. if (uinfo->value.enumerated.item >= num_chmodes)
  4680. uinfo->value.enumerated.item = num_chmodes - 1;
  4681. sprintf(uinfo->value.enumerated.name, "%dch",
  4682. chmode[uinfo->value.enumerated.item].channels);
  4683. return 0;
  4684. }
  4685. EXPORT_SYMBOL_GPL(snd_hda_ch_mode_info);
  4686. /**
  4687. * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
  4688. */
  4689. int snd_hda_ch_mode_get(struct hda_codec *codec,
  4690. struct snd_ctl_elem_value *ucontrol,
  4691. const struct hda_channel_mode *chmode,
  4692. int num_chmodes,
  4693. int max_channels)
  4694. {
  4695. int i;
  4696. for (i = 0; i < num_chmodes; i++) {
  4697. if (max_channels == chmode[i].channels) {
  4698. ucontrol->value.enumerated.item[0] = i;
  4699. break;
  4700. }
  4701. }
  4702. return 0;
  4703. }
  4704. EXPORT_SYMBOL_GPL(snd_hda_ch_mode_get);
  4705. /**
  4706. * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
  4707. */
  4708. int snd_hda_ch_mode_put(struct hda_codec *codec,
  4709. struct snd_ctl_elem_value *ucontrol,
  4710. const struct hda_channel_mode *chmode,
  4711. int num_chmodes,
  4712. int *max_channelsp)
  4713. {
  4714. unsigned int mode;
  4715. mode = ucontrol->value.enumerated.item[0];
  4716. if (mode >= num_chmodes)
  4717. return -EINVAL;
  4718. if (*max_channelsp == chmode[mode].channels)
  4719. return 0;
  4720. /* change the current channel setting */
  4721. *max_channelsp = chmode[mode].channels;
  4722. if (chmode[mode].sequence)
  4723. snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
  4724. return 1;
  4725. }
  4726. EXPORT_SYMBOL_GPL(snd_hda_ch_mode_put);
  4727. /*
  4728. * input MUX helper
  4729. */
  4730. /**
  4731. * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
  4732. */
  4733. int snd_hda_input_mux_info(const struct hda_input_mux *imux,
  4734. struct snd_ctl_elem_info *uinfo)
  4735. {
  4736. unsigned int index;
  4737. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  4738. uinfo->count = 1;
  4739. uinfo->value.enumerated.items = imux->num_items;
  4740. if (!imux->num_items)
  4741. return 0;
  4742. index = uinfo->value.enumerated.item;
  4743. if (index >= imux->num_items)
  4744. index = imux->num_items - 1;
  4745. strcpy(uinfo->value.enumerated.name, imux->items[index].label);
  4746. return 0;
  4747. }
  4748. EXPORT_SYMBOL_GPL(snd_hda_input_mux_info);
  4749. /**
  4750. * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
  4751. */
  4752. int snd_hda_input_mux_put(struct hda_codec *codec,
  4753. const struct hda_input_mux *imux,
  4754. struct snd_ctl_elem_value *ucontrol,
  4755. hda_nid_t nid,
  4756. unsigned int *cur_val)
  4757. {
  4758. unsigned int idx;
  4759. if (!imux->num_items)
  4760. return 0;
  4761. idx = ucontrol->value.enumerated.item[0];
  4762. if (idx >= imux->num_items)
  4763. idx = imux->num_items - 1;
  4764. if (*cur_val == idx)
  4765. return 0;
  4766. snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
  4767. imux->items[idx].index);
  4768. *cur_val = idx;
  4769. return 1;
  4770. }
  4771. EXPORT_SYMBOL_GPL(snd_hda_input_mux_put);
  4772. /*
  4773. * process kcontrol info callback of a simple string enum array
  4774. * when @num_items is 0 or @texts is NULL, assume a boolean enum array
  4775. */
  4776. int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
  4777. struct snd_ctl_elem_info *uinfo,
  4778. int num_items, const char * const *texts)
  4779. {
  4780. static const char * const texts_default[] = {
  4781. "Disabled", "Enabled"
  4782. };
  4783. if (!texts || !num_items) {
  4784. num_items = 2;
  4785. texts = texts_default;
  4786. }
  4787. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  4788. uinfo->count = 1;
  4789. uinfo->value.enumerated.items = num_items;
  4790. if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
  4791. uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
  4792. strcpy(uinfo->value.enumerated.name,
  4793. texts[uinfo->value.enumerated.item]);
  4794. return 0;
  4795. }
  4796. EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info);
  4797. /*
  4798. * Multi-channel / digital-out PCM helper functions
  4799. */
  4800. /* setup SPDIF output stream */
  4801. static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
  4802. unsigned int stream_tag, unsigned int format)
  4803. {
  4804. struct hda_spdif_out *spdif;
  4805. unsigned int curr_fmt;
  4806. bool reset;
  4807. spdif = snd_hda_spdif_out_of_nid(codec, nid);
  4808. curr_fmt = snd_hda_codec_read(codec, nid, 0,
  4809. AC_VERB_GET_STREAM_FORMAT, 0);
  4810. reset = codec->spdif_status_reset &&
  4811. (spdif->ctls & AC_DIG1_ENABLE) &&
  4812. curr_fmt != format;
  4813. /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
  4814. updated */
  4815. if (reset)
  4816. set_dig_out_convert(codec, nid,
  4817. spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
  4818. -1);
  4819. snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
  4820. if (codec->slave_dig_outs) {
  4821. const hda_nid_t *d;
  4822. for (d = codec->slave_dig_outs; *d; d++)
  4823. snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
  4824. format);
  4825. }
  4826. /* turn on again (if needed) */
  4827. if (reset)
  4828. set_dig_out_convert(codec, nid,
  4829. spdif->ctls & 0xff, -1);
  4830. }
  4831. static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
  4832. {
  4833. snd_hda_codec_cleanup_stream(codec, nid);
  4834. if (codec->slave_dig_outs) {
  4835. const hda_nid_t *d;
  4836. for (d = codec->slave_dig_outs; *d; d++)
  4837. snd_hda_codec_cleanup_stream(codec, *d);
  4838. }
  4839. }
  4840. /**
  4841. * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
  4842. * @bus: HD-audio bus
  4843. */
  4844. void snd_hda_bus_reboot_notify(struct hda_bus *bus)
  4845. {
  4846. struct hda_codec *codec;
  4847. if (!bus)
  4848. return;
  4849. list_for_each_entry(codec, &bus->codec_list, list) {
  4850. if (hda_codec_is_power_on(codec) &&
  4851. codec->patch_ops.reboot_notify)
  4852. codec->patch_ops.reboot_notify(codec);
  4853. }
  4854. }
  4855. EXPORT_SYMBOL_GPL(snd_hda_bus_reboot_notify);
  4856. /**
  4857. * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
  4858. */
  4859. int snd_hda_multi_out_dig_open(struct hda_codec *codec,
  4860. struct hda_multi_out *mout)
  4861. {
  4862. mutex_lock(&codec->spdif_mutex);
  4863. if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
  4864. /* already opened as analog dup; reset it once */
  4865. cleanup_dig_out_stream(codec, mout->dig_out_nid);
  4866. mout->dig_out_used = HDA_DIG_EXCLUSIVE;
  4867. mutex_unlock(&codec->spdif_mutex);
  4868. return 0;
  4869. }
  4870. EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open);
  4871. /**
  4872. * snd_hda_multi_out_dig_prepare - prepare the digital out stream
  4873. */
  4874. int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
  4875. struct hda_multi_out *mout,
  4876. unsigned int stream_tag,
  4877. unsigned int format,
  4878. struct snd_pcm_substream *substream)
  4879. {
  4880. mutex_lock(&codec->spdif_mutex);
  4881. setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
  4882. mutex_unlock(&codec->spdif_mutex);
  4883. return 0;
  4884. }
  4885. EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare);
  4886. /**
  4887. * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
  4888. */
  4889. int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
  4890. struct hda_multi_out *mout)
  4891. {
  4892. mutex_lock(&codec->spdif_mutex);
  4893. cleanup_dig_out_stream(codec, mout->dig_out_nid);
  4894. mutex_unlock(&codec->spdif_mutex);
  4895. return 0;
  4896. }
  4897. EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup);
  4898. /**
  4899. * snd_hda_multi_out_dig_close - release the digital out stream
  4900. */
  4901. int snd_hda_multi_out_dig_close(struct hda_codec *codec,
  4902. struct hda_multi_out *mout)
  4903. {
  4904. mutex_lock(&codec->spdif_mutex);
  4905. mout->dig_out_used = 0;
  4906. mutex_unlock(&codec->spdif_mutex);
  4907. return 0;
  4908. }
  4909. EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close);
  4910. /**
  4911. * snd_hda_multi_out_analog_open - open analog outputs
  4912. *
  4913. * Open analog outputs and set up the hw-constraints.
  4914. * If the digital outputs can be opened as slave, open the digital
  4915. * outputs, too.
  4916. */
  4917. int snd_hda_multi_out_analog_open(struct hda_codec *codec,
  4918. struct hda_multi_out *mout,
  4919. struct snd_pcm_substream *substream,
  4920. struct hda_pcm_stream *hinfo)
  4921. {
  4922. struct snd_pcm_runtime *runtime = substream->runtime;
  4923. runtime->hw.channels_max = mout->max_channels;
  4924. if (mout->dig_out_nid) {
  4925. if (!mout->analog_rates) {
  4926. mout->analog_rates = hinfo->rates;
  4927. mout->analog_formats = hinfo->formats;
  4928. mout->analog_maxbps = hinfo->maxbps;
  4929. } else {
  4930. runtime->hw.rates = mout->analog_rates;
  4931. runtime->hw.formats = mout->analog_formats;
  4932. hinfo->maxbps = mout->analog_maxbps;
  4933. }
  4934. if (!mout->spdif_rates) {
  4935. snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
  4936. &mout->spdif_rates,
  4937. &mout->spdif_formats,
  4938. &mout->spdif_maxbps);
  4939. }
  4940. mutex_lock(&codec->spdif_mutex);
  4941. if (mout->share_spdif) {
  4942. if ((runtime->hw.rates & mout->spdif_rates) &&
  4943. (runtime->hw.formats & mout->spdif_formats)) {
  4944. runtime->hw.rates &= mout->spdif_rates;
  4945. runtime->hw.formats &= mout->spdif_formats;
  4946. if (mout->spdif_maxbps < hinfo->maxbps)
  4947. hinfo->maxbps = mout->spdif_maxbps;
  4948. } else {
  4949. mout->share_spdif = 0;
  4950. /* FIXME: need notify? */
  4951. }
  4952. }
  4953. mutex_unlock(&codec->spdif_mutex);
  4954. }
  4955. return snd_pcm_hw_constraint_step(substream->runtime, 0,
  4956. SNDRV_PCM_HW_PARAM_CHANNELS, 2);
  4957. }
  4958. EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open);
  4959. /**
  4960. * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
  4961. *
  4962. * Set up the i/o for analog out.
  4963. * When the digital out is available, copy the front out to digital out, too.
  4964. */
  4965. int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
  4966. struct hda_multi_out *mout,
  4967. unsigned int stream_tag,
  4968. unsigned int format,
  4969. struct snd_pcm_substream *substream)
  4970. {
  4971. const hda_nid_t *nids = mout->dac_nids;
  4972. int chs = substream->runtime->channels;
  4973. struct hda_spdif_out *spdif;
  4974. int i;
  4975. mutex_lock(&codec->spdif_mutex);
  4976. spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
  4977. if (mout->dig_out_nid && mout->share_spdif &&
  4978. mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
  4979. if (chs == 2 &&
  4980. snd_hda_is_supported_format(codec, mout->dig_out_nid,
  4981. format) &&
  4982. !(spdif->status & IEC958_AES0_NONAUDIO)) {
  4983. mout->dig_out_used = HDA_DIG_ANALOG_DUP;
  4984. setup_dig_out_stream(codec, mout->dig_out_nid,
  4985. stream_tag, format);
  4986. } else {
  4987. mout->dig_out_used = 0;
  4988. cleanup_dig_out_stream(codec, mout->dig_out_nid);
  4989. }
  4990. }
  4991. mutex_unlock(&codec->spdif_mutex);
  4992. /* front */
  4993. snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
  4994. 0, format);
  4995. if (!mout->no_share_stream &&
  4996. mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
  4997. /* headphone out will just decode front left/right (stereo) */
  4998. snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
  4999. 0, format);
  5000. /* extra outputs copied from front */
  5001. for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
  5002. if (!mout->no_share_stream && mout->hp_out_nid[i])
  5003. snd_hda_codec_setup_stream(codec,
  5004. mout->hp_out_nid[i],
  5005. stream_tag, 0, format);
  5006. /* surrounds */
  5007. for (i = 1; i < mout->num_dacs; i++) {
  5008. if (chs >= (i + 1) * 2) /* independent out */
  5009. snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
  5010. i * 2, format);
  5011. else if (!mout->no_share_stream) /* copy front */
  5012. snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
  5013. 0, format);
  5014. }
  5015. /* extra surrounds */
  5016. for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
  5017. int ch = 0;
  5018. if (!mout->extra_out_nid[i])
  5019. break;
  5020. if (chs >= (i + 1) * 2)
  5021. ch = i * 2;
  5022. else if (!mout->no_share_stream)
  5023. break;
  5024. snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
  5025. stream_tag, ch, format);
  5026. }
  5027. return 0;
  5028. }
  5029. EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare);
  5030. /**
  5031. * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
  5032. */
  5033. int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
  5034. struct hda_multi_out *mout)
  5035. {
  5036. const hda_nid_t *nids = mout->dac_nids;
  5037. int i;
  5038. for (i = 0; i < mout->num_dacs; i++)
  5039. snd_hda_codec_cleanup_stream(codec, nids[i]);
  5040. if (mout->hp_nid)
  5041. snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
  5042. for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
  5043. if (mout->hp_out_nid[i])
  5044. snd_hda_codec_cleanup_stream(codec,
  5045. mout->hp_out_nid[i]);
  5046. for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
  5047. if (mout->extra_out_nid[i])
  5048. snd_hda_codec_cleanup_stream(codec,
  5049. mout->extra_out_nid[i]);
  5050. mutex_lock(&codec->spdif_mutex);
  5051. if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
  5052. cleanup_dig_out_stream(codec, mout->dig_out_nid);
  5053. mout->dig_out_used = 0;
  5054. }
  5055. mutex_unlock(&codec->spdif_mutex);
  5056. return 0;
  5057. }
  5058. EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup);
  5059. /**
  5060. * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
  5061. *
  5062. * Guess the suitable VREF pin bits to be set as the pin-control value.
  5063. * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
  5064. */
  5065. unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
  5066. {
  5067. unsigned int pincap;
  5068. unsigned int oldval;
  5069. oldval = snd_hda_codec_read(codec, pin, 0,
  5070. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  5071. pincap = snd_hda_query_pin_caps(codec, pin);
  5072. pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
  5073. /* Exception: if the default pin setup is vref50, we give it priority */
  5074. if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
  5075. return AC_PINCTL_VREF_80;
  5076. else if (pincap & AC_PINCAP_VREF_50)
  5077. return AC_PINCTL_VREF_50;
  5078. else if (pincap & AC_PINCAP_VREF_100)
  5079. return AC_PINCTL_VREF_100;
  5080. else if (pincap & AC_PINCAP_VREF_GRD)
  5081. return AC_PINCTL_VREF_GRD;
  5082. return AC_PINCTL_VREF_HIZ;
  5083. }
  5084. EXPORT_SYMBOL_GPL(snd_hda_get_default_vref);
  5085. /* correct the pin ctl value for matching with the pin cap */
  5086. unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
  5087. hda_nid_t pin, unsigned int val)
  5088. {
  5089. static unsigned int cap_lists[][2] = {
  5090. { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
  5091. { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
  5092. { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
  5093. { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
  5094. };
  5095. unsigned int cap;
  5096. if (!val)
  5097. return 0;
  5098. cap = snd_hda_query_pin_caps(codec, pin);
  5099. if (!cap)
  5100. return val; /* don't know what to do... */
  5101. if (val & AC_PINCTL_OUT_EN) {
  5102. if (!(cap & AC_PINCAP_OUT))
  5103. val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
  5104. else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
  5105. val &= ~AC_PINCTL_HP_EN;
  5106. }
  5107. if (val & AC_PINCTL_IN_EN) {
  5108. if (!(cap & AC_PINCAP_IN))
  5109. val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
  5110. else {
  5111. unsigned int vcap, vref;
  5112. int i;
  5113. vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
  5114. vref = val & AC_PINCTL_VREFEN;
  5115. for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
  5116. if (vref == cap_lists[i][0] &&
  5117. !(vcap & cap_lists[i][1])) {
  5118. if (i == ARRAY_SIZE(cap_lists) - 1)
  5119. vref = AC_PINCTL_VREF_HIZ;
  5120. else
  5121. vref = cap_lists[i + 1][0];
  5122. }
  5123. }
  5124. val &= ~AC_PINCTL_VREFEN;
  5125. val |= vref;
  5126. }
  5127. }
  5128. return val;
  5129. }
  5130. EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl);
  5131. int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
  5132. unsigned int val, bool cached)
  5133. {
  5134. val = snd_hda_correct_pin_ctl(codec, pin, val);
  5135. snd_hda_codec_set_pin_target(codec, pin, val);
  5136. if (cached)
  5137. return snd_hda_codec_update_cache(codec, pin, 0,
  5138. AC_VERB_SET_PIN_WIDGET_CONTROL, val);
  5139. else
  5140. return snd_hda_codec_write(codec, pin, 0,
  5141. AC_VERB_SET_PIN_WIDGET_CONTROL, val);
  5142. }
  5143. EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl);
  5144. /**
  5145. * snd_hda_add_imux_item - Add an item to input_mux
  5146. *
  5147. * When the same label is used already in the existing items, the number
  5148. * suffix is appended to the label. This label index number is stored
  5149. * to type_idx when non-NULL pointer is given.
  5150. */
  5151. int snd_hda_add_imux_item(struct hda_codec *codec,
  5152. struct hda_input_mux *imux, const char *label,
  5153. int index, int *type_idx)
  5154. {
  5155. int i, label_idx = 0;
  5156. if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
  5157. codec_err(codec, "hda_codec: Too many imux items!\n");
  5158. return -EINVAL;
  5159. }
  5160. for (i = 0; i < imux->num_items; i++) {
  5161. if (!strncmp(label, imux->items[i].label, strlen(label)))
  5162. label_idx++;
  5163. }
  5164. if (type_idx)
  5165. *type_idx = label_idx;
  5166. if (label_idx > 0)
  5167. snprintf(imux->items[imux->num_items].label,
  5168. sizeof(imux->items[imux->num_items].label),
  5169. "%s %d", label, label_idx);
  5170. else
  5171. strlcpy(imux->items[imux->num_items].label, label,
  5172. sizeof(imux->items[imux->num_items].label));
  5173. imux->items[imux->num_items].index = index;
  5174. imux->num_items++;
  5175. return 0;
  5176. }
  5177. EXPORT_SYMBOL_GPL(snd_hda_add_imux_item);
  5178. #ifdef CONFIG_PM
  5179. /*
  5180. * power management
  5181. */
  5182. static void hda_async_suspend(void *data, async_cookie_t cookie)
  5183. {
  5184. hda_call_codec_suspend(data, false);
  5185. }
  5186. static void hda_async_resume(void *data, async_cookie_t cookie)
  5187. {
  5188. hda_call_codec_resume(data);
  5189. }
  5190. /**
  5191. * snd_hda_suspend - suspend the codecs
  5192. * @bus: the HDA bus
  5193. *
  5194. * Returns 0 if successful.
  5195. */
  5196. int snd_hda_suspend(struct hda_bus *bus)
  5197. {
  5198. struct hda_codec *codec;
  5199. ASYNC_DOMAIN_EXCLUSIVE(domain);
  5200. list_for_each_entry(codec, &bus->codec_list, list) {
  5201. cancel_delayed_work_sync(&codec->jackpoll_work);
  5202. if (hda_codec_is_power_on(codec)) {
  5203. if (bus->num_codecs > 1)
  5204. async_schedule_domain(hda_async_suspend, codec,
  5205. &domain);
  5206. else
  5207. hda_call_codec_suspend(codec, false);
  5208. }
  5209. }
  5210. if (bus->num_codecs > 1)
  5211. async_synchronize_full_domain(&domain);
  5212. return 0;
  5213. }
  5214. EXPORT_SYMBOL_GPL(snd_hda_suspend);
  5215. /**
  5216. * snd_hda_resume - resume the codecs
  5217. * @bus: the HDA bus
  5218. *
  5219. * Returns 0 if successful.
  5220. */
  5221. int snd_hda_resume(struct hda_bus *bus)
  5222. {
  5223. struct hda_codec *codec;
  5224. ASYNC_DOMAIN_EXCLUSIVE(domain);
  5225. list_for_each_entry(codec, &bus->codec_list, list) {
  5226. if (bus->num_codecs > 1)
  5227. async_schedule_domain(hda_async_resume, codec, &domain);
  5228. else
  5229. hda_call_codec_resume(codec);
  5230. }
  5231. if (bus->num_codecs > 1)
  5232. async_synchronize_full_domain(&domain);
  5233. return 0;
  5234. }
  5235. EXPORT_SYMBOL_GPL(snd_hda_resume);
  5236. #endif /* CONFIG_PM */
  5237. /*
  5238. * generic arrays
  5239. */
  5240. /**
  5241. * snd_array_new - get a new element from the given array
  5242. * @array: the array object
  5243. *
  5244. * Get a new element from the given array. If it exceeds the
  5245. * pre-allocated array size, re-allocate the array.
  5246. *
  5247. * Returns NULL if allocation failed.
  5248. */
  5249. void *snd_array_new(struct snd_array *array)
  5250. {
  5251. if (snd_BUG_ON(!array->elem_size))
  5252. return NULL;
  5253. if (array->used >= array->alloced) {
  5254. int num = array->alloced + array->alloc_align;
  5255. int size = (num + 1) * array->elem_size;
  5256. void *nlist;
  5257. if (snd_BUG_ON(num >= 4096))
  5258. return NULL;
  5259. nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO);
  5260. if (!nlist)
  5261. return NULL;
  5262. array->list = nlist;
  5263. array->alloced = num;
  5264. }
  5265. return snd_array_elem(array, array->used++);
  5266. }
  5267. EXPORT_SYMBOL_GPL(snd_array_new);
  5268. /**
  5269. * snd_array_free - free the given array elements
  5270. * @array: the array object
  5271. */
  5272. void snd_array_free(struct snd_array *array)
  5273. {
  5274. kfree(array->list);
  5275. array->used = 0;
  5276. array->alloced = 0;
  5277. array->list = NULL;
  5278. }
  5279. EXPORT_SYMBOL_GPL(snd_array_free);
  5280. /**
  5281. * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
  5282. * @pcm: PCM caps bits
  5283. * @buf: the string buffer to write
  5284. * @buflen: the max buffer length
  5285. *
  5286. * used by hda_proc.c and hda_eld.c
  5287. */
  5288. void snd_print_pcm_bits(int pcm, char *buf, int buflen)
  5289. {
  5290. static unsigned int bits[] = { 8, 16, 20, 24, 32 };
  5291. int i, j;
  5292. for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
  5293. if (pcm & (AC_SUPPCM_BITS_8 << i))
  5294. j += snprintf(buf + j, buflen - j, " %d", bits[i]);
  5295. buf[j] = '\0'; /* necessary when j == 0 */
  5296. }
  5297. EXPORT_SYMBOL_GPL(snd_print_pcm_bits);
  5298. MODULE_DESCRIPTION("HDA codec core");
  5299. MODULE_LICENSE("GPL");