patch_ca0132.c 121 KB

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