patch_ca0132.c 124 KB

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