ca0132.c 278 KB

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