patch_ca0132.c 207 KB

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