patch_ca0132.c 209 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716
  1. /*
  2. * HD audio interface patch for Creative CA0132 chip
  3. *
  4. * Copyright (c) 2011, Creative Technology Ltd.
  5. *
  6. * Based on patch_ca0110.c
  7. * Copyright (c) 2008 Takashi Iwai <tiwai@suse.de>
  8. *
  9. * This driver is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This driver is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. #include <linux/init.h>
  24. #include <linux/delay.h>
  25. #include <linux/slab.h>
  26. #include <linux/mutex.h>
  27. #include <linux/module.h>
  28. #include <linux/firmware.h>
  29. #include <linux/kernel.h>
  30. #include <linux/types.h>
  31. #include <linux/io.h>
  32. #include <linux/pci.h>
  33. #include <asm/io.h>
  34. #include <sound/core.h>
  35. #include "hda_codec.h"
  36. #include "hda_local.h"
  37. #include "hda_auto_parser.h"
  38. #include "hda_jack.h"
  39. #include "ca0132_regs.h"
  40. /* Enable this to see controls for tuning purpose. */
  41. /*#define ENABLE_TUNING_CONTROLS*/
  42. #ifdef ENABLE_TUNING_CONTROLS
  43. #include <sound/tlv.h>
  44. #endif
  45. #define FLOAT_ZERO 0x00000000
  46. #define FLOAT_ONE 0x3f800000
  47. #define FLOAT_TWO 0x40000000
  48. #define FLOAT_THREE 0x40400000
  49. #define FLOAT_EIGHT 0x41000000
  50. #define FLOAT_MINUS_5 0xc0a00000
  51. #define UNSOL_TAG_DSP 0x16
  52. #define DSP_DMA_WRITE_BUFLEN_INIT (1UL<<18)
  53. #define DSP_DMA_WRITE_BUFLEN_OVLY (1UL<<15)
  54. #define DMA_TRANSFER_FRAME_SIZE_NWORDS 8
  55. #define DMA_TRANSFER_MAX_FRAME_SIZE_NWORDS 32
  56. #define DMA_OVERLAY_FRAME_SIZE_NWORDS 2
  57. #define MASTERCONTROL 0x80
  58. #define MASTERCONTROL_ALLOC_DMA_CHAN 10
  59. #define MASTERCONTROL_QUERY_SPEAKER_EQ_ADDRESS 60
  60. #define WIDGET_CHIP_CTRL 0x15
  61. #define WIDGET_DSP_CTRL 0x16
  62. #define MEM_CONNID_MICIN1 3
  63. #define MEM_CONNID_MICIN2 5
  64. #define MEM_CONNID_MICOUT1 12
  65. #define MEM_CONNID_MICOUT2 14
  66. #define MEM_CONNID_WUH 10
  67. #define MEM_CONNID_DSP 16
  68. #define MEM_CONNID_DMIC 100
  69. #define SCP_SET 0
  70. #define SCP_GET 1
  71. #define EFX_FILE "ctefx.bin"
  72. #define SBZ_EFX_FILE "ctefx-sbz.bin"
  73. #define R3DI_EFX_FILE "ctefx-r3di.bin"
  74. #ifdef CONFIG_SND_HDA_CODEC_CA0132_DSP
  75. MODULE_FIRMWARE(EFX_FILE);
  76. MODULE_FIRMWARE(SBZ_EFX_FILE);
  77. MODULE_FIRMWARE(R3DI_EFX_FILE);
  78. #endif
  79. static const char *const dirstr[2] = { "Playback", "Capture" };
  80. #define NUM_OF_OUTPUTS 3
  81. enum {
  82. SPEAKER_OUT,
  83. HEADPHONE_OUT,
  84. SURROUND_OUT
  85. };
  86. enum {
  87. DIGITAL_MIC,
  88. LINE_MIC_IN
  89. };
  90. /* Strings for Input Source Enum Control */
  91. static const char *const in_src_str[3] = { "Microphone", "Line In", "Front Microphone" };
  92. #define IN_SRC_NUM_OF_INPUTS 3
  93. enum {
  94. REAR_MIC,
  95. REAR_LINE_IN,
  96. FRONT_MIC,
  97. };
  98. enum {
  99. #define VNODE_START_NID 0x80
  100. VNID_SPK = VNODE_START_NID, /* Speaker vnid */
  101. VNID_MIC,
  102. VNID_HP_SEL,
  103. VNID_AMIC1_SEL,
  104. VNID_HP_ASEL,
  105. VNID_AMIC1_ASEL,
  106. VNODE_END_NID,
  107. #define VNODES_COUNT (VNODE_END_NID - VNODE_START_NID)
  108. #define EFFECT_START_NID 0x90
  109. #define OUT_EFFECT_START_NID EFFECT_START_NID
  110. SURROUND = OUT_EFFECT_START_NID,
  111. CRYSTALIZER,
  112. DIALOG_PLUS,
  113. SMART_VOLUME,
  114. X_BASS,
  115. EQUALIZER,
  116. OUT_EFFECT_END_NID,
  117. #define OUT_EFFECTS_COUNT (OUT_EFFECT_END_NID - OUT_EFFECT_START_NID)
  118. #define IN_EFFECT_START_NID OUT_EFFECT_END_NID
  119. ECHO_CANCELLATION = IN_EFFECT_START_NID,
  120. VOICE_FOCUS,
  121. MIC_SVM,
  122. NOISE_REDUCTION,
  123. IN_EFFECT_END_NID,
  124. #define IN_EFFECTS_COUNT (IN_EFFECT_END_NID - IN_EFFECT_START_NID)
  125. VOICEFX = IN_EFFECT_END_NID,
  126. PLAY_ENHANCEMENT,
  127. CRYSTAL_VOICE,
  128. EFFECT_END_NID,
  129. OUTPUT_SOURCE_ENUM,
  130. INPUT_SOURCE_ENUM,
  131. XBASS_XOVER,
  132. EQ_PRESET_ENUM,
  133. SMART_VOLUME_ENUM,
  134. MIC_BOOST_ENUM
  135. #define EFFECTS_COUNT (EFFECT_END_NID - EFFECT_START_NID)
  136. };
  137. /* Effects values size*/
  138. #define EFFECT_VALS_MAX_COUNT 12
  139. /*
  140. * Default values for the effect slider controls, they are in order of their
  141. * effect NID's. Surround, Crystalizer, Dialog Plus, Smart Volume, and then
  142. * X-bass.
  143. */
  144. static const unsigned int effect_slider_defaults[] = {67, 65, 50, 74, 50};
  145. /* Amount of effect level sliders for ca0132_alt controls. */
  146. #define EFFECT_LEVEL_SLIDERS 5
  147. /* Latency introduced by DSP blocks in milliseconds. */
  148. #define DSP_CAPTURE_INIT_LATENCY 0
  149. #define DSP_CRYSTAL_VOICE_LATENCY 124
  150. #define DSP_PLAYBACK_INIT_LATENCY 13
  151. #define DSP_PLAY_ENHANCEMENT_LATENCY 30
  152. #define DSP_SPEAKER_OUT_LATENCY 7
  153. struct ct_effect {
  154. char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
  155. hda_nid_t nid;
  156. int mid; /*effect module ID*/
  157. int reqs[EFFECT_VALS_MAX_COUNT]; /*effect module request*/
  158. int direct; /* 0:output; 1:input*/
  159. int params; /* number of default non-on/off params */
  160. /*effect default values, 1st is on/off. */
  161. unsigned int def_vals[EFFECT_VALS_MAX_COUNT];
  162. };
  163. #define EFX_DIR_OUT 0
  164. #define EFX_DIR_IN 1
  165. static const struct ct_effect ca0132_effects[EFFECTS_COUNT] = {
  166. { .name = "Surround",
  167. .nid = SURROUND,
  168. .mid = 0x96,
  169. .reqs = {0, 1},
  170. .direct = EFX_DIR_OUT,
  171. .params = 1,
  172. .def_vals = {0x3F800000, 0x3F2B851F}
  173. },
  174. { .name = "Crystalizer",
  175. .nid = CRYSTALIZER,
  176. .mid = 0x96,
  177. .reqs = {7, 8},
  178. .direct = EFX_DIR_OUT,
  179. .params = 1,
  180. .def_vals = {0x3F800000, 0x3F266666}
  181. },
  182. { .name = "Dialog Plus",
  183. .nid = DIALOG_PLUS,
  184. .mid = 0x96,
  185. .reqs = {2, 3},
  186. .direct = EFX_DIR_OUT,
  187. .params = 1,
  188. .def_vals = {0x00000000, 0x3F000000}
  189. },
  190. { .name = "Smart Volume",
  191. .nid = SMART_VOLUME,
  192. .mid = 0x96,
  193. .reqs = {4, 5, 6},
  194. .direct = EFX_DIR_OUT,
  195. .params = 2,
  196. .def_vals = {0x3F800000, 0x3F3D70A4, 0x00000000}
  197. },
  198. { .name = "X-Bass",
  199. .nid = X_BASS,
  200. .mid = 0x96,
  201. .reqs = {24, 23, 25},
  202. .direct = EFX_DIR_OUT,
  203. .params = 2,
  204. .def_vals = {0x3F800000, 0x42A00000, 0x3F000000}
  205. },
  206. { .name = "Equalizer",
  207. .nid = EQUALIZER,
  208. .mid = 0x96,
  209. .reqs = {9, 10, 11, 12, 13, 14,
  210. 15, 16, 17, 18, 19, 20},
  211. .direct = EFX_DIR_OUT,
  212. .params = 11,
  213. .def_vals = {0x00000000, 0x00000000, 0x00000000, 0x00000000,
  214. 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  215. 0x00000000, 0x00000000, 0x00000000, 0x00000000}
  216. },
  217. { .name = "Echo Cancellation",
  218. .nid = ECHO_CANCELLATION,
  219. .mid = 0x95,
  220. .reqs = {0, 1, 2, 3},
  221. .direct = EFX_DIR_IN,
  222. .params = 3,
  223. .def_vals = {0x00000000, 0x3F3A9692, 0x00000000, 0x00000000}
  224. },
  225. { .name = "Voice Focus",
  226. .nid = VOICE_FOCUS,
  227. .mid = 0x95,
  228. .reqs = {6, 7, 8, 9},
  229. .direct = EFX_DIR_IN,
  230. .params = 3,
  231. .def_vals = {0x3F800000, 0x3D7DF3B6, 0x41F00000, 0x41F00000}
  232. },
  233. { .name = "Mic SVM",
  234. .nid = MIC_SVM,
  235. .mid = 0x95,
  236. .reqs = {44, 45},
  237. .direct = EFX_DIR_IN,
  238. .params = 1,
  239. .def_vals = {0x00000000, 0x3F3D70A4}
  240. },
  241. { .name = "Noise Reduction",
  242. .nid = NOISE_REDUCTION,
  243. .mid = 0x95,
  244. .reqs = {4, 5},
  245. .direct = EFX_DIR_IN,
  246. .params = 1,
  247. .def_vals = {0x3F800000, 0x3F000000}
  248. },
  249. { .name = "VoiceFX",
  250. .nid = VOICEFX,
  251. .mid = 0x95,
  252. .reqs = {10, 11, 12, 13, 14, 15, 16, 17, 18},
  253. .direct = EFX_DIR_IN,
  254. .params = 8,
  255. .def_vals = {0x00000000, 0x43C80000, 0x44AF0000, 0x44FA0000,
  256. 0x3F800000, 0x3F800000, 0x3F800000, 0x00000000,
  257. 0x00000000}
  258. }
  259. };
  260. /* Tuning controls */
  261. #ifdef ENABLE_TUNING_CONTROLS
  262. enum {
  263. #define TUNING_CTL_START_NID 0xC0
  264. WEDGE_ANGLE = TUNING_CTL_START_NID,
  265. SVM_LEVEL,
  266. EQUALIZER_BAND_0,
  267. EQUALIZER_BAND_1,
  268. EQUALIZER_BAND_2,
  269. EQUALIZER_BAND_3,
  270. EQUALIZER_BAND_4,
  271. EQUALIZER_BAND_5,
  272. EQUALIZER_BAND_6,
  273. EQUALIZER_BAND_7,
  274. EQUALIZER_BAND_8,
  275. EQUALIZER_BAND_9,
  276. TUNING_CTL_END_NID
  277. #define TUNING_CTLS_COUNT (TUNING_CTL_END_NID - TUNING_CTL_START_NID)
  278. };
  279. struct ct_tuning_ctl {
  280. char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
  281. hda_nid_t parent_nid;
  282. hda_nid_t nid;
  283. int mid; /*effect module ID*/
  284. int req; /*effect module request*/
  285. int direct; /* 0:output; 1:input*/
  286. unsigned int def_val;/*effect default values*/
  287. };
  288. static const struct ct_tuning_ctl ca0132_tuning_ctls[] = {
  289. { .name = "Wedge Angle",
  290. .parent_nid = VOICE_FOCUS,
  291. .nid = WEDGE_ANGLE,
  292. .mid = 0x95,
  293. .req = 8,
  294. .direct = EFX_DIR_IN,
  295. .def_val = 0x41F00000
  296. },
  297. { .name = "SVM Level",
  298. .parent_nid = MIC_SVM,
  299. .nid = SVM_LEVEL,
  300. .mid = 0x95,
  301. .req = 45,
  302. .direct = EFX_DIR_IN,
  303. .def_val = 0x3F3D70A4
  304. },
  305. { .name = "EQ Band0",
  306. .parent_nid = EQUALIZER,
  307. .nid = EQUALIZER_BAND_0,
  308. .mid = 0x96,
  309. .req = 11,
  310. .direct = EFX_DIR_OUT,
  311. .def_val = 0x00000000
  312. },
  313. { .name = "EQ Band1",
  314. .parent_nid = EQUALIZER,
  315. .nid = EQUALIZER_BAND_1,
  316. .mid = 0x96,
  317. .req = 12,
  318. .direct = EFX_DIR_OUT,
  319. .def_val = 0x00000000
  320. },
  321. { .name = "EQ Band2",
  322. .parent_nid = EQUALIZER,
  323. .nid = EQUALIZER_BAND_2,
  324. .mid = 0x96,
  325. .req = 13,
  326. .direct = EFX_DIR_OUT,
  327. .def_val = 0x00000000
  328. },
  329. { .name = "EQ Band3",
  330. .parent_nid = EQUALIZER,
  331. .nid = EQUALIZER_BAND_3,
  332. .mid = 0x96,
  333. .req = 14,
  334. .direct = EFX_DIR_OUT,
  335. .def_val = 0x00000000
  336. },
  337. { .name = "EQ Band4",
  338. .parent_nid = EQUALIZER,
  339. .nid = EQUALIZER_BAND_4,
  340. .mid = 0x96,
  341. .req = 15,
  342. .direct = EFX_DIR_OUT,
  343. .def_val = 0x00000000
  344. },
  345. { .name = "EQ Band5",
  346. .parent_nid = EQUALIZER,
  347. .nid = EQUALIZER_BAND_5,
  348. .mid = 0x96,
  349. .req = 16,
  350. .direct = EFX_DIR_OUT,
  351. .def_val = 0x00000000
  352. },
  353. { .name = "EQ Band6",
  354. .parent_nid = EQUALIZER,
  355. .nid = EQUALIZER_BAND_6,
  356. .mid = 0x96,
  357. .req = 17,
  358. .direct = EFX_DIR_OUT,
  359. .def_val = 0x00000000
  360. },
  361. { .name = "EQ Band7",
  362. .parent_nid = EQUALIZER,
  363. .nid = EQUALIZER_BAND_7,
  364. .mid = 0x96,
  365. .req = 18,
  366. .direct = EFX_DIR_OUT,
  367. .def_val = 0x00000000
  368. },
  369. { .name = "EQ Band8",
  370. .parent_nid = EQUALIZER,
  371. .nid = EQUALIZER_BAND_8,
  372. .mid = 0x96,
  373. .req = 19,
  374. .direct = EFX_DIR_OUT,
  375. .def_val = 0x00000000
  376. },
  377. { .name = "EQ Band9",
  378. .parent_nid = EQUALIZER,
  379. .nid = EQUALIZER_BAND_9,
  380. .mid = 0x96,
  381. .req = 20,
  382. .direct = EFX_DIR_OUT,
  383. .def_val = 0x00000000
  384. }
  385. };
  386. #endif
  387. /* Voice FX Presets */
  388. #define VOICEFX_MAX_PARAM_COUNT 9
  389. struct ct_voicefx {
  390. char *name;
  391. hda_nid_t nid;
  392. int mid;
  393. int reqs[VOICEFX_MAX_PARAM_COUNT]; /*effect module request*/
  394. };
  395. struct ct_voicefx_preset {
  396. char *name; /*preset name*/
  397. unsigned int vals[VOICEFX_MAX_PARAM_COUNT];
  398. };
  399. static const struct ct_voicefx ca0132_voicefx = {
  400. .name = "VoiceFX Capture Switch",
  401. .nid = VOICEFX,
  402. .mid = 0x95,
  403. .reqs = {10, 11, 12, 13, 14, 15, 16, 17, 18}
  404. };
  405. static const struct ct_voicefx_preset ca0132_voicefx_presets[] = {
  406. { .name = "Neutral",
  407. .vals = { 0x00000000, 0x43C80000, 0x44AF0000,
  408. 0x44FA0000, 0x3F800000, 0x3F800000,
  409. 0x3F800000, 0x00000000, 0x00000000 }
  410. },
  411. { .name = "Female2Male",
  412. .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
  413. 0x44FA0000, 0x3F19999A, 0x3F866666,
  414. 0x3F800000, 0x00000000, 0x00000000 }
  415. },
  416. { .name = "Male2Female",
  417. .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
  418. 0x450AC000, 0x4017AE14, 0x3F6B851F,
  419. 0x3F800000, 0x00000000, 0x00000000 }
  420. },
  421. { .name = "ScrappyKid",
  422. .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
  423. 0x44FA0000, 0x40400000, 0x3F28F5C3,
  424. 0x3F800000, 0x00000000, 0x00000000 }
  425. },
  426. { .name = "Elderly",
  427. .vals = { 0x3F800000, 0x44324000, 0x44BB8000,
  428. 0x44E10000, 0x3FB33333, 0x3FB9999A,
  429. 0x3F800000, 0x3E3A2E43, 0x00000000 }
  430. },
  431. { .name = "Orc",
  432. .vals = { 0x3F800000, 0x43EA0000, 0x44A52000,
  433. 0x45098000, 0x3F266666, 0x3FC00000,
  434. 0x3F800000, 0x00000000, 0x00000000 }
  435. },
  436. { .name = "Elf",
  437. .vals = { 0x3F800000, 0x43C70000, 0x44AE6000,
  438. 0x45193000, 0x3F8E147B, 0x3F75C28F,
  439. 0x3F800000, 0x00000000, 0x00000000 }
  440. },
  441. { .name = "Dwarf",
  442. .vals = { 0x3F800000, 0x43930000, 0x44BEE000,
  443. 0x45007000, 0x3F451EB8, 0x3F7851EC,
  444. 0x3F800000, 0x00000000, 0x00000000 }
  445. },
  446. { .name = "AlienBrute",
  447. .vals = { 0x3F800000, 0x43BFC5AC, 0x44B28FDF,
  448. 0x451F6000, 0x3F266666, 0x3FA7D945,
  449. 0x3F800000, 0x3CF5C28F, 0x00000000 }
  450. },
  451. { .name = "Robot",
  452. .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
  453. 0x44FA0000, 0x3FB2718B, 0x3F800000,
  454. 0xBC07010E, 0x00000000, 0x00000000 }
  455. },
  456. { .name = "Marine",
  457. .vals = { 0x3F800000, 0x43C20000, 0x44906000,
  458. 0x44E70000, 0x3F4CCCCD, 0x3F8A3D71,
  459. 0x3F0A3D71, 0x00000000, 0x00000000 }
  460. },
  461. { .name = "Emo",
  462. .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
  463. 0x44FA0000, 0x3F800000, 0x3F800000,
  464. 0x3E4CCCCD, 0x00000000, 0x00000000 }
  465. },
  466. { .name = "DeepVoice",
  467. .vals = { 0x3F800000, 0x43A9C5AC, 0x44AA4FDF,
  468. 0x44FFC000, 0x3EDBB56F, 0x3F99C4CA,
  469. 0x3F800000, 0x00000000, 0x00000000 }
  470. },
  471. { .name = "Munchkin",
  472. .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
  473. 0x44FA0000, 0x3F800000, 0x3F1A043C,
  474. 0x3F800000, 0x00000000, 0x00000000 }
  475. }
  476. };
  477. /* ca0132 EQ presets, taken from Windows Sound Blaster Z Driver */
  478. #define EQ_PRESET_MAX_PARAM_COUNT 11
  479. struct ct_eq {
  480. char *name;
  481. hda_nid_t nid;
  482. int mid;
  483. int reqs[EQ_PRESET_MAX_PARAM_COUNT]; /*effect module request*/
  484. };
  485. struct ct_eq_preset {
  486. char *name; /*preset name*/
  487. unsigned int vals[EQ_PRESET_MAX_PARAM_COUNT];
  488. };
  489. static const struct ct_eq ca0132_alt_eq_enum = {
  490. .name = "FX: Equalizer Preset Switch",
  491. .nid = EQ_PRESET_ENUM,
  492. .mid = 0x96,
  493. .reqs = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
  494. };
  495. static const struct ct_eq_preset ca0132_alt_eq_presets[] = {
  496. { .name = "Flat",
  497. .vals = { 0x00000000, 0x00000000, 0x00000000,
  498. 0x00000000, 0x00000000, 0x00000000,
  499. 0x00000000, 0x00000000, 0x00000000,
  500. 0x00000000, 0x00000000 }
  501. },
  502. { .name = "Acoustic",
  503. .vals = { 0x00000000, 0x00000000, 0x3F8CCCCD,
  504. 0x40000000, 0x00000000, 0x00000000,
  505. 0x00000000, 0x00000000, 0x40000000,
  506. 0x40000000, 0x40000000 }
  507. },
  508. { .name = "Classical",
  509. .vals = { 0x00000000, 0x00000000, 0x40C00000,
  510. 0x40C00000, 0x40466666, 0x00000000,
  511. 0x00000000, 0x00000000, 0x00000000,
  512. 0x40466666, 0x40466666 }
  513. },
  514. { .name = "Country",
  515. .vals = { 0x00000000, 0xBF99999A, 0x00000000,
  516. 0x3FA66666, 0x3FA66666, 0x3F8CCCCD,
  517. 0x00000000, 0x00000000, 0x40000000,
  518. 0x40466666, 0x40800000 }
  519. },
  520. { .name = "Dance",
  521. .vals = { 0x00000000, 0xBF99999A, 0x40000000,
  522. 0x40466666, 0x40866666, 0xBF99999A,
  523. 0xBF99999A, 0x00000000, 0x00000000,
  524. 0x40800000, 0x40800000 }
  525. },
  526. { .name = "Jazz",
  527. .vals = { 0x00000000, 0x00000000, 0x00000000,
  528. 0x3F8CCCCD, 0x40800000, 0x40800000,
  529. 0x40800000, 0x00000000, 0x3F8CCCCD,
  530. 0x40466666, 0x40466666 }
  531. },
  532. { .name = "New Age",
  533. .vals = { 0x00000000, 0x00000000, 0x40000000,
  534. 0x40000000, 0x00000000, 0x00000000,
  535. 0x00000000, 0x3F8CCCCD, 0x40000000,
  536. 0x40000000, 0x40000000 }
  537. },
  538. { .name = "Pop",
  539. .vals = { 0x00000000, 0xBFCCCCCD, 0x00000000,
  540. 0x40000000, 0x40000000, 0x00000000,
  541. 0xBF99999A, 0xBF99999A, 0x00000000,
  542. 0x40466666, 0x40C00000 }
  543. },
  544. { .name = "Rock",
  545. .vals = { 0x00000000, 0xBF99999A, 0xBF99999A,
  546. 0x3F8CCCCD, 0x40000000, 0xBF99999A,
  547. 0xBF99999A, 0x00000000, 0x00000000,
  548. 0x40800000, 0x40800000 }
  549. },
  550. { .name = "Vocal",
  551. .vals = { 0x00000000, 0xC0000000, 0xBF99999A,
  552. 0xBF99999A, 0x00000000, 0x40466666,
  553. 0x40800000, 0x40466666, 0x00000000,
  554. 0x00000000, 0x3F8CCCCD }
  555. }
  556. };
  557. /* DSP command sequences for ca0132_alt_select_out */
  558. #define ALT_OUT_SET_MAX_COMMANDS 9 /* Max number of commands in sequence */
  559. struct ca0132_alt_out_set {
  560. char *name; /*preset name*/
  561. unsigned char commands;
  562. unsigned int mids[ALT_OUT_SET_MAX_COMMANDS];
  563. unsigned int reqs[ALT_OUT_SET_MAX_COMMANDS];
  564. unsigned int vals[ALT_OUT_SET_MAX_COMMANDS];
  565. };
  566. static const struct ca0132_alt_out_set alt_out_presets[] = {
  567. { .name = "Line Out",
  568. .commands = 7,
  569. .mids = { 0x96, 0x96, 0x96, 0x8F,
  570. 0x96, 0x96, 0x96 },
  571. .reqs = { 0x19, 0x17, 0x18, 0x01,
  572. 0x1F, 0x15, 0x3A },
  573. .vals = { 0x3F000000, 0x42A00000, 0x00000000,
  574. 0x00000000, 0x00000000, 0x00000000,
  575. 0x00000000 }
  576. },
  577. { .name = "Headphone",
  578. .commands = 7,
  579. .mids = { 0x96, 0x96, 0x96, 0x8F,
  580. 0x96, 0x96, 0x96 },
  581. .reqs = { 0x19, 0x17, 0x18, 0x01,
  582. 0x1F, 0x15, 0x3A },
  583. .vals = { 0x3F000000, 0x42A00000, 0x00000000,
  584. 0x00000000, 0x00000000, 0x00000000,
  585. 0x00000000 }
  586. },
  587. { .name = "Surround",
  588. .commands = 8,
  589. .mids = { 0x96, 0x8F, 0x96, 0x96,
  590. 0x96, 0x96, 0x96, 0x96 },
  591. .reqs = { 0x18, 0x01, 0x1F, 0x15,
  592. 0x3A, 0x1A, 0x1B, 0x1C },
  593. .vals = { 0x00000000, 0x00000000, 0x00000000,
  594. 0x00000000, 0x00000000, 0x00000000,
  595. 0x00000000, 0x00000000 }
  596. }
  597. };
  598. /*
  599. * DSP volume setting structs. Req 1 is left volume, req 2 is right volume,
  600. * and I don't know what the third req is, but it's always zero. I assume it's
  601. * some sort of update or set command to tell the DSP there's new volume info.
  602. */
  603. #define DSP_VOL_OUT 0
  604. #define DSP_VOL_IN 1
  605. struct ct_dsp_volume_ctl {
  606. hda_nid_t vnid;
  607. int mid; /* module ID*/
  608. unsigned int reqs[3]; /* scp req ID */
  609. };
  610. static const struct ct_dsp_volume_ctl ca0132_alt_vol_ctls[] = {
  611. { .vnid = VNID_SPK,
  612. .mid = 0x32,
  613. .reqs = {3, 4, 2}
  614. },
  615. { .vnid = VNID_MIC,
  616. .mid = 0x37,
  617. .reqs = {2, 3, 1}
  618. }
  619. };
  620. enum hda_cmd_vendor_io {
  621. /* for DspIO node */
  622. VENDOR_DSPIO_SCP_WRITE_DATA_LOW = 0x000,
  623. VENDOR_DSPIO_SCP_WRITE_DATA_HIGH = 0x100,
  624. VENDOR_DSPIO_STATUS = 0xF01,
  625. VENDOR_DSPIO_SCP_POST_READ_DATA = 0x702,
  626. VENDOR_DSPIO_SCP_READ_DATA = 0xF02,
  627. VENDOR_DSPIO_DSP_INIT = 0x703,
  628. VENDOR_DSPIO_SCP_POST_COUNT_QUERY = 0x704,
  629. VENDOR_DSPIO_SCP_READ_COUNT = 0xF04,
  630. /* for ChipIO node */
  631. VENDOR_CHIPIO_ADDRESS_LOW = 0x000,
  632. VENDOR_CHIPIO_ADDRESS_HIGH = 0x100,
  633. VENDOR_CHIPIO_STREAM_FORMAT = 0x200,
  634. VENDOR_CHIPIO_DATA_LOW = 0x300,
  635. VENDOR_CHIPIO_DATA_HIGH = 0x400,
  636. VENDOR_CHIPIO_GET_PARAMETER = 0xF00,
  637. VENDOR_CHIPIO_STATUS = 0xF01,
  638. VENDOR_CHIPIO_HIC_POST_READ = 0x702,
  639. VENDOR_CHIPIO_HIC_READ_DATA = 0xF03,
  640. VENDOR_CHIPIO_8051_DATA_WRITE = 0x707,
  641. VENDOR_CHIPIO_8051_DATA_READ = 0xF07,
  642. VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE = 0x70A,
  643. VENDOR_CHIPIO_CT_EXTENSIONS_GET = 0xF0A,
  644. VENDOR_CHIPIO_PLL_PMU_WRITE = 0x70C,
  645. VENDOR_CHIPIO_PLL_PMU_READ = 0xF0C,
  646. VENDOR_CHIPIO_8051_ADDRESS_LOW = 0x70D,
  647. VENDOR_CHIPIO_8051_ADDRESS_HIGH = 0x70E,
  648. VENDOR_CHIPIO_FLAG_SET = 0x70F,
  649. VENDOR_CHIPIO_FLAGS_GET = 0xF0F,
  650. VENDOR_CHIPIO_PARAM_SET = 0x710,
  651. VENDOR_CHIPIO_PARAM_GET = 0xF10,
  652. VENDOR_CHIPIO_PORT_ALLOC_CONFIG_SET = 0x711,
  653. VENDOR_CHIPIO_PORT_ALLOC_SET = 0x712,
  654. VENDOR_CHIPIO_PORT_ALLOC_GET = 0xF12,
  655. VENDOR_CHIPIO_PORT_FREE_SET = 0x713,
  656. VENDOR_CHIPIO_PARAM_EX_ID_GET = 0xF17,
  657. VENDOR_CHIPIO_PARAM_EX_ID_SET = 0x717,
  658. VENDOR_CHIPIO_PARAM_EX_VALUE_GET = 0xF18,
  659. VENDOR_CHIPIO_PARAM_EX_VALUE_SET = 0x718,
  660. VENDOR_CHIPIO_DMIC_CTL_SET = 0x788,
  661. VENDOR_CHIPIO_DMIC_CTL_GET = 0xF88,
  662. VENDOR_CHIPIO_DMIC_PIN_SET = 0x789,
  663. VENDOR_CHIPIO_DMIC_PIN_GET = 0xF89,
  664. VENDOR_CHIPIO_DMIC_MCLK_SET = 0x78A,
  665. VENDOR_CHIPIO_DMIC_MCLK_GET = 0xF8A,
  666. VENDOR_CHIPIO_EAPD_SEL_SET = 0x78D
  667. };
  668. /*
  669. * Control flag IDs
  670. */
  671. enum control_flag_id {
  672. /* Connection manager stream setup is bypassed/enabled */
  673. CONTROL_FLAG_C_MGR = 0,
  674. /* DSP DMA is bypassed/enabled */
  675. CONTROL_FLAG_DMA = 1,
  676. /* 8051 'idle' mode is disabled/enabled */
  677. CONTROL_FLAG_IDLE_ENABLE = 2,
  678. /* Tracker for the SPDIF-in path is bypassed/enabled */
  679. CONTROL_FLAG_TRACKER = 3,
  680. /* DigitalOut to Spdif2Out connection is disabled/enabled */
  681. CONTROL_FLAG_SPDIF2OUT = 4,
  682. /* Digital Microphone is disabled/enabled */
  683. CONTROL_FLAG_DMIC = 5,
  684. /* ADC_B rate is 48 kHz/96 kHz */
  685. CONTROL_FLAG_ADC_B_96KHZ = 6,
  686. /* ADC_C rate is 48 kHz/96 kHz */
  687. CONTROL_FLAG_ADC_C_96KHZ = 7,
  688. /* DAC rate is 48 kHz/96 kHz (affects all DACs) */
  689. CONTROL_FLAG_DAC_96KHZ = 8,
  690. /* DSP rate is 48 kHz/96 kHz */
  691. CONTROL_FLAG_DSP_96KHZ = 9,
  692. /* SRC clock is 98 MHz/196 MHz (196 MHz forces rate to 96 KHz) */
  693. CONTROL_FLAG_SRC_CLOCK_196MHZ = 10,
  694. /* SRC rate is 48 kHz/96 kHz (48 kHz disabled when clock is 196 MHz) */
  695. CONTROL_FLAG_SRC_RATE_96KHZ = 11,
  696. /* Decode Loop (DSP->SRC->DSP) is disabled/enabled */
  697. CONTROL_FLAG_DECODE_LOOP = 12,
  698. /* De-emphasis filter on DAC-1 disabled/enabled */
  699. CONTROL_FLAG_DAC1_DEEMPHASIS = 13,
  700. /* De-emphasis filter on DAC-2 disabled/enabled */
  701. CONTROL_FLAG_DAC2_DEEMPHASIS = 14,
  702. /* De-emphasis filter on DAC-3 disabled/enabled */
  703. CONTROL_FLAG_DAC3_DEEMPHASIS = 15,
  704. /* High-pass filter on ADC_B disabled/enabled */
  705. CONTROL_FLAG_ADC_B_HIGH_PASS = 16,
  706. /* High-pass filter on ADC_C disabled/enabled */
  707. CONTROL_FLAG_ADC_C_HIGH_PASS = 17,
  708. /* Common mode on Port_A disabled/enabled */
  709. CONTROL_FLAG_PORT_A_COMMON_MODE = 18,
  710. /* Common mode on Port_D disabled/enabled */
  711. CONTROL_FLAG_PORT_D_COMMON_MODE = 19,
  712. /* Impedance for ramp generator on Port_A 16 Ohm/10K Ohm */
  713. CONTROL_FLAG_PORT_A_10KOHM_LOAD = 20,
  714. /* Impedance for ramp generator on Port_D, 16 Ohm/10K Ohm */
  715. CONTROL_FLAG_PORT_D_10KOHM_LOAD = 21,
  716. /* ASI rate is 48kHz/96kHz */
  717. CONTROL_FLAG_ASI_96KHZ = 22,
  718. /* DAC power settings able to control attached ports no/yes */
  719. CONTROL_FLAG_DACS_CONTROL_PORTS = 23,
  720. /* Clock Stop OK reporting is disabled/enabled */
  721. CONTROL_FLAG_CONTROL_STOP_OK_ENABLE = 24,
  722. /* Number of control flags */
  723. CONTROL_FLAGS_MAX = (CONTROL_FLAG_CONTROL_STOP_OK_ENABLE+1)
  724. };
  725. /*
  726. * Control parameter IDs
  727. */
  728. enum control_param_id {
  729. /* 0: None, 1: Mic1In*/
  730. CONTROL_PARAM_VIP_SOURCE = 1,
  731. /* 0: force HDA, 1: allow DSP if HDA Spdif1Out stream is idle */
  732. CONTROL_PARAM_SPDIF1_SOURCE = 2,
  733. /* Port A output stage gain setting to use when 16 Ohm output
  734. * impedance is selected*/
  735. CONTROL_PARAM_PORTA_160OHM_GAIN = 8,
  736. /* Port D output stage gain setting to use when 16 Ohm output
  737. * impedance is selected*/
  738. CONTROL_PARAM_PORTD_160OHM_GAIN = 10,
  739. /* Stream Control */
  740. /* Select stream with the given ID */
  741. CONTROL_PARAM_STREAM_ID = 24,
  742. /* Source connection point for the selected stream */
  743. CONTROL_PARAM_STREAM_SOURCE_CONN_POINT = 25,
  744. /* Destination connection point for the selected stream */
  745. CONTROL_PARAM_STREAM_DEST_CONN_POINT = 26,
  746. /* Number of audio channels in the selected stream */
  747. CONTROL_PARAM_STREAMS_CHANNELS = 27,
  748. /*Enable control for the selected stream */
  749. CONTROL_PARAM_STREAM_CONTROL = 28,
  750. /* Connection Point Control */
  751. /* Select connection point with the given ID */
  752. CONTROL_PARAM_CONN_POINT_ID = 29,
  753. /* Connection point sample rate */
  754. CONTROL_PARAM_CONN_POINT_SAMPLE_RATE = 30,
  755. /* Node Control */
  756. /* Select HDA node with the given ID */
  757. CONTROL_PARAM_NODE_ID = 31
  758. };
  759. /*
  760. * Dsp Io Status codes
  761. */
  762. enum hda_vendor_status_dspio {
  763. /* Success */
  764. VENDOR_STATUS_DSPIO_OK = 0x00,
  765. /* Busy, unable to accept new command, the host must retry */
  766. VENDOR_STATUS_DSPIO_BUSY = 0x01,
  767. /* SCP command queue is full */
  768. VENDOR_STATUS_DSPIO_SCP_COMMAND_QUEUE_FULL = 0x02,
  769. /* SCP response queue is empty */
  770. VENDOR_STATUS_DSPIO_SCP_RESPONSE_QUEUE_EMPTY = 0x03
  771. };
  772. /*
  773. * Chip Io Status codes
  774. */
  775. enum hda_vendor_status_chipio {
  776. /* Success */
  777. VENDOR_STATUS_CHIPIO_OK = 0x00,
  778. /* Busy, unable to accept new command, the host must retry */
  779. VENDOR_STATUS_CHIPIO_BUSY = 0x01
  780. };
  781. /*
  782. * CA0132 sample rate
  783. */
  784. enum ca0132_sample_rate {
  785. SR_6_000 = 0x00,
  786. SR_8_000 = 0x01,
  787. SR_9_600 = 0x02,
  788. SR_11_025 = 0x03,
  789. SR_16_000 = 0x04,
  790. SR_22_050 = 0x05,
  791. SR_24_000 = 0x06,
  792. SR_32_000 = 0x07,
  793. SR_44_100 = 0x08,
  794. SR_48_000 = 0x09,
  795. SR_88_200 = 0x0A,
  796. SR_96_000 = 0x0B,
  797. SR_144_000 = 0x0C,
  798. SR_176_400 = 0x0D,
  799. SR_192_000 = 0x0E,
  800. SR_384_000 = 0x0F,
  801. SR_COUNT = 0x10,
  802. SR_RATE_UNKNOWN = 0x1F
  803. };
  804. enum dsp_download_state {
  805. DSP_DOWNLOAD_FAILED = -1,
  806. DSP_DOWNLOAD_INIT = 0,
  807. DSP_DOWNLOADING = 1,
  808. DSP_DOWNLOADED = 2
  809. };
  810. /* retrieve parameters from hda format */
  811. #define get_hdafmt_chs(fmt) (fmt & 0xf)
  812. #define get_hdafmt_bits(fmt) ((fmt >> 4) & 0x7)
  813. #define get_hdafmt_rate(fmt) ((fmt >> 8) & 0x7f)
  814. #define get_hdafmt_type(fmt) ((fmt >> 15) & 0x1)
  815. /*
  816. * CA0132 specific
  817. */
  818. struct ca0132_spec {
  819. const struct snd_kcontrol_new *mixers[5];
  820. unsigned int num_mixers;
  821. const struct hda_verb *base_init_verbs;
  822. const struct hda_verb *base_exit_verbs;
  823. const struct hda_verb *chip_init_verbs;
  824. const struct hda_verb *desktop_init_verbs;
  825. struct hda_verb *spec_init_verbs;
  826. struct auto_pin_cfg autocfg;
  827. /* Nodes configurations */
  828. struct hda_multi_out multiout;
  829. hda_nid_t out_pins[AUTO_CFG_MAX_OUTS];
  830. hda_nid_t dacs[AUTO_CFG_MAX_OUTS];
  831. unsigned int num_outputs;
  832. hda_nid_t input_pins[AUTO_PIN_LAST];
  833. hda_nid_t adcs[AUTO_PIN_LAST];
  834. hda_nid_t dig_out;
  835. hda_nid_t dig_in;
  836. unsigned int num_inputs;
  837. hda_nid_t shared_mic_nid;
  838. hda_nid_t shared_out_nid;
  839. hda_nid_t unsol_tag_hp;
  840. hda_nid_t unsol_tag_front_hp; /* for desktop ca0132 codecs */
  841. hda_nid_t unsol_tag_amic1;
  842. /* chip access */
  843. struct mutex chipio_mutex; /* chip access mutex */
  844. u32 curr_chip_addx;
  845. /* DSP download related */
  846. enum dsp_download_state dsp_state;
  847. unsigned int dsp_stream_id;
  848. unsigned int wait_scp;
  849. unsigned int wait_scp_header;
  850. unsigned int wait_num_data;
  851. unsigned int scp_resp_header;
  852. unsigned int scp_resp_data[4];
  853. unsigned int scp_resp_count;
  854. bool alt_firmware_present;
  855. bool startup_check_entered;
  856. bool dsp_reload;
  857. /* mixer and effects related */
  858. unsigned char dmic_ctl;
  859. int cur_out_type;
  860. int cur_mic_type;
  861. long vnode_lvol[VNODES_COUNT];
  862. long vnode_rvol[VNODES_COUNT];
  863. long vnode_lswitch[VNODES_COUNT];
  864. long vnode_rswitch[VNODES_COUNT];
  865. long effects_switch[EFFECTS_COUNT];
  866. long voicefx_val;
  867. long cur_mic_boost;
  868. /* ca0132_alt control related values */
  869. unsigned char in_enum_val;
  870. unsigned char out_enum_val;
  871. unsigned char mic_boost_enum_val;
  872. unsigned char smart_volume_setting;
  873. long fx_ctl_val[EFFECT_LEVEL_SLIDERS];
  874. long xbass_xover_freq;
  875. long eq_preset_val;
  876. unsigned int tlv[4];
  877. struct hda_vmaster_mute_hook vmaster_mute;
  878. struct hda_codec *codec;
  879. struct delayed_work unsol_hp_work;
  880. int quirk;
  881. #ifdef ENABLE_TUNING_CONTROLS
  882. long cur_ctl_vals[TUNING_CTLS_COUNT];
  883. #endif
  884. /*
  885. * The Recon3D, Sound Blaster Z, Sound Blaster ZxR, and Sound Blaster
  886. * AE-5 all use PCI region 2 to toggle GPIO and other currently unknown
  887. * things.
  888. */
  889. bool use_pci_mmio;
  890. void __iomem *mem_base;
  891. /*
  892. * Whether or not to use the alt functions like alt_select_out,
  893. * alt_select_in, etc. Only used on desktop codecs for now, because of
  894. * surround sound support.
  895. */
  896. bool use_alt_functions;
  897. /*
  898. * Whether or not to use alt controls: volume effect sliders, EQ
  899. * presets, smart volume presets, and new control names with FX prefix.
  900. * Renames PlayEnhancement and CrystalVoice too.
  901. */
  902. bool use_alt_controls;
  903. };
  904. /*
  905. * CA0132 quirks table
  906. */
  907. enum {
  908. QUIRK_NONE,
  909. QUIRK_ALIENWARE,
  910. QUIRK_ALIENWARE_M17XR4,
  911. QUIRK_SBZ,
  912. QUIRK_R3DI,
  913. QUIRK_R3D,
  914. };
  915. static const struct hda_pintbl alienware_pincfgs[] = {
  916. { 0x0b, 0x90170110 }, /* Builtin Speaker */
  917. { 0x0c, 0x411111f0 }, /* N/A */
  918. { 0x0d, 0x411111f0 }, /* N/A */
  919. { 0x0e, 0x411111f0 }, /* N/A */
  920. { 0x0f, 0x0321101f }, /* HP */
  921. { 0x10, 0x411111f0 }, /* Headset? disabled for now */
  922. { 0x11, 0x03a11021 }, /* Mic */
  923. { 0x12, 0xd5a30140 }, /* Builtin Mic */
  924. { 0x13, 0x411111f0 }, /* N/A */
  925. { 0x18, 0x411111f0 }, /* N/A */
  926. {}
  927. };
  928. /* Sound Blaster Z pin configs taken from Windows Driver */
  929. static const struct hda_pintbl sbz_pincfgs[] = {
  930. { 0x0b, 0x01017010 }, /* Port G -- Lineout FRONT L/R */
  931. { 0x0c, 0x014510f0 }, /* SPDIF Out 1 */
  932. { 0x0d, 0x014510f0 }, /* Digital Out */
  933. { 0x0e, 0x01c510f0 }, /* SPDIF In */
  934. { 0x0f, 0x0221701f }, /* Port A -- BackPanel HP */
  935. { 0x10, 0x01017012 }, /* Port D -- Center/LFE or FP Hp */
  936. { 0x11, 0x01017014 }, /* Port B -- LineMicIn2 / Rear L/R */
  937. { 0x12, 0x01a170f0 }, /* Port C -- LineIn1 */
  938. { 0x13, 0x908700f0 }, /* What U Hear In*/
  939. { 0x18, 0x50d000f0 }, /* N/A */
  940. {}
  941. };
  942. /* Recon3D pin configs taken from Windows Driver */
  943. static const struct hda_pintbl r3d_pincfgs[] = {
  944. { 0x0b, 0x01014110 }, /* Port G -- Lineout FRONT L/R */
  945. { 0x0c, 0x014510f0 }, /* SPDIF Out 1 */
  946. { 0x0d, 0x014510f0 }, /* Digital Out */
  947. { 0x0e, 0x01c520f0 }, /* SPDIF In */
  948. { 0x0f, 0x0221401f }, /* Port A -- BackPanel HP */
  949. { 0x10, 0x01016011 }, /* Port D -- Center/LFE or FP Hp */
  950. { 0x11, 0x01011014 }, /* Port B -- LineMicIn2 / Rear L/R */
  951. { 0x12, 0x02a090f0 }, /* Port C -- LineIn1 */
  952. { 0x13, 0x908700f0 }, /* What U Hear In*/
  953. { 0x18, 0x50d000f0 }, /* N/A */
  954. {}
  955. };
  956. /* Recon3D integrated pin configs taken from Windows Driver */
  957. static const struct hda_pintbl r3di_pincfgs[] = {
  958. { 0x0b, 0x01014110 }, /* Port G -- Lineout FRONT L/R */
  959. { 0x0c, 0x014510f0 }, /* SPDIF Out 1 */
  960. { 0x0d, 0x014510f0 }, /* Digital Out */
  961. { 0x0e, 0x41c520f0 }, /* SPDIF In */
  962. { 0x0f, 0x0221401f }, /* Port A -- BackPanel HP */
  963. { 0x10, 0x01016011 }, /* Port D -- Center/LFE or FP Hp */
  964. { 0x11, 0x01011014 }, /* Port B -- LineMicIn2 / Rear L/R */
  965. { 0x12, 0x02a090f0 }, /* Port C -- LineIn1 */
  966. { 0x13, 0x908700f0 }, /* What U Hear In*/
  967. { 0x18, 0x500000f0 }, /* N/A */
  968. {}
  969. };
  970. static const struct snd_pci_quirk ca0132_quirks[] = {
  971. SND_PCI_QUIRK(0x1028, 0x057b, "Alienware M17x R4", QUIRK_ALIENWARE_M17XR4),
  972. SND_PCI_QUIRK(0x1028, 0x0685, "Alienware 15 2015", QUIRK_ALIENWARE),
  973. SND_PCI_QUIRK(0x1028, 0x0688, "Alienware 17 2015", QUIRK_ALIENWARE),
  974. SND_PCI_QUIRK(0x1028, 0x0708, "Alienware 15 R2 2016", QUIRK_ALIENWARE),
  975. SND_PCI_QUIRK(0x1102, 0x0010, "Sound Blaster Z", QUIRK_SBZ),
  976. SND_PCI_QUIRK(0x1102, 0x0023, "Sound Blaster Z", QUIRK_SBZ),
  977. SND_PCI_QUIRK(0x1458, 0xA016, "Recon3Di", QUIRK_R3DI),
  978. SND_PCI_QUIRK(0x1458, 0xA026, "Gigabyte G1.Sniper Z97", QUIRK_R3DI),
  979. SND_PCI_QUIRK(0x1458, 0xA036, "Gigabyte GA-Z170X-Gaming 7", QUIRK_R3DI),
  980. SND_PCI_QUIRK(0x3842, 0x1038, "EVGA X99 Classified", QUIRK_R3DI),
  981. SND_PCI_QUIRK(0x1102, 0x0013, "Recon3D", QUIRK_R3D),
  982. {}
  983. };
  984. /*
  985. * CA0132 codec access
  986. */
  987. static unsigned int codec_send_command(struct hda_codec *codec, hda_nid_t nid,
  988. unsigned int verb, unsigned int parm, unsigned int *res)
  989. {
  990. unsigned int response;
  991. response = snd_hda_codec_read(codec, nid, 0, verb, parm);
  992. *res = response;
  993. return ((response == -1) ? -1 : 0);
  994. }
  995. static int codec_set_converter_format(struct hda_codec *codec, hda_nid_t nid,
  996. unsigned short converter_format, unsigned int *res)
  997. {
  998. return codec_send_command(codec, nid, VENDOR_CHIPIO_STREAM_FORMAT,
  999. converter_format & 0xffff, res);
  1000. }
  1001. static int codec_set_converter_stream_channel(struct hda_codec *codec,
  1002. hda_nid_t nid, unsigned char stream,
  1003. unsigned char channel, unsigned int *res)
  1004. {
  1005. unsigned char converter_stream_channel = 0;
  1006. converter_stream_channel = (stream << 4) | (channel & 0x0f);
  1007. return codec_send_command(codec, nid, AC_VERB_SET_CHANNEL_STREAMID,
  1008. converter_stream_channel, res);
  1009. }
  1010. /* Chip access helper function */
  1011. static int chipio_send(struct hda_codec *codec,
  1012. unsigned int reg,
  1013. unsigned int data)
  1014. {
  1015. unsigned int res;
  1016. unsigned long timeout = jiffies + msecs_to_jiffies(1000);
  1017. /* send bits of data specified by reg */
  1018. do {
  1019. res = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
  1020. reg, data);
  1021. if (res == VENDOR_STATUS_CHIPIO_OK)
  1022. return 0;
  1023. msleep(20);
  1024. } while (time_before(jiffies, timeout));
  1025. return -EIO;
  1026. }
  1027. /*
  1028. * Write chip address through the vendor widget -- NOT protected by the Mutex!
  1029. */
  1030. static int chipio_write_address(struct hda_codec *codec,
  1031. unsigned int chip_addx)
  1032. {
  1033. struct ca0132_spec *spec = codec->spec;
  1034. int res;
  1035. if (spec->curr_chip_addx == chip_addx)
  1036. return 0;
  1037. /* send low 16 bits of the address */
  1038. res = chipio_send(codec, VENDOR_CHIPIO_ADDRESS_LOW,
  1039. chip_addx & 0xffff);
  1040. if (res != -EIO) {
  1041. /* send high 16 bits of the address */
  1042. res = chipio_send(codec, VENDOR_CHIPIO_ADDRESS_HIGH,
  1043. chip_addx >> 16);
  1044. }
  1045. spec->curr_chip_addx = (res < 0) ? ~0U : chip_addx;
  1046. return res;
  1047. }
  1048. /*
  1049. * Write data through the vendor widget -- NOT protected by the Mutex!
  1050. */
  1051. static int chipio_write_data(struct hda_codec *codec, unsigned int data)
  1052. {
  1053. struct ca0132_spec *spec = codec->spec;
  1054. int res;
  1055. /* send low 16 bits of the data */
  1056. res = chipio_send(codec, VENDOR_CHIPIO_DATA_LOW, data & 0xffff);
  1057. if (res != -EIO) {
  1058. /* send high 16 bits of the data */
  1059. res = chipio_send(codec, VENDOR_CHIPIO_DATA_HIGH,
  1060. data >> 16);
  1061. }
  1062. /*If no error encountered, automatically increment the address
  1063. as per chip behaviour*/
  1064. spec->curr_chip_addx = (res != -EIO) ?
  1065. (spec->curr_chip_addx + 4) : ~0U;
  1066. return res;
  1067. }
  1068. /*
  1069. * Write multiple data through the vendor widget -- NOT protected by the Mutex!
  1070. */
  1071. static int chipio_write_data_multiple(struct hda_codec *codec,
  1072. const u32 *data,
  1073. unsigned int count)
  1074. {
  1075. int status = 0;
  1076. if (data == NULL) {
  1077. codec_dbg(codec, "chipio_write_data null ptr\n");
  1078. return -EINVAL;
  1079. }
  1080. while ((count-- != 0) && (status == 0))
  1081. status = chipio_write_data(codec, *data++);
  1082. return status;
  1083. }
  1084. /*
  1085. * Read data through the vendor widget -- NOT protected by the Mutex!
  1086. */
  1087. static int chipio_read_data(struct hda_codec *codec, unsigned int *data)
  1088. {
  1089. struct ca0132_spec *spec = codec->spec;
  1090. int res;
  1091. /* post read */
  1092. res = chipio_send(codec, VENDOR_CHIPIO_HIC_POST_READ, 0);
  1093. if (res != -EIO) {
  1094. /* read status */
  1095. res = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
  1096. }
  1097. if (res != -EIO) {
  1098. /* read data */
  1099. *data = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
  1100. VENDOR_CHIPIO_HIC_READ_DATA,
  1101. 0);
  1102. }
  1103. /*If no error encountered, automatically increment the address
  1104. as per chip behaviour*/
  1105. spec->curr_chip_addx = (res != -EIO) ?
  1106. (spec->curr_chip_addx + 4) : ~0U;
  1107. return res;
  1108. }
  1109. /*
  1110. * Write given value to the given address through the chip I/O widget.
  1111. * protected by the Mutex
  1112. */
  1113. static int chipio_write(struct hda_codec *codec,
  1114. unsigned int chip_addx, const unsigned int data)
  1115. {
  1116. struct ca0132_spec *spec = codec->spec;
  1117. int err;
  1118. mutex_lock(&spec->chipio_mutex);
  1119. /* write the address, and if successful proceed to write data */
  1120. err = chipio_write_address(codec, chip_addx);
  1121. if (err < 0)
  1122. goto exit;
  1123. err = chipio_write_data(codec, data);
  1124. if (err < 0)
  1125. goto exit;
  1126. exit:
  1127. mutex_unlock(&spec->chipio_mutex);
  1128. return err;
  1129. }
  1130. /*
  1131. * Write given value to the given address through the chip I/O widget.
  1132. * not protected by the Mutex
  1133. */
  1134. static int chipio_write_no_mutex(struct hda_codec *codec,
  1135. unsigned int chip_addx, const unsigned int data)
  1136. {
  1137. int err;
  1138. /* write the address, and if successful proceed to write data */
  1139. err = chipio_write_address(codec, chip_addx);
  1140. if (err < 0)
  1141. goto exit;
  1142. err = chipio_write_data(codec, data);
  1143. if (err < 0)
  1144. goto exit;
  1145. exit:
  1146. return err;
  1147. }
  1148. /*
  1149. * Write multiple values to the given address through the chip I/O widget.
  1150. * protected by the Mutex
  1151. */
  1152. static int chipio_write_multiple(struct hda_codec *codec,
  1153. u32 chip_addx,
  1154. const u32 *data,
  1155. unsigned int count)
  1156. {
  1157. struct ca0132_spec *spec = codec->spec;
  1158. int status;
  1159. mutex_lock(&spec->chipio_mutex);
  1160. status = chipio_write_address(codec, chip_addx);
  1161. if (status < 0)
  1162. goto error;
  1163. status = chipio_write_data_multiple(codec, data, count);
  1164. error:
  1165. mutex_unlock(&spec->chipio_mutex);
  1166. return status;
  1167. }
  1168. /*
  1169. * Read the given address through the chip I/O widget
  1170. * protected by the Mutex
  1171. */
  1172. static int chipio_read(struct hda_codec *codec,
  1173. unsigned int chip_addx, unsigned int *data)
  1174. {
  1175. struct ca0132_spec *spec = codec->spec;
  1176. int err;
  1177. mutex_lock(&spec->chipio_mutex);
  1178. /* write the address, and if successful proceed to write data */
  1179. err = chipio_write_address(codec, chip_addx);
  1180. if (err < 0)
  1181. goto exit;
  1182. err = chipio_read_data(codec, data);
  1183. if (err < 0)
  1184. goto exit;
  1185. exit:
  1186. mutex_unlock(&spec->chipio_mutex);
  1187. return err;
  1188. }
  1189. /*
  1190. * Set chip control flags through the chip I/O widget.
  1191. */
  1192. static void chipio_set_control_flag(struct hda_codec *codec,
  1193. enum control_flag_id flag_id,
  1194. bool flag_state)
  1195. {
  1196. unsigned int val;
  1197. unsigned int flag_bit;
  1198. flag_bit = (flag_state ? 1 : 0);
  1199. val = (flag_bit << 7) | (flag_id);
  1200. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  1201. VENDOR_CHIPIO_FLAG_SET, val);
  1202. }
  1203. /*
  1204. * Set chip parameters through the chip I/O widget.
  1205. */
  1206. static void chipio_set_control_param(struct hda_codec *codec,
  1207. enum control_param_id param_id, int param_val)
  1208. {
  1209. struct ca0132_spec *spec = codec->spec;
  1210. int val;
  1211. if ((param_id < 32) && (param_val < 8)) {
  1212. val = (param_val << 5) | (param_id);
  1213. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  1214. VENDOR_CHIPIO_PARAM_SET, val);
  1215. } else {
  1216. mutex_lock(&spec->chipio_mutex);
  1217. if (chipio_send(codec, VENDOR_CHIPIO_STATUS, 0) == 0) {
  1218. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  1219. VENDOR_CHIPIO_PARAM_EX_ID_SET,
  1220. param_id);
  1221. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  1222. VENDOR_CHIPIO_PARAM_EX_VALUE_SET,
  1223. param_val);
  1224. }
  1225. mutex_unlock(&spec->chipio_mutex);
  1226. }
  1227. }
  1228. /*
  1229. * Set chip parameters through the chip I/O widget. NO MUTEX.
  1230. */
  1231. static void chipio_set_control_param_no_mutex(struct hda_codec *codec,
  1232. enum control_param_id param_id, int param_val)
  1233. {
  1234. int val;
  1235. if ((param_id < 32) && (param_val < 8)) {
  1236. val = (param_val << 5) | (param_id);
  1237. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  1238. VENDOR_CHIPIO_PARAM_SET, val);
  1239. } else {
  1240. if (chipio_send(codec, VENDOR_CHIPIO_STATUS, 0) == 0) {
  1241. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  1242. VENDOR_CHIPIO_PARAM_EX_ID_SET,
  1243. param_id);
  1244. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  1245. VENDOR_CHIPIO_PARAM_EX_VALUE_SET,
  1246. param_val);
  1247. }
  1248. }
  1249. }
  1250. /*
  1251. * Connect stream to a source point, and then connect
  1252. * that source point to a destination point.
  1253. */
  1254. static void chipio_set_stream_source_dest(struct hda_codec *codec,
  1255. int streamid, int source_point, int dest_point)
  1256. {
  1257. chipio_set_control_param_no_mutex(codec,
  1258. CONTROL_PARAM_STREAM_ID, streamid);
  1259. chipio_set_control_param_no_mutex(codec,
  1260. CONTROL_PARAM_STREAM_SOURCE_CONN_POINT, source_point);
  1261. chipio_set_control_param_no_mutex(codec,
  1262. CONTROL_PARAM_STREAM_DEST_CONN_POINT, dest_point);
  1263. }
  1264. /*
  1265. * Set number of channels in the selected stream.
  1266. */
  1267. static void chipio_set_stream_channels(struct hda_codec *codec,
  1268. int streamid, unsigned int channels)
  1269. {
  1270. chipio_set_control_param_no_mutex(codec,
  1271. CONTROL_PARAM_STREAM_ID, streamid);
  1272. chipio_set_control_param_no_mutex(codec,
  1273. CONTROL_PARAM_STREAMS_CHANNELS, channels);
  1274. }
  1275. /*
  1276. * Enable/Disable audio stream.
  1277. */
  1278. static void chipio_set_stream_control(struct hda_codec *codec,
  1279. int streamid, int enable)
  1280. {
  1281. chipio_set_control_param_no_mutex(codec,
  1282. CONTROL_PARAM_STREAM_ID, streamid);
  1283. chipio_set_control_param_no_mutex(codec,
  1284. CONTROL_PARAM_STREAM_CONTROL, enable);
  1285. }
  1286. /*
  1287. * Set sampling rate of the connection point. NO MUTEX.
  1288. */
  1289. static void chipio_set_conn_rate_no_mutex(struct hda_codec *codec,
  1290. int connid, enum ca0132_sample_rate rate)
  1291. {
  1292. chipio_set_control_param_no_mutex(codec,
  1293. CONTROL_PARAM_CONN_POINT_ID, connid);
  1294. chipio_set_control_param_no_mutex(codec,
  1295. CONTROL_PARAM_CONN_POINT_SAMPLE_RATE, rate);
  1296. }
  1297. /*
  1298. * Set sampling rate of the connection point.
  1299. */
  1300. static void chipio_set_conn_rate(struct hda_codec *codec,
  1301. int connid, enum ca0132_sample_rate rate)
  1302. {
  1303. chipio_set_control_param(codec, CONTROL_PARAM_CONN_POINT_ID, connid);
  1304. chipio_set_control_param(codec, CONTROL_PARAM_CONN_POINT_SAMPLE_RATE,
  1305. rate);
  1306. }
  1307. /*
  1308. * Enable clocks.
  1309. */
  1310. static void chipio_enable_clocks(struct hda_codec *codec)
  1311. {
  1312. struct ca0132_spec *spec = codec->spec;
  1313. mutex_lock(&spec->chipio_mutex);
  1314. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  1315. VENDOR_CHIPIO_8051_ADDRESS_LOW, 0);
  1316. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  1317. VENDOR_CHIPIO_PLL_PMU_WRITE, 0xff);
  1318. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  1319. VENDOR_CHIPIO_8051_ADDRESS_LOW, 5);
  1320. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  1321. VENDOR_CHIPIO_PLL_PMU_WRITE, 0x0b);
  1322. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  1323. VENDOR_CHIPIO_8051_ADDRESS_LOW, 6);
  1324. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  1325. VENDOR_CHIPIO_PLL_PMU_WRITE, 0xff);
  1326. mutex_unlock(&spec->chipio_mutex);
  1327. }
  1328. /*
  1329. * CA0132 DSP IO stuffs
  1330. */
  1331. static int dspio_send(struct hda_codec *codec, unsigned int reg,
  1332. unsigned int data)
  1333. {
  1334. int res;
  1335. unsigned long timeout = jiffies + msecs_to_jiffies(1000);
  1336. /* send bits of data specified by reg to dsp */
  1337. do {
  1338. res = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0, reg, data);
  1339. if ((res >= 0) && (res != VENDOR_STATUS_DSPIO_BUSY))
  1340. return res;
  1341. msleep(20);
  1342. } while (time_before(jiffies, timeout));
  1343. return -EIO;
  1344. }
  1345. /*
  1346. * Wait for DSP to be ready for commands
  1347. */
  1348. static void dspio_write_wait(struct hda_codec *codec)
  1349. {
  1350. int status;
  1351. unsigned long timeout = jiffies + msecs_to_jiffies(1000);
  1352. do {
  1353. status = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0,
  1354. VENDOR_DSPIO_STATUS, 0);
  1355. if ((status == VENDOR_STATUS_DSPIO_OK) ||
  1356. (status == VENDOR_STATUS_DSPIO_SCP_RESPONSE_QUEUE_EMPTY))
  1357. break;
  1358. msleep(1);
  1359. } while (time_before(jiffies, timeout));
  1360. }
  1361. /*
  1362. * Write SCP data to DSP
  1363. */
  1364. static int dspio_write(struct hda_codec *codec, unsigned int scp_data)
  1365. {
  1366. struct ca0132_spec *spec = codec->spec;
  1367. int status;
  1368. dspio_write_wait(codec);
  1369. mutex_lock(&spec->chipio_mutex);
  1370. status = dspio_send(codec, VENDOR_DSPIO_SCP_WRITE_DATA_LOW,
  1371. scp_data & 0xffff);
  1372. if (status < 0)
  1373. goto error;
  1374. status = dspio_send(codec, VENDOR_DSPIO_SCP_WRITE_DATA_HIGH,
  1375. scp_data >> 16);
  1376. if (status < 0)
  1377. goto error;
  1378. /* OK, now check if the write itself has executed*/
  1379. status = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0,
  1380. VENDOR_DSPIO_STATUS, 0);
  1381. error:
  1382. mutex_unlock(&spec->chipio_mutex);
  1383. return (status == VENDOR_STATUS_DSPIO_SCP_COMMAND_QUEUE_FULL) ?
  1384. -EIO : 0;
  1385. }
  1386. /*
  1387. * Write multiple SCP data to DSP
  1388. */
  1389. static int dspio_write_multiple(struct hda_codec *codec,
  1390. unsigned int *buffer, unsigned int size)
  1391. {
  1392. int status = 0;
  1393. unsigned int count;
  1394. if (buffer == NULL)
  1395. return -EINVAL;
  1396. count = 0;
  1397. while (count < size) {
  1398. status = dspio_write(codec, *buffer++);
  1399. if (status != 0)
  1400. break;
  1401. count++;
  1402. }
  1403. return status;
  1404. }
  1405. static int dspio_read(struct hda_codec *codec, unsigned int *data)
  1406. {
  1407. int status;
  1408. status = dspio_send(codec, VENDOR_DSPIO_SCP_POST_READ_DATA, 0);
  1409. if (status == -EIO)
  1410. return status;
  1411. status = dspio_send(codec, VENDOR_DSPIO_STATUS, 0);
  1412. if (status == -EIO ||
  1413. status == VENDOR_STATUS_DSPIO_SCP_RESPONSE_QUEUE_EMPTY)
  1414. return -EIO;
  1415. *data = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0,
  1416. VENDOR_DSPIO_SCP_READ_DATA, 0);
  1417. return 0;
  1418. }
  1419. static int dspio_read_multiple(struct hda_codec *codec, unsigned int *buffer,
  1420. unsigned int *buf_size, unsigned int size_count)
  1421. {
  1422. int status = 0;
  1423. unsigned int size = *buf_size;
  1424. unsigned int count;
  1425. unsigned int skip_count;
  1426. unsigned int dummy;
  1427. if (buffer == NULL)
  1428. return -1;
  1429. count = 0;
  1430. while (count < size && count < size_count) {
  1431. status = dspio_read(codec, buffer++);
  1432. if (status != 0)
  1433. break;
  1434. count++;
  1435. }
  1436. skip_count = count;
  1437. if (status == 0) {
  1438. while (skip_count < size) {
  1439. status = dspio_read(codec, &dummy);
  1440. if (status != 0)
  1441. break;
  1442. skip_count++;
  1443. }
  1444. }
  1445. *buf_size = count;
  1446. return status;
  1447. }
  1448. /*
  1449. * Construct the SCP header using corresponding fields
  1450. */
  1451. static inline unsigned int
  1452. make_scp_header(unsigned int target_id, unsigned int source_id,
  1453. unsigned int get_flag, unsigned int req,
  1454. unsigned int device_flag, unsigned int resp_flag,
  1455. unsigned int error_flag, unsigned int data_size)
  1456. {
  1457. unsigned int header = 0;
  1458. header = (data_size & 0x1f) << 27;
  1459. header |= (error_flag & 0x01) << 26;
  1460. header |= (resp_flag & 0x01) << 25;
  1461. header |= (device_flag & 0x01) << 24;
  1462. header |= (req & 0x7f) << 17;
  1463. header |= (get_flag & 0x01) << 16;
  1464. header |= (source_id & 0xff) << 8;
  1465. header |= target_id & 0xff;
  1466. return header;
  1467. }
  1468. /*
  1469. * Extract corresponding fields from SCP header
  1470. */
  1471. static inline void
  1472. extract_scp_header(unsigned int header,
  1473. unsigned int *target_id, unsigned int *source_id,
  1474. unsigned int *get_flag, unsigned int *req,
  1475. unsigned int *device_flag, unsigned int *resp_flag,
  1476. unsigned int *error_flag, unsigned int *data_size)
  1477. {
  1478. if (data_size)
  1479. *data_size = (header >> 27) & 0x1f;
  1480. if (error_flag)
  1481. *error_flag = (header >> 26) & 0x01;
  1482. if (resp_flag)
  1483. *resp_flag = (header >> 25) & 0x01;
  1484. if (device_flag)
  1485. *device_flag = (header >> 24) & 0x01;
  1486. if (req)
  1487. *req = (header >> 17) & 0x7f;
  1488. if (get_flag)
  1489. *get_flag = (header >> 16) & 0x01;
  1490. if (source_id)
  1491. *source_id = (header >> 8) & 0xff;
  1492. if (target_id)
  1493. *target_id = header & 0xff;
  1494. }
  1495. #define SCP_MAX_DATA_WORDS (16)
  1496. /* Structure to contain any SCP message */
  1497. struct scp_msg {
  1498. unsigned int hdr;
  1499. unsigned int data[SCP_MAX_DATA_WORDS];
  1500. };
  1501. static void dspio_clear_response_queue(struct hda_codec *codec)
  1502. {
  1503. unsigned long timeout = jiffies + msecs_to_jiffies(1000);
  1504. unsigned int dummy = 0;
  1505. int status;
  1506. /* clear all from the response queue */
  1507. do {
  1508. status = dspio_read(codec, &dummy);
  1509. } while (status == 0 && time_before(jiffies, timeout));
  1510. }
  1511. static int dspio_get_response_data(struct hda_codec *codec)
  1512. {
  1513. struct ca0132_spec *spec = codec->spec;
  1514. unsigned int data = 0;
  1515. unsigned int count;
  1516. if (dspio_read(codec, &data) < 0)
  1517. return -EIO;
  1518. if ((data & 0x00ffffff) == spec->wait_scp_header) {
  1519. spec->scp_resp_header = data;
  1520. spec->scp_resp_count = data >> 27;
  1521. count = spec->wait_num_data;
  1522. dspio_read_multiple(codec, spec->scp_resp_data,
  1523. &spec->scp_resp_count, count);
  1524. return 0;
  1525. }
  1526. return -EIO;
  1527. }
  1528. /*
  1529. * Send SCP message to DSP
  1530. */
  1531. static int dspio_send_scp_message(struct hda_codec *codec,
  1532. unsigned char *send_buf,
  1533. unsigned int send_buf_size,
  1534. unsigned char *return_buf,
  1535. unsigned int return_buf_size,
  1536. unsigned int *bytes_returned)
  1537. {
  1538. struct ca0132_spec *spec = codec->spec;
  1539. int status = -1;
  1540. unsigned int scp_send_size = 0;
  1541. unsigned int total_size;
  1542. bool waiting_for_resp = false;
  1543. unsigned int header;
  1544. struct scp_msg *ret_msg;
  1545. unsigned int resp_src_id, resp_target_id;
  1546. unsigned int data_size, src_id, target_id, get_flag, device_flag;
  1547. if (bytes_returned)
  1548. *bytes_returned = 0;
  1549. /* get scp header from buffer */
  1550. header = *((unsigned int *)send_buf);
  1551. extract_scp_header(header, &target_id, &src_id, &get_flag, NULL,
  1552. &device_flag, NULL, NULL, &data_size);
  1553. scp_send_size = data_size + 1;
  1554. total_size = (scp_send_size * 4);
  1555. if (send_buf_size < total_size)
  1556. return -EINVAL;
  1557. if (get_flag || device_flag) {
  1558. if (!return_buf || return_buf_size < 4 || !bytes_returned)
  1559. return -EINVAL;
  1560. spec->wait_scp_header = *((unsigned int *)send_buf);
  1561. /* swap source id with target id */
  1562. resp_target_id = src_id;
  1563. resp_src_id = target_id;
  1564. spec->wait_scp_header &= 0xffff0000;
  1565. spec->wait_scp_header |= (resp_src_id << 8) | (resp_target_id);
  1566. spec->wait_num_data = return_buf_size/sizeof(unsigned int) - 1;
  1567. spec->wait_scp = 1;
  1568. waiting_for_resp = true;
  1569. }
  1570. status = dspio_write_multiple(codec, (unsigned int *)send_buf,
  1571. scp_send_size);
  1572. if (status < 0) {
  1573. spec->wait_scp = 0;
  1574. return status;
  1575. }
  1576. if (waiting_for_resp) {
  1577. unsigned long timeout = jiffies + msecs_to_jiffies(1000);
  1578. memset(return_buf, 0, return_buf_size);
  1579. do {
  1580. msleep(20);
  1581. } while (spec->wait_scp && time_before(jiffies, timeout));
  1582. waiting_for_resp = false;
  1583. if (!spec->wait_scp) {
  1584. ret_msg = (struct scp_msg *)return_buf;
  1585. memcpy(&ret_msg->hdr, &spec->scp_resp_header, 4);
  1586. memcpy(&ret_msg->data, spec->scp_resp_data,
  1587. spec->wait_num_data);
  1588. *bytes_returned = (spec->scp_resp_count + 1) * 4;
  1589. status = 0;
  1590. } else {
  1591. status = -EIO;
  1592. }
  1593. spec->wait_scp = 0;
  1594. }
  1595. return status;
  1596. }
  1597. /**
  1598. * Prepare and send the SCP message to DSP
  1599. * @codec: the HDA codec
  1600. * @mod_id: ID of the DSP module to send the command
  1601. * @req: ID of request to send to the DSP module
  1602. * @dir: SET or GET
  1603. * @data: pointer to the data to send with the request, request specific
  1604. * @len: length of the data, in bytes
  1605. * @reply: point to the buffer to hold data returned for a reply
  1606. * @reply_len: length of the reply buffer returned from GET
  1607. *
  1608. * Returns zero or a negative error code.
  1609. */
  1610. static int dspio_scp(struct hda_codec *codec,
  1611. int mod_id, int src_id, int req, int dir, const void *data,
  1612. unsigned int len, void *reply, unsigned int *reply_len)
  1613. {
  1614. int status = 0;
  1615. struct scp_msg scp_send, scp_reply;
  1616. unsigned int ret_bytes, send_size, ret_size;
  1617. unsigned int send_get_flag, reply_resp_flag, reply_error_flag;
  1618. unsigned int reply_data_size;
  1619. memset(&scp_send, 0, sizeof(scp_send));
  1620. memset(&scp_reply, 0, sizeof(scp_reply));
  1621. if ((len != 0 && data == NULL) || (len > SCP_MAX_DATA_WORDS))
  1622. return -EINVAL;
  1623. if (dir == SCP_GET && reply == NULL) {
  1624. codec_dbg(codec, "dspio_scp get but has no buffer\n");
  1625. return -EINVAL;
  1626. }
  1627. if (reply != NULL && (reply_len == NULL || (*reply_len == 0))) {
  1628. codec_dbg(codec, "dspio_scp bad resp buf len parms\n");
  1629. return -EINVAL;
  1630. }
  1631. scp_send.hdr = make_scp_header(mod_id, src_id, (dir == SCP_GET), req,
  1632. 0, 0, 0, len/sizeof(unsigned int));
  1633. if (data != NULL && len > 0) {
  1634. len = min((unsigned int)(sizeof(scp_send.data)), len);
  1635. memcpy(scp_send.data, data, len);
  1636. }
  1637. ret_bytes = 0;
  1638. send_size = sizeof(unsigned int) + len;
  1639. status = dspio_send_scp_message(codec, (unsigned char *)&scp_send,
  1640. send_size, (unsigned char *)&scp_reply,
  1641. sizeof(scp_reply), &ret_bytes);
  1642. if (status < 0) {
  1643. codec_dbg(codec, "dspio_scp: send scp msg failed\n");
  1644. return status;
  1645. }
  1646. /* extract send and reply headers members */
  1647. extract_scp_header(scp_send.hdr, NULL, NULL, &send_get_flag,
  1648. NULL, NULL, NULL, NULL, NULL);
  1649. extract_scp_header(scp_reply.hdr, NULL, NULL, NULL, NULL, NULL,
  1650. &reply_resp_flag, &reply_error_flag,
  1651. &reply_data_size);
  1652. if (!send_get_flag)
  1653. return 0;
  1654. if (reply_resp_flag && !reply_error_flag) {
  1655. ret_size = (ret_bytes - sizeof(scp_reply.hdr))
  1656. / sizeof(unsigned int);
  1657. if (*reply_len < ret_size*sizeof(unsigned int)) {
  1658. codec_dbg(codec, "reply too long for buf\n");
  1659. return -EINVAL;
  1660. } else if (ret_size != reply_data_size) {
  1661. codec_dbg(codec, "RetLen and HdrLen .NE.\n");
  1662. return -EINVAL;
  1663. } else if (!reply) {
  1664. codec_dbg(codec, "NULL reply\n");
  1665. return -EINVAL;
  1666. } else {
  1667. *reply_len = ret_size*sizeof(unsigned int);
  1668. memcpy(reply, scp_reply.data, *reply_len);
  1669. }
  1670. } else {
  1671. codec_dbg(codec, "reply ill-formed or errflag set\n");
  1672. return -EIO;
  1673. }
  1674. return status;
  1675. }
  1676. /*
  1677. * Set DSP parameters
  1678. */
  1679. static int dspio_set_param(struct hda_codec *codec, int mod_id,
  1680. int src_id, int req, const void *data, unsigned int len)
  1681. {
  1682. return dspio_scp(codec, mod_id, src_id, req, SCP_SET, data, len, NULL,
  1683. NULL);
  1684. }
  1685. static int dspio_set_uint_param(struct hda_codec *codec, int mod_id,
  1686. int req, const unsigned int data)
  1687. {
  1688. return dspio_set_param(codec, mod_id, 0x20, req, &data,
  1689. sizeof(unsigned int));
  1690. }
  1691. static int dspio_set_uint_param_no_source(struct hda_codec *codec, int mod_id,
  1692. int req, const unsigned int data)
  1693. {
  1694. return dspio_set_param(codec, mod_id, 0x00, req, &data,
  1695. sizeof(unsigned int));
  1696. }
  1697. /*
  1698. * Allocate a DSP DMA channel via an SCP message
  1699. */
  1700. static int dspio_alloc_dma_chan(struct hda_codec *codec, unsigned int *dma_chan)
  1701. {
  1702. int status = 0;
  1703. unsigned int size = sizeof(dma_chan);
  1704. codec_dbg(codec, " dspio_alloc_dma_chan() -- begin\n");
  1705. status = dspio_scp(codec, MASTERCONTROL, 0x20,
  1706. MASTERCONTROL_ALLOC_DMA_CHAN, SCP_GET, NULL, 0,
  1707. dma_chan, &size);
  1708. if (status < 0) {
  1709. codec_dbg(codec, "dspio_alloc_dma_chan: SCP Failed\n");
  1710. return status;
  1711. }
  1712. if ((*dma_chan + 1) == 0) {
  1713. codec_dbg(codec, "no free dma channels to allocate\n");
  1714. return -EBUSY;
  1715. }
  1716. codec_dbg(codec, "dspio_alloc_dma_chan: chan=%d\n", *dma_chan);
  1717. codec_dbg(codec, " dspio_alloc_dma_chan() -- complete\n");
  1718. return status;
  1719. }
  1720. /*
  1721. * Free a DSP DMA via an SCP message
  1722. */
  1723. static int dspio_free_dma_chan(struct hda_codec *codec, unsigned int dma_chan)
  1724. {
  1725. int status = 0;
  1726. unsigned int dummy = 0;
  1727. codec_dbg(codec, " dspio_free_dma_chan() -- begin\n");
  1728. codec_dbg(codec, "dspio_free_dma_chan: chan=%d\n", dma_chan);
  1729. status = dspio_scp(codec, MASTERCONTROL, 0x20,
  1730. MASTERCONTROL_ALLOC_DMA_CHAN, SCP_SET, &dma_chan,
  1731. sizeof(dma_chan), NULL, &dummy);
  1732. if (status < 0) {
  1733. codec_dbg(codec, "dspio_free_dma_chan: SCP Failed\n");
  1734. return status;
  1735. }
  1736. codec_dbg(codec, " dspio_free_dma_chan() -- complete\n");
  1737. return status;
  1738. }
  1739. /*
  1740. * (Re)start the DSP
  1741. */
  1742. static int dsp_set_run_state(struct hda_codec *codec)
  1743. {
  1744. unsigned int dbg_ctrl_reg;
  1745. unsigned int halt_state;
  1746. int err;
  1747. err = chipio_read(codec, DSP_DBGCNTL_INST_OFFSET, &dbg_ctrl_reg);
  1748. if (err < 0)
  1749. return err;
  1750. halt_state = (dbg_ctrl_reg & DSP_DBGCNTL_STATE_MASK) >>
  1751. DSP_DBGCNTL_STATE_LOBIT;
  1752. if (halt_state != 0) {
  1753. dbg_ctrl_reg &= ~((halt_state << DSP_DBGCNTL_SS_LOBIT) &
  1754. DSP_DBGCNTL_SS_MASK);
  1755. err = chipio_write(codec, DSP_DBGCNTL_INST_OFFSET,
  1756. dbg_ctrl_reg);
  1757. if (err < 0)
  1758. return err;
  1759. dbg_ctrl_reg |= (halt_state << DSP_DBGCNTL_EXEC_LOBIT) &
  1760. DSP_DBGCNTL_EXEC_MASK;
  1761. err = chipio_write(codec, DSP_DBGCNTL_INST_OFFSET,
  1762. dbg_ctrl_reg);
  1763. if (err < 0)
  1764. return err;
  1765. }
  1766. return 0;
  1767. }
  1768. /*
  1769. * Reset the DSP
  1770. */
  1771. static int dsp_reset(struct hda_codec *codec)
  1772. {
  1773. unsigned int res;
  1774. int retry = 20;
  1775. codec_dbg(codec, "dsp_reset\n");
  1776. do {
  1777. res = dspio_send(codec, VENDOR_DSPIO_DSP_INIT, 0);
  1778. retry--;
  1779. } while (res == -EIO && retry);
  1780. if (!retry) {
  1781. codec_dbg(codec, "dsp_reset timeout\n");
  1782. return -EIO;
  1783. }
  1784. return 0;
  1785. }
  1786. /*
  1787. * Convert chip address to DSP address
  1788. */
  1789. static unsigned int dsp_chip_to_dsp_addx(unsigned int chip_addx,
  1790. bool *code, bool *yram)
  1791. {
  1792. *code = *yram = false;
  1793. if (UC_RANGE(chip_addx, 1)) {
  1794. *code = true;
  1795. return UC_OFF(chip_addx);
  1796. } else if (X_RANGE_ALL(chip_addx, 1)) {
  1797. return X_OFF(chip_addx);
  1798. } else if (Y_RANGE_ALL(chip_addx, 1)) {
  1799. *yram = true;
  1800. return Y_OFF(chip_addx);
  1801. }
  1802. return INVALID_CHIP_ADDRESS;
  1803. }
  1804. /*
  1805. * Check if the DSP DMA is active
  1806. */
  1807. static bool dsp_is_dma_active(struct hda_codec *codec, unsigned int dma_chan)
  1808. {
  1809. unsigned int dma_chnlstart_reg;
  1810. chipio_read(codec, DSPDMAC_CHNLSTART_INST_OFFSET, &dma_chnlstart_reg);
  1811. return ((dma_chnlstart_reg & (1 <<
  1812. (DSPDMAC_CHNLSTART_EN_LOBIT + dma_chan))) != 0);
  1813. }
  1814. static int dsp_dma_setup_common(struct hda_codec *codec,
  1815. unsigned int chip_addx,
  1816. unsigned int dma_chan,
  1817. unsigned int port_map_mask,
  1818. bool ovly)
  1819. {
  1820. int status = 0;
  1821. unsigned int chnl_prop;
  1822. unsigned int dsp_addx;
  1823. unsigned int active;
  1824. bool code, yram;
  1825. codec_dbg(codec, "-- dsp_dma_setup_common() -- Begin ---------\n");
  1826. if (dma_chan >= DSPDMAC_DMA_CFG_CHANNEL_COUNT) {
  1827. codec_dbg(codec, "dma chan num invalid\n");
  1828. return -EINVAL;
  1829. }
  1830. if (dsp_is_dma_active(codec, dma_chan)) {
  1831. codec_dbg(codec, "dma already active\n");
  1832. return -EBUSY;
  1833. }
  1834. dsp_addx = dsp_chip_to_dsp_addx(chip_addx, &code, &yram);
  1835. if (dsp_addx == INVALID_CHIP_ADDRESS) {
  1836. codec_dbg(codec, "invalid chip addr\n");
  1837. return -ENXIO;
  1838. }
  1839. chnl_prop = DSPDMAC_CHNLPROP_AC_MASK;
  1840. active = 0;
  1841. codec_dbg(codec, " dsp_dma_setup_common() start reg pgm\n");
  1842. if (ovly) {
  1843. status = chipio_read(codec, DSPDMAC_CHNLPROP_INST_OFFSET,
  1844. &chnl_prop);
  1845. if (status < 0) {
  1846. codec_dbg(codec, "read CHNLPROP Reg fail\n");
  1847. return status;
  1848. }
  1849. codec_dbg(codec, "dsp_dma_setup_common() Read CHNLPROP\n");
  1850. }
  1851. if (!code)
  1852. chnl_prop &= ~(1 << (DSPDMAC_CHNLPROP_MSPCE_LOBIT + dma_chan));
  1853. else
  1854. chnl_prop |= (1 << (DSPDMAC_CHNLPROP_MSPCE_LOBIT + dma_chan));
  1855. chnl_prop &= ~(1 << (DSPDMAC_CHNLPROP_DCON_LOBIT + dma_chan));
  1856. status = chipio_write(codec, DSPDMAC_CHNLPROP_INST_OFFSET, chnl_prop);
  1857. if (status < 0) {
  1858. codec_dbg(codec, "write CHNLPROP Reg fail\n");
  1859. return status;
  1860. }
  1861. codec_dbg(codec, " dsp_dma_setup_common() Write CHNLPROP\n");
  1862. if (ovly) {
  1863. status = chipio_read(codec, DSPDMAC_ACTIVE_INST_OFFSET,
  1864. &active);
  1865. if (status < 0) {
  1866. codec_dbg(codec, "read ACTIVE Reg fail\n");
  1867. return status;
  1868. }
  1869. codec_dbg(codec, "dsp_dma_setup_common() Read ACTIVE\n");
  1870. }
  1871. active &= (~(1 << (DSPDMAC_ACTIVE_AAR_LOBIT + dma_chan))) &
  1872. DSPDMAC_ACTIVE_AAR_MASK;
  1873. status = chipio_write(codec, DSPDMAC_ACTIVE_INST_OFFSET, active);
  1874. if (status < 0) {
  1875. codec_dbg(codec, "write ACTIVE Reg fail\n");
  1876. return status;
  1877. }
  1878. codec_dbg(codec, " dsp_dma_setup_common() Write ACTIVE\n");
  1879. status = chipio_write(codec, DSPDMAC_AUDCHSEL_INST_OFFSET(dma_chan),
  1880. port_map_mask);
  1881. if (status < 0) {
  1882. codec_dbg(codec, "write AUDCHSEL Reg fail\n");
  1883. return status;
  1884. }
  1885. codec_dbg(codec, " dsp_dma_setup_common() Write AUDCHSEL\n");
  1886. status = chipio_write(codec, DSPDMAC_IRQCNT_INST_OFFSET(dma_chan),
  1887. DSPDMAC_IRQCNT_BICNT_MASK | DSPDMAC_IRQCNT_CICNT_MASK);
  1888. if (status < 0) {
  1889. codec_dbg(codec, "write IRQCNT Reg fail\n");
  1890. return status;
  1891. }
  1892. codec_dbg(codec, " dsp_dma_setup_common() Write IRQCNT\n");
  1893. codec_dbg(codec,
  1894. "ChipA=0x%x,DspA=0x%x,dmaCh=%u, "
  1895. "CHSEL=0x%x,CHPROP=0x%x,Active=0x%x\n",
  1896. chip_addx, dsp_addx, dma_chan,
  1897. port_map_mask, chnl_prop, active);
  1898. codec_dbg(codec, "-- dsp_dma_setup_common() -- Complete ------\n");
  1899. return 0;
  1900. }
  1901. /*
  1902. * Setup the DSP DMA per-transfer-specific registers
  1903. */
  1904. static int dsp_dma_setup(struct hda_codec *codec,
  1905. unsigned int chip_addx,
  1906. unsigned int count,
  1907. unsigned int dma_chan)
  1908. {
  1909. int status = 0;
  1910. bool code, yram;
  1911. unsigned int dsp_addx;
  1912. unsigned int addr_field;
  1913. unsigned int incr_field;
  1914. unsigned int base_cnt;
  1915. unsigned int cur_cnt;
  1916. unsigned int dma_cfg = 0;
  1917. unsigned int adr_ofs = 0;
  1918. unsigned int xfr_cnt = 0;
  1919. const unsigned int max_dma_count = 1 << (DSPDMAC_XFRCNT_BCNT_HIBIT -
  1920. DSPDMAC_XFRCNT_BCNT_LOBIT + 1);
  1921. codec_dbg(codec, "-- dsp_dma_setup() -- Begin ---------\n");
  1922. if (count > max_dma_count) {
  1923. codec_dbg(codec, "count too big\n");
  1924. return -EINVAL;
  1925. }
  1926. dsp_addx = dsp_chip_to_dsp_addx(chip_addx, &code, &yram);
  1927. if (dsp_addx == INVALID_CHIP_ADDRESS) {
  1928. codec_dbg(codec, "invalid chip addr\n");
  1929. return -ENXIO;
  1930. }
  1931. codec_dbg(codec, " dsp_dma_setup() start reg pgm\n");
  1932. addr_field = dsp_addx << DSPDMAC_DMACFG_DBADR_LOBIT;
  1933. incr_field = 0;
  1934. if (!code) {
  1935. addr_field <<= 1;
  1936. if (yram)
  1937. addr_field |= (1 << DSPDMAC_DMACFG_DBADR_LOBIT);
  1938. incr_field = (1 << DSPDMAC_DMACFG_AINCR_LOBIT);
  1939. }
  1940. dma_cfg = addr_field + incr_field;
  1941. status = chipio_write(codec, DSPDMAC_DMACFG_INST_OFFSET(dma_chan),
  1942. dma_cfg);
  1943. if (status < 0) {
  1944. codec_dbg(codec, "write DMACFG Reg fail\n");
  1945. return status;
  1946. }
  1947. codec_dbg(codec, " dsp_dma_setup() Write DMACFG\n");
  1948. adr_ofs = (count - 1) << (DSPDMAC_DSPADROFS_BOFS_LOBIT +
  1949. (code ? 0 : 1));
  1950. status = chipio_write(codec, DSPDMAC_DSPADROFS_INST_OFFSET(dma_chan),
  1951. adr_ofs);
  1952. if (status < 0) {
  1953. codec_dbg(codec, "write DSPADROFS Reg fail\n");
  1954. return status;
  1955. }
  1956. codec_dbg(codec, " dsp_dma_setup() Write DSPADROFS\n");
  1957. base_cnt = (count - 1) << DSPDMAC_XFRCNT_BCNT_LOBIT;
  1958. cur_cnt = (count - 1) << DSPDMAC_XFRCNT_CCNT_LOBIT;
  1959. xfr_cnt = base_cnt | cur_cnt;
  1960. status = chipio_write(codec,
  1961. DSPDMAC_XFRCNT_INST_OFFSET(dma_chan), xfr_cnt);
  1962. if (status < 0) {
  1963. codec_dbg(codec, "write XFRCNT Reg fail\n");
  1964. return status;
  1965. }
  1966. codec_dbg(codec, " dsp_dma_setup() Write XFRCNT\n");
  1967. codec_dbg(codec,
  1968. "ChipA=0x%x, cnt=0x%x, DMACFG=0x%x, "
  1969. "ADROFS=0x%x, XFRCNT=0x%x\n",
  1970. chip_addx, count, dma_cfg, adr_ofs, xfr_cnt);
  1971. codec_dbg(codec, "-- dsp_dma_setup() -- Complete ---------\n");
  1972. return 0;
  1973. }
  1974. /*
  1975. * Start the DSP DMA
  1976. */
  1977. static int dsp_dma_start(struct hda_codec *codec,
  1978. unsigned int dma_chan, bool ovly)
  1979. {
  1980. unsigned int reg = 0;
  1981. int status = 0;
  1982. codec_dbg(codec, "-- dsp_dma_start() -- Begin ---------\n");
  1983. if (ovly) {
  1984. status = chipio_read(codec,
  1985. DSPDMAC_CHNLSTART_INST_OFFSET, &reg);
  1986. if (status < 0) {
  1987. codec_dbg(codec, "read CHNLSTART reg fail\n");
  1988. return status;
  1989. }
  1990. codec_dbg(codec, "-- dsp_dma_start() Read CHNLSTART\n");
  1991. reg &= ~(DSPDMAC_CHNLSTART_EN_MASK |
  1992. DSPDMAC_CHNLSTART_DIS_MASK);
  1993. }
  1994. status = chipio_write(codec, DSPDMAC_CHNLSTART_INST_OFFSET,
  1995. reg | (1 << (dma_chan + DSPDMAC_CHNLSTART_EN_LOBIT)));
  1996. if (status < 0) {
  1997. codec_dbg(codec, "write CHNLSTART reg fail\n");
  1998. return status;
  1999. }
  2000. codec_dbg(codec, "-- dsp_dma_start() -- Complete ---------\n");
  2001. return status;
  2002. }
  2003. /*
  2004. * Stop the DSP DMA
  2005. */
  2006. static int dsp_dma_stop(struct hda_codec *codec,
  2007. unsigned int dma_chan, bool ovly)
  2008. {
  2009. unsigned int reg = 0;
  2010. int status = 0;
  2011. codec_dbg(codec, "-- dsp_dma_stop() -- Begin ---------\n");
  2012. if (ovly) {
  2013. status = chipio_read(codec,
  2014. DSPDMAC_CHNLSTART_INST_OFFSET, &reg);
  2015. if (status < 0) {
  2016. codec_dbg(codec, "read CHNLSTART reg fail\n");
  2017. return status;
  2018. }
  2019. codec_dbg(codec, "-- dsp_dma_stop() Read CHNLSTART\n");
  2020. reg &= ~(DSPDMAC_CHNLSTART_EN_MASK |
  2021. DSPDMAC_CHNLSTART_DIS_MASK);
  2022. }
  2023. status = chipio_write(codec, DSPDMAC_CHNLSTART_INST_OFFSET,
  2024. reg | (1 << (dma_chan + DSPDMAC_CHNLSTART_DIS_LOBIT)));
  2025. if (status < 0) {
  2026. codec_dbg(codec, "write CHNLSTART reg fail\n");
  2027. return status;
  2028. }
  2029. codec_dbg(codec, "-- dsp_dma_stop() -- Complete ---------\n");
  2030. return status;
  2031. }
  2032. /**
  2033. * Allocate router ports
  2034. *
  2035. * @codec: the HDA codec
  2036. * @num_chans: number of channels in the stream
  2037. * @ports_per_channel: number of ports per channel
  2038. * @start_device: start device
  2039. * @port_map: pointer to the port list to hold the allocated ports
  2040. *
  2041. * Returns zero or a negative error code.
  2042. */
  2043. static int dsp_allocate_router_ports(struct hda_codec *codec,
  2044. unsigned int num_chans,
  2045. unsigned int ports_per_channel,
  2046. unsigned int start_device,
  2047. unsigned int *port_map)
  2048. {
  2049. int status = 0;
  2050. int res;
  2051. u8 val;
  2052. status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
  2053. if (status < 0)
  2054. return status;
  2055. val = start_device << 6;
  2056. val |= (ports_per_channel - 1) << 4;
  2057. val |= num_chans - 1;
  2058. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  2059. VENDOR_CHIPIO_PORT_ALLOC_CONFIG_SET,
  2060. val);
  2061. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  2062. VENDOR_CHIPIO_PORT_ALLOC_SET,
  2063. MEM_CONNID_DSP);
  2064. status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
  2065. if (status < 0)
  2066. return status;
  2067. res = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
  2068. VENDOR_CHIPIO_PORT_ALLOC_GET, 0);
  2069. *port_map = res;
  2070. return (res < 0) ? res : 0;
  2071. }
  2072. /*
  2073. * Free router ports
  2074. */
  2075. static int dsp_free_router_ports(struct hda_codec *codec)
  2076. {
  2077. int status = 0;
  2078. status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
  2079. if (status < 0)
  2080. return status;
  2081. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  2082. VENDOR_CHIPIO_PORT_FREE_SET,
  2083. MEM_CONNID_DSP);
  2084. status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
  2085. return status;
  2086. }
  2087. /*
  2088. * Allocate DSP ports for the download stream
  2089. */
  2090. static int dsp_allocate_ports(struct hda_codec *codec,
  2091. unsigned int num_chans,
  2092. unsigned int rate_multi, unsigned int *port_map)
  2093. {
  2094. int status;
  2095. codec_dbg(codec, " dsp_allocate_ports() -- begin\n");
  2096. if ((rate_multi != 1) && (rate_multi != 2) && (rate_multi != 4)) {
  2097. codec_dbg(codec, "bad rate multiple\n");
  2098. return -EINVAL;
  2099. }
  2100. status = dsp_allocate_router_ports(codec, num_chans,
  2101. rate_multi, 0, port_map);
  2102. codec_dbg(codec, " dsp_allocate_ports() -- complete\n");
  2103. return status;
  2104. }
  2105. static int dsp_allocate_ports_format(struct hda_codec *codec,
  2106. const unsigned short fmt,
  2107. unsigned int *port_map)
  2108. {
  2109. int status;
  2110. unsigned int num_chans;
  2111. unsigned int sample_rate_div = ((get_hdafmt_rate(fmt) >> 0) & 3) + 1;
  2112. unsigned int sample_rate_mul = ((get_hdafmt_rate(fmt) >> 3) & 3) + 1;
  2113. unsigned int rate_multi = sample_rate_mul / sample_rate_div;
  2114. if ((rate_multi != 1) && (rate_multi != 2) && (rate_multi != 4)) {
  2115. codec_dbg(codec, "bad rate multiple\n");
  2116. return -EINVAL;
  2117. }
  2118. num_chans = get_hdafmt_chs(fmt) + 1;
  2119. status = dsp_allocate_ports(codec, num_chans, rate_multi, port_map);
  2120. return status;
  2121. }
  2122. /*
  2123. * free DSP ports
  2124. */
  2125. static int dsp_free_ports(struct hda_codec *codec)
  2126. {
  2127. int status;
  2128. codec_dbg(codec, " dsp_free_ports() -- begin\n");
  2129. status = dsp_free_router_ports(codec);
  2130. if (status < 0) {
  2131. codec_dbg(codec, "free router ports fail\n");
  2132. return status;
  2133. }
  2134. codec_dbg(codec, " dsp_free_ports() -- complete\n");
  2135. return status;
  2136. }
  2137. /*
  2138. * HDA DMA engine stuffs for DSP code download
  2139. */
  2140. struct dma_engine {
  2141. struct hda_codec *codec;
  2142. unsigned short m_converter_format;
  2143. struct snd_dma_buffer *dmab;
  2144. unsigned int buf_size;
  2145. };
  2146. enum dma_state {
  2147. DMA_STATE_STOP = 0,
  2148. DMA_STATE_RUN = 1
  2149. };
  2150. static int dma_convert_to_hda_format(struct hda_codec *codec,
  2151. unsigned int sample_rate,
  2152. unsigned short channels,
  2153. unsigned short *hda_format)
  2154. {
  2155. unsigned int format_val;
  2156. format_val = snd_hdac_calc_stream_format(sample_rate,
  2157. channels, SNDRV_PCM_FORMAT_S32_LE, 32, 0);
  2158. if (hda_format)
  2159. *hda_format = (unsigned short)format_val;
  2160. return 0;
  2161. }
  2162. /*
  2163. * Reset DMA for DSP download
  2164. */
  2165. static int dma_reset(struct dma_engine *dma)
  2166. {
  2167. struct hda_codec *codec = dma->codec;
  2168. struct ca0132_spec *spec = codec->spec;
  2169. int status;
  2170. if (dma->dmab->area)
  2171. snd_hda_codec_load_dsp_cleanup(codec, dma->dmab);
  2172. status = snd_hda_codec_load_dsp_prepare(codec,
  2173. dma->m_converter_format,
  2174. dma->buf_size,
  2175. dma->dmab);
  2176. if (status < 0)
  2177. return status;
  2178. spec->dsp_stream_id = status;
  2179. return 0;
  2180. }
  2181. static int dma_set_state(struct dma_engine *dma, enum dma_state state)
  2182. {
  2183. bool cmd;
  2184. switch (state) {
  2185. case DMA_STATE_STOP:
  2186. cmd = false;
  2187. break;
  2188. case DMA_STATE_RUN:
  2189. cmd = true;
  2190. break;
  2191. default:
  2192. return 0;
  2193. }
  2194. snd_hda_codec_load_dsp_trigger(dma->codec, cmd);
  2195. return 0;
  2196. }
  2197. static unsigned int dma_get_buffer_size(struct dma_engine *dma)
  2198. {
  2199. return dma->dmab->bytes;
  2200. }
  2201. static unsigned char *dma_get_buffer_addr(struct dma_engine *dma)
  2202. {
  2203. return dma->dmab->area;
  2204. }
  2205. static int dma_xfer(struct dma_engine *dma,
  2206. const unsigned int *data,
  2207. unsigned int count)
  2208. {
  2209. memcpy(dma->dmab->area, data, count);
  2210. return 0;
  2211. }
  2212. static void dma_get_converter_format(
  2213. struct dma_engine *dma,
  2214. unsigned short *format)
  2215. {
  2216. if (format)
  2217. *format = dma->m_converter_format;
  2218. }
  2219. static unsigned int dma_get_stream_id(struct dma_engine *dma)
  2220. {
  2221. struct ca0132_spec *spec = dma->codec->spec;
  2222. return spec->dsp_stream_id;
  2223. }
  2224. struct dsp_image_seg {
  2225. u32 magic;
  2226. u32 chip_addr;
  2227. u32 count;
  2228. u32 data[0];
  2229. };
  2230. static const u32 g_magic_value = 0x4c46584d;
  2231. static const u32 g_chip_addr_magic_value = 0xFFFFFF01;
  2232. static bool is_valid(const struct dsp_image_seg *p)
  2233. {
  2234. return p->magic == g_magic_value;
  2235. }
  2236. static bool is_hci_prog_list_seg(const struct dsp_image_seg *p)
  2237. {
  2238. return g_chip_addr_magic_value == p->chip_addr;
  2239. }
  2240. static bool is_last(const struct dsp_image_seg *p)
  2241. {
  2242. return p->count == 0;
  2243. }
  2244. static size_t dsp_sizeof(const struct dsp_image_seg *p)
  2245. {
  2246. return sizeof(*p) + p->count*sizeof(u32);
  2247. }
  2248. static const struct dsp_image_seg *get_next_seg_ptr(
  2249. const struct dsp_image_seg *p)
  2250. {
  2251. return (struct dsp_image_seg *)((unsigned char *)(p) + dsp_sizeof(p));
  2252. }
  2253. /*
  2254. * CA0132 chip DSP transfer stuffs. For DSP download.
  2255. */
  2256. #define INVALID_DMA_CHANNEL (~0U)
  2257. /*
  2258. * Program a list of address/data pairs via the ChipIO widget.
  2259. * The segment data is in the format of successive pairs of words.
  2260. * These are repeated as indicated by the segment's count field.
  2261. */
  2262. static int dspxfr_hci_write(struct hda_codec *codec,
  2263. const struct dsp_image_seg *fls)
  2264. {
  2265. int status;
  2266. const u32 *data;
  2267. unsigned int count;
  2268. if (fls == NULL || fls->chip_addr != g_chip_addr_magic_value) {
  2269. codec_dbg(codec, "hci_write invalid params\n");
  2270. return -EINVAL;
  2271. }
  2272. count = fls->count;
  2273. data = (u32 *)(fls->data);
  2274. while (count >= 2) {
  2275. status = chipio_write(codec, data[0], data[1]);
  2276. if (status < 0) {
  2277. codec_dbg(codec, "hci_write chipio failed\n");
  2278. return status;
  2279. }
  2280. count -= 2;
  2281. data += 2;
  2282. }
  2283. return 0;
  2284. }
  2285. /**
  2286. * Write a block of data into DSP code or data RAM using pre-allocated
  2287. * DMA engine.
  2288. *
  2289. * @codec: the HDA codec
  2290. * @fls: pointer to a fast load image
  2291. * @reloc: Relocation address for loading single-segment overlays, or 0 for
  2292. * no relocation
  2293. * @dma_engine: pointer to DMA engine to be used for DSP download
  2294. * @dma_chan: The number of DMA channels used for DSP download
  2295. * @port_map_mask: port mapping
  2296. * @ovly: TRUE if overlay format is required
  2297. *
  2298. * Returns zero or a negative error code.
  2299. */
  2300. static int dspxfr_one_seg(struct hda_codec *codec,
  2301. const struct dsp_image_seg *fls,
  2302. unsigned int reloc,
  2303. struct dma_engine *dma_engine,
  2304. unsigned int dma_chan,
  2305. unsigned int port_map_mask,
  2306. bool ovly)
  2307. {
  2308. int status = 0;
  2309. bool comm_dma_setup_done = false;
  2310. const unsigned int *data;
  2311. unsigned int chip_addx;
  2312. unsigned int words_to_write;
  2313. unsigned int buffer_size_words;
  2314. unsigned char *buffer_addx;
  2315. unsigned short hda_format;
  2316. unsigned int sample_rate_div;
  2317. unsigned int sample_rate_mul;
  2318. unsigned int num_chans;
  2319. unsigned int hda_frame_size_words;
  2320. unsigned int remainder_words;
  2321. const u32 *data_remainder;
  2322. u32 chip_addx_remainder;
  2323. unsigned int run_size_words;
  2324. const struct dsp_image_seg *hci_write = NULL;
  2325. unsigned long timeout;
  2326. bool dma_active;
  2327. if (fls == NULL)
  2328. return -EINVAL;
  2329. if (is_hci_prog_list_seg(fls)) {
  2330. hci_write = fls;
  2331. fls = get_next_seg_ptr(fls);
  2332. }
  2333. if (hci_write && (!fls || is_last(fls))) {
  2334. codec_dbg(codec, "hci_write\n");
  2335. return dspxfr_hci_write(codec, hci_write);
  2336. }
  2337. if (fls == NULL || dma_engine == NULL || port_map_mask == 0) {
  2338. codec_dbg(codec, "Invalid Params\n");
  2339. return -EINVAL;
  2340. }
  2341. data = fls->data;
  2342. chip_addx = fls->chip_addr,
  2343. words_to_write = fls->count;
  2344. if (!words_to_write)
  2345. return hci_write ? dspxfr_hci_write(codec, hci_write) : 0;
  2346. if (reloc)
  2347. chip_addx = (chip_addx & (0xFFFF0000 << 2)) + (reloc << 2);
  2348. if (!UC_RANGE(chip_addx, words_to_write) &&
  2349. !X_RANGE_ALL(chip_addx, words_to_write) &&
  2350. !Y_RANGE_ALL(chip_addx, words_to_write)) {
  2351. codec_dbg(codec, "Invalid chip_addx Params\n");
  2352. return -EINVAL;
  2353. }
  2354. buffer_size_words = (unsigned int)dma_get_buffer_size(dma_engine) /
  2355. sizeof(u32);
  2356. buffer_addx = dma_get_buffer_addr(dma_engine);
  2357. if (buffer_addx == NULL) {
  2358. codec_dbg(codec, "dma_engine buffer NULL\n");
  2359. return -EINVAL;
  2360. }
  2361. dma_get_converter_format(dma_engine, &hda_format);
  2362. sample_rate_div = ((get_hdafmt_rate(hda_format) >> 0) & 3) + 1;
  2363. sample_rate_mul = ((get_hdafmt_rate(hda_format) >> 3) & 3) + 1;
  2364. num_chans = get_hdafmt_chs(hda_format) + 1;
  2365. hda_frame_size_words = ((sample_rate_div == 0) ? 0 :
  2366. (num_chans * sample_rate_mul / sample_rate_div));
  2367. if (hda_frame_size_words == 0) {
  2368. codec_dbg(codec, "frmsz zero\n");
  2369. return -EINVAL;
  2370. }
  2371. buffer_size_words = min(buffer_size_words,
  2372. (unsigned int)(UC_RANGE(chip_addx, 1) ?
  2373. 65536 : 32768));
  2374. buffer_size_words -= buffer_size_words % hda_frame_size_words;
  2375. codec_dbg(codec,
  2376. "chpadr=0x%08x frmsz=%u nchan=%u "
  2377. "rate_mul=%u div=%u bufsz=%u\n",
  2378. chip_addx, hda_frame_size_words, num_chans,
  2379. sample_rate_mul, sample_rate_div, buffer_size_words);
  2380. if (buffer_size_words < hda_frame_size_words) {
  2381. codec_dbg(codec, "dspxfr_one_seg:failed\n");
  2382. return -EINVAL;
  2383. }
  2384. remainder_words = words_to_write % hda_frame_size_words;
  2385. data_remainder = data;
  2386. chip_addx_remainder = chip_addx;
  2387. data += remainder_words;
  2388. chip_addx += remainder_words*sizeof(u32);
  2389. words_to_write -= remainder_words;
  2390. while (words_to_write != 0) {
  2391. run_size_words = min(buffer_size_words, words_to_write);
  2392. codec_dbg(codec, "dspxfr (seg loop)cnt=%u rs=%u remainder=%u\n",
  2393. words_to_write, run_size_words, remainder_words);
  2394. dma_xfer(dma_engine, data, run_size_words*sizeof(u32));
  2395. if (!comm_dma_setup_done) {
  2396. status = dsp_dma_stop(codec, dma_chan, ovly);
  2397. if (status < 0)
  2398. return status;
  2399. status = dsp_dma_setup_common(codec, chip_addx,
  2400. dma_chan, port_map_mask, ovly);
  2401. if (status < 0)
  2402. return status;
  2403. comm_dma_setup_done = true;
  2404. }
  2405. status = dsp_dma_setup(codec, chip_addx,
  2406. run_size_words, dma_chan);
  2407. if (status < 0)
  2408. return status;
  2409. status = dsp_dma_start(codec, dma_chan, ovly);
  2410. if (status < 0)
  2411. return status;
  2412. if (!dsp_is_dma_active(codec, dma_chan)) {
  2413. codec_dbg(codec, "dspxfr:DMA did not start\n");
  2414. return -EIO;
  2415. }
  2416. status = dma_set_state(dma_engine, DMA_STATE_RUN);
  2417. if (status < 0)
  2418. return status;
  2419. if (remainder_words != 0) {
  2420. status = chipio_write_multiple(codec,
  2421. chip_addx_remainder,
  2422. data_remainder,
  2423. remainder_words);
  2424. if (status < 0)
  2425. return status;
  2426. remainder_words = 0;
  2427. }
  2428. if (hci_write) {
  2429. status = dspxfr_hci_write(codec, hci_write);
  2430. if (status < 0)
  2431. return status;
  2432. hci_write = NULL;
  2433. }
  2434. timeout = jiffies + msecs_to_jiffies(2000);
  2435. do {
  2436. dma_active = dsp_is_dma_active(codec, dma_chan);
  2437. if (!dma_active)
  2438. break;
  2439. msleep(20);
  2440. } while (time_before(jiffies, timeout));
  2441. if (dma_active)
  2442. break;
  2443. codec_dbg(codec, "+++++ DMA complete\n");
  2444. dma_set_state(dma_engine, DMA_STATE_STOP);
  2445. status = dma_reset(dma_engine);
  2446. if (status < 0)
  2447. return status;
  2448. data += run_size_words;
  2449. chip_addx += run_size_words*sizeof(u32);
  2450. words_to_write -= run_size_words;
  2451. }
  2452. if (remainder_words != 0) {
  2453. status = chipio_write_multiple(codec, chip_addx_remainder,
  2454. data_remainder, remainder_words);
  2455. }
  2456. return status;
  2457. }
  2458. /**
  2459. * Write the entire DSP image of a DSP code/data overlay to DSP memories
  2460. *
  2461. * @codec: the HDA codec
  2462. * @fls_data: pointer to a fast load image
  2463. * @reloc: Relocation address for loading single-segment overlays, or 0 for
  2464. * no relocation
  2465. * @sample_rate: sampling rate of the stream used for DSP download
  2466. * @channels: channels of the stream used for DSP download
  2467. * @ovly: TRUE if overlay format is required
  2468. *
  2469. * Returns zero or a negative error code.
  2470. */
  2471. static int dspxfr_image(struct hda_codec *codec,
  2472. const struct dsp_image_seg *fls_data,
  2473. unsigned int reloc,
  2474. unsigned int sample_rate,
  2475. unsigned short channels,
  2476. bool ovly)
  2477. {
  2478. struct ca0132_spec *spec = codec->spec;
  2479. int status;
  2480. unsigned short hda_format = 0;
  2481. unsigned int response;
  2482. unsigned char stream_id = 0;
  2483. struct dma_engine *dma_engine;
  2484. unsigned int dma_chan;
  2485. unsigned int port_map_mask;
  2486. if (fls_data == NULL)
  2487. return -EINVAL;
  2488. dma_engine = kzalloc(sizeof(*dma_engine), GFP_KERNEL);
  2489. if (!dma_engine)
  2490. return -ENOMEM;
  2491. dma_engine->dmab = kzalloc(sizeof(*dma_engine->dmab), GFP_KERNEL);
  2492. if (!dma_engine->dmab) {
  2493. kfree(dma_engine);
  2494. return -ENOMEM;
  2495. }
  2496. dma_engine->codec = codec;
  2497. dma_convert_to_hda_format(codec, sample_rate, channels, &hda_format);
  2498. dma_engine->m_converter_format = hda_format;
  2499. dma_engine->buf_size = (ovly ? DSP_DMA_WRITE_BUFLEN_OVLY :
  2500. DSP_DMA_WRITE_BUFLEN_INIT) * 2;
  2501. dma_chan = ovly ? INVALID_DMA_CHANNEL : 0;
  2502. status = codec_set_converter_format(codec, WIDGET_CHIP_CTRL,
  2503. hda_format, &response);
  2504. if (status < 0) {
  2505. codec_dbg(codec, "set converter format fail\n");
  2506. goto exit;
  2507. }
  2508. status = snd_hda_codec_load_dsp_prepare(codec,
  2509. dma_engine->m_converter_format,
  2510. dma_engine->buf_size,
  2511. dma_engine->dmab);
  2512. if (status < 0)
  2513. goto exit;
  2514. spec->dsp_stream_id = status;
  2515. if (ovly) {
  2516. status = dspio_alloc_dma_chan(codec, &dma_chan);
  2517. if (status < 0) {
  2518. codec_dbg(codec, "alloc dmachan fail\n");
  2519. dma_chan = INVALID_DMA_CHANNEL;
  2520. goto exit;
  2521. }
  2522. }
  2523. port_map_mask = 0;
  2524. status = dsp_allocate_ports_format(codec, hda_format,
  2525. &port_map_mask);
  2526. if (status < 0) {
  2527. codec_dbg(codec, "alloc ports fail\n");
  2528. goto exit;
  2529. }
  2530. stream_id = dma_get_stream_id(dma_engine);
  2531. status = codec_set_converter_stream_channel(codec,
  2532. WIDGET_CHIP_CTRL, stream_id, 0, &response);
  2533. if (status < 0) {
  2534. codec_dbg(codec, "set stream chan fail\n");
  2535. goto exit;
  2536. }
  2537. while ((fls_data != NULL) && !is_last(fls_data)) {
  2538. if (!is_valid(fls_data)) {
  2539. codec_dbg(codec, "FLS check fail\n");
  2540. status = -EINVAL;
  2541. goto exit;
  2542. }
  2543. status = dspxfr_one_seg(codec, fls_data, reloc,
  2544. dma_engine, dma_chan,
  2545. port_map_mask, ovly);
  2546. if (status < 0)
  2547. break;
  2548. if (is_hci_prog_list_seg(fls_data))
  2549. fls_data = get_next_seg_ptr(fls_data);
  2550. if ((fls_data != NULL) && !is_last(fls_data))
  2551. fls_data = get_next_seg_ptr(fls_data);
  2552. }
  2553. if (port_map_mask != 0)
  2554. status = dsp_free_ports(codec);
  2555. if (status < 0)
  2556. goto exit;
  2557. status = codec_set_converter_stream_channel(codec,
  2558. WIDGET_CHIP_CTRL, 0, 0, &response);
  2559. exit:
  2560. if (ovly && (dma_chan != INVALID_DMA_CHANNEL))
  2561. dspio_free_dma_chan(codec, dma_chan);
  2562. if (dma_engine->dmab->area)
  2563. snd_hda_codec_load_dsp_cleanup(codec, dma_engine->dmab);
  2564. kfree(dma_engine->dmab);
  2565. kfree(dma_engine);
  2566. return status;
  2567. }
  2568. /*
  2569. * CA0132 DSP download stuffs.
  2570. */
  2571. static void dspload_post_setup(struct hda_codec *codec)
  2572. {
  2573. struct ca0132_spec *spec = codec->spec;
  2574. codec_dbg(codec, "---- dspload_post_setup ------\n");
  2575. if (!spec->use_alt_functions) {
  2576. /*set DSP speaker to 2.0 configuration*/
  2577. chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x18), 0x08080080);
  2578. chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x19), 0x3f800000);
  2579. /*update write pointer*/
  2580. chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x29), 0x00000002);
  2581. }
  2582. }
  2583. /**
  2584. * dspload_image - Download DSP from a DSP Image Fast Load structure.
  2585. *
  2586. * @codec: the HDA codec
  2587. * @fls: pointer to a fast load image
  2588. * @ovly: TRUE if overlay format is required
  2589. * @reloc: Relocation address for loading single-segment overlays, or 0 for
  2590. * no relocation
  2591. * @autostart: TRUE if DSP starts after loading; ignored if ovly is TRUE
  2592. * @router_chans: number of audio router channels to be allocated (0 means use
  2593. * internal defaults; max is 32)
  2594. *
  2595. * Download DSP from a DSP Image Fast Load structure. This structure is a
  2596. * linear, non-constant sized element array of structures, each of which
  2597. * contain the count of the data to be loaded, the data itself, and the
  2598. * corresponding starting chip address of the starting data location.
  2599. * Returns zero or a negative error code.
  2600. */
  2601. static int dspload_image(struct hda_codec *codec,
  2602. const struct dsp_image_seg *fls,
  2603. bool ovly,
  2604. unsigned int reloc,
  2605. bool autostart,
  2606. int router_chans)
  2607. {
  2608. int status = 0;
  2609. unsigned int sample_rate;
  2610. unsigned short channels;
  2611. codec_dbg(codec, "---- dspload_image begin ------\n");
  2612. if (router_chans == 0) {
  2613. if (!ovly)
  2614. router_chans = DMA_TRANSFER_FRAME_SIZE_NWORDS;
  2615. else
  2616. router_chans = DMA_OVERLAY_FRAME_SIZE_NWORDS;
  2617. }
  2618. sample_rate = 48000;
  2619. channels = (unsigned short)router_chans;
  2620. while (channels > 16) {
  2621. sample_rate *= 2;
  2622. channels /= 2;
  2623. }
  2624. do {
  2625. codec_dbg(codec, "Ready to program DMA\n");
  2626. if (!ovly)
  2627. status = dsp_reset(codec);
  2628. if (status < 0)
  2629. break;
  2630. codec_dbg(codec, "dsp_reset() complete\n");
  2631. status = dspxfr_image(codec, fls, reloc, sample_rate, channels,
  2632. ovly);
  2633. if (status < 0)
  2634. break;
  2635. codec_dbg(codec, "dspxfr_image() complete\n");
  2636. if (autostart && !ovly) {
  2637. dspload_post_setup(codec);
  2638. status = dsp_set_run_state(codec);
  2639. }
  2640. codec_dbg(codec, "LOAD FINISHED\n");
  2641. } while (0);
  2642. return status;
  2643. }
  2644. #ifdef CONFIG_SND_HDA_CODEC_CA0132_DSP
  2645. static bool dspload_is_loaded(struct hda_codec *codec)
  2646. {
  2647. unsigned int data = 0;
  2648. int status = 0;
  2649. status = chipio_read(codec, 0x40004, &data);
  2650. if ((status < 0) || (data != 1))
  2651. return false;
  2652. return true;
  2653. }
  2654. #else
  2655. #define dspload_is_loaded(codec) false
  2656. #endif
  2657. static bool dspload_wait_loaded(struct hda_codec *codec)
  2658. {
  2659. unsigned long timeout = jiffies + msecs_to_jiffies(2000);
  2660. do {
  2661. if (dspload_is_loaded(codec)) {
  2662. codec_info(codec, "ca0132 DSP downloaded and running\n");
  2663. return true;
  2664. }
  2665. msleep(20);
  2666. } while (time_before(jiffies, timeout));
  2667. codec_err(codec, "ca0132 failed to download DSP\n");
  2668. return false;
  2669. }
  2670. /*
  2671. * Setup GPIO for the other variants of Core3D.
  2672. */
  2673. /*
  2674. * For cards with PCI-E region2 (Sound Blaster Z/ZxR, Recon3D, and AE-5)
  2675. * the mmio address 0x320 is used to set GPIO pins. The format for the data
  2676. * The first eight bits are just the number of the pin. So far, I've only seen
  2677. * this number go to 7.
  2678. */
  2679. static void ca0132_mmio_gpio_set(struct hda_codec *codec, unsigned int gpio_pin,
  2680. bool enable)
  2681. {
  2682. struct ca0132_spec *spec = codec->spec;
  2683. unsigned short gpio_data;
  2684. gpio_data = gpio_pin & 0xF;
  2685. gpio_data |= ((enable << 8) & 0x100);
  2686. writew(gpio_data, spec->mem_base + 0x320);
  2687. }
  2688. /*
  2689. * Sets up the GPIO pins so that they are discoverable. If this isn't done,
  2690. * the card shows as having no GPIO pins.
  2691. */
  2692. static void ca0132_gpio_init(struct hda_codec *codec)
  2693. {
  2694. struct ca0132_spec *spec = codec->spec;
  2695. switch (spec->quirk) {
  2696. case QUIRK_SBZ:
  2697. snd_hda_codec_write(codec, 0x01, 0, 0x793, 0x00);
  2698. snd_hda_codec_write(codec, 0x01, 0, 0x794, 0x53);
  2699. snd_hda_codec_write(codec, 0x01, 0, 0x790, 0x23);
  2700. break;
  2701. case QUIRK_R3DI:
  2702. snd_hda_codec_write(codec, 0x01, 0, 0x793, 0x00);
  2703. snd_hda_codec_write(codec, 0x01, 0, 0x794, 0x5B);
  2704. break;
  2705. }
  2706. }
  2707. /* Sets the GPIO for audio output. */
  2708. static void ca0132_gpio_setup(struct hda_codec *codec)
  2709. {
  2710. struct ca0132_spec *spec = codec->spec;
  2711. switch (spec->quirk) {
  2712. case QUIRK_SBZ:
  2713. snd_hda_codec_write(codec, 0x01, 0,
  2714. AC_VERB_SET_GPIO_DIRECTION, 0x07);
  2715. snd_hda_codec_write(codec, 0x01, 0,
  2716. AC_VERB_SET_GPIO_MASK, 0x07);
  2717. snd_hda_codec_write(codec, 0x01, 0,
  2718. AC_VERB_SET_GPIO_DATA, 0x04);
  2719. snd_hda_codec_write(codec, 0x01, 0,
  2720. AC_VERB_SET_GPIO_DATA, 0x06);
  2721. break;
  2722. case QUIRK_R3DI:
  2723. snd_hda_codec_write(codec, 0x01, 0,
  2724. AC_VERB_SET_GPIO_DIRECTION, 0x1E);
  2725. snd_hda_codec_write(codec, 0x01, 0,
  2726. AC_VERB_SET_GPIO_MASK, 0x1F);
  2727. snd_hda_codec_write(codec, 0x01, 0,
  2728. AC_VERB_SET_GPIO_DATA, 0x0C);
  2729. break;
  2730. }
  2731. }
  2732. /*
  2733. * GPIO control functions for the Recon3D integrated.
  2734. */
  2735. enum r3di_gpio_bit {
  2736. /* Bit 1 - Switch between front/rear mic. 0 = rear, 1 = front */
  2737. R3DI_MIC_SELECT_BIT = 1,
  2738. /* Bit 2 - Switch between headphone/line out. 0 = Headphone, 1 = Line */
  2739. R3DI_OUT_SELECT_BIT = 2,
  2740. /*
  2741. * I dunno what this actually does, but it stays on until the dsp
  2742. * is downloaded.
  2743. */
  2744. R3DI_GPIO_DSP_DOWNLOADING = 3,
  2745. /*
  2746. * Same as above, no clue what it does, but it comes on after the dsp
  2747. * is downloaded.
  2748. */
  2749. R3DI_GPIO_DSP_DOWNLOADED = 4
  2750. };
  2751. enum r3di_mic_select {
  2752. /* Set GPIO bit 1 to 0 for rear mic */
  2753. R3DI_REAR_MIC = 0,
  2754. /* Set GPIO bit 1 to 1 for front microphone*/
  2755. R3DI_FRONT_MIC = 1
  2756. };
  2757. enum r3di_out_select {
  2758. /* Set GPIO bit 2 to 0 for headphone */
  2759. R3DI_HEADPHONE_OUT = 0,
  2760. /* Set GPIO bit 2 to 1 for speaker */
  2761. R3DI_LINE_OUT = 1
  2762. };
  2763. enum r3di_dsp_status {
  2764. /* Set GPIO bit 3 to 1 until DSP is downloaded */
  2765. R3DI_DSP_DOWNLOADING = 0,
  2766. /* Set GPIO bit 4 to 1 once DSP is downloaded */
  2767. R3DI_DSP_DOWNLOADED = 1
  2768. };
  2769. static void r3di_gpio_mic_set(struct hda_codec *codec,
  2770. enum r3di_mic_select cur_mic)
  2771. {
  2772. unsigned int cur_gpio;
  2773. /* Get the current GPIO Data setup */
  2774. cur_gpio = snd_hda_codec_read(codec, 0x01, 0, AC_VERB_GET_GPIO_DATA, 0);
  2775. switch (cur_mic) {
  2776. case R3DI_REAR_MIC:
  2777. cur_gpio &= ~(1 << R3DI_MIC_SELECT_BIT);
  2778. break;
  2779. case R3DI_FRONT_MIC:
  2780. cur_gpio |= (1 << R3DI_MIC_SELECT_BIT);
  2781. break;
  2782. }
  2783. snd_hda_codec_write(codec, codec->core.afg, 0,
  2784. AC_VERB_SET_GPIO_DATA, cur_gpio);
  2785. }
  2786. static void r3di_gpio_out_set(struct hda_codec *codec,
  2787. enum r3di_out_select cur_out)
  2788. {
  2789. unsigned int cur_gpio;
  2790. /* Get the current GPIO Data setup */
  2791. cur_gpio = snd_hda_codec_read(codec, 0x01, 0, AC_VERB_GET_GPIO_DATA, 0);
  2792. switch (cur_out) {
  2793. case R3DI_HEADPHONE_OUT:
  2794. cur_gpio &= ~(1 << R3DI_OUT_SELECT_BIT);
  2795. break;
  2796. case R3DI_LINE_OUT:
  2797. cur_gpio |= (1 << R3DI_OUT_SELECT_BIT);
  2798. break;
  2799. }
  2800. snd_hda_codec_write(codec, codec->core.afg, 0,
  2801. AC_VERB_SET_GPIO_DATA, cur_gpio);
  2802. }
  2803. static void r3di_gpio_dsp_status_set(struct hda_codec *codec,
  2804. enum r3di_dsp_status dsp_status)
  2805. {
  2806. unsigned int cur_gpio;
  2807. /* Get the current GPIO Data setup */
  2808. cur_gpio = snd_hda_codec_read(codec, 0x01, 0, AC_VERB_GET_GPIO_DATA, 0);
  2809. switch (dsp_status) {
  2810. case R3DI_DSP_DOWNLOADING:
  2811. cur_gpio |= (1 << R3DI_GPIO_DSP_DOWNLOADING);
  2812. snd_hda_codec_write(codec, codec->core.afg, 0,
  2813. AC_VERB_SET_GPIO_DATA, cur_gpio);
  2814. break;
  2815. case R3DI_DSP_DOWNLOADED:
  2816. /* Set DOWNLOADING bit to 0. */
  2817. cur_gpio &= ~(1 << R3DI_GPIO_DSP_DOWNLOADING);
  2818. snd_hda_codec_write(codec, codec->core.afg, 0,
  2819. AC_VERB_SET_GPIO_DATA, cur_gpio);
  2820. cur_gpio |= (1 << R3DI_GPIO_DSP_DOWNLOADED);
  2821. break;
  2822. }
  2823. snd_hda_codec_write(codec, codec->core.afg, 0,
  2824. AC_VERB_SET_GPIO_DATA, cur_gpio);
  2825. }
  2826. /*
  2827. * PCM callbacks
  2828. */
  2829. static int ca0132_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  2830. struct hda_codec *codec,
  2831. unsigned int stream_tag,
  2832. unsigned int format,
  2833. struct snd_pcm_substream *substream)
  2834. {
  2835. struct ca0132_spec *spec = codec->spec;
  2836. snd_hda_codec_setup_stream(codec, spec->dacs[0], stream_tag, 0, format);
  2837. return 0;
  2838. }
  2839. static int ca0132_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
  2840. struct hda_codec *codec,
  2841. struct snd_pcm_substream *substream)
  2842. {
  2843. struct ca0132_spec *spec = codec->spec;
  2844. if (spec->dsp_state == DSP_DOWNLOADING)
  2845. return 0;
  2846. /*If Playback effects are on, allow stream some time to flush
  2847. *effects tail*/
  2848. if (spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID])
  2849. msleep(50);
  2850. snd_hda_codec_cleanup_stream(codec, spec->dacs[0]);
  2851. return 0;
  2852. }
  2853. static unsigned int ca0132_playback_pcm_delay(struct hda_pcm_stream *info,
  2854. struct hda_codec *codec,
  2855. struct snd_pcm_substream *substream)
  2856. {
  2857. struct ca0132_spec *spec = codec->spec;
  2858. unsigned int latency = DSP_PLAYBACK_INIT_LATENCY;
  2859. struct snd_pcm_runtime *runtime = substream->runtime;
  2860. if (spec->dsp_state != DSP_DOWNLOADED)
  2861. return 0;
  2862. /* Add latency if playback enhancement and either effect is enabled. */
  2863. if (spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID]) {
  2864. if ((spec->effects_switch[SURROUND - EFFECT_START_NID]) ||
  2865. (spec->effects_switch[DIALOG_PLUS - EFFECT_START_NID]))
  2866. latency += DSP_PLAY_ENHANCEMENT_LATENCY;
  2867. }
  2868. /* Applying Speaker EQ adds latency as well. */
  2869. if (spec->cur_out_type == SPEAKER_OUT)
  2870. latency += DSP_SPEAKER_OUT_LATENCY;
  2871. return (latency * runtime->rate) / 1000;
  2872. }
  2873. /*
  2874. * Digital out
  2875. */
  2876. static int ca0132_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
  2877. struct hda_codec *codec,
  2878. struct snd_pcm_substream *substream)
  2879. {
  2880. struct ca0132_spec *spec = codec->spec;
  2881. return snd_hda_multi_out_dig_open(codec, &spec->multiout);
  2882. }
  2883. static int ca0132_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  2884. struct hda_codec *codec,
  2885. unsigned int stream_tag,
  2886. unsigned int format,
  2887. struct snd_pcm_substream *substream)
  2888. {
  2889. struct ca0132_spec *spec = codec->spec;
  2890. return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
  2891. stream_tag, format, substream);
  2892. }
  2893. static int ca0132_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
  2894. struct hda_codec *codec,
  2895. struct snd_pcm_substream *substream)
  2896. {
  2897. struct ca0132_spec *spec = codec->spec;
  2898. return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
  2899. }
  2900. static int ca0132_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
  2901. struct hda_codec *codec,
  2902. struct snd_pcm_substream *substream)
  2903. {
  2904. struct ca0132_spec *spec = codec->spec;
  2905. return snd_hda_multi_out_dig_close(codec, &spec->multiout);
  2906. }
  2907. /*
  2908. * Analog capture
  2909. */
  2910. static int ca0132_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
  2911. struct hda_codec *codec,
  2912. unsigned int stream_tag,
  2913. unsigned int format,
  2914. struct snd_pcm_substream *substream)
  2915. {
  2916. snd_hda_codec_setup_stream(codec, hinfo->nid,
  2917. stream_tag, 0, format);
  2918. return 0;
  2919. }
  2920. static int ca0132_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
  2921. struct hda_codec *codec,
  2922. struct snd_pcm_substream *substream)
  2923. {
  2924. struct ca0132_spec *spec = codec->spec;
  2925. if (spec->dsp_state == DSP_DOWNLOADING)
  2926. return 0;
  2927. snd_hda_codec_cleanup_stream(codec, hinfo->nid);
  2928. return 0;
  2929. }
  2930. static unsigned int ca0132_capture_pcm_delay(struct hda_pcm_stream *info,
  2931. struct hda_codec *codec,
  2932. struct snd_pcm_substream *substream)
  2933. {
  2934. struct ca0132_spec *spec = codec->spec;
  2935. unsigned int latency = DSP_CAPTURE_INIT_LATENCY;
  2936. struct snd_pcm_runtime *runtime = substream->runtime;
  2937. if (spec->dsp_state != DSP_DOWNLOADED)
  2938. return 0;
  2939. if (spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID])
  2940. latency += DSP_CRYSTAL_VOICE_LATENCY;
  2941. return (latency * runtime->rate) / 1000;
  2942. }
  2943. /*
  2944. * Controls stuffs.
  2945. */
  2946. /*
  2947. * Mixer controls helpers.
  2948. */
  2949. #define CA0132_CODEC_VOL_MONO(xname, nid, channel, dir) \
  2950. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  2951. .name = xname, \
  2952. .subdevice = HDA_SUBDEV_AMP_FLAG, \
  2953. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
  2954. SNDRV_CTL_ELEM_ACCESS_TLV_READ | \
  2955. SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK, \
  2956. .info = ca0132_volume_info, \
  2957. .get = ca0132_volume_get, \
  2958. .put = ca0132_volume_put, \
  2959. .tlv = { .c = ca0132_volume_tlv }, \
  2960. .private_value = HDA_COMPOSE_AMP_VAL(nid, channel, 0, dir) }
  2961. /*
  2962. * Creates a mixer control that uses defaults of HDA_CODEC_VOL except for the
  2963. * volume put, which is used for setting the DSP volume. This was done because
  2964. * the ca0132 functions were taking too much time and causing lag.
  2965. */
  2966. #define CA0132_ALT_CODEC_VOL_MONO(xname, nid, channel, dir) \
  2967. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  2968. .name = xname, \
  2969. .subdevice = HDA_SUBDEV_AMP_FLAG, \
  2970. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
  2971. SNDRV_CTL_ELEM_ACCESS_TLV_READ | \
  2972. SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK, \
  2973. .info = snd_hda_mixer_amp_volume_info, \
  2974. .get = snd_hda_mixer_amp_volume_get, \
  2975. .put = ca0132_alt_volume_put, \
  2976. .tlv = { .c = snd_hda_mixer_amp_tlv }, \
  2977. .private_value = HDA_COMPOSE_AMP_VAL(nid, channel, 0, dir) }
  2978. #define CA0132_CODEC_MUTE_MONO(xname, nid, channel, dir) \
  2979. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  2980. .name = xname, \
  2981. .subdevice = HDA_SUBDEV_AMP_FLAG, \
  2982. .info = snd_hda_mixer_amp_switch_info, \
  2983. .get = ca0132_switch_get, \
  2984. .put = ca0132_switch_put, \
  2985. .private_value = HDA_COMPOSE_AMP_VAL(nid, channel, 0, dir) }
  2986. /* stereo */
  2987. #define CA0132_CODEC_VOL(xname, nid, dir) \
  2988. CA0132_CODEC_VOL_MONO(xname, nid, 3, dir)
  2989. #define CA0132_ALT_CODEC_VOL(xname, nid, dir) \
  2990. CA0132_ALT_CODEC_VOL_MONO(xname, nid, 3, dir)
  2991. #define CA0132_CODEC_MUTE(xname, nid, dir) \
  2992. CA0132_CODEC_MUTE_MONO(xname, nid, 3, dir)
  2993. /* lookup tables */
  2994. /*
  2995. * Lookup table with decibel values for the DSP. When volume is changed in
  2996. * Windows, the DSP is also sent the dB value in floating point. In Windows,
  2997. * these values have decimal points, probably because the Windows driver
  2998. * actually uses floating point. We can't here, so I made a lookup table of
  2999. * values -90 to 9. -90 is the lowest decibel value for both the ADC's and the
  3000. * DAC's, and 9 is the maximum.
  3001. */
  3002. static const unsigned int float_vol_db_lookup[] = {
  3003. 0xC2B40000, 0xC2B20000, 0xC2B00000, 0xC2AE0000, 0xC2AC0000, 0xC2AA0000,
  3004. 0xC2A80000, 0xC2A60000, 0xC2A40000, 0xC2A20000, 0xC2A00000, 0xC29E0000,
  3005. 0xC29C0000, 0xC29A0000, 0xC2980000, 0xC2960000, 0xC2940000, 0xC2920000,
  3006. 0xC2900000, 0xC28E0000, 0xC28C0000, 0xC28A0000, 0xC2880000, 0xC2860000,
  3007. 0xC2840000, 0xC2820000, 0xC2800000, 0xC27C0000, 0xC2780000, 0xC2740000,
  3008. 0xC2700000, 0xC26C0000, 0xC2680000, 0xC2640000, 0xC2600000, 0xC25C0000,
  3009. 0xC2580000, 0xC2540000, 0xC2500000, 0xC24C0000, 0xC2480000, 0xC2440000,
  3010. 0xC2400000, 0xC23C0000, 0xC2380000, 0xC2340000, 0xC2300000, 0xC22C0000,
  3011. 0xC2280000, 0xC2240000, 0xC2200000, 0xC21C0000, 0xC2180000, 0xC2140000,
  3012. 0xC2100000, 0xC20C0000, 0xC2080000, 0xC2040000, 0xC2000000, 0xC1F80000,
  3013. 0xC1F00000, 0xC1E80000, 0xC1E00000, 0xC1D80000, 0xC1D00000, 0xC1C80000,
  3014. 0xC1C00000, 0xC1B80000, 0xC1B00000, 0xC1A80000, 0xC1A00000, 0xC1980000,
  3015. 0xC1900000, 0xC1880000, 0xC1800000, 0xC1700000, 0xC1600000, 0xC1500000,
  3016. 0xC1400000, 0xC1300000, 0xC1200000, 0xC1100000, 0xC1000000, 0xC0E00000,
  3017. 0xC0C00000, 0xC0A00000, 0xC0800000, 0xC0400000, 0xC0000000, 0xBF800000,
  3018. 0x00000000, 0x3F800000, 0x40000000, 0x40400000, 0x40800000, 0x40A00000,
  3019. 0x40C00000, 0x40E00000, 0x41000000, 0x41100000
  3020. };
  3021. /*
  3022. * This table counts from float 0 to 1 in increments of .01, which is
  3023. * useful for a few different sliders.
  3024. */
  3025. static const unsigned int float_zero_to_one_lookup[] = {
  3026. 0x00000000, 0x3C23D70A, 0x3CA3D70A, 0x3CF5C28F, 0x3D23D70A, 0x3D4CCCCD,
  3027. 0x3D75C28F, 0x3D8F5C29, 0x3DA3D70A, 0x3DB851EC, 0x3DCCCCCD, 0x3DE147AE,
  3028. 0x3DF5C28F, 0x3E051EB8, 0x3E0F5C29, 0x3E19999A, 0x3E23D70A, 0x3E2E147B,
  3029. 0x3E3851EC, 0x3E428F5C, 0x3E4CCCCD, 0x3E570A3D, 0x3E6147AE, 0x3E6B851F,
  3030. 0x3E75C28F, 0x3E800000, 0x3E851EB8, 0x3E8A3D71, 0x3E8F5C29, 0x3E947AE1,
  3031. 0x3E99999A, 0x3E9EB852, 0x3EA3D70A, 0x3EA8F5C3, 0x3EAE147B, 0x3EB33333,
  3032. 0x3EB851EC, 0x3EBD70A4, 0x3EC28F5C, 0x3EC7AE14, 0x3ECCCCCD, 0x3ED1EB85,
  3033. 0x3ED70A3D, 0x3EDC28F6, 0x3EE147AE, 0x3EE66666, 0x3EEB851F, 0x3EF0A3D7,
  3034. 0x3EF5C28F, 0x3EFAE148, 0x3F000000, 0x3F028F5C, 0x3F051EB8, 0x3F07AE14,
  3035. 0x3F0A3D71, 0x3F0CCCCD, 0x3F0F5C29, 0x3F11EB85, 0x3F147AE1, 0x3F170A3D,
  3036. 0x3F19999A, 0x3F1C28F6, 0x3F1EB852, 0x3F2147AE, 0x3F23D70A, 0x3F266666,
  3037. 0x3F28F5C3, 0x3F2B851F, 0x3F2E147B, 0x3F30A3D7, 0x3F333333, 0x3F35C28F,
  3038. 0x3F3851EC, 0x3F3AE148, 0x3F3D70A4, 0x3F400000, 0x3F428F5C, 0x3F451EB8,
  3039. 0x3F47AE14, 0x3F4A3D71, 0x3F4CCCCD, 0x3F4F5C29, 0x3F51EB85, 0x3F547AE1,
  3040. 0x3F570A3D, 0x3F59999A, 0x3F5C28F6, 0x3F5EB852, 0x3F6147AE, 0x3F63D70A,
  3041. 0x3F666666, 0x3F68F5C3, 0x3F6B851F, 0x3F6E147B, 0x3F70A3D7, 0x3F733333,
  3042. 0x3F75C28F, 0x3F7851EC, 0x3F7AE148, 0x3F7D70A4, 0x3F800000
  3043. };
  3044. /*
  3045. * This table counts from float 10 to 1000, which is the range of the x-bass
  3046. * crossover slider in Windows.
  3047. */
  3048. static const unsigned int float_xbass_xover_lookup[] = {
  3049. 0x41200000, 0x41A00000, 0x41F00000, 0x42200000, 0x42480000, 0x42700000,
  3050. 0x428C0000, 0x42A00000, 0x42B40000, 0x42C80000, 0x42DC0000, 0x42F00000,
  3051. 0x43020000, 0x430C0000, 0x43160000, 0x43200000, 0x432A0000, 0x43340000,
  3052. 0x433E0000, 0x43480000, 0x43520000, 0x435C0000, 0x43660000, 0x43700000,
  3053. 0x437A0000, 0x43820000, 0x43870000, 0x438C0000, 0x43910000, 0x43960000,
  3054. 0x439B0000, 0x43A00000, 0x43A50000, 0x43AA0000, 0x43AF0000, 0x43B40000,
  3055. 0x43B90000, 0x43BE0000, 0x43C30000, 0x43C80000, 0x43CD0000, 0x43D20000,
  3056. 0x43D70000, 0x43DC0000, 0x43E10000, 0x43E60000, 0x43EB0000, 0x43F00000,
  3057. 0x43F50000, 0x43FA0000, 0x43FF0000, 0x44020000, 0x44048000, 0x44070000,
  3058. 0x44098000, 0x440C0000, 0x440E8000, 0x44110000, 0x44138000, 0x44160000,
  3059. 0x44188000, 0x441B0000, 0x441D8000, 0x44200000, 0x44228000, 0x44250000,
  3060. 0x44278000, 0x442A0000, 0x442C8000, 0x442F0000, 0x44318000, 0x44340000,
  3061. 0x44368000, 0x44390000, 0x443B8000, 0x443E0000, 0x44408000, 0x44430000,
  3062. 0x44458000, 0x44480000, 0x444A8000, 0x444D0000, 0x444F8000, 0x44520000,
  3063. 0x44548000, 0x44570000, 0x44598000, 0x445C0000, 0x445E8000, 0x44610000,
  3064. 0x44638000, 0x44660000, 0x44688000, 0x446B0000, 0x446D8000, 0x44700000,
  3065. 0x44728000, 0x44750000, 0x44778000, 0x447A0000
  3066. };
  3067. /* The following are for tuning of products */
  3068. #ifdef ENABLE_TUNING_CONTROLS
  3069. static unsigned int voice_focus_vals_lookup[] = {
  3070. 0x41A00000, 0x41A80000, 0x41B00000, 0x41B80000, 0x41C00000, 0x41C80000,
  3071. 0x41D00000, 0x41D80000, 0x41E00000, 0x41E80000, 0x41F00000, 0x41F80000,
  3072. 0x42000000, 0x42040000, 0x42080000, 0x420C0000, 0x42100000, 0x42140000,
  3073. 0x42180000, 0x421C0000, 0x42200000, 0x42240000, 0x42280000, 0x422C0000,
  3074. 0x42300000, 0x42340000, 0x42380000, 0x423C0000, 0x42400000, 0x42440000,
  3075. 0x42480000, 0x424C0000, 0x42500000, 0x42540000, 0x42580000, 0x425C0000,
  3076. 0x42600000, 0x42640000, 0x42680000, 0x426C0000, 0x42700000, 0x42740000,
  3077. 0x42780000, 0x427C0000, 0x42800000, 0x42820000, 0x42840000, 0x42860000,
  3078. 0x42880000, 0x428A0000, 0x428C0000, 0x428E0000, 0x42900000, 0x42920000,
  3079. 0x42940000, 0x42960000, 0x42980000, 0x429A0000, 0x429C0000, 0x429E0000,
  3080. 0x42A00000, 0x42A20000, 0x42A40000, 0x42A60000, 0x42A80000, 0x42AA0000,
  3081. 0x42AC0000, 0x42AE0000, 0x42B00000, 0x42B20000, 0x42B40000, 0x42B60000,
  3082. 0x42B80000, 0x42BA0000, 0x42BC0000, 0x42BE0000, 0x42C00000, 0x42C20000,
  3083. 0x42C40000, 0x42C60000, 0x42C80000, 0x42CA0000, 0x42CC0000, 0x42CE0000,
  3084. 0x42D00000, 0x42D20000, 0x42D40000, 0x42D60000, 0x42D80000, 0x42DA0000,
  3085. 0x42DC0000, 0x42DE0000, 0x42E00000, 0x42E20000, 0x42E40000, 0x42E60000,
  3086. 0x42E80000, 0x42EA0000, 0x42EC0000, 0x42EE0000, 0x42F00000, 0x42F20000,
  3087. 0x42F40000, 0x42F60000, 0x42F80000, 0x42FA0000, 0x42FC0000, 0x42FE0000,
  3088. 0x43000000, 0x43010000, 0x43020000, 0x43030000, 0x43040000, 0x43050000,
  3089. 0x43060000, 0x43070000, 0x43080000, 0x43090000, 0x430A0000, 0x430B0000,
  3090. 0x430C0000, 0x430D0000, 0x430E0000, 0x430F0000, 0x43100000, 0x43110000,
  3091. 0x43120000, 0x43130000, 0x43140000, 0x43150000, 0x43160000, 0x43170000,
  3092. 0x43180000, 0x43190000, 0x431A0000, 0x431B0000, 0x431C0000, 0x431D0000,
  3093. 0x431E0000, 0x431F0000, 0x43200000, 0x43210000, 0x43220000, 0x43230000,
  3094. 0x43240000, 0x43250000, 0x43260000, 0x43270000, 0x43280000, 0x43290000,
  3095. 0x432A0000, 0x432B0000, 0x432C0000, 0x432D0000, 0x432E0000, 0x432F0000,
  3096. 0x43300000, 0x43310000, 0x43320000, 0x43330000, 0x43340000
  3097. };
  3098. static unsigned int mic_svm_vals_lookup[] = {
  3099. 0x00000000, 0x3C23D70A, 0x3CA3D70A, 0x3CF5C28F, 0x3D23D70A, 0x3D4CCCCD,
  3100. 0x3D75C28F, 0x3D8F5C29, 0x3DA3D70A, 0x3DB851EC, 0x3DCCCCCD, 0x3DE147AE,
  3101. 0x3DF5C28F, 0x3E051EB8, 0x3E0F5C29, 0x3E19999A, 0x3E23D70A, 0x3E2E147B,
  3102. 0x3E3851EC, 0x3E428F5C, 0x3E4CCCCD, 0x3E570A3D, 0x3E6147AE, 0x3E6B851F,
  3103. 0x3E75C28F, 0x3E800000, 0x3E851EB8, 0x3E8A3D71, 0x3E8F5C29, 0x3E947AE1,
  3104. 0x3E99999A, 0x3E9EB852, 0x3EA3D70A, 0x3EA8F5C3, 0x3EAE147B, 0x3EB33333,
  3105. 0x3EB851EC, 0x3EBD70A4, 0x3EC28F5C, 0x3EC7AE14, 0x3ECCCCCD, 0x3ED1EB85,
  3106. 0x3ED70A3D, 0x3EDC28F6, 0x3EE147AE, 0x3EE66666, 0x3EEB851F, 0x3EF0A3D7,
  3107. 0x3EF5C28F, 0x3EFAE148, 0x3F000000, 0x3F028F5C, 0x3F051EB8, 0x3F07AE14,
  3108. 0x3F0A3D71, 0x3F0CCCCD, 0x3F0F5C29, 0x3F11EB85, 0x3F147AE1, 0x3F170A3D,
  3109. 0x3F19999A, 0x3F1C28F6, 0x3F1EB852, 0x3F2147AE, 0x3F23D70A, 0x3F266666,
  3110. 0x3F28F5C3, 0x3F2B851F, 0x3F2E147B, 0x3F30A3D7, 0x3F333333, 0x3F35C28F,
  3111. 0x3F3851EC, 0x3F3AE148, 0x3F3D70A4, 0x3F400000, 0x3F428F5C, 0x3F451EB8,
  3112. 0x3F47AE14, 0x3F4A3D71, 0x3F4CCCCD, 0x3F4F5C29, 0x3F51EB85, 0x3F547AE1,
  3113. 0x3F570A3D, 0x3F59999A, 0x3F5C28F6, 0x3F5EB852, 0x3F6147AE, 0x3F63D70A,
  3114. 0x3F666666, 0x3F68F5C3, 0x3F6B851F, 0x3F6E147B, 0x3F70A3D7, 0x3F733333,
  3115. 0x3F75C28F, 0x3F7851EC, 0x3F7AE148, 0x3F7D70A4, 0x3F800000
  3116. };
  3117. static unsigned int equalizer_vals_lookup[] = {
  3118. 0xC1C00000, 0xC1B80000, 0xC1B00000, 0xC1A80000, 0xC1A00000, 0xC1980000,
  3119. 0xC1900000, 0xC1880000, 0xC1800000, 0xC1700000, 0xC1600000, 0xC1500000,
  3120. 0xC1400000, 0xC1300000, 0xC1200000, 0xC1100000, 0xC1000000, 0xC0E00000,
  3121. 0xC0C00000, 0xC0A00000, 0xC0800000, 0xC0400000, 0xC0000000, 0xBF800000,
  3122. 0x00000000, 0x3F800000, 0x40000000, 0x40400000, 0x40800000, 0x40A00000,
  3123. 0x40C00000, 0x40E00000, 0x41000000, 0x41100000, 0x41200000, 0x41300000,
  3124. 0x41400000, 0x41500000, 0x41600000, 0x41700000, 0x41800000, 0x41880000,
  3125. 0x41900000, 0x41980000, 0x41A00000, 0x41A80000, 0x41B00000, 0x41B80000,
  3126. 0x41C00000
  3127. };
  3128. static int tuning_ctl_set(struct hda_codec *codec, hda_nid_t nid,
  3129. unsigned int *lookup, int idx)
  3130. {
  3131. int i = 0;
  3132. for (i = 0; i < TUNING_CTLS_COUNT; i++)
  3133. if (nid == ca0132_tuning_ctls[i].nid)
  3134. break;
  3135. snd_hda_power_up(codec);
  3136. dspio_set_param(codec, ca0132_tuning_ctls[i].mid, 0x20,
  3137. ca0132_tuning_ctls[i].req,
  3138. &(lookup[idx]), sizeof(unsigned int));
  3139. snd_hda_power_down(codec);
  3140. return 1;
  3141. }
  3142. static int tuning_ctl_get(struct snd_kcontrol *kcontrol,
  3143. struct snd_ctl_elem_value *ucontrol)
  3144. {
  3145. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  3146. struct ca0132_spec *spec = codec->spec;
  3147. hda_nid_t nid = get_amp_nid(kcontrol);
  3148. long *valp = ucontrol->value.integer.value;
  3149. int idx = nid - TUNING_CTL_START_NID;
  3150. *valp = spec->cur_ctl_vals[idx];
  3151. return 0;
  3152. }
  3153. static int voice_focus_ctl_info(struct snd_kcontrol *kcontrol,
  3154. struct snd_ctl_elem_info *uinfo)
  3155. {
  3156. int chs = get_amp_channels(kcontrol);
  3157. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  3158. uinfo->count = chs == 3 ? 2 : 1;
  3159. uinfo->value.integer.min = 20;
  3160. uinfo->value.integer.max = 180;
  3161. uinfo->value.integer.step = 1;
  3162. return 0;
  3163. }
  3164. static int voice_focus_ctl_put(struct snd_kcontrol *kcontrol,
  3165. struct snd_ctl_elem_value *ucontrol)
  3166. {
  3167. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  3168. struct ca0132_spec *spec = codec->spec;
  3169. hda_nid_t nid = get_amp_nid(kcontrol);
  3170. long *valp = ucontrol->value.integer.value;
  3171. int idx;
  3172. idx = nid - TUNING_CTL_START_NID;
  3173. /* any change? */
  3174. if (spec->cur_ctl_vals[idx] == *valp)
  3175. return 0;
  3176. spec->cur_ctl_vals[idx] = *valp;
  3177. idx = *valp - 20;
  3178. tuning_ctl_set(codec, nid, voice_focus_vals_lookup, idx);
  3179. return 1;
  3180. }
  3181. static int mic_svm_ctl_info(struct snd_kcontrol *kcontrol,
  3182. struct snd_ctl_elem_info *uinfo)
  3183. {
  3184. int chs = get_amp_channels(kcontrol);
  3185. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  3186. uinfo->count = chs == 3 ? 2 : 1;
  3187. uinfo->value.integer.min = 0;
  3188. uinfo->value.integer.max = 100;
  3189. uinfo->value.integer.step = 1;
  3190. return 0;
  3191. }
  3192. static int mic_svm_ctl_put(struct snd_kcontrol *kcontrol,
  3193. struct snd_ctl_elem_value *ucontrol)
  3194. {
  3195. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  3196. struct ca0132_spec *spec = codec->spec;
  3197. hda_nid_t nid = get_amp_nid(kcontrol);
  3198. long *valp = ucontrol->value.integer.value;
  3199. int idx;
  3200. idx = nid - TUNING_CTL_START_NID;
  3201. /* any change? */
  3202. if (spec->cur_ctl_vals[idx] == *valp)
  3203. return 0;
  3204. spec->cur_ctl_vals[idx] = *valp;
  3205. idx = *valp;
  3206. tuning_ctl_set(codec, nid, mic_svm_vals_lookup, idx);
  3207. return 0;
  3208. }
  3209. static int equalizer_ctl_info(struct snd_kcontrol *kcontrol,
  3210. struct snd_ctl_elem_info *uinfo)
  3211. {
  3212. int chs = get_amp_channels(kcontrol);
  3213. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  3214. uinfo->count = chs == 3 ? 2 : 1;
  3215. uinfo->value.integer.min = 0;
  3216. uinfo->value.integer.max = 48;
  3217. uinfo->value.integer.step = 1;
  3218. return 0;
  3219. }
  3220. static int equalizer_ctl_put(struct snd_kcontrol *kcontrol,
  3221. struct snd_ctl_elem_value *ucontrol)
  3222. {
  3223. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  3224. struct ca0132_spec *spec = codec->spec;
  3225. hda_nid_t nid = get_amp_nid(kcontrol);
  3226. long *valp = ucontrol->value.integer.value;
  3227. int idx;
  3228. idx = nid - TUNING_CTL_START_NID;
  3229. /* any change? */
  3230. if (spec->cur_ctl_vals[idx] == *valp)
  3231. return 0;
  3232. spec->cur_ctl_vals[idx] = *valp;
  3233. idx = *valp;
  3234. tuning_ctl_set(codec, nid, equalizer_vals_lookup, idx);
  3235. return 1;
  3236. }
  3237. static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(voice_focus_db_scale, 2000, 100, 0);
  3238. static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(eq_db_scale, -2400, 100, 0);
  3239. static int add_tuning_control(struct hda_codec *codec,
  3240. hda_nid_t pnid, hda_nid_t nid,
  3241. const char *name, int dir)
  3242. {
  3243. char namestr[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
  3244. int type = dir ? HDA_INPUT : HDA_OUTPUT;
  3245. struct snd_kcontrol_new knew =
  3246. HDA_CODEC_VOLUME_MONO(namestr, nid, 1, 0, type);
  3247. knew.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  3248. SNDRV_CTL_ELEM_ACCESS_TLV_READ;
  3249. knew.tlv.c = 0;
  3250. knew.tlv.p = 0;
  3251. switch (pnid) {
  3252. case VOICE_FOCUS:
  3253. knew.info = voice_focus_ctl_info;
  3254. knew.get = tuning_ctl_get;
  3255. knew.put = voice_focus_ctl_put;
  3256. knew.tlv.p = voice_focus_db_scale;
  3257. break;
  3258. case MIC_SVM:
  3259. knew.info = mic_svm_ctl_info;
  3260. knew.get = tuning_ctl_get;
  3261. knew.put = mic_svm_ctl_put;
  3262. break;
  3263. case EQUALIZER:
  3264. knew.info = equalizer_ctl_info;
  3265. knew.get = tuning_ctl_get;
  3266. knew.put = equalizer_ctl_put;
  3267. knew.tlv.p = eq_db_scale;
  3268. break;
  3269. default:
  3270. return 0;
  3271. }
  3272. knew.private_value =
  3273. HDA_COMPOSE_AMP_VAL(nid, 1, 0, type);
  3274. sprintf(namestr, "%s %s Volume", name, dirstr[dir]);
  3275. return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
  3276. }
  3277. static int add_tuning_ctls(struct hda_codec *codec)
  3278. {
  3279. int i;
  3280. int err;
  3281. for (i = 0; i < TUNING_CTLS_COUNT; i++) {
  3282. err = add_tuning_control(codec,
  3283. ca0132_tuning_ctls[i].parent_nid,
  3284. ca0132_tuning_ctls[i].nid,
  3285. ca0132_tuning_ctls[i].name,
  3286. ca0132_tuning_ctls[i].direct);
  3287. if (err < 0)
  3288. return err;
  3289. }
  3290. return 0;
  3291. }
  3292. static void ca0132_init_tuning_defaults(struct hda_codec *codec)
  3293. {
  3294. struct ca0132_spec *spec = codec->spec;
  3295. int i;
  3296. /* Wedge Angle defaults to 30. 10 below is 30 - 20. 20 is min. */
  3297. spec->cur_ctl_vals[WEDGE_ANGLE - TUNING_CTL_START_NID] = 10;
  3298. /* SVM level defaults to 0.74. */
  3299. spec->cur_ctl_vals[SVM_LEVEL - TUNING_CTL_START_NID] = 74;
  3300. /* EQ defaults to 0dB. */
  3301. for (i = 2; i < TUNING_CTLS_COUNT; i++)
  3302. spec->cur_ctl_vals[i] = 24;
  3303. }
  3304. #endif /*ENABLE_TUNING_CONTROLS*/
  3305. /*
  3306. * Select the active output.
  3307. * If autodetect is enabled, output will be selected based on jack detection.
  3308. * If jack inserted, headphone will be selected, else built-in speakers
  3309. * If autodetect is disabled, output will be selected based on selection.
  3310. */
  3311. static int ca0132_select_out(struct hda_codec *codec)
  3312. {
  3313. struct ca0132_spec *spec = codec->spec;
  3314. unsigned int pin_ctl;
  3315. int jack_present;
  3316. int auto_jack;
  3317. unsigned int tmp;
  3318. int err;
  3319. codec_dbg(codec, "ca0132_select_out\n");
  3320. snd_hda_power_up_pm(codec);
  3321. auto_jack = spec->vnode_lswitch[VNID_HP_ASEL - VNODE_START_NID];
  3322. if (auto_jack)
  3323. jack_present = snd_hda_jack_detect(codec, spec->unsol_tag_hp);
  3324. else
  3325. jack_present =
  3326. spec->vnode_lswitch[VNID_HP_SEL - VNODE_START_NID];
  3327. if (jack_present)
  3328. spec->cur_out_type = HEADPHONE_OUT;
  3329. else
  3330. spec->cur_out_type = SPEAKER_OUT;
  3331. if (spec->cur_out_type == SPEAKER_OUT) {
  3332. codec_dbg(codec, "ca0132_select_out speaker\n");
  3333. /*speaker out config*/
  3334. tmp = FLOAT_ONE;
  3335. err = dspio_set_uint_param(codec, 0x80, 0x04, tmp);
  3336. if (err < 0)
  3337. goto exit;
  3338. /*enable speaker EQ*/
  3339. tmp = FLOAT_ONE;
  3340. err = dspio_set_uint_param(codec, 0x8f, 0x00, tmp);
  3341. if (err < 0)
  3342. goto exit;
  3343. /* Setup EAPD */
  3344. snd_hda_codec_write(codec, spec->out_pins[1], 0,
  3345. VENDOR_CHIPIO_EAPD_SEL_SET, 0x02);
  3346. snd_hda_codec_write(codec, spec->out_pins[0], 0,
  3347. AC_VERB_SET_EAPD_BTLENABLE, 0x00);
  3348. snd_hda_codec_write(codec, spec->out_pins[0], 0,
  3349. VENDOR_CHIPIO_EAPD_SEL_SET, 0x00);
  3350. snd_hda_codec_write(codec, spec->out_pins[0], 0,
  3351. AC_VERB_SET_EAPD_BTLENABLE, 0x02);
  3352. /* disable headphone node */
  3353. pin_ctl = snd_hda_codec_read(codec, spec->out_pins[1], 0,
  3354. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  3355. snd_hda_set_pin_ctl(codec, spec->out_pins[1],
  3356. pin_ctl & ~PIN_HP);
  3357. /* enable speaker node */
  3358. pin_ctl = snd_hda_codec_read(codec, spec->out_pins[0], 0,
  3359. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  3360. snd_hda_set_pin_ctl(codec, spec->out_pins[0],
  3361. pin_ctl | PIN_OUT);
  3362. } else {
  3363. codec_dbg(codec, "ca0132_select_out hp\n");
  3364. /*headphone out config*/
  3365. tmp = FLOAT_ZERO;
  3366. err = dspio_set_uint_param(codec, 0x80, 0x04, tmp);
  3367. if (err < 0)
  3368. goto exit;
  3369. /*disable speaker EQ*/
  3370. tmp = FLOAT_ZERO;
  3371. err = dspio_set_uint_param(codec, 0x8f, 0x00, tmp);
  3372. if (err < 0)
  3373. goto exit;
  3374. /* Setup EAPD */
  3375. snd_hda_codec_write(codec, spec->out_pins[0], 0,
  3376. VENDOR_CHIPIO_EAPD_SEL_SET, 0x00);
  3377. snd_hda_codec_write(codec, spec->out_pins[0], 0,
  3378. AC_VERB_SET_EAPD_BTLENABLE, 0x00);
  3379. snd_hda_codec_write(codec, spec->out_pins[1], 0,
  3380. VENDOR_CHIPIO_EAPD_SEL_SET, 0x02);
  3381. snd_hda_codec_write(codec, spec->out_pins[0], 0,
  3382. AC_VERB_SET_EAPD_BTLENABLE, 0x02);
  3383. /* disable speaker*/
  3384. pin_ctl = snd_hda_codec_read(codec, spec->out_pins[0], 0,
  3385. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  3386. snd_hda_set_pin_ctl(codec, spec->out_pins[0],
  3387. pin_ctl & ~PIN_HP);
  3388. /* enable headphone*/
  3389. pin_ctl = snd_hda_codec_read(codec, spec->out_pins[1], 0,
  3390. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  3391. snd_hda_set_pin_ctl(codec, spec->out_pins[1],
  3392. pin_ctl | PIN_HP);
  3393. }
  3394. exit:
  3395. snd_hda_power_down_pm(codec);
  3396. return err < 0 ? err : 0;
  3397. }
  3398. /*
  3399. * This function behaves similarly to the ca0132_select_out funciton above,
  3400. * except with a few differences. It adds the ability to select the current
  3401. * output with an enumerated control "output source" if the auto detect
  3402. * mute switch is set to off. If the auto detect mute switch is enabled, it
  3403. * will detect either headphone or lineout(SPEAKER_OUT) from jack detection.
  3404. * It also adds the ability to auto-detect the front headphone port. The only
  3405. * way to select surround is to disable auto detect, and set Surround with the
  3406. * enumerated control.
  3407. */
  3408. static int ca0132_alt_select_out(struct hda_codec *codec)
  3409. {
  3410. struct ca0132_spec *spec = codec->spec;
  3411. unsigned int pin_ctl;
  3412. int jack_present;
  3413. int auto_jack;
  3414. unsigned int i;
  3415. unsigned int tmp;
  3416. int err;
  3417. /* Default Headphone is rear headphone */
  3418. hda_nid_t headphone_nid = spec->out_pins[1];
  3419. codec_dbg(codec, "%s\n", __func__);
  3420. snd_hda_power_up_pm(codec);
  3421. auto_jack = spec->vnode_lswitch[VNID_HP_ASEL - VNODE_START_NID];
  3422. /*
  3423. * If headphone rear or front is plugged in, set to headphone.
  3424. * If neither is plugged in, set to rear line out. Only if
  3425. * hp/speaker auto detect is enabled.
  3426. */
  3427. if (auto_jack) {
  3428. jack_present = snd_hda_jack_detect(codec, spec->unsol_tag_hp) ||
  3429. snd_hda_jack_detect(codec, spec->unsol_tag_front_hp);
  3430. if (jack_present)
  3431. spec->cur_out_type = HEADPHONE_OUT;
  3432. else
  3433. spec->cur_out_type = SPEAKER_OUT;
  3434. } else
  3435. spec->cur_out_type = spec->out_enum_val;
  3436. /* Begin DSP output switch */
  3437. tmp = FLOAT_ONE;
  3438. err = dspio_set_uint_param(codec, 0x96, 0x3A, tmp);
  3439. if (err < 0)
  3440. goto exit;
  3441. switch (spec->cur_out_type) {
  3442. case SPEAKER_OUT:
  3443. codec_dbg(codec, "%s speaker\n", __func__);
  3444. /*speaker out config*/
  3445. switch (spec->quirk) {
  3446. case QUIRK_SBZ:
  3447. ca0132_mmio_gpio_set(codec, 7, false);
  3448. ca0132_mmio_gpio_set(codec, 4, true);
  3449. ca0132_mmio_gpio_set(codec, 1, true);
  3450. chipio_set_control_param(codec, 0x0D, 0x18);
  3451. break;
  3452. case QUIRK_R3DI:
  3453. chipio_set_control_param(codec, 0x0D, 0x24);
  3454. r3di_gpio_out_set(codec, R3DI_LINE_OUT);
  3455. break;
  3456. case QUIRK_R3D:
  3457. chipio_set_control_param(codec, 0x0D, 0x24);
  3458. ca0132_mmio_gpio_set(codec, 1, true);
  3459. break;
  3460. }
  3461. /* disable headphone node */
  3462. pin_ctl = snd_hda_codec_read(codec, spec->out_pins[1], 0,
  3463. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  3464. snd_hda_set_pin_ctl(codec, spec->out_pins[1],
  3465. pin_ctl & ~PIN_HP);
  3466. /* enable line-out node */
  3467. pin_ctl = snd_hda_codec_read(codec, spec->out_pins[0], 0,
  3468. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  3469. snd_hda_set_pin_ctl(codec, spec->out_pins[0],
  3470. pin_ctl | PIN_OUT);
  3471. /* Enable EAPD */
  3472. snd_hda_codec_write(codec, spec->out_pins[0], 0,
  3473. AC_VERB_SET_EAPD_BTLENABLE, 0x01);
  3474. /* If PlayEnhancement is enabled, set different source */
  3475. if (spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID])
  3476. dspio_set_uint_param(codec, 0x80, 0x04, FLOAT_ONE);
  3477. else
  3478. dspio_set_uint_param(codec, 0x80, 0x04, FLOAT_EIGHT);
  3479. break;
  3480. case HEADPHONE_OUT:
  3481. codec_dbg(codec, "%s hp\n", __func__);
  3482. /* Headphone out config*/
  3483. switch (spec->quirk) {
  3484. case QUIRK_SBZ:
  3485. ca0132_mmio_gpio_set(codec, 7, true);
  3486. ca0132_mmio_gpio_set(codec, 4, true);
  3487. ca0132_mmio_gpio_set(codec, 1, false);
  3488. chipio_set_control_param(codec, 0x0D, 0x12);
  3489. break;
  3490. case QUIRK_R3DI:
  3491. chipio_set_control_param(codec, 0x0D, 0x21);
  3492. r3di_gpio_out_set(codec, R3DI_HEADPHONE_OUT);
  3493. break;
  3494. case QUIRK_R3D:
  3495. chipio_set_control_param(codec, 0x0D, 0x21);
  3496. ca0132_mmio_gpio_set(codec, 0x1, false);
  3497. break;
  3498. }
  3499. snd_hda_codec_write(codec, spec->out_pins[0], 0,
  3500. AC_VERB_SET_EAPD_BTLENABLE, 0x00);
  3501. /* disable speaker*/
  3502. pin_ctl = snd_hda_codec_read(codec, spec->out_pins[0], 0,
  3503. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  3504. snd_hda_set_pin_ctl(codec, spec->out_pins[0],
  3505. pin_ctl & ~PIN_HP);
  3506. /* enable headphone, either front or rear */
  3507. if (snd_hda_jack_detect(codec, spec->unsol_tag_front_hp))
  3508. headphone_nid = spec->out_pins[2];
  3509. else if (snd_hda_jack_detect(codec, spec->unsol_tag_hp))
  3510. headphone_nid = spec->out_pins[1];
  3511. pin_ctl = snd_hda_codec_read(codec, headphone_nid, 0,
  3512. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  3513. snd_hda_set_pin_ctl(codec, headphone_nid,
  3514. pin_ctl | PIN_HP);
  3515. if (spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID])
  3516. dspio_set_uint_param(codec, 0x80, 0x04, FLOAT_ONE);
  3517. else
  3518. dspio_set_uint_param(codec, 0x80, 0x04, FLOAT_ZERO);
  3519. break;
  3520. case SURROUND_OUT:
  3521. codec_dbg(codec, "%s surround\n", __func__);
  3522. /* Surround out config*/
  3523. switch (spec->quirk) {
  3524. case QUIRK_SBZ:
  3525. ca0132_mmio_gpio_set(codec, 7, false);
  3526. ca0132_mmio_gpio_set(codec, 4, true);
  3527. ca0132_mmio_gpio_set(codec, 1, true);
  3528. chipio_set_control_param(codec, 0x0D, 0x18);
  3529. break;
  3530. case QUIRK_R3DI:
  3531. chipio_set_control_param(codec, 0x0D, 0x24);
  3532. r3di_gpio_out_set(codec, R3DI_LINE_OUT);
  3533. break;
  3534. case QUIRK_R3D:
  3535. ca0132_mmio_gpio_set(codec, 1, true);
  3536. chipio_set_control_param(codec, 0x0D, 0x24);
  3537. break;
  3538. }
  3539. /* enable line out node */
  3540. pin_ctl = snd_hda_codec_read(codec, spec->out_pins[0], 0,
  3541. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  3542. snd_hda_set_pin_ctl(codec, spec->out_pins[0],
  3543. pin_ctl | PIN_OUT);
  3544. /* Disable headphone out */
  3545. pin_ctl = snd_hda_codec_read(codec, spec->out_pins[1], 0,
  3546. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  3547. snd_hda_set_pin_ctl(codec, spec->out_pins[1],
  3548. pin_ctl & ~PIN_HP);
  3549. /* Enable EAPD on line out */
  3550. snd_hda_codec_write(codec, spec->out_pins[0], 0,
  3551. AC_VERB_SET_EAPD_BTLENABLE, 0x01);
  3552. /* enable center/lfe out node */
  3553. pin_ctl = snd_hda_codec_read(codec, spec->out_pins[2], 0,
  3554. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  3555. snd_hda_set_pin_ctl(codec, spec->out_pins[2],
  3556. pin_ctl | PIN_OUT);
  3557. /* Now set rear surround node as out. */
  3558. pin_ctl = snd_hda_codec_read(codec, spec->out_pins[3], 0,
  3559. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  3560. snd_hda_set_pin_ctl(codec, spec->out_pins[3],
  3561. pin_ctl | PIN_OUT);
  3562. if (spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID])
  3563. dspio_set_uint_param(codec, 0x80, 0x04, FLOAT_ONE);
  3564. else
  3565. dspio_set_uint_param(codec, 0x80, 0x04, FLOAT_EIGHT);
  3566. break;
  3567. }
  3568. /* run through the output dsp commands for line-out */
  3569. for (i = 0; i < alt_out_presets[spec->cur_out_type].commands; i++) {
  3570. err = dspio_set_uint_param(codec,
  3571. alt_out_presets[spec->cur_out_type].mids[i],
  3572. alt_out_presets[spec->cur_out_type].reqs[i],
  3573. alt_out_presets[spec->cur_out_type].vals[i]);
  3574. if (err < 0)
  3575. goto exit;
  3576. }
  3577. exit:
  3578. snd_hda_power_down_pm(codec);
  3579. return err < 0 ? err : 0;
  3580. }
  3581. static void ca0132_unsol_hp_delayed(struct work_struct *work)
  3582. {
  3583. struct ca0132_spec *spec = container_of(
  3584. to_delayed_work(work), struct ca0132_spec, unsol_hp_work);
  3585. struct hda_jack_tbl *jack;
  3586. if (spec->use_alt_functions)
  3587. ca0132_alt_select_out(spec->codec);
  3588. else
  3589. ca0132_select_out(spec->codec);
  3590. jack = snd_hda_jack_tbl_get(spec->codec, spec->unsol_tag_hp);
  3591. if (jack) {
  3592. jack->block_report = 0;
  3593. snd_hda_jack_report_sync(spec->codec);
  3594. }
  3595. }
  3596. static void ca0132_set_dmic(struct hda_codec *codec, int enable);
  3597. static int ca0132_mic_boost_set(struct hda_codec *codec, long val);
  3598. static int ca0132_effects_set(struct hda_codec *codec, hda_nid_t nid, long val);
  3599. static void resume_mic1(struct hda_codec *codec, unsigned int oldval);
  3600. static int stop_mic1(struct hda_codec *codec);
  3601. static int ca0132_cvoice_switch_set(struct hda_codec *codec);
  3602. static int ca0132_alt_mic_boost_set(struct hda_codec *codec, long val);
  3603. /*
  3604. * Select the active VIP source
  3605. */
  3606. static int ca0132_set_vipsource(struct hda_codec *codec, int val)
  3607. {
  3608. struct ca0132_spec *spec = codec->spec;
  3609. unsigned int tmp;
  3610. if (spec->dsp_state != DSP_DOWNLOADED)
  3611. return 0;
  3612. /* if CrystalVoice if off, vipsource should be 0 */
  3613. if (!spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID] ||
  3614. (val == 0)) {
  3615. chipio_set_control_param(codec, CONTROL_PARAM_VIP_SOURCE, 0);
  3616. chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
  3617. chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
  3618. if (spec->cur_mic_type == DIGITAL_MIC)
  3619. tmp = FLOAT_TWO;
  3620. else
  3621. tmp = FLOAT_ONE;
  3622. dspio_set_uint_param(codec, 0x80, 0x00, tmp);
  3623. tmp = FLOAT_ZERO;
  3624. dspio_set_uint_param(codec, 0x80, 0x05, tmp);
  3625. } else {
  3626. chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_16_000);
  3627. chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_16_000);
  3628. if (spec->cur_mic_type == DIGITAL_MIC)
  3629. tmp = FLOAT_TWO;
  3630. else
  3631. tmp = FLOAT_ONE;
  3632. dspio_set_uint_param(codec, 0x80, 0x00, tmp);
  3633. tmp = FLOAT_ONE;
  3634. dspio_set_uint_param(codec, 0x80, 0x05, tmp);
  3635. msleep(20);
  3636. chipio_set_control_param(codec, CONTROL_PARAM_VIP_SOURCE, val);
  3637. }
  3638. return 1;
  3639. }
  3640. static int ca0132_alt_set_vipsource(struct hda_codec *codec, int val)
  3641. {
  3642. struct ca0132_spec *spec = codec->spec;
  3643. unsigned int tmp;
  3644. if (spec->dsp_state != DSP_DOWNLOADED)
  3645. return 0;
  3646. codec_dbg(codec, "%s\n", __func__);
  3647. chipio_set_stream_control(codec, 0x03, 0);
  3648. chipio_set_stream_control(codec, 0x04, 0);
  3649. /* if CrystalVoice is off, vipsource should be 0 */
  3650. if (!spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID] ||
  3651. (val == 0) || spec->in_enum_val == REAR_LINE_IN) {
  3652. codec_dbg(codec, "%s: off.", __func__);
  3653. chipio_set_control_param(codec, CONTROL_PARAM_VIP_SOURCE, 0);
  3654. tmp = FLOAT_ZERO;
  3655. dspio_set_uint_param(codec, 0x80, 0x05, tmp);
  3656. chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
  3657. chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
  3658. if (spec->quirk == QUIRK_R3DI)
  3659. chipio_set_conn_rate(codec, 0x0F, SR_96_000);
  3660. if (spec->in_enum_val == REAR_LINE_IN)
  3661. tmp = FLOAT_ZERO;
  3662. else {
  3663. if (spec->quirk == QUIRK_SBZ)
  3664. tmp = FLOAT_THREE;
  3665. else
  3666. tmp = FLOAT_ONE;
  3667. }
  3668. dspio_set_uint_param(codec, 0x80, 0x00, tmp);
  3669. } else {
  3670. codec_dbg(codec, "%s: on.", __func__);
  3671. chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_16_000);
  3672. chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_16_000);
  3673. if (spec->quirk == QUIRK_R3DI)
  3674. chipio_set_conn_rate(codec, 0x0F, SR_16_000);
  3675. if (spec->effects_switch[VOICE_FOCUS - EFFECT_START_NID])
  3676. tmp = FLOAT_TWO;
  3677. else
  3678. tmp = FLOAT_ONE;
  3679. dspio_set_uint_param(codec, 0x80, 0x00, tmp);
  3680. tmp = FLOAT_ONE;
  3681. dspio_set_uint_param(codec, 0x80, 0x05, tmp);
  3682. msleep(20);
  3683. chipio_set_control_param(codec, CONTROL_PARAM_VIP_SOURCE, val);
  3684. }
  3685. chipio_set_stream_control(codec, 0x03, 1);
  3686. chipio_set_stream_control(codec, 0x04, 1);
  3687. return 1;
  3688. }
  3689. /*
  3690. * Select the active microphone.
  3691. * If autodetect is enabled, mic will be selected based on jack detection.
  3692. * If jack inserted, ext.mic will be selected, else built-in mic
  3693. * If autodetect is disabled, mic will be selected based on selection.
  3694. */
  3695. static int ca0132_select_mic(struct hda_codec *codec)
  3696. {
  3697. struct ca0132_spec *spec = codec->spec;
  3698. int jack_present;
  3699. int auto_jack;
  3700. codec_dbg(codec, "ca0132_select_mic\n");
  3701. snd_hda_power_up_pm(codec);
  3702. auto_jack = spec->vnode_lswitch[VNID_AMIC1_ASEL - VNODE_START_NID];
  3703. if (auto_jack)
  3704. jack_present = snd_hda_jack_detect(codec, spec->unsol_tag_amic1);
  3705. else
  3706. jack_present =
  3707. spec->vnode_lswitch[VNID_AMIC1_SEL - VNODE_START_NID];
  3708. if (jack_present)
  3709. spec->cur_mic_type = LINE_MIC_IN;
  3710. else
  3711. spec->cur_mic_type = DIGITAL_MIC;
  3712. if (spec->cur_mic_type == DIGITAL_MIC) {
  3713. /* enable digital Mic */
  3714. chipio_set_conn_rate(codec, MEM_CONNID_DMIC, SR_32_000);
  3715. ca0132_set_dmic(codec, 1);
  3716. ca0132_mic_boost_set(codec, 0);
  3717. /* set voice focus */
  3718. ca0132_effects_set(codec, VOICE_FOCUS,
  3719. spec->effects_switch
  3720. [VOICE_FOCUS - EFFECT_START_NID]);
  3721. } else {
  3722. /* disable digital Mic */
  3723. chipio_set_conn_rate(codec, MEM_CONNID_DMIC, SR_96_000);
  3724. ca0132_set_dmic(codec, 0);
  3725. ca0132_mic_boost_set(codec, spec->cur_mic_boost);
  3726. /* disable voice focus */
  3727. ca0132_effects_set(codec, VOICE_FOCUS, 0);
  3728. }
  3729. snd_hda_power_down_pm(codec);
  3730. return 0;
  3731. }
  3732. /*
  3733. * Select the active input.
  3734. * Mic detection isn't used, because it's kind of pointless on the SBZ.
  3735. * The front mic has no jack-detection, so the only way to switch to it
  3736. * is to do it manually in alsamixer.
  3737. */
  3738. static int ca0132_alt_select_in(struct hda_codec *codec)
  3739. {
  3740. struct ca0132_spec *spec = codec->spec;
  3741. unsigned int tmp;
  3742. codec_dbg(codec, "%s\n", __func__);
  3743. snd_hda_power_up_pm(codec);
  3744. chipio_set_stream_control(codec, 0x03, 0);
  3745. chipio_set_stream_control(codec, 0x04, 0);
  3746. spec->cur_mic_type = spec->in_enum_val;
  3747. switch (spec->cur_mic_type) {
  3748. case REAR_MIC:
  3749. switch (spec->quirk) {
  3750. case QUIRK_SBZ:
  3751. case QUIRK_R3D:
  3752. ca0132_mmio_gpio_set(codec, 0, false);
  3753. tmp = FLOAT_THREE;
  3754. break;
  3755. case QUIRK_R3DI:
  3756. r3di_gpio_mic_set(codec, R3DI_REAR_MIC);
  3757. tmp = FLOAT_ONE;
  3758. break;
  3759. default:
  3760. tmp = FLOAT_ONE;
  3761. break;
  3762. }
  3763. chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
  3764. chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
  3765. if (spec->quirk == QUIRK_R3DI)
  3766. chipio_set_conn_rate(codec, 0x0F, SR_96_000);
  3767. dspio_set_uint_param(codec, 0x80, 0x00, tmp);
  3768. chipio_set_stream_control(codec, 0x03, 1);
  3769. chipio_set_stream_control(codec, 0x04, 1);
  3770. if (spec->quirk == QUIRK_SBZ) {
  3771. chipio_write(codec, 0x18B098, 0x0000000C);
  3772. chipio_write(codec, 0x18B09C, 0x0000000C);
  3773. }
  3774. ca0132_alt_mic_boost_set(codec, spec->mic_boost_enum_val);
  3775. break;
  3776. case REAR_LINE_IN:
  3777. ca0132_mic_boost_set(codec, 0);
  3778. switch (spec->quirk) {
  3779. case QUIRK_SBZ:
  3780. case QUIRK_R3D:
  3781. ca0132_mmio_gpio_set(codec, 0, false);
  3782. break;
  3783. case QUIRK_R3DI:
  3784. r3di_gpio_mic_set(codec, R3DI_REAR_MIC);
  3785. break;
  3786. }
  3787. chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
  3788. chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
  3789. if (spec->quirk == QUIRK_R3DI)
  3790. chipio_set_conn_rate(codec, 0x0F, SR_96_000);
  3791. tmp = FLOAT_ZERO;
  3792. dspio_set_uint_param(codec, 0x80, 0x00, tmp);
  3793. if (spec->quirk == QUIRK_SBZ) {
  3794. chipio_write(codec, 0x18B098, 0x00000000);
  3795. chipio_write(codec, 0x18B09C, 0x00000000);
  3796. }
  3797. chipio_set_stream_control(codec, 0x03, 1);
  3798. chipio_set_stream_control(codec, 0x04, 1);
  3799. break;
  3800. case FRONT_MIC:
  3801. switch (spec->quirk) {
  3802. case QUIRK_SBZ:
  3803. case QUIRK_R3D:
  3804. ca0132_mmio_gpio_set(codec, 0, true);
  3805. ca0132_mmio_gpio_set(codec, 5, false);
  3806. tmp = FLOAT_THREE;
  3807. break;
  3808. case QUIRK_R3DI:
  3809. r3di_gpio_mic_set(codec, R3DI_FRONT_MIC);
  3810. tmp = FLOAT_ONE;
  3811. break;
  3812. default:
  3813. tmp = FLOAT_ONE;
  3814. break;
  3815. }
  3816. chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
  3817. chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
  3818. if (spec->quirk == QUIRK_R3DI)
  3819. chipio_set_conn_rate(codec, 0x0F, SR_96_000);
  3820. dspio_set_uint_param(codec, 0x80, 0x00, tmp);
  3821. chipio_set_stream_control(codec, 0x03, 1);
  3822. chipio_set_stream_control(codec, 0x04, 1);
  3823. if (spec->quirk == QUIRK_SBZ) {
  3824. chipio_write(codec, 0x18B098, 0x0000000C);
  3825. chipio_write(codec, 0x18B09C, 0x000000CC);
  3826. }
  3827. ca0132_alt_mic_boost_set(codec, spec->mic_boost_enum_val);
  3828. break;
  3829. }
  3830. ca0132_cvoice_switch_set(codec);
  3831. snd_hda_power_down_pm(codec);
  3832. return 0;
  3833. }
  3834. /*
  3835. * Check if VNODE settings take effect immediately.
  3836. */
  3837. static bool ca0132_is_vnode_effective(struct hda_codec *codec,
  3838. hda_nid_t vnid,
  3839. hda_nid_t *shared_nid)
  3840. {
  3841. struct ca0132_spec *spec = codec->spec;
  3842. hda_nid_t nid;
  3843. switch (vnid) {
  3844. case VNID_SPK:
  3845. nid = spec->shared_out_nid;
  3846. break;
  3847. case VNID_MIC:
  3848. nid = spec->shared_mic_nid;
  3849. break;
  3850. default:
  3851. return false;
  3852. }
  3853. if (shared_nid)
  3854. *shared_nid = nid;
  3855. return true;
  3856. }
  3857. /*
  3858. * The following functions are control change helpers.
  3859. * They return 0 if no changed. Return 1 if changed.
  3860. */
  3861. static int ca0132_voicefx_set(struct hda_codec *codec, int enable)
  3862. {
  3863. struct ca0132_spec *spec = codec->spec;
  3864. unsigned int tmp;
  3865. /* based on CrystalVoice state to enable VoiceFX. */
  3866. if (enable) {
  3867. tmp = spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID] ?
  3868. FLOAT_ONE : FLOAT_ZERO;
  3869. } else {
  3870. tmp = FLOAT_ZERO;
  3871. }
  3872. dspio_set_uint_param(codec, ca0132_voicefx.mid,
  3873. ca0132_voicefx.reqs[0], tmp);
  3874. return 1;
  3875. }
  3876. /*
  3877. * Set the effects parameters
  3878. */
  3879. static int ca0132_effects_set(struct hda_codec *codec, hda_nid_t nid, long val)
  3880. {
  3881. struct ca0132_spec *spec = codec->spec;
  3882. unsigned int on, tmp;
  3883. int num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT;
  3884. int err = 0;
  3885. int idx = nid - EFFECT_START_NID;
  3886. if ((idx < 0) || (idx >= num_fx))
  3887. return 0; /* no changed */
  3888. /* for out effect, qualify with PE */
  3889. if ((nid >= OUT_EFFECT_START_NID) && (nid < OUT_EFFECT_END_NID)) {
  3890. /* if PE if off, turn off out effects. */
  3891. if (!spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID])
  3892. val = 0;
  3893. }
  3894. /* for in effect, qualify with CrystalVoice */
  3895. if ((nid >= IN_EFFECT_START_NID) && (nid < IN_EFFECT_END_NID)) {
  3896. /* if CrystalVoice if off, turn off in effects. */
  3897. if (!spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID])
  3898. val = 0;
  3899. /* Voice Focus applies to 2-ch Mic, Digital Mic */
  3900. if ((nid == VOICE_FOCUS) && (spec->cur_mic_type != DIGITAL_MIC))
  3901. val = 0;
  3902. /* If Voice Focus on SBZ, set to two channel. */
  3903. if ((nid == VOICE_FOCUS) && (spec->use_pci_mmio)
  3904. && (spec->cur_mic_type != REAR_LINE_IN)) {
  3905. if (spec->effects_switch[CRYSTAL_VOICE -
  3906. EFFECT_START_NID]) {
  3907. if (spec->effects_switch[VOICE_FOCUS -
  3908. EFFECT_START_NID]) {
  3909. tmp = FLOAT_TWO;
  3910. val = 1;
  3911. } else
  3912. tmp = FLOAT_ONE;
  3913. dspio_set_uint_param(codec, 0x80, 0x00, tmp);
  3914. }
  3915. }
  3916. /*
  3917. * For SBZ noise reduction, there's an extra command
  3918. * to module ID 0x47. No clue why.
  3919. */
  3920. if ((nid == NOISE_REDUCTION) && (spec->use_pci_mmio)
  3921. && (spec->cur_mic_type != REAR_LINE_IN)) {
  3922. if (spec->effects_switch[CRYSTAL_VOICE -
  3923. EFFECT_START_NID]) {
  3924. if (spec->effects_switch[NOISE_REDUCTION -
  3925. EFFECT_START_NID])
  3926. tmp = FLOAT_ONE;
  3927. else
  3928. tmp = FLOAT_ZERO;
  3929. } else
  3930. tmp = FLOAT_ZERO;
  3931. dspio_set_uint_param(codec, 0x47, 0x00, tmp);
  3932. }
  3933. /* If rear line in disable effects. */
  3934. if (spec->use_alt_functions &&
  3935. spec->in_enum_val == REAR_LINE_IN)
  3936. val = 0;
  3937. }
  3938. codec_dbg(codec, "ca0132_effect_set: nid=0x%x, val=%ld\n",
  3939. nid, val);
  3940. on = (val == 0) ? FLOAT_ZERO : FLOAT_ONE;
  3941. err = dspio_set_uint_param(codec, ca0132_effects[idx].mid,
  3942. ca0132_effects[idx].reqs[0], on);
  3943. if (err < 0)
  3944. return 0; /* no changed */
  3945. return 1;
  3946. }
  3947. /*
  3948. * Turn on/off Playback Enhancements
  3949. */
  3950. static int ca0132_pe_switch_set(struct hda_codec *codec)
  3951. {
  3952. struct ca0132_spec *spec = codec->spec;
  3953. hda_nid_t nid;
  3954. int i, ret = 0;
  3955. codec_dbg(codec, "ca0132_pe_switch_set: val=%ld\n",
  3956. spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID]);
  3957. if (spec->use_alt_functions)
  3958. ca0132_alt_select_out(codec);
  3959. i = OUT_EFFECT_START_NID - EFFECT_START_NID;
  3960. nid = OUT_EFFECT_START_NID;
  3961. /* PE affects all out effects */
  3962. for (; nid < OUT_EFFECT_END_NID; nid++, i++)
  3963. ret |= ca0132_effects_set(codec, nid, spec->effects_switch[i]);
  3964. return ret;
  3965. }
  3966. /* Check if Mic1 is streaming, if so, stop streaming */
  3967. static int stop_mic1(struct hda_codec *codec)
  3968. {
  3969. struct ca0132_spec *spec = codec->spec;
  3970. unsigned int oldval = snd_hda_codec_read(codec, spec->adcs[0], 0,
  3971. AC_VERB_GET_CONV, 0);
  3972. if (oldval != 0)
  3973. snd_hda_codec_write(codec, spec->adcs[0], 0,
  3974. AC_VERB_SET_CHANNEL_STREAMID,
  3975. 0);
  3976. return oldval;
  3977. }
  3978. /* Resume Mic1 streaming if it was stopped. */
  3979. static void resume_mic1(struct hda_codec *codec, unsigned int oldval)
  3980. {
  3981. struct ca0132_spec *spec = codec->spec;
  3982. /* Restore the previous stream and channel */
  3983. if (oldval != 0)
  3984. snd_hda_codec_write(codec, spec->adcs[0], 0,
  3985. AC_VERB_SET_CHANNEL_STREAMID,
  3986. oldval);
  3987. }
  3988. /*
  3989. * Turn on/off CrystalVoice
  3990. */
  3991. static int ca0132_cvoice_switch_set(struct hda_codec *codec)
  3992. {
  3993. struct ca0132_spec *spec = codec->spec;
  3994. hda_nid_t nid;
  3995. int i, ret = 0;
  3996. unsigned int oldval;
  3997. codec_dbg(codec, "ca0132_cvoice_switch_set: val=%ld\n",
  3998. spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID]);
  3999. i = IN_EFFECT_START_NID - EFFECT_START_NID;
  4000. nid = IN_EFFECT_START_NID;
  4001. /* CrystalVoice affects all in effects */
  4002. for (; nid < IN_EFFECT_END_NID; nid++, i++)
  4003. ret |= ca0132_effects_set(codec, nid, spec->effects_switch[i]);
  4004. /* including VoiceFX */
  4005. ret |= ca0132_voicefx_set(codec, (spec->voicefx_val ? 1 : 0));
  4006. /* set correct vipsource */
  4007. oldval = stop_mic1(codec);
  4008. if (spec->use_alt_functions)
  4009. ret |= ca0132_alt_set_vipsource(codec, 1);
  4010. else
  4011. ret |= ca0132_set_vipsource(codec, 1);
  4012. resume_mic1(codec, oldval);
  4013. return ret;
  4014. }
  4015. static int ca0132_mic_boost_set(struct hda_codec *codec, long val)
  4016. {
  4017. struct ca0132_spec *spec = codec->spec;
  4018. int ret = 0;
  4019. if (val) /* on */
  4020. ret = snd_hda_codec_amp_update(codec, spec->input_pins[0], 0,
  4021. HDA_INPUT, 0, HDA_AMP_VOLMASK, 3);
  4022. else /* off */
  4023. ret = snd_hda_codec_amp_update(codec, spec->input_pins[0], 0,
  4024. HDA_INPUT, 0, HDA_AMP_VOLMASK, 0);
  4025. return ret;
  4026. }
  4027. static int ca0132_alt_mic_boost_set(struct hda_codec *codec, long val)
  4028. {
  4029. struct ca0132_spec *spec = codec->spec;
  4030. int ret = 0;
  4031. ret = snd_hda_codec_amp_update(codec, spec->input_pins[0], 0,
  4032. HDA_INPUT, 0, HDA_AMP_VOLMASK, val);
  4033. return ret;
  4034. }
  4035. static int ca0132_vnode_switch_set(struct snd_kcontrol *kcontrol,
  4036. struct snd_ctl_elem_value *ucontrol)
  4037. {
  4038. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  4039. hda_nid_t nid = get_amp_nid(kcontrol);
  4040. hda_nid_t shared_nid = 0;
  4041. bool effective;
  4042. int ret = 0;
  4043. struct ca0132_spec *spec = codec->spec;
  4044. int auto_jack;
  4045. if (nid == VNID_HP_SEL) {
  4046. auto_jack =
  4047. spec->vnode_lswitch[VNID_HP_ASEL - VNODE_START_NID];
  4048. if (!auto_jack) {
  4049. if (spec->use_alt_functions)
  4050. ca0132_alt_select_out(codec);
  4051. else
  4052. ca0132_select_out(codec);
  4053. }
  4054. return 1;
  4055. }
  4056. if (nid == VNID_AMIC1_SEL) {
  4057. auto_jack =
  4058. spec->vnode_lswitch[VNID_AMIC1_ASEL - VNODE_START_NID];
  4059. if (!auto_jack)
  4060. ca0132_select_mic(codec);
  4061. return 1;
  4062. }
  4063. if (nid == VNID_HP_ASEL) {
  4064. if (spec->use_alt_functions)
  4065. ca0132_alt_select_out(codec);
  4066. else
  4067. ca0132_select_out(codec);
  4068. return 1;
  4069. }
  4070. if (nid == VNID_AMIC1_ASEL) {
  4071. ca0132_select_mic(codec);
  4072. return 1;
  4073. }
  4074. /* if effective conditions, then update hw immediately. */
  4075. effective = ca0132_is_vnode_effective(codec, nid, &shared_nid);
  4076. if (effective) {
  4077. int dir = get_amp_direction(kcontrol);
  4078. int ch = get_amp_channels(kcontrol);
  4079. unsigned long pval;
  4080. mutex_lock(&codec->control_mutex);
  4081. pval = kcontrol->private_value;
  4082. kcontrol->private_value = HDA_COMPOSE_AMP_VAL(shared_nid, ch,
  4083. 0, dir);
  4084. ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
  4085. kcontrol->private_value = pval;
  4086. mutex_unlock(&codec->control_mutex);
  4087. }
  4088. return ret;
  4089. }
  4090. /* End of control change helpers. */
  4091. /*
  4092. * Below I've added controls to mess with the effect levels, I've only enabled
  4093. * them on the Sound Blaster Z, but they would probably also work on the
  4094. * Chromebook. I figured they were probably tuned specifically for it, and left
  4095. * out for a reason.
  4096. */
  4097. /* Sets DSP effect level from the sliders above the controls */
  4098. static int ca0132_alt_slider_ctl_set(struct hda_codec *codec, hda_nid_t nid,
  4099. const unsigned int *lookup, int idx)
  4100. {
  4101. int i = 0;
  4102. unsigned int y;
  4103. /*
  4104. * For X_BASS, req 2 is actually crossover freq instead of
  4105. * effect level
  4106. */
  4107. if (nid == X_BASS)
  4108. y = 2;
  4109. else
  4110. y = 1;
  4111. snd_hda_power_up(codec);
  4112. if (nid == XBASS_XOVER) {
  4113. for (i = 0; i < OUT_EFFECTS_COUNT; i++)
  4114. if (ca0132_effects[i].nid == X_BASS)
  4115. break;
  4116. dspio_set_param(codec, ca0132_effects[i].mid, 0x20,
  4117. ca0132_effects[i].reqs[1],
  4118. &(lookup[idx - 1]), sizeof(unsigned int));
  4119. } else {
  4120. /* Find the actual effect structure */
  4121. for (i = 0; i < OUT_EFFECTS_COUNT; i++)
  4122. if (nid == ca0132_effects[i].nid)
  4123. break;
  4124. dspio_set_param(codec, ca0132_effects[i].mid, 0x20,
  4125. ca0132_effects[i].reqs[y],
  4126. &(lookup[idx]), sizeof(unsigned int));
  4127. }
  4128. snd_hda_power_down(codec);
  4129. return 0;
  4130. }
  4131. static int ca0132_alt_xbass_xover_slider_ctl_get(struct snd_kcontrol *kcontrol,
  4132. struct snd_ctl_elem_value *ucontrol)
  4133. {
  4134. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  4135. struct ca0132_spec *spec = codec->spec;
  4136. long *valp = ucontrol->value.integer.value;
  4137. *valp = spec->xbass_xover_freq;
  4138. return 0;
  4139. }
  4140. static int ca0132_alt_slider_ctl_get(struct snd_kcontrol *kcontrol,
  4141. struct snd_ctl_elem_value *ucontrol)
  4142. {
  4143. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  4144. struct ca0132_spec *spec = codec->spec;
  4145. hda_nid_t nid = get_amp_nid(kcontrol);
  4146. long *valp = ucontrol->value.integer.value;
  4147. int idx = nid - OUT_EFFECT_START_NID;
  4148. *valp = spec->fx_ctl_val[idx];
  4149. return 0;
  4150. }
  4151. /*
  4152. * The X-bass crossover starts at 10hz, so the min is 1. The
  4153. * frequency is set in multiples of 10.
  4154. */
  4155. static int ca0132_alt_xbass_xover_slider_info(struct snd_kcontrol *kcontrol,
  4156. struct snd_ctl_elem_info *uinfo)
  4157. {
  4158. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  4159. uinfo->count = 1;
  4160. uinfo->value.integer.min = 1;
  4161. uinfo->value.integer.max = 100;
  4162. uinfo->value.integer.step = 1;
  4163. return 0;
  4164. }
  4165. static int ca0132_alt_effect_slider_info(struct snd_kcontrol *kcontrol,
  4166. struct snd_ctl_elem_info *uinfo)
  4167. {
  4168. int chs = get_amp_channels(kcontrol);
  4169. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  4170. uinfo->count = chs == 3 ? 2 : 1;
  4171. uinfo->value.integer.min = 0;
  4172. uinfo->value.integer.max = 100;
  4173. uinfo->value.integer.step = 1;
  4174. return 0;
  4175. }
  4176. static int ca0132_alt_xbass_xover_slider_put(struct snd_kcontrol *kcontrol,
  4177. struct snd_ctl_elem_value *ucontrol)
  4178. {
  4179. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  4180. struct ca0132_spec *spec = codec->spec;
  4181. hda_nid_t nid = get_amp_nid(kcontrol);
  4182. long *valp = ucontrol->value.integer.value;
  4183. int idx;
  4184. /* any change? */
  4185. if (spec->xbass_xover_freq == *valp)
  4186. return 0;
  4187. spec->xbass_xover_freq = *valp;
  4188. idx = *valp;
  4189. ca0132_alt_slider_ctl_set(codec, nid, float_xbass_xover_lookup, idx);
  4190. return 0;
  4191. }
  4192. static int ca0132_alt_effect_slider_put(struct snd_kcontrol *kcontrol,
  4193. struct snd_ctl_elem_value *ucontrol)
  4194. {
  4195. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  4196. struct ca0132_spec *spec = codec->spec;
  4197. hda_nid_t nid = get_amp_nid(kcontrol);
  4198. long *valp = ucontrol->value.integer.value;
  4199. int idx;
  4200. idx = nid - EFFECT_START_NID;
  4201. /* any change? */
  4202. if (spec->fx_ctl_val[idx] == *valp)
  4203. return 0;
  4204. spec->fx_ctl_val[idx] = *valp;
  4205. idx = *valp;
  4206. ca0132_alt_slider_ctl_set(codec, nid, float_zero_to_one_lookup, idx);
  4207. return 0;
  4208. }
  4209. /*
  4210. * Mic Boost Enum for alternative ca0132 codecs. I didn't like that the original
  4211. * only has off or full 30 dB, and didn't like making a volume slider that has
  4212. * traditional 0-100 in alsamixer that goes in big steps. I like enum better.
  4213. */
  4214. #define MIC_BOOST_NUM_OF_STEPS 4
  4215. #define MIC_BOOST_ENUM_MAX_STRLEN 10
  4216. static int ca0132_alt_mic_boost_info(struct snd_kcontrol *kcontrol,
  4217. struct snd_ctl_elem_info *uinfo)
  4218. {
  4219. char *sfx = "dB";
  4220. char namestr[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
  4221. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  4222. uinfo->count = 1;
  4223. uinfo->value.enumerated.items = MIC_BOOST_NUM_OF_STEPS;
  4224. if (uinfo->value.enumerated.item >= MIC_BOOST_NUM_OF_STEPS)
  4225. uinfo->value.enumerated.item = MIC_BOOST_NUM_OF_STEPS - 1;
  4226. sprintf(namestr, "%d %s", (uinfo->value.enumerated.item * 10), sfx);
  4227. strcpy(uinfo->value.enumerated.name, namestr);
  4228. return 0;
  4229. }
  4230. static int ca0132_alt_mic_boost_get(struct snd_kcontrol *kcontrol,
  4231. struct snd_ctl_elem_value *ucontrol)
  4232. {
  4233. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  4234. struct ca0132_spec *spec = codec->spec;
  4235. ucontrol->value.enumerated.item[0] = spec->mic_boost_enum_val;
  4236. return 0;
  4237. }
  4238. static int ca0132_alt_mic_boost_put(struct snd_kcontrol *kcontrol,
  4239. struct snd_ctl_elem_value *ucontrol)
  4240. {
  4241. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  4242. struct ca0132_spec *spec = codec->spec;
  4243. int sel = ucontrol->value.enumerated.item[0];
  4244. unsigned int items = MIC_BOOST_NUM_OF_STEPS;
  4245. if (sel >= items)
  4246. return 0;
  4247. codec_dbg(codec, "ca0132_alt_mic_boost: boost=%d\n",
  4248. sel);
  4249. spec->mic_boost_enum_val = sel;
  4250. if (spec->in_enum_val != REAR_LINE_IN)
  4251. ca0132_alt_mic_boost_set(codec, spec->mic_boost_enum_val);
  4252. return 1;
  4253. }
  4254. /*
  4255. * Input Select Control for alternative ca0132 codecs. This exists because
  4256. * front microphone has no auto-detect, and we need a way to set the rear
  4257. * as line-in
  4258. */
  4259. static int ca0132_alt_input_source_info(struct snd_kcontrol *kcontrol,
  4260. struct snd_ctl_elem_info *uinfo)
  4261. {
  4262. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  4263. uinfo->count = 1;
  4264. uinfo->value.enumerated.items = IN_SRC_NUM_OF_INPUTS;
  4265. if (uinfo->value.enumerated.item >= IN_SRC_NUM_OF_INPUTS)
  4266. uinfo->value.enumerated.item = IN_SRC_NUM_OF_INPUTS - 1;
  4267. strcpy(uinfo->value.enumerated.name,
  4268. in_src_str[uinfo->value.enumerated.item]);
  4269. return 0;
  4270. }
  4271. static int ca0132_alt_input_source_get(struct snd_kcontrol *kcontrol,
  4272. struct snd_ctl_elem_value *ucontrol)
  4273. {
  4274. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  4275. struct ca0132_spec *spec = codec->spec;
  4276. ucontrol->value.enumerated.item[0] = spec->in_enum_val;
  4277. return 0;
  4278. }
  4279. static int ca0132_alt_input_source_put(struct snd_kcontrol *kcontrol,
  4280. struct snd_ctl_elem_value *ucontrol)
  4281. {
  4282. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  4283. struct ca0132_spec *spec = codec->spec;
  4284. int sel = ucontrol->value.enumerated.item[0];
  4285. unsigned int items = IN_SRC_NUM_OF_INPUTS;
  4286. if (sel >= items)
  4287. return 0;
  4288. codec_dbg(codec, "ca0132_alt_input_select: sel=%d, preset=%s\n",
  4289. sel, in_src_str[sel]);
  4290. spec->in_enum_val = sel;
  4291. ca0132_alt_select_in(codec);
  4292. return 1;
  4293. }
  4294. /* Sound Blaster Z Output Select Control */
  4295. static int ca0132_alt_output_select_get_info(struct snd_kcontrol *kcontrol,
  4296. struct snd_ctl_elem_info *uinfo)
  4297. {
  4298. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  4299. uinfo->count = 1;
  4300. uinfo->value.enumerated.items = NUM_OF_OUTPUTS;
  4301. if (uinfo->value.enumerated.item >= NUM_OF_OUTPUTS)
  4302. uinfo->value.enumerated.item = NUM_OF_OUTPUTS - 1;
  4303. strcpy(uinfo->value.enumerated.name,
  4304. alt_out_presets[uinfo->value.enumerated.item].name);
  4305. return 0;
  4306. }
  4307. static int ca0132_alt_output_select_get(struct snd_kcontrol *kcontrol,
  4308. struct snd_ctl_elem_value *ucontrol)
  4309. {
  4310. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  4311. struct ca0132_spec *spec = codec->spec;
  4312. ucontrol->value.enumerated.item[0] = spec->out_enum_val;
  4313. return 0;
  4314. }
  4315. static int ca0132_alt_output_select_put(struct snd_kcontrol *kcontrol,
  4316. struct snd_ctl_elem_value *ucontrol)
  4317. {
  4318. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  4319. struct ca0132_spec *spec = codec->spec;
  4320. int sel = ucontrol->value.enumerated.item[0];
  4321. unsigned int items = NUM_OF_OUTPUTS;
  4322. unsigned int auto_jack;
  4323. if (sel >= items)
  4324. return 0;
  4325. codec_dbg(codec, "ca0132_alt_output_select: sel=%d, preset=%s\n",
  4326. sel, alt_out_presets[sel].name);
  4327. spec->out_enum_val = sel;
  4328. auto_jack = spec->vnode_lswitch[VNID_HP_ASEL - VNODE_START_NID];
  4329. if (!auto_jack)
  4330. ca0132_alt_select_out(codec);
  4331. return 1;
  4332. }
  4333. /*
  4334. * Smart Volume output setting control. Three different settings, Normal,
  4335. * which takes the value from the smart volume slider. The two others, loud
  4336. * and night, disregard the slider value and have uneditable values.
  4337. */
  4338. #define NUM_OF_SVM_SETTINGS 3
  4339. static const char *const out_svm_set_enum_str[3] = {"Normal", "Loud", "Night" };
  4340. static int ca0132_alt_svm_setting_info(struct snd_kcontrol *kcontrol,
  4341. struct snd_ctl_elem_info *uinfo)
  4342. {
  4343. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  4344. uinfo->count = 1;
  4345. uinfo->value.enumerated.items = NUM_OF_SVM_SETTINGS;
  4346. if (uinfo->value.enumerated.item >= NUM_OF_SVM_SETTINGS)
  4347. uinfo->value.enumerated.item = NUM_OF_SVM_SETTINGS - 1;
  4348. strcpy(uinfo->value.enumerated.name,
  4349. out_svm_set_enum_str[uinfo->value.enumerated.item]);
  4350. return 0;
  4351. }
  4352. static int ca0132_alt_svm_setting_get(struct snd_kcontrol *kcontrol,
  4353. struct snd_ctl_elem_value *ucontrol)
  4354. {
  4355. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  4356. struct ca0132_spec *spec = codec->spec;
  4357. ucontrol->value.enumerated.item[0] = spec->smart_volume_setting;
  4358. return 0;
  4359. }
  4360. static int ca0132_alt_svm_setting_put(struct snd_kcontrol *kcontrol,
  4361. struct snd_ctl_elem_value *ucontrol)
  4362. {
  4363. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  4364. struct ca0132_spec *spec = codec->spec;
  4365. int sel = ucontrol->value.enumerated.item[0];
  4366. unsigned int items = NUM_OF_SVM_SETTINGS;
  4367. unsigned int idx = SMART_VOLUME - EFFECT_START_NID;
  4368. unsigned int tmp;
  4369. if (sel >= items)
  4370. return 0;
  4371. codec_dbg(codec, "ca0132_alt_svm_setting: sel=%d, preset=%s\n",
  4372. sel, out_svm_set_enum_str[sel]);
  4373. spec->smart_volume_setting = sel;
  4374. switch (sel) {
  4375. case 0:
  4376. tmp = FLOAT_ZERO;
  4377. break;
  4378. case 1:
  4379. tmp = FLOAT_ONE;
  4380. break;
  4381. case 2:
  4382. tmp = FLOAT_TWO;
  4383. break;
  4384. default:
  4385. tmp = FLOAT_ZERO;
  4386. break;
  4387. }
  4388. /* Req 2 is the Smart Volume Setting req. */
  4389. dspio_set_uint_param(codec, ca0132_effects[idx].mid,
  4390. ca0132_effects[idx].reqs[2], tmp);
  4391. return 1;
  4392. }
  4393. /* Sound Blaster Z EQ preset controls */
  4394. static int ca0132_alt_eq_preset_info(struct snd_kcontrol *kcontrol,
  4395. struct snd_ctl_elem_info *uinfo)
  4396. {
  4397. unsigned int items = ARRAY_SIZE(ca0132_alt_eq_presets);
  4398. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  4399. uinfo->count = 1;
  4400. uinfo->value.enumerated.items = items;
  4401. if (uinfo->value.enumerated.item >= items)
  4402. uinfo->value.enumerated.item = items - 1;
  4403. strcpy(uinfo->value.enumerated.name,
  4404. ca0132_alt_eq_presets[uinfo->value.enumerated.item].name);
  4405. return 0;
  4406. }
  4407. static int ca0132_alt_eq_preset_get(struct snd_kcontrol *kcontrol,
  4408. struct snd_ctl_elem_value *ucontrol)
  4409. {
  4410. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  4411. struct ca0132_spec *spec = codec->spec;
  4412. ucontrol->value.enumerated.item[0] = spec->eq_preset_val;
  4413. return 0;
  4414. }
  4415. static int ca0132_alt_eq_preset_put(struct snd_kcontrol *kcontrol,
  4416. struct snd_ctl_elem_value *ucontrol)
  4417. {
  4418. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  4419. struct ca0132_spec *spec = codec->spec;
  4420. int i, err = 0;
  4421. int sel = ucontrol->value.enumerated.item[0];
  4422. unsigned int items = ARRAY_SIZE(ca0132_alt_eq_presets);
  4423. if (sel >= items)
  4424. return 0;
  4425. codec_dbg(codec, "%s: sel=%d, preset=%s\n", __func__, sel,
  4426. ca0132_alt_eq_presets[sel].name);
  4427. /*
  4428. * Idx 0 is default.
  4429. * Default needs to qualify with CrystalVoice state.
  4430. */
  4431. for (i = 0; i < EQ_PRESET_MAX_PARAM_COUNT; i++) {
  4432. err = dspio_set_uint_param(codec, ca0132_alt_eq_enum.mid,
  4433. ca0132_alt_eq_enum.reqs[i],
  4434. ca0132_alt_eq_presets[sel].vals[i]);
  4435. if (err < 0)
  4436. break;
  4437. }
  4438. if (err >= 0)
  4439. spec->eq_preset_val = sel;
  4440. return 1;
  4441. }
  4442. static int ca0132_voicefx_info(struct snd_kcontrol *kcontrol,
  4443. struct snd_ctl_elem_info *uinfo)
  4444. {
  4445. unsigned int items = ARRAY_SIZE(ca0132_voicefx_presets);
  4446. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  4447. uinfo->count = 1;
  4448. uinfo->value.enumerated.items = items;
  4449. if (uinfo->value.enumerated.item >= items)
  4450. uinfo->value.enumerated.item = items - 1;
  4451. strcpy(uinfo->value.enumerated.name,
  4452. ca0132_voicefx_presets[uinfo->value.enumerated.item].name);
  4453. return 0;
  4454. }
  4455. static int ca0132_voicefx_get(struct snd_kcontrol *kcontrol,
  4456. struct snd_ctl_elem_value *ucontrol)
  4457. {
  4458. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  4459. struct ca0132_spec *spec = codec->spec;
  4460. ucontrol->value.enumerated.item[0] = spec->voicefx_val;
  4461. return 0;
  4462. }
  4463. static int ca0132_voicefx_put(struct snd_kcontrol *kcontrol,
  4464. struct snd_ctl_elem_value *ucontrol)
  4465. {
  4466. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  4467. struct ca0132_spec *spec = codec->spec;
  4468. int i, err = 0;
  4469. int sel = ucontrol->value.enumerated.item[0];
  4470. if (sel >= ARRAY_SIZE(ca0132_voicefx_presets))
  4471. return 0;
  4472. codec_dbg(codec, "ca0132_voicefx_put: sel=%d, preset=%s\n",
  4473. sel, ca0132_voicefx_presets[sel].name);
  4474. /*
  4475. * Idx 0 is default.
  4476. * Default needs to qualify with CrystalVoice state.
  4477. */
  4478. for (i = 0; i < VOICEFX_MAX_PARAM_COUNT; i++) {
  4479. err = dspio_set_uint_param(codec, ca0132_voicefx.mid,
  4480. ca0132_voicefx.reqs[i],
  4481. ca0132_voicefx_presets[sel].vals[i]);
  4482. if (err < 0)
  4483. break;
  4484. }
  4485. if (err >= 0) {
  4486. spec->voicefx_val = sel;
  4487. /* enable voice fx */
  4488. ca0132_voicefx_set(codec, (sel ? 1 : 0));
  4489. }
  4490. return 1;
  4491. }
  4492. static int ca0132_switch_get(struct snd_kcontrol *kcontrol,
  4493. struct snd_ctl_elem_value *ucontrol)
  4494. {
  4495. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  4496. struct ca0132_spec *spec = codec->spec;
  4497. hda_nid_t nid = get_amp_nid(kcontrol);
  4498. int ch = get_amp_channels(kcontrol);
  4499. long *valp = ucontrol->value.integer.value;
  4500. /* vnode */
  4501. if ((nid >= VNODE_START_NID) && (nid < VNODE_END_NID)) {
  4502. if (ch & 1) {
  4503. *valp = spec->vnode_lswitch[nid - VNODE_START_NID];
  4504. valp++;
  4505. }
  4506. if (ch & 2) {
  4507. *valp = spec->vnode_rswitch[nid - VNODE_START_NID];
  4508. valp++;
  4509. }
  4510. return 0;
  4511. }
  4512. /* effects, include PE and CrystalVoice */
  4513. if ((nid >= EFFECT_START_NID) && (nid < EFFECT_END_NID)) {
  4514. *valp = spec->effects_switch[nid - EFFECT_START_NID];
  4515. return 0;
  4516. }
  4517. /* mic boost */
  4518. if (nid == spec->input_pins[0]) {
  4519. *valp = spec->cur_mic_boost;
  4520. return 0;
  4521. }
  4522. return 0;
  4523. }
  4524. static int ca0132_switch_put(struct snd_kcontrol *kcontrol,
  4525. struct snd_ctl_elem_value *ucontrol)
  4526. {
  4527. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  4528. struct ca0132_spec *spec = codec->spec;
  4529. hda_nid_t nid = get_amp_nid(kcontrol);
  4530. int ch = get_amp_channels(kcontrol);
  4531. long *valp = ucontrol->value.integer.value;
  4532. int changed = 1;
  4533. codec_dbg(codec, "ca0132_switch_put: nid=0x%x, val=%ld\n",
  4534. nid, *valp);
  4535. snd_hda_power_up(codec);
  4536. /* vnode */
  4537. if ((nid >= VNODE_START_NID) && (nid < VNODE_END_NID)) {
  4538. if (ch & 1) {
  4539. spec->vnode_lswitch[nid - VNODE_START_NID] = *valp;
  4540. valp++;
  4541. }
  4542. if (ch & 2) {
  4543. spec->vnode_rswitch[nid - VNODE_START_NID] = *valp;
  4544. valp++;
  4545. }
  4546. changed = ca0132_vnode_switch_set(kcontrol, ucontrol);
  4547. goto exit;
  4548. }
  4549. /* PE */
  4550. if (nid == PLAY_ENHANCEMENT) {
  4551. spec->effects_switch[nid - EFFECT_START_NID] = *valp;
  4552. changed = ca0132_pe_switch_set(codec);
  4553. goto exit;
  4554. }
  4555. /* CrystalVoice */
  4556. if (nid == CRYSTAL_VOICE) {
  4557. spec->effects_switch[nid - EFFECT_START_NID] = *valp;
  4558. changed = ca0132_cvoice_switch_set(codec);
  4559. goto exit;
  4560. }
  4561. /* out and in effects */
  4562. if (((nid >= OUT_EFFECT_START_NID) && (nid < OUT_EFFECT_END_NID)) ||
  4563. ((nid >= IN_EFFECT_START_NID) && (nid < IN_EFFECT_END_NID))) {
  4564. spec->effects_switch[nid - EFFECT_START_NID] = *valp;
  4565. changed = ca0132_effects_set(codec, nid, *valp);
  4566. goto exit;
  4567. }
  4568. /* mic boost */
  4569. if (nid == spec->input_pins[0]) {
  4570. spec->cur_mic_boost = *valp;
  4571. if (spec->use_alt_functions) {
  4572. if (spec->in_enum_val != REAR_LINE_IN)
  4573. changed = ca0132_mic_boost_set(codec, *valp);
  4574. } else {
  4575. /* Mic boost does not apply to Digital Mic */
  4576. if (spec->cur_mic_type != DIGITAL_MIC)
  4577. changed = ca0132_mic_boost_set(codec, *valp);
  4578. }
  4579. goto exit;
  4580. }
  4581. exit:
  4582. snd_hda_power_down(codec);
  4583. return changed;
  4584. }
  4585. /*
  4586. * Volume related
  4587. */
  4588. /*
  4589. * Sets the internal DSP decibel level to match the DAC for output, and the
  4590. * ADC for input. Currently only the SBZ sets dsp capture volume level, and
  4591. * all alternative codecs set DSP playback volume.
  4592. */
  4593. static void ca0132_alt_dsp_volume_put(struct hda_codec *codec, hda_nid_t nid)
  4594. {
  4595. struct ca0132_spec *spec = codec->spec;
  4596. unsigned int dsp_dir;
  4597. unsigned int lookup_val;
  4598. if (nid == VNID_SPK)
  4599. dsp_dir = DSP_VOL_OUT;
  4600. else
  4601. dsp_dir = DSP_VOL_IN;
  4602. lookup_val = spec->vnode_lvol[nid - VNODE_START_NID];
  4603. dspio_set_uint_param(codec,
  4604. ca0132_alt_vol_ctls[dsp_dir].mid,
  4605. ca0132_alt_vol_ctls[dsp_dir].reqs[0],
  4606. float_vol_db_lookup[lookup_val]);
  4607. lookup_val = spec->vnode_rvol[nid - VNODE_START_NID];
  4608. dspio_set_uint_param(codec,
  4609. ca0132_alt_vol_ctls[dsp_dir].mid,
  4610. ca0132_alt_vol_ctls[dsp_dir].reqs[1],
  4611. float_vol_db_lookup[lookup_val]);
  4612. dspio_set_uint_param(codec,
  4613. ca0132_alt_vol_ctls[dsp_dir].mid,
  4614. ca0132_alt_vol_ctls[dsp_dir].reqs[2], FLOAT_ZERO);
  4615. }
  4616. static int ca0132_volume_info(struct snd_kcontrol *kcontrol,
  4617. struct snd_ctl_elem_info *uinfo)
  4618. {
  4619. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  4620. struct ca0132_spec *spec = codec->spec;
  4621. hda_nid_t nid = get_amp_nid(kcontrol);
  4622. int ch = get_amp_channels(kcontrol);
  4623. int dir = get_amp_direction(kcontrol);
  4624. unsigned long pval;
  4625. int err;
  4626. switch (nid) {
  4627. case VNID_SPK:
  4628. /* follow shared_out info */
  4629. nid = spec->shared_out_nid;
  4630. mutex_lock(&codec->control_mutex);
  4631. pval = kcontrol->private_value;
  4632. kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
  4633. err = snd_hda_mixer_amp_volume_info(kcontrol, uinfo);
  4634. kcontrol->private_value = pval;
  4635. mutex_unlock(&codec->control_mutex);
  4636. break;
  4637. case VNID_MIC:
  4638. /* follow shared_mic info */
  4639. nid = spec->shared_mic_nid;
  4640. mutex_lock(&codec->control_mutex);
  4641. pval = kcontrol->private_value;
  4642. kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
  4643. err = snd_hda_mixer_amp_volume_info(kcontrol, uinfo);
  4644. kcontrol->private_value = pval;
  4645. mutex_unlock(&codec->control_mutex);
  4646. break;
  4647. default:
  4648. err = snd_hda_mixer_amp_volume_info(kcontrol, uinfo);
  4649. }
  4650. return err;
  4651. }
  4652. static int ca0132_volume_get(struct snd_kcontrol *kcontrol,
  4653. struct snd_ctl_elem_value *ucontrol)
  4654. {
  4655. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  4656. struct ca0132_spec *spec = codec->spec;
  4657. hda_nid_t nid = get_amp_nid(kcontrol);
  4658. int ch = get_amp_channels(kcontrol);
  4659. long *valp = ucontrol->value.integer.value;
  4660. /* store the left and right volume */
  4661. if (ch & 1) {
  4662. *valp = spec->vnode_lvol[nid - VNODE_START_NID];
  4663. valp++;
  4664. }
  4665. if (ch & 2) {
  4666. *valp = spec->vnode_rvol[nid - VNODE_START_NID];
  4667. valp++;
  4668. }
  4669. return 0;
  4670. }
  4671. static int ca0132_volume_put(struct snd_kcontrol *kcontrol,
  4672. struct snd_ctl_elem_value *ucontrol)
  4673. {
  4674. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  4675. struct ca0132_spec *spec = codec->spec;
  4676. hda_nid_t nid = get_amp_nid(kcontrol);
  4677. int ch = get_amp_channels(kcontrol);
  4678. long *valp = ucontrol->value.integer.value;
  4679. hda_nid_t shared_nid = 0;
  4680. bool effective;
  4681. int changed = 1;
  4682. /* store the left and right volume */
  4683. if (ch & 1) {
  4684. spec->vnode_lvol[nid - VNODE_START_NID] = *valp;
  4685. valp++;
  4686. }
  4687. if (ch & 2) {
  4688. spec->vnode_rvol[nid - VNODE_START_NID] = *valp;
  4689. valp++;
  4690. }
  4691. /* if effective conditions, then update hw immediately. */
  4692. effective = ca0132_is_vnode_effective(codec, nid, &shared_nid);
  4693. if (effective) {
  4694. int dir = get_amp_direction(kcontrol);
  4695. unsigned long pval;
  4696. snd_hda_power_up(codec);
  4697. mutex_lock(&codec->control_mutex);
  4698. pval = kcontrol->private_value;
  4699. kcontrol->private_value = HDA_COMPOSE_AMP_VAL(shared_nid, ch,
  4700. 0, dir);
  4701. changed = snd_hda_mixer_amp_volume_put(kcontrol, ucontrol);
  4702. kcontrol->private_value = pval;
  4703. mutex_unlock(&codec->control_mutex);
  4704. snd_hda_power_down(codec);
  4705. }
  4706. return changed;
  4707. }
  4708. /*
  4709. * This function is the same as the one above, because using an if statement
  4710. * inside of the above volume control for the DSP volume would cause too much
  4711. * lag. This is a lot more smooth.
  4712. */
  4713. static int ca0132_alt_volume_put(struct snd_kcontrol *kcontrol,
  4714. struct snd_ctl_elem_value *ucontrol)
  4715. {
  4716. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  4717. struct ca0132_spec *spec = codec->spec;
  4718. hda_nid_t nid = get_amp_nid(kcontrol);
  4719. int ch = get_amp_channels(kcontrol);
  4720. long *valp = ucontrol->value.integer.value;
  4721. hda_nid_t vnid = 0;
  4722. int changed = 1;
  4723. switch (nid) {
  4724. case 0x02:
  4725. vnid = VNID_SPK;
  4726. break;
  4727. case 0x07:
  4728. vnid = VNID_MIC;
  4729. break;
  4730. }
  4731. /* store the left and right volume */
  4732. if (ch & 1) {
  4733. spec->vnode_lvol[vnid - VNODE_START_NID] = *valp;
  4734. valp++;
  4735. }
  4736. if (ch & 2) {
  4737. spec->vnode_rvol[vnid - VNODE_START_NID] = *valp;
  4738. valp++;
  4739. }
  4740. snd_hda_power_up(codec);
  4741. ca0132_alt_dsp_volume_put(codec, vnid);
  4742. mutex_lock(&codec->control_mutex);
  4743. changed = snd_hda_mixer_amp_volume_put(kcontrol, ucontrol);
  4744. mutex_unlock(&codec->control_mutex);
  4745. snd_hda_power_down(codec);
  4746. return changed;
  4747. }
  4748. static int ca0132_volume_tlv(struct snd_kcontrol *kcontrol, int op_flag,
  4749. unsigned int size, unsigned int __user *tlv)
  4750. {
  4751. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  4752. struct ca0132_spec *spec = codec->spec;
  4753. hda_nid_t nid = get_amp_nid(kcontrol);
  4754. int ch = get_amp_channels(kcontrol);
  4755. int dir = get_amp_direction(kcontrol);
  4756. unsigned long pval;
  4757. int err;
  4758. switch (nid) {
  4759. case VNID_SPK:
  4760. /* follow shared_out tlv */
  4761. nid = spec->shared_out_nid;
  4762. mutex_lock(&codec->control_mutex);
  4763. pval = kcontrol->private_value;
  4764. kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
  4765. err = snd_hda_mixer_amp_tlv(kcontrol, op_flag, size, tlv);
  4766. kcontrol->private_value = pval;
  4767. mutex_unlock(&codec->control_mutex);
  4768. break;
  4769. case VNID_MIC:
  4770. /* follow shared_mic tlv */
  4771. nid = spec->shared_mic_nid;
  4772. mutex_lock(&codec->control_mutex);
  4773. pval = kcontrol->private_value;
  4774. kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
  4775. err = snd_hda_mixer_amp_tlv(kcontrol, op_flag, size, tlv);
  4776. kcontrol->private_value = pval;
  4777. mutex_unlock(&codec->control_mutex);
  4778. break;
  4779. default:
  4780. err = snd_hda_mixer_amp_tlv(kcontrol, op_flag, size, tlv);
  4781. }
  4782. return err;
  4783. }
  4784. /* Add volume slider control for effect level */
  4785. static int ca0132_alt_add_effect_slider(struct hda_codec *codec, hda_nid_t nid,
  4786. const char *pfx, int dir)
  4787. {
  4788. char namestr[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
  4789. int type = dir ? HDA_INPUT : HDA_OUTPUT;
  4790. struct snd_kcontrol_new knew =
  4791. HDA_CODEC_VOLUME_MONO(namestr, nid, 1, 0, type);
  4792. sprintf(namestr, "FX: %s %s Volume", pfx, dirstr[dir]);
  4793. knew.tlv.c = NULL;
  4794. switch (nid) {
  4795. case XBASS_XOVER:
  4796. knew.info = ca0132_alt_xbass_xover_slider_info;
  4797. knew.get = ca0132_alt_xbass_xover_slider_ctl_get;
  4798. knew.put = ca0132_alt_xbass_xover_slider_put;
  4799. break;
  4800. default:
  4801. knew.info = ca0132_alt_effect_slider_info;
  4802. knew.get = ca0132_alt_slider_ctl_get;
  4803. knew.put = ca0132_alt_effect_slider_put;
  4804. knew.private_value =
  4805. HDA_COMPOSE_AMP_VAL(nid, 1, 0, type);
  4806. break;
  4807. }
  4808. return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
  4809. }
  4810. /*
  4811. * Added FX: prefix for the alternative codecs, because otherwise the surround
  4812. * effect would conflict with the Surround sound volume control. Also seems more
  4813. * clear as to what the switches do. Left alone for others.
  4814. */
  4815. static int add_fx_switch(struct hda_codec *codec, hda_nid_t nid,
  4816. const char *pfx, int dir)
  4817. {
  4818. struct ca0132_spec *spec = codec->spec;
  4819. char namestr[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
  4820. int type = dir ? HDA_INPUT : HDA_OUTPUT;
  4821. struct snd_kcontrol_new knew =
  4822. CA0132_CODEC_MUTE_MONO(namestr, nid, 1, type);
  4823. /* If using alt_controls, add FX: prefix. But, don't add FX:
  4824. * prefix to OutFX or InFX enable controls.
  4825. */
  4826. if ((spec->use_alt_controls) && (nid <= IN_EFFECT_END_NID))
  4827. sprintf(namestr, "FX: %s %s Switch", pfx, dirstr[dir]);
  4828. else
  4829. sprintf(namestr, "%s %s Switch", pfx, dirstr[dir]);
  4830. return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
  4831. }
  4832. static int add_voicefx(struct hda_codec *codec)
  4833. {
  4834. struct snd_kcontrol_new knew =
  4835. HDA_CODEC_MUTE_MONO(ca0132_voicefx.name,
  4836. VOICEFX, 1, 0, HDA_INPUT);
  4837. knew.info = ca0132_voicefx_info;
  4838. knew.get = ca0132_voicefx_get;
  4839. knew.put = ca0132_voicefx_put;
  4840. return snd_hda_ctl_add(codec, VOICEFX, snd_ctl_new1(&knew, codec));
  4841. }
  4842. /* Create the EQ Preset control */
  4843. static int add_ca0132_alt_eq_presets(struct hda_codec *codec)
  4844. {
  4845. struct snd_kcontrol_new knew =
  4846. HDA_CODEC_MUTE_MONO(ca0132_alt_eq_enum.name,
  4847. EQ_PRESET_ENUM, 1, 0, HDA_OUTPUT);
  4848. knew.info = ca0132_alt_eq_preset_info;
  4849. knew.get = ca0132_alt_eq_preset_get;
  4850. knew.put = ca0132_alt_eq_preset_put;
  4851. return snd_hda_ctl_add(codec, EQ_PRESET_ENUM,
  4852. snd_ctl_new1(&knew, codec));
  4853. }
  4854. /*
  4855. * Add enumerated control for the three different settings of the smart volume
  4856. * output effect. Normal just uses the slider value, and loud and night are
  4857. * their own things that ignore that value.
  4858. */
  4859. static int ca0132_alt_add_svm_enum(struct hda_codec *codec)
  4860. {
  4861. struct snd_kcontrol_new knew =
  4862. HDA_CODEC_MUTE_MONO("FX: Smart Volume Setting",
  4863. SMART_VOLUME_ENUM, 1, 0, HDA_OUTPUT);
  4864. knew.info = ca0132_alt_svm_setting_info;
  4865. knew.get = ca0132_alt_svm_setting_get;
  4866. knew.put = ca0132_alt_svm_setting_put;
  4867. return snd_hda_ctl_add(codec, SMART_VOLUME_ENUM,
  4868. snd_ctl_new1(&knew, codec));
  4869. }
  4870. /*
  4871. * Create an Output Select enumerated control for codecs with surround
  4872. * out capabilities.
  4873. */
  4874. static int ca0132_alt_add_output_enum(struct hda_codec *codec)
  4875. {
  4876. struct snd_kcontrol_new knew =
  4877. HDA_CODEC_MUTE_MONO("Output Select",
  4878. OUTPUT_SOURCE_ENUM, 1, 0, HDA_OUTPUT);
  4879. knew.info = ca0132_alt_output_select_get_info;
  4880. knew.get = ca0132_alt_output_select_get;
  4881. knew.put = ca0132_alt_output_select_put;
  4882. return snd_hda_ctl_add(codec, OUTPUT_SOURCE_ENUM,
  4883. snd_ctl_new1(&knew, codec));
  4884. }
  4885. /*
  4886. * Create an Input Source enumerated control for the alternate ca0132 codecs
  4887. * because the front microphone has no auto-detect, and Line-in has to be set
  4888. * somehow.
  4889. */
  4890. static int ca0132_alt_add_input_enum(struct hda_codec *codec)
  4891. {
  4892. struct snd_kcontrol_new knew =
  4893. HDA_CODEC_MUTE_MONO("Input Source",
  4894. INPUT_SOURCE_ENUM, 1, 0, HDA_INPUT);
  4895. knew.info = ca0132_alt_input_source_info;
  4896. knew.get = ca0132_alt_input_source_get;
  4897. knew.put = ca0132_alt_input_source_put;
  4898. return snd_hda_ctl_add(codec, INPUT_SOURCE_ENUM,
  4899. snd_ctl_new1(&knew, codec));
  4900. }
  4901. /*
  4902. * Add mic boost enumerated control. Switches through 0dB to 30dB. This adds
  4903. * more control than the original mic boost, which is either full 30dB or off.
  4904. */
  4905. static int ca0132_alt_add_mic_boost_enum(struct hda_codec *codec)
  4906. {
  4907. struct snd_kcontrol_new knew =
  4908. HDA_CODEC_MUTE_MONO("Mic Boost Capture Switch",
  4909. MIC_BOOST_ENUM, 1, 0, HDA_INPUT);
  4910. knew.info = ca0132_alt_mic_boost_info;
  4911. knew.get = ca0132_alt_mic_boost_get;
  4912. knew.put = ca0132_alt_mic_boost_put;
  4913. return snd_hda_ctl_add(codec, MIC_BOOST_ENUM,
  4914. snd_ctl_new1(&knew, codec));
  4915. }
  4916. /*
  4917. * Need to create slave controls for the alternate codecs that have surround
  4918. * capabilities.
  4919. */
  4920. static const char * const ca0132_alt_slave_pfxs[] = {
  4921. "Front", "Surround", "Center", "LFE", NULL,
  4922. };
  4923. /*
  4924. * Also need special channel map, because the default one is incorrect.
  4925. * I think this has to do with the pin for rear surround being 0x11,
  4926. * and the center/lfe being 0x10. Usually the pin order is the opposite.
  4927. */
  4928. static const struct snd_pcm_chmap_elem ca0132_alt_chmaps[] = {
  4929. { .channels = 2,
  4930. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
  4931. { .channels = 4,
  4932. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
  4933. SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
  4934. { .channels = 6,
  4935. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
  4936. SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
  4937. SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
  4938. { }
  4939. };
  4940. /* Add the correct chmap for streams with 6 channels. */
  4941. static void ca0132_alt_add_chmap_ctls(struct hda_codec *codec)
  4942. {
  4943. int err = 0;
  4944. struct hda_pcm *pcm;
  4945. list_for_each_entry(pcm, &codec->pcm_list_head, list) {
  4946. struct hda_pcm_stream *hinfo =
  4947. &pcm->stream[SNDRV_PCM_STREAM_PLAYBACK];
  4948. struct snd_pcm_chmap *chmap;
  4949. const struct snd_pcm_chmap_elem *elem;
  4950. elem = ca0132_alt_chmaps;
  4951. if (hinfo->channels_max == 6) {
  4952. err = snd_pcm_add_chmap_ctls(pcm->pcm,
  4953. SNDRV_PCM_STREAM_PLAYBACK,
  4954. elem, hinfo->channels_max, 0, &chmap);
  4955. if (err < 0)
  4956. codec_dbg(codec, "snd_pcm_add_chmap_ctls failed!");
  4957. }
  4958. }
  4959. }
  4960. /*
  4961. * When changing Node IDs for Mixer Controls below, make sure to update
  4962. * Node IDs in ca0132_config() as well.
  4963. */
  4964. static const struct snd_kcontrol_new ca0132_mixer[] = {
  4965. CA0132_CODEC_VOL("Master Playback Volume", VNID_SPK, HDA_OUTPUT),
  4966. CA0132_CODEC_MUTE("Master Playback Switch", VNID_SPK, HDA_OUTPUT),
  4967. CA0132_CODEC_VOL("Capture Volume", VNID_MIC, HDA_INPUT),
  4968. CA0132_CODEC_MUTE("Capture Switch", VNID_MIC, HDA_INPUT),
  4969. HDA_CODEC_VOLUME("Analog-Mic2 Capture Volume", 0x08, 0, HDA_INPUT),
  4970. HDA_CODEC_MUTE("Analog-Mic2 Capture Switch", 0x08, 0, HDA_INPUT),
  4971. HDA_CODEC_VOLUME("What U Hear Capture Volume", 0x0a, 0, HDA_INPUT),
  4972. HDA_CODEC_MUTE("What U Hear Capture Switch", 0x0a, 0, HDA_INPUT),
  4973. CA0132_CODEC_MUTE_MONO("Mic1-Boost (30dB) Capture Switch",
  4974. 0x12, 1, HDA_INPUT),
  4975. CA0132_CODEC_MUTE_MONO("HP/Speaker Playback Switch",
  4976. VNID_HP_SEL, 1, HDA_OUTPUT),
  4977. CA0132_CODEC_MUTE_MONO("AMic1/DMic Capture Switch",
  4978. VNID_AMIC1_SEL, 1, HDA_INPUT),
  4979. CA0132_CODEC_MUTE_MONO("HP/Speaker Auto Detect Playback Switch",
  4980. VNID_HP_ASEL, 1, HDA_OUTPUT),
  4981. CA0132_CODEC_MUTE_MONO("AMic1/DMic Auto Detect Capture Switch",
  4982. VNID_AMIC1_ASEL, 1, HDA_INPUT),
  4983. { } /* end */
  4984. };
  4985. /*
  4986. * Desktop specific control mixer. Removes auto-detect for mic, and adds
  4987. * surround controls. Also sets both the Front Playback and Capture Volume
  4988. * controls to alt so they set the DSP's decibel level.
  4989. */
  4990. static const struct snd_kcontrol_new desktop_mixer[] = {
  4991. CA0132_ALT_CODEC_VOL("Front Playback Volume", 0x02, HDA_OUTPUT),
  4992. CA0132_CODEC_MUTE("Front Playback Switch", VNID_SPK, HDA_OUTPUT),
  4993. HDA_CODEC_VOLUME("Surround Playback Volume", 0x04, 0, HDA_OUTPUT),
  4994. HDA_CODEC_MUTE("Surround Playback Switch", 0x04, 0, HDA_OUTPUT),
  4995. HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x03, 1, 0, HDA_OUTPUT),
  4996. HDA_CODEC_MUTE_MONO("Center Playback Switch", 0x03, 1, 0, HDA_OUTPUT),
  4997. HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x03, 2, 0, HDA_OUTPUT),
  4998. HDA_CODEC_MUTE_MONO("LFE Playback Switch", 0x03, 2, 0, HDA_OUTPUT),
  4999. CA0132_ALT_CODEC_VOL("Capture Volume", 0x07, HDA_INPUT),
  5000. CA0132_CODEC_MUTE("Capture Switch", VNID_MIC, HDA_INPUT),
  5001. HDA_CODEC_VOLUME("What U Hear Capture Volume", 0x0a, 0, HDA_INPUT),
  5002. HDA_CODEC_MUTE("What U Hear Capture Switch", 0x0a, 0, HDA_INPUT),
  5003. CA0132_CODEC_MUTE_MONO("HP/Speaker Auto Detect Playback Switch",
  5004. VNID_HP_ASEL, 1, HDA_OUTPUT),
  5005. { } /* end */
  5006. };
  5007. /*
  5008. * Same as the Sound Blaster Z, except doesn't use the alt volume for capture
  5009. * because it doesn't set decibel levels for the DSP for capture.
  5010. */
  5011. static const struct snd_kcontrol_new r3di_mixer[] = {
  5012. CA0132_ALT_CODEC_VOL("Front Playback Volume", 0x02, HDA_OUTPUT),
  5013. CA0132_CODEC_MUTE("Front Playback Switch", VNID_SPK, HDA_OUTPUT),
  5014. HDA_CODEC_VOLUME("Surround Playback Volume", 0x04, 0, HDA_OUTPUT),
  5015. HDA_CODEC_MUTE("Surround Playback Switch", 0x04, 0, HDA_OUTPUT),
  5016. HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x03, 1, 0, HDA_OUTPUT),
  5017. HDA_CODEC_MUTE_MONO("Center Playback Switch", 0x03, 1, 0, HDA_OUTPUT),
  5018. HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x03, 2, 0, HDA_OUTPUT),
  5019. HDA_CODEC_MUTE_MONO("LFE Playback Switch", 0x03, 2, 0, HDA_OUTPUT),
  5020. CA0132_CODEC_VOL("Capture Volume", VNID_MIC, HDA_INPUT),
  5021. CA0132_CODEC_MUTE("Capture Switch", VNID_MIC, HDA_INPUT),
  5022. HDA_CODEC_VOLUME("What U Hear Capture Volume", 0x0a, 0, HDA_INPUT),
  5023. HDA_CODEC_MUTE("What U Hear Capture Switch", 0x0a, 0, HDA_INPUT),
  5024. CA0132_CODEC_MUTE_MONO("HP/Speaker Auto Detect Playback Switch",
  5025. VNID_HP_ASEL, 1, HDA_OUTPUT),
  5026. { } /* end */
  5027. };
  5028. static int ca0132_build_controls(struct hda_codec *codec)
  5029. {
  5030. struct ca0132_spec *spec = codec->spec;
  5031. int i, num_fx, num_sliders;
  5032. int err = 0;
  5033. /* Add Mixer controls */
  5034. for (i = 0; i < spec->num_mixers; i++) {
  5035. err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
  5036. if (err < 0)
  5037. return err;
  5038. }
  5039. /* Setup vmaster with surround slaves for desktop ca0132 devices */
  5040. if (spec->use_alt_functions) {
  5041. snd_hda_set_vmaster_tlv(codec, spec->dacs[0], HDA_OUTPUT,
  5042. spec->tlv);
  5043. snd_hda_add_vmaster(codec, "Master Playback Volume",
  5044. spec->tlv, ca0132_alt_slave_pfxs,
  5045. "Playback Volume");
  5046. err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
  5047. NULL, ca0132_alt_slave_pfxs,
  5048. "Playback Switch",
  5049. true, &spec->vmaster_mute.sw_kctl);
  5050. }
  5051. /* Add in and out effects controls.
  5052. * VoiceFX, PE and CrystalVoice are added separately.
  5053. */
  5054. num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT;
  5055. for (i = 0; i < num_fx; i++) {
  5056. /* Desktop cards break if Echo Cancellation is used. */
  5057. if (spec->use_pci_mmio) {
  5058. if (i == (ECHO_CANCELLATION - IN_EFFECT_START_NID +
  5059. OUT_EFFECTS_COUNT))
  5060. continue;
  5061. }
  5062. err = add_fx_switch(codec, ca0132_effects[i].nid,
  5063. ca0132_effects[i].name,
  5064. ca0132_effects[i].direct);
  5065. if (err < 0)
  5066. return err;
  5067. }
  5068. /*
  5069. * If codec has use_alt_controls set to true, add effect level sliders,
  5070. * EQ presets, and Smart Volume presets. Also, change names to add FX
  5071. * prefix, and change PlayEnhancement and CrystalVoice to match.
  5072. */
  5073. if (spec->use_alt_controls) {
  5074. ca0132_alt_add_svm_enum(codec);
  5075. add_ca0132_alt_eq_presets(codec);
  5076. err = add_fx_switch(codec, PLAY_ENHANCEMENT,
  5077. "Enable OutFX", 0);
  5078. if (err < 0)
  5079. return err;
  5080. err = add_fx_switch(codec, CRYSTAL_VOICE,
  5081. "Enable InFX", 1);
  5082. if (err < 0)
  5083. return err;
  5084. num_sliders = OUT_EFFECTS_COUNT - 1;
  5085. for (i = 0; i < num_sliders; i++) {
  5086. err = ca0132_alt_add_effect_slider(codec,
  5087. ca0132_effects[i].nid,
  5088. ca0132_effects[i].name,
  5089. ca0132_effects[i].direct);
  5090. if (err < 0)
  5091. return err;
  5092. }
  5093. err = ca0132_alt_add_effect_slider(codec, XBASS_XOVER,
  5094. "X-Bass Crossover", EFX_DIR_OUT);
  5095. if (err < 0)
  5096. return err;
  5097. } else {
  5098. err = add_fx_switch(codec, PLAY_ENHANCEMENT,
  5099. "PlayEnhancement", 0);
  5100. if (err < 0)
  5101. return err;
  5102. err = add_fx_switch(codec, CRYSTAL_VOICE,
  5103. "CrystalVoice", 1);
  5104. if (err < 0)
  5105. return err;
  5106. }
  5107. add_voicefx(codec);
  5108. /*
  5109. * If the codec uses alt_functions, you need the enumerated controls
  5110. * to select the new outputs and inputs, plus add the new mic boost
  5111. * setting control.
  5112. */
  5113. if (spec->use_alt_functions) {
  5114. ca0132_alt_add_output_enum(codec);
  5115. ca0132_alt_add_input_enum(codec);
  5116. ca0132_alt_add_mic_boost_enum(codec);
  5117. }
  5118. #ifdef ENABLE_TUNING_CONTROLS
  5119. add_tuning_ctls(codec);
  5120. #endif
  5121. err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
  5122. if (err < 0)
  5123. return err;
  5124. if (spec->dig_out) {
  5125. err = snd_hda_create_spdif_out_ctls(codec, spec->dig_out,
  5126. spec->dig_out);
  5127. if (err < 0)
  5128. return err;
  5129. err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
  5130. if (err < 0)
  5131. return err;
  5132. /* spec->multiout.share_spdif = 1; */
  5133. }
  5134. if (spec->dig_in) {
  5135. err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
  5136. if (err < 0)
  5137. return err;
  5138. }
  5139. if (spec->use_alt_functions)
  5140. ca0132_alt_add_chmap_ctls(codec);
  5141. return 0;
  5142. }
  5143. /*
  5144. * PCM
  5145. */
  5146. static const struct hda_pcm_stream ca0132_pcm_analog_playback = {
  5147. .substreams = 1,
  5148. .channels_min = 2,
  5149. .channels_max = 6,
  5150. .ops = {
  5151. .prepare = ca0132_playback_pcm_prepare,
  5152. .cleanup = ca0132_playback_pcm_cleanup,
  5153. .get_delay = ca0132_playback_pcm_delay,
  5154. },
  5155. };
  5156. static const struct hda_pcm_stream ca0132_pcm_analog_capture = {
  5157. .substreams = 1,
  5158. .channels_min = 2,
  5159. .channels_max = 2,
  5160. .ops = {
  5161. .prepare = ca0132_capture_pcm_prepare,
  5162. .cleanup = ca0132_capture_pcm_cleanup,
  5163. .get_delay = ca0132_capture_pcm_delay,
  5164. },
  5165. };
  5166. static const struct hda_pcm_stream ca0132_pcm_digital_playback = {
  5167. .substreams = 1,
  5168. .channels_min = 2,
  5169. .channels_max = 2,
  5170. .ops = {
  5171. .open = ca0132_dig_playback_pcm_open,
  5172. .close = ca0132_dig_playback_pcm_close,
  5173. .prepare = ca0132_dig_playback_pcm_prepare,
  5174. .cleanup = ca0132_dig_playback_pcm_cleanup
  5175. },
  5176. };
  5177. static const struct hda_pcm_stream ca0132_pcm_digital_capture = {
  5178. .substreams = 1,
  5179. .channels_min = 2,
  5180. .channels_max = 2,
  5181. };
  5182. static int ca0132_build_pcms(struct hda_codec *codec)
  5183. {
  5184. struct ca0132_spec *spec = codec->spec;
  5185. struct hda_pcm *info;
  5186. info = snd_hda_codec_pcm_new(codec, "CA0132 Analog");
  5187. if (!info)
  5188. return -ENOMEM;
  5189. if (spec->use_alt_functions) {
  5190. info->own_chmap = true;
  5191. info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap
  5192. = ca0132_alt_chmaps;
  5193. }
  5194. info->stream[SNDRV_PCM_STREAM_PLAYBACK] = ca0132_pcm_analog_playback;
  5195. info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dacs[0];
  5196. info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
  5197. spec->multiout.max_channels;
  5198. info->stream[SNDRV_PCM_STREAM_CAPTURE] = ca0132_pcm_analog_capture;
  5199. info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = 1;
  5200. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[0];
  5201. /* With the DSP enabled, desktops don't use this ADC. */
  5202. if (!spec->use_alt_functions) {
  5203. info = snd_hda_codec_pcm_new(codec, "CA0132 Analog Mic-In2");
  5204. if (!info)
  5205. return -ENOMEM;
  5206. info->stream[SNDRV_PCM_STREAM_CAPTURE] =
  5207. ca0132_pcm_analog_capture;
  5208. info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = 1;
  5209. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[1];
  5210. }
  5211. info = snd_hda_codec_pcm_new(codec, "CA0132 What U Hear");
  5212. if (!info)
  5213. return -ENOMEM;
  5214. info->stream[SNDRV_PCM_STREAM_CAPTURE] = ca0132_pcm_analog_capture;
  5215. info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = 1;
  5216. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[2];
  5217. if (!spec->dig_out && !spec->dig_in)
  5218. return 0;
  5219. info = snd_hda_codec_pcm_new(codec, "CA0132 Digital");
  5220. if (!info)
  5221. return -ENOMEM;
  5222. info->pcm_type = HDA_PCM_TYPE_SPDIF;
  5223. if (spec->dig_out) {
  5224. info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
  5225. ca0132_pcm_digital_playback;
  5226. info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dig_out;
  5227. }
  5228. if (spec->dig_in) {
  5229. info->stream[SNDRV_PCM_STREAM_CAPTURE] =
  5230. ca0132_pcm_digital_capture;
  5231. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
  5232. }
  5233. return 0;
  5234. }
  5235. static void init_output(struct hda_codec *codec, hda_nid_t pin, hda_nid_t dac)
  5236. {
  5237. if (pin) {
  5238. snd_hda_set_pin_ctl(codec, pin, PIN_HP);
  5239. if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
  5240. snd_hda_codec_write(codec, pin, 0,
  5241. AC_VERB_SET_AMP_GAIN_MUTE,
  5242. AMP_OUT_UNMUTE);
  5243. }
  5244. if (dac && (get_wcaps(codec, dac) & AC_WCAP_OUT_AMP))
  5245. snd_hda_codec_write(codec, dac, 0,
  5246. AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO);
  5247. }
  5248. static void init_input(struct hda_codec *codec, hda_nid_t pin, hda_nid_t adc)
  5249. {
  5250. if (pin) {
  5251. snd_hda_set_pin_ctl(codec, pin, PIN_VREF80);
  5252. if (get_wcaps(codec, pin) & AC_WCAP_IN_AMP)
  5253. snd_hda_codec_write(codec, pin, 0,
  5254. AC_VERB_SET_AMP_GAIN_MUTE,
  5255. AMP_IN_UNMUTE(0));
  5256. }
  5257. if (adc && (get_wcaps(codec, adc) & AC_WCAP_IN_AMP)) {
  5258. snd_hda_codec_write(codec, adc, 0, AC_VERB_SET_AMP_GAIN_MUTE,
  5259. AMP_IN_UNMUTE(0));
  5260. /* init to 0 dB and unmute. */
  5261. snd_hda_codec_amp_stereo(codec, adc, HDA_INPUT, 0,
  5262. HDA_AMP_VOLMASK, 0x5a);
  5263. snd_hda_codec_amp_stereo(codec, adc, HDA_INPUT, 0,
  5264. HDA_AMP_MUTE, 0);
  5265. }
  5266. }
  5267. static void refresh_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir)
  5268. {
  5269. unsigned int caps;
  5270. caps = snd_hda_param_read(codec, nid, dir == HDA_OUTPUT ?
  5271. AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
  5272. snd_hda_override_amp_caps(codec, nid, dir, caps);
  5273. }
  5274. /*
  5275. * Switch between Digital built-in mic and analog mic.
  5276. */
  5277. static void ca0132_set_dmic(struct hda_codec *codec, int enable)
  5278. {
  5279. struct ca0132_spec *spec = codec->spec;
  5280. unsigned int tmp;
  5281. u8 val;
  5282. unsigned int oldval;
  5283. codec_dbg(codec, "ca0132_set_dmic: enable=%d\n", enable);
  5284. oldval = stop_mic1(codec);
  5285. ca0132_set_vipsource(codec, 0);
  5286. if (enable) {
  5287. /* set DMic input as 2-ch */
  5288. tmp = FLOAT_TWO;
  5289. dspio_set_uint_param(codec, 0x80, 0x00, tmp);
  5290. val = spec->dmic_ctl;
  5291. val |= 0x80;
  5292. snd_hda_codec_write(codec, spec->input_pins[0], 0,
  5293. VENDOR_CHIPIO_DMIC_CTL_SET, val);
  5294. if (!(spec->dmic_ctl & 0x20))
  5295. chipio_set_control_flag(codec, CONTROL_FLAG_DMIC, 1);
  5296. } else {
  5297. /* set AMic input as mono */
  5298. tmp = FLOAT_ONE;
  5299. dspio_set_uint_param(codec, 0x80, 0x00, tmp);
  5300. val = spec->dmic_ctl;
  5301. /* clear bit7 and bit5 to disable dmic */
  5302. val &= 0x5f;
  5303. snd_hda_codec_write(codec, spec->input_pins[0], 0,
  5304. VENDOR_CHIPIO_DMIC_CTL_SET, val);
  5305. if (!(spec->dmic_ctl & 0x20))
  5306. chipio_set_control_flag(codec, CONTROL_FLAG_DMIC, 0);
  5307. }
  5308. ca0132_set_vipsource(codec, 1);
  5309. resume_mic1(codec, oldval);
  5310. }
  5311. /*
  5312. * Initialization for Digital Mic.
  5313. */
  5314. static void ca0132_init_dmic(struct hda_codec *codec)
  5315. {
  5316. struct ca0132_spec *spec = codec->spec;
  5317. u8 val;
  5318. /* Setup Digital Mic here, but don't enable.
  5319. * Enable based on jack detect.
  5320. */
  5321. /* MCLK uses MPIO1, set to enable.
  5322. * Bit 2-0: MPIO select
  5323. * Bit 3: set to disable
  5324. * Bit 7-4: reserved
  5325. */
  5326. val = 0x01;
  5327. snd_hda_codec_write(codec, spec->input_pins[0], 0,
  5328. VENDOR_CHIPIO_DMIC_MCLK_SET, val);
  5329. /* Data1 uses MPIO3. Data2 not use
  5330. * Bit 2-0: Data1 MPIO select
  5331. * Bit 3: set disable Data1
  5332. * Bit 6-4: Data2 MPIO select
  5333. * Bit 7: set disable Data2
  5334. */
  5335. val = 0x83;
  5336. snd_hda_codec_write(codec, spec->input_pins[0], 0,
  5337. VENDOR_CHIPIO_DMIC_PIN_SET, val);
  5338. /* Use Ch-0 and Ch-1. Rate is 48K, mode 1. Disable DMic first.
  5339. * Bit 3-0: Channel mask
  5340. * Bit 4: set for 48KHz, clear for 32KHz
  5341. * Bit 5: mode
  5342. * Bit 6: set to select Data2, clear for Data1
  5343. * Bit 7: set to enable DMic, clear for AMic
  5344. */
  5345. if (spec->quirk == QUIRK_ALIENWARE_M17XR4)
  5346. val = 0x33;
  5347. else
  5348. val = 0x23;
  5349. /* keep a copy of dmic ctl val for enable/disable dmic purpuse */
  5350. spec->dmic_ctl = val;
  5351. snd_hda_codec_write(codec, spec->input_pins[0], 0,
  5352. VENDOR_CHIPIO_DMIC_CTL_SET, val);
  5353. }
  5354. /*
  5355. * Initialization for Analog Mic 2
  5356. */
  5357. static void ca0132_init_analog_mic2(struct hda_codec *codec)
  5358. {
  5359. struct ca0132_spec *spec = codec->spec;
  5360. mutex_lock(&spec->chipio_mutex);
  5361. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  5362. VENDOR_CHIPIO_8051_ADDRESS_LOW, 0x20);
  5363. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  5364. VENDOR_CHIPIO_8051_ADDRESS_HIGH, 0x19);
  5365. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  5366. VENDOR_CHIPIO_8051_DATA_WRITE, 0x00);
  5367. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  5368. VENDOR_CHIPIO_8051_ADDRESS_LOW, 0x2D);
  5369. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  5370. VENDOR_CHIPIO_8051_ADDRESS_HIGH, 0x19);
  5371. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  5372. VENDOR_CHIPIO_8051_DATA_WRITE, 0x00);
  5373. mutex_unlock(&spec->chipio_mutex);
  5374. }
  5375. static void ca0132_refresh_widget_caps(struct hda_codec *codec)
  5376. {
  5377. struct ca0132_spec *spec = codec->spec;
  5378. int i;
  5379. codec_dbg(codec, "ca0132_refresh_widget_caps.\n");
  5380. snd_hda_codec_update_widgets(codec);
  5381. for (i = 0; i < spec->multiout.num_dacs; i++)
  5382. refresh_amp_caps(codec, spec->dacs[i], HDA_OUTPUT);
  5383. for (i = 0; i < spec->num_outputs; i++)
  5384. refresh_amp_caps(codec, spec->out_pins[i], HDA_OUTPUT);
  5385. for (i = 0; i < spec->num_inputs; i++) {
  5386. refresh_amp_caps(codec, spec->adcs[i], HDA_INPUT);
  5387. refresh_amp_caps(codec, spec->input_pins[i], HDA_INPUT);
  5388. }
  5389. }
  5390. /*
  5391. * Recon3D r3d_setup_defaults sub functions.
  5392. */
  5393. static void r3d_dsp_scp_startup(struct hda_codec *codec)
  5394. {
  5395. unsigned int tmp;
  5396. tmp = 0x00000000;
  5397. dspio_set_uint_param_no_source(codec, 0x80, 0x0A, tmp);
  5398. tmp = 0x00000001;
  5399. dspio_set_uint_param_no_source(codec, 0x80, 0x0B, tmp);
  5400. tmp = 0x00000004;
  5401. dspio_set_uint_param_no_source(codec, 0x80, 0x0C, tmp);
  5402. tmp = 0x00000005;
  5403. dspio_set_uint_param_no_source(codec, 0x80, 0x0C, tmp);
  5404. tmp = 0x00000000;
  5405. dspio_set_uint_param_no_source(codec, 0x80, 0x0C, tmp);
  5406. }
  5407. static void r3d_dsp_initial_mic_setup(struct hda_codec *codec)
  5408. {
  5409. unsigned int tmp;
  5410. /* Mic 1 Setup */
  5411. chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
  5412. chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
  5413. /* This ConnPointID is unique to Recon3Di. Haven't seen it elsewhere */
  5414. chipio_set_conn_rate(codec, 0x0F, SR_96_000);
  5415. tmp = FLOAT_ONE;
  5416. dspio_set_uint_param(codec, 0x80, 0x00, tmp);
  5417. /* Mic 2 Setup, even though it isn't connected on SBZ */
  5418. chipio_set_conn_rate(codec, MEM_CONNID_MICIN2, SR_96_000);
  5419. chipio_set_conn_rate(codec, MEM_CONNID_MICOUT2, SR_96_000);
  5420. chipio_set_conn_rate(codec, 0x0F, SR_96_000);
  5421. tmp = FLOAT_ZERO;
  5422. dspio_set_uint_param(codec, 0x80, 0x01, tmp);
  5423. }
  5424. /*
  5425. * Initialize Sound Blaster Z analog microphones.
  5426. */
  5427. static void sbz_init_analog_mics(struct hda_codec *codec)
  5428. {
  5429. unsigned int tmp;
  5430. /* Mic 1 Setup */
  5431. chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
  5432. chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
  5433. tmp = FLOAT_THREE;
  5434. dspio_set_uint_param(codec, 0x80, 0x00, tmp);
  5435. /* Mic 2 Setup, even though it isn't connected on SBZ */
  5436. chipio_set_conn_rate(codec, MEM_CONNID_MICIN2, SR_96_000);
  5437. chipio_set_conn_rate(codec, MEM_CONNID_MICOUT2, SR_96_000);
  5438. tmp = FLOAT_ZERO;
  5439. dspio_set_uint_param(codec, 0x80, 0x01, tmp);
  5440. }
  5441. /*
  5442. * Sets the source of stream 0x14 to connpointID 0x48, and the destination
  5443. * connpointID to 0x91. If this isn't done, the destination is 0x71, and
  5444. * you get no sound. I'm guessing this has to do with the Sound Blaster Z
  5445. * having an updated DAC, which changes the destination to that DAC.
  5446. */
  5447. static void sbz_connect_streams(struct hda_codec *codec)
  5448. {
  5449. struct ca0132_spec *spec = codec->spec;
  5450. mutex_lock(&spec->chipio_mutex);
  5451. codec_dbg(codec, "Connect Streams entered, mutex locked and loaded.\n");
  5452. chipio_set_stream_channels(codec, 0x0C, 6);
  5453. chipio_set_stream_control(codec, 0x0C, 1);
  5454. /* This value is 0x43 for 96khz, and 0x83 for 192khz. */
  5455. chipio_write_no_mutex(codec, 0x18a020, 0x00000043);
  5456. /* Setup stream 0x14 with it's source and destination points */
  5457. chipio_set_stream_source_dest(codec, 0x14, 0x48, 0x91);
  5458. chipio_set_conn_rate_no_mutex(codec, 0x48, SR_96_000);
  5459. chipio_set_conn_rate_no_mutex(codec, 0x91, SR_96_000);
  5460. chipio_set_stream_channels(codec, 0x14, 2);
  5461. chipio_set_stream_control(codec, 0x14, 1);
  5462. codec_dbg(codec, "Connect Streams exited, mutex released.\n");
  5463. mutex_unlock(&spec->chipio_mutex);
  5464. }
  5465. /*
  5466. * Write data through ChipIO to setup proper stream destinations.
  5467. * Not sure how it exactly works, but it seems to direct data
  5468. * to different destinations. Example is f8 to c0, e0 to c0.
  5469. * All I know is, if you don't set these, you get no sound.
  5470. */
  5471. static void sbz_chipio_startup_data(struct hda_codec *codec)
  5472. {
  5473. struct ca0132_spec *spec = codec->spec;
  5474. mutex_lock(&spec->chipio_mutex);
  5475. codec_dbg(codec, "Startup Data entered, mutex locked and loaded.\n");
  5476. /* These control audio output */
  5477. chipio_write_no_mutex(codec, 0x190060, 0x0001f8c0);
  5478. chipio_write_no_mutex(codec, 0x190064, 0x0001f9c1);
  5479. chipio_write_no_mutex(codec, 0x190068, 0x0001fac6);
  5480. chipio_write_no_mutex(codec, 0x19006c, 0x0001fbc7);
  5481. /* Signal to update I think */
  5482. chipio_write_no_mutex(codec, 0x19042c, 0x00000001);
  5483. chipio_set_stream_channels(codec, 0x0C, 6);
  5484. chipio_set_stream_control(codec, 0x0C, 1);
  5485. /* No clue what these control */
  5486. chipio_write_no_mutex(codec, 0x190030, 0x0001e0c0);
  5487. chipio_write_no_mutex(codec, 0x190034, 0x0001e1c1);
  5488. chipio_write_no_mutex(codec, 0x190038, 0x0001e4c2);
  5489. chipio_write_no_mutex(codec, 0x19003c, 0x0001e5c3);
  5490. chipio_write_no_mutex(codec, 0x190040, 0x0001e2c4);
  5491. chipio_write_no_mutex(codec, 0x190044, 0x0001e3c5);
  5492. chipio_write_no_mutex(codec, 0x190048, 0x0001e8c6);
  5493. chipio_write_no_mutex(codec, 0x19004c, 0x0001e9c7);
  5494. chipio_write_no_mutex(codec, 0x190050, 0x0001ecc8);
  5495. chipio_write_no_mutex(codec, 0x190054, 0x0001edc9);
  5496. chipio_write_no_mutex(codec, 0x190058, 0x0001eaca);
  5497. chipio_write_no_mutex(codec, 0x19005c, 0x0001ebcb);
  5498. chipio_write_no_mutex(codec, 0x19042c, 0x00000001);
  5499. codec_dbg(codec, "Startup Data exited, mutex released.\n");
  5500. mutex_unlock(&spec->chipio_mutex);
  5501. }
  5502. /*
  5503. * Sound Blaster Z uses these after DSP is loaded. Weird SCP commands
  5504. * without a 0x20 source like normal.
  5505. */
  5506. static void sbz_dsp_scp_startup(struct hda_codec *codec)
  5507. {
  5508. unsigned int tmp;
  5509. tmp = 0x00000003;
  5510. dspio_set_uint_param_no_source(codec, 0x80, 0x0C, tmp);
  5511. tmp = 0x00000000;
  5512. dspio_set_uint_param_no_source(codec, 0x80, 0x0A, tmp);
  5513. tmp = 0x00000001;
  5514. dspio_set_uint_param_no_source(codec, 0x80, 0x0B, tmp);
  5515. tmp = 0x00000004;
  5516. dspio_set_uint_param_no_source(codec, 0x80, 0x0C, tmp);
  5517. tmp = 0x00000005;
  5518. dspio_set_uint_param_no_source(codec, 0x80, 0x0C, tmp);
  5519. tmp = 0x00000000;
  5520. dspio_set_uint_param_no_source(codec, 0x80, 0x0C, tmp);
  5521. }
  5522. static void sbz_dsp_initial_mic_setup(struct hda_codec *codec)
  5523. {
  5524. unsigned int tmp;
  5525. chipio_set_stream_control(codec, 0x03, 0);
  5526. chipio_set_stream_control(codec, 0x04, 0);
  5527. chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
  5528. chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
  5529. tmp = FLOAT_THREE;
  5530. dspio_set_uint_param(codec, 0x80, 0x00, tmp);
  5531. chipio_set_stream_control(codec, 0x03, 1);
  5532. chipio_set_stream_control(codec, 0x04, 1);
  5533. chipio_write(codec, 0x18b098, 0x0000000c);
  5534. chipio_write(codec, 0x18b09C, 0x0000000c);
  5535. }
  5536. /*
  5537. * Setup default parameters for DSP
  5538. */
  5539. static void ca0132_setup_defaults(struct hda_codec *codec)
  5540. {
  5541. struct ca0132_spec *spec = codec->spec;
  5542. unsigned int tmp;
  5543. int num_fx;
  5544. int idx, i;
  5545. if (spec->dsp_state != DSP_DOWNLOADED)
  5546. return;
  5547. /* out, in effects + voicefx */
  5548. num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT + 1;
  5549. for (idx = 0; idx < num_fx; idx++) {
  5550. for (i = 0; i <= ca0132_effects[idx].params; i++) {
  5551. dspio_set_uint_param(codec, ca0132_effects[idx].mid,
  5552. ca0132_effects[idx].reqs[i],
  5553. ca0132_effects[idx].def_vals[i]);
  5554. }
  5555. }
  5556. /*remove DSP headroom*/
  5557. tmp = FLOAT_ZERO;
  5558. dspio_set_uint_param(codec, 0x96, 0x3C, tmp);
  5559. /*set speaker EQ bypass attenuation*/
  5560. dspio_set_uint_param(codec, 0x8f, 0x01, tmp);
  5561. /* set AMic1 and AMic2 as mono mic */
  5562. tmp = FLOAT_ONE;
  5563. dspio_set_uint_param(codec, 0x80, 0x00, tmp);
  5564. dspio_set_uint_param(codec, 0x80, 0x01, tmp);
  5565. /* set AMic1 as CrystalVoice input */
  5566. tmp = FLOAT_ONE;
  5567. dspio_set_uint_param(codec, 0x80, 0x05, tmp);
  5568. /* set WUH source */
  5569. tmp = FLOAT_TWO;
  5570. dspio_set_uint_param(codec, 0x31, 0x00, tmp);
  5571. }
  5572. /*
  5573. * Setup default parameters for Recon3D/Recon3Di DSP.
  5574. */
  5575. static void r3d_setup_defaults(struct hda_codec *codec)
  5576. {
  5577. struct ca0132_spec *spec = codec->spec;
  5578. unsigned int tmp;
  5579. int num_fx;
  5580. int idx, i;
  5581. if (spec->dsp_state != DSP_DOWNLOADED)
  5582. return;
  5583. r3d_dsp_scp_startup(codec);
  5584. r3d_dsp_initial_mic_setup(codec);
  5585. /*remove DSP headroom*/
  5586. tmp = FLOAT_ZERO;
  5587. dspio_set_uint_param(codec, 0x96, 0x3C, tmp);
  5588. /* set WUH source */
  5589. tmp = FLOAT_TWO;
  5590. dspio_set_uint_param(codec, 0x31, 0x00, tmp);
  5591. chipio_set_conn_rate(codec, MEM_CONNID_WUH, SR_48_000);
  5592. /* Set speaker source? */
  5593. dspio_set_uint_param(codec, 0x32, 0x00, tmp);
  5594. if (spec->quirk == QUIRK_R3DI)
  5595. r3di_gpio_dsp_status_set(codec, R3DI_DSP_DOWNLOADED);
  5596. /* Setup effect defaults */
  5597. num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT + 1;
  5598. for (idx = 0; idx < num_fx; idx++) {
  5599. for (i = 0; i <= ca0132_effects[idx].params; i++) {
  5600. dspio_set_uint_param(codec,
  5601. ca0132_effects[idx].mid,
  5602. ca0132_effects[idx].reqs[i],
  5603. ca0132_effects[idx].def_vals[i]);
  5604. }
  5605. }
  5606. }
  5607. /*
  5608. * Setup default parameters for the Sound Blaster Z DSP. A lot more going on
  5609. * than the Chromebook setup.
  5610. */
  5611. static void sbz_setup_defaults(struct hda_codec *codec)
  5612. {
  5613. struct ca0132_spec *spec = codec->spec;
  5614. unsigned int tmp, stream_format;
  5615. int num_fx;
  5616. int idx, i;
  5617. if (spec->dsp_state != DSP_DOWNLOADED)
  5618. return;
  5619. sbz_dsp_scp_startup(codec);
  5620. sbz_init_analog_mics(codec);
  5621. sbz_connect_streams(codec);
  5622. sbz_chipio_startup_data(codec);
  5623. chipio_set_stream_control(codec, 0x03, 1);
  5624. chipio_set_stream_control(codec, 0x04, 1);
  5625. /*
  5626. * Sets internal input loopback to off, used to have a switch to
  5627. * enable input loopback, but turned out to be way too buggy.
  5628. */
  5629. tmp = FLOAT_ONE;
  5630. dspio_set_uint_param(codec, 0x37, 0x08, tmp);
  5631. dspio_set_uint_param(codec, 0x37, 0x10, tmp);
  5632. /*remove DSP headroom*/
  5633. tmp = FLOAT_ZERO;
  5634. dspio_set_uint_param(codec, 0x96, 0x3C, tmp);
  5635. /* set WUH source */
  5636. tmp = FLOAT_TWO;
  5637. dspio_set_uint_param(codec, 0x31, 0x00, tmp);
  5638. chipio_set_conn_rate(codec, MEM_CONNID_WUH, SR_48_000);
  5639. /* Set speaker source? */
  5640. dspio_set_uint_param(codec, 0x32, 0x00, tmp);
  5641. sbz_dsp_initial_mic_setup(codec);
  5642. /* out, in effects + voicefx */
  5643. num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT + 1;
  5644. for (idx = 0; idx < num_fx; idx++) {
  5645. for (i = 0; i <= ca0132_effects[idx].params; i++) {
  5646. dspio_set_uint_param(codec,
  5647. ca0132_effects[idx].mid,
  5648. ca0132_effects[idx].reqs[i],
  5649. ca0132_effects[idx].def_vals[i]);
  5650. }
  5651. }
  5652. /*
  5653. * Have to make a stream to bind the sound output to, otherwise
  5654. * you'll get dead audio. Before I did this, it would bind to an
  5655. * audio input, and would never work
  5656. */
  5657. stream_format = snd_hdac_calc_stream_format(48000, 2,
  5658. SNDRV_PCM_FORMAT_S32_LE, 32, 0);
  5659. snd_hda_codec_setup_stream(codec, spec->dacs[0], spec->dsp_stream_id,
  5660. 0, stream_format);
  5661. snd_hda_codec_cleanup_stream(codec, spec->dacs[0]);
  5662. snd_hda_codec_setup_stream(codec, spec->dacs[0], spec->dsp_stream_id,
  5663. 0, stream_format);
  5664. snd_hda_codec_cleanup_stream(codec, spec->dacs[0]);
  5665. }
  5666. /*
  5667. * Initialization of flags in chip
  5668. */
  5669. static void ca0132_init_flags(struct hda_codec *codec)
  5670. {
  5671. struct ca0132_spec *spec = codec->spec;
  5672. if (spec->use_alt_functions) {
  5673. chipio_set_control_flag(codec, CONTROL_FLAG_DSP_96KHZ, 1);
  5674. chipio_set_control_flag(codec, CONTROL_FLAG_DAC_96KHZ, 1);
  5675. chipio_set_control_flag(codec, CONTROL_FLAG_ADC_B_96KHZ, 1);
  5676. chipio_set_control_flag(codec, CONTROL_FLAG_ADC_C_96KHZ, 1);
  5677. chipio_set_control_flag(codec, CONTROL_FLAG_SRC_RATE_96KHZ, 1);
  5678. chipio_set_control_flag(codec, CONTROL_FLAG_IDLE_ENABLE, 0);
  5679. chipio_set_control_flag(codec, CONTROL_FLAG_SPDIF2OUT, 0);
  5680. chipio_set_control_flag(codec,
  5681. CONTROL_FLAG_PORT_D_10KOHM_LOAD, 0);
  5682. chipio_set_control_flag(codec,
  5683. CONTROL_FLAG_PORT_A_10KOHM_LOAD, 1);
  5684. } else {
  5685. chipio_set_control_flag(codec, CONTROL_FLAG_IDLE_ENABLE, 0);
  5686. chipio_set_control_flag(codec,
  5687. CONTROL_FLAG_PORT_A_COMMON_MODE, 0);
  5688. chipio_set_control_flag(codec,
  5689. CONTROL_FLAG_PORT_D_COMMON_MODE, 0);
  5690. chipio_set_control_flag(codec,
  5691. CONTROL_FLAG_PORT_A_10KOHM_LOAD, 0);
  5692. chipio_set_control_flag(codec,
  5693. CONTROL_FLAG_PORT_D_10KOHM_LOAD, 0);
  5694. chipio_set_control_flag(codec, CONTROL_FLAG_ADC_C_HIGH_PASS, 1);
  5695. }
  5696. }
  5697. /*
  5698. * Initialization of parameters in chip
  5699. */
  5700. static void ca0132_init_params(struct hda_codec *codec)
  5701. {
  5702. struct ca0132_spec *spec = codec->spec;
  5703. if (spec->use_alt_functions) {
  5704. chipio_set_conn_rate(codec, MEM_CONNID_WUH, SR_48_000);
  5705. chipio_set_conn_rate(codec, 0x0B, SR_48_000);
  5706. chipio_set_control_param(codec, CONTROL_PARAM_SPDIF1_SOURCE, 0);
  5707. chipio_set_control_param(codec, 0, 0);
  5708. chipio_set_control_param(codec, CONTROL_PARAM_VIP_SOURCE, 0);
  5709. }
  5710. chipio_set_control_param(codec, CONTROL_PARAM_PORTA_160OHM_GAIN, 6);
  5711. chipio_set_control_param(codec, CONTROL_PARAM_PORTD_160OHM_GAIN, 6);
  5712. }
  5713. static void ca0132_set_dsp_msr(struct hda_codec *codec, bool is96k)
  5714. {
  5715. chipio_set_control_flag(codec, CONTROL_FLAG_DSP_96KHZ, is96k);
  5716. chipio_set_control_flag(codec, CONTROL_FLAG_DAC_96KHZ, is96k);
  5717. chipio_set_control_flag(codec, CONTROL_FLAG_SRC_RATE_96KHZ, is96k);
  5718. chipio_set_control_flag(codec, CONTROL_FLAG_SRC_CLOCK_196MHZ, is96k);
  5719. chipio_set_control_flag(codec, CONTROL_FLAG_ADC_B_96KHZ, is96k);
  5720. chipio_set_control_flag(codec, CONTROL_FLAG_ADC_C_96KHZ, is96k);
  5721. chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
  5722. chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
  5723. chipio_set_conn_rate(codec, MEM_CONNID_WUH, SR_48_000);
  5724. }
  5725. static bool ca0132_download_dsp_images(struct hda_codec *codec)
  5726. {
  5727. bool dsp_loaded = false;
  5728. struct ca0132_spec *spec = codec->spec;
  5729. const struct dsp_image_seg *dsp_os_image;
  5730. const struct firmware *fw_entry;
  5731. /*
  5732. * Alternate firmwares for different variants. The Recon3Di apparently
  5733. * can use the default firmware, but I'll leave the option in case
  5734. * it needs it again.
  5735. */
  5736. switch (spec->quirk) {
  5737. case QUIRK_SBZ:
  5738. if (request_firmware(&fw_entry, SBZ_EFX_FILE,
  5739. codec->card->dev) != 0) {
  5740. codec_dbg(codec, "SBZ alt firmware not detected. ");
  5741. spec->alt_firmware_present = false;
  5742. } else {
  5743. codec_dbg(codec, "Sound Blaster Z firmware selected.");
  5744. spec->alt_firmware_present = true;
  5745. }
  5746. break;
  5747. case QUIRK_R3DI:
  5748. if (request_firmware(&fw_entry, R3DI_EFX_FILE,
  5749. codec->card->dev) != 0) {
  5750. codec_dbg(codec, "Recon3Di alt firmware not detected.");
  5751. spec->alt_firmware_present = false;
  5752. } else {
  5753. codec_dbg(codec, "Recon3Di firmware selected.");
  5754. spec->alt_firmware_present = true;
  5755. }
  5756. break;
  5757. default:
  5758. spec->alt_firmware_present = false;
  5759. break;
  5760. }
  5761. /*
  5762. * Use default ctefx.bin if no alt firmware is detected, or if none
  5763. * exists for your particular codec.
  5764. */
  5765. if (!spec->alt_firmware_present) {
  5766. codec_dbg(codec, "Default firmware selected.");
  5767. if (request_firmware(&fw_entry, EFX_FILE,
  5768. codec->card->dev) != 0)
  5769. return false;
  5770. }
  5771. dsp_os_image = (struct dsp_image_seg *)(fw_entry->data);
  5772. if (dspload_image(codec, dsp_os_image, 0, 0, true, 0)) {
  5773. codec_err(codec, "ca0132 DSP load image failed\n");
  5774. goto exit_download;
  5775. }
  5776. dsp_loaded = dspload_wait_loaded(codec);
  5777. exit_download:
  5778. release_firmware(fw_entry);
  5779. return dsp_loaded;
  5780. }
  5781. static void ca0132_download_dsp(struct hda_codec *codec)
  5782. {
  5783. struct ca0132_spec *spec = codec->spec;
  5784. #ifndef CONFIG_SND_HDA_CODEC_CA0132_DSP
  5785. return; /* NOP */
  5786. #endif
  5787. if (spec->dsp_state == DSP_DOWNLOAD_FAILED)
  5788. return; /* don't retry failures */
  5789. chipio_enable_clocks(codec);
  5790. if (spec->dsp_state != DSP_DOWNLOADED) {
  5791. spec->dsp_state = DSP_DOWNLOADING;
  5792. if (!ca0132_download_dsp_images(codec))
  5793. spec->dsp_state = DSP_DOWNLOAD_FAILED;
  5794. else
  5795. spec->dsp_state = DSP_DOWNLOADED;
  5796. }
  5797. /* For codecs using alt functions, this is already done earlier */
  5798. if (spec->dsp_state == DSP_DOWNLOADED && (!spec->use_alt_functions))
  5799. ca0132_set_dsp_msr(codec, true);
  5800. }
  5801. static void ca0132_process_dsp_response(struct hda_codec *codec,
  5802. struct hda_jack_callback *callback)
  5803. {
  5804. struct ca0132_spec *spec = codec->spec;
  5805. codec_dbg(codec, "ca0132_process_dsp_response\n");
  5806. snd_hda_power_up_pm(codec);
  5807. if (spec->wait_scp) {
  5808. if (dspio_get_response_data(codec) >= 0)
  5809. spec->wait_scp = 0;
  5810. }
  5811. dspio_clear_response_queue(codec);
  5812. snd_hda_power_down_pm(codec);
  5813. }
  5814. static void hp_callback(struct hda_codec *codec, struct hda_jack_callback *cb)
  5815. {
  5816. struct ca0132_spec *spec = codec->spec;
  5817. struct hda_jack_tbl *tbl;
  5818. /* Delay enabling the HP amp, to let the mic-detection
  5819. * state machine run.
  5820. */
  5821. tbl = snd_hda_jack_tbl_get(codec, cb->nid);
  5822. if (tbl)
  5823. tbl->block_report = 1;
  5824. schedule_delayed_work(&spec->unsol_hp_work, msecs_to_jiffies(500));
  5825. }
  5826. static void amic_callback(struct hda_codec *codec, struct hda_jack_callback *cb)
  5827. {
  5828. struct ca0132_spec *spec = codec->spec;
  5829. if (spec->use_alt_functions)
  5830. ca0132_alt_select_in(codec);
  5831. else
  5832. ca0132_select_mic(codec);
  5833. }
  5834. static void ca0132_init_unsol(struct hda_codec *codec)
  5835. {
  5836. struct ca0132_spec *spec = codec->spec;
  5837. snd_hda_jack_detect_enable_callback(codec, spec->unsol_tag_hp, hp_callback);
  5838. snd_hda_jack_detect_enable_callback(codec, spec->unsol_tag_amic1,
  5839. amic_callback);
  5840. snd_hda_jack_detect_enable_callback(codec, UNSOL_TAG_DSP,
  5841. ca0132_process_dsp_response);
  5842. /* Front headphone jack detection */
  5843. if (spec->use_alt_functions)
  5844. snd_hda_jack_detect_enable_callback(codec,
  5845. spec->unsol_tag_front_hp, hp_callback);
  5846. }
  5847. /*
  5848. * Verbs tables.
  5849. */
  5850. /* Sends before DSP download. */
  5851. static struct hda_verb ca0132_base_init_verbs[] = {
  5852. /*enable ct extension*/
  5853. {0x15, VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE, 0x1},
  5854. {}
  5855. };
  5856. /* Send at exit. */
  5857. static struct hda_verb ca0132_base_exit_verbs[] = {
  5858. /*set afg to D3*/
  5859. {0x01, AC_VERB_SET_POWER_STATE, 0x03},
  5860. /*disable ct extension*/
  5861. {0x15, VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE, 0},
  5862. {}
  5863. };
  5864. /* Other verbs tables. Sends after DSP download. */
  5865. static struct hda_verb ca0132_init_verbs0[] = {
  5866. /* chip init verbs */
  5867. {0x15, 0x70D, 0xF0},
  5868. {0x15, 0x70E, 0xFE},
  5869. {0x15, 0x707, 0x75},
  5870. {0x15, 0x707, 0xD3},
  5871. {0x15, 0x707, 0x09},
  5872. {0x15, 0x707, 0x53},
  5873. {0x15, 0x707, 0xD4},
  5874. {0x15, 0x707, 0xEF},
  5875. {0x15, 0x707, 0x75},
  5876. {0x15, 0x707, 0xD3},
  5877. {0x15, 0x707, 0x09},
  5878. {0x15, 0x707, 0x02},
  5879. {0x15, 0x707, 0x37},
  5880. {0x15, 0x707, 0x78},
  5881. {0x15, 0x53C, 0xCE},
  5882. {0x15, 0x575, 0xC9},
  5883. {0x15, 0x53D, 0xCE},
  5884. {0x15, 0x5B7, 0xC9},
  5885. {0x15, 0x70D, 0xE8},
  5886. {0x15, 0x70E, 0xFE},
  5887. {0x15, 0x707, 0x02},
  5888. {0x15, 0x707, 0x68},
  5889. {0x15, 0x707, 0x62},
  5890. {0x15, 0x53A, 0xCE},
  5891. {0x15, 0x546, 0xC9},
  5892. {0x15, 0x53B, 0xCE},
  5893. {0x15, 0x5E8, 0xC9},
  5894. {}
  5895. };
  5896. /* Extra init verbs for desktop cards. */
  5897. static struct hda_verb ca0132_init_verbs1[] = {
  5898. {0x15, 0x70D, 0x20},
  5899. {0x15, 0x70E, 0x19},
  5900. {0x15, 0x707, 0x00},
  5901. {0x15, 0x539, 0xCE},
  5902. {0x15, 0x546, 0xC9},
  5903. {0x15, 0x70D, 0xB7},
  5904. {0x15, 0x70E, 0x09},
  5905. {0x15, 0x707, 0x10},
  5906. {0x15, 0x70D, 0xAF},
  5907. {0x15, 0x70E, 0x09},
  5908. {0x15, 0x707, 0x01},
  5909. {0x15, 0x707, 0x05},
  5910. {0x15, 0x70D, 0x73},
  5911. {0x15, 0x70E, 0x09},
  5912. {0x15, 0x707, 0x14},
  5913. {0x15, 0x6FF, 0xC4},
  5914. {}
  5915. };
  5916. static void ca0132_init_chip(struct hda_codec *codec)
  5917. {
  5918. struct ca0132_spec *spec = codec->spec;
  5919. int num_fx;
  5920. int i;
  5921. unsigned int on;
  5922. mutex_init(&spec->chipio_mutex);
  5923. spec->cur_out_type = SPEAKER_OUT;
  5924. if (!spec->use_alt_functions)
  5925. spec->cur_mic_type = DIGITAL_MIC;
  5926. else
  5927. spec->cur_mic_type = REAR_MIC;
  5928. spec->cur_mic_boost = 0;
  5929. for (i = 0; i < VNODES_COUNT; i++) {
  5930. spec->vnode_lvol[i] = 0x5a;
  5931. spec->vnode_rvol[i] = 0x5a;
  5932. spec->vnode_lswitch[i] = 0;
  5933. spec->vnode_rswitch[i] = 0;
  5934. }
  5935. /*
  5936. * Default states for effects are in ca0132_effects[].
  5937. */
  5938. num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT;
  5939. for (i = 0; i < num_fx; i++) {
  5940. on = (unsigned int)ca0132_effects[i].reqs[0];
  5941. spec->effects_switch[i] = on ? 1 : 0;
  5942. }
  5943. /*
  5944. * Sets defaults for the effect slider controls, only for alternative
  5945. * ca0132 codecs. Also sets x-bass crossover frequency to 80hz.
  5946. */
  5947. if (spec->use_alt_controls) {
  5948. spec->xbass_xover_freq = 8;
  5949. for (i = 0; i < EFFECT_LEVEL_SLIDERS; i++)
  5950. spec->fx_ctl_val[i] = effect_slider_defaults[i];
  5951. }
  5952. spec->voicefx_val = 0;
  5953. spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID] = 1;
  5954. spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID] = 0;
  5955. #ifdef ENABLE_TUNING_CONTROLS
  5956. ca0132_init_tuning_defaults(codec);
  5957. #endif
  5958. }
  5959. /*
  5960. * Recon3Di exit specific commands.
  5961. */
  5962. /* prevents popping noise on shutdown */
  5963. static void r3di_gpio_shutdown(struct hda_codec *codec)
  5964. {
  5965. snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA, 0x00);
  5966. }
  5967. /*
  5968. * Sound Blaster Z exit specific commands.
  5969. */
  5970. static void sbz_region2_exit(struct hda_codec *codec)
  5971. {
  5972. struct ca0132_spec *spec = codec->spec;
  5973. unsigned int i;
  5974. for (i = 0; i < 4; i++)
  5975. writeb(0x0, spec->mem_base + 0x100);
  5976. for (i = 0; i < 8; i++)
  5977. writeb(0xb3, spec->mem_base + 0x304);
  5978. ca0132_mmio_gpio_set(codec, 0, false);
  5979. ca0132_mmio_gpio_set(codec, 1, false);
  5980. ca0132_mmio_gpio_set(codec, 4, true);
  5981. ca0132_mmio_gpio_set(codec, 5, false);
  5982. ca0132_mmio_gpio_set(codec, 7, false);
  5983. }
  5984. static void sbz_set_pin_ctl_default(struct hda_codec *codec)
  5985. {
  5986. hda_nid_t pins[5] = {0x0B, 0x0C, 0x0E, 0x12, 0x13};
  5987. unsigned int i;
  5988. snd_hda_codec_write(codec, 0x11, 0,
  5989. AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40);
  5990. for (i = 0; i < 5; i++)
  5991. snd_hda_codec_write(codec, pins[i], 0,
  5992. AC_VERB_SET_PIN_WIDGET_CONTROL, 0x00);
  5993. }
  5994. static void ca0132_clear_unsolicited(struct hda_codec *codec)
  5995. {
  5996. hda_nid_t pins[7] = {0x0B, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13};
  5997. unsigned int i;
  5998. for (i = 0; i < 7; i++) {
  5999. snd_hda_codec_write(codec, pins[i], 0,
  6000. AC_VERB_SET_UNSOLICITED_ENABLE, 0x00);
  6001. }
  6002. }
  6003. /* On shutdown, sends commands in sets of three */
  6004. static void sbz_gpio_shutdown_commands(struct hda_codec *codec, int dir,
  6005. int mask, int data)
  6006. {
  6007. if (dir >= 0)
  6008. snd_hda_codec_write(codec, 0x01, 0,
  6009. AC_VERB_SET_GPIO_DIRECTION, dir);
  6010. if (mask >= 0)
  6011. snd_hda_codec_write(codec, 0x01, 0,
  6012. AC_VERB_SET_GPIO_MASK, mask);
  6013. if (data >= 0)
  6014. snd_hda_codec_write(codec, 0x01, 0,
  6015. AC_VERB_SET_GPIO_DATA, data);
  6016. }
  6017. static void sbz_exit_chip(struct hda_codec *codec)
  6018. {
  6019. chipio_set_stream_control(codec, 0x03, 0);
  6020. chipio_set_stream_control(codec, 0x04, 0);
  6021. /* Mess with GPIO */
  6022. sbz_gpio_shutdown_commands(codec, 0x07, 0x07, -1);
  6023. sbz_gpio_shutdown_commands(codec, 0x07, 0x07, 0x05);
  6024. sbz_gpio_shutdown_commands(codec, 0x07, 0x07, 0x01);
  6025. chipio_set_stream_control(codec, 0x14, 0);
  6026. chipio_set_stream_control(codec, 0x0C, 0);
  6027. chipio_set_conn_rate(codec, 0x41, SR_192_000);
  6028. chipio_set_conn_rate(codec, 0x91, SR_192_000);
  6029. chipio_write(codec, 0x18a020, 0x00000083);
  6030. sbz_gpio_shutdown_commands(codec, 0x07, 0x07, 0x03);
  6031. sbz_gpio_shutdown_commands(codec, 0x07, 0x07, 0x07);
  6032. sbz_gpio_shutdown_commands(codec, 0x07, 0x07, 0x06);
  6033. chipio_set_stream_control(codec, 0x0C, 0);
  6034. chipio_set_control_param(codec, 0x0D, 0x24);
  6035. ca0132_clear_unsolicited(codec);
  6036. sbz_set_pin_ctl_default(codec);
  6037. snd_hda_codec_write(codec, 0x0B, 0,
  6038. AC_VERB_SET_EAPD_BTLENABLE, 0x00);
  6039. sbz_region2_exit(codec);
  6040. }
  6041. static void r3d_exit_chip(struct hda_codec *codec)
  6042. {
  6043. ca0132_clear_unsolicited(codec);
  6044. snd_hda_codec_write(codec, 0x01, 0, 0x793, 0x00);
  6045. snd_hda_codec_write(codec, 0x01, 0, 0x794, 0x5b);
  6046. }
  6047. static void ca0132_exit_chip(struct hda_codec *codec)
  6048. {
  6049. /* put any chip cleanup stuffs here. */
  6050. if (dspload_is_loaded(codec))
  6051. dsp_reset(codec);
  6052. }
  6053. /*
  6054. * This fixes a problem that was hard to reproduce. Very rarely, I would
  6055. * boot up, and there would be no sound, but the DSP indicated it had loaded
  6056. * properly. I did a few memory dumps to see if anything was different, and
  6057. * there were a few areas of memory uninitialized with a1a2a3a4. This function
  6058. * checks if those areas are uninitialized, and if they are, it'll attempt to
  6059. * reload the card 3 times. Usually it fixes by the second.
  6060. */
  6061. static void sbz_dsp_startup_check(struct hda_codec *codec)
  6062. {
  6063. struct ca0132_spec *spec = codec->spec;
  6064. unsigned int dsp_data_check[4];
  6065. unsigned int cur_address = 0x390;
  6066. unsigned int i;
  6067. unsigned int failure = 0;
  6068. unsigned int reload = 3;
  6069. if (spec->startup_check_entered)
  6070. return;
  6071. spec->startup_check_entered = true;
  6072. for (i = 0; i < 4; i++) {
  6073. chipio_read(codec, cur_address, &dsp_data_check[i]);
  6074. cur_address += 0x4;
  6075. }
  6076. for (i = 0; i < 4; i++) {
  6077. if (dsp_data_check[i] == 0xa1a2a3a4)
  6078. failure = 1;
  6079. }
  6080. codec_dbg(codec, "Startup Check: %d ", failure);
  6081. if (failure)
  6082. codec_info(codec, "DSP not initialized properly. Attempting to fix.");
  6083. /*
  6084. * While the failure condition is true, and we haven't reached our
  6085. * three reload limit, continue trying to reload the driver and
  6086. * fix the issue.
  6087. */
  6088. while (failure && (reload != 0)) {
  6089. codec_info(codec, "Reloading... Tries left: %d", reload);
  6090. sbz_exit_chip(codec);
  6091. spec->dsp_state = DSP_DOWNLOAD_INIT;
  6092. codec->patch_ops.init(codec);
  6093. failure = 0;
  6094. for (i = 0; i < 4; i++) {
  6095. chipio_read(codec, cur_address, &dsp_data_check[i]);
  6096. cur_address += 0x4;
  6097. }
  6098. for (i = 0; i < 4; i++) {
  6099. if (dsp_data_check[i] == 0xa1a2a3a4)
  6100. failure = 1;
  6101. }
  6102. reload--;
  6103. }
  6104. if (!failure && reload < 3)
  6105. codec_info(codec, "DSP fixed.");
  6106. if (!failure)
  6107. return;
  6108. codec_info(codec, "DSP failed to initialize properly. Either try a full shutdown or a suspend to clear the internal memory.");
  6109. }
  6110. /*
  6111. * This is for the extra volume verbs 0x797 (left) and 0x798 (right). These add
  6112. * extra precision for decibel values. If you had the dB value in floating point
  6113. * you would take the value after the decimal point, multiply by 64, and divide
  6114. * by 2. So for 8.59, it's (59 * 64) / 100. Useful if someone wanted to
  6115. * implement fixed point or floating point dB volumes. For now, I'll set them
  6116. * to 0 just incase a value has lingered from a boot into Windows.
  6117. */
  6118. static void ca0132_alt_vol_setup(struct hda_codec *codec)
  6119. {
  6120. snd_hda_codec_write(codec, 0x02, 0, 0x797, 0x00);
  6121. snd_hda_codec_write(codec, 0x02, 0, 0x798, 0x00);
  6122. snd_hda_codec_write(codec, 0x03, 0, 0x797, 0x00);
  6123. snd_hda_codec_write(codec, 0x03, 0, 0x798, 0x00);
  6124. snd_hda_codec_write(codec, 0x04, 0, 0x797, 0x00);
  6125. snd_hda_codec_write(codec, 0x04, 0, 0x798, 0x00);
  6126. snd_hda_codec_write(codec, 0x07, 0, 0x797, 0x00);
  6127. snd_hda_codec_write(codec, 0x07, 0, 0x798, 0x00);
  6128. }
  6129. /*
  6130. * Extra commands that don't really fit anywhere else.
  6131. */
  6132. static void sbz_pre_dsp_setup(struct hda_codec *codec)
  6133. {
  6134. struct ca0132_spec *spec = codec->spec;
  6135. writel(0x00820680, spec->mem_base + 0x01C);
  6136. writel(0x00820680, spec->mem_base + 0x01C);
  6137. snd_hda_codec_write(codec, 0x15, 0, 0xd00, 0xfc);
  6138. snd_hda_codec_write(codec, 0x15, 0, 0xd00, 0xfd);
  6139. snd_hda_codec_write(codec, 0x15, 0, 0xd00, 0xfe);
  6140. snd_hda_codec_write(codec, 0x15, 0, 0xd00, 0xff);
  6141. chipio_write(codec, 0x18b0a4, 0x000000c2);
  6142. snd_hda_codec_write(codec, 0x11, 0,
  6143. AC_VERB_SET_PIN_WIDGET_CONTROL, 0x44);
  6144. }
  6145. static void r3d_pre_dsp_setup(struct hda_codec *codec)
  6146. {
  6147. snd_hda_codec_write(codec, 0x15, 0, 0xd00, 0xfc);
  6148. snd_hda_codec_write(codec, 0x15, 0, 0xd00, 0xfd);
  6149. snd_hda_codec_write(codec, 0x15, 0, 0xd00, 0xfe);
  6150. snd_hda_codec_write(codec, 0x15, 0, 0xd00, 0xff);
  6151. chipio_write(codec, 0x18b0a4, 0x000000c2);
  6152. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  6153. VENDOR_CHIPIO_8051_ADDRESS_LOW, 0x1E);
  6154. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  6155. VENDOR_CHIPIO_8051_ADDRESS_HIGH, 0x1C);
  6156. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  6157. VENDOR_CHIPIO_8051_DATA_WRITE, 0x5B);
  6158. snd_hda_codec_write(codec, 0x11, 0,
  6159. AC_VERB_SET_PIN_WIDGET_CONTROL, 0x44);
  6160. }
  6161. static void r3di_pre_dsp_setup(struct hda_codec *codec)
  6162. {
  6163. chipio_write(codec, 0x18b0a4, 0x000000c2);
  6164. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  6165. VENDOR_CHIPIO_8051_ADDRESS_LOW, 0x1E);
  6166. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  6167. VENDOR_CHIPIO_8051_ADDRESS_HIGH, 0x1C);
  6168. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  6169. VENDOR_CHIPIO_8051_DATA_WRITE, 0x5B);
  6170. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  6171. VENDOR_CHIPIO_8051_ADDRESS_LOW, 0x20);
  6172. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  6173. VENDOR_CHIPIO_8051_ADDRESS_HIGH, 0x19);
  6174. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  6175. VENDOR_CHIPIO_8051_DATA_WRITE, 0x00);
  6176. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  6177. VENDOR_CHIPIO_8051_DATA_WRITE, 0x40);
  6178. snd_hda_codec_write(codec, 0x11, 0,
  6179. AC_VERB_SET_PIN_WIDGET_CONTROL, 0x04);
  6180. }
  6181. /*
  6182. * These are sent before the DSP is downloaded. Not sure
  6183. * what they do, or if they're necessary. Could possibly
  6184. * be removed. Figure they're better to leave in.
  6185. */
  6186. static void ca0132_mmio_init(struct hda_codec *codec)
  6187. {
  6188. struct ca0132_spec *spec = codec->spec;
  6189. writel(0x00000000, spec->mem_base + 0x400);
  6190. writel(0x00000000, spec->mem_base + 0x408);
  6191. writel(0x00000000, spec->mem_base + 0x40C);
  6192. writel(0x00880680, spec->mem_base + 0x01C);
  6193. writel(0x00000083, spec->mem_base + 0xC0C);
  6194. writel(0x00000030, spec->mem_base + 0xC00);
  6195. writel(0x00000000, spec->mem_base + 0xC04);
  6196. writel(0x00000003, spec->mem_base + 0xC0C);
  6197. writel(0x00000003, spec->mem_base + 0xC0C);
  6198. writel(0x00000003, spec->mem_base + 0xC0C);
  6199. writel(0x00000003, spec->mem_base + 0xC0C);
  6200. writel(0x000000C1, spec->mem_base + 0xC08);
  6201. writel(0x000000F1, spec->mem_base + 0xC08);
  6202. writel(0x00000001, spec->mem_base + 0xC08);
  6203. writel(0x000000C7, spec->mem_base + 0xC08);
  6204. writel(0x000000C1, spec->mem_base + 0xC08);
  6205. writel(0x00000080, spec->mem_base + 0xC04);
  6206. }
  6207. /*
  6208. * Extra init functions for alternative ca0132 codecs. Done
  6209. * here so they don't clutter up the main ca0132_init function
  6210. * anymore than they have to.
  6211. */
  6212. static void ca0132_alt_init(struct hda_codec *codec)
  6213. {
  6214. struct ca0132_spec *spec = codec->spec;
  6215. ca0132_alt_vol_setup(codec);
  6216. switch (spec->quirk) {
  6217. case QUIRK_SBZ:
  6218. codec_dbg(codec, "SBZ alt_init");
  6219. ca0132_gpio_init(codec);
  6220. sbz_pre_dsp_setup(codec);
  6221. snd_hda_sequence_write(codec, spec->chip_init_verbs);
  6222. snd_hda_sequence_write(codec, spec->desktop_init_verbs);
  6223. break;
  6224. case QUIRK_R3DI:
  6225. codec_dbg(codec, "R3DI alt_init");
  6226. ca0132_gpio_init(codec);
  6227. ca0132_gpio_setup(codec);
  6228. r3di_gpio_dsp_status_set(codec, R3DI_DSP_DOWNLOADING);
  6229. r3di_pre_dsp_setup(codec);
  6230. snd_hda_sequence_write(codec, spec->chip_init_verbs);
  6231. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0, 0x6FF, 0xC4);
  6232. break;
  6233. case QUIRK_R3D:
  6234. r3d_pre_dsp_setup(codec);
  6235. snd_hda_sequence_write(codec, spec->chip_init_verbs);
  6236. snd_hda_sequence_write(codec, spec->desktop_init_verbs);
  6237. break;
  6238. }
  6239. }
  6240. static int ca0132_init(struct hda_codec *codec)
  6241. {
  6242. struct ca0132_spec *spec = codec->spec;
  6243. struct auto_pin_cfg *cfg = &spec->autocfg;
  6244. int i;
  6245. bool dsp_loaded;
  6246. /*
  6247. * If the DSP is already downloaded, and init has been entered again,
  6248. * there's only two reasons for it. One, the codec has awaken from a
  6249. * suspended state, and in that case dspload_is_loaded will return
  6250. * false, and the init will be ran again. The other reason it gets
  6251. * re entered is on startup for some reason it triggers a suspend and
  6252. * resume state. In this case, it will check if the DSP is downloaded,
  6253. * and not run the init function again. For codecs using alt_functions,
  6254. * it will check if the DSP is loaded properly.
  6255. */
  6256. if (spec->dsp_state == DSP_DOWNLOADED) {
  6257. dsp_loaded = dspload_is_loaded(codec);
  6258. if (!dsp_loaded) {
  6259. spec->dsp_reload = true;
  6260. spec->dsp_state = DSP_DOWNLOAD_INIT;
  6261. } else {
  6262. if (spec->quirk == QUIRK_SBZ)
  6263. sbz_dsp_startup_check(codec);
  6264. return 0;
  6265. }
  6266. }
  6267. if (spec->dsp_state != DSP_DOWNLOAD_FAILED)
  6268. spec->dsp_state = DSP_DOWNLOAD_INIT;
  6269. spec->curr_chip_addx = INVALID_CHIP_ADDRESS;
  6270. if (spec->use_pci_mmio)
  6271. ca0132_mmio_init(codec);
  6272. snd_hda_power_up_pm(codec);
  6273. ca0132_init_unsol(codec);
  6274. ca0132_init_params(codec);
  6275. ca0132_init_flags(codec);
  6276. snd_hda_sequence_write(codec, spec->base_init_verbs);
  6277. if (spec->use_alt_functions)
  6278. ca0132_alt_init(codec);
  6279. ca0132_download_dsp(codec);
  6280. ca0132_refresh_widget_caps(codec);
  6281. switch (spec->quirk) {
  6282. case QUIRK_R3DI:
  6283. case QUIRK_R3D:
  6284. r3d_setup_defaults(codec);
  6285. break;
  6286. case QUIRK_SBZ:
  6287. sbz_setup_defaults(codec);
  6288. break;
  6289. default:
  6290. ca0132_setup_defaults(codec);
  6291. ca0132_init_analog_mic2(codec);
  6292. ca0132_init_dmic(codec);
  6293. break;
  6294. }
  6295. for (i = 0; i < spec->num_outputs; i++)
  6296. init_output(codec, spec->out_pins[i], spec->dacs[0]);
  6297. init_output(codec, cfg->dig_out_pins[0], spec->dig_out);
  6298. for (i = 0; i < spec->num_inputs; i++)
  6299. init_input(codec, spec->input_pins[i], spec->adcs[i]);
  6300. init_input(codec, cfg->dig_in_pin, spec->dig_in);
  6301. if (!spec->use_alt_functions) {
  6302. snd_hda_sequence_write(codec, spec->chip_init_verbs);
  6303. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  6304. VENDOR_CHIPIO_PARAM_EX_ID_SET, 0x0D);
  6305. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  6306. VENDOR_CHIPIO_PARAM_EX_VALUE_SET, 0x20);
  6307. }
  6308. if (spec->quirk == QUIRK_SBZ)
  6309. ca0132_gpio_setup(codec);
  6310. snd_hda_sequence_write(codec, spec->spec_init_verbs);
  6311. if (spec->use_alt_functions) {
  6312. ca0132_alt_select_out(codec);
  6313. ca0132_alt_select_in(codec);
  6314. } else {
  6315. ca0132_select_out(codec);
  6316. ca0132_select_mic(codec);
  6317. }
  6318. snd_hda_jack_report_sync(codec);
  6319. /*
  6320. * Re set the PlayEnhancement switch on a resume event, because the
  6321. * controls will not be reloaded.
  6322. */
  6323. if (spec->dsp_reload) {
  6324. spec->dsp_reload = false;
  6325. ca0132_pe_switch_set(codec);
  6326. }
  6327. snd_hda_power_down_pm(codec);
  6328. return 0;
  6329. }
  6330. static void ca0132_free(struct hda_codec *codec)
  6331. {
  6332. struct ca0132_spec *spec = codec->spec;
  6333. cancel_delayed_work_sync(&spec->unsol_hp_work);
  6334. snd_hda_power_up(codec);
  6335. switch (spec->quirk) {
  6336. case QUIRK_SBZ:
  6337. sbz_exit_chip(codec);
  6338. break;
  6339. case QUIRK_R3D:
  6340. r3d_exit_chip(codec);
  6341. break;
  6342. case QUIRK_R3DI:
  6343. r3di_gpio_shutdown(codec);
  6344. break;
  6345. }
  6346. snd_hda_sequence_write(codec, spec->base_exit_verbs);
  6347. ca0132_exit_chip(codec);
  6348. snd_hda_power_down(codec);
  6349. #ifdef CONFIG_PCI
  6350. if (spec->mem_base)
  6351. pci_iounmap(codec->bus->pci, spec->mem_base);
  6352. #endif
  6353. kfree(spec->spec_init_verbs);
  6354. kfree(codec->spec);
  6355. }
  6356. static void ca0132_reboot_notify(struct hda_codec *codec)
  6357. {
  6358. codec->patch_ops.free(codec);
  6359. }
  6360. #ifdef CONFIG_PM
  6361. static int ca0132_suspend(struct hda_codec *codec)
  6362. {
  6363. struct ca0132_spec *spec = codec->spec;
  6364. cancel_delayed_work_sync(&spec->unsol_hp_work);
  6365. return 0;
  6366. }
  6367. #endif
  6368. static const struct hda_codec_ops ca0132_patch_ops = {
  6369. .build_controls = ca0132_build_controls,
  6370. .build_pcms = ca0132_build_pcms,
  6371. .init = ca0132_init,
  6372. .free = ca0132_free,
  6373. .unsol_event = snd_hda_jack_unsol_event,
  6374. #ifdef CONFIG_PM
  6375. .suspend = ca0132_suspend,
  6376. #endif
  6377. .reboot_notify = ca0132_reboot_notify,
  6378. };
  6379. static void ca0132_config(struct hda_codec *codec)
  6380. {
  6381. struct ca0132_spec *spec = codec->spec;
  6382. spec->dacs[0] = 0x2;
  6383. spec->dacs[1] = 0x3;
  6384. spec->dacs[2] = 0x4;
  6385. spec->multiout.dac_nids = spec->dacs;
  6386. spec->multiout.num_dacs = 3;
  6387. if (!spec->use_alt_functions)
  6388. spec->multiout.max_channels = 2;
  6389. else
  6390. spec->multiout.max_channels = 6;
  6391. switch (spec->quirk) {
  6392. case QUIRK_ALIENWARE:
  6393. codec_dbg(codec, "ca0132_config: QUIRK_ALIENWARE applied.\n");
  6394. snd_hda_apply_pincfgs(codec, alienware_pincfgs);
  6395. spec->num_outputs = 2;
  6396. spec->out_pins[0] = 0x0b; /* speaker out */
  6397. spec->out_pins[1] = 0x0f;
  6398. spec->shared_out_nid = 0x2;
  6399. spec->unsol_tag_hp = 0x0f;
  6400. spec->adcs[0] = 0x7; /* digital mic / analog mic1 */
  6401. spec->adcs[1] = 0x8; /* analog mic2 */
  6402. spec->adcs[2] = 0xa; /* what u hear */
  6403. spec->num_inputs = 3;
  6404. spec->input_pins[0] = 0x12;
  6405. spec->input_pins[1] = 0x11;
  6406. spec->input_pins[2] = 0x13;
  6407. spec->shared_mic_nid = 0x7;
  6408. spec->unsol_tag_amic1 = 0x11;
  6409. break;
  6410. case QUIRK_SBZ:
  6411. case QUIRK_R3D:
  6412. if (spec->quirk == QUIRK_SBZ) {
  6413. codec_dbg(codec, "%s: QUIRK_SBZ applied.\n", __func__);
  6414. snd_hda_apply_pincfgs(codec, sbz_pincfgs);
  6415. }
  6416. if (spec->quirk == QUIRK_R3D) {
  6417. codec_dbg(codec, "%s: QUIRK_R3D applied.\n", __func__);
  6418. snd_hda_apply_pincfgs(codec, r3d_pincfgs);
  6419. }
  6420. spec->num_outputs = 2;
  6421. spec->out_pins[0] = 0x0B; /* Line out */
  6422. spec->out_pins[1] = 0x0F; /* Rear headphone out */
  6423. spec->out_pins[2] = 0x10; /* Front Headphone / Center/LFE*/
  6424. spec->out_pins[3] = 0x11; /* Rear surround */
  6425. spec->shared_out_nid = 0x2;
  6426. spec->unsol_tag_hp = spec->out_pins[1];
  6427. spec->unsol_tag_front_hp = spec->out_pins[2];
  6428. spec->adcs[0] = 0x7; /* Rear Mic / Line-in */
  6429. spec->adcs[1] = 0x8; /* Front Mic, but only if no DSP */
  6430. spec->adcs[2] = 0xa; /* what u hear */
  6431. spec->num_inputs = 2;
  6432. spec->input_pins[0] = 0x12; /* Rear Mic / Line-in */
  6433. spec->input_pins[1] = 0x13; /* What U Hear */
  6434. spec->shared_mic_nid = 0x7;
  6435. spec->unsol_tag_amic1 = spec->input_pins[0];
  6436. /* SPDIF I/O */
  6437. spec->dig_out = 0x05;
  6438. spec->multiout.dig_out_nid = spec->dig_out;
  6439. spec->dig_in = 0x09;
  6440. break;
  6441. case QUIRK_R3DI:
  6442. codec_dbg(codec, "%s: QUIRK_R3DI applied.\n", __func__);
  6443. snd_hda_apply_pincfgs(codec, r3di_pincfgs);
  6444. spec->num_outputs = 2;
  6445. spec->out_pins[0] = 0x0B; /* Line out */
  6446. spec->out_pins[1] = 0x0F; /* Rear headphone out */
  6447. spec->out_pins[2] = 0x10; /* Front Headphone / Center/LFE*/
  6448. spec->out_pins[3] = 0x11; /* Rear surround */
  6449. spec->shared_out_nid = 0x2;
  6450. spec->unsol_tag_hp = spec->out_pins[1];
  6451. spec->unsol_tag_front_hp = spec->out_pins[2];
  6452. spec->adcs[0] = 0x07; /* Rear Mic / Line-in */
  6453. spec->adcs[1] = 0x08; /* Front Mic, but only if no DSP */
  6454. spec->adcs[2] = 0x0a; /* what u hear */
  6455. spec->num_inputs = 2;
  6456. spec->input_pins[0] = 0x12; /* Rear Mic / Line-in */
  6457. spec->input_pins[1] = 0x13; /* What U Hear */
  6458. spec->shared_mic_nid = 0x7;
  6459. spec->unsol_tag_amic1 = spec->input_pins[0];
  6460. /* SPDIF I/O */
  6461. spec->dig_out = 0x05;
  6462. spec->multiout.dig_out_nid = spec->dig_out;
  6463. break;
  6464. default:
  6465. spec->num_outputs = 2;
  6466. spec->out_pins[0] = 0x0b; /* speaker out */
  6467. spec->out_pins[1] = 0x10; /* headphone out */
  6468. spec->shared_out_nid = 0x2;
  6469. spec->unsol_tag_hp = spec->out_pins[1];
  6470. spec->adcs[0] = 0x7; /* digital mic / analog mic1 */
  6471. spec->adcs[1] = 0x8; /* analog mic2 */
  6472. spec->adcs[2] = 0xa; /* what u hear */
  6473. spec->num_inputs = 3;
  6474. spec->input_pins[0] = 0x12;
  6475. spec->input_pins[1] = 0x11;
  6476. spec->input_pins[2] = 0x13;
  6477. spec->shared_mic_nid = 0x7;
  6478. spec->unsol_tag_amic1 = spec->input_pins[0];
  6479. /* SPDIF I/O */
  6480. spec->dig_out = 0x05;
  6481. spec->multiout.dig_out_nid = spec->dig_out;
  6482. spec->dig_in = 0x09;
  6483. break;
  6484. }
  6485. }
  6486. static int ca0132_prepare_verbs(struct hda_codec *codec)
  6487. {
  6488. /* Verbs + terminator (an empty element) */
  6489. #define NUM_SPEC_VERBS 2
  6490. struct ca0132_spec *spec = codec->spec;
  6491. spec->chip_init_verbs = ca0132_init_verbs0;
  6492. if (spec->quirk == QUIRK_SBZ || spec->quirk == QUIRK_R3D)
  6493. spec->desktop_init_verbs = ca0132_init_verbs1;
  6494. spec->spec_init_verbs = kcalloc(NUM_SPEC_VERBS,
  6495. sizeof(struct hda_verb),
  6496. GFP_KERNEL);
  6497. if (!spec->spec_init_verbs)
  6498. return -ENOMEM;
  6499. /* config EAPD */
  6500. spec->spec_init_verbs[0].nid = 0x0b;
  6501. spec->spec_init_verbs[0].param = 0x78D;
  6502. spec->spec_init_verbs[0].verb = 0x00;
  6503. /* Previously commented configuration */
  6504. /*
  6505. spec->spec_init_verbs[2].nid = 0x0b;
  6506. spec->spec_init_verbs[2].param = AC_VERB_SET_EAPD_BTLENABLE;
  6507. spec->spec_init_verbs[2].verb = 0x02;
  6508. spec->spec_init_verbs[3].nid = 0x10;
  6509. spec->spec_init_verbs[3].param = 0x78D;
  6510. spec->spec_init_verbs[3].verb = 0x02;
  6511. spec->spec_init_verbs[4].nid = 0x10;
  6512. spec->spec_init_verbs[4].param = AC_VERB_SET_EAPD_BTLENABLE;
  6513. spec->spec_init_verbs[4].verb = 0x02;
  6514. */
  6515. /* Terminator: spec->spec_init_verbs[NUM_SPEC_VERBS-1] */
  6516. return 0;
  6517. }
  6518. static int patch_ca0132(struct hda_codec *codec)
  6519. {
  6520. struct ca0132_spec *spec;
  6521. int err;
  6522. const struct snd_pci_quirk *quirk;
  6523. codec_dbg(codec, "patch_ca0132\n");
  6524. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  6525. if (!spec)
  6526. return -ENOMEM;
  6527. codec->spec = spec;
  6528. spec->codec = codec;
  6529. codec->patch_ops = ca0132_patch_ops;
  6530. codec->pcm_format_first = 1;
  6531. codec->no_sticky_stream = 1;
  6532. /* Detect codec quirk */
  6533. quirk = snd_pci_quirk_lookup(codec->bus->pci, ca0132_quirks);
  6534. if (quirk)
  6535. spec->quirk = quirk->value;
  6536. else
  6537. spec->quirk = QUIRK_NONE;
  6538. spec->dsp_state = DSP_DOWNLOAD_INIT;
  6539. spec->num_mixers = 1;
  6540. /* Set which mixers each quirk uses. */
  6541. switch (spec->quirk) {
  6542. case QUIRK_SBZ:
  6543. spec->mixers[0] = desktop_mixer;
  6544. snd_hda_codec_set_name(codec, "Sound Blaster Z");
  6545. break;
  6546. case QUIRK_R3D:
  6547. spec->mixers[0] = desktop_mixer;
  6548. snd_hda_codec_set_name(codec, "Recon3D");
  6549. break;
  6550. case QUIRK_R3DI:
  6551. spec->mixers[0] = r3di_mixer;
  6552. snd_hda_codec_set_name(codec, "Recon3Di");
  6553. break;
  6554. default:
  6555. spec->mixers[0] = ca0132_mixer;
  6556. break;
  6557. }
  6558. /* Setup whether or not to use alt functions/controls/pci_mmio */
  6559. switch (spec->quirk) {
  6560. case QUIRK_SBZ:
  6561. case QUIRK_R3D:
  6562. spec->use_alt_controls = true;
  6563. spec->use_alt_functions = true;
  6564. spec->use_pci_mmio = true;
  6565. break;
  6566. case QUIRK_R3DI:
  6567. spec->use_alt_controls = true;
  6568. spec->use_alt_functions = true;
  6569. spec->use_pci_mmio = false;
  6570. break;
  6571. default:
  6572. spec->use_alt_controls = false;
  6573. spec->use_alt_functions = false;
  6574. spec->use_pci_mmio = false;
  6575. break;
  6576. }
  6577. if (spec->use_pci_mmio) {
  6578. spec->mem_base = pci_iomap(codec->bus->pci, 2, 0xC20);
  6579. if (spec->mem_base == NULL) {
  6580. codec_warn(codec, "pci_iomap failed! Setting quirk to QUIRK_NONE.");
  6581. spec->quirk = QUIRK_NONE;
  6582. }
  6583. }
  6584. spec->base_init_verbs = ca0132_base_init_verbs;
  6585. spec->base_exit_verbs = ca0132_base_exit_verbs;
  6586. INIT_DELAYED_WORK(&spec->unsol_hp_work, ca0132_unsol_hp_delayed);
  6587. ca0132_init_chip(codec);
  6588. ca0132_config(codec);
  6589. err = ca0132_prepare_verbs(codec);
  6590. if (err < 0)
  6591. goto error;
  6592. err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
  6593. if (err < 0)
  6594. goto error;
  6595. return 0;
  6596. error:
  6597. ca0132_free(codec);
  6598. return err;
  6599. }
  6600. /*
  6601. * patch entries
  6602. */
  6603. static struct hda_device_id snd_hda_id_ca0132[] = {
  6604. HDA_CODEC_ENTRY(0x11020011, "CA0132", patch_ca0132),
  6605. {} /* terminator */
  6606. };
  6607. MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_ca0132);
  6608. MODULE_LICENSE("GPL");
  6609. MODULE_DESCRIPTION("Creative Sound Core3D codec");
  6610. static struct hda_codec_driver ca0132_driver = {
  6611. .id = snd_hda_id_ca0132,
  6612. };
  6613. module_hda_codec_driver(ca0132_driver);