patch_ca0132.c 120 KB

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