patch_ca0132.c 125 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806
  1. /*
  2. * HD audio interface patch for Creative CA0132 chip
  3. *
  4. * Copyright (c) 2011, Creative Technology Ltd.
  5. *
  6. * Based on patch_ca0110.c
  7. * Copyright (c) 2008 Takashi Iwai <tiwai@suse.de>
  8. *
  9. * This driver is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This driver is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. #include <linux/init.h>
  24. #include <linux/delay.h>
  25. #include <linux/slab.h>
  26. #include <linux/mutex.h>
  27. #include <linux/module.h>
  28. #include <linux/firmware.h>
  29. #include <sound/core.h>
  30. #include "hda_codec.h"
  31. #include "hda_local.h"
  32. #include "hda_auto_parser.h"
  33. #include "hda_jack.h"
  34. #include "ca0132_regs.h"
  35. /* Enable this to see controls for tuning purpose. */
  36. /*#define ENABLE_TUNING_CONTROLS*/
  37. #ifdef ENABLE_TUNING_CONTROLS
  38. #include <sound/tlv.h>
  39. #endif
  40. #define FLOAT_ZERO 0x00000000
  41. #define FLOAT_ONE 0x3f800000
  42. #define FLOAT_TWO 0x40000000
  43. #define FLOAT_MINUS_5 0xc0a00000
  44. #define UNSOL_TAG_DSP 0x16
  45. #define DSP_DMA_WRITE_BUFLEN_INIT (1UL<<18)
  46. #define DSP_DMA_WRITE_BUFLEN_OVLY (1UL<<15)
  47. #define DMA_TRANSFER_FRAME_SIZE_NWORDS 8
  48. #define DMA_TRANSFER_MAX_FRAME_SIZE_NWORDS 32
  49. #define DMA_OVERLAY_FRAME_SIZE_NWORDS 2
  50. #define MASTERCONTROL 0x80
  51. #define MASTERCONTROL_ALLOC_DMA_CHAN 10
  52. #define MASTERCONTROL_QUERY_SPEAKER_EQ_ADDRESS 60
  53. #define WIDGET_CHIP_CTRL 0x15
  54. #define WIDGET_DSP_CTRL 0x16
  55. #define MEM_CONNID_MICIN1 3
  56. #define MEM_CONNID_MICIN2 5
  57. #define MEM_CONNID_MICOUT1 12
  58. #define MEM_CONNID_MICOUT2 14
  59. #define MEM_CONNID_WUH 10
  60. #define MEM_CONNID_DSP 16
  61. #define MEM_CONNID_DMIC 100
  62. #define SCP_SET 0
  63. #define SCP_GET 1
  64. #define EFX_FILE "ctefx.bin"
  65. #ifdef CONFIG_SND_HDA_CODEC_CA0132_DSP
  66. MODULE_FIRMWARE(EFX_FILE);
  67. #endif
  68. static char *dirstr[2] = { "Playback", "Capture" };
  69. enum {
  70. SPEAKER_OUT,
  71. HEADPHONE_OUT
  72. };
  73. enum {
  74. DIGITAL_MIC,
  75. LINE_MIC_IN
  76. };
  77. enum {
  78. #define VNODE_START_NID 0x80
  79. VNID_SPK = VNODE_START_NID, /* Speaker vnid */
  80. VNID_MIC,
  81. VNID_HP_SEL,
  82. VNID_AMIC1_SEL,
  83. VNID_HP_ASEL,
  84. VNID_AMIC1_ASEL,
  85. VNODE_END_NID,
  86. #define VNODES_COUNT (VNODE_END_NID - VNODE_START_NID)
  87. #define EFFECT_START_NID 0x90
  88. #define OUT_EFFECT_START_NID EFFECT_START_NID
  89. SURROUND = OUT_EFFECT_START_NID,
  90. CRYSTALIZER,
  91. DIALOG_PLUS,
  92. SMART_VOLUME,
  93. X_BASS,
  94. EQUALIZER,
  95. OUT_EFFECT_END_NID,
  96. #define OUT_EFFECTS_COUNT (OUT_EFFECT_END_NID - OUT_EFFECT_START_NID)
  97. #define IN_EFFECT_START_NID OUT_EFFECT_END_NID
  98. ECHO_CANCELLATION = IN_EFFECT_START_NID,
  99. VOICE_FOCUS,
  100. MIC_SVM,
  101. NOISE_REDUCTION,
  102. IN_EFFECT_END_NID,
  103. #define IN_EFFECTS_COUNT (IN_EFFECT_END_NID - IN_EFFECT_START_NID)
  104. VOICEFX = IN_EFFECT_END_NID,
  105. PLAY_ENHANCEMENT,
  106. CRYSTAL_VOICE,
  107. EFFECT_END_NID
  108. #define EFFECTS_COUNT (EFFECT_END_NID - EFFECT_START_NID)
  109. };
  110. /* Effects values size*/
  111. #define EFFECT_VALS_MAX_COUNT 12
  112. /* Latency introduced by DSP blocks in milliseconds. */
  113. #define DSP_CAPTURE_INIT_LATENCY 0
  114. #define DSP_CRYSTAL_VOICE_LATENCY 124
  115. #define DSP_PLAYBACK_INIT_LATENCY 13
  116. #define DSP_PLAY_ENHANCEMENT_LATENCY 30
  117. #define DSP_SPEAKER_OUT_LATENCY 7
  118. struct ct_effect {
  119. char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
  120. hda_nid_t nid;
  121. int mid; /*effect module ID*/
  122. int reqs[EFFECT_VALS_MAX_COUNT]; /*effect module request*/
  123. int direct; /* 0:output; 1:input*/
  124. int params; /* number of default non-on/off params */
  125. /*effect default values, 1st is on/off. */
  126. unsigned int def_vals[EFFECT_VALS_MAX_COUNT];
  127. };
  128. #define EFX_DIR_OUT 0
  129. #define EFX_DIR_IN 1
  130. static struct ct_effect ca0132_effects[EFFECTS_COUNT] = {
  131. { .name = "Surround",
  132. .nid = SURROUND,
  133. .mid = 0x96,
  134. .reqs = {0, 1},
  135. .direct = EFX_DIR_OUT,
  136. .params = 1,
  137. .def_vals = {0x3F800000, 0x3F2B851F}
  138. },
  139. { .name = "Crystalizer",
  140. .nid = CRYSTALIZER,
  141. .mid = 0x96,
  142. .reqs = {7, 8},
  143. .direct = EFX_DIR_OUT,
  144. .params = 1,
  145. .def_vals = {0x3F800000, 0x3F266666}
  146. },
  147. { .name = "Dialog Plus",
  148. .nid = DIALOG_PLUS,
  149. .mid = 0x96,
  150. .reqs = {2, 3},
  151. .direct = EFX_DIR_OUT,
  152. .params = 1,
  153. .def_vals = {0x00000000, 0x3F000000}
  154. },
  155. { .name = "Smart Volume",
  156. .nid = SMART_VOLUME,
  157. .mid = 0x96,
  158. .reqs = {4, 5, 6},
  159. .direct = EFX_DIR_OUT,
  160. .params = 2,
  161. .def_vals = {0x3F800000, 0x3F3D70A4, 0x00000000}
  162. },
  163. { .name = "X-Bass",
  164. .nid = X_BASS,
  165. .mid = 0x96,
  166. .reqs = {24, 23, 25},
  167. .direct = EFX_DIR_OUT,
  168. .params = 2,
  169. .def_vals = {0x3F800000, 0x42A00000, 0x3F000000}
  170. },
  171. { .name = "Equalizer",
  172. .nid = EQUALIZER,
  173. .mid = 0x96,
  174. .reqs = {9, 10, 11, 12, 13, 14,
  175. 15, 16, 17, 18, 19, 20},
  176. .direct = EFX_DIR_OUT,
  177. .params = 11,
  178. .def_vals = {0x00000000, 0x00000000, 0x00000000, 0x00000000,
  179. 0x00000000, 0x00000000, 0x00000000, 0x00000000,
  180. 0x00000000, 0x00000000, 0x00000000, 0x00000000}
  181. },
  182. { .name = "Echo Cancellation",
  183. .nid = ECHO_CANCELLATION,
  184. .mid = 0x95,
  185. .reqs = {0, 1, 2, 3},
  186. .direct = EFX_DIR_IN,
  187. .params = 3,
  188. .def_vals = {0x00000000, 0x3F3A9692, 0x00000000, 0x00000000}
  189. },
  190. { .name = "Voice Focus",
  191. .nid = VOICE_FOCUS,
  192. .mid = 0x95,
  193. .reqs = {6, 7, 8, 9},
  194. .direct = EFX_DIR_IN,
  195. .params = 3,
  196. .def_vals = {0x3F800000, 0x3D7DF3B6, 0x41F00000, 0x41F00000}
  197. },
  198. { .name = "Mic SVM",
  199. .nid = MIC_SVM,
  200. .mid = 0x95,
  201. .reqs = {44, 45},
  202. .direct = EFX_DIR_IN,
  203. .params = 1,
  204. .def_vals = {0x00000000, 0x3F3D70A4}
  205. },
  206. { .name = "Noise Reduction",
  207. .nid = NOISE_REDUCTION,
  208. .mid = 0x95,
  209. .reqs = {4, 5},
  210. .direct = EFX_DIR_IN,
  211. .params = 1,
  212. .def_vals = {0x3F800000, 0x3F000000}
  213. },
  214. { .name = "VoiceFX",
  215. .nid = VOICEFX,
  216. .mid = 0x95,
  217. .reqs = {10, 11, 12, 13, 14, 15, 16, 17, 18},
  218. .direct = EFX_DIR_IN,
  219. .params = 8,
  220. .def_vals = {0x00000000, 0x43C80000, 0x44AF0000, 0x44FA0000,
  221. 0x3F800000, 0x3F800000, 0x3F800000, 0x00000000,
  222. 0x00000000}
  223. }
  224. };
  225. /* Tuning controls */
  226. #ifdef ENABLE_TUNING_CONTROLS
  227. enum {
  228. #define TUNING_CTL_START_NID 0xC0
  229. WEDGE_ANGLE = TUNING_CTL_START_NID,
  230. SVM_LEVEL,
  231. EQUALIZER_BAND_0,
  232. EQUALIZER_BAND_1,
  233. EQUALIZER_BAND_2,
  234. EQUALIZER_BAND_3,
  235. EQUALIZER_BAND_4,
  236. EQUALIZER_BAND_5,
  237. EQUALIZER_BAND_6,
  238. EQUALIZER_BAND_7,
  239. EQUALIZER_BAND_8,
  240. EQUALIZER_BAND_9,
  241. TUNING_CTL_END_NID
  242. #define TUNING_CTLS_COUNT (TUNING_CTL_END_NID - TUNING_CTL_START_NID)
  243. };
  244. struct ct_tuning_ctl {
  245. char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
  246. hda_nid_t parent_nid;
  247. hda_nid_t nid;
  248. int mid; /*effect module ID*/
  249. int req; /*effect module request*/
  250. int direct; /* 0:output; 1:input*/
  251. unsigned int def_val;/*effect default values*/
  252. };
  253. static struct ct_tuning_ctl ca0132_tuning_ctls[] = {
  254. { .name = "Wedge Angle",
  255. .parent_nid = VOICE_FOCUS,
  256. .nid = WEDGE_ANGLE,
  257. .mid = 0x95,
  258. .req = 8,
  259. .direct = EFX_DIR_IN,
  260. .def_val = 0x41F00000
  261. },
  262. { .name = "SVM Level",
  263. .parent_nid = MIC_SVM,
  264. .nid = SVM_LEVEL,
  265. .mid = 0x95,
  266. .req = 45,
  267. .direct = EFX_DIR_IN,
  268. .def_val = 0x3F3D70A4
  269. },
  270. { .name = "EQ Band0",
  271. .parent_nid = EQUALIZER,
  272. .nid = EQUALIZER_BAND_0,
  273. .mid = 0x96,
  274. .req = 11,
  275. .direct = EFX_DIR_OUT,
  276. .def_val = 0x00000000
  277. },
  278. { .name = "EQ Band1",
  279. .parent_nid = EQUALIZER,
  280. .nid = EQUALIZER_BAND_1,
  281. .mid = 0x96,
  282. .req = 12,
  283. .direct = EFX_DIR_OUT,
  284. .def_val = 0x00000000
  285. },
  286. { .name = "EQ Band2",
  287. .parent_nid = EQUALIZER,
  288. .nid = EQUALIZER_BAND_2,
  289. .mid = 0x96,
  290. .req = 13,
  291. .direct = EFX_DIR_OUT,
  292. .def_val = 0x00000000
  293. },
  294. { .name = "EQ Band3",
  295. .parent_nid = EQUALIZER,
  296. .nid = EQUALIZER_BAND_3,
  297. .mid = 0x96,
  298. .req = 14,
  299. .direct = EFX_DIR_OUT,
  300. .def_val = 0x00000000
  301. },
  302. { .name = "EQ Band4",
  303. .parent_nid = EQUALIZER,
  304. .nid = EQUALIZER_BAND_4,
  305. .mid = 0x96,
  306. .req = 15,
  307. .direct = EFX_DIR_OUT,
  308. .def_val = 0x00000000
  309. },
  310. { .name = "EQ Band5",
  311. .parent_nid = EQUALIZER,
  312. .nid = EQUALIZER_BAND_5,
  313. .mid = 0x96,
  314. .req = 16,
  315. .direct = EFX_DIR_OUT,
  316. .def_val = 0x00000000
  317. },
  318. { .name = "EQ Band6",
  319. .parent_nid = EQUALIZER,
  320. .nid = EQUALIZER_BAND_6,
  321. .mid = 0x96,
  322. .req = 17,
  323. .direct = EFX_DIR_OUT,
  324. .def_val = 0x00000000
  325. },
  326. { .name = "EQ Band7",
  327. .parent_nid = EQUALIZER,
  328. .nid = EQUALIZER_BAND_7,
  329. .mid = 0x96,
  330. .req = 18,
  331. .direct = EFX_DIR_OUT,
  332. .def_val = 0x00000000
  333. },
  334. { .name = "EQ Band8",
  335. .parent_nid = EQUALIZER,
  336. .nid = EQUALIZER_BAND_8,
  337. .mid = 0x96,
  338. .req = 19,
  339. .direct = EFX_DIR_OUT,
  340. .def_val = 0x00000000
  341. },
  342. { .name = "EQ Band9",
  343. .parent_nid = EQUALIZER,
  344. .nid = EQUALIZER_BAND_9,
  345. .mid = 0x96,
  346. .req = 20,
  347. .direct = EFX_DIR_OUT,
  348. .def_val = 0x00000000
  349. }
  350. };
  351. #endif
  352. /* Voice FX Presets */
  353. #define VOICEFX_MAX_PARAM_COUNT 9
  354. struct ct_voicefx {
  355. char *name;
  356. hda_nid_t nid;
  357. int mid;
  358. int reqs[VOICEFX_MAX_PARAM_COUNT]; /*effect module request*/
  359. };
  360. struct ct_voicefx_preset {
  361. char *name; /*preset name*/
  362. unsigned int vals[VOICEFX_MAX_PARAM_COUNT];
  363. };
  364. static struct ct_voicefx ca0132_voicefx = {
  365. .name = "VoiceFX Capture Switch",
  366. .nid = VOICEFX,
  367. .mid = 0x95,
  368. .reqs = {10, 11, 12, 13, 14, 15, 16, 17, 18}
  369. };
  370. static struct ct_voicefx_preset ca0132_voicefx_presets[] = {
  371. { .name = "Neutral",
  372. .vals = { 0x00000000, 0x43C80000, 0x44AF0000,
  373. 0x44FA0000, 0x3F800000, 0x3F800000,
  374. 0x3F800000, 0x00000000, 0x00000000 }
  375. },
  376. { .name = "Female2Male",
  377. .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
  378. 0x44FA0000, 0x3F19999A, 0x3F866666,
  379. 0x3F800000, 0x00000000, 0x00000000 }
  380. },
  381. { .name = "Male2Female",
  382. .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
  383. 0x450AC000, 0x4017AE14, 0x3F6B851F,
  384. 0x3F800000, 0x00000000, 0x00000000 }
  385. },
  386. { .name = "ScrappyKid",
  387. .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
  388. 0x44FA0000, 0x40400000, 0x3F28F5C3,
  389. 0x3F800000, 0x00000000, 0x00000000 }
  390. },
  391. { .name = "Elderly",
  392. .vals = { 0x3F800000, 0x44324000, 0x44BB8000,
  393. 0x44E10000, 0x3FB33333, 0x3FB9999A,
  394. 0x3F800000, 0x3E3A2E43, 0x00000000 }
  395. },
  396. { .name = "Orc",
  397. .vals = { 0x3F800000, 0x43EA0000, 0x44A52000,
  398. 0x45098000, 0x3F266666, 0x3FC00000,
  399. 0x3F800000, 0x00000000, 0x00000000 }
  400. },
  401. { .name = "Elf",
  402. .vals = { 0x3F800000, 0x43C70000, 0x44AE6000,
  403. 0x45193000, 0x3F8E147B, 0x3F75C28F,
  404. 0x3F800000, 0x00000000, 0x00000000 }
  405. },
  406. { .name = "Dwarf",
  407. .vals = { 0x3F800000, 0x43930000, 0x44BEE000,
  408. 0x45007000, 0x3F451EB8, 0x3F7851EC,
  409. 0x3F800000, 0x00000000, 0x00000000 }
  410. },
  411. { .name = "AlienBrute",
  412. .vals = { 0x3F800000, 0x43BFC5AC, 0x44B28FDF,
  413. 0x451F6000, 0x3F266666, 0x3FA7D945,
  414. 0x3F800000, 0x3CF5C28F, 0x00000000 }
  415. },
  416. { .name = "Robot",
  417. .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
  418. 0x44FA0000, 0x3FB2718B, 0x3F800000,
  419. 0xBC07010E, 0x00000000, 0x00000000 }
  420. },
  421. { .name = "Marine",
  422. .vals = { 0x3F800000, 0x43C20000, 0x44906000,
  423. 0x44E70000, 0x3F4CCCCD, 0x3F8A3D71,
  424. 0x3F0A3D71, 0x00000000, 0x00000000 }
  425. },
  426. { .name = "Emo",
  427. .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
  428. 0x44FA0000, 0x3F800000, 0x3F800000,
  429. 0x3E4CCCCD, 0x00000000, 0x00000000 }
  430. },
  431. { .name = "DeepVoice",
  432. .vals = { 0x3F800000, 0x43A9C5AC, 0x44AA4FDF,
  433. 0x44FFC000, 0x3EDBB56F, 0x3F99C4CA,
  434. 0x3F800000, 0x00000000, 0x00000000 }
  435. },
  436. { .name = "Munchkin",
  437. .vals = { 0x3F800000, 0x43C80000, 0x44AF0000,
  438. 0x44FA0000, 0x3F800000, 0x3F1A043C,
  439. 0x3F800000, 0x00000000, 0x00000000 }
  440. }
  441. };
  442. enum hda_cmd_vendor_io {
  443. /* for DspIO node */
  444. VENDOR_DSPIO_SCP_WRITE_DATA_LOW = 0x000,
  445. VENDOR_DSPIO_SCP_WRITE_DATA_HIGH = 0x100,
  446. VENDOR_DSPIO_STATUS = 0xF01,
  447. VENDOR_DSPIO_SCP_POST_READ_DATA = 0x702,
  448. VENDOR_DSPIO_SCP_READ_DATA = 0xF02,
  449. VENDOR_DSPIO_DSP_INIT = 0x703,
  450. VENDOR_DSPIO_SCP_POST_COUNT_QUERY = 0x704,
  451. VENDOR_DSPIO_SCP_READ_COUNT = 0xF04,
  452. /* for ChipIO node */
  453. VENDOR_CHIPIO_ADDRESS_LOW = 0x000,
  454. VENDOR_CHIPIO_ADDRESS_HIGH = 0x100,
  455. VENDOR_CHIPIO_STREAM_FORMAT = 0x200,
  456. VENDOR_CHIPIO_DATA_LOW = 0x300,
  457. VENDOR_CHIPIO_DATA_HIGH = 0x400,
  458. VENDOR_CHIPIO_GET_PARAMETER = 0xF00,
  459. VENDOR_CHIPIO_STATUS = 0xF01,
  460. VENDOR_CHIPIO_HIC_POST_READ = 0x702,
  461. VENDOR_CHIPIO_HIC_READ_DATA = 0xF03,
  462. VENDOR_CHIPIO_8051_DATA_WRITE = 0x707,
  463. VENDOR_CHIPIO_8051_DATA_READ = 0xF07,
  464. VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE = 0x70A,
  465. VENDOR_CHIPIO_CT_EXTENSIONS_GET = 0xF0A,
  466. VENDOR_CHIPIO_PLL_PMU_WRITE = 0x70C,
  467. VENDOR_CHIPIO_PLL_PMU_READ = 0xF0C,
  468. VENDOR_CHIPIO_8051_ADDRESS_LOW = 0x70D,
  469. VENDOR_CHIPIO_8051_ADDRESS_HIGH = 0x70E,
  470. VENDOR_CHIPIO_FLAG_SET = 0x70F,
  471. VENDOR_CHIPIO_FLAGS_GET = 0xF0F,
  472. VENDOR_CHIPIO_PARAM_SET = 0x710,
  473. VENDOR_CHIPIO_PARAM_GET = 0xF10,
  474. VENDOR_CHIPIO_PORT_ALLOC_CONFIG_SET = 0x711,
  475. VENDOR_CHIPIO_PORT_ALLOC_SET = 0x712,
  476. VENDOR_CHIPIO_PORT_ALLOC_GET = 0xF12,
  477. VENDOR_CHIPIO_PORT_FREE_SET = 0x713,
  478. VENDOR_CHIPIO_PARAM_EX_ID_GET = 0xF17,
  479. VENDOR_CHIPIO_PARAM_EX_ID_SET = 0x717,
  480. VENDOR_CHIPIO_PARAM_EX_VALUE_GET = 0xF18,
  481. VENDOR_CHIPIO_PARAM_EX_VALUE_SET = 0x718,
  482. VENDOR_CHIPIO_DMIC_CTL_SET = 0x788,
  483. VENDOR_CHIPIO_DMIC_CTL_GET = 0xF88,
  484. VENDOR_CHIPIO_DMIC_PIN_SET = 0x789,
  485. VENDOR_CHIPIO_DMIC_PIN_GET = 0xF89,
  486. VENDOR_CHIPIO_DMIC_MCLK_SET = 0x78A,
  487. VENDOR_CHIPIO_DMIC_MCLK_GET = 0xF8A,
  488. VENDOR_CHIPIO_EAPD_SEL_SET = 0x78D
  489. };
  490. /*
  491. * Control flag IDs
  492. */
  493. enum control_flag_id {
  494. /* Connection manager stream setup is bypassed/enabled */
  495. CONTROL_FLAG_C_MGR = 0,
  496. /* DSP DMA is bypassed/enabled */
  497. CONTROL_FLAG_DMA = 1,
  498. /* 8051 'idle' mode is disabled/enabled */
  499. CONTROL_FLAG_IDLE_ENABLE = 2,
  500. /* Tracker for the SPDIF-in path is bypassed/enabled */
  501. CONTROL_FLAG_TRACKER = 3,
  502. /* DigitalOut to Spdif2Out connection is disabled/enabled */
  503. CONTROL_FLAG_SPDIF2OUT = 4,
  504. /* Digital Microphone is disabled/enabled */
  505. CONTROL_FLAG_DMIC = 5,
  506. /* ADC_B rate is 48 kHz/96 kHz */
  507. CONTROL_FLAG_ADC_B_96KHZ = 6,
  508. /* ADC_C rate is 48 kHz/96 kHz */
  509. CONTROL_FLAG_ADC_C_96KHZ = 7,
  510. /* DAC rate is 48 kHz/96 kHz (affects all DACs) */
  511. CONTROL_FLAG_DAC_96KHZ = 8,
  512. /* DSP rate is 48 kHz/96 kHz */
  513. CONTROL_FLAG_DSP_96KHZ = 9,
  514. /* SRC clock is 98 MHz/196 MHz (196 MHz forces rate to 96 KHz) */
  515. CONTROL_FLAG_SRC_CLOCK_196MHZ = 10,
  516. /* SRC rate is 48 kHz/96 kHz (48 kHz disabled when clock is 196 MHz) */
  517. CONTROL_FLAG_SRC_RATE_96KHZ = 11,
  518. /* Decode Loop (DSP->SRC->DSP) is disabled/enabled */
  519. CONTROL_FLAG_DECODE_LOOP = 12,
  520. /* De-emphasis filter on DAC-1 disabled/enabled */
  521. CONTROL_FLAG_DAC1_DEEMPHASIS = 13,
  522. /* De-emphasis filter on DAC-2 disabled/enabled */
  523. CONTROL_FLAG_DAC2_DEEMPHASIS = 14,
  524. /* De-emphasis filter on DAC-3 disabled/enabled */
  525. CONTROL_FLAG_DAC3_DEEMPHASIS = 15,
  526. /* High-pass filter on ADC_B disabled/enabled */
  527. CONTROL_FLAG_ADC_B_HIGH_PASS = 16,
  528. /* High-pass filter on ADC_C disabled/enabled */
  529. CONTROL_FLAG_ADC_C_HIGH_PASS = 17,
  530. /* Common mode on Port_A disabled/enabled */
  531. CONTROL_FLAG_PORT_A_COMMON_MODE = 18,
  532. /* Common mode on Port_D disabled/enabled */
  533. CONTROL_FLAG_PORT_D_COMMON_MODE = 19,
  534. /* Impedance for ramp generator on Port_A 16 Ohm/10K Ohm */
  535. CONTROL_FLAG_PORT_A_10KOHM_LOAD = 20,
  536. /* Impedance for ramp generator on Port_D, 16 Ohm/10K Ohm */
  537. CONTROL_FLAG_PORT_D_10KOHM_LOAD = 21,
  538. /* ASI rate is 48kHz/96kHz */
  539. CONTROL_FLAG_ASI_96KHZ = 22,
  540. /* DAC power settings able to control attached ports no/yes */
  541. CONTROL_FLAG_DACS_CONTROL_PORTS = 23,
  542. /* Clock Stop OK reporting is disabled/enabled */
  543. CONTROL_FLAG_CONTROL_STOP_OK_ENABLE = 24,
  544. /* Number of control flags */
  545. CONTROL_FLAGS_MAX = (CONTROL_FLAG_CONTROL_STOP_OK_ENABLE+1)
  546. };
  547. /*
  548. * Control parameter IDs
  549. */
  550. enum control_param_id {
  551. /* 0: None, 1: Mic1In*/
  552. CONTROL_PARAM_VIP_SOURCE = 1,
  553. /* 0: force HDA, 1: allow DSP if HDA Spdif1Out stream is idle */
  554. CONTROL_PARAM_SPDIF1_SOURCE = 2,
  555. /* Port A output stage gain setting to use when 16 Ohm output
  556. * impedance is selected*/
  557. CONTROL_PARAM_PORTA_160OHM_GAIN = 8,
  558. /* Port D output stage gain setting to use when 16 Ohm output
  559. * impedance is selected*/
  560. CONTROL_PARAM_PORTD_160OHM_GAIN = 10,
  561. /* Stream Control */
  562. /* Select stream with the given ID */
  563. CONTROL_PARAM_STREAM_ID = 24,
  564. /* Source connection point for the selected stream */
  565. CONTROL_PARAM_STREAM_SOURCE_CONN_POINT = 25,
  566. /* Destination connection point for the selected stream */
  567. CONTROL_PARAM_STREAM_DEST_CONN_POINT = 26,
  568. /* Number of audio channels in the selected stream */
  569. CONTROL_PARAM_STREAMS_CHANNELS = 27,
  570. /*Enable control for the selected stream */
  571. CONTROL_PARAM_STREAM_CONTROL = 28,
  572. /* Connection Point Control */
  573. /* Select connection point with the given ID */
  574. CONTROL_PARAM_CONN_POINT_ID = 29,
  575. /* Connection point sample rate */
  576. CONTROL_PARAM_CONN_POINT_SAMPLE_RATE = 30,
  577. /* Node Control */
  578. /* Select HDA node with the given ID */
  579. CONTROL_PARAM_NODE_ID = 31
  580. };
  581. /*
  582. * Dsp Io Status codes
  583. */
  584. enum hda_vendor_status_dspio {
  585. /* Success */
  586. VENDOR_STATUS_DSPIO_OK = 0x00,
  587. /* Busy, unable to accept new command, the host must retry */
  588. VENDOR_STATUS_DSPIO_BUSY = 0x01,
  589. /* SCP command queue is full */
  590. VENDOR_STATUS_DSPIO_SCP_COMMAND_QUEUE_FULL = 0x02,
  591. /* SCP response queue is empty */
  592. VENDOR_STATUS_DSPIO_SCP_RESPONSE_QUEUE_EMPTY = 0x03
  593. };
  594. /*
  595. * Chip Io Status codes
  596. */
  597. enum hda_vendor_status_chipio {
  598. /* Success */
  599. VENDOR_STATUS_CHIPIO_OK = 0x00,
  600. /* Busy, unable to accept new command, the host must retry */
  601. VENDOR_STATUS_CHIPIO_BUSY = 0x01
  602. };
  603. /*
  604. * CA0132 sample rate
  605. */
  606. enum ca0132_sample_rate {
  607. SR_6_000 = 0x00,
  608. SR_8_000 = 0x01,
  609. SR_9_600 = 0x02,
  610. SR_11_025 = 0x03,
  611. SR_16_000 = 0x04,
  612. SR_22_050 = 0x05,
  613. SR_24_000 = 0x06,
  614. SR_32_000 = 0x07,
  615. SR_44_100 = 0x08,
  616. SR_48_000 = 0x09,
  617. SR_88_200 = 0x0A,
  618. SR_96_000 = 0x0B,
  619. SR_144_000 = 0x0C,
  620. SR_176_400 = 0x0D,
  621. SR_192_000 = 0x0E,
  622. SR_384_000 = 0x0F,
  623. SR_COUNT = 0x10,
  624. SR_RATE_UNKNOWN = 0x1F
  625. };
  626. enum dsp_download_state {
  627. DSP_DOWNLOAD_FAILED = -1,
  628. DSP_DOWNLOAD_INIT = 0,
  629. DSP_DOWNLOADING = 1,
  630. DSP_DOWNLOADED = 2
  631. };
  632. /* retrieve parameters from hda format */
  633. #define get_hdafmt_chs(fmt) (fmt & 0xf)
  634. #define get_hdafmt_bits(fmt) ((fmt >> 4) & 0x7)
  635. #define get_hdafmt_rate(fmt) ((fmt >> 8) & 0x7f)
  636. #define get_hdafmt_type(fmt) ((fmt >> 15) & 0x1)
  637. /*
  638. * CA0132 specific
  639. */
  640. struct ca0132_spec {
  641. struct snd_kcontrol_new *mixers[5];
  642. unsigned int num_mixers;
  643. const struct hda_verb *base_init_verbs;
  644. const struct hda_verb *base_exit_verbs;
  645. const struct hda_verb *chip_init_verbs;
  646. struct hda_verb *spec_init_verbs;
  647. struct auto_pin_cfg autocfg;
  648. /* Nodes configurations */
  649. struct hda_multi_out multiout;
  650. hda_nid_t out_pins[AUTO_CFG_MAX_OUTS];
  651. hda_nid_t dacs[AUTO_CFG_MAX_OUTS];
  652. unsigned int num_outputs;
  653. hda_nid_t input_pins[AUTO_PIN_LAST];
  654. hda_nid_t adcs[AUTO_PIN_LAST];
  655. hda_nid_t dig_out;
  656. hda_nid_t dig_in;
  657. unsigned int num_inputs;
  658. hda_nid_t shared_mic_nid;
  659. hda_nid_t shared_out_nid;
  660. hda_nid_t unsol_tag_hp;
  661. hda_nid_t unsol_tag_amic1;
  662. /* chip access */
  663. struct mutex chipio_mutex; /* chip access mutex */
  664. u32 curr_chip_addx;
  665. /* DSP download related */
  666. enum dsp_download_state dsp_state;
  667. unsigned int dsp_stream_id;
  668. unsigned int wait_scp;
  669. unsigned int wait_scp_header;
  670. unsigned int wait_num_data;
  671. unsigned int scp_resp_header;
  672. unsigned int scp_resp_data[4];
  673. unsigned int scp_resp_count;
  674. /* mixer and effects related */
  675. unsigned char dmic_ctl;
  676. int cur_out_type;
  677. int cur_mic_type;
  678. long vnode_lvol[VNODES_COUNT];
  679. long vnode_rvol[VNODES_COUNT];
  680. long vnode_lswitch[VNODES_COUNT];
  681. long vnode_rswitch[VNODES_COUNT];
  682. long effects_switch[EFFECTS_COUNT];
  683. long voicefx_val;
  684. long cur_mic_boost;
  685. struct hda_codec *codec;
  686. struct delayed_work unsol_hp_work;
  687. int quirk;
  688. #ifdef ENABLE_TUNING_CONTROLS
  689. long cur_ctl_vals[TUNING_CTLS_COUNT];
  690. #endif
  691. };
  692. /*
  693. * CA0132 quirks table
  694. */
  695. enum {
  696. QUIRK_NONE,
  697. QUIRK_ALIENWARE,
  698. };
  699. static const struct hda_pintbl alienware_pincfgs[] = {
  700. { 0x0b, 0x90170110 }, /* Builtin Speaker */
  701. { 0x0c, 0x411111f0 }, /* N/A */
  702. { 0x0d, 0x411111f0 }, /* N/A */
  703. { 0x0e, 0x411111f0 }, /* N/A */
  704. { 0x0f, 0x0321101f }, /* HP */
  705. { 0x10, 0x411111f0 }, /* Headset? disabled for now */
  706. { 0x11, 0x03a11021 }, /* Mic */
  707. { 0x12, 0xd5a30140 }, /* Builtin Mic */
  708. { 0x13, 0x411111f0 }, /* N/A */
  709. { 0x18, 0x411111f0 }, /* N/A */
  710. {}
  711. };
  712. static const struct snd_pci_quirk ca0132_quirks[] = {
  713. SND_PCI_QUIRK(0x1028, 0x0685, "Alienware 15 2015", QUIRK_ALIENWARE),
  714. SND_PCI_QUIRK(0x1028, 0x0688, "Alienware 17 2015", QUIRK_ALIENWARE),
  715. SND_PCI_QUIRK(0x1028, 0x0708, "Alienware 15 R2 2016", QUIRK_ALIENWARE),
  716. {}
  717. };
  718. /*
  719. * CA0132 codec access
  720. */
  721. static unsigned int codec_send_command(struct hda_codec *codec, hda_nid_t nid,
  722. unsigned int verb, unsigned int parm, unsigned int *res)
  723. {
  724. unsigned int response;
  725. response = snd_hda_codec_read(codec, nid, 0, verb, parm);
  726. *res = response;
  727. return ((response == -1) ? -1 : 0);
  728. }
  729. static int codec_set_converter_format(struct hda_codec *codec, hda_nid_t nid,
  730. unsigned short converter_format, unsigned int *res)
  731. {
  732. return codec_send_command(codec, nid, VENDOR_CHIPIO_STREAM_FORMAT,
  733. converter_format & 0xffff, res);
  734. }
  735. static int codec_set_converter_stream_channel(struct hda_codec *codec,
  736. hda_nid_t nid, unsigned char stream,
  737. unsigned char channel, unsigned int *res)
  738. {
  739. unsigned char converter_stream_channel = 0;
  740. converter_stream_channel = (stream << 4) | (channel & 0x0f);
  741. return codec_send_command(codec, nid, AC_VERB_SET_CHANNEL_STREAMID,
  742. converter_stream_channel, res);
  743. }
  744. /* Chip access helper function */
  745. static int chipio_send(struct hda_codec *codec,
  746. unsigned int reg,
  747. unsigned int data)
  748. {
  749. unsigned int res;
  750. unsigned long timeout = jiffies + msecs_to_jiffies(1000);
  751. /* send bits of data specified by reg */
  752. do {
  753. res = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
  754. reg, data);
  755. if (res == VENDOR_STATUS_CHIPIO_OK)
  756. return 0;
  757. msleep(20);
  758. } while (time_before(jiffies, timeout));
  759. return -EIO;
  760. }
  761. /*
  762. * Write chip address through the vendor widget -- NOT protected by the Mutex!
  763. */
  764. static int chipio_write_address(struct hda_codec *codec,
  765. unsigned int chip_addx)
  766. {
  767. struct ca0132_spec *spec = codec->spec;
  768. int res;
  769. if (spec->curr_chip_addx == chip_addx)
  770. return 0;
  771. /* send low 16 bits of the address */
  772. res = chipio_send(codec, VENDOR_CHIPIO_ADDRESS_LOW,
  773. chip_addx & 0xffff);
  774. if (res != -EIO) {
  775. /* send high 16 bits of the address */
  776. res = chipio_send(codec, VENDOR_CHIPIO_ADDRESS_HIGH,
  777. chip_addx >> 16);
  778. }
  779. spec->curr_chip_addx = (res < 0) ? ~0UL : chip_addx;
  780. return res;
  781. }
  782. /*
  783. * Write data through the vendor widget -- NOT protected by the Mutex!
  784. */
  785. static int chipio_write_data(struct hda_codec *codec, unsigned int data)
  786. {
  787. struct ca0132_spec *spec = codec->spec;
  788. int res;
  789. /* send low 16 bits of the data */
  790. res = chipio_send(codec, VENDOR_CHIPIO_DATA_LOW, data & 0xffff);
  791. if (res != -EIO) {
  792. /* send high 16 bits of the data */
  793. res = chipio_send(codec, VENDOR_CHIPIO_DATA_HIGH,
  794. data >> 16);
  795. }
  796. /*If no error encountered, automatically increment the address
  797. as per chip behaviour*/
  798. spec->curr_chip_addx = (res != -EIO) ?
  799. (spec->curr_chip_addx + 4) : ~0UL;
  800. return res;
  801. }
  802. /*
  803. * Write multiple data through the vendor widget -- NOT protected by the Mutex!
  804. */
  805. static int chipio_write_data_multiple(struct hda_codec *codec,
  806. const u32 *data,
  807. unsigned int count)
  808. {
  809. int status = 0;
  810. if (data == NULL) {
  811. codec_dbg(codec, "chipio_write_data null ptr\n");
  812. return -EINVAL;
  813. }
  814. while ((count-- != 0) && (status == 0))
  815. status = chipio_write_data(codec, *data++);
  816. return status;
  817. }
  818. /*
  819. * Read data through the vendor widget -- NOT protected by the Mutex!
  820. */
  821. static int chipio_read_data(struct hda_codec *codec, unsigned int *data)
  822. {
  823. struct ca0132_spec *spec = codec->spec;
  824. int res;
  825. /* post read */
  826. res = chipio_send(codec, VENDOR_CHIPIO_HIC_POST_READ, 0);
  827. if (res != -EIO) {
  828. /* read status */
  829. res = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
  830. }
  831. if (res != -EIO) {
  832. /* read data */
  833. *data = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
  834. VENDOR_CHIPIO_HIC_READ_DATA,
  835. 0);
  836. }
  837. /*If no error encountered, automatically increment the address
  838. as per chip behaviour*/
  839. spec->curr_chip_addx = (res != -EIO) ?
  840. (spec->curr_chip_addx + 4) : ~0UL;
  841. return res;
  842. }
  843. /*
  844. * Write given value to the given address through the chip I/O widget.
  845. * protected by the Mutex
  846. */
  847. static int chipio_write(struct hda_codec *codec,
  848. unsigned int chip_addx, const unsigned int data)
  849. {
  850. struct ca0132_spec *spec = codec->spec;
  851. int err;
  852. mutex_lock(&spec->chipio_mutex);
  853. /* write the address, and if successful proceed to write data */
  854. err = chipio_write_address(codec, chip_addx);
  855. if (err < 0)
  856. goto exit;
  857. err = chipio_write_data(codec, data);
  858. if (err < 0)
  859. goto exit;
  860. exit:
  861. mutex_unlock(&spec->chipio_mutex);
  862. return err;
  863. }
  864. /*
  865. * Write multiple values to the given address through the chip I/O widget.
  866. * protected by the Mutex
  867. */
  868. static int chipio_write_multiple(struct hda_codec *codec,
  869. u32 chip_addx,
  870. const u32 *data,
  871. unsigned int count)
  872. {
  873. struct ca0132_spec *spec = codec->spec;
  874. int status;
  875. mutex_lock(&spec->chipio_mutex);
  876. status = chipio_write_address(codec, chip_addx);
  877. if (status < 0)
  878. goto error;
  879. status = chipio_write_data_multiple(codec, data, count);
  880. error:
  881. mutex_unlock(&spec->chipio_mutex);
  882. return status;
  883. }
  884. /*
  885. * Read the given address through the chip I/O widget
  886. * protected by the Mutex
  887. */
  888. static int chipio_read(struct hda_codec *codec,
  889. unsigned int chip_addx, unsigned int *data)
  890. {
  891. struct ca0132_spec *spec = codec->spec;
  892. int err;
  893. mutex_lock(&spec->chipio_mutex);
  894. /* write the address, and if successful proceed to write data */
  895. err = chipio_write_address(codec, chip_addx);
  896. if (err < 0)
  897. goto exit;
  898. err = chipio_read_data(codec, data);
  899. if (err < 0)
  900. goto exit;
  901. exit:
  902. mutex_unlock(&spec->chipio_mutex);
  903. return err;
  904. }
  905. /*
  906. * Set chip control flags through the chip I/O widget.
  907. */
  908. static void chipio_set_control_flag(struct hda_codec *codec,
  909. enum control_flag_id flag_id,
  910. bool flag_state)
  911. {
  912. unsigned int val;
  913. unsigned int flag_bit;
  914. flag_bit = (flag_state ? 1 : 0);
  915. val = (flag_bit << 7) | (flag_id);
  916. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  917. VENDOR_CHIPIO_FLAG_SET, val);
  918. }
  919. /*
  920. * Set chip parameters through the chip I/O widget.
  921. */
  922. static void chipio_set_control_param(struct hda_codec *codec,
  923. enum control_param_id param_id, int param_val)
  924. {
  925. struct ca0132_spec *spec = codec->spec;
  926. int val;
  927. if ((param_id < 32) && (param_val < 8)) {
  928. val = (param_val << 5) | (param_id);
  929. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  930. VENDOR_CHIPIO_PARAM_SET, val);
  931. } else {
  932. mutex_lock(&spec->chipio_mutex);
  933. if (chipio_send(codec, VENDOR_CHIPIO_STATUS, 0) == 0) {
  934. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  935. VENDOR_CHIPIO_PARAM_EX_ID_SET,
  936. param_id);
  937. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  938. VENDOR_CHIPIO_PARAM_EX_VALUE_SET,
  939. param_val);
  940. }
  941. mutex_unlock(&spec->chipio_mutex);
  942. }
  943. }
  944. /*
  945. * Set sampling rate of the connection point.
  946. */
  947. static void chipio_set_conn_rate(struct hda_codec *codec,
  948. int connid, enum ca0132_sample_rate rate)
  949. {
  950. chipio_set_control_param(codec, CONTROL_PARAM_CONN_POINT_ID, connid);
  951. chipio_set_control_param(codec, CONTROL_PARAM_CONN_POINT_SAMPLE_RATE,
  952. rate);
  953. }
  954. /*
  955. * Enable clocks.
  956. */
  957. static void chipio_enable_clocks(struct hda_codec *codec)
  958. {
  959. struct ca0132_spec *spec = codec->spec;
  960. mutex_lock(&spec->chipio_mutex);
  961. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  962. VENDOR_CHIPIO_8051_ADDRESS_LOW, 0);
  963. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  964. VENDOR_CHIPIO_PLL_PMU_WRITE, 0xff);
  965. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  966. VENDOR_CHIPIO_8051_ADDRESS_LOW, 5);
  967. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  968. VENDOR_CHIPIO_PLL_PMU_WRITE, 0x0b);
  969. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  970. VENDOR_CHIPIO_8051_ADDRESS_LOW, 6);
  971. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  972. VENDOR_CHIPIO_PLL_PMU_WRITE, 0xff);
  973. mutex_unlock(&spec->chipio_mutex);
  974. }
  975. /*
  976. * CA0132 DSP IO stuffs
  977. */
  978. static int dspio_send(struct hda_codec *codec, unsigned int reg,
  979. unsigned int data)
  980. {
  981. int res;
  982. unsigned long timeout = jiffies + msecs_to_jiffies(1000);
  983. /* send bits of data specified by reg to dsp */
  984. do {
  985. res = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0, reg, data);
  986. if ((res >= 0) && (res != VENDOR_STATUS_DSPIO_BUSY))
  987. return res;
  988. msleep(20);
  989. } while (time_before(jiffies, timeout));
  990. return -EIO;
  991. }
  992. /*
  993. * Wait for DSP to be ready for commands
  994. */
  995. static void dspio_write_wait(struct hda_codec *codec)
  996. {
  997. int status;
  998. unsigned long timeout = jiffies + msecs_to_jiffies(1000);
  999. do {
  1000. status = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0,
  1001. VENDOR_DSPIO_STATUS, 0);
  1002. if ((status == VENDOR_STATUS_DSPIO_OK) ||
  1003. (status == VENDOR_STATUS_DSPIO_SCP_RESPONSE_QUEUE_EMPTY))
  1004. break;
  1005. msleep(1);
  1006. } while (time_before(jiffies, timeout));
  1007. }
  1008. /*
  1009. * Write SCP data to DSP
  1010. */
  1011. static int dspio_write(struct hda_codec *codec, unsigned int scp_data)
  1012. {
  1013. struct ca0132_spec *spec = codec->spec;
  1014. int status;
  1015. dspio_write_wait(codec);
  1016. mutex_lock(&spec->chipio_mutex);
  1017. status = dspio_send(codec, VENDOR_DSPIO_SCP_WRITE_DATA_LOW,
  1018. scp_data & 0xffff);
  1019. if (status < 0)
  1020. goto error;
  1021. status = dspio_send(codec, VENDOR_DSPIO_SCP_WRITE_DATA_HIGH,
  1022. scp_data >> 16);
  1023. if (status < 0)
  1024. goto error;
  1025. /* OK, now check if the write itself has executed*/
  1026. status = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0,
  1027. VENDOR_DSPIO_STATUS, 0);
  1028. error:
  1029. mutex_unlock(&spec->chipio_mutex);
  1030. return (status == VENDOR_STATUS_DSPIO_SCP_COMMAND_QUEUE_FULL) ?
  1031. -EIO : 0;
  1032. }
  1033. /*
  1034. * Write multiple SCP data to DSP
  1035. */
  1036. static int dspio_write_multiple(struct hda_codec *codec,
  1037. unsigned int *buffer, unsigned int size)
  1038. {
  1039. int status = 0;
  1040. unsigned int count;
  1041. if ((buffer == NULL))
  1042. return -EINVAL;
  1043. count = 0;
  1044. while (count < size) {
  1045. status = dspio_write(codec, *buffer++);
  1046. if (status != 0)
  1047. break;
  1048. count++;
  1049. }
  1050. return status;
  1051. }
  1052. static int dspio_read(struct hda_codec *codec, unsigned int *data)
  1053. {
  1054. int status;
  1055. status = dspio_send(codec, VENDOR_DSPIO_SCP_POST_READ_DATA, 0);
  1056. if (status == -EIO)
  1057. return status;
  1058. status = dspio_send(codec, VENDOR_DSPIO_STATUS, 0);
  1059. if (status == -EIO ||
  1060. status == VENDOR_STATUS_DSPIO_SCP_RESPONSE_QUEUE_EMPTY)
  1061. return -EIO;
  1062. *data = snd_hda_codec_read(codec, WIDGET_DSP_CTRL, 0,
  1063. VENDOR_DSPIO_SCP_READ_DATA, 0);
  1064. return 0;
  1065. }
  1066. static int dspio_read_multiple(struct hda_codec *codec, unsigned int *buffer,
  1067. unsigned int *buf_size, unsigned int size_count)
  1068. {
  1069. int status = 0;
  1070. unsigned int size = *buf_size;
  1071. unsigned int count;
  1072. unsigned int skip_count;
  1073. unsigned int dummy;
  1074. if ((buffer == NULL))
  1075. return -1;
  1076. count = 0;
  1077. while (count < size && count < size_count) {
  1078. status = dspio_read(codec, buffer++);
  1079. if (status != 0)
  1080. break;
  1081. count++;
  1082. }
  1083. skip_count = count;
  1084. if (status == 0) {
  1085. while (skip_count < size) {
  1086. status = dspio_read(codec, &dummy);
  1087. if (status != 0)
  1088. break;
  1089. skip_count++;
  1090. }
  1091. }
  1092. *buf_size = count;
  1093. return status;
  1094. }
  1095. /*
  1096. * Construct the SCP header using corresponding fields
  1097. */
  1098. static inline unsigned int
  1099. make_scp_header(unsigned int target_id, unsigned int source_id,
  1100. unsigned int get_flag, unsigned int req,
  1101. unsigned int device_flag, unsigned int resp_flag,
  1102. unsigned int error_flag, unsigned int data_size)
  1103. {
  1104. unsigned int header = 0;
  1105. header = (data_size & 0x1f) << 27;
  1106. header |= (error_flag & 0x01) << 26;
  1107. header |= (resp_flag & 0x01) << 25;
  1108. header |= (device_flag & 0x01) << 24;
  1109. header |= (req & 0x7f) << 17;
  1110. header |= (get_flag & 0x01) << 16;
  1111. header |= (source_id & 0xff) << 8;
  1112. header |= target_id & 0xff;
  1113. return header;
  1114. }
  1115. /*
  1116. * Extract corresponding fields from SCP header
  1117. */
  1118. static inline void
  1119. extract_scp_header(unsigned int header,
  1120. unsigned int *target_id, unsigned int *source_id,
  1121. unsigned int *get_flag, unsigned int *req,
  1122. unsigned int *device_flag, unsigned int *resp_flag,
  1123. unsigned int *error_flag, unsigned int *data_size)
  1124. {
  1125. if (data_size)
  1126. *data_size = (header >> 27) & 0x1f;
  1127. if (error_flag)
  1128. *error_flag = (header >> 26) & 0x01;
  1129. if (resp_flag)
  1130. *resp_flag = (header >> 25) & 0x01;
  1131. if (device_flag)
  1132. *device_flag = (header >> 24) & 0x01;
  1133. if (req)
  1134. *req = (header >> 17) & 0x7f;
  1135. if (get_flag)
  1136. *get_flag = (header >> 16) & 0x01;
  1137. if (source_id)
  1138. *source_id = (header >> 8) & 0xff;
  1139. if (target_id)
  1140. *target_id = header & 0xff;
  1141. }
  1142. #define SCP_MAX_DATA_WORDS (16)
  1143. /* Structure to contain any SCP message */
  1144. struct scp_msg {
  1145. unsigned int hdr;
  1146. unsigned int data[SCP_MAX_DATA_WORDS];
  1147. };
  1148. static void dspio_clear_response_queue(struct hda_codec *codec)
  1149. {
  1150. unsigned int dummy = 0;
  1151. int status = -1;
  1152. /* clear all from the response queue */
  1153. do {
  1154. status = dspio_read(codec, &dummy);
  1155. } while (status == 0);
  1156. }
  1157. static int dspio_get_response_data(struct hda_codec *codec)
  1158. {
  1159. struct ca0132_spec *spec = codec->spec;
  1160. unsigned int data = 0;
  1161. unsigned int count;
  1162. if (dspio_read(codec, &data) < 0)
  1163. return -EIO;
  1164. if ((data & 0x00ffffff) == spec->wait_scp_header) {
  1165. spec->scp_resp_header = data;
  1166. spec->scp_resp_count = data >> 27;
  1167. count = spec->wait_num_data;
  1168. dspio_read_multiple(codec, spec->scp_resp_data,
  1169. &spec->scp_resp_count, count);
  1170. return 0;
  1171. }
  1172. return -EIO;
  1173. }
  1174. /*
  1175. * Send SCP message to DSP
  1176. */
  1177. static int dspio_send_scp_message(struct hda_codec *codec,
  1178. unsigned char *send_buf,
  1179. unsigned int send_buf_size,
  1180. unsigned char *return_buf,
  1181. unsigned int return_buf_size,
  1182. unsigned int *bytes_returned)
  1183. {
  1184. struct ca0132_spec *spec = codec->spec;
  1185. int status = -1;
  1186. unsigned int scp_send_size = 0;
  1187. unsigned int total_size;
  1188. bool waiting_for_resp = false;
  1189. unsigned int header;
  1190. struct scp_msg *ret_msg;
  1191. unsigned int resp_src_id, resp_target_id;
  1192. unsigned int data_size, src_id, target_id, get_flag, device_flag;
  1193. if (bytes_returned)
  1194. *bytes_returned = 0;
  1195. /* get scp header from buffer */
  1196. header = *((unsigned int *)send_buf);
  1197. extract_scp_header(header, &target_id, &src_id, &get_flag, NULL,
  1198. &device_flag, NULL, NULL, &data_size);
  1199. scp_send_size = data_size + 1;
  1200. total_size = (scp_send_size * 4);
  1201. if (send_buf_size < total_size)
  1202. return -EINVAL;
  1203. if (get_flag || device_flag) {
  1204. if (!return_buf || return_buf_size < 4 || !bytes_returned)
  1205. return -EINVAL;
  1206. spec->wait_scp_header = *((unsigned int *)send_buf);
  1207. /* swap source id with target id */
  1208. resp_target_id = src_id;
  1209. resp_src_id = target_id;
  1210. spec->wait_scp_header &= 0xffff0000;
  1211. spec->wait_scp_header |= (resp_src_id << 8) | (resp_target_id);
  1212. spec->wait_num_data = return_buf_size/sizeof(unsigned int) - 1;
  1213. spec->wait_scp = 1;
  1214. waiting_for_resp = true;
  1215. }
  1216. status = dspio_write_multiple(codec, (unsigned int *)send_buf,
  1217. scp_send_size);
  1218. if (status < 0) {
  1219. spec->wait_scp = 0;
  1220. return status;
  1221. }
  1222. if (waiting_for_resp) {
  1223. unsigned long timeout = jiffies + msecs_to_jiffies(1000);
  1224. memset(return_buf, 0, return_buf_size);
  1225. do {
  1226. msleep(20);
  1227. } while (spec->wait_scp && time_before(jiffies, timeout));
  1228. waiting_for_resp = false;
  1229. if (!spec->wait_scp) {
  1230. ret_msg = (struct scp_msg *)return_buf;
  1231. memcpy(&ret_msg->hdr, &spec->scp_resp_header, 4);
  1232. memcpy(&ret_msg->data, spec->scp_resp_data,
  1233. spec->wait_num_data);
  1234. *bytes_returned = (spec->scp_resp_count + 1) * 4;
  1235. status = 0;
  1236. } else {
  1237. status = -EIO;
  1238. }
  1239. spec->wait_scp = 0;
  1240. }
  1241. return status;
  1242. }
  1243. /**
  1244. * Prepare and send the SCP message to DSP
  1245. * @codec: the HDA codec
  1246. * @mod_id: ID of the DSP module to send the command
  1247. * @req: ID of request to send to the DSP module
  1248. * @dir: SET or GET
  1249. * @data: pointer to the data to send with the request, request specific
  1250. * @len: length of the data, in bytes
  1251. * @reply: point to the buffer to hold data returned for a reply
  1252. * @reply_len: length of the reply buffer returned from GET
  1253. *
  1254. * Returns zero or a negative error code.
  1255. */
  1256. static int dspio_scp(struct hda_codec *codec,
  1257. int mod_id, int req, int dir, void *data, unsigned int len,
  1258. void *reply, unsigned int *reply_len)
  1259. {
  1260. int status = 0;
  1261. struct scp_msg scp_send, scp_reply;
  1262. unsigned int ret_bytes, send_size, ret_size;
  1263. unsigned int send_get_flag, reply_resp_flag, reply_error_flag;
  1264. unsigned int reply_data_size;
  1265. memset(&scp_send, 0, sizeof(scp_send));
  1266. memset(&scp_reply, 0, sizeof(scp_reply));
  1267. if ((len != 0 && data == NULL) || (len > SCP_MAX_DATA_WORDS))
  1268. return -EINVAL;
  1269. if (dir == SCP_GET && reply == NULL) {
  1270. codec_dbg(codec, "dspio_scp get but has no buffer\n");
  1271. return -EINVAL;
  1272. }
  1273. if (reply != NULL && (reply_len == NULL || (*reply_len == 0))) {
  1274. codec_dbg(codec, "dspio_scp bad resp buf len parms\n");
  1275. return -EINVAL;
  1276. }
  1277. scp_send.hdr = make_scp_header(mod_id, 0x20, (dir == SCP_GET), req,
  1278. 0, 0, 0, len/sizeof(unsigned int));
  1279. if (data != NULL && len > 0) {
  1280. len = min((unsigned int)(sizeof(scp_send.data)), len);
  1281. memcpy(scp_send.data, data, len);
  1282. }
  1283. ret_bytes = 0;
  1284. send_size = sizeof(unsigned int) + len;
  1285. status = dspio_send_scp_message(codec, (unsigned char *)&scp_send,
  1286. send_size, (unsigned char *)&scp_reply,
  1287. sizeof(scp_reply), &ret_bytes);
  1288. if (status < 0) {
  1289. codec_dbg(codec, "dspio_scp: send scp msg failed\n");
  1290. return status;
  1291. }
  1292. /* extract send and reply headers members */
  1293. extract_scp_header(scp_send.hdr, NULL, NULL, &send_get_flag,
  1294. NULL, NULL, NULL, NULL, NULL);
  1295. extract_scp_header(scp_reply.hdr, NULL, NULL, NULL, NULL, NULL,
  1296. &reply_resp_flag, &reply_error_flag,
  1297. &reply_data_size);
  1298. if (!send_get_flag)
  1299. return 0;
  1300. if (reply_resp_flag && !reply_error_flag) {
  1301. ret_size = (ret_bytes - sizeof(scp_reply.hdr))
  1302. / sizeof(unsigned int);
  1303. if (*reply_len < ret_size*sizeof(unsigned int)) {
  1304. codec_dbg(codec, "reply too long for buf\n");
  1305. return -EINVAL;
  1306. } else if (ret_size != reply_data_size) {
  1307. codec_dbg(codec, "RetLen and HdrLen .NE.\n");
  1308. return -EINVAL;
  1309. } else if (!reply) {
  1310. codec_dbg(codec, "NULL reply\n");
  1311. return -EINVAL;
  1312. } else {
  1313. *reply_len = ret_size*sizeof(unsigned int);
  1314. memcpy(reply, scp_reply.data, *reply_len);
  1315. }
  1316. } else {
  1317. codec_dbg(codec, "reply ill-formed or errflag set\n");
  1318. return -EIO;
  1319. }
  1320. return status;
  1321. }
  1322. /*
  1323. * Set DSP parameters
  1324. */
  1325. static int dspio_set_param(struct hda_codec *codec, int mod_id,
  1326. int req, void *data, unsigned int len)
  1327. {
  1328. return dspio_scp(codec, mod_id, req, SCP_SET, data, len, NULL, NULL);
  1329. }
  1330. static int dspio_set_uint_param(struct hda_codec *codec, int mod_id,
  1331. int req, unsigned int data)
  1332. {
  1333. return dspio_set_param(codec, mod_id, req, &data, sizeof(unsigned int));
  1334. }
  1335. /*
  1336. * Allocate a DSP DMA channel via an SCP message
  1337. */
  1338. static int dspio_alloc_dma_chan(struct hda_codec *codec, unsigned int *dma_chan)
  1339. {
  1340. int status = 0;
  1341. unsigned int size = sizeof(dma_chan);
  1342. codec_dbg(codec, " dspio_alloc_dma_chan() -- begin\n");
  1343. status = dspio_scp(codec, MASTERCONTROL, MASTERCONTROL_ALLOC_DMA_CHAN,
  1344. SCP_GET, NULL, 0, dma_chan, &size);
  1345. if (status < 0) {
  1346. codec_dbg(codec, "dspio_alloc_dma_chan: SCP Failed\n");
  1347. return status;
  1348. }
  1349. if ((*dma_chan + 1) == 0) {
  1350. codec_dbg(codec, "no free dma channels to allocate\n");
  1351. return -EBUSY;
  1352. }
  1353. codec_dbg(codec, "dspio_alloc_dma_chan: chan=%d\n", *dma_chan);
  1354. codec_dbg(codec, " dspio_alloc_dma_chan() -- complete\n");
  1355. return status;
  1356. }
  1357. /*
  1358. * Free a DSP DMA via an SCP message
  1359. */
  1360. static int dspio_free_dma_chan(struct hda_codec *codec, unsigned int dma_chan)
  1361. {
  1362. int status = 0;
  1363. unsigned int dummy = 0;
  1364. codec_dbg(codec, " dspio_free_dma_chan() -- begin\n");
  1365. codec_dbg(codec, "dspio_free_dma_chan: chan=%d\n", dma_chan);
  1366. status = dspio_scp(codec, MASTERCONTROL, MASTERCONTROL_ALLOC_DMA_CHAN,
  1367. SCP_SET, &dma_chan, sizeof(dma_chan), NULL, &dummy);
  1368. if (status < 0) {
  1369. codec_dbg(codec, "dspio_free_dma_chan: SCP Failed\n");
  1370. return status;
  1371. }
  1372. codec_dbg(codec, " dspio_free_dma_chan() -- complete\n");
  1373. return status;
  1374. }
  1375. /*
  1376. * (Re)start the DSP
  1377. */
  1378. static int dsp_set_run_state(struct hda_codec *codec)
  1379. {
  1380. unsigned int dbg_ctrl_reg;
  1381. unsigned int halt_state;
  1382. int err;
  1383. err = chipio_read(codec, DSP_DBGCNTL_INST_OFFSET, &dbg_ctrl_reg);
  1384. if (err < 0)
  1385. return err;
  1386. halt_state = (dbg_ctrl_reg & DSP_DBGCNTL_STATE_MASK) >>
  1387. DSP_DBGCNTL_STATE_LOBIT;
  1388. if (halt_state != 0) {
  1389. dbg_ctrl_reg &= ~((halt_state << DSP_DBGCNTL_SS_LOBIT) &
  1390. DSP_DBGCNTL_SS_MASK);
  1391. err = chipio_write(codec, DSP_DBGCNTL_INST_OFFSET,
  1392. dbg_ctrl_reg);
  1393. if (err < 0)
  1394. return err;
  1395. dbg_ctrl_reg |= (halt_state << DSP_DBGCNTL_EXEC_LOBIT) &
  1396. DSP_DBGCNTL_EXEC_MASK;
  1397. err = chipio_write(codec, DSP_DBGCNTL_INST_OFFSET,
  1398. dbg_ctrl_reg);
  1399. if (err < 0)
  1400. return err;
  1401. }
  1402. return 0;
  1403. }
  1404. /*
  1405. * Reset the DSP
  1406. */
  1407. static int dsp_reset(struct hda_codec *codec)
  1408. {
  1409. unsigned int res;
  1410. int retry = 20;
  1411. codec_dbg(codec, "dsp_reset\n");
  1412. do {
  1413. res = dspio_send(codec, VENDOR_DSPIO_DSP_INIT, 0);
  1414. retry--;
  1415. } while (res == -EIO && retry);
  1416. if (!retry) {
  1417. codec_dbg(codec, "dsp_reset timeout\n");
  1418. return -EIO;
  1419. }
  1420. return 0;
  1421. }
  1422. /*
  1423. * Convert chip address to DSP address
  1424. */
  1425. static unsigned int dsp_chip_to_dsp_addx(unsigned int chip_addx,
  1426. bool *code, bool *yram)
  1427. {
  1428. *code = *yram = false;
  1429. if (UC_RANGE(chip_addx, 1)) {
  1430. *code = true;
  1431. return UC_OFF(chip_addx);
  1432. } else if (X_RANGE_ALL(chip_addx, 1)) {
  1433. return X_OFF(chip_addx);
  1434. } else if (Y_RANGE_ALL(chip_addx, 1)) {
  1435. *yram = true;
  1436. return Y_OFF(chip_addx);
  1437. }
  1438. return INVALID_CHIP_ADDRESS;
  1439. }
  1440. /*
  1441. * Check if the DSP DMA is active
  1442. */
  1443. static bool dsp_is_dma_active(struct hda_codec *codec, unsigned int dma_chan)
  1444. {
  1445. unsigned int dma_chnlstart_reg;
  1446. chipio_read(codec, DSPDMAC_CHNLSTART_INST_OFFSET, &dma_chnlstart_reg);
  1447. return ((dma_chnlstart_reg & (1 <<
  1448. (DSPDMAC_CHNLSTART_EN_LOBIT + dma_chan))) != 0);
  1449. }
  1450. static int dsp_dma_setup_common(struct hda_codec *codec,
  1451. unsigned int chip_addx,
  1452. unsigned int dma_chan,
  1453. unsigned int port_map_mask,
  1454. bool ovly)
  1455. {
  1456. int status = 0;
  1457. unsigned int chnl_prop;
  1458. unsigned int dsp_addx;
  1459. unsigned int active;
  1460. bool code, yram;
  1461. codec_dbg(codec, "-- dsp_dma_setup_common() -- Begin ---------\n");
  1462. if (dma_chan >= DSPDMAC_DMA_CFG_CHANNEL_COUNT) {
  1463. codec_dbg(codec, "dma chan num invalid\n");
  1464. return -EINVAL;
  1465. }
  1466. if (dsp_is_dma_active(codec, dma_chan)) {
  1467. codec_dbg(codec, "dma already active\n");
  1468. return -EBUSY;
  1469. }
  1470. dsp_addx = dsp_chip_to_dsp_addx(chip_addx, &code, &yram);
  1471. if (dsp_addx == INVALID_CHIP_ADDRESS) {
  1472. codec_dbg(codec, "invalid chip addr\n");
  1473. return -ENXIO;
  1474. }
  1475. chnl_prop = DSPDMAC_CHNLPROP_AC_MASK;
  1476. active = 0;
  1477. codec_dbg(codec, " dsp_dma_setup_common() start reg pgm\n");
  1478. if (ovly) {
  1479. status = chipio_read(codec, DSPDMAC_CHNLPROP_INST_OFFSET,
  1480. &chnl_prop);
  1481. if (status < 0) {
  1482. codec_dbg(codec, "read CHNLPROP Reg fail\n");
  1483. return status;
  1484. }
  1485. codec_dbg(codec, "dsp_dma_setup_common() Read CHNLPROP\n");
  1486. }
  1487. if (!code)
  1488. chnl_prop &= ~(1 << (DSPDMAC_CHNLPROP_MSPCE_LOBIT + dma_chan));
  1489. else
  1490. chnl_prop |= (1 << (DSPDMAC_CHNLPROP_MSPCE_LOBIT + dma_chan));
  1491. chnl_prop &= ~(1 << (DSPDMAC_CHNLPROP_DCON_LOBIT + dma_chan));
  1492. status = chipio_write(codec, DSPDMAC_CHNLPROP_INST_OFFSET, chnl_prop);
  1493. if (status < 0) {
  1494. codec_dbg(codec, "write CHNLPROP Reg fail\n");
  1495. return status;
  1496. }
  1497. codec_dbg(codec, " dsp_dma_setup_common() Write CHNLPROP\n");
  1498. if (ovly) {
  1499. status = chipio_read(codec, DSPDMAC_ACTIVE_INST_OFFSET,
  1500. &active);
  1501. if (status < 0) {
  1502. codec_dbg(codec, "read ACTIVE Reg fail\n");
  1503. return status;
  1504. }
  1505. codec_dbg(codec, "dsp_dma_setup_common() Read ACTIVE\n");
  1506. }
  1507. active &= (~(1 << (DSPDMAC_ACTIVE_AAR_LOBIT + dma_chan))) &
  1508. DSPDMAC_ACTIVE_AAR_MASK;
  1509. status = chipio_write(codec, DSPDMAC_ACTIVE_INST_OFFSET, active);
  1510. if (status < 0) {
  1511. codec_dbg(codec, "write ACTIVE Reg fail\n");
  1512. return status;
  1513. }
  1514. codec_dbg(codec, " dsp_dma_setup_common() Write ACTIVE\n");
  1515. status = chipio_write(codec, DSPDMAC_AUDCHSEL_INST_OFFSET(dma_chan),
  1516. port_map_mask);
  1517. if (status < 0) {
  1518. codec_dbg(codec, "write AUDCHSEL Reg fail\n");
  1519. return status;
  1520. }
  1521. codec_dbg(codec, " dsp_dma_setup_common() Write AUDCHSEL\n");
  1522. status = chipio_write(codec, DSPDMAC_IRQCNT_INST_OFFSET(dma_chan),
  1523. DSPDMAC_IRQCNT_BICNT_MASK | DSPDMAC_IRQCNT_CICNT_MASK);
  1524. if (status < 0) {
  1525. codec_dbg(codec, "write IRQCNT Reg fail\n");
  1526. return status;
  1527. }
  1528. codec_dbg(codec, " dsp_dma_setup_common() Write IRQCNT\n");
  1529. codec_dbg(codec,
  1530. "ChipA=0x%x,DspA=0x%x,dmaCh=%u, "
  1531. "CHSEL=0x%x,CHPROP=0x%x,Active=0x%x\n",
  1532. chip_addx, dsp_addx, dma_chan,
  1533. port_map_mask, chnl_prop, active);
  1534. codec_dbg(codec, "-- dsp_dma_setup_common() -- Complete ------\n");
  1535. return 0;
  1536. }
  1537. /*
  1538. * Setup the DSP DMA per-transfer-specific registers
  1539. */
  1540. static int dsp_dma_setup(struct hda_codec *codec,
  1541. unsigned int chip_addx,
  1542. unsigned int count,
  1543. unsigned int dma_chan)
  1544. {
  1545. int status = 0;
  1546. bool code, yram;
  1547. unsigned int dsp_addx;
  1548. unsigned int addr_field;
  1549. unsigned int incr_field;
  1550. unsigned int base_cnt;
  1551. unsigned int cur_cnt;
  1552. unsigned int dma_cfg = 0;
  1553. unsigned int adr_ofs = 0;
  1554. unsigned int xfr_cnt = 0;
  1555. const unsigned int max_dma_count = 1 << (DSPDMAC_XFRCNT_BCNT_HIBIT -
  1556. DSPDMAC_XFRCNT_BCNT_LOBIT + 1);
  1557. codec_dbg(codec, "-- dsp_dma_setup() -- Begin ---------\n");
  1558. if (count > max_dma_count) {
  1559. codec_dbg(codec, "count too big\n");
  1560. return -EINVAL;
  1561. }
  1562. dsp_addx = dsp_chip_to_dsp_addx(chip_addx, &code, &yram);
  1563. if (dsp_addx == INVALID_CHIP_ADDRESS) {
  1564. codec_dbg(codec, "invalid chip addr\n");
  1565. return -ENXIO;
  1566. }
  1567. codec_dbg(codec, " dsp_dma_setup() start reg pgm\n");
  1568. addr_field = dsp_addx << DSPDMAC_DMACFG_DBADR_LOBIT;
  1569. incr_field = 0;
  1570. if (!code) {
  1571. addr_field <<= 1;
  1572. if (yram)
  1573. addr_field |= (1 << DSPDMAC_DMACFG_DBADR_LOBIT);
  1574. incr_field = (1 << DSPDMAC_DMACFG_AINCR_LOBIT);
  1575. }
  1576. dma_cfg = addr_field + incr_field;
  1577. status = chipio_write(codec, DSPDMAC_DMACFG_INST_OFFSET(dma_chan),
  1578. dma_cfg);
  1579. if (status < 0) {
  1580. codec_dbg(codec, "write DMACFG Reg fail\n");
  1581. return status;
  1582. }
  1583. codec_dbg(codec, " dsp_dma_setup() Write DMACFG\n");
  1584. adr_ofs = (count - 1) << (DSPDMAC_DSPADROFS_BOFS_LOBIT +
  1585. (code ? 0 : 1));
  1586. status = chipio_write(codec, DSPDMAC_DSPADROFS_INST_OFFSET(dma_chan),
  1587. adr_ofs);
  1588. if (status < 0) {
  1589. codec_dbg(codec, "write DSPADROFS Reg fail\n");
  1590. return status;
  1591. }
  1592. codec_dbg(codec, " dsp_dma_setup() Write DSPADROFS\n");
  1593. base_cnt = (count - 1) << DSPDMAC_XFRCNT_BCNT_LOBIT;
  1594. cur_cnt = (count - 1) << DSPDMAC_XFRCNT_CCNT_LOBIT;
  1595. xfr_cnt = base_cnt | cur_cnt;
  1596. status = chipio_write(codec,
  1597. DSPDMAC_XFRCNT_INST_OFFSET(dma_chan), xfr_cnt);
  1598. if (status < 0) {
  1599. codec_dbg(codec, "write XFRCNT Reg fail\n");
  1600. return status;
  1601. }
  1602. codec_dbg(codec, " dsp_dma_setup() Write XFRCNT\n");
  1603. codec_dbg(codec,
  1604. "ChipA=0x%x, cnt=0x%x, DMACFG=0x%x, "
  1605. "ADROFS=0x%x, XFRCNT=0x%x\n",
  1606. chip_addx, count, dma_cfg, adr_ofs, xfr_cnt);
  1607. codec_dbg(codec, "-- dsp_dma_setup() -- Complete ---------\n");
  1608. return 0;
  1609. }
  1610. /*
  1611. * Start the DSP DMA
  1612. */
  1613. static int dsp_dma_start(struct hda_codec *codec,
  1614. unsigned int dma_chan, bool ovly)
  1615. {
  1616. unsigned int reg = 0;
  1617. int status = 0;
  1618. codec_dbg(codec, "-- dsp_dma_start() -- Begin ---------\n");
  1619. if (ovly) {
  1620. status = chipio_read(codec,
  1621. DSPDMAC_CHNLSTART_INST_OFFSET, &reg);
  1622. if (status < 0) {
  1623. codec_dbg(codec, "read CHNLSTART reg fail\n");
  1624. return status;
  1625. }
  1626. codec_dbg(codec, "-- dsp_dma_start() Read CHNLSTART\n");
  1627. reg &= ~(DSPDMAC_CHNLSTART_EN_MASK |
  1628. DSPDMAC_CHNLSTART_DIS_MASK);
  1629. }
  1630. status = chipio_write(codec, DSPDMAC_CHNLSTART_INST_OFFSET,
  1631. reg | (1 << (dma_chan + DSPDMAC_CHNLSTART_EN_LOBIT)));
  1632. if (status < 0) {
  1633. codec_dbg(codec, "write CHNLSTART reg fail\n");
  1634. return status;
  1635. }
  1636. codec_dbg(codec, "-- dsp_dma_start() -- Complete ---------\n");
  1637. return status;
  1638. }
  1639. /*
  1640. * Stop the DSP DMA
  1641. */
  1642. static int dsp_dma_stop(struct hda_codec *codec,
  1643. unsigned int dma_chan, bool ovly)
  1644. {
  1645. unsigned int reg = 0;
  1646. int status = 0;
  1647. codec_dbg(codec, "-- dsp_dma_stop() -- Begin ---------\n");
  1648. if (ovly) {
  1649. status = chipio_read(codec,
  1650. DSPDMAC_CHNLSTART_INST_OFFSET, &reg);
  1651. if (status < 0) {
  1652. codec_dbg(codec, "read CHNLSTART reg fail\n");
  1653. return status;
  1654. }
  1655. codec_dbg(codec, "-- dsp_dma_stop() Read CHNLSTART\n");
  1656. reg &= ~(DSPDMAC_CHNLSTART_EN_MASK |
  1657. DSPDMAC_CHNLSTART_DIS_MASK);
  1658. }
  1659. status = chipio_write(codec, DSPDMAC_CHNLSTART_INST_OFFSET,
  1660. reg | (1 << (dma_chan + DSPDMAC_CHNLSTART_DIS_LOBIT)));
  1661. if (status < 0) {
  1662. codec_dbg(codec, "write CHNLSTART reg fail\n");
  1663. return status;
  1664. }
  1665. codec_dbg(codec, "-- dsp_dma_stop() -- Complete ---------\n");
  1666. return status;
  1667. }
  1668. /**
  1669. * Allocate router ports
  1670. *
  1671. * @codec: the HDA codec
  1672. * @num_chans: number of channels in the stream
  1673. * @ports_per_channel: number of ports per channel
  1674. * @start_device: start device
  1675. * @port_map: pointer to the port list to hold the allocated ports
  1676. *
  1677. * Returns zero or a negative error code.
  1678. */
  1679. static int dsp_allocate_router_ports(struct hda_codec *codec,
  1680. unsigned int num_chans,
  1681. unsigned int ports_per_channel,
  1682. unsigned int start_device,
  1683. unsigned int *port_map)
  1684. {
  1685. int status = 0;
  1686. int res;
  1687. u8 val;
  1688. status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
  1689. if (status < 0)
  1690. return status;
  1691. val = start_device << 6;
  1692. val |= (ports_per_channel - 1) << 4;
  1693. val |= num_chans - 1;
  1694. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  1695. VENDOR_CHIPIO_PORT_ALLOC_CONFIG_SET,
  1696. val);
  1697. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  1698. VENDOR_CHIPIO_PORT_ALLOC_SET,
  1699. MEM_CONNID_DSP);
  1700. status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
  1701. if (status < 0)
  1702. return status;
  1703. res = snd_hda_codec_read(codec, WIDGET_CHIP_CTRL, 0,
  1704. VENDOR_CHIPIO_PORT_ALLOC_GET, 0);
  1705. *port_map = res;
  1706. return (res < 0) ? res : 0;
  1707. }
  1708. /*
  1709. * Free router ports
  1710. */
  1711. static int dsp_free_router_ports(struct hda_codec *codec)
  1712. {
  1713. int status = 0;
  1714. status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
  1715. if (status < 0)
  1716. return status;
  1717. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  1718. VENDOR_CHIPIO_PORT_FREE_SET,
  1719. MEM_CONNID_DSP);
  1720. status = chipio_send(codec, VENDOR_CHIPIO_STATUS, 0);
  1721. return status;
  1722. }
  1723. /*
  1724. * Allocate DSP ports for the download stream
  1725. */
  1726. static int dsp_allocate_ports(struct hda_codec *codec,
  1727. unsigned int num_chans,
  1728. unsigned int rate_multi, unsigned int *port_map)
  1729. {
  1730. int status;
  1731. codec_dbg(codec, " dsp_allocate_ports() -- begin\n");
  1732. if ((rate_multi != 1) && (rate_multi != 2) && (rate_multi != 4)) {
  1733. codec_dbg(codec, "bad rate multiple\n");
  1734. return -EINVAL;
  1735. }
  1736. status = dsp_allocate_router_ports(codec, num_chans,
  1737. rate_multi, 0, port_map);
  1738. codec_dbg(codec, " dsp_allocate_ports() -- complete\n");
  1739. return status;
  1740. }
  1741. static int dsp_allocate_ports_format(struct hda_codec *codec,
  1742. const unsigned short fmt,
  1743. unsigned int *port_map)
  1744. {
  1745. int status;
  1746. unsigned int num_chans;
  1747. unsigned int sample_rate_div = ((get_hdafmt_rate(fmt) >> 0) & 3) + 1;
  1748. unsigned int sample_rate_mul = ((get_hdafmt_rate(fmt) >> 3) & 3) + 1;
  1749. unsigned int rate_multi = sample_rate_mul / sample_rate_div;
  1750. if ((rate_multi != 1) && (rate_multi != 2) && (rate_multi != 4)) {
  1751. codec_dbg(codec, "bad rate multiple\n");
  1752. return -EINVAL;
  1753. }
  1754. num_chans = get_hdafmt_chs(fmt) + 1;
  1755. status = dsp_allocate_ports(codec, num_chans, rate_multi, port_map);
  1756. return status;
  1757. }
  1758. /*
  1759. * free DSP ports
  1760. */
  1761. static int dsp_free_ports(struct hda_codec *codec)
  1762. {
  1763. int status;
  1764. codec_dbg(codec, " dsp_free_ports() -- begin\n");
  1765. status = dsp_free_router_ports(codec);
  1766. if (status < 0) {
  1767. codec_dbg(codec, "free router ports fail\n");
  1768. return status;
  1769. }
  1770. codec_dbg(codec, " dsp_free_ports() -- complete\n");
  1771. return status;
  1772. }
  1773. /*
  1774. * HDA DMA engine stuffs for DSP code download
  1775. */
  1776. struct dma_engine {
  1777. struct hda_codec *codec;
  1778. unsigned short m_converter_format;
  1779. struct snd_dma_buffer *dmab;
  1780. unsigned int buf_size;
  1781. };
  1782. enum dma_state {
  1783. DMA_STATE_STOP = 0,
  1784. DMA_STATE_RUN = 1
  1785. };
  1786. static int dma_convert_to_hda_format(struct hda_codec *codec,
  1787. unsigned int sample_rate,
  1788. unsigned short channels,
  1789. unsigned short *hda_format)
  1790. {
  1791. unsigned int format_val;
  1792. format_val = snd_hdac_calc_stream_format(sample_rate,
  1793. channels, SNDRV_PCM_FORMAT_S32_LE, 32, 0);
  1794. if (hda_format)
  1795. *hda_format = (unsigned short)format_val;
  1796. return 0;
  1797. }
  1798. /*
  1799. * Reset DMA for DSP download
  1800. */
  1801. static int dma_reset(struct dma_engine *dma)
  1802. {
  1803. struct hda_codec *codec = dma->codec;
  1804. struct ca0132_spec *spec = codec->spec;
  1805. int status;
  1806. if (dma->dmab->area)
  1807. snd_hda_codec_load_dsp_cleanup(codec, dma->dmab);
  1808. status = snd_hda_codec_load_dsp_prepare(codec,
  1809. dma->m_converter_format,
  1810. dma->buf_size,
  1811. dma->dmab);
  1812. if (status < 0)
  1813. return status;
  1814. spec->dsp_stream_id = status;
  1815. return 0;
  1816. }
  1817. static int dma_set_state(struct dma_engine *dma, enum dma_state state)
  1818. {
  1819. bool cmd;
  1820. switch (state) {
  1821. case DMA_STATE_STOP:
  1822. cmd = false;
  1823. break;
  1824. case DMA_STATE_RUN:
  1825. cmd = true;
  1826. break;
  1827. default:
  1828. return 0;
  1829. }
  1830. snd_hda_codec_load_dsp_trigger(dma->codec, cmd);
  1831. return 0;
  1832. }
  1833. static unsigned int dma_get_buffer_size(struct dma_engine *dma)
  1834. {
  1835. return dma->dmab->bytes;
  1836. }
  1837. static unsigned char *dma_get_buffer_addr(struct dma_engine *dma)
  1838. {
  1839. return dma->dmab->area;
  1840. }
  1841. static int dma_xfer(struct dma_engine *dma,
  1842. const unsigned int *data,
  1843. unsigned int count)
  1844. {
  1845. memcpy(dma->dmab->area, data, count);
  1846. return 0;
  1847. }
  1848. static void dma_get_converter_format(
  1849. struct dma_engine *dma,
  1850. unsigned short *format)
  1851. {
  1852. if (format)
  1853. *format = dma->m_converter_format;
  1854. }
  1855. static unsigned int dma_get_stream_id(struct dma_engine *dma)
  1856. {
  1857. struct ca0132_spec *spec = dma->codec->spec;
  1858. return spec->dsp_stream_id;
  1859. }
  1860. struct dsp_image_seg {
  1861. u32 magic;
  1862. u32 chip_addr;
  1863. u32 count;
  1864. u32 data[0];
  1865. };
  1866. static const u32 g_magic_value = 0x4c46584d;
  1867. static const u32 g_chip_addr_magic_value = 0xFFFFFF01;
  1868. static bool is_valid(const struct dsp_image_seg *p)
  1869. {
  1870. return p->magic == g_magic_value;
  1871. }
  1872. static bool is_hci_prog_list_seg(const struct dsp_image_seg *p)
  1873. {
  1874. return g_chip_addr_magic_value == p->chip_addr;
  1875. }
  1876. static bool is_last(const struct dsp_image_seg *p)
  1877. {
  1878. return p->count == 0;
  1879. }
  1880. static size_t dsp_sizeof(const struct dsp_image_seg *p)
  1881. {
  1882. return sizeof(*p) + p->count*sizeof(u32);
  1883. }
  1884. static const struct dsp_image_seg *get_next_seg_ptr(
  1885. const struct dsp_image_seg *p)
  1886. {
  1887. return (struct dsp_image_seg *)((unsigned char *)(p) + dsp_sizeof(p));
  1888. }
  1889. /*
  1890. * CA0132 chip DSP transfer stuffs. For DSP download.
  1891. */
  1892. #define INVALID_DMA_CHANNEL (~0U)
  1893. /*
  1894. * Program a list of address/data pairs via the ChipIO widget.
  1895. * The segment data is in the format of successive pairs of words.
  1896. * These are repeated as indicated by the segment's count field.
  1897. */
  1898. static int dspxfr_hci_write(struct hda_codec *codec,
  1899. const struct dsp_image_seg *fls)
  1900. {
  1901. int status;
  1902. const u32 *data;
  1903. unsigned int count;
  1904. if (fls == NULL || fls->chip_addr != g_chip_addr_magic_value) {
  1905. codec_dbg(codec, "hci_write invalid params\n");
  1906. return -EINVAL;
  1907. }
  1908. count = fls->count;
  1909. data = (u32 *)(fls->data);
  1910. while (count >= 2) {
  1911. status = chipio_write(codec, data[0], data[1]);
  1912. if (status < 0) {
  1913. codec_dbg(codec, "hci_write chipio failed\n");
  1914. return status;
  1915. }
  1916. count -= 2;
  1917. data += 2;
  1918. }
  1919. return 0;
  1920. }
  1921. /**
  1922. * Write a block of data into DSP code or data RAM using pre-allocated
  1923. * DMA engine.
  1924. *
  1925. * @codec: the HDA codec
  1926. * @fls: pointer to a fast load image
  1927. * @reloc: Relocation address for loading single-segment overlays, or 0 for
  1928. * no relocation
  1929. * @dma_engine: pointer to DMA engine to be used for DSP download
  1930. * @dma_chan: The number of DMA channels used for DSP download
  1931. * @port_map_mask: port mapping
  1932. * @ovly: TRUE if overlay format is required
  1933. *
  1934. * Returns zero or a negative error code.
  1935. */
  1936. static int dspxfr_one_seg(struct hda_codec *codec,
  1937. const struct dsp_image_seg *fls,
  1938. unsigned int reloc,
  1939. struct dma_engine *dma_engine,
  1940. unsigned int dma_chan,
  1941. unsigned int port_map_mask,
  1942. bool ovly)
  1943. {
  1944. int status = 0;
  1945. bool comm_dma_setup_done = false;
  1946. const unsigned int *data;
  1947. unsigned int chip_addx;
  1948. unsigned int words_to_write;
  1949. unsigned int buffer_size_words;
  1950. unsigned char *buffer_addx;
  1951. unsigned short hda_format;
  1952. unsigned int sample_rate_div;
  1953. unsigned int sample_rate_mul;
  1954. unsigned int num_chans;
  1955. unsigned int hda_frame_size_words;
  1956. unsigned int remainder_words;
  1957. const u32 *data_remainder;
  1958. u32 chip_addx_remainder;
  1959. unsigned int run_size_words;
  1960. const struct dsp_image_seg *hci_write = NULL;
  1961. unsigned long timeout;
  1962. bool dma_active;
  1963. if (fls == NULL)
  1964. return -EINVAL;
  1965. if (is_hci_prog_list_seg(fls)) {
  1966. hci_write = fls;
  1967. fls = get_next_seg_ptr(fls);
  1968. }
  1969. if (hci_write && (!fls || is_last(fls))) {
  1970. codec_dbg(codec, "hci_write\n");
  1971. return dspxfr_hci_write(codec, hci_write);
  1972. }
  1973. if (fls == NULL || dma_engine == NULL || port_map_mask == 0) {
  1974. codec_dbg(codec, "Invalid Params\n");
  1975. return -EINVAL;
  1976. }
  1977. data = fls->data;
  1978. chip_addx = fls->chip_addr,
  1979. words_to_write = fls->count;
  1980. if (!words_to_write)
  1981. return hci_write ? dspxfr_hci_write(codec, hci_write) : 0;
  1982. if (reloc)
  1983. chip_addx = (chip_addx & (0xFFFF0000 << 2)) + (reloc << 2);
  1984. if (!UC_RANGE(chip_addx, words_to_write) &&
  1985. !X_RANGE_ALL(chip_addx, words_to_write) &&
  1986. !Y_RANGE_ALL(chip_addx, words_to_write)) {
  1987. codec_dbg(codec, "Invalid chip_addx Params\n");
  1988. return -EINVAL;
  1989. }
  1990. buffer_size_words = (unsigned int)dma_get_buffer_size(dma_engine) /
  1991. sizeof(u32);
  1992. buffer_addx = dma_get_buffer_addr(dma_engine);
  1993. if (buffer_addx == NULL) {
  1994. codec_dbg(codec, "dma_engine buffer NULL\n");
  1995. return -EINVAL;
  1996. }
  1997. dma_get_converter_format(dma_engine, &hda_format);
  1998. sample_rate_div = ((get_hdafmt_rate(hda_format) >> 0) & 3) + 1;
  1999. sample_rate_mul = ((get_hdafmt_rate(hda_format) >> 3) & 3) + 1;
  2000. num_chans = get_hdafmt_chs(hda_format) + 1;
  2001. hda_frame_size_words = ((sample_rate_div == 0) ? 0 :
  2002. (num_chans * sample_rate_mul / sample_rate_div));
  2003. if (hda_frame_size_words == 0) {
  2004. codec_dbg(codec, "frmsz zero\n");
  2005. return -EINVAL;
  2006. }
  2007. buffer_size_words = min(buffer_size_words,
  2008. (unsigned int)(UC_RANGE(chip_addx, 1) ?
  2009. 65536 : 32768));
  2010. buffer_size_words -= buffer_size_words % hda_frame_size_words;
  2011. codec_dbg(codec,
  2012. "chpadr=0x%08x frmsz=%u nchan=%u "
  2013. "rate_mul=%u div=%u bufsz=%u\n",
  2014. chip_addx, hda_frame_size_words, num_chans,
  2015. sample_rate_mul, sample_rate_div, buffer_size_words);
  2016. if (buffer_size_words < hda_frame_size_words) {
  2017. codec_dbg(codec, "dspxfr_one_seg:failed\n");
  2018. return -EINVAL;
  2019. }
  2020. remainder_words = words_to_write % hda_frame_size_words;
  2021. data_remainder = data;
  2022. chip_addx_remainder = chip_addx;
  2023. data += remainder_words;
  2024. chip_addx += remainder_words*sizeof(u32);
  2025. words_to_write -= remainder_words;
  2026. while (words_to_write != 0) {
  2027. run_size_words = min(buffer_size_words, words_to_write);
  2028. codec_dbg(codec, "dspxfr (seg loop)cnt=%u rs=%u remainder=%u\n",
  2029. words_to_write, run_size_words, remainder_words);
  2030. dma_xfer(dma_engine, data, run_size_words*sizeof(u32));
  2031. if (!comm_dma_setup_done) {
  2032. status = dsp_dma_stop(codec, dma_chan, ovly);
  2033. if (status < 0)
  2034. return status;
  2035. status = dsp_dma_setup_common(codec, chip_addx,
  2036. dma_chan, port_map_mask, ovly);
  2037. if (status < 0)
  2038. return status;
  2039. comm_dma_setup_done = true;
  2040. }
  2041. status = dsp_dma_setup(codec, chip_addx,
  2042. run_size_words, dma_chan);
  2043. if (status < 0)
  2044. return status;
  2045. status = dsp_dma_start(codec, dma_chan, ovly);
  2046. if (status < 0)
  2047. return status;
  2048. if (!dsp_is_dma_active(codec, dma_chan)) {
  2049. codec_dbg(codec, "dspxfr:DMA did not start\n");
  2050. return -EIO;
  2051. }
  2052. status = dma_set_state(dma_engine, DMA_STATE_RUN);
  2053. if (status < 0)
  2054. return status;
  2055. if (remainder_words != 0) {
  2056. status = chipio_write_multiple(codec,
  2057. chip_addx_remainder,
  2058. data_remainder,
  2059. remainder_words);
  2060. if (status < 0)
  2061. return status;
  2062. remainder_words = 0;
  2063. }
  2064. if (hci_write) {
  2065. status = dspxfr_hci_write(codec, hci_write);
  2066. if (status < 0)
  2067. return status;
  2068. hci_write = NULL;
  2069. }
  2070. timeout = jiffies + msecs_to_jiffies(2000);
  2071. do {
  2072. dma_active = dsp_is_dma_active(codec, dma_chan);
  2073. if (!dma_active)
  2074. break;
  2075. msleep(20);
  2076. } while (time_before(jiffies, timeout));
  2077. if (dma_active)
  2078. break;
  2079. codec_dbg(codec, "+++++ DMA complete\n");
  2080. dma_set_state(dma_engine, DMA_STATE_STOP);
  2081. status = dma_reset(dma_engine);
  2082. if (status < 0)
  2083. return status;
  2084. data += run_size_words;
  2085. chip_addx += run_size_words*sizeof(u32);
  2086. words_to_write -= run_size_words;
  2087. }
  2088. if (remainder_words != 0) {
  2089. status = chipio_write_multiple(codec, chip_addx_remainder,
  2090. data_remainder, remainder_words);
  2091. }
  2092. return status;
  2093. }
  2094. /**
  2095. * Write the entire DSP image of a DSP code/data overlay to DSP memories
  2096. *
  2097. * @codec: the HDA codec
  2098. * @fls_data: pointer to a fast load image
  2099. * @reloc: Relocation address for loading single-segment overlays, or 0 for
  2100. * no relocation
  2101. * @sample_rate: sampling rate of the stream used for DSP download
  2102. * @channels: channels of the stream used for DSP download
  2103. * @ovly: TRUE if overlay format is required
  2104. *
  2105. * Returns zero or a negative error code.
  2106. */
  2107. static int dspxfr_image(struct hda_codec *codec,
  2108. const struct dsp_image_seg *fls_data,
  2109. unsigned int reloc,
  2110. unsigned int sample_rate,
  2111. unsigned short channels,
  2112. bool ovly)
  2113. {
  2114. struct ca0132_spec *spec = codec->spec;
  2115. int status;
  2116. unsigned short hda_format = 0;
  2117. unsigned int response;
  2118. unsigned char stream_id = 0;
  2119. struct dma_engine *dma_engine;
  2120. unsigned int dma_chan;
  2121. unsigned int port_map_mask;
  2122. if (fls_data == NULL)
  2123. return -EINVAL;
  2124. dma_engine = kzalloc(sizeof(*dma_engine), GFP_KERNEL);
  2125. if (!dma_engine)
  2126. return -ENOMEM;
  2127. dma_engine->dmab = kzalloc(sizeof(*dma_engine->dmab), GFP_KERNEL);
  2128. if (!dma_engine->dmab) {
  2129. kfree(dma_engine);
  2130. return -ENOMEM;
  2131. }
  2132. dma_engine->codec = codec;
  2133. dma_convert_to_hda_format(codec, sample_rate, channels, &hda_format);
  2134. dma_engine->m_converter_format = hda_format;
  2135. dma_engine->buf_size = (ovly ? DSP_DMA_WRITE_BUFLEN_OVLY :
  2136. DSP_DMA_WRITE_BUFLEN_INIT) * 2;
  2137. dma_chan = ovly ? INVALID_DMA_CHANNEL : 0;
  2138. status = codec_set_converter_format(codec, WIDGET_CHIP_CTRL,
  2139. hda_format, &response);
  2140. if (status < 0) {
  2141. codec_dbg(codec, "set converter format fail\n");
  2142. goto exit;
  2143. }
  2144. status = snd_hda_codec_load_dsp_prepare(codec,
  2145. dma_engine->m_converter_format,
  2146. dma_engine->buf_size,
  2147. dma_engine->dmab);
  2148. if (status < 0)
  2149. goto exit;
  2150. spec->dsp_stream_id = status;
  2151. if (ovly) {
  2152. status = dspio_alloc_dma_chan(codec, &dma_chan);
  2153. if (status < 0) {
  2154. codec_dbg(codec, "alloc dmachan fail\n");
  2155. dma_chan = INVALID_DMA_CHANNEL;
  2156. goto exit;
  2157. }
  2158. }
  2159. port_map_mask = 0;
  2160. status = dsp_allocate_ports_format(codec, hda_format,
  2161. &port_map_mask);
  2162. if (status < 0) {
  2163. codec_dbg(codec, "alloc ports fail\n");
  2164. goto exit;
  2165. }
  2166. stream_id = dma_get_stream_id(dma_engine);
  2167. status = codec_set_converter_stream_channel(codec,
  2168. WIDGET_CHIP_CTRL, stream_id, 0, &response);
  2169. if (status < 0) {
  2170. codec_dbg(codec, "set stream chan fail\n");
  2171. goto exit;
  2172. }
  2173. while ((fls_data != NULL) && !is_last(fls_data)) {
  2174. if (!is_valid(fls_data)) {
  2175. codec_dbg(codec, "FLS check fail\n");
  2176. status = -EINVAL;
  2177. goto exit;
  2178. }
  2179. status = dspxfr_one_seg(codec, fls_data, reloc,
  2180. dma_engine, dma_chan,
  2181. port_map_mask, ovly);
  2182. if (status < 0)
  2183. break;
  2184. if (is_hci_prog_list_seg(fls_data))
  2185. fls_data = get_next_seg_ptr(fls_data);
  2186. if ((fls_data != NULL) && !is_last(fls_data))
  2187. fls_data = get_next_seg_ptr(fls_data);
  2188. }
  2189. if (port_map_mask != 0)
  2190. status = dsp_free_ports(codec);
  2191. if (status < 0)
  2192. goto exit;
  2193. status = codec_set_converter_stream_channel(codec,
  2194. WIDGET_CHIP_CTRL, 0, 0, &response);
  2195. exit:
  2196. if (ovly && (dma_chan != INVALID_DMA_CHANNEL))
  2197. dspio_free_dma_chan(codec, dma_chan);
  2198. if (dma_engine->dmab->area)
  2199. snd_hda_codec_load_dsp_cleanup(codec, dma_engine->dmab);
  2200. kfree(dma_engine->dmab);
  2201. kfree(dma_engine);
  2202. return status;
  2203. }
  2204. /*
  2205. * CA0132 DSP download stuffs.
  2206. */
  2207. static void dspload_post_setup(struct hda_codec *codec)
  2208. {
  2209. codec_dbg(codec, "---- dspload_post_setup ------\n");
  2210. /*set DSP speaker to 2.0 configuration*/
  2211. chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x18), 0x08080080);
  2212. chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x19), 0x3f800000);
  2213. /*update write pointer*/
  2214. chipio_write(codec, XRAM_XRAM_INST_OFFSET(0x29), 0x00000002);
  2215. }
  2216. /**
  2217. * dspload_image - Download DSP from a DSP Image Fast Load structure.
  2218. *
  2219. * @codec: the HDA codec
  2220. * @fls: pointer to a fast load image
  2221. * @ovly: TRUE if overlay format is required
  2222. * @reloc: Relocation address for loading single-segment overlays, or 0 for
  2223. * no relocation
  2224. * @autostart: TRUE if DSP starts after loading; ignored if ovly is TRUE
  2225. * @router_chans: number of audio router channels to be allocated (0 means use
  2226. * internal defaults; max is 32)
  2227. *
  2228. * Download DSP from a DSP Image Fast Load structure. This structure is a
  2229. * linear, non-constant sized element array of structures, each of which
  2230. * contain the count of the data to be loaded, the data itself, and the
  2231. * corresponding starting chip address of the starting data location.
  2232. * Returns zero or a negative error code.
  2233. */
  2234. static int dspload_image(struct hda_codec *codec,
  2235. const struct dsp_image_seg *fls,
  2236. bool ovly,
  2237. unsigned int reloc,
  2238. bool autostart,
  2239. int router_chans)
  2240. {
  2241. int status = 0;
  2242. unsigned int sample_rate;
  2243. unsigned short channels;
  2244. codec_dbg(codec, "---- dspload_image begin ------\n");
  2245. if (router_chans == 0) {
  2246. if (!ovly)
  2247. router_chans = DMA_TRANSFER_FRAME_SIZE_NWORDS;
  2248. else
  2249. router_chans = DMA_OVERLAY_FRAME_SIZE_NWORDS;
  2250. }
  2251. sample_rate = 48000;
  2252. channels = (unsigned short)router_chans;
  2253. while (channels > 16) {
  2254. sample_rate *= 2;
  2255. channels /= 2;
  2256. }
  2257. do {
  2258. codec_dbg(codec, "Ready to program DMA\n");
  2259. if (!ovly)
  2260. status = dsp_reset(codec);
  2261. if (status < 0)
  2262. break;
  2263. codec_dbg(codec, "dsp_reset() complete\n");
  2264. status = dspxfr_image(codec, fls, reloc, sample_rate, channels,
  2265. ovly);
  2266. if (status < 0)
  2267. break;
  2268. codec_dbg(codec, "dspxfr_image() complete\n");
  2269. if (autostart && !ovly) {
  2270. dspload_post_setup(codec);
  2271. status = dsp_set_run_state(codec);
  2272. }
  2273. codec_dbg(codec, "LOAD FINISHED\n");
  2274. } while (0);
  2275. return status;
  2276. }
  2277. #ifdef CONFIG_SND_HDA_CODEC_CA0132_DSP
  2278. static bool dspload_is_loaded(struct hda_codec *codec)
  2279. {
  2280. unsigned int data = 0;
  2281. int status = 0;
  2282. status = chipio_read(codec, 0x40004, &data);
  2283. if ((status < 0) || (data != 1))
  2284. return false;
  2285. return true;
  2286. }
  2287. #else
  2288. #define dspload_is_loaded(codec) false
  2289. #endif
  2290. static bool dspload_wait_loaded(struct hda_codec *codec)
  2291. {
  2292. unsigned long timeout = jiffies + msecs_to_jiffies(2000);
  2293. do {
  2294. if (dspload_is_loaded(codec)) {
  2295. codec_info(codec, "ca0132 DSP downloaded and running\n");
  2296. return true;
  2297. }
  2298. msleep(20);
  2299. } while (time_before(jiffies, timeout));
  2300. codec_err(codec, "ca0132 failed to download DSP\n");
  2301. return false;
  2302. }
  2303. /*
  2304. * PCM callbacks
  2305. */
  2306. static int ca0132_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  2307. struct hda_codec *codec,
  2308. unsigned int stream_tag,
  2309. unsigned int format,
  2310. struct snd_pcm_substream *substream)
  2311. {
  2312. struct ca0132_spec *spec = codec->spec;
  2313. snd_hda_codec_setup_stream(codec, spec->dacs[0], stream_tag, 0, format);
  2314. return 0;
  2315. }
  2316. static int ca0132_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
  2317. struct hda_codec *codec,
  2318. struct snd_pcm_substream *substream)
  2319. {
  2320. struct ca0132_spec *spec = codec->spec;
  2321. if (spec->dsp_state == DSP_DOWNLOADING)
  2322. return 0;
  2323. /*If Playback effects are on, allow stream some time to flush
  2324. *effects tail*/
  2325. if (spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID])
  2326. msleep(50);
  2327. snd_hda_codec_cleanup_stream(codec, spec->dacs[0]);
  2328. return 0;
  2329. }
  2330. static unsigned int ca0132_playback_pcm_delay(struct hda_pcm_stream *info,
  2331. struct hda_codec *codec,
  2332. struct snd_pcm_substream *substream)
  2333. {
  2334. struct ca0132_spec *spec = codec->spec;
  2335. unsigned int latency = DSP_PLAYBACK_INIT_LATENCY;
  2336. struct snd_pcm_runtime *runtime = substream->runtime;
  2337. if (spec->dsp_state != DSP_DOWNLOADED)
  2338. return 0;
  2339. /* Add latency if playback enhancement and either effect is enabled. */
  2340. if (spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID]) {
  2341. if ((spec->effects_switch[SURROUND - EFFECT_START_NID]) ||
  2342. (spec->effects_switch[DIALOG_PLUS - EFFECT_START_NID]))
  2343. latency += DSP_PLAY_ENHANCEMENT_LATENCY;
  2344. }
  2345. /* Applying Speaker EQ adds latency as well. */
  2346. if (spec->cur_out_type == SPEAKER_OUT)
  2347. latency += DSP_SPEAKER_OUT_LATENCY;
  2348. return (latency * runtime->rate) / 1000;
  2349. }
  2350. /*
  2351. * Digital out
  2352. */
  2353. static int ca0132_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
  2354. struct hda_codec *codec,
  2355. struct snd_pcm_substream *substream)
  2356. {
  2357. struct ca0132_spec *spec = codec->spec;
  2358. return snd_hda_multi_out_dig_open(codec, &spec->multiout);
  2359. }
  2360. static int ca0132_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  2361. struct hda_codec *codec,
  2362. unsigned int stream_tag,
  2363. unsigned int format,
  2364. struct snd_pcm_substream *substream)
  2365. {
  2366. struct ca0132_spec *spec = codec->spec;
  2367. return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
  2368. stream_tag, format, substream);
  2369. }
  2370. static int ca0132_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
  2371. struct hda_codec *codec,
  2372. struct snd_pcm_substream *substream)
  2373. {
  2374. struct ca0132_spec *spec = codec->spec;
  2375. return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
  2376. }
  2377. static int ca0132_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
  2378. struct hda_codec *codec,
  2379. struct snd_pcm_substream *substream)
  2380. {
  2381. struct ca0132_spec *spec = codec->spec;
  2382. return snd_hda_multi_out_dig_close(codec, &spec->multiout);
  2383. }
  2384. /*
  2385. * Analog capture
  2386. */
  2387. static int ca0132_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
  2388. struct hda_codec *codec,
  2389. unsigned int stream_tag,
  2390. unsigned int format,
  2391. struct snd_pcm_substream *substream)
  2392. {
  2393. snd_hda_codec_setup_stream(codec, hinfo->nid,
  2394. stream_tag, 0, format);
  2395. return 0;
  2396. }
  2397. static int ca0132_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
  2398. struct hda_codec *codec,
  2399. struct snd_pcm_substream *substream)
  2400. {
  2401. struct ca0132_spec *spec = codec->spec;
  2402. if (spec->dsp_state == DSP_DOWNLOADING)
  2403. return 0;
  2404. snd_hda_codec_cleanup_stream(codec, hinfo->nid);
  2405. return 0;
  2406. }
  2407. static unsigned int ca0132_capture_pcm_delay(struct hda_pcm_stream *info,
  2408. struct hda_codec *codec,
  2409. struct snd_pcm_substream *substream)
  2410. {
  2411. struct ca0132_spec *spec = codec->spec;
  2412. unsigned int latency = DSP_CAPTURE_INIT_LATENCY;
  2413. struct snd_pcm_runtime *runtime = substream->runtime;
  2414. if (spec->dsp_state != DSP_DOWNLOADED)
  2415. return 0;
  2416. if (spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID])
  2417. latency += DSP_CRYSTAL_VOICE_LATENCY;
  2418. return (latency * runtime->rate) / 1000;
  2419. }
  2420. /*
  2421. * Controls stuffs.
  2422. */
  2423. /*
  2424. * Mixer controls helpers.
  2425. */
  2426. #define CA0132_CODEC_VOL_MONO(xname, nid, channel, dir) \
  2427. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  2428. .name = xname, \
  2429. .subdevice = HDA_SUBDEV_AMP_FLAG, \
  2430. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
  2431. SNDRV_CTL_ELEM_ACCESS_TLV_READ | \
  2432. SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK, \
  2433. .info = ca0132_volume_info, \
  2434. .get = ca0132_volume_get, \
  2435. .put = ca0132_volume_put, \
  2436. .tlv = { .c = ca0132_volume_tlv }, \
  2437. .private_value = HDA_COMPOSE_AMP_VAL(nid, channel, 0, dir) }
  2438. #define CA0132_CODEC_MUTE_MONO(xname, nid, channel, dir) \
  2439. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  2440. .name = xname, \
  2441. .subdevice = HDA_SUBDEV_AMP_FLAG, \
  2442. .info = snd_hda_mixer_amp_switch_info, \
  2443. .get = ca0132_switch_get, \
  2444. .put = ca0132_switch_put, \
  2445. .private_value = HDA_COMPOSE_AMP_VAL(nid, channel, 0, dir) }
  2446. /* stereo */
  2447. #define CA0132_CODEC_VOL(xname, nid, dir) \
  2448. CA0132_CODEC_VOL_MONO(xname, nid, 3, dir)
  2449. #define CA0132_CODEC_MUTE(xname, nid, dir) \
  2450. CA0132_CODEC_MUTE_MONO(xname, nid, 3, dir)
  2451. /* The followings are for tuning of products */
  2452. #ifdef ENABLE_TUNING_CONTROLS
  2453. static unsigned int voice_focus_vals_lookup[] = {
  2454. 0x41A00000, 0x41A80000, 0x41B00000, 0x41B80000, 0x41C00000, 0x41C80000,
  2455. 0x41D00000, 0x41D80000, 0x41E00000, 0x41E80000, 0x41F00000, 0x41F80000,
  2456. 0x42000000, 0x42040000, 0x42080000, 0x420C0000, 0x42100000, 0x42140000,
  2457. 0x42180000, 0x421C0000, 0x42200000, 0x42240000, 0x42280000, 0x422C0000,
  2458. 0x42300000, 0x42340000, 0x42380000, 0x423C0000, 0x42400000, 0x42440000,
  2459. 0x42480000, 0x424C0000, 0x42500000, 0x42540000, 0x42580000, 0x425C0000,
  2460. 0x42600000, 0x42640000, 0x42680000, 0x426C0000, 0x42700000, 0x42740000,
  2461. 0x42780000, 0x427C0000, 0x42800000, 0x42820000, 0x42840000, 0x42860000,
  2462. 0x42880000, 0x428A0000, 0x428C0000, 0x428E0000, 0x42900000, 0x42920000,
  2463. 0x42940000, 0x42960000, 0x42980000, 0x429A0000, 0x429C0000, 0x429E0000,
  2464. 0x42A00000, 0x42A20000, 0x42A40000, 0x42A60000, 0x42A80000, 0x42AA0000,
  2465. 0x42AC0000, 0x42AE0000, 0x42B00000, 0x42B20000, 0x42B40000, 0x42B60000,
  2466. 0x42B80000, 0x42BA0000, 0x42BC0000, 0x42BE0000, 0x42C00000, 0x42C20000,
  2467. 0x42C40000, 0x42C60000, 0x42C80000, 0x42CA0000, 0x42CC0000, 0x42CE0000,
  2468. 0x42D00000, 0x42D20000, 0x42D40000, 0x42D60000, 0x42D80000, 0x42DA0000,
  2469. 0x42DC0000, 0x42DE0000, 0x42E00000, 0x42E20000, 0x42E40000, 0x42E60000,
  2470. 0x42E80000, 0x42EA0000, 0x42EC0000, 0x42EE0000, 0x42F00000, 0x42F20000,
  2471. 0x42F40000, 0x42F60000, 0x42F80000, 0x42FA0000, 0x42FC0000, 0x42FE0000,
  2472. 0x43000000, 0x43010000, 0x43020000, 0x43030000, 0x43040000, 0x43050000,
  2473. 0x43060000, 0x43070000, 0x43080000, 0x43090000, 0x430A0000, 0x430B0000,
  2474. 0x430C0000, 0x430D0000, 0x430E0000, 0x430F0000, 0x43100000, 0x43110000,
  2475. 0x43120000, 0x43130000, 0x43140000, 0x43150000, 0x43160000, 0x43170000,
  2476. 0x43180000, 0x43190000, 0x431A0000, 0x431B0000, 0x431C0000, 0x431D0000,
  2477. 0x431E0000, 0x431F0000, 0x43200000, 0x43210000, 0x43220000, 0x43230000,
  2478. 0x43240000, 0x43250000, 0x43260000, 0x43270000, 0x43280000, 0x43290000,
  2479. 0x432A0000, 0x432B0000, 0x432C0000, 0x432D0000, 0x432E0000, 0x432F0000,
  2480. 0x43300000, 0x43310000, 0x43320000, 0x43330000, 0x43340000
  2481. };
  2482. static unsigned int mic_svm_vals_lookup[] = {
  2483. 0x00000000, 0x3C23D70A, 0x3CA3D70A, 0x3CF5C28F, 0x3D23D70A, 0x3D4CCCCD,
  2484. 0x3D75C28F, 0x3D8F5C29, 0x3DA3D70A, 0x3DB851EC, 0x3DCCCCCD, 0x3DE147AE,
  2485. 0x3DF5C28F, 0x3E051EB8, 0x3E0F5C29, 0x3E19999A, 0x3E23D70A, 0x3E2E147B,
  2486. 0x3E3851EC, 0x3E428F5C, 0x3E4CCCCD, 0x3E570A3D, 0x3E6147AE, 0x3E6B851F,
  2487. 0x3E75C28F, 0x3E800000, 0x3E851EB8, 0x3E8A3D71, 0x3E8F5C29, 0x3E947AE1,
  2488. 0x3E99999A, 0x3E9EB852, 0x3EA3D70A, 0x3EA8F5C3, 0x3EAE147B, 0x3EB33333,
  2489. 0x3EB851EC, 0x3EBD70A4, 0x3EC28F5C, 0x3EC7AE14, 0x3ECCCCCD, 0x3ED1EB85,
  2490. 0x3ED70A3D, 0x3EDC28F6, 0x3EE147AE, 0x3EE66666, 0x3EEB851F, 0x3EF0A3D7,
  2491. 0x3EF5C28F, 0x3EFAE148, 0x3F000000, 0x3F028F5C, 0x3F051EB8, 0x3F07AE14,
  2492. 0x3F0A3D71, 0x3F0CCCCD, 0x3F0F5C29, 0x3F11EB85, 0x3F147AE1, 0x3F170A3D,
  2493. 0x3F19999A, 0x3F1C28F6, 0x3F1EB852, 0x3F2147AE, 0x3F23D70A, 0x3F266666,
  2494. 0x3F28F5C3, 0x3F2B851F, 0x3F2E147B, 0x3F30A3D7, 0x3F333333, 0x3F35C28F,
  2495. 0x3F3851EC, 0x3F3AE148, 0x3F3D70A4, 0x3F400000, 0x3F428F5C, 0x3F451EB8,
  2496. 0x3F47AE14, 0x3F4A3D71, 0x3F4CCCCD, 0x3F4F5C29, 0x3F51EB85, 0x3F547AE1,
  2497. 0x3F570A3D, 0x3F59999A, 0x3F5C28F6, 0x3F5EB852, 0x3F6147AE, 0x3F63D70A,
  2498. 0x3F666666, 0x3F68F5C3, 0x3F6B851F, 0x3F6E147B, 0x3F70A3D7, 0x3F733333,
  2499. 0x3F75C28F, 0x3F7851EC, 0x3F7AE148, 0x3F7D70A4, 0x3F800000
  2500. };
  2501. static unsigned int equalizer_vals_lookup[] = {
  2502. 0xC1C00000, 0xC1B80000, 0xC1B00000, 0xC1A80000, 0xC1A00000, 0xC1980000,
  2503. 0xC1900000, 0xC1880000, 0xC1800000, 0xC1700000, 0xC1600000, 0xC1500000,
  2504. 0xC1400000, 0xC1300000, 0xC1200000, 0xC1100000, 0xC1000000, 0xC0E00000,
  2505. 0xC0C00000, 0xC0A00000, 0xC0800000, 0xC0400000, 0xC0000000, 0xBF800000,
  2506. 0x00000000, 0x3F800000, 0x40000000, 0x40400000, 0x40800000, 0x40A00000,
  2507. 0x40C00000, 0x40E00000, 0x41000000, 0x41100000, 0x41200000, 0x41300000,
  2508. 0x41400000, 0x41500000, 0x41600000, 0x41700000, 0x41800000, 0x41880000,
  2509. 0x41900000, 0x41980000, 0x41A00000, 0x41A80000, 0x41B00000, 0x41B80000,
  2510. 0x41C00000
  2511. };
  2512. static int tuning_ctl_set(struct hda_codec *codec, hda_nid_t nid,
  2513. unsigned int *lookup, int idx)
  2514. {
  2515. int i = 0;
  2516. for (i = 0; i < TUNING_CTLS_COUNT; i++)
  2517. if (nid == ca0132_tuning_ctls[i].nid)
  2518. break;
  2519. snd_hda_power_up(codec);
  2520. dspio_set_param(codec, ca0132_tuning_ctls[i].mid,
  2521. ca0132_tuning_ctls[i].req,
  2522. &(lookup[idx]), sizeof(unsigned int));
  2523. snd_hda_power_down(codec);
  2524. return 1;
  2525. }
  2526. static int tuning_ctl_get(struct snd_kcontrol *kcontrol,
  2527. struct snd_ctl_elem_value *ucontrol)
  2528. {
  2529. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2530. struct ca0132_spec *spec = codec->spec;
  2531. hda_nid_t nid = get_amp_nid(kcontrol);
  2532. long *valp = ucontrol->value.integer.value;
  2533. int idx = nid - TUNING_CTL_START_NID;
  2534. *valp = spec->cur_ctl_vals[idx];
  2535. return 0;
  2536. }
  2537. static int voice_focus_ctl_info(struct snd_kcontrol *kcontrol,
  2538. struct snd_ctl_elem_info *uinfo)
  2539. {
  2540. int chs = get_amp_channels(kcontrol);
  2541. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2542. uinfo->count = chs == 3 ? 2 : 1;
  2543. uinfo->value.integer.min = 20;
  2544. uinfo->value.integer.max = 180;
  2545. uinfo->value.integer.step = 1;
  2546. return 0;
  2547. }
  2548. static int voice_focus_ctl_put(struct snd_kcontrol *kcontrol,
  2549. struct snd_ctl_elem_value *ucontrol)
  2550. {
  2551. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2552. struct ca0132_spec *spec = codec->spec;
  2553. hda_nid_t nid = get_amp_nid(kcontrol);
  2554. long *valp = ucontrol->value.integer.value;
  2555. int idx;
  2556. idx = nid - TUNING_CTL_START_NID;
  2557. /* any change? */
  2558. if (spec->cur_ctl_vals[idx] == *valp)
  2559. return 0;
  2560. spec->cur_ctl_vals[idx] = *valp;
  2561. idx = *valp - 20;
  2562. tuning_ctl_set(codec, nid, voice_focus_vals_lookup, idx);
  2563. return 1;
  2564. }
  2565. static int mic_svm_ctl_info(struct snd_kcontrol *kcontrol,
  2566. struct snd_ctl_elem_info *uinfo)
  2567. {
  2568. int chs = get_amp_channels(kcontrol);
  2569. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2570. uinfo->count = chs == 3 ? 2 : 1;
  2571. uinfo->value.integer.min = 0;
  2572. uinfo->value.integer.max = 100;
  2573. uinfo->value.integer.step = 1;
  2574. return 0;
  2575. }
  2576. static int mic_svm_ctl_put(struct snd_kcontrol *kcontrol,
  2577. struct snd_ctl_elem_value *ucontrol)
  2578. {
  2579. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2580. struct ca0132_spec *spec = codec->spec;
  2581. hda_nid_t nid = get_amp_nid(kcontrol);
  2582. long *valp = ucontrol->value.integer.value;
  2583. int idx;
  2584. idx = nid - TUNING_CTL_START_NID;
  2585. /* any change? */
  2586. if (spec->cur_ctl_vals[idx] == *valp)
  2587. return 0;
  2588. spec->cur_ctl_vals[idx] = *valp;
  2589. idx = *valp;
  2590. tuning_ctl_set(codec, nid, mic_svm_vals_lookup, idx);
  2591. return 0;
  2592. }
  2593. static int equalizer_ctl_info(struct snd_kcontrol *kcontrol,
  2594. struct snd_ctl_elem_info *uinfo)
  2595. {
  2596. int chs = get_amp_channels(kcontrol);
  2597. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2598. uinfo->count = chs == 3 ? 2 : 1;
  2599. uinfo->value.integer.min = 0;
  2600. uinfo->value.integer.max = 48;
  2601. uinfo->value.integer.step = 1;
  2602. return 0;
  2603. }
  2604. static int equalizer_ctl_put(struct snd_kcontrol *kcontrol,
  2605. struct snd_ctl_elem_value *ucontrol)
  2606. {
  2607. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2608. struct ca0132_spec *spec = codec->spec;
  2609. hda_nid_t nid = get_amp_nid(kcontrol);
  2610. long *valp = ucontrol->value.integer.value;
  2611. int idx;
  2612. idx = nid - TUNING_CTL_START_NID;
  2613. /* any change? */
  2614. if (spec->cur_ctl_vals[idx] == *valp)
  2615. return 0;
  2616. spec->cur_ctl_vals[idx] = *valp;
  2617. idx = *valp;
  2618. tuning_ctl_set(codec, nid, equalizer_vals_lookup, idx);
  2619. return 1;
  2620. }
  2621. static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(voice_focus_db_scale, 2000, 100, 0);
  2622. static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(eq_db_scale, -2400, 100, 0);
  2623. static int add_tuning_control(struct hda_codec *codec,
  2624. hda_nid_t pnid, hda_nid_t nid,
  2625. const char *name, int dir)
  2626. {
  2627. char namestr[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
  2628. int type = dir ? HDA_INPUT : HDA_OUTPUT;
  2629. struct snd_kcontrol_new knew =
  2630. HDA_CODEC_VOLUME_MONO(namestr, nid, 1, 0, type);
  2631. knew.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  2632. SNDRV_CTL_ELEM_ACCESS_TLV_READ;
  2633. knew.tlv.c = 0;
  2634. knew.tlv.p = 0;
  2635. switch (pnid) {
  2636. case VOICE_FOCUS:
  2637. knew.info = voice_focus_ctl_info;
  2638. knew.get = tuning_ctl_get;
  2639. knew.put = voice_focus_ctl_put;
  2640. knew.tlv.p = voice_focus_db_scale;
  2641. break;
  2642. case MIC_SVM:
  2643. knew.info = mic_svm_ctl_info;
  2644. knew.get = tuning_ctl_get;
  2645. knew.put = mic_svm_ctl_put;
  2646. break;
  2647. case EQUALIZER:
  2648. knew.info = equalizer_ctl_info;
  2649. knew.get = tuning_ctl_get;
  2650. knew.put = equalizer_ctl_put;
  2651. knew.tlv.p = eq_db_scale;
  2652. break;
  2653. default:
  2654. return 0;
  2655. }
  2656. knew.private_value =
  2657. HDA_COMPOSE_AMP_VAL(nid, 1, 0, type);
  2658. sprintf(namestr, "%s %s Volume", name, dirstr[dir]);
  2659. return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
  2660. }
  2661. static int add_tuning_ctls(struct hda_codec *codec)
  2662. {
  2663. int i;
  2664. int err;
  2665. for (i = 0; i < TUNING_CTLS_COUNT; i++) {
  2666. err = add_tuning_control(codec,
  2667. ca0132_tuning_ctls[i].parent_nid,
  2668. ca0132_tuning_ctls[i].nid,
  2669. ca0132_tuning_ctls[i].name,
  2670. ca0132_tuning_ctls[i].direct);
  2671. if (err < 0)
  2672. return err;
  2673. }
  2674. return 0;
  2675. }
  2676. static void ca0132_init_tuning_defaults(struct hda_codec *codec)
  2677. {
  2678. struct ca0132_spec *spec = codec->spec;
  2679. int i;
  2680. /* Wedge Angle defaults to 30. 10 below is 30 - 20. 20 is min. */
  2681. spec->cur_ctl_vals[WEDGE_ANGLE - TUNING_CTL_START_NID] = 10;
  2682. /* SVM level defaults to 0.74. */
  2683. spec->cur_ctl_vals[SVM_LEVEL - TUNING_CTL_START_NID] = 74;
  2684. /* EQ defaults to 0dB. */
  2685. for (i = 2; i < TUNING_CTLS_COUNT; i++)
  2686. spec->cur_ctl_vals[i] = 24;
  2687. }
  2688. #endif /*ENABLE_TUNING_CONTROLS*/
  2689. /*
  2690. * Select the active output.
  2691. * If autodetect is enabled, output will be selected based on jack detection.
  2692. * If jack inserted, headphone will be selected, else built-in speakers
  2693. * If autodetect is disabled, output will be selected based on selection.
  2694. */
  2695. static int ca0132_select_out(struct hda_codec *codec)
  2696. {
  2697. struct ca0132_spec *spec = codec->spec;
  2698. unsigned int pin_ctl;
  2699. int jack_present;
  2700. int auto_jack;
  2701. unsigned int tmp;
  2702. int err;
  2703. codec_dbg(codec, "ca0132_select_out\n");
  2704. snd_hda_power_up_pm(codec);
  2705. auto_jack = spec->vnode_lswitch[VNID_HP_ASEL - VNODE_START_NID];
  2706. if (auto_jack)
  2707. jack_present = snd_hda_jack_detect(codec, spec->unsol_tag_hp);
  2708. else
  2709. jack_present =
  2710. spec->vnode_lswitch[VNID_HP_SEL - VNODE_START_NID];
  2711. if (jack_present)
  2712. spec->cur_out_type = HEADPHONE_OUT;
  2713. else
  2714. spec->cur_out_type = SPEAKER_OUT;
  2715. if (spec->cur_out_type == SPEAKER_OUT) {
  2716. codec_dbg(codec, "ca0132_select_out speaker\n");
  2717. /*speaker out config*/
  2718. tmp = FLOAT_ONE;
  2719. err = dspio_set_uint_param(codec, 0x80, 0x04, tmp);
  2720. if (err < 0)
  2721. goto exit;
  2722. /*enable speaker EQ*/
  2723. tmp = FLOAT_ONE;
  2724. err = dspio_set_uint_param(codec, 0x8f, 0x00, tmp);
  2725. if (err < 0)
  2726. goto exit;
  2727. /* Setup EAPD */
  2728. snd_hda_codec_write(codec, spec->out_pins[1], 0,
  2729. VENDOR_CHIPIO_EAPD_SEL_SET, 0x02);
  2730. snd_hda_codec_write(codec, spec->out_pins[0], 0,
  2731. AC_VERB_SET_EAPD_BTLENABLE, 0x00);
  2732. snd_hda_codec_write(codec, spec->out_pins[0], 0,
  2733. VENDOR_CHIPIO_EAPD_SEL_SET, 0x00);
  2734. snd_hda_codec_write(codec, spec->out_pins[0], 0,
  2735. AC_VERB_SET_EAPD_BTLENABLE, 0x02);
  2736. /* disable headphone node */
  2737. pin_ctl = snd_hda_codec_read(codec, spec->out_pins[1], 0,
  2738. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  2739. snd_hda_set_pin_ctl(codec, spec->out_pins[1],
  2740. pin_ctl & ~PIN_HP);
  2741. /* enable speaker node */
  2742. pin_ctl = snd_hda_codec_read(codec, spec->out_pins[0], 0,
  2743. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  2744. snd_hda_set_pin_ctl(codec, spec->out_pins[0],
  2745. pin_ctl | PIN_OUT);
  2746. } else {
  2747. codec_dbg(codec, "ca0132_select_out hp\n");
  2748. /*headphone out config*/
  2749. tmp = FLOAT_ZERO;
  2750. err = dspio_set_uint_param(codec, 0x80, 0x04, tmp);
  2751. if (err < 0)
  2752. goto exit;
  2753. /*disable speaker EQ*/
  2754. tmp = FLOAT_ZERO;
  2755. err = dspio_set_uint_param(codec, 0x8f, 0x00, tmp);
  2756. if (err < 0)
  2757. goto exit;
  2758. /* Setup EAPD */
  2759. snd_hda_codec_write(codec, spec->out_pins[0], 0,
  2760. VENDOR_CHIPIO_EAPD_SEL_SET, 0x00);
  2761. snd_hda_codec_write(codec, spec->out_pins[0], 0,
  2762. AC_VERB_SET_EAPD_BTLENABLE, 0x00);
  2763. snd_hda_codec_write(codec, spec->out_pins[1], 0,
  2764. VENDOR_CHIPIO_EAPD_SEL_SET, 0x02);
  2765. snd_hda_codec_write(codec, spec->out_pins[0], 0,
  2766. AC_VERB_SET_EAPD_BTLENABLE, 0x02);
  2767. /* disable speaker*/
  2768. pin_ctl = snd_hda_codec_read(codec, spec->out_pins[0], 0,
  2769. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  2770. snd_hda_set_pin_ctl(codec, spec->out_pins[0],
  2771. pin_ctl & ~PIN_HP);
  2772. /* enable headphone*/
  2773. pin_ctl = snd_hda_codec_read(codec, spec->out_pins[1], 0,
  2774. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  2775. snd_hda_set_pin_ctl(codec, spec->out_pins[1],
  2776. pin_ctl | PIN_HP);
  2777. }
  2778. exit:
  2779. snd_hda_power_down_pm(codec);
  2780. return err < 0 ? err : 0;
  2781. }
  2782. static void ca0132_unsol_hp_delayed(struct work_struct *work)
  2783. {
  2784. struct ca0132_spec *spec = container_of(
  2785. to_delayed_work(work), struct ca0132_spec, unsol_hp_work);
  2786. struct hda_jack_tbl *jack;
  2787. ca0132_select_out(spec->codec);
  2788. jack = snd_hda_jack_tbl_get(spec->codec, spec->unsol_tag_hp);
  2789. if (jack) {
  2790. jack->block_report = 0;
  2791. snd_hda_jack_report_sync(spec->codec);
  2792. }
  2793. }
  2794. static void ca0132_set_dmic(struct hda_codec *codec, int enable);
  2795. static int ca0132_mic_boost_set(struct hda_codec *codec, long val);
  2796. static int ca0132_effects_set(struct hda_codec *codec, hda_nid_t nid, long val);
  2797. /*
  2798. * Select the active VIP source
  2799. */
  2800. static int ca0132_set_vipsource(struct hda_codec *codec, int val)
  2801. {
  2802. struct ca0132_spec *spec = codec->spec;
  2803. unsigned int tmp;
  2804. if (spec->dsp_state != DSP_DOWNLOADED)
  2805. return 0;
  2806. /* if CrystalVoice if off, vipsource should be 0 */
  2807. if (!spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID] ||
  2808. (val == 0)) {
  2809. chipio_set_control_param(codec, CONTROL_PARAM_VIP_SOURCE, 0);
  2810. chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
  2811. chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
  2812. if (spec->cur_mic_type == DIGITAL_MIC)
  2813. tmp = FLOAT_TWO;
  2814. else
  2815. tmp = FLOAT_ONE;
  2816. dspio_set_uint_param(codec, 0x80, 0x00, tmp);
  2817. tmp = FLOAT_ZERO;
  2818. dspio_set_uint_param(codec, 0x80, 0x05, tmp);
  2819. } else {
  2820. chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_16_000);
  2821. chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_16_000);
  2822. if (spec->cur_mic_type == DIGITAL_MIC)
  2823. tmp = FLOAT_TWO;
  2824. else
  2825. tmp = FLOAT_ONE;
  2826. dspio_set_uint_param(codec, 0x80, 0x00, tmp);
  2827. tmp = FLOAT_ONE;
  2828. dspio_set_uint_param(codec, 0x80, 0x05, tmp);
  2829. msleep(20);
  2830. chipio_set_control_param(codec, CONTROL_PARAM_VIP_SOURCE, val);
  2831. }
  2832. return 1;
  2833. }
  2834. /*
  2835. * Select the active microphone.
  2836. * If autodetect is enabled, mic will be selected based on jack detection.
  2837. * If jack inserted, ext.mic will be selected, else built-in mic
  2838. * If autodetect is disabled, mic will be selected based on selection.
  2839. */
  2840. static int ca0132_select_mic(struct hda_codec *codec)
  2841. {
  2842. struct ca0132_spec *spec = codec->spec;
  2843. int jack_present;
  2844. int auto_jack;
  2845. codec_dbg(codec, "ca0132_select_mic\n");
  2846. snd_hda_power_up_pm(codec);
  2847. auto_jack = spec->vnode_lswitch[VNID_AMIC1_ASEL - VNODE_START_NID];
  2848. if (auto_jack)
  2849. jack_present = snd_hda_jack_detect(codec, spec->unsol_tag_amic1);
  2850. else
  2851. jack_present =
  2852. spec->vnode_lswitch[VNID_AMIC1_SEL - VNODE_START_NID];
  2853. if (jack_present)
  2854. spec->cur_mic_type = LINE_MIC_IN;
  2855. else
  2856. spec->cur_mic_type = DIGITAL_MIC;
  2857. if (spec->cur_mic_type == DIGITAL_MIC) {
  2858. /* enable digital Mic */
  2859. chipio_set_conn_rate(codec, MEM_CONNID_DMIC, SR_32_000);
  2860. ca0132_set_dmic(codec, 1);
  2861. ca0132_mic_boost_set(codec, 0);
  2862. /* set voice focus */
  2863. ca0132_effects_set(codec, VOICE_FOCUS,
  2864. spec->effects_switch
  2865. [VOICE_FOCUS - EFFECT_START_NID]);
  2866. } else {
  2867. /* disable digital Mic */
  2868. chipio_set_conn_rate(codec, MEM_CONNID_DMIC, SR_96_000);
  2869. ca0132_set_dmic(codec, 0);
  2870. ca0132_mic_boost_set(codec, spec->cur_mic_boost);
  2871. /* disable voice focus */
  2872. ca0132_effects_set(codec, VOICE_FOCUS, 0);
  2873. }
  2874. snd_hda_power_down_pm(codec);
  2875. return 0;
  2876. }
  2877. /*
  2878. * Check if VNODE settings take effect immediately.
  2879. */
  2880. static bool ca0132_is_vnode_effective(struct hda_codec *codec,
  2881. hda_nid_t vnid,
  2882. hda_nid_t *shared_nid)
  2883. {
  2884. struct ca0132_spec *spec = codec->spec;
  2885. hda_nid_t nid;
  2886. switch (vnid) {
  2887. case VNID_SPK:
  2888. nid = spec->shared_out_nid;
  2889. break;
  2890. case VNID_MIC:
  2891. nid = spec->shared_mic_nid;
  2892. break;
  2893. default:
  2894. return false;
  2895. }
  2896. if (shared_nid)
  2897. *shared_nid = nid;
  2898. return true;
  2899. }
  2900. /*
  2901. * The following functions are control change helpers.
  2902. * They return 0 if no changed. Return 1 if changed.
  2903. */
  2904. static int ca0132_voicefx_set(struct hda_codec *codec, int enable)
  2905. {
  2906. struct ca0132_spec *spec = codec->spec;
  2907. unsigned int tmp;
  2908. /* based on CrystalVoice state to enable VoiceFX. */
  2909. if (enable) {
  2910. tmp = spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID] ?
  2911. FLOAT_ONE : FLOAT_ZERO;
  2912. } else {
  2913. tmp = FLOAT_ZERO;
  2914. }
  2915. dspio_set_uint_param(codec, ca0132_voicefx.mid,
  2916. ca0132_voicefx.reqs[0], tmp);
  2917. return 1;
  2918. }
  2919. /*
  2920. * Set the effects parameters
  2921. */
  2922. static int ca0132_effects_set(struct hda_codec *codec, hda_nid_t nid, long val)
  2923. {
  2924. struct ca0132_spec *spec = codec->spec;
  2925. unsigned int on;
  2926. int num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT;
  2927. int err = 0;
  2928. int idx = nid - EFFECT_START_NID;
  2929. if ((idx < 0) || (idx >= num_fx))
  2930. return 0; /* no changed */
  2931. /* for out effect, qualify with PE */
  2932. if ((nid >= OUT_EFFECT_START_NID) && (nid < OUT_EFFECT_END_NID)) {
  2933. /* if PE if off, turn off out effects. */
  2934. if (!spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID])
  2935. val = 0;
  2936. }
  2937. /* for in effect, qualify with CrystalVoice */
  2938. if ((nid >= IN_EFFECT_START_NID) && (nid < IN_EFFECT_END_NID)) {
  2939. /* if CrystalVoice if off, turn off in effects. */
  2940. if (!spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID])
  2941. val = 0;
  2942. /* Voice Focus applies to 2-ch Mic, Digital Mic */
  2943. if ((nid == VOICE_FOCUS) && (spec->cur_mic_type != DIGITAL_MIC))
  2944. val = 0;
  2945. }
  2946. codec_dbg(codec, "ca0132_effect_set: nid=0x%x, val=%ld\n",
  2947. nid, val);
  2948. on = (val == 0) ? FLOAT_ZERO : FLOAT_ONE;
  2949. err = dspio_set_uint_param(codec, ca0132_effects[idx].mid,
  2950. ca0132_effects[idx].reqs[0], on);
  2951. if (err < 0)
  2952. return 0; /* no changed */
  2953. return 1;
  2954. }
  2955. /*
  2956. * Turn on/off Playback Enhancements
  2957. */
  2958. static int ca0132_pe_switch_set(struct hda_codec *codec)
  2959. {
  2960. struct ca0132_spec *spec = codec->spec;
  2961. hda_nid_t nid;
  2962. int i, ret = 0;
  2963. codec_dbg(codec, "ca0132_pe_switch_set: val=%ld\n",
  2964. spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID]);
  2965. i = OUT_EFFECT_START_NID - EFFECT_START_NID;
  2966. nid = OUT_EFFECT_START_NID;
  2967. /* PE affects all out effects */
  2968. for (; nid < OUT_EFFECT_END_NID; nid++, i++)
  2969. ret |= ca0132_effects_set(codec, nid, spec->effects_switch[i]);
  2970. return ret;
  2971. }
  2972. /* Check if Mic1 is streaming, if so, stop streaming */
  2973. static int stop_mic1(struct hda_codec *codec)
  2974. {
  2975. struct ca0132_spec *spec = codec->spec;
  2976. unsigned int oldval = snd_hda_codec_read(codec, spec->adcs[0], 0,
  2977. AC_VERB_GET_CONV, 0);
  2978. if (oldval != 0)
  2979. snd_hda_codec_write(codec, spec->adcs[0], 0,
  2980. AC_VERB_SET_CHANNEL_STREAMID,
  2981. 0);
  2982. return oldval;
  2983. }
  2984. /* Resume Mic1 streaming if it was stopped. */
  2985. static void resume_mic1(struct hda_codec *codec, unsigned int oldval)
  2986. {
  2987. struct ca0132_spec *spec = codec->spec;
  2988. /* Restore the previous stream and channel */
  2989. if (oldval != 0)
  2990. snd_hda_codec_write(codec, spec->adcs[0], 0,
  2991. AC_VERB_SET_CHANNEL_STREAMID,
  2992. oldval);
  2993. }
  2994. /*
  2995. * Turn on/off CrystalVoice
  2996. */
  2997. static int ca0132_cvoice_switch_set(struct hda_codec *codec)
  2998. {
  2999. struct ca0132_spec *spec = codec->spec;
  3000. hda_nid_t nid;
  3001. int i, ret = 0;
  3002. unsigned int oldval;
  3003. codec_dbg(codec, "ca0132_cvoice_switch_set: val=%ld\n",
  3004. spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID]);
  3005. i = IN_EFFECT_START_NID - EFFECT_START_NID;
  3006. nid = IN_EFFECT_START_NID;
  3007. /* CrystalVoice affects all in effects */
  3008. for (; nid < IN_EFFECT_END_NID; nid++, i++)
  3009. ret |= ca0132_effects_set(codec, nid, spec->effects_switch[i]);
  3010. /* including VoiceFX */
  3011. ret |= ca0132_voicefx_set(codec, (spec->voicefx_val ? 1 : 0));
  3012. /* set correct vipsource */
  3013. oldval = stop_mic1(codec);
  3014. ret |= ca0132_set_vipsource(codec, 1);
  3015. resume_mic1(codec, oldval);
  3016. return ret;
  3017. }
  3018. static int ca0132_mic_boost_set(struct hda_codec *codec, long val)
  3019. {
  3020. struct ca0132_spec *spec = codec->spec;
  3021. int ret = 0;
  3022. if (val) /* on */
  3023. ret = snd_hda_codec_amp_update(codec, spec->input_pins[0], 0,
  3024. HDA_INPUT, 0, HDA_AMP_VOLMASK, 3);
  3025. else /* off */
  3026. ret = snd_hda_codec_amp_update(codec, spec->input_pins[0], 0,
  3027. HDA_INPUT, 0, HDA_AMP_VOLMASK, 0);
  3028. return ret;
  3029. }
  3030. static int ca0132_vnode_switch_set(struct snd_kcontrol *kcontrol,
  3031. struct snd_ctl_elem_value *ucontrol)
  3032. {
  3033. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  3034. hda_nid_t nid = get_amp_nid(kcontrol);
  3035. hda_nid_t shared_nid = 0;
  3036. bool effective;
  3037. int ret = 0;
  3038. struct ca0132_spec *spec = codec->spec;
  3039. int auto_jack;
  3040. if (nid == VNID_HP_SEL) {
  3041. auto_jack =
  3042. spec->vnode_lswitch[VNID_HP_ASEL - VNODE_START_NID];
  3043. if (!auto_jack)
  3044. ca0132_select_out(codec);
  3045. return 1;
  3046. }
  3047. if (nid == VNID_AMIC1_SEL) {
  3048. auto_jack =
  3049. spec->vnode_lswitch[VNID_AMIC1_ASEL - VNODE_START_NID];
  3050. if (!auto_jack)
  3051. ca0132_select_mic(codec);
  3052. return 1;
  3053. }
  3054. if (nid == VNID_HP_ASEL) {
  3055. ca0132_select_out(codec);
  3056. return 1;
  3057. }
  3058. if (nid == VNID_AMIC1_ASEL) {
  3059. ca0132_select_mic(codec);
  3060. return 1;
  3061. }
  3062. /* if effective conditions, then update hw immediately. */
  3063. effective = ca0132_is_vnode_effective(codec, nid, &shared_nid);
  3064. if (effective) {
  3065. int dir = get_amp_direction(kcontrol);
  3066. int ch = get_amp_channels(kcontrol);
  3067. unsigned long pval;
  3068. mutex_lock(&codec->control_mutex);
  3069. pval = kcontrol->private_value;
  3070. kcontrol->private_value = HDA_COMPOSE_AMP_VAL(shared_nid, ch,
  3071. 0, dir);
  3072. ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
  3073. kcontrol->private_value = pval;
  3074. mutex_unlock(&codec->control_mutex);
  3075. }
  3076. return ret;
  3077. }
  3078. /* End of control change helpers. */
  3079. static int ca0132_voicefx_info(struct snd_kcontrol *kcontrol,
  3080. struct snd_ctl_elem_info *uinfo)
  3081. {
  3082. unsigned int items = sizeof(ca0132_voicefx_presets)
  3083. / sizeof(struct ct_voicefx_preset);
  3084. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  3085. uinfo->count = 1;
  3086. uinfo->value.enumerated.items = items;
  3087. if (uinfo->value.enumerated.item >= items)
  3088. uinfo->value.enumerated.item = items - 1;
  3089. strcpy(uinfo->value.enumerated.name,
  3090. ca0132_voicefx_presets[uinfo->value.enumerated.item].name);
  3091. return 0;
  3092. }
  3093. static int ca0132_voicefx_get(struct snd_kcontrol *kcontrol,
  3094. struct snd_ctl_elem_value *ucontrol)
  3095. {
  3096. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  3097. struct ca0132_spec *spec = codec->spec;
  3098. ucontrol->value.enumerated.item[0] = spec->voicefx_val;
  3099. return 0;
  3100. }
  3101. static int ca0132_voicefx_put(struct snd_kcontrol *kcontrol,
  3102. struct snd_ctl_elem_value *ucontrol)
  3103. {
  3104. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  3105. struct ca0132_spec *spec = codec->spec;
  3106. int i, err = 0;
  3107. int sel = ucontrol->value.enumerated.item[0];
  3108. unsigned int items = sizeof(ca0132_voicefx_presets)
  3109. / sizeof(struct ct_voicefx_preset);
  3110. if (sel >= items)
  3111. return 0;
  3112. codec_dbg(codec, "ca0132_voicefx_put: sel=%d, preset=%s\n",
  3113. sel, ca0132_voicefx_presets[sel].name);
  3114. /*
  3115. * Idx 0 is default.
  3116. * Default needs to qualify with CrystalVoice state.
  3117. */
  3118. for (i = 0; i < VOICEFX_MAX_PARAM_COUNT; i++) {
  3119. err = dspio_set_uint_param(codec, ca0132_voicefx.mid,
  3120. ca0132_voicefx.reqs[i],
  3121. ca0132_voicefx_presets[sel].vals[i]);
  3122. if (err < 0)
  3123. break;
  3124. }
  3125. if (err >= 0) {
  3126. spec->voicefx_val = sel;
  3127. /* enable voice fx */
  3128. ca0132_voicefx_set(codec, (sel ? 1 : 0));
  3129. }
  3130. return 1;
  3131. }
  3132. static int ca0132_switch_get(struct snd_kcontrol *kcontrol,
  3133. struct snd_ctl_elem_value *ucontrol)
  3134. {
  3135. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  3136. struct ca0132_spec *spec = codec->spec;
  3137. hda_nid_t nid = get_amp_nid(kcontrol);
  3138. int ch = get_amp_channels(kcontrol);
  3139. long *valp = ucontrol->value.integer.value;
  3140. /* vnode */
  3141. if ((nid >= VNODE_START_NID) && (nid < VNODE_END_NID)) {
  3142. if (ch & 1) {
  3143. *valp = spec->vnode_lswitch[nid - VNODE_START_NID];
  3144. valp++;
  3145. }
  3146. if (ch & 2) {
  3147. *valp = spec->vnode_rswitch[nid - VNODE_START_NID];
  3148. valp++;
  3149. }
  3150. return 0;
  3151. }
  3152. /* effects, include PE and CrystalVoice */
  3153. if ((nid >= EFFECT_START_NID) && (nid < EFFECT_END_NID)) {
  3154. *valp = spec->effects_switch[nid - EFFECT_START_NID];
  3155. return 0;
  3156. }
  3157. /* mic boost */
  3158. if (nid == spec->input_pins[0]) {
  3159. *valp = spec->cur_mic_boost;
  3160. return 0;
  3161. }
  3162. return 0;
  3163. }
  3164. static int ca0132_switch_put(struct snd_kcontrol *kcontrol,
  3165. struct snd_ctl_elem_value *ucontrol)
  3166. {
  3167. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  3168. struct ca0132_spec *spec = codec->spec;
  3169. hda_nid_t nid = get_amp_nid(kcontrol);
  3170. int ch = get_amp_channels(kcontrol);
  3171. long *valp = ucontrol->value.integer.value;
  3172. int changed = 1;
  3173. codec_dbg(codec, "ca0132_switch_put: nid=0x%x, val=%ld\n",
  3174. nid, *valp);
  3175. snd_hda_power_up(codec);
  3176. /* vnode */
  3177. if ((nid >= VNODE_START_NID) && (nid < VNODE_END_NID)) {
  3178. if (ch & 1) {
  3179. spec->vnode_lswitch[nid - VNODE_START_NID] = *valp;
  3180. valp++;
  3181. }
  3182. if (ch & 2) {
  3183. spec->vnode_rswitch[nid - VNODE_START_NID] = *valp;
  3184. valp++;
  3185. }
  3186. changed = ca0132_vnode_switch_set(kcontrol, ucontrol);
  3187. goto exit;
  3188. }
  3189. /* PE */
  3190. if (nid == PLAY_ENHANCEMENT) {
  3191. spec->effects_switch[nid - EFFECT_START_NID] = *valp;
  3192. changed = ca0132_pe_switch_set(codec);
  3193. goto exit;
  3194. }
  3195. /* CrystalVoice */
  3196. if (nid == CRYSTAL_VOICE) {
  3197. spec->effects_switch[nid - EFFECT_START_NID] = *valp;
  3198. changed = ca0132_cvoice_switch_set(codec);
  3199. goto exit;
  3200. }
  3201. /* out and in effects */
  3202. if (((nid >= OUT_EFFECT_START_NID) && (nid < OUT_EFFECT_END_NID)) ||
  3203. ((nid >= IN_EFFECT_START_NID) && (nid < IN_EFFECT_END_NID))) {
  3204. spec->effects_switch[nid - EFFECT_START_NID] = *valp;
  3205. changed = ca0132_effects_set(codec, nid, *valp);
  3206. goto exit;
  3207. }
  3208. /* mic boost */
  3209. if (nid == spec->input_pins[0]) {
  3210. spec->cur_mic_boost = *valp;
  3211. /* Mic boost does not apply to Digital Mic */
  3212. if (spec->cur_mic_type != DIGITAL_MIC)
  3213. changed = ca0132_mic_boost_set(codec, *valp);
  3214. goto exit;
  3215. }
  3216. exit:
  3217. snd_hda_power_down(codec);
  3218. return changed;
  3219. }
  3220. /*
  3221. * Volume related
  3222. */
  3223. static int ca0132_volume_info(struct snd_kcontrol *kcontrol,
  3224. struct snd_ctl_elem_info *uinfo)
  3225. {
  3226. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  3227. struct ca0132_spec *spec = codec->spec;
  3228. hda_nid_t nid = get_amp_nid(kcontrol);
  3229. int ch = get_amp_channels(kcontrol);
  3230. int dir = get_amp_direction(kcontrol);
  3231. unsigned long pval;
  3232. int err;
  3233. switch (nid) {
  3234. case VNID_SPK:
  3235. /* follow shared_out info */
  3236. nid = spec->shared_out_nid;
  3237. mutex_lock(&codec->control_mutex);
  3238. pval = kcontrol->private_value;
  3239. kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
  3240. err = snd_hda_mixer_amp_volume_info(kcontrol, uinfo);
  3241. kcontrol->private_value = pval;
  3242. mutex_unlock(&codec->control_mutex);
  3243. break;
  3244. case VNID_MIC:
  3245. /* follow shared_mic info */
  3246. nid = spec->shared_mic_nid;
  3247. mutex_lock(&codec->control_mutex);
  3248. pval = kcontrol->private_value;
  3249. kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
  3250. err = snd_hda_mixer_amp_volume_info(kcontrol, uinfo);
  3251. kcontrol->private_value = pval;
  3252. mutex_unlock(&codec->control_mutex);
  3253. break;
  3254. default:
  3255. err = snd_hda_mixer_amp_volume_info(kcontrol, uinfo);
  3256. }
  3257. return err;
  3258. }
  3259. static int ca0132_volume_get(struct snd_kcontrol *kcontrol,
  3260. struct snd_ctl_elem_value *ucontrol)
  3261. {
  3262. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  3263. struct ca0132_spec *spec = codec->spec;
  3264. hda_nid_t nid = get_amp_nid(kcontrol);
  3265. int ch = get_amp_channels(kcontrol);
  3266. long *valp = ucontrol->value.integer.value;
  3267. /* store the left and right volume */
  3268. if (ch & 1) {
  3269. *valp = spec->vnode_lvol[nid - VNODE_START_NID];
  3270. valp++;
  3271. }
  3272. if (ch & 2) {
  3273. *valp = spec->vnode_rvol[nid - VNODE_START_NID];
  3274. valp++;
  3275. }
  3276. return 0;
  3277. }
  3278. static int ca0132_volume_put(struct snd_kcontrol *kcontrol,
  3279. struct snd_ctl_elem_value *ucontrol)
  3280. {
  3281. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  3282. struct ca0132_spec *spec = codec->spec;
  3283. hda_nid_t nid = get_amp_nid(kcontrol);
  3284. int ch = get_amp_channels(kcontrol);
  3285. long *valp = ucontrol->value.integer.value;
  3286. hda_nid_t shared_nid = 0;
  3287. bool effective;
  3288. int changed = 1;
  3289. /* store the left and right volume */
  3290. if (ch & 1) {
  3291. spec->vnode_lvol[nid - VNODE_START_NID] = *valp;
  3292. valp++;
  3293. }
  3294. if (ch & 2) {
  3295. spec->vnode_rvol[nid - VNODE_START_NID] = *valp;
  3296. valp++;
  3297. }
  3298. /* if effective conditions, then update hw immediately. */
  3299. effective = ca0132_is_vnode_effective(codec, nid, &shared_nid);
  3300. if (effective) {
  3301. int dir = get_amp_direction(kcontrol);
  3302. unsigned long pval;
  3303. snd_hda_power_up(codec);
  3304. mutex_lock(&codec->control_mutex);
  3305. pval = kcontrol->private_value;
  3306. kcontrol->private_value = HDA_COMPOSE_AMP_VAL(shared_nid, ch,
  3307. 0, dir);
  3308. changed = snd_hda_mixer_amp_volume_put(kcontrol, ucontrol);
  3309. kcontrol->private_value = pval;
  3310. mutex_unlock(&codec->control_mutex);
  3311. snd_hda_power_down(codec);
  3312. }
  3313. return changed;
  3314. }
  3315. static int ca0132_volume_tlv(struct snd_kcontrol *kcontrol, int op_flag,
  3316. unsigned int size, unsigned int __user *tlv)
  3317. {
  3318. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  3319. struct ca0132_spec *spec = codec->spec;
  3320. hda_nid_t nid = get_amp_nid(kcontrol);
  3321. int ch = get_amp_channels(kcontrol);
  3322. int dir = get_amp_direction(kcontrol);
  3323. unsigned long pval;
  3324. int err;
  3325. switch (nid) {
  3326. case VNID_SPK:
  3327. /* follow shared_out tlv */
  3328. nid = spec->shared_out_nid;
  3329. mutex_lock(&codec->control_mutex);
  3330. pval = kcontrol->private_value;
  3331. kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
  3332. err = snd_hda_mixer_amp_tlv(kcontrol, op_flag, size, tlv);
  3333. kcontrol->private_value = pval;
  3334. mutex_unlock(&codec->control_mutex);
  3335. break;
  3336. case VNID_MIC:
  3337. /* follow shared_mic tlv */
  3338. nid = spec->shared_mic_nid;
  3339. mutex_lock(&codec->control_mutex);
  3340. pval = kcontrol->private_value;
  3341. kcontrol->private_value = HDA_COMPOSE_AMP_VAL(nid, ch, 0, dir);
  3342. err = snd_hda_mixer_amp_tlv(kcontrol, op_flag, size, tlv);
  3343. kcontrol->private_value = pval;
  3344. mutex_unlock(&codec->control_mutex);
  3345. break;
  3346. default:
  3347. err = snd_hda_mixer_amp_tlv(kcontrol, op_flag, size, tlv);
  3348. }
  3349. return err;
  3350. }
  3351. static int add_fx_switch(struct hda_codec *codec, hda_nid_t nid,
  3352. const char *pfx, int dir)
  3353. {
  3354. char namestr[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
  3355. int type = dir ? HDA_INPUT : HDA_OUTPUT;
  3356. struct snd_kcontrol_new knew =
  3357. CA0132_CODEC_MUTE_MONO(namestr, nid, 1, type);
  3358. sprintf(namestr, "%s %s Switch", pfx, dirstr[dir]);
  3359. return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
  3360. }
  3361. static int add_voicefx(struct hda_codec *codec)
  3362. {
  3363. struct snd_kcontrol_new knew =
  3364. HDA_CODEC_MUTE_MONO(ca0132_voicefx.name,
  3365. VOICEFX, 1, 0, HDA_INPUT);
  3366. knew.info = ca0132_voicefx_info;
  3367. knew.get = ca0132_voicefx_get;
  3368. knew.put = ca0132_voicefx_put;
  3369. return snd_hda_ctl_add(codec, VOICEFX, snd_ctl_new1(&knew, codec));
  3370. }
  3371. /*
  3372. * When changing Node IDs for Mixer Controls below, make sure to update
  3373. * Node IDs in ca0132_config() as well.
  3374. */
  3375. static struct snd_kcontrol_new ca0132_mixer[] = {
  3376. CA0132_CODEC_VOL("Master Playback Volume", VNID_SPK, HDA_OUTPUT),
  3377. CA0132_CODEC_MUTE("Master Playback Switch", VNID_SPK, HDA_OUTPUT),
  3378. CA0132_CODEC_VOL("Capture Volume", VNID_MIC, HDA_INPUT),
  3379. CA0132_CODEC_MUTE("Capture Switch", VNID_MIC, HDA_INPUT),
  3380. HDA_CODEC_VOLUME("Analog-Mic2 Capture Volume", 0x08, 0, HDA_INPUT),
  3381. HDA_CODEC_MUTE("Analog-Mic2 Capture Switch", 0x08, 0, HDA_INPUT),
  3382. HDA_CODEC_VOLUME("What U Hear Capture Volume", 0x0a, 0, HDA_INPUT),
  3383. HDA_CODEC_MUTE("What U Hear Capture Switch", 0x0a, 0, HDA_INPUT),
  3384. CA0132_CODEC_MUTE_MONO("Mic1-Boost (30dB) Capture Switch",
  3385. 0x12, 1, HDA_INPUT),
  3386. CA0132_CODEC_MUTE_MONO("HP/Speaker Playback Switch",
  3387. VNID_HP_SEL, 1, HDA_OUTPUT),
  3388. CA0132_CODEC_MUTE_MONO("AMic1/DMic Capture Switch",
  3389. VNID_AMIC1_SEL, 1, HDA_INPUT),
  3390. CA0132_CODEC_MUTE_MONO("HP/Speaker Auto Detect Playback Switch",
  3391. VNID_HP_ASEL, 1, HDA_OUTPUT),
  3392. CA0132_CODEC_MUTE_MONO("AMic1/DMic Auto Detect Capture Switch",
  3393. VNID_AMIC1_ASEL, 1, HDA_INPUT),
  3394. { } /* end */
  3395. };
  3396. static int ca0132_build_controls(struct hda_codec *codec)
  3397. {
  3398. struct ca0132_spec *spec = codec->spec;
  3399. int i, num_fx;
  3400. int err = 0;
  3401. /* Add Mixer controls */
  3402. for (i = 0; i < spec->num_mixers; i++) {
  3403. err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
  3404. if (err < 0)
  3405. return err;
  3406. }
  3407. /* Add in and out effects controls.
  3408. * VoiceFX, PE and CrystalVoice are added separately.
  3409. */
  3410. num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT;
  3411. for (i = 0; i < num_fx; i++) {
  3412. err = add_fx_switch(codec, ca0132_effects[i].nid,
  3413. ca0132_effects[i].name,
  3414. ca0132_effects[i].direct);
  3415. if (err < 0)
  3416. return err;
  3417. }
  3418. err = add_fx_switch(codec, PLAY_ENHANCEMENT, "PlayEnhancement", 0);
  3419. if (err < 0)
  3420. return err;
  3421. err = add_fx_switch(codec, CRYSTAL_VOICE, "CrystalVoice", 1);
  3422. if (err < 0)
  3423. return err;
  3424. add_voicefx(codec);
  3425. #ifdef ENABLE_TUNING_CONTROLS
  3426. add_tuning_ctls(codec);
  3427. #endif
  3428. err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
  3429. if (err < 0)
  3430. return err;
  3431. if (spec->dig_out) {
  3432. err = snd_hda_create_spdif_out_ctls(codec, spec->dig_out,
  3433. spec->dig_out);
  3434. if (err < 0)
  3435. return err;
  3436. err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
  3437. if (err < 0)
  3438. return err;
  3439. /* spec->multiout.share_spdif = 1; */
  3440. }
  3441. if (spec->dig_in) {
  3442. err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
  3443. if (err < 0)
  3444. return err;
  3445. }
  3446. return 0;
  3447. }
  3448. /*
  3449. * PCM
  3450. */
  3451. static struct hda_pcm_stream ca0132_pcm_analog_playback = {
  3452. .substreams = 1,
  3453. .channels_min = 2,
  3454. .channels_max = 6,
  3455. .ops = {
  3456. .prepare = ca0132_playback_pcm_prepare,
  3457. .cleanup = ca0132_playback_pcm_cleanup,
  3458. .get_delay = ca0132_playback_pcm_delay,
  3459. },
  3460. };
  3461. static struct hda_pcm_stream ca0132_pcm_analog_capture = {
  3462. .substreams = 1,
  3463. .channels_min = 2,
  3464. .channels_max = 2,
  3465. .ops = {
  3466. .prepare = ca0132_capture_pcm_prepare,
  3467. .cleanup = ca0132_capture_pcm_cleanup,
  3468. .get_delay = ca0132_capture_pcm_delay,
  3469. },
  3470. };
  3471. static struct hda_pcm_stream ca0132_pcm_digital_playback = {
  3472. .substreams = 1,
  3473. .channels_min = 2,
  3474. .channels_max = 2,
  3475. .ops = {
  3476. .open = ca0132_dig_playback_pcm_open,
  3477. .close = ca0132_dig_playback_pcm_close,
  3478. .prepare = ca0132_dig_playback_pcm_prepare,
  3479. .cleanup = ca0132_dig_playback_pcm_cleanup
  3480. },
  3481. };
  3482. static struct hda_pcm_stream ca0132_pcm_digital_capture = {
  3483. .substreams = 1,
  3484. .channels_min = 2,
  3485. .channels_max = 2,
  3486. };
  3487. static int ca0132_build_pcms(struct hda_codec *codec)
  3488. {
  3489. struct ca0132_spec *spec = codec->spec;
  3490. struct hda_pcm *info;
  3491. info = snd_hda_codec_pcm_new(codec, "CA0132 Analog");
  3492. if (!info)
  3493. return -ENOMEM;
  3494. info->stream[SNDRV_PCM_STREAM_PLAYBACK] = ca0132_pcm_analog_playback;
  3495. info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dacs[0];
  3496. info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
  3497. spec->multiout.max_channels;
  3498. info->stream[SNDRV_PCM_STREAM_CAPTURE] = ca0132_pcm_analog_capture;
  3499. info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = 1;
  3500. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[0];
  3501. info = snd_hda_codec_pcm_new(codec, "CA0132 Analog Mic-In2");
  3502. if (!info)
  3503. return -ENOMEM;
  3504. info->stream[SNDRV_PCM_STREAM_CAPTURE] = ca0132_pcm_analog_capture;
  3505. info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = 1;
  3506. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[1];
  3507. info = snd_hda_codec_pcm_new(codec, "CA0132 What U Hear");
  3508. if (!info)
  3509. return -ENOMEM;
  3510. info->stream[SNDRV_PCM_STREAM_CAPTURE] = ca0132_pcm_analog_capture;
  3511. info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = 1;
  3512. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[2];
  3513. if (!spec->dig_out && !spec->dig_in)
  3514. return 0;
  3515. info = snd_hda_codec_pcm_new(codec, "CA0132 Digital");
  3516. if (!info)
  3517. return -ENOMEM;
  3518. info->pcm_type = HDA_PCM_TYPE_SPDIF;
  3519. if (spec->dig_out) {
  3520. info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
  3521. ca0132_pcm_digital_playback;
  3522. info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dig_out;
  3523. }
  3524. if (spec->dig_in) {
  3525. info->stream[SNDRV_PCM_STREAM_CAPTURE] =
  3526. ca0132_pcm_digital_capture;
  3527. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
  3528. }
  3529. return 0;
  3530. }
  3531. static void init_output(struct hda_codec *codec, hda_nid_t pin, hda_nid_t dac)
  3532. {
  3533. if (pin) {
  3534. snd_hda_set_pin_ctl(codec, pin, PIN_HP);
  3535. if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
  3536. snd_hda_codec_write(codec, pin, 0,
  3537. AC_VERB_SET_AMP_GAIN_MUTE,
  3538. AMP_OUT_UNMUTE);
  3539. }
  3540. if (dac && (get_wcaps(codec, dac) & AC_WCAP_OUT_AMP))
  3541. snd_hda_codec_write(codec, dac, 0,
  3542. AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO);
  3543. }
  3544. static void init_input(struct hda_codec *codec, hda_nid_t pin, hda_nid_t adc)
  3545. {
  3546. if (pin) {
  3547. snd_hda_set_pin_ctl(codec, pin, PIN_VREF80);
  3548. if (get_wcaps(codec, pin) & AC_WCAP_IN_AMP)
  3549. snd_hda_codec_write(codec, pin, 0,
  3550. AC_VERB_SET_AMP_GAIN_MUTE,
  3551. AMP_IN_UNMUTE(0));
  3552. }
  3553. if (adc && (get_wcaps(codec, adc) & AC_WCAP_IN_AMP)) {
  3554. snd_hda_codec_write(codec, adc, 0, AC_VERB_SET_AMP_GAIN_MUTE,
  3555. AMP_IN_UNMUTE(0));
  3556. /* init to 0 dB and unmute. */
  3557. snd_hda_codec_amp_stereo(codec, adc, HDA_INPUT, 0,
  3558. HDA_AMP_VOLMASK, 0x5a);
  3559. snd_hda_codec_amp_stereo(codec, adc, HDA_INPUT, 0,
  3560. HDA_AMP_MUTE, 0);
  3561. }
  3562. }
  3563. static void refresh_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir)
  3564. {
  3565. unsigned int caps;
  3566. caps = snd_hda_param_read(codec, nid, dir == HDA_OUTPUT ?
  3567. AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
  3568. snd_hda_override_amp_caps(codec, nid, dir, caps);
  3569. }
  3570. /*
  3571. * Switch between Digital built-in mic and analog mic.
  3572. */
  3573. static void ca0132_set_dmic(struct hda_codec *codec, int enable)
  3574. {
  3575. struct ca0132_spec *spec = codec->spec;
  3576. unsigned int tmp;
  3577. u8 val;
  3578. unsigned int oldval;
  3579. codec_dbg(codec, "ca0132_set_dmic: enable=%d\n", enable);
  3580. oldval = stop_mic1(codec);
  3581. ca0132_set_vipsource(codec, 0);
  3582. if (enable) {
  3583. /* set DMic input as 2-ch */
  3584. tmp = FLOAT_TWO;
  3585. dspio_set_uint_param(codec, 0x80, 0x00, tmp);
  3586. val = spec->dmic_ctl;
  3587. val |= 0x80;
  3588. snd_hda_codec_write(codec, spec->input_pins[0], 0,
  3589. VENDOR_CHIPIO_DMIC_CTL_SET, val);
  3590. if (!(spec->dmic_ctl & 0x20))
  3591. chipio_set_control_flag(codec, CONTROL_FLAG_DMIC, 1);
  3592. } else {
  3593. /* set AMic input as mono */
  3594. tmp = FLOAT_ONE;
  3595. dspio_set_uint_param(codec, 0x80, 0x00, tmp);
  3596. val = spec->dmic_ctl;
  3597. /* clear bit7 and bit5 to disable dmic */
  3598. val &= 0x5f;
  3599. snd_hda_codec_write(codec, spec->input_pins[0], 0,
  3600. VENDOR_CHIPIO_DMIC_CTL_SET, val);
  3601. if (!(spec->dmic_ctl & 0x20))
  3602. chipio_set_control_flag(codec, CONTROL_FLAG_DMIC, 0);
  3603. }
  3604. ca0132_set_vipsource(codec, 1);
  3605. resume_mic1(codec, oldval);
  3606. }
  3607. /*
  3608. * Initialization for Digital Mic.
  3609. */
  3610. static void ca0132_init_dmic(struct hda_codec *codec)
  3611. {
  3612. struct ca0132_spec *spec = codec->spec;
  3613. u8 val;
  3614. /* Setup Digital Mic here, but don't enable.
  3615. * Enable based on jack detect.
  3616. */
  3617. /* MCLK uses MPIO1, set to enable.
  3618. * Bit 2-0: MPIO select
  3619. * Bit 3: set to disable
  3620. * Bit 7-4: reserved
  3621. */
  3622. val = 0x01;
  3623. snd_hda_codec_write(codec, spec->input_pins[0], 0,
  3624. VENDOR_CHIPIO_DMIC_MCLK_SET, val);
  3625. /* Data1 uses MPIO3. Data2 not use
  3626. * Bit 2-0: Data1 MPIO select
  3627. * Bit 3: set disable Data1
  3628. * Bit 6-4: Data2 MPIO select
  3629. * Bit 7: set disable Data2
  3630. */
  3631. val = 0x83;
  3632. snd_hda_codec_write(codec, spec->input_pins[0], 0,
  3633. VENDOR_CHIPIO_DMIC_PIN_SET, val);
  3634. /* Use Ch-0 and Ch-1. Rate is 48K, mode 1. Disable DMic first.
  3635. * Bit 3-0: Channel mask
  3636. * Bit 4: set for 48KHz, clear for 32KHz
  3637. * Bit 5: mode
  3638. * Bit 6: set to select Data2, clear for Data1
  3639. * Bit 7: set to enable DMic, clear for AMic
  3640. */
  3641. val = 0x23;
  3642. /* keep a copy of dmic ctl val for enable/disable dmic purpuse */
  3643. spec->dmic_ctl = val;
  3644. snd_hda_codec_write(codec, spec->input_pins[0], 0,
  3645. VENDOR_CHIPIO_DMIC_CTL_SET, val);
  3646. }
  3647. /*
  3648. * Initialization for Analog Mic 2
  3649. */
  3650. static void ca0132_init_analog_mic2(struct hda_codec *codec)
  3651. {
  3652. struct ca0132_spec *spec = codec->spec;
  3653. mutex_lock(&spec->chipio_mutex);
  3654. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  3655. VENDOR_CHIPIO_8051_ADDRESS_LOW, 0x20);
  3656. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  3657. VENDOR_CHIPIO_8051_ADDRESS_HIGH, 0x19);
  3658. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  3659. VENDOR_CHIPIO_8051_DATA_WRITE, 0x00);
  3660. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  3661. VENDOR_CHIPIO_8051_ADDRESS_LOW, 0x2D);
  3662. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  3663. VENDOR_CHIPIO_8051_ADDRESS_HIGH, 0x19);
  3664. snd_hda_codec_write(codec, WIDGET_CHIP_CTRL, 0,
  3665. VENDOR_CHIPIO_8051_DATA_WRITE, 0x00);
  3666. mutex_unlock(&spec->chipio_mutex);
  3667. }
  3668. static void ca0132_refresh_widget_caps(struct hda_codec *codec)
  3669. {
  3670. struct ca0132_spec *spec = codec->spec;
  3671. int i;
  3672. codec_dbg(codec, "ca0132_refresh_widget_caps.\n");
  3673. snd_hda_codec_update_widgets(codec);
  3674. for (i = 0; i < spec->multiout.num_dacs; i++)
  3675. refresh_amp_caps(codec, spec->dacs[i], HDA_OUTPUT);
  3676. for (i = 0; i < spec->num_outputs; i++)
  3677. refresh_amp_caps(codec, spec->out_pins[i], HDA_OUTPUT);
  3678. for (i = 0; i < spec->num_inputs; i++) {
  3679. refresh_amp_caps(codec, spec->adcs[i], HDA_INPUT);
  3680. refresh_amp_caps(codec, spec->input_pins[i], HDA_INPUT);
  3681. }
  3682. }
  3683. /*
  3684. * Setup default parameters for DSP
  3685. */
  3686. static void ca0132_setup_defaults(struct hda_codec *codec)
  3687. {
  3688. struct ca0132_spec *spec = codec->spec;
  3689. unsigned int tmp;
  3690. int num_fx;
  3691. int idx, i;
  3692. if (spec->dsp_state != DSP_DOWNLOADED)
  3693. return;
  3694. /* out, in effects + voicefx */
  3695. num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT + 1;
  3696. for (idx = 0; idx < num_fx; idx++) {
  3697. for (i = 0; i <= ca0132_effects[idx].params; i++) {
  3698. dspio_set_uint_param(codec, ca0132_effects[idx].mid,
  3699. ca0132_effects[idx].reqs[i],
  3700. ca0132_effects[idx].def_vals[i]);
  3701. }
  3702. }
  3703. /*remove DSP headroom*/
  3704. tmp = FLOAT_ZERO;
  3705. dspio_set_uint_param(codec, 0x96, 0x3C, tmp);
  3706. /*set speaker EQ bypass attenuation*/
  3707. dspio_set_uint_param(codec, 0x8f, 0x01, tmp);
  3708. /* set AMic1 and AMic2 as mono mic */
  3709. tmp = FLOAT_ONE;
  3710. dspio_set_uint_param(codec, 0x80, 0x00, tmp);
  3711. dspio_set_uint_param(codec, 0x80, 0x01, tmp);
  3712. /* set AMic1 as CrystalVoice input */
  3713. tmp = FLOAT_ONE;
  3714. dspio_set_uint_param(codec, 0x80, 0x05, tmp);
  3715. /* set WUH source */
  3716. tmp = FLOAT_TWO;
  3717. dspio_set_uint_param(codec, 0x31, 0x00, tmp);
  3718. }
  3719. /*
  3720. * Initialization of flags in chip
  3721. */
  3722. static void ca0132_init_flags(struct hda_codec *codec)
  3723. {
  3724. chipio_set_control_flag(codec, CONTROL_FLAG_IDLE_ENABLE, 0);
  3725. chipio_set_control_flag(codec, CONTROL_FLAG_PORT_A_COMMON_MODE, 0);
  3726. chipio_set_control_flag(codec, CONTROL_FLAG_PORT_D_COMMON_MODE, 0);
  3727. chipio_set_control_flag(codec, CONTROL_FLAG_PORT_A_10KOHM_LOAD, 0);
  3728. chipio_set_control_flag(codec, CONTROL_FLAG_PORT_D_10KOHM_LOAD, 0);
  3729. chipio_set_control_flag(codec, CONTROL_FLAG_ADC_C_HIGH_PASS, 1);
  3730. }
  3731. /*
  3732. * Initialization of parameters in chip
  3733. */
  3734. static void ca0132_init_params(struct hda_codec *codec)
  3735. {
  3736. chipio_set_control_param(codec, CONTROL_PARAM_PORTA_160OHM_GAIN, 6);
  3737. chipio_set_control_param(codec, CONTROL_PARAM_PORTD_160OHM_GAIN, 6);
  3738. }
  3739. static void ca0132_set_dsp_msr(struct hda_codec *codec, bool is96k)
  3740. {
  3741. chipio_set_control_flag(codec, CONTROL_FLAG_DSP_96KHZ, is96k);
  3742. chipio_set_control_flag(codec, CONTROL_FLAG_DAC_96KHZ, is96k);
  3743. chipio_set_control_flag(codec, CONTROL_FLAG_SRC_RATE_96KHZ, is96k);
  3744. chipio_set_control_flag(codec, CONTROL_FLAG_SRC_CLOCK_196MHZ, is96k);
  3745. chipio_set_control_flag(codec, CONTROL_FLAG_ADC_B_96KHZ, is96k);
  3746. chipio_set_control_flag(codec, CONTROL_FLAG_ADC_C_96KHZ, is96k);
  3747. chipio_set_conn_rate(codec, MEM_CONNID_MICIN1, SR_96_000);
  3748. chipio_set_conn_rate(codec, MEM_CONNID_MICOUT1, SR_96_000);
  3749. chipio_set_conn_rate(codec, MEM_CONNID_WUH, SR_48_000);
  3750. }
  3751. static bool ca0132_download_dsp_images(struct hda_codec *codec)
  3752. {
  3753. bool dsp_loaded = false;
  3754. const struct dsp_image_seg *dsp_os_image;
  3755. const struct firmware *fw_entry;
  3756. if (request_firmware(&fw_entry, EFX_FILE, codec->card->dev) != 0)
  3757. return false;
  3758. dsp_os_image = (struct dsp_image_seg *)(fw_entry->data);
  3759. if (dspload_image(codec, dsp_os_image, 0, 0, true, 0)) {
  3760. codec_err(codec, "ca0132 DSP load image failed\n");
  3761. goto exit_download;
  3762. }
  3763. dsp_loaded = dspload_wait_loaded(codec);
  3764. exit_download:
  3765. release_firmware(fw_entry);
  3766. return dsp_loaded;
  3767. }
  3768. static void ca0132_download_dsp(struct hda_codec *codec)
  3769. {
  3770. struct ca0132_spec *spec = codec->spec;
  3771. #ifndef CONFIG_SND_HDA_CODEC_CA0132_DSP
  3772. return; /* NOP */
  3773. #endif
  3774. if (spec->dsp_state == DSP_DOWNLOAD_FAILED)
  3775. return; /* don't retry failures */
  3776. chipio_enable_clocks(codec);
  3777. spec->dsp_state = DSP_DOWNLOADING;
  3778. if (!ca0132_download_dsp_images(codec))
  3779. spec->dsp_state = DSP_DOWNLOAD_FAILED;
  3780. else
  3781. spec->dsp_state = DSP_DOWNLOADED;
  3782. if (spec->dsp_state == DSP_DOWNLOADED)
  3783. ca0132_set_dsp_msr(codec, true);
  3784. }
  3785. static void ca0132_process_dsp_response(struct hda_codec *codec,
  3786. struct hda_jack_callback *callback)
  3787. {
  3788. struct ca0132_spec *spec = codec->spec;
  3789. codec_dbg(codec, "ca0132_process_dsp_response\n");
  3790. if (spec->wait_scp) {
  3791. if (dspio_get_response_data(codec) >= 0)
  3792. spec->wait_scp = 0;
  3793. }
  3794. dspio_clear_response_queue(codec);
  3795. }
  3796. static void hp_callback(struct hda_codec *codec, struct hda_jack_callback *cb)
  3797. {
  3798. struct ca0132_spec *spec = codec->spec;
  3799. struct hda_jack_tbl *tbl;
  3800. /* Delay enabling the HP amp, to let the mic-detection
  3801. * state machine run.
  3802. */
  3803. cancel_delayed_work_sync(&spec->unsol_hp_work);
  3804. schedule_delayed_work(&spec->unsol_hp_work, msecs_to_jiffies(500));
  3805. tbl = snd_hda_jack_tbl_get(codec, cb->nid);
  3806. if (tbl)
  3807. tbl->block_report = 1;
  3808. }
  3809. static void amic_callback(struct hda_codec *codec, struct hda_jack_callback *cb)
  3810. {
  3811. ca0132_select_mic(codec);
  3812. }
  3813. static void ca0132_init_unsol(struct hda_codec *codec)
  3814. {
  3815. struct ca0132_spec *spec = codec->spec;
  3816. snd_hda_jack_detect_enable_callback(codec, spec->unsol_tag_hp, hp_callback);
  3817. snd_hda_jack_detect_enable_callback(codec, spec->unsol_tag_amic1,
  3818. amic_callback);
  3819. snd_hda_jack_detect_enable_callback(codec, UNSOL_TAG_DSP,
  3820. ca0132_process_dsp_response);
  3821. }
  3822. /*
  3823. * Verbs tables.
  3824. */
  3825. /* Sends before DSP download. */
  3826. static struct hda_verb ca0132_base_init_verbs[] = {
  3827. /*enable ct extension*/
  3828. {0x15, VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE, 0x1},
  3829. {}
  3830. };
  3831. /* Send at exit. */
  3832. static struct hda_verb ca0132_base_exit_verbs[] = {
  3833. /*set afg to D3*/
  3834. {0x01, AC_VERB_SET_POWER_STATE, 0x03},
  3835. /*disable ct extension*/
  3836. {0x15, VENDOR_CHIPIO_CT_EXTENSIONS_ENABLE, 0},
  3837. {}
  3838. };
  3839. /* Other verbs tables. Sends after DSP download. */
  3840. static struct hda_verb ca0132_init_verbs0[] = {
  3841. /* chip init verbs */
  3842. {0x15, 0x70D, 0xF0},
  3843. {0x15, 0x70E, 0xFE},
  3844. {0x15, 0x707, 0x75},
  3845. {0x15, 0x707, 0xD3},
  3846. {0x15, 0x707, 0x09},
  3847. {0x15, 0x707, 0x53},
  3848. {0x15, 0x707, 0xD4},
  3849. {0x15, 0x707, 0xEF},
  3850. {0x15, 0x707, 0x75},
  3851. {0x15, 0x707, 0xD3},
  3852. {0x15, 0x707, 0x09},
  3853. {0x15, 0x707, 0x02},
  3854. {0x15, 0x707, 0x37},
  3855. {0x15, 0x707, 0x78},
  3856. {0x15, 0x53C, 0xCE},
  3857. {0x15, 0x575, 0xC9},
  3858. {0x15, 0x53D, 0xCE},
  3859. {0x15, 0x5B7, 0xC9},
  3860. {0x15, 0x70D, 0xE8},
  3861. {0x15, 0x70E, 0xFE},
  3862. {0x15, 0x707, 0x02},
  3863. {0x15, 0x707, 0x68},
  3864. {0x15, 0x707, 0x62},
  3865. {0x15, 0x53A, 0xCE},
  3866. {0x15, 0x546, 0xC9},
  3867. {0x15, 0x53B, 0xCE},
  3868. {0x15, 0x5E8, 0xC9},
  3869. {0x15, 0x717, 0x0D},
  3870. {0x15, 0x718, 0x20},
  3871. {}
  3872. };
  3873. static void ca0132_init_chip(struct hda_codec *codec)
  3874. {
  3875. struct ca0132_spec *spec = codec->spec;
  3876. int num_fx;
  3877. int i;
  3878. unsigned int on;
  3879. mutex_init(&spec->chipio_mutex);
  3880. spec->cur_out_type = SPEAKER_OUT;
  3881. spec->cur_mic_type = DIGITAL_MIC;
  3882. spec->cur_mic_boost = 0;
  3883. for (i = 0; i < VNODES_COUNT; i++) {
  3884. spec->vnode_lvol[i] = 0x5a;
  3885. spec->vnode_rvol[i] = 0x5a;
  3886. spec->vnode_lswitch[i] = 0;
  3887. spec->vnode_rswitch[i] = 0;
  3888. }
  3889. /*
  3890. * Default states for effects are in ca0132_effects[].
  3891. */
  3892. num_fx = OUT_EFFECTS_COUNT + IN_EFFECTS_COUNT;
  3893. for (i = 0; i < num_fx; i++) {
  3894. on = (unsigned int)ca0132_effects[i].reqs[0];
  3895. spec->effects_switch[i] = on ? 1 : 0;
  3896. }
  3897. spec->voicefx_val = 0;
  3898. spec->effects_switch[PLAY_ENHANCEMENT - EFFECT_START_NID] = 1;
  3899. spec->effects_switch[CRYSTAL_VOICE - EFFECT_START_NID] = 0;
  3900. #ifdef ENABLE_TUNING_CONTROLS
  3901. ca0132_init_tuning_defaults(codec);
  3902. #endif
  3903. }
  3904. static void ca0132_exit_chip(struct hda_codec *codec)
  3905. {
  3906. /* put any chip cleanup stuffs here. */
  3907. if (dspload_is_loaded(codec))
  3908. dsp_reset(codec);
  3909. }
  3910. static int ca0132_init(struct hda_codec *codec)
  3911. {
  3912. struct ca0132_spec *spec = codec->spec;
  3913. struct auto_pin_cfg *cfg = &spec->autocfg;
  3914. int i;
  3915. if (spec->dsp_state != DSP_DOWNLOAD_FAILED)
  3916. spec->dsp_state = DSP_DOWNLOAD_INIT;
  3917. spec->curr_chip_addx = INVALID_CHIP_ADDRESS;
  3918. snd_hda_power_up_pm(codec);
  3919. ca0132_init_unsol(codec);
  3920. ca0132_init_params(codec);
  3921. ca0132_init_flags(codec);
  3922. snd_hda_sequence_write(codec, spec->base_init_verbs);
  3923. ca0132_download_dsp(codec);
  3924. ca0132_refresh_widget_caps(codec);
  3925. ca0132_setup_defaults(codec);
  3926. ca0132_init_analog_mic2(codec);
  3927. ca0132_init_dmic(codec);
  3928. for (i = 0; i < spec->num_outputs; i++)
  3929. init_output(codec, spec->out_pins[i], spec->dacs[0]);
  3930. init_output(codec, cfg->dig_out_pins[0], spec->dig_out);
  3931. for (i = 0; i < spec->num_inputs; i++)
  3932. init_input(codec, spec->input_pins[i], spec->adcs[i]);
  3933. init_input(codec, cfg->dig_in_pin, spec->dig_in);
  3934. snd_hda_sequence_write(codec, spec->chip_init_verbs);
  3935. snd_hda_sequence_write(codec, spec->spec_init_verbs);
  3936. ca0132_select_out(codec);
  3937. ca0132_select_mic(codec);
  3938. snd_hda_jack_report_sync(codec);
  3939. snd_hda_power_down_pm(codec);
  3940. return 0;
  3941. }
  3942. static void ca0132_free(struct hda_codec *codec)
  3943. {
  3944. struct ca0132_spec *spec = codec->spec;
  3945. cancel_delayed_work_sync(&spec->unsol_hp_work);
  3946. snd_hda_power_up(codec);
  3947. snd_hda_sequence_write(codec, spec->base_exit_verbs);
  3948. ca0132_exit_chip(codec);
  3949. snd_hda_power_down(codec);
  3950. kfree(spec->spec_init_verbs);
  3951. kfree(codec->spec);
  3952. }
  3953. static struct hda_codec_ops ca0132_patch_ops = {
  3954. .build_controls = ca0132_build_controls,
  3955. .build_pcms = ca0132_build_pcms,
  3956. .init = ca0132_init,
  3957. .free = ca0132_free,
  3958. .unsol_event = snd_hda_jack_unsol_event,
  3959. };
  3960. static void ca0132_config(struct hda_codec *codec)
  3961. {
  3962. struct ca0132_spec *spec = codec->spec;
  3963. struct auto_pin_cfg *cfg = &spec->autocfg;
  3964. spec->dacs[0] = 0x2;
  3965. spec->dacs[1] = 0x3;
  3966. spec->dacs[2] = 0x4;
  3967. spec->multiout.dac_nids = spec->dacs;
  3968. spec->multiout.num_dacs = 3;
  3969. spec->multiout.max_channels = 2;
  3970. if (spec->quirk == QUIRK_ALIENWARE) {
  3971. codec_dbg(codec, "ca0132_config: QUIRK_ALIENWARE applied.\n");
  3972. snd_hda_apply_pincfgs(codec, alienware_pincfgs);
  3973. spec->num_outputs = 2;
  3974. spec->out_pins[0] = 0x0b; /* speaker out */
  3975. spec->out_pins[1] = 0x0f;
  3976. spec->shared_out_nid = 0x2;
  3977. spec->unsol_tag_hp = 0x0f;
  3978. spec->adcs[0] = 0x7; /* digital mic / analog mic1 */
  3979. spec->adcs[1] = 0x8; /* analog mic2 */
  3980. spec->adcs[2] = 0xa; /* what u hear */
  3981. spec->num_inputs = 3;
  3982. spec->input_pins[0] = 0x12;
  3983. spec->input_pins[1] = 0x11;
  3984. spec->input_pins[2] = 0x13;
  3985. spec->shared_mic_nid = 0x7;
  3986. spec->unsol_tag_amic1 = 0x11;
  3987. } else {
  3988. spec->num_outputs = 2;
  3989. spec->out_pins[0] = 0x0b; /* speaker out */
  3990. spec->out_pins[1] = 0x10; /* headphone out */
  3991. spec->shared_out_nid = 0x2;
  3992. spec->unsol_tag_hp = spec->out_pins[1];
  3993. spec->adcs[0] = 0x7; /* digital mic / analog mic1 */
  3994. spec->adcs[1] = 0x8; /* analog mic2 */
  3995. spec->adcs[2] = 0xa; /* what u hear */
  3996. spec->num_inputs = 3;
  3997. spec->input_pins[0] = 0x12;
  3998. spec->input_pins[1] = 0x11;
  3999. spec->input_pins[2] = 0x13;
  4000. spec->shared_mic_nid = 0x7;
  4001. spec->unsol_tag_amic1 = spec->input_pins[0];
  4002. /* SPDIF I/O */
  4003. spec->dig_out = 0x05;
  4004. spec->multiout.dig_out_nid = spec->dig_out;
  4005. cfg->dig_out_pins[0] = 0x0c;
  4006. cfg->dig_outs = 1;
  4007. cfg->dig_out_type[0] = HDA_PCM_TYPE_SPDIF;
  4008. spec->dig_in = 0x09;
  4009. cfg->dig_in_pin = 0x0e;
  4010. cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
  4011. }
  4012. }
  4013. static int ca0132_prepare_verbs(struct hda_codec *codec)
  4014. {
  4015. /* Verbs + terminator (an empty element) */
  4016. #define NUM_SPEC_VERBS 4
  4017. struct ca0132_spec *spec = codec->spec;
  4018. spec->chip_init_verbs = ca0132_init_verbs0;
  4019. spec->spec_init_verbs = kzalloc(sizeof(struct hda_verb) * NUM_SPEC_VERBS, GFP_KERNEL);
  4020. if (!spec->spec_init_verbs)
  4021. return -ENOMEM;
  4022. /* HP jack autodetection */
  4023. spec->spec_init_verbs[0].nid = spec->unsol_tag_hp;
  4024. spec->spec_init_verbs[0].param = AC_VERB_SET_UNSOLICITED_ENABLE;
  4025. spec->spec_init_verbs[0].verb = AC_USRSP_EN | spec->unsol_tag_hp;
  4026. /* MIC1 jack autodetection */
  4027. spec->spec_init_verbs[1].nid = spec->unsol_tag_amic1;
  4028. spec->spec_init_verbs[1].param = AC_VERB_SET_UNSOLICITED_ENABLE;
  4029. spec->spec_init_verbs[1].verb = AC_USRSP_EN | spec->unsol_tag_amic1;
  4030. /* config EAPD */
  4031. spec->spec_init_verbs[2].nid = 0x0b;
  4032. spec->spec_init_verbs[2].param = 0x78D;
  4033. spec->spec_init_verbs[2].verb = 0x00;
  4034. /* Previously commented configuration */
  4035. /*
  4036. spec->spec_init_verbs[3].nid = 0x0b;
  4037. spec->spec_init_verbs[3].param = AC_VERB_SET_EAPD_BTLENABLE;
  4038. spec->spec_init_verbs[3].verb = 0x02;
  4039. spec->spec_init_verbs[4].nid = 0x10;
  4040. spec->spec_init_verbs[4].param = 0x78D;
  4041. spec->spec_init_verbs[4].verb = 0x02;
  4042. spec->spec_init_verbs[5].nid = 0x10;
  4043. spec->spec_init_verbs[5].param = AC_VERB_SET_EAPD_BTLENABLE;
  4044. spec->spec_init_verbs[5].verb = 0x02;
  4045. */
  4046. /* Terminator: spec->spec_init_verbs[NUM_SPEC_VERBS-1] */
  4047. return 0;
  4048. }
  4049. static int patch_ca0132(struct hda_codec *codec)
  4050. {
  4051. struct ca0132_spec *spec;
  4052. int err;
  4053. const struct snd_pci_quirk *quirk;
  4054. codec_dbg(codec, "patch_ca0132\n");
  4055. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  4056. if (!spec)
  4057. return -ENOMEM;
  4058. codec->spec = spec;
  4059. spec->codec = codec;
  4060. codec->patch_ops = ca0132_patch_ops;
  4061. codec->pcm_format_first = 1;
  4062. codec->no_sticky_stream = 1;
  4063. /* Detect codec quirk */
  4064. quirk = snd_pci_quirk_lookup(codec->bus->pci, ca0132_quirks);
  4065. if (quirk)
  4066. spec->quirk = quirk->value;
  4067. else
  4068. spec->quirk = QUIRK_NONE;
  4069. spec->dsp_state = DSP_DOWNLOAD_INIT;
  4070. spec->num_mixers = 1;
  4071. spec->mixers[0] = ca0132_mixer;
  4072. spec->base_init_verbs = ca0132_base_init_verbs;
  4073. spec->base_exit_verbs = ca0132_base_exit_verbs;
  4074. INIT_DELAYED_WORK(&spec->unsol_hp_work, ca0132_unsol_hp_delayed);
  4075. ca0132_init_chip(codec);
  4076. ca0132_config(codec);
  4077. err = ca0132_prepare_verbs(codec);
  4078. if (err < 0)
  4079. return err;
  4080. err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
  4081. if (err < 0)
  4082. return err;
  4083. return 0;
  4084. }
  4085. /*
  4086. * patch entries
  4087. */
  4088. static struct hda_device_id snd_hda_id_ca0132[] = {
  4089. HDA_CODEC_ENTRY(0x11020011, "CA0132", patch_ca0132),
  4090. {} /* terminator */
  4091. };
  4092. MODULE_DEVICE_TABLE(hdaudio, snd_hda_id_ca0132);
  4093. MODULE_LICENSE("GPL");
  4094. MODULE_DESCRIPTION("Creative Sound Core3D codec");
  4095. static struct hda_codec_driver ca0132_driver = {
  4096. .id = snd_hda_id_ca0132,
  4097. };
  4098. module_hda_codec_driver(ca0132_driver);