hda_codec.c 153 KB

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