patch_ca0132.c 122 KB

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