patch_ca0132.c 121 KB

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