hda_codec.c 151 KB

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