patch_ca0132.c 278 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * HD audio interface patch for Creative CA0132 chip
  4. *
  5. * Copyright (c) 2011, Creative Technology Ltd.
  6. *
  7. * Based on patch_ca0110.c
  8. * Copyright (c) 2008 Takashi Iwai <[email protected]>
  9. */
  10. #include <linux/init.h>
  11. #include <linux/delay.h>
  12. #include <linux/slab.h>
  13. #include <linux/mutex.h>
  14. #include <linux/module.h>
  15. #include <linux/firmware.h>
  16. #include <linux/kernel.h>
  17. #include <linux/types.h>
  18. #include <linux/io.h>
  19. #include <linux/pci.h>
  20. #include <asm/io.h>
  21. #include <sound/core.h>
  22. #include <sound/hda_codec.h>
  23. #include "hda_local.h"
  24. #include "hda_auto_parser.h"
  25. #include "hda_jack.h"
  26. #include "ca0132_regs.h"
  27. /* Enable this to see controls for tuning purpose. */
  28. /*#define ENABLE_TUNING_CONTROLS*/
  29. #ifdef ENABLE_TUNING_CONTROLS
  30. #include <sound/tlv.h>
  31. #endif
  32. #define FLOAT_ZERO 0x00000000
  33. #define FLOAT_ONE 0x3f800000
  34. #define FLOAT_TWO 0x40000000
  35. #define FLOAT_THREE 0x40400000
  36. #define FLOAT_FIVE 0x40a00000
  37. #define FLOAT_SIX 0x40c00000
  38. #define FLOAT_EIGHT 0x41000000
  39. #define FLOAT_MINUS_5 0xc0a00000
  40. #define UNSOL_TAG_DSP 0x16
  41. #define DSP_DMA_WRITE_BUFLEN_INIT (1UL<<18)
  42. #define DSP_DMA_WRITE_BUFLEN_OVLY (1UL<<15)
  43. #define DMA_TRANSFER_FRAME_SIZE_NWORDS 8
  44. #define DMA_TRANSFER_MAX_FRAME_SIZE_NWORDS 32
  45. #define DMA_OVERLAY_FRAME_SIZE_NWORDS 2
  46. #define MASTERCONTROL 0x80
  47. #define MASTERCONTROL_ALLOC_DMA_CHAN 10
  48. #define MASTERCONTROL_QUERY_SPEAKER_EQ_ADDRESS 60
  49. #define WIDGET_CHIP_CTRL 0x15
  50. #define WIDGET_DSP_CTRL 0x16
  51. #define MEM_CONNID_MICIN1 3
  52. #define MEM_CONNID_MICIN2 5
  53. #define MEM_CONNID_MICOUT1 12
  54. #define MEM_CONNID_MICOUT2 14
  55. #define MEM_CONNID_WUH 10
  56. #define MEM_CONNID_DSP 16
  57. #define MEM_CONNID_DMIC 100
  58. #define SCP_SET 0
  59. #define SCP_GET 1
  60. #define EFX_FILE "ctefx.bin"
  61. #define DESKTOP_EFX_FILE "ctefx-desktop.bin"
  62. #define R3DI_EFX_FILE "ctefx-r3di.bin"
  63. #ifdef CONFIG_SND_HDA_CODEC_CA0132_DSP
  64. MODULE_FIRMWARE(EFX_FILE);
  65. MODULE_FIRMWARE(DESKTOP_EFX_FILE);
  66. MODULE_FIRMWARE(R3DI_EFX_FILE);
  67. #endif
  68. static const char *const dirstr[2] = { "Playback", "Capture" };
  69. #define NUM_OF_OUTPUTS 2
  70. static const char *const out_type_str[2] = { "Speakers", "Headphone" };
  71. enum {
  72. SPEAKER_OUT,
  73. HEADPHONE_OUT,
  74. };
  75. enum {
  76. DIGITAL_MIC,
  77. LINE_MIC_IN
  78. };
  79. /* Strings for Input Source Enum Control */
  80. static const char *const in_src_str[3] = { "Microphone", "Line In", "Front Microphone" };
  81. #define IN_SRC_NUM_OF_INPUTS 3
  82. enum {
  83. REAR_MIC,
  84. REAR_LINE_IN,
  85. FRONT_MIC,
  86. };
  87. enum {
  88. #define VNODE_START_NID 0x80
  89. VNID_SPK = VNODE_START_NID, /* Speaker vnid */
  90. VNID_MIC,
  91. VNID_HP_SEL,
  92. VNID_AMIC1_SEL,
  93. VNID_HP_ASEL,
  94. VNID_AMIC1_ASEL,
  95. VNODE_END_NID,
  96. #define VNODES_COUNT (VNODE_END_NID - VNODE_START_NID)
  97. #define EFFECT_START_NID 0x90
  98. #define OUT_EFFECT_START_NID EFFECT_START_NID
  99. SURROUND = OUT_EFFECT_START_NID,
  100. CRYSTALIZER,
  101. DIALOG_PLUS,
  102. SMART_VOLUME,
  103. X_BASS,
  104. EQUALIZER,
  105. OUT_EFFECT_END_NID,
  106. #define OUT_EFFECTS_COUNT (OUT_EFFECT_END_NID - OUT_EFFECT_START_NID)
  107. #define IN_EFFECT_START_NID OUT_EFFECT_END_NID
  108. ECHO_CANCELLATION = IN_EFFECT_START_NID,
  109. VOICE_FOCUS,
  110. MIC_SVM,
  111. NOISE_REDUCTION,
  112. IN_EFFECT_END_NID,
  113. #define IN_EFFECTS_COUNT (IN_EFFECT_END_NID - IN_EFFECT_START_NID)
  114. VOICEFX = IN_EFFECT_END_NID,
  115. PLAY_ENHANCEMENT,
  116. CRYSTAL_VOICE,
  117. EFFECT_END_NID,
  118. OUTPUT_SOURCE_ENUM,
  119. INPUT_SOURCE_ENUM,
  120. XBASS_XOVER,
  121. EQ_PRESET_ENUM,
  122. SMART_VOLUME_ENUM,
  123. MIC_BOOST_ENUM,
  124. AE5_HEADPHONE_GAIN_ENUM,
  125. AE5_SOUND_FILTER_ENUM,
  126. ZXR_HEADPHONE_GAIN,
  127. SPEAKER_CHANNEL_CFG_ENUM,
  128. SPEAKER_FULL_RANGE_FRONT,
  129. SPEAKER_FULL_RANGE_REAR,
  130. BASS_REDIRECTION,
  131. BASS_REDIRECTION_XOVER,
  132. #define EFFECTS_COUNT (EFFECT_END_NID - EFFECT_START_NID)
  133. };
  134. /* Effects values size*/
  135. #define EFFECT_VALS_MAX_COUNT 12
  136. /*
  137. * Default values for the effect slider controls, they are in order of their
  138. * effect NID's. Surround, Crystalizer, Dialog Plus, Smart Volume, and then
  139. * X-bass.
  140. */
  141. static const unsigned int effect_slider_defaults[] = {67, 65, 50, 74, 50};
  142. /* Amount of effect level sliders for ca0132_alt controls. */
  143. #define EFFECT_LEVEL_SLIDERS 5
  144. /* Latency introduced by DSP blocks in milliseconds. */
  145. #define DSP_CAPTURE_INIT_LATENCY 0
  146. #define DSP_CRYSTAL_VOICE_LATENCY 124
  147. #define DSP_PLAYBACK_INIT_LATENCY 13
  148. #define DSP_PLAY_ENHANCEMENT_LATENCY 30
  149. #define DSP_SPEAKER_OUT_LATENCY 7
  150. struct ct_effect {
  151. char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
  152. hda_nid_t nid;
  153. int mid; /*effect module ID*/
  154. int reqs[EFFECT_VALS_MAX_COUNT]; /*effect module request*/
  155. int direct; /* 0:output; 1:input*/
  156. int params; /* number of default non-on/off params */
  157. /*effect default values, 1st is on/off. */
  158. unsigned int def_vals[EFFECT_VALS_MAX_COUNT];
  159. };
  160. #define EFX_DIR_OUT 0
  161. #define EFX_DIR_IN 1
  162. static const struct ct_effect ca0132_effects[EFFECTS_COUNT] = {
  163. { .name = "Surround",
  164. .nid = SURROUND,
  165. .mid = 0x96,
  166. .reqs = {0, 1},
  167. .direct = EFX_DIR_OUT,
  168. .params = 1,
  169. .def_vals = {0x3F800000, 0x3F2B851F}
  170. },
  171. { .name = "Crystalizer",
  172. .nid = CRYSTALIZER,
  173. .mid = 0x96,
  174. .reqs = {7, 8},
  175. .direct = EFX_DIR_OUT,
  176. .params = 1,
  177. .def_vals = {0x3F800000, 0x3F266666}
  178. },
  179. { .name = "Dialog Plus",
  180. .nid = DIALOG_PLUS,
  181. .mid = 0x96,
  182. .reqs = {2, 3},
  183. .direct = EFX_DIR_OUT,
  184. .params = 1,
  185. .def_vals = {0x00000000, 0x3F000000}
  186. },
  187. { .name = "Smart Volume",
  188. .nid = SMART_VOLUME,
  189. .mid = 0x96,
  190. .reqs = {4, 5, 6},
  191. .direct = EFX_DIR_OUT,
  192. .params = 2,
  193. .def_vals = {0x3F800000, 0x3F3D70A4, 0x00000000}
  194. },
  195. { .name = "X-Bass",
  196. .nid = X_BASS,
  197. .mid = 0x96,
  198. .reqs = {24, 23, 25},
  199. .direct = EFX_DIR_OUT,
  200. .params = 2,
  201. .def_vals = {0x3F800000, 0x42A00000, 0x3F000000}
  202. },
  203. { .name = "Equalizer",
  204. .nid = EQUALIZER,
  205. .mid = 0x96,
  206. .reqs = {9, 10, 11, 12, 13, 14,
  207. 15, 16, 17, 18, 19, 20},
  208. .direct = EFX_DIR_OUT,
  209. .params = 11,
  210. .def_vals = {0x00000000, 0x00000000, 0x00000000, 0x00000000,
  211. 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  212. 0x00000000, 0x00000000, 0x00000000, 0x00000000}
  213. },
  214. { .name = "Echo Cancellation",
  215. .nid = ECHO_CANCELLATION,
  216. .mid = 0x95,
  217. .reqs = {0, 1, 2, 3},
  218. .direct = EFX_DIR_IN,
  219. .params = 3,
  220. .def_vals = {0x00000000, 0x3F3A9692, 0x00000000, 0x00000000}
  221. },
  222. { .name = "Voice Focus",
  223. .nid = VOICE_FOCUS,
  224. .mid = 0x95,
  225. .reqs = {6, 7, 8, 9},
  226. .direct = EFX_DIR_IN,
  227. .params = 3,
  228. .def_vals = {0x3F800000, 0x3D7DF3B6, 0x41F00000, 0x41F00000}
  229. },
  230. { .name = "Mic SVM",
  231. .nid = MIC_SVM,
  232. .mid = 0x95,
  233. .reqs = {44, 45},
  234. .direct = EFX_DIR_IN,
  235. .params = 1,
  236. .def_vals = {0x00000000, 0x3F3D70A4}
  237. },
  238. { .name = "Noise Reduction",
  239. .nid = NOISE_REDUCTION,
  240. .mid = 0x95,
  241. .reqs = {4, 5},
  242. .direct = EFX_DIR_IN,
  243. .params = 1,
  244. .def_vals = {0x3F800000, 0x3F000000}
  245. },
  246. { .name = "VoiceFX",
  247. .nid = VOICEFX,
  248. .mid = 0x95,
  249. .reqs = {10, 11, 12, 13, 14, 15, 16, 17, 18},
  250. .direct = EFX_DIR_IN,
  251. .params = 8,
  252. .def_vals = {0x00000000, 0x43C80000, 0x44AF0000, 0x44FA0000,
  253. 0x3F800000, 0x3F800000, 0x3F800000, 0x00000000,
  254. 0x00000000}
  255. }
  256. };
  257. /* Tuning controls */
  258. #ifdef ENABLE_TUNING_CONTROLS
  259. enum {
  260. #define TUNING_CTL_START_NID 0xC0
  261. WEDGE_ANGLE = TUNING_CTL_START_NID,
  262. SVM_LEVEL,
  263. EQUALIZER_BAND_0,
  264. EQUALIZER_BAND_1,
  265. EQUALIZER_BAND_2,
  266. EQUALIZER_BAND_3,
  267. EQUALIZER_BAND_4,
  268. EQUALIZER_BAND_5,
  269. EQUALIZER_BAND_6,
  270. EQUALIZER_BAND_7,
  271. EQUALIZER_BAND_8,
  272. EQUALIZER_BAND_9,
  273. TUNING_CTL_END_NID
  274. #define TUNING_CTLS_COUNT (TUNING_CTL_END_NID - TUNING_CTL_START_NID)
  275. };
  276. struct ct_tuning_ctl {
  277. char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
  278. hda_nid_t parent_nid;
  279. hda_nid_t nid;
  280. int mid; /*effect module ID*/
  281. int req; /*effect module request*/
  282. int direct; /* 0:output; 1:input*/
  283. unsigned int def_val;/*effect default values*/
  284. };
  285. static const struct ct_tuning_ctl ca0132_tuning_ctls[] = {
  286. { .name = "Wedge Angle",
  287. .parent_nid = VOICE_FOCUS,
  288. .nid = WEDGE_ANGLE,
  289. .mid = 0x95,
  290. .req = 8,
  291. .direct = EFX_DIR_IN,
  292. .def_val = 0x41F00000
  293. },
  294. { .name = "SVM Level",
  295. .parent_nid = MIC_SVM,
  296. .nid = SVM_LEVEL,
  297. .mid = 0x95,
  298. .req = 45,
  299. .direct = EFX_DIR_IN,
  300. .def_val = 0x3F3D70A4
  301. },
  302. { .name = "EQ Band0",
  303. .parent_nid = EQUALIZER,
  304. .nid = EQUALIZER_BAND_0,
  305. .mid = 0x96,
  306. .req = 11,
  307. .direct = EFX_DIR_OUT,
  308. .def_val = 0x00000000
  309. },
  310. { .name = "EQ Band1",
  311. .parent_nid = EQUALIZER,
  312. .nid = EQUALIZER_BAND_1,
  313. .mid = 0x96,
  314. .req = 12,
  315. .direct = EFX_DIR_OUT,
  316. .def_val = 0x00000000
  317. },
  318. { .name = "EQ Band2",
  319. .parent_nid = EQUALIZER,
  320. .nid = EQUALIZER_BAND_2,
  321. .mid = 0x96,
  322. .req = 13,
  323. .direct = EFX_DIR_OUT,
  324. .def_val = 0x00000000
  325. },
  326. { .name = "EQ Band3",
  327. .parent_nid = EQUALIZER,
  328. .nid = EQUALIZER_BAND_3,
  329. .mid = 0x96,
  330. .req = 14,
  331. .direct = EFX_DIR_OUT,
  332. .def_val = 0x00000000
  333. },
  334. { .name = "EQ Band4",
  335. .parent_nid = EQUALIZER,
  336. .nid = EQUALIZER_BAND_4,
  337. .mid = 0x96,
  338. .req = 15,
  339. .direct = EFX_DIR_OUT,
  340. .def_val = 0x00000000
  341. },
  342. { .name = "EQ Band5",
  343. .parent_nid = EQUALIZER,
  344. .nid = EQUALIZER_BAND_5,
  345. .mid = 0x96,
  346. .req = 16,
  347. .direct = EFX_DIR_OUT,
  348. .def_val = 0x00000000
  349. },
  350. { .name = "EQ Band6",
  351. .parent_nid = EQUALIZER,
  352. .nid = EQUALIZER_BAND_6,
  353. .mid = 0x96,
  354. .req = 17,
  355. .direct = EFX_DIR_OUT,
  356. .def_val = 0x00000000
  357. },
  358. { .name = "EQ Band7",
  359. .parent_nid = EQUALIZER,
  360. .nid = EQUALIZER_BAND_7,
  361. .mid = 0x96,
  362. .req = 18,
  363. .direct = EFX_DIR_OUT,
  364. .def_val = 0x00000000
  365. },
  366. { .name = "EQ Band8",
  367. .parent_nid = EQUALIZER,
  368. .nid = EQUALIZER_BAND_8,
  369. .mid = 0x96,
  370. .req = 19,
  371. .direct = EFX_DIR_OUT,
  372. .def_val = 0x00000000
  373. },
  374. { .name = "EQ Band9",
  375. .parent_nid = EQUALIZER,
  376. .nid = EQUALIZER_BAND_9,
  377. .mid = 0x96,
  378. .req = 20,
  379. .direct = EFX_DIR_OUT,
  380. .def_val = 0x00000000
  381. }
  382. };
  383. #endif
  384. /* Voice FX Presets */
  385. #define VOICEFX_MAX_PARAM_COUNT 9
  386. struct ct_voicefx {
  387. char *name;
  388. hda_nid_t nid;
  389. int mid;
  390. int reqs[VOICEFX_MAX_PARAM_COUNT]; /*effect module request*/
  391. };
  392. struct ct_voicefx_preset {
  393. char *name; /*preset name*/
  394. unsigned int vals[VOICEFX_MAX_PARAM_COUNT];
  395. };
  396. static const struct ct_voicefx ca0132_voicefx = {
  397. .name = "VoiceFX Capture Switch",
  398. .nid = VOICEFX,
  399. .mid = 0x95,
  400. .reqs = {10, 11, 12, 13, 14, 15, 16, 17, 18}
  401. };
  402. static const struct ct_voicefx_preset ca0132_voicefx_presets[] = {
  403. { .name = "Neutral",
  404. .vals = { 0x00000000, 0x43C80000, 0x44AF0000,
  405. 0x44FA0000, 0x3F800000, 0x3F800000,
  406. 0x3F800000, 0x00000000, 0x00000000 }
  407. },
  408. { .name = "Female2Male",
  409. .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
  410. 0x44FA0000, 0x3F19999A, 0x3F866666,
  411. 0x3F800000, 0x00000000, 0x00000000 }
  412. },
  413. { .name = "Male2Female",
  414. .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
  415. 0x450AC000, 0x4017AE14, 0x3F6B851F,
  416. 0x3F800000, 0x00000000, 0x00000000 }
  417. },
  418. { .name = "ScrappyKid",
  419. .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
  420. 0x44FA0000, 0x40400000, 0x3F28F5C3,
  421. 0x3F800000, 0x00000000, 0x00000000 }
  422. },
  423. { .name = "Elderly",
  424. .vals = { 0x3F800000, 0x44324000, 0x44BB8000,
  425. 0x44E10000, 0x3FB33333, 0x3FB9999A,
  426. 0x3F800000, 0x3E3A2E43, 0x00000000 }
  427. },
  428. { .name = "Orc",
  429. .vals = { 0x3F800000, 0x43EA0000, 0x44A52000,
  430. 0x45098000, 0x3F266666, 0x3FC00000,
  431. 0x3F800000, 0x00000000, 0x00000000 }
  432. },
  433. { .name = "Elf",
  434. .vals = { 0x3F800000, 0x43C70000, 0x44AE6000,
  435. 0x45193000, 0x3F8E147B, 0x3F75C28F,
  436. 0x3F800000, 0x00000000, 0x00000000 }
  437. },
  438. { .name = "Dwarf",
  439. .vals = { 0x3F800000, 0x43930000, 0x44BEE000,
  440. 0x45007000, 0x3F451EB8, 0x3F7851EC,
  441. 0x3F800000, 0x00000000, 0x00000000 }
  442. },
  443. { .name = "AlienBrute",
  444. .vals = { 0x3F800000, 0x43BFC5AC, 0x44B28FDF,
  445. 0x451F6000, 0x3F266666, 0x3FA7D945,
  446. 0x3F800000, 0x3CF5C28F, 0x00000000 }
  447. },
  448. { .name = "Robot",
  449. .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
  450. 0x44FA0000, 0x3FB2718B, 0x3F800000,
  451. 0xBC07010E, 0x00000000, 0x00000000 }
  452. },
  453. { .name = "Marine",
  454. .vals = { 0x3F800000, 0x43C20000, 0x44906000,
  455. 0x44E70000, 0x3F4CCCCD, 0x3F8A3D71,
  456. 0x3F0A3D71, 0x00000000, 0x00000000 }
  457. },
  458. { .name = "Emo",
  459. .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
  460. 0x44FA0000, 0x3F800000, 0x3F800000,
  461. 0x3E4CCCCD, 0x00000000, 0x00000000 }
  462. },
  463. { .name = "DeepVoice",
  464. .vals = { 0x3F800000, 0x43A9C5AC, 0x44AA4FDF,
  465. 0x44FFC000, 0x3EDBB56F, 0x3F99C4CA,
  466. 0x3F800000, 0x00000000, 0x00000000 }
  467. },
  468. { .name = "Munchkin",
  469. .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
  470. 0x44FA0000, 0x3F800000, 0x3F1A043C,
  471. 0x3F800000, 0x00000000, 0x00000000 }
  472. }
  473. };
  474. /* ca0132 EQ presets, taken from Windows Sound Blaster Z Driver */
  475. #define EQ_PRESET_MAX_PARAM_COUNT 11
  476. struct ct_eq {
  477. char *name;
  478. hda_nid_t nid;
  479. int mid;
  480. int reqs[EQ_PRESET_MAX_PARAM_COUNT]; /*effect module request*/
  481. };
  482. struct ct_eq_preset {
  483. char *name; /*preset name*/
  484. unsigned int vals[EQ_PRESET_MAX_PARAM_COUNT];
  485. };
  486. static const struct ct_eq ca0132_alt_eq_enum = {
  487. .name = "FX: Equalizer Preset Switch",
  488. .nid = EQ_PRESET_ENUM,
  489. .mid = 0x96,
  490. .reqs = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
  491. };
  492. static const struct ct_eq_preset ca0132_alt_eq_presets[] = {
  493. { .name = "Flat",
  494. .vals = { 0x00000000, 0x00000000, 0x00000000,
  495. 0x00000000, 0x00000000, 0x00000000,
  496. 0x00000000, 0x00000000, 0x00000000,
  497. 0x00000000, 0x00000000 }
  498. },
  499. { .name = "Acoustic",
  500. .vals = { 0x00000000, 0x00000000, 0x3F8CCCCD,
  501. 0x40000000, 0x00000000, 0x00000000,
  502. 0x00000000, 0x00000000, 0x40000000,
  503. 0x40000000, 0x40000000 }
  504. },
  505. { .name = "Classical",
  506. .vals = { 0x00000000, 0x00000000, 0x40C00000,
  507. 0x40C00000, 0x40466666, 0x00000000,
  508. 0x00000000, 0x00000000, 0x00000000,
  509. 0x40466666, 0x40466666 }
  510. },
  511. { .name = "Country",
  512. .vals = { 0x00000000, 0xBF99999A, 0x00000000,
  513. 0x3FA66666, 0x3FA66666, 0x3F8CCCCD,
  514. 0x00000000, 0x00000000, 0x40000000,
  515. 0x40466666, 0x40800000 }
  516. },
  517. { .name = "Dance",
  518. .vals = { 0x00000000, 0xBF99999A, 0x40000000,
  519. 0x40466666, 0x40866666, 0xBF99999A,
  520. 0xBF99999A, 0x00000000, 0x00000000,
  521. 0x40800000, 0x40800000 }
  522. },
  523. { .name = "Jazz",
  524. .vals = { 0x00000000, 0x00000000, 0x00000000,
  525. 0x3F8CCCCD, 0x40800000, 0x40800000,
  526. 0x40800000, 0x00000000, 0x3F8CCCCD,
  527. 0x40466666, 0x40466666 }
  528. },
  529. { .name = "New Age",
  530. .vals = { 0x00000000, 0x00000000, 0x40000000,
  531. 0x40000000, 0x00000000, 0x00000000,
  532. 0x00000000, 0x3F8CCCCD, 0x40000000,
  533. 0x40000000, 0x40000000 }
  534. },
  535. { .name = "Pop",
  536. .vals = { 0x00000000, 0xBFCCCCCD, 0x00000000,
  537. 0x40000000, 0x40000000, 0x00000000,
  538. 0xBF99999A, 0xBF99999A, 0x00000000,
  539. 0x40466666, 0x40C00000 }
  540. },
  541. { .name = "Rock",
  542. .vals = { 0x00000000, 0xBF99999A, 0xBF99999A,
  543. 0x3F8CCCCD, 0x40000000, 0xBF99999A,
  544. 0xBF99999A, 0x00000000, 0x00000000,
  545. 0x40800000, 0x40800000 }
  546. },
  547. { .name = "Vocal",
  548. .vals = { 0x00000000, 0xC0000000, 0xBF99999A,
  549. 0xBF99999A, 0x00000000, 0x40466666,
  550. 0x40800000, 0x40466666, 0x00000000,
  551. 0x00000000, 0x3F8CCCCD }
  552. }
  553. };
  554. /*
  555. * DSP reqs for handling full-range speakers/bass redirection. If a speaker is
  556. * set as not being full range, and bass redirection is enabled, all
  557. * frequencies below the crossover frequency are redirected to the LFE
  558. * channel. If the surround configuration has no LFE channel, this can't be
  559. * enabled. X-Bass must be disabled when using these.
  560. */
  561. enum speaker_range_reqs {
  562. SPEAKER_BASS_REDIRECT = 0x15,
  563. SPEAKER_BASS_REDIRECT_XOVER_FREQ = 0x16,
  564. /* Between 0x16-0x1a are the X-Bass reqs. */
  565. SPEAKER_FULL_RANGE_FRONT_L_R = 0x1a,
  566. SPEAKER_FULL_RANGE_CENTER_LFE = 0x1b,
  567. SPEAKER_FULL_RANGE_REAR_L_R = 0x1c,
  568. SPEAKER_FULL_RANGE_SURROUND_L_R = 0x1d,
  569. SPEAKER_BASS_REDIRECT_SUB_GAIN = 0x1e,
  570. };
  571. /*
  572. * Definitions for the DSP req's to handle speaker tuning. These all belong to
  573. * module ID 0x96, the output effects module.
  574. */
  575. enum speaker_tuning_reqs {
  576. /*
  577. * Currently, this value is always set to 0.0f. However, on Windows,
  578. * when selecting certain headphone profiles on the new Sound Blaster
  579. * connect software, the QUERY_SPEAKER_EQ_ADDRESS req on mid 0x80 is
  580. * sent. This gets the speaker EQ address area, which is then used to
  581. * send over (presumably) an equalizer profile for the specific
  582. * headphone setup. It is sent using the same method the DSP
  583. * firmware is uploaded with, which I believe is why the 'ctspeq.bin'
  584. * file exists in linux firmware tree but goes unused. It would also
  585. * explain why the QUERY_SPEAKER_EQ_ADDRESS req is defined but unused.
  586. * Once this profile is sent over, SPEAKER_TUNING_USE_SPEAKER_EQ is
  587. * set to 1.0f.
  588. */
  589. SPEAKER_TUNING_USE_SPEAKER_EQ = 0x1f,
  590. SPEAKER_TUNING_ENABLE_CENTER_EQ = 0x20,
  591. SPEAKER_TUNING_FRONT_LEFT_VOL_LEVEL = 0x21,
  592. SPEAKER_TUNING_FRONT_RIGHT_VOL_LEVEL = 0x22,
  593. SPEAKER_TUNING_CENTER_VOL_LEVEL = 0x23,
  594. SPEAKER_TUNING_LFE_VOL_LEVEL = 0x24,
  595. SPEAKER_TUNING_REAR_LEFT_VOL_LEVEL = 0x25,
  596. SPEAKER_TUNING_REAR_RIGHT_VOL_LEVEL = 0x26,
  597. SPEAKER_TUNING_SURROUND_LEFT_VOL_LEVEL = 0x27,
  598. SPEAKER_TUNING_SURROUND_RIGHT_VOL_LEVEL = 0x28,
  599. /*
  600. * Inversion is used when setting headphone virtualization to line
  601. * out. Not sure why this is, but it's the only place it's ever used.
  602. */
  603. SPEAKER_TUNING_FRONT_LEFT_INVERT = 0x29,
  604. SPEAKER_TUNING_FRONT_RIGHT_INVERT = 0x2a,
  605. SPEAKER_TUNING_CENTER_INVERT = 0x2b,
  606. SPEAKER_TUNING_LFE_INVERT = 0x2c,
  607. SPEAKER_TUNING_REAR_LEFT_INVERT = 0x2d,
  608. SPEAKER_TUNING_REAR_RIGHT_INVERT = 0x2e,
  609. SPEAKER_TUNING_SURROUND_LEFT_INVERT = 0x2f,
  610. SPEAKER_TUNING_SURROUND_RIGHT_INVERT = 0x30,
  611. /* Delay is used when setting surround speaker distance in Windows. */
  612. SPEAKER_TUNING_FRONT_LEFT_DELAY = 0x31,
  613. SPEAKER_TUNING_FRONT_RIGHT_DELAY = 0x32,
  614. SPEAKER_TUNING_CENTER_DELAY = 0x33,
  615. SPEAKER_TUNING_LFE_DELAY = 0x34,
  616. SPEAKER_TUNING_REAR_LEFT_DELAY = 0x35,
  617. SPEAKER_TUNING_REAR_RIGHT_DELAY = 0x36,
  618. SPEAKER_TUNING_SURROUND_LEFT_DELAY = 0x37,
  619. SPEAKER_TUNING_SURROUND_RIGHT_DELAY = 0x38,
  620. /* Of these two, only mute seems to ever be used. */
  621. SPEAKER_TUNING_MAIN_VOLUME = 0x39,
  622. SPEAKER_TUNING_MUTE = 0x3a,
  623. };
  624. /* Surround output channel count configuration structures. */
  625. #define SPEAKER_CHANNEL_CFG_COUNT 5
  626. enum {
  627. SPEAKER_CHANNELS_2_0,
  628. SPEAKER_CHANNELS_2_1,
  629. SPEAKER_CHANNELS_4_0,
  630. SPEAKER_CHANNELS_4_1,
  631. SPEAKER_CHANNELS_5_1,
  632. };
  633. struct ca0132_alt_speaker_channel_cfg {
  634. char *name;
  635. unsigned int val;
  636. };
  637. static const struct ca0132_alt_speaker_channel_cfg speaker_channel_cfgs[] = {
  638. { .name = "2.0",
  639. .val = FLOAT_ONE
  640. },
  641. { .name = "2.1",
  642. .val = FLOAT_TWO
  643. },
  644. { .name = "4.0",
  645. .val = FLOAT_FIVE
  646. },
  647. { .name = "4.1",
  648. .val = FLOAT_SIX
  649. },
  650. { .name = "5.1",
  651. .val = FLOAT_EIGHT
  652. }
  653. };
  654. /*
  655. * DSP volume setting structs. Req 1 is left volume, req 2 is right volume,
  656. * and I don't know what the third req is, but it's always zero. I assume it's
  657. * some sort of update or set command to tell the DSP there's new volume info.
  658. */
  659. #define DSP_VOL_OUT 0
  660. #define DSP_VOL_IN 1
  661. struct ct_dsp_volume_ctl {
  662. hda_nid_t vnid;
  663. int mid; /* module ID*/
  664. unsigned int reqs[3]; /* scp req ID */
  665. };
  666. static const struct ct_dsp_volume_ctl ca0132_alt_vol_ctls[] = {
  667. { .vnid = VNID_SPK,
  668. .mid = 0x32,
  669. .reqs = {3, 4, 2}
  670. },
  671. { .vnid = VNID_MIC,
  672. .mid = 0x37,
  673. .reqs = {2, 3, 1}
  674. }
  675. };
  676. /* Values for ca0113_mmio_command_set for selecting output. */
  677. #define AE_CA0113_OUT_SET_COMMANDS 6
  678. struct ae_ca0113_output_set {
  679. unsigned int group[AE_CA0113_OUT_SET_COMMANDS];
  680. unsigned int target[AE_CA0113_OUT_SET_COMMANDS];
  681. unsigned int vals[NUM_OF_OUTPUTS][AE_CA0113_OUT_SET_COMMANDS];
  682. };
  683. static const struct ae_ca0113_output_set ae5_ca0113_output_presets = {
  684. .group = { 0x30, 0x30, 0x48, 0x48, 0x48, 0x30 },
  685. .target = { 0x2e, 0x30, 0x0d, 0x17, 0x19, 0x32 },
  686. /* Speakers. */
  687. .vals = { { 0x00, 0x00, 0x40, 0x00, 0x00, 0x3f },
  688. /* Headphones. */
  689. { 0x3f, 0x3f, 0x00, 0x00, 0x00, 0x00 } },
  690. };
  691. static const struct ae_ca0113_output_set ae7_ca0113_output_presets = {
  692. .group = { 0x30, 0x30, 0x48, 0x48, 0x48, 0x30 },
  693. .target = { 0x2e, 0x30, 0x0d, 0x17, 0x19, 0x32 },
  694. /* Speakers. */
  695. .vals = { { 0x00, 0x00, 0x40, 0x00, 0x00, 0x3f },
  696. /* Headphones. */
  697. { 0x3f, 0x3f, 0x00, 0x00, 0x02, 0x00 } },
  698. };
  699. /* ae5 ca0113 command sequences to set headphone gain levels. */
  700. #define AE5_HEADPHONE_GAIN_PRESET_MAX_COMMANDS 4
  701. struct ae5_headphone_gain_set {
  702. char *name;
  703. unsigned int vals[AE5_HEADPHONE_GAIN_PRESET_MAX_COMMANDS];
  704. };
  705. static const struct ae5_headphone_gain_set ae5_headphone_gain_presets[] = {
  706. { .name = "Low (16-31",
  707. .vals = { 0xff, 0x2c, 0xf5, 0x32 }
  708. },
  709. { .name = "Medium (32-149",
  710. .vals = { 0x38, 0xa8, 0x3e, 0x4c }
  711. },
  712. { .name = "High (150-600",
  713. .vals = { 0xff, 0xff, 0xff, 0x7f }
  714. }
  715. };
  716. struct ae5_filter_set {
  717. char *name;
  718. unsigned int val;
  719. };
  720. static const struct ae5_filter_set ae5_filter_presets[] = {
  721. { .name = "Slow Roll Off",
  722. .val = 0xa0
  723. },
  724. { .name = "Minimum Phase",
  725. .val = 0xc0
  726. },
  727. { .name = "Fast Roll Off",
  728. .val = 0x80
  729. }
  730. };
  731. /*
  732. * Data structures for storing audio router remapping data. These are used to
  733. * remap a currently active streams ports.
  734. */
  735. struct chipio_stream_remap_data {
  736. unsigned int stream_id;
  737. unsigned int count;
  738. unsigned int offset[16];
  739. unsigned int value[16];
  740. };
  741. static const struct chipio_stream_remap_data stream_remap_data[] = {
  742. { .stream_id = 0x14,
  743. .count = 0x04,
  744. .offset = { 0x00, 0x04, 0x08, 0x0c },
  745. .value = { 0x0001f8c0, 0x0001f9c1, 0x0001fac6, 0x0001fbc7 },
  746. },
  747. { .stream_id = 0x0c,
  748. .count = 0x0c,
  749. .offset = { 0x00, 0x04, 0x08, 0x0c, 0x10, 0x14, 0x18, 0x1c,
  750. 0x20, 0x24, 0x28, 0x2c },
  751. .value = { 0x0001e0c0, 0x0001e1c1, 0x0001e4c2, 0x0001e5c3,
  752. 0x0001e2c4, 0x0001e3c5, 0x0001e8c6, 0x0001e9c7,
  753. 0x0001ecc8, 0x0001edc9, 0x0001eaca, 0x0001ebcb },
  754. },
  755. { .stream_id = 0x0c,
  756. .count = 0x08,
  757. .offset = { 0x08, 0x0c, 0x10, 0x14, 0x20, 0x24, 0x28, 0x2c },
  758. .value = { 0x000140c2, 0x000141c3, 0x000150c4, 0x000151c5,
  759. 0x000142c8, 0x000143c9, 0x000152ca, 0x000153cb },
  760. }
  761. };
  762. enum hda_cmd_vendor_io {
  763. /* for DspIO node */
  764. VENDOR_DSPIO_SCP_WRITE_DATA_LOW = 0x000,
  765. VENDOR_DSPIO_SCP_WRITE_DATA_HIGH = 0x100,
  766. VENDOR_DSPIO_STATUS = 0xF01,
  767. VENDOR_DSPIO_SCP_POST_READ_DATA = 0x702,
  768. VENDOR_DSPIO_SCP_READ_DATA = 0xF02,
  769. VENDOR_DSPIO_DSP_INIT = 0x703,
  770. VENDOR_DSPIO_SCP_POST_COUNT_QUERY = 0x704,
  771. VENDOR_DSPIO_SCP_READ_COUNT = 0xF04,
  772. /* for ChipIO node */
  773. VENDOR_CHIPIO_ADDRESS_LOW = 0x000,
  774. VENDOR_CHIPIO_ADDRESS_HIGH = 0x100,
  775. VENDOR_CHIPIO_STREAM_FORMAT = 0x200,
  776. VENDOR_CHIPIO_DATA_LOW = 0x300,
  777. VENDOR_CHIPIO_DATA_HIGH = 0x400,
  778. VENDOR_CHIPIO_8051_WRITE_DIRECT = 0x500,
  779. VENDOR_CHIPIO_8051_READ_DIRECT = 0xD00,
  780. VENDOR_CHIPIO_GET_PARAMETER = 0xF00,
  781. VENDOR_CHIPIO_STATUS = 0xF01,
  782. VENDOR_CHIPIO_HIC_POST_READ = 0x702,
  783. VENDOR_CHIPIO_HIC_READ_DATA = 0xF03,
  784. VENDOR_CHIPIO_8051_DATA_WRITE = 0x707,
  785. VENDOR_CHIPIO_8051_DATA_READ = 0xF07,
  786. VENDOR_CHIPIO_8051_PMEM_READ = 0xF08,
  787. VENDOR_CHIPIO_8051_IRAM_WRITE = 0x709,
  788. VENDOR_CHIPIO_8051_IRAM_READ = 0xF09,
  789. VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE = 0x70A,
  790. VENDOR_CHIPIO_CT_EXTENSIONS_GET = 0xF0A,
  791. VENDOR_CHIPIO_PLL_PMU_WRITE = 0x70C,
  792. VENDOR_CHIPIO_PLL_PMU_READ = 0xF0C,
  793. VENDOR_CHIPIO_8051_ADDRESS_LOW = 0x70D,
  794. VENDOR_CHIPIO_8051_ADDRESS_HIGH = 0x70E,
  795. VENDOR_CHIPIO_FLAG_SET = 0x70F,
  796. VENDOR_CHIPIO_FLAGS_GET = 0xF0F,
  797. VENDOR_CHIPIO_PARAM_SET = 0x710,
  798. VENDOR_CHIPIO_PARAM_GET = 0xF10,
  799. VENDOR_CHIPIO_PORT_ALLOC_CONFIG_SET = 0x711,
  800. VENDOR_CHIPIO_PORT_ALLOC_SET = 0x712,
  801. VENDOR_CHIPIO_PORT_ALLOC_GET = 0xF12,
  802. VENDOR_CHIPIO_PORT_FREE_SET = 0x713,
  803. VENDOR_CHIPIO_PARAM_EX_ID_GET = 0xF17,
  804. VENDOR_CHIPIO_PARAM_EX_ID_SET = 0x717,
  805. VENDOR_CHIPIO_PARAM_EX_VALUE_GET = 0xF18,
  806. VENDOR_CHIPIO_PARAM_EX_VALUE_SET = 0x718,
  807. VENDOR_CHIPIO_DMIC_CTL_SET = 0x788,
  808. VENDOR_CHIPIO_DMIC_CTL_GET = 0xF88,
  809. VENDOR_CHIPIO_DMIC_PIN_SET = 0x789,
  810. VENDOR_CHIPIO_DMIC_PIN_GET = 0xF89,
  811. VENDOR_CHIPIO_DMIC_MCLK_SET = 0x78A,
  812. VENDOR_CHIPIO_DMIC_MCLK_GET = 0xF8A,
  813. VENDOR_CHIPIO_EAPD_SEL_SET = 0x78D
  814. };
  815. /*
  816. * Control flag IDs
  817. */
  818. enum control_flag_id {
  819. /* Connection manager stream setup is bypassed/enabled */
  820. CONTROL_FLAG_C_MGR = 0,
  821. /* DSP DMA is bypassed/enabled */
  822. CONTROL_FLAG_DMA = 1,
  823. /* 8051 'idle' mode is disabled/enabled */
  824. CONTROL_FLAG_IDLE_ENABLE = 2,
  825. /* Tracker for the SPDIF-in path is bypassed/enabled */
  826. CONTROL_FLAG_TRACKER = 3,
  827. /* DigitalOut to Spdif2Out connection is disabled/enabled */
  828. CONTROL_FLAG_SPDIF2OUT = 4,
  829. /* Digital Microphone is disabled/enabled */
  830. CONTROL_FLAG_DMIC = 5,
  831. /* ADC_B rate is 48 kHz/96 kHz */
  832. CONTROL_FLAG_ADC_B_96KHZ = 6,
  833. /* ADC_C rate is 48 kHz/96 kHz */
  834. CONTROL_FLAG_ADC_C_96KHZ = 7,
  835. /* DAC rate is 48 kHz/96 kHz (affects all DACs) */
  836. CONTROL_FLAG_DAC_96KHZ = 8,
  837. /* DSP rate is 48 kHz/96 kHz */
  838. CONTROL_FLAG_DSP_96KHZ = 9,
  839. /* SRC clock is 98 MHz/196 MHz (196 MHz forces rate to 96 KHz) */
  840. CONTROL_FLAG_SRC_CLOCK_196MHZ = 10,
  841. /* SRC rate is 48 kHz/96 kHz (48 kHz disabled when clock is 196 MHz) */
  842. CONTROL_FLAG_SRC_RATE_96KHZ = 11,
  843. /* Decode Loop (DSP->SRC->DSP) is disabled/enabled */
  844. CONTROL_FLAG_DECODE_LOOP = 12,
  845. /* De-emphasis filter on DAC-1 disabled/enabled */
  846. CONTROL_FLAG_DAC1_DEEMPHASIS = 13,
  847. /* De-emphasis filter on DAC-2 disabled/enabled */
  848. CONTROL_FLAG_DAC2_DEEMPHASIS = 14,
  849. /* De-emphasis filter on DAC-3 disabled/enabled */
  850. CONTROL_FLAG_DAC3_DEEMPHASIS = 15,
  851. /* High-pass filter on ADC_B disabled/enabled */
  852. CONTROL_FLAG_ADC_B_HIGH_PASS = 16,
  853. /* High-pass filter on ADC_C disabled/enabled */
  854. CONTROL_FLAG_ADC_C_HIGH_PASS = 17,
  855. /* Common mode on Port_A disabled/enabled */
  856. CONTROL_FLAG_PORT_A_COMMON_MODE = 18,
  857. /* Common mode on Port_D disabled/enabled */
  858. CONTROL_FLAG_PORT_D_COMMON_MODE = 19,
  859. /* Impedance for ramp generator on Port_A 16 Ohm/10K Ohm */
  860. CONTROL_FLAG_PORT_A_10KOHM_LOAD = 20,
  861. /* Impedance for ramp generator on Port_D, 16 Ohm/10K Ohm */
  862. CONTROL_FLAG_PORT_D_10KOHM_LOAD = 21,
  863. /* ASI rate is 48kHz/96kHz */
  864. CONTROL_FLAG_ASI_96KHZ = 22,
  865. /* DAC power settings able to control attached ports no/yes */
  866. CONTROL_FLAG_DACS_CONTROL_PORTS = 23,
  867. /* Clock Stop OK reporting is disabled/enabled */
  868. CONTROL_FLAG_CONTROL_STOP_OK_ENABLE = 24,
  869. /* Number of control flags */
  870. CONTROL_FLAGS_MAX = (CONTROL_FLAG_CONTROL_STOP_OK_ENABLE+1)
  871. };
  872. /*
  873. * Control parameter IDs
  874. */
  875. enum control_param_id {
  876. /* 0: None, 1: Mic1In*/
  877. CONTROL_PARAM_VIP_SOURCE = 1,
  878. /* 0: force HDA, 1: allow DSP if HDA Spdif1Out stream is idle */
  879. CONTROL_PARAM_SPDIF1_SOURCE = 2,
  880. /* Port A output stage gain setting to use when 16 Ohm output
  881. * impedance is selected*/
  882. CONTROL_PARAM_PORTA_160OHM_GAIN = 8,
  883. /* Port D output stage gain setting to use when 16 Ohm output
  884. * impedance is selected*/
  885. CONTROL_PARAM_PORTD_160OHM_GAIN = 10,
  886. /*
  887. * This control param name was found in the 8051 memory, and makes
  888. * sense given the fact the AE-5 uses it and has the ASI flag set.
  889. */
  890. CONTROL_PARAM_ASI = 23,
  891. /* Stream Control */
  892. /* Select stream with the given ID */
  893. CONTROL_PARAM_STREAM_ID = 24,
  894. /* Source connection point for the selected stream */
  895. CONTROL_PARAM_STREAM_SOURCE_CONN_POINT = 25,
  896. /* Destination connection point for the selected stream */
  897. CONTROL_PARAM_STREAM_DEST_CONN_POINT = 26,
  898. /* Number of audio channels in the selected stream */
  899. CONTROL_PARAM_STREAMS_CHANNELS = 27,
  900. /*Enable control for the selected stream */
  901. CONTROL_PARAM_STREAM_CONTROL = 28,
  902. /* Connection Point Control */
  903. /* Select connection point with the given ID */
  904. CONTROL_PARAM_CONN_POINT_ID = 29,
  905. /* Connection point sample rate */
  906. CONTROL_PARAM_CONN_POINT_SAMPLE_RATE = 30,
  907. /* Node Control */
  908. /* Select HDA node with the given ID */
  909. CONTROL_PARAM_NODE_ID = 31
  910. };
  911. /*
  912. * Dsp Io Status codes
  913. */
  914. enum hda_vendor_status_dspio {
  915. /* Success */
  916. VENDOR_STATUS_DSPIO_OK = 0x00,
  917. /* Busy, unable to accept new command, the host must retry */
  918. VENDOR_STATUS_DSPIO_BUSY = 0x01,
  919. /* SCP command queue is full */
  920. VENDOR_STATUS_DSPIO_SCP_COMMAND_QUEUE_FULL = 0x02,
  921. /* SCP response queue is empty */
  922. VENDOR_STATUS_DSPIO_SCP_RESPONSE_QUEUE_EMPTY = 0x03
  923. };
  924. /*
  925. * Chip Io Status codes
  926. */
  927. enum hda_vendor_status_chipio {
  928. /* Success */
  929. VENDOR_STATUS_CHIPIO_OK = 0x00,
  930. /* Busy, unable to accept new command, the host must retry */
  931. VENDOR_STATUS_CHIPIO_BUSY = 0x01
  932. };
  933. /*
  934. * CA0132 sample rate
  935. */
  936. enum ca0132_sample_rate {
  937. SR_6_000 = 0x00,
  938. SR_8_000 = 0x01,
  939. SR_9_600 = 0x02,
  940. SR_11_025 = 0x03,
  941. SR_16_000 = 0x04,
  942. SR_22_050 = 0x05,
  943. SR_24_000 = 0x06,
  944. SR_32_000 = 0x07,
  945. SR_44_100 = 0x08,
  946. SR_48_000 = 0x09,
  947. SR_88_200 = 0x0A,
  948. SR_96_000 = 0x0B,
  949. SR_144_000 = 0x0C,
  950. SR_176_400 = 0x0D,
  951. SR_192_000 = 0x0E,
  952. SR_384_000 = 0x0F,
  953. SR_COUNT = 0x10,
  954. SR_RATE_UNKNOWN = 0x1F
  955. };
  956. enum dsp_download_state {
  957. DSP_DOWNLOAD_FAILED = -1,
  958. DSP_DOWNLOAD_INIT = 0,
  959. DSP_DOWNLOADING = 1,
  960. DSP_DOWNLOADED = 2
  961. };
  962. /* retrieve parameters from hda format */
  963. #define get_hdafmt_chs(fmt) (fmt & 0xf)
  964. #define get_hdafmt_bits(fmt) ((fmt >> 4) & 0x7)
  965. #define get_hdafmt_rate(fmt) ((fmt >> 8) & 0x7f)
  966. #define get_hdafmt_type(fmt) ((fmt >> 15) & 0x1)
  967. /*
  968. * CA0132 specific
  969. */
  970. struct ca0132_spec {
  971. const struct snd_kcontrol_new *mixers[5];
  972. unsigned int num_mixers;
  973. const struct hda_verb *base_init_verbs;
  974. const struct hda_verb *base_exit_verbs;
  975. const struct hda_verb *chip_init_verbs;
  976. const struct hda_verb *desktop_init_verbs;
  977. struct hda_verb *spec_init_verbs;
  978. struct auto_pin_cfg autocfg;
  979. /* Nodes configurations */
  980. struct hda_multi_out multiout;
  981. hda_nid_t out_pins[AUTO_CFG_MAX_OUTS];
  982. hda_nid_t dacs[AUTO_CFG_MAX_OUTS];
  983. unsigned int num_outputs;
  984. hda_nid_t input_pins[AUTO_PIN_LAST];
  985. hda_nid_t adcs[AUTO_PIN_LAST];
  986. hda_nid_t dig_out;
  987. hda_nid_t dig_in;
  988. unsigned int num_inputs;
  989. hda_nid_t shared_mic_nid;
  990. hda_nid_t shared_out_nid;
  991. hda_nid_t unsol_tag_hp;
  992. hda_nid_t unsol_tag_front_hp; /* for desktop ca0132 codecs */
  993. hda_nid_t unsol_tag_amic1;
  994. /* chip access */
  995. struct mutex chipio_mutex; /* chip access mutex */
  996. u32 curr_chip_addx;
  997. /* DSP download related */
  998. enum dsp_download_state dsp_state;
  999. unsigned int dsp_stream_id;
  1000. unsigned int wait_scp;
  1001. unsigned int wait_scp_header;
  1002. unsigned int wait_num_data;
  1003. unsigned int scp_resp_header;
  1004. unsigned int scp_resp_data[4];
  1005. unsigned int scp_resp_count;
  1006. bool startup_check_entered;
  1007. bool dsp_reload;
  1008. /* mixer and effects related */
  1009. unsigned char dmic_ctl;
  1010. int cur_out_type;
  1011. int cur_mic_type;
  1012. long vnode_lvol[VNODES_COUNT];
  1013. long vnode_rvol[VNODES_COUNT];
  1014. long vnode_lswitch[VNODES_COUNT];
  1015. long vnode_rswitch[VNODES_COUNT];
  1016. long effects_switch[EFFECTS_COUNT];
  1017. long voicefx_val;
  1018. long cur_mic_boost;
  1019. /* ca0132_alt control related values */
  1020. unsigned char in_enum_val;
  1021. unsigned char out_enum_val;
  1022. unsigned char channel_cfg_val;
  1023. unsigned char speaker_range_val[2];
  1024. unsigned char mic_boost_enum_val;
  1025. unsigned char smart_volume_setting;
  1026. unsigned char bass_redirection_val;
  1027. long bass_redirect_xover_freq;
  1028. long fx_ctl_val[EFFECT_LEVEL_SLIDERS];
  1029. long xbass_xover_freq;
  1030. long eq_preset_val;
  1031. unsigned int tlv[4];
  1032. struct hda_vmaster_mute_hook vmaster_mute;
  1033. /* AE-5 Control values */
  1034. unsigned char ae5_headphone_gain_val;
  1035. unsigned char ae5_filter_val;
  1036. /* ZxR Control Values */
  1037. unsigned char zxr_gain_set;
  1038. struct hda_codec *codec;
  1039. struct delayed_work unsol_hp_work;
  1040. int quirk;
  1041. #ifdef ENABLE_TUNING_CONTROLS
  1042. long cur_ctl_vals[TUNING_CTLS_COUNT];
  1043. #endif
  1044. /*
  1045. * The Recon3D, Sound Blaster Z, Sound Blaster ZxR, and Sound Blaster
  1046. * AE-5 all use PCI region 2 to toggle GPIO and other currently unknown
  1047. * things.
  1048. */
  1049. bool use_pci_mmio;
  1050. void __iomem *mem_base;
  1051. /*
  1052. * Whether or not to use the alt functions like alt_select_out,
  1053. * alt_select_in, etc. Only used on desktop codecs for now, because of
  1054. * surround sound support.
  1055. */
  1056. bool use_alt_functions;
  1057. /*
  1058. * Whether or not to use alt controls: volume effect sliders, EQ
  1059. * presets, smart volume presets, and new control names with FX prefix.
  1060. * Renames PlayEnhancement and CrystalVoice too.
  1061. */
  1062. bool use_alt_controls;
  1063. };
  1064. /*
  1065. * CA0132 quirks table
  1066. */
  1067. enum {
  1068. QUIRK_NONE,
  1069. QUIRK_ALIENWARE,
  1070. QUIRK_ALIENWARE_M17XR4,
  1071. QUIRK_SBZ,
  1072. QUIRK_ZXR,
  1073. QUIRK_ZXR_DBPRO,
  1074. QUIRK_R3DI,
  1075. QUIRK_R3D,
  1076. QUIRK_AE5,
  1077. QUIRK_AE7,
  1078. };
  1079. #ifdef CONFIG_PCI
  1080. #define ca0132_quirk(spec) ((spec)->quirk)
  1081. #define ca0132_use_pci_mmio(spec) ((spec)->use_pci_mmio)
  1082. #define ca0132_use_alt_functions(spec) ((spec)->use_alt_functions)
  1083. #define ca0132_use_alt_controls(spec) ((spec)->use_alt_controls)
  1084. #else
  1085. #define ca0132_quirk(spec) ({ (void)(spec); QUIRK_NONE; })
  1086. #define ca0132_use_alt_functions(spec) ({ (void)(spec); false; })
  1087. #define ca0132_use_pci_mmio(spec) ({ (void)(spec); false; })
  1088. #define ca0132_use_alt_controls(spec) ({ (void)(spec); false; })
  1089. #endif
  1090. static const struct hda_pintbl alienware_pincfgs[] = {
  1091. { 0x0b, 0x90170110 }, /* Builtin Speaker */
  1092. { 0x0c, 0x411111f0 }, /* N/A */
  1093. { 0x0d, 0x411111f0 }, /* N/A */
  1094. { 0x0e, 0x411111f0 }, /* N/A */
  1095. { 0x0f, 0x0321101f }, /* HP */
  1096. { 0x10, 0x411111f0 }, /* Headset? disabled for now */
  1097. { 0x11, 0x03a11021 }, /* Mic */
  1098. { 0x12, 0xd5a30140 }, /* Builtin Mic */
  1099. { 0x13, 0x411111f0 }, /* N/A */
  1100. { 0x18, 0x411111f0 }, /* N/A */
  1101. {}
  1102. };
  1103. /* Sound Blaster Z pin configs taken from Windows Driver */
  1104. static const struct hda_pintbl sbz_pincfgs[] = {
  1105. { 0x0b, 0x01017010 }, /* Port G -- Lineout FRONT L/R */
  1106. { 0x0c, 0x014510f0 }, /* SPDIF Out 1 */
  1107. { 0x0d, 0x014510f0 }, /* Digital Out */
  1108. { 0x0e, 0x01c510f0 }, /* SPDIF In */
  1109. { 0x0f, 0x0221701f }, /* Port A -- BackPanel HP */
  1110. { 0x10, 0x01017012 }, /* Port D -- Center/LFE or FP Hp */
  1111. { 0x11, 0x01017014 }, /* Port B -- LineMicIn2 / Rear L/R */
  1112. { 0x12, 0x01a170f0 }, /* Port C -- LineIn1 */
  1113. { 0x13, 0x908700f0 }, /* What U Hear In*/
  1114. { 0x18, 0x50d000f0 }, /* N/A */
  1115. {}
  1116. };
  1117. /* Sound Blaster ZxR pin configs taken from Windows Driver */
  1118. static const struct hda_pintbl zxr_pincfgs[] = {
  1119. { 0x0b, 0x01047110 }, /* Port G -- Lineout FRONT L/R */
  1120. { 0x0c, 0x414510f0 }, /* SPDIF Out 1 - Disabled*/
  1121. { 0x0d, 0x014510f0 }, /* Digital Out */
  1122. { 0x0e, 0x41c520f0 }, /* SPDIF In - Disabled*/
  1123. { 0x0f, 0x0122711f }, /* Port A -- BackPanel HP */
  1124. { 0x10, 0x01017111 }, /* Port D -- Center/LFE */
  1125. { 0x11, 0x01017114 }, /* Port B -- LineMicIn2 / Rear L/R */
  1126. { 0x12, 0x01a271f0 }, /* Port C -- LineIn1 */
  1127. { 0x13, 0x908700f0 }, /* What U Hear In*/
  1128. { 0x18, 0x50d000f0 }, /* N/A */
  1129. {}
  1130. };
  1131. /* Recon3D pin configs taken from Windows Driver */
  1132. static const struct hda_pintbl r3d_pincfgs[] = {
  1133. { 0x0b, 0x01014110 }, /* Port G -- Lineout FRONT L/R */
  1134. { 0x0c, 0x014510f0 }, /* SPDIF Out 1 */
  1135. { 0x0d, 0x014510f0 }, /* Digital Out */
  1136. { 0x0e, 0x01c520f0 }, /* SPDIF In */
  1137. { 0x0f, 0x0221401f }, /* Port A -- BackPanel HP */
  1138. { 0x10, 0x01016011 }, /* Port D -- Center/LFE or FP Hp */
  1139. { 0x11, 0x01011014 }, /* Port B -- LineMicIn2 / Rear L/R */
  1140. { 0x12, 0x02a090f0 }, /* Port C -- LineIn1 */
  1141. { 0x13, 0x908700f0 }, /* What U Hear In*/
  1142. { 0x18, 0x50d000f0 }, /* N/A */
  1143. {}
  1144. };
  1145. /* Sound Blaster AE-5 pin configs taken from Windows Driver */
  1146. static const struct hda_pintbl ae5_pincfgs[] = {
  1147. { 0x0b, 0x01017010 }, /* Port G -- Lineout FRONT L/R */
  1148. { 0x0c, 0x014510f0 }, /* SPDIF Out 1 */
  1149. { 0x0d, 0x014510f0 }, /* Digital Out */
  1150. { 0x0e, 0x01c510f0 }, /* SPDIF In */
  1151. { 0x0f, 0x01017114 }, /* Port A -- Rear L/R. */
  1152. { 0x10, 0x01017012 }, /* Port D -- Center/LFE or FP Hp */
  1153. { 0x11, 0x012170ff }, /* Port B -- LineMicIn2 / Rear Headphone */
  1154. { 0x12, 0x01a170f0 }, /* Port C -- LineIn1 */
  1155. { 0x13, 0x908700f0 }, /* What U Hear In*/
  1156. { 0x18, 0x50d000f0 }, /* N/A */
  1157. {}
  1158. };
  1159. /* Recon3D integrated pin configs taken from Windows Driver */
  1160. static const struct hda_pintbl r3di_pincfgs[] = {
  1161. { 0x0b, 0x01014110 }, /* Port G -- Lineout FRONT L/R */
  1162. { 0x0c, 0x014510f0 }, /* SPDIF Out 1 */
  1163. { 0x0d, 0x014510f0 }, /* Digital Out */
  1164. { 0x0e, 0x41c520f0 }, /* SPDIF In */
  1165. { 0x0f, 0x0221401f }, /* Port A -- BackPanel HP */
  1166. { 0x10, 0x01016011 }, /* Port D -- Center/LFE or FP Hp */
  1167. { 0x11, 0x01011014 }, /* Port B -- LineMicIn2 / Rear L/R */
  1168. { 0x12, 0x02a090f0 }, /* Port C -- LineIn1 */
  1169. { 0x13, 0x908700f0 }, /* What U Hear In*/
  1170. { 0x18, 0x500000f0 }, /* N/A */
  1171. {}
  1172. };
  1173. static const struct hda_pintbl ae7_pincfgs[] = {
  1174. { 0x0b, 0x01017010 },
  1175. { 0x0c, 0x014510f0 },
  1176. { 0x0d, 0x414510f0 },
  1177. { 0x0e, 0x01c520f0 },
  1178. { 0x0f, 0x01017114 },
  1179. { 0x10, 0x01017011 },
  1180. { 0x11, 0x018170ff },
  1181. { 0x12, 0x01a170f0 },
  1182. { 0x13, 0x908700f0 },
  1183. { 0x18, 0x500000f0 },
  1184. {}
  1185. };
  1186. static const struct snd_pci_quirk ca0132_quirks[] = {
  1187. SND_PCI_QUIRK(0x1028, 0x057b, "Alienware M17x R4", QUIRK_ALIENWARE_M17XR4),
  1188. SND_PCI_QUIRK(0x1028, 0x0685, "Alienware 15 2015", QUIRK_ALIENWARE),
  1189. SND_PCI_QUIRK(0x1028, 0x0688, "Alienware 17 2015", QUIRK_ALIENWARE),
  1190. SND_PCI_QUIRK(0x1028, 0x0708, "Alienware 15 R2 2016", QUIRK_ALIENWARE),
  1191. SND_PCI_QUIRK(0x1102, 0x0010, "Sound Blaster Z", QUIRK_SBZ),
  1192. SND_PCI_QUIRK(0x1102, 0x0023, "Sound Blaster Z", QUIRK_SBZ),
  1193. SND_PCI_QUIRK(0x1102, 0x0027, "Sound Blaster Z", QUIRK_SBZ),
  1194. SND_PCI_QUIRK(0x1102, 0x0033, "Sound Blaster ZxR", QUIRK_SBZ),
  1195. SND_PCI_QUIRK(0x1458, 0xA016, "Recon3Di", QUIRK_R3DI),
  1196. SND_PCI_QUIRK(0x1458, 0xA026, "Gigabyte G1.Sniper Z97", QUIRK_R3DI),
  1197. SND_PCI_QUIRK(0x1458, 0xA036, "Gigabyte GA-Z170X-Gaming 7", QUIRK_R3DI),
  1198. SND_PCI_QUIRK(0x3842, 0x1038, "EVGA X99 Classified", QUIRK_R3DI),
  1199. SND_PCI_QUIRK(0x3842, 0x104b, "EVGA X299 Dark", QUIRK_R3DI),
  1200. SND_PCI_QUIRK(0x3842, 0x1055, "EVGA Z390 DARK", QUIRK_R3DI),
  1201. SND_PCI_QUIRK(0x1102, 0x0013, "Recon3D", QUIRK_R3D),
  1202. SND_PCI_QUIRK(0x1102, 0x0018, "Recon3D", QUIRK_R3D),
  1203. SND_PCI_QUIRK(0x1102, 0x0051, "Sound Blaster AE-5", QUIRK_AE5),
  1204. SND_PCI_QUIRK(0x1102, 0x0191, "Sound Blaster AE-5 Plus", QUIRK_AE5),
  1205. SND_PCI_QUIRK(0x1102, 0x0081, "Sound Blaster AE-7", QUIRK_AE7),
  1206. {}
  1207. };
  1208. /* Output selection quirk info structures. */
  1209. #define MAX_QUIRK_MMIO_GPIO_SET_VALS 3
  1210. #define MAX_QUIRK_SCP_SET_VALS 2
  1211. struct ca0132_alt_out_set_info {
  1212. unsigned int dac2port; /* ParamID 0x0d value. */
  1213. bool has_hda_gpio;
  1214. char hda_gpio_pin;
  1215. char hda_gpio_set;
  1216. unsigned int mmio_gpio_count;
  1217. char mmio_gpio_pin[MAX_QUIRK_MMIO_GPIO_SET_VALS];
  1218. char mmio_gpio_set[MAX_QUIRK_MMIO_GPIO_SET_VALS];
  1219. unsigned int scp_cmds_count;
  1220. unsigned int scp_cmd_mid[MAX_QUIRK_SCP_SET_VALS];
  1221. unsigned int scp_cmd_req[MAX_QUIRK_SCP_SET_VALS];
  1222. unsigned int scp_cmd_val[MAX_QUIRK_SCP_SET_VALS];
  1223. bool has_chipio_write;
  1224. unsigned int chipio_write_addr;
  1225. unsigned int chipio_write_data;
  1226. };
  1227. struct ca0132_alt_out_set_quirk_data {
  1228. int quirk_id;
  1229. bool has_headphone_gain;
  1230. bool is_ae_series;
  1231. struct ca0132_alt_out_set_info out_set_info[NUM_OF_OUTPUTS];
  1232. };
  1233. static const struct ca0132_alt_out_set_quirk_data quirk_out_set_data[] = {
  1234. { .quirk_id = QUIRK_R3DI,
  1235. .has_headphone_gain = false,
  1236. .is_ae_series = false,
  1237. .out_set_info = {
  1238. /* Speakers. */
  1239. { .dac2port = 0x24,
  1240. .has_hda_gpio = true,
  1241. .hda_gpio_pin = 2,
  1242. .hda_gpio_set = 1,
  1243. .mmio_gpio_count = 0,
  1244. .scp_cmds_count = 0,
  1245. .has_chipio_write = false,
  1246. },
  1247. /* Headphones. */
  1248. { .dac2port = 0x21,
  1249. .has_hda_gpio = true,
  1250. .hda_gpio_pin = 2,
  1251. .hda_gpio_set = 0,
  1252. .mmio_gpio_count = 0,
  1253. .scp_cmds_count = 0,
  1254. .has_chipio_write = false,
  1255. } },
  1256. },
  1257. { .quirk_id = QUIRK_R3D,
  1258. .has_headphone_gain = false,
  1259. .is_ae_series = false,
  1260. .out_set_info = {
  1261. /* Speakers. */
  1262. { .dac2port = 0x24,
  1263. .has_hda_gpio = false,
  1264. .mmio_gpio_count = 1,
  1265. .mmio_gpio_pin = { 1 },
  1266. .mmio_gpio_set = { 1 },
  1267. .scp_cmds_count = 0,
  1268. .has_chipio_write = false,
  1269. },
  1270. /* Headphones. */
  1271. { .dac2port = 0x21,
  1272. .has_hda_gpio = false,
  1273. .mmio_gpio_count = 1,
  1274. .mmio_gpio_pin = { 1 },
  1275. .mmio_gpio_set = { 0 },
  1276. .scp_cmds_count = 0,
  1277. .has_chipio_write = false,
  1278. } },
  1279. },
  1280. { .quirk_id = QUIRK_SBZ,
  1281. .has_headphone_gain = false,
  1282. .is_ae_series = false,
  1283. .out_set_info = {
  1284. /* Speakers. */
  1285. { .dac2port = 0x18,
  1286. .has_hda_gpio = false,
  1287. .mmio_gpio_count = 3,
  1288. .mmio_gpio_pin = { 7, 4, 1 },
  1289. .mmio_gpio_set = { 0, 1, 1 },
  1290. .scp_cmds_count = 0,
  1291. .has_chipio_write = false, },
  1292. /* Headphones. */
  1293. { .dac2port = 0x12,
  1294. .has_hda_gpio = false,
  1295. .mmio_gpio_count = 3,
  1296. .mmio_gpio_pin = { 7, 4, 1 },
  1297. .mmio_gpio_set = { 1, 1, 0 },
  1298. .scp_cmds_count = 0,
  1299. .has_chipio_write = false,
  1300. } },
  1301. },
  1302. { .quirk_id = QUIRK_ZXR,
  1303. .has_headphone_gain = true,
  1304. .is_ae_series = false,
  1305. .out_set_info = {
  1306. /* Speakers. */
  1307. { .dac2port = 0x24,
  1308. .has_hda_gpio = false,
  1309. .mmio_gpio_count = 3,
  1310. .mmio_gpio_pin = { 2, 3, 5 },
  1311. .mmio_gpio_set = { 1, 1, 0 },
  1312. .scp_cmds_count = 0,
  1313. .has_chipio_write = false,
  1314. },
  1315. /* Headphones. */
  1316. { .dac2port = 0x21,
  1317. .has_hda_gpio = false,
  1318. .mmio_gpio_count = 3,
  1319. .mmio_gpio_pin = { 2, 3, 5 },
  1320. .mmio_gpio_set = { 0, 1, 1 },
  1321. .scp_cmds_count = 0,
  1322. .has_chipio_write = false,
  1323. } },
  1324. },
  1325. { .quirk_id = QUIRK_AE5,
  1326. .has_headphone_gain = true,
  1327. .is_ae_series = true,
  1328. .out_set_info = {
  1329. /* Speakers. */
  1330. { .dac2port = 0xa4,
  1331. .has_hda_gpio = false,
  1332. .mmio_gpio_count = 0,
  1333. .scp_cmds_count = 2,
  1334. .scp_cmd_mid = { 0x96, 0x96 },
  1335. .scp_cmd_req = { SPEAKER_TUNING_FRONT_LEFT_INVERT,
  1336. SPEAKER_TUNING_FRONT_RIGHT_INVERT },
  1337. .scp_cmd_val = { FLOAT_ZERO, FLOAT_ZERO },
  1338. .has_chipio_write = true,
  1339. .chipio_write_addr = 0x0018b03c,
  1340. .chipio_write_data = 0x00000012
  1341. },
  1342. /* Headphones. */
  1343. { .dac2port = 0xa1,
  1344. .has_hda_gpio = false,
  1345. .mmio_gpio_count = 0,
  1346. .scp_cmds_count = 2,
  1347. .scp_cmd_mid = { 0x96, 0x96 },
  1348. .scp_cmd_req = { SPEAKER_TUNING_FRONT_LEFT_INVERT,
  1349. SPEAKER_TUNING_FRONT_RIGHT_INVERT },
  1350. .scp_cmd_val = { FLOAT_ONE, FLOAT_ONE },
  1351. .has_chipio_write = true,
  1352. .chipio_write_addr = 0x0018b03c,
  1353. .chipio_write_data = 0x00000012
  1354. } },
  1355. },
  1356. { .quirk_id = QUIRK_AE7,
  1357. .has_headphone_gain = true,
  1358. .is_ae_series = true,
  1359. .out_set_info = {
  1360. /* Speakers. */
  1361. { .dac2port = 0x58,
  1362. .has_hda_gpio = false,
  1363. .mmio_gpio_count = 1,
  1364. .mmio_gpio_pin = { 0 },
  1365. .mmio_gpio_set = { 1 },
  1366. .scp_cmds_count = 2,
  1367. .scp_cmd_mid = { 0x96, 0x96 },
  1368. .scp_cmd_req = { SPEAKER_TUNING_FRONT_LEFT_INVERT,
  1369. SPEAKER_TUNING_FRONT_RIGHT_INVERT },
  1370. .scp_cmd_val = { FLOAT_ZERO, FLOAT_ZERO },
  1371. .has_chipio_write = true,
  1372. .chipio_write_addr = 0x0018b03c,
  1373. .chipio_write_data = 0x00000000
  1374. },
  1375. /* Headphones. */
  1376. { .dac2port = 0x58,
  1377. .has_hda_gpio = false,
  1378. .mmio_gpio_count = 1,
  1379. .mmio_gpio_pin = { 0 },
  1380. .mmio_gpio_set = { 1 },
  1381. .scp_cmds_count = 2,
  1382. .scp_cmd_mid = { 0x96, 0x96 },
  1383. .scp_cmd_req = { SPEAKER_TUNING_FRONT_LEFT_INVERT,
  1384. SPEAKER_TUNING_FRONT_RIGHT_INVERT },
  1385. .scp_cmd_val = { FLOAT_ONE, FLOAT_ONE },
  1386. .has_chipio_write = true,
  1387. .chipio_write_addr = 0x0018b03c,
  1388. .chipio_write_data = 0x00000010
  1389. } },
  1390. }
  1391. };
  1392. /*
  1393. * CA0132 codec access
  1394. */
  1395. static unsigned int codec_send_command(struct hda_codec *codec, hda_nid_t nid,
  1396. unsigned int verb, unsigned int parm, unsigned int *res)
  1397. {
  1398. unsigned int response;
  1399. response = snd_hda_codec_read(codec, nid, 0, verb, parm);
  1400. *res = response;
  1401. return ((response == -1) ? -1 : 0);
  1402. }
  1403. static int codec_set_converter_format(struct hda_codec *codec, hda_nid_t nid,
  1404. unsigned short converter_format, unsigned int *res)
  1405. {
  1406. return codec_send_command(codec, nid, VENDOR_CHIPIO_STREAM_FORMAT,
  1407. converter_format & 0xffff, res);
  1408. }
  1409. static int codec_set_converter_stream_channel(struct hda_codec *codec,
  1410. hda_nid_t nid, unsigned char stream,
  1411. unsigned char channel, unsigned int *res)
  1412. {
  1413. unsigned char converter_stream_channel = 0;
  1414. converter_stream_channel = (stream << 4) | (channel & 0x0f);
  1415. return codec_send_command(codec, nid, AC_VERB_SET_CHANNEL_STREAMID,
  1416. converter_stream_channel, res);
  1417. }
  1418. /* Chip access helper function */
  1419. static int chipio_send(struct hda_codec *codec,
  1420. unsigned int reg,
  1421. unsigned int data)
  1422. {
  1423. unsigned int res;
  1424. unsigned long timeout = jiffies + msecs_to_jiffies(1000);
  1425. /* send bits of data specified by reg */
  1426. do {
  1427. res = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
  1428. reg, data);
  1429. if (res == VENDOR_STATUS_CHIPIO_OK)
  1430. return 0;
  1431. msleep(20);
  1432. } while (time_before(jiffies, timeout));
  1433. return -EIO;
  1434. }
  1435. /*
  1436. * Write chip address through the vendor widget -- NOT protected by the Mutex!
  1437. */
  1438. static int chipio_write_address(struct hda_codec *codec,
  1439. unsigned int chip_addx)
  1440. {
  1441. struct ca0132_spec *spec = codec->spec;
  1442. int res;
  1443. if (spec->curr_chip_addx == chip_addx)
  1444. return 0;
  1445. /* send low 16 bits of the address */
  1446. res = chipio_send(codec, VENDOR_CHIPIO_ADDRESS_LOW,
  1447. chip_addx & 0xffff);
  1448. if (res != -EIO) {
  1449. /* send high 16 bits of the address */
  1450. res = chipio_send(codec, VENDOR_CHIPIO_ADDRESS_HIGH,
  1451. chip_addx >> 16);
  1452. }
  1453. spec->curr_chip_addx = (res < 0) ? ~0U : chip_addx;
  1454. return res;
  1455. }
  1456. /*
  1457. * Write data through the vendor widget -- NOT protected by the Mutex!
  1458. */
  1459. static int chipio_write_data(struct hda_codec *codec, unsigned int data)
  1460. {
  1461. struct ca0132_spec *spec = codec->spec;
  1462. int res;
  1463. /* send low 16 bits of the data */
  1464. res = chipio_send(codec, VENDOR_CHIPIO_DATA_LOW, data & 0xffff);
  1465. if (res != -EIO) {
  1466. /* send high 16 bits of the data */
  1467. res = chipio_send(codec, VENDOR_CHIPIO_DATA_HIGH,
  1468. data >> 16);
  1469. }
  1470. /*If no error encountered, automatically increment the address
  1471. as per chip behaviour*/
  1472. spec->curr_chip_addx = (res != -EIO) ?
  1473. (spec->curr_chip_addx + 4) : ~0U;
  1474. return res;
  1475. }
  1476. /*
  1477. * Write multiple data through the vendor widget -- NOT protected by the Mutex!
  1478. */
  1479. static int chipio_write_data_multiple(struct hda_codec *codec,
  1480. const u32 *data,
  1481. unsigned int count)
  1482. {
  1483. int status = 0;
  1484. if (data == NULL) {
  1485. codec_dbg(codec, "chipio_write_data null ptr\n");
  1486. return -EINVAL;
  1487. }
  1488. while ((count-- != 0) && (status == 0))
  1489. status = chipio_write_data(codec, *data++);
  1490. return status;
  1491. }
  1492. /*
  1493. * Read data through the vendor widget -- NOT protected by the Mutex!
  1494. */
  1495. static int chipio_read_data(struct hda_codec *codec, unsigned int *data)
  1496. {
  1497. struct ca0132_spec *spec = codec->spec;
  1498. int res;
  1499. /* post read */
  1500. res = chipio_send(codec, VENDOR_CHIPIO_HIC_POST_READ, 0);
  1501. if (res != -EIO) {
  1502. /* read status */
  1503. res = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
  1504. }
  1505. if (res != -EIO) {
  1506. /* read data */
  1507. *data = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
  1508. VENDOR_CHIPIO_HIC_READ_DATA,
  1509. 0);
  1510. }
  1511. /*If no error encountered, automatically increment the address
  1512. as per chip behaviour*/
  1513. spec->curr_chip_addx = (res != -EIO) ?
  1514. (spec->curr_chip_addx + 4) : ~0U;
  1515. return res;
  1516. }
  1517. /*
  1518. * Write given value to the given address through the chip I/O widget.
  1519. * protected by the Mutex
  1520. */
  1521. static int chipio_write(struct hda_codec *codec,
  1522. unsigned int chip_addx, const unsigned int data)
  1523. {
  1524. struct ca0132_spec *spec = codec->spec;
  1525. int err;
  1526. mutex_lock(&spec->chipio_mutex);
  1527. /* write the address, and if successful proceed to write data */
  1528. err = chipio_write_address(codec, chip_addx);
  1529. if (err < 0)
  1530. goto exit;
  1531. err = chipio_write_data(codec, data);
  1532. if (err < 0)
  1533. goto exit;
  1534. exit:
  1535. mutex_unlock(&spec->chipio_mutex);
  1536. return err;
  1537. }
  1538. /*
  1539. * Write given value to the given address through the chip I/O widget.
  1540. * not protected by the Mutex
  1541. */
  1542. static int chipio_write_no_mutex(struct hda_codec *codec,
  1543. unsigned int chip_addx, const unsigned int data)
  1544. {
  1545. int err;
  1546. /* write the address, and if successful proceed to write data */
  1547. err = chipio_write_address(codec, chip_addx);
  1548. if (err < 0)
  1549. goto exit;
  1550. err = chipio_write_data(codec, data);
  1551. if (err < 0)
  1552. goto exit;
  1553. exit:
  1554. return err;
  1555. }
  1556. /*
  1557. * Write multiple values to the given address through the chip I/O widget.
  1558. * protected by the Mutex
  1559. */
  1560. static int chipio_write_multiple(struct hda_codec *codec,
  1561. u32 chip_addx,
  1562. const u32 *data,
  1563. unsigned int count)
  1564. {
  1565. struct ca0132_spec *spec = codec->spec;
  1566. int status;
  1567. mutex_lock(&spec->chipio_mutex);
  1568. status = chipio_write_address(codec, chip_addx);
  1569. if (status < 0)
  1570. goto error;
  1571. status = chipio_write_data_multiple(codec, data, count);
  1572. error:
  1573. mutex_unlock(&spec->chipio_mutex);
  1574. return status;
  1575. }
  1576. /*
  1577. * Read the given address through the chip I/O widget
  1578. * protected by the Mutex
  1579. */
  1580. static int chipio_read(struct hda_codec *codec,
  1581. unsigned int chip_addx, unsigned int *data)
  1582. {
  1583. struct ca0132_spec *spec = codec->spec;
  1584. int err;
  1585. mutex_lock(&spec->chipio_mutex);
  1586. /* write the address, and if successful proceed to write data */
  1587. err = chipio_write_address(codec, chip_addx);
  1588. if (err < 0)
  1589. goto exit;
  1590. err = chipio_read_data(codec, data);
  1591. if (err < 0)
  1592. goto exit;
  1593. exit:
  1594. mutex_unlock(&spec->chipio_mutex);
  1595. return err;
  1596. }
  1597. /*
  1598. * Set chip control flags through the chip I/O widget.
  1599. */
  1600. static void chipio_set_control_flag(struct hda_codec *codec,
  1601. enum control_flag_id flag_id,
  1602. bool flag_state)
  1603. {
  1604. unsigned int val;
  1605. unsigned int flag_bit;
  1606. flag_bit = (flag_state ? 1 : 0);
  1607. val = (flag_bit << 7) | (flag_id);
  1608. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  1609. VENDOR_CHIPIO_FLAG_SET, val);
  1610. }
  1611. /*
  1612. * Set chip parameters through the chip I/O widget.
  1613. */
  1614. static void chipio_set_control_param(struct hda_codec *codec,
  1615. enum control_param_id param_id, int param_val)
  1616. {
  1617. struct ca0132_spec *spec = codec->spec;
  1618. int val;
  1619. if ((param_id < 32) && (param_val < 8)) {
  1620. val = (param_val << 5) | (param_id);
  1621. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  1622. VENDOR_CHIPIO_PARAM_SET, val);
  1623. } else {
  1624. mutex_lock(&spec->chipio_mutex);
  1625. if (chipio_send(codec, VENDOR_CHIPIO_STATUS, 0) == 0) {
  1626. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  1627. VENDOR_CHIPIO_PARAM_EX_ID_SET,
  1628. param_id);
  1629. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  1630. VENDOR_CHIPIO_PARAM_EX_VALUE_SET,
  1631. param_val);
  1632. }
  1633. mutex_unlock(&spec->chipio_mutex);
  1634. }
  1635. }
  1636. /*
  1637. * Set chip parameters through the chip I/O widget. NO MUTEX.
  1638. */
  1639. static void chipio_set_control_param_no_mutex(struct hda_codec *codec,
  1640. enum control_param_id param_id, int param_val)
  1641. {
  1642. int val;
  1643. if ((param_id < 32) && (param_val < 8)) {
  1644. val = (param_val << 5) | (param_id);
  1645. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  1646. VENDOR_CHIPIO_PARAM_SET, val);
  1647. } else {
  1648. if (chipio_send(codec, VENDOR_CHIPIO_STATUS, 0) == 0) {
  1649. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  1650. VENDOR_CHIPIO_PARAM_EX_ID_SET,
  1651. param_id);
  1652. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  1653. VENDOR_CHIPIO_PARAM_EX_VALUE_SET,
  1654. param_val);
  1655. }
  1656. }
  1657. }
  1658. /*
  1659. * Connect stream to a source point, and then connect
  1660. * that source point to a destination point.
  1661. */
  1662. static void chipio_set_stream_source_dest(struct hda_codec *codec,
  1663. int streamid, int source_point, int dest_point)
  1664. {
  1665. chipio_set_control_param_no_mutex(codec,
  1666. CONTROL_PARAM_STREAM_ID, streamid);
  1667. chipio_set_control_param_no_mutex(codec,
  1668. CONTROL_PARAM_STREAM_SOURCE_CONN_POINT, source_point);
  1669. chipio_set_control_param_no_mutex(codec,
  1670. CONTROL_PARAM_STREAM_DEST_CONN_POINT, dest_point);
  1671. }
  1672. /*
  1673. * Set number of channels in the selected stream.
  1674. */
  1675. static void chipio_set_stream_channels(struct hda_codec *codec,
  1676. int streamid, unsigned int channels)
  1677. {
  1678. chipio_set_control_param_no_mutex(codec,
  1679. CONTROL_PARAM_STREAM_ID, streamid);
  1680. chipio_set_control_param_no_mutex(codec,
  1681. CONTROL_PARAM_STREAMS_CHANNELS, channels);
  1682. }
  1683. /*
  1684. * Enable/Disable audio stream.
  1685. */
  1686. static void chipio_set_stream_control(struct hda_codec *codec,
  1687. int streamid, int enable)
  1688. {
  1689. chipio_set_control_param_no_mutex(codec,
  1690. CONTROL_PARAM_STREAM_ID, streamid);
  1691. chipio_set_control_param_no_mutex(codec,
  1692. CONTROL_PARAM_STREAM_CONTROL, enable);
  1693. }
  1694. /*
  1695. * Get ChipIO audio stream's status.
  1696. */
  1697. static void chipio_get_stream_control(struct hda_codec *codec,
  1698. int streamid, unsigned int *enable)
  1699. {
  1700. chipio_set_control_param_no_mutex(codec,
  1701. CONTROL_PARAM_STREAM_ID, streamid);
  1702. *enable = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
  1703. VENDOR_CHIPIO_PARAM_GET,
  1704. CONTROL_PARAM_STREAM_CONTROL);
  1705. }
  1706. /*
  1707. * Set sampling rate of the connection point. NO MUTEX.
  1708. */
  1709. static void chipio_set_conn_rate_no_mutex(struct hda_codec *codec,
  1710. int connid, enum ca0132_sample_rate rate)
  1711. {
  1712. chipio_set_control_param_no_mutex(codec,
  1713. CONTROL_PARAM_CONN_POINT_ID, connid);
  1714. chipio_set_control_param_no_mutex(codec,
  1715. CONTROL_PARAM_CONN_POINT_SAMPLE_RATE, rate);
  1716. }
  1717. /*
  1718. * Set sampling rate of the connection point.
  1719. */
  1720. static void chipio_set_conn_rate(struct hda_codec *codec,
  1721. int connid, enum ca0132_sample_rate rate)
  1722. {
  1723. chipio_set_control_param(codec, CONTROL_PARAM_CONN_POINT_ID, connid);
  1724. chipio_set_control_param(codec, CONTROL_PARAM_CONN_POINT_SAMPLE_RATE,
  1725. rate);
  1726. }
  1727. /*
  1728. * Writes to the 8051's internal address space directly instead of indirectly,
  1729. * giving access to the special function registers located at addresses
  1730. * 0x80-0xFF.
  1731. */
  1732. static void chipio_8051_write_direct(struct hda_codec *codec,
  1733. unsigned int addr, unsigned int data)
  1734. {
  1735. unsigned int verb;
  1736. verb = VENDOR_CHIPIO_8051_WRITE_DIRECT | data;
  1737. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0, verb, addr);
  1738. }
  1739. /*
  1740. * Writes to the 8051's exram, which has 16-bits of address space.
  1741. * Data at addresses 0x2000-0x7fff is mirrored to 0x8000-0xdfff.
  1742. * Data at 0x8000-0xdfff can also be used as program memory for the 8051 by
  1743. * setting the pmem bank selection SFR.
  1744. * 0xe000-0xffff is always mapped as program memory, with only 0xf000-0xffff
  1745. * being writable.
  1746. */
  1747. static void chipio_8051_set_address(struct hda_codec *codec, unsigned int addr)
  1748. {
  1749. unsigned int tmp;
  1750. /* Lower 8-bits. */
  1751. tmp = addr & 0xff;
  1752. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  1753. VENDOR_CHIPIO_8051_ADDRESS_LOW, tmp);
  1754. /* Upper 8-bits. */
  1755. tmp = (addr >> 8) & 0xff;
  1756. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  1757. VENDOR_CHIPIO_8051_ADDRESS_HIGH, tmp);
  1758. }
  1759. static void chipio_8051_set_data(struct hda_codec *codec, unsigned int data)
  1760. {
  1761. /* 8-bits of data. */
  1762. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  1763. VENDOR_CHIPIO_8051_DATA_WRITE, data & 0xff);
  1764. }
  1765. static unsigned int chipio_8051_get_data(struct hda_codec *codec)
  1766. {
  1767. return snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
  1768. VENDOR_CHIPIO_8051_DATA_READ, 0);
  1769. }
  1770. /* PLL_PMU writes share the lower address register of the 8051 exram writes. */
  1771. static void chipio_8051_set_data_pll(struct hda_codec *codec, unsigned int data)
  1772. {
  1773. /* 8-bits of data. */
  1774. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  1775. VENDOR_CHIPIO_PLL_PMU_WRITE, data & 0xff);
  1776. }
  1777. static void chipio_8051_write_exram(struct hda_codec *codec,
  1778. unsigned int addr, unsigned int data)
  1779. {
  1780. struct ca0132_spec *spec = codec->spec;
  1781. mutex_lock(&spec->chipio_mutex);
  1782. chipio_8051_set_address(codec, addr);
  1783. chipio_8051_set_data(codec, data);
  1784. mutex_unlock(&spec->chipio_mutex);
  1785. }
  1786. static void chipio_8051_write_exram_no_mutex(struct hda_codec *codec,
  1787. unsigned int addr, unsigned int data)
  1788. {
  1789. chipio_8051_set_address(codec, addr);
  1790. chipio_8051_set_data(codec, data);
  1791. }
  1792. /* Readback data from the 8051's exram. No mutex. */
  1793. static void chipio_8051_read_exram(struct hda_codec *codec,
  1794. unsigned int addr, unsigned int *data)
  1795. {
  1796. chipio_8051_set_address(codec, addr);
  1797. *data = chipio_8051_get_data(codec);
  1798. }
  1799. static void chipio_8051_write_pll_pmu(struct hda_codec *codec,
  1800. unsigned int addr, unsigned int data)
  1801. {
  1802. struct ca0132_spec *spec = codec->spec;
  1803. mutex_lock(&spec->chipio_mutex);
  1804. chipio_8051_set_address(codec, addr & 0xff);
  1805. chipio_8051_set_data_pll(codec, data);
  1806. mutex_unlock(&spec->chipio_mutex);
  1807. }
  1808. static void chipio_8051_write_pll_pmu_no_mutex(struct hda_codec *codec,
  1809. unsigned int addr, unsigned int data)
  1810. {
  1811. chipio_8051_set_address(codec, addr & 0xff);
  1812. chipio_8051_set_data_pll(codec, data);
  1813. }
  1814. /*
  1815. * Enable clocks.
  1816. */
  1817. static void chipio_enable_clocks(struct hda_codec *codec)
  1818. {
  1819. struct ca0132_spec *spec = codec->spec;
  1820. mutex_lock(&spec->chipio_mutex);
  1821. chipio_8051_write_pll_pmu_no_mutex(codec, 0x00, 0xff);
  1822. chipio_8051_write_pll_pmu_no_mutex(codec, 0x05, 0x0b);
  1823. chipio_8051_write_pll_pmu_no_mutex(codec, 0x06, 0xff);
  1824. mutex_unlock(&spec->chipio_mutex);
  1825. }
  1826. /*
  1827. * CA0132 DSP IO stuffs
  1828. */
  1829. static int dspio_send(struct hda_codec *codec, unsigned int reg,
  1830. unsigned int data)
  1831. {
  1832. int res;
  1833. unsigned long timeout = jiffies + msecs_to_jiffies(1000);
  1834. /* send bits of data specified by reg to dsp */
  1835. do {
  1836. res = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0, reg, data);
  1837. if ((res >= 0) && (res != VENDOR_STATUS_DSPIO_BUSY))
  1838. return res;
  1839. msleep(20);
  1840. } while (time_before(jiffies, timeout));
  1841. return -EIO;
  1842. }
  1843. /*
  1844. * Wait for DSP to be ready for commands
  1845. */
  1846. static void dspio_write_wait(struct hda_codec *codec)
  1847. {
  1848. int status;
  1849. unsigned long timeout = jiffies + msecs_to_jiffies(1000);
  1850. do {
  1851. status = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0,
  1852. VENDOR_DSPIO_STATUS, 0);
  1853. if ((status == VENDOR_STATUS_DSPIO_OK) ||
  1854. (status == VENDOR_STATUS_DSPIO_SCP_RESPONSE_QUEUE_EMPTY))
  1855. break;
  1856. msleep(1);
  1857. } while (time_before(jiffies, timeout));
  1858. }
  1859. /*
  1860. * Write SCP data to DSP
  1861. */
  1862. static int dspio_write(struct hda_codec *codec, unsigned int scp_data)
  1863. {
  1864. struct ca0132_spec *spec = codec->spec;
  1865. int status;
  1866. dspio_write_wait(codec);
  1867. mutex_lock(&spec->chipio_mutex);
  1868. status = dspio_send(codec, VENDOR_DSPIO_SCP_WRITE_DATA_LOW,
  1869. scp_data & 0xffff);
  1870. if (status < 0)
  1871. goto error;
  1872. status = dspio_send(codec, VENDOR_DSPIO_SCP_WRITE_DATA_HIGH,
  1873. scp_data >> 16);
  1874. if (status < 0)
  1875. goto error;
  1876. /* OK, now check if the write itself has executed*/
  1877. status = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0,
  1878. VENDOR_DSPIO_STATUS, 0);
  1879. error:
  1880. mutex_unlock(&spec->chipio_mutex);
  1881. return (status == VENDOR_STATUS_DSPIO_SCP_COMMAND_QUEUE_FULL) ?
  1882. -EIO : 0;
  1883. }
  1884. /*
  1885. * Write multiple SCP data to DSP
  1886. */
  1887. static int dspio_write_multiple(struct hda_codec *codec,
  1888. unsigned int *buffer, unsigned int size)
  1889. {
  1890. int status = 0;
  1891. unsigned int count;
  1892. if (buffer == NULL)
  1893. return -EINVAL;
  1894. count = 0;
  1895. while (count < size) {
  1896. status = dspio_write(codec, *buffer++);
  1897. if (status != 0)
  1898. break;
  1899. count++;
  1900. }
  1901. return status;
  1902. }
  1903. static int dspio_read(struct hda_codec *codec, unsigned int *data)
  1904. {
  1905. int status;
  1906. status = dspio_send(codec, VENDOR_DSPIO_SCP_POST_READ_DATA, 0);
  1907. if (status == -EIO)
  1908. return status;
  1909. status = dspio_send(codec, VENDOR_DSPIO_STATUS, 0);
  1910. if (status == -EIO ||
  1911. status == VENDOR_STATUS_DSPIO_SCP_RESPONSE_QUEUE_EMPTY)
  1912. return -EIO;
  1913. *data = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0,
  1914. VENDOR_DSPIO_SCP_READ_DATA, 0);
  1915. return 0;
  1916. }
  1917. static int dspio_read_multiple(struct hda_codec *codec, unsigned int *buffer,
  1918. unsigned int *buf_size, unsigned int size_count)
  1919. {
  1920. int status = 0;
  1921. unsigned int size = *buf_size;
  1922. unsigned int count;
  1923. unsigned int skip_count;
  1924. unsigned int dummy;
  1925. if (buffer == NULL)
  1926. return -1;
  1927. count = 0;
  1928. while (count < size && count < size_count) {
  1929. status = dspio_read(codec, buffer++);
  1930. if (status != 0)
  1931. break;
  1932. count++;
  1933. }
  1934. skip_count = count;
  1935. if (status == 0) {
  1936. while (skip_count < size) {
  1937. status = dspio_read(codec, &dummy);
  1938. if (status != 0)
  1939. break;
  1940. skip_count++;
  1941. }
  1942. }
  1943. *buf_size = count;
  1944. return status;
  1945. }
  1946. /*
  1947. * Construct the SCP header using corresponding fields
  1948. */
  1949. static inline unsigned int
  1950. make_scp_header(unsigned int target_id, unsigned int source_id,
  1951. unsigned int get_flag, unsigned int req,
  1952. unsigned int device_flag, unsigned int resp_flag,
  1953. unsigned int error_flag, unsigned int data_size)
  1954. {
  1955. unsigned int header = 0;
  1956. header = (data_size & 0x1f) << 27;
  1957. header |= (error_flag & 0x01) << 26;
  1958. header |= (resp_flag & 0x01) << 25;
  1959. header |= (device_flag & 0x01) << 24;
  1960. header |= (req & 0x7f) << 17;
  1961. header |= (get_flag & 0x01) << 16;
  1962. header |= (source_id & 0xff) << 8;
  1963. header |= target_id & 0xff;
  1964. return header;
  1965. }
  1966. /*
  1967. * Extract corresponding fields from SCP header
  1968. */
  1969. static inline void
  1970. extract_scp_header(unsigned int header,
  1971. unsigned int *target_id, unsigned int *source_id,
  1972. unsigned int *get_flag, unsigned int *req,
  1973. unsigned int *device_flag, unsigned int *resp_flag,
  1974. unsigned int *error_flag, unsigned int *data_size)
  1975. {
  1976. if (data_size)
  1977. *data_size = (header >> 27) & 0x1f;
  1978. if (error_flag)
  1979. *error_flag = (header >> 26) & 0x01;
  1980. if (resp_flag)
  1981. *resp_flag = (header >> 25) & 0x01;
  1982. if (device_flag)
  1983. *device_flag = (header >> 24) & 0x01;
  1984. if (req)
  1985. *req = (header >> 17) & 0x7f;
  1986. if (get_flag)
  1987. *get_flag = (header >> 16) & 0x01;
  1988. if (source_id)
  1989. *source_id = (header >> 8) & 0xff;
  1990. if (target_id)
  1991. *target_id = header & 0xff;
  1992. }
  1993. #define SCP_MAX_DATA_WORDS (16)
  1994. /* Structure to contain any SCP message */
  1995. struct scp_msg {
  1996. unsigned int hdr;
  1997. unsigned int data[SCP_MAX_DATA_WORDS];
  1998. };
  1999. static void dspio_clear_response_queue(struct hda_codec *codec)
  2000. {
  2001. unsigned long timeout = jiffies + msecs_to_jiffies(1000);
  2002. unsigned int dummy = 0;
  2003. int status;
  2004. /* clear all from the response queue */
  2005. do {
  2006. status = dspio_read(codec, &dummy);
  2007. } while (status == 0 && time_before(jiffies, timeout));
  2008. }
  2009. static int dspio_get_response_data(struct hda_codec *codec)
  2010. {
  2011. struct ca0132_spec *spec = codec->spec;
  2012. unsigned int data = 0;
  2013. unsigned int count;
  2014. if (dspio_read(codec, &data) < 0)
  2015. return -EIO;
  2016. if ((data & 0x00ffffff) == spec->wait_scp_header) {
  2017. spec->scp_resp_header = data;
  2018. spec->scp_resp_count = data >> 27;
  2019. count = spec->wait_num_data;
  2020. dspio_read_multiple(codec, spec->scp_resp_data,
  2021. &spec->scp_resp_count, count);
  2022. return 0;
  2023. }
  2024. return -EIO;
  2025. }
  2026. /*
  2027. * Send SCP message to DSP
  2028. */
  2029. static int dspio_send_scp_message(struct hda_codec *codec,
  2030. unsigned char *send_buf,
  2031. unsigned int send_buf_size,
  2032. unsigned char *return_buf,
  2033. unsigned int return_buf_size,
  2034. unsigned int *bytes_returned)
  2035. {
  2036. struct ca0132_spec *spec = codec->spec;
  2037. int status;
  2038. unsigned int scp_send_size = 0;
  2039. unsigned int total_size;
  2040. bool waiting_for_resp = false;
  2041. unsigned int header;
  2042. struct scp_msg *ret_msg;
  2043. unsigned int resp_src_id, resp_target_id;
  2044. unsigned int data_size, src_id, target_id, get_flag, device_flag;
  2045. if (bytes_returned)
  2046. *bytes_returned = 0;
  2047. /* get scp header from buffer */
  2048. header = *((unsigned int *)send_buf);
  2049. extract_scp_header(header, &target_id, &src_id, &get_flag, NULL,
  2050. &device_flag, NULL, NULL, &data_size);
  2051. scp_send_size = data_size + 1;
  2052. total_size = (scp_send_size * 4);
  2053. if (send_buf_size < total_size)
  2054. return -EINVAL;
  2055. if (get_flag || device_flag) {
  2056. if (!return_buf || return_buf_size < 4 || !bytes_returned)
  2057. return -EINVAL;
  2058. spec->wait_scp_header = *((unsigned int *)send_buf);
  2059. /* swap source id with target id */
  2060. resp_target_id = src_id;
  2061. resp_src_id = target_id;
  2062. spec->wait_scp_header &= 0xffff0000;
  2063. spec->wait_scp_header |= (resp_src_id << 8) | (resp_target_id);
  2064. spec->wait_num_data = return_buf_size/sizeof(unsigned int) - 1;
  2065. spec->wait_scp = 1;
  2066. waiting_for_resp = true;
  2067. }
  2068. status = dspio_write_multiple(codec, (unsigned int *)send_buf,
  2069. scp_send_size);
  2070. if (status < 0) {
  2071. spec->wait_scp = 0;
  2072. return status;
  2073. }
  2074. if (waiting_for_resp) {
  2075. unsigned long timeout = jiffies + msecs_to_jiffies(1000);
  2076. memset(return_buf, 0, return_buf_size);
  2077. do {
  2078. msleep(20);
  2079. } while (spec->wait_scp && time_before(jiffies, timeout));
  2080. waiting_for_resp = false;
  2081. if (!spec->wait_scp) {
  2082. ret_msg = (struct scp_msg *)return_buf;
  2083. memcpy(&ret_msg->hdr, &spec->scp_resp_header, 4);
  2084. memcpy(&ret_msg->data, spec->scp_resp_data,
  2085. spec->wait_num_data);
  2086. *bytes_returned = (spec->scp_resp_count + 1) * 4;
  2087. status = 0;
  2088. } else {
  2089. status = -EIO;
  2090. }
  2091. spec->wait_scp = 0;
  2092. }
  2093. return status;
  2094. }
  2095. /**
  2096. * dspio_scp - Prepare and send the SCP message to DSP
  2097. * @codec: the HDA codec
  2098. * @mod_id: ID of the DSP module to send the command
  2099. * @src_id: ID of the source
  2100. * @req: ID of request to send to the DSP module
  2101. * @dir: SET or GET
  2102. * @data: pointer to the data to send with the request, request specific
  2103. * @len: length of the data, in bytes
  2104. * @reply: point to the buffer to hold data returned for a reply
  2105. * @reply_len: length of the reply buffer returned from GET
  2106. *
  2107. * Returns zero or a negative error code.
  2108. */
  2109. static int dspio_scp(struct hda_codec *codec,
  2110. int mod_id, int src_id, int req, int dir, const void *data,
  2111. unsigned int len, void *reply, unsigned int *reply_len)
  2112. {
  2113. int status = 0;
  2114. struct scp_msg scp_send, scp_reply;
  2115. unsigned int ret_bytes, send_size, ret_size;
  2116. unsigned int send_get_flag, reply_resp_flag, reply_error_flag;
  2117. unsigned int reply_data_size;
  2118. memset(&scp_send, 0, sizeof(scp_send));
  2119. memset(&scp_reply, 0, sizeof(scp_reply));
  2120. if ((len != 0 && data == NULL) || (len > SCP_MAX_DATA_WORDS))
  2121. return -EINVAL;
  2122. if (dir == SCP_GET && reply == NULL) {
  2123. codec_dbg(codec, "dspio_scp get but has no buffer\n");
  2124. return -EINVAL;
  2125. }
  2126. if (reply != NULL && (reply_len == NULL || (*reply_len == 0))) {
  2127. codec_dbg(codec, "dspio_scp bad resp buf len parms\n");
  2128. return -EINVAL;
  2129. }
  2130. scp_send.hdr = make_scp_header(mod_id, src_id, (dir == SCP_GET), req,
  2131. 0, 0, 0, len/sizeof(unsigned int));
  2132. if (data != NULL && len > 0) {
  2133. len = min((unsigned int)(sizeof(scp_send.data)), len);
  2134. memcpy(scp_send.data, data, len);
  2135. }
  2136. ret_bytes = 0;
  2137. send_size = sizeof(unsigned int) + len;
  2138. status = dspio_send_scp_message(codec, (unsigned char *)&scp_send,
  2139. send_size, (unsigned char *)&scp_reply,
  2140. sizeof(scp_reply), &ret_bytes);
  2141. if (status < 0) {
  2142. codec_dbg(codec, "dspio_scp: send scp msg failed\n");
  2143. return status;
  2144. }
  2145. /* extract send and reply headers members */
  2146. extract_scp_header(scp_send.hdr, NULL, NULL, &send_get_flag,
  2147. NULL, NULL, NULL, NULL, NULL);
  2148. extract_scp_header(scp_reply.hdr, NULL, NULL, NULL, NULL, NULL,
  2149. &reply_resp_flag, &reply_error_flag,
  2150. &reply_data_size);
  2151. if (!send_get_flag)
  2152. return 0;
  2153. if (reply_resp_flag && !reply_error_flag) {
  2154. ret_size = (ret_bytes - sizeof(scp_reply.hdr))
  2155. / sizeof(unsigned int);
  2156. if (*reply_len < ret_size*sizeof(unsigned int)) {
  2157. codec_dbg(codec, "reply too long for buf\n");
  2158. return -EINVAL;
  2159. } else if (ret_size != reply_data_size) {
  2160. codec_dbg(codec, "RetLen and HdrLen .NE.\n");
  2161. return -EINVAL;
  2162. } else if (!reply) {
  2163. codec_dbg(codec, "NULL reply\n");
  2164. return -EINVAL;
  2165. } else {
  2166. *reply_len = ret_size*sizeof(unsigned int);
  2167. memcpy(reply, scp_reply.data, *reply_len);
  2168. }
  2169. } else {
  2170. codec_dbg(codec, "reply ill-formed or errflag set\n");
  2171. return -EIO;
  2172. }
  2173. return status;
  2174. }
  2175. /*
  2176. * Set DSP parameters
  2177. */
  2178. static int dspio_set_param(struct hda_codec *codec, int mod_id,
  2179. int src_id, int req, const void *data, unsigned int len)
  2180. {
  2181. return dspio_scp(codec, mod_id, src_id, req, SCP_SET, data, len, NULL,
  2182. NULL);
  2183. }
  2184. static int dspio_set_uint_param(struct hda_codec *codec, int mod_id,
  2185. int req, const unsigned int data)
  2186. {
  2187. return dspio_set_param(codec, mod_id, 0x20, req, &data,
  2188. sizeof(unsigned int));
  2189. }
  2190. /*
  2191. * Allocate a DSP DMA channel via an SCP message
  2192. */
  2193. static int dspio_alloc_dma_chan(struct hda_codec *codec, unsigned int *dma_chan)
  2194. {
  2195. int status = 0;
  2196. unsigned int size = sizeof(*dma_chan);
  2197. codec_dbg(codec, " dspio_alloc_dma_chan() -- begin\n");
  2198. status = dspio_scp(codec, MASTERCONTROL, 0x20,
  2199. MASTERCONTROL_ALLOC_DMA_CHAN, SCP_GET, NULL, 0,
  2200. dma_chan, &size);
  2201. if (status < 0) {
  2202. codec_dbg(codec, "dspio_alloc_dma_chan: SCP Failed\n");
  2203. return status;
  2204. }
  2205. if ((*dma_chan + 1) == 0) {
  2206. codec_dbg(codec, "no free dma channels to allocate\n");
  2207. return -EBUSY;
  2208. }
  2209. codec_dbg(codec, "dspio_alloc_dma_chan: chan=%d\n", *dma_chan);
  2210. codec_dbg(codec, " dspio_alloc_dma_chan() -- complete\n");
  2211. return status;
  2212. }
  2213. /*
  2214. * Free a DSP DMA via an SCP message
  2215. */
  2216. static int dspio_free_dma_chan(struct hda_codec *codec, unsigned int dma_chan)
  2217. {
  2218. int status = 0;
  2219. unsigned int dummy = 0;
  2220. codec_dbg(codec, " dspio_free_dma_chan() -- begin\n");
  2221. codec_dbg(codec, "dspio_free_dma_chan: chan=%d\n", dma_chan);
  2222. status = dspio_scp(codec, MASTERCONTROL, 0x20,
  2223. MASTERCONTROL_ALLOC_DMA_CHAN, SCP_SET, &dma_chan,
  2224. sizeof(dma_chan), NULL, &dummy);
  2225. if (status < 0) {
  2226. codec_dbg(codec, "dspio_free_dma_chan: SCP Failed\n");
  2227. return status;
  2228. }
  2229. codec_dbg(codec, " dspio_free_dma_chan() -- complete\n");
  2230. return status;
  2231. }
  2232. /*
  2233. * (Re)start the DSP
  2234. */
  2235. static int dsp_set_run_state(struct hda_codec *codec)
  2236. {
  2237. unsigned int dbg_ctrl_reg;
  2238. unsigned int halt_state;
  2239. int err;
  2240. err = chipio_read(codec, DSP_DBGCNTL_INST_OFFSET, &dbg_ctrl_reg);
  2241. if (err < 0)
  2242. return err;
  2243. halt_state = (dbg_ctrl_reg & DSP_DBGCNTL_STATE_MASK) >>
  2244. DSP_DBGCNTL_STATE_LOBIT;
  2245. if (halt_state != 0) {
  2246. dbg_ctrl_reg &= ~((halt_state << DSP_DBGCNTL_SS_LOBIT) &
  2247. DSP_DBGCNTL_SS_MASK);
  2248. err = chipio_write(codec, DSP_DBGCNTL_INST_OFFSET,
  2249. dbg_ctrl_reg);
  2250. if (err < 0)
  2251. return err;
  2252. dbg_ctrl_reg |= (halt_state << DSP_DBGCNTL_EXEC_LOBIT) &
  2253. DSP_DBGCNTL_EXEC_MASK;
  2254. err = chipio_write(codec, DSP_DBGCNTL_INST_OFFSET,
  2255. dbg_ctrl_reg);
  2256. if (err < 0)
  2257. return err;
  2258. }
  2259. return 0;
  2260. }
  2261. /*
  2262. * Reset the DSP
  2263. */
  2264. static int dsp_reset(struct hda_codec *codec)
  2265. {
  2266. unsigned int res;
  2267. int retry = 20;
  2268. codec_dbg(codec, "dsp_reset\n");
  2269. do {
  2270. res = dspio_send(codec, VENDOR_DSPIO_DSP_INIT, 0);
  2271. retry--;
  2272. } while (res == -EIO && retry);
  2273. if (!retry) {
  2274. codec_dbg(codec, "dsp_reset timeout\n");
  2275. return -EIO;
  2276. }
  2277. return 0;
  2278. }
  2279. /*
  2280. * Convert chip address to DSP address
  2281. */
  2282. static unsigned int dsp_chip_to_dsp_addx(unsigned int chip_addx,
  2283. bool *code, bool *yram)
  2284. {
  2285. *code = *yram = false;
  2286. if (UC_RANGE(chip_addx, 1)) {
  2287. *code = true;
  2288. return UC_OFF(chip_addx);
  2289. } else if (X_RANGE_ALL(chip_addx, 1)) {
  2290. return X_OFF(chip_addx);
  2291. } else if (Y_RANGE_ALL(chip_addx, 1)) {
  2292. *yram = true;
  2293. return Y_OFF(chip_addx);
  2294. }
  2295. return INVALID_CHIP_ADDRESS;
  2296. }
  2297. /*
  2298. * Check if the DSP DMA is active
  2299. */
  2300. static bool dsp_is_dma_active(struct hda_codec *codec, unsigned int dma_chan)
  2301. {
  2302. unsigned int dma_chnlstart_reg;
  2303. chipio_read(codec, DSPDMAC_CHNLSTART_INST_OFFSET, &dma_chnlstart_reg);
  2304. return ((dma_chnlstart_reg & (1 <<
  2305. (DSPDMAC_CHNLSTART_EN_LOBIT + dma_chan))) != 0);
  2306. }
  2307. static int dsp_dma_setup_common(struct hda_codec *codec,
  2308. unsigned int chip_addx,
  2309. unsigned int dma_chan,
  2310. unsigned int port_map_mask,
  2311. bool ovly)
  2312. {
  2313. int status = 0;
  2314. unsigned int chnl_prop;
  2315. unsigned int dsp_addx;
  2316. unsigned int active;
  2317. bool code, yram;
  2318. codec_dbg(codec, "-- dsp_dma_setup_common() -- Begin ---------\n");
  2319. if (dma_chan >= DSPDMAC_DMA_CFG_CHANNEL_COUNT) {
  2320. codec_dbg(codec, "dma chan num invalid\n");
  2321. return -EINVAL;
  2322. }
  2323. if (dsp_is_dma_active(codec, dma_chan)) {
  2324. codec_dbg(codec, "dma already active\n");
  2325. return -EBUSY;
  2326. }
  2327. dsp_addx = dsp_chip_to_dsp_addx(chip_addx, &code, &yram);
  2328. if (dsp_addx == INVALID_CHIP_ADDRESS) {
  2329. codec_dbg(codec, "invalid chip addr\n");
  2330. return -ENXIO;
  2331. }
  2332. chnl_prop = DSPDMAC_CHNLPROP_AC_MASK;
  2333. active = 0;
  2334. codec_dbg(codec, " dsp_dma_setup_common() start reg pgm\n");
  2335. if (ovly) {
  2336. status = chipio_read(codec, DSPDMAC_CHNLPROP_INST_OFFSET,
  2337. &chnl_prop);
  2338. if (status < 0) {
  2339. codec_dbg(codec, "read CHNLPROP Reg fail\n");
  2340. return status;
  2341. }
  2342. codec_dbg(codec, "dsp_dma_setup_common() Read CHNLPROP\n");
  2343. }
  2344. if (!code)
  2345. chnl_prop &= ~(1 << (DSPDMAC_CHNLPROP_MSPCE_LOBIT + dma_chan));
  2346. else
  2347. chnl_prop |= (1 << (DSPDMAC_CHNLPROP_MSPCE_LOBIT + dma_chan));
  2348. chnl_prop &= ~(1 << (DSPDMAC_CHNLPROP_DCON_LOBIT + dma_chan));
  2349. status = chipio_write(codec, DSPDMAC_CHNLPROP_INST_OFFSET, chnl_prop);
  2350. if (status < 0) {
  2351. codec_dbg(codec, "write CHNLPROP Reg fail\n");
  2352. return status;
  2353. }
  2354. codec_dbg(codec, " dsp_dma_setup_common() Write CHNLPROP\n");
  2355. if (ovly) {
  2356. status = chipio_read(codec, DSPDMAC_ACTIVE_INST_OFFSET,
  2357. &active);
  2358. if (status < 0) {
  2359. codec_dbg(codec, "read ACTIVE Reg fail\n");
  2360. return status;
  2361. }
  2362. codec_dbg(codec, "dsp_dma_setup_common() Read ACTIVE\n");
  2363. }
  2364. active &= (~(1 << (DSPDMAC_ACTIVE_AAR_LOBIT + dma_chan))) &
  2365. DSPDMAC_ACTIVE_AAR_MASK;
  2366. status = chipio_write(codec, DSPDMAC_ACTIVE_INST_OFFSET, active);
  2367. if (status < 0) {
  2368. codec_dbg(codec, "write ACTIVE Reg fail\n");
  2369. return status;
  2370. }
  2371. codec_dbg(codec, " dsp_dma_setup_common() Write ACTIVE\n");
  2372. status = chipio_write(codec, DSPDMAC_AUDCHSEL_INST_OFFSET(dma_chan),
  2373. port_map_mask);
  2374. if (status < 0) {
  2375. codec_dbg(codec, "write AUDCHSEL Reg fail\n");
  2376. return status;
  2377. }
  2378. codec_dbg(codec, " dsp_dma_setup_common() Write AUDCHSEL\n");
  2379. status = chipio_write(codec, DSPDMAC_IRQCNT_INST_OFFSET(dma_chan),
  2380. DSPDMAC_IRQCNT_BICNT_MASK | DSPDMAC_IRQCNT_CICNT_MASK);
  2381. if (status < 0) {
  2382. codec_dbg(codec, "write IRQCNT Reg fail\n");
  2383. return status;
  2384. }
  2385. codec_dbg(codec, " dsp_dma_setup_common() Write IRQCNT\n");
  2386. codec_dbg(codec,
  2387. "ChipA=0x%x,DspA=0x%x,dmaCh=%u, "
  2388. "CHSEL=0x%x,CHPROP=0x%x,Active=0x%x\n",
  2389. chip_addx, dsp_addx, dma_chan,
  2390. port_map_mask, chnl_prop, active);
  2391. codec_dbg(codec, "-- dsp_dma_setup_common() -- Complete ------\n");
  2392. return 0;
  2393. }
  2394. /*
  2395. * Setup the DSP DMA per-transfer-specific registers
  2396. */
  2397. static int dsp_dma_setup(struct hda_codec *codec,
  2398. unsigned int chip_addx,
  2399. unsigned int count,
  2400. unsigned int dma_chan)
  2401. {
  2402. int status = 0;
  2403. bool code, yram;
  2404. unsigned int dsp_addx;
  2405. unsigned int addr_field;
  2406. unsigned int incr_field;
  2407. unsigned int base_cnt;
  2408. unsigned int cur_cnt;
  2409. unsigned int dma_cfg = 0;
  2410. unsigned int adr_ofs = 0;
  2411. unsigned int xfr_cnt = 0;
  2412. const unsigned int max_dma_count = 1 << (DSPDMAC_XFRCNT_BCNT_HIBIT -
  2413. DSPDMAC_XFRCNT_BCNT_LOBIT + 1);
  2414. codec_dbg(codec, "-- dsp_dma_setup() -- Begin ---------\n");
  2415. if (count > max_dma_count) {
  2416. codec_dbg(codec, "count too big\n");
  2417. return -EINVAL;
  2418. }
  2419. dsp_addx = dsp_chip_to_dsp_addx(chip_addx, &code, &yram);
  2420. if (dsp_addx == INVALID_CHIP_ADDRESS) {
  2421. codec_dbg(codec, "invalid chip addr\n");
  2422. return -ENXIO;
  2423. }
  2424. codec_dbg(codec, " dsp_dma_setup() start reg pgm\n");
  2425. addr_field = dsp_addx << DSPDMAC_DMACFG_DBADR_LOBIT;
  2426. incr_field = 0;
  2427. if (!code) {
  2428. addr_field <<= 1;
  2429. if (yram)
  2430. addr_field |= (1 << DSPDMAC_DMACFG_DBADR_LOBIT);
  2431. incr_field = (1 << DSPDMAC_DMACFG_AINCR_LOBIT);
  2432. }
  2433. dma_cfg = addr_field + incr_field;
  2434. status = chipio_write(codec, DSPDMAC_DMACFG_INST_OFFSET(dma_chan),
  2435. dma_cfg);
  2436. if (status < 0) {
  2437. codec_dbg(codec, "write DMACFG Reg fail\n");
  2438. return status;
  2439. }
  2440. codec_dbg(codec, " dsp_dma_setup() Write DMACFG\n");
  2441. adr_ofs = (count - 1) << (DSPDMAC_DSPADROFS_BOFS_LOBIT +
  2442. (code ? 0 : 1));
  2443. status = chipio_write(codec, DSPDMAC_DSPADROFS_INST_OFFSET(dma_chan),
  2444. adr_ofs);
  2445. if (status < 0) {
  2446. codec_dbg(codec, "write DSPADROFS Reg fail\n");
  2447. return status;
  2448. }
  2449. codec_dbg(codec, " dsp_dma_setup() Write DSPADROFS\n");
  2450. base_cnt = (count - 1) << DSPDMAC_XFRCNT_BCNT_LOBIT;
  2451. cur_cnt = (count - 1) << DSPDMAC_XFRCNT_CCNT_LOBIT;
  2452. xfr_cnt = base_cnt | cur_cnt;
  2453. status = chipio_write(codec,
  2454. DSPDMAC_XFRCNT_INST_OFFSET(dma_chan), xfr_cnt);
  2455. if (status < 0) {
  2456. codec_dbg(codec, "write XFRCNT Reg fail\n");
  2457. return status;
  2458. }
  2459. codec_dbg(codec, " dsp_dma_setup() Write XFRCNT\n");
  2460. codec_dbg(codec,
  2461. "ChipA=0x%x, cnt=0x%x, DMACFG=0x%x, "
  2462. "ADROFS=0x%x, XFRCNT=0x%x\n",
  2463. chip_addx, count, dma_cfg, adr_ofs, xfr_cnt);
  2464. codec_dbg(codec, "-- dsp_dma_setup() -- Complete ---------\n");
  2465. return 0;
  2466. }
  2467. /*
  2468. * Start the DSP DMA
  2469. */
  2470. static int dsp_dma_start(struct hda_codec *codec,
  2471. unsigned int dma_chan, bool ovly)
  2472. {
  2473. unsigned int reg = 0;
  2474. int status = 0;
  2475. codec_dbg(codec, "-- dsp_dma_start() -- Begin ---------\n");
  2476. if (ovly) {
  2477. status = chipio_read(codec,
  2478. DSPDMAC_CHNLSTART_INST_OFFSET, &reg);
  2479. if (status < 0) {
  2480. codec_dbg(codec, "read CHNLSTART reg fail\n");
  2481. return status;
  2482. }
  2483. codec_dbg(codec, "-- dsp_dma_start() Read CHNLSTART\n");
  2484. reg &= ~(DSPDMAC_CHNLSTART_EN_MASK |
  2485. DSPDMAC_CHNLSTART_DIS_MASK);
  2486. }
  2487. status = chipio_write(codec, DSPDMAC_CHNLSTART_INST_OFFSET,
  2488. reg | (1 << (dma_chan + DSPDMAC_CHNLSTART_EN_LOBIT)));
  2489. if (status < 0) {
  2490. codec_dbg(codec, "write CHNLSTART reg fail\n");
  2491. return status;
  2492. }
  2493. codec_dbg(codec, "-- dsp_dma_start() -- Complete ---------\n");
  2494. return status;
  2495. }
  2496. /*
  2497. * Stop the DSP DMA
  2498. */
  2499. static int dsp_dma_stop(struct hda_codec *codec,
  2500. unsigned int dma_chan, bool ovly)
  2501. {
  2502. unsigned int reg = 0;
  2503. int status = 0;
  2504. codec_dbg(codec, "-- dsp_dma_stop() -- Begin ---------\n");
  2505. if (ovly) {
  2506. status = chipio_read(codec,
  2507. DSPDMAC_CHNLSTART_INST_OFFSET, &reg);
  2508. if (status < 0) {
  2509. codec_dbg(codec, "read CHNLSTART reg fail\n");
  2510. return status;
  2511. }
  2512. codec_dbg(codec, "-- dsp_dma_stop() Read CHNLSTART\n");
  2513. reg &= ~(DSPDMAC_CHNLSTART_EN_MASK |
  2514. DSPDMAC_CHNLSTART_DIS_MASK);
  2515. }
  2516. status = chipio_write(codec, DSPDMAC_CHNLSTART_INST_OFFSET,
  2517. reg | (1 << (dma_chan + DSPDMAC_CHNLSTART_DIS_LOBIT)));
  2518. if (status < 0) {
  2519. codec_dbg(codec, "write CHNLSTART reg fail\n");
  2520. return status;
  2521. }
  2522. codec_dbg(codec, "-- dsp_dma_stop() -- Complete ---------\n");
  2523. return status;
  2524. }
  2525. /**
  2526. * dsp_allocate_router_ports - Allocate router ports
  2527. *
  2528. * @codec: the HDA codec
  2529. * @num_chans: number of channels in the stream
  2530. * @ports_per_channel: number of ports per channel
  2531. * @start_device: start device
  2532. * @port_map: pointer to the port list to hold the allocated ports
  2533. *
  2534. * Returns zero or a negative error code.
  2535. */
  2536. static int dsp_allocate_router_ports(struct hda_codec *codec,
  2537. unsigned int num_chans,
  2538. unsigned int ports_per_channel,
  2539. unsigned int start_device,
  2540. unsigned int *port_map)
  2541. {
  2542. int status = 0;
  2543. int res;
  2544. u8 val;
  2545. status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
  2546. if (status < 0)
  2547. return status;
  2548. val = start_device << 6;
  2549. val |= (ports_per_channel - 1) << 4;
  2550. val |= num_chans - 1;
  2551. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  2552. VENDOR_CHIPIO_PORT_ALLOC_CONFIG_SET,
  2553. val);
  2554. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  2555. VENDOR_CHIPIO_PORT_ALLOC_SET,
  2556. MEM_CONNID_DSP);
  2557. status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
  2558. if (status < 0)
  2559. return status;
  2560. res = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
  2561. VENDOR_CHIPIO_PORT_ALLOC_GET, 0);
  2562. *port_map = res;
  2563. return (res < 0) ? res : 0;
  2564. }
  2565. /*
  2566. * Free router ports
  2567. */
  2568. static int dsp_free_router_ports(struct hda_codec *codec)
  2569. {
  2570. int status = 0;
  2571. status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
  2572. if (status < 0)
  2573. return status;
  2574. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  2575. VENDOR_CHIPIO_PORT_FREE_SET,
  2576. MEM_CONNID_DSP);
  2577. status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
  2578. return status;
  2579. }
  2580. /*
  2581. * Allocate DSP ports for the download stream
  2582. */
  2583. static int dsp_allocate_ports(struct hda_codec *codec,
  2584. unsigned int num_chans,
  2585. unsigned int rate_multi, unsigned int *port_map)
  2586. {
  2587. int status;
  2588. codec_dbg(codec, " dsp_allocate_ports() -- begin\n");
  2589. if ((rate_multi != 1) && (rate_multi != 2) && (rate_multi != 4)) {
  2590. codec_dbg(codec, "bad rate multiple\n");
  2591. return -EINVAL;
  2592. }
  2593. status = dsp_allocate_router_ports(codec, num_chans,
  2594. rate_multi, 0, port_map);
  2595. codec_dbg(codec, " dsp_allocate_ports() -- complete\n");
  2596. return status;
  2597. }
  2598. static int dsp_allocate_ports_format(struct hda_codec *codec,
  2599. const unsigned short fmt,
  2600. unsigned int *port_map)
  2601. {
  2602. unsigned int num_chans;
  2603. unsigned int sample_rate_div = ((get_hdafmt_rate(fmt) >> 0) & 3) + 1;
  2604. unsigned int sample_rate_mul = ((get_hdafmt_rate(fmt) >> 3) & 3) + 1;
  2605. unsigned int rate_multi = sample_rate_mul / sample_rate_div;
  2606. if ((rate_multi != 1) && (rate_multi != 2) && (rate_multi != 4)) {
  2607. codec_dbg(codec, "bad rate multiple\n");
  2608. return -EINVAL;
  2609. }
  2610. num_chans = get_hdafmt_chs(fmt) + 1;
  2611. return dsp_allocate_ports(codec, num_chans, rate_multi, port_map);
  2612. }
  2613. /*
  2614. * free DSP ports
  2615. */
  2616. static int dsp_free_ports(struct hda_codec *codec)
  2617. {
  2618. int status;
  2619. codec_dbg(codec, " dsp_free_ports() -- begin\n");
  2620. status = dsp_free_router_ports(codec);
  2621. if (status < 0) {
  2622. codec_dbg(codec, "free router ports fail\n");
  2623. return status;
  2624. }
  2625. codec_dbg(codec, " dsp_free_ports() -- complete\n");
  2626. return status;
  2627. }
  2628. /*
  2629. * HDA DMA engine stuffs for DSP code download
  2630. */
  2631. struct dma_engine {
  2632. struct hda_codec *codec;
  2633. unsigned short m_converter_format;
  2634. struct snd_dma_buffer *dmab;
  2635. unsigned int buf_size;
  2636. };
  2637. enum dma_state {
  2638. DMA_STATE_STOP = 0,
  2639. DMA_STATE_RUN = 1
  2640. };
  2641. static int dma_convert_to_hda_format(struct hda_codec *codec,
  2642. unsigned int sample_rate,
  2643. unsigned short channels,
  2644. unsigned short *hda_format)
  2645. {
  2646. unsigned int format_val;
  2647. format_val = snd_hdac_calc_stream_format(sample_rate,
  2648. channels, SNDRV_PCM_FORMAT_S32_LE, 32, 0);
  2649. if (hda_format)
  2650. *hda_format = (unsigned short)format_val;
  2651. return 0;
  2652. }
  2653. /*
  2654. * Reset DMA for DSP download
  2655. */
  2656. static int dma_reset(struct dma_engine *dma)
  2657. {
  2658. struct hda_codec *codec = dma->codec;
  2659. struct ca0132_spec *spec = codec->spec;
  2660. int status;
  2661. if (dma->dmab->area)
  2662. snd_hda_codec_load_dsp_cleanup(codec, dma->dmab);
  2663. status = snd_hda_codec_load_dsp_prepare(codec,
  2664. dma->m_converter_format,
  2665. dma->buf_size,
  2666. dma->dmab);
  2667. if (status < 0)
  2668. return status;
  2669. spec->dsp_stream_id = status;
  2670. return 0;
  2671. }
  2672. static int dma_set_state(struct dma_engine *dma, enum dma_state state)
  2673. {
  2674. bool cmd;
  2675. switch (state) {
  2676. case DMA_STATE_STOP:
  2677. cmd = false;
  2678. break;
  2679. case DMA_STATE_RUN:
  2680. cmd = true;
  2681. break;
  2682. default:
  2683. return 0;
  2684. }
  2685. snd_hda_codec_load_dsp_trigger(dma->codec, cmd);
  2686. return 0;
  2687. }
  2688. static unsigned int dma_get_buffer_size(struct dma_engine *dma)
  2689. {
  2690. return dma->dmab->bytes;
  2691. }
  2692. static unsigned char *dma_get_buffer_addr(struct dma_engine *dma)
  2693. {
  2694. return dma->dmab->area;
  2695. }
  2696. static int dma_xfer(struct dma_engine *dma,
  2697. const unsigned int *data,
  2698. unsigned int count)
  2699. {
  2700. memcpy(dma->dmab->area, data, count);
  2701. return 0;
  2702. }
  2703. static void dma_get_converter_format(
  2704. struct dma_engine *dma,
  2705. unsigned short *format)
  2706. {
  2707. if (format)
  2708. *format = dma->m_converter_format;
  2709. }
  2710. static unsigned int dma_get_stream_id(struct dma_engine *dma)
  2711. {
  2712. struct ca0132_spec *spec = dma->codec->spec;
  2713. return spec->dsp_stream_id;
  2714. }
  2715. struct dsp_image_seg {
  2716. u32 magic;
  2717. u32 chip_addr;
  2718. u32 count;
  2719. u32 data[];
  2720. };
  2721. static const u32 g_magic_value = 0x4c46584d;
  2722. static const u32 g_chip_addr_magic_value = 0xFFFFFF01;
  2723. static bool is_valid(const struct dsp_image_seg *p)
  2724. {
  2725. return p->magic == g_magic_value;
  2726. }
  2727. static bool is_hci_prog_list_seg(const struct dsp_image_seg *p)
  2728. {
  2729. return g_chip_addr_magic_value == p->chip_addr;
  2730. }
  2731. static bool is_last(const struct dsp_image_seg *p)
  2732. {
  2733. return p->count == 0;
  2734. }
  2735. static size_t dsp_sizeof(const struct dsp_image_seg *p)
  2736. {
  2737. return struct_size(p, data, p->count);
  2738. }
  2739. static const struct dsp_image_seg *get_next_seg_ptr(
  2740. const struct dsp_image_seg *p)
  2741. {
  2742. return (struct dsp_image_seg *)((unsigned char *)(p) + dsp_sizeof(p));
  2743. }
  2744. /*
  2745. * CA0132 chip DSP transfer stuffs. For DSP download.
  2746. */
  2747. #define INVALID_DMA_CHANNEL (~0U)
  2748. /*
  2749. * Program a list of address/data pairs via the ChipIO widget.
  2750. * The segment data is in the format of successive pairs of words.
  2751. * These are repeated as indicated by the segment's count field.
  2752. */
  2753. static int dspxfr_hci_write(struct hda_codec *codec,
  2754. const struct dsp_image_seg *fls)
  2755. {
  2756. int status;
  2757. const u32 *data;
  2758. unsigned int count;
  2759. if (fls == NULL || fls->chip_addr != g_chip_addr_magic_value) {
  2760. codec_dbg(codec, "hci_write invalid params\n");
  2761. return -EINVAL;
  2762. }
  2763. count = fls->count;
  2764. data = (u32 *)(fls->data);
  2765. while (count >= 2) {
  2766. status = chipio_write(codec, data[0], data[1]);
  2767. if (status < 0) {
  2768. codec_dbg(codec, "hci_write chipio failed\n");
  2769. return status;
  2770. }
  2771. count -= 2;
  2772. data += 2;
  2773. }
  2774. return 0;
  2775. }
  2776. /**
  2777. * dspxfr_one_seg - Write a block of data into DSP code or data RAM using pre-allocated DMA engine.
  2778. *
  2779. * @codec: the HDA codec
  2780. * @fls: pointer to a fast load image
  2781. * @reloc: Relocation address for loading single-segment overlays, or 0 for
  2782. * no relocation
  2783. * @dma_engine: pointer to DMA engine to be used for DSP download
  2784. * @dma_chan: The number of DMA channels used for DSP download
  2785. * @port_map_mask: port mapping
  2786. * @ovly: TRUE if overlay format is required
  2787. *
  2788. * Returns zero or a negative error code.
  2789. */
  2790. static int dspxfr_one_seg(struct hda_codec *codec,
  2791. const struct dsp_image_seg *fls,
  2792. unsigned int reloc,
  2793. struct dma_engine *dma_engine,
  2794. unsigned int dma_chan,
  2795. unsigned int port_map_mask,
  2796. bool ovly)
  2797. {
  2798. int status = 0;
  2799. bool comm_dma_setup_done = false;
  2800. const unsigned int *data;
  2801. unsigned int chip_addx;
  2802. unsigned int words_to_write;
  2803. unsigned int buffer_size_words;
  2804. unsigned char *buffer_addx;
  2805. unsigned short hda_format;
  2806. unsigned int sample_rate_div;
  2807. unsigned int sample_rate_mul;
  2808. unsigned int num_chans;
  2809. unsigned int hda_frame_size_words;
  2810. unsigned int remainder_words;
  2811. const u32 *data_remainder;
  2812. u32 chip_addx_remainder;
  2813. unsigned int run_size_words;
  2814. const struct dsp_image_seg *hci_write = NULL;
  2815. unsigned long timeout;
  2816. bool dma_active;
  2817. if (fls == NULL)
  2818. return -EINVAL;
  2819. if (is_hci_prog_list_seg(fls)) {
  2820. hci_write = fls;
  2821. fls = get_next_seg_ptr(fls);
  2822. }
  2823. if (hci_write && (!fls || is_last(fls))) {
  2824. codec_dbg(codec, "hci_write\n");
  2825. return dspxfr_hci_write(codec, hci_write);
  2826. }
  2827. if (fls == NULL || dma_engine == NULL || port_map_mask == 0) {
  2828. codec_dbg(codec, "Invalid Params\n");
  2829. return -EINVAL;
  2830. }
  2831. data = fls->data;
  2832. chip_addx = fls->chip_addr;
  2833. words_to_write = fls->count;
  2834. if (!words_to_write)
  2835. return hci_write ? dspxfr_hci_write(codec, hci_write) : 0;
  2836. if (reloc)
  2837. chip_addx = (chip_addx & (0xFFFF0000 << 2)) + (reloc << 2);
  2838. if (!UC_RANGE(chip_addx, words_to_write) &&
  2839. !X_RANGE_ALL(chip_addx, words_to_write) &&
  2840. !Y_RANGE_ALL(chip_addx, words_to_write)) {
  2841. codec_dbg(codec, "Invalid chip_addx Params\n");
  2842. return -EINVAL;
  2843. }
  2844. buffer_size_words = (unsigned int)dma_get_buffer_size(dma_engine) /
  2845. sizeof(u32);
  2846. buffer_addx = dma_get_buffer_addr(dma_engine);
  2847. if (buffer_addx == NULL) {
  2848. codec_dbg(codec, "dma_engine buffer NULL\n");
  2849. return -EINVAL;
  2850. }
  2851. dma_get_converter_format(dma_engine, &hda_format);
  2852. sample_rate_div = ((get_hdafmt_rate(hda_format) >> 0) & 3) + 1;
  2853. sample_rate_mul = ((get_hdafmt_rate(hda_format) >> 3) & 3) + 1;
  2854. num_chans = get_hdafmt_chs(hda_format) + 1;
  2855. hda_frame_size_words = ((sample_rate_div == 0) ? 0 :
  2856. (num_chans * sample_rate_mul / sample_rate_div));
  2857. if (hda_frame_size_words == 0) {
  2858. codec_dbg(codec, "frmsz zero\n");
  2859. return -EINVAL;
  2860. }
  2861. buffer_size_words = min(buffer_size_words,
  2862. (unsigned int)(UC_RANGE(chip_addx, 1) ?
  2863. 65536 : 32768));
  2864. buffer_size_words -= buffer_size_words % hda_frame_size_words;
  2865. codec_dbg(codec,
  2866. "chpadr=0x%08x frmsz=%u nchan=%u "
  2867. "rate_mul=%u div=%u bufsz=%u\n",
  2868. chip_addx, hda_frame_size_words, num_chans,
  2869. sample_rate_mul, sample_rate_div, buffer_size_words);
  2870. if (buffer_size_words < hda_frame_size_words) {
  2871. codec_dbg(codec, "dspxfr_one_seg:failed\n");
  2872. return -EINVAL;
  2873. }
  2874. remainder_words = words_to_write % hda_frame_size_words;
  2875. data_remainder = data;
  2876. chip_addx_remainder = chip_addx;
  2877. data += remainder_words;
  2878. chip_addx += remainder_words*sizeof(u32);
  2879. words_to_write -= remainder_words;
  2880. while (words_to_write != 0) {
  2881. run_size_words = min(buffer_size_words, words_to_write);
  2882. codec_dbg(codec, "dspxfr (seg loop)cnt=%u rs=%u remainder=%u\n",
  2883. words_to_write, run_size_words, remainder_words);
  2884. dma_xfer(dma_engine, data, run_size_words*sizeof(u32));
  2885. if (!comm_dma_setup_done) {
  2886. status = dsp_dma_stop(codec, dma_chan, ovly);
  2887. if (status < 0)
  2888. return status;
  2889. status = dsp_dma_setup_common(codec, chip_addx,
  2890. dma_chan, port_map_mask, ovly);
  2891. if (status < 0)
  2892. return status;
  2893. comm_dma_setup_done = true;
  2894. }
  2895. status = dsp_dma_setup(codec, chip_addx,
  2896. run_size_words, dma_chan);
  2897. if (status < 0)
  2898. return status;
  2899. status = dsp_dma_start(codec, dma_chan, ovly);
  2900. if (status < 0)
  2901. return status;
  2902. if (!dsp_is_dma_active(codec, dma_chan)) {
  2903. codec_dbg(codec, "dspxfr:DMA did not start\n");
  2904. return -EIO;
  2905. }
  2906. status = dma_set_state(dma_engine, DMA_STATE_RUN);
  2907. if (status < 0)
  2908. return status;
  2909. if (remainder_words != 0) {
  2910. status = chipio_write_multiple(codec,
  2911. chip_addx_remainder,
  2912. data_remainder,
  2913. remainder_words);
  2914. if (status < 0)
  2915. return status;
  2916. remainder_words = 0;
  2917. }
  2918. if (hci_write) {
  2919. status = dspxfr_hci_write(codec, hci_write);
  2920. if (status < 0)
  2921. return status;
  2922. hci_write = NULL;
  2923. }
  2924. timeout = jiffies + msecs_to_jiffies(2000);
  2925. do {
  2926. dma_active = dsp_is_dma_active(codec, dma_chan);
  2927. if (!dma_active)
  2928. break;
  2929. msleep(20);
  2930. } while (time_before(jiffies, timeout));
  2931. if (dma_active)
  2932. break;
  2933. codec_dbg(codec, "+++++ DMA complete\n");
  2934. dma_set_state(dma_engine, DMA_STATE_STOP);
  2935. status = dma_reset(dma_engine);
  2936. if (status < 0)
  2937. return status;
  2938. data += run_size_words;
  2939. chip_addx += run_size_words*sizeof(u32);
  2940. words_to_write -= run_size_words;
  2941. }
  2942. if (remainder_words != 0) {
  2943. status = chipio_write_multiple(codec, chip_addx_remainder,
  2944. data_remainder, remainder_words);
  2945. }
  2946. return status;
  2947. }
  2948. /**
  2949. * dspxfr_image - Write the entire DSP image of a DSP code/data overlay to DSP memories
  2950. *
  2951. * @codec: the HDA codec
  2952. * @fls_data: pointer to a fast load image
  2953. * @reloc: Relocation address for loading single-segment overlays, or 0 for
  2954. * no relocation
  2955. * @sample_rate: sampling rate of the stream used for DSP download
  2956. * @channels: channels of the stream used for DSP download
  2957. * @ovly: TRUE if overlay format is required
  2958. *
  2959. * Returns zero or a negative error code.
  2960. */
  2961. static int dspxfr_image(struct hda_codec *codec,
  2962. const struct dsp_image_seg *fls_data,
  2963. unsigned int reloc,
  2964. unsigned int sample_rate,
  2965. unsigned short channels,
  2966. bool ovly)
  2967. {
  2968. struct ca0132_spec *spec = codec->spec;
  2969. int status;
  2970. unsigned short hda_format = 0;
  2971. unsigned int response;
  2972. unsigned char stream_id = 0;
  2973. struct dma_engine *dma_engine;
  2974. unsigned int dma_chan;
  2975. unsigned int port_map_mask;
  2976. if (fls_data == NULL)
  2977. return -EINVAL;
  2978. dma_engine = kzalloc(sizeof(*dma_engine), GFP_KERNEL);
  2979. if (!dma_engine)
  2980. return -ENOMEM;
  2981. dma_engine->dmab = kzalloc(sizeof(*dma_engine->dmab), GFP_KERNEL);
  2982. if (!dma_engine->dmab) {
  2983. kfree(dma_engine);
  2984. return -ENOMEM;
  2985. }
  2986. dma_engine->codec = codec;
  2987. dma_convert_to_hda_format(codec, sample_rate, channels, &hda_format);
  2988. dma_engine->m_converter_format = hda_format;
  2989. dma_engine->buf_size = (ovly ? DSP_DMA_WRITE_BUFLEN_OVLY :
  2990. DSP_DMA_WRITE_BUFLEN_INIT) * 2;
  2991. dma_chan = ovly ? INVALID_DMA_CHANNEL : 0;
  2992. status = codec_set_converter_format(codec, WIDGET_CHIP_CTRL,
  2993. hda_format, &response);
  2994. if (status < 0) {
  2995. codec_dbg(codec, "set converter format fail\n");
  2996. goto exit;
  2997. }
  2998. status = snd_hda_codec_load_dsp_prepare(codec,
  2999. dma_engine->m_converter_format,
  3000. dma_engine->buf_size,
  3001. dma_engine->dmab);
  3002. if (status < 0)
  3003. goto exit;
  3004. spec->dsp_stream_id = status;
  3005. if (ovly) {
  3006. status = dspio_alloc_dma_chan(codec, &dma_chan);
  3007. if (status < 0) {
  3008. codec_dbg(codec, "alloc dmachan fail\n");
  3009. dma_chan = INVALID_DMA_CHANNEL;
  3010. goto exit;
  3011. }
  3012. }
  3013. port_map_mask = 0;
  3014. status = dsp_allocate_ports_format(codec, hda_format,
  3015. &port_map_mask);
  3016. if (status < 0) {
  3017. codec_dbg(codec, "alloc ports fail\n");
  3018. goto exit;
  3019. }
  3020. stream_id = dma_get_stream_id(dma_engine);
  3021. status = codec_set_converter_stream_channel(codec,
  3022. WIDGET_CHIP_CTRL, stream_id, 0, &response);
  3023. if (status < 0) {
  3024. codec_dbg(codec, "set stream chan fail\n");
  3025. goto exit;
  3026. }
  3027. while ((fls_data != NULL) && !is_last(fls_data)) {
  3028. if (!is_valid(fls_data)) {
  3029. codec_dbg(codec, "FLS check fail\n");
  3030. status = -EINVAL;
  3031. goto exit;
  3032. }
  3033. status = dspxfr_one_seg(codec, fls_data, reloc,
  3034. dma_engine, dma_chan,
  3035. port_map_mask, ovly);
  3036. if (status < 0)
  3037. break;
  3038. if (is_hci_prog_list_seg(fls_data))
  3039. fls_data = get_next_seg_ptr(fls_data);
  3040. if ((fls_data != NULL) && !is_last(fls_data))
  3041. fls_data = get_next_seg_ptr(fls_data);
  3042. }
  3043. if (port_map_mask != 0)
  3044. status = dsp_free_ports(codec);
  3045. if (status < 0)
  3046. goto exit;
  3047. status = codec_set_converter_stream_channel(codec,
  3048. WIDGET_CHIP_CTRL, 0, 0, &response);
  3049. exit:
  3050. if (ovly && (dma_chan != INVALID_DMA_CHANNEL))
  3051. dspio_free_dma_chan(codec, dma_chan);
  3052. if (dma_engine->dmab->area)
  3053. snd_hda_codec_load_dsp_cleanup(codec, dma_engine->dmab);
  3054. kfree(dma_engine->dmab);
  3055. kfree(dma_engine);
  3056. return status;
  3057. }
  3058. /*
  3059. * CA0132 DSP download stuffs.
  3060. */
  3061. static void dspload_post_setup(struct hda_codec *codec)
  3062. {
  3063. struct ca0132_spec *spec = codec->spec;
  3064. codec_dbg(codec, "---- dspload_post_setup ------\n");
  3065. if (!ca0132_use_alt_functions(spec)) {
  3066. /*set DSP speaker to 2.0 configuration*/
  3067. chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x18), 0x08080080);
  3068. chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x19), 0x3f800000);
  3069. /*update write pointer*/
  3070. chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x29), 0x00000002);
  3071. }
  3072. }
  3073. /**
  3074. * dspload_image - Download DSP from a DSP Image Fast Load structure.
  3075. *
  3076. * @codec: the HDA codec
  3077. * @fls: pointer to a fast load image
  3078. * @ovly: TRUE if overlay format is required
  3079. * @reloc: Relocation address for loading single-segment overlays, or 0 for
  3080. * no relocation
  3081. * @autostart: TRUE if DSP starts after loading; ignored if ovly is TRUE
  3082. * @router_chans: number of audio router channels to be allocated (0 means use
  3083. * internal defaults; max is 32)
  3084. *
  3085. * Download DSP from a DSP Image Fast Load structure. This structure is a
  3086. * linear, non-constant sized element array of structures, each of which
  3087. * contain the count of the data to be loaded, the data itself, and the
  3088. * corresponding starting chip address of the starting data location.
  3089. * Returns zero or a negative error code.
  3090. */
  3091. static int dspload_image(struct hda_codec *codec,
  3092. const struct dsp_image_seg *fls,
  3093. bool ovly,
  3094. unsigned int reloc,
  3095. bool autostart,
  3096. int router_chans)
  3097. {
  3098. int status = 0;
  3099. unsigned int sample_rate;
  3100. unsigned short channels;
  3101. codec_dbg(codec, "---- dspload_image begin ------\n");
  3102. if (router_chans == 0) {
  3103. if (!ovly)
  3104. router_chans = DMA_TRANSFER_FRAME_SIZE_NWORDS;
  3105. else
  3106. router_chans = DMA_OVERLAY_FRAME_SIZE_NWORDS;
  3107. }
  3108. sample_rate = 48000;
  3109. channels = (unsigned short)router_chans;
  3110. while (channels > 16) {
  3111. sample_rate *= 2;
  3112. channels /= 2;
  3113. }
  3114. do {
  3115. codec_dbg(codec, "Ready to program DMA\n");
  3116. if (!ovly)
  3117. status = dsp_reset(codec);
  3118. if (status < 0)
  3119. break;
  3120. codec_dbg(codec, "dsp_reset() complete\n");
  3121. status = dspxfr_image(codec, fls, reloc, sample_rate, channels,
  3122. ovly);
  3123. if (status < 0)
  3124. break;
  3125. codec_dbg(codec, "dspxfr_image() complete\n");
  3126. if (autostart && !ovly) {
  3127. dspload_post_setup(codec);
  3128. status = dsp_set_run_state(codec);
  3129. }
  3130. codec_dbg(codec, "LOAD FINISHED\n");
  3131. } while (0);
  3132. return status;
  3133. }
  3134. #ifdef CONFIG_SND_HDA_CODEC_CA0132_DSP
  3135. static bool dspload_is_loaded(struct hda_codec *codec)
  3136. {
  3137. unsigned int data = 0;
  3138. int status = 0;
  3139. status = chipio_read(codec, 0x40004, &data);
  3140. if ((status < 0) || (data != 1))
  3141. return false;
  3142. return true;
  3143. }
  3144. #else
  3145. #define dspload_is_loaded(codec) false
  3146. #endif
  3147. static bool dspload_wait_loaded(struct hda_codec *codec)
  3148. {
  3149. unsigned long timeout = jiffies + msecs_to_jiffies(2000);
  3150. do {
  3151. if (dspload_is_loaded(codec)) {
  3152. codec_info(codec, "ca0132 DSP downloaded and running\n");
  3153. return true;
  3154. }
  3155. msleep(20);
  3156. } while (time_before(jiffies, timeout));
  3157. codec_err(codec, "ca0132 failed to download DSP\n");
  3158. return false;
  3159. }
  3160. /*
  3161. * ca0113 related functions. The ca0113 acts as the HDA bus for the pci-e
  3162. * based cards, and has a second mmio region, region2, that's used for special
  3163. * commands.
  3164. */
  3165. /*
  3166. * For cards with PCI-E region2 (Sound Blaster Z/ZxR, Recon3D, and AE-5)
  3167. * the mmio address 0x320 is used to set GPIO pins. The format for the data
  3168. * The first eight bits are just the number of the pin. So far, I've only seen
  3169. * this number go to 7.
  3170. * AE-5 note: The AE-5 seems to use pins 2 and 3 to somehow set the color value
  3171. * of the on-card LED. It seems to use pin 2 for data, then toggles 3 to on and
  3172. * then off to send that bit.
  3173. */
  3174. static void ca0113_mmio_gpio_set(struct hda_codec *codec, unsigned int gpio_pin,
  3175. bool enable)
  3176. {
  3177. struct ca0132_spec *spec = codec->spec;
  3178. unsigned short gpio_data;
  3179. gpio_data = gpio_pin & 0xF;
  3180. gpio_data |= ((enable << 8) & 0x100);
  3181. writew(gpio_data, spec->mem_base + 0x320);
  3182. }
  3183. /*
  3184. * Special pci region2 commands that are only used by the AE-5. They follow
  3185. * a set format, and require reads at certain points to seemingly 'clear'
  3186. * the response data. My first tests didn't do these reads, and would cause
  3187. * the card to get locked up until the memory was read. These commands
  3188. * seem to work with three distinct values that I've taken to calling group,
  3189. * target-id, and value.
  3190. */
  3191. static void ca0113_mmio_command_set(struct hda_codec *codec, unsigned int group,
  3192. unsigned int target, unsigned int value)
  3193. {
  3194. struct ca0132_spec *spec = codec->spec;
  3195. unsigned int write_val;
  3196. writel(0x0000007e, spec->mem_base + 0x210);
  3197. readl(spec->mem_base + 0x210);
  3198. writel(0x0000005a, spec->mem_base + 0x210);
  3199. readl(spec->mem_base + 0x210);
  3200. readl(spec->mem_base + 0x210);
  3201. writel(0x00800005, spec->mem_base + 0x20c);
  3202. writel(group, spec->mem_base + 0x804);
  3203. writel(0x00800005, spec->mem_base + 0x20c);
  3204. write_val = (target & 0xff);
  3205. write_val |= (value << 8);
  3206. writel(write_val, spec->mem_base + 0x204);
  3207. /*
  3208. * Need delay here or else it goes too fast and works inconsistently.
  3209. */
  3210. msleep(20);
  3211. readl(spec->mem_base + 0x860);
  3212. readl(spec->mem_base + 0x854);
  3213. readl(spec->mem_base + 0x840);
  3214. writel(0x00800004, spec->mem_base + 0x20c);
  3215. writel(0x00000000, spec->mem_base + 0x210);
  3216. readl(spec->mem_base + 0x210);
  3217. readl(spec->mem_base + 0x210);
  3218. }
  3219. /*
  3220. * This second type of command is used for setting the sound filter type.
  3221. */
  3222. static void ca0113_mmio_command_set_type2(struct hda_codec *codec,
  3223. unsigned int group, unsigned int target, unsigned int value)
  3224. {
  3225. struct ca0132_spec *spec = codec->spec;
  3226. unsigned int write_val;
  3227. writel(0x0000007e, spec->mem_base + 0x210);
  3228. readl(spec->mem_base + 0x210);
  3229. writel(0x0000005a, spec->mem_base + 0x210);
  3230. readl(spec->mem_base + 0x210);
  3231. readl(spec->mem_base + 0x210);
  3232. writel(0x00800003, spec->mem_base + 0x20c);
  3233. writel(group, spec->mem_base + 0x804);
  3234. writel(0x00800005, spec->mem_base + 0x20c);
  3235. write_val = (target & 0xff);
  3236. write_val |= (value << 8);
  3237. writel(write_val, spec->mem_base + 0x204);
  3238. msleep(20);
  3239. readl(spec->mem_base + 0x860);
  3240. readl(spec->mem_base + 0x854);
  3241. readl(spec->mem_base + 0x840);
  3242. writel(0x00800004, spec->mem_base + 0x20c);
  3243. writel(0x00000000, spec->mem_base + 0x210);
  3244. readl(spec->mem_base + 0x210);
  3245. readl(spec->mem_base + 0x210);
  3246. }
  3247. /*
  3248. * Setup GPIO for the other variants of Core3D.
  3249. */
  3250. /*
  3251. * Sets up the GPIO pins so that they are discoverable. If this isn't done,
  3252. * the card shows as having no GPIO pins.
  3253. */
  3254. static void ca0132_gpio_init(struct hda_codec *codec)
  3255. {
  3256. struct ca0132_spec *spec = codec->spec;
  3257. switch (ca0132_quirk(spec)) {
  3258. case QUIRK_SBZ:
  3259. case QUIRK_AE5:
  3260. case QUIRK_AE7:
  3261. snd_hda_codec_write(codec, 0x01, 0, 0x793, 0x00);
  3262. snd_hda_codec_write(codec, 0x01, 0, 0x794, 0x53);
  3263. snd_hda_codec_write(codec, 0x01, 0, 0x790, 0x23);
  3264. break;
  3265. case QUIRK_R3DI:
  3266. snd_hda_codec_write(codec, 0x01, 0, 0x793, 0x00);
  3267. snd_hda_codec_write(codec, 0x01, 0, 0x794, 0x5B);
  3268. break;
  3269. default:
  3270. break;
  3271. }
  3272. }
  3273. /* Sets the GPIO for audio output. */
  3274. static void ca0132_gpio_setup(struct hda_codec *codec)
  3275. {
  3276. struct ca0132_spec *spec = codec->spec;
  3277. switch (ca0132_quirk(spec)) {
  3278. case QUIRK_SBZ:
  3279. snd_hda_codec_write(codec, 0x01, 0,
  3280. AC_VERB_SET_GPIO_DIRECTION, 0x07);
  3281. snd_hda_codec_write(codec, 0x01, 0,
  3282. AC_VERB_SET_GPIO_MASK, 0x07);
  3283. snd_hda_codec_write(codec, 0x01, 0,
  3284. AC_VERB_SET_GPIO_DATA, 0x04);
  3285. snd_hda_codec_write(codec, 0x01, 0,
  3286. AC_VERB_SET_GPIO_DATA, 0x06);
  3287. break;
  3288. case QUIRK_R3DI:
  3289. snd_hda_codec_write(codec, 0x01, 0,
  3290. AC_VERB_SET_GPIO_DIRECTION, 0x1E);
  3291. snd_hda_codec_write(codec, 0x01, 0,
  3292. AC_VERB_SET_GPIO_MASK, 0x1F);
  3293. snd_hda_codec_write(codec, 0x01, 0,
  3294. AC_VERB_SET_GPIO_DATA, 0x0C);
  3295. break;
  3296. default:
  3297. break;
  3298. }
  3299. }
  3300. /*
  3301. * GPIO control functions for the Recon3D integrated.
  3302. */
  3303. enum r3di_gpio_bit {
  3304. /* Bit 1 - Switch between front/rear mic. 0 = rear, 1 = front */
  3305. R3DI_MIC_SELECT_BIT = 1,
  3306. /* Bit 2 - Switch between headphone/line out. 0 = Headphone, 1 = Line */
  3307. R3DI_OUT_SELECT_BIT = 2,
  3308. /*
  3309. * I dunno what this actually does, but it stays on until the dsp
  3310. * is downloaded.
  3311. */
  3312. R3DI_GPIO_DSP_DOWNLOADING = 3,
  3313. /*
  3314. * Same as above, no clue what it does, but it comes on after the dsp
  3315. * is downloaded.
  3316. */
  3317. R3DI_GPIO_DSP_DOWNLOADED = 4
  3318. };
  3319. enum r3di_mic_select {
  3320. /* Set GPIO bit 1 to 0 for rear mic */
  3321. R3DI_REAR_MIC = 0,
  3322. /* Set GPIO bit 1 to 1 for front microphone*/
  3323. R3DI_FRONT_MIC = 1
  3324. };
  3325. enum r3di_out_select {
  3326. /* Set GPIO bit 2 to 0 for headphone */
  3327. R3DI_HEADPHONE_OUT = 0,
  3328. /* Set GPIO bit 2 to 1 for speaker */
  3329. R3DI_LINE_OUT = 1
  3330. };
  3331. enum r3di_dsp_status {
  3332. /* Set GPIO bit 3 to 1 until DSP is downloaded */
  3333. R3DI_DSP_DOWNLOADING = 0,
  3334. /* Set GPIO bit 4 to 1 once DSP is downloaded */
  3335. R3DI_DSP_DOWNLOADED = 1
  3336. };
  3337. static void r3di_gpio_mic_set(struct hda_codec *codec,
  3338. enum r3di_mic_select cur_mic)
  3339. {
  3340. unsigned int cur_gpio;
  3341. /* Get the current GPIO Data setup */
  3342. cur_gpio = snd_hda_codec_read(codec, 0x01, 0, AC_VERB_GET_GPIO_DATA, 0);
  3343. switch (cur_mic) {
  3344. case R3DI_REAR_MIC:
  3345. cur_gpio &= ~(1 << R3DI_MIC_SELECT_BIT);
  3346. break;
  3347. case R3DI_FRONT_MIC:
  3348. cur_gpio |= (1 << R3DI_MIC_SELECT_BIT);
  3349. break;
  3350. }
  3351. snd_hda_codec_write(codec, codec->core.afg, 0,
  3352. AC_VERB_SET_GPIO_DATA, cur_gpio);
  3353. }
  3354. static void r3di_gpio_dsp_status_set(struct hda_codec *codec,
  3355. enum r3di_dsp_status dsp_status)
  3356. {
  3357. unsigned int cur_gpio;
  3358. /* Get the current GPIO Data setup */
  3359. cur_gpio = snd_hda_codec_read(codec, 0x01, 0, AC_VERB_GET_GPIO_DATA, 0);
  3360. switch (dsp_status) {
  3361. case R3DI_DSP_DOWNLOADING:
  3362. cur_gpio |= (1 << R3DI_GPIO_DSP_DOWNLOADING);
  3363. snd_hda_codec_write(codec, codec->core.afg, 0,
  3364. AC_VERB_SET_GPIO_DATA, cur_gpio);
  3365. break;
  3366. case R3DI_DSP_DOWNLOADED:
  3367. /* Set DOWNLOADING bit to 0. */
  3368. cur_gpio &= ~(1 << R3DI_GPIO_DSP_DOWNLOADING);
  3369. snd_hda_codec_write(codec, codec->core.afg, 0,
  3370. AC_VERB_SET_GPIO_DATA, cur_gpio);
  3371. cur_gpio |= (1 << R3DI_GPIO_DSP_DOWNLOADED);
  3372. break;
  3373. }
  3374. snd_hda_codec_write(codec, codec->core.afg, 0,
  3375. AC_VERB_SET_GPIO_DATA, cur_gpio);
  3376. }
  3377. /*
  3378. * PCM callbacks
  3379. */
  3380. static int ca0132_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  3381. struct hda_codec *codec,
  3382. unsigned int stream_tag,
  3383. unsigned int format,
  3384. struct snd_pcm_substream *substream)
  3385. {
  3386. struct ca0132_spec *spec = codec->spec;
  3387. snd_hda_codec_setup_stream(codec, spec->dacs[0], stream_tag, 0, format);
  3388. return 0;
  3389. }
  3390. static int ca0132_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
  3391. struct hda_codec *codec,
  3392. struct snd_pcm_substream *substream)
  3393. {
  3394. struct ca0132_spec *spec = codec->spec;
  3395. if (spec->dsp_state == DSP_DOWNLOADING)
  3396. return 0;
  3397. /*If Playback effects are on, allow stream some time to flush
  3398. *effects tail*/
  3399. if (spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID])
  3400. msleep(50);
  3401. snd_hda_codec_cleanup_stream(codec, spec->dacs[0]);
  3402. return 0;
  3403. }
  3404. static unsigned int ca0132_playback_pcm_delay(struct hda_pcm_stream *info,
  3405. struct hda_codec *codec,
  3406. struct snd_pcm_substream *substream)
  3407. {
  3408. struct ca0132_spec *spec = codec->spec;
  3409. unsigned int latency = DSP_PLAYBACK_INIT_LATENCY;
  3410. struct snd_pcm_runtime *runtime = substream->runtime;
  3411. if (spec->dsp_state != DSP_DOWNLOADED)
  3412. return 0;
  3413. /* Add latency if playback enhancement and either effect is enabled. */
  3414. if (spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID]) {
  3415. if ((spec->effects_switch[SURROUND - EFFECT_START_NID]) ||
  3416. (spec->effects_switch[DIALOG_PLUS - EFFECT_START_NID]))
  3417. latency += DSP_PLAY_ENHANCEMENT_LATENCY;
  3418. }
  3419. /* Applying Speaker EQ adds latency as well. */
  3420. if (spec->cur_out_type == SPEAKER_OUT)
  3421. latency += DSP_SPEAKER_OUT_LATENCY;
  3422. return (latency * runtime->rate) / 1000;
  3423. }
  3424. /*
  3425. * Digital out
  3426. */
  3427. static int ca0132_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
  3428. struct hda_codec *codec,
  3429. struct snd_pcm_substream *substream)
  3430. {
  3431. struct ca0132_spec *spec = codec->spec;
  3432. return snd_hda_multi_out_dig_open(codec, &spec->multiout);
  3433. }
  3434. static int ca0132_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  3435. struct hda_codec *codec,
  3436. unsigned int stream_tag,
  3437. unsigned int format,
  3438. struct snd_pcm_substream *substream)
  3439. {
  3440. struct ca0132_spec *spec = codec->spec;
  3441. return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
  3442. stream_tag, format, substream);
  3443. }
  3444. static int ca0132_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
  3445. struct hda_codec *codec,
  3446. struct snd_pcm_substream *substream)
  3447. {
  3448. struct ca0132_spec *spec = codec->spec;
  3449. return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
  3450. }
  3451. static int ca0132_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
  3452. struct hda_codec *codec,
  3453. struct snd_pcm_substream *substream)
  3454. {
  3455. struct ca0132_spec *spec = codec->spec;
  3456. return snd_hda_multi_out_dig_close(codec, &spec->multiout);
  3457. }
  3458. /*
  3459. * Analog capture
  3460. */
  3461. static int ca0132_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
  3462. struct hda_codec *codec,
  3463. unsigned int stream_tag,
  3464. unsigned int format,
  3465. struct snd_pcm_substream *substream)
  3466. {
  3467. snd_hda_codec_setup_stream(codec, hinfo->nid,
  3468. stream_tag, 0, format);
  3469. return 0;
  3470. }
  3471. static int ca0132_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
  3472. struct hda_codec *codec,
  3473. struct snd_pcm_substream *substream)
  3474. {
  3475. struct ca0132_spec *spec = codec->spec;
  3476. if (spec->dsp_state == DSP_DOWNLOADING)
  3477. return 0;
  3478. snd_hda_codec_cleanup_stream(codec, hinfo->nid);
  3479. return 0;
  3480. }
  3481. static unsigned int ca0132_capture_pcm_delay(struct hda_pcm_stream *info,
  3482. struct hda_codec *codec,
  3483. struct snd_pcm_substream *substream)
  3484. {
  3485. struct ca0132_spec *spec = codec->spec;
  3486. unsigned int latency = DSP_CAPTURE_INIT_LATENCY;
  3487. struct snd_pcm_runtime *runtime = substream->runtime;
  3488. if (spec->dsp_state != DSP_DOWNLOADED)
  3489. return 0;
  3490. if (spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID])
  3491. latency += DSP_CRYSTAL_VOICE_LATENCY;
  3492. return (latency * runtime->rate) / 1000;
  3493. }
  3494. /*
  3495. * Controls stuffs.
  3496. */
  3497. /*
  3498. * Mixer controls helpers.
  3499. */
  3500. #define CA0132_CODEC_VOL_MONO(xname, nid, channel, dir) \
  3501. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  3502. .name = xname, \
  3503. .subdevice = HDA_SUBDEV_AMP_FLAG, \
  3504. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
  3505. SNDRV_CTL_ELEM_ACCESS_TLV_READ | \
  3506. SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK, \
  3507. .info = ca0132_volume_info, \
  3508. .get = ca0132_volume_get, \
  3509. .put = ca0132_volume_put, \
  3510. .tlv = { .c = ca0132_volume_tlv }, \
  3511. .private_value = HDA_COMPOSE_AMP_VAL(nid, channel, 0, dir) }
  3512. /*
  3513. * Creates a mixer control that uses defaults of HDA_CODEC_VOL except for the
  3514. * volume put, which is used for setting the DSP volume. This was done because
  3515. * the ca0132 functions were taking too much time and causing lag.
  3516. */
  3517. #define CA0132_ALT_CODEC_VOL_MONO(xname, nid, channel, dir) \
  3518. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  3519. .name = xname, \
  3520. .subdevice = HDA_SUBDEV_AMP_FLAG, \
  3521. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
  3522. SNDRV_CTL_ELEM_ACCESS_TLV_READ | \
  3523. SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK, \
  3524. .info = snd_hda_mixer_amp_volume_info, \
  3525. .get = snd_hda_mixer_amp_volume_get, \
  3526. .put = ca0132_alt_volume_put, \
  3527. .tlv = { .c = snd_hda_mixer_amp_tlv }, \
  3528. .private_value = HDA_COMPOSE_AMP_VAL(nid, channel, 0, dir) }
  3529. #define CA0132_CODEC_MUTE_MONO(xname, nid, channel, dir) \
  3530. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  3531. .name = xname, \
  3532. .subdevice = HDA_SUBDEV_AMP_FLAG, \
  3533. .info = snd_hda_mixer_amp_switch_info, \
  3534. .get = ca0132_switch_get, \
  3535. .put = ca0132_switch_put, \
  3536. .private_value = HDA_COMPOSE_AMP_VAL(nid, channel, 0, dir) }
  3537. /* stereo */
  3538. #define CA0132_CODEC_VOL(xname, nid, dir) \
  3539. CA0132_CODEC_VOL_MONO(xname, nid, 3, dir)
  3540. #define CA0132_ALT_CODEC_VOL(xname, nid, dir) \
  3541. CA0132_ALT_CODEC_VOL_MONO(xname, nid, 3, dir)
  3542. #define CA0132_CODEC_MUTE(xname, nid, dir) \
  3543. CA0132_CODEC_MUTE_MONO(xname, nid, 3, dir)
  3544. /* lookup tables */
  3545. /*
  3546. * Lookup table with decibel values for the DSP. When volume is changed in
  3547. * Windows, the DSP is also sent the dB value in floating point. In Windows,
  3548. * these values have decimal points, probably because the Windows driver
  3549. * actually uses floating point. We can't here, so I made a lookup table of
  3550. * values -90 to 9. -90 is the lowest decibel value for both the ADC's and the
  3551. * DAC's, and 9 is the maximum.
  3552. */
  3553. static const unsigned int float_vol_db_lookup[] = {
  3554. 0xC2B40000, 0xC2B20000, 0xC2B00000, 0xC2AE0000, 0xC2AC0000, 0xC2AA0000,
  3555. 0xC2A80000, 0xC2A60000, 0xC2A40000, 0xC2A20000, 0xC2A00000, 0xC29E0000,
  3556. 0xC29C0000, 0xC29A0000, 0xC2980000, 0xC2960000, 0xC2940000, 0xC2920000,
  3557. 0xC2900000, 0xC28E0000, 0xC28C0000, 0xC28A0000, 0xC2880000, 0xC2860000,
  3558. 0xC2840000, 0xC2820000, 0xC2800000, 0xC27C0000, 0xC2780000, 0xC2740000,
  3559. 0xC2700000, 0xC26C0000, 0xC2680000, 0xC2640000, 0xC2600000, 0xC25C0000,
  3560. 0xC2580000, 0xC2540000, 0xC2500000, 0xC24C0000, 0xC2480000, 0xC2440000,
  3561. 0xC2400000, 0xC23C0000, 0xC2380000, 0xC2340000, 0xC2300000, 0xC22C0000,
  3562. 0xC2280000, 0xC2240000, 0xC2200000, 0xC21C0000, 0xC2180000, 0xC2140000,
  3563. 0xC2100000, 0xC20C0000, 0xC2080000, 0xC2040000, 0xC2000000, 0xC1F80000,
  3564. 0xC1F00000, 0xC1E80000, 0xC1E00000, 0xC1D80000, 0xC1D00000, 0xC1C80000,
  3565. 0xC1C00000, 0xC1B80000, 0xC1B00000, 0xC1A80000, 0xC1A00000, 0xC1980000,
  3566. 0xC1900000, 0xC1880000, 0xC1800000, 0xC1700000, 0xC1600000, 0xC1500000,
  3567. 0xC1400000, 0xC1300000, 0xC1200000, 0xC1100000, 0xC1000000, 0xC0E00000,
  3568. 0xC0C00000, 0xC0A00000, 0xC0800000, 0xC0400000, 0xC0000000, 0xBF800000,
  3569. 0x00000000, 0x3F800000, 0x40000000, 0x40400000, 0x40800000, 0x40A00000,
  3570. 0x40C00000, 0x40E00000, 0x41000000, 0x41100000
  3571. };
  3572. /*
  3573. * This table counts from float 0 to 1 in increments of .01, which is
  3574. * useful for a few different sliders.
  3575. */
  3576. static const unsigned int float_zero_to_one_lookup[] = {
  3577. 0x00000000, 0x3C23D70A, 0x3CA3D70A, 0x3CF5C28F, 0x3D23D70A, 0x3D4CCCCD,
  3578. 0x3D75C28F, 0x3D8F5C29, 0x3DA3D70A, 0x3DB851EC, 0x3DCCCCCD, 0x3DE147AE,
  3579. 0x3DF5C28F, 0x3E051EB8, 0x3E0F5C29, 0x3E19999A, 0x3E23D70A, 0x3E2E147B,
  3580. 0x3E3851EC, 0x3E428F5C, 0x3E4CCCCD, 0x3E570A3D, 0x3E6147AE, 0x3E6B851F,
  3581. 0x3E75C28F, 0x3E800000, 0x3E851EB8, 0x3E8A3D71, 0x3E8F5C29, 0x3E947AE1,
  3582. 0x3E99999A, 0x3E9EB852, 0x3EA3D70A, 0x3EA8F5C3, 0x3EAE147B, 0x3EB33333,
  3583. 0x3EB851EC, 0x3EBD70A4, 0x3EC28F5C, 0x3EC7AE14, 0x3ECCCCCD, 0x3ED1EB85,
  3584. 0x3ED70A3D, 0x3EDC28F6, 0x3EE147AE, 0x3EE66666, 0x3EEB851F, 0x3EF0A3D7,
  3585. 0x3EF5C28F, 0x3EFAE148, 0x3F000000, 0x3F028F5C, 0x3F051EB8, 0x3F07AE14,
  3586. 0x3F0A3D71, 0x3F0CCCCD, 0x3F0F5C29, 0x3F11EB85, 0x3F147AE1, 0x3F170A3D,
  3587. 0x3F19999A, 0x3F1C28F6, 0x3F1EB852, 0x3F2147AE, 0x3F23D70A, 0x3F266666,
  3588. 0x3F28F5C3, 0x3F2B851F, 0x3F2E147B, 0x3F30A3D7, 0x3F333333, 0x3F35C28F,
  3589. 0x3F3851EC, 0x3F3AE148, 0x3F3D70A4, 0x3F400000, 0x3F428F5C, 0x3F451EB8,
  3590. 0x3F47AE14, 0x3F4A3D71, 0x3F4CCCCD, 0x3F4F5C29, 0x3F51EB85, 0x3F547AE1,
  3591. 0x3F570A3D, 0x3F59999A, 0x3F5C28F6, 0x3F5EB852, 0x3F6147AE, 0x3F63D70A,
  3592. 0x3F666666, 0x3F68F5C3, 0x3F6B851F, 0x3F6E147B, 0x3F70A3D7, 0x3F733333,
  3593. 0x3F75C28F, 0x3F7851EC, 0x3F7AE148, 0x3F7D70A4, 0x3F800000
  3594. };
  3595. /*
  3596. * This table counts from float 10 to 1000, which is the range of the x-bass
  3597. * crossover slider in Windows.
  3598. */
  3599. static const unsigned int float_xbass_xover_lookup[] = {
  3600. 0x41200000, 0x41A00000, 0x41F00000, 0x42200000, 0x42480000, 0x42700000,
  3601. 0x428C0000, 0x42A00000, 0x42B40000, 0x42C80000, 0x42DC0000, 0x42F00000,
  3602. 0x43020000, 0x430C0000, 0x43160000, 0x43200000, 0x432A0000, 0x43340000,
  3603. 0x433E0000, 0x43480000, 0x43520000, 0x435C0000, 0x43660000, 0x43700000,
  3604. 0x437A0000, 0x43820000, 0x43870000, 0x438C0000, 0x43910000, 0x43960000,
  3605. 0x439B0000, 0x43A00000, 0x43A50000, 0x43AA0000, 0x43AF0000, 0x43B40000,
  3606. 0x43B90000, 0x43BE0000, 0x43C30000, 0x43C80000, 0x43CD0000, 0x43D20000,
  3607. 0x43D70000, 0x43DC0000, 0x43E10000, 0x43E60000, 0x43EB0000, 0x43F00000,
  3608. 0x43F50000, 0x43FA0000, 0x43FF0000, 0x44020000, 0x44048000, 0x44070000,
  3609. 0x44098000, 0x440C0000, 0x440E8000, 0x44110000, 0x44138000, 0x44160000,
  3610. 0x44188000, 0x441B0000, 0x441D8000, 0x44200000, 0x44228000, 0x44250000,
  3611. 0x44278000, 0x442A0000, 0x442C8000, 0x442F0000, 0x44318000, 0x44340000,
  3612. 0x44368000, 0x44390000, 0x443B8000, 0x443E0000, 0x44408000, 0x44430000,
  3613. 0x44458000, 0x44480000, 0x444A8000, 0x444D0000, 0x444F8000, 0x44520000,
  3614. 0x44548000, 0x44570000, 0x44598000, 0x445C0000, 0x445E8000, 0x44610000,
  3615. 0x44638000, 0x44660000, 0x44688000, 0x446B0000, 0x446D8000, 0x44700000,
  3616. 0x44728000, 0x44750000, 0x44778000, 0x447A0000
  3617. };
  3618. /* The following are for tuning of products */
  3619. #ifdef ENABLE_TUNING_CONTROLS
  3620. static const unsigned int voice_focus_vals_lookup[] = {
  3621. 0x41A00000, 0x41A80000, 0x41B00000, 0x41B80000, 0x41C00000, 0x41C80000,
  3622. 0x41D00000, 0x41D80000, 0x41E00000, 0x41E80000, 0x41F00000, 0x41F80000,
  3623. 0x42000000, 0x42040000, 0x42080000, 0x420C0000, 0x42100000, 0x42140000,
  3624. 0x42180000, 0x421C0000, 0x42200000, 0x42240000, 0x42280000, 0x422C0000,
  3625. 0x42300000, 0x42340000, 0x42380000, 0x423C0000, 0x42400000, 0x42440000,
  3626. 0x42480000, 0x424C0000, 0x42500000, 0x42540000, 0x42580000, 0x425C0000,
  3627. 0x42600000, 0x42640000, 0x42680000, 0x426C0000, 0x42700000, 0x42740000,
  3628. 0x42780000, 0x427C0000, 0x42800000, 0x42820000, 0x42840000, 0x42860000,
  3629. 0x42880000, 0x428A0000, 0x428C0000, 0x428E0000, 0x42900000, 0x42920000,
  3630. 0x42940000, 0x42960000, 0x42980000, 0x429A0000, 0x429C0000, 0x429E0000,
  3631. 0x42A00000, 0x42A20000, 0x42A40000, 0x42A60000, 0x42A80000, 0x42AA0000,
  3632. 0x42AC0000, 0x42AE0000, 0x42B00000, 0x42B20000, 0x42B40000, 0x42B60000,
  3633. 0x42B80000, 0x42BA0000, 0x42BC0000, 0x42BE0000, 0x42C00000, 0x42C20000,
  3634. 0x42C40000, 0x42C60000, 0x42C80000, 0x42CA0000, 0x42CC0000, 0x42CE0000,
  3635. 0x42D00000, 0x42D20000, 0x42D40000, 0x42D60000, 0x42D80000, 0x42DA0000,
  3636. 0x42DC0000, 0x42DE0000, 0x42E00000, 0x42E20000, 0x42E40000, 0x42E60000,
  3637. 0x42E80000, 0x42EA0000, 0x42EC0000, 0x42EE0000, 0x42F00000, 0x42F20000,
  3638. 0x42F40000, 0x42F60000, 0x42F80000, 0x42FA0000, 0x42FC0000, 0x42FE0000,
  3639. 0x43000000, 0x43010000, 0x43020000, 0x43030000, 0x43040000, 0x43050000,
  3640. 0x43060000, 0x43070000, 0x43080000, 0x43090000, 0x430A0000, 0x430B0000,
  3641. 0x430C0000, 0x430D0000, 0x430E0000, 0x430F0000, 0x43100000, 0x43110000,
  3642. 0x43120000, 0x43130000, 0x43140000, 0x43150000, 0x43160000, 0x43170000,
  3643. 0x43180000, 0x43190000, 0x431A0000, 0x431B0000, 0x431C0000, 0x431D0000,
  3644. 0x431E0000, 0x431F0000, 0x43200000, 0x43210000, 0x43220000, 0x43230000,
  3645. 0x43240000, 0x43250000, 0x43260000, 0x43270000, 0x43280000, 0x43290000,
  3646. 0x432A0000, 0x432B0000, 0x432C0000, 0x432D0000, 0x432E0000, 0x432F0000,
  3647. 0x43300000, 0x43310000, 0x43320000, 0x43330000, 0x43340000
  3648. };
  3649. static const unsigned int mic_svm_vals_lookup[] = {
  3650. 0x00000000, 0x3C23D70A, 0x3CA3D70A, 0x3CF5C28F, 0x3D23D70A, 0x3D4CCCCD,
  3651. 0x3D75C28F, 0x3D8F5C29, 0x3DA3D70A, 0x3DB851EC, 0x3DCCCCCD, 0x3DE147AE,
  3652. 0x3DF5C28F, 0x3E051EB8, 0x3E0F5C29, 0x3E19999A, 0x3E23D70A, 0x3E2E147B,
  3653. 0x3E3851EC, 0x3E428F5C, 0x3E4CCCCD, 0x3E570A3D, 0x3E6147AE, 0x3E6B851F,
  3654. 0x3E75C28F, 0x3E800000, 0x3E851EB8, 0x3E8A3D71, 0x3E8F5C29, 0x3E947AE1,
  3655. 0x3E99999A, 0x3E9EB852, 0x3EA3D70A, 0x3EA8F5C3, 0x3EAE147B, 0x3EB33333,
  3656. 0x3EB851EC, 0x3EBD70A4, 0x3EC28F5C, 0x3EC7AE14, 0x3ECCCCCD, 0x3ED1EB85,
  3657. 0x3ED70A3D, 0x3EDC28F6, 0x3EE147AE, 0x3EE66666, 0x3EEB851F, 0x3EF0A3D7,
  3658. 0x3EF5C28F, 0x3EFAE148, 0x3F000000, 0x3F028F5C, 0x3F051EB8, 0x3F07AE14,
  3659. 0x3F0A3D71, 0x3F0CCCCD, 0x3F0F5C29, 0x3F11EB85, 0x3F147AE1, 0x3F170A3D,
  3660. 0x3F19999A, 0x3F1C28F6, 0x3F1EB852, 0x3F2147AE, 0x3F23D70A, 0x3F266666,
  3661. 0x3F28F5C3, 0x3F2B851F, 0x3F2E147B, 0x3F30A3D7, 0x3F333333, 0x3F35C28F,
  3662. 0x3F3851EC, 0x3F3AE148, 0x3F3D70A4, 0x3F400000, 0x3F428F5C, 0x3F451EB8,
  3663. 0x3F47AE14, 0x3F4A3D71, 0x3F4CCCCD, 0x3F4F5C29, 0x3F51EB85, 0x3F547AE1,
  3664. 0x3F570A3D, 0x3F59999A, 0x3F5C28F6, 0x3F5EB852, 0x3F6147AE, 0x3F63D70A,
  3665. 0x3F666666, 0x3F68F5C3, 0x3F6B851F, 0x3F6E147B, 0x3F70A3D7, 0x3F733333,
  3666. 0x3F75C28F, 0x3F7851EC, 0x3F7AE148, 0x3F7D70A4, 0x3F800000
  3667. };
  3668. static const unsigned int equalizer_vals_lookup[] = {
  3669. 0xC1C00000, 0xC1B80000, 0xC1B00000, 0xC1A80000, 0xC1A00000, 0xC1980000,
  3670. 0xC1900000, 0xC1880000, 0xC1800000, 0xC1700000, 0xC1600000, 0xC1500000,
  3671. 0xC1400000, 0xC1300000, 0xC1200000, 0xC1100000, 0xC1000000, 0xC0E00000,
  3672. 0xC0C00000, 0xC0A00000, 0xC0800000, 0xC0400000, 0xC0000000, 0xBF800000,
  3673. 0x00000000, 0x3F800000, 0x40000000, 0x40400000, 0x40800000, 0x40A00000,
  3674. 0x40C00000, 0x40E00000, 0x41000000, 0x41100000, 0x41200000, 0x41300000,
  3675. 0x41400000, 0x41500000, 0x41600000, 0x41700000, 0x41800000, 0x41880000,
  3676. 0x41900000, 0x41980000, 0x41A00000, 0x41A80000, 0x41B00000, 0x41B80000,
  3677. 0x41C00000
  3678. };
  3679. static int tuning_ctl_set(struct hda_codec *codec, hda_nid_t nid,
  3680. const unsigned int *lookup, int idx)
  3681. {
  3682. int i = 0;
  3683. for (i = 0; i < TUNING_CTLS_COUNT; i++)
  3684. if (nid == ca0132_tuning_ctls[i].nid)
  3685. goto found;
  3686. return -EINVAL;
  3687. found:
  3688. snd_hda_power_up(codec);
  3689. dspio_set_param(codec, ca0132_tuning_ctls[i].mid, 0x20,
  3690. ca0132_tuning_ctls[i].req,
  3691. &(lookup[idx]), sizeof(unsigned int));
  3692. snd_hda_power_down(codec);
  3693. return 1;
  3694. }
  3695. static int tuning_ctl_get(struct snd_kcontrol *kcontrol,
  3696. struct snd_ctl_elem_value *ucontrol)
  3697. {
  3698. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  3699. struct ca0132_spec *spec = codec->spec;
  3700. hda_nid_t nid = get_amp_nid(kcontrol);
  3701. long *valp = ucontrol->value.integer.value;
  3702. int idx = nid - TUNING_CTL_START_NID;
  3703. *valp = spec->cur_ctl_vals[idx];
  3704. return 0;
  3705. }
  3706. static int voice_focus_ctl_info(struct snd_kcontrol *kcontrol,
  3707. struct snd_ctl_elem_info *uinfo)
  3708. {
  3709. int chs = get_amp_channels(kcontrol);
  3710. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  3711. uinfo->count = chs == 3 ? 2 : 1;
  3712. uinfo->value.integer.min = 20;
  3713. uinfo->value.integer.max = 180;
  3714. uinfo->value.integer.step = 1;
  3715. return 0;
  3716. }
  3717. static int voice_focus_ctl_put(struct snd_kcontrol *kcontrol,
  3718. struct snd_ctl_elem_value *ucontrol)
  3719. {
  3720. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  3721. struct ca0132_spec *spec = codec->spec;
  3722. hda_nid_t nid = get_amp_nid(kcontrol);
  3723. long *valp = ucontrol->value.integer.value;
  3724. int idx;
  3725. idx = nid - TUNING_CTL_START_NID;
  3726. /* any change? */
  3727. if (spec->cur_ctl_vals[idx] == *valp)
  3728. return 0;
  3729. spec->cur_ctl_vals[idx] = *valp;
  3730. idx = *valp - 20;
  3731. tuning_ctl_set(codec, nid, voice_focus_vals_lookup, idx);
  3732. return 1;
  3733. }
  3734. static int mic_svm_ctl_info(struct snd_kcontrol *kcontrol,
  3735. struct snd_ctl_elem_info *uinfo)
  3736. {
  3737. int chs = get_amp_channels(kcontrol);
  3738. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  3739. uinfo->count = chs == 3 ? 2 : 1;
  3740. uinfo->value.integer.min = 0;
  3741. uinfo->value.integer.max = 100;
  3742. uinfo->value.integer.step = 1;
  3743. return 0;
  3744. }
  3745. static int mic_svm_ctl_put(struct snd_kcontrol *kcontrol,
  3746. struct snd_ctl_elem_value *ucontrol)
  3747. {
  3748. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  3749. struct ca0132_spec *spec = codec->spec;
  3750. hda_nid_t nid = get_amp_nid(kcontrol);
  3751. long *valp = ucontrol->value.integer.value;
  3752. int idx;
  3753. idx = nid - TUNING_CTL_START_NID;
  3754. /* any change? */
  3755. if (spec->cur_ctl_vals[idx] == *valp)
  3756. return 0;
  3757. spec->cur_ctl_vals[idx] = *valp;
  3758. idx = *valp;
  3759. tuning_ctl_set(codec, nid, mic_svm_vals_lookup, idx);
  3760. return 0;
  3761. }
  3762. static int equalizer_ctl_info(struct snd_kcontrol *kcontrol,
  3763. struct snd_ctl_elem_info *uinfo)
  3764. {
  3765. int chs = get_amp_channels(kcontrol);
  3766. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  3767. uinfo->count = chs == 3 ? 2 : 1;
  3768. uinfo->value.integer.min = 0;
  3769. uinfo->value.integer.max = 48;
  3770. uinfo->value.integer.step = 1;
  3771. return 0;
  3772. }
  3773. static int equalizer_ctl_put(struct snd_kcontrol *kcontrol,
  3774. struct snd_ctl_elem_value *ucontrol)
  3775. {
  3776. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  3777. struct ca0132_spec *spec = codec->spec;
  3778. hda_nid_t nid = get_amp_nid(kcontrol);
  3779. long *valp = ucontrol->value.integer.value;
  3780. int idx;
  3781. idx = nid - TUNING_CTL_START_NID;
  3782. /* any change? */
  3783. if (spec->cur_ctl_vals[idx] == *valp)
  3784. return 0;
  3785. spec->cur_ctl_vals[idx] = *valp;
  3786. idx = *valp;
  3787. tuning_ctl_set(codec, nid, equalizer_vals_lookup, idx);
  3788. return 1;
  3789. }
  3790. static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(voice_focus_db_scale, 2000, 100, 0);
  3791. static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(eq_db_scale, -2400, 100, 0);
  3792. static int add_tuning_control(struct hda_codec *codec,
  3793. hda_nid_t pnid, hda_nid_t nid,
  3794. const char *name, int dir)
  3795. {
  3796. char namestr[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
  3797. int type = dir ? HDA_INPUT : HDA_OUTPUT;
  3798. struct snd_kcontrol_new knew =
  3799. HDA_CODEC_VOLUME_MONO(namestr, nid, 1, 0, type);
  3800. knew.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  3801. SNDRV_CTL_ELEM_ACCESS_TLV_READ;
  3802. knew.tlv.c = 0;
  3803. knew.tlv.p = 0;
  3804. switch (pnid) {
  3805. case VOICE_FOCUS:
  3806. knew.info = voice_focus_ctl_info;
  3807. knew.get = tuning_ctl_get;
  3808. knew.put = voice_focus_ctl_put;
  3809. knew.tlv.p = voice_focus_db_scale;
  3810. break;
  3811. case MIC_SVM:
  3812. knew.info = mic_svm_ctl_info;
  3813. knew.get = tuning_ctl_get;
  3814. knew.put = mic_svm_ctl_put;
  3815. break;
  3816. case EQUALIZER:
  3817. knew.info = equalizer_ctl_info;
  3818. knew.get = tuning_ctl_get;
  3819. knew.put = equalizer_ctl_put;
  3820. knew.tlv.p = eq_db_scale;
  3821. break;
  3822. default:
  3823. return 0;
  3824. }
  3825. knew.private_value =
  3826. HDA_COMPOSE_AMP_VAL(nid, 1, 0, type);
  3827. sprintf(namestr, "%s %s Volume", name, dirstr[dir]);
  3828. return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
  3829. }
  3830. static int add_tuning_ctls(struct hda_codec *codec)
  3831. {
  3832. int i;
  3833. int err;
  3834. for (i = 0; i < TUNING_CTLS_COUNT; i++) {
  3835. err = add_tuning_control(codec,
  3836. ca0132_tuning_ctls[i].parent_nid,
  3837. ca0132_tuning_ctls[i].nid,
  3838. ca0132_tuning_ctls[i].name,
  3839. ca0132_tuning_ctls[i].direct);
  3840. if (err < 0)
  3841. return err;
  3842. }
  3843. return 0;
  3844. }
  3845. static void ca0132_init_tuning_defaults(struct hda_codec *codec)
  3846. {
  3847. struct ca0132_spec *spec = codec->spec;
  3848. int i;
  3849. /* Wedge Angle defaults to 30. 10 below is 30 - 20. 20 is min. */
  3850. spec->cur_ctl_vals[WEDGE_ANGLE - TUNING_CTL_START_NID] = 10;
  3851. /* SVM level defaults to 0.74. */
  3852. spec->cur_ctl_vals[SVM_LEVEL - TUNING_CTL_START_NID] = 74;
  3853. /* EQ defaults to 0dB. */
  3854. for (i = 2; i < TUNING_CTLS_COUNT; i++)
  3855. spec->cur_ctl_vals[i] = 24;
  3856. }
  3857. #endif /*ENABLE_TUNING_CONTROLS*/
  3858. /*
  3859. * Select the active output.
  3860. * If autodetect is enabled, output will be selected based on jack detection.
  3861. * If jack inserted, headphone will be selected, else built-in speakers
  3862. * If autodetect is disabled, output will be selected based on selection.
  3863. */
  3864. static int ca0132_select_out(struct hda_codec *codec)
  3865. {
  3866. struct ca0132_spec *spec = codec->spec;
  3867. unsigned int pin_ctl;
  3868. int jack_present;
  3869. int auto_jack;
  3870. unsigned int tmp;
  3871. int err;
  3872. codec_dbg(codec, "ca0132_select_out\n");
  3873. snd_hda_power_up_pm(codec);
  3874. auto_jack = spec->vnode_lswitch[VNID_HP_ASEL - VNODE_START_NID];
  3875. if (auto_jack)
  3876. jack_present = snd_hda_jack_detect(codec, spec->unsol_tag_hp);
  3877. else
  3878. jack_present =
  3879. spec->vnode_lswitch[VNID_HP_SEL - VNODE_START_NID];
  3880. if (jack_present)
  3881. spec->cur_out_type = HEADPHONE_OUT;
  3882. else
  3883. spec->cur_out_type = SPEAKER_OUT;
  3884. if (spec->cur_out_type == SPEAKER_OUT) {
  3885. codec_dbg(codec, "ca0132_select_out speaker\n");
  3886. /*speaker out config*/
  3887. tmp = FLOAT_ONE;
  3888. err = dspio_set_uint_param(codec, 0x80, 0x04, tmp);
  3889. if (err < 0)
  3890. goto exit;
  3891. /*enable speaker EQ*/
  3892. tmp = FLOAT_ONE;
  3893. err = dspio_set_uint_param(codec, 0x8f, 0x00, tmp);
  3894. if (err < 0)
  3895. goto exit;
  3896. /* Setup EAPD */
  3897. snd_hda_codec_write(codec, spec->out_pins[1], 0,
  3898. VENDOR_CHIPIO_EAPD_SEL_SET, 0x02);
  3899. snd_hda_codec_write(codec, spec->out_pins[0], 0,
  3900. AC_VERB_SET_EAPD_BTLENABLE, 0x00);
  3901. snd_hda_codec_write(codec, spec->out_pins[0], 0,
  3902. VENDOR_CHIPIO_EAPD_SEL_SET, 0x00);
  3903. snd_hda_codec_write(codec, spec->out_pins[0], 0,
  3904. AC_VERB_SET_EAPD_BTLENABLE, 0x02);
  3905. /* disable headphone node */
  3906. pin_ctl = snd_hda_codec_read(codec, spec->out_pins[1], 0,
  3907. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  3908. snd_hda_set_pin_ctl(codec, spec->out_pins[1],
  3909. pin_ctl & ~PIN_HP);
  3910. /* enable speaker node */
  3911. pin_ctl = snd_hda_codec_read(codec, spec->out_pins[0], 0,
  3912. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  3913. snd_hda_set_pin_ctl(codec, spec->out_pins[0],
  3914. pin_ctl | PIN_OUT);
  3915. } else {
  3916. codec_dbg(codec, "ca0132_select_out hp\n");
  3917. /*headphone out config*/
  3918. tmp = FLOAT_ZERO;
  3919. err = dspio_set_uint_param(codec, 0x80, 0x04, tmp);
  3920. if (err < 0)
  3921. goto exit;
  3922. /*disable speaker EQ*/
  3923. tmp = FLOAT_ZERO;
  3924. err = dspio_set_uint_param(codec, 0x8f, 0x00, tmp);
  3925. if (err < 0)
  3926. goto exit;
  3927. /* Setup EAPD */
  3928. snd_hda_codec_write(codec, spec->out_pins[0], 0,
  3929. VENDOR_CHIPIO_EAPD_SEL_SET, 0x00);
  3930. snd_hda_codec_write(codec, spec->out_pins[0], 0,
  3931. AC_VERB_SET_EAPD_BTLENABLE, 0x00);
  3932. snd_hda_codec_write(codec, spec->out_pins[1], 0,
  3933. VENDOR_CHIPIO_EAPD_SEL_SET, 0x02);
  3934. snd_hda_codec_write(codec, spec->out_pins[0], 0,
  3935. AC_VERB_SET_EAPD_BTLENABLE, 0x02);
  3936. /* disable speaker*/
  3937. pin_ctl = snd_hda_codec_read(codec, spec->out_pins[0], 0,
  3938. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  3939. snd_hda_set_pin_ctl(codec, spec->out_pins[0],
  3940. pin_ctl & ~PIN_HP);
  3941. /* enable headphone*/
  3942. pin_ctl = snd_hda_codec_read(codec, spec->out_pins[1], 0,
  3943. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  3944. snd_hda_set_pin_ctl(codec, spec->out_pins[1],
  3945. pin_ctl | PIN_HP);
  3946. }
  3947. exit:
  3948. snd_hda_power_down_pm(codec);
  3949. return err < 0 ? err : 0;
  3950. }
  3951. static int ae5_headphone_gain_set(struct hda_codec *codec, long val);
  3952. static int zxr_headphone_gain_set(struct hda_codec *codec, long val);
  3953. static int ca0132_effects_set(struct hda_codec *codec, hda_nid_t nid, long val);
  3954. static void ae5_mmio_select_out(struct hda_codec *codec)
  3955. {
  3956. struct ca0132_spec *spec = codec->spec;
  3957. const struct ae_ca0113_output_set *out_cmds;
  3958. unsigned int i;
  3959. if (ca0132_quirk(spec) == QUIRK_AE5)
  3960. out_cmds = &ae5_ca0113_output_presets;
  3961. else
  3962. out_cmds = &ae7_ca0113_output_presets;
  3963. for (i = 0; i < AE_CA0113_OUT_SET_COMMANDS; i++)
  3964. ca0113_mmio_command_set(codec, out_cmds->group[i],
  3965. out_cmds->target[i],
  3966. out_cmds->vals[spec->cur_out_type][i]);
  3967. }
  3968. static int ca0132_alt_set_full_range_speaker(struct hda_codec *codec)
  3969. {
  3970. struct ca0132_spec *spec = codec->spec;
  3971. int quirk = ca0132_quirk(spec);
  3972. unsigned int tmp;
  3973. int err;
  3974. /* 2.0/4.0 setup has no LFE channel, so setting full-range does nothing. */
  3975. if (spec->channel_cfg_val == SPEAKER_CHANNELS_4_0
  3976. || spec->channel_cfg_val == SPEAKER_CHANNELS_2_0)
  3977. return 0;
  3978. /* Set front L/R full range. Zero for full-range, one for redirection. */
  3979. tmp = spec->speaker_range_val[0] ? FLOAT_ZERO : FLOAT_ONE;
  3980. err = dspio_set_uint_param(codec, 0x96,
  3981. SPEAKER_FULL_RANGE_FRONT_L_R, tmp);
  3982. if (err < 0)
  3983. return err;
  3984. /* When setting full-range rear, both rear and center/lfe are set. */
  3985. tmp = spec->speaker_range_val[1] ? FLOAT_ZERO : FLOAT_ONE;
  3986. err = dspio_set_uint_param(codec, 0x96,
  3987. SPEAKER_FULL_RANGE_CENTER_LFE, tmp);
  3988. if (err < 0)
  3989. return err;
  3990. err = dspio_set_uint_param(codec, 0x96,
  3991. SPEAKER_FULL_RANGE_REAR_L_R, tmp);
  3992. if (err < 0)
  3993. return err;
  3994. /*
  3995. * Only the AE series cards set this value when setting full-range,
  3996. * and it's always 1.0f.
  3997. */
  3998. if (quirk == QUIRK_AE5 || quirk == QUIRK_AE7) {
  3999. err = dspio_set_uint_param(codec, 0x96,
  4000. SPEAKER_FULL_RANGE_SURROUND_L_R, FLOAT_ONE);
  4001. if (err < 0)
  4002. return err;
  4003. }
  4004. return 0;
  4005. }
  4006. static int ca0132_alt_surround_set_bass_redirection(struct hda_codec *codec,
  4007. bool val)
  4008. {
  4009. struct ca0132_spec *spec = codec->spec;
  4010. unsigned int tmp;
  4011. int err;
  4012. if (val && spec->channel_cfg_val != SPEAKER_CHANNELS_4_0 &&
  4013. spec->channel_cfg_val != SPEAKER_CHANNELS_2_0)
  4014. tmp = FLOAT_ONE;
  4015. else
  4016. tmp = FLOAT_ZERO;
  4017. err = dspio_set_uint_param(codec, 0x96, SPEAKER_BASS_REDIRECT, tmp);
  4018. if (err < 0)
  4019. return err;
  4020. /* If it is enabled, make sure to set the crossover frequency. */
  4021. if (tmp) {
  4022. tmp = float_xbass_xover_lookup[spec->xbass_xover_freq];
  4023. err = dspio_set_uint_param(codec, 0x96,
  4024. SPEAKER_BASS_REDIRECT_XOVER_FREQ, tmp);
  4025. if (err < 0)
  4026. return err;
  4027. }
  4028. return 0;
  4029. }
  4030. /*
  4031. * These are the commands needed to setup output on each of the different card
  4032. * types.
  4033. */
  4034. static void ca0132_alt_select_out_get_quirk_data(struct hda_codec *codec,
  4035. const struct ca0132_alt_out_set_quirk_data **quirk_data)
  4036. {
  4037. struct ca0132_spec *spec = codec->spec;
  4038. int quirk = ca0132_quirk(spec);
  4039. unsigned int i;
  4040. *quirk_data = NULL;
  4041. for (i = 0; i < ARRAY_SIZE(quirk_out_set_data); i++) {
  4042. if (quirk_out_set_data[i].quirk_id == quirk) {
  4043. *quirk_data = &quirk_out_set_data[i];
  4044. return;
  4045. }
  4046. }
  4047. }
  4048. static int ca0132_alt_select_out_quirk_set(struct hda_codec *codec)
  4049. {
  4050. const struct ca0132_alt_out_set_quirk_data *quirk_data;
  4051. const struct ca0132_alt_out_set_info *out_info;
  4052. struct ca0132_spec *spec = codec->spec;
  4053. unsigned int i, gpio_data;
  4054. int err;
  4055. ca0132_alt_select_out_get_quirk_data(codec, &quirk_data);
  4056. if (!quirk_data)
  4057. return 0;
  4058. out_info = &quirk_data->out_set_info[spec->cur_out_type];
  4059. if (quirk_data->is_ae_series)
  4060. ae5_mmio_select_out(codec);
  4061. if (out_info->has_hda_gpio) {
  4062. gpio_data = snd_hda_codec_read(codec, codec->core.afg, 0,
  4063. AC_VERB_GET_GPIO_DATA, 0);
  4064. if (out_info->hda_gpio_set)
  4065. gpio_data |= (1 << out_info->hda_gpio_pin);
  4066. else
  4067. gpio_data &= ~(1 << out_info->hda_gpio_pin);
  4068. snd_hda_codec_write(codec, codec->core.afg, 0,
  4069. AC_VERB_SET_GPIO_DATA, gpio_data);
  4070. }
  4071. if (out_info->mmio_gpio_count) {
  4072. for (i = 0; i < out_info->mmio_gpio_count; i++) {
  4073. ca0113_mmio_gpio_set(codec, out_info->mmio_gpio_pin[i],
  4074. out_info->mmio_gpio_set[i]);
  4075. }
  4076. }
  4077. if (out_info->scp_cmds_count) {
  4078. for (i = 0; i < out_info->scp_cmds_count; i++) {
  4079. err = dspio_set_uint_param(codec,
  4080. out_info->scp_cmd_mid[i],
  4081. out_info->scp_cmd_req[i],
  4082. out_info->scp_cmd_val[i]);
  4083. if (err < 0)
  4084. return err;
  4085. }
  4086. }
  4087. chipio_set_control_param(codec, 0x0d, out_info->dac2port);
  4088. if (out_info->has_chipio_write) {
  4089. chipio_write(codec, out_info->chipio_write_addr,
  4090. out_info->chipio_write_data);
  4091. }
  4092. if (quirk_data->has_headphone_gain) {
  4093. if (spec->cur_out_type != HEADPHONE_OUT) {
  4094. if (quirk_data->is_ae_series)
  4095. ae5_headphone_gain_set(codec, 2);
  4096. else
  4097. zxr_headphone_gain_set(codec, 0);
  4098. } else {
  4099. if (quirk_data->is_ae_series)
  4100. ae5_headphone_gain_set(codec,
  4101. spec->ae5_headphone_gain_val);
  4102. else
  4103. zxr_headphone_gain_set(codec,
  4104. spec->zxr_gain_set);
  4105. }
  4106. }
  4107. return 0;
  4108. }
  4109. static void ca0132_set_out_node_pincfg(struct hda_codec *codec, hda_nid_t nid,
  4110. bool out_enable, bool hp_enable)
  4111. {
  4112. unsigned int pin_ctl;
  4113. pin_ctl = snd_hda_codec_read(codec, nid, 0,
  4114. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  4115. pin_ctl = hp_enable ? pin_ctl | PIN_HP_AMP : pin_ctl & ~PIN_HP_AMP;
  4116. pin_ctl = out_enable ? pin_ctl | PIN_OUT : pin_ctl & ~PIN_OUT;
  4117. snd_hda_set_pin_ctl(codec, nid, pin_ctl);
  4118. }
  4119. /*
  4120. * This function behaves similarly to the ca0132_select_out funciton above,
  4121. * except with a few differences. It adds the ability to select the current
  4122. * output with an enumerated control "output source" if the auto detect
  4123. * mute switch is set to off. If the auto detect mute switch is enabled, it
  4124. * will detect either headphone or lineout(SPEAKER_OUT) from jack detection.
  4125. * It also adds the ability to auto-detect the front headphone port.
  4126. */
  4127. static int ca0132_alt_select_out(struct hda_codec *codec)
  4128. {
  4129. struct ca0132_spec *spec = codec->spec;
  4130. unsigned int tmp, outfx_set;
  4131. int jack_present;
  4132. int auto_jack;
  4133. int err;
  4134. /* Default Headphone is rear headphone */
  4135. hda_nid_t headphone_nid = spec->out_pins[1];
  4136. codec_dbg(codec, "%s\n", __func__);
  4137. snd_hda_power_up_pm(codec);
  4138. auto_jack = spec->vnode_lswitch[VNID_HP_ASEL - VNODE_START_NID];
  4139. /*
  4140. * If headphone rear or front is plugged in, set to headphone.
  4141. * If neither is plugged in, set to rear line out. Only if
  4142. * hp/speaker auto detect is enabled.
  4143. */
  4144. if (auto_jack) {
  4145. jack_present = snd_hda_jack_detect(codec, spec->unsol_tag_hp) ||
  4146. snd_hda_jack_detect(codec, spec->unsol_tag_front_hp);
  4147. if (jack_present)
  4148. spec->cur_out_type = HEADPHONE_OUT;
  4149. else
  4150. spec->cur_out_type = SPEAKER_OUT;
  4151. } else
  4152. spec->cur_out_type = spec->out_enum_val;
  4153. outfx_set = spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID];
  4154. /* Begin DSP output switch, mute DSP volume. */
  4155. err = dspio_set_uint_param(codec, 0x96, SPEAKER_TUNING_MUTE, FLOAT_ONE);
  4156. if (err < 0)
  4157. goto exit;
  4158. if (ca0132_alt_select_out_quirk_set(codec) < 0)
  4159. goto exit;
  4160. switch (spec->cur_out_type) {
  4161. case SPEAKER_OUT:
  4162. codec_dbg(codec, "%s speaker\n", __func__);
  4163. /* Enable EAPD */
  4164. snd_hda_codec_write(codec, spec->out_pins[0], 0,
  4165. AC_VERB_SET_EAPD_BTLENABLE, 0x01);
  4166. /* Disable headphone node. */
  4167. ca0132_set_out_node_pincfg(codec, spec->out_pins[1], 0, 0);
  4168. /* Set front L-R to output. */
  4169. ca0132_set_out_node_pincfg(codec, spec->out_pins[0], 1, 0);
  4170. /* Set Center/LFE to output. */
  4171. ca0132_set_out_node_pincfg(codec, spec->out_pins[2], 1, 0);
  4172. /* Set rear surround to output. */
  4173. ca0132_set_out_node_pincfg(codec, spec->out_pins[3], 1, 0);
  4174. /*
  4175. * Without PlayEnhancement being enabled, if we've got a 2.0
  4176. * setup, set it to floating point eight to disable any DSP
  4177. * processing effects.
  4178. */
  4179. if (!outfx_set && spec->channel_cfg_val == SPEAKER_CHANNELS_2_0)
  4180. tmp = FLOAT_EIGHT;
  4181. else
  4182. tmp = speaker_channel_cfgs[spec->channel_cfg_val].val;
  4183. err = dspio_set_uint_param(codec, 0x80, 0x04, tmp);
  4184. if (err < 0)
  4185. goto exit;
  4186. break;
  4187. case HEADPHONE_OUT:
  4188. codec_dbg(codec, "%s hp\n", __func__);
  4189. snd_hda_codec_write(codec, spec->out_pins[0], 0,
  4190. AC_VERB_SET_EAPD_BTLENABLE, 0x00);
  4191. /* Disable all speaker nodes. */
  4192. ca0132_set_out_node_pincfg(codec, spec->out_pins[0], 0, 0);
  4193. ca0132_set_out_node_pincfg(codec, spec->out_pins[2], 0, 0);
  4194. ca0132_set_out_node_pincfg(codec, spec->out_pins[3], 0, 0);
  4195. /* enable headphone, either front or rear */
  4196. if (snd_hda_jack_detect(codec, spec->unsol_tag_front_hp))
  4197. headphone_nid = spec->out_pins[2];
  4198. else if (snd_hda_jack_detect(codec, spec->unsol_tag_hp))
  4199. headphone_nid = spec->out_pins[1];
  4200. ca0132_set_out_node_pincfg(codec, headphone_nid, 1, 1);
  4201. if (outfx_set)
  4202. err = dspio_set_uint_param(codec, 0x80, 0x04, FLOAT_ONE);
  4203. else
  4204. err = dspio_set_uint_param(codec, 0x80, 0x04, FLOAT_ZERO);
  4205. if (err < 0)
  4206. goto exit;
  4207. break;
  4208. }
  4209. /*
  4210. * If output effects are enabled, set the X-Bass effect value again to
  4211. * make sure that it's properly enabled/disabled for speaker
  4212. * configurations with an LFE channel.
  4213. */
  4214. if (outfx_set)
  4215. ca0132_effects_set(codec, X_BASS,
  4216. spec->effects_switch[X_BASS - EFFECT_START_NID]);
  4217. /* Set speaker EQ bypass attenuation to 0. */
  4218. err = dspio_set_uint_param(codec, 0x8f, 0x01, FLOAT_ZERO);
  4219. if (err < 0)
  4220. goto exit;
  4221. /*
  4222. * Although unused on all cards but the AE series, this is always set
  4223. * to zero when setting the output.
  4224. */
  4225. err = dspio_set_uint_param(codec, 0x96,
  4226. SPEAKER_TUNING_USE_SPEAKER_EQ, FLOAT_ZERO);
  4227. if (err < 0)
  4228. goto exit;
  4229. if (spec->cur_out_type == SPEAKER_OUT)
  4230. err = ca0132_alt_surround_set_bass_redirection(codec,
  4231. spec->bass_redirection_val);
  4232. else
  4233. err = ca0132_alt_surround_set_bass_redirection(codec, 0);
  4234. /* Unmute DSP now that we're done with output selection. */
  4235. err = dspio_set_uint_param(codec, 0x96,
  4236. SPEAKER_TUNING_MUTE, FLOAT_ZERO);
  4237. if (err < 0)
  4238. goto exit;
  4239. if (spec->cur_out_type == SPEAKER_OUT) {
  4240. err = ca0132_alt_set_full_range_speaker(codec);
  4241. if (err < 0)
  4242. goto exit;
  4243. }
  4244. exit:
  4245. snd_hda_power_down_pm(codec);
  4246. return err < 0 ? err : 0;
  4247. }
  4248. static void ca0132_unsol_hp_delayed(struct work_struct *work)
  4249. {
  4250. struct ca0132_spec *spec = container_of(
  4251. to_delayed_work(work), struct ca0132_spec, unsol_hp_work);
  4252. struct hda_jack_tbl *jack;
  4253. if (ca0132_use_alt_functions(spec))
  4254. ca0132_alt_select_out(spec->codec);
  4255. else
  4256. ca0132_select_out(spec->codec);
  4257. jack = snd_hda_jack_tbl_get(spec->codec, spec->unsol_tag_hp);
  4258. if (jack) {
  4259. jack->block_report = 0;
  4260. snd_hda_jack_report_sync(spec->codec);
  4261. }
  4262. }
  4263. static void ca0132_set_dmic(struct hda_codec *codec, int enable);
  4264. static int ca0132_mic_boost_set(struct hda_codec *codec, long val);
  4265. static void resume_mic1(struct hda_codec *codec, unsigned int oldval);
  4266. static int stop_mic1(struct hda_codec *codec);
  4267. static int ca0132_cvoice_switch_set(struct hda_codec *codec);
  4268. static int ca0132_alt_mic_boost_set(struct hda_codec *codec, long val);
  4269. /*
  4270. * Select the active VIP source
  4271. */
  4272. static int ca0132_set_vipsource(struct hda_codec *codec, int val)
  4273. {
  4274. struct ca0132_spec *spec = codec->spec;
  4275. unsigned int tmp;
  4276. if (spec->dsp_state != DSP_DOWNLOADED)
  4277. return 0;
  4278. /* if CrystalVoice if off, vipsource should be 0 */
  4279. if (!spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID] ||
  4280. (val == 0)) {
  4281. chipio_set_control_param(codec, CONTROL_PARAM_VIP_SOURCE, 0);
  4282. chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
  4283. chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
  4284. if (spec->cur_mic_type == DIGITAL_MIC)
  4285. tmp = FLOAT_TWO;
  4286. else
  4287. tmp = FLOAT_ONE;
  4288. dspio_set_uint_param(codec, 0x80, 0x00, tmp);
  4289. tmp = FLOAT_ZERO;
  4290. dspio_set_uint_param(codec, 0x80, 0x05, tmp);
  4291. } else {
  4292. chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_16_000);
  4293. chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_16_000);
  4294. if (spec->cur_mic_type == DIGITAL_MIC)
  4295. tmp = FLOAT_TWO;
  4296. else
  4297. tmp = FLOAT_ONE;
  4298. dspio_set_uint_param(codec, 0x80, 0x00, tmp);
  4299. tmp = FLOAT_ONE;
  4300. dspio_set_uint_param(codec, 0x80, 0x05, tmp);
  4301. msleep(20);
  4302. chipio_set_control_param(codec, CONTROL_PARAM_VIP_SOURCE, val);
  4303. }
  4304. return 1;
  4305. }
  4306. static int ca0132_alt_set_vipsource(struct hda_codec *codec, int val)
  4307. {
  4308. struct ca0132_spec *spec = codec->spec;
  4309. unsigned int tmp;
  4310. if (spec->dsp_state != DSP_DOWNLOADED)
  4311. return 0;
  4312. codec_dbg(codec, "%s\n", __func__);
  4313. chipio_set_stream_control(codec, 0x03, 0);
  4314. chipio_set_stream_control(codec, 0x04, 0);
  4315. /* if CrystalVoice is off, vipsource should be 0 */
  4316. if (!spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID] ||
  4317. (val == 0) || spec->in_enum_val == REAR_LINE_IN) {
  4318. codec_dbg(codec, "%s: off.", __func__);
  4319. chipio_set_control_param(codec, CONTROL_PARAM_VIP_SOURCE, 0);
  4320. tmp = FLOAT_ZERO;
  4321. dspio_set_uint_param(codec, 0x80, 0x05, tmp);
  4322. chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
  4323. chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
  4324. if (ca0132_quirk(spec) == QUIRK_R3DI)
  4325. chipio_set_conn_rate(codec, 0x0F, SR_96_000);
  4326. if (spec->in_enum_val == REAR_LINE_IN)
  4327. tmp = FLOAT_ZERO;
  4328. else {
  4329. if (ca0132_quirk(spec) == QUIRK_SBZ)
  4330. tmp = FLOAT_THREE;
  4331. else
  4332. tmp = FLOAT_ONE;
  4333. }
  4334. dspio_set_uint_param(codec, 0x80, 0x00, tmp);
  4335. } else {
  4336. codec_dbg(codec, "%s: on.", __func__);
  4337. chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_16_000);
  4338. chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_16_000);
  4339. if (ca0132_quirk(spec) == QUIRK_R3DI)
  4340. chipio_set_conn_rate(codec, 0x0F, SR_16_000);
  4341. if (spec->effects_switch[VOICE_FOCUS - EFFECT_START_NID])
  4342. tmp = FLOAT_TWO;
  4343. else
  4344. tmp = FLOAT_ONE;
  4345. dspio_set_uint_param(codec, 0x80, 0x00, tmp);
  4346. tmp = FLOAT_ONE;
  4347. dspio_set_uint_param(codec, 0x80, 0x05, tmp);
  4348. msleep(20);
  4349. chipio_set_control_param(codec, CONTROL_PARAM_VIP_SOURCE, val);
  4350. }
  4351. chipio_set_stream_control(codec, 0x03, 1);
  4352. chipio_set_stream_control(codec, 0x04, 1);
  4353. return 1;
  4354. }
  4355. /*
  4356. * Select the active microphone.
  4357. * If autodetect is enabled, mic will be selected based on jack detection.
  4358. * If jack inserted, ext.mic will be selected, else built-in mic
  4359. * If autodetect is disabled, mic will be selected based on selection.
  4360. */
  4361. static int ca0132_select_mic(struct hda_codec *codec)
  4362. {
  4363. struct ca0132_spec *spec = codec->spec;
  4364. int jack_present;
  4365. int auto_jack;
  4366. codec_dbg(codec, "ca0132_select_mic\n");
  4367. snd_hda_power_up_pm(codec);
  4368. auto_jack = spec->vnode_lswitch[VNID_AMIC1_ASEL - VNODE_START_NID];
  4369. if (auto_jack)
  4370. jack_present = snd_hda_jack_detect(codec, spec->unsol_tag_amic1);
  4371. else
  4372. jack_present =
  4373. spec->vnode_lswitch[VNID_AMIC1_SEL - VNODE_START_NID];
  4374. if (jack_present)
  4375. spec->cur_mic_type = LINE_MIC_IN;
  4376. else
  4377. spec->cur_mic_type = DIGITAL_MIC;
  4378. if (spec->cur_mic_type == DIGITAL_MIC) {
  4379. /* enable digital Mic */
  4380. chipio_set_conn_rate(codec, MEM_CONNID_DMIC, SR_32_000);
  4381. ca0132_set_dmic(codec, 1);
  4382. ca0132_mic_boost_set(codec, 0);
  4383. /* set voice focus */
  4384. ca0132_effects_set(codec, VOICE_FOCUS,
  4385. spec->effects_switch
  4386. [VOICE_FOCUS - EFFECT_START_NID]);
  4387. } else {
  4388. /* disable digital Mic */
  4389. chipio_set_conn_rate(codec, MEM_CONNID_DMIC, SR_96_000);
  4390. ca0132_set_dmic(codec, 0);
  4391. ca0132_mic_boost_set(codec, spec->cur_mic_boost);
  4392. /* disable voice focus */
  4393. ca0132_effects_set(codec, VOICE_FOCUS, 0);
  4394. }
  4395. snd_hda_power_down_pm(codec);
  4396. return 0;
  4397. }
  4398. /*
  4399. * Select the active input.
  4400. * Mic detection isn't used, because it's kind of pointless on the SBZ.
  4401. * The front mic has no jack-detection, so the only way to switch to it
  4402. * is to do it manually in alsamixer.
  4403. */
  4404. static int ca0132_alt_select_in(struct hda_codec *codec)
  4405. {
  4406. struct ca0132_spec *spec = codec->spec;
  4407. unsigned int tmp;
  4408. codec_dbg(codec, "%s\n", __func__);
  4409. snd_hda_power_up_pm(codec);
  4410. chipio_set_stream_control(codec, 0x03, 0);
  4411. chipio_set_stream_control(codec, 0x04, 0);
  4412. spec->cur_mic_type = spec->in_enum_val;
  4413. switch (spec->cur_mic_type) {
  4414. case REAR_MIC:
  4415. switch (ca0132_quirk(spec)) {
  4416. case QUIRK_SBZ:
  4417. case QUIRK_R3D:
  4418. ca0113_mmio_gpio_set(codec, 0, false);
  4419. tmp = FLOAT_THREE;
  4420. break;
  4421. case QUIRK_ZXR:
  4422. tmp = FLOAT_THREE;
  4423. break;
  4424. case QUIRK_R3DI:
  4425. r3di_gpio_mic_set(codec, R3DI_REAR_MIC);
  4426. tmp = FLOAT_ONE;
  4427. break;
  4428. case QUIRK_AE5:
  4429. ca0113_mmio_command_set(codec, 0x30, 0x28, 0x00);
  4430. tmp = FLOAT_THREE;
  4431. break;
  4432. case QUIRK_AE7:
  4433. ca0113_mmio_command_set(codec, 0x30, 0x28, 0x00);
  4434. tmp = FLOAT_THREE;
  4435. chipio_set_conn_rate(codec, MEM_CONNID_MICIN2,
  4436. SR_96_000);
  4437. chipio_set_conn_rate(codec, MEM_CONNID_MICOUT2,
  4438. SR_96_000);
  4439. dspio_set_uint_param(codec, 0x80, 0x01, FLOAT_ZERO);
  4440. break;
  4441. default:
  4442. tmp = FLOAT_ONE;
  4443. break;
  4444. }
  4445. chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
  4446. chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
  4447. if (ca0132_quirk(spec) == QUIRK_R3DI)
  4448. chipio_set_conn_rate(codec, 0x0F, SR_96_000);
  4449. dspio_set_uint_param(codec, 0x80, 0x00, tmp);
  4450. chipio_set_stream_control(codec, 0x03, 1);
  4451. chipio_set_stream_control(codec, 0x04, 1);
  4452. switch (ca0132_quirk(spec)) {
  4453. case QUIRK_SBZ:
  4454. chipio_write(codec, 0x18B098, 0x0000000C);
  4455. chipio_write(codec, 0x18B09C, 0x0000000C);
  4456. break;
  4457. case QUIRK_ZXR:
  4458. chipio_write(codec, 0x18B098, 0x0000000C);
  4459. chipio_write(codec, 0x18B09C, 0x000000CC);
  4460. break;
  4461. case QUIRK_AE5:
  4462. chipio_write(codec, 0x18B098, 0x0000000C);
  4463. chipio_write(codec, 0x18B09C, 0x0000004C);
  4464. break;
  4465. default:
  4466. break;
  4467. }
  4468. ca0132_alt_mic_boost_set(codec, spec->mic_boost_enum_val);
  4469. break;
  4470. case REAR_LINE_IN:
  4471. ca0132_mic_boost_set(codec, 0);
  4472. switch (ca0132_quirk(spec)) {
  4473. case QUIRK_SBZ:
  4474. case QUIRK_R3D:
  4475. ca0113_mmio_gpio_set(codec, 0, false);
  4476. break;
  4477. case QUIRK_R3DI:
  4478. r3di_gpio_mic_set(codec, R3DI_REAR_MIC);
  4479. break;
  4480. case QUIRK_AE5:
  4481. ca0113_mmio_command_set(codec, 0x30, 0x28, 0x00);
  4482. break;
  4483. case QUIRK_AE7:
  4484. ca0113_mmio_command_set(codec, 0x30, 0x28, 0x3f);
  4485. chipio_set_conn_rate(codec, MEM_CONNID_MICIN2,
  4486. SR_96_000);
  4487. chipio_set_conn_rate(codec, MEM_CONNID_MICOUT2,
  4488. SR_96_000);
  4489. dspio_set_uint_param(codec, 0x80, 0x01, FLOAT_ZERO);
  4490. break;
  4491. default:
  4492. break;
  4493. }
  4494. chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
  4495. chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
  4496. if (ca0132_quirk(spec) == QUIRK_R3DI)
  4497. chipio_set_conn_rate(codec, 0x0F, SR_96_000);
  4498. if (ca0132_quirk(spec) == QUIRK_AE7)
  4499. tmp = FLOAT_THREE;
  4500. else
  4501. tmp = FLOAT_ZERO;
  4502. dspio_set_uint_param(codec, 0x80, 0x00, tmp);
  4503. switch (ca0132_quirk(spec)) {
  4504. case QUIRK_SBZ:
  4505. case QUIRK_AE5:
  4506. chipio_write(codec, 0x18B098, 0x00000000);
  4507. chipio_write(codec, 0x18B09C, 0x00000000);
  4508. break;
  4509. default:
  4510. break;
  4511. }
  4512. chipio_set_stream_control(codec, 0x03, 1);
  4513. chipio_set_stream_control(codec, 0x04, 1);
  4514. break;
  4515. case FRONT_MIC:
  4516. switch (ca0132_quirk(spec)) {
  4517. case QUIRK_SBZ:
  4518. case QUIRK_R3D:
  4519. ca0113_mmio_gpio_set(codec, 0, true);
  4520. ca0113_mmio_gpio_set(codec, 5, false);
  4521. tmp = FLOAT_THREE;
  4522. break;
  4523. case QUIRK_R3DI:
  4524. r3di_gpio_mic_set(codec, R3DI_FRONT_MIC);
  4525. tmp = FLOAT_ONE;
  4526. break;
  4527. case QUIRK_AE5:
  4528. ca0113_mmio_command_set(codec, 0x30, 0x28, 0x3f);
  4529. tmp = FLOAT_THREE;
  4530. break;
  4531. default:
  4532. tmp = FLOAT_ONE;
  4533. break;
  4534. }
  4535. chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
  4536. chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
  4537. if (ca0132_quirk(spec) == QUIRK_R3DI)
  4538. chipio_set_conn_rate(codec, 0x0F, SR_96_000);
  4539. dspio_set_uint_param(codec, 0x80, 0x00, tmp);
  4540. chipio_set_stream_control(codec, 0x03, 1);
  4541. chipio_set_stream_control(codec, 0x04, 1);
  4542. switch (ca0132_quirk(spec)) {
  4543. case QUIRK_SBZ:
  4544. chipio_write(codec, 0x18B098, 0x0000000C);
  4545. chipio_write(codec, 0x18B09C, 0x000000CC);
  4546. break;
  4547. case QUIRK_AE5:
  4548. chipio_write(codec, 0x18B098, 0x0000000C);
  4549. chipio_write(codec, 0x18B09C, 0x0000004C);
  4550. break;
  4551. default:
  4552. break;
  4553. }
  4554. ca0132_alt_mic_boost_set(codec, spec->mic_boost_enum_val);
  4555. break;
  4556. }
  4557. ca0132_cvoice_switch_set(codec);
  4558. snd_hda_power_down_pm(codec);
  4559. return 0;
  4560. }
  4561. /*
  4562. * Check if VNODE settings take effect immediately.
  4563. */
  4564. static bool ca0132_is_vnode_effective(struct hda_codec *codec,
  4565. hda_nid_t vnid,
  4566. hda_nid_t *shared_nid)
  4567. {
  4568. struct ca0132_spec *spec = codec->spec;
  4569. hda_nid_t nid;
  4570. switch (vnid) {
  4571. case VNID_SPK:
  4572. nid = spec->shared_out_nid;
  4573. break;
  4574. case VNID_MIC:
  4575. nid = spec->shared_mic_nid;
  4576. break;
  4577. default:
  4578. return false;
  4579. }
  4580. if (shared_nid)
  4581. *shared_nid = nid;
  4582. return true;
  4583. }
  4584. /*
  4585. * The following functions are control change helpers.
  4586. * They return 0 if no changed. Return 1 if changed.
  4587. */
  4588. static int ca0132_voicefx_set(struct hda_codec *codec, int enable)
  4589. {
  4590. struct ca0132_spec *spec = codec->spec;
  4591. unsigned int tmp;
  4592. /* based on CrystalVoice state to enable VoiceFX. */
  4593. if (enable) {
  4594. tmp = spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID] ?
  4595. FLOAT_ONE : FLOAT_ZERO;
  4596. } else {
  4597. tmp = FLOAT_ZERO;
  4598. }
  4599. dspio_set_uint_param(codec, ca0132_voicefx.mid,
  4600. ca0132_voicefx.reqs[0], tmp);
  4601. return 1;
  4602. }
  4603. /*
  4604. * Set the effects parameters
  4605. */
  4606. static int ca0132_effects_set(struct hda_codec *codec, hda_nid_t nid, long val)
  4607. {
  4608. struct ca0132_spec *spec = codec->spec;
  4609. unsigned int on, tmp, channel_cfg;
  4610. int num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT;
  4611. int err = 0;
  4612. int idx = nid - EFFECT_START_NID;
  4613. if ((idx < 0) || (idx >= num_fx))
  4614. return 0; /* no changed */
  4615. /* for out effect, qualify with PE */
  4616. if ((nid >= OUT_EFFECT_START_NID) && (nid < OUT_EFFECT_END_NID)) {
  4617. /* if PE if off, turn off out effects. */
  4618. if (!spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID])
  4619. val = 0;
  4620. if (spec->cur_out_type == SPEAKER_OUT && nid == X_BASS) {
  4621. channel_cfg = spec->channel_cfg_val;
  4622. if (channel_cfg != SPEAKER_CHANNELS_2_0 &&
  4623. channel_cfg != SPEAKER_CHANNELS_4_0)
  4624. val = 0;
  4625. }
  4626. }
  4627. /* for in effect, qualify with CrystalVoice */
  4628. if ((nid >= IN_EFFECT_START_NID) && (nid < IN_EFFECT_END_NID)) {
  4629. /* if CrystalVoice if off, turn off in effects. */
  4630. if (!spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID])
  4631. val = 0;
  4632. /* Voice Focus applies to 2-ch Mic, Digital Mic */
  4633. if ((nid == VOICE_FOCUS) && (spec->cur_mic_type != DIGITAL_MIC))
  4634. val = 0;
  4635. /* If Voice Focus on SBZ, set to two channel. */
  4636. if ((nid == VOICE_FOCUS) && ca0132_use_pci_mmio(spec)
  4637. && (spec->cur_mic_type != REAR_LINE_IN)) {
  4638. if (spec->effects_switch[CRYSTAL_VOICE -
  4639. EFFECT_START_NID]) {
  4640. if (spec->effects_switch[VOICE_FOCUS -
  4641. EFFECT_START_NID]) {
  4642. tmp = FLOAT_TWO;
  4643. val = 1;
  4644. } else
  4645. tmp = FLOAT_ONE;
  4646. dspio_set_uint_param(codec, 0x80, 0x00, tmp);
  4647. }
  4648. }
  4649. /*
  4650. * For SBZ noise reduction, there's an extra command
  4651. * to module ID 0x47. No clue why.
  4652. */
  4653. if ((nid == NOISE_REDUCTION) && ca0132_use_pci_mmio(spec)
  4654. && (spec->cur_mic_type != REAR_LINE_IN)) {
  4655. if (spec->effects_switch[CRYSTAL_VOICE -
  4656. EFFECT_START_NID]) {
  4657. if (spec->effects_switch[NOISE_REDUCTION -
  4658. EFFECT_START_NID])
  4659. tmp = FLOAT_ONE;
  4660. else
  4661. tmp = FLOAT_ZERO;
  4662. } else
  4663. tmp = FLOAT_ZERO;
  4664. dspio_set_uint_param(codec, 0x47, 0x00, tmp);
  4665. }
  4666. /* If rear line in disable effects. */
  4667. if (ca0132_use_alt_functions(spec) &&
  4668. spec->in_enum_val == REAR_LINE_IN)
  4669. val = 0;
  4670. }
  4671. codec_dbg(codec, "ca0132_effect_set: nid=0x%x, val=%ld\n",
  4672. nid, val);
  4673. on = (val == 0) ? FLOAT_ZERO : FLOAT_ONE;
  4674. err = dspio_set_uint_param(codec, ca0132_effects[idx].mid,
  4675. ca0132_effects[idx].reqs[0], on);
  4676. if (err < 0)
  4677. return 0; /* no changed */
  4678. return 1;
  4679. }
  4680. /*
  4681. * Turn on/off Playback Enhancements
  4682. */
  4683. static int ca0132_pe_switch_set(struct hda_codec *codec)
  4684. {
  4685. struct ca0132_spec *spec = codec->spec;
  4686. hda_nid_t nid;
  4687. int i, ret = 0;
  4688. codec_dbg(codec, "ca0132_pe_switch_set: val=%ld\n",
  4689. spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID]);
  4690. if (ca0132_use_alt_functions(spec))
  4691. ca0132_alt_select_out(codec);
  4692. i = OUT_EFFECT_START_NID - EFFECT_START_NID;
  4693. nid = OUT_EFFECT_START_NID;
  4694. /* PE affects all out effects */
  4695. for (; nid < OUT_EFFECT_END_NID; nid++, i++)
  4696. ret |= ca0132_effects_set(codec, nid, spec->effects_switch[i]);
  4697. return ret;
  4698. }
  4699. /* Check if Mic1 is streaming, if so, stop streaming */
  4700. static int stop_mic1(struct hda_codec *codec)
  4701. {
  4702. struct ca0132_spec *spec = codec->spec;
  4703. unsigned int oldval = snd_hda_codec_read(codec, spec->adcs[0], 0,
  4704. AC_VERB_GET_CONV, 0);
  4705. if (oldval != 0)
  4706. snd_hda_codec_write(codec, spec->adcs[0], 0,
  4707. AC_VERB_SET_CHANNEL_STREAMID,
  4708. 0);
  4709. return oldval;
  4710. }
  4711. /* Resume Mic1 streaming if it was stopped. */
  4712. static void resume_mic1(struct hda_codec *codec, unsigned int oldval)
  4713. {
  4714. struct ca0132_spec *spec = codec->spec;
  4715. /* Restore the previous stream and channel */
  4716. if (oldval != 0)
  4717. snd_hda_codec_write(codec, spec->adcs[0], 0,
  4718. AC_VERB_SET_CHANNEL_STREAMID,
  4719. oldval);
  4720. }
  4721. /*
  4722. * Turn on/off CrystalVoice
  4723. */
  4724. static int ca0132_cvoice_switch_set(struct hda_codec *codec)
  4725. {
  4726. struct ca0132_spec *spec = codec->spec;
  4727. hda_nid_t nid;
  4728. int i, ret = 0;
  4729. unsigned int oldval;
  4730. codec_dbg(codec, "ca0132_cvoice_switch_set: val=%ld\n",
  4731. spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID]);
  4732. i = IN_EFFECT_START_NID - EFFECT_START_NID;
  4733. nid = IN_EFFECT_START_NID;
  4734. /* CrystalVoice affects all in effects */
  4735. for (; nid < IN_EFFECT_END_NID; nid++, i++)
  4736. ret |= ca0132_effects_set(codec, nid, spec->effects_switch[i]);
  4737. /* including VoiceFX */
  4738. ret |= ca0132_voicefx_set(codec, (spec->voicefx_val ? 1 : 0));
  4739. /* set correct vipsource */
  4740. oldval = stop_mic1(codec);
  4741. if (ca0132_use_alt_functions(spec))
  4742. ret |= ca0132_alt_set_vipsource(codec, 1);
  4743. else
  4744. ret |= ca0132_set_vipsource(codec, 1);
  4745. resume_mic1(codec, oldval);
  4746. return ret;
  4747. }
  4748. static int ca0132_mic_boost_set(struct hda_codec *codec, long val)
  4749. {
  4750. struct ca0132_spec *spec = codec->spec;
  4751. int ret = 0;
  4752. if (val) /* on */
  4753. ret = snd_hda_codec_amp_update(codec, spec->input_pins[0], 0,
  4754. HDA_INPUT, 0, HDA_AMP_VOLMASK, 3);
  4755. else /* off */
  4756. ret = snd_hda_codec_amp_update(codec, spec->input_pins[0], 0,
  4757. HDA_INPUT, 0, HDA_AMP_VOLMASK, 0);
  4758. return ret;
  4759. }
  4760. static int ca0132_alt_mic_boost_set(struct hda_codec *codec, long val)
  4761. {
  4762. struct ca0132_spec *spec = codec->spec;
  4763. int ret = 0;
  4764. ret = snd_hda_codec_amp_update(codec, spec->input_pins[0], 0,
  4765. HDA_INPUT, 0, HDA_AMP_VOLMASK, val);
  4766. return ret;
  4767. }
  4768. static int ae5_headphone_gain_set(struct hda_codec *codec, long val)
  4769. {
  4770. unsigned int i;
  4771. for (i = 0; i < 4; i++)
  4772. ca0113_mmio_command_set(codec, 0x48, 0x11 + i,
  4773. ae5_headphone_gain_presets[val].vals[i]);
  4774. return 0;
  4775. }
  4776. /*
  4777. * gpio pin 1 is a relay that switches on/off, apparently setting the headphone
  4778. * amplifier to handle a 600 ohm load.
  4779. */
  4780. static int zxr_headphone_gain_set(struct hda_codec *codec, long val)
  4781. {
  4782. ca0113_mmio_gpio_set(codec, 1, val);
  4783. return 0;
  4784. }
  4785. static int ca0132_vnode_switch_set(struct snd_kcontrol *kcontrol,
  4786. struct snd_ctl_elem_value *ucontrol)
  4787. {
  4788. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  4789. hda_nid_t nid = get_amp_nid(kcontrol);
  4790. hda_nid_t shared_nid = 0;
  4791. bool effective;
  4792. int ret = 0;
  4793. struct ca0132_spec *spec = codec->spec;
  4794. int auto_jack;
  4795. if (nid == VNID_HP_SEL) {
  4796. auto_jack =
  4797. spec->vnode_lswitch[VNID_HP_ASEL - VNODE_START_NID];
  4798. if (!auto_jack) {
  4799. if (ca0132_use_alt_functions(spec))
  4800. ca0132_alt_select_out(codec);
  4801. else
  4802. ca0132_select_out(codec);
  4803. }
  4804. return 1;
  4805. }
  4806. if (nid == VNID_AMIC1_SEL) {
  4807. auto_jack =
  4808. spec->vnode_lswitch[VNID_AMIC1_ASEL - VNODE_START_NID];
  4809. if (!auto_jack)
  4810. ca0132_select_mic(codec);
  4811. return 1;
  4812. }
  4813. if (nid == VNID_HP_ASEL) {
  4814. if (ca0132_use_alt_functions(spec))
  4815. ca0132_alt_select_out(codec);
  4816. else
  4817. ca0132_select_out(codec);
  4818. return 1;
  4819. }
  4820. if (nid == VNID_AMIC1_ASEL) {
  4821. ca0132_select_mic(codec);
  4822. return 1;
  4823. }
  4824. /* if effective conditions, then update hw immediately. */
  4825. effective = ca0132_is_vnode_effective(codec, nid, &shared_nid);
  4826. if (effective) {
  4827. int dir = get_amp_direction(kcontrol);
  4828. int ch = get_amp_channels(kcontrol);
  4829. unsigned long pval;
  4830. mutex_lock(&codec->control_mutex);
  4831. pval = kcontrol->private_value;
  4832. kcontrol->private_value = HDA_COMPOSE_AMP_VAL(shared_nid, ch,
  4833. 0, dir);
  4834. ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
  4835. kcontrol->private_value = pval;
  4836. mutex_unlock(&codec->control_mutex);
  4837. }
  4838. return ret;
  4839. }
  4840. /* End of control change helpers. */
  4841. static void ca0132_alt_bass_redirection_xover_set(struct hda_codec *codec,
  4842. long idx)
  4843. {
  4844. snd_hda_power_up(codec);
  4845. dspio_set_param(codec, 0x96, 0x20, SPEAKER_BASS_REDIRECT_XOVER_FREQ,
  4846. &(float_xbass_xover_lookup[idx]), sizeof(unsigned int));
  4847. snd_hda_power_down(codec);
  4848. }
  4849. /*
  4850. * Below I've added controls to mess with the effect levels, I've only enabled
  4851. * them on the Sound Blaster Z, but they would probably also work on the
  4852. * Chromebook. I figured they were probably tuned specifically for it, and left
  4853. * out for a reason.
  4854. */
  4855. /* Sets DSP effect level from the sliders above the controls */
  4856. static int ca0132_alt_slider_ctl_set(struct hda_codec *codec, hda_nid_t nid,
  4857. const unsigned int *lookup, int idx)
  4858. {
  4859. int i = 0;
  4860. unsigned int y;
  4861. /*
  4862. * For X_BASS, req 2 is actually crossover freq instead of
  4863. * effect level
  4864. */
  4865. if (nid == X_BASS)
  4866. y = 2;
  4867. else
  4868. y = 1;
  4869. snd_hda_power_up(codec);
  4870. if (nid == XBASS_XOVER) {
  4871. for (i = 0; i < OUT_EFFECTS_COUNT; i++)
  4872. if (ca0132_effects[i].nid == X_BASS)
  4873. break;
  4874. dspio_set_param(codec, ca0132_effects[i].mid, 0x20,
  4875. ca0132_effects[i].reqs[1],
  4876. &(lookup[idx - 1]), sizeof(unsigned int));
  4877. } else {
  4878. /* Find the actual effect structure */
  4879. for (i = 0; i < OUT_EFFECTS_COUNT; i++)
  4880. if (nid == ca0132_effects[i].nid)
  4881. break;
  4882. dspio_set_param(codec, ca0132_effects[i].mid, 0x20,
  4883. ca0132_effects[i].reqs[y],
  4884. &(lookup[idx]), sizeof(unsigned int));
  4885. }
  4886. snd_hda_power_down(codec);
  4887. return 0;
  4888. }
  4889. static int ca0132_alt_xbass_xover_slider_ctl_get(struct snd_kcontrol *kcontrol,
  4890. struct snd_ctl_elem_value *ucontrol)
  4891. {
  4892. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  4893. struct ca0132_spec *spec = codec->spec;
  4894. long *valp = ucontrol->value.integer.value;
  4895. hda_nid_t nid = get_amp_nid(kcontrol);
  4896. if (nid == BASS_REDIRECTION_XOVER)
  4897. *valp = spec->bass_redirect_xover_freq;
  4898. else
  4899. *valp = spec->xbass_xover_freq;
  4900. return 0;
  4901. }
  4902. static int ca0132_alt_slider_ctl_get(struct snd_kcontrol *kcontrol,
  4903. struct snd_ctl_elem_value *ucontrol)
  4904. {
  4905. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  4906. struct ca0132_spec *spec = codec->spec;
  4907. hda_nid_t nid = get_amp_nid(kcontrol);
  4908. long *valp = ucontrol->value.integer.value;
  4909. int idx = nid - OUT_EFFECT_START_NID;
  4910. *valp = spec->fx_ctl_val[idx];
  4911. return 0;
  4912. }
  4913. /*
  4914. * The X-bass crossover starts at 10hz, so the min is 1. The
  4915. * frequency is set in multiples of 10.
  4916. */
  4917. static int ca0132_alt_xbass_xover_slider_info(struct snd_kcontrol *kcontrol,
  4918. struct snd_ctl_elem_info *uinfo)
  4919. {
  4920. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  4921. uinfo->count = 1;
  4922. uinfo->value.integer.min = 1;
  4923. uinfo->value.integer.max = 100;
  4924. uinfo->value.integer.step = 1;
  4925. return 0;
  4926. }
  4927. static int ca0132_alt_effect_slider_info(struct snd_kcontrol *kcontrol,
  4928. struct snd_ctl_elem_info *uinfo)
  4929. {
  4930. int chs = get_amp_channels(kcontrol);
  4931. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  4932. uinfo->count = chs == 3 ? 2 : 1;
  4933. uinfo->value.integer.min = 0;
  4934. uinfo->value.integer.max = 100;
  4935. uinfo->value.integer.step = 1;
  4936. return 0;
  4937. }
  4938. static int ca0132_alt_xbass_xover_slider_put(struct snd_kcontrol *kcontrol,
  4939. struct snd_ctl_elem_value *ucontrol)
  4940. {
  4941. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  4942. struct ca0132_spec *spec = codec->spec;
  4943. hda_nid_t nid = get_amp_nid(kcontrol);
  4944. long *valp = ucontrol->value.integer.value;
  4945. long *cur_val;
  4946. int idx;
  4947. if (nid == BASS_REDIRECTION_XOVER)
  4948. cur_val = &spec->bass_redirect_xover_freq;
  4949. else
  4950. cur_val = &spec->xbass_xover_freq;
  4951. /* any change? */
  4952. if (*cur_val == *valp)
  4953. return 0;
  4954. *cur_val = *valp;
  4955. idx = *valp;
  4956. if (nid == BASS_REDIRECTION_XOVER)
  4957. ca0132_alt_bass_redirection_xover_set(codec, *cur_val);
  4958. else
  4959. ca0132_alt_slider_ctl_set(codec, nid, float_xbass_xover_lookup, idx);
  4960. return 0;
  4961. }
  4962. static int ca0132_alt_effect_slider_put(struct snd_kcontrol *kcontrol,
  4963. struct snd_ctl_elem_value *ucontrol)
  4964. {
  4965. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  4966. struct ca0132_spec *spec = codec->spec;
  4967. hda_nid_t nid = get_amp_nid(kcontrol);
  4968. long *valp = ucontrol->value.integer.value;
  4969. int idx;
  4970. idx = nid - EFFECT_START_NID;
  4971. /* any change? */
  4972. if (spec->fx_ctl_val[idx] == *valp)
  4973. return 0;
  4974. spec->fx_ctl_val[idx] = *valp;
  4975. idx = *valp;
  4976. ca0132_alt_slider_ctl_set(codec, nid, float_zero_to_one_lookup, idx);
  4977. return 0;
  4978. }
  4979. /*
  4980. * Mic Boost Enum for alternative ca0132 codecs. I didn't like that the original
  4981. * only has off or full 30 dB, and didn't like making a volume slider that has
  4982. * traditional 0-100 in alsamixer that goes in big steps. I like enum better.
  4983. */
  4984. #define MIC_BOOST_NUM_OF_STEPS 4
  4985. #define MIC_BOOST_ENUM_MAX_STRLEN 10
  4986. static int ca0132_alt_mic_boost_info(struct snd_kcontrol *kcontrol,
  4987. struct snd_ctl_elem_info *uinfo)
  4988. {
  4989. char *sfx = "dB";
  4990. char namestr[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
  4991. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  4992. uinfo->count = 1;
  4993. uinfo->value.enumerated.items = MIC_BOOST_NUM_OF_STEPS;
  4994. if (uinfo->value.enumerated.item >= MIC_BOOST_NUM_OF_STEPS)
  4995. uinfo->value.enumerated.item = MIC_BOOST_NUM_OF_STEPS - 1;
  4996. sprintf(namestr, "%d %s", (uinfo->value.enumerated.item * 10), sfx);
  4997. strcpy(uinfo->value.enumerated.name, namestr);
  4998. return 0;
  4999. }
  5000. static int ca0132_alt_mic_boost_get(struct snd_kcontrol *kcontrol,
  5001. struct snd_ctl_elem_value *ucontrol)
  5002. {
  5003. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  5004. struct ca0132_spec *spec = codec->spec;
  5005. ucontrol->value.enumerated.item[0] = spec->mic_boost_enum_val;
  5006. return 0;
  5007. }
  5008. static int ca0132_alt_mic_boost_put(struct snd_kcontrol *kcontrol,
  5009. struct snd_ctl_elem_value *ucontrol)
  5010. {
  5011. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  5012. struct ca0132_spec *spec = codec->spec;
  5013. int sel = ucontrol->value.enumerated.item[0];
  5014. unsigned int items = MIC_BOOST_NUM_OF_STEPS;
  5015. if (sel >= items)
  5016. return 0;
  5017. codec_dbg(codec, "ca0132_alt_mic_boost: boost=%d\n",
  5018. sel);
  5019. spec->mic_boost_enum_val = sel;
  5020. if (spec->in_enum_val != REAR_LINE_IN)
  5021. ca0132_alt_mic_boost_set(codec, spec->mic_boost_enum_val);
  5022. return 1;
  5023. }
  5024. /*
  5025. * Sound BlasterX AE-5 Headphone Gain Controls.
  5026. */
  5027. #define AE5_HEADPHONE_GAIN_MAX 3
  5028. static int ae5_headphone_gain_info(struct snd_kcontrol *kcontrol,
  5029. struct snd_ctl_elem_info *uinfo)
  5030. {
  5031. char *sfx = " Ohms)";
  5032. char namestr[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
  5033. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  5034. uinfo->count = 1;
  5035. uinfo->value.enumerated.items = AE5_HEADPHONE_GAIN_MAX;
  5036. if (uinfo->value.enumerated.item >= AE5_HEADPHONE_GAIN_MAX)
  5037. uinfo->value.enumerated.item = AE5_HEADPHONE_GAIN_MAX - 1;
  5038. sprintf(namestr, "%s %s",
  5039. ae5_headphone_gain_presets[uinfo->value.enumerated.item].name,
  5040. sfx);
  5041. strcpy(uinfo->value.enumerated.name, namestr);
  5042. return 0;
  5043. }
  5044. static int ae5_headphone_gain_get(struct snd_kcontrol *kcontrol,
  5045. struct snd_ctl_elem_value *ucontrol)
  5046. {
  5047. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  5048. struct ca0132_spec *spec = codec->spec;
  5049. ucontrol->value.enumerated.item[0] = spec->ae5_headphone_gain_val;
  5050. return 0;
  5051. }
  5052. static int ae5_headphone_gain_put(struct snd_kcontrol *kcontrol,
  5053. struct snd_ctl_elem_value *ucontrol)
  5054. {
  5055. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  5056. struct ca0132_spec *spec = codec->spec;
  5057. int sel = ucontrol->value.enumerated.item[0];
  5058. unsigned int items = AE5_HEADPHONE_GAIN_MAX;
  5059. if (sel >= items)
  5060. return 0;
  5061. codec_dbg(codec, "ae5_headphone_gain: boost=%d\n",
  5062. sel);
  5063. spec->ae5_headphone_gain_val = sel;
  5064. if (spec->out_enum_val == HEADPHONE_OUT)
  5065. ae5_headphone_gain_set(codec, spec->ae5_headphone_gain_val);
  5066. return 1;
  5067. }
  5068. /*
  5069. * Sound BlasterX AE-5 sound filter enumerated control.
  5070. */
  5071. #define AE5_SOUND_FILTER_MAX 3
  5072. static int ae5_sound_filter_info(struct snd_kcontrol *kcontrol,
  5073. struct snd_ctl_elem_info *uinfo)
  5074. {
  5075. char namestr[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
  5076. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  5077. uinfo->count = 1;
  5078. uinfo->value.enumerated.items = AE5_SOUND_FILTER_MAX;
  5079. if (uinfo->value.enumerated.item >= AE5_SOUND_FILTER_MAX)
  5080. uinfo->value.enumerated.item = AE5_SOUND_FILTER_MAX - 1;
  5081. sprintf(namestr, "%s",
  5082. ae5_filter_presets[uinfo->value.enumerated.item].name);
  5083. strcpy(uinfo->value.enumerated.name, namestr);
  5084. return 0;
  5085. }
  5086. static int ae5_sound_filter_get(struct snd_kcontrol *kcontrol,
  5087. struct snd_ctl_elem_value *ucontrol)
  5088. {
  5089. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  5090. struct ca0132_spec *spec = codec->spec;
  5091. ucontrol->value.enumerated.item[0] = spec->ae5_filter_val;
  5092. return 0;
  5093. }
  5094. static int ae5_sound_filter_put(struct snd_kcontrol *kcontrol,
  5095. struct snd_ctl_elem_value *ucontrol)
  5096. {
  5097. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  5098. struct ca0132_spec *spec = codec->spec;
  5099. int sel = ucontrol->value.enumerated.item[0];
  5100. unsigned int items = AE5_SOUND_FILTER_MAX;
  5101. if (sel >= items)
  5102. return 0;
  5103. codec_dbg(codec, "ae5_sound_filter: %s\n",
  5104. ae5_filter_presets[sel].name);
  5105. spec->ae5_filter_val = sel;
  5106. ca0113_mmio_command_set_type2(codec, 0x48, 0x07,
  5107. ae5_filter_presets[sel].val);
  5108. return 1;
  5109. }
  5110. /*
  5111. * Input Select Control for alternative ca0132 codecs. This exists because
  5112. * front microphone has no auto-detect, and we need a way to set the rear
  5113. * as line-in
  5114. */
  5115. static int ca0132_alt_input_source_info(struct snd_kcontrol *kcontrol,
  5116. struct snd_ctl_elem_info *uinfo)
  5117. {
  5118. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  5119. uinfo->count = 1;
  5120. uinfo->value.enumerated.items = IN_SRC_NUM_OF_INPUTS;
  5121. if (uinfo->value.enumerated.item >= IN_SRC_NUM_OF_INPUTS)
  5122. uinfo->value.enumerated.item = IN_SRC_NUM_OF_INPUTS - 1;
  5123. strcpy(uinfo->value.enumerated.name,
  5124. in_src_str[uinfo->value.enumerated.item]);
  5125. return 0;
  5126. }
  5127. static int ca0132_alt_input_source_get(struct snd_kcontrol *kcontrol,
  5128. struct snd_ctl_elem_value *ucontrol)
  5129. {
  5130. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  5131. struct ca0132_spec *spec = codec->spec;
  5132. ucontrol->value.enumerated.item[0] = spec->in_enum_val;
  5133. return 0;
  5134. }
  5135. static int ca0132_alt_input_source_put(struct snd_kcontrol *kcontrol,
  5136. struct snd_ctl_elem_value *ucontrol)
  5137. {
  5138. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  5139. struct ca0132_spec *spec = codec->spec;
  5140. int sel = ucontrol->value.enumerated.item[0];
  5141. unsigned int items = IN_SRC_NUM_OF_INPUTS;
  5142. /*
  5143. * The AE-7 has no front microphone, so limit items to 2: rear mic and
  5144. * line-in.
  5145. */
  5146. if (ca0132_quirk(spec) == QUIRK_AE7)
  5147. items = 2;
  5148. if (sel >= items)
  5149. return 0;
  5150. codec_dbg(codec, "ca0132_alt_input_select: sel=%d, preset=%s\n",
  5151. sel, in_src_str[sel]);
  5152. spec->in_enum_val = sel;
  5153. ca0132_alt_select_in(codec);
  5154. return 1;
  5155. }
  5156. /* Sound Blaster Z Output Select Control */
  5157. static int ca0132_alt_output_select_get_info(struct snd_kcontrol *kcontrol,
  5158. struct snd_ctl_elem_info *uinfo)
  5159. {
  5160. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  5161. uinfo->count = 1;
  5162. uinfo->value.enumerated.items = NUM_OF_OUTPUTS;
  5163. if (uinfo->value.enumerated.item >= NUM_OF_OUTPUTS)
  5164. uinfo->value.enumerated.item = NUM_OF_OUTPUTS - 1;
  5165. strcpy(uinfo->value.enumerated.name,
  5166. out_type_str[uinfo->value.enumerated.item]);
  5167. return 0;
  5168. }
  5169. static int ca0132_alt_output_select_get(struct snd_kcontrol *kcontrol,
  5170. struct snd_ctl_elem_value *ucontrol)
  5171. {
  5172. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  5173. struct ca0132_spec *spec = codec->spec;
  5174. ucontrol->value.enumerated.item[0] = spec->out_enum_val;
  5175. return 0;
  5176. }
  5177. static int ca0132_alt_output_select_put(struct snd_kcontrol *kcontrol,
  5178. struct snd_ctl_elem_value *ucontrol)
  5179. {
  5180. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  5181. struct ca0132_spec *spec = codec->spec;
  5182. int sel = ucontrol->value.enumerated.item[0];
  5183. unsigned int items = NUM_OF_OUTPUTS;
  5184. unsigned int auto_jack;
  5185. if (sel >= items)
  5186. return 0;
  5187. codec_dbg(codec, "ca0132_alt_output_select: sel=%d, preset=%s\n",
  5188. sel, out_type_str[sel]);
  5189. spec->out_enum_val = sel;
  5190. auto_jack = spec->vnode_lswitch[VNID_HP_ASEL - VNODE_START_NID];
  5191. if (!auto_jack)
  5192. ca0132_alt_select_out(codec);
  5193. return 1;
  5194. }
  5195. /* Select surround output type: 2.1, 4.0, 4.1, or 5.1. */
  5196. static int ca0132_alt_speaker_channel_cfg_get_info(struct snd_kcontrol *kcontrol,
  5197. struct snd_ctl_elem_info *uinfo)
  5198. {
  5199. unsigned int items = SPEAKER_CHANNEL_CFG_COUNT;
  5200. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  5201. uinfo->count = 1;
  5202. uinfo->value.enumerated.items = items;
  5203. if (uinfo->value.enumerated.item >= items)
  5204. uinfo->value.enumerated.item = items - 1;
  5205. strcpy(uinfo->value.enumerated.name,
  5206. speaker_channel_cfgs[uinfo->value.enumerated.item].name);
  5207. return 0;
  5208. }
  5209. static int ca0132_alt_speaker_channel_cfg_get(struct snd_kcontrol *kcontrol,
  5210. struct snd_ctl_elem_value *ucontrol)
  5211. {
  5212. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  5213. struct ca0132_spec *spec = codec->spec;
  5214. ucontrol->value.enumerated.item[0] = spec->channel_cfg_val;
  5215. return 0;
  5216. }
  5217. static int ca0132_alt_speaker_channel_cfg_put(struct snd_kcontrol *kcontrol,
  5218. struct snd_ctl_elem_value *ucontrol)
  5219. {
  5220. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  5221. struct ca0132_spec *spec = codec->spec;
  5222. int sel = ucontrol->value.enumerated.item[0];
  5223. unsigned int items = SPEAKER_CHANNEL_CFG_COUNT;
  5224. if (sel >= items)
  5225. return 0;
  5226. codec_dbg(codec, "ca0132_alt_speaker_channels: sel=%d, channels=%s\n",
  5227. sel, speaker_channel_cfgs[sel].name);
  5228. spec->channel_cfg_val = sel;
  5229. if (spec->out_enum_val == SPEAKER_OUT)
  5230. ca0132_alt_select_out(codec);
  5231. return 1;
  5232. }
  5233. /*
  5234. * Smart Volume output setting control. Three different settings, Normal,
  5235. * which takes the value from the smart volume slider. The two others, loud
  5236. * and night, disregard the slider value and have uneditable values.
  5237. */
  5238. #define NUM_OF_SVM_SETTINGS 3
  5239. static const char *const out_svm_set_enum_str[3] = {"Normal", "Loud", "Night" };
  5240. static int ca0132_alt_svm_setting_info(struct snd_kcontrol *kcontrol,
  5241. struct snd_ctl_elem_info *uinfo)
  5242. {
  5243. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  5244. uinfo->count = 1;
  5245. uinfo->value.enumerated.items = NUM_OF_SVM_SETTINGS;
  5246. if (uinfo->value.enumerated.item >= NUM_OF_SVM_SETTINGS)
  5247. uinfo->value.enumerated.item = NUM_OF_SVM_SETTINGS - 1;
  5248. strcpy(uinfo->value.enumerated.name,
  5249. out_svm_set_enum_str[uinfo->value.enumerated.item]);
  5250. return 0;
  5251. }
  5252. static int ca0132_alt_svm_setting_get(struct snd_kcontrol *kcontrol,
  5253. struct snd_ctl_elem_value *ucontrol)
  5254. {
  5255. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  5256. struct ca0132_spec *spec = codec->spec;
  5257. ucontrol->value.enumerated.item[0] = spec->smart_volume_setting;
  5258. return 0;
  5259. }
  5260. static int ca0132_alt_svm_setting_put(struct snd_kcontrol *kcontrol,
  5261. struct snd_ctl_elem_value *ucontrol)
  5262. {
  5263. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  5264. struct ca0132_spec *spec = codec->spec;
  5265. int sel = ucontrol->value.enumerated.item[0];
  5266. unsigned int items = NUM_OF_SVM_SETTINGS;
  5267. unsigned int idx = SMART_VOLUME - EFFECT_START_NID;
  5268. unsigned int tmp;
  5269. if (sel >= items)
  5270. return 0;
  5271. codec_dbg(codec, "ca0132_alt_svm_setting: sel=%d, preset=%s\n",
  5272. sel, out_svm_set_enum_str[sel]);
  5273. spec->smart_volume_setting = sel;
  5274. switch (sel) {
  5275. case 0:
  5276. tmp = FLOAT_ZERO;
  5277. break;
  5278. case 1:
  5279. tmp = FLOAT_ONE;
  5280. break;
  5281. case 2:
  5282. tmp = FLOAT_TWO;
  5283. break;
  5284. default:
  5285. tmp = FLOAT_ZERO;
  5286. break;
  5287. }
  5288. /* Req 2 is the Smart Volume Setting req. */
  5289. dspio_set_uint_param(codec, ca0132_effects[idx].mid,
  5290. ca0132_effects[idx].reqs[2], tmp);
  5291. return 1;
  5292. }
  5293. /* Sound Blaster Z EQ preset controls */
  5294. static int ca0132_alt_eq_preset_info(struct snd_kcontrol *kcontrol,
  5295. struct snd_ctl_elem_info *uinfo)
  5296. {
  5297. unsigned int items = ARRAY_SIZE(ca0132_alt_eq_presets);
  5298. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  5299. uinfo->count = 1;
  5300. uinfo->value.enumerated.items = items;
  5301. if (uinfo->value.enumerated.item >= items)
  5302. uinfo->value.enumerated.item = items - 1;
  5303. strcpy(uinfo->value.enumerated.name,
  5304. ca0132_alt_eq_presets[uinfo->value.enumerated.item].name);
  5305. return 0;
  5306. }
  5307. static int ca0132_alt_eq_preset_get(struct snd_kcontrol *kcontrol,
  5308. struct snd_ctl_elem_value *ucontrol)
  5309. {
  5310. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  5311. struct ca0132_spec *spec = codec->spec;
  5312. ucontrol->value.enumerated.item[0] = spec->eq_preset_val;
  5313. return 0;
  5314. }
  5315. static int ca0132_alt_eq_preset_put(struct snd_kcontrol *kcontrol,
  5316. struct snd_ctl_elem_value *ucontrol)
  5317. {
  5318. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  5319. struct ca0132_spec *spec = codec->spec;
  5320. int i, err = 0;
  5321. int sel = ucontrol->value.enumerated.item[0];
  5322. unsigned int items = ARRAY_SIZE(ca0132_alt_eq_presets);
  5323. if (sel >= items)
  5324. return 0;
  5325. codec_dbg(codec, "%s: sel=%d, preset=%s\n", __func__, sel,
  5326. ca0132_alt_eq_presets[sel].name);
  5327. /*
  5328. * Idx 0 is default.
  5329. * Default needs to qualify with CrystalVoice state.
  5330. */
  5331. for (i = 0; i < EQ_PRESET_MAX_PARAM_COUNT; i++) {
  5332. err = dspio_set_uint_param(codec, ca0132_alt_eq_enum.mid,
  5333. ca0132_alt_eq_enum.reqs[i],
  5334. ca0132_alt_eq_presets[sel].vals[i]);
  5335. if (err < 0)
  5336. break;
  5337. }
  5338. if (err >= 0)
  5339. spec->eq_preset_val = sel;
  5340. return 1;
  5341. }
  5342. static int ca0132_voicefx_info(struct snd_kcontrol *kcontrol,
  5343. struct snd_ctl_elem_info *uinfo)
  5344. {
  5345. unsigned int items = ARRAY_SIZE(ca0132_voicefx_presets);
  5346. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  5347. uinfo->count = 1;
  5348. uinfo->value.enumerated.items = items;
  5349. if (uinfo->value.enumerated.item >= items)
  5350. uinfo->value.enumerated.item = items - 1;
  5351. strcpy(uinfo->value.enumerated.name,
  5352. ca0132_voicefx_presets[uinfo->value.enumerated.item].name);
  5353. return 0;
  5354. }
  5355. static int ca0132_voicefx_get(struct snd_kcontrol *kcontrol,
  5356. struct snd_ctl_elem_value *ucontrol)
  5357. {
  5358. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  5359. struct ca0132_spec *spec = codec->spec;
  5360. ucontrol->value.enumerated.item[0] = spec->voicefx_val;
  5361. return 0;
  5362. }
  5363. static int ca0132_voicefx_put(struct snd_kcontrol *kcontrol,
  5364. struct snd_ctl_elem_value *ucontrol)
  5365. {
  5366. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  5367. struct ca0132_spec *spec = codec->spec;
  5368. int i, err = 0;
  5369. int sel = ucontrol->value.enumerated.item[0];
  5370. if (sel >= ARRAY_SIZE(ca0132_voicefx_presets))
  5371. return 0;
  5372. codec_dbg(codec, "ca0132_voicefx_put: sel=%d, preset=%s\n",
  5373. sel, ca0132_voicefx_presets[sel].name);
  5374. /*
  5375. * Idx 0 is default.
  5376. * Default needs to qualify with CrystalVoice state.
  5377. */
  5378. for (i = 0; i < VOICEFX_MAX_PARAM_COUNT; i++) {
  5379. err = dspio_set_uint_param(codec, ca0132_voicefx.mid,
  5380. ca0132_voicefx.reqs[i],
  5381. ca0132_voicefx_presets[sel].vals[i]);
  5382. if (err < 0)
  5383. break;
  5384. }
  5385. if (err >= 0) {
  5386. spec->voicefx_val = sel;
  5387. /* enable voice fx */
  5388. ca0132_voicefx_set(codec, (sel ? 1 : 0));
  5389. }
  5390. return 1;
  5391. }
  5392. static int ca0132_switch_get(struct snd_kcontrol *kcontrol,
  5393. struct snd_ctl_elem_value *ucontrol)
  5394. {
  5395. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  5396. struct ca0132_spec *spec = codec->spec;
  5397. hda_nid_t nid = get_amp_nid(kcontrol);
  5398. int ch = get_amp_channels(kcontrol);
  5399. long *valp = ucontrol->value.integer.value;
  5400. /* vnode */
  5401. if ((nid >= VNODE_START_NID) && (nid < VNODE_END_NID)) {
  5402. if (ch & 1) {
  5403. *valp = spec->vnode_lswitch[nid - VNODE_START_NID];
  5404. valp++;
  5405. }
  5406. if (ch & 2) {
  5407. *valp = spec->vnode_rswitch[nid - VNODE_START_NID];
  5408. valp++;
  5409. }
  5410. return 0;
  5411. }
  5412. /* effects, include PE and CrystalVoice */
  5413. if ((nid >= EFFECT_START_NID) && (nid < EFFECT_END_NID)) {
  5414. *valp = spec->effects_switch[nid - EFFECT_START_NID];
  5415. return 0;
  5416. }
  5417. /* mic boost */
  5418. if (nid == spec->input_pins[0]) {
  5419. *valp = spec->cur_mic_boost;
  5420. return 0;
  5421. }
  5422. if (nid == ZXR_HEADPHONE_GAIN) {
  5423. *valp = spec->zxr_gain_set;
  5424. return 0;
  5425. }
  5426. if (nid == SPEAKER_FULL_RANGE_FRONT || nid == SPEAKER_FULL_RANGE_REAR) {
  5427. *valp = spec->speaker_range_val[nid - SPEAKER_FULL_RANGE_FRONT];
  5428. return 0;
  5429. }
  5430. if (nid == BASS_REDIRECTION) {
  5431. *valp = spec->bass_redirection_val;
  5432. return 0;
  5433. }
  5434. return 0;
  5435. }
  5436. static int ca0132_switch_put(struct snd_kcontrol *kcontrol,
  5437. struct snd_ctl_elem_value *ucontrol)
  5438. {
  5439. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  5440. struct ca0132_spec *spec = codec->spec;
  5441. hda_nid_t nid = get_amp_nid(kcontrol);
  5442. int ch = get_amp_channels(kcontrol);
  5443. long *valp = ucontrol->value.integer.value;
  5444. int changed = 1;
  5445. codec_dbg(codec, "ca0132_switch_put: nid=0x%x, val=%ld\n",
  5446. nid, *valp);
  5447. snd_hda_power_up(codec);
  5448. /* vnode */
  5449. if ((nid >= VNODE_START_NID) && (nid < VNODE_END_NID)) {
  5450. if (ch & 1) {
  5451. spec->vnode_lswitch[nid - VNODE_START_NID] = *valp;
  5452. valp++;
  5453. }
  5454. if (ch & 2) {
  5455. spec->vnode_rswitch[nid - VNODE_START_NID] = *valp;
  5456. valp++;
  5457. }
  5458. changed = ca0132_vnode_switch_set(kcontrol, ucontrol);
  5459. goto exit;
  5460. }
  5461. /* PE */
  5462. if (nid == PLAY_ENHANCEMENT) {
  5463. spec->effects_switch[nid - EFFECT_START_NID] = *valp;
  5464. changed = ca0132_pe_switch_set(codec);
  5465. goto exit;
  5466. }
  5467. /* CrystalVoice */
  5468. if (nid == CRYSTAL_VOICE) {
  5469. spec->effects_switch[nid - EFFECT_START_NID] = *valp;
  5470. changed = ca0132_cvoice_switch_set(codec);
  5471. goto exit;
  5472. }
  5473. /* out and in effects */
  5474. if (((nid >= OUT_EFFECT_START_NID) && (nid < OUT_EFFECT_END_NID)) ||
  5475. ((nid >= IN_EFFECT_START_NID) && (nid < IN_EFFECT_END_NID))) {
  5476. spec->effects_switch[nid - EFFECT_START_NID] = *valp;
  5477. changed = ca0132_effects_set(codec, nid, *valp);
  5478. goto exit;
  5479. }
  5480. /* mic boost */
  5481. if (nid == spec->input_pins[0]) {
  5482. spec->cur_mic_boost = *valp;
  5483. if (ca0132_use_alt_functions(spec)) {
  5484. if (spec->in_enum_val != REAR_LINE_IN)
  5485. changed = ca0132_mic_boost_set(codec, *valp);
  5486. } else {
  5487. /* Mic boost does not apply to Digital Mic */
  5488. if (spec->cur_mic_type != DIGITAL_MIC)
  5489. changed = ca0132_mic_boost_set(codec, *valp);
  5490. }
  5491. goto exit;
  5492. }
  5493. if (nid == ZXR_HEADPHONE_GAIN) {
  5494. spec->zxr_gain_set = *valp;
  5495. if (spec->cur_out_type == HEADPHONE_OUT)
  5496. changed = zxr_headphone_gain_set(codec, *valp);
  5497. else
  5498. changed = 0;
  5499. goto exit;
  5500. }
  5501. if (nid == SPEAKER_FULL_RANGE_FRONT || nid == SPEAKER_FULL_RANGE_REAR) {
  5502. spec->speaker_range_val[nid - SPEAKER_FULL_RANGE_FRONT] = *valp;
  5503. if (spec->cur_out_type == SPEAKER_OUT)
  5504. ca0132_alt_set_full_range_speaker(codec);
  5505. changed = 0;
  5506. }
  5507. if (nid == BASS_REDIRECTION) {
  5508. spec->bass_redirection_val = *valp;
  5509. if (spec->cur_out_type == SPEAKER_OUT)
  5510. ca0132_alt_surround_set_bass_redirection(codec, *valp);
  5511. changed = 0;
  5512. }
  5513. exit:
  5514. snd_hda_power_down(codec);
  5515. return changed;
  5516. }
  5517. /*
  5518. * Volume related
  5519. */
  5520. /*
  5521. * Sets the internal DSP decibel level to match the DAC for output, and the
  5522. * ADC for input. Currently only the SBZ sets dsp capture volume level, and
  5523. * all alternative codecs set DSP playback volume.
  5524. */
  5525. static void ca0132_alt_dsp_volume_put(struct hda_codec *codec, hda_nid_t nid)
  5526. {
  5527. struct ca0132_spec *spec = codec->spec;
  5528. unsigned int dsp_dir;
  5529. unsigned int lookup_val;
  5530. if (nid == VNID_SPK)
  5531. dsp_dir = DSP_VOL_OUT;
  5532. else
  5533. dsp_dir = DSP_VOL_IN;
  5534. lookup_val = spec->vnode_lvol[nid - VNODE_START_NID];
  5535. dspio_set_uint_param(codec,
  5536. ca0132_alt_vol_ctls[dsp_dir].mid,
  5537. ca0132_alt_vol_ctls[dsp_dir].reqs[0],
  5538. float_vol_db_lookup[lookup_val]);
  5539. lookup_val = spec->vnode_rvol[nid - VNODE_START_NID];
  5540. dspio_set_uint_param(codec,
  5541. ca0132_alt_vol_ctls[dsp_dir].mid,
  5542. ca0132_alt_vol_ctls[dsp_dir].reqs[1],
  5543. float_vol_db_lookup[lookup_val]);
  5544. dspio_set_uint_param(codec,
  5545. ca0132_alt_vol_ctls[dsp_dir].mid,
  5546. ca0132_alt_vol_ctls[dsp_dir].reqs[2], FLOAT_ZERO);
  5547. }
  5548. static int ca0132_volume_info(struct snd_kcontrol *kcontrol,
  5549. struct snd_ctl_elem_info *uinfo)
  5550. {
  5551. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  5552. struct ca0132_spec *spec = codec->spec;
  5553. hda_nid_t nid = get_amp_nid(kcontrol);
  5554. int ch = get_amp_channels(kcontrol);
  5555. int dir = get_amp_direction(kcontrol);
  5556. unsigned long pval;
  5557. int err;
  5558. switch (nid) {
  5559. case VNID_SPK:
  5560. /* follow shared_out info */
  5561. nid = spec->shared_out_nid;
  5562. mutex_lock(&codec->control_mutex);
  5563. pval = kcontrol->private_value;
  5564. kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
  5565. err = snd_hda_mixer_amp_volume_info(kcontrol, uinfo);
  5566. kcontrol->private_value = pval;
  5567. mutex_unlock(&codec->control_mutex);
  5568. break;
  5569. case VNID_MIC:
  5570. /* follow shared_mic info */
  5571. nid = spec->shared_mic_nid;
  5572. mutex_lock(&codec->control_mutex);
  5573. pval = kcontrol->private_value;
  5574. kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
  5575. err = snd_hda_mixer_amp_volume_info(kcontrol, uinfo);
  5576. kcontrol->private_value = pval;
  5577. mutex_unlock(&codec->control_mutex);
  5578. break;
  5579. default:
  5580. err = snd_hda_mixer_amp_volume_info(kcontrol, uinfo);
  5581. }
  5582. return err;
  5583. }
  5584. static int ca0132_volume_get(struct snd_kcontrol *kcontrol,
  5585. struct snd_ctl_elem_value *ucontrol)
  5586. {
  5587. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  5588. struct ca0132_spec *spec = codec->spec;
  5589. hda_nid_t nid = get_amp_nid(kcontrol);
  5590. int ch = get_amp_channels(kcontrol);
  5591. long *valp = ucontrol->value.integer.value;
  5592. /* store the left and right volume */
  5593. if (ch & 1) {
  5594. *valp = spec->vnode_lvol[nid - VNODE_START_NID];
  5595. valp++;
  5596. }
  5597. if (ch & 2) {
  5598. *valp = spec->vnode_rvol[nid - VNODE_START_NID];
  5599. valp++;
  5600. }
  5601. return 0;
  5602. }
  5603. static int ca0132_volume_put(struct snd_kcontrol *kcontrol,
  5604. struct snd_ctl_elem_value *ucontrol)
  5605. {
  5606. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  5607. struct ca0132_spec *spec = codec->spec;
  5608. hda_nid_t nid = get_amp_nid(kcontrol);
  5609. int ch = get_amp_channels(kcontrol);
  5610. long *valp = ucontrol->value.integer.value;
  5611. hda_nid_t shared_nid = 0;
  5612. bool effective;
  5613. int changed = 1;
  5614. /* store the left and right volume */
  5615. if (ch & 1) {
  5616. spec->vnode_lvol[nid - VNODE_START_NID] = *valp;
  5617. valp++;
  5618. }
  5619. if (ch & 2) {
  5620. spec->vnode_rvol[nid - VNODE_START_NID] = *valp;
  5621. valp++;
  5622. }
  5623. /* if effective conditions, then update hw immediately. */
  5624. effective = ca0132_is_vnode_effective(codec, nid, &shared_nid);
  5625. if (effective) {
  5626. int dir = get_amp_direction(kcontrol);
  5627. unsigned long pval;
  5628. snd_hda_power_up(codec);
  5629. mutex_lock(&codec->control_mutex);
  5630. pval = kcontrol->private_value;
  5631. kcontrol->private_value = HDA_COMPOSE_AMP_VAL(shared_nid, ch,
  5632. 0, dir);
  5633. changed = snd_hda_mixer_amp_volume_put(kcontrol, ucontrol);
  5634. kcontrol->private_value = pval;
  5635. mutex_unlock(&codec->control_mutex);
  5636. snd_hda_power_down(codec);
  5637. }
  5638. return changed;
  5639. }
  5640. /*
  5641. * This function is the same as the one above, because using an if statement
  5642. * inside of the above volume control for the DSP volume would cause too much
  5643. * lag. This is a lot more smooth.
  5644. */
  5645. static int ca0132_alt_volume_put(struct snd_kcontrol *kcontrol,
  5646. struct snd_ctl_elem_value *ucontrol)
  5647. {
  5648. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  5649. struct ca0132_spec *spec = codec->spec;
  5650. hda_nid_t nid = get_amp_nid(kcontrol);
  5651. int ch = get_amp_channels(kcontrol);
  5652. long *valp = ucontrol->value.integer.value;
  5653. hda_nid_t vnid = 0;
  5654. int changed;
  5655. switch (nid) {
  5656. case 0x02:
  5657. vnid = VNID_SPK;
  5658. break;
  5659. case 0x07:
  5660. vnid = VNID_MIC;
  5661. break;
  5662. }
  5663. /* store the left and right volume */
  5664. if (ch & 1) {
  5665. spec->vnode_lvol[vnid - VNODE_START_NID] = *valp;
  5666. valp++;
  5667. }
  5668. if (ch & 2) {
  5669. spec->vnode_rvol[vnid - VNODE_START_NID] = *valp;
  5670. valp++;
  5671. }
  5672. snd_hda_power_up(codec);
  5673. ca0132_alt_dsp_volume_put(codec, vnid);
  5674. mutex_lock(&codec->control_mutex);
  5675. changed = snd_hda_mixer_amp_volume_put(kcontrol, ucontrol);
  5676. mutex_unlock(&codec->control_mutex);
  5677. snd_hda_power_down(codec);
  5678. return changed;
  5679. }
  5680. static int ca0132_volume_tlv(struct snd_kcontrol *kcontrol, int op_flag,
  5681. unsigned int size, unsigned int __user *tlv)
  5682. {
  5683. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  5684. struct ca0132_spec *spec = codec->spec;
  5685. hda_nid_t nid = get_amp_nid(kcontrol);
  5686. int ch = get_amp_channels(kcontrol);
  5687. int dir = get_amp_direction(kcontrol);
  5688. unsigned long pval;
  5689. int err;
  5690. switch (nid) {
  5691. case VNID_SPK:
  5692. /* follow shared_out tlv */
  5693. nid = spec->shared_out_nid;
  5694. mutex_lock(&codec->control_mutex);
  5695. pval = kcontrol->private_value;
  5696. kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
  5697. err = snd_hda_mixer_amp_tlv(kcontrol, op_flag, size, tlv);
  5698. kcontrol->private_value = pval;
  5699. mutex_unlock(&codec->control_mutex);
  5700. break;
  5701. case VNID_MIC:
  5702. /* follow shared_mic tlv */
  5703. nid = spec->shared_mic_nid;
  5704. mutex_lock(&codec->control_mutex);
  5705. pval = kcontrol->private_value;
  5706. kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
  5707. err = snd_hda_mixer_amp_tlv(kcontrol, op_flag, size, tlv);
  5708. kcontrol->private_value = pval;
  5709. mutex_unlock(&codec->control_mutex);
  5710. break;
  5711. default:
  5712. err = snd_hda_mixer_amp_tlv(kcontrol, op_flag, size, tlv);
  5713. }
  5714. return err;
  5715. }
  5716. /* Add volume slider control for effect level */
  5717. static int ca0132_alt_add_effect_slider(struct hda_codec *codec, hda_nid_t nid,
  5718. const char *pfx, int dir)
  5719. {
  5720. char namestr[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
  5721. int type = dir ? HDA_INPUT : HDA_OUTPUT;
  5722. struct snd_kcontrol_new knew =
  5723. HDA_CODEC_VOLUME_MONO(namestr, nid, 1, 0, type);
  5724. sprintf(namestr, "FX: %s %s Volume", pfx, dirstr[dir]);
  5725. knew.tlv.c = NULL;
  5726. switch (nid) {
  5727. case XBASS_XOVER:
  5728. knew.info = ca0132_alt_xbass_xover_slider_info;
  5729. knew.get = ca0132_alt_xbass_xover_slider_ctl_get;
  5730. knew.put = ca0132_alt_xbass_xover_slider_put;
  5731. break;
  5732. default:
  5733. knew.info = ca0132_alt_effect_slider_info;
  5734. knew.get = ca0132_alt_slider_ctl_get;
  5735. knew.put = ca0132_alt_effect_slider_put;
  5736. knew.private_value =
  5737. HDA_COMPOSE_AMP_VAL(nid, 1, 0, type);
  5738. break;
  5739. }
  5740. return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
  5741. }
  5742. /*
  5743. * Added FX: prefix for the alternative codecs, because otherwise the surround
  5744. * effect would conflict with the Surround sound volume control. Also seems more
  5745. * clear as to what the switches do. Left alone for others.
  5746. */
  5747. static int add_fx_switch(struct hda_codec *codec, hda_nid_t nid,
  5748. const char *pfx, int dir)
  5749. {
  5750. struct ca0132_spec *spec = codec->spec;
  5751. char namestr[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
  5752. int type = dir ? HDA_INPUT : HDA_OUTPUT;
  5753. struct snd_kcontrol_new knew =
  5754. CA0132_CODEC_MUTE_MONO(namestr, nid, 1, type);
  5755. /* If using alt_controls, add FX: prefix. But, don't add FX:
  5756. * prefix to OutFX or InFX enable controls.
  5757. */
  5758. if (ca0132_use_alt_controls(spec) && (nid <= IN_EFFECT_END_NID))
  5759. sprintf(namestr, "FX: %s %s Switch", pfx, dirstr[dir]);
  5760. else
  5761. sprintf(namestr, "%s %s Switch", pfx, dirstr[dir]);
  5762. return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
  5763. }
  5764. static int add_voicefx(struct hda_codec *codec)
  5765. {
  5766. struct snd_kcontrol_new knew =
  5767. HDA_CODEC_MUTE_MONO(ca0132_voicefx.name,
  5768. VOICEFX, 1, 0, HDA_INPUT);
  5769. knew.info = ca0132_voicefx_info;
  5770. knew.get = ca0132_voicefx_get;
  5771. knew.put = ca0132_voicefx_put;
  5772. return snd_hda_ctl_add(codec, VOICEFX, snd_ctl_new1(&knew, codec));
  5773. }
  5774. /* Create the EQ Preset control */
  5775. static int add_ca0132_alt_eq_presets(struct hda_codec *codec)
  5776. {
  5777. struct snd_kcontrol_new knew =
  5778. HDA_CODEC_MUTE_MONO(ca0132_alt_eq_enum.name,
  5779. EQ_PRESET_ENUM, 1, 0, HDA_OUTPUT);
  5780. knew.info = ca0132_alt_eq_preset_info;
  5781. knew.get = ca0132_alt_eq_preset_get;
  5782. knew.put = ca0132_alt_eq_preset_put;
  5783. return snd_hda_ctl_add(codec, EQ_PRESET_ENUM,
  5784. snd_ctl_new1(&knew, codec));
  5785. }
  5786. /*
  5787. * Add enumerated control for the three different settings of the smart volume
  5788. * output effect. Normal just uses the slider value, and loud and night are
  5789. * their own things that ignore that value.
  5790. */
  5791. static int ca0132_alt_add_svm_enum(struct hda_codec *codec)
  5792. {
  5793. struct snd_kcontrol_new knew =
  5794. HDA_CODEC_MUTE_MONO("FX: Smart Volume Setting",
  5795. SMART_VOLUME_ENUM, 1, 0, HDA_OUTPUT);
  5796. knew.info = ca0132_alt_svm_setting_info;
  5797. knew.get = ca0132_alt_svm_setting_get;
  5798. knew.put = ca0132_alt_svm_setting_put;
  5799. return snd_hda_ctl_add(codec, SMART_VOLUME_ENUM,
  5800. snd_ctl_new1(&knew, codec));
  5801. }
  5802. /*
  5803. * Create an Output Select enumerated control for codecs with surround
  5804. * out capabilities.
  5805. */
  5806. static int ca0132_alt_add_output_enum(struct hda_codec *codec)
  5807. {
  5808. struct snd_kcontrol_new knew =
  5809. HDA_CODEC_MUTE_MONO("Output Select",
  5810. OUTPUT_SOURCE_ENUM, 1, 0, HDA_OUTPUT);
  5811. knew.info = ca0132_alt_output_select_get_info;
  5812. knew.get = ca0132_alt_output_select_get;
  5813. knew.put = ca0132_alt_output_select_put;
  5814. return snd_hda_ctl_add(codec, OUTPUT_SOURCE_ENUM,
  5815. snd_ctl_new1(&knew, codec));
  5816. }
  5817. /*
  5818. * Add a control for selecting channel count on speaker output. Setting this
  5819. * allows the DSP to do bass redirection and channel upmixing on surround
  5820. * configurations.
  5821. */
  5822. static int ca0132_alt_add_speaker_channel_cfg_enum(struct hda_codec *codec)
  5823. {
  5824. struct snd_kcontrol_new knew =
  5825. HDA_CODEC_MUTE_MONO("Surround Channel Config",
  5826. SPEAKER_CHANNEL_CFG_ENUM, 1, 0, HDA_OUTPUT);
  5827. knew.info = ca0132_alt_speaker_channel_cfg_get_info;
  5828. knew.get = ca0132_alt_speaker_channel_cfg_get;
  5829. knew.put = ca0132_alt_speaker_channel_cfg_put;
  5830. return snd_hda_ctl_add(codec, SPEAKER_CHANNEL_CFG_ENUM,
  5831. snd_ctl_new1(&knew, codec));
  5832. }
  5833. /*
  5834. * Full range front stereo and rear surround switches. When these are set to
  5835. * full range, the lower frequencies from these channels are no longer
  5836. * redirected to the LFE channel.
  5837. */
  5838. static int ca0132_alt_add_front_full_range_switch(struct hda_codec *codec)
  5839. {
  5840. struct snd_kcontrol_new knew =
  5841. CA0132_CODEC_MUTE_MONO("Full-Range Front Speakers",
  5842. SPEAKER_FULL_RANGE_FRONT, 1, HDA_OUTPUT);
  5843. return snd_hda_ctl_add(codec, SPEAKER_FULL_RANGE_FRONT,
  5844. snd_ctl_new1(&knew, codec));
  5845. }
  5846. static int ca0132_alt_add_rear_full_range_switch(struct hda_codec *codec)
  5847. {
  5848. struct snd_kcontrol_new knew =
  5849. CA0132_CODEC_MUTE_MONO("Full-Range Rear Speakers",
  5850. SPEAKER_FULL_RANGE_REAR, 1, HDA_OUTPUT);
  5851. return snd_hda_ctl_add(codec, SPEAKER_FULL_RANGE_REAR,
  5852. snd_ctl_new1(&knew, codec));
  5853. }
  5854. /*
  5855. * Bass redirection redirects audio below the crossover frequency to the LFE
  5856. * channel on speakers that are set as not being full-range. On configurations
  5857. * without an LFE channel, it does nothing. Bass redirection seems to be the
  5858. * replacement for X-Bass on configurations with an LFE channel.
  5859. */
  5860. static int ca0132_alt_add_bass_redirection_crossover(struct hda_codec *codec)
  5861. {
  5862. const char *namestr = "Bass Redirection Crossover";
  5863. struct snd_kcontrol_new knew =
  5864. HDA_CODEC_VOLUME_MONO(namestr, BASS_REDIRECTION_XOVER, 1, 0,
  5865. HDA_OUTPUT);
  5866. knew.tlv.c = NULL;
  5867. knew.info = ca0132_alt_xbass_xover_slider_info;
  5868. knew.get = ca0132_alt_xbass_xover_slider_ctl_get;
  5869. knew.put = ca0132_alt_xbass_xover_slider_put;
  5870. return snd_hda_ctl_add(codec, BASS_REDIRECTION_XOVER,
  5871. snd_ctl_new1(&knew, codec));
  5872. }
  5873. static int ca0132_alt_add_bass_redirection_switch(struct hda_codec *codec)
  5874. {
  5875. const char *namestr = "Bass Redirection";
  5876. struct snd_kcontrol_new knew =
  5877. CA0132_CODEC_MUTE_MONO(namestr, BASS_REDIRECTION, 1,
  5878. HDA_OUTPUT);
  5879. return snd_hda_ctl_add(codec, BASS_REDIRECTION,
  5880. snd_ctl_new1(&knew, codec));
  5881. }
  5882. /*
  5883. * Create an Input Source enumerated control for the alternate ca0132 codecs
  5884. * because the front microphone has no auto-detect, and Line-in has to be set
  5885. * somehow.
  5886. */
  5887. static int ca0132_alt_add_input_enum(struct hda_codec *codec)
  5888. {
  5889. struct snd_kcontrol_new knew =
  5890. HDA_CODEC_MUTE_MONO("Input Source",
  5891. INPUT_SOURCE_ENUM, 1, 0, HDA_INPUT);
  5892. knew.info = ca0132_alt_input_source_info;
  5893. knew.get = ca0132_alt_input_source_get;
  5894. knew.put = ca0132_alt_input_source_put;
  5895. return snd_hda_ctl_add(codec, INPUT_SOURCE_ENUM,
  5896. snd_ctl_new1(&knew, codec));
  5897. }
  5898. /*
  5899. * Add mic boost enumerated control. Switches through 0dB to 30dB. This adds
  5900. * more control than the original mic boost, which is either full 30dB or off.
  5901. */
  5902. static int ca0132_alt_add_mic_boost_enum(struct hda_codec *codec)
  5903. {
  5904. struct snd_kcontrol_new knew =
  5905. HDA_CODEC_MUTE_MONO("Mic Boost Capture Switch",
  5906. MIC_BOOST_ENUM, 1, 0, HDA_INPUT);
  5907. knew.info = ca0132_alt_mic_boost_info;
  5908. knew.get = ca0132_alt_mic_boost_get;
  5909. knew.put = ca0132_alt_mic_boost_put;
  5910. return snd_hda_ctl_add(codec, MIC_BOOST_ENUM,
  5911. snd_ctl_new1(&knew, codec));
  5912. }
  5913. /*
  5914. * Add headphone gain enumerated control for the AE-5. This switches between
  5915. * three modes, low, medium, and high. When non-headphone outputs are selected,
  5916. * it is automatically set to high. This is the same behavior as Windows.
  5917. */
  5918. static int ae5_add_headphone_gain_enum(struct hda_codec *codec)
  5919. {
  5920. struct snd_kcontrol_new knew =
  5921. HDA_CODEC_MUTE_MONO("AE-5: Headphone Gain",
  5922. AE5_HEADPHONE_GAIN_ENUM, 1, 0, HDA_OUTPUT);
  5923. knew.info = ae5_headphone_gain_info;
  5924. knew.get = ae5_headphone_gain_get;
  5925. knew.put = ae5_headphone_gain_put;
  5926. return snd_hda_ctl_add(codec, AE5_HEADPHONE_GAIN_ENUM,
  5927. snd_ctl_new1(&knew, codec));
  5928. }
  5929. /*
  5930. * Add sound filter enumerated control for the AE-5. This adds three different
  5931. * settings: Slow Roll Off, Minimum Phase, and Fast Roll Off. From what I've
  5932. * read into it, it changes the DAC's interpolation filter.
  5933. */
  5934. static int ae5_add_sound_filter_enum(struct hda_codec *codec)
  5935. {
  5936. struct snd_kcontrol_new knew =
  5937. HDA_CODEC_MUTE_MONO("AE-5: Sound Filter",
  5938. AE5_SOUND_FILTER_ENUM, 1, 0, HDA_OUTPUT);
  5939. knew.info = ae5_sound_filter_info;
  5940. knew.get = ae5_sound_filter_get;
  5941. knew.put = ae5_sound_filter_put;
  5942. return snd_hda_ctl_add(codec, AE5_SOUND_FILTER_ENUM,
  5943. snd_ctl_new1(&knew, codec));
  5944. }
  5945. static int zxr_add_headphone_gain_switch(struct hda_codec *codec)
  5946. {
  5947. struct snd_kcontrol_new knew =
  5948. CA0132_CODEC_MUTE_MONO("ZxR: 600 Ohm Gain",
  5949. ZXR_HEADPHONE_GAIN, 1, HDA_OUTPUT);
  5950. return snd_hda_ctl_add(codec, ZXR_HEADPHONE_GAIN,
  5951. snd_ctl_new1(&knew, codec));
  5952. }
  5953. /*
  5954. * Need to create follower controls for the alternate codecs that have surround
  5955. * capabilities.
  5956. */
  5957. static const char * const ca0132_alt_follower_pfxs[] = {
  5958. "Front", "Surround", "Center", "LFE", NULL,
  5959. };
  5960. /*
  5961. * Also need special channel map, because the default one is incorrect.
  5962. * I think this has to do with the pin for rear surround being 0x11,
  5963. * and the center/lfe being 0x10. Usually the pin order is the opposite.
  5964. */
  5965. static const struct snd_pcm_chmap_elem ca0132_alt_chmaps[] = {
  5966. { .channels = 2,
  5967. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
  5968. { .channels = 4,
  5969. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
  5970. SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
  5971. { .channels = 6,
  5972. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
  5973. SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE,
  5974. SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
  5975. { }
  5976. };
  5977. /* Add the correct chmap for streams with 6 channels. */
  5978. static void ca0132_alt_add_chmap_ctls(struct hda_codec *codec)
  5979. {
  5980. int err = 0;
  5981. struct hda_pcm *pcm;
  5982. list_for_each_entry(pcm, &codec->pcm_list_head, list) {
  5983. struct hda_pcm_stream *hinfo =
  5984. &pcm->stream[SNDRV_PCM_STREAM_PLAYBACK];
  5985. struct snd_pcm_chmap *chmap;
  5986. const struct snd_pcm_chmap_elem *elem;
  5987. elem = ca0132_alt_chmaps;
  5988. if (hinfo->channels_max == 6) {
  5989. err = snd_pcm_add_chmap_ctls(pcm->pcm,
  5990. SNDRV_PCM_STREAM_PLAYBACK,
  5991. elem, hinfo->channels_max, 0, &chmap);
  5992. if (err < 0)
  5993. codec_dbg(codec, "snd_pcm_add_chmap_ctls failed!");
  5994. }
  5995. }
  5996. }
  5997. /*
  5998. * When changing Node IDs for Mixer Controls below, make sure to update
  5999. * Node IDs in ca0132_config() as well.
  6000. */
  6001. static const struct snd_kcontrol_new ca0132_mixer[] = {
  6002. CA0132_CODEC_VOL("Master Playback Volume", VNID_SPK, HDA_OUTPUT),
  6003. CA0132_CODEC_MUTE("Master Playback Switch", VNID_SPK, HDA_OUTPUT),
  6004. CA0132_CODEC_VOL("Capture Volume", VNID_MIC, HDA_INPUT),
  6005. CA0132_CODEC_MUTE("Capture Switch", VNID_MIC, HDA_INPUT),
  6006. HDA_CODEC_VOLUME("Analog-Mic2 Capture Volume", 0x08, 0, HDA_INPUT),
  6007. HDA_CODEC_MUTE("Analog-Mic2 Capture Switch", 0x08, 0, HDA_INPUT),
  6008. HDA_CODEC_VOLUME("What U Hear Capture Volume", 0x0a, 0, HDA_INPUT),
  6009. HDA_CODEC_MUTE("What U Hear Capture Switch", 0x0a, 0, HDA_INPUT),
  6010. CA0132_CODEC_MUTE_MONO("Mic1-Boost (30dB) Capture Switch",
  6011. 0x12, 1, HDA_INPUT),
  6012. CA0132_CODEC_MUTE_MONO("HP/Speaker Playback Switch",
  6013. VNID_HP_SEL, 1, HDA_OUTPUT),
  6014. CA0132_CODEC_MUTE_MONO("AMic1/DMic Capture Switch",
  6015. VNID_AMIC1_SEL, 1, HDA_INPUT),
  6016. CA0132_CODEC_MUTE_MONO("HP/Speaker Auto Detect Playback Switch",
  6017. VNID_HP_ASEL, 1, HDA_OUTPUT),
  6018. CA0132_CODEC_MUTE_MONO("AMic1/DMic Auto Detect Capture Switch",
  6019. VNID_AMIC1_ASEL, 1, HDA_INPUT),
  6020. { } /* end */
  6021. };
  6022. /*
  6023. * Desktop specific control mixer. Removes auto-detect for mic, and adds
  6024. * surround controls. Also sets both the Front Playback and Capture Volume
  6025. * controls to alt so they set the DSP's decibel level.
  6026. */
  6027. static const struct snd_kcontrol_new desktop_mixer[] = {
  6028. CA0132_ALT_CODEC_VOL("Front Playback Volume", 0x02, HDA_OUTPUT),
  6029. CA0132_CODEC_MUTE("Front Playback Switch", VNID_SPK, HDA_OUTPUT),
  6030. HDA_CODEC_VOLUME("Surround Playback Volume", 0x04, 0, HDA_OUTPUT),
  6031. HDA_CODEC_MUTE("Surround Playback Switch", 0x04, 0, HDA_OUTPUT),
  6032. HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x03, 1, 0, HDA_OUTPUT),
  6033. HDA_CODEC_MUTE_MONO("Center Playback Switch", 0x03, 1, 0, HDA_OUTPUT),
  6034. HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x03, 2, 0, HDA_OUTPUT),
  6035. HDA_CODEC_MUTE_MONO("LFE Playback Switch", 0x03, 2, 0, HDA_OUTPUT),
  6036. CA0132_ALT_CODEC_VOL("Capture Volume", 0x07, HDA_INPUT),
  6037. CA0132_CODEC_MUTE("Capture Switch", VNID_MIC, HDA_INPUT),
  6038. HDA_CODEC_VOLUME("What U Hear Capture Volume", 0x0a, 0, HDA_INPUT),
  6039. HDA_CODEC_MUTE("What U Hear Capture Switch", 0x0a, 0, HDA_INPUT),
  6040. CA0132_CODEC_MUTE_MONO("HP/Speaker Auto Detect Playback Switch",
  6041. VNID_HP_ASEL, 1, HDA_OUTPUT),
  6042. { } /* end */
  6043. };
  6044. /*
  6045. * Same as the Sound Blaster Z, except doesn't use the alt volume for capture
  6046. * because it doesn't set decibel levels for the DSP for capture.
  6047. */
  6048. static const struct snd_kcontrol_new r3di_mixer[] = {
  6049. CA0132_ALT_CODEC_VOL("Front Playback Volume", 0x02, HDA_OUTPUT),
  6050. CA0132_CODEC_MUTE("Front Playback Switch", VNID_SPK, HDA_OUTPUT),
  6051. HDA_CODEC_VOLUME("Surround Playback Volume", 0x04, 0, HDA_OUTPUT),
  6052. HDA_CODEC_MUTE("Surround Playback Switch", 0x04, 0, HDA_OUTPUT),
  6053. HDA_CODEC_VOLUME_MONO("Center Playback Volume", 0x03, 1, 0, HDA_OUTPUT),
  6054. HDA_CODEC_MUTE_MONO("Center Playback Switch", 0x03, 1, 0, HDA_OUTPUT),
  6055. HDA_CODEC_VOLUME_MONO("LFE Playback Volume", 0x03, 2, 0, HDA_OUTPUT),
  6056. HDA_CODEC_MUTE_MONO("LFE Playback Switch", 0x03, 2, 0, HDA_OUTPUT),
  6057. CA0132_CODEC_VOL("Capture Volume", VNID_MIC, HDA_INPUT),
  6058. CA0132_CODEC_MUTE("Capture Switch", VNID_MIC, HDA_INPUT),
  6059. HDA_CODEC_VOLUME("What U Hear Capture Volume", 0x0a, 0, HDA_INPUT),
  6060. HDA_CODEC_MUTE("What U Hear Capture Switch", 0x0a, 0, HDA_INPUT),
  6061. CA0132_CODEC_MUTE_MONO("HP/Speaker Auto Detect Playback Switch",
  6062. VNID_HP_ASEL, 1, HDA_OUTPUT),
  6063. { } /* end */
  6064. };
  6065. static int ca0132_build_controls(struct hda_codec *codec)
  6066. {
  6067. struct ca0132_spec *spec = codec->spec;
  6068. int i, num_fx, num_sliders;
  6069. int err = 0;
  6070. /* Add Mixer controls */
  6071. for (i = 0; i < spec->num_mixers; i++) {
  6072. err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
  6073. if (err < 0)
  6074. return err;
  6075. }
  6076. /* Setup vmaster with surround followers for desktop ca0132 devices */
  6077. if (ca0132_use_alt_functions(spec)) {
  6078. snd_hda_set_vmaster_tlv(codec, spec->dacs[0], HDA_OUTPUT,
  6079. spec->tlv);
  6080. snd_hda_add_vmaster(codec, "Master Playback Volume",
  6081. spec->tlv, ca0132_alt_follower_pfxs,
  6082. "Playback Volume", 0);
  6083. err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
  6084. NULL, ca0132_alt_follower_pfxs,
  6085. "Playback Switch",
  6086. true, 0, &spec->vmaster_mute.sw_kctl);
  6087. if (err < 0)
  6088. return err;
  6089. }
  6090. /* Add in and out effects controls.
  6091. * VoiceFX, PE and CrystalVoice are added separately.
  6092. */
  6093. num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT;
  6094. for (i = 0; i < num_fx; i++) {
  6095. /* Desktop cards break if Echo Cancellation is used. */
  6096. if (ca0132_use_pci_mmio(spec)) {
  6097. if (i == (ECHO_CANCELLATION - IN_EFFECT_START_NID +
  6098. OUT_EFFECTS_COUNT))
  6099. continue;
  6100. }
  6101. err = add_fx_switch(codec, ca0132_effects[i].nid,
  6102. ca0132_effects[i].name,
  6103. ca0132_effects[i].direct);
  6104. if (err < 0)
  6105. return err;
  6106. }
  6107. /*
  6108. * If codec has use_alt_controls set to true, add effect level sliders,
  6109. * EQ presets, and Smart Volume presets. Also, change names to add FX
  6110. * prefix, and change PlayEnhancement and CrystalVoice to match.
  6111. */
  6112. if (ca0132_use_alt_controls(spec)) {
  6113. err = ca0132_alt_add_svm_enum(codec);
  6114. if (err < 0)
  6115. return err;
  6116. err = add_ca0132_alt_eq_presets(codec);
  6117. if (err < 0)
  6118. return err;
  6119. err = add_fx_switch(codec, PLAY_ENHANCEMENT,
  6120. "Enable OutFX", 0);
  6121. if (err < 0)
  6122. return err;
  6123. err = add_fx_switch(codec, CRYSTAL_VOICE,
  6124. "Enable InFX", 1);
  6125. if (err < 0)
  6126. return err;
  6127. num_sliders = OUT_EFFECTS_COUNT - 1;
  6128. for (i = 0; i < num_sliders; i++) {
  6129. err = ca0132_alt_add_effect_slider(codec,
  6130. ca0132_effects[i].nid,
  6131. ca0132_effects[i].name,
  6132. ca0132_effects[i].direct);
  6133. if (err < 0)
  6134. return err;
  6135. }
  6136. err = ca0132_alt_add_effect_slider(codec, XBASS_XOVER,
  6137. "X-Bass Crossover", EFX_DIR_OUT);
  6138. if (err < 0)
  6139. return err;
  6140. } else {
  6141. err = add_fx_switch(codec, PLAY_ENHANCEMENT,
  6142. "PlayEnhancement", 0);
  6143. if (err < 0)
  6144. return err;
  6145. err = add_fx_switch(codec, CRYSTAL_VOICE,
  6146. "CrystalVoice", 1);
  6147. if (err < 0)
  6148. return err;
  6149. }
  6150. err = add_voicefx(codec);
  6151. if (err < 0)
  6152. return err;
  6153. /*
  6154. * If the codec uses alt_functions, you need the enumerated controls
  6155. * to select the new outputs and inputs, plus add the new mic boost
  6156. * setting control.
  6157. */
  6158. if (ca0132_use_alt_functions(spec)) {
  6159. err = ca0132_alt_add_output_enum(codec);
  6160. if (err < 0)
  6161. return err;
  6162. err = ca0132_alt_add_speaker_channel_cfg_enum(codec);
  6163. if (err < 0)
  6164. return err;
  6165. err = ca0132_alt_add_front_full_range_switch(codec);
  6166. if (err < 0)
  6167. return err;
  6168. err = ca0132_alt_add_rear_full_range_switch(codec);
  6169. if (err < 0)
  6170. return err;
  6171. err = ca0132_alt_add_bass_redirection_crossover(codec);
  6172. if (err < 0)
  6173. return err;
  6174. err = ca0132_alt_add_bass_redirection_switch(codec);
  6175. if (err < 0)
  6176. return err;
  6177. err = ca0132_alt_add_mic_boost_enum(codec);
  6178. if (err < 0)
  6179. return err;
  6180. /*
  6181. * ZxR only has microphone input, there is no front panel
  6182. * header on the card, and aux-in is handled by the DBPro board.
  6183. */
  6184. if (ca0132_quirk(spec) != QUIRK_ZXR) {
  6185. err = ca0132_alt_add_input_enum(codec);
  6186. if (err < 0)
  6187. return err;
  6188. }
  6189. }
  6190. switch (ca0132_quirk(spec)) {
  6191. case QUIRK_AE5:
  6192. case QUIRK_AE7:
  6193. err = ae5_add_headphone_gain_enum(codec);
  6194. if (err < 0)
  6195. return err;
  6196. err = ae5_add_sound_filter_enum(codec);
  6197. if (err < 0)
  6198. return err;
  6199. break;
  6200. case QUIRK_ZXR:
  6201. err = zxr_add_headphone_gain_switch(codec);
  6202. if (err < 0)
  6203. return err;
  6204. break;
  6205. default:
  6206. break;
  6207. }
  6208. #ifdef ENABLE_TUNING_CONTROLS
  6209. add_tuning_ctls(codec);
  6210. #endif
  6211. err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
  6212. if (err < 0)
  6213. return err;
  6214. if (spec->dig_out) {
  6215. err = snd_hda_create_spdif_out_ctls(codec, spec->dig_out,
  6216. spec->dig_out);
  6217. if (err < 0)
  6218. return err;
  6219. err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
  6220. if (err < 0)
  6221. return err;
  6222. /* spec->multiout.share_spdif = 1; */
  6223. }
  6224. if (spec->dig_in) {
  6225. err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
  6226. if (err < 0)
  6227. return err;
  6228. }
  6229. if (ca0132_use_alt_functions(spec))
  6230. ca0132_alt_add_chmap_ctls(codec);
  6231. return 0;
  6232. }
  6233. static int dbpro_build_controls(struct hda_codec *codec)
  6234. {
  6235. struct ca0132_spec *spec = codec->spec;
  6236. int err = 0;
  6237. if (spec->dig_out) {
  6238. err = snd_hda_create_spdif_out_ctls(codec, spec->dig_out,
  6239. spec->dig_out);
  6240. if (err < 0)
  6241. return err;
  6242. }
  6243. if (spec->dig_in) {
  6244. err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
  6245. if (err < 0)
  6246. return err;
  6247. }
  6248. return 0;
  6249. }
  6250. /*
  6251. * PCM
  6252. */
  6253. static const struct hda_pcm_stream ca0132_pcm_analog_playback = {
  6254. .substreams = 1,
  6255. .channels_min = 2,
  6256. .channels_max = 6,
  6257. .ops = {
  6258. .prepare = ca0132_playback_pcm_prepare,
  6259. .cleanup = ca0132_playback_pcm_cleanup,
  6260. .get_delay = ca0132_playback_pcm_delay,
  6261. },
  6262. };
  6263. static const struct hda_pcm_stream ca0132_pcm_analog_capture = {
  6264. .substreams = 1,
  6265. .channels_min = 2,
  6266. .channels_max = 2,
  6267. .ops = {
  6268. .prepare = ca0132_capture_pcm_prepare,
  6269. .cleanup = ca0132_capture_pcm_cleanup,
  6270. .get_delay = ca0132_capture_pcm_delay,
  6271. },
  6272. };
  6273. static const struct hda_pcm_stream ca0132_pcm_digital_playback = {
  6274. .substreams = 1,
  6275. .channels_min = 2,
  6276. .channels_max = 2,
  6277. .ops = {
  6278. .open = ca0132_dig_playback_pcm_open,
  6279. .close = ca0132_dig_playback_pcm_close,
  6280. .prepare = ca0132_dig_playback_pcm_prepare,
  6281. .cleanup = ca0132_dig_playback_pcm_cleanup
  6282. },
  6283. };
  6284. static const struct hda_pcm_stream ca0132_pcm_digital_capture = {
  6285. .substreams = 1,
  6286. .channels_min = 2,
  6287. .channels_max = 2,
  6288. };
  6289. static int ca0132_build_pcms(struct hda_codec *codec)
  6290. {
  6291. struct ca0132_spec *spec = codec->spec;
  6292. struct hda_pcm *info;
  6293. info = snd_hda_codec_pcm_new(codec, "CA0132 Analog");
  6294. if (!info)
  6295. return -ENOMEM;
  6296. if (ca0132_use_alt_functions(spec)) {
  6297. info->own_chmap = true;
  6298. info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap
  6299. = ca0132_alt_chmaps;
  6300. }
  6301. info->stream[SNDRV_PCM_STREAM_PLAYBACK] = ca0132_pcm_analog_playback;
  6302. info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dacs[0];
  6303. info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
  6304. spec->multiout.max_channels;
  6305. info->stream[SNDRV_PCM_STREAM_CAPTURE] = ca0132_pcm_analog_capture;
  6306. info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = 1;
  6307. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[0];
  6308. /* With the DSP enabled, desktops don't use this ADC. */
  6309. if (!ca0132_use_alt_functions(spec)) {
  6310. info = snd_hda_codec_pcm_new(codec, "CA0132 Analog Mic-In2");
  6311. if (!info)
  6312. return -ENOMEM;
  6313. info->stream[SNDRV_PCM_STREAM_CAPTURE] =
  6314. ca0132_pcm_analog_capture;
  6315. info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = 1;
  6316. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[1];
  6317. }
  6318. info = snd_hda_codec_pcm_new(codec, "CA0132 What U Hear");
  6319. if (!info)
  6320. return -ENOMEM;
  6321. info->stream[SNDRV_PCM_STREAM_CAPTURE] = ca0132_pcm_analog_capture;
  6322. info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = 1;
  6323. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[2];
  6324. if (!spec->dig_out && !spec->dig_in)
  6325. return 0;
  6326. info = snd_hda_codec_pcm_new(codec, "CA0132 Digital");
  6327. if (!info)
  6328. return -ENOMEM;
  6329. info->pcm_type = HDA_PCM_TYPE_SPDIF;
  6330. if (spec->dig_out) {
  6331. info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
  6332. ca0132_pcm_digital_playback;
  6333. info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dig_out;
  6334. }
  6335. if (spec->dig_in) {
  6336. info->stream[SNDRV_PCM_STREAM_CAPTURE] =
  6337. ca0132_pcm_digital_capture;
  6338. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
  6339. }
  6340. return 0;
  6341. }
  6342. static int dbpro_build_pcms(struct hda_codec *codec)
  6343. {
  6344. struct ca0132_spec *spec = codec->spec;
  6345. struct hda_pcm *info;
  6346. info = snd_hda_codec_pcm_new(codec, "CA0132 Alt Analog");
  6347. if (!info)
  6348. return -ENOMEM;
  6349. info->stream[SNDRV_PCM_STREAM_CAPTURE] = ca0132_pcm_analog_capture;
  6350. info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = 1;
  6351. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[0];
  6352. if (!spec->dig_out && !spec->dig_in)
  6353. return 0;
  6354. info = snd_hda_codec_pcm_new(codec, "CA0132 Digital");
  6355. if (!info)
  6356. return -ENOMEM;
  6357. info->pcm_type = HDA_PCM_TYPE_SPDIF;
  6358. if (spec->dig_out) {
  6359. info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
  6360. ca0132_pcm_digital_playback;
  6361. info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dig_out;
  6362. }
  6363. if (spec->dig_in) {
  6364. info->stream[SNDRV_PCM_STREAM_CAPTURE] =
  6365. ca0132_pcm_digital_capture;
  6366. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
  6367. }
  6368. return 0;
  6369. }
  6370. static void init_output(struct hda_codec *codec, hda_nid_t pin, hda_nid_t dac)
  6371. {
  6372. if (pin) {
  6373. snd_hda_set_pin_ctl(codec, pin, PIN_HP);
  6374. if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
  6375. snd_hda_codec_write(codec, pin, 0,
  6376. AC_VERB_SET_AMP_GAIN_MUTE,
  6377. AMP_OUT_UNMUTE);
  6378. }
  6379. if (dac && (get_wcaps(codec, dac) & AC_WCAP_OUT_AMP))
  6380. snd_hda_codec_write(codec, dac, 0,
  6381. AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO);
  6382. }
  6383. static void init_input(struct hda_codec *codec, hda_nid_t pin, hda_nid_t adc)
  6384. {
  6385. if (pin) {
  6386. snd_hda_set_pin_ctl(codec, pin, PIN_VREF80);
  6387. if (get_wcaps(codec, pin) & AC_WCAP_IN_AMP)
  6388. snd_hda_codec_write(codec, pin, 0,
  6389. AC_VERB_SET_AMP_GAIN_MUTE,
  6390. AMP_IN_UNMUTE(0));
  6391. }
  6392. if (adc && (get_wcaps(codec, adc) & AC_WCAP_IN_AMP)) {
  6393. snd_hda_codec_write(codec, adc, 0, AC_VERB_SET_AMP_GAIN_MUTE,
  6394. AMP_IN_UNMUTE(0));
  6395. /* init to 0 dB and unmute. */
  6396. snd_hda_codec_amp_stereo(codec, adc, HDA_INPUT, 0,
  6397. HDA_AMP_VOLMASK, 0x5a);
  6398. snd_hda_codec_amp_stereo(codec, adc, HDA_INPUT, 0,
  6399. HDA_AMP_MUTE, 0);
  6400. }
  6401. }
  6402. static void refresh_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir)
  6403. {
  6404. unsigned int caps;
  6405. caps = snd_hda_param_read(codec, nid, dir == HDA_OUTPUT ?
  6406. AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
  6407. snd_hda_override_amp_caps(codec, nid, dir, caps);
  6408. }
  6409. /*
  6410. * Switch between Digital built-in mic and analog mic.
  6411. */
  6412. static void ca0132_set_dmic(struct hda_codec *codec, int enable)
  6413. {
  6414. struct ca0132_spec *spec = codec->spec;
  6415. unsigned int tmp;
  6416. u8 val;
  6417. unsigned int oldval;
  6418. codec_dbg(codec, "ca0132_set_dmic: enable=%d\n", enable);
  6419. oldval = stop_mic1(codec);
  6420. ca0132_set_vipsource(codec, 0);
  6421. if (enable) {
  6422. /* set DMic input as 2-ch */
  6423. tmp = FLOAT_TWO;
  6424. dspio_set_uint_param(codec, 0x80, 0x00, tmp);
  6425. val = spec->dmic_ctl;
  6426. val |= 0x80;
  6427. snd_hda_codec_write(codec, spec->input_pins[0], 0,
  6428. VENDOR_CHIPIO_DMIC_CTL_SET, val);
  6429. if (!(spec->dmic_ctl & 0x20))
  6430. chipio_set_control_flag(codec, CONTROL_FLAG_DMIC, 1);
  6431. } else {
  6432. /* set AMic input as mono */
  6433. tmp = FLOAT_ONE;
  6434. dspio_set_uint_param(codec, 0x80, 0x00, tmp);
  6435. val = spec->dmic_ctl;
  6436. /* clear bit7 and bit5 to disable dmic */
  6437. val &= 0x5f;
  6438. snd_hda_codec_write(codec, spec->input_pins[0], 0,
  6439. VENDOR_CHIPIO_DMIC_CTL_SET, val);
  6440. if (!(spec->dmic_ctl & 0x20))
  6441. chipio_set_control_flag(codec, CONTROL_FLAG_DMIC, 0);
  6442. }
  6443. ca0132_set_vipsource(codec, 1);
  6444. resume_mic1(codec, oldval);
  6445. }
  6446. /*
  6447. * Initialization for Digital Mic.
  6448. */
  6449. static void ca0132_init_dmic(struct hda_codec *codec)
  6450. {
  6451. struct ca0132_spec *spec = codec->spec;
  6452. u8 val;
  6453. /* Setup Digital Mic here, but don't enable.
  6454. * Enable based on jack detect.
  6455. */
  6456. /* MCLK uses MPIO1, set to enable.
  6457. * Bit 2-0: MPIO select
  6458. * Bit 3: set to disable
  6459. * Bit 7-4: reserved
  6460. */
  6461. val = 0x01;
  6462. snd_hda_codec_write(codec, spec->input_pins[0], 0,
  6463. VENDOR_CHIPIO_DMIC_MCLK_SET, val);
  6464. /* Data1 uses MPIO3. Data2 not use
  6465. * Bit 2-0: Data1 MPIO select
  6466. * Bit 3: set disable Data1
  6467. * Bit 6-4: Data2 MPIO select
  6468. * Bit 7: set disable Data2
  6469. */
  6470. val = 0x83;
  6471. snd_hda_codec_write(codec, spec->input_pins[0], 0,
  6472. VENDOR_CHIPIO_DMIC_PIN_SET, val);
  6473. /* Use Ch-0 and Ch-1. Rate is 48K, mode 1. Disable DMic first.
  6474. * Bit 3-0: Channel mask
  6475. * Bit 4: set for 48KHz, clear for 32KHz
  6476. * Bit 5: mode
  6477. * Bit 6: set to select Data2, clear for Data1
  6478. * Bit 7: set to enable DMic, clear for AMic
  6479. */
  6480. if (ca0132_quirk(spec) == QUIRK_ALIENWARE_M17XR4)
  6481. val = 0x33;
  6482. else
  6483. val = 0x23;
  6484. /* keep a copy of dmic ctl val for enable/disable dmic purpuse */
  6485. spec->dmic_ctl = val;
  6486. snd_hda_codec_write(codec, spec->input_pins[0], 0,
  6487. VENDOR_CHIPIO_DMIC_CTL_SET, val);
  6488. }
  6489. /*
  6490. * Initialization for Analog Mic 2
  6491. */
  6492. static void ca0132_init_analog_mic2(struct hda_codec *codec)
  6493. {
  6494. struct ca0132_spec *spec = codec->spec;
  6495. mutex_lock(&spec->chipio_mutex);
  6496. chipio_8051_write_exram_no_mutex(codec, 0x1920, 0x00);
  6497. chipio_8051_write_exram_no_mutex(codec, 0x192d, 0x00);
  6498. mutex_unlock(&spec->chipio_mutex);
  6499. }
  6500. static void ca0132_refresh_widget_caps(struct hda_codec *codec)
  6501. {
  6502. struct ca0132_spec *spec = codec->spec;
  6503. int i;
  6504. codec_dbg(codec, "ca0132_refresh_widget_caps.\n");
  6505. snd_hda_codec_update_widgets(codec);
  6506. for (i = 0; i < spec->multiout.num_dacs; i++)
  6507. refresh_amp_caps(codec, spec->dacs[i], HDA_OUTPUT);
  6508. for (i = 0; i < spec->num_outputs; i++)
  6509. refresh_amp_caps(codec, spec->out_pins[i], HDA_OUTPUT);
  6510. for (i = 0; i < spec->num_inputs; i++) {
  6511. refresh_amp_caps(codec, spec->adcs[i], HDA_INPUT);
  6512. refresh_amp_caps(codec, spec->input_pins[i], HDA_INPUT);
  6513. }
  6514. }
  6515. /* If there is an active channel for some reason, find it and free it. */
  6516. static void ca0132_alt_free_active_dma_channels(struct hda_codec *codec)
  6517. {
  6518. unsigned int i, tmp;
  6519. int status;
  6520. /* Read active DSPDMAC channel register. */
  6521. status = chipio_read(codec, DSPDMAC_CHNLSTART_MODULE_OFFSET, &tmp);
  6522. if (status >= 0) {
  6523. /* AND against 0xfff to get the active channel bits. */
  6524. tmp = tmp & 0xfff;
  6525. /* If there are no active channels, nothing to free. */
  6526. if (!tmp)
  6527. return;
  6528. } else {
  6529. codec_dbg(codec, "%s: Failed to read active DSP DMA channel register.\n",
  6530. __func__);
  6531. return;
  6532. }
  6533. /*
  6534. * Check each DSP DMA channel for activity, and if the channel is
  6535. * active, free it.
  6536. */
  6537. for (i = 0; i < DSPDMAC_DMA_CFG_CHANNEL_COUNT; i++) {
  6538. if (dsp_is_dma_active(codec, i)) {
  6539. status = dspio_free_dma_chan(codec, i);
  6540. if (status < 0)
  6541. codec_dbg(codec, "%s: Failed to free active DSP DMA channel %d.\n",
  6542. __func__, i);
  6543. }
  6544. }
  6545. }
  6546. /*
  6547. * In the case of CT_EXTENSIONS_ENABLE being set to 1, and the DSP being in
  6548. * use, audio is no longer routed directly to the DAC/ADC from the HDA stream.
  6549. * Instead, audio is now routed through the DSP's DMA controllers, which
  6550. * the DSP is tasked with setting up itself. Through debugging, it seems the
  6551. * cause of most of the no-audio on startup issues were due to improperly
  6552. * configured DSP DMA channels.
  6553. *
  6554. * Normally, the DSP configures these the first time an HDA audio stream is
  6555. * started post DSP firmware download. That is why creating a 'dummy' stream
  6556. * worked in fixing the audio in some cases. This works most of the time, but
  6557. * sometimes if a stream is started/stopped before the DSP can setup the DMA
  6558. * configuration registers, it ends up in a broken state. Issues can also
  6559. * arise if streams are started in an unusual order, i.e the audio output dma
  6560. * channel being sandwiched between the mic1 and mic2 dma channels.
  6561. *
  6562. * The solution to this is to make sure that the DSP has no DMA channels
  6563. * in use post DSP firmware download, and then to manually start each default
  6564. * DSP stream that uses the DMA channels. These are 0x0c, the audio output
  6565. * stream, 0x03, analog mic 1, and 0x04, analog mic 2.
  6566. */
  6567. static void ca0132_alt_start_dsp_audio_streams(struct hda_codec *codec)
  6568. {
  6569. static const unsigned int dsp_dma_stream_ids[] = { 0x0c, 0x03, 0x04 };
  6570. struct ca0132_spec *spec = codec->spec;
  6571. unsigned int i, tmp;
  6572. /*
  6573. * Check if any of the default streams are active, and if they are,
  6574. * stop them.
  6575. */
  6576. mutex_lock(&spec->chipio_mutex);
  6577. for (i = 0; i < ARRAY_SIZE(dsp_dma_stream_ids); i++) {
  6578. chipio_get_stream_control(codec, dsp_dma_stream_ids[i], &tmp);
  6579. if (tmp) {
  6580. chipio_set_stream_control(codec,
  6581. dsp_dma_stream_ids[i], 0);
  6582. }
  6583. }
  6584. mutex_unlock(&spec->chipio_mutex);
  6585. /*
  6586. * If all DSP streams are inactive, there should be no active DSP DMA
  6587. * channels. Check and make sure this is the case, and if it isn't,
  6588. * free any active channels.
  6589. */
  6590. ca0132_alt_free_active_dma_channels(codec);
  6591. mutex_lock(&spec->chipio_mutex);
  6592. /* Make sure stream 0x0c is six channels. */
  6593. chipio_set_stream_channels(codec, 0x0c, 6);
  6594. for (i = 0; i < ARRAY_SIZE(dsp_dma_stream_ids); i++) {
  6595. chipio_set_stream_control(codec,
  6596. dsp_dma_stream_ids[i], 1);
  6597. /* Give the DSP some time to setup the DMA channel. */
  6598. msleep(75);
  6599. }
  6600. mutex_unlock(&spec->chipio_mutex);
  6601. }
  6602. /*
  6603. * The region of ChipIO memory from 0x190000-0x1903fc is a sort of 'audio
  6604. * router', where each entry represents a 48khz audio channel, with a format
  6605. * of an 8-bit destination, an 8-bit source, and an unknown 2-bit number
  6606. * value. The 2-bit number value is seemingly 0 if inactive, 1 if active,
  6607. * and 3 if it's using Sample Rate Converter ports.
  6608. * An example is:
  6609. * 0x0001f8c0
  6610. * In this case, f8 is the destination, and c0 is the source. The number value
  6611. * is 1.
  6612. * This region of memory is normally managed internally by the 8051, where
  6613. * the region of exram memory from 0x1477-0x1575 has each byte represent an
  6614. * entry within the 0x190000 range, and when a range of entries is in use, the
  6615. * ending value is overwritten with 0xff.
  6616. * 0x1578 in exram is a table of 0x25 entries, corresponding to the ChipIO
  6617. * streamID's, where each entry is a starting 0x190000 port offset.
  6618. * 0x159d in exram is the same as 0x1578, except it contains the ending port
  6619. * offset for the corresponding streamID.
  6620. *
  6621. * On certain cards, such as the SBZ/ZxR/AE7, these are originally setup by
  6622. * the 8051, then manually overwritten to remap the ports to work with the
  6623. * new DACs.
  6624. *
  6625. * Currently known portID's:
  6626. * 0x00-0x1f: HDA audio stream input/output ports.
  6627. * 0x80-0xbf: Sample rate converter input/outputs. Only valid ports seem to
  6628. * have the lower-nibble set to 0x1, 0x2, and 0x9.
  6629. * 0xc0-0xdf: DSP DMA input/output ports. Dynamically assigned.
  6630. * 0xe0-0xff: DAC/ADC audio input/output ports.
  6631. *
  6632. * Currently known streamID's:
  6633. * 0x03: Mic1 ADC to DSP.
  6634. * 0x04: Mic2 ADC to DSP.
  6635. * 0x05: HDA node 0x02 audio stream to DSP.
  6636. * 0x0f: DSP Mic exit to HDA node 0x07.
  6637. * 0x0c: DSP processed audio to DACs.
  6638. * 0x14: DAC0, front L/R.
  6639. *
  6640. * It is possible to route the HDA audio streams directly to the DAC and
  6641. * bypass the DSP entirely, with the only downside being that since the DSP
  6642. * does volume control, the only volume control you'll get is through PCM on
  6643. * the PC side, in the same way volume is handled for optical out. This may be
  6644. * useful for debugging.
  6645. */
  6646. static void chipio_remap_stream(struct hda_codec *codec,
  6647. const struct chipio_stream_remap_data *remap_data)
  6648. {
  6649. unsigned int i, stream_offset;
  6650. /* Get the starting port for the stream to be remapped. */
  6651. chipio_8051_read_exram(codec, 0x1578 + remap_data->stream_id,
  6652. &stream_offset);
  6653. /*
  6654. * Check if the stream's port value is 0xff, because the 8051 may not
  6655. * have gotten around to setting up the stream yet. Wait until it's
  6656. * setup to remap it's ports.
  6657. */
  6658. if (stream_offset == 0xff) {
  6659. for (i = 0; i < 5; i++) {
  6660. msleep(25);
  6661. chipio_8051_read_exram(codec, 0x1578 + remap_data->stream_id,
  6662. &stream_offset);
  6663. if (stream_offset != 0xff)
  6664. break;
  6665. }
  6666. }
  6667. if (stream_offset == 0xff) {
  6668. codec_info(codec, "%s: Stream 0x%02x ports aren't allocated, remap failed!\n",
  6669. __func__, remap_data->stream_id);
  6670. return;
  6671. }
  6672. /* Offset isn't in bytes, its in 32-bit words, so multiply it by 4. */
  6673. stream_offset *= 0x04;
  6674. stream_offset += 0x190000;
  6675. for (i = 0; i < remap_data->count; i++) {
  6676. chipio_write_no_mutex(codec,
  6677. stream_offset + remap_data->offset[i],
  6678. remap_data->value[i]);
  6679. }
  6680. /* Update stream map configuration. */
  6681. chipio_write_no_mutex(codec, 0x19042c, 0x00000001);
  6682. }
  6683. /*
  6684. * Default speaker tuning values setup for alternative codecs.
  6685. */
  6686. static const unsigned int sbz_default_delay_values[] = {
  6687. /* Non-zero values are floating point 0.000198. */
  6688. 0x394f9e38, 0x394f9e38, 0x00000000, 0x00000000, 0x00000000, 0x00000000
  6689. };
  6690. static const unsigned int zxr_default_delay_values[] = {
  6691. /* Non-zero values are floating point 0.000220. */
  6692. 0x00000000, 0x00000000, 0x3966afcd, 0x3966afcd, 0x3966afcd, 0x3966afcd
  6693. };
  6694. static const unsigned int ae5_default_delay_values[] = {
  6695. /* Non-zero values are floating point 0.000100. */
  6696. 0x00000000, 0x00000000, 0x38d1b717, 0x38d1b717, 0x38d1b717, 0x38d1b717
  6697. };
  6698. /*
  6699. * If we never change these, probably only need them on initialization.
  6700. */
  6701. static void ca0132_alt_init_speaker_tuning(struct hda_codec *codec)
  6702. {
  6703. struct ca0132_spec *spec = codec->spec;
  6704. unsigned int i, tmp, start_req, end_req;
  6705. const unsigned int *values;
  6706. switch (ca0132_quirk(spec)) {
  6707. case QUIRK_SBZ:
  6708. values = sbz_default_delay_values;
  6709. break;
  6710. case QUIRK_ZXR:
  6711. values = zxr_default_delay_values;
  6712. break;
  6713. case QUIRK_AE5:
  6714. case QUIRK_AE7:
  6715. values = ae5_default_delay_values;
  6716. break;
  6717. default:
  6718. values = sbz_default_delay_values;
  6719. break;
  6720. }
  6721. tmp = FLOAT_ZERO;
  6722. dspio_set_uint_param(codec, 0x96, SPEAKER_TUNING_ENABLE_CENTER_EQ, tmp);
  6723. start_req = SPEAKER_TUNING_FRONT_LEFT_VOL_LEVEL;
  6724. end_req = SPEAKER_TUNING_REAR_RIGHT_VOL_LEVEL;
  6725. for (i = start_req; i < end_req + 1; i++)
  6726. dspio_set_uint_param(codec, 0x96, i, tmp);
  6727. start_req = SPEAKER_TUNING_FRONT_LEFT_INVERT;
  6728. end_req = SPEAKER_TUNING_REAR_RIGHT_INVERT;
  6729. for (i = start_req; i < end_req + 1; i++)
  6730. dspio_set_uint_param(codec, 0x96, i, tmp);
  6731. for (i = 0; i < 6; i++)
  6732. dspio_set_uint_param(codec, 0x96,
  6733. SPEAKER_TUNING_FRONT_LEFT_DELAY + i, values[i]);
  6734. }
  6735. /*
  6736. * Initialize mic for non-chromebook ca0132 implementations.
  6737. */
  6738. static void ca0132_alt_init_analog_mics(struct hda_codec *codec)
  6739. {
  6740. struct ca0132_spec *spec = codec->spec;
  6741. unsigned int tmp;
  6742. /* Mic 1 Setup */
  6743. chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
  6744. chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
  6745. if (ca0132_quirk(spec) == QUIRK_R3DI) {
  6746. chipio_set_conn_rate(codec, 0x0F, SR_96_000);
  6747. tmp = FLOAT_ONE;
  6748. } else
  6749. tmp = FLOAT_THREE;
  6750. dspio_set_uint_param(codec, 0x80, 0x00, tmp);
  6751. /* Mic 2 setup (not present on desktop cards) */
  6752. chipio_set_conn_rate(codec, MEM_CONNID_MICIN2, SR_96_000);
  6753. chipio_set_conn_rate(codec, MEM_CONNID_MICOUT2, SR_96_000);
  6754. if (ca0132_quirk(spec) == QUIRK_R3DI)
  6755. chipio_set_conn_rate(codec, 0x0F, SR_96_000);
  6756. tmp = FLOAT_ZERO;
  6757. dspio_set_uint_param(codec, 0x80, 0x01, tmp);
  6758. }
  6759. /*
  6760. * Sets the source of stream 0x14 to connpointID 0x48, and the destination
  6761. * connpointID to 0x91. If this isn't done, the destination is 0x71, and
  6762. * you get no sound. I'm guessing this has to do with the Sound Blaster Z
  6763. * having an updated DAC, which changes the destination to that DAC.
  6764. */
  6765. static void sbz_connect_streams(struct hda_codec *codec)
  6766. {
  6767. struct ca0132_spec *spec = codec->spec;
  6768. mutex_lock(&spec->chipio_mutex);
  6769. codec_dbg(codec, "Connect Streams entered, mutex locked and loaded.\n");
  6770. /* This value is 0x43 for 96khz, and 0x83 for 192khz. */
  6771. chipio_write_no_mutex(codec, 0x18a020, 0x00000043);
  6772. /* Setup stream 0x14 with it's source and destination points */
  6773. chipio_set_stream_source_dest(codec, 0x14, 0x48, 0x91);
  6774. chipio_set_conn_rate_no_mutex(codec, 0x48, SR_96_000);
  6775. chipio_set_conn_rate_no_mutex(codec, 0x91, SR_96_000);
  6776. chipio_set_stream_channels(codec, 0x14, 2);
  6777. chipio_set_stream_control(codec, 0x14, 1);
  6778. codec_dbg(codec, "Connect Streams exited, mutex released.\n");
  6779. mutex_unlock(&spec->chipio_mutex);
  6780. }
  6781. /*
  6782. * Write data through ChipIO to setup proper stream destinations.
  6783. * Not sure how it exactly works, but it seems to direct data
  6784. * to different destinations. Example is f8 to c0, e0 to c0.
  6785. * All I know is, if you don't set these, you get no sound.
  6786. */
  6787. static void sbz_chipio_startup_data(struct hda_codec *codec)
  6788. {
  6789. const struct chipio_stream_remap_data *dsp_out_remap_data;
  6790. struct ca0132_spec *spec = codec->spec;
  6791. mutex_lock(&spec->chipio_mutex);
  6792. codec_dbg(codec, "Startup Data entered, mutex locked and loaded.\n");
  6793. /* Remap DAC0's output ports. */
  6794. chipio_remap_stream(codec, &stream_remap_data[0]);
  6795. /* Remap DSP audio output stream ports. */
  6796. switch (ca0132_quirk(spec)) {
  6797. case QUIRK_SBZ:
  6798. dsp_out_remap_data = &stream_remap_data[1];
  6799. break;
  6800. case QUIRK_ZXR:
  6801. dsp_out_remap_data = &stream_remap_data[2];
  6802. break;
  6803. default:
  6804. dsp_out_remap_data = NULL;
  6805. break;
  6806. }
  6807. if (dsp_out_remap_data)
  6808. chipio_remap_stream(codec, dsp_out_remap_data);
  6809. codec_dbg(codec, "Startup Data exited, mutex released.\n");
  6810. mutex_unlock(&spec->chipio_mutex);
  6811. }
  6812. static void ca0132_alt_dsp_initial_mic_setup(struct hda_codec *codec)
  6813. {
  6814. struct ca0132_spec *spec = codec->spec;
  6815. unsigned int tmp;
  6816. chipio_set_stream_control(codec, 0x03, 0);
  6817. chipio_set_stream_control(codec, 0x04, 0);
  6818. chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
  6819. chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
  6820. tmp = FLOAT_THREE;
  6821. dspio_set_uint_param(codec, 0x80, 0x00, tmp);
  6822. chipio_set_stream_control(codec, 0x03, 1);
  6823. chipio_set_stream_control(codec, 0x04, 1);
  6824. switch (ca0132_quirk(spec)) {
  6825. case QUIRK_SBZ:
  6826. chipio_write(codec, 0x18b098, 0x0000000c);
  6827. chipio_write(codec, 0x18b09C, 0x0000000c);
  6828. break;
  6829. case QUIRK_AE5:
  6830. chipio_write(codec, 0x18b098, 0x0000000c);
  6831. chipio_write(codec, 0x18b09c, 0x0000004c);
  6832. break;
  6833. default:
  6834. break;
  6835. }
  6836. }
  6837. static void ae5_post_dsp_register_set(struct hda_codec *codec)
  6838. {
  6839. struct ca0132_spec *spec = codec->spec;
  6840. chipio_8051_write_direct(codec, 0x93, 0x10);
  6841. chipio_8051_write_pll_pmu(codec, 0x44, 0xc2);
  6842. writeb(0xff, spec->mem_base + 0x304);
  6843. writeb(0xff, spec->mem_base + 0x304);
  6844. writeb(0xff, spec->mem_base + 0x304);
  6845. writeb(0xff, spec->mem_base + 0x304);
  6846. writeb(0x00, spec->mem_base + 0x100);
  6847. writeb(0xff, spec->mem_base + 0x304);
  6848. writeb(0x00, spec->mem_base + 0x100);
  6849. writeb(0xff, spec->mem_base + 0x304);
  6850. writeb(0x00, spec->mem_base + 0x100);
  6851. writeb(0xff, spec->mem_base + 0x304);
  6852. writeb(0x00, spec->mem_base + 0x100);
  6853. writeb(0xff, spec->mem_base + 0x304);
  6854. ca0113_mmio_command_set(codec, 0x30, 0x2b, 0x3f);
  6855. ca0113_mmio_command_set(codec, 0x30, 0x2d, 0x3f);
  6856. ca0113_mmio_command_set(codec, 0x48, 0x07, 0x83);
  6857. }
  6858. static void ae5_post_dsp_param_setup(struct hda_codec *codec)
  6859. {
  6860. /*
  6861. * Param3 in the 8051's memory is represented by the ascii string 'mch'
  6862. * which seems to be 'multichannel'. This is also mentioned in the
  6863. * AE-5's registry values in Windows.
  6864. */
  6865. chipio_set_control_param(codec, 3, 0);
  6866. /*
  6867. * I believe ASI is 'audio serial interface' and that it's used to
  6868. * change colors on the external LED strip connected to the AE-5.
  6869. */
  6870. chipio_set_control_flag(codec, CONTROL_FLAG_ASI_96KHZ, 1);
  6871. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0, 0x724, 0x83);
  6872. chipio_set_control_param(codec, CONTROL_PARAM_ASI, 0);
  6873. chipio_8051_write_exram(codec, 0xfa92, 0x22);
  6874. }
  6875. static void ae5_post_dsp_pll_setup(struct hda_codec *codec)
  6876. {
  6877. chipio_8051_write_pll_pmu(codec, 0x41, 0xc8);
  6878. chipio_8051_write_pll_pmu(codec, 0x45, 0xcc);
  6879. chipio_8051_write_pll_pmu(codec, 0x40, 0xcb);
  6880. chipio_8051_write_pll_pmu(codec, 0x43, 0xc7);
  6881. chipio_8051_write_pll_pmu(codec, 0x51, 0x8d);
  6882. }
  6883. static void ae5_post_dsp_stream_setup(struct hda_codec *codec)
  6884. {
  6885. struct ca0132_spec *spec = codec->spec;
  6886. mutex_lock(&spec->chipio_mutex);
  6887. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0, 0x725, 0x81);
  6888. chipio_set_conn_rate_no_mutex(codec, 0x70, SR_96_000);
  6889. chipio_set_stream_source_dest(codec, 0x5, 0x43, 0x0);
  6890. chipio_set_stream_source_dest(codec, 0x18, 0x9, 0xd0);
  6891. chipio_set_conn_rate_no_mutex(codec, 0xd0, SR_96_000);
  6892. chipio_set_stream_channels(codec, 0x18, 6);
  6893. chipio_set_stream_control(codec, 0x18, 1);
  6894. chipio_set_control_param_no_mutex(codec, CONTROL_PARAM_ASI, 4);
  6895. chipio_8051_write_pll_pmu_no_mutex(codec, 0x43, 0xc7);
  6896. ca0113_mmio_command_set(codec, 0x48, 0x01, 0x80);
  6897. mutex_unlock(&spec->chipio_mutex);
  6898. }
  6899. static void ae5_post_dsp_startup_data(struct hda_codec *codec)
  6900. {
  6901. struct ca0132_spec *spec = codec->spec;
  6902. mutex_lock(&spec->chipio_mutex);
  6903. chipio_write_no_mutex(codec, 0x189000, 0x0001f101);
  6904. chipio_write_no_mutex(codec, 0x189004, 0x0001f101);
  6905. chipio_write_no_mutex(codec, 0x189024, 0x00014004);
  6906. chipio_write_no_mutex(codec, 0x189028, 0x0002000f);
  6907. ca0113_mmio_command_set(codec, 0x48, 0x0a, 0x05);
  6908. chipio_set_control_param_no_mutex(codec, CONTROL_PARAM_ASI, 7);
  6909. ca0113_mmio_command_set(codec, 0x48, 0x0b, 0x12);
  6910. ca0113_mmio_command_set(codec, 0x48, 0x04, 0x00);
  6911. ca0113_mmio_command_set(codec, 0x48, 0x06, 0x48);
  6912. ca0113_mmio_command_set(codec, 0x48, 0x0a, 0x05);
  6913. ca0113_mmio_command_set(codec, 0x48, 0x07, 0x83);
  6914. ca0113_mmio_command_set(codec, 0x48, 0x0f, 0x00);
  6915. ca0113_mmio_command_set(codec, 0x48, 0x10, 0x00);
  6916. ca0113_mmio_gpio_set(codec, 0, true);
  6917. ca0113_mmio_gpio_set(codec, 1, true);
  6918. ca0113_mmio_command_set(codec, 0x48, 0x07, 0x80);
  6919. chipio_write_no_mutex(codec, 0x18b03c, 0x00000012);
  6920. ca0113_mmio_command_set(codec, 0x48, 0x0f, 0x00);
  6921. ca0113_mmio_command_set(codec, 0x48, 0x10, 0x00);
  6922. mutex_unlock(&spec->chipio_mutex);
  6923. }
  6924. static void ae7_post_dsp_setup_ports(struct hda_codec *codec)
  6925. {
  6926. struct ca0132_spec *spec = codec->spec;
  6927. mutex_lock(&spec->chipio_mutex);
  6928. /* Seems to share the same port remapping as the SBZ. */
  6929. chipio_remap_stream(codec, &stream_remap_data[1]);
  6930. ca0113_mmio_command_set(codec, 0x30, 0x30, 0x00);
  6931. ca0113_mmio_command_set(codec, 0x48, 0x0d, 0x40);
  6932. ca0113_mmio_command_set(codec, 0x48, 0x17, 0x00);
  6933. ca0113_mmio_command_set(codec, 0x48, 0x19, 0x00);
  6934. ca0113_mmio_command_set(codec, 0x48, 0x11, 0xff);
  6935. ca0113_mmio_command_set(codec, 0x48, 0x12, 0xff);
  6936. ca0113_mmio_command_set(codec, 0x48, 0x13, 0xff);
  6937. ca0113_mmio_command_set(codec, 0x48, 0x14, 0x7f);
  6938. mutex_unlock(&spec->chipio_mutex);
  6939. }
  6940. static void ae7_post_dsp_asi_stream_setup(struct hda_codec *codec)
  6941. {
  6942. struct ca0132_spec *spec = codec->spec;
  6943. mutex_lock(&spec->chipio_mutex);
  6944. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0, 0x725, 0x81);
  6945. ca0113_mmio_command_set(codec, 0x30, 0x2b, 0x00);
  6946. chipio_set_conn_rate_no_mutex(codec, 0x70, SR_96_000);
  6947. chipio_set_stream_source_dest(codec, 0x05, 0x43, 0x00);
  6948. chipio_set_stream_source_dest(codec, 0x18, 0x09, 0xd0);
  6949. chipio_set_conn_rate_no_mutex(codec, 0xd0, SR_96_000);
  6950. chipio_set_stream_channels(codec, 0x18, 6);
  6951. chipio_set_stream_control(codec, 0x18, 1);
  6952. chipio_set_control_param_no_mutex(codec, CONTROL_PARAM_ASI, 4);
  6953. mutex_unlock(&spec->chipio_mutex);
  6954. }
  6955. static void ae7_post_dsp_pll_setup(struct hda_codec *codec)
  6956. {
  6957. static const unsigned int addr[] = {
  6958. 0x41, 0x45, 0x40, 0x43, 0x51
  6959. };
  6960. static const unsigned int data[] = {
  6961. 0xc8, 0xcc, 0xcb, 0xc7, 0x8d
  6962. };
  6963. unsigned int i;
  6964. for (i = 0; i < ARRAY_SIZE(addr); i++)
  6965. chipio_8051_write_pll_pmu_no_mutex(codec, addr[i], data[i]);
  6966. }
  6967. static void ae7_post_dsp_asi_setup_ports(struct hda_codec *codec)
  6968. {
  6969. struct ca0132_spec *spec = codec->spec;
  6970. static const unsigned int target[] = {
  6971. 0x0b, 0x04, 0x06, 0x0a, 0x0c, 0x11, 0x12, 0x13, 0x14
  6972. };
  6973. static const unsigned int data[] = {
  6974. 0x12, 0x00, 0x48, 0x05, 0x5f, 0xff, 0xff, 0xff, 0x7f
  6975. };
  6976. unsigned int i;
  6977. mutex_lock(&spec->chipio_mutex);
  6978. chipio_8051_write_pll_pmu_no_mutex(codec, 0x43, 0xc7);
  6979. chipio_write_no_mutex(codec, 0x189000, 0x0001f101);
  6980. chipio_write_no_mutex(codec, 0x189004, 0x0001f101);
  6981. chipio_write_no_mutex(codec, 0x189024, 0x00014004);
  6982. chipio_write_no_mutex(codec, 0x189028, 0x0002000f);
  6983. ae7_post_dsp_pll_setup(codec);
  6984. chipio_set_control_param_no_mutex(codec, CONTROL_PARAM_ASI, 7);
  6985. for (i = 0; i < ARRAY_SIZE(target); i++)
  6986. ca0113_mmio_command_set(codec, 0x48, target[i], data[i]);
  6987. ca0113_mmio_command_set_type2(codec, 0x48, 0x07, 0x83);
  6988. ca0113_mmio_command_set(codec, 0x48, 0x0f, 0x00);
  6989. ca0113_mmio_command_set(codec, 0x48, 0x10, 0x00);
  6990. chipio_set_stream_source_dest(codec, 0x21, 0x64, 0x56);
  6991. chipio_set_stream_channels(codec, 0x21, 2);
  6992. chipio_set_conn_rate_no_mutex(codec, 0x56, SR_8_000);
  6993. chipio_set_control_param_no_mutex(codec, CONTROL_PARAM_NODE_ID, 0x09);
  6994. /*
  6995. * In the 8051's memory, this param is referred to as 'n2sid', which I
  6996. * believe is 'node to streamID'. It seems to be a way to assign a
  6997. * stream to a given HDA node.
  6998. */
  6999. chipio_set_control_param_no_mutex(codec, 0x20, 0x21);
  7000. chipio_write_no_mutex(codec, 0x18b038, 0x00000088);
  7001. /*
  7002. * Now, at this point on Windows, an actual stream is setup and
  7003. * seemingly sends data to the HDA node 0x09, which is the digital
  7004. * audio input node. This is left out here, because obviously I don't
  7005. * know what data is being sent. Interestingly, the AE-5 seems to go
  7006. * through the motions of getting here and never actually takes this
  7007. * step, but the AE-7 does.
  7008. */
  7009. ca0113_mmio_gpio_set(codec, 0, 1);
  7010. ca0113_mmio_gpio_set(codec, 1, 1);
  7011. ca0113_mmio_command_set_type2(codec, 0x48, 0x07, 0x83);
  7012. chipio_write_no_mutex(codec, 0x18b03c, 0x00000000);
  7013. ca0113_mmio_command_set(codec, 0x48, 0x0f, 0x00);
  7014. ca0113_mmio_command_set(codec, 0x48, 0x10, 0x00);
  7015. chipio_set_stream_source_dest(codec, 0x05, 0x43, 0x00);
  7016. chipio_set_stream_source_dest(codec, 0x18, 0x09, 0xd0);
  7017. chipio_set_conn_rate_no_mutex(codec, 0xd0, SR_96_000);
  7018. chipio_set_stream_channels(codec, 0x18, 6);
  7019. /*
  7020. * Runs again, this has been repeated a few times, but I'm just
  7021. * following what the Windows driver does.
  7022. */
  7023. ae7_post_dsp_pll_setup(codec);
  7024. chipio_set_control_param_no_mutex(codec, CONTROL_PARAM_ASI, 7);
  7025. mutex_unlock(&spec->chipio_mutex);
  7026. }
  7027. /*
  7028. * The Windows driver has commands that seem to setup ASI, which I believe to
  7029. * be some sort of audio serial interface. My current speculation is that it's
  7030. * related to communicating with the new DAC.
  7031. */
  7032. static void ae7_post_dsp_asi_setup(struct hda_codec *codec)
  7033. {
  7034. chipio_8051_write_direct(codec, 0x93, 0x10);
  7035. chipio_8051_write_pll_pmu(codec, 0x44, 0xc2);
  7036. ca0113_mmio_command_set_type2(codec, 0x48, 0x07, 0x83);
  7037. ca0113_mmio_command_set(codec, 0x30, 0x2e, 0x3f);
  7038. chipio_set_control_param(codec, 3, 3);
  7039. chipio_set_control_flag(codec, CONTROL_FLAG_ASI_96KHZ, 1);
  7040. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0, 0x724, 0x83);
  7041. chipio_set_control_param(codec, CONTROL_PARAM_ASI, 0);
  7042. snd_hda_codec_write(codec, 0x17, 0, 0x794, 0x00);
  7043. chipio_8051_write_exram(codec, 0xfa92, 0x22);
  7044. ae7_post_dsp_pll_setup(codec);
  7045. ae7_post_dsp_asi_stream_setup(codec);
  7046. chipio_8051_write_pll_pmu(codec, 0x43, 0xc7);
  7047. ae7_post_dsp_asi_setup_ports(codec);
  7048. }
  7049. /*
  7050. * Setup default parameters for DSP
  7051. */
  7052. static void ca0132_setup_defaults(struct hda_codec *codec)
  7053. {
  7054. struct ca0132_spec *spec = codec->spec;
  7055. unsigned int tmp;
  7056. int num_fx;
  7057. int idx, i;
  7058. if (spec->dsp_state != DSP_DOWNLOADED)
  7059. return;
  7060. /* out, in effects + voicefx */
  7061. num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT + 1;
  7062. for (idx = 0; idx < num_fx; idx++) {
  7063. for (i = 0; i <= ca0132_effects[idx].params; i++) {
  7064. dspio_set_uint_param(codec, ca0132_effects[idx].mid,
  7065. ca0132_effects[idx].reqs[i],
  7066. ca0132_effects[idx].def_vals[i]);
  7067. }
  7068. }
  7069. /*remove DSP headroom*/
  7070. tmp = FLOAT_ZERO;
  7071. dspio_set_uint_param(codec, 0x96, 0x3C, tmp);
  7072. /*set speaker EQ bypass attenuation*/
  7073. dspio_set_uint_param(codec, 0x8f, 0x01, tmp);
  7074. /* set AMic1 and AMic2 as mono mic */
  7075. tmp = FLOAT_ONE;
  7076. dspio_set_uint_param(codec, 0x80, 0x00, tmp);
  7077. dspio_set_uint_param(codec, 0x80, 0x01, tmp);
  7078. /* set AMic1 as CrystalVoice input */
  7079. tmp = FLOAT_ONE;
  7080. dspio_set_uint_param(codec, 0x80, 0x05, tmp);
  7081. /* set WUH source */
  7082. tmp = FLOAT_TWO;
  7083. dspio_set_uint_param(codec, 0x31, 0x00, tmp);
  7084. }
  7085. /*
  7086. * Setup default parameters for Recon3D/Recon3Di DSP.
  7087. */
  7088. static void r3d_setup_defaults(struct hda_codec *codec)
  7089. {
  7090. struct ca0132_spec *spec = codec->spec;
  7091. unsigned int tmp;
  7092. int num_fx;
  7093. int idx, i;
  7094. if (spec->dsp_state != DSP_DOWNLOADED)
  7095. return;
  7096. ca0132_alt_init_analog_mics(codec);
  7097. ca0132_alt_start_dsp_audio_streams(codec);
  7098. /*remove DSP headroom*/
  7099. tmp = FLOAT_ZERO;
  7100. dspio_set_uint_param(codec, 0x96, 0x3C, tmp);
  7101. /* set WUH source */
  7102. tmp = FLOAT_TWO;
  7103. dspio_set_uint_param(codec, 0x31, 0x00, tmp);
  7104. chipio_set_conn_rate(codec, MEM_CONNID_WUH, SR_48_000);
  7105. /* Set speaker source? */
  7106. dspio_set_uint_param(codec, 0x32, 0x00, tmp);
  7107. if (ca0132_quirk(spec) == QUIRK_R3DI)
  7108. r3di_gpio_dsp_status_set(codec, R3DI_DSP_DOWNLOADED);
  7109. /* Disable mute on Center/LFE. */
  7110. if (ca0132_quirk(spec) == QUIRK_R3D) {
  7111. ca0113_mmio_gpio_set(codec, 2, false);
  7112. ca0113_mmio_gpio_set(codec, 4, true);
  7113. }
  7114. /* Setup effect defaults */
  7115. num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT + 1;
  7116. for (idx = 0; idx < num_fx; idx++) {
  7117. for (i = 0; i <= ca0132_effects[idx].params; i++) {
  7118. dspio_set_uint_param(codec,
  7119. ca0132_effects[idx].mid,
  7120. ca0132_effects[idx].reqs[i],
  7121. ca0132_effects[idx].def_vals[i]);
  7122. }
  7123. }
  7124. }
  7125. /*
  7126. * Setup default parameters for the Sound Blaster Z DSP. A lot more going on
  7127. * than the Chromebook setup.
  7128. */
  7129. static void sbz_setup_defaults(struct hda_codec *codec)
  7130. {
  7131. struct ca0132_spec *spec = codec->spec;
  7132. unsigned int tmp;
  7133. int num_fx;
  7134. int idx, i;
  7135. if (spec->dsp_state != DSP_DOWNLOADED)
  7136. return;
  7137. ca0132_alt_init_analog_mics(codec);
  7138. ca0132_alt_start_dsp_audio_streams(codec);
  7139. sbz_connect_streams(codec);
  7140. sbz_chipio_startup_data(codec);
  7141. /*
  7142. * Sets internal input loopback to off, used to have a switch to
  7143. * enable input loopback, but turned out to be way too buggy.
  7144. */
  7145. tmp = FLOAT_ONE;
  7146. dspio_set_uint_param(codec, 0x37, 0x08, tmp);
  7147. dspio_set_uint_param(codec, 0x37, 0x10, tmp);
  7148. /*remove DSP headroom*/
  7149. tmp = FLOAT_ZERO;
  7150. dspio_set_uint_param(codec, 0x96, 0x3C, tmp);
  7151. /* set WUH source */
  7152. tmp = FLOAT_TWO;
  7153. dspio_set_uint_param(codec, 0x31, 0x00, tmp);
  7154. chipio_set_conn_rate(codec, MEM_CONNID_WUH, SR_48_000);
  7155. /* Set speaker source? */
  7156. dspio_set_uint_param(codec, 0x32, 0x00, tmp);
  7157. ca0132_alt_dsp_initial_mic_setup(codec);
  7158. /* out, in effects + voicefx */
  7159. num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT + 1;
  7160. for (idx = 0; idx < num_fx; idx++) {
  7161. for (i = 0; i <= ca0132_effects[idx].params; i++) {
  7162. dspio_set_uint_param(codec,
  7163. ca0132_effects[idx].mid,
  7164. ca0132_effects[idx].reqs[i],
  7165. ca0132_effects[idx].def_vals[i]);
  7166. }
  7167. }
  7168. ca0132_alt_init_speaker_tuning(codec);
  7169. }
  7170. /*
  7171. * Setup default parameters for the Sound BlasterX AE-5 DSP.
  7172. */
  7173. static void ae5_setup_defaults(struct hda_codec *codec)
  7174. {
  7175. struct ca0132_spec *spec = codec->spec;
  7176. unsigned int tmp;
  7177. int num_fx;
  7178. int idx, i;
  7179. if (spec->dsp_state != DSP_DOWNLOADED)
  7180. return;
  7181. ca0132_alt_init_analog_mics(codec);
  7182. ca0132_alt_start_dsp_audio_streams(codec);
  7183. /* New, unknown SCP req's */
  7184. tmp = FLOAT_ZERO;
  7185. dspio_set_uint_param(codec, 0x96, 0x29, tmp);
  7186. dspio_set_uint_param(codec, 0x96, 0x2a, tmp);
  7187. dspio_set_uint_param(codec, 0x80, 0x0d, tmp);
  7188. dspio_set_uint_param(codec, 0x80, 0x0e, tmp);
  7189. ca0113_mmio_command_set(codec, 0x30, 0x2e, 0x3f);
  7190. ca0113_mmio_gpio_set(codec, 0, false);
  7191. ca0113_mmio_command_set(codec, 0x30, 0x28, 0x00);
  7192. /* Internal loopback off */
  7193. tmp = FLOAT_ONE;
  7194. dspio_set_uint_param(codec, 0x37, 0x08, tmp);
  7195. dspio_set_uint_param(codec, 0x37, 0x10, tmp);
  7196. /*remove DSP headroom*/
  7197. tmp = FLOAT_ZERO;
  7198. dspio_set_uint_param(codec, 0x96, 0x3C, tmp);
  7199. /* set WUH source */
  7200. tmp = FLOAT_TWO;
  7201. dspio_set_uint_param(codec, 0x31, 0x00, tmp);
  7202. chipio_set_conn_rate(codec, MEM_CONNID_WUH, SR_48_000);
  7203. /* Set speaker source? */
  7204. dspio_set_uint_param(codec, 0x32, 0x00, tmp);
  7205. ca0132_alt_dsp_initial_mic_setup(codec);
  7206. ae5_post_dsp_register_set(codec);
  7207. ae5_post_dsp_param_setup(codec);
  7208. ae5_post_dsp_pll_setup(codec);
  7209. ae5_post_dsp_stream_setup(codec);
  7210. ae5_post_dsp_startup_data(codec);
  7211. /* out, in effects + voicefx */
  7212. num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT + 1;
  7213. for (idx = 0; idx < num_fx; idx++) {
  7214. for (i = 0; i <= ca0132_effects[idx].params; i++) {
  7215. dspio_set_uint_param(codec,
  7216. ca0132_effects[idx].mid,
  7217. ca0132_effects[idx].reqs[i],
  7218. ca0132_effects[idx].def_vals[i]);
  7219. }
  7220. }
  7221. ca0132_alt_init_speaker_tuning(codec);
  7222. }
  7223. /*
  7224. * Setup default parameters for the Sound Blaster AE-7 DSP.
  7225. */
  7226. static void ae7_setup_defaults(struct hda_codec *codec)
  7227. {
  7228. struct ca0132_spec *spec = codec->spec;
  7229. unsigned int tmp;
  7230. int num_fx;
  7231. int idx, i;
  7232. if (spec->dsp_state != DSP_DOWNLOADED)
  7233. return;
  7234. ca0132_alt_init_analog_mics(codec);
  7235. ca0132_alt_start_dsp_audio_streams(codec);
  7236. ae7_post_dsp_setup_ports(codec);
  7237. tmp = FLOAT_ZERO;
  7238. dspio_set_uint_param(codec, 0x96,
  7239. SPEAKER_TUNING_FRONT_LEFT_INVERT, tmp);
  7240. dspio_set_uint_param(codec, 0x96,
  7241. SPEAKER_TUNING_FRONT_RIGHT_INVERT, tmp);
  7242. ca0113_mmio_command_set(codec, 0x30, 0x2e, 0x3f);
  7243. /* New, unknown SCP req's */
  7244. dspio_set_uint_param(codec, 0x80, 0x0d, tmp);
  7245. dspio_set_uint_param(codec, 0x80, 0x0e, tmp);
  7246. ca0113_mmio_gpio_set(codec, 0, false);
  7247. /* Internal loopback off */
  7248. tmp = FLOAT_ONE;
  7249. dspio_set_uint_param(codec, 0x37, 0x08, tmp);
  7250. dspio_set_uint_param(codec, 0x37, 0x10, tmp);
  7251. /*remove DSP headroom*/
  7252. tmp = FLOAT_ZERO;
  7253. dspio_set_uint_param(codec, 0x96, 0x3C, tmp);
  7254. /* set WUH source */
  7255. tmp = FLOAT_TWO;
  7256. dspio_set_uint_param(codec, 0x31, 0x00, tmp);
  7257. chipio_set_conn_rate(codec, MEM_CONNID_WUH, SR_48_000);
  7258. /* Set speaker source? */
  7259. dspio_set_uint_param(codec, 0x32, 0x00, tmp);
  7260. ca0113_mmio_command_set(codec, 0x30, 0x28, 0x00);
  7261. /*
  7262. * This is the second time we've called this, but this is seemingly
  7263. * what Windows does.
  7264. */
  7265. ca0132_alt_init_analog_mics(codec);
  7266. ae7_post_dsp_asi_setup(codec);
  7267. /*
  7268. * Not sure why, but these are both set to 1. They're only set to 0
  7269. * upon shutdown.
  7270. */
  7271. ca0113_mmio_gpio_set(codec, 0, true);
  7272. ca0113_mmio_gpio_set(codec, 1, true);
  7273. /* Volume control related. */
  7274. ca0113_mmio_command_set(codec, 0x48, 0x0f, 0x04);
  7275. ca0113_mmio_command_set(codec, 0x48, 0x10, 0x04);
  7276. ca0113_mmio_command_set_type2(codec, 0x48, 0x07, 0x80);
  7277. /* out, in effects + voicefx */
  7278. num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT + 1;
  7279. for (idx = 0; idx < num_fx; idx++) {
  7280. for (i = 0; i <= ca0132_effects[idx].params; i++) {
  7281. dspio_set_uint_param(codec,
  7282. ca0132_effects[idx].mid,
  7283. ca0132_effects[idx].reqs[i],
  7284. ca0132_effects[idx].def_vals[i]);
  7285. }
  7286. }
  7287. ca0132_alt_init_speaker_tuning(codec);
  7288. }
  7289. /*
  7290. * Initialization of flags in chip
  7291. */
  7292. static void ca0132_init_flags(struct hda_codec *codec)
  7293. {
  7294. struct ca0132_spec *spec = codec->spec;
  7295. if (ca0132_use_alt_functions(spec)) {
  7296. chipio_set_control_flag(codec, CONTROL_FLAG_DSP_96KHZ, 1);
  7297. chipio_set_control_flag(codec, CONTROL_FLAG_DAC_96KHZ, 1);
  7298. chipio_set_control_flag(codec, CONTROL_FLAG_ADC_B_96KHZ, 1);
  7299. chipio_set_control_flag(codec, CONTROL_FLAG_ADC_C_96KHZ, 1);
  7300. chipio_set_control_flag(codec, CONTROL_FLAG_SRC_RATE_96KHZ, 1);
  7301. chipio_set_control_flag(codec, CONTROL_FLAG_IDLE_ENABLE, 0);
  7302. chipio_set_control_flag(codec, CONTROL_FLAG_SPDIF2OUT, 0);
  7303. chipio_set_control_flag(codec,
  7304. CONTROL_FLAG_PORT_D_10KOHM_LOAD, 0);
  7305. chipio_set_control_flag(codec,
  7306. CONTROL_FLAG_PORT_A_10KOHM_LOAD, 1);
  7307. } else {
  7308. chipio_set_control_flag(codec, CONTROL_FLAG_IDLE_ENABLE, 0);
  7309. chipio_set_control_flag(codec,
  7310. CONTROL_FLAG_PORT_A_COMMON_MODE, 0);
  7311. chipio_set_control_flag(codec,
  7312. CONTROL_FLAG_PORT_D_COMMON_MODE, 0);
  7313. chipio_set_control_flag(codec,
  7314. CONTROL_FLAG_PORT_A_10KOHM_LOAD, 0);
  7315. chipio_set_control_flag(codec,
  7316. CONTROL_FLAG_PORT_D_10KOHM_LOAD, 0);
  7317. chipio_set_control_flag(codec, CONTROL_FLAG_ADC_C_HIGH_PASS, 1);
  7318. }
  7319. }
  7320. /*
  7321. * Initialization of parameters in chip
  7322. */
  7323. static void ca0132_init_params(struct hda_codec *codec)
  7324. {
  7325. struct ca0132_spec *spec = codec->spec;
  7326. if (ca0132_use_alt_functions(spec)) {
  7327. chipio_set_conn_rate(codec, MEM_CONNID_WUH, SR_48_000);
  7328. chipio_set_conn_rate(codec, 0x0B, SR_48_000);
  7329. chipio_set_control_param(codec, CONTROL_PARAM_SPDIF1_SOURCE, 0);
  7330. chipio_set_control_param(codec, 0, 0);
  7331. chipio_set_control_param(codec, CONTROL_PARAM_VIP_SOURCE, 0);
  7332. }
  7333. chipio_set_control_param(codec, CONTROL_PARAM_PORTA_160OHM_GAIN, 6);
  7334. chipio_set_control_param(codec, CONTROL_PARAM_PORTD_160OHM_GAIN, 6);
  7335. }
  7336. static void ca0132_set_dsp_msr(struct hda_codec *codec, bool is96k)
  7337. {
  7338. chipio_set_control_flag(codec, CONTROL_FLAG_DSP_96KHZ, is96k);
  7339. chipio_set_control_flag(codec, CONTROL_FLAG_DAC_96KHZ, is96k);
  7340. chipio_set_control_flag(codec, CONTROL_FLAG_SRC_RATE_96KHZ, is96k);
  7341. chipio_set_control_flag(codec, CONTROL_FLAG_SRC_CLOCK_196MHZ, is96k);
  7342. chipio_set_control_flag(codec, CONTROL_FLAG_ADC_B_96KHZ, is96k);
  7343. chipio_set_control_flag(codec, CONTROL_FLAG_ADC_C_96KHZ, is96k);
  7344. chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
  7345. chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
  7346. chipio_set_conn_rate(codec, MEM_CONNID_WUH, SR_48_000);
  7347. }
  7348. static bool ca0132_download_dsp_images(struct hda_codec *codec)
  7349. {
  7350. bool dsp_loaded = false;
  7351. struct ca0132_spec *spec = codec->spec;
  7352. const struct dsp_image_seg *dsp_os_image;
  7353. const struct firmware *fw_entry = NULL;
  7354. /*
  7355. * Alternate firmwares for different variants. The Recon3Di apparently
  7356. * can use the default firmware, but I'll leave the option in case
  7357. * it needs it again.
  7358. */
  7359. switch (ca0132_quirk(spec)) {
  7360. case QUIRK_SBZ:
  7361. case QUIRK_R3D:
  7362. case QUIRK_AE5:
  7363. if (request_firmware(&fw_entry, DESKTOP_EFX_FILE,
  7364. codec->card->dev) != 0)
  7365. codec_dbg(codec, "Desktop firmware not found.");
  7366. else
  7367. codec_dbg(codec, "Desktop firmware selected.");
  7368. break;
  7369. case QUIRK_R3DI:
  7370. if (request_firmware(&fw_entry, R3DI_EFX_FILE,
  7371. codec->card->dev) != 0)
  7372. codec_dbg(codec, "Recon3Di alt firmware not detected.");
  7373. else
  7374. codec_dbg(codec, "Recon3Di firmware selected.");
  7375. break;
  7376. default:
  7377. break;
  7378. }
  7379. /*
  7380. * Use default ctefx.bin if no alt firmware is detected, or if none
  7381. * exists for your particular codec.
  7382. */
  7383. if (!fw_entry) {
  7384. codec_dbg(codec, "Default firmware selected.");
  7385. if (request_firmware(&fw_entry, EFX_FILE,
  7386. codec->card->dev) != 0)
  7387. return false;
  7388. }
  7389. dsp_os_image = (struct dsp_image_seg *)(fw_entry->data);
  7390. if (dspload_image(codec, dsp_os_image, 0, 0, true, 0)) {
  7391. codec_err(codec, "ca0132 DSP load image failed\n");
  7392. goto exit_download;
  7393. }
  7394. dsp_loaded = dspload_wait_loaded(codec);
  7395. exit_download:
  7396. release_firmware(fw_entry);
  7397. return dsp_loaded;
  7398. }
  7399. static void ca0132_download_dsp(struct hda_codec *codec)
  7400. {
  7401. struct ca0132_spec *spec = codec->spec;
  7402. #ifndef CONFIG_SND_HDA_CODEC_CA0132_DSP
  7403. return; /* NOP */
  7404. #endif
  7405. if (spec->dsp_state == DSP_DOWNLOAD_FAILED)
  7406. return; /* don't retry failures */
  7407. chipio_enable_clocks(codec);
  7408. if (spec->dsp_state != DSP_DOWNLOADED) {
  7409. spec->dsp_state = DSP_DOWNLOADING;
  7410. if (!ca0132_download_dsp_images(codec))
  7411. spec->dsp_state = DSP_DOWNLOAD_FAILED;
  7412. else
  7413. spec->dsp_state = DSP_DOWNLOADED;
  7414. }
  7415. /* For codecs using alt functions, this is already done earlier */
  7416. if (spec->dsp_state == DSP_DOWNLOADED && !ca0132_use_alt_functions(spec))
  7417. ca0132_set_dsp_msr(codec, true);
  7418. }
  7419. static void ca0132_process_dsp_response(struct hda_codec *codec,
  7420. struct hda_jack_callback *callback)
  7421. {
  7422. struct ca0132_spec *spec = codec->spec;
  7423. codec_dbg(codec, "ca0132_process_dsp_response\n");
  7424. snd_hda_power_up_pm(codec);
  7425. if (spec->wait_scp) {
  7426. if (dspio_get_response_data(codec) >= 0)
  7427. spec->wait_scp = 0;
  7428. }
  7429. dspio_clear_response_queue(codec);
  7430. snd_hda_power_down_pm(codec);
  7431. }
  7432. static void hp_callback(struct hda_codec *codec, struct hda_jack_callback *cb)
  7433. {
  7434. struct ca0132_spec *spec = codec->spec;
  7435. struct hda_jack_tbl *tbl;
  7436. /* Delay enabling the HP amp, to let the mic-detection
  7437. * state machine run.
  7438. */
  7439. tbl = snd_hda_jack_tbl_get(codec, cb->nid);
  7440. if (tbl)
  7441. tbl->block_report = 1;
  7442. schedule_delayed_work(&spec->unsol_hp_work, msecs_to_jiffies(500));
  7443. }
  7444. static void amic_callback(struct hda_codec *codec, struct hda_jack_callback *cb)
  7445. {
  7446. struct ca0132_spec *spec = codec->spec;
  7447. if (ca0132_use_alt_functions(spec))
  7448. ca0132_alt_select_in(codec);
  7449. else
  7450. ca0132_select_mic(codec);
  7451. }
  7452. static void ca0132_setup_unsol(struct hda_codec *codec)
  7453. {
  7454. struct ca0132_spec *spec = codec->spec;
  7455. snd_hda_jack_detect_enable_callback(codec, spec->unsol_tag_hp, hp_callback);
  7456. snd_hda_jack_detect_enable_callback(codec, spec->unsol_tag_amic1,
  7457. amic_callback);
  7458. snd_hda_jack_detect_enable_callback(codec, UNSOL_TAG_DSP,
  7459. ca0132_process_dsp_response);
  7460. /* Front headphone jack detection */
  7461. if (ca0132_use_alt_functions(spec))
  7462. snd_hda_jack_detect_enable_callback(codec,
  7463. spec->unsol_tag_front_hp, hp_callback);
  7464. }
  7465. /*
  7466. * Verbs tables.
  7467. */
  7468. /* Sends before DSP download. */
  7469. static const struct hda_verb ca0132_base_init_verbs[] = {
  7470. /*enable ct extension*/
  7471. {0x15, VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE, 0x1},
  7472. {}
  7473. };
  7474. /* Send at exit. */
  7475. static const struct hda_verb ca0132_base_exit_verbs[] = {
  7476. /*set afg to D3*/
  7477. {0x01, AC_VERB_SET_POWER_STATE, 0x03},
  7478. /*disable ct extension*/
  7479. {0x15, VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE, 0},
  7480. {}
  7481. };
  7482. /* Other verbs tables. Sends after DSP download. */
  7483. static const struct hda_verb ca0132_init_verbs0[] = {
  7484. /* chip init verbs */
  7485. {0x15, 0x70D, 0xF0},
  7486. {0x15, 0x70E, 0xFE},
  7487. {0x15, 0x707, 0x75},
  7488. {0x15, 0x707, 0xD3},
  7489. {0x15, 0x707, 0x09},
  7490. {0x15, 0x707, 0x53},
  7491. {0x15, 0x707, 0xD4},
  7492. {0x15, 0x707, 0xEF},
  7493. {0x15, 0x707, 0x75},
  7494. {0x15, 0x707, 0xD3},
  7495. {0x15, 0x707, 0x09},
  7496. {0x15, 0x707, 0x02},
  7497. {0x15, 0x707, 0x37},
  7498. {0x15, 0x707, 0x78},
  7499. {0x15, 0x53C, 0xCE},
  7500. {0x15, 0x575, 0xC9},
  7501. {0x15, 0x53D, 0xCE},
  7502. {0x15, 0x5B7, 0xC9},
  7503. {0x15, 0x70D, 0xE8},
  7504. {0x15, 0x70E, 0xFE},
  7505. {0x15, 0x707, 0x02},
  7506. {0x15, 0x707, 0x68},
  7507. {0x15, 0x707, 0x62},
  7508. {0x15, 0x53A, 0xCE},
  7509. {0x15, 0x546, 0xC9},
  7510. {0x15, 0x53B, 0xCE},
  7511. {0x15, 0x5E8, 0xC9},
  7512. {}
  7513. };
  7514. /* Extra init verbs for desktop cards. */
  7515. static const struct hda_verb ca0132_init_verbs1[] = {
  7516. {0x15, 0x70D, 0x20},
  7517. {0x15, 0x70E, 0x19},
  7518. {0x15, 0x707, 0x00},
  7519. {0x15, 0x539, 0xCE},
  7520. {0x15, 0x546, 0xC9},
  7521. {0x15, 0x70D, 0xB7},
  7522. {0x15, 0x70E, 0x09},
  7523. {0x15, 0x707, 0x10},
  7524. {0x15, 0x70D, 0xAF},
  7525. {0x15, 0x70E, 0x09},
  7526. {0x15, 0x707, 0x01},
  7527. {0x15, 0x707, 0x05},
  7528. {0x15, 0x70D, 0x73},
  7529. {0x15, 0x70E, 0x09},
  7530. {0x15, 0x707, 0x14},
  7531. {0x15, 0x6FF, 0xC4},
  7532. {}
  7533. };
  7534. static void ca0132_init_chip(struct hda_codec *codec)
  7535. {
  7536. struct ca0132_spec *spec = codec->spec;
  7537. int num_fx;
  7538. int i;
  7539. unsigned int on;
  7540. mutex_init(&spec->chipio_mutex);
  7541. /*
  7542. * The Windows driver always does this upon startup, which seems to
  7543. * clear out any previous configuration. This should help issues where
  7544. * a boot into Windows prior to a boot into Linux breaks things. Also,
  7545. * Windows always sends the reset twice.
  7546. */
  7547. if (ca0132_use_alt_functions(spec)) {
  7548. chipio_set_control_flag(codec, CONTROL_FLAG_IDLE_ENABLE, 0);
  7549. chipio_write_no_mutex(codec, 0x18b0a4, 0x000000c2);
  7550. snd_hda_codec_write(codec, codec->core.afg, 0,
  7551. AC_VERB_SET_CODEC_RESET, 0);
  7552. snd_hda_codec_write(codec, codec->core.afg, 0,
  7553. AC_VERB_SET_CODEC_RESET, 0);
  7554. }
  7555. spec->cur_out_type = SPEAKER_OUT;
  7556. if (!ca0132_use_alt_functions(spec))
  7557. spec->cur_mic_type = DIGITAL_MIC;
  7558. else
  7559. spec->cur_mic_type = REAR_MIC;
  7560. spec->cur_mic_boost = 0;
  7561. for (i = 0; i < VNODES_COUNT; i++) {
  7562. spec->vnode_lvol[i] = 0x5a;
  7563. spec->vnode_rvol[i] = 0x5a;
  7564. spec->vnode_lswitch[i] = 0;
  7565. spec->vnode_rswitch[i] = 0;
  7566. }
  7567. /*
  7568. * Default states for effects are in ca0132_effects[].
  7569. */
  7570. num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT;
  7571. for (i = 0; i < num_fx; i++) {
  7572. on = (unsigned int)ca0132_effects[i].reqs[0];
  7573. spec->effects_switch[i] = on ? 1 : 0;
  7574. }
  7575. /*
  7576. * Sets defaults for the effect slider controls, only for alternative
  7577. * ca0132 codecs. Also sets x-bass crossover frequency to 80hz.
  7578. */
  7579. if (ca0132_use_alt_controls(spec)) {
  7580. /* Set speakers to default to full range. */
  7581. spec->speaker_range_val[0] = 1;
  7582. spec->speaker_range_val[1] = 1;
  7583. spec->xbass_xover_freq = 8;
  7584. for (i = 0; i < EFFECT_LEVEL_SLIDERS; i++)
  7585. spec->fx_ctl_val[i] = effect_slider_defaults[i];
  7586. spec->bass_redirect_xover_freq = 8;
  7587. }
  7588. spec->voicefx_val = 0;
  7589. spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID] = 1;
  7590. spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID] = 0;
  7591. /*
  7592. * The ZxR doesn't have a front panel header, and it's line-in is on
  7593. * the daughter board. So, there is no input enum control, and we need
  7594. * to make sure that spec->in_enum_val is set properly.
  7595. */
  7596. if (ca0132_quirk(spec) == QUIRK_ZXR)
  7597. spec->in_enum_val = REAR_MIC;
  7598. #ifdef ENABLE_TUNING_CONTROLS
  7599. ca0132_init_tuning_defaults(codec);
  7600. #endif
  7601. }
  7602. /*
  7603. * Recon3Di exit specific commands.
  7604. */
  7605. /* prevents popping noise on shutdown */
  7606. static void r3di_gpio_shutdown(struct hda_codec *codec)
  7607. {
  7608. snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_GPIO_DATA, 0x00);
  7609. }
  7610. /*
  7611. * Sound Blaster Z exit specific commands.
  7612. */
  7613. static void sbz_region2_exit(struct hda_codec *codec)
  7614. {
  7615. struct ca0132_spec *spec = codec->spec;
  7616. unsigned int i;
  7617. for (i = 0; i < 4; i++)
  7618. writeb(0x0, spec->mem_base + 0x100);
  7619. for (i = 0; i < 8; i++)
  7620. writeb(0xb3, spec->mem_base + 0x304);
  7621. ca0113_mmio_gpio_set(codec, 0, false);
  7622. ca0113_mmio_gpio_set(codec, 1, false);
  7623. ca0113_mmio_gpio_set(codec, 4, true);
  7624. ca0113_mmio_gpio_set(codec, 5, false);
  7625. ca0113_mmio_gpio_set(codec, 7, false);
  7626. }
  7627. static void sbz_set_pin_ctl_default(struct hda_codec *codec)
  7628. {
  7629. static const hda_nid_t pins[] = {0x0B, 0x0C, 0x0E, 0x12, 0x13};
  7630. unsigned int i;
  7631. snd_hda_codec_write(codec, 0x11, 0,
  7632. AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40);
  7633. for (i = 0; i < ARRAY_SIZE(pins); i++)
  7634. snd_hda_codec_write(codec, pins[i], 0,
  7635. AC_VERB_SET_PIN_WIDGET_CONTROL, 0x00);
  7636. }
  7637. static void ca0132_clear_unsolicited(struct hda_codec *codec)
  7638. {
  7639. static const hda_nid_t pins[] = {0x0B, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13};
  7640. unsigned int i;
  7641. for (i = 0; i < ARRAY_SIZE(pins); i++) {
  7642. snd_hda_codec_write(codec, pins[i], 0,
  7643. AC_VERB_SET_UNSOLICITED_ENABLE, 0x00);
  7644. }
  7645. }
  7646. /* On shutdown, sends commands in sets of three */
  7647. static void sbz_gpio_shutdown_commands(struct hda_codec *codec, int dir,
  7648. int mask, int data)
  7649. {
  7650. if (dir >= 0)
  7651. snd_hda_codec_write(codec, 0x01, 0,
  7652. AC_VERB_SET_GPIO_DIRECTION, dir);
  7653. if (mask >= 0)
  7654. snd_hda_codec_write(codec, 0x01, 0,
  7655. AC_VERB_SET_GPIO_MASK, mask);
  7656. if (data >= 0)
  7657. snd_hda_codec_write(codec, 0x01, 0,
  7658. AC_VERB_SET_GPIO_DATA, data);
  7659. }
  7660. static void zxr_dbpro_power_state_shutdown(struct hda_codec *codec)
  7661. {
  7662. static const hda_nid_t pins[] = {0x05, 0x0c, 0x09, 0x0e, 0x08, 0x11, 0x01};
  7663. unsigned int i;
  7664. for (i = 0; i < ARRAY_SIZE(pins); i++)
  7665. snd_hda_codec_write(codec, pins[i], 0,
  7666. AC_VERB_SET_POWER_STATE, 0x03);
  7667. }
  7668. static void sbz_exit_chip(struct hda_codec *codec)
  7669. {
  7670. chipio_set_stream_control(codec, 0x03, 0);
  7671. chipio_set_stream_control(codec, 0x04, 0);
  7672. /* Mess with GPIO */
  7673. sbz_gpio_shutdown_commands(codec, 0x07, 0x07, -1);
  7674. sbz_gpio_shutdown_commands(codec, 0x07, 0x07, 0x05);
  7675. sbz_gpio_shutdown_commands(codec, 0x07, 0x07, 0x01);
  7676. chipio_set_stream_control(codec, 0x14, 0);
  7677. chipio_set_stream_control(codec, 0x0C, 0);
  7678. chipio_set_conn_rate(codec, 0x41, SR_192_000);
  7679. chipio_set_conn_rate(codec, 0x91, SR_192_000);
  7680. chipio_write(codec, 0x18a020, 0x00000083);
  7681. sbz_gpio_shutdown_commands(codec, 0x07, 0x07, 0x03);
  7682. sbz_gpio_shutdown_commands(codec, 0x07, 0x07, 0x07);
  7683. sbz_gpio_shutdown_commands(codec, 0x07, 0x07, 0x06);
  7684. chipio_set_stream_control(codec, 0x0C, 0);
  7685. chipio_set_control_param(codec, 0x0D, 0x24);
  7686. ca0132_clear_unsolicited(codec);
  7687. sbz_set_pin_ctl_default(codec);
  7688. snd_hda_codec_write(codec, 0x0B, 0,
  7689. AC_VERB_SET_EAPD_BTLENABLE, 0x00);
  7690. sbz_region2_exit(codec);
  7691. }
  7692. static void r3d_exit_chip(struct hda_codec *codec)
  7693. {
  7694. ca0132_clear_unsolicited(codec);
  7695. snd_hda_codec_write(codec, 0x01, 0, 0x793, 0x00);
  7696. snd_hda_codec_write(codec, 0x01, 0, 0x794, 0x5b);
  7697. }
  7698. static void ae5_exit_chip(struct hda_codec *codec)
  7699. {
  7700. chipio_set_stream_control(codec, 0x03, 0);
  7701. chipio_set_stream_control(codec, 0x04, 0);
  7702. ca0113_mmio_command_set(codec, 0x30, 0x32, 0x3f);
  7703. ca0113_mmio_command_set(codec, 0x48, 0x07, 0x83);
  7704. ca0113_mmio_command_set(codec, 0x48, 0x07, 0x83);
  7705. ca0113_mmio_command_set(codec, 0x30, 0x30, 0x00);
  7706. ca0113_mmio_command_set(codec, 0x30, 0x2b, 0x00);
  7707. ca0113_mmio_command_set(codec, 0x30, 0x2d, 0x00);
  7708. ca0113_mmio_gpio_set(codec, 0, false);
  7709. ca0113_mmio_gpio_set(codec, 1, false);
  7710. snd_hda_codec_write(codec, 0x01, 0, 0x793, 0x00);
  7711. snd_hda_codec_write(codec, 0x01, 0, 0x794, 0x53);
  7712. chipio_set_control_param(codec, CONTROL_PARAM_ASI, 0);
  7713. chipio_set_stream_control(codec, 0x18, 0);
  7714. chipio_set_stream_control(codec, 0x0c, 0);
  7715. snd_hda_codec_write(codec, 0x01, 0, 0x724, 0x83);
  7716. }
  7717. static void ae7_exit_chip(struct hda_codec *codec)
  7718. {
  7719. chipio_set_stream_control(codec, 0x18, 0);
  7720. chipio_set_stream_source_dest(codec, 0x21, 0xc8, 0xc8);
  7721. chipio_set_stream_channels(codec, 0x21, 0);
  7722. chipio_set_control_param(codec, CONTROL_PARAM_NODE_ID, 0x09);
  7723. chipio_set_control_param(codec, 0x20, 0x01);
  7724. chipio_set_control_param(codec, CONTROL_PARAM_ASI, 0);
  7725. chipio_set_stream_control(codec, 0x18, 0);
  7726. chipio_set_stream_control(codec, 0x0c, 0);
  7727. ca0113_mmio_command_set(codec, 0x30, 0x2b, 0x00);
  7728. snd_hda_codec_write(codec, 0x15, 0, 0x724, 0x83);
  7729. ca0113_mmio_command_set_type2(codec, 0x48, 0x07, 0x83);
  7730. ca0113_mmio_command_set(codec, 0x30, 0x30, 0x00);
  7731. ca0113_mmio_command_set(codec, 0x30, 0x2e, 0x00);
  7732. ca0113_mmio_gpio_set(codec, 0, false);
  7733. ca0113_mmio_gpio_set(codec, 1, false);
  7734. ca0113_mmio_command_set(codec, 0x30, 0x32, 0x3f);
  7735. snd_hda_codec_write(codec, 0x01, 0, 0x793, 0x00);
  7736. snd_hda_codec_write(codec, 0x01, 0, 0x794, 0x53);
  7737. }
  7738. static void zxr_exit_chip(struct hda_codec *codec)
  7739. {
  7740. chipio_set_stream_control(codec, 0x03, 0);
  7741. chipio_set_stream_control(codec, 0x04, 0);
  7742. chipio_set_stream_control(codec, 0x14, 0);
  7743. chipio_set_stream_control(codec, 0x0C, 0);
  7744. chipio_set_conn_rate(codec, 0x41, SR_192_000);
  7745. chipio_set_conn_rate(codec, 0x91, SR_192_000);
  7746. chipio_write(codec, 0x18a020, 0x00000083);
  7747. snd_hda_codec_write(codec, 0x01, 0, 0x793, 0x00);
  7748. snd_hda_codec_write(codec, 0x01, 0, 0x794, 0x53);
  7749. ca0132_clear_unsolicited(codec);
  7750. sbz_set_pin_ctl_default(codec);
  7751. snd_hda_codec_write(codec, 0x0B, 0, AC_VERB_SET_EAPD_BTLENABLE, 0x00);
  7752. ca0113_mmio_gpio_set(codec, 5, false);
  7753. ca0113_mmio_gpio_set(codec, 2, false);
  7754. ca0113_mmio_gpio_set(codec, 3, false);
  7755. ca0113_mmio_gpio_set(codec, 0, false);
  7756. ca0113_mmio_gpio_set(codec, 4, true);
  7757. ca0113_mmio_gpio_set(codec, 0, true);
  7758. ca0113_mmio_gpio_set(codec, 5, true);
  7759. ca0113_mmio_gpio_set(codec, 2, false);
  7760. ca0113_mmio_gpio_set(codec, 3, false);
  7761. }
  7762. static void ca0132_exit_chip(struct hda_codec *codec)
  7763. {
  7764. /* put any chip cleanup stuffs here. */
  7765. if (dspload_is_loaded(codec))
  7766. dsp_reset(codec);
  7767. }
  7768. /*
  7769. * This fixes a problem that was hard to reproduce. Very rarely, I would
  7770. * boot up, and there would be no sound, but the DSP indicated it had loaded
  7771. * properly. I did a few memory dumps to see if anything was different, and
  7772. * there were a few areas of memory uninitialized with a1a2a3a4. This function
  7773. * checks if those areas are uninitialized, and if they are, it'll attempt to
  7774. * reload the card 3 times. Usually it fixes by the second.
  7775. */
  7776. static void sbz_dsp_startup_check(struct hda_codec *codec)
  7777. {
  7778. struct ca0132_spec *spec = codec->spec;
  7779. unsigned int dsp_data_check[4];
  7780. unsigned int cur_address = 0x390;
  7781. unsigned int i;
  7782. unsigned int failure = 0;
  7783. unsigned int reload = 3;
  7784. if (spec->startup_check_entered)
  7785. return;
  7786. spec->startup_check_entered = true;
  7787. for (i = 0; i < 4; i++) {
  7788. chipio_read(codec, cur_address, &dsp_data_check[i]);
  7789. cur_address += 0x4;
  7790. }
  7791. for (i = 0; i < 4; i++) {
  7792. if (dsp_data_check[i] == 0xa1a2a3a4)
  7793. failure = 1;
  7794. }
  7795. codec_dbg(codec, "Startup Check: %d ", failure);
  7796. if (failure)
  7797. codec_info(codec, "DSP not initialized properly. Attempting to fix.");
  7798. /*
  7799. * While the failure condition is true, and we haven't reached our
  7800. * three reload limit, continue trying to reload the driver and
  7801. * fix the issue.
  7802. */
  7803. while (failure && (reload != 0)) {
  7804. codec_info(codec, "Reloading... Tries left: %d", reload);
  7805. sbz_exit_chip(codec);
  7806. spec->dsp_state = DSP_DOWNLOAD_INIT;
  7807. codec->patch_ops.init(codec);
  7808. failure = 0;
  7809. for (i = 0; i < 4; i++) {
  7810. chipio_read(codec, cur_address, &dsp_data_check[i]);
  7811. cur_address += 0x4;
  7812. }
  7813. for (i = 0; i < 4; i++) {
  7814. if (dsp_data_check[i] == 0xa1a2a3a4)
  7815. failure = 1;
  7816. }
  7817. reload--;
  7818. }
  7819. if (!failure && reload < 3)
  7820. codec_info(codec, "DSP fixed.");
  7821. if (!failure)
  7822. return;
  7823. codec_info(codec, "DSP failed to initialize properly. Either try a full shutdown or a suspend to clear the internal memory.");
  7824. }
  7825. /*
  7826. * This is for the extra volume verbs 0x797 (left) and 0x798 (right). These add
  7827. * extra precision for decibel values. If you had the dB value in floating point
  7828. * you would take the value after the decimal point, multiply by 64, and divide
  7829. * by 2. So for 8.59, it's (59 * 64) / 100. Useful if someone wanted to
  7830. * implement fixed point or floating point dB volumes. For now, I'll set them
  7831. * to 0 just incase a value has lingered from a boot into Windows.
  7832. */
  7833. static void ca0132_alt_vol_setup(struct hda_codec *codec)
  7834. {
  7835. snd_hda_codec_write(codec, 0x02, 0, 0x797, 0x00);
  7836. snd_hda_codec_write(codec, 0x02, 0, 0x798, 0x00);
  7837. snd_hda_codec_write(codec, 0x03, 0, 0x797, 0x00);
  7838. snd_hda_codec_write(codec, 0x03, 0, 0x798, 0x00);
  7839. snd_hda_codec_write(codec, 0x04, 0, 0x797, 0x00);
  7840. snd_hda_codec_write(codec, 0x04, 0, 0x798, 0x00);
  7841. snd_hda_codec_write(codec, 0x07, 0, 0x797, 0x00);
  7842. snd_hda_codec_write(codec, 0x07, 0, 0x798, 0x00);
  7843. }
  7844. /*
  7845. * Extra commands that don't really fit anywhere else.
  7846. */
  7847. static void sbz_pre_dsp_setup(struct hda_codec *codec)
  7848. {
  7849. struct ca0132_spec *spec = codec->spec;
  7850. writel(0x00820680, spec->mem_base + 0x01C);
  7851. writel(0x00820680, spec->mem_base + 0x01C);
  7852. chipio_write(codec, 0x18b0a4, 0x000000c2);
  7853. snd_hda_codec_write(codec, 0x11, 0,
  7854. AC_VERB_SET_PIN_WIDGET_CONTROL, 0x44);
  7855. }
  7856. static void r3d_pre_dsp_setup(struct hda_codec *codec)
  7857. {
  7858. chipio_write(codec, 0x18b0a4, 0x000000c2);
  7859. chipio_8051_write_exram(codec, 0x1c1e, 0x5b);
  7860. snd_hda_codec_write(codec, 0x11, 0,
  7861. AC_VERB_SET_PIN_WIDGET_CONTROL, 0x44);
  7862. }
  7863. static void r3di_pre_dsp_setup(struct hda_codec *codec)
  7864. {
  7865. chipio_write(codec, 0x18b0a4, 0x000000c2);
  7866. chipio_8051_write_exram(codec, 0x1c1e, 0x5b);
  7867. chipio_8051_write_exram(codec, 0x1920, 0x00);
  7868. chipio_8051_write_exram(codec, 0x1921, 0x40);
  7869. snd_hda_codec_write(codec, 0x11, 0,
  7870. AC_VERB_SET_PIN_WIDGET_CONTROL, 0x04);
  7871. }
  7872. /*
  7873. * The ZxR seems to use alternative DAC's for the surround channels, which
  7874. * require PLL PMU setup for the clock rate, I'm guessing. Without setting
  7875. * this up, we get no audio out of the surround jacks.
  7876. */
  7877. static void zxr_pre_dsp_setup(struct hda_codec *codec)
  7878. {
  7879. static const unsigned int addr[] = { 0x43, 0x40, 0x41, 0x42, 0x45 };
  7880. static const unsigned int data[] = { 0x08, 0x0c, 0x0b, 0x07, 0x0d };
  7881. unsigned int i;
  7882. chipio_write(codec, 0x189000, 0x0001f100);
  7883. msleep(50);
  7884. chipio_write(codec, 0x18900c, 0x0001f100);
  7885. msleep(50);
  7886. /*
  7887. * This writes a RET instruction at the entry point of the function at
  7888. * 0xfa92 in exram. This function seems to have something to do with
  7889. * ASI. Might be some way to prevent the card from reconfiguring the
  7890. * ASI stuff itself.
  7891. */
  7892. chipio_8051_write_exram(codec, 0xfa92, 0x22);
  7893. chipio_8051_write_pll_pmu(codec, 0x51, 0x98);
  7894. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0, 0x725, 0x82);
  7895. chipio_set_control_param(codec, CONTROL_PARAM_ASI, 3);
  7896. chipio_write(codec, 0x18902c, 0x00000000);
  7897. msleep(50);
  7898. chipio_write(codec, 0x18902c, 0x00000003);
  7899. msleep(50);
  7900. for (i = 0; i < ARRAY_SIZE(addr); i++)
  7901. chipio_8051_write_pll_pmu(codec, addr[i], data[i]);
  7902. }
  7903. /*
  7904. * These are sent before the DSP is downloaded. Not sure
  7905. * what they do, or if they're necessary. Could possibly
  7906. * be removed. Figure they're better to leave in.
  7907. */
  7908. static const unsigned int ca0113_mmio_init_address_sbz[] = {
  7909. 0x400, 0x408, 0x40c, 0x01c, 0xc0c, 0xc00, 0xc04, 0xc0c, 0xc0c, 0xc0c,
  7910. 0xc0c, 0xc08, 0xc08, 0xc08, 0xc08, 0xc08, 0xc04
  7911. };
  7912. static const unsigned int ca0113_mmio_init_data_sbz[] = {
  7913. 0x00000030, 0x00000000, 0x00000003, 0x00000003, 0x00000003,
  7914. 0x00000003, 0x000000c1, 0x000000f1, 0x00000001, 0x000000c7,
  7915. 0x000000c1, 0x00000080
  7916. };
  7917. static const unsigned int ca0113_mmio_init_data_zxr[] = {
  7918. 0x00000030, 0x00000000, 0x00000000, 0x00000003, 0x00000003,
  7919. 0x00000003, 0x00000001, 0x000000f1, 0x00000001, 0x000000c7,
  7920. 0x000000c1, 0x00000080
  7921. };
  7922. static const unsigned int ca0113_mmio_init_address_ae5[] = {
  7923. 0x400, 0x42c, 0x46c, 0x4ac, 0x4ec, 0x43c, 0x47c, 0x4bc, 0x4fc, 0x408,
  7924. 0x100, 0x410, 0x40c, 0x100, 0x100, 0x830, 0x86c, 0x800, 0x86c, 0x800,
  7925. 0x804, 0x20c, 0x01c, 0xc0c, 0xc00, 0xc04, 0xc0c, 0xc0c, 0xc0c, 0xc0c,
  7926. 0xc08, 0xc08, 0xc08, 0xc08, 0xc08, 0xc04, 0x01c
  7927. };
  7928. static const unsigned int ca0113_mmio_init_data_ae5[] = {
  7929. 0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  7930. 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000001,
  7931. 0x00000600, 0x00000014, 0x00000001, 0x0000060f, 0x0000070f,
  7932. 0x00000aff, 0x00000000, 0x0000006b, 0x00000001, 0x0000006b,
  7933. 0x00000057, 0x00800000, 0x00880680, 0x00000080, 0x00000030,
  7934. 0x00000000, 0x00000000, 0x00000003, 0x00000003, 0x00000003,
  7935. 0x00000001, 0x000000f1, 0x00000001, 0x000000c7, 0x000000c1,
  7936. 0x00000080, 0x00880680
  7937. };
  7938. static void ca0132_mmio_init_sbz(struct hda_codec *codec)
  7939. {
  7940. struct ca0132_spec *spec = codec->spec;
  7941. unsigned int tmp[2], i, count, cur_addr;
  7942. const unsigned int *addr, *data;
  7943. addr = ca0113_mmio_init_address_sbz;
  7944. for (i = 0; i < 3; i++)
  7945. writel(0x00000000, spec->mem_base + addr[i]);
  7946. cur_addr = i;
  7947. switch (ca0132_quirk(spec)) {
  7948. case QUIRK_ZXR:
  7949. tmp[0] = 0x00880480;
  7950. tmp[1] = 0x00000080;
  7951. break;
  7952. case QUIRK_SBZ:
  7953. tmp[0] = 0x00820680;
  7954. tmp[1] = 0x00000083;
  7955. break;
  7956. case QUIRK_R3D:
  7957. tmp[0] = 0x00880680;
  7958. tmp[1] = 0x00000083;
  7959. break;
  7960. default:
  7961. tmp[0] = 0x00000000;
  7962. tmp[1] = 0x00000000;
  7963. break;
  7964. }
  7965. for (i = 0; i < 2; i++)
  7966. writel(tmp[i], spec->mem_base + addr[cur_addr + i]);
  7967. cur_addr += i;
  7968. switch (ca0132_quirk(spec)) {
  7969. case QUIRK_ZXR:
  7970. count = ARRAY_SIZE(ca0113_mmio_init_data_zxr);
  7971. data = ca0113_mmio_init_data_zxr;
  7972. break;
  7973. default:
  7974. count = ARRAY_SIZE(ca0113_mmio_init_data_sbz);
  7975. data = ca0113_mmio_init_data_sbz;
  7976. break;
  7977. }
  7978. for (i = 0; i < count; i++)
  7979. writel(data[i], spec->mem_base + addr[cur_addr + i]);
  7980. }
  7981. static void ca0132_mmio_init_ae5(struct hda_codec *codec)
  7982. {
  7983. struct ca0132_spec *spec = codec->spec;
  7984. const unsigned int *addr, *data;
  7985. unsigned int i, count;
  7986. addr = ca0113_mmio_init_address_ae5;
  7987. data = ca0113_mmio_init_data_ae5;
  7988. count = ARRAY_SIZE(ca0113_mmio_init_data_ae5);
  7989. if (ca0132_quirk(spec) == QUIRK_AE7) {
  7990. writel(0x00000680, spec->mem_base + 0x1c);
  7991. writel(0x00880680, spec->mem_base + 0x1c);
  7992. }
  7993. for (i = 0; i < count; i++) {
  7994. /*
  7995. * AE-7 shares all writes with the AE-5, except that it writes
  7996. * a different value to 0x20c.
  7997. */
  7998. if (i == 21 && ca0132_quirk(spec) == QUIRK_AE7) {
  7999. writel(0x00800001, spec->mem_base + addr[i]);
  8000. continue;
  8001. }
  8002. writel(data[i], spec->mem_base + addr[i]);
  8003. }
  8004. if (ca0132_quirk(spec) == QUIRK_AE5)
  8005. writel(0x00880680, spec->mem_base + 0x1c);
  8006. }
  8007. static void ca0132_mmio_init(struct hda_codec *codec)
  8008. {
  8009. struct ca0132_spec *spec = codec->spec;
  8010. switch (ca0132_quirk(spec)) {
  8011. case QUIRK_R3D:
  8012. case QUIRK_SBZ:
  8013. case QUIRK_ZXR:
  8014. ca0132_mmio_init_sbz(codec);
  8015. break;
  8016. case QUIRK_AE5:
  8017. ca0132_mmio_init_ae5(codec);
  8018. break;
  8019. default:
  8020. break;
  8021. }
  8022. }
  8023. static const unsigned int ca0132_ae5_register_set_addresses[] = {
  8024. 0x304, 0x304, 0x304, 0x304, 0x100, 0x304, 0x100, 0x304, 0x100, 0x304,
  8025. 0x100, 0x304, 0x86c, 0x800, 0x86c, 0x800, 0x804
  8026. };
  8027. static const unsigned char ca0132_ae5_register_set_data[] = {
  8028. 0x0f, 0x0e, 0x1f, 0x0c, 0x3f, 0x08, 0x7f, 0x00, 0xff, 0x00, 0x6b,
  8029. 0x01, 0x6b, 0x57
  8030. };
  8031. /*
  8032. * This function writes to some SFR's, does some region2 writes, and then
  8033. * eventually resets the codec with the 0x7ff verb. Not quite sure why it does
  8034. * what it does.
  8035. */
  8036. static void ae5_register_set(struct hda_codec *codec)
  8037. {
  8038. struct ca0132_spec *spec = codec->spec;
  8039. unsigned int count = ARRAY_SIZE(ca0132_ae5_register_set_addresses);
  8040. const unsigned int *addr = ca0132_ae5_register_set_addresses;
  8041. const unsigned char *data = ca0132_ae5_register_set_data;
  8042. unsigned int i, cur_addr;
  8043. unsigned char tmp[3];
  8044. if (ca0132_quirk(spec) == QUIRK_AE7)
  8045. chipio_8051_write_pll_pmu(codec, 0x41, 0xc8);
  8046. chipio_8051_write_direct(codec, 0x93, 0x10);
  8047. chipio_8051_write_pll_pmu(codec, 0x44, 0xc2);
  8048. if (ca0132_quirk(spec) == QUIRK_AE7) {
  8049. tmp[0] = 0x03;
  8050. tmp[1] = 0x03;
  8051. tmp[2] = 0x07;
  8052. } else {
  8053. tmp[0] = 0x0f;
  8054. tmp[1] = 0x0f;
  8055. tmp[2] = 0x0f;
  8056. }
  8057. for (i = cur_addr = 0; i < 3; i++, cur_addr++)
  8058. writeb(tmp[i], spec->mem_base + addr[cur_addr]);
  8059. /*
  8060. * First writes are in single bytes, final are in 4 bytes. So, we use
  8061. * writeb, then writel.
  8062. */
  8063. for (i = 0; cur_addr < 12; i++, cur_addr++)
  8064. writeb(data[i], spec->mem_base + addr[cur_addr]);
  8065. for (; cur_addr < count; i++, cur_addr++)
  8066. writel(data[i], spec->mem_base + addr[cur_addr]);
  8067. writel(0x00800001, spec->mem_base + 0x20c);
  8068. if (ca0132_quirk(spec) == QUIRK_AE7) {
  8069. ca0113_mmio_command_set_type2(codec, 0x48, 0x07, 0x83);
  8070. ca0113_mmio_command_set(codec, 0x30, 0x2e, 0x3f);
  8071. } else {
  8072. ca0113_mmio_command_set(codec, 0x30, 0x2d, 0x3f);
  8073. }
  8074. chipio_8051_write_direct(codec, 0x90, 0x00);
  8075. chipio_8051_write_direct(codec, 0x90, 0x10);
  8076. if (ca0132_quirk(spec) == QUIRK_AE5)
  8077. ca0113_mmio_command_set(codec, 0x48, 0x07, 0x83);
  8078. }
  8079. /*
  8080. * Extra init functions for alternative ca0132 codecs. Done
  8081. * here so they don't clutter up the main ca0132_init function
  8082. * anymore than they have to.
  8083. */
  8084. static void ca0132_alt_init(struct hda_codec *codec)
  8085. {
  8086. struct ca0132_spec *spec = codec->spec;
  8087. ca0132_alt_vol_setup(codec);
  8088. switch (ca0132_quirk(spec)) {
  8089. case QUIRK_SBZ:
  8090. codec_dbg(codec, "SBZ alt_init");
  8091. ca0132_gpio_init(codec);
  8092. sbz_pre_dsp_setup(codec);
  8093. snd_hda_sequence_write(codec, spec->chip_init_verbs);
  8094. snd_hda_sequence_write(codec, spec->desktop_init_verbs);
  8095. break;
  8096. case QUIRK_R3DI:
  8097. codec_dbg(codec, "R3DI alt_init");
  8098. ca0132_gpio_init(codec);
  8099. ca0132_gpio_setup(codec);
  8100. r3di_gpio_dsp_status_set(codec, R3DI_DSP_DOWNLOADING);
  8101. r3di_pre_dsp_setup(codec);
  8102. snd_hda_sequence_write(codec, spec->chip_init_verbs);
  8103. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0, 0x6FF, 0xC4);
  8104. break;
  8105. case QUIRK_R3D:
  8106. r3d_pre_dsp_setup(codec);
  8107. snd_hda_sequence_write(codec, spec->chip_init_verbs);
  8108. snd_hda_sequence_write(codec, spec->desktop_init_verbs);
  8109. break;
  8110. case QUIRK_AE5:
  8111. ca0132_gpio_init(codec);
  8112. chipio_8051_write_pll_pmu(codec, 0x49, 0x88);
  8113. chipio_write(codec, 0x18b030, 0x00000020);
  8114. snd_hda_sequence_write(codec, spec->chip_init_verbs);
  8115. snd_hda_sequence_write(codec, spec->desktop_init_verbs);
  8116. ca0113_mmio_command_set(codec, 0x30, 0x32, 0x3f);
  8117. break;
  8118. case QUIRK_AE7:
  8119. ca0132_gpio_init(codec);
  8120. chipio_8051_write_pll_pmu(codec, 0x49, 0x88);
  8121. snd_hda_sequence_write(codec, spec->chip_init_verbs);
  8122. snd_hda_sequence_write(codec, spec->desktop_init_verbs);
  8123. chipio_write(codec, 0x18b008, 0x000000f8);
  8124. chipio_write(codec, 0x18b008, 0x000000f0);
  8125. chipio_write(codec, 0x18b030, 0x00000020);
  8126. ca0113_mmio_command_set(codec, 0x30, 0x32, 0x3f);
  8127. break;
  8128. case QUIRK_ZXR:
  8129. chipio_8051_write_pll_pmu(codec, 0x49, 0x88);
  8130. snd_hda_sequence_write(codec, spec->chip_init_verbs);
  8131. snd_hda_sequence_write(codec, spec->desktop_init_verbs);
  8132. zxr_pre_dsp_setup(codec);
  8133. break;
  8134. default:
  8135. break;
  8136. }
  8137. }
  8138. static int ca0132_init(struct hda_codec *codec)
  8139. {
  8140. struct ca0132_spec *spec = codec->spec;
  8141. struct auto_pin_cfg *cfg = &spec->autocfg;
  8142. int i;
  8143. bool dsp_loaded;
  8144. /*
  8145. * If the DSP is already downloaded, and init has been entered again,
  8146. * there's only two reasons for it. One, the codec has awaken from a
  8147. * suspended state, and in that case dspload_is_loaded will return
  8148. * false, and the init will be ran again. The other reason it gets
  8149. * re entered is on startup for some reason it triggers a suspend and
  8150. * resume state. In this case, it will check if the DSP is downloaded,
  8151. * and not run the init function again. For codecs using alt_functions,
  8152. * it will check if the DSP is loaded properly.
  8153. */
  8154. if (spec->dsp_state == DSP_DOWNLOADED) {
  8155. dsp_loaded = dspload_is_loaded(codec);
  8156. if (!dsp_loaded) {
  8157. spec->dsp_reload = true;
  8158. spec->dsp_state = DSP_DOWNLOAD_INIT;
  8159. } else {
  8160. if (ca0132_quirk(spec) == QUIRK_SBZ)
  8161. sbz_dsp_startup_check(codec);
  8162. return 0;
  8163. }
  8164. }
  8165. if (spec->dsp_state != DSP_DOWNLOAD_FAILED)
  8166. spec->dsp_state = DSP_DOWNLOAD_INIT;
  8167. spec->curr_chip_addx = INVALID_CHIP_ADDRESS;
  8168. if (ca0132_use_pci_mmio(spec))
  8169. ca0132_mmio_init(codec);
  8170. snd_hda_power_up_pm(codec);
  8171. if (ca0132_quirk(spec) == QUIRK_AE5 || ca0132_quirk(spec) == QUIRK_AE7)
  8172. ae5_register_set(codec);
  8173. ca0132_init_params(codec);
  8174. ca0132_init_flags(codec);
  8175. snd_hda_sequence_write(codec, spec->base_init_verbs);
  8176. if (ca0132_use_alt_functions(spec))
  8177. ca0132_alt_init(codec);
  8178. ca0132_download_dsp(codec);
  8179. ca0132_refresh_widget_caps(codec);
  8180. switch (ca0132_quirk(spec)) {
  8181. case QUIRK_R3DI:
  8182. case QUIRK_R3D:
  8183. r3d_setup_defaults(codec);
  8184. break;
  8185. case QUIRK_SBZ:
  8186. case QUIRK_ZXR:
  8187. sbz_setup_defaults(codec);
  8188. break;
  8189. case QUIRK_AE5:
  8190. ae5_setup_defaults(codec);
  8191. break;
  8192. case QUIRK_AE7:
  8193. ae7_setup_defaults(codec);
  8194. break;
  8195. default:
  8196. ca0132_setup_defaults(codec);
  8197. ca0132_init_analog_mic2(codec);
  8198. ca0132_init_dmic(codec);
  8199. break;
  8200. }
  8201. for (i = 0; i < spec->num_outputs; i++)
  8202. init_output(codec, spec->out_pins[i], spec->dacs[0]);
  8203. init_output(codec, cfg->dig_out_pins[0], spec->dig_out);
  8204. for (i = 0; i < spec->num_inputs; i++)
  8205. init_input(codec, spec->input_pins[i], spec->adcs[i]);
  8206. init_input(codec, cfg->dig_in_pin, spec->dig_in);
  8207. if (!ca0132_use_alt_functions(spec)) {
  8208. snd_hda_sequence_write(codec, spec->chip_init_verbs);
  8209. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  8210. VENDOR_CHIPIO_PARAM_EX_ID_SET, 0x0D);
  8211. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  8212. VENDOR_CHIPIO_PARAM_EX_VALUE_SET, 0x20);
  8213. }
  8214. if (ca0132_quirk(spec) == QUIRK_SBZ)
  8215. ca0132_gpio_setup(codec);
  8216. snd_hda_sequence_write(codec, spec->spec_init_verbs);
  8217. if (ca0132_use_alt_functions(spec)) {
  8218. ca0132_alt_select_out(codec);
  8219. ca0132_alt_select_in(codec);
  8220. } else {
  8221. ca0132_select_out(codec);
  8222. ca0132_select_mic(codec);
  8223. }
  8224. snd_hda_jack_report_sync(codec);
  8225. /*
  8226. * Re set the PlayEnhancement switch on a resume event, because the
  8227. * controls will not be reloaded.
  8228. */
  8229. if (spec->dsp_reload) {
  8230. spec->dsp_reload = false;
  8231. ca0132_pe_switch_set(codec);
  8232. }
  8233. snd_hda_power_down_pm(codec);
  8234. return 0;
  8235. }
  8236. static int dbpro_init(struct hda_codec *codec)
  8237. {
  8238. struct ca0132_spec *spec = codec->spec;
  8239. struct auto_pin_cfg *cfg = &spec->autocfg;
  8240. unsigned int i;
  8241. init_output(codec, cfg->dig_out_pins[0], spec->dig_out);
  8242. init_input(codec, cfg->dig_in_pin, spec->dig_in);
  8243. for (i = 0; i < spec->num_inputs; i++)
  8244. init_input(codec, spec->input_pins[i], spec->adcs[i]);
  8245. return 0;
  8246. }
  8247. static void ca0132_free(struct hda_codec *codec)
  8248. {
  8249. struct ca0132_spec *spec = codec->spec;
  8250. cancel_delayed_work_sync(&spec->unsol_hp_work);
  8251. snd_hda_power_up(codec);
  8252. switch (ca0132_quirk(spec)) {
  8253. case QUIRK_SBZ:
  8254. sbz_exit_chip(codec);
  8255. break;
  8256. case QUIRK_ZXR:
  8257. zxr_exit_chip(codec);
  8258. break;
  8259. case QUIRK_R3D:
  8260. r3d_exit_chip(codec);
  8261. break;
  8262. case QUIRK_AE5:
  8263. ae5_exit_chip(codec);
  8264. break;
  8265. case QUIRK_AE7:
  8266. ae7_exit_chip(codec);
  8267. break;
  8268. case QUIRK_R3DI:
  8269. r3di_gpio_shutdown(codec);
  8270. break;
  8271. default:
  8272. break;
  8273. }
  8274. snd_hda_sequence_write(codec, spec->base_exit_verbs);
  8275. ca0132_exit_chip(codec);
  8276. snd_hda_power_down(codec);
  8277. #ifdef CONFIG_PCI
  8278. if (spec->mem_base)
  8279. pci_iounmap(codec->bus->pci, spec->mem_base);
  8280. #endif
  8281. kfree(spec->spec_init_verbs);
  8282. kfree(codec->spec);
  8283. }
  8284. static void dbpro_free(struct hda_codec *codec)
  8285. {
  8286. struct ca0132_spec *spec = codec->spec;
  8287. zxr_dbpro_power_state_shutdown(codec);
  8288. kfree(spec->spec_init_verbs);
  8289. kfree(codec->spec);
  8290. }
  8291. #ifdef CONFIG_PM
  8292. static int ca0132_suspend(struct hda_codec *codec)
  8293. {
  8294. struct ca0132_spec *spec = codec->spec;
  8295. cancel_delayed_work_sync(&spec->unsol_hp_work);
  8296. return 0;
  8297. }
  8298. #endif
  8299. static const struct hda_codec_ops ca0132_patch_ops = {
  8300. .build_controls = ca0132_build_controls,
  8301. .build_pcms = ca0132_build_pcms,
  8302. .init = ca0132_init,
  8303. .free = ca0132_free,
  8304. .unsol_event = snd_hda_jack_unsol_event,
  8305. #ifdef CONFIG_PM
  8306. .suspend = ca0132_suspend,
  8307. #endif
  8308. };
  8309. static const struct hda_codec_ops dbpro_patch_ops = {
  8310. .build_controls = dbpro_build_controls,
  8311. .build_pcms = dbpro_build_pcms,
  8312. .init = dbpro_init,
  8313. .free = dbpro_free,
  8314. };
  8315. static void ca0132_config(struct hda_codec *codec)
  8316. {
  8317. struct ca0132_spec *spec = codec->spec;
  8318. spec->dacs[0] = 0x2;
  8319. spec->dacs[1] = 0x3;
  8320. spec->dacs[2] = 0x4;
  8321. spec->multiout.dac_nids = spec->dacs;
  8322. spec->multiout.num_dacs = 3;
  8323. if (!ca0132_use_alt_functions(spec))
  8324. spec->multiout.max_channels = 2;
  8325. else
  8326. spec->multiout.max_channels = 6;
  8327. switch (ca0132_quirk(spec)) {
  8328. case QUIRK_ALIENWARE:
  8329. codec_dbg(codec, "%s: QUIRK_ALIENWARE applied.\n", __func__);
  8330. snd_hda_apply_pincfgs(codec, alienware_pincfgs);
  8331. break;
  8332. case QUIRK_SBZ:
  8333. codec_dbg(codec, "%s: QUIRK_SBZ applied.\n", __func__);
  8334. snd_hda_apply_pincfgs(codec, sbz_pincfgs);
  8335. break;
  8336. case QUIRK_ZXR:
  8337. codec_dbg(codec, "%s: QUIRK_ZXR applied.\n", __func__);
  8338. snd_hda_apply_pincfgs(codec, zxr_pincfgs);
  8339. break;
  8340. case QUIRK_R3D:
  8341. codec_dbg(codec, "%s: QUIRK_R3D applied.\n", __func__);
  8342. snd_hda_apply_pincfgs(codec, r3d_pincfgs);
  8343. break;
  8344. case QUIRK_R3DI:
  8345. codec_dbg(codec, "%s: QUIRK_R3DI applied.\n", __func__);
  8346. snd_hda_apply_pincfgs(codec, r3di_pincfgs);
  8347. break;
  8348. case QUIRK_AE5:
  8349. codec_dbg(codec, "%s: QUIRK_AE5 applied.\n", __func__);
  8350. snd_hda_apply_pincfgs(codec, ae5_pincfgs);
  8351. break;
  8352. case QUIRK_AE7:
  8353. codec_dbg(codec, "%s: QUIRK_AE7 applied.\n", __func__);
  8354. snd_hda_apply_pincfgs(codec, ae7_pincfgs);
  8355. break;
  8356. default:
  8357. break;
  8358. }
  8359. switch (ca0132_quirk(spec)) {
  8360. case QUIRK_ALIENWARE:
  8361. spec->num_outputs = 2;
  8362. spec->out_pins[0] = 0x0b; /* speaker out */
  8363. spec->out_pins[1] = 0x0f;
  8364. spec->shared_out_nid = 0x2;
  8365. spec->unsol_tag_hp = 0x0f;
  8366. spec->adcs[0] = 0x7; /* digital mic / analog mic1 */
  8367. spec->adcs[1] = 0x8; /* analog mic2 */
  8368. spec->adcs[2] = 0xa; /* what u hear */
  8369. spec->num_inputs = 3;
  8370. spec->input_pins[0] = 0x12;
  8371. spec->input_pins[1] = 0x11;
  8372. spec->input_pins[2] = 0x13;
  8373. spec->shared_mic_nid = 0x7;
  8374. spec->unsol_tag_amic1 = 0x11;
  8375. break;
  8376. case QUIRK_SBZ:
  8377. case QUIRK_R3D:
  8378. spec->num_outputs = 2;
  8379. spec->out_pins[0] = 0x0B; /* Line out */
  8380. spec->out_pins[1] = 0x0F; /* Rear headphone out */
  8381. spec->out_pins[2] = 0x10; /* Front Headphone / Center/LFE*/
  8382. spec->out_pins[3] = 0x11; /* Rear surround */
  8383. spec->shared_out_nid = 0x2;
  8384. spec->unsol_tag_hp = spec->out_pins[1];
  8385. spec->unsol_tag_front_hp = spec->out_pins[2];
  8386. spec->adcs[0] = 0x7; /* Rear Mic / Line-in */
  8387. spec->adcs[1] = 0x8; /* Front Mic, but only if no DSP */
  8388. spec->adcs[2] = 0xa; /* what u hear */
  8389. spec->num_inputs = 2;
  8390. spec->input_pins[0] = 0x12; /* Rear Mic / Line-in */
  8391. spec->input_pins[1] = 0x13; /* What U Hear */
  8392. spec->shared_mic_nid = 0x7;
  8393. spec->unsol_tag_amic1 = spec->input_pins[0];
  8394. /* SPDIF I/O */
  8395. spec->dig_out = 0x05;
  8396. spec->multiout.dig_out_nid = spec->dig_out;
  8397. spec->dig_in = 0x09;
  8398. break;
  8399. case QUIRK_ZXR:
  8400. spec->num_outputs = 2;
  8401. spec->out_pins[0] = 0x0B; /* Line out */
  8402. spec->out_pins[1] = 0x0F; /* Rear headphone out */
  8403. spec->out_pins[2] = 0x10; /* Center/LFE */
  8404. spec->out_pins[3] = 0x11; /* Rear surround */
  8405. spec->shared_out_nid = 0x2;
  8406. spec->unsol_tag_hp = spec->out_pins[1];
  8407. spec->unsol_tag_front_hp = spec->out_pins[2];
  8408. spec->adcs[0] = 0x7; /* Rear Mic / Line-in */
  8409. spec->adcs[1] = 0x8; /* Not connected, no front mic */
  8410. spec->adcs[2] = 0xa; /* what u hear */
  8411. spec->num_inputs = 2;
  8412. spec->input_pins[0] = 0x12; /* Rear Mic / Line-in */
  8413. spec->input_pins[1] = 0x13; /* What U Hear */
  8414. spec->shared_mic_nid = 0x7;
  8415. spec->unsol_tag_amic1 = spec->input_pins[0];
  8416. break;
  8417. case QUIRK_ZXR_DBPRO:
  8418. spec->adcs[0] = 0x8; /* ZxR DBPro Aux In */
  8419. spec->num_inputs = 1;
  8420. spec->input_pins[0] = 0x11; /* RCA Line-in */
  8421. spec->dig_out = 0x05;
  8422. spec->multiout.dig_out_nid = spec->dig_out;
  8423. spec->dig_in = 0x09;
  8424. break;
  8425. case QUIRK_AE5:
  8426. case QUIRK_AE7:
  8427. spec->num_outputs = 2;
  8428. spec->out_pins[0] = 0x0B; /* Line out */
  8429. spec->out_pins[1] = 0x11; /* Rear headphone out */
  8430. spec->out_pins[2] = 0x10; /* Front Headphone / Center/LFE*/
  8431. spec->out_pins[3] = 0x0F; /* Rear surround */
  8432. spec->shared_out_nid = 0x2;
  8433. spec->unsol_tag_hp = spec->out_pins[1];
  8434. spec->unsol_tag_front_hp = spec->out_pins[2];
  8435. spec->adcs[0] = 0x7; /* Rear Mic / Line-in */
  8436. spec->adcs[1] = 0x8; /* Front Mic, but only if no DSP */
  8437. spec->adcs[2] = 0xa; /* what u hear */
  8438. spec->num_inputs = 2;
  8439. spec->input_pins[0] = 0x12; /* Rear Mic / Line-in */
  8440. spec->input_pins[1] = 0x13; /* What U Hear */
  8441. spec->shared_mic_nid = 0x7;
  8442. spec->unsol_tag_amic1 = spec->input_pins[0];
  8443. /* SPDIF I/O */
  8444. spec->dig_out = 0x05;
  8445. spec->multiout.dig_out_nid = spec->dig_out;
  8446. break;
  8447. case QUIRK_R3DI:
  8448. spec->num_outputs = 2;
  8449. spec->out_pins[0] = 0x0B; /* Line out */
  8450. spec->out_pins[1] = 0x0F; /* Rear headphone out */
  8451. spec->out_pins[2] = 0x10; /* Front Headphone / Center/LFE*/
  8452. spec->out_pins[3] = 0x11; /* Rear surround */
  8453. spec->shared_out_nid = 0x2;
  8454. spec->unsol_tag_hp = spec->out_pins[1];
  8455. spec->unsol_tag_front_hp = spec->out_pins[2];
  8456. spec->adcs[0] = 0x07; /* Rear Mic / Line-in */
  8457. spec->adcs[1] = 0x08; /* Front Mic, but only if no DSP */
  8458. spec->adcs[2] = 0x0a; /* what u hear */
  8459. spec->num_inputs = 2;
  8460. spec->input_pins[0] = 0x12; /* Rear Mic / Line-in */
  8461. spec->input_pins[1] = 0x13; /* What U Hear */
  8462. spec->shared_mic_nid = 0x7;
  8463. spec->unsol_tag_amic1 = spec->input_pins[0];
  8464. /* SPDIF I/O */
  8465. spec->dig_out = 0x05;
  8466. spec->multiout.dig_out_nid = spec->dig_out;
  8467. break;
  8468. default:
  8469. spec->num_outputs = 2;
  8470. spec->out_pins[0] = 0x0b; /* speaker out */
  8471. spec->out_pins[1] = 0x10; /* headphone out */
  8472. spec->shared_out_nid = 0x2;
  8473. spec->unsol_tag_hp = spec->out_pins[1];
  8474. spec->adcs[0] = 0x7; /* digital mic / analog mic1 */
  8475. spec->adcs[1] = 0x8; /* analog mic2 */
  8476. spec->adcs[2] = 0xa; /* what u hear */
  8477. spec->num_inputs = 3;
  8478. spec->input_pins[0] = 0x12;
  8479. spec->input_pins[1] = 0x11;
  8480. spec->input_pins[2] = 0x13;
  8481. spec->shared_mic_nid = 0x7;
  8482. spec->unsol_tag_amic1 = spec->input_pins[0];
  8483. /* SPDIF I/O */
  8484. spec->dig_out = 0x05;
  8485. spec->multiout.dig_out_nid = spec->dig_out;
  8486. spec->dig_in = 0x09;
  8487. break;
  8488. }
  8489. }
  8490. static int ca0132_prepare_verbs(struct hda_codec *codec)
  8491. {
  8492. /* Verbs + terminator (an empty element) */
  8493. #define NUM_SPEC_VERBS 2
  8494. struct ca0132_spec *spec = codec->spec;
  8495. spec->chip_init_verbs = ca0132_init_verbs0;
  8496. /*
  8497. * Since desktop cards use pci_mmio, this can be used to determine
  8498. * whether or not to use these verbs instead of a separate bool.
  8499. */
  8500. if (ca0132_use_pci_mmio(spec))
  8501. spec->desktop_init_verbs = ca0132_init_verbs1;
  8502. spec->spec_init_verbs = kcalloc(NUM_SPEC_VERBS,
  8503. sizeof(struct hda_verb),
  8504. GFP_KERNEL);
  8505. if (!spec->spec_init_verbs)
  8506. return -ENOMEM;
  8507. /* config EAPD */
  8508. spec->spec_init_verbs[0].nid = 0x0b;
  8509. spec->spec_init_verbs[0].param = 0x78D;
  8510. spec->spec_init_verbs[0].verb = 0x00;
  8511. /* Previously commented configuration */
  8512. /*
  8513. spec->spec_init_verbs[2].nid = 0x0b;
  8514. spec->spec_init_verbs[2].param = AC_VERB_SET_EAPD_BTLENABLE;
  8515. spec->spec_init_verbs[2].verb = 0x02;
  8516. spec->spec_init_verbs[3].nid = 0x10;
  8517. spec->spec_init_verbs[3].param = 0x78D;
  8518. spec->spec_init_verbs[3].verb = 0x02;
  8519. spec->spec_init_verbs[4].nid = 0x10;
  8520. spec->spec_init_verbs[4].param = AC_VERB_SET_EAPD_BTLENABLE;
  8521. spec->spec_init_verbs[4].verb = 0x02;
  8522. */
  8523. /* Terminator: spec->spec_init_verbs[NUM_SPEC_VERBS-1] */
  8524. return 0;
  8525. }
  8526. /*
  8527. * The Sound Blaster ZxR shares the same PCI subsystem ID as some regular
  8528. * Sound Blaster Z cards. However, they have different HDA codec subsystem
  8529. * ID's. So, we check for the ZxR's subsystem ID, as well as the DBPro
  8530. * daughter boards ID.
  8531. */
  8532. static void sbz_detect_quirk(struct hda_codec *codec)
  8533. {
  8534. struct ca0132_spec *spec = codec->spec;
  8535. switch (codec->core.subsystem_id) {
  8536. case 0x11020033:
  8537. spec->quirk = QUIRK_ZXR;
  8538. break;
  8539. case 0x1102003f:
  8540. spec->quirk = QUIRK_ZXR_DBPRO;
  8541. break;
  8542. default:
  8543. spec->quirk = QUIRK_SBZ;
  8544. break;
  8545. }
  8546. }
  8547. static int patch_ca0132(struct hda_codec *codec)
  8548. {
  8549. struct ca0132_spec *spec;
  8550. int err;
  8551. const struct snd_pci_quirk *quirk;
  8552. codec_dbg(codec, "patch_ca0132\n");
  8553. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  8554. if (!spec)
  8555. return -ENOMEM;
  8556. codec->spec = spec;
  8557. spec->codec = codec;
  8558. /* Detect codec quirk */
  8559. quirk = snd_pci_quirk_lookup(codec->bus->pci, ca0132_quirks);
  8560. if (quirk)
  8561. spec->quirk = quirk->value;
  8562. else
  8563. spec->quirk = QUIRK_NONE;
  8564. if (ca0132_quirk(spec) == QUIRK_SBZ)
  8565. sbz_detect_quirk(codec);
  8566. if (ca0132_quirk(spec) == QUIRK_ZXR_DBPRO)
  8567. codec->patch_ops = dbpro_patch_ops;
  8568. else
  8569. codec->patch_ops = ca0132_patch_ops;
  8570. codec->pcm_format_first = 1;
  8571. codec->no_sticky_stream = 1;
  8572. spec->dsp_state = DSP_DOWNLOAD_INIT;
  8573. spec->num_mixers = 1;
  8574. /* Set which mixers each quirk uses. */
  8575. switch (ca0132_quirk(spec)) {
  8576. case QUIRK_SBZ:
  8577. spec->mixers[0] = desktop_mixer;
  8578. snd_hda_codec_set_name(codec, "Sound Blaster Z");
  8579. break;
  8580. case QUIRK_ZXR:
  8581. spec->mixers[0] = desktop_mixer;
  8582. snd_hda_codec_set_name(codec, "Sound Blaster ZxR");
  8583. break;
  8584. case QUIRK_ZXR_DBPRO:
  8585. break;
  8586. case QUIRK_R3D:
  8587. spec->mixers[0] = desktop_mixer;
  8588. snd_hda_codec_set_name(codec, "Recon3D");
  8589. break;
  8590. case QUIRK_R3DI:
  8591. spec->mixers[0] = r3di_mixer;
  8592. snd_hda_codec_set_name(codec, "Recon3Di");
  8593. break;
  8594. case QUIRK_AE5:
  8595. spec->mixers[0] = desktop_mixer;
  8596. snd_hda_codec_set_name(codec, "Sound BlasterX AE-5");
  8597. break;
  8598. case QUIRK_AE7:
  8599. spec->mixers[0] = desktop_mixer;
  8600. snd_hda_codec_set_name(codec, "Sound Blaster AE-7");
  8601. break;
  8602. default:
  8603. spec->mixers[0] = ca0132_mixer;
  8604. break;
  8605. }
  8606. /* Setup whether or not to use alt functions/controls/pci_mmio */
  8607. switch (ca0132_quirk(spec)) {
  8608. case QUIRK_SBZ:
  8609. case QUIRK_R3D:
  8610. case QUIRK_AE5:
  8611. case QUIRK_AE7:
  8612. case QUIRK_ZXR:
  8613. spec->use_alt_controls = true;
  8614. spec->use_alt_functions = true;
  8615. spec->use_pci_mmio = true;
  8616. break;
  8617. case QUIRK_R3DI:
  8618. spec->use_alt_controls = true;
  8619. spec->use_alt_functions = true;
  8620. spec->use_pci_mmio = false;
  8621. break;
  8622. default:
  8623. spec->use_alt_controls = false;
  8624. spec->use_alt_functions = false;
  8625. spec->use_pci_mmio = false;
  8626. break;
  8627. }
  8628. #ifdef CONFIG_PCI
  8629. if (spec->use_pci_mmio) {
  8630. spec->mem_base = pci_iomap(codec->bus->pci, 2, 0xC20);
  8631. if (spec->mem_base == NULL) {
  8632. codec_warn(codec, "pci_iomap failed! Setting quirk to QUIRK_NONE.");
  8633. spec->quirk = QUIRK_NONE;
  8634. }
  8635. }
  8636. #endif
  8637. spec->base_init_verbs = ca0132_base_init_verbs;
  8638. spec->base_exit_verbs = ca0132_base_exit_verbs;
  8639. INIT_DELAYED_WORK(&spec->unsol_hp_work, ca0132_unsol_hp_delayed);
  8640. ca0132_init_chip(codec);
  8641. ca0132_config(codec);
  8642. err = ca0132_prepare_verbs(codec);
  8643. if (err < 0)
  8644. goto error;
  8645. err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
  8646. if (err < 0)
  8647. goto error;
  8648. ca0132_setup_unsol(codec);
  8649. return 0;
  8650. error:
  8651. ca0132_free(codec);
  8652. return err;
  8653. }
  8654. /*
  8655. * patch entries
  8656. */
  8657. static const struct hda_device_id snd_hda_id_ca0132[] = {
  8658. HDA_CODEC_ENTRY(0x11020011, "CA0132", patch_ca0132),
  8659. {} /* terminator */
  8660. };
  8661. MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_ca0132);
  8662. MODULE_LICENSE("GPL");
  8663. MODULE_DESCRIPTION("Creative Sound Core3D codec");
  8664. static struct hda_codec_driver ca0132_driver = {
  8665. .id = snd_hda_id_ca0132,
  8666. };
  8667. module_hda_codec_driver(ca0132_driver);