wm8994.c 125 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526
  1. /*
  2. * wm8994.c -- WM8994 ALSA SoC Audio driver
  3. *
  4. * Copyright 2009-12 Wolfson Microelectronics plc
  5. *
  6. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  7. *
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/moduleparam.h>
  15. #include <linux/init.h>
  16. #include <linux/delay.h>
  17. #include <linux/pm.h>
  18. #include <linux/gcd.h>
  19. #include <linux/i2c.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/pm_runtime.h>
  22. #include <linux/regulator/consumer.h>
  23. #include <linux/slab.h>
  24. #include <sound/core.h>
  25. #include <sound/jack.h>
  26. #include <sound/pcm.h>
  27. #include <sound/pcm_params.h>
  28. #include <sound/soc.h>
  29. #include <sound/initval.h>
  30. #include <sound/tlv.h>
  31. #include <trace/events/asoc.h>
  32. #include <linux/mfd/wm8994/core.h>
  33. #include <linux/mfd/wm8994/registers.h>
  34. #include <linux/mfd/wm8994/pdata.h>
  35. #include <linux/mfd/wm8994/gpio.h>
  36. #include "wm8994.h"
  37. #include "wm_hubs.h"
  38. #define WM1811_JACKDET_MODE_NONE 0x0000
  39. #define WM1811_JACKDET_MODE_JACK 0x0100
  40. #define WM1811_JACKDET_MODE_MIC 0x0080
  41. #define WM1811_JACKDET_MODE_AUDIO 0x0180
  42. #define WM8994_NUM_DRC 3
  43. #define WM8994_NUM_EQ 3
  44. static struct {
  45. unsigned int reg;
  46. unsigned int mask;
  47. } wm8994_vu_bits[] = {
  48. { WM8994_LEFT_LINE_INPUT_1_2_VOLUME, WM8994_IN1_VU },
  49. { WM8994_RIGHT_LINE_INPUT_1_2_VOLUME, WM8994_IN1_VU },
  50. { WM8994_LEFT_LINE_INPUT_3_4_VOLUME, WM8994_IN2_VU },
  51. { WM8994_RIGHT_LINE_INPUT_3_4_VOLUME, WM8994_IN2_VU },
  52. { WM8994_SPEAKER_VOLUME_LEFT, WM8994_SPKOUT_VU },
  53. { WM8994_SPEAKER_VOLUME_RIGHT, WM8994_SPKOUT_VU },
  54. { WM8994_LEFT_OUTPUT_VOLUME, WM8994_HPOUT1_VU },
  55. { WM8994_RIGHT_OUTPUT_VOLUME, WM8994_HPOUT1_VU },
  56. { WM8994_LEFT_OPGA_VOLUME, WM8994_MIXOUT_VU },
  57. { WM8994_RIGHT_OPGA_VOLUME, WM8994_MIXOUT_VU },
  58. { WM8994_AIF1_DAC1_LEFT_VOLUME, WM8994_AIF1DAC1_VU },
  59. { WM8994_AIF1_DAC1_RIGHT_VOLUME, WM8994_AIF1DAC1_VU },
  60. { WM8994_AIF1_DAC2_LEFT_VOLUME, WM8994_AIF1DAC2_VU },
  61. { WM8994_AIF1_DAC2_RIGHT_VOLUME, WM8994_AIF1DAC2_VU },
  62. { WM8994_AIF2_DAC_LEFT_VOLUME, WM8994_AIF2DAC_VU },
  63. { WM8994_AIF2_DAC_RIGHT_VOLUME, WM8994_AIF2DAC_VU },
  64. { WM8994_AIF1_ADC1_LEFT_VOLUME, WM8994_AIF1ADC1_VU },
  65. { WM8994_AIF1_ADC1_RIGHT_VOLUME, WM8994_AIF1ADC1_VU },
  66. { WM8994_AIF1_ADC2_LEFT_VOLUME, WM8994_AIF1ADC2_VU },
  67. { WM8994_AIF1_ADC2_RIGHT_VOLUME, WM8994_AIF1ADC2_VU },
  68. { WM8994_AIF2_ADC_LEFT_VOLUME, WM8994_AIF2ADC_VU },
  69. { WM8994_AIF2_ADC_RIGHT_VOLUME, WM8994_AIF1ADC2_VU },
  70. { WM8994_DAC1_LEFT_VOLUME, WM8994_DAC1_VU },
  71. { WM8994_DAC1_RIGHT_VOLUME, WM8994_DAC1_VU },
  72. { WM8994_DAC2_LEFT_VOLUME, WM8994_DAC2_VU },
  73. { WM8994_DAC2_RIGHT_VOLUME, WM8994_DAC2_VU },
  74. };
  75. static int wm8994_drc_base[] = {
  76. WM8994_AIF1_DRC1_1,
  77. WM8994_AIF1_DRC2_1,
  78. WM8994_AIF2_DRC_1,
  79. };
  80. static int wm8994_retune_mobile_base[] = {
  81. WM8994_AIF1_DAC1_EQ_GAINS_1,
  82. WM8994_AIF1_DAC2_EQ_GAINS_1,
  83. WM8994_AIF2_EQ_GAINS_1,
  84. };
  85. static const struct wm8958_micd_rate micdet_rates[] = {
  86. { 32768, true, 1, 4 },
  87. { 32768, false, 1, 1 },
  88. { 44100 * 256, true, 7, 10 },
  89. { 44100 * 256, false, 7, 10 },
  90. };
  91. static const struct wm8958_micd_rate jackdet_rates[] = {
  92. { 32768, true, 0, 1 },
  93. { 32768, false, 0, 1 },
  94. { 44100 * 256, true, 10, 10 },
  95. { 44100 * 256, false, 7, 8 },
  96. };
  97. static void wm8958_micd_set_rate(struct snd_soc_codec *codec)
  98. {
  99. struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
  100. struct wm8994 *control = wm8994->wm8994;
  101. int best, i, sysclk, val;
  102. bool idle;
  103. const struct wm8958_micd_rate *rates;
  104. int num_rates;
  105. idle = !wm8994->jack_mic;
  106. sysclk = snd_soc_read(codec, WM8994_CLOCKING_1);
  107. if (sysclk & WM8994_SYSCLK_SRC)
  108. sysclk = wm8994->aifclk[1];
  109. else
  110. sysclk = wm8994->aifclk[0];
  111. if (control->pdata.micd_rates) {
  112. rates = control->pdata.micd_rates;
  113. num_rates = control->pdata.num_micd_rates;
  114. } else if (wm8994->jackdet) {
  115. rates = jackdet_rates;
  116. num_rates = ARRAY_SIZE(jackdet_rates);
  117. } else {
  118. rates = micdet_rates;
  119. num_rates = ARRAY_SIZE(micdet_rates);
  120. }
  121. best = 0;
  122. for (i = 0; i < num_rates; i++) {
  123. if (rates[i].idle != idle)
  124. continue;
  125. if (abs(rates[i].sysclk - sysclk) <
  126. abs(rates[best].sysclk - sysclk))
  127. best = i;
  128. else if (rates[best].idle != idle)
  129. best = i;
  130. }
  131. val = rates[best].start << WM8958_MICD_BIAS_STARTTIME_SHIFT
  132. | rates[best].rate << WM8958_MICD_RATE_SHIFT;
  133. dev_dbg(codec->dev, "MICD rate %d,%d for %dHz %s\n",
  134. rates[best].start, rates[best].rate, sysclk,
  135. idle ? "idle" : "active");
  136. snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
  137. WM8958_MICD_BIAS_STARTTIME_MASK |
  138. WM8958_MICD_RATE_MASK, val);
  139. }
  140. static int configure_aif_clock(struct snd_soc_codec *codec, int aif)
  141. {
  142. struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
  143. int rate;
  144. int reg1 = 0;
  145. int offset;
  146. if (aif)
  147. offset = 4;
  148. else
  149. offset = 0;
  150. switch (wm8994->sysclk[aif]) {
  151. case WM8994_SYSCLK_MCLK1:
  152. rate = wm8994->mclk[0];
  153. break;
  154. case WM8994_SYSCLK_MCLK2:
  155. reg1 |= 0x8;
  156. rate = wm8994->mclk[1];
  157. break;
  158. case WM8994_SYSCLK_FLL1:
  159. reg1 |= 0x10;
  160. rate = wm8994->fll[0].out;
  161. break;
  162. case WM8994_SYSCLK_FLL2:
  163. reg1 |= 0x18;
  164. rate = wm8994->fll[1].out;
  165. break;
  166. default:
  167. return -EINVAL;
  168. }
  169. if (rate >= 13500000) {
  170. rate /= 2;
  171. reg1 |= WM8994_AIF1CLK_DIV;
  172. dev_dbg(codec->dev, "Dividing AIF%d clock to %dHz\n",
  173. aif + 1, rate);
  174. }
  175. wm8994->aifclk[aif] = rate;
  176. snd_soc_update_bits(codec, WM8994_AIF1_CLOCKING_1 + offset,
  177. WM8994_AIF1CLK_SRC_MASK | WM8994_AIF1CLK_DIV,
  178. reg1);
  179. return 0;
  180. }
  181. static int configure_clock(struct snd_soc_codec *codec)
  182. {
  183. struct snd_soc_dapm_context *dapm = snd_soc_codec_get_dapm(codec);
  184. struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
  185. int change, new;
  186. /* Bring up the AIF clocks first */
  187. configure_aif_clock(codec, 0);
  188. configure_aif_clock(codec, 1);
  189. /* Then switch CLK_SYS over to the higher of them; a change
  190. * can only happen as a result of a clocking change which can
  191. * only be made outside of DAPM so we can safely redo the
  192. * clocking.
  193. */
  194. /* If they're equal it doesn't matter which is used */
  195. if (wm8994->aifclk[0] == wm8994->aifclk[1]) {
  196. wm8958_micd_set_rate(codec);
  197. return 0;
  198. }
  199. if (wm8994->aifclk[0] < wm8994->aifclk[1])
  200. new = WM8994_SYSCLK_SRC;
  201. else
  202. new = 0;
  203. change = snd_soc_update_bits(codec, WM8994_CLOCKING_1,
  204. WM8994_SYSCLK_SRC, new);
  205. if (change)
  206. snd_soc_dapm_sync(dapm);
  207. wm8958_micd_set_rate(codec);
  208. return 0;
  209. }
  210. static int check_clk_sys(struct snd_soc_dapm_widget *source,
  211. struct snd_soc_dapm_widget *sink)
  212. {
  213. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(source->dapm);
  214. int reg = snd_soc_read(codec, WM8994_CLOCKING_1);
  215. const char *clk;
  216. /* Check what we're currently using for CLK_SYS */
  217. if (reg & WM8994_SYSCLK_SRC)
  218. clk = "AIF2CLK";
  219. else
  220. clk = "AIF1CLK";
  221. return strcmp(source->name, clk) == 0;
  222. }
  223. static const char *sidetone_hpf_text[] = {
  224. "2.7kHz", "1.35kHz", "675Hz", "370Hz", "180Hz", "90Hz", "45Hz"
  225. };
  226. static SOC_ENUM_SINGLE_DECL(sidetone_hpf,
  227. WM8994_SIDETONE, 7, sidetone_hpf_text);
  228. static const char *adc_hpf_text[] = {
  229. "HiFi", "Voice 1", "Voice 2", "Voice 3"
  230. };
  231. static SOC_ENUM_SINGLE_DECL(aif1adc1_hpf,
  232. WM8994_AIF1_ADC1_FILTERS, 13, adc_hpf_text);
  233. static SOC_ENUM_SINGLE_DECL(aif1adc2_hpf,
  234. WM8994_AIF1_ADC2_FILTERS, 13, adc_hpf_text);
  235. static SOC_ENUM_SINGLE_DECL(aif2adc_hpf,
  236. WM8994_AIF2_ADC_FILTERS, 13, adc_hpf_text);
  237. static const DECLARE_TLV_DB_SCALE(aif_tlv, 0, 600, 0);
  238. static const DECLARE_TLV_DB_SCALE(digital_tlv, -7200, 75, 1);
  239. static const DECLARE_TLV_DB_SCALE(st_tlv, -3600, 300, 0);
  240. static const DECLARE_TLV_DB_SCALE(wm8994_3d_tlv, -1600, 183, 0);
  241. static const DECLARE_TLV_DB_SCALE(eq_tlv, -1200, 100, 0);
  242. static const DECLARE_TLV_DB_SCALE(ng_tlv, -10200, 600, 0);
  243. static const DECLARE_TLV_DB_SCALE(mixin_boost_tlv, 0, 900, 0);
  244. #define WM8994_DRC_SWITCH(xname, reg, shift) \
  245. SOC_SINGLE_EXT(xname, reg, shift, 1, 0, \
  246. snd_soc_get_volsw, wm8994_put_drc_sw)
  247. static int wm8994_put_drc_sw(struct snd_kcontrol *kcontrol,
  248. struct snd_ctl_elem_value *ucontrol)
  249. {
  250. struct soc_mixer_control *mc =
  251. (struct soc_mixer_control *)kcontrol->private_value;
  252. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  253. int mask, ret;
  254. /* Can't enable both ADC and DAC paths simultaneously */
  255. if (mc->shift == WM8994_AIF1DAC1_DRC_ENA_SHIFT)
  256. mask = WM8994_AIF1ADC1L_DRC_ENA_MASK |
  257. WM8994_AIF1ADC1R_DRC_ENA_MASK;
  258. else
  259. mask = WM8994_AIF1DAC1_DRC_ENA_MASK;
  260. ret = snd_soc_read(codec, mc->reg);
  261. if (ret < 0)
  262. return ret;
  263. if (ret & mask)
  264. return -EINVAL;
  265. return snd_soc_put_volsw(kcontrol, ucontrol);
  266. }
  267. static void wm8994_set_drc(struct snd_soc_codec *codec, int drc)
  268. {
  269. struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
  270. struct wm8994 *control = wm8994->wm8994;
  271. struct wm8994_pdata *pdata = &control->pdata;
  272. int base = wm8994_drc_base[drc];
  273. int cfg = wm8994->drc_cfg[drc];
  274. int save, i;
  275. /* Save any enables; the configuration should clear them. */
  276. save = snd_soc_read(codec, base);
  277. save &= WM8994_AIF1DAC1_DRC_ENA | WM8994_AIF1ADC1L_DRC_ENA |
  278. WM8994_AIF1ADC1R_DRC_ENA;
  279. for (i = 0; i < WM8994_DRC_REGS; i++)
  280. snd_soc_update_bits(codec, base + i, 0xffff,
  281. pdata->drc_cfgs[cfg].regs[i]);
  282. snd_soc_update_bits(codec, base, WM8994_AIF1DAC1_DRC_ENA |
  283. WM8994_AIF1ADC1L_DRC_ENA |
  284. WM8994_AIF1ADC1R_DRC_ENA, save);
  285. }
  286. /* Icky as hell but saves code duplication */
  287. static int wm8994_get_drc(const char *name)
  288. {
  289. if (strcmp(name, "AIF1DRC1 Mode") == 0)
  290. return 0;
  291. if (strcmp(name, "AIF1DRC2 Mode") == 0)
  292. return 1;
  293. if (strcmp(name, "AIF2DRC Mode") == 0)
  294. return 2;
  295. return -EINVAL;
  296. }
  297. static int wm8994_put_drc_enum(struct snd_kcontrol *kcontrol,
  298. struct snd_ctl_elem_value *ucontrol)
  299. {
  300. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  301. struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
  302. struct wm8994 *control = wm8994->wm8994;
  303. struct wm8994_pdata *pdata = &control->pdata;
  304. int drc = wm8994_get_drc(kcontrol->id.name);
  305. int value = ucontrol->value.enumerated.item[0];
  306. if (drc < 0)
  307. return drc;
  308. if (value >= pdata->num_drc_cfgs)
  309. return -EINVAL;
  310. wm8994->drc_cfg[drc] = value;
  311. wm8994_set_drc(codec, drc);
  312. return 0;
  313. }
  314. static int wm8994_get_drc_enum(struct snd_kcontrol *kcontrol,
  315. struct snd_ctl_elem_value *ucontrol)
  316. {
  317. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  318. struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
  319. int drc = wm8994_get_drc(kcontrol->id.name);
  320. if (drc < 0)
  321. return drc;
  322. ucontrol->value.enumerated.item[0] = wm8994->drc_cfg[drc];
  323. return 0;
  324. }
  325. static void wm8994_set_retune_mobile(struct snd_soc_codec *codec, int block)
  326. {
  327. struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
  328. struct wm8994 *control = wm8994->wm8994;
  329. struct wm8994_pdata *pdata = &control->pdata;
  330. int base = wm8994_retune_mobile_base[block];
  331. int iface, best, best_val, save, i, cfg;
  332. if (!pdata || !wm8994->num_retune_mobile_texts)
  333. return;
  334. switch (block) {
  335. case 0:
  336. case 1:
  337. iface = 0;
  338. break;
  339. case 2:
  340. iface = 1;
  341. break;
  342. default:
  343. return;
  344. }
  345. /* Find the version of the currently selected configuration
  346. * with the nearest sample rate. */
  347. cfg = wm8994->retune_mobile_cfg[block];
  348. best = 0;
  349. best_val = INT_MAX;
  350. for (i = 0; i < pdata->num_retune_mobile_cfgs; i++) {
  351. if (strcmp(pdata->retune_mobile_cfgs[i].name,
  352. wm8994->retune_mobile_texts[cfg]) == 0 &&
  353. abs(pdata->retune_mobile_cfgs[i].rate
  354. - wm8994->dac_rates[iface]) < best_val) {
  355. best = i;
  356. best_val = abs(pdata->retune_mobile_cfgs[i].rate
  357. - wm8994->dac_rates[iface]);
  358. }
  359. }
  360. dev_dbg(codec->dev, "ReTune Mobile %d %s/%dHz for %dHz sample rate\n",
  361. block,
  362. pdata->retune_mobile_cfgs[best].name,
  363. pdata->retune_mobile_cfgs[best].rate,
  364. wm8994->dac_rates[iface]);
  365. /* The EQ will be disabled while reconfiguring it, remember the
  366. * current configuration.
  367. */
  368. save = snd_soc_read(codec, base);
  369. save &= WM8994_AIF1DAC1_EQ_ENA;
  370. for (i = 0; i < WM8994_EQ_REGS; i++)
  371. snd_soc_update_bits(codec, base + i, 0xffff,
  372. pdata->retune_mobile_cfgs[best].regs[i]);
  373. snd_soc_update_bits(codec, base, WM8994_AIF1DAC1_EQ_ENA, save);
  374. }
  375. /* Icky as hell but saves code duplication */
  376. static int wm8994_get_retune_mobile_block(const char *name)
  377. {
  378. if (strcmp(name, "AIF1.1 EQ Mode") == 0)
  379. return 0;
  380. if (strcmp(name, "AIF1.2 EQ Mode") == 0)
  381. return 1;
  382. if (strcmp(name, "AIF2 EQ Mode") == 0)
  383. return 2;
  384. return -EINVAL;
  385. }
  386. static int wm8994_put_retune_mobile_enum(struct snd_kcontrol *kcontrol,
  387. struct snd_ctl_elem_value *ucontrol)
  388. {
  389. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  390. struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
  391. struct wm8994 *control = wm8994->wm8994;
  392. struct wm8994_pdata *pdata = &control->pdata;
  393. int block = wm8994_get_retune_mobile_block(kcontrol->id.name);
  394. int value = ucontrol->value.enumerated.item[0];
  395. if (block < 0)
  396. return block;
  397. if (value >= pdata->num_retune_mobile_cfgs)
  398. return -EINVAL;
  399. wm8994->retune_mobile_cfg[block] = value;
  400. wm8994_set_retune_mobile(codec, block);
  401. return 0;
  402. }
  403. static int wm8994_get_retune_mobile_enum(struct snd_kcontrol *kcontrol,
  404. struct snd_ctl_elem_value *ucontrol)
  405. {
  406. struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
  407. struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
  408. int block = wm8994_get_retune_mobile_block(kcontrol->id.name);
  409. if (block < 0)
  410. return block;
  411. ucontrol->value.enumerated.item[0] = wm8994->retune_mobile_cfg[block];
  412. return 0;
  413. }
  414. static const char *aif_chan_src_text[] = {
  415. "Left", "Right"
  416. };
  417. static SOC_ENUM_SINGLE_DECL(aif1adcl_src,
  418. WM8994_AIF1_CONTROL_1, 15, aif_chan_src_text);
  419. static SOC_ENUM_SINGLE_DECL(aif1adcr_src,
  420. WM8994_AIF1_CONTROL_1, 14, aif_chan_src_text);
  421. static SOC_ENUM_SINGLE_DECL(aif2adcl_src,
  422. WM8994_AIF2_CONTROL_1, 15, aif_chan_src_text);
  423. static SOC_ENUM_SINGLE_DECL(aif2adcr_src,
  424. WM8994_AIF2_CONTROL_1, 14, aif_chan_src_text);
  425. static SOC_ENUM_SINGLE_DECL(aif1dacl_src,
  426. WM8994_AIF1_CONTROL_2, 15, aif_chan_src_text);
  427. static SOC_ENUM_SINGLE_DECL(aif1dacr_src,
  428. WM8994_AIF1_CONTROL_2, 14, aif_chan_src_text);
  429. static SOC_ENUM_SINGLE_DECL(aif2dacl_src,
  430. WM8994_AIF2_CONTROL_2, 15, aif_chan_src_text);
  431. static SOC_ENUM_SINGLE_DECL(aif2dacr_src,
  432. WM8994_AIF2_CONTROL_2, 14, aif_chan_src_text);
  433. static const char *osr_text[] = {
  434. "Low Power", "High Performance",
  435. };
  436. static SOC_ENUM_SINGLE_DECL(dac_osr,
  437. WM8994_OVERSAMPLING, 0, osr_text);
  438. static SOC_ENUM_SINGLE_DECL(adc_osr,
  439. WM8994_OVERSAMPLING, 1, osr_text);
  440. static const struct snd_kcontrol_new wm8994_snd_controls[] = {
  441. SOC_DOUBLE_R_TLV("AIF1ADC1 Volume", WM8994_AIF1_ADC1_LEFT_VOLUME,
  442. WM8994_AIF1_ADC1_RIGHT_VOLUME,
  443. 1, 119, 0, digital_tlv),
  444. SOC_DOUBLE_R_TLV("AIF1ADC2 Volume", WM8994_AIF1_ADC2_LEFT_VOLUME,
  445. WM8994_AIF1_ADC2_RIGHT_VOLUME,
  446. 1, 119, 0, digital_tlv),
  447. SOC_DOUBLE_R_TLV("AIF2ADC Volume", WM8994_AIF2_ADC_LEFT_VOLUME,
  448. WM8994_AIF2_ADC_RIGHT_VOLUME,
  449. 1, 119, 0, digital_tlv),
  450. SOC_ENUM("AIF1ADCL Source", aif1adcl_src),
  451. SOC_ENUM("AIF1ADCR Source", aif1adcr_src),
  452. SOC_ENUM("AIF2ADCL Source", aif2adcl_src),
  453. SOC_ENUM("AIF2ADCR Source", aif2adcr_src),
  454. SOC_ENUM("AIF1DACL Source", aif1dacl_src),
  455. SOC_ENUM("AIF1DACR Source", aif1dacr_src),
  456. SOC_ENUM("AIF2DACL Source", aif2dacl_src),
  457. SOC_ENUM("AIF2DACR Source", aif2dacr_src),
  458. SOC_DOUBLE_R_TLV("AIF1DAC1 Volume", WM8994_AIF1_DAC1_LEFT_VOLUME,
  459. WM8994_AIF1_DAC1_RIGHT_VOLUME, 1, 96, 0, digital_tlv),
  460. SOC_DOUBLE_R_TLV("AIF1DAC2 Volume", WM8994_AIF1_DAC2_LEFT_VOLUME,
  461. WM8994_AIF1_DAC2_RIGHT_VOLUME, 1, 96, 0, digital_tlv),
  462. SOC_DOUBLE_R_TLV("AIF2DAC Volume", WM8994_AIF2_DAC_LEFT_VOLUME,
  463. WM8994_AIF2_DAC_RIGHT_VOLUME, 1, 96, 0, digital_tlv),
  464. SOC_SINGLE_TLV("AIF1 Boost Volume", WM8994_AIF1_CONTROL_2, 10, 3, 0, aif_tlv),
  465. SOC_SINGLE_TLV("AIF2 Boost Volume", WM8994_AIF2_CONTROL_2, 10, 3, 0, aif_tlv),
  466. SOC_SINGLE("AIF1DAC1 EQ Switch", WM8994_AIF1_DAC1_EQ_GAINS_1, 0, 1, 0),
  467. SOC_SINGLE("AIF1DAC2 EQ Switch", WM8994_AIF1_DAC2_EQ_GAINS_1, 0, 1, 0),
  468. SOC_SINGLE("AIF2 EQ Switch", WM8994_AIF2_EQ_GAINS_1, 0, 1, 0),
  469. WM8994_DRC_SWITCH("AIF1DAC1 DRC Switch", WM8994_AIF1_DRC1_1, 2),
  470. WM8994_DRC_SWITCH("AIF1ADC1L DRC Switch", WM8994_AIF1_DRC1_1, 1),
  471. WM8994_DRC_SWITCH("AIF1ADC1R DRC Switch", WM8994_AIF1_DRC1_1, 0),
  472. WM8994_DRC_SWITCH("AIF1DAC2 DRC Switch", WM8994_AIF1_DRC2_1, 2),
  473. WM8994_DRC_SWITCH("AIF1ADC2L DRC Switch", WM8994_AIF1_DRC2_1, 1),
  474. WM8994_DRC_SWITCH("AIF1ADC2R DRC Switch", WM8994_AIF1_DRC2_1, 0),
  475. WM8994_DRC_SWITCH("AIF2DAC DRC Switch", WM8994_AIF2_DRC_1, 2),
  476. WM8994_DRC_SWITCH("AIF2ADCL DRC Switch", WM8994_AIF2_DRC_1, 1),
  477. WM8994_DRC_SWITCH("AIF2ADCR DRC Switch", WM8994_AIF2_DRC_1, 0),
  478. SOC_SINGLE_TLV("DAC1 Right Sidetone Volume", WM8994_DAC1_MIXER_VOLUMES,
  479. 5, 12, 0, st_tlv),
  480. SOC_SINGLE_TLV("DAC1 Left Sidetone Volume", WM8994_DAC1_MIXER_VOLUMES,
  481. 0, 12, 0, st_tlv),
  482. SOC_SINGLE_TLV("DAC2 Right Sidetone Volume", WM8994_DAC2_MIXER_VOLUMES,
  483. 5, 12, 0, st_tlv),
  484. SOC_SINGLE_TLV("DAC2 Left Sidetone Volume", WM8994_DAC2_MIXER_VOLUMES,
  485. 0, 12, 0, st_tlv),
  486. SOC_ENUM("Sidetone HPF Mux", sidetone_hpf),
  487. SOC_SINGLE("Sidetone HPF Switch", WM8994_SIDETONE, 6, 1, 0),
  488. SOC_ENUM("AIF1ADC1 HPF Mode", aif1adc1_hpf),
  489. SOC_DOUBLE("AIF1ADC1 HPF Switch", WM8994_AIF1_ADC1_FILTERS, 12, 11, 1, 0),
  490. SOC_ENUM("AIF1ADC2 HPF Mode", aif1adc2_hpf),
  491. SOC_DOUBLE("AIF1ADC2 HPF Switch", WM8994_AIF1_ADC2_FILTERS, 12, 11, 1, 0),
  492. SOC_ENUM("AIF2ADC HPF Mode", aif2adc_hpf),
  493. SOC_DOUBLE("AIF2ADC HPF Switch", WM8994_AIF2_ADC_FILTERS, 12, 11, 1, 0),
  494. SOC_ENUM("ADC OSR", adc_osr),
  495. SOC_ENUM("DAC OSR", dac_osr),
  496. SOC_DOUBLE_R_TLV("DAC1 Volume", WM8994_DAC1_LEFT_VOLUME,
  497. WM8994_DAC1_RIGHT_VOLUME, 1, 96, 0, digital_tlv),
  498. SOC_DOUBLE_R("DAC1 Switch", WM8994_DAC1_LEFT_VOLUME,
  499. WM8994_DAC1_RIGHT_VOLUME, 9, 1, 1),
  500. SOC_DOUBLE_R_TLV("DAC2 Volume", WM8994_DAC2_LEFT_VOLUME,
  501. WM8994_DAC2_RIGHT_VOLUME, 1, 96, 0, digital_tlv),
  502. SOC_DOUBLE_R("DAC2 Switch", WM8994_DAC2_LEFT_VOLUME,
  503. WM8994_DAC2_RIGHT_VOLUME, 9, 1, 1),
  504. SOC_SINGLE_TLV("SPKL DAC2 Volume", WM8994_SPKMIXL_ATTENUATION,
  505. 6, 1, 1, wm_hubs_spkmix_tlv),
  506. SOC_SINGLE_TLV("SPKL DAC1 Volume", WM8994_SPKMIXL_ATTENUATION,
  507. 2, 1, 1, wm_hubs_spkmix_tlv),
  508. SOC_SINGLE_TLV("SPKR DAC2 Volume", WM8994_SPKMIXR_ATTENUATION,
  509. 6, 1, 1, wm_hubs_spkmix_tlv),
  510. SOC_SINGLE_TLV("SPKR DAC1 Volume", WM8994_SPKMIXR_ATTENUATION,
  511. 2, 1, 1, wm_hubs_spkmix_tlv),
  512. SOC_SINGLE_TLV("AIF1DAC1 3D Stereo Volume", WM8994_AIF1_DAC1_FILTERS_2,
  513. 10, 15, 0, wm8994_3d_tlv),
  514. SOC_SINGLE("AIF1DAC1 3D Stereo Switch", WM8994_AIF1_DAC1_FILTERS_2,
  515. 8, 1, 0),
  516. SOC_SINGLE_TLV("AIF1DAC2 3D Stereo Volume", WM8994_AIF1_DAC2_FILTERS_2,
  517. 10, 15, 0, wm8994_3d_tlv),
  518. SOC_SINGLE("AIF1DAC2 3D Stereo Switch", WM8994_AIF1_DAC2_FILTERS_2,
  519. 8, 1, 0),
  520. SOC_SINGLE_TLV("AIF2DAC 3D Stereo Volume", WM8994_AIF2_DAC_FILTERS_2,
  521. 10, 15, 0, wm8994_3d_tlv),
  522. SOC_SINGLE("AIF2DAC 3D Stereo Switch", WM8994_AIF2_DAC_FILTERS_2,
  523. 8, 1, 0),
  524. };
  525. static const struct snd_kcontrol_new wm8994_eq_controls[] = {
  526. SOC_SINGLE_TLV("AIF1DAC1 EQ1 Volume", WM8994_AIF1_DAC1_EQ_GAINS_1, 11, 31, 0,
  527. eq_tlv),
  528. SOC_SINGLE_TLV("AIF1DAC1 EQ2 Volume", WM8994_AIF1_DAC1_EQ_GAINS_1, 6, 31, 0,
  529. eq_tlv),
  530. SOC_SINGLE_TLV("AIF1DAC1 EQ3 Volume", WM8994_AIF1_DAC1_EQ_GAINS_1, 1, 31, 0,
  531. eq_tlv),
  532. SOC_SINGLE_TLV("AIF1DAC1 EQ4 Volume", WM8994_AIF1_DAC1_EQ_GAINS_2, 11, 31, 0,
  533. eq_tlv),
  534. SOC_SINGLE_TLV("AIF1DAC1 EQ5 Volume", WM8994_AIF1_DAC1_EQ_GAINS_2, 6, 31, 0,
  535. eq_tlv),
  536. SOC_SINGLE_TLV("AIF1DAC2 EQ1 Volume", WM8994_AIF1_DAC2_EQ_GAINS_1, 11, 31, 0,
  537. eq_tlv),
  538. SOC_SINGLE_TLV("AIF1DAC2 EQ2 Volume", WM8994_AIF1_DAC2_EQ_GAINS_1, 6, 31, 0,
  539. eq_tlv),
  540. SOC_SINGLE_TLV("AIF1DAC2 EQ3 Volume", WM8994_AIF1_DAC2_EQ_GAINS_1, 1, 31, 0,
  541. eq_tlv),
  542. SOC_SINGLE_TLV("AIF1DAC2 EQ4 Volume", WM8994_AIF1_DAC2_EQ_GAINS_2, 11, 31, 0,
  543. eq_tlv),
  544. SOC_SINGLE_TLV("AIF1DAC2 EQ5 Volume", WM8994_AIF1_DAC2_EQ_GAINS_2, 6, 31, 0,
  545. eq_tlv),
  546. SOC_SINGLE_TLV("AIF2 EQ1 Volume", WM8994_AIF2_EQ_GAINS_1, 11, 31, 0,
  547. eq_tlv),
  548. SOC_SINGLE_TLV("AIF2 EQ2 Volume", WM8994_AIF2_EQ_GAINS_1, 6, 31, 0,
  549. eq_tlv),
  550. SOC_SINGLE_TLV("AIF2 EQ3 Volume", WM8994_AIF2_EQ_GAINS_1, 1, 31, 0,
  551. eq_tlv),
  552. SOC_SINGLE_TLV("AIF2 EQ4 Volume", WM8994_AIF2_EQ_GAINS_2, 11, 31, 0,
  553. eq_tlv),
  554. SOC_SINGLE_TLV("AIF2 EQ5 Volume", WM8994_AIF2_EQ_GAINS_2, 6, 31, 0,
  555. eq_tlv),
  556. };
  557. static const struct snd_kcontrol_new wm8994_drc_controls[] = {
  558. SND_SOC_BYTES_MASK("AIF1.1 DRC", WM8994_AIF1_DRC1_1, 5,
  559. WM8994_AIF1DAC1_DRC_ENA | WM8994_AIF1ADC1L_DRC_ENA |
  560. WM8994_AIF1ADC1R_DRC_ENA),
  561. SND_SOC_BYTES_MASK("AIF1.2 DRC", WM8994_AIF1_DRC2_1, 5,
  562. WM8994_AIF1DAC2_DRC_ENA | WM8994_AIF1ADC2L_DRC_ENA |
  563. WM8994_AIF1ADC2R_DRC_ENA),
  564. SND_SOC_BYTES_MASK("AIF2 DRC", WM8994_AIF2_DRC_1, 5,
  565. WM8994_AIF2DAC_DRC_ENA | WM8994_AIF2ADCL_DRC_ENA |
  566. WM8994_AIF2ADCR_DRC_ENA),
  567. };
  568. static const char *wm8958_ng_text[] = {
  569. "30ms", "125ms", "250ms", "500ms",
  570. };
  571. static SOC_ENUM_SINGLE_DECL(wm8958_aif1dac1_ng_hold,
  572. WM8958_AIF1_DAC1_NOISE_GATE,
  573. WM8958_AIF1DAC1_NG_THR_SHIFT,
  574. wm8958_ng_text);
  575. static SOC_ENUM_SINGLE_DECL(wm8958_aif1dac2_ng_hold,
  576. WM8958_AIF1_DAC2_NOISE_GATE,
  577. WM8958_AIF1DAC2_NG_THR_SHIFT,
  578. wm8958_ng_text);
  579. static SOC_ENUM_SINGLE_DECL(wm8958_aif2dac_ng_hold,
  580. WM8958_AIF2_DAC_NOISE_GATE,
  581. WM8958_AIF2DAC_NG_THR_SHIFT,
  582. wm8958_ng_text);
  583. static const struct snd_kcontrol_new wm8958_snd_controls[] = {
  584. SOC_SINGLE_TLV("AIF3 Boost Volume", WM8958_AIF3_CONTROL_2, 10, 3, 0, aif_tlv),
  585. SOC_SINGLE("AIF1DAC1 Noise Gate Switch", WM8958_AIF1_DAC1_NOISE_GATE,
  586. WM8958_AIF1DAC1_NG_ENA_SHIFT, 1, 0),
  587. SOC_ENUM("AIF1DAC1 Noise Gate Hold Time", wm8958_aif1dac1_ng_hold),
  588. SOC_SINGLE_TLV("AIF1DAC1 Noise Gate Threshold Volume",
  589. WM8958_AIF1_DAC1_NOISE_GATE, WM8958_AIF1DAC1_NG_THR_SHIFT,
  590. 7, 1, ng_tlv),
  591. SOC_SINGLE("AIF1DAC2 Noise Gate Switch", WM8958_AIF1_DAC2_NOISE_GATE,
  592. WM8958_AIF1DAC2_NG_ENA_SHIFT, 1, 0),
  593. SOC_ENUM("AIF1DAC2 Noise Gate Hold Time", wm8958_aif1dac2_ng_hold),
  594. SOC_SINGLE_TLV("AIF1DAC2 Noise Gate Threshold Volume",
  595. WM8958_AIF1_DAC2_NOISE_GATE, WM8958_AIF1DAC2_NG_THR_SHIFT,
  596. 7, 1, ng_tlv),
  597. SOC_SINGLE("AIF2DAC Noise Gate Switch", WM8958_AIF2_DAC_NOISE_GATE,
  598. WM8958_AIF2DAC_NG_ENA_SHIFT, 1, 0),
  599. SOC_ENUM("AIF2DAC Noise Gate Hold Time", wm8958_aif2dac_ng_hold),
  600. SOC_SINGLE_TLV("AIF2DAC Noise Gate Threshold Volume",
  601. WM8958_AIF2_DAC_NOISE_GATE, WM8958_AIF2DAC_NG_THR_SHIFT,
  602. 7, 1, ng_tlv),
  603. };
  604. static const struct snd_kcontrol_new wm1811_snd_controls[] = {
  605. SOC_SINGLE_TLV("MIXINL IN1LP Boost Volume", WM8994_INPUT_MIXER_1, 7, 1, 0,
  606. mixin_boost_tlv),
  607. SOC_SINGLE_TLV("MIXINL IN1RP Boost Volume", WM8994_INPUT_MIXER_1, 8, 1, 0,
  608. mixin_boost_tlv),
  609. };
  610. /* We run all mode setting through a function to enforce audio mode */
  611. static void wm1811_jackdet_set_mode(struct snd_soc_codec *codec, u16 mode)
  612. {
  613. struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
  614. if (!wm8994->jackdet || !wm8994->micdet[0].jack)
  615. return;
  616. if (wm8994->active_refcount)
  617. mode = WM1811_JACKDET_MODE_AUDIO;
  618. if (mode == wm8994->jackdet_mode)
  619. return;
  620. wm8994->jackdet_mode = mode;
  621. /* Always use audio mode to detect while the system is active */
  622. if (mode != WM1811_JACKDET_MODE_NONE)
  623. mode = WM1811_JACKDET_MODE_AUDIO;
  624. snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
  625. WM1811_JACKDET_MODE_MASK, mode);
  626. }
  627. static void active_reference(struct snd_soc_codec *codec)
  628. {
  629. struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
  630. mutex_lock(&wm8994->accdet_lock);
  631. wm8994->active_refcount++;
  632. dev_dbg(codec->dev, "Active refcount incremented, now %d\n",
  633. wm8994->active_refcount);
  634. /* If we're using jack detection go into audio mode */
  635. wm1811_jackdet_set_mode(codec, WM1811_JACKDET_MODE_AUDIO);
  636. mutex_unlock(&wm8994->accdet_lock);
  637. }
  638. static void active_dereference(struct snd_soc_codec *codec)
  639. {
  640. struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
  641. u16 mode;
  642. mutex_lock(&wm8994->accdet_lock);
  643. wm8994->active_refcount--;
  644. dev_dbg(codec->dev, "Active refcount decremented, now %d\n",
  645. wm8994->active_refcount);
  646. if (wm8994->active_refcount == 0) {
  647. /* Go into appropriate detection only mode */
  648. if (wm8994->jack_mic || wm8994->mic_detecting)
  649. mode = WM1811_JACKDET_MODE_MIC;
  650. else
  651. mode = WM1811_JACKDET_MODE_JACK;
  652. wm1811_jackdet_set_mode(codec, mode);
  653. }
  654. mutex_unlock(&wm8994->accdet_lock);
  655. }
  656. static int clk_sys_event(struct snd_soc_dapm_widget *w,
  657. struct snd_kcontrol *kcontrol, int event)
  658. {
  659. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  660. struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
  661. switch (event) {
  662. case SND_SOC_DAPM_PRE_PMU:
  663. return configure_clock(codec);
  664. case SND_SOC_DAPM_POST_PMU:
  665. /*
  666. * JACKDET won't run until we start the clock and it
  667. * only reports deltas, make sure we notify the state
  668. * up the stack on startup. Use a *very* generous
  669. * timeout for paranoia, there's no urgency and we
  670. * don't want false reports.
  671. */
  672. if (wm8994->jackdet && !wm8994->clk_has_run) {
  673. queue_delayed_work(system_power_efficient_wq,
  674. &wm8994->jackdet_bootstrap,
  675. msecs_to_jiffies(1000));
  676. wm8994->clk_has_run = true;
  677. }
  678. break;
  679. case SND_SOC_DAPM_POST_PMD:
  680. configure_clock(codec);
  681. break;
  682. }
  683. return 0;
  684. }
  685. static void vmid_reference(struct snd_soc_codec *codec)
  686. {
  687. struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
  688. pm_runtime_get_sync(codec->dev);
  689. wm8994->vmid_refcount++;
  690. dev_dbg(codec->dev, "Referencing VMID, refcount is now %d\n",
  691. wm8994->vmid_refcount);
  692. if (wm8994->vmid_refcount == 1) {
  693. snd_soc_update_bits(codec, WM8994_ANTIPOP_1,
  694. WM8994_LINEOUT1_DISCH |
  695. WM8994_LINEOUT2_DISCH, 0);
  696. wm_hubs_vmid_ena(codec);
  697. switch (wm8994->vmid_mode) {
  698. default:
  699. WARN_ON(NULL == "Invalid VMID mode");
  700. case WM8994_VMID_NORMAL:
  701. /* Startup bias, VMID ramp & buffer */
  702. snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
  703. WM8994_BIAS_SRC |
  704. WM8994_VMID_DISCH |
  705. WM8994_STARTUP_BIAS_ENA |
  706. WM8994_VMID_BUF_ENA |
  707. WM8994_VMID_RAMP_MASK,
  708. WM8994_BIAS_SRC |
  709. WM8994_STARTUP_BIAS_ENA |
  710. WM8994_VMID_BUF_ENA |
  711. (0x2 << WM8994_VMID_RAMP_SHIFT));
  712. /* Main bias enable, VMID=2x40k */
  713. snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_1,
  714. WM8994_BIAS_ENA |
  715. WM8994_VMID_SEL_MASK,
  716. WM8994_BIAS_ENA | 0x2);
  717. msleep(300);
  718. snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
  719. WM8994_VMID_RAMP_MASK |
  720. WM8994_BIAS_SRC,
  721. 0);
  722. break;
  723. case WM8994_VMID_FORCE:
  724. /* Startup bias, slow VMID ramp & buffer */
  725. snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
  726. WM8994_BIAS_SRC |
  727. WM8994_VMID_DISCH |
  728. WM8994_STARTUP_BIAS_ENA |
  729. WM8994_VMID_BUF_ENA |
  730. WM8994_VMID_RAMP_MASK,
  731. WM8994_BIAS_SRC |
  732. WM8994_STARTUP_BIAS_ENA |
  733. WM8994_VMID_BUF_ENA |
  734. (0x2 << WM8994_VMID_RAMP_SHIFT));
  735. /* Main bias enable, VMID=2x40k */
  736. snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_1,
  737. WM8994_BIAS_ENA |
  738. WM8994_VMID_SEL_MASK,
  739. WM8994_BIAS_ENA | 0x2);
  740. msleep(400);
  741. snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
  742. WM8994_VMID_RAMP_MASK |
  743. WM8994_BIAS_SRC,
  744. 0);
  745. break;
  746. }
  747. }
  748. }
  749. static void vmid_dereference(struct snd_soc_codec *codec)
  750. {
  751. struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
  752. wm8994->vmid_refcount--;
  753. dev_dbg(codec->dev, "Dereferencing VMID, refcount is now %d\n",
  754. wm8994->vmid_refcount);
  755. if (wm8994->vmid_refcount == 0) {
  756. if (wm8994->hubs.lineout1_se)
  757. snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_3,
  758. WM8994_LINEOUT1N_ENA |
  759. WM8994_LINEOUT1P_ENA,
  760. WM8994_LINEOUT1N_ENA |
  761. WM8994_LINEOUT1P_ENA);
  762. if (wm8994->hubs.lineout2_se)
  763. snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_3,
  764. WM8994_LINEOUT2N_ENA |
  765. WM8994_LINEOUT2P_ENA,
  766. WM8994_LINEOUT2N_ENA |
  767. WM8994_LINEOUT2P_ENA);
  768. /* Start discharging VMID */
  769. snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
  770. WM8994_BIAS_SRC |
  771. WM8994_VMID_DISCH,
  772. WM8994_BIAS_SRC |
  773. WM8994_VMID_DISCH);
  774. snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_1,
  775. WM8994_VMID_SEL_MASK, 0);
  776. msleep(400);
  777. /* Active discharge */
  778. snd_soc_update_bits(codec, WM8994_ANTIPOP_1,
  779. WM8994_LINEOUT1_DISCH |
  780. WM8994_LINEOUT2_DISCH,
  781. WM8994_LINEOUT1_DISCH |
  782. WM8994_LINEOUT2_DISCH);
  783. snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_3,
  784. WM8994_LINEOUT1N_ENA |
  785. WM8994_LINEOUT1P_ENA |
  786. WM8994_LINEOUT2N_ENA |
  787. WM8994_LINEOUT2P_ENA, 0);
  788. /* Switch off startup biases */
  789. snd_soc_update_bits(codec, WM8994_ANTIPOP_2,
  790. WM8994_BIAS_SRC |
  791. WM8994_STARTUP_BIAS_ENA |
  792. WM8994_VMID_BUF_ENA |
  793. WM8994_VMID_RAMP_MASK, 0);
  794. snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_1,
  795. WM8994_VMID_SEL_MASK, 0);
  796. }
  797. pm_runtime_put(codec->dev);
  798. }
  799. static int vmid_event(struct snd_soc_dapm_widget *w,
  800. struct snd_kcontrol *kcontrol, int event)
  801. {
  802. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  803. switch (event) {
  804. case SND_SOC_DAPM_PRE_PMU:
  805. vmid_reference(codec);
  806. break;
  807. case SND_SOC_DAPM_POST_PMD:
  808. vmid_dereference(codec);
  809. break;
  810. }
  811. return 0;
  812. }
  813. static bool wm8994_check_class_w_digital(struct snd_soc_codec *codec)
  814. {
  815. int source = 0; /* GCC flow analysis can't track enable */
  816. int reg, reg_r;
  817. /* We also need the same AIF source for L/R and only one path */
  818. reg = snd_soc_read(codec, WM8994_DAC1_LEFT_MIXER_ROUTING);
  819. switch (reg) {
  820. case WM8994_AIF2DACL_TO_DAC1L:
  821. dev_vdbg(codec->dev, "Class W source AIF2DAC\n");
  822. source = 2 << WM8994_CP_DYN_SRC_SEL_SHIFT;
  823. break;
  824. case WM8994_AIF1DAC2L_TO_DAC1L:
  825. dev_vdbg(codec->dev, "Class W source AIF1DAC2\n");
  826. source = 1 << WM8994_CP_DYN_SRC_SEL_SHIFT;
  827. break;
  828. case WM8994_AIF1DAC1L_TO_DAC1L:
  829. dev_vdbg(codec->dev, "Class W source AIF1DAC1\n");
  830. source = 0 << WM8994_CP_DYN_SRC_SEL_SHIFT;
  831. break;
  832. default:
  833. dev_vdbg(codec->dev, "DAC mixer setting: %x\n", reg);
  834. return false;
  835. }
  836. reg_r = snd_soc_read(codec, WM8994_DAC1_RIGHT_MIXER_ROUTING);
  837. if (reg_r != reg) {
  838. dev_vdbg(codec->dev, "Left and right DAC mixers different\n");
  839. return false;
  840. }
  841. /* Set the source up */
  842. snd_soc_update_bits(codec, WM8994_CLASS_W_1,
  843. WM8994_CP_DYN_SRC_SEL_MASK, source);
  844. return true;
  845. }
  846. static int aif1clk_ev(struct snd_soc_dapm_widget *w,
  847. struct snd_kcontrol *kcontrol, int event)
  848. {
  849. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  850. struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
  851. struct wm8994 *control = wm8994->wm8994;
  852. int mask = WM8994_AIF1DAC1L_ENA | WM8994_AIF1DAC1R_ENA;
  853. int i;
  854. int dac;
  855. int adc;
  856. int val;
  857. switch (control->type) {
  858. case WM8994:
  859. case WM8958:
  860. mask |= WM8994_AIF1DAC2L_ENA | WM8994_AIF1DAC2R_ENA;
  861. break;
  862. default:
  863. break;
  864. }
  865. switch (event) {
  866. case SND_SOC_DAPM_PRE_PMU:
  867. /* Don't enable timeslot 2 if not in use */
  868. if (wm8994->channels[0] <= 2)
  869. mask &= ~(WM8994_AIF1DAC2L_ENA | WM8994_AIF1DAC2R_ENA);
  870. val = snd_soc_read(codec, WM8994_AIF1_CONTROL_1);
  871. if ((val & WM8994_AIF1ADCL_SRC) &&
  872. (val & WM8994_AIF1ADCR_SRC))
  873. adc = WM8994_AIF1ADC1R_ENA | WM8994_AIF1ADC2R_ENA;
  874. else if (!(val & WM8994_AIF1ADCL_SRC) &&
  875. !(val & WM8994_AIF1ADCR_SRC))
  876. adc = WM8994_AIF1ADC1L_ENA | WM8994_AIF1ADC2L_ENA;
  877. else
  878. adc = WM8994_AIF1ADC1R_ENA | WM8994_AIF1ADC2R_ENA |
  879. WM8994_AIF1ADC1L_ENA | WM8994_AIF1ADC2L_ENA;
  880. val = snd_soc_read(codec, WM8994_AIF1_CONTROL_2);
  881. if ((val & WM8994_AIF1DACL_SRC) &&
  882. (val & WM8994_AIF1DACR_SRC))
  883. dac = WM8994_AIF1DAC1R_ENA | WM8994_AIF1DAC2R_ENA;
  884. else if (!(val & WM8994_AIF1DACL_SRC) &&
  885. !(val & WM8994_AIF1DACR_SRC))
  886. dac = WM8994_AIF1DAC1L_ENA | WM8994_AIF1DAC2L_ENA;
  887. else
  888. dac = WM8994_AIF1DAC1R_ENA | WM8994_AIF1DAC2R_ENA |
  889. WM8994_AIF1DAC1L_ENA | WM8994_AIF1DAC2L_ENA;
  890. snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_4,
  891. mask, adc);
  892. snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_5,
  893. mask, dac);
  894. snd_soc_update_bits(codec, WM8994_CLOCKING_1,
  895. WM8994_AIF1DSPCLK_ENA |
  896. WM8994_SYSDSPCLK_ENA,
  897. WM8994_AIF1DSPCLK_ENA |
  898. WM8994_SYSDSPCLK_ENA);
  899. snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_4, mask,
  900. WM8994_AIF1ADC1R_ENA |
  901. WM8994_AIF1ADC1L_ENA |
  902. WM8994_AIF1ADC2R_ENA |
  903. WM8994_AIF1ADC2L_ENA);
  904. snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_5, mask,
  905. WM8994_AIF1DAC1R_ENA |
  906. WM8994_AIF1DAC1L_ENA |
  907. WM8994_AIF1DAC2R_ENA |
  908. WM8994_AIF1DAC2L_ENA);
  909. break;
  910. case SND_SOC_DAPM_POST_PMU:
  911. for (i = 0; i < ARRAY_SIZE(wm8994_vu_bits); i++)
  912. snd_soc_write(codec, wm8994_vu_bits[i].reg,
  913. snd_soc_read(codec,
  914. wm8994_vu_bits[i].reg));
  915. break;
  916. case SND_SOC_DAPM_PRE_PMD:
  917. case SND_SOC_DAPM_POST_PMD:
  918. snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_5,
  919. mask, 0);
  920. snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_4,
  921. mask, 0);
  922. val = snd_soc_read(codec, WM8994_CLOCKING_1);
  923. if (val & WM8994_AIF2DSPCLK_ENA)
  924. val = WM8994_SYSDSPCLK_ENA;
  925. else
  926. val = 0;
  927. snd_soc_update_bits(codec, WM8994_CLOCKING_1,
  928. WM8994_SYSDSPCLK_ENA |
  929. WM8994_AIF1DSPCLK_ENA, val);
  930. break;
  931. }
  932. return 0;
  933. }
  934. static int aif2clk_ev(struct snd_soc_dapm_widget *w,
  935. struct snd_kcontrol *kcontrol, int event)
  936. {
  937. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  938. int i;
  939. int dac;
  940. int adc;
  941. int val;
  942. switch (event) {
  943. case SND_SOC_DAPM_PRE_PMU:
  944. val = snd_soc_read(codec, WM8994_AIF2_CONTROL_1);
  945. if ((val & WM8994_AIF2ADCL_SRC) &&
  946. (val & WM8994_AIF2ADCR_SRC))
  947. adc = WM8994_AIF2ADCR_ENA;
  948. else if (!(val & WM8994_AIF2ADCL_SRC) &&
  949. !(val & WM8994_AIF2ADCR_SRC))
  950. adc = WM8994_AIF2ADCL_ENA;
  951. else
  952. adc = WM8994_AIF2ADCL_ENA | WM8994_AIF2ADCR_ENA;
  953. val = snd_soc_read(codec, WM8994_AIF2_CONTROL_2);
  954. if ((val & WM8994_AIF2DACL_SRC) &&
  955. (val & WM8994_AIF2DACR_SRC))
  956. dac = WM8994_AIF2DACR_ENA;
  957. else if (!(val & WM8994_AIF2DACL_SRC) &&
  958. !(val & WM8994_AIF2DACR_SRC))
  959. dac = WM8994_AIF2DACL_ENA;
  960. else
  961. dac = WM8994_AIF2DACL_ENA | WM8994_AIF2DACR_ENA;
  962. snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_4,
  963. WM8994_AIF2ADCL_ENA |
  964. WM8994_AIF2ADCR_ENA, adc);
  965. snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_5,
  966. WM8994_AIF2DACL_ENA |
  967. WM8994_AIF2DACR_ENA, dac);
  968. snd_soc_update_bits(codec, WM8994_CLOCKING_1,
  969. WM8994_AIF2DSPCLK_ENA |
  970. WM8994_SYSDSPCLK_ENA,
  971. WM8994_AIF2DSPCLK_ENA |
  972. WM8994_SYSDSPCLK_ENA);
  973. snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_4,
  974. WM8994_AIF2ADCL_ENA |
  975. WM8994_AIF2ADCR_ENA,
  976. WM8994_AIF2ADCL_ENA |
  977. WM8994_AIF2ADCR_ENA);
  978. snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_5,
  979. WM8994_AIF2DACL_ENA |
  980. WM8994_AIF2DACR_ENA,
  981. WM8994_AIF2DACL_ENA |
  982. WM8994_AIF2DACR_ENA);
  983. break;
  984. case SND_SOC_DAPM_POST_PMU:
  985. for (i = 0; i < ARRAY_SIZE(wm8994_vu_bits); i++)
  986. snd_soc_write(codec, wm8994_vu_bits[i].reg,
  987. snd_soc_read(codec,
  988. wm8994_vu_bits[i].reg));
  989. break;
  990. case SND_SOC_DAPM_PRE_PMD:
  991. case SND_SOC_DAPM_POST_PMD:
  992. snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_5,
  993. WM8994_AIF2DACL_ENA |
  994. WM8994_AIF2DACR_ENA, 0);
  995. snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_4,
  996. WM8994_AIF2ADCL_ENA |
  997. WM8994_AIF2ADCR_ENA, 0);
  998. val = snd_soc_read(codec, WM8994_CLOCKING_1);
  999. if (val & WM8994_AIF1DSPCLK_ENA)
  1000. val = WM8994_SYSDSPCLK_ENA;
  1001. else
  1002. val = 0;
  1003. snd_soc_update_bits(codec, WM8994_CLOCKING_1,
  1004. WM8994_SYSDSPCLK_ENA |
  1005. WM8994_AIF2DSPCLK_ENA, val);
  1006. break;
  1007. }
  1008. return 0;
  1009. }
  1010. static int aif1clk_late_ev(struct snd_soc_dapm_widget *w,
  1011. struct snd_kcontrol *kcontrol, int event)
  1012. {
  1013. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  1014. struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
  1015. switch (event) {
  1016. case SND_SOC_DAPM_PRE_PMU:
  1017. wm8994->aif1clk_enable = 1;
  1018. break;
  1019. case SND_SOC_DAPM_POST_PMD:
  1020. wm8994->aif1clk_disable = 1;
  1021. break;
  1022. }
  1023. return 0;
  1024. }
  1025. static int aif2clk_late_ev(struct snd_soc_dapm_widget *w,
  1026. struct snd_kcontrol *kcontrol, int event)
  1027. {
  1028. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  1029. struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
  1030. switch (event) {
  1031. case SND_SOC_DAPM_PRE_PMU:
  1032. wm8994->aif2clk_enable = 1;
  1033. break;
  1034. case SND_SOC_DAPM_POST_PMD:
  1035. wm8994->aif2clk_disable = 1;
  1036. break;
  1037. }
  1038. return 0;
  1039. }
  1040. static int late_enable_ev(struct snd_soc_dapm_widget *w,
  1041. struct snd_kcontrol *kcontrol, int event)
  1042. {
  1043. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  1044. struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
  1045. switch (event) {
  1046. case SND_SOC_DAPM_PRE_PMU:
  1047. if (wm8994->aif1clk_enable) {
  1048. aif1clk_ev(w, kcontrol, SND_SOC_DAPM_PRE_PMU);
  1049. snd_soc_update_bits(codec, WM8994_AIF1_CLOCKING_1,
  1050. WM8994_AIF1CLK_ENA_MASK,
  1051. WM8994_AIF1CLK_ENA);
  1052. aif1clk_ev(w, kcontrol, SND_SOC_DAPM_POST_PMU);
  1053. wm8994->aif1clk_enable = 0;
  1054. }
  1055. if (wm8994->aif2clk_enable) {
  1056. aif2clk_ev(w, kcontrol, SND_SOC_DAPM_PRE_PMU);
  1057. snd_soc_update_bits(codec, WM8994_AIF2_CLOCKING_1,
  1058. WM8994_AIF2CLK_ENA_MASK,
  1059. WM8994_AIF2CLK_ENA);
  1060. aif2clk_ev(w, kcontrol, SND_SOC_DAPM_POST_PMU);
  1061. wm8994->aif2clk_enable = 0;
  1062. }
  1063. break;
  1064. }
  1065. /* We may also have postponed startup of DSP, handle that. */
  1066. wm8958_aif_ev(w, kcontrol, event);
  1067. return 0;
  1068. }
  1069. static int late_disable_ev(struct snd_soc_dapm_widget *w,
  1070. struct snd_kcontrol *kcontrol, int event)
  1071. {
  1072. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  1073. struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
  1074. switch (event) {
  1075. case SND_SOC_DAPM_POST_PMD:
  1076. if (wm8994->aif1clk_disable) {
  1077. aif1clk_ev(w, kcontrol, SND_SOC_DAPM_PRE_PMD);
  1078. snd_soc_update_bits(codec, WM8994_AIF1_CLOCKING_1,
  1079. WM8994_AIF1CLK_ENA_MASK, 0);
  1080. aif1clk_ev(w, kcontrol, SND_SOC_DAPM_POST_PMD);
  1081. wm8994->aif1clk_disable = 0;
  1082. }
  1083. if (wm8994->aif2clk_disable) {
  1084. aif2clk_ev(w, kcontrol, SND_SOC_DAPM_PRE_PMD);
  1085. snd_soc_update_bits(codec, WM8994_AIF2_CLOCKING_1,
  1086. WM8994_AIF2CLK_ENA_MASK, 0);
  1087. aif2clk_ev(w, kcontrol, SND_SOC_DAPM_POST_PMD);
  1088. wm8994->aif2clk_disable = 0;
  1089. }
  1090. break;
  1091. }
  1092. return 0;
  1093. }
  1094. static int adc_mux_ev(struct snd_soc_dapm_widget *w,
  1095. struct snd_kcontrol *kcontrol, int event)
  1096. {
  1097. late_enable_ev(w, kcontrol, event);
  1098. return 0;
  1099. }
  1100. static int micbias_ev(struct snd_soc_dapm_widget *w,
  1101. struct snd_kcontrol *kcontrol, int event)
  1102. {
  1103. late_enable_ev(w, kcontrol, event);
  1104. return 0;
  1105. }
  1106. static int dac_ev(struct snd_soc_dapm_widget *w,
  1107. struct snd_kcontrol *kcontrol, int event)
  1108. {
  1109. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  1110. unsigned int mask = 1 << w->shift;
  1111. snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_5,
  1112. mask, mask);
  1113. return 0;
  1114. }
  1115. static const char *adc_mux_text[] = {
  1116. "ADC",
  1117. "DMIC",
  1118. };
  1119. static SOC_ENUM_SINGLE_VIRT_DECL(adc_enum, adc_mux_text);
  1120. static const struct snd_kcontrol_new adcl_mux =
  1121. SOC_DAPM_ENUM("ADCL Mux", adc_enum);
  1122. static const struct snd_kcontrol_new adcr_mux =
  1123. SOC_DAPM_ENUM("ADCR Mux", adc_enum);
  1124. static const struct snd_kcontrol_new left_speaker_mixer[] = {
  1125. SOC_DAPM_SINGLE("DAC2 Switch", WM8994_SPEAKER_MIXER, 9, 1, 0),
  1126. SOC_DAPM_SINGLE("Input Switch", WM8994_SPEAKER_MIXER, 7, 1, 0),
  1127. SOC_DAPM_SINGLE("IN1LP Switch", WM8994_SPEAKER_MIXER, 5, 1, 0),
  1128. SOC_DAPM_SINGLE("Output Switch", WM8994_SPEAKER_MIXER, 3, 1, 0),
  1129. SOC_DAPM_SINGLE("DAC1 Switch", WM8994_SPEAKER_MIXER, 1, 1, 0),
  1130. };
  1131. static const struct snd_kcontrol_new right_speaker_mixer[] = {
  1132. SOC_DAPM_SINGLE("DAC2 Switch", WM8994_SPEAKER_MIXER, 8, 1, 0),
  1133. SOC_DAPM_SINGLE("Input Switch", WM8994_SPEAKER_MIXER, 6, 1, 0),
  1134. SOC_DAPM_SINGLE("IN1RP Switch", WM8994_SPEAKER_MIXER, 4, 1, 0),
  1135. SOC_DAPM_SINGLE("Output Switch", WM8994_SPEAKER_MIXER, 2, 1, 0),
  1136. SOC_DAPM_SINGLE("DAC1 Switch", WM8994_SPEAKER_MIXER, 0, 1, 0),
  1137. };
  1138. /* Debugging; dump chip status after DAPM transitions */
  1139. static int post_ev(struct snd_soc_dapm_widget *w,
  1140. struct snd_kcontrol *kcontrol, int event)
  1141. {
  1142. struct snd_soc_codec *codec = snd_soc_dapm_to_codec(w->dapm);
  1143. dev_dbg(codec->dev, "SRC status: %x\n",
  1144. snd_soc_read(codec,
  1145. WM8994_RATE_STATUS));
  1146. return 0;
  1147. }
  1148. static const struct snd_kcontrol_new aif1adc1l_mix[] = {
  1149. SOC_DAPM_SINGLE("ADC/DMIC Switch", WM8994_AIF1_ADC1_LEFT_MIXER_ROUTING,
  1150. 1, 1, 0),
  1151. SOC_DAPM_SINGLE("AIF2 Switch", WM8994_AIF1_ADC1_LEFT_MIXER_ROUTING,
  1152. 0, 1, 0),
  1153. };
  1154. static const struct snd_kcontrol_new aif1adc1r_mix[] = {
  1155. SOC_DAPM_SINGLE("ADC/DMIC Switch", WM8994_AIF1_ADC1_RIGHT_MIXER_ROUTING,
  1156. 1, 1, 0),
  1157. SOC_DAPM_SINGLE("AIF2 Switch", WM8994_AIF1_ADC1_RIGHT_MIXER_ROUTING,
  1158. 0, 1, 0),
  1159. };
  1160. static const struct snd_kcontrol_new aif1adc2l_mix[] = {
  1161. SOC_DAPM_SINGLE("DMIC Switch", WM8994_AIF1_ADC2_LEFT_MIXER_ROUTING,
  1162. 1, 1, 0),
  1163. SOC_DAPM_SINGLE("AIF2 Switch", WM8994_AIF1_ADC2_LEFT_MIXER_ROUTING,
  1164. 0, 1, 0),
  1165. };
  1166. static const struct snd_kcontrol_new aif1adc2r_mix[] = {
  1167. SOC_DAPM_SINGLE("DMIC Switch", WM8994_AIF1_ADC2_RIGHT_MIXER_ROUTING,
  1168. 1, 1, 0),
  1169. SOC_DAPM_SINGLE("AIF2 Switch", WM8994_AIF1_ADC2_RIGHT_MIXER_ROUTING,
  1170. 0, 1, 0),
  1171. };
  1172. static const struct snd_kcontrol_new aif2dac2l_mix[] = {
  1173. SOC_DAPM_SINGLE("Right Sidetone Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
  1174. 5, 1, 0),
  1175. SOC_DAPM_SINGLE("Left Sidetone Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
  1176. 4, 1, 0),
  1177. SOC_DAPM_SINGLE("AIF2 Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
  1178. 2, 1, 0),
  1179. SOC_DAPM_SINGLE("AIF1.2 Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
  1180. 1, 1, 0),
  1181. SOC_DAPM_SINGLE("AIF1.1 Switch", WM8994_DAC2_LEFT_MIXER_ROUTING,
  1182. 0, 1, 0),
  1183. };
  1184. static const struct snd_kcontrol_new aif2dac2r_mix[] = {
  1185. SOC_DAPM_SINGLE("Right Sidetone Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
  1186. 5, 1, 0),
  1187. SOC_DAPM_SINGLE("Left Sidetone Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
  1188. 4, 1, 0),
  1189. SOC_DAPM_SINGLE("AIF2 Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
  1190. 2, 1, 0),
  1191. SOC_DAPM_SINGLE("AIF1.2 Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
  1192. 1, 1, 0),
  1193. SOC_DAPM_SINGLE("AIF1.1 Switch", WM8994_DAC2_RIGHT_MIXER_ROUTING,
  1194. 0, 1, 0),
  1195. };
  1196. #define WM8994_CLASS_W_SWITCH(xname, reg, shift, max, invert) \
  1197. SOC_SINGLE_EXT(xname, reg, shift, max, invert, \
  1198. snd_soc_dapm_get_volsw, wm8994_put_class_w)
  1199. static int wm8994_put_class_w(struct snd_kcontrol *kcontrol,
  1200. struct snd_ctl_elem_value *ucontrol)
  1201. {
  1202. struct snd_soc_codec *codec = snd_soc_dapm_kcontrol_codec(kcontrol);
  1203. int ret;
  1204. ret = snd_soc_dapm_put_volsw(kcontrol, ucontrol);
  1205. wm_hubs_update_class_w(codec);
  1206. return ret;
  1207. }
  1208. static const struct snd_kcontrol_new dac1l_mix[] = {
  1209. WM8994_CLASS_W_SWITCH("Right Sidetone Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
  1210. 5, 1, 0),
  1211. WM8994_CLASS_W_SWITCH("Left Sidetone Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
  1212. 4, 1, 0),
  1213. WM8994_CLASS_W_SWITCH("AIF2 Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
  1214. 2, 1, 0),
  1215. WM8994_CLASS_W_SWITCH("AIF1.2 Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
  1216. 1, 1, 0),
  1217. WM8994_CLASS_W_SWITCH("AIF1.1 Switch", WM8994_DAC1_LEFT_MIXER_ROUTING,
  1218. 0, 1, 0),
  1219. };
  1220. static const struct snd_kcontrol_new dac1r_mix[] = {
  1221. WM8994_CLASS_W_SWITCH("Right Sidetone Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
  1222. 5, 1, 0),
  1223. WM8994_CLASS_W_SWITCH("Left Sidetone Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
  1224. 4, 1, 0),
  1225. WM8994_CLASS_W_SWITCH("AIF2 Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
  1226. 2, 1, 0),
  1227. WM8994_CLASS_W_SWITCH("AIF1.2 Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
  1228. 1, 1, 0),
  1229. WM8994_CLASS_W_SWITCH("AIF1.1 Switch", WM8994_DAC1_RIGHT_MIXER_ROUTING,
  1230. 0, 1, 0),
  1231. };
  1232. static const char *sidetone_text[] = {
  1233. "ADC/DMIC1", "DMIC2",
  1234. };
  1235. static SOC_ENUM_SINGLE_DECL(sidetone1_enum,
  1236. WM8994_SIDETONE, 0, sidetone_text);
  1237. static const struct snd_kcontrol_new sidetone1_mux =
  1238. SOC_DAPM_ENUM("Left Sidetone Mux", sidetone1_enum);
  1239. static SOC_ENUM_SINGLE_DECL(sidetone2_enum,
  1240. WM8994_SIDETONE, 1, sidetone_text);
  1241. static const struct snd_kcontrol_new sidetone2_mux =
  1242. SOC_DAPM_ENUM("Right Sidetone Mux", sidetone2_enum);
  1243. static const char *aif1dac_text[] = {
  1244. "AIF1DACDAT", "AIF3DACDAT",
  1245. };
  1246. static const char *loopback_text[] = {
  1247. "None", "ADCDAT",
  1248. };
  1249. static SOC_ENUM_SINGLE_DECL(aif1_loopback_enum,
  1250. WM8994_AIF1_CONTROL_2,
  1251. WM8994_AIF1_LOOPBACK_SHIFT,
  1252. loopback_text);
  1253. static const struct snd_kcontrol_new aif1_loopback =
  1254. SOC_DAPM_ENUM("AIF1 Loopback", aif1_loopback_enum);
  1255. static SOC_ENUM_SINGLE_DECL(aif2_loopback_enum,
  1256. WM8994_AIF2_CONTROL_2,
  1257. WM8994_AIF2_LOOPBACK_SHIFT,
  1258. loopback_text);
  1259. static const struct snd_kcontrol_new aif2_loopback =
  1260. SOC_DAPM_ENUM("AIF2 Loopback", aif2_loopback_enum);
  1261. static SOC_ENUM_SINGLE_DECL(aif1dac_enum,
  1262. WM8994_POWER_MANAGEMENT_6, 0, aif1dac_text);
  1263. static const struct snd_kcontrol_new aif1dac_mux =
  1264. SOC_DAPM_ENUM("AIF1DAC Mux", aif1dac_enum);
  1265. static const char *aif2dac_text[] = {
  1266. "AIF2DACDAT", "AIF3DACDAT",
  1267. };
  1268. static SOC_ENUM_SINGLE_DECL(aif2dac_enum,
  1269. WM8994_POWER_MANAGEMENT_6, 1, aif2dac_text);
  1270. static const struct snd_kcontrol_new aif2dac_mux =
  1271. SOC_DAPM_ENUM("AIF2DAC Mux", aif2dac_enum);
  1272. static const char *aif2adc_text[] = {
  1273. "AIF2ADCDAT", "AIF3DACDAT",
  1274. };
  1275. static SOC_ENUM_SINGLE_DECL(aif2adc_enum,
  1276. WM8994_POWER_MANAGEMENT_6, 2, aif2adc_text);
  1277. static const struct snd_kcontrol_new aif2adc_mux =
  1278. SOC_DAPM_ENUM("AIF2ADC Mux", aif2adc_enum);
  1279. static const char *aif3adc_text[] = {
  1280. "AIF1ADCDAT", "AIF2ADCDAT", "AIF2DACDAT", "Mono PCM",
  1281. };
  1282. static SOC_ENUM_SINGLE_DECL(wm8994_aif3adc_enum,
  1283. WM8994_POWER_MANAGEMENT_6, 3, aif3adc_text);
  1284. static const struct snd_kcontrol_new wm8994_aif3adc_mux =
  1285. SOC_DAPM_ENUM("AIF3ADC Mux", wm8994_aif3adc_enum);
  1286. static SOC_ENUM_SINGLE_DECL(wm8958_aif3adc_enum,
  1287. WM8994_POWER_MANAGEMENT_6, 3, aif3adc_text);
  1288. static const struct snd_kcontrol_new wm8958_aif3adc_mux =
  1289. SOC_DAPM_ENUM("AIF3ADC Mux", wm8958_aif3adc_enum);
  1290. static const char *mono_pcm_out_text[] = {
  1291. "None", "AIF2ADCL", "AIF2ADCR",
  1292. };
  1293. static SOC_ENUM_SINGLE_DECL(mono_pcm_out_enum,
  1294. WM8994_POWER_MANAGEMENT_6, 9, mono_pcm_out_text);
  1295. static const struct snd_kcontrol_new mono_pcm_out_mux =
  1296. SOC_DAPM_ENUM("Mono PCM Out Mux", mono_pcm_out_enum);
  1297. static const char *aif2dac_src_text[] = {
  1298. "AIF2", "AIF3",
  1299. };
  1300. /* Note that these two control shouldn't be simultaneously switched to AIF3 */
  1301. static SOC_ENUM_SINGLE_DECL(aif2dacl_src_enum,
  1302. WM8994_POWER_MANAGEMENT_6, 7, aif2dac_src_text);
  1303. static const struct snd_kcontrol_new aif2dacl_src_mux =
  1304. SOC_DAPM_ENUM("AIF2DACL Mux", aif2dacl_src_enum);
  1305. static SOC_ENUM_SINGLE_DECL(aif2dacr_src_enum,
  1306. WM8994_POWER_MANAGEMENT_6, 8, aif2dac_src_text);
  1307. static const struct snd_kcontrol_new aif2dacr_src_mux =
  1308. SOC_DAPM_ENUM("AIF2DACR Mux", aif2dacr_src_enum);
  1309. static const struct snd_soc_dapm_widget wm8994_lateclk_revd_widgets[] = {
  1310. SND_SOC_DAPM_SUPPLY("AIF1CLK", SND_SOC_NOPM, 0, 0, aif1clk_late_ev,
  1311. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1312. SND_SOC_DAPM_SUPPLY("AIF2CLK", SND_SOC_NOPM, 0, 0, aif2clk_late_ev,
  1313. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1314. SND_SOC_DAPM_PGA_E("Late DAC1L Enable PGA", SND_SOC_NOPM, 0, 0, NULL, 0,
  1315. late_enable_ev, SND_SOC_DAPM_PRE_PMU),
  1316. SND_SOC_DAPM_PGA_E("Late DAC1R Enable PGA", SND_SOC_NOPM, 0, 0, NULL, 0,
  1317. late_enable_ev, SND_SOC_DAPM_PRE_PMU),
  1318. SND_SOC_DAPM_PGA_E("Late DAC2L Enable PGA", SND_SOC_NOPM, 0, 0, NULL, 0,
  1319. late_enable_ev, SND_SOC_DAPM_PRE_PMU),
  1320. SND_SOC_DAPM_PGA_E("Late DAC2R Enable PGA", SND_SOC_NOPM, 0, 0, NULL, 0,
  1321. late_enable_ev, SND_SOC_DAPM_PRE_PMU),
  1322. SND_SOC_DAPM_PGA_E("Direct Voice", SND_SOC_NOPM, 0, 0, NULL, 0,
  1323. late_enable_ev, SND_SOC_DAPM_PRE_PMU),
  1324. SND_SOC_DAPM_MIXER_E("SPKL", WM8994_POWER_MANAGEMENT_3, 8, 0,
  1325. left_speaker_mixer, ARRAY_SIZE(left_speaker_mixer),
  1326. late_enable_ev, SND_SOC_DAPM_PRE_PMU),
  1327. SND_SOC_DAPM_MIXER_E("SPKR", WM8994_POWER_MANAGEMENT_3, 9, 0,
  1328. right_speaker_mixer, ARRAY_SIZE(right_speaker_mixer),
  1329. late_enable_ev, SND_SOC_DAPM_PRE_PMU),
  1330. SND_SOC_DAPM_MUX_E("Left Headphone Mux", SND_SOC_NOPM, 0, 0, &wm_hubs_hpl_mux,
  1331. late_enable_ev, SND_SOC_DAPM_PRE_PMU),
  1332. SND_SOC_DAPM_MUX_E("Right Headphone Mux", SND_SOC_NOPM, 0, 0, &wm_hubs_hpr_mux,
  1333. late_enable_ev, SND_SOC_DAPM_PRE_PMU),
  1334. SND_SOC_DAPM_POST("Late Disable PGA", late_disable_ev)
  1335. };
  1336. static const struct snd_soc_dapm_widget wm8994_lateclk_widgets[] = {
  1337. SND_SOC_DAPM_SUPPLY("AIF1CLK", WM8994_AIF1_CLOCKING_1, 0, 0, aif1clk_ev,
  1338. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  1339. SND_SOC_DAPM_PRE_PMD),
  1340. SND_SOC_DAPM_SUPPLY("AIF2CLK", WM8994_AIF2_CLOCKING_1, 0, 0, aif2clk_ev,
  1341. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  1342. SND_SOC_DAPM_PRE_PMD),
  1343. SND_SOC_DAPM_PGA("Direct Voice", SND_SOC_NOPM, 0, 0, NULL, 0),
  1344. SND_SOC_DAPM_MIXER("SPKL", WM8994_POWER_MANAGEMENT_3, 8, 0,
  1345. left_speaker_mixer, ARRAY_SIZE(left_speaker_mixer)),
  1346. SND_SOC_DAPM_MIXER("SPKR", WM8994_POWER_MANAGEMENT_3, 9, 0,
  1347. right_speaker_mixer, ARRAY_SIZE(right_speaker_mixer)),
  1348. SND_SOC_DAPM_MUX("Left Headphone Mux", SND_SOC_NOPM, 0, 0, &wm_hubs_hpl_mux),
  1349. SND_SOC_DAPM_MUX("Right Headphone Mux", SND_SOC_NOPM, 0, 0, &wm_hubs_hpr_mux),
  1350. };
  1351. static const struct snd_soc_dapm_widget wm8994_dac_revd_widgets[] = {
  1352. SND_SOC_DAPM_DAC_E("DAC2L", NULL, SND_SOC_NOPM, 3, 0,
  1353. dac_ev, SND_SOC_DAPM_PRE_PMU),
  1354. SND_SOC_DAPM_DAC_E("DAC2R", NULL, SND_SOC_NOPM, 2, 0,
  1355. dac_ev, SND_SOC_DAPM_PRE_PMU),
  1356. SND_SOC_DAPM_DAC_E("DAC1L", NULL, SND_SOC_NOPM, 1, 0,
  1357. dac_ev, SND_SOC_DAPM_PRE_PMU),
  1358. SND_SOC_DAPM_DAC_E("DAC1R", NULL, SND_SOC_NOPM, 0, 0,
  1359. dac_ev, SND_SOC_DAPM_PRE_PMU),
  1360. };
  1361. static const struct snd_soc_dapm_widget wm8994_dac_widgets[] = {
  1362. SND_SOC_DAPM_DAC("DAC2L", NULL, WM8994_POWER_MANAGEMENT_5, 3, 0),
  1363. SND_SOC_DAPM_DAC("DAC2R", NULL, WM8994_POWER_MANAGEMENT_5, 2, 0),
  1364. SND_SOC_DAPM_DAC("DAC1L", NULL, WM8994_POWER_MANAGEMENT_5, 1, 0),
  1365. SND_SOC_DAPM_DAC("DAC1R", NULL, WM8994_POWER_MANAGEMENT_5, 0, 0),
  1366. };
  1367. static const struct snd_soc_dapm_widget wm8994_adc_revd_widgets[] = {
  1368. SND_SOC_DAPM_MUX_E("ADCL Mux", WM8994_POWER_MANAGEMENT_4, 1, 0, &adcl_mux,
  1369. adc_mux_ev, SND_SOC_DAPM_PRE_PMU),
  1370. SND_SOC_DAPM_MUX_E("ADCR Mux", WM8994_POWER_MANAGEMENT_4, 0, 0, &adcr_mux,
  1371. adc_mux_ev, SND_SOC_DAPM_PRE_PMU),
  1372. };
  1373. static const struct snd_soc_dapm_widget wm8994_adc_widgets[] = {
  1374. SND_SOC_DAPM_MUX("ADCL Mux", WM8994_POWER_MANAGEMENT_4, 1, 0, &adcl_mux),
  1375. SND_SOC_DAPM_MUX("ADCR Mux", WM8994_POWER_MANAGEMENT_4, 0, 0, &adcr_mux),
  1376. };
  1377. static const struct snd_soc_dapm_widget wm8994_dapm_widgets[] = {
  1378. SND_SOC_DAPM_INPUT("DMIC1DAT"),
  1379. SND_SOC_DAPM_INPUT("DMIC2DAT"),
  1380. SND_SOC_DAPM_INPUT("Clock"),
  1381. SND_SOC_DAPM_SUPPLY_S("MICBIAS Supply", 1, SND_SOC_NOPM, 0, 0, micbias_ev,
  1382. SND_SOC_DAPM_PRE_PMU),
  1383. SND_SOC_DAPM_SUPPLY("VMID", SND_SOC_NOPM, 0, 0, vmid_event,
  1384. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  1385. SND_SOC_DAPM_SUPPLY("CLK_SYS", SND_SOC_NOPM, 0, 0, clk_sys_event,
  1386. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  1387. SND_SOC_DAPM_PRE_PMD),
  1388. SND_SOC_DAPM_SUPPLY("DSP1CLK", SND_SOC_NOPM, 3, 0, NULL, 0),
  1389. SND_SOC_DAPM_SUPPLY("DSP2CLK", SND_SOC_NOPM, 2, 0, NULL, 0),
  1390. SND_SOC_DAPM_SUPPLY("DSPINTCLK", SND_SOC_NOPM, 1, 0, NULL, 0),
  1391. SND_SOC_DAPM_AIF_OUT("AIF1ADC1L", NULL,
  1392. 0, SND_SOC_NOPM, 9, 0),
  1393. SND_SOC_DAPM_AIF_OUT("AIF1ADC1R", NULL,
  1394. 0, SND_SOC_NOPM, 8, 0),
  1395. SND_SOC_DAPM_AIF_IN_E("AIF1DAC1L", NULL, 0,
  1396. SND_SOC_NOPM, 9, 0, wm8958_aif_ev,
  1397. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
  1398. SND_SOC_DAPM_AIF_IN_E("AIF1DAC1R", NULL, 0,
  1399. SND_SOC_NOPM, 8, 0, wm8958_aif_ev,
  1400. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
  1401. SND_SOC_DAPM_AIF_OUT("AIF1ADC2L", NULL,
  1402. 0, SND_SOC_NOPM, 11, 0),
  1403. SND_SOC_DAPM_AIF_OUT("AIF1ADC2R", NULL,
  1404. 0, SND_SOC_NOPM, 10, 0),
  1405. SND_SOC_DAPM_AIF_IN_E("AIF1DAC2L", NULL, 0,
  1406. SND_SOC_NOPM, 11, 0, wm8958_aif_ev,
  1407. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
  1408. SND_SOC_DAPM_AIF_IN_E("AIF1DAC2R", NULL, 0,
  1409. SND_SOC_NOPM, 10, 0, wm8958_aif_ev,
  1410. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
  1411. SND_SOC_DAPM_MIXER("AIF1ADC1L Mixer", SND_SOC_NOPM, 0, 0,
  1412. aif1adc1l_mix, ARRAY_SIZE(aif1adc1l_mix)),
  1413. SND_SOC_DAPM_MIXER("AIF1ADC1R Mixer", SND_SOC_NOPM, 0, 0,
  1414. aif1adc1r_mix, ARRAY_SIZE(aif1adc1r_mix)),
  1415. SND_SOC_DAPM_MIXER("AIF1ADC2L Mixer", SND_SOC_NOPM, 0, 0,
  1416. aif1adc2l_mix, ARRAY_SIZE(aif1adc2l_mix)),
  1417. SND_SOC_DAPM_MIXER("AIF1ADC2R Mixer", SND_SOC_NOPM, 0, 0,
  1418. aif1adc2r_mix, ARRAY_SIZE(aif1adc2r_mix)),
  1419. SND_SOC_DAPM_MIXER("AIF2DAC2L Mixer", SND_SOC_NOPM, 0, 0,
  1420. aif2dac2l_mix, ARRAY_SIZE(aif2dac2l_mix)),
  1421. SND_SOC_DAPM_MIXER("AIF2DAC2R Mixer", SND_SOC_NOPM, 0, 0,
  1422. aif2dac2r_mix, ARRAY_SIZE(aif2dac2r_mix)),
  1423. SND_SOC_DAPM_MUX("Left Sidetone", SND_SOC_NOPM, 0, 0, &sidetone1_mux),
  1424. SND_SOC_DAPM_MUX("Right Sidetone", SND_SOC_NOPM, 0, 0, &sidetone2_mux),
  1425. SND_SOC_DAPM_MIXER("DAC1L Mixer", SND_SOC_NOPM, 0, 0,
  1426. dac1l_mix, ARRAY_SIZE(dac1l_mix)),
  1427. SND_SOC_DAPM_MIXER("DAC1R Mixer", SND_SOC_NOPM, 0, 0,
  1428. dac1r_mix, ARRAY_SIZE(dac1r_mix)),
  1429. SND_SOC_DAPM_AIF_OUT("AIF2ADCL", NULL, 0,
  1430. SND_SOC_NOPM, 13, 0),
  1431. SND_SOC_DAPM_AIF_OUT("AIF2ADCR", NULL, 0,
  1432. SND_SOC_NOPM, 12, 0),
  1433. SND_SOC_DAPM_AIF_IN_E("AIF2DACL", NULL, 0,
  1434. SND_SOC_NOPM, 13, 0, wm8958_aif_ev,
  1435. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
  1436. SND_SOC_DAPM_AIF_IN_E("AIF2DACR", NULL, 0,
  1437. SND_SOC_NOPM, 12, 0, wm8958_aif_ev,
  1438. SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
  1439. SND_SOC_DAPM_AIF_IN("AIF1DACDAT", NULL, 0, SND_SOC_NOPM, 0, 0),
  1440. SND_SOC_DAPM_AIF_IN("AIF2DACDAT", NULL, 0, SND_SOC_NOPM, 0, 0),
  1441. SND_SOC_DAPM_AIF_OUT("AIF1ADCDAT", NULL, 0, SND_SOC_NOPM, 0, 0),
  1442. SND_SOC_DAPM_AIF_OUT("AIF2ADCDAT", NULL, 0, SND_SOC_NOPM, 0, 0),
  1443. SND_SOC_DAPM_MUX("AIF1DAC Mux", SND_SOC_NOPM, 0, 0, &aif1dac_mux),
  1444. SND_SOC_DAPM_MUX("AIF2DAC Mux", SND_SOC_NOPM, 0, 0, &aif2dac_mux),
  1445. SND_SOC_DAPM_MUX("AIF2ADC Mux", SND_SOC_NOPM, 0, 0, &aif2adc_mux),
  1446. SND_SOC_DAPM_AIF_IN("AIF3DACDAT", NULL, 0, SND_SOC_NOPM, 0, 0),
  1447. SND_SOC_DAPM_AIF_OUT("AIF3ADCDAT", NULL, 0, SND_SOC_NOPM, 0, 0),
  1448. SND_SOC_DAPM_SUPPLY("TOCLK", WM8994_CLOCKING_1, 4, 0, NULL, 0),
  1449. SND_SOC_DAPM_ADC("DMIC2L", NULL, WM8994_POWER_MANAGEMENT_4, 5, 0),
  1450. SND_SOC_DAPM_ADC("DMIC2R", NULL, WM8994_POWER_MANAGEMENT_4, 4, 0),
  1451. SND_SOC_DAPM_ADC("DMIC1L", NULL, WM8994_POWER_MANAGEMENT_4, 3, 0),
  1452. SND_SOC_DAPM_ADC("DMIC1R", NULL, WM8994_POWER_MANAGEMENT_4, 2, 0),
  1453. /* Power is done with the muxes since the ADC power also controls the
  1454. * downsampling chain, the chip will automatically manage the analogue
  1455. * specific portions.
  1456. */
  1457. SND_SOC_DAPM_ADC("ADCL", NULL, SND_SOC_NOPM, 1, 0),
  1458. SND_SOC_DAPM_ADC("ADCR", NULL, SND_SOC_NOPM, 0, 0),
  1459. SND_SOC_DAPM_MUX("AIF1 Loopback", SND_SOC_NOPM, 0, 0, &aif1_loopback),
  1460. SND_SOC_DAPM_MUX("AIF2 Loopback", SND_SOC_NOPM, 0, 0, &aif2_loopback),
  1461. SND_SOC_DAPM_POST("Debug log", post_ev),
  1462. };
  1463. static const struct snd_soc_dapm_widget wm8994_specific_dapm_widgets[] = {
  1464. SND_SOC_DAPM_MUX("AIF3ADC Mux", SND_SOC_NOPM, 0, 0, &wm8994_aif3adc_mux),
  1465. };
  1466. static const struct snd_soc_dapm_widget wm8958_dapm_widgets[] = {
  1467. SND_SOC_DAPM_SUPPLY("AIF3", WM8994_POWER_MANAGEMENT_6, 5, 1, NULL, 0),
  1468. SND_SOC_DAPM_MUX("Mono PCM Out Mux", SND_SOC_NOPM, 0, 0, &mono_pcm_out_mux),
  1469. SND_SOC_DAPM_MUX("AIF2DACL Mux", SND_SOC_NOPM, 0, 0, &aif2dacl_src_mux),
  1470. SND_SOC_DAPM_MUX("AIF2DACR Mux", SND_SOC_NOPM, 0, 0, &aif2dacr_src_mux),
  1471. SND_SOC_DAPM_MUX("AIF3ADC Mux", SND_SOC_NOPM, 0, 0, &wm8958_aif3adc_mux),
  1472. };
  1473. static const struct snd_soc_dapm_route intercon[] = {
  1474. { "CLK_SYS", NULL, "AIF1CLK", check_clk_sys },
  1475. { "CLK_SYS", NULL, "AIF2CLK", check_clk_sys },
  1476. { "DSP1CLK", NULL, "CLK_SYS" },
  1477. { "DSP2CLK", NULL, "CLK_SYS" },
  1478. { "DSPINTCLK", NULL, "CLK_SYS" },
  1479. { "AIF1ADC1L", NULL, "AIF1CLK" },
  1480. { "AIF1ADC1L", NULL, "DSP1CLK" },
  1481. { "AIF1ADC1R", NULL, "AIF1CLK" },
  1482. { "AIF1ADC1R", NULL, "DSP1CLK" },
  1483. { "AIF1ADC1R", NULL, "DSPINTCLK" },
  1484. { "AIF1DAC1L", NULL, "AIF1CLK" },
  1485. { "AIF1DAC1L", NULL, "DSP1CLK" },
  1486. { "AIF1DAC1R", NULL, "AIF1CLK" },
  1487. { "AIF1DAC1R", NULL, "DSP1CLK" },
  1488. { "AIF1DAC1R", NULL, "DSPINTCLK" },
  1489. { "AIF1ADC2L", NULL, "AIF1CLK" },
  1490. { "AIF1ADC2L", NULL, "DSP1CLK" },
  1491. { "AIF1ADC2R", NULL, "AIF1CLK" },
  1492. { "AIF1ADC2R", NULL, "DSP1CLK" },
  1493. { "AIF1ADC2R", NULL, "DSPINTCLK" },
  1494. { "AIF1DAC2L", NULL, "AIF1CLK" },
  1495. { "AIF1DAC2L", NULL, "DSP1CLK" },
  1496. { "AIF1DAC2R", NULL, "AIF1CLK" },
  1497. { "AIF1DAC2R", NULL, "DSP1CLK" },
  1498. { "AIF1DAC2R", NULL, "DSPINTCLK" },
  1499. { "AIF2ADCL", NULL, "AIF2CLK" },
  1500. { "AIF2ADCL", NULL, "DSP2CLK" },
  1501. { "AIF2ADCR", NULL, "AIF2CLK" },
  1502. { "AIF2ADCR", NULL, "DSP2CLK" },
  1503. { "AIF2ADCR", NULL, "DSPINTCLK" },
  1504. { "AIF2DACL", NULL, "AIF2CLK" },
  1505. { "AIF2DACL", NULL, "DSP2CLK" },
  1506. { "AIF2DACR", NULL, "AIF2CLK" },
  1507. { "AIF2DACR", NULL, "DSP2CLK" },
  1508. { "AIF2DACR", NULL, "DSPINTCLK" },
  1509. { "DMIC1L", NULL, "DMIC1DAT" },
  1510. { "DMIC1L", NULL, "CLK_SYS" },
  1511. { "DMIC1R", NULL, "DMIC1DAT" },
  1512. { "DMIC1R", NULL, "CLK_SYS" },
  1513. { "DMIC2L", NULL, "DMIC2DAT" },
  1514. { "DMIC2L", NULL, "CLK_SYS" },
  1515. { "DMIC2R", NULL, "DMIC2DAT" },
  1516. { "DMIC2R", NULL, "CLK_SYS" },
  1517. { "ADCL", NULL, "AIF1CLK" },
  1518. { "ADCL", NULL, "DSP1CLK" },
  1519. { "ADCL", NULL, "DSPINTCLK" },
  1520. { "ADCR", NULL, "AIF1CLK" },
  1521. { "ADCR", NULL, "DSP1CLK" },
  1522. { "ADCR", NULL, "DSPINTCLK" },
  1523. { "ADCL Mux", "ADC", "ADCL" },
  1524. { "ADCL Mux", "DMIC", "DMIC1L" },
  1525. { "ADCR Mux", "ADC", "ADCR" },
  1526. { "ADCR Mux", "DMIC", "DMIC1R" },
  1527. { "DAC1L", NULL, "AIF1CLK" },
  1528. { "DAC1L", NULL, "DSP1CLK" },
  1529. { "DAC1L", NULL, "DSPINTCLK" },
  1530. { "DAC1R", NULL, "AIF1CLK" },
  1531. { "DAC1R", NULL, "DSP1CLK" },
  1532. { "DAC1R", NULL, "DSPINTCLK" },
  1533. { "DAC2L", NULL, "AIF2CLK" },
  1534. { "DAC2L", NULL, "DSP2CLK" },
  1535. { "DAC2L", NULL, "DSPINTCLK" },
  1536. { "DAC2R", NULL, "AIF2DACR" },
  1537. { "DAC2R", NULL, "AIF2CLK" },
  1538. { "DAC2R", NULL, "DSP2CLK" },
  1539. { "DAC2R", NULL, "DSPINTCLK" },
  1540. { "TOCLK", NULL, "CLK_SYS" },
  1541. { "AIF1DACDAT", NULL, "AIF1 Playback" },
  1542. { "AIF2DACDAT", NULL, "AIF2 Playback" },
  1543. { "AIF3DACDAT", NULL, "AIF3 Playback" },
  1544. { "AIF1 Capture", NULL, "AIF1ADCDAT" },
  1545. { "AIF2 Capture", NULL, "AIF2ADCDAT" },
  1546. { "AIF3 Capture", NULL, "AIF3ADCDAT" },
  1547. /* AIF1 outputs */
  1548. { "AIF1ADC1L", NULL, "AIF1ADC1L Mixer" },
  1549. { "AIF1ADC1L Mixer", "ADC/DMIC Switch", "ADCL Mux" },
  1550. { "AIF1ADC1L Mixer", "AIF2 Switch", "AIF2DACL" },
  1551. { "AIF1ADC1R", NULL, "AIF1ADC1R Mixer" },
  1552. { "AIF1ADC1R Mixer", "ADC/DMIC Switch", "ADCR Mux" },
  1553. { "AIF1ADC1R Mixer", "AIF2 Switch", "AIF2DACR" },
  1554. { "AIF1ADC2L", NULL, "AIF1ADC2L Mixer" },
  1555. { "AIF1ADC2L Mixer", "DMIC Switch", "DMIC2L" },
  1556. { "AIF1ADC2L Mixer", "AIF2 Switch", "AIF2DACL" },
  1557. { "AIF1ADC2R", NULL, "AIF1ADC2R Mixer" },
  1558. { "AIF1ADC2R Mixer", "DMIC Switch", "DMIC2R" },
  1559. { "AIF1ADC2R Mixer", "AIF2 Switch", "AIF2DACR" },
  1560. /* Pin level routing for AIF3 */
  1561. { "AIF1DAC1L", NULL, "AIF1DAC Mux" },
  1562. { "AIF1DAC1R", NULL, "AIF1DAC Mux" },
  1563. { "AIF1DAC2L", NULL, "AIF1DAC Mux" },
  1564. { "AIF1DAC2R", NULL, "AIF1DAC Mux" },
  1565. { "AIF1DAC Mux", "AIF1DACDAT", "AIF1 Loopback" },
  1566. { "AIF1DAC Mux", "AIF3DACDAT", "AIF3DACDAT" },
  1567. { "AIF2DAC Mux", "AIF2DACDAT", "AIF2 Loopback" },
  1568. { "AIF2DAC Mux", "AIF3DACDAT", "AIF3DACDAT" },
  1569. { "AIF2ADC Mux", "AIF2ADCDAT", "AIF2ADCL" },
  1570. { "AIF2ADC Mux", "AIF2ADCDAT", "AIF2ADCR" },
  1571. { "AIF2ADC Mux", "AIF3DACDAT", "AIF3ADCDAT" },
  1572. /* DAC1 inputs */
  1573. { "DAC1L Mixer", "AIF2 Switch", "AIF2DACL" },
  1574. { "DAC1L Mixer", "AIF1.2 Switch", "AIF1DAC2L" },
  1575. { "DAC1L Mixer", "AIF1.1 Switch", "AIF1DAC1L" },
  1576. { "DAC1L Mixer", "Left Sidetone Switch", "Left Sidetone" },
  1577. { "DAC1L Mixer", "Right Sidetone Switch", "Right Sidetone" },
  1578. { "DAC1R Mixer", "AIF2 Switch", "AIF2DACR" },
  1579. { "DAC1R Mixer", "AIF1.2 Switch", "AIF1DAC2R" },
  1580. { "DAC1R Mixer", "AIF1.1 Switch", "AIF1DAC1R" },
  1581. { "DAC1R Mixer", "Left Sidetone Switch", "Left Sidetone" },
  1582. { "DAC1R Mixer", "Right Sidetone Switch", "Right Sidetone" },
  1583. /* DAC2/AIF2 outputs */
  1584. { "AIF2ADCL", NULL, "AIF2DAC2L Mixer" },
  1585. { "AIF2DAC2L Mixer", "AIF2 Switch", "AIF2DACL" },
  1586. { "AIF2DAC2L Mixer", "AIF1.2 Switch", "AIF1DAC2L" },
  1587. { "AIF2DAC2L Mixer", "AIF1.1 Switch", "AIF1DAC1L" },
  1588. { "AIF2DAC2L Mixer", "Left Sidetone Switch", "Left Sidetone" },
  1589. { "AIF2DAC2L Mixer", "Right Sidetone Switch", "Right Sidetone" },
  1590. { "AIF2ADCR", NULL, "AIF2DAC2R Mixer" },
  1591. { "AIF2DAC2R Mixer", "AIF2 Switch", "AIF2DACR" },
  1592. { "AIF2DAC2R Mixer", "AIF1.2 Switch", "AIF1DAC2R" },
  1593. { "AIF2DAC2R Mixer", "AIF1.1 Switch", "AIF1DAC1R" },
  1594. { "AIF2DAC2R Mixer", "Left Sidetone Switch", "Left Sidetone" },
  1595. { "AIF2DAC2R Mixer", "Right Sidetone Switch", "Right Sidetone" },
  1596. { "AIF1ADCDAT", NULL, "AIF1ADC1L" },
  1597. { "AIF1ADCDAT", NULL, "AIF1ADC1R" },
  1598. { "AIF1ADCDAT", NULL, "AIF1ADC2L" },
  1599. { "AIF1ADCDAT", NULL, "AIF1ADC2R" },
  1600. { "AIF2ADCDAT", NULL, "AIF2ADC Mux" },
  1601. /* AIF3 output */
  1602. { "AIF3ADC Mux", "AIF1ADCDAT", "AIF1ADC1L" },
  1603. { "AIF3ADC Mux", "AIF1ADCDAT", "AIF1ADC1R" },
  1604. { "AIF3ADC Mux", "AIF1ADCDAT", "AIF1ADC2L" },
  1605. { "AIF3ADC Mux", "AIF1ADCDAT", "AIF1ADC2R" },
  1606. { "AIF3ADC Mux", "AIF2ADCDAT", "AIF2ADCL" },
  1607. { "AIF3ADC Mux", "AIF2ADCDAT", "AIF2ADCR" },
  1608. { "AIF3ADC Mux", "AIF2DACDAT", "AIF2DACL" },
  1609. { "AIF3ADC Mux", "AIF2DACDAT", "AIF2DACR" },
  1610. { "AIF3ADCDAT", NULL, "AIF3ADC Mux" },
  1611. /* Loopback */
  1612. { "AIF1 Loopback", "ADCDAT", "AIF1ADCDAT" },
  1613. { "AIF1 Loopback", "None", "AIF1DACDAT" },
  1614. { "AIF2 Loopback", "ADCDAT", "AIF2ADCDAT" },
  1615. { "AIF2 Loopback", "None", "AIF2DACDAT" },
  1616. /* Sidetone */
  1617. { "Left Sidetone", "ADC/DMIC1", "ADCL Mux" },
  1618. { "Left Sidetone", "DMIC2", "DMIC2L" },
  1619. { "Right Sidetone", "ADC/DMIC1", "ADCR Mux" },
  1620. { "Right Sidetone", "DMIC2", "DMIC2R" },
  1621. /* Output stages */
  1622. { "Left Output Mixer", "DAC Switch", "DAC1L" },
  1623. { "Right Output Mixer", "DAC Switch", "DAC1R" },
  1624. { "SPKL", "DAC1 Switch", "DAC1L" },
  1625. { "SPKL", "DAC2 Switch", "DAC2L" },
  1626. { "SPKR", "DAC1 Switch", "DAC1R" },
  1627. { "SPKR", "DAC2 Switch", "DAC2R" },
  1628. { "Left Headphone Mux", "DAC", "DAC1L" },
  1629. { "Right Headphone Mux", "DAC", "DAC1R" },
  1630. };
  1631. static const struct snd_soc_dapm_route wm8994_lateclk_revd_intercon[] = {
  1632. { "DAC1L", NULL, "Late DAC1L Enable PGA" },
  1633. { "Late DAC1L Enable PGA", NULL, "DAC1L Mixer" },
  1634. { "DAC1R", NULL, "Late DAC1R Enable PGA" },
  1635. { "Late DAC1R Enable PGA", NULL, "DAC1R Mixer" },
  1636. { "DAC2L", NULL, "Late DAC2L Enable PGA" },
  1637. { "Late DAC2L Enable PGA", NULL, "AIF2DAC2L Mixer" },
  1638. { "DAC2R", NULL, "Late DAC2R Enable PGA" },
  1639. { "Late DAC2R Enable PGA", NULL, "AIF2DAC2R Mixer" }
  1640. };
  1641. static const struct snd_soc_dapm_route wm8994_lateclk_intercon[] = {
  1642. { "DAC1L", NULL, "DAC1L Mixer" },
  1643. { "DAC1R", NULL, "DAC1R Mixer" },
  1644. { "DAC2L", NULL, "AIF2DAC2L Mixer" },
  1645. { "DAC2R", NULL, "AIF2DAC2R Mixer" },
  1646. };
  1647. static const struct snd_soc_dapm_route wm8994_revd_intercon[] = {
  1648. { "AIF1DACDAT", NULL, "AIF2DACDAT" },
  1649. { "AIF2DACDAT", NULL, "AIF1DACDAT" },
  1650. { "AIF1ADCDAT", NULL, "AIF2ADCDAT" },
  1651. { "AIF2ADCDAT", NULL, "AIF1ADCDAT" },
  1652. { "MICBIAS1", NULL, "CLK_SYS" },
  1653. { "MICBIAS1", NULL, "MICBIAS Supply" },
  1654. { "MICBIAS2", NULL, "CLK_SYS" },
  1655. { "MICBIAS2", NULL, "MICBIAS Supply" },
  1656. };
  1657. static const struct snd_soc_dapm_route wm8994_intercon[] = {
  1658. { "AIF2DACL", NULL, "AIF2DAC Mux" },
  1659. { "AIF2DACR", NULL, "AIF2DAC Mux" },
  1660. { "MICBIAS1", NULL, "VMID" },
  1661. { "MICBIAS2", NULL, "VMID" },
  1662. };
  1663. static const struct snd_soc_dapm_route wm8958_intercon[] = {
  1664. { "AIF2DACL", NULL, "AIF2DACL Mux" },
  1665. { "AIF2DACR", NULL, "AIF2DACR Mux" },
  1666. { "AIF2DACL Mux", "AIF2", "AIF2DAC Mux" },
  1667. { "AIF2DACL Mux", "AIF3", "AIF3DACDAT" },
  1668. { "AIF2DACR Mux", "AIF2", "AIF2DAC Mux" },
  1669. { "AIF2DACR Mux", "AIF3", "AIF3DACDAT" },
  1670. { "AIF3DACDAT", NULL, "AIF3" },
  1671. { "AIF3ADCDAT", NULL, "AIF3" },
  1672. { "Mono PCM Out Mux", "AIF2ADCL", "AIF2ADCL" },
  1673. { "Mono PCM Out Mux", "AIF2ADCR", "AIF2ADCR" },
  1674. { "AIF3ADC Mux", "Mono PCM", "Mono PCM Out Mux" },
  1675. };
  1676. /* The size in bits of the FLL divide multiplied by 10
  1677. * to allow rounding later */
  1678. #define FIXED_FLL_SIZE ((1 << 16) * 10)
  1679. struct fll_div {
  1680. u16 outdiv;
  1681. u16 n;
  1682. u16 k;
  1683. u16 lambda;
  1684. u16 clk_ref_div;
  1685. u16 fll_fratio;
  1686. };
  1687. static int wm8994_get_fll_config(struct wm8994 *control, struct fll_div *fll,
  1688. int freq_in, int freq_out)
  1689. {
  1690. u64 Kpart;
  1691. unsigned int K, Ndiv, Nmod, gcd_fll;
  1692. pr_debug("FLL input=%dHz, output=%dHz\n", freq_in, freq_out);
  1693. /* Scale the input frequency down to <= 13.5MHz */
  1694. fll->clk_ref_div = 0;
  1695. while (freq_in > 13500000) {
  1696. fll->clk_ref_div++;
  1697. freq_in /= 2;
  1698. if (fll->clk_ref_div > 3)
  1699. return -EINVAL;
  1700. }
  1701. pr_debug("CLK_REF_DIV=%d, Fref=%dHz\n", fll->clk_ref_div, freq_in);
  1702. /* Scale the output to give 90MHz<=Fvco<=100MHz */
  1703. fll->outdiv = 3;
  1704. while (freq_out * (fll->outdiv + 1) < 90000000) {
  1705. fll->outdiv++;
  1706. if (fll->outdiv > 63)
  1707. return -EINVAL;
  1708. }
  1709. freq_out *= fll->outdiv + 1;
  1710. pr_debug("OUTDIV=%d, Fvco=%dHz\n", fll->outdiv, freq_out);
  1711. if (freq_in > 1000000) {
  1712. fll->fll_fratio = 0;
  1713. } else if (freq_in > 256000) {
  1714. fll->fll_fratio = 1;
  1715. freq_in *= 2;
  1716. } else if (freq_in > 128000) {
  1717. fll->fll_fratio = 2;
  1718. freq_in *= 4;
  1719. } else if (freq_in > 64000) {
  1720. fll->fll_fratio = 3;
  1721. freq_in *= 8;
  1722. } else {
  1723. fll->fll_fratio = 4;
  1724. freq_in *= 16;
  1725. }
  1726. pr_debug("FLL_FRATIO=%d, Fref=%dHz\n", fll->fll_fratio, freq_in);
  1727. /* Now, calculate N.K */
  1728. Ndiv = freq_out / freq_in;
  1729. fll->n = Ndiv;
  1730. Nmod = freq_out % freq_in;
  1731. pr_debug("Nmod=%d\n", Nmod);
  1732. switch (control->type) {
  1733. case WM8994:
  1734. /* Calculate fractional part - scale up so we can round. */
  1735. Kpart = FIXED_FLL_SIZE * (long long)Nmod;
  1736. do_div(Kpart, freq_in);
  1737. K = Kpart & 0xFFFFFFFF;
  1738. if ((K % 10) >= 5)
  1739. K += 5;
  1740. /* Move down to proper range now rounding is done */
  1741. fll->k = K / 10;
  1742. fll->lambda = 0;
  1743. pr_debug("N=%x K=%x\n", fll->n, fll->k);
  1744. break;
  1745. default:
  1746. gcd_fll = gcd(freq_out, freq_in);
  1747. fll->k = (freq_out - (freq_in * fll->n)) / gcd_fll;
  1748. fll->lambda = freq_in / gcd_fll;
  1749. }
  1750. return 0;
  1751. }
  1752. static int _wm8994_set_fll(struct snd_soc_codec *codec, int id, int src,
  1753. unsigned int freq_in, unsigned int freq_out)
  1754. {
  1755. struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
  1756. struct wm8994 *control = wm8994->wm8994;
  1757. int reg_offset, ret;
  1758. struct fll_div fll;
  1759. u16 reg, clk1, aif_reg, aif_src;
  1760. unsigned long timeout;
  1761. bool was_enabled;
  1762. switch (id) {
  1763. case WM8994_FLL1:
  1764. reg_offset = 0;
  1765. id = 0;
  1766. aif_src = 0x10;
  1767. break;
  1768. case WM8994_FLL2:
  1769. reg_offset = 0x20;
  1770. id = 1;
  1771. aif_src = 0x18;
  1772. break;
  1773. default:
  1774. return -EINVAL;
  1775. }
  1776. reg = snd_soc_read(codec, WM8994_FLL1_CONTROL_1 + reg_offset);
  1777. was_enabled = reg & WM8994_FLL1_ENA;
  1778. switch (src) {
  1779. case 0:
  1780. /* Allow no source specification when stopping */
  1781. if (freq_out)
  1782. return -EINVAL;
  1783. src = wm8994->fll[id].src;
  1784. break;
  1785. case WM8994_FLL_SRC_MCLK1:
  1786. case WM8994_FLL_SRC_MCLK2:
  1787. case WM8994_FLL_SRC_LRCLK:
  1788. case WM8994_FLL_SRC_BCLK:
  1789. break;
  1790. case WM8994_FLL_SRC_INTERNAL:
  1791. freq_in = 12000000;
  1792. freq_out = 12000000;
  1793. break;
  1794. default:
  1795. return -EINVAL;
  1796. }
  1797. /* Are we changing anything? */
  1798. if (wm8994->fll[id].src == src &&
  1799. wm8994->fll[id].in == freq_in && wm8994->fll[id].out == freq_out)
  1800. return 0;
  1801. /* If we're stopping the FLL redo the old config - no
  1802. * registers will actually be written but we avoid GCC flow
  1803. * analysis bugs spewing warnings.
  1804. */
  1805. if (freq_out)
  1806. ret = wm8994_get_fll_config(control, &fll, freq_in, freq_out);
  1807. else
  1808. ret = wm8994_get_fll_config(control, &fll, wm8994->fll[id].in,
  1809. wm8994->fll[id].out);
  1810. if (ret < 0)
  1811. return ret;
  1812. /* Make sure that we're not providing SYSCLK right now */
  1813. clk1 = snd_soc_read(codec, WM8994_CLOCKING_1);
  1814. if (clk1 & WM8994_SYSCLK_SRC)
  1815. aif_reg = WM8994_AIF2_CLOCKING_1;
  1816. else
  1817. aif_reg = WM8994_AIF1_CLOCKING_1;
  1818. reg = snd_soc_read(codec, aif_reg);
  1819. if ((reg & WM8994_AIF1CLK_ENA) &&
  1820. (reg & WM8994_AIF1CLK_SRC_MASK) == aif_src) {
  1821. dev_err(codec->dev, "FLL%d is currently providing SYSCLK\n",
  1822. id + 1);
  1823. return -EBUSY;
  1824. }
  1825. /* We always need to disable the FLL while reconfiguring */
  1826. snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_1 + reg_offset,
  1827. WM8994_FLL1_ENA, 0);
  1828. if (wm8994->fll_byp && src == WM8994_FLL_SRC_BCLK &&
  1829. freq_in == freq_out && freq_out) {
  1830. dev_dbg(codec->dev, "Bypassing FLL%d\n", id + 1);
  1831. snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_5 + reg_offset,
  1832. WM8958_FLL1_BYP, WM8958_FLL1_BYP);
  1833. goto out;
  1834. }
  1835. reg = (fll.outdiv << WM8994_FLL1_OUTDIV_SHIFT) |
  1836. (fll.fll_fratio << WM8994_FLL1_FRATIO_SHIFT);
  1837. snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_2 + reg_offset,
  1838. WM8994_FLL1_OUTDIV_MASK |
  1839. WM8994_FLL1_FRATIO_MASK, reg);
  1840. snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_3 + reg_offset,
  1841. WM8994_FLL1_K_MASK, fll.k);
  1842. snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_4 + reg_offset,
  1843. WM8994_FLL1_N_MASK,
  1844. fll.n << WM8994_FLL1_N_SHIFT);
  1845. if (fll.lambda) {
  1846. snd_soc_update_bits(codec, WM8958_FLL1_EFS_1 + reg_offset,
  1847. WM8958_FLL1_LAMBDA_MASK,
  1848. fll.lambda);
  1849. snd_soc_update_bits(codec, WM8958_FLL1_EFS_2 + reg_offset,
  1850. WM8958_FLL1_EFS_ENA, WM8958_FLL1_EFS_ENA);
  1851. } else {
  1852. snd_soc_update_bits(codec, WM8958_FLL1_EFS_2 + reg_offset,
  1853. WM8958_FLL1_EFS_ENA, 0);
  1854. }
  1855. snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_5 + reg_offset,
  1856. WM8994_FLL1_FRC_NCO | WM8958_FLL1_BYP |
  1857. WM8994_FLL1_REFCLK_DIV_MASK |
  1858. WM8994_FLL1_REFCLK_SRC_MASK,
  1859. ((src == WM8994_FLL_SRC_INTERNAL)
  1860. << WM8994_FLL1_FRC_NCO_SHIFT) |
  1861. (fll.clk_ref_div << WM8994_FLL1_REFCLK_DIV_SHIFT) |
  1862. (src - 1));
  1863. /* Clear any pending completion from a previous failure */
  1864. try_wait_for_completion(&wm8994->fll_locked[id]);
  1865. /* Enable (with fractional mode if required) */
  1866. if (freq_out) {
  1867. /* Enable VMID if we need it */
  1868. if (!was_enabled) {
  1869. active_reference(codec);
  1870. switch (control->type) {
  1871. case WM8994:
  1872. vmid_reference(codec);
  1873. break;
  1874. case WM8958:
  1875. if (control->revision < 1)
  1876. vmid_reference(codec);
  1877. break;
  1878. default:
  1879. break;
  1880. }
  1881. }
  1882. reg = WM8994_FLL1_ENA;
  1883. if (fll.k)
  1884. reg |= WM8994_FLL1_FRAC;
  1885. if (src == WM8994_FLL_SRC_INTERNAL)
  1886. reg |= WM8994_FLL1_OSC_ENA;
  1887. snd_soc_update_bits(codec, WM8994_FLL1_CONTROL_1 + reg_offset,
  1888. WM8994_FLL1_ENA | WM8994_FLL1_OSC_ENA |
  1889. WM8994_FLL1_FRAC, reg);
  1890. if (wm8994->fll_locked_irq) {
  1891. timeout = wait_for_completion_timeout(&wm8994->fll_locked[id],
  1892. msecs_to_jiffies(10));
  1893. if (timeout == 0)
  1894. dev_warn(codec->dev,
  1895. "Timed out waiting for FLL lock\n");
  1896. } else {
  1897. msleep(5);
  1898. }
  1899. } else {
  1900. if (was_enabled) {
  1901. switch (control->type) {
  1902. case WM8994:
  1903. vmid_dereference(codec);
  1904. break;
  1905. case WM8958:
  1906. if (control->revision < 1)
  1907. vmid_dereference(codec);
  1908. break;
  1909. default:
  1910. break;
  1911. }
  1912. active_dereference(codec);
  1913. }
  1914. }
  1915. out:
  1916. wm8994->fll[id].in = freq_in;
  1917. wm8994->fll[id].out = freq_out;
  1918. wm8994->fll[id].src = src;
  1919. configure_clock(codec);
  1920. /*
  1921. * If SYSCLK will be less than 50kHz adjust AIFnCLK dividers
  1922. * for detection.
  1923. */
  1924. if (max(wm8994->aifclk[0], wm8994->aifclk[1]) < 50000) {
  1925. dev_dbg(codec->dev, "Configuring AIFs for 128fs\n");
  1926. wm8994->aifdiv[0] = snd_soc_read(codec, WM8994_AIF1_RATE)
  1927. & WM8994_AIF1CLK_RATE_MASK;
  1928. wm8994->aifdiv[1] = snd_soc_read(codec, WM8994_AIF2_RATE)
  1929. & WM8994_AIF1CLK_RATE_MASK;
  1930. snd_soc_update_bits(codec, WM8994_AIF1_RATE,
  1931. WM8994_AIF1CLK_RATE_MASK, 0x1);
  1932. snd_soc_update_bits(codec, WM8994_AIF2_RATE,
  1933. WM8994_AIF2CLK_RATE_MASK, 0x1);
  1934. } else if (wm8994->aifdiv[0]) {
  1935. snd_soc_update_bits(codec, WM8994_AIF1_RATE,
  1936. WM8994_AIF1CLK_RATE_MASK,
  1937. wm8994->aifdiv[0]);
  1938. snd_soc_update_bits(codec, WM8994_AIF2_RATE,
  1939. WM8994_AIF2CLK_RATE_MASK,
  1940. wm8994->aifdiv[1]);
  1941. wm8994->aifdiv[0] = 0;
  1942. wm8994->aifdiv[1] = 0;
  1943. }
  1944. return 0;
  1945. }
  1946. static irqreturn_t wm8994_fll_locked_irq(int irq, void *data)
  1947. {
  1948. struct completion *completion = data;
  1949. complete(completion);
  1950. return IRQ_HANDLED;
  1951. }
  1952. static int opclk_divs[] = { 10, 20, 30, 40, 55, 60, 80, 120, 160 };
  1953. static int wm8994_set_fll(struct snd_soc_dai *dai, int id, int src,
  1954. unsigned int freq_in, unsigned int freq_out)
  1955. {
  1956. return _wm8994_set_fll(dai->codec, id, src, freq_in, freq_out);
  1957. }
  1958. static int wm8994_set_dai_sysclk(struct snd_soc_dai *dai,
  1959. int clk_id, unsigned int freq, int dir)
  1960. {
  1961. struct snd_soc_codec *codec = dai->codec;
  1962. struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
  1963. int i;
  1964. switch (dai->id) {
  1965. case 1:
  1966. case 2:
  1967. break;
  1968. default:
  1969. /* AIF3 shares clocking with AIF1/2 */
  1970. return -EINVAL;
  1971. }
  1972. switch (clk_id) {
  1973. case WM8994_SYSCLK_MCLK1:
  1974. wm8994->sysclk[dai->id - 1] = WM8994_SYSCLK_MCLK1;
  1975. wm8994->mclk[0] = freq;
  1976. dev_dbg(dai->dev, "AIF%d using MCLK1 at %uHz\n",
  1977. dai->id, freq);
  1978. break;
  1979. case WM8994_SYSCLK_MCLK2:
  1980. /* TODO: Set GPIO AF */
  1981. wm8994->sysclk[dai->id - 1] = WM8994_SYSCLK_MCLK2;
  1982. wm8994->mclk[1] = freq;
  1983. dev_dbg(dai->dev, "AIF%d using MCLK2 at %uHz\n",
  1984. dai->id, freq);
  1985. break;
  1986. case WM8994_SYSCLK_FLL1:
  1987. wm8994->sysclk[dai->id - 1] = WM8994_SYSCLK_FLL1;
  1988. dev_dbg(dai->dev, "AIF%d using FLL1\n", dai->id);
  1989. break;
  1990. case WM8994_SYSCLK_FLL2:
  1991. wm8994->sysclk[dai->id - 1] = WM8994_SYSCLK_FLL2;
  1992. dev_dbg(dai->dev, "AIF%d using FLL2\n", dai->id);
  1993. break;
  1994. case WM8994_SYSCLK_OPCLK:
  1995. /* Special case - a division (times 10) is given and
  1996. * no effect on main clocking.
  1997. */
  1998. if (freq) {
  1999. for (i = 0; i < ARRAY_SIZE(opclk_divs); i++)
  2000. if (opclk_divs[i] == freq)
  2001. break;
  2002. if (i == ARRAY_SIZE(opclk_divs))
  2003. return -EINVAL;
  2004. snd_soc_update_bits(codec, WM8994_CLOCKING_2,
  2005. WM8994_OPCLK_DIV_MASK, i);
  2006. snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_2,
  2007. WM8994_OPCLK_ENA, WM8994_OPCLK_ENA);
  2008. } else {
  2009. snd_soc_update_bits(codec, WM8994_POWER_MANAGEMENT_2,
  2010. WM8994_OPCLK_ENA, 0);
  2011. }
  2012. default:
  2013. return -EINVAL;
  2014. }
  2015. configure_clock(codec);
  2016. /*
  2017. * If SYSCLK will be less than 50kHz adjust AIFnCLK dividers
  2018. * for detection.
  2019. */
  2020. if (max(wm8994->aifclk[0], wm8994->aifclk[1]) < 50000) {
  2021. dev_dbg(codec->dev, "Configuring AIFs for 128fs\n");
  2022. wm8994->aifdiv[0] = snd_soc_read(codec, WM8994_AIF1_RATE)
  2023. & WM8994_AIF1CLK_RATE_MASK;
  2024. wm8994->aifdiv[1] = snd_soc_read(codec, WM8994_AIF2_RATE)
  2025. & WM8994_AIF1CLK_RATE_MASK;
  2026. snd_soc_update_bits(codec, WM8994_AIF1_RATE,
  2027. WM8994_AIF1CLK_RATE_MASK, 0x1);
  2028. snd_soc_update_bits(codec, WM8994_AIF2_RATE,
  2029. WM8994_AIF2CLK_RATE_MASK, 0x1);
  2030. } else if (wm8994->aifdiv[0]) {
  2031. snd_soc_update_bits(codec, WM8994_AIF1_RATE,
  2032. WM8994_AIF1CLK_RATE_MASK,
  2033. wm8994->aifdiv[0]);
  2034. snd_soc_update_bits(codec, WM8994_AIF2_RATE,
  2035. WM8994_AIF2CLK_RATE_MASK,
  2036. wm8994->aifdiv[1]);
  2037. wm8994->aifdiv[0] = 0;
  2038. wm8994->aifdiv[1] = 0;
  2039. }
  2040. return 0;
  2041. }
  2042. static int wm8994_set_bias_level(struct snd_soc_codec *codec,
  2043. enum snd_soc_bias_level level)
  2044. {
  2045. struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
  2046. struct wm8994 *control = wm8994->wm8994;
  2047. wm_hubs_set_bias_level(codec, level);
  2048. switch (level) {
  2049. case SND_SOC_BIAS_ON:
  2050. break;
  2051. case SND_SOC_BIAS_PREPARE:
  2052. /* MICBIAS into regulating mode */
  2053. switch (control->type) {
  2054. case WM8958:
  2055. case WM1811:
  2056. snd_soc_update_bits(codec, WM8958_MICBIAS1,
  2057. WM8958_MICB1_MODE, 0);
  2058. snd_soc_update_bits(codec, WM8958_MICBIAS2,
  2059. WM8958_MICB2_MODE, 0);
  2060. break;
  2061. default:
  2062. break;
  2063. }
  2064. if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_STANDBY)
  2065. active_reference(codec);
  2066. break;
  2067. case SND_SOC_BIAS_STANDBY:
  2068. if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_OFF) {
  2069. switch (control->type) {
  2070. case WM8958:
  2071. if (control->revision == 0) {
  2072. /* Optimise performance for rev A */
  2073. snd_soc_update_bits(codec,
  2074. WM8958_CHARGE_PUMP_2,
  2075. WM8958_CP_DISCH,
  2076. WM8958_CP_DISCH);
  2077. }
  2078. break;
  2079. default:
  2080. break;
  2081. }
  2082. /* Discharge LINEOUT1 & 2 */
  2083. snd_soc_update_bits(codec, WM8994_ANTIPOP_1,
  2084. WM8994_LINEOUT1_DISCH |
  2085. WM8994_LINEOUT2_DISCH,
  2086. WM8994_LINEOUT1_DISCH |
  2087. WM8994_LINEOUT2_DISCH);
  2088. }
  2089. if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_PREPARE)
  2090. active_dereference(codec);
  2091. /* MICBIAS into bypass mode on newer devices */
  2092. switch (control->type) {
  2093. case WM8958:
  2094. case WM1811:
  2095. snd_soc_update_bits(codec, WM8958_MICBIAS1,
  2096. WM8958_MICB1_MODE,
  2097. WM8958_MICB1_MODE);
  2098. snd_soc_update_bits(codec, WM8958_MICBIAS2,
  2099. WM8958_MICB2_MODE,
  2100. WM8958_MICB2_MODE);
  2101. break;
  2102. default:
  2103. break;
  2104. }
  2105. break;
  2106. case SND_SOC_BIAS_OFF:
  2107. if (snd_soc_codec_get_bias_level(codec) == SND_SOC_BIAS_STANDBY)
  2108. wm8994->cur_fw = NULL;
  2109. break;
  2110. }
  2111. return 0;
  2112. }
  2113. int wm8994_vmid_mode(struct snd_soc_codec *codec, enum wm8994_vmid_mode mode)
  2114. {
  2115. struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
  2116. struct snd_soc_dapm_context *dapm = snd_soc_codec_get_dapm(codec);
  2117. switch (mode) {
  2118. case WM8994_VMID_NORMAL:
  2119. snd_soc_dapm_mutex_lock(dapm);
  2120. if (wm8994->hubs.lineout1_se) {
  2121. snd_soc_dapm_disable_pin_unlocked(dapm,
  2122. "LINEOUT1N Driver");
  2123. snd_soc_dapm_disable_pin_unlocked(dapm,
  2124. "LINEOUT1P Driver");
  2125. }
  2126. if (wm8994->hubs.lineout2_se) {
  2127. snd_soc_dapm_disable_pin_unlocked(dapm,
  2128. "LINEOUT2N Driver");
  2129. snd_soc_dapm_disable_pin_unlocked(dapm,
  2130. "LINEOUT2P Driver");
  2131. }
  2132. /* Do the sync with the old mode to allow it to clean up */
  2133. snd_soc_dapm_sync_unlocked(dapm);
  2134. wm8994->vmid_mode = mode;
  2135. snd_soc_dapm_mutex_unlock(dapm);
  2136. break;
  2137. case WM8994_VMID_FORCE:
  2138. snd_soc_dapm_mutex_lock(dapm);
  2139. if (wm8994->hubs.lineout1_se) {
  2140. snd_soc_dapm_force_enable_pin_unlocked(dapm,
  2141. "LINEOUT1N Driver");
  2142. snd_soc_dapm_force_enable_pin_unlocked(dapm,
  2143. "LINEOUT1P Driver");
  2144. }
  2145. if (wm8994->hubs.lineout2_se) {
  2146. snd_soc_dapm_force_enable_pin_unlocked(dapm,
  2147. "LINEOUT2N Driver");
  2148. snd_soc_dapm_force_enable_pin_unlocked(dapm,
  2149. "LINEOUT2P Driver");
  2150. }
  2151. wm8994->vmid_mode = mode;
  2152. snd_soc_dapm_sync_unlocked(dapm);
  2153. snd_soc_dapm_mutex_unlock(dapm);
  2154. break;
  2155. default:
  2156. return -EINVAL;
  2157. }
  2158. return 0;
  2159. }
  2160. static int wm8994_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  2161. {
  2162. struct snd_soc_codec *codec = dai->codec;
  2163. struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
  2164. struct wm8994 *control = wm8994->wm8994;
  2165. int ms_reg;
  2166. int aif1_reg;
  2167. int dac_reg;
  2168. int adc_reg;
  2169. int ms = 0;
  2170. int aif1 = 0;
  2171. int lrclk = 0;
  2172. switch (dai->id) {
  2173. case 1:
  2174. ms_reg = WM8994_AIF1_MASTER_SLAVE;
  2175. aif1_reg = WM8994_AIF1_CONTROL_1;
  2176. dac_reg = WM8994_AIF1DAC_LRCLK;
  2177. adc_reg = WM8994_AIF1ADC_LRCLK;
  2178. break;
  2179. case 2:
  2180. ms_reg = WM8994_AIF2_MASTER_SLAVE;
  2181. aif1_reg = WM8994_AIF2_CONTROL_1;
  2182. dac_reg = WM8994_AIF1DAC_LRCLK;
  2183. adc_reg = WM8994_AIF1ADC_LRCLK;
  2184. break;
  2185. default:
  2186. return -EINVAL;
  2187. }
  2188. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  2189. case SND_SOC_DAIFMT_CBS_CFS:
  2190. break;
  2191. case SND_SOC_DAIFMT_CBM_CFM:
  2192. ms = WM8994_AIF1_MSTR;
  2193. break;
  2194. default:
  2195. return -EINVAL;
  2196. }
  2197. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  2198. case SND_SOC_DAIFMT_DSP_B:
  2199. aif1 |= WM8994_AIF1_LRCLK_INV;
  2200. lrclk |= WM8958_AIF1_LRCLK_INV;
  2201. case SND_SOC_DAIFMT_DSP_A:
  2202. aif1 |= 0x18;
  2203. break;
  2204. case SND_SOC_DAIFMT_I2S:
  2205. aif1 |= 0x10;
  2206. break;
  2207. case SND_SOC_DAIFMT_RIGHT_J:
  2208. break;
  2209. case SND_SOC_DAIFMT_LEFT_J:
  2210. aif1 |= 0x8;
  2211. break;
  2212. default:
  2213. return -EINVAL;
  2214. }
  2215. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  2216. case SND_SOC_DAIFMT_DSP_A:
  2217. case SND_SOC_DAIFMT_DSP_B:
  2218. /* frame inversion not valid for DSP modes */
  2219. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  2220. case SND_SOC_DAIFMT_NB_NF:
  2221. break;
  2222. case SND_SOC_DAIFMT_IB_NF:
  2223. aif1 |= WM8994_AIF1_BCLK_INV;
  2224. break;
  2225. default:
  2226. return -EINVAL;
  2227. }
  2228. break;
  2229. case SND_SOC_DAIFMT_I2S:
  2230. case SND_SOC_DAIFMT_RIGHT_J:
  2231. case SND_SOC_DAIFMT_LEFT_J:
  2232. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  2233. case SND_SOC_DAIFMT_NB_NF:
  2234. break;
  2235. case SND_SOC_DAIFMT_IB_IF:
  2236. aif1 |= WM8994_AIF1_BCLK_INV | WM8994_AIF1_LRCLK_INV;
  2237. lrclk |= WM8958_AIF1_LRCLK_INV;
  2238. break;
  2239. case SND_SOC_DAIFMT_IB_NF:
  2240. aif1 |= WM8994_AIF1_BCLK_INV;
  2241. break;
  2242. case SND_SOC_DAIFMT_NB_IF:
  2243. aif1 |= WM8994_AIF1_LRCLK_INV;
  2244. lrclk |= WM8958_AIF1_LRCLK_INV;
  2245. break;
  2246. default:
  2247. return -EINVAL;
  2248. }
  2249. break;
  2250. default:
  2251. return -EINVAL;
  2252. }
  2253. /* The AIF2 format configuration needs to be mirrored to AIF3
  2254. * on WM8958 if it's in use so just do it all the time. */
  2255. switch (control->type) {
  2256. case WM1811:
  2257. case WM8958:
  2258. if (dai->id == 2)
  2259. snd_soc_update_bits(codec, WM8958_AIF3_CONTROL_1,
  2260. WM8994_AIF1_LRCLK_INV |
  2261. WM8958_AIF3_FMT_MASK, aif1);
  2262. break;
  2263. default:
  2264. break;
  2265. }
  2266. snd_soc_update_bits(codec, aif1_reg,
  2267. WM8994_AIF1_BCLK_INV | WM8994_AIF1_LRCLK_INV |
  2268. WM8994_AIF1_FMT_MASK,
  2269. aif1);
  2270. snd_soc_update_bits(codec, ms_reg, WM8994_AIF1_MSTR,
  2271. ms);
  2272. snd_soc_update_bits(codec, dac_reg,
  2273. WM8958_AIF1_LRCLK_INV, lrclk);
  2274. snd_soc_update_bits(codec, adc_reg,
  2275. WM8958_AIF1_LRCLK_INV, lrclk);
  2276. return 0;
  2277. }
  2278. static struct {
  2279. int val, rate;
  2280. } srs[] = {
  2281. { 0, 8000 },
  2282. { 1, 11025 },
  2283. { 2, 12000 },
  2284. { 3, 16000 },
  2285. { 4, 22050 },
  2286. { 5, 24000 },
  2287. { 6, 32000 },
  2288. { 7, 44100 },
  2289. { 8, 48000 },
  2290. { 9, 88200 },
  2291. { 10, 96000 },
  2292. };
  2293. static int fs_ratios[] = {
  2294. 64, 128, 192, 256, 384, 512, 768, 1024, 1408, 1536
  2295. };
  2296. static int bclk_divs[] = {
  2297. 10, 15, 20, 30, 40, 50, 60, 80, 110, 120, 160, 220, 240, 320, 440, 480,
  2298. 640, 880, 960, 1280, 1760, 1920
  2299. };
  2300. static int wm8994_hw_params(struct snd_pcm_substream *substream,
  2301. struct snd_pcm_hw_params *params,
  2302. struct snd_soc_dai *dai)
  2303. {
  2304. struct snd_soc_codec *codec = dai->codec;
  2305. struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
  2306. struct wm8994 *control = wm8994->wm8994;
  2307. struct wm8994_pdata *pdata = &control->pdata;
  2308. int aif1_reg;
  2309. int aif2_reg;
  2310. int bclk_reg;
  2311. int lrclk_reg;
  2312. int rate_reg;
  2313. int aif1 = 0;
  2314. int aif2 = 0;
  2315. int bclk = 0;
  2316. int lrclk = 0;
  2317. int rate_val = 0;
  2318. int id = dai->id - 1;
  2319. int i, cur_val, best_val, bclk_rate, best;
  2320. switch (dai->id) {
  2321. case 1:
  2322. aif1_reg = WM8994_AIF1_CONTROL_1;
  2323. aif2_reg = WM8994_AIF1_CONTROL_2;
  2324. bclk_reg = WM8994_AIF1_BCLK;
  2325. rate_reg = WM8994_AIF1_RATE;
  2326. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK ||
  2327. wm8994->lrclk_shared[0]) {
  2328. lrclk_reg = WM8994_AIF1DAC_LRCLK;
  2329. } else {
  2330. lrclk_reg = WM8994_AIF1ADC_LRCLK;
  2331. dev_dbg(codec->dev, "AIF1 using split LRCLK\n");
  2332. }
  2333. break;
  2334. case 2:
  2335. aif1_reg = WM8994_AIF2_CONTROL_1;
  2336. aif2_reg = WM8994_AIF2_CONTROL_2;
  2337. bclk_reg = WM8994_AIF2_BCLK;
  2338. rate_reg = WM8994_AIF2_RATE;
  2339. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK ||
  2340. wm8994->lrclk_shared[1]) {
  2341. lrclk_reg = WM8994_AIF2DAC_LRCLK;
  2342. } else {
  2343. lrclk_reg = WM8994_AIF2ADC_LRCLK;
  2344. dev_dbg(codec->dev, "AIF2 using split LRCLK\n");
  2345. }
  2346. break;
  2347. default:
  2348. return -EINVAL;
  2349. }
  2350. bclk_rate = params_rate(params);
  2351. switch (params_width(params)) {
  2352. case 16:
  2353. bclk_rate *= 16;
  2354. break;
  2355. case 20:
  2356. bclk_rate *= 20;
  2357. aif1 |= 0x20;
  2358. break;
  2359. case 24:
  2360. bclk_rate *= 24;
  2361. aif1 |= 0x40;
  2362. break;
  2363. case 32:
  2364. bclk_rate *= 32;
  2365. aif1 |= 0x60;
  2366. break;
  2367. default:
  2368. return -EINVAL;
  2369. }
  2370. wm8994->channels[id] = params_channels(params);
  2371. if (pdata->max_channels_clocked[id] &&
  2372. wm8994->channels[id] > pdata->max_channels_clocked[id]) {
  2373. dev_dbg(dai->dev, "Constraining channels to %d from %d\n",
  2374. pdata->max_channels_clocked[id], wm8994->channels[id]);
  2375. wm8994->channels[id] = pdata->max_channels_clocked[id];
  2376. }
  2377. switch (wm8994->channels[id]) {
  2378. case 1:
  2379. case 2:
  2380. bclk_rate *= 2;
  2381. break;
  2382. default:
  2383. bclk_rate *= 4;
  2384. break;
  2385. }
  2386. /* Try to find an appropriate sample rate; look for an exact match. */
  2387. for (i = 0; i < ARRAY_SIZE(srs); i++)
  2388. if (srs[i].rate == params_rate(params))
  2389. break;
  2390. if (i == ARRAY_SIZE(srs))
  2391. return -EINVAL;
  2392. rate_val |= srs[i].val << WM8994_AIF1_SR_SHIFT;
  2393. dev_dbg(dai->dev, "Sample rate is %dHz\n", srs[i].rate);
  2394. dev_dbg(dai->dev, "AIF%dCLK is %dHz, target BCLK %dHz\n",
  2395. dai->id, wm8994->aifclk[id], bclk_rate);
  2396. if (wm8994->channels[id] == 1 &&
  2397. (snd_soc_read(codec, aif1_reg) & 0x18) == 0x18)
  2398. aif2 |= WM8994_AIF1_MONO;
  2399. if (wm8994->aifclk[id] == 0) {
  2400. dev_err(dai->dev, "AIF%dCLK not configured\n", dai->id);
  2401. return -EINVAL;
  2402. }
  2403. /* AIFCLK/fs ratio; look for a close match in either direction */
  2404. best = 0;
  2405. best_val = abs((fs_ratios[0] * params_rate(params))
  2406. - wm8994->aifclk[id]);
  2407. for (i = 1; i < ARRAY_SIZE(fs_ratios); i++) {
  2408. cur_val = abs((fs_ratios[i] * params_rate(params))
  2409. - wm8994->aifclk[id]);
  2410. if (cur_val >= best_val)
  2411. continue;
  2412. best = i;
  2413. best_val = cur_val;
  2414. }
  2415. dev_dbg(dai->dev, "Selected AIF%dCLK/fs = %d\n",
  2416. dai->id, fs_ratios[best]);
  2417. rate_val |= best;
  2418. /* We may not get quite the right frequency if using
  2419. * approximate clocks so look for the closest match that is
  2420. * higher than the target (we need to ensure that there enough
  2421. * BCLKs to clock out the samples).
  2422. */
  2423. best = 0;
  2424. for (i = 0; i < ARRAY_SIZE(bclk_divs); i++) {
  2425. cur_val = (wm8994->aifclk[id] * 10 / bclk_divs[i]) - bclk_rate;
  2426. if (cur_val < 0) /* BCLK table is sorted */
  2427. break;
  2428. best = i;
  2429. }
  2430. bclk_rate = wm8994->aifclk[id] * 10 / bclk_divs[best];
  2431. dev_dbg(dai->dev, "Using BCLK_DIV %d for actual BCLK %dHz\n",
  2432. bclk_divs[best], bclk_rate);
  2433. bclk |= best << WM8994_AIF1_BCLK_DIV_SHIFT;
  2434. lrclk = bclk_rate / params_rate(params);
  2435. if (!lrclk) {
  2436. dev_err(dai->dev, "Unable to generate LRCLK from %dHz BCLK\n",
  2437. bclk_rate);
  2438. return -EINVAL;
  2439. }
  2440. dev_dbg(dai->dev, "Using LRCLK rate %d for actual LRCLK %dHz\n",
  2441. lrclk, bclk_rate / lrclk);
  2442. snd_soc_update_bits(codec, aif1_reg, WM8994_AIF1_WL_MASK, aif1);
  2443. snd_soc_update_bits(codec, aif2_reg, WM8994_AIF1_MONO, aif2);
  2444. snd_soc_update_bits(codec, bclk_reg, WM8994_AIF1_BCLK_DIV_MASK, bclk);
  2445. snd_soc_update_bits(codec, lrclk_reg, WM8994_AIF1DAC_RATE_MASK,
  2446. lrclk);
  2447. snd_soc_update_bits(codec, rate_reg, WM8994_AIF1_SR_MASK |
  2448. WM8994_AIF1CLK_RATE_MASK, rate_val);
  2449. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  2450. switch (dai->id) {
  2451. case 1:
  2452. wm8994->dac_rates[0] = params_rate(params);
  2453. wm8994_set_retune_mobile(codec, 0);
  2454. wm8994_set_retune_mobile(codec, 1);
  2455. break;
  2456. case 2:
  2457. wm8994->dac_rates[1] = params_rate(params);
  2458. wm8994_set_retune_mobile(codec, 2);
  2459. break;
  2460. }
  2461. }
  2462. return 0;
  2463. }
  2464. static int wm8994_aif3_hw_params(struct snd_pcm_substream *substream,
  2465. struct snd_pcm_hw_params *params,
  2466. struct snd_soc_dai *dai)
  2467. {
  2468. struct snd_soc_codec *codec = dai->codec;
  2469. struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
  2470. struct wm8994 *control = wm8994->wm8994;
  2471. int aif1_reg;
  2472. int aif1 = 0;
  2473. switch (dai->id) {
  2474. case 3:
  2475. switch (control->type) {
  2476. case WM1811:
  2477. case WM8958:
  2478. aif1_reg = WM8958_AIF3_CONTROL_1;
  2479. break;
  2480. default:
  2481. return 0;
  2482. }
  2483. break;
  2484. default:
  2485. return 0;
  2486. }
  2487. switch (params_width(params)) {
  2488. case 16:
  2489. break;
  2490. case 20:
  2491. aif1 |= 0x20;
  2492. break;
  2493. case 24:
  2494. aif1 |= 0x40;
  2495. break;
  2496. case 32:
  2497. aif1 |= 0x60;
  2498. break;
  2499. default:
  2500. return -EINVAL;
  2501. }
  2502. return snd_soc_update_bits(codec, aif1_reg, WM8994_AIF1_WL_MASK, aif1);
  2503. }
  2504. static int wm8994_aif_mute(struct snd_soc_dai *codec_dai, int mute)
  2505. {
  2506. struct snd_soc_codec *codec = codec_dai->codec;
  2507. int mute_reg;
  2508. int reg;
  2509. switch (codec_dai->id) {
  2510. case 1:
  2511. mute_reg = WM8994_AIF1_DAC1_FILTERS_1;
  2512. break;
  2513. case 2:
  2514. mute_reg = WM8994_AIF2_DAC_FILTERS_1;
  2515. break;
  2516. default:
  2517. return -EINVAL;
  2518. }
  2519. if (mute)
  2520. reg = WM8994_AIF1DAC1_MUTE;
  2521. else
  2522. reg = 0;
  2523. snd_soc_update_bits(codec, mute_reg, WM8994_AIF1DAC1_MUTE, reg);
  2524. return 0;
  2525. }
  2526. static int wm8994_set_tristate(struct snd_soc_dai *codec_dai, int tristate)
  2527. {
  2528. struct snd_soc_codec *codec = codec_dai->codec;
  2529. int reg, val, mask;
  2530. switch (codec_dai->id) {
  2531. case 1:
  2532. reg = WM8994_AIF1_MASTER_SLAVE;
  2533. mask = WM8994_AIF1_TRI;
  2534. break;
  2535. case 2:
  2536. reg = WM8994_AIF2_MASTER_SLAVE;
  2537. mask = WM8994_AIF2_TRI;
  2538. break;
  2539. default:
  2540. return -EINVAL;
  2541. }
  2542. if (tristate)
  2543. val = mask;
  2544. else
  2545. val = 0;
  2546. return snd_soc_update_bits(codec, reg, mask, val);
  2547. }
  2548. static int wm8994_aif2_probe(struct snd_soc_dai *dai)
  2549. {
  2550. struct snd_soc_codec *codec = dai->codec;
  2551. /* Disable the pulls on the AIF if we're using it to save power. */
  2552. snd_soc_update_bits(codec, WM8994_GPIO_3,
  2553. WM8994_GPN_PU | WM8994_GPN_PD, 0);
  2554. snd_soc_update_bits(codec, WM8994_GPIO_4,
  2555. WM8994_GPN_PU | WM8994_GPN_PD, 0);
  2556. snd_soc_update_bits(codec, WM8994_GPIO_5,
  2557. WM8994_GPN_PU | WM8994_GPN_PD, 0);
  2558. return 0;
  2559. }
  2560. #define WM8994_RATES SNDRV_PCM_RATE_8000_96000
  2561. #define WM8994_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
  2562. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
  2563. static const struct snd_soc_dai_ops wm8994_aif1_dai_ops = {
  2564. .set_sysclk = wm8994_set_dai_sysclk,
  2565. .set_fmt = wm8994_set_dai_fmt,
  2566. .hw_params = wm8994_hw_params,
  2567. .digital_mute = wm8994_aif_mute,
  2568. .set_pll = wm8994_set_fll,
  2569. .set_tristate = wm8994_set_tristate,
  2570. };
  2571. static const struct snd_soc_dai_ops wm8994_aif2_dai_ops = {
  2572. .set_sysclk = wm8994_set_dai_sysclk,
  2573. .set_fmt = wm8994_set_dai_fmt,
  2574. .hw_params = wm8994_hw_params,
  2575. .digital_mute = wm8994_aif_mute,
  2576. .set_pll = wm8994_set_fll,
  2577. .set_tristate = wm8994_set_tristate,
  2578. };
  2579. static const struct snd_soc_dai_ops wm8994_aif3_dai_ops = {
  2580. .hw_params = wm8994_aif3_hw_params,
  2581. };
  2582. static struct snd_soc_dai_driver wm8994_dai[] = {
  2583. {
  2584. .name = "wm8994-aif1",
  2585. .id = 1,
  2586. .playback = {
  2587. .stream_name = "AIF1 Playback",
  2588. .channels_min = 1,
  2589. .channels_max = 2,
  2590. .rates = WM8994_RATES,
  2591. .formats = WM8994_FORMATS,
  2592. .sig_bits = 24,
  2593. },
  2594. .capture = {
  2595. .stream_name = "AIF1 Capture",
  2596. .channels_min = 1,
  2597. .channels_max = 2,
  2598. .rates = WM8994_RATES,
  2599. .formats = WM8994_FORMATS,
  2600. .sig_bits = 24,
  2601. },
  2602. .ops = &wm8994_aif1_dai_ops,
  2603. },
  2604. {
  2605. .name = "wm8994-aif2",
  2606. .id = 2,
  2607. .playback = {
  2608. .stream_name = "AIF2 Playback",
  2609. .channels_min = 1,
  2610. .channels_max = 2,
  2611. .rates = WM8994_RATES,
  2612. .formats = WM8994_FORMATS,
  2613. .sig_bits = 24,
  2614. },
  2615. .capture = {
  2616. .stream_name = "AIF2 Capture",
  2617. .channels_min = 1,
  2618. .channels_max = 2,
  2619. .rates = WM8994_RATES,
  2620. .formats = WM8994_FORMATS,
  2621. .sig_bits = 24,
  2622. },
  2623. .probe = wm8994_aif2_probe,
  2624. .ops = &wm8994_aif2_dai_ops,
  2625. },
  2626. {
  2627. .name = "wm8994-aif3",
  2628. .id = 3,
  2629. .playback = {
  2630. .stream_name = "AIF3 Playback",
  2631. .channels_min = 1,
  2632. .channels_max = 2,
  2633. .rates = WM8994_RATES,
  2634. .formats = WM8994_FORMATS,
  2635. .sig_bits = 24,
  2636. },
  2637. .capture = {
  2638. .stream_name = "AIF3 Capture",
  2639. .channels_min = 1,
  2640. .channels_max = 2,
  2641. .rates = WM8994_RATES,
  2642. .formats = WM8994_FORMATS,
  2643. .sig_bits = 24,
  2644. },
  2645. .ops = &wm8994_aif3_dai_ops,
  2646. }
  2647. };
  2648. #ifdef CONFIG_PM
  2649. static int wm8994_codec_suspend(struct snd_soc_codec *codec)
  2650. {
  2651. struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
  2652. int i, ret;
  2653. for (i = 0; i < ARRAY_SIZE(wm8994->fll); i++) {
  2654. memcpy(&wm8994->fll_suspend[i], &wm8994->fll[i],
  2655. sizeof(struct wm8994_fll_config));
  2656. ret = _wm8994_set_fll(codec, i + 1, 0, 0, 0);
  2657. if (ret < 0)
  2658. dev_warn(codec->dev, "Failed to stop FLL%d: %d\n",
  2659. i + 1, ret);
  2660. }
  2661. snd_soc_codec_force_bias_level(codec, SND_SOC_BIAS_OFF);
  2662. return 0;
  2663. }
  2664. static int wm8994_codec_resume(struct snd_soc_codec *codec)
  2665. {
  2666. struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
  2667. int i, ret;
  2668. for (i = 0; i < ARRAY_SIZE(wm8994->fll); i++) {
  2669. if (!wm8994->fll_suspend[i].out)
  2670. continue;
  2671. ret = _wm8994_set_fll(codec, i + 1,
  2672. wm8994->fll_suspend[i].src,
  2673. wm8994->fll_suspend[i].in,
  2674. wm8994->fll_suspend[i].out);
  2675. if (ret < 0)
  2676. dev_warn(codec->dev, "Failed to restore FLL%d: %d\n",
  2677. i + 1, ret);
  2678. }
  2679. return 0;
  2680. }
  2681. #else
  2682. #define wm8994_codec_suspend NULL
  2683. #define wm8994_codec_resume NULL
  2684. #endif
  2685. static void wm8994_handle_retune_mobile_pdata(struct wm8994_priv *wm8994)
  2686. {
  2687. struct snd_soc_codec *codec = wm8994->hubs.codec;
  2688. struct wm8994 *control = wm8994->wm8994;
  2689. struct wm8994_pdata *pdata = &control->pdata;
  2690. struct snd_kcontrol_new controls[] = {
  2691. SOC_ENUM_EXT("AIF1.1 EQ Mode",
  2692. wm8994->retune_mobile_enum,
  2693. wm8994_get_retune_mobile_enum,
  2694. wm8994_put_retune_mobile_enum),
  2695. SOC_ENUM_EXT("AIF1.2 EQ Mode",
  2696. wm8994->retune_mobile_enum,
  2697. wm8994_get_retune_mobile_enum,
  2698. wm8994_put_retune_mobile_enum),
  2699. SOC_ENUM_EXT("AIF2 EQ Mode",
  2700. wm8994->retune_mobile_enum,
  2701. wm8994_get_retune_mobile_enum,
  2702. wm8994_put_retune_mobile_enum),
  2703. };
  2704. int ret, i, j;
  2705. const char **t;
  2706. /* We need an array of texts for the enum API but the number
  2707. * of texts is likely to be less than the number of
  2708. * configurations due to the sample rate dependency of the
  2709. * configurations. */
  2710. wm8994->num_retune_mobile_texts = 0;
  2711. wm8994->retune_mobile_texts = NULL;
  2712. for (i = 0; i < pdata->num_retune_mobile_cfgs; i++) {
  2713. for (j = 0; j < wm8994->num_retune_mobile_texts; j++) {
  2714. if (strcmp(pdata->retune_mobile_cfgs[i].name,
  2715. wm8994->retune_mobile_texts[j]) == 0)
  2716. break;
  2717. }
  2718. if (j != wm8994->num_retune_mobile_texts)
  2719. continue;
  2720. /* Expand the array... */
  2721. t = krealloc(wm8994->retune_mobile_texts,
  2722. sizeof(char *) *
  2723. (wm8994->num_retune_mobile_texts + 1),
  2724. GFP_KERNEL);
  2725. if (t == NULL)
  2726. continue;
  2727. /* ...store the new entry... */
  2728. t[wm8994->num_retune_mobile_texts] =
  2729. pdata->retune_mobile_cfgs[i].name;
  2730. /* ...and remember the new version. */
  2731. wm8994->num_retune_mobile_texts++;
  2732. wm8994->retune_mobile_texts = t;
  2733. }
  2734. dev_dbg(codec->dev, "Allocated %d unique ReTune Mobile names\n",
  2735. wm8994->num_retune_mobile_texts);
  2736. wm8994->retune_mobile_enum.items = wm8994->num_retune_mobile_texts;
  2737. wm8994->retune_mobile_enum.texts = wm8994->retune_mobile_texts;
  2738. ret = snd_soc_add_codec_controls(wm8994->hubs.codec, controls,
  2739. ARRAY_SIZE(controls));
  2740. if (ret != 0)
  2741. dev_err(wm8994->hubs.codec->dev,
  2742. "Failed to add ReTune Mobile controls: %d\n", ret);
  2743. }
  2744. static void wm8994_handle_pdata(struct wm8994_priv *wm8994)
  2745. {
  2746. struct snd_soc_codec *codec = wm8994->hubs.codec;
  2747. struct wm8994 *control = wm8994->wm8994;
  2748. struct wm8994_pdata *pdata = &control->pdata;
  2749. int ret, i;
  2750. if (!pdata)
  2751. return;
  2752. wm_hubs_handle_analogue_pdata(codec, pdata->lineout1_diff,
  2753. pdata->lineout2_diff,
  2754. pdata->lineout1fb,
  2755. pdata->lineout2fb,
  2756. pdata->jd_scthr,
  2757. pdata->jd_thr,
  2758. pdata->micb1_delay,
  2759. pdata->micb2_delay,
  2760. pdata->micbias1_lvl,
  2761. pdata->micbias2_lvl);
  2762. dev_dbg(codec->dev, "%d DRC configurations\n", pdata->num_drc_cfgs);
  2763. if (pdata->num_drc_cfgs) {
  2764. struct snd_kcontrol_new controls[] = {
  2765. SOC_ENUM_EXT("AIF1DRC1 Mode", wm8994->drc_enum,
  2766. wm8994_get_drc_enum, wm8994_put_drc_enum),
  2767. SOC_ENUM_EXT("AIF1DRC2 Mode", wm8994->drc_enum,
  2768. wm8994_get_drc_enum, wm8994_put_drc_enum),
  2769. SOC_ENUM_EXT("AIF2DRC Mode", wm8994->drc_enum,
  2770. wm8994_get_drc_enum, wm8994_put_drc_enum),
  2771. };
  2772. /* We need an array of texts for the enum API */
  2773. wm8994->drc_texts = devm_kzalloc(wm8994->hubs.codec->dev,
  2774. sizeof(char *) * pdata->num_drc_cfgs, GFP_KERNEL);
  2775. if (!wm8994->drc_texts)
  2776. return;
  2777. for (i = 0; i < pdata->num_drc_cfgs; i++)
  2778. wm8994->drc_texts[i] = pdata->drc_cfgs[i].name;
  2779. wm8994->drc_enum.items = pdata->num_drc_cfgs;
  2780. wm8994->drc_enum.texts = wm8994->drc_texts;
  2781. ret = snd_soc_add_codec_controls(wm8994->hubs.codec, controls,
  2782. ARRAY_SIZE(controls));
  2783. for (i = 0; i < WM8994_NUM_DRC; i++)
  2784. wm8994_set_drc(codec, i);
  2785. } else {
  2786. ret = snd_soc_add_codec_controls(wm8994->hubs.codec,
  2787. wm8994_drc_controls,
  2788. ARRAY_SIZE(wm8994_drc_controls));
  2789. }
  2790. if (ret != 0)
  2791. dev_err(wm8994->hubs.codec->dev,
  2792. "Failed to add DRC mode controls: %d\n", ret);
  2793. dev_dbg(codec->dev, "%d ReTune Mobile configurations\n",
  2794. pdata->num_retune_mobile_cfgs);
  2795. if (pdata->num_retune_mobile_cfgs)
  2796. wm8994_handle_retune_mobile_pdata(wm8994);
  2797. else
  2798. snd_soc_add_codec_controls(wm8994->hubs.codec, wm8994_eq_controls,
  2799. ARRAY_SIZE(wm8994_eq_controls));
  2800. for (i = 0; i < ARRAY_SIZE(pdata->micbias); i++) {
  2801. if (pdata->micbias[i]) {
  2802. snd_soc_write(codec, WM8958_MICBIAS1 + i,
  2803. pdata->micbias[i] & 0xffff);
  2804. }
  2805. }
  2806. }
  2807. /**
  2808. * wm8994_mic_detect - Enable microphone detection via the WM8994 IRQ
  2809. *
  2810. * @codec: WM8994 codec
  2811. * @jack: jack to report detection events on
  2812. * @micbias: microphone bias to detect on
  2813. *
  2814. * Enable microphone detection via IRQ on the WM8994. If GPIOs are
  2815. * being used to bring out signals to the processor then only platform
  2816. * data configuration is needed for WM8994 and processor GPIOs should
  2817. * be configured using snd_soc_jack_add_gpios() instead.
  2818. *
  2819. * Configuration of detection levels is available via the micbias1_lvl
  2820. * and micbias2_lvl platform data members.
  2821. */
  2822. int wm8994_mic_detect(struct snd_soc_codec *codec, struct snd_soc_jack *jack,
  2823. int micbias)
  2824. {
  2825. struct snd_soc_dapm_context *dapm = snd_soc_codec_get_dapm(codec);
  2826. struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
  2827. struct wm8994_micdet *micdet;
  2828. struct wm8994 *control = wm8994->wm8994;
  2829. int reg, ret;
  2830. if (control->type != WM8994) {
  2831. dev_warn(codec->dev, "Not a WM8994\n");
  2832. return -EINVAL;
  2833. }
  2834. switch (micbias) {
  2835. case 1:
  2836. micdet = &wm8994->micdet[0];
  2837. if (jack)
  2838. ret = snd_soc_dapm_force_enable_pin(dapm, "MICBIAS1");
  2839. else
  2840. ret = snd_soc_dapm_disable_pin(dapm, "MICBIAS1");
  2841. break;
  2842. case 2:
  2843. micdet = &wm8994->micdet[1];
  2844. if (jack)
  2845. ret = snd_soc_dapm_force_enable_pin(dapm, "MICBIAS1");
  2846. else
  2847. ret = snd_soc_dapm_disable_pin(dapm, "MICBIAS1");
  2848. break;
  2849. default:
  2850. dev_warn(codec->dev, "Invalid MICBIAS %d\n", micbias);
  2851. return -EINVAL;
  2852. }
  2853. if (ret != 0)
  2854. dev_warn(codec->dev, "Failed to configure MICBIAS%d: %d\n",
  2855. micbias, ret);
  2856. dev_dbg(codec->dev, "Configuring microphone detection on %d %p\n",
  2857. micbias, jack);
  2858. /* Store the configuration */
  2859. micdet->jack = jack;
  2860. micdet->detecting = true;
  2861. /* If either of the jacks is set up then enable detection */
  2862. if (wm8994->micdet[0].jack || wm8994->micdet[1].jack)
  2863. reg = WM8994_MICD_ENA;
  2864. else
  2865. reg = 0;
  2866. snd_soc_update_bits(codec, WM8994_MICBIAS, WM8994_MICD_ENA, reg);
  2867. /* enable MICDET and MICSHRT deboune */
  2868. snd_soc_update_bits(codec, WM8994_IRQ_DEBOUNCE,
  2869. WM8994_MIC1_DET_DB_MASK | WM8994_MIC1_SHRT_DB_MASK |
  2870. WM8994_MIC2_DET_DB_MASK | WM8994_MIC2_SHRT_DB_MASK,
  2871. WM8994_MIC1_DET_DB | WM8994_MIC1_SHRT_DB);
  2872. snd_soc_dapm_sync(dapm);
  2873. return 0;
  2874. }
  2875. EXPORT_SYMBOL_GPL(wm8994_mic_detect);
  2876. static void wm8994_mic_work(struct work_struct *work)
  2877. {
  2878. struct wm8994_priv *priv = container_of(work,
  2879. struct wm8994_priv,
  2880. mic_work.work);
  2881. struct regmap *regmap = priv->wm8994->regmap;
  2882. struct device *dev = priv->wm8994->dev;
  2883. unsigned int reg;
  2884. int ret;
  2885. int report;
  2886. pm_runtime_get_sync(dev);
  2887. ret = regmap_read(regmap, WM8994_INTERRUPT_RAW_STATUS_2, &reg);
  2888. if (ret < 0) {
  2889. dev_err(dev, "Failed to read microphone status: %d\n",
  2890. ret);
  2891. pm_runtime_put(dev);
  2892. return;
  2893. }
  2894. dev_dbg(dev, "Microphone status: %x\n", reg);
  2895. report = 0;
  2896. if (reg & WM8994_MIC1_DET_STS) {
  2897. if (priv->micdet[0].detecting)
  2898. report = SND_JACK_HEADSET;
  2899. }
  2900. if (reg & WM8994_MIC1_SHRT_STS) {
  2901. if (priv->micdet[0].detecting)
  2902. report = SND_JACK_HEADPHONE;
  2903. else
  2904. report |= SND_JACK_BTN_0;
  2905. }
  2906. if (report)
  2907. priv->micdet[0].detecting = false;
  2908. else
  2909. priv->micdet[0].detecting = true;
  2910. snd_soc_jack_report(priv->micdet[0].jack, report,
  2911. SND_JACK_HEADSET | SND_JACK_BTN_0);
  2912. report = 0;
  2913. if (reg & WM8994_MIC2_DET_STS) {
  2914. if (priv->micdet[1].detecting)
  2915. report = SND_JACK_HEADSET;
  2916. }
  2917. if (reg & WM8994_MIC2_SHRT_STS) {
  2918. if (priv->micdet[1].detecting)
  2919. report = SND_JACK_HEADPHONE;
  2920. else
  2921. report |= SND_JACK_BTN_0;
  2922. }
  2923. if (report)
  2924. priv->micdet[1].detecting = false;
  2925. else
  2926. priv->micdet[1].detecting = true;
  2927. snd_soc_jack_report(priv->micdet[1].jack, report,
  2928. SND_JACK_HEADSET | SND_JACK_BTN_0);
  2929. pm_runtime_put(dev);
  2930. }
  2931. static irqreturn_t wm8994_mic_irq(int irq, void *data)
  2932. {
  2933. struct wm8994_priv *priv = data;
  2934. struct snd_soc_codec *codec = priv->hubs.codec;
  2935. #ifndef CONFIG_SND_SOC_WM8994_MODULE
  2936. trace_snd_soc_jack_irq(dev_name(codec->dev));
  2937. #endif
  2938. pm_wakeup_event(codec->dev, 300);
  2939. queue_delayed_work(system_power_efficient_wq,
  2940. &priv->mic_work, msecs_to_jiffies(250));
  2941. return IRQ_HANDLED;
  2942. }
  2943. /* Should be called with accdet_lock held */
  2944. static void wm1811_micd_stop(struct snd_soc_codec *codec)
  2945. {
  2946. struct snd_soc_dapm_context *dapm = snd_soc_codec_get_dapm(codec);
  2947. struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
  2948. if (!wm8994->jackdet)
  2949. return;
  2950. snd_soc_update_bits(codec, WM8958_MIC_DETECT_1, WM8958_MICD_ENA, 0);
  2951. wm1811_jackdet_set_mode(codec, WM1811_JACKDET_MODE_JACK);
  2952. if (wm8994->wm8994->pdata.jd_ext_cap)
  2953. snd_soc_dapm_disable_pin(dapm, "MICBIAS2");
  2954. }
  2955. static void wm8958_button_det(struct snd_soc_codec *codec, u16 status)
  2956. {
  2957. struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
  2958. int report;
  2959. report = 0;
  2960. if (status & 0x4)
  2961. report |= SND_JACK_BTN_0;
  2962. if (status & 0x8)
  2963. report |= SND_JACK_BTN_1;
  2964. if (status & 0x10)
  2965. report |= SND_JACK_BTN_2;
  2966. if (status & 0x20)
  2967. report |= SND_JACK_BTN_3;
  2968. if (status & 0x40)
  2969. report |= SND_JACK_BTN_4;
  2970. if (status & 0x80)
  2971. report |= SND_JACK_BTN_5;
  2972. snd_soc_jack_report(wm8994->micdet[0].jack, report,
  2973. wm8994->btn_mask);
  2974. }
  2975. static void wm8958_open_circuit_work(struct work_struct *work)
  2976. {
  2977. struct wm8994_priv *wm8994 = container_of(work,
  2978. struct wm8994_priv,
  2979. open_circuit_work.work);
  2980. struct device *dev = wm8994->wm8994->dev;
  2981. mutex_lock(&wm8994->accdet_lock);
  2982. wm1811_micd_stop(wm8994->hubs.codec);
  2983. dev_dbg(dev, "Reporting open circuit\n");
  2984. wm8994->jack_mic = false;
  2985. wm8994->mic_detecting = true;
  2986. wm8958_micd_set_rate(wm8994->hubs.codec);
  2987. snd_soc_jack_report(wm8994->micdet[0].jack, 0,
  2988. wm8994->btn_mask |
  2989. SND_JACK_HEADSET);
  2990. mutex_unlock(&wm8994->accdet_lock);
  2991. }
  2992. static void wm8958_mic_id(void *data, u16 status)
  2993. {
  2994. struct snd_soc_codec *codec = data;
  2995. struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
  2996. /* Either nothing present or just starting detection */
  2997. if (!(status & WM8958_MICD_STS)) {
  2998. /* If nothing present then clear our statuses */
  2999. dev_dbg(codec->dev, "Detected open circuit\n");
  3000. queue_delayed_work(system_power_efficient_wq,
  3001. &wm8994->open_circuit_work,
  3002. msecs_to_jiffies(2500));
  3003. return;
  3004. }
  3005. /* If the measurement is showing a high impedence we've got a
  3006. * microphone.
  3007. */
  3008. if (status & 0x600) {
  3009. dev_dbg(codec->dev, "Detected microphone\n");
  3010. wm8994->mic_detecting = false;
  3011. wm8994->jack_mic = true;
  3012. wm8958_micd_set_rate(codec);
  3013. snd_soc_jack_report(wm8994->micdet[0].jack, SND_JACK_HEADSET,
  3014. SND_JACK_HEADSET);
  3015. }
  3016. if (status & 0xfc) {
  3017. dev_dbg(codec->dev, "Detected headphone\n");
  3018. wm8994->mic_detecting = false;
  3019. wm8958_micd_set_rate(codec);
  3020. /* If we have jackdet that will detect removal */
  3021. wm1811_micd_stop(codec);
  3022. snd_soc_jack_report(wm8994->micdet[0].jack, SND_JACK_HEADPHONE,
  3023. SND_JACK_HEADSET);
  3024. }
  3025. }
  3026. /* Deferred mic detection to allow for extra settling time */
  3027. static void wm1811_mic_work(struct work_struct *work)
  3028. {
  3029. struct wm8994_priv *wm8994 = container_of(work, struct wm8994_priv,
  3030. mic_work.work);
  3031. struct wm8994 *control = wm8994->wm8994;
  3032. struct snd_soc_codec *codec = wm8994->hubs.codec;
  3033. struct snd_soc_dapm_context *dapm = snd_soc_codec_get_dapm(codec);
  3034. pm_runtime_get_sync(codec->dev);
  3035. /* If required for an external cap force MICBIAS on */
  3036. if (control->pdata.jd_ext_cap) {
  3037. snd_soc_dapm_force_enable_pin(dapm, "MICBIAS2");
  3038. snd_soc_dapm_sync(dapm);
  3039. }
  3040. mutex_lock(&wm8994->accdet_lock);
  3041. dev_dbg(codec->dev, "Starting mic detection\n");
  3042. /* Use a user-supplied callback if we have one */
  3043. if (wm8994->micd_cb) {
  3044. wm8994->micd_cb(wm8994->micd_cb_data);
  3045. } else {
  3046. /*
  3047. * Start off measument of microphone impedence to find out
  3048. * what's actually there.
  3049. */
  3050. wm8994->mic_detecting = true;
  3051. wm1811_jackdet_set_mode(codec, WM1811_JACKDET_MODE_MIC);
  3052. snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
  3053. WM8958_MICD_ENA, WM8958_MICD_ENA);
  3054. }
  3055. mutex_unlock(&wm8994->accdet_lock);
  3056. pm_runtime_put(codec->dev);
  3057. }
  3058. static irqreturn_t wm1811_jackdet_irq(int irq, void *data)
  3059. {
  3060. struct wm8994_priv *wm8994 = data;
  3061. struct wm8994 *control = wm8994->wm8994;
  3062. struct snd_soc_codec *codec = wm8994->hubs.codec;
  3063. struct snd_soc_dapm_context *dapm = snd_soc_codec_get_dapm(codec);
  3064. int reg, delay;
  3065. bool present;
  3066. pm_runtime_get_sync(codec->dev);
  3067. cancel_delayed_work_sync(&wm8994->mic_complete_work);
  3068. mutex_lock(&wm8994->accdet_lock);
  3069. reg = snd_soc_read(codec, WM1811_JACKDET_CTRL);
  3070. if (reg < 0) {
  3071. dev_err(codec->dev, "Failed to read jack status: %d\n", reg);
  3072. mutex_unlock(&wm8994->accdet_lock);
  3073. pm_runtime_put(codec->dev);
  3074. return IRQ_NONE;
  3075. }
  3076. dev_dbg(codec->dev, "JACKDET %x\n", reg);
  3077. present = reg & WM1811_JACKDET_LVL;
  3078. if (present) {
  3079. dev_dbg(codec->dev, "Jack detected\n");
  3080. wm8958_micd_set_rate(codec);
  3081. snd_soc_update_bits(codec, WM8958_MICBIAS2,
  3082. WM8958_MICB2_DISCH, 0);
  3083. /* Disable debounce while inserted */
  3084. snd_soc_update_bits(codec, WM1811_JACKDET_CTRL,
  3085. WM1811_JACKDET_DB, 0);
  3086. delay = control->pdata.micdet_delay;
  3087. queue_delayed_work(system_power_efficient_wq,
  3088. &wm8994->mic_work,
  3089. msecs_to_jiffies(delay));
  3090. } else {
  3091. dev_dbg(codec->dev, "Jack not detected\n");
  3092. cancel_delayed_work_sync(&wm8994->mic_work);
  3093. snd_soc_update_bits(codec, WM8958_MICBIAS2,
  3094. WM8958_MICB2_DISCH, WM8958_MICB2_DISCH);
  3095. /* Enable debounce while removed */
  3096. snd_soc_update_bits(codec, WM1811_JACKDET_CTRL,
  3097. WM1811_JACKDET_DB, WM1811_JACKDET_DB);
  3098. wm8994->mic_detecting = false;
  3099. wm8994->jack_mic = false;
  3100. snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
  3101. WM8958_MICD_ENA, 0);
  3102. wm1811_jackdet_set_mode(codec, WM1811_JACKDET_MODE_JACK);
  3103. }
  3104. mutex_unlock(&wm8994->accdet_lock);
  3105. /* Turn off MICBIAS if it was on for an external cap */
  3106. if (control->pdata.jd_ext_cap && !present)
  3107. snd_soc_dapm_disable_pin(dapm, "MICBIAS2");
  3108. if (present)
  3109. snd_soc_jack_report(wm8994->micdet[0].jack,
  3110. SND_JACK_MECHANICAL, SND_JACK_MECHANICAL);
  3111. else
  3112. snd_soc_jack_report(wm8994->micdet[0].jack, 0,
  3113. SND_JACK_MECHANICAL | SND_JACK_HEADSET |
  3114. wm8994->btn_mask);
  3115. /* Since we only report deltas force an update, ensures we
  3116. * avoid bootstrapping issues with the core. */
  3117. snd_soc_jack_report(wm8994->micdet[0].jack, 0, 0);
  3118. pm_runtime_put(codec->dev);
  3119. return IRQ_HANDLED;
  3120. }
  3121. static void wm1811_jackdet_bootstrap(struct work_struct *work)
  3122. {
  3123. struct wm8994_priv *wm8994 = container_of(work,
  3124. struct wm8994_priv,
  3125. jackdet_bootstrap.work);
  3126. wm1811_jackdet_irq(0, wm8994);
  3127. }
  3128. /**
  3129. * wm8958_mic_detect - Enable microphone detection via the WM8958 IRQ
  3130. *
  3131. * @codec: WM8958 codec
  3132. * @jack: jack to report detection events on
  3133. *
  3134. * Enable microphone detection functionality for the WM8958. By
  3135. * default simple detection which supports the detection of up to 6
  3136. * buttons plus video and microphone functionality is supported.
  3137. *
  3138. * The WM8958 has an advanced jack detection facility which is able to
  3139. * support complex accessory detection, especially when used in
  3140. * conjunction with external circuitry. In order to provide maximum
  3141. * flexiblity a callback is provided which allows a completely custom
  3142. * detection algorithm.
  3143. */
  3144. int wm8958_mic_detect(struct snd_soc_codec *codec, struct snd_soc_jack *jack,
  3145. wm1811_micdet_cb det_cb, void *det_cb_data,
  3146. wm1811_mic_id_cb id_cb, void *id_cb_data)
  3147. {
  3148. struct snd_soc_dapm_context *dapm = snd_soc_codec_get_dapm(codec);
  3149. struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
  3150. struct wm8994 *control = wm8994->wm8994;
  3151. u16 micd_lvl_sel;
  3152. switch (control->type) {
  3153. case WM1811:
  3154. case WM8958:
  3155. break;
  3156. default:
  3157. return -EINVAL;
  3158. }
  3159. if (jack) {
  3160. snd_soc_dapm_force_enable_pin(dapm, "CLK_SYS");
  3161. snd_soc_dapm_sync(dapm);
  3162. wm8994->micdet[0].jack = jack;
  3163. if (det_cb) {
  3164. wm8994->micd_cb = det_cb;
  3165. wm8994->micd_cb_data = det_cb_data;
  3166. } else {
  3167. wm8994->mic_detecting = true;
  3168. wm8994->jack_mic = false;
  3169. }
  3170. if (id_cb) {
  3171. wm8994->mic_id_cb = id_cb;
  3172. wm8994->mic_id_cb_data = id_cb_data;
  3173. } else {
  3174. wm8994->mic_id_cb = wm8958_mic_id;
  3175. wm8994->mic_id_cb_data = codec;
  3176. }
  3177. wm8958_micd_set_rate(codec);
  3178. /* Detect microphones and short circuits by default */
  3179. if (control->pdata.micd_lvl_sel)
  3180. micd_lvl_sel = control->pdata.micd_lvl_sel;
  3181. else
  3182. micd_lvl_sel = 0x41;
  3183. wm8994->btn_mask = SND_JACK_BTN_0 | SND_JACK_BTN_1 |
  3184. SND_JACK_BTN_2 | SND_JACK_BTN_3 |
  3185. SND_JACK_BTN_4 | SND_JACK_BTN_5;
  3186. snd_soc_update_bits(codec, WM8958_MIC_DETECT_2,
  3187. WM8958_MICD_LVL_SEL_MASK, micd_lvl_sel);
  3188. WARN_ON(snd_soc_codec_get_bias_level(codec) > SND_SOC_BIAS_STANDBY);
  3189. /*
  3190. * If we can use jack detection start off with that,
  3191. * otherwise jump straight to microphone detection.
  3192. */
  3193. if (wm8994->jackdet) {
  3194. /* Disable debounce for the initial detect */
  3195. snd_soc_update_bits(codec, WM1811_JACKDET_CTRL,
  3196. WM1811_JACKDET_DB, 0);
  3197. snd_soc_update_bits(codec, WM8958_MICBIAS2,
  3198. WM8958_MICB2_DISCH,
  3199. WM8958_MICB2_DISCH);
  3200. snd_soc_update_bits(codec, WM8994_LDO_1,
  3201. WM8994_LDO1_DISCH, 0);
  3202. wm1811_jackdet_set_mode(codec,
  3203. WM1811_JACKDET_MODE_JACK);
  3204. } else {
  3205. snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
  3206. WM8958_MICD_ENA, WM8958_MICD_ENA);
  3207. }
  3208. } else {
  3209. snd_soc_update_bits(codec, WM8958_MIC_DETECT_1,
  3210. WM8958_MICD_ENA, 0);
  3211. wm1811_jackdet_set_mode(codec, WM1811_JACKDET_MODE_NONE);
  3212. snd_soc_dapm_disable_pin(dapm, "CLK_SYS");
  3213. snd_soc_dapm_sync(dapm);
  3214. }
  3215. return 0;
  3216. }
  3217. EXPORT_SYMBOL_GPL(wm8958_mic_detect);
  3218. static void wm8958_mic_work(struct work_struct *work)
  3219. {
  3220. struct wm8994_priv *wm8994 = container_of(work,
  3221. struct wm8994_priv,
  3222. mic_complete_work.work);
  3223. struct snd_soc_codec *codec = wm8994->hubs.codec;
  3224. pm_runtime_get_sync(codec->dev);
  3225. mutex_lock(&wm8994->accdet_lock);
  3226. wm8994->mic_id_cb(wm8994->mic_id_cb_data, wm8994->mic_status);
  3227. mutex_unlock(&wm8994->accdet_lock);
  3228. pm_runtime_put(codec->dev);
  3229. }
  3230. static irqreturn_t wm8958_mic_irq(int irq, void *data)
  3231. {
  3232. struct wm8994_priv *wm8994 = data;
  3233. struct snd_soc_codec *codec = wm8994->hubs.codec;
  3234. int reg, count, ret, id_delay;
  3235. /*
  3236. * Jack detection may have detected a removal simulataneously
  3237. * with an update of the MICDET status; if so it will have
  3238. * stopped detection and we can ignore this interrupt.
  3239. */
  3240. if (!(snd_soc_read(codec, WM8958_MIC_DETECT_1) & WM8958_MICD_ENA))
  3241. return IRQ_HANDLED;
  3242. cancel_delayed_work_sync(&wm8994->mic_complete_work);
  3243. cancel_delayed_work_sync(&wm8994->open_circuit_work);
  3244. pm_runtime_get_sync(codec->dev);
  3245. /* We may occasionally read a detection without an impedence
  3246. * range being provided - if that happens loop again.
  3247. */
  3248. count = 10;
  3249. do {
  3250. reg = snd_soc_read(codec, WM8958_MIC_DETECT_3);
  3251. if (reg < 0) {
  3252. dev_err(codec->dev,
  3253. "Failed to read mic detect status: %d\n",
  3254. reg);
  3255. pm_runtime_put(codec->dev);
  3256. return IRQ_NONE;
  3257. }
  3258. if (!(reg & WM8958_MICD_VALID)) {
  3259. dev_dbg(codec->dev, "Mic detect data not valid\n");
  3260. goto out;
  3261. }
  3262. if (!(reg & WM8958_MICD_STS) || (reg & WM8958_MICD_LVL_MASK))
  3263. break;
  3264. msleep(1);
  3265. } while (count--);
  3266. if (count == 0)
  3267. dev_warn(codec->dev, "No impedance range reported for jack\n");
  3268. #ifndef CONFIG_SND_SOC_WM8994_MODULE
  3269. trace_snd_soc_jack_irq(dev_name(codec->dev));
  3270. #endif
  3271. /* Avoid a transient report when the accessory is being removed */
  3272. if (wm8994->jackdet) {
  3273. ret = snd_soc_read(codec, WM1811_JACKDET_CTRL);
  3274. if (ret < 0) {
  3275. dev_err(codec->dev, "Failed to read jack status: %d\n",
  3276. ret);
  3277. } else if (!(ret & WM1811_JACKDET_LVL)) {
  3278. dev_dbg(codec->dev, "Ignoring removed jack\n");
  3279. goto out;
  3280. }
  3281. } else if (!(reg & WM8958_MICD_STS)) {
  3282. snd_soc_jack_report(wm8994->micdet[0].jack, 0,
  3283. SND_JACK_MECHANICAL | SND_JACK_HEADSET |
  3284. wm8994->btn_mask);
  3285. wm8994->mic_detecting = true;
  3286. goto out;
  3287. }
  3288. wm8994->mic_status = reg;
  3289. id_delay = wm8994->wm8994->pdata.mic_id_delay;
  3290. if (wm8994->mic_detecting)
  3291. queue_delayed_work(system_power_efficient_wq,
  3292. &wm8994->mic_complete_work,
  3293. msecs_to_jiffies(id_delay));
  3294. else
  3295. wm8958_button_det(codec, reg);
  3296. out:
  3297. pm_runtime_put(codec->dev);
  3298. return IRQ_HANDLED;
  3299. }
  3300. static irqreturn_t wm8994_fifo_error(int irq, void *data)
  3301. {
  3302. struct snd_soc_codec *codec = data;
  3303. dev_err(codec->dev, "FIFO error\n");
  3304. return IRQ_HANDLED;
  3305. }
  3306. static irqreturn_t wm8994_temp_warn(int irq, void *data)
  3307. {
  3308. struct snd_soc_codec *codec = data;
  3309. dev_err(codec->dev, "Thermal warning\n");
  3310. return IRQ_HANDLED;
  3311. }
  3312. static irqreturn_t wm8994_temp_shut(int irq, void *data)
  3313. {
  3314. struct snd_soc_codec *codec = data;
  3315. dev_crit(codec->dev, "Thermal shutdown\n");
  3316. return IRQ_HANDLED;
  3317. }
  3318. static int wm8994_codec_probe(struct snd_soc_codec *codec)
  3319. {
  3320. struct snd_soc_dapm_context *dapm = snd_soc_codec_get_dapm(codec);
  3321. struct wm8994 *control = dev_get_drvdata(codec->dev->parent);
  3322. struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
  3323. unsigned int reg;
  3324. int ret, i;
  3325. wm8994->hubs.codec = codec;
  3326. mutex_init(&wm8994->accdet_lock);
  3327. INIT_DELAYED_WORK(&wm8994->jackdet_bootstrap,
  3328. wm1811_jackdet_bootstrap);
  3329. INIT_DELAYED_WORK(&wm8994->open_circuit_work,
  3330. wm8958_open_circuit_work);
  3331. switch (control->type) {
  3332. case WM8994:
  3333. INIT_DELAYED_WORK(&wm8994->mic_work, wm8994_mic_work);
  3334. break;
  3335. case WM1811:
  3336. INIT_DELAYED_WORK(&wm8994->mic_work, wm1811_mic_work);
  3337. break;
  3338. default:
  3339. break;
  3340. }
  3341. INIT_DELAYED_WORK(&wm8994->mic_complete_work, wm8958_mic_work);
  3342. for (i = 0; i < ARRAY_SIZE(wm8994->fll_locked); i++)
  3343. init_completion(&wm8994->fll_locked[i]);
  3344. wm8994->micdet_irq = control->pdata.micdet_irq;
  3345. /* By default use idle_bias_off, will override for WM8994 */
  3346. dapm->idle_bias_off = 1;
  3347. /* Set revision-specific configuration */
  3348. switch (control->type) {
  3349. case WM8994:
  3350. /* Single ended line outputs should have VMID on. */
  3351. if (!control->pdata.lineout1_diff ||
  3352. !control->pdata.lineout2_diff)
  3353. dapm->idle_bias_off = 0;
  3354. switch (control->revision) {
  3355. case 2:
  3356. case 3:
  3357. wm8994->hubs.dcs_codes_l = -5;
  3358. wm8994->hubs.dcs_codes_r = -5;
  3359. wm8994->hubs.hp_startup_mode = 1;
  3360. wm8994->hubs.dcs_readback_mode = 1;
  3361. wm8994->hubs.series_startup = 1;
  3362. break;
  3363. default:
  3364. wm8994->hubs.dcs_readback_mode = 2;
  3365. break;
  3366. }
  3367. break;
  3368. case WM8958:
  3369. wm8994->hubs.dcs_readback_mode = 1;
  3370. wm8994->hubs.hp_startup_mode = 1;
  3371. switch (control->revision) {
  3372. case 0:
  3373. break;
  3374. default:
  3375. wm8994->fll_byp = true;
  3376. break;
  3377. }
  3378. break;
  3379. case WM1811:
  3380. wm8994->hubs.dcs_readback_mode = 2;
  3381. wm8994->hubs.no_series_update = 1;
  3382. wm8994->hubs.hp_startup_mode = 1;
  3383. wm8994->hubs.no_cache_dac_hp_direct = true;
  3384. wm8994->fll_byp = true;
  3385. wm8994->hubs.dcs_codes_l = -9;
  3386. wm8994->hubs.dcs_codes_r = -7;
  3387. snd_soc_update_bits(codec, WM8994_ANALOGUE_HP_1,
  3388. WM1811_HPOUT1_ATTN, WM1811_HPOUT1_ATTN);
  3389. break;
  3390. default:
  3391. break;
  3392. }
  3393. wm8994_request_irq(wm8994->wm8994, WM8994_IRQ_FIFOS_ERR,
  3394. wm8994_fifo_error, "FIFO error", codec);
  3395. wm8994_request_irq(wm8994->wm8994, WM8994_IRQ_TEMP_WARN,
  3396. wm8994_temp_warn, "Thermal warning", codec);
  3397. wm8994_request_irq(wm8994->wm8994, WM8994_IRQ_TEMP_SHUT,
  3398. wm8994_temp_shut, "Thermal shutdown", codec);
  3399. switch (control->type) {
  3400. case WM8994:
  3401. if (wm8994->micdet_irq)
  3402. ret = request_threaded_irq(wm8994->micdet_irq, NULL,
  3403. wm8994_mic_irq,
  3404. IRQF_TRIGGER_RISING |
  3405. IRQF_ONESHOT,
  3406. "Mic1 detect",
  3407. wm8994);
  3408. else
  3409. ret = wm8994_request_irq(wm8994->wm8994,
  3410. WM8994_IRQ_MIC1_DET,
  3411. wm8994_mic_irq, "Mic 1 detect",
  3412. wm8994);
  3413. if (ret != 0)
  3414. dev_warn(codec->dev,
  3415. "Failed to request Mic1 detect IRQ: %d\n",
  3416. ret);
  3417. ret = wm8994_request_irq(wm8994->wm8994,
  3418. WM8994_IRQ_MIC1_SHRT,
  3419. wm8994_mic_irq, "Mic 1 short",
  3420. wm8994);
  3421. if (ret != 0)
  3422. dev_warn(codec->dev,
  3423. "Failed to request Mic1 short IRQ: %d\n",
  3424. ret);
  3425. ret = wm8994_request_irq(wm8994->wm8994,
  3426. WM8994_IRQ_MIC2_DET,
  3427. wm8994_mic_irq, "Mic 2 detect",
  3428. wm8994);
  3429. if (ret != 0)
  3430. dev_warn(codec->dev,
  3431. "Failed to request Mic2 detect IRQ: %d\n",
  3432. ret);
  3433. ret = wm8994_request_irq(wm8994->wm8994,
  3434. WM8994_IRQ_MIC2_SHRT,
  3435. wm8994_mic_irq, "Mic 2 short",
  3436. wm8994);
  3437. if (ret != 0)
  3438. dev_warn(codec->dev,
  3439. "Failed to request Mic2 short IRQ: %d\n",
  3440. ret);
  3441. break;
  3442. case WM8958:
  3443. case WM1811:
  3444. if (wm8994->micdet_irq) {
  3445. ret = request_threaded_irq(wm8994->micdet_irq, NULL,
  3446. wm8958_mic_irq,
  3447. IRQF_TRIGGER_RISING |
  3448. IRQF_ONESHOT,
  3449. "Mic detect",
  3450. wm8994);
  3451. if (ret != 0)
  3452. dev_warn(codec->dev,
  3453. "Failed to request Mic detect IRQ: %d\n",
  3454. ret);
  3455. } else {
  3456. wm8994_request_irq(wm8994->wm8994, WM8994_IRQ_MIC1_DET,
  3457. wm8958_mic_irq, "Mic detect",
  3458. wm8994);
  3459. }
  3460. }
  3461. switch (control->type) {
  3462. case WM1811:
  3463. if (control->cust_id > 1 || control->revision > 1) {
  3464. ret = wm8994_request_irq(wm8994->wm8994,
  3465. WM8994_IRQ_GPIO(6),
  3466. wm1811_jackdet_irq, "JACKDET",
  3467. wm8994);
  3468. if (ret == 0)
  3469. wm8994->jackdet = true;
  3470. }
  3471. break;
  3472. default:
  3473. break;
  3474. }
  3475. wm8994->fll_locked_irq = true;
  3476. for (i = 0; i < ARRAY_SIZE(wm8994->fll_locked); i++) {
  3477. ret = wm8994_request_irq(wm8994->wm8994,
  3478. WM8994_IRQ_FLL1_LOCK + i,
  3479. wm8994_fll_locked_irq, "FLL lock",
  3480. &wm8994->fll_locked[i]);
  3481. if (ret != 0)
  3482. wm8994->fll_locked_irq = false;
  3483. }
  3484. /* Make sure we can read from the GPIOs if they're inputs */
  3485. pm_runtime_get_sync(codec->dev);
  3486. /* Remember if AIFnLRCLK is configured as a GPIO. This should be
  3487. * configured on init - if a system wants to do this dynamically
  3488. * at runtime we can deal with that then.
  3489. */
  3490. ret = regmap_read(control->regmap, WM8994_GPIO_1, &reg);
  3491. if (ret < 0) {
  3492. dev_err(codec->dev, "Failed to read GPIO1 state: %d\n", ret);
  3493. goto err_irq;
  3494. }
  3495. if ((reg & WM8994_GPN_FN_MASK) != WM8994_GP_FN_PIN_SPECIFIC) {
  3496. wm8994->lrclk_shared[0] = 1;
  3497. wm8994_dai[0].symmetric_rates = 1;
  3498. } else {
  3499. wm8994->lrclk_shared[0] = 0;
  3500. }
  3501. ret = regmap_read(control->regmap, WM8994_GPIO_6, &reg);
  3502. if (ret < 0) {
  3503. dev_err(codec->dev, "Failed to read GPIO6 state: %d\n", ret);
  3504. goto err_irq;
  3505. }
  3506. if ((reg & WM8994_GPN_FN_MASK) != WM8994_GP_FN_PIN_SPECIFIC) {
  3507. wm8994->lrclk_shared[1] = 1;
  3508. wm8994_dai[1].symmetric_rates = 1;
  3509. } else {
  3510. wm8994->lrclk_shared[1] = 0;
  3511. }
  3512. pm_runtime_put(codec->dev);
  3513. /* Latch volume update bits */
  3514. for (i = 0; i < ARRAY_SIZE(wm8994_vu_bits); i++)
  3515. snd_soc_update_bits(codec, wm8994_vu_bits[i].reg,
  3516. wm8994_vu_bits[i].mask,
  3517. wm8994_vu_bits[i].mask);
  3518. /* Set the low bit of the 3D stereo depth so TLV matches */
  3519. snd_soc_update_bits(codec, WM8994_AIF1_DAC1_FILTERS_2,
  3520. 1 << WM8994_AIF1DAC1_3D_GAIN_SHIFT,
  3521. 1 << WM8994_AIF1DAC1_3D_GAIN_SHIFT);
  3522. snd_soc_update_bits(codec, WM8994_AIF1_DAC2_FILTERS_2,
  3523. 1 << WM8994_AIF1DAC2_3D_GAIN_SHIFT,
  3524. 1 << WM8994_AIF1DAC2_3D_GAIN_SHIFT);
  3525. snd_soc_update_bits(codec, WM8994_AIF2_DAC_FILTERS_2,
  3526. 1 << WM8994_AIF2DAC_3D_GAIN_SHIFT,
  3527. 1 << WM8994_AIF2DAC_3D_GAIN_SHIFT);
  3528. /* Unconditionally enable AIF1 ADC TDM mode on chips which can
  3529. * use this; it only affects behaviour on idle TDM clock
  3530. * cycles. */
  3531. switch (control->type) {
  3532. case WM8994:
  3533. case WM8958:
  3534. snd_soc_update_bits(codec, WM8994_AIF1_CONTROL_1,
  3535. WM8994_AIF1ADC_TDM, WM8994_AIF1ADC_TDM);
  3536. break;
  3537. default:
  3538. break;
  3539. }
  3540. /* Put MICBIAS into bypass mode by default on newer devices */
  3541. switch (control->type) {
  3542. case WM8958:
  3543. case WM1811:
  3544. snd_soc_update_bits(codec, WM8958_MICBIAS1,
  3545. WM8958_MICB1_MODE, WM8958_MICB1_MODE);
  3546. snd_soc_update_bits(codec, WM8958_MICBIAS2,
  3547. WM8958_MICB2_MODE, WM8958_MICB2_MODE);
  3548. break;
  3549. default:
  3550. break;
  3551. }
  3552. wm8994->hubs.check_class_w_digital = wm8994_check_class_w_digital;
  3553. wm_hubs_update_class_w(codec);
  3554. wm8994_handle_pdata(wm8994);
  3555. wm_hubs_add_analogue_controls(codec);
  3556. snd_soc_add_codec_controls(codec, wm8994_snd_controls,
  3557. ARRAY_SIZE(wm8994_snd_controls));
  3558. snd_soc_dapm_new_controls(dapm, wm8994_dapm_widgets,
  3559. ARRAY_SIZE(wm8994_dapm_widgets));
  3560. switch (control->type) {
  3561. case WM8994:
  3562. snd_soc_dapm_new_controls(dapm, wm8994_specific_dapm_widgets,
  3563. ARRAY_SIZE(wm8994_specific_dapm_widgets));
  3564. if (control->revision < 4) {
  3565. snd_soc_dapm_new_controls(dapm, wm8994_lateclk_revd_widgets,
  3566. ARRAY_SIZE(wm8994_lateclk_revd_widgets));
  3567. snd_soc_dapm_new_controls(dapm, wm8994_adc_revd_widgets,
  3568. ARRAY_SIZE(wm8994_adc_revd_widgets));
  3569. snd_soc_dapm_new_controls(dapm, wm8994_dac_revd_widgets,
  3570. ARRAY_SIZE(wm8994_dac_revd_widgets));
  3571. } else {
  3572. snd_soc_dapm_new_controls(dapm, wm8994_lateclk_widgets,
  3573. ARRAY_SIZE(wm8994_lateclk_widgets));
  3574. snd_soc_dapm_new_controls(dapm, wm8994_adc_widgets,
  3575. ARRAY_SIZE(wm8994_adc_widgets));
  3576. snd_soc_dapm_new_controls(dapm, wm8994_dac_widgets,
  3577. ARRAY_SIZE(wm8994_dac_widgets));
  3578. }
  3579. break;
  3580. case WM8958:
  3581. snd_soc_add_codec_controls(codec, wm8958_snd_controls,
  3582. ARRAY_SIZE(wm8958_snd_controls));
  3583. snd_soc_dapm_new_controls(dapm, wm8958_dapm_widgets,
  3584. ARRAY_SIZE(wm8958_dapm_widgets));
  3585. if (control->revision < 1) {
  3586. snd_soc_dapm_new_controls(dapm, wm8994_lateclk_revd_widgets,
  3587. ARRAY_SIZE(wm8994_lateclk_revd_widgets));
  3588. snd_soc_dapm_new_controls(dapm, wm8994_adc_revd_widgets,
  3589. ARRAY_SIZE(wm8994_adc_revd_widgets));
  3590. snd_soc_dapm_new_controls(dapm, wm8994_dac_revd_widgets,
  3591. ARRAY_SIZE(wm8994_dac_revd_widgets));
  3592. } else {
  3593. snd_soc_dapm_new_controls(dapm, wm8994_lateclk_widgets,
  3594. ARRAY_SIZE(wm8994_lateclk_widgets));
  3595. snd_soc_dapm_new_controls(dapm, wm8994_adc_widgets,
  3596. ARRAY_SIZE(wm8994_adc_widgets));
  3597. snd_soc_dapm_new_controls(dapm, wm8994_dac_widgets,
  3598. ARRAY_SIZE(wm8994_dac_widgets));
  3599. }
  3600. break;
  3601. case WM1811:
  3602. snd_soc_add_codec_controls(codec, wm8958_snd_controls,
  3603. ARRAY_SIZE(wm8958_snd_controls));
  3604. snd_soc_dapm_new_controls(dapm, wm8958_dapm_widgets,
  3605. ARRAY_SIZE(wm8958_dapm_widgets));
  3606. snd_soc_dapm_new_controls(dapm, wm8994_lateclk_widgets,
  3607. ARRAY_SIZE(wm8994_lateclk_widgets));
  3608. snd_soc_dapm_new_controls(dapm, wm8994_adc_widgets,
  3609. ARRAY_SIZE(wm8994_adc_widgets));
  3610. snd_soc_dapm_new_controls(dapm, wm8994_dac_widgets,
  3611. ARRAY_SIZE(wm8994_dac_widgets));
  3612. break;
  3613. }
  3614. wm_hubs_add_analogue_routes(codec, 0, 0);
  3615. ret = wm8994_request_irq(wm8994->wm8994, WM8994_IRQ_DCS_DONE,
  3616. wm_hubs_dcs_done, "DC servo done",
  3617. &wm8994->hubs);
  3618. if (ret == 0)
  3619. wm8994->hubs.dcs_done_irq = true;
  3620. snd_soc_dapm_add_routes(dapm, intercon, ARRAY_SIZE(intercon));
  3621. switch (control->type) {
  3622. case WM8994:
  3623. snd_soc_dapm_add_routes(dapm, wm8994_intercon,
  3624. ARRAY_SIZE(wm8994_intercon));
  3625. if (control->revision < 4) {
  3626. snd_soc_dapm_add_routes(dapm, wm8994_revd_intercon,
  3627. ARRAY_SIZE(wm8994_revd_intercon));
  3628. snd_soc_dapm_add_routes(dapm, wm8994_lateclk_revd_intercon,
  3629. ARRAY_SIZE(wm8994_lateclk_revd_intercon));
  3630. } else {
  3631. snd_soc_dapm_add_routes(dapm, wm8994_lateclk_intercon,
  3632. ARRAY_SIZE(wm8994_lateclk_intercon));
  3633. }
  3634. break;
  3635. case WM8958:
  3636. if (control->revision < 1) {
  3637. snd_soc_dapm_add_routes(dapm, wm8994_intercon,
  3638. ARRAY_SIZE(wm8994_intercon));
  3639. snd_soc_dapm_add_routes(dapm, wm8994_revd_intercon,
  3640. ARRAY_SIZE(wm8994_revd_intercon));
  3641. snd_soc_dapm_add_routes(dapm, wm8994_lateclk_revd_intercon,
  3642. ARRAY_SIZE(wm8994_lateclk_revd_intercon));
  3643. } else {
  3644. snd_soc_dapm_add_routes(dapm, wm8994_lateclk_intercon,
  3645. ARRAY_SIZE(wm8994_lateclk_intercon));
  3646. snd_soc_dapm_add_routes(dapm, wm8958_intercon,
  3647. ARRAY_SIZE(wm8958_intercon));
  3648. }
  3649. wm8958_dsp2_init(codec);
  3650. break;
  3651. case WM1811:
  3652. snd_soc_dapm_add_routes(dapm, wm8994_lateclk_intercon,
  3653. ARRAY_SIZE(wm8994_lateclk_intercon));
  3654. snd_soc_dapm_add_routes(dapm, wm8958_intercon,
  3655. ARRAY_SIZE(wm8958_intercon));
  3656. break;
  3657. }
  3658. return 0;
  3659. err_irq:
  3660. if (wm8994->jackdet)
  3661. wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_GPIO(6), wm8994);
  3662. wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_MIC2_SHRT, wm8994);
  3663. wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_MIC2_DET, wm8994);
  3664. wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_MIC1_SHRT, wm8994);
  3665. if (wm8994->micdet_irq)
  3666. free_irq(wm8994->micdet_irq, wm8994);
  3667. for (i = 0; i < ARRAY_SIZE(wm8994->fll_locked); i++)
  3668. wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_FLL1_LOCK + i,
  3669. &wm8994->fll_locked[i]);
  3670. wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_DCS_DONE,
  3671. &wm8994->hubs);
  3672. wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_FIFOS_ERR, codec);
  3673. wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_TEMP_SHUT, codec);
  3674. wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_TEMP_WARN, codec);
  3675. return ret;
  3676. }
  3677. static int wm8994_codec_remove(struct snd_soc_codec *codec)
  3678. {
  3679. struct wm8994_priv *wm8994 = snd_soc_codec_get_drvdata(codec);
  3680. struct wm8994 *control = wm8994->wm8994;
  3681. int i;
  3682. for (i = 0; i < ARRAY_SIZE(wm8994->fll_locked); i++)
  3683. wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_FLL1_LOCK + i,
  3684. &wm8994->fll_locked[i]);
  3685. wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_DCS_DONE,
  3686. &wm8994->hubs);
  3687. wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_FIFOS_ERR, codec);
  3688. wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_TEMP_SHUT, codec);
  3689. wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_TEMP_WARN, codec);
  3690. if (wm8994->jackdet)
  3691. wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_GPIO(6), wm8994);
  3692. switch (control->type) {
  3693. case WM8994:
  3694. if (wm8994->micdet_irq)
  3695. free_irq(wm8994->micdet_irq, wm8994);
  3696. wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_MIC2_DET,
  3697. wm8994);
  3698. wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_MIC1_SHRT,
  3699. wm8994);
  3700. wm8994_free_irq(wm8994->wm8994, WM8994_IRQ_MIC1_DET,
  3701. wm8994);
  3702. break;
  3703. case WM1811:
  3704. case WM8958:
  3705. if (wm8994->micdet_irq)
  3706. free_irq(wm8994->micdet_irq, wm8994);
  3707. break;
  3708. }
  3709. release_firmware(wm8994->mbc);
  3710. release_firmware(wm8994->mbc_vss);
  3711. release_firmware(wm8994->enh_eq);
  3712. kfree(wm8994->retune_mobile_texts);
  3713. return 0;
  3714. }
  3715. static struct regmap *wm8994_get_regmap(struct device *dev)
  3716. {
  3717. struct wm8994 *control = dev_get_drvdata(dev->parent);
  3718. return control->regmap;
  3719. }
  3720. static const struct snd_soc_codec_driver soc_codec_dev_wm8994 = {
  3721. .probe = wm8994_codec_probe,
  3722. .remove = wm8994_codec_remove,
  3723. .suspend = wm8994_codec_suspend,
  3724. .resume = wm8994_codec_resume,
  3725. .get_regmap = wm8994_get_regmap,
  3726. .set_bias_level = wm8994_set_bias_level,
  3727. };
  3728. static int wm8994_probe(struct platform_device *pdev)
  3729. {
  3730. struct wm8994_priv *wm8994;
  3731. wm8994 = devm_kzalloc(&pdev->dev, sizeof(struct wm8994_priv),
  3732. GFP_KERNEL);
  3733. if (wm8994 == NULL)
  3734. return -ENOMEM;
  3735. platform_set_drvdata(pdev, wm8994);
  3736. mutex_init(&wm8994->fw_lock);
  3737. wm8994->wm8994 = dev_get_drvdata(pdev->dev.parent);
  3738. pm_runtime_enable(&pdev->dev);
  3739. pm_runtime_idle(&pdev->dev);
  3740. return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_wm8994,
  3741. wm8994_dai, ARRAY_SIZE(wm8994_dai));
  3742. }
  3743. static int wm8994_remove(struct platform_device *pdev)
  3744. {
  3745. snd_soc_unregister_codec(&pdev->dev);
  3746. pm_runtime_disable(&pdev->dev);
  3747. return 0;
  3748. }
  3749. #ifdef CONFIG_PM_SLEEP
  3750. static int wm8994_suspend(struct device *dev)
  3751. {
  3752. struct wm8994_priv *wm8994 = dev_get_drvdata(dev);
  3753. /* Drop down to power saving mode when system is suspended */
  3754. if (wm8994->jackdet && !wm8994->active_refcount)
  3755. regmap_update_bits(wm8994->wm8994->regmap, WM8994_ANTIPOP_2,
  3756. WM1811_JACKDET_MODE_MASK,
  3757. wm8994->jackdet_mode);
  3758. return 0;
  3759. }
  3760. static int wm8994_resume(struct device *dev)
  3761. {
  3762. struct wm8994_priv *wm8994 = dev_get_drvdata(dev);
  3763. if (wm8994->jackdet && wm8994->jackdet_mode)
  3764. regmap_update_bits(wm8994->wm8994->regmap, WM8994_ANTIPOP_2,
  3765. WM1811_JACKDET_MODE_MASK,
  3766. WM1811_JACKDET_MODE_AUDIO);
  3767. return 0;
  3768. }
  3769. #endif
  3770. static const struct dev_pm_ops wm8994_pm_ops = {
  3771. SET_SYSTEM_SLEEP_PM_OPS(wm8994_suspend, wm8994_resume)
  3772. };
  3773. static struct platform_driver wm8994_codec_driver = {
  3774. .driver = {
  3775. .name = "wm8994-codec",
  3776. .pm = &wm8994_pm_ops,
  3777. },
  3778. .probe = wm8994_probe,
  3779. .remove = wm8994_remove,
  3780. };
  3781. module_platform_driver(wm8994_codec_driver);
  3782. MODULE_DESCRIPTION("ASoC WM8994 driver");
  3783. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  3784. MODULE_LICENSE("GPL");
  3785. MODULE_ALIAS("platform:wm8994-codec");