patch_ca0132.c 123 KB

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