patch_ca0132.c 122 KB

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