wcd938x.c 119 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2018-2020, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/module.h>
  6. #include <linux/slab.h>
  7. #include <linux/platform_device.h>
  8. #include <linux/device.h>
  9. #include <linux/delay.h>
  10. #include <linux/kernel.h>
  11. #include <linux/component.h>
  12. #include <sound/soc.h>
  13. #include <sound/tlv.h>
  14. #include <soc/soundwire.h>
  15. #include <linux/regmap.h>
  16. #include <sound/soc.h>
  17. #include <sound/soc-dapm.h>
  18. #include <asoc/wcdcal-hwdep.h>
  19. #include <asoc/msm-cdc-pinctrl.h>
  20. #include <asoc/msm-cdc-supply.h>
  21. #include <dt-bindings/sound/audio-codec-port-types.h>
  22. #include "wcd938x-registers.h"
  23. #include "wcd938x.h"
  24. #include "internal.h"
  25. #define NUM_SWRS_DT_PARAMS 5
  26. #define WCD938X_VARIANT_ENTRY_SIZE 32
  27. #define WCD938X_VERSION_1_0 1
  28. #define WCD938X_VERSION_ENTRY_SIZE 32
  29. #define EAR_RX_PATH_AUX 1
  30. #define ADC_MODE_VAL_HIFI 0x01
  31. #define ADC_MODE_VAL_LO_HIF 0x02
  32. #define ADC_MODE_VAL_NORMAL 0x03
  33. #define ADC_MODE_VAL_LP 0x05
  34. #define ADC_MODE_VAL_ULP1 0x09
  35. #define ADC_MODE_VAL_ULP2 0x0B
  36. #define NUM_ATTEMPTS 5
  37. #define DAPM_MICBIAS1_STANDALONE "MIC BIAS1 Standalone"
  38. #define DAPM_MICBIAS2_STANDALONE "MIC BIAS2 Standalone"
  39. #define DAPM_MICBIAS3_STANDALONE "MIC BIAS3 Standalone"
  40. #define DAPM_MICBIAS4_STANDALONE "MIC BIAS4 Standalone"
  41. #define WCD938X_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |\
  42. SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_48000 |\
  43. SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_192000 |\
  44. SNDRV_PCM_RATE_384000)
  45. /* Fractional Rates */
  46. #define WCD938X_FRAC_RATES (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_88200 |\
  47. SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_352800)
  48. #define WCD938X_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
  49. SNDRV_PCM_FMTBIT_S24_LE |\
  50. SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE)
  51. enum {
  52. CODEC_TX = 0,
  53. CODEC_RX,
  54. };
  55. enum {
  56. WCD_ADC1 = 0,
  57. WCD_ADC2,
  58. WCD_ADC3,
  59. WCD_ADC4,
  60. ALLOW_BUCK_DISABLE,
  61. HPH_COMP_DELAY,
  62. HPH_PA_DELAY,
  63. AMIC2_BCS_ENABLE,
  64. };
  65. enum {
  66. ADC_MODE_INVALID = 0,
  67. ADC_MODE_HIFI,
  68. ADC_MODE_LO_HIF,
  69. ADC_MODE_NORMAL,
  70. ADC_MODE_LP,
  71. ADC_MODE_ULP1,
  72. ADC_MODE_ULP2,
  73. };
  74. static u8 tx_mode_bit[] = {
  75. [ADC_MODE_INVALID] = 0x00,
  76. [ADC_MODE_HIFI] = 0x01,
  77. [ADC_MODE_LO_HIF] = 0x02,
  78. [ADC_MODE_NORMAL] = 0x04,
  79. [ADC_MODE_LP] = 0x08,
  80. [ADC_MODE_ULP1] = 0x10,
  81. [ADC_MODE_ULP2] = 0x20,
  82. };
  83. static const DECLARE_TLV_DB_SCALE(line_gain, 0, 7, 1);
  84. static const DECLARE_TLV_DB_SCALE(analog_gain, 0, 25, 1);
  85. static int wcd938x_handle_post_irq(void *data);
  86. static int wcd938x_reset(struct device *dev);
  87. static int wcd938x_reset_low(struct device *dev);
  88. static const struct regmap_irq wcd938x_irqs[WCD938X_NUM_IRQS] = {
  89. REGMAP_IRQ_REG(WCD938X_IRQ_MBHC_BUTTON_PRESS_DET, 0, 0x01),
  90. REGMAP_IRQ_REG(WCD938X_IRQ_MBHC_BUTTON_RELEASE_DET, 0, 0x02),
  91. REGMAP_IRQ_REG(WCD938X_IRQ_MBHC_ELECT_INS_REM_DET, 0, 0x04),
  92. REGMAP_IRQ_REG(WCD938X_IRQ_MBHC_ELECT_INS_REM_LEG_DET, 0, 0x08),
  93. REGMAP_IRQ_REG(WCD938X_IRQ_MBHC_SW_DET, 0, 0x10),
  94. REGMAP_IRQ_REG(WCD938X_IRQ_HPHR_OCP_INT, 0, 0x20),
  95. REGMAP_IRQ_REG(WCD938X_IRQ_HPHR_CNP_INT, 0, 0x40),
  96. REGMAP_IRQ_REG(WCD938X_IRQ_HPHL_OCP_INT, 0, 0x80),
  97. REGMAP_IRQ_REG(WCD938X_IRQ_HPHL_CNP_INT, 1, 0x01),
  98. REGMAP_IRQ_REG(WCD938X_IRQ_EAR_CNP_INT, 1, 0x02),
  99. REGMAP_IRQ_REG(WCD938X_IRQ_EAR_SCD_INT, 1, 0x04),
  100. REGMAP_IRQ_REG(WCD938X_IRQ_AUX_CNP_INT, 1, 0x08),
  101. REGMAP_IRQ_REG(WCD938X_IRQ_AUX_SCD_INT, 1, 0x10),
  102. REGMAP_IRQ_REG(WCD938X_IRQ_HPHL_PDM_WD_INT, 1, 0x20),
  103. REGMAP_IRQ_REG(WCD938X_IRQ_HPHR_PDM_WD_INT, 1, 0x40),
  104. REGMAP_IRQ_REG(WCD938X_IRQ_AUX_PDM_WD_INT, 1, 0x80),
  105. REGMAP_IRQ_REG(WCD938X_IRQ_LDORT_SCD_INT, 2, 0x01),
  106. REGMAP_IRQ_REG(WCD938X_IRQ_MBHC_MOISTURE_INT, 2, 0x02),
  107. REGMAP_IRQ_REG(WCD938X_IRQ_HPHL_SURGE_DET_INT, 2, 0x04),
  108. REGMAP_IRQ_REG(WCD938X_IRQ_HPHR_SURGE_DET_INT, 2, 0x08),
  109. };
  110. static struct regmap_irq_chip wcd938x_regmap_irq_chip = {
  111. .name = "wcd938x",
  112. .irqs = wcd938x_irqs,
  113. .num_irqs = ARRAY_SIZE(wcd938x_irqs),
  114. .num_regs = 3,
  115. .status_base = WCD938X_DIGITAL_INTR_STATUS_0,
  116. .mask_base = WCD938X_DIGITAL_INTR_MASK_0,
  117. .type_base = WCD938X_DIGITAL_INTR_LEVEL_0,
  118. .ack_base = WCD938X_DIGITAL_INTR_CLEAR_0,
  119. .use_ack = 1,
  120. .runtime_pm = false,
  121. .handle_post_irq = wcd938x_handle_post_irq,
  122. .irq_drv_data = NULL,
  123. };
  124. static int wcd938x_handle_post_irq(void *data)
  125. {
  126. struct wcd938x_priv *wcd938x = data;
  127. u32 sts1 = 0, sts2 = 0, sts3 = 0;
  128. regmap_read(wcd938x->regmap, WCD938X_DIGITAL_INTR_STATUS_0, &sts1);
  129. regmap_read(wcd938x->regmap, WCD938X_DIGITAL_INTR_STATUS_1, &sts2);
  130. regmap_read(wcd938x->regmap, WCD938X_DIGITAL_INTR_STATUS_2, &sts3);
  131. wcd938x->tx_swr_dev->slave_irq_pending =
  132. ((sts1 || sts2 || sts3) ? true : false);
  133. return IRQ_HANDLED;
  134. }
  135. static int wcd938x_swr_slv_get_current_bank(struct swr_device *dev, u8 devnum)
  136. {
  137. int ret = 0;
  138. int bank = 0;
  139. ret = swr_read(dev, devnum, SWR_SCP_CONTROL, &bank, 1);
  140. if (ret)
  141. return -EINVAL;
  142. return ((bank & 0x40) ? 1: 0);
  143. }
  144. static int wcd938x_get_clk_rate(int mode)
  145. {
  146. int rate;
  147. switch (mode) {
  148. case ADC_MODE_ULP2:
  149. rate = SWR_CLK_RATE_0P6MHZ;
  150. break;
  151. case ADC_MODE_ULP1:
  152. rate = SWR_CLK_RATE_1P2MHZ;
  153. break;
  154. case ADC_MODE_LP:
  155. rate = SWR_CLK_RATE_4P8MHZ;
  156. break;
  157. case ADC_MODE_NORMAL:
  158. case ADC_MODE_LO_HIF:
  159. case ADC_MODE_HIFI:
  160. case ADC_MODE_INVALID:
  161. default:
  162. rate = SWR_CLK_RATE_9P6MHZ;
  163. break;
  164. }
  165. return rate;
  166. }
  167. static int wcd938x_set_swr_clk_rate(struct snd_soc_component *component,
  168. int rate, int bank)
  169. {
  170. u8 mask = (bank ? 0xF0 : 0x0F);
  171. u8 val = 0;
  172. switch (rate) {
  173. case SWR_CLK_RATE_0P6MHZ:
  174. val = (bank ? 0x60 : 0x06);
  175. break;
  176. case SWR_CLK_RATE_1P2MHZ:
  177. val = (bank ? 0x50 : 0x05);
  178. break;
  179. case SWR_CLK_RATE_2P4MHZ:
  180. val = (bank ? 0x30 : 0x03);
  181. break;
  182. case SWR_CLK_RATE_4P8MHZ:
  183. val = (bank ? 0x10 : 0x01);
  184. break;
  185. case SWR_CLK_RATE_9P6MHZ:
  186. default:
  187. val = 0x00;
  188. break;
  189. }
  190. snd_soc_component_update_bits(component,
  191. WCD938X_DIGITAL_SWR_TX_CLK_RATE,
  192. mask, val);
  193. return 0;
  194. }
  195. static int wcd938x_init_reg(struct snd_soc_component *component)
  196. {
  197. snd_soc_component_update_bits(component, WCD938X_SLEEP_CTL, 0x0E, 0x0E);
  198. snd_soc_component_update_bits(component, WCD938X_SLEEP_CTL, 0x80, 0x80);
  199. /* 1 msec delay as per HW requirement */
  200. usleep_range(1000, 1010);
  201. snd_soc_component_update_bits(component, WCD938X_SLEEP_CTL, 0x40, 0x40);
  202. /* 1 msec delay as per HW requirement */
  203. usleep_range(1000, 1010);
  204. snd_soc_component_update_bits(component, WCD938X_LDORXTX_CONFIG,
  205. 0x10, 0x00);
  206. snd_soc_component_update_bits(component, WCD938X_BIAS_VBG_FINE_ADJ,
  207. 0xF0, 0x80);
  208. snd_soc_component_update_bits(component, WCD938X_ANA_BIAS, 0x80, 0x80);
  209. snd_soc_component_update_bits(component, WCD938X_ANA_BIAS, 0x40, 0x40);
  210. /* 10 msec delay as per HW requirement */
  211. usleep_range(10000, 10010);
  212. snd_soc_component_update_bits(component, WCD938X_ANA_BIAS, 0x40, 0x00);
  213. snd_soc_component_update_bits(component,
  214. WCD938X_HPH_NEW_INT_RDAC_GAIN_CTL,
  215. 0xF0, 0x00);
  216. snd_soc_component_update_bits(component,
  217. WCD938X_HPH_NEW_INT_RDAC_HD2_CTL_L_NEW,
  218. 0x1F, 0x15);
  219. snd_soc_component_update_bits(component,
  220. WCD938X_HPH_NEW_INT_RDAC_HD2_CTL_R_NEW,
  221. 0x1F, 0x15);
  222. snd_soc_component_update_bits(component, WCD938X_HPH_REFBUFF_UHQA_CTL,
  223. 0xC0, 0x80);
  224. snd_soc_component_update_bits(component, WCD938X_DIGITAL_CDC_DMIC_CTL,
  225. 0x02, 0x02);
  226. snd_soc_component_update_bits(component,
  227. WCD938X_TX_COM_NEW_INT_TXFE_ICTRL_STG2CASC_ULP,
  228. 0xFF, 0x14);
  229. snd_soc_component_update_bits(component,
  230. WCD938X_TX_COM_NEW_INT_TXFE_ICTRL_STG2MAIN_ULP,
  231. 0x1F, 0x08);
  232. snd_soc_component_update_bits(component,
  233. WCD938X_DIGITAL_TX_REQ_FB_CTL_0, 0xFF, 0x55);
  234. snd_soc_component_update_bits(component,
  235. WCD938X_DIGITAL_TX_REQ_FB_CTL_1, 0xFF, 0x44);
  236. snd_soc_component_update_bits(component,
  237. WCD938X_DIGITAL_TX_REQ_FB_CTL_2, 0xFF, 0x11);
  238. snd_soc_component_update_bits(component,
  239. WCD938X_DIGITAL_TX_REQ_FB_CTL_3, 0xFF, 0x00);
  240. snd_soc_component_update_bits(component,
  241. WCD938X_DIGITAL_TX_REQ_FB_CTL_4, 0xFF, 0x00);
  242. snd_soc_component_update_bits(component,
  243. WCD938X_MICB1_TEST_CTL_1, 0xE0, 0xE0);
  244. snd_soc_component_update_bits(component,
  245. WCD938X_MICB2_TEST_CTL_1, 0xE0, 0xE0);
  246. snd_soc_component_update_bits(component,
  247. WCD938X_MICB3_TEST_CTL_1, 0xE0, 0xE0);
  248. snd_soc_component_update_bits(component,
  249. WCD938X_MICB4_TEST_CTL_1, 0xE0, 0xE0);
  250. snd_soc_component_update_bits(component,
  251. WCD938X_TX_3_4_TEST_BLK_EN2, 0x01, 0x00);
  252. snd_soc_component_update_bits(component, WCD938X_SLEEP_CTL, 0x0E,
  253. ((snd_soc_component_read32(component,
  254. WCD938X_DIGITAL_EFUSE_REG_30) & 0x07) << 1));
  255. snd_soc_component_update_bits(component,
  256. WCD938X_HPH_SURGE_HPHLR_SURGE_EN, 0xC0, 0xC0);
  257. return 0;
  258. }
  259. static int wcd938x_set_port_params(struct snd_soc_component *component,
  260. u8 slv_prt_type, u8 *port_id, u8 *num_ch,
  261. u8 *ch_mask, u32 *ch_rate,
  262. u8 *port_type, u8 path)
  263. {
  264. int i, j;
  265. u8 num_ports = 0;
  266. struct codec_port_info (*map)[MAX_PORT][MAX_CH_PER_PORT];
  267. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  268. switch (path) {
  269. case CODEC_RX:
  270. map = &wcd938x->rx_port_mapping;
  271. num_ports = wcd938x->num_rx_ports;
  272. break;
  273. case CODEC_TX:
  274. map = &wcd938x->tx_port_mapping;
  275. num_ports = wcd938x->num_tx_ports;
  276. break;
  277. default:
  278. dev_err(component->dev, "%s Invalid path selected %u\n",
  279. __func__, path);
  280. return -EINVAL;
  281. }
  282. for (i = 0; i <= num_ports; i++) {
  283. for (j = 0; j < MAX_CH_PER_PORT; j++) {
  284. if ((*map)[i][j].slave_port_type == slv_prt_type)
  285. goto found;
  286. }
  287. }
  288. found:
  289. if (i > num_ports || j == MAX_CH_PER_PORT) {
  290. dev_err(component->dev, "%s Failed to find slave port for type %u\n",
  291. __func__, slv_prt_type);
  292. return -EINVAL;
  293. }
  294. *port_id = i;
  295. *num_ch = (*map)[i][j].num_ch;
  296. *ch_mask = (*map)[i][j].ch_mask;
  297. *ch_rate = (*map)[i][j].ch_rate;
  298. *port_type = (*map)[i][j].master_port_type;
  299. return 0;
  300. }
  301. static int wcd938x_parse_port_mapping(struct device *dev,
  302. char *prop, u8 path)
  303. {
  304. u32 *dt_array, map_size, map_length;
  305. u32 port_num = 0, ch_mask, ch_rate, old_port_num = 0;
  306. u32 slave_port_type, master_port_type;
  307. u32 i, ch_iter = 0;
  308. int ret = 0;
  309. u8 *num_ports = NULL;
  310. struct codec_port_info (*map)[MAX_PORT][MAX_CH_PER_PORT];
  311. struct wcd938x_priv *wcd938x = dev_get_drvdata(dev);
  312. switch (path) {
  313. case CODEC_RX:
  314. map = &wcd938x->rx_port_mapping;
  315. num_ports = &wcd938x->num_rx_ports;
  316. break;
  317. case CODEC_TX:
  318. map = &wcd938x->tx_port_mapping;
  319. num_ports = &wcd938x->num_tx_ports;
  320. break;
  321. default:
  322. dev_err(dev, "%s Invalid path selected %u\n",
  323. __func__, path);
  324. return -EINVAL;
  325. }
  326. if (!of_find_property(dev->of_node, prop,
  327. &map_size)) {
  328. dev_err(dev, "missing port mapping prop %s\n", prop);
  329. ret = -EINVAL;
  330. goto err_port_map;
  331. }
  332. map_length = map_size / (NUM_SWRS_DT_PARAMS * sizeof(u32));
  333. dt_array = kzalloc(map_size, GFP_KERNEL);
  334. if (!dt_array) {
  335. ret = -ENOMEM;
  336. goto err_alloc;
  337. }
  338. ret = of_property_read_u32_array(dev->of_node, prop, dt_array,
  339. NUM_SWRS_DT_PARAMS * map_length);
  340. if (ret) {
  341. dev_err(dev, "%s: Failed to read port mapping from prop %s\n",
  342. __func__, prop);
  343. goto err_pdata_fail;
  344. }
  345. for (i = 0; i < map_length; i++) {
  346. port_num = dt_array[NUM_SWRS_DT_PARAMS * i];
  347. slave_port_type = dt_array[NUM_SWRS_DT_PARAMS * i + 1];
  348. ch_mask = dt_array[NUM_SWRS_DT_PARAMS * i + 2];
  349. ch_rate = dt_array[NUM_SWRS_DT_PARAMS * i + 3];
  350. master_port_type = dt_array[NUM_SWRS_DT_PARAMS * i + 4];
  351. if (port_num != old_port_num)
  352. ch_iter = 0;
  353. (*map)[port_num][ch_iter].slave_port_type = slave_port_type;
  354. (*map)[port_num][ch_iter].ch_mask = ch_mask;
  355. (*map)[port_num][ch_iter].master_port_type = master_port_type;
  356. (*map)[port_num][ch_iter].num_ch = __sw_hweight8(ch_mask);
  357. (*map)[port_num][ch_iter++].ch_rate = ch_rate;
  358. old_port_num = port_num;
  359. }
  360. *num_ports = port_num;
  361. kfree(dt_array);
  362. return 0;
  363. err_pdata_fail:
  364. kfree(dt_array);
  365. err_alloc:
  366. err_port_map:
  367. return ret;
  368. }
  369. static int wcd938x_tx_connect_port(struct snd_soc_component *component,
  370. u8 slv_port_type, int clk_rate,
  371. u8 enable)
  372. {
  373. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  374. u8 port_id, num_ch, ch_mask;
  375. u8 ch_type = 0;
  376. u32 ch_rate;
  377. int slave_ch_idx;
  378. u8 num_port = 1;
  379. int ret = 0;
  380. ret = wcd938x_set_port_params(component, slv_port_type, &port_id,
  381. &num_ch, &ch_mask, &ch_rate,
  382. &ch_type, CODEC_TX);
  383. if (ret)
  384. return ret;
  385. if (clk_rate)
  386. ch_rate = clk_rate;
  387. slave_ch_idx = wcd938x_slave_get_slave_ch_val(slv_port_type);
  388. if (slave_ch_idx != -EINVAL)
  389. ch_type = wcd938x->tx_master_ch_map[slave_ch_idx];
  390. dev_dbg(component->dev, "%s slv_ch_idx: %d, mstr_ch_type: %d\n",
  391. __func__, slave_ch_idx, ch_type);
  392. if (enable)
  393. ret = swr_connect_port(wcd938x->tx_swr_dev, &port_id,
  394. num_port, &ch_mask, &ch_rate,
  395. &num_ch, &ch_type);
  396. else
  397. ret = swr_disconnect_port(wcd938x->tx_swr_dev, &port_id,
  398. num_port, &ch_mask, &ch_type);
  399. return ret;
  400. }
  401. static int wcd938x_rx_connect_port(struct snd_soc_component *component,
  402. u8 slv_port_type, u8 enable)
  403. {
  404. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  405. u8 port_id, num_ch, ch_mask, port_type;
  406. u32 ch_rate;
  407. u8 num_port = 1;
  408. int ret = 0;
  409. ret = wcd938x_set_port_params(component, slv_port_type, &port_id,
  410. &num_ch, &ch_mask, &ch_rate,
  411. &port_type, CODEC_RX);
  412. if (ret)
  413. return ret;
  414. if (enable)
  415. ret = swr_connect_port(wcd938x->rx_swr_dev, &port_id,
  416. num_port, &ch_mask, &ch_rate,
  417. &num_ch, &port_type);
  418. else
  419. ret = swr_disconnect_port(wcd938x->rx_swr_dev, &port_id,
  420. num_port, &ch_mask, &port_type);
  421. return ret;
  422. }
  423. static int wcd938x_rx_clk_enable(struct snd_soc_component *component)
  424. {
  425. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  426. if (wcd938x->rx_clk_cnt == 0) {
  427. snd_soc_component_update_bits(component,
  428. WCD938X_DIGITAL_CDC_ANA_CLK_CTL, 0x01, 0x01);
  429. snd_soc_component_update_bits(component,
  430. WCD938X_ANA_RX_SUPPLIES, 0x01, 0x01);
  431. snd_soc_component_update_bits(component,
  432. WCD938X_DIGITAL_CDC_RX0_CTL, 0x40, 0x00);
  433. snd_soc_component_update_bits(component,
  434. WCD938X_DIGITAL_CDC_RX1_CTL, 0x40, 0x00);
  435. snd_soc_component_update_bits(component,
  436. WCD938X_DIGITAL_CDC_RX2_CTL, 0x40, 0x00);
  437. snd_soc_component_update_bits(component,
  438. WCD938X_DIGITAL_CDC_ANA_CLK_CTL, 0x02, 0x02);
  439. snd_soc_component_update_bits(component,
  440. WCD938X_AUX_AUXPA, 0x10, 0x10);
  441. }
  442. wcd938x->rx_clk_cnt++;
  443. return 0;
  444. }
  445. static int wcd938x_rx_clk_disable(struct snd_soc_component *component)
  446. {
  447. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  448. wcd938x->rx_clk_cnt--;
  449. if (wcd938x->rx_clk_cnt == 0) {
  450. snd_soc_component_update_bits(component,
  451. WCD938X_ANA_RX_SUPPLIES, 0x40, 0x00);
  452. snd_soc_component_update_bits(component,
  453. WCD938X_ANA_RX_SUPPLIES, 0x80, 0x00);
  454. snd_soc_component_update_bits(component,
  455. WCD938X_ANA_RX_SUPPLIES, 0x01, 0x00);
  456. snd_soc_component_update_bits(component,
  457. WCD938X_DIGITAL_CDC_ANA_CLK_CTL, 0x02, 0x00);
  458. snd_soc_component_update_bits(component,
  459. WCD938X_DIGITAL_CDC_ANA_CLK_CTL, 0x01, 0x00);
  460. }
  461. return 0;
  462. }
  463. /*
  464. * wcd938x_soc_get_mbhc: get wcd938x_mbhc handle of corresponding component
  465. * @component: handle to snd_soc_component *
  466. *
  467. * return wcd938x_mbhc handle or error code in case of failure
  468. */
  469. struct wcd938x_mbhc *wcd938x_soc_get_mbhc(struct snd_soc_component *component)
  470. {
  471. struct wcd938x_priv *wcd938x;
  472. if (!component) {
  473. pr_err("%s: Invalid params, NULL component\n", __func__);
  474. return NULL;
  475. }
  476. wcd938x = snd_soc_component_get_drvdata(component);
  477. if (!wcd938x) {
  478. pr_err("%s: wcd938x is NULL\n", __func__);
  479. return NULL;
  480. }
  481. return wcd938x->mbhc;
  482. }
  483. EXPORT_SYMBOL(wcd938x_soc_get_mbhc);
  484. static int wcd938x_codec_hphl_dac_event(struct snd_soc_dapm_widget *w,
  485. struct snd_kcontrol *kcontrol,
  486. int event)
  487. {
  488. struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
  489. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  490. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  491. w->name, event);
  492. switch (event) {
  493. case SND_SOC_DAPM_PRE_PMU:
  494. wcd938x_rx_clk_enable(component);
  495. snd_soc_component_update_bits(component,
  496. WCD938X_DIGITAL_CDC_DIG_CLK_CTL, 0x01, 0x01);
  497. snd_soc_component_update_bits(component,
  498. WCD938X_DIGITAL_CDC_HPH_GAIN_CTL, 0x04, 0x04);
  499. snd_soc_component_update_bits(component,
  500. WCD938X_HPH_RDAC_CLK_CTL1, 0x80, 0x00);
  501. break;
  502. case SND_SOC_DAPM_POST_PMU:
  503. snd_soc_component_update_bits(component,
  504. WCD938X_HPH_NEW_INT_RDAC_HD2_CTL_L, 0x0F, 0x02);
  505. if (wcd938x->comp1_enable) {
  506. snd_soc_component_update_bits(component,
  507. WCD938X_DIGITAL_CDC_COMP_CTL_0, 0x02, 0x02);
  508. /* 5msec compander delay as per HW requirement */
  509. if (!wcd938x->comp2_enable ||
  510. (snd_soc_component_read32(component,
  511. WCD938X_DIGITAL_CDC_COMP_CTL_0) & 0x01))
  512. usleep_range(5000, 5010);
  513. snd_soc_component_update_bits(component,
  514. WCD938X_HPH_NEW_INT_HPH_TIMER1, 0x02, 0x00);
  515. } else {
  516. snd_soc_component_update_bits(component,
  517. WCD938X_DIGITAL_CDC_COMP_CTL_0,
  518. 0x02, 0x00);
  519. snd_soc_component_update_bits(component,
  520. WCD938X_HPH_L_EN, 0x20, 0x20);
  521. }
  522. break;
  523. case SND_SOC_DAPM_POST_PMD:
  524. snd_soc_component_update_bits(component,
  525. WCD938X_HPH_NEW_INT_RDAC_HD2_CTL_R,
  526. 0x0F, 0x01);
  527. break;
  528. }
  529. return 0;
  530. }
  531. static int wcd938x_codec_hphr_dac_event(struct snd_soc_dapm_widget *w,
  532. struct snd_kcontrol *kcontrol,
  533. int event)
  534. {
  535. struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
  536. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  537. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  538. w->name, event);
  539. switch (event) {
  540. case SND_SOC_DAPM_PRE_PMU:
  541. wcd938x_rx_clk_enable(component);
  542. snd_soc_component_update_bits(component,
  543. WCD938X_DIGITAL_CDC_DIG_CLK_CTL, 0x02, 0x02);
  544. snd_soc_component_update_bits(component,
  545. WCD938X_DIGITAL_CDC_HPH_GAIN_CTL, 0x08, 0x08);
  546. snd_soc_component_update_bits(component,
  547. WCD938X_HPH_RDAC_CLK_CTL1, 0x80, 0x00);
  548. break;
  549. case SND_SOC_DAPM_POST_PMU:
  550. snd_soc_component_update_bits(component,
  551. WCD938X_HPH_NEW_INT_RDAC_HD2_CTL_R, 0x0F, 0x02);
  552. if (wcd938x->comp2_enable) {
  553. snd_soc_component_update_bits(component,
  554. WCD938X_DIGITAL_CDC_COMP_CTL_0, 0x01, 0x01);
  555. /* 5msec compander delay as per HW requirement */
  556. if (!wcd938x->comp1_enable ||
  557. (snd_soc_component_read32(component,
  558. WCD938X_DIGITAL_CDC_COMP_CTL_0) & 0x02))
  559. usleep_range(5000, 5010);
  560. snd_soc_component_update_bits(component,
  561. WCD938X_HPH_NEW_INT_HPH_TIMER1, 0x02, 0x00);
  562. } else {
  563. snd_soc_component_update_bits(component,
  564. WCD938X_DIGITAL_CDC_COMP_CTL_0,
  565. 0x01, 0x00);
  566. snd_soc_component_update_bits(component,
  567. WCD938X_HPH_R_EN, 0x20, 0x20);
  568. }
  569. break;
  570. case SND_SOC_DAPM_POST_PMD:
  571. snd_soc_component_update_bits(component,
  572. WCD938X_HPH_NEW_INT_RDAC_HD2_CTL_R,
  573. 0x0F, 0x01);
  574. break;
  575. }
  576. return 0;
  577. }
  578. static int wcd938x_codec_ear_dac_event(struct snd_soc_dapm_widget *w,
  579. struct snd_kcontrol *kcontrol,
  580. int event)
  581. {
  582. struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
  583. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  584. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  585. w->name, event);
  586. switch (event) {
  587. case SND_SOC_DAPM_PRE_PMU:
  588. wcd938x_rx_clk_enable(component);
  589. wcd938x->ear_rx_path =
  590. snd_soc_component_read32(
  591. component, WCD938X_DIGITAL_CDC_EAR_PATH_CTL);
  592. if (wcd938x->ear_rx_path & EAR_RX_PATH_AUX) {
  593. snd_soc_component_update_bits(component,
  594. WCD938X_EAR_EAR_DAC_CON, 0x80, 0x00);
  595. snd_soc_component_update_bits(component,
  596. WCD938X_DIGITAL_CDC_AUX_GAIN_CTL, 0x01, 0x01);
  597. snd_soc_component_update_bits(component,
  598. WCD938X_DIGITAL_CDC_DIG_CLK_CTL, 0x04, 0x04);
  599. snd_soc_component_update_bits(component,
  600. WCD938X_ANA_EAR_COMPANDER_CTL, 0x80, 0x80);
  601. } else {
  602. snd_soc_component_update_bits(component,
  603. WCD938X_DIGITAL_CDC_HPH_GAIN_CTL, 0x04, 0x04);
  604. snd_soc_component_update_bits(component,
  605. WCD938X_DIGITAL_CDC_DIG_CLK_CTL, 0x01, 0x01);
  606. if (wcd938x->comp1_enable)
  607. snd_soc_component_update_bits(component,
  608. WCD938X_DIGITAL_CDC_COMP_CTL_0,
  609. 0x02, 0x02);
  610. }
  611. /* 5 msec delay as per HW requirement */
  612. usleep_range(5000, 5010);
  613. if (wcd938x->flyback_cur_det_disable == 0)
  614. snd_soc_component_update_bits(component,
  615. WCD938X_FLYBACK_EN,
  616. 0x04, 0x00);
  617. wcd938x->flyback_cur_det_disable++;
  618. wcd_cls_h_fsm(component, &wcd938x->clsh_info,
  619. WCD_CLSH_EVENT_PRE_DAC,
  620. WCD_CLSH_STATE_EAR,
  621. wcd938x->hph_mode);
  622. break;
  623. case SND_SOC_DAPM_POST_PMD:
  624. if (wcd938x->ear_rx_path & EAR_RX_PATH_AUX) {
  625. snd_soc_component_update_bits(component,
  626. WCD938X_DIGITAL_CDC_AUX_GAIN_CTL, 0x01, 0x00);
  627. snd_soc_component_update_bits(component,
  628. WCD938X_DIGITAL_CDC_DIG_CLK_CTL, 0x04, 0x00);
  629. } else {
  630. snd_soc_component_update_bits(component,
  631. WCD938X_DIGITAL_CDC_HPH_GAIN_CTL, 0x04, 0x00);
  632. snd_soc_component_update_bits(component,
  633. WCD938X_DIGITAL_CDC_DIG_CLK_CTL, 0x01, 0x00);
  634. if (wcd938x->comp1_enable)
  635. snd_soc_component_update_bits(component,
  636. WCD938X_DIGITAL_CDC_COMP_CTL_0,
  637. 0x02, 0x00);
  638. }
  639. snd_soc_component_update_bits(component,
  640. WCD938X_ANA_EAR_COMPANDER_CTL, 0x80, 0x00);
  641. snd_soc_component_update_bits(component,
  642. WCD938X_EAR_EAR_DAC_CON, 0x80, 0x80);
  643. break;
  644. };
  645. return 0;
  646. }
  647. static int wcd938x_codec_aux_dac_event(struct snd_soc_dapm_widget *w,
  648. struct snd_kcontrol *kcontrol,
  649. int event)
  650. {
  651. struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
  652. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  653. int ret = 0;
  654. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  655. w->name, event);
  656. switch (event) {
  657. case SND_SOC_DAPM_PRE_PMU:
  658. wcd938x_rx_clk_enable(component);
  659. snd_soc_component_update_bits(component,
  660. WCD938X_DIGITAL_CDC_ANA_CLK_CTL, 0x04, 0x04);
  661. snd_soc_component_update_bits(component,
  662. WCD938X_DIGITAL_CDC_DIG_CLK_CTL, 0x04, 0x04);
  663. snd_soc_component_update_bits(component,
  664. WCD938X_DIGITAL_CDC_AUX_GAIN_CTL, 0x01, 0x01);
  665. if (wcd938x->flyback_cur_det_disable == 0)
  666. snd_soc_component_update_bits(component,
  667. WCD938X_FLYBACK_EN,
  668. 0x04, 0x00);
  669. wcd938x->flyback_cur_det_disable++;
  670. wcd_cls_h_fsm(component, &wcd938x->clsh_info,
  671. WCD_CLSH_EVENT_PRE_DAC,
  672. WCD_CLSH_STATE_AUX,
  673. wcd938x->hph_mode);
  674. break;
  675. case SND_SOC_DAPM_POST_PMD:
  676. snd_soc_component_update_bits(component,
  677. WCD938X_DIGITAL_CDC_ANA_CLK_CTL, 0x04, 0x00);
  678. break;
  679. };
  680. return ret;
  681. }
  682. static int wcd938x_codec_enable_hphr_pa(struct snd_soc_dapm_widget *w,
  683. struct snd_kcontrol *kcontrol,
  684. int event)
  685. {
  686. struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
  687. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  688. int ret = 0;
  689. int hph_mode = wcd938x->hph_mode;
  690. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  691. w->name, event);
  692. switch (event) {
  693. case SND_SOC_DAPM_PRE_PMU:
  694. if (wcd938x->ldoh)
  695. snd_soc_component_update_bits(component,
  696. WCD938X_LDOH_MODE,
  697. 0x80, 0x80);
  698. if (wcd938x->update_wcd_event)
  699. wcd938x->update_wcd_event(wcd938x->handle,
  700. WCD_BOLERO_EVT_RX_MUTE,
  701. (WCD_RX2 << 0x10 | 0x1));
  702. ret = swr_slvdev_datapath_control(wcd938x->rx_swr_dev,
  703. wcd938x->rx_swr_dev->dev_num,
  704. true);
  705. wcd_cls_h_fsm(component, &wcd938x->clsh_info,
  706. WCD_CLSH_EVENT_PRE_DAC,
  707. WCD_CLSH_STATE_HPHR,
  708. hph_mode);
  709. wcd_clsh_set_hph_mode(component, CLS_H_HIFI);
  710. if (hph_mode == CLS_H_LP || hph_mode == CLS_H_LOHIFI ||
  711. hph_mode == CLS_H_ULP) {
  712. snd_soc_component_update_bits(component,
  713. WCD938X_HPH_REFBUFF_LP_CTL, 0x01, 0x01);
  714. }
  715. snd_soc_component_update_bits(component, WCD938X_ANA_HPH,
  716. 0x10, 0x10);
  717. wcd_clsh_set_hph_mode(component, hph_mode);
  718. /* 100 usec delay as per HW requirement */
  719. usleep_range(100, 110);
  720. set_bit(HPH_PA_DELAY, &wcd938x->status_mask);
  721. snd_soc_component_update_bits(component,
  722. WCD938X_DIGITAL_PDM_WD_CTL1, 0x17, 0x13);
  723. break;
  724. case SND_SOC_DAPM_POST_PMU:
  725. /*
  726. * 7ms sleep is required if compander is enabled as per
  727. * HW requirement. If compander is disabled, then
  728. * 20ms delay is required.
  729. */
  730. if (test_bit(HPH_PA_DELAY, &wcd938x->status_mask)) {
  731. if (!wcd938x->comp2_enable)
  732. usleep_range(20000, 20100);
  733. else
  734. usleep_range(7000, 7100);
  735. if (hph_mode == CLS_H_LP ||
  736. hph_mode == CLS_H_LOHIFI ||
  737. hph_mode == CLS_H_ULP)
  738. snd_soc_component_update_bits(component,
  739. WCD938X_HPH_REFBUFF_LP_CTL, 0x01,
  740. 0x00);
  741. clear_bit(HPH_PA_DELAY, &wcd938x->status_mask);
  742. }
  743. snd_soc_component_update_bits(component,
  744. WCD938X_HPH_NEW_INT_HPH_TIMER1, 0x02, 0x02);
  745. if (hph_mode == CLS_AB || hph_mode == CLS_AB_HIFI ||
  746. hph_mode == CLS_AB_LP || hph_mode == CLS_AB_LOHIFI)
  747. snd_soc_component_update_bits(component,
  748. WCD938X_ANA_RX_SUPPLIES, 0x02, 0x02);
  749. if (wcd938x->update_wcd_event)
  750. wcd938x->update_wcd_event(wcd938x->handle,
  751. WCD_BOLERO_EVT_RX_MUTE,
  752. (WCD_RX2 << 0x10));
  753. wcd_enable_irq(&wcd938x->irq_info,
  754. WCD938X_IRQ_HPHR_PDM_WD_INT);
  755. break;
  756. case SND_SOC_DAPM_PRE_PMD:
  757. if (wcd938x->update_wcd_event)
  758. wcd938x->update_wcd_event(wcd938x->handle,
  759. WCD_BOLERO_EVT_RX_MUTE,
  760. (WCD_RX2 << 0x10 | 0x1));
  761. wcd_disable_irq(&wcd938x->irq_info,
  762. WCD938X_IRQ_HPHR_PDM_WD_INT);
  763. if (wcd938x->update_wcd_event && wcd938x->comp2_enable)
  764. wcd938x->update_wcd_event(wcd938x->handle,
  765. WCD_BOLERO_EVT_RX_COMPANDER_SOFT_RST,
  766. (WCD_RX2 << 0x10));
  767. /*
  768. * 7ms sleep is required if compander is enabled as per
  769. * HW requirement. If compander is disabled, then
  770. * 20ms delay is required.
  771. */
  772. if (!wcd938x->comp2_enable)
  773. usleep_range(20000, 20100);
  774. else
  775. usleep_range(7000, 7100);
  776. snd_soc_component_update_bits(component, WCD938X_ANA_HPH,
  777. 0x40, 0x00);
  778. blocking_notifier_call_chain(&wcd938x->mbhc->notifier,
  779. WCD_EVENT_PRE_HPHR_PA_OFF,
  780. &wcd938x->mbhc->wcd_mbhc);
  781. set_bit(HPH_PA_DELAY, &wcd938x->status_mask);
  782. break;
  783. case SND_SOC_DAPM_POST_PMD:
  784. /*
  785. * 7ms sleep is required if compander is enabled as per
  786. * HW requirement. If compander is disabled, then
  787. * 20ms delay is required.
  788. */
  789. if (test_bit(HPH_PA_DELAY, &wcd938x->status_mask)) {
  790. if (!wcd938x->comp2_enable)
  791. usleep_range(20000, 20100);
  792. else
  793. usleep_range(7000, 7100);
  794. clear_bit(HPH_PA_DELAY, &wcd938x->status_mask);
  795. }
  796. blocking_notifier_call_chain(&wcd938x->mbhc->notifier,
  797. WCD_EVENT_POST_HPHR_PA_OFF,
  798. &wcd938x->mbhc->wcd_mbhc);
  799. snd_soc_component_update_bits(component, WCD938X_ANA_HPH,
  800. 0x10, 0x00);
  801. snd_soc_component_update_bits(component,
  802. WCD938X_DIGITAL_PDM_WD_CTL1, 0x17, 0x00);
  803. wcd_cls_h_fsm(component, &wcd938x->clsh_info,
  804. WCD_CLSH_EVENT_POST_PA,
  805. WCD_CLSH_STATE_HPHR,
  806. hph_mode);
  807. if (wcd938x->ldoh)
  808. snd_soc_component_update_bits(component,
  809. WCD938X_LDOH_MODE,
  810. 0x80, 0x00);
  811. break;
  812. };
  813. return ret;
  814. }
  815. static int wcd938x_codec_enable_hphl_pa(struct snd_soc_dapm_widget *w,
  816. struct snd_kcontrol *kcontrol,
  817. int event)
  818. {
  819. struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
  820. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  821. int ret = 0;
  822. int hph_mode = wcd938x->hph_mode;
  823. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  824. w->name, event);
  825. switch (event) {
  826. case SND_SOC_DAPM_PRE_PMU:
  827. if (wcd938x->ldoh)
  828. snd_soc_component_update_bits(component,
  829. WCD938X_LDOH_MODE,
  830. 0x80, 0x80);
  831. if (wcd938x->update_wcd_event)
  832. wcd938x->update_wcd_event(wcd938x->handle,
  833. WCD_BOLERO_EVT_RX_MUTE,
  834. (WCD_RX1 << 0x10 | 0x01));
  835. ret = swr_slvdev_datapath_control(wcd938x->rx_swr_dev,
  836. wcd938x->rx_swr_dev->dev_num,
  837. true);
  838. wcd_cls_h_fsm(component, &wcd938x->clsh_info,
  839. WCD_CLSH_EVENT_PRE_DAC,
  840. WCD_CLSH_STATE_HPHL,
  841. hph_mode);
  842. wcd_clsh_set_hph_mode(component, CLS_H_HIFI);
  843. if (hph_mode == CLS_H_LP || hph_mode == CLS_H_LOHIFI ||
  844. hph_mode == CLS_H_ULP) {
  845. snd_soc_component_update_bits(component,
  846. WCD938X_HPH_REFBUFF_LP_CTL, 0x01, 0x01);
  847. }
  848. snd_soc_component_update_bits(component, WCD938X_ANA_HPH,
  849. 0x20, 0x20);
  850. wcd_clsh_set_hph_mode(component, hph_mode);
  851. /* 100 usec delay as per HW requirement */
  852. usleep_range(100, 110);
  853. set_bit(HPH_PA_DELAY, &wcd938x->status_mask);
  854. snd_soc_component_update_bits(component,
  855. WCD938X_DIGITAL_PDM_WD_CTL0, 0x17, 0x13);
  856. break;
  857. case SND_SOC_DAPM_POST_PMU:
  858. /*
  859. * 7ms sleep is required if compander is enabled as per
  860. * HW requirement. If compander is disabled, then
  861. * 20ms delay is required.
  862. */
  863. if (test_bit(HPH_PA_DELAY, &wcd938x->status_mask)) {
  864. if (!wcd938x->comp1_enable)
  865. usleep_range(20000, 20100);
  866. else
  867. usleep_range(7000, 7100);
  868. if (hph_mode == CLS_H_LP ||
  869. hph_mode == CLS_H_LOHIFI ||
  870. hph_mode == CLS_H_ULP)
  871. snd_soc_component_update_bits(component,
  872. WCD938X_HPH_REFBUFF_LP_CTL,
  873. 0x01, 0x00);
  874. clear_bit(HPH_PA_DELAY, &wcd938x->status_mask);
  875. }
  876. snd_soc_component_update_bits(component,
  877. WCD938X_HPH_NEW_INT_HPH_TIMER1, 0x02, 0x02);
  878. if (hph_mode == CLS_AB || hph_mode == CLS_AB_HIFI ||
  879. hph_mode == CLS_AB_LP || hph_mode == CLS_AB_LOHIFI)
  880. snd_soc_component_update_bits(component,
  881. WCD938X_ANA_RX_SUPPLIES, 0x02, 0x02);
  882. if (wcd938x->update_wcd_event)
  883. wcd938x->update_wcd_event(wcd938x->handle,
  884. WCD_BOLERO_EVT_RX_MUTE,
  885. (WCD_RX1 << 0x10));
  886. wcd_enable_irq(&wcd938x->irq_info,
  887. WCD938X_IRQ_HPHL_PDM_WD_INT);
  888. break;
  889. case SND_SOC_DAPM_PRE_PMD:
  890. if (wcd938x->update_wcd_event)
  891. wcd938x->update_wcd_event(wcd938x->handle,
  892. WCD_BOLERO_EVT_RX_MUTE,
  893. (WCD_RX1 << 0x10 | 0x1));
  894. wcd_disable_irq(&wcd938x->irq_info,
  895. WCD938X_IRQ_HPHL_PDM_WD_INT);
  896. if (wcd938x->update_wcd_event && wcd938x->comp1_enable)
  897. wcd938x->update_wcd_event(wcd938x->handle,
  898. WCD_BOLERO_EVT_RX_COMPANDER_SOFT_RST,
  899. (WCD_RX1 << 0x10));
  900. /*
  901. * 7ms sleep is required if compander is enabled as per
  902. * HW requirement. If compander is disabled, then
  903. * 20ms delay is required.
  904. */
  905. if (!wcd938x->comp1_enable)
  906. usleep_range(20000, 20100);
  907. else
  908. usleep_range(7000, 7100);
  909. snd_soc_component_update_bits(component, WCD938X_ANA_HPH,
  910. 0x80, 0x00);
  911. blocking_notifier_call_chain(&wcd938x->mbhc->notifier,
  912. WCD_EVENT_PRE_HPHL_PA_OFF,
  913. &wcd938x->mbhc->wcd_mbhc);
  914. set_bit(HPH_PA_DELAY, &wcd938x->status_mask);
  915. break;
  916. case SND_SOC_DAPM_POST_PMD:
  917. /*
  918. * 7ms sleep is required if compander is enabled as per
  919. * HW requirement. If compander is disabled, then
  920. * 20ms delay is required.
  921. */
  922. if (test_bit(HPH_PA_DELAY, &wcd938x->status_mask)) {
  923. if (!wcd938x->comp1_enable)
  924. usleep_range(21000, 21100);
  925. else
  926. usleep_range(7000, 7100);
  927. clear_bit(HPH_PA_DELAY, &wcd938x->status_mask);
  928. }
  929. blocking_notifier_call_chain(&wcd938x->mbhc->notifier,
  930. WCD_EVENT_POST_HPHL_PA_OFF,
  931. &wcd938x->mbhc->wcd_mbhc);
  932. snd_soc_component_update_bits(component, WCD938X_ANA_HPH,
  933. 0x20, 0x00);
  934. snd_soc_component_update_bits(component,
  935. WCD938X_DIGITAL_PDM_WD_CTL0, 0x17, 0x00);
  936. wcd_cls_h_fsm(component, &wcd938x->clsh_info,
  937. WCD_CLSH_EVENT_POST_PA,
  938. WCD_CLSH_STATE_HPHL,
  939. hph_mode);
  940. if (wcd938x->ldoh)
  941. snd_soc_component_update_bits(component,
  942. WCD938X_LDOH_MODE,
  943. 0x80, 0x00);
  944. break;
  945. };
  946. return ret;
  947. }
  948. static int wcd938x_codec_enable_aux_pa(struct snd_soc_dapm_widget *w,
  949. struct snd_kcontrol *kcontrol,
  950. int event)
  951. {
  952. struct snd_soc_component *component =
  953. snd_soc_dapm_to_component(w->dapm);
  954. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  955. int hph_mode = wcd938x->hph_mode;
  956. int ret = 0;
  957. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  958. w->name, event);
  959. switch (event) {
  960. case SND_SOC_DAPM_PRE_PMU:
  961. ret = swr_slvdev_datapath_control(wcd938x->rx_swr_dev,
  962. wcd938x->rx_swr_dev->dev_num,
  963. true);
  964. snd_soc_component_update_bits(component,
  965. WCD938X_DIGITAL_PDM_WD_CTL2, 0x05, 0x05);
  966. break;
  967. case SND_SOC_DAPM_POST_PMU:
  968. /* 1 msec delay as per HW requirement */
  969. usleep_range(1000, 1010);
  970. if (hph_mode == CLS_AB || hph_mode == CLS_AB_HIFI ||
  971. hph_mode == CLS_AB_LP || hph_mode == CLS_AB_LOHIFI)
  972. snd_soc_component_update_bits(component,
  973. WCD938X_ANA_RX_SUPPLIES,
  974. 0x02, 0x02);
  975. if (wcd938x->update_wcd_event)
  976. wcd938x->update_wcd_event(wcd938x->handle,
  977. WCD_BOLERO_EVT_RX_MUTE,
  978. (WCD_RX3 << 0x10));
  979. wcd_enable_irq(&wcd938x->irq_info, WCD938X_IRQ_AUX_PDM_WD_INT);
  980. break;
  981. case SND_SOC_DAPM_PRE_PMD:
  982. wcd_disable_irq(&wcd938x->irq_info,
  983. WCD938X_IRQ_AUX_PDM_WD_INT);
  984. if (wcd938x->update_wcd_event)
  985. wcd938x->update_wcd_event(wcd938x->handle,
  986. WCD_BOLERO_EVT_RX_MUTE,
  987. (WCD_RX3 << 0x10 | 0x1));
  988. break;
  989. case SND_SOC_DAPM_POST_PMD:
  990. /* 1 msec delay as per HW requirement */
  991. usleep_range(1000, 1010);
  992. snd_soc_component_update_bits(component,
  993. WCD938X_DIGITAL_PDM_WD_CTL2, 0x05, 0x00);
  994. wcd_cls_h_fsm(component, &wcd938x->clsh_info,
  995. WCD_CLSH_EVENT_POST_PA,
  996. WCD_CLSH_STATE_AUX,
  997. hph_mode);
  998. wcd938x->flyback_cur_det_disable--;
  999. if (wcd938x->flyback_cur_det_disable == 0)
  1000. snd_soc_component_update_bits(component,
  1001. WCD938X_FLYBACK_EN,
  1002. 0x04, 0x04);
  1003. break;
  1004. };
  1005. return ret;
  1006. }
  1007. static int wcd938x_codec_enable_ear_pa(struct snd_soc_dapm_widget *w,
  1008. struct snd_kcontrol *kcontrol,
  1009. int event)
  1010. {
  1011. struct snd_soc_component *component =
  1012. snd_soc_dapm_to_component(w->dapm);
  1013. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  1014. int hph_mode = wcd938x->hph_mode;
  1015. int ret = 0;
  1016. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  1017. w->name, event);
  1018. switch (event) {
  1019. case SND_SOC_DAPM_PRE_PMU:
  1020. ret = swr_slvdev_datapath_control(wcd938x->rx_swr_dev,
  1021. wcd938x->rx_swr_dev->dev_num,
  1022. true);
  1023. /*
  1024. * Enable watchdog interrupt for HPHL or AUX
  1025. * depending on mux value
  1026. */
  1027. wcd938x->ear_rx_path =
  1028. snd_soc_component_read32(
  1029. component, WCD938X_DIGITAL_CDC_EAR_PATH_CTL);
  1030. if (wcd938x->ear_rx_path & EAR_RX_PATH_AUX)
  1031. snd_soc_component_update_bits(component,
  1032. WCD938X_DIGITAL_PDM_WD_CTL2,
  1033. 0x05, 0x05);
  1034. else
  1035. snd_soc_component_update_bits(component,
  1036. WCD938X_DIGITAL_PDM_WD_CTL0,
  1037. 0x17, 0x13);
  1038. if (!wcd938x->comp1_enable)
  1039. snd_soc_component_update_bits(component,
  1040. WCD938X_ANA_EAR_COMPANDER_CTL, 0x80, 0x80);
  1041. break;
  1042. case SND_SOC_DAPM_POST_PMU:
  1043. /* 6 msec delay as per HW requirement */
  1044. usleep_range(6000, 6010);
  1045. if (hph_mode == CLS_AB || hph_mode == CLS_AB_HIFI ||
  1046. hph_mode == CLS_AB_LP || hph_mode == CLS_AB_LOHIFI)
  1047. snd_soc_component_update_bits(component,
  1048. WCD938X_ANA_RX_SUPPLIES,
  1049. 0x02, 0x02);
  1050. if (wcd938x->ear_rx_path & EAR_RX_PATH_AUX) {
  1051. if (wcd938x->update_wcd_event)
  1052. wcd938x->update_wcd_event(wcd938x->handle,
  1053. WCD_BOLERO_EVT_RX_MUTE,
  1054. (WCD_RX3 << 0x10));
  1055. wcd_enable_irq(&wcd938x->irq_info,
  1056. WCD938X_IRQ_AUX_PDM_WD_INT);
  1057. } else {
  1058. if (wcd938x->update_wcd_event)
  1059. wcd938x->update_wcd_event(wcd938x->handle,
  1060. WCD_BOLERO_EVT_RX_MUTE,
  1061. (WCD_RX1 << 0x10));
  1062. wcd_enable_irq(&wcd938x->irq_info,
  1063. WCD938X_IRQ_HPHL_PDM_WD_INT);
  1064. }
  1065. break;
  1066. case SND_SOC_DAPM_PRE_PMD:
  1067. if (wcd938x->ear_rx_path & EAR_RX_PATH_AUX) {
  1068. wcd_disable_irq(&wcd938x->irq_info,
  1069. WCD938X_IRQ_AUX_PDM_WD_INT);
  1070. if (wcd938x->update_wcd_event)
  1071. wcd938x->update_wcd_event(wcd938x->handle,
  1072. WCD_BOLERO_EVT_RX_MUTE,
  1073. (WCD_RX3 << 0x10 | 0x1));
  1074. } else {
  1075. wcd_disable_irq(&wcd938x->irq_info,
  1076. WCD938X_IRQ_HPHL_PDM_WD_INT);
  1077. if (wcd938x->update_wcd_event)
  1078. wcd938x->update_wcd_event(wcd938x->handle,
  1079. WCD_BOLERO_EVT_RX_MUTE,
  1080. (WCD_RX1 << 0x10 | 0x1));
  1081. }
  1082. break;
  1083. case SND_SOC_DAPM_POST_PMD:
  1084. if (!wcd938x->comp1_enable)
  1085. snd_soc_component_update_bits(component,
  1086. WCD938X_ANA_EAR_COMPANDER_CTL, 0x80, 0x00);
  1087. /* 7 msec delay as per HW requirement */
  1088. usleep_range(7000, 7010);
  1089. if (wcd938x->ear_rx_path & EAR_RX_PATH_AUX)
  1090. snd_soc_component_update_bits(component,
  1091. WCD938X_DIGITAL_PDM_WD_CTL2,
  1092. 0x05, 0x00);
  1093. else
  1094. snd_soc_component_update_bits(component,
  1095. WCD938X_DIGITAL_PDM_WD_CTL0,
  1096. 0x17, 0x00);
  1097. wcd_cls_h_fsm(component, &wcd938x->clsh_info,
  1098. WCD_CLSH_EVENT_POST_PA,
  1099. WCD_CLSH_STATE_EAR,
  1100. hph_mode);
  1101. wcd938x->flyback_cur_det_disable--;
  1102. if (wcd938x->flyback_cur_det_disable == 0)
  1103. snd_soc_component_update_bits(component,
  1104. WCD938X_FLYBACK_EN,
  1105. 0x04, 0x04);
  1106. break;
  1107. };
  1108. return ret;
  1109. }
  1110. static int wcd938x_enable_clsh(struct snd_soc_dapm_widget *w,
  1111. struct snd_kcontrol *kcontrol,
  1112. int event)
  1113. {
  1114. struct snd_soc_component *component =
  1115. snd_soc_dapm_to_component(w->dapm);
  1116. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  1117. int mode = wcd938x->hph_mode;
  1118. int ret = 0;
  1119. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  1120. w->name, event);
  1121. if (mode == CLS_H_LOHIFI || mode == CLS_H_ULP ||
  1122. mode == CLS_H_HIFI || mode == CLS_H_LP) {
  1123. wcd938x_rx_connect_port(component, CLSH,
  1124. SND_SOC_DAPM_EVENT_ON(event));
  1125. }
  1126. if (SND_SOC_DAPM_EVENT_OFF(event))
  1127. ret = swr_slvdev_datapath_control(
  1128. wcd938x->rx_swr_dev,
  1129. wcd938x->rx_swr_dev->dev_num,
  1130. false);
  1131. return ret;
  1132. }
  1133. static int wcd938x_enable_rx1(struct snd_soc_dapm_widget *w,
  1134. struct snd_kcontrol *kcontrol,
  1135. int event)
  1136. {
  1137. struct snd_soc_component *component =
  1138. snd_soc_dapm_to_component(w->dapm);
  1139. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  1140. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  1141. w->name, event);
  1142. switch (event) {
  1143. case SND_SOC_DAPM_PRE_PMU:
  1144. wcd938x_rx_connect_port(component, HPH_L, true);
  1145. if (wcd938x->comp1_enable)
  1146. wcd938x_rx_connect_port(component, COMP_L, true);
  1147. break;
  1148. case SND_SOC_DAPM_POST_PMD:
  1149. wcd938x_rx_connect_port(component, HPH_L, false);
  1150. if (wcd938x->comp1_enable)
  1151. wcd938x_rx_connect_port(component, COMP_L, false);
  1152. wcd938x_rx_clk_disable(component);
  1153. snd_soc_component_update_bits(component,
  1154. WCD938X_DIGITAL_CDC_DIG_CLK_CTL,
  1155. 0x01, 0x00);
  1156. break;
  1157. };
  1158. return 0;
  1159. }
  1160. static int wcd938x_enable_rx2(struct snd_soc_dapm_widget *w,
  1161. struct snd_kcontrol *kcontrol, int event)
  1162. {
  1163. struct snd_soc_component *component =
  1164. snd_soc_dapm_to_component(w->dapm);
  1165. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  1166. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  1167. w->name, event);
  1168. switch (event) {
  1169. case SND_SOC_DAPM_PRE_PMU:
  1170. wcd938x_rx_connect_port(component, HPH_R, true);
  1171. if (wcd938x->comp2_enable)
  1172. wcd938x_rx_connect_port(component, COMP_R, true);
  1173. break;
  1174. case SND_SOC_DAPM_POST_PMD:
  1175. wcd938x_rx_connect_port(component, HPH_R, false);
  1176. if (wcd938x->comp2_enable)
  1177. wcd938x_rx_connect_port(component, COMP_R, false);
  1178. wcd938x_rx_clk_disable(component);
  1179. snd_soc_component_update_bits(component,
  1180. WCD938X_DIGITAL_CDC_DIG_CLK_CTL,
  1181. 0x02, 0x00);
  1182. break;
  1183. };
  1184. return 0;
  1185. }
  1186. static int wcd938x_enable_rx3(struct snd_soc_dapm_widget *w,
  1187. struct snd_kcontrol *kcontrol,
  1188. int event)
  1189. {
  1190. struct snd_soc_component *component =
  1191. snd_soc_dapm_to_component(w->dapm);
  1192. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  1193. w->name, event);
  1194. switch (event) {
  1195. case SND_SOC_DAPM_PRE_PMU:
  1196. wcd938x_rx_connect_port(component, LO, true);
  1197. break;
  1198. case SND_SOC_DAPM_POST_PMD:
  1199. wcd938x_rx_connect_port(component, LO, false);
  1200. /* 6 msec delay as per HW requirement */
  1201. usleep_range(6000, 6010);
  1202. wcd938x_rx_clk_disable(component);
  1203. snd_soc_component_update_bits(component,
  1204. WCD938X_DIGITAL_CDC_DIG_CLK_CTL, 0x04, 0x00);
  1205. break;
  1206. }
  1207. return 0;
  1208. }
  1209. static int wcd938x_codec_enable_dmic(struct snd_soc_dapm_widget *w,
  1210. struct snd_kcontrol *kcontrol,
  1211. int event)
  1212. {
  1213. struct snd_soc_component *component =
  1214. snd_soc_dapm_to_component(w->dapm);
  1215. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  1216. u16 dmic_clk_reg, dmic_clk_en_reg;
  1217. s32 *dmic_clk_cnt;
  1218. u8 dmic_ctl_shift = 0;
  1219. u8 dmic_clk_shift = 0;
  1220. u8 dmic_clk_mask = 0;
  1221. u16 dmic2_left_en = 0;
  1222. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  1223. w->name, event);
  1224. switch (w->shift) {
  1225. case 0:
  1226. case 1:
  1227. dmic_clk_cnt = &(wcd938x->dmic_0_1_clk_cnt);
  1228. dmic_clk_reg = WCD938X_DIGITAL_CDC_DMIC_RATE_1_2;
  1229. dmic_clk_en_reg = WCD938X_DIGITAL_CDC_DMIC1_CTL;
  1230. dmic_clk_mask = 0x0F;
  1231. dmic_clk_shift = 0x00;
  1232. dmic_ctl_shift = 0x00;
  1233. break;
  1234. case 2:
  1235. dmic2_left_en = WCD938X_DIGITAL_CDC_DMIC2_CTL;
  1236. case 3:
  1237. dmic_clk_cnt = &(wcd938x->dmic_2_3_clk_cnt);
  1238. dmic_clk_reg = WCD938X_DIGITAL_CDC_DMIC_RATE_1_2;
  1239. dmic_clk_en_reg = WCD938X_DIGITAL_CDC_DMIC2_CTL;
  1240. dmic_clk_mask = 0xF0;
  1241. dmic_clk_shift = 0x04;
  1242. dmic_ctl_shift = 0x01;
  1243. break;
  1244. case 4:
  1245. case 5:
  1246. dmic_clk_cnt = &(wcd938x->dmic_4_5_clk_cnt);
  1247. dmic_clk_reg = WCD938X_DIGITAL_CDC_DMIC_RATE_3_4;
  1248. dmic_clk_en_reg = WCD938X_DIGITAL_CDC_DMIC3_CTL;
  1249. dmic_clk_mask = 0x0F;
  1250. dmic_clk_shift = 0x00;
  1251. dmic_ctl_shift = 0x02;
  1252. break;
  1253. case 6:
  1254. case 7:
  1255. dmic_clk_cnt = &(wcd938x->dmic_6_7_clk_cnt);
  1256. dmic_clk_reg = WCD938X_DIGITAL_CDC_DMIC_RATE_3_4;
  1257. dmic_clk_en_reg = WCD938X_DIGITAL_CDC_DMIC4_CTL;
  1258. dmic_clk_mask = 0xF0;
  1259. dmic_clk_shift = 0x04;
  1260. dmic_ctl_shift = 0x03;
  1261. break;
  1262. default:
  1263. dev_err(component->dev, "%s: Invalid DMIC Selection\n",
  1264. __func__);
  1265. return -EINVAL;
  1266. };
  1267. dev_dbg(component->dev, "%s: event %d DMIC%d dmic_clk_cnt %d\n",
  1268. __func__, event, (w->shift +1), *dmic_clk_cnt);
  1269. switch (event) {
  1270. case SND_SOC_DAPM_PRE_PMU:
  1271. snd_soc_component_update_bits(component,
  1272. WCD938X_DIGITAL_CDC_AMIC_CTL,
  1273. (0x01 << dmic_ctl_shift), 0x00);
  1274. /* 250us sleep as per HW requirement */
  1275. usleep_range(250, 260);
  1276. if (dmic2_left_en)
  1277. snd_soc_component_update_bits(component,
  1278. dmic2_left_en, 0x80, 0x80);
  1279. /* Setting DMIC clock rate to 2.4MHz */
  1280. snd_soc_component_update_bits(component,
  1281. dmic_clk_reg, dmic_clk_mask,
  1282. (0x03 << dmic_clk_shift));
  1283. snd_soc_component_update_bits(component,
  1284. dmic_clk_en_reg, 0x08, 0x08);
  1285. /* enable clock scaling */
  1286. snd_soc_component_update_bits(component,
  1287. WCD938X_DIGITAL_CDC_DMIC_CTL, 0x06, 0x06);
  1288. wcd938x_tx_connect_port(component, DMIC0 + (w->shift),
  1289. SWR_CLK_RATE_2P4MHZ, true);
  1290. break;
  1291. case SND_SOC_DAPM_POST_PMD:
  1292. wcd938x_tx_connect_port(component, DMIC0 + (w->shift), 0,
  1293. false);
  1294. snd_soc_component_update_bits(component,
  1295. WCD938X_DIGITAL_CDC_AMIC_CTL,
  1296. (0x01 << dmic_ctl_shift),
  1297. (0x01 << dmic_ctl_shift));
  1298. if (dmic2_left_en)
  1299. snd_soc_component_update_bits(component,
  1300. dmic2_left_en, 0x80, 0x00);
  1301. snd_soc_component_update_bits(component,
  1302. dmic_clk_en_reg, 0x08, 0x00);
  1303. break;
  1304. };
  1305. return 0;
  1306. }
  1307. /*
  1308. * wcd938x_get_micb_vout_ctl_val: converts micbias from volts to register value
  1309. * @micb_mv: micbias in mv
  1310. *
  1311. * return register value converted
  1312. */
  1313. int wcd938x_get_micb_vout_ctl_val(u32 micb_mv)
  1314. {
  1315. /* min micbias voltage is 1V and maximum is 2.85V */
  1316. if (micb_mv < 1000 || micb_mv > 2850) {
  1317. pr_err("%s: unsupported micbias voltage\n", __func__);
  1318. return -EINVAL;
  1319. }
  1320. return (micb_mv - 1000) / 50;
  1321. }
  1322. EXPORT_SYMBOL(wcd938x_get_micb_vout_ctl_val);
  1323. /*
  1324. * wcd938x_mbhc_micb_adjust_voltage: adjust specific micbias voltage
  1325. * @component: handle to snd_soc_component *
  1326. * @req_volt: micbias voltage to be set
  1327. * @micb_num: micbias to be set, e.g. micbias1 or micbias2
  1328. *
  1329. * return 0 if adjustment is success or error code in case of failure
  1330. */
  1331. int wcd938x_mbhc_micb_adjust_voltage(struct snd_soc_component *component,
  1332. int req_volt, int micb_num)
  1333. {
  1334. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  1335. int cur_vout_ctl, req_vout_ctl;
  1336. int micb_reg, micb_val, micb_en;
  1337. int ret = 0;
  1338. switch (micb_num) {
  1339. case MIC_BIAS_1:
  1340. micb_reg = WCD938X_ANA_MICB1;
  1341. break;
  1342. case MIC_BIAS_2:
  1343. micb_reg = WCD938X_ANA_MICB2;
  1344. break;
  1345. case MIC_BIAS_3:
  1346. micb_reg = WCD938X_ANA_MICB3;
  1347. break;
  1348. case MIC_BIAS_4:
  1349. micb_reg = WCD938X_ANA_MICB4;
  1350. break;
  1351. default:
  1352. return -EINVAL;
  1353. }
  1354. mutex_lock(&wcd938x->micb_lock);
  1355. /*
  1356. * If requested micbias voltage is same as current micbias
  1357. * voltage, then just return. Otherwise, adjust voltage as
  1358. * per requested value. If micbias is already enabled, then
  1359. * to avoid slow micbias ramp-up or down enable pull-up
  1360. * momentarily, change the micbias value and then re-enable
  1361. * micbias.
  1362. */
  1363. micb_val = snd_soc_component_read32(component, micb_reg);
  1364. micb_en = (micb_val & 0xC0) >> 6;
  1365. cur_vout_ctl = micb_val & 0x3F;
  1366. req_vout_ctl = wcd938x_get_micb_vout_ctl_val(req_volt);
  1367. if (req_vout_ctl < 0) {
  1368. ret = -EINVAL;
  1369. goto exit;
  1370. }
  1371. if (cur_vout_ctl == req_vout_ctl) {
  1372. ret = 0;
  1373. goto exit;
  1374. }
  1375. dev_dbg(component->dev, "%s: micb_num: %d, cur_mv: %d, req_mv: %d, micb_en: %d\n",
  1376. __func__, micb_num, WCD_VOUT_CTL_TO_MICB(cur_vout_ctl),
  1377. req_volt, micb_en);
  1378. if (micb_en == 0x1)
  1379. snd_soc_component_update_bits(component, micb_reg, 0xC0, 0x80);
  1380. snd_soc_component_update_bits(component, micb_reg, 0x3F, req_vout_ctl);
  1381. if (micb_en == 0x1) {
  1382. snd_soc_component_update_bits(component, micb_reg, 0xC0, 0x40);
  1383. /*
  1384. * Add 2ms delay as per HW requirement after enabling
  1385. * micbias
  1386. */
  1387. usleep_range(2000, 2100);
  1388. }
  1389. exit:
  1390. mutex_unlock(&wcd938x->micb_lock);
  1391. return ret;
  1392. }
  1393. EXPORT_SYMBOL(wcd938x_mbhc_micb_adjust_voltage);
  1394. static int wcd938x_tx_swr_ctrl(struct snd_soc_dapm_widget *w,
  1395. struct snd_kcontrol *kcontrol,
  1396. int event)
  1397. {
  1398. struct snd_soc_component *component =
  1399. snd_soc_dapm_to_component(w->dapm);
  1400. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  1401. int ret = 0;
  1402. int bank = 0;
  1403. u8 mode = 0;
  1404. int i = 0;
  1405. int rate = 0;
  1406. bank = (wcd938x_swr_slv_get_current_bank(wcd938x->tx_swr_dev,
  1407. wcd938x->tx_swr_dev->dev_num) ? 0 : 1);
  1408. switch (event) {
  1409. case SND_SOC_DAPM_PRE_PMU:
  1410. if (strnstr(w->name, "ADC", sizeof("ADC"))) {
  1411. if (test_bit(WCD_ADC1, &wcd938x->status_mask))
  1412. mode |= tx_mode_bit[wcd938x->tx_mode[WCD_ADC1]];
  1413. if (test_bit(WCD_ADC2, &wcd938x->status_mask))
  1414. mode |= tx_mode_bit[wcd938x->tx_mode[WCD_ADC2]];
  1415. if (test_bit(WCD_ADC3, &wcd938x->status_mask))
  1416. mode |= tx_mode_bit[wcd938x->tx_mode[WCD_ADC3]];
  1417. if (test_bit(WCD_ADC4, &wcd938x->status_mask))
  1418. mode |= tx_mode_bit[wcd938x->tx_mode[WCD_ADC4]];
  1419. if (mode != 0) {
  1420. for (i = 0; i < ADC_MODE_ULP2; i++) {
  1421. if (mode & (1 << i)) {
  1422. i++;
  1423. break;
  1424. }
  1425. }
  1426. }
  1427. rate = wcd938x_get_clk_rate(i);
  1428. wcd938x_set_swr_clk_rate(component, rate, bank);
  1429. }
  1430. ret = swr_slvdev_datapath_control(wcd938x->tx_swr_dev,
  1431. wcd938x->tx_swr_dev->dev_num,
  1432. true);
  1433. if (strnstr(w->name, "ADC", sizeof("ADC"))) {
  1434. /* Copy clk settings to active bank */
  1435. wcd938x_set_swr_clk_rate(component, rate, !bank);
  1436. }
  1437. break;
  1438. case SND_SOC_DAPM_POST_PMD:
  1439. if (strnstr(w->name, "ADC", sizeof("ADC"))) {
  1440. rate = wcd938x_get_clk_rate(ADC_MODE_INVALID);
  1441. wcd938x_set_swr_clk_rate(component, rate, !bank);
  1442. }
  1443. ret = swr_slvdev_datapath_control(wcd938x->tx_swr_dev,
  1444. wcd938x->tx_swr_dev->dev_num,
  1445. false);
  1446. if (strnstr(w->name, "ADC", sizeof("ADC")))
  1447. wcd938x_set_swr_clk_rate(component, rate, bank);
  1448. break;
  1449. };
  1450. return ret;
  1451. }
  1452. static int wcd938x_get_adc_mode(int val)
  1453. {
  1454. int ret = 0;
  1455. switch (val) {
  1456. case ADC_MODE_INVALID:
  1457. ret = ADC_MODE_VAL_NORMAL;
  1458. break;
  1459. case ADC_MODE_HIFI:
  1460. ret = ADC_MODE_VAL_HIFI;
  1461. break;
  1462. case ADC_MODE_LO_HIF:
  1463. ret = ADC_MODE_VAL_LO_HIF;
  1464. break;
  1465. case ADC_MODE_NORMAL:
  1466. ret = ADC_MODE_VAL_NORMAL;
  1467. break;
  1468. case ADC_MODE_LP:
  1469. ret = ADC_MODE_VAL_LP;
  1470. break;
  1471. case ADC_MODE_ULP1:
  1472. ret = ADC_MODE_VAL_ULP1;
  1473. break;
  1474. case ADC_MODE_ULP2:
  1475. ret = ADC_MODE_VAL_ULP2;
  1476. break;
  1477. default:
  1478. ret = -EINVAL;
  1479. pr_err("%s: invalid ADC mode value %d\n", __func__, val);
  1480. break;
  1481. }
  1482. return ret;
  1483. }
  1484. static int wcd938x_codec_enable_adc(struct snd_soc_dapm_widget *w,
  1485. struct snd_kcontrol *kcontrol,
  1486. int event){
  1487. struct snd_soc_component *component =
  1488. snd_soc_dapm_to_component(w->dapm);
  1489. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  1490. int clk_rate = 0;
  1491. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  1492. w->name, event);
  1493. switch (event) {
  1494. case SND_SOC_DAPM_PRE_PMU:
  1495. snd_soc_component_update_bits(component,
  1496. WCD938X_DIGITAL_CDC_ANA_CLK_CTL, 0x08, 0x08);
  1497. snd_soc_component_update_bits(component,
  1498. WCD938X_DIGITAL_CDC_ANA_CLK_CTL, 0x10, 0x10);
  1499. set_bit(w->shift, &wcd938x->status_mask);
  1500. clk_rate = wcd938x_get_clk_rate(wcd938x->tx_mode[w->shift]);
  1501. /* Enable BCS for Headset mic */
  1502. if (w->shift == 1 && !(snd_soc_component_read32(component,
  1503. WCD938X_TX_NEW_AMIC_MUX_CFG) & 0x80)) {
  1504. if (!wcd938x->bcs_dis)
  1505. wcd938x_tx_connect_port(component, MBHC,
  1506. SWR_CLK_RATE_4P8MHZ, true);
  1507. set_bit(AMIC2_BCS_ENABLE, &wcd938x->status_mask);
  1508. }
  1509. wcd938x_tx_connect_port(component, ADC1 + (w->shift), clk_rate,
  1510. true);
  1511. break;
  1512. case SND_SOC_DAPM_POST_PMD:
  1513. wcd938x_tx_connect_port(component, ADC1 + (w->shift), 0, false);
  1514. if (w->shift == 1 &&
  1515. test_bit(AMIC2_BCS_ENABLE, &wcd938x->status_mask)) {
  1516. if (!wcd938x->bcs_dis)
  1517. wcd938x_tx_connect_port(component, MBHC, 0,
  1518. false);
  1519. clear_bit(AMIC2_BCS_ENABLE, &wcd938x->status_mask);
  1520. }
  1521. snd_soc_component_update_bits(component,
  1522. WCD938X_DIGITAL_CDC_ANA_CLK_CTL, 0x08, 0x00);
  1523. clear_bit(w->shift, &wcd938x->status_mask);
  1524. break;
  1525. };
  1526. return 0;
  1527. }
  1528. void wcd938x_disable_bcs_before_slow_insert(struct snd_soc_component *component,
  1529. bool bcs_disable)
  1530. {
  1531. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  1532. if (wcd938x->update_wcd_event) {
  1533. if (bcs_disable)
  1534. wcd938x->update_wcd_event(wcd938x->handle,
  1535. WCD_BOLERO_EVT_BCS_CLK_OFF, 0);
  1536. else
  1537. wcd938x->update_wcd_event(wcd938x->handle,
  1538. WCD_BOLERO_EVT_BCS_CLK_OFF, 1);
  1539. }
  1540. }
  1541. int wcd938x_tx_channel_config(struct snd_soc_component *component,
  1542. int channel, int mode)
  1543. {
  1544. int reg = WCD938X_ANA_TX_CH2, mask = 0, val = 0;
  1545. int ret = 0;
  1546. switch (channel) {
  1547. case 0:
  1548. reg = WCD938X_ANA_TX_CH2;
  1549. mask = 0x40;
  1550. break;
  1551. case 1:
  1552. reg = WCD938X_ANA_TX_CH2;
  1553. mask = 0x20;
  1554. break;
  1555. case 2:
  1556. reg = WCD938X_ANA_TX_CH4;
  1557. mask = 0x40;
  1558. break;
  1559. case 3:
  1560. reg = WCD938X_ANA_TX_CH4;
  1561. mask = 0x20;
  1562. break;
  1563. default:
  1564. pr_err("%s: Invalid channel num %d\n", __func__, channel);
  1565. ret = -EINVAL;
  1566. break;
  1567. }
  1568. if (!mode)
  1569. val = 0x00;
  1570. else
  1571. val = mask;
  1572. if (!ret)
  1573. snd_soc_component_update_bits(component, reg, mask, val);
  1574. return ret;
  1575. }
  1576. static int wcd938x_enable_req(struct snd_soc_dapm_widget *w,
  1577. struct snd_kcontrol *kcontrol, int event)
  1578. {
  1579. struct snd_soc_component *component =
  1580. snd_soc_dapm_to_component(w->dapm);
  1581. int mode;
  1582. int ret = 0;
  1583. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  1584. dev_dbg(component->dev, "%s wname: %s event: %d\n", __func__,
  1585. w->name, event);
  1586. switch (event) {
  1587. case SND_SOC_DAPM_PRE_PMU:
  1588. snd_soc_component_update_bits(component,
  1589. WCD938X_DIGITAL_CDC_REQ_CTL, 0x02, 0x02);
  1590. snd_soc_component_update_bits(component,
  1591. WCD938X_DIGITAL_CDC_REQ_CTL, 0x01, 0x00);
  1592. ret = wcd938x_tx_channel_config(component, w->shift, 1);
  1593. mode = wcd938x_get_adc_mode(wcd938x->tx_mode[w->shift]);
  1594. if (mode < 0) {
  1595. dev_info(component->dev,
  1596. "%s: invalid mode, setting to normal mode\n",
  1597. __func__);
  1598. mode = ADC_MODE_VAL_NORMAL;
  1599. }
  1600. switch (w->shift) {
  1601. case 0:
  1602. snd_soc_component_update_bits(component,
  1603. WCD938X_DIGITAL_CDC_TX_ANA_MODE_0_1, 0x0F,
  1604. mode);
  1605. snd_soc_component_update_bits(component,
  1606. WCD938X_DIGITAL_CDC_DIG_CLK_CTL, 0x10, 0x10);
  1607. break;
  1608. case 1:
  1609. snd_soc_component_update_bits(component,
  1610. WCD938X_DIGITAL_CDC_TX_ANA_MODE_0_1, 0xF0,
  1611. mode << 4);
  1612. snd_soc_component_update_bits(component,
  1613. WCD938X_DIGITAL_CDC_DIG_CLK_CTL, 0x20, 0x20);
  1614. break;
  1615. case 2:
  1616. snd_soc_component_update_bits(component,
  1617. WCD938X_DIGITAL_CDC_TX_ANA_MODE_2_3, 0x0F,
  1618. mode);
  1619. snd_soc_component_update_bits(component,
  1620. WCD938X_DIGITAL_CDC_DIG_CLK_CTL, 0x40, 0x40);
  1621. break;
  1622. case 3:
  1623. snd_soc_component_update_bits(component,
  1624. WCD938X_DIGITAL_CDC_TX_ANA_MODE_2_3, 0xF0,
  1625. mode << 4);
  1626. snd_soc_component_update_bits(component,
  1627. WCD938X_DIGITAL_CDC_DIG_CLK_CTL, 0x80, 0x80);
  1628. break;
  1629. default:
  1630. break;
  1631. }
  1632. ret |= wcd938x_tx_channel_config(component, w->shift, 0);
  1633. break;
  1634. case SND_SOC_DAPM_POST_PMD:
  1635. switch (w->shift) {
  1636. case 0:
  1637. snd_soc_component_update_bits(component,
  1638. WCD938X_DIGITAL_CDC_TX_ANA_MODE_0_1, 0x0F,
  1639. 0x00);
  1640. snd_soc_component_update_bits(component,
  1641. WCD938X_DIGITAL_CDC_DIG_CLK_CTL, 0x10, 0x00);
  1642. break;
  1643. case 1:
  1644. snd_soc_component_update_bits(component,
  1645. WCD938X_DIGITAL_CDC_TX_ANA_MODE_0_1, 0xF0,
  1646. 0x00);
  1647. snd_soc_component_update_bits(component,
  1648. WCD938X_DIGITAL_CDC_DIG_CLK_CTL, 0x20, 0x00);
  1649. break;
  1650. case 2:
  1651. snd_soc_component_update_bits(component,
  1652. WCD938X_DIGITAL_CDC_TX_ANA_MODE_2_3, 0x0F,
  1653. 0x00);
  1654. snd_soc_component_update_bits(component,
  1655. WCD938X_DIGITAL_CDC_DIG_CLK_CTL, 0x40, 0x00);
  1656. break;
  1657. case 3:
  1658. snd_soc_component_update_bits(component,
  1659. WCD938X_DIGITAL_CDC_TX_ANA_MODE_2_3, 0xF0,
  1660. 0x00);
  1661. snd_soc_component_update_bits(component,
  1662. WCD938X_DIGITAL_CDC_DIG_CLK_CTL, 0x80, 0x00);
  1663. break;
  1664. default:
  1665. break;
  1666. }
  1667. snd_soc_component_update_bits(component,
  1668. WCD938X_DIGITAL_CDC_ANA_CLK_CTL, 0x10, 0x00);
  1669. break;
  1670. };
  1671. return ret;
  1672. }
  1673. int wcd938x_micbias_control(struct snd_soc_component *component,
  1674. int micb_num, int req, bool is_dapm)
  1675. {
  1676. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  1677. int micb_index = micb_num - 1;
  1678. u16 micb_reg;
  1679. int pre_off_event = 0, post_off_event = 0;
  1680. int post_on_event = 0, post_dapm_off = 0;
  1681. int post_dapm_on = 0;
  1682. int ret = 0;
  1683. if ((micb_index < 0) || (micb_index > WCD938X_MAX_MICBIAS - 1)) {
  1684. dev_err(component->dev,
  1685. "%s: Invalid micbias index, micb_ind:%d\n",
  1686. __func__, micb_index);
  1687. return -EINVAL;
  1688. }
  1689. if (NULL == wcd938x) {
  1690. dev_err(component->dev,
  1691. "%s: wcd938x private data is NULL\n", __func__);
  1692. return -EINVAL;
  1693. }
  1694. switch (micb_num) {
  1695. case MIC_BIAS_1:
  1696. micb_reg = WCD938X_ANA_MICB1;
  1697. break;
  1698. case MIC_BIAS_2:
  1699. micb_reg = WCD938X_ANA_MICB2;
  1700. pre_off_event = WCD_EVENT_PRE_MICBIAS_2_OFF;
  1701. post_off_event = WCD_EVENT_POST_MICBIAS_2_OFF;
  1702. post_on_event = WCD_EVENT_POST_MICBIAS_2_ON;
  1703. post_dapm_on = WCD_EVENT_POST_DAPM_MICBIAS_2_ON;
  1704. post_dapm_off = WCD_EVENT_POST_DAPM_MICBIAS_2_OFF;
  1705. break;
  1706. case MIC_BIAS_3:
  1707. micb_reg = WCD938X_ANA_MICB3;
  1708. break;
  1709. case MIC_BIAS_4:
  1710. micb_reg = WCD938X_ANA_MICB4;
  1711. break;
  1712. default:
  1713. dev_err(component->dev, "%s: Invalid micbias number: %d\n",
  1714. __func__, micb_num);
  1715. return -EINVAL;
  1716. };
  1717. mutex_lock(&wcd938x->micb_lock);
  1718. switch (req) {
  1719. case MICB_PULLUP_ENABLE:
  1720. if (!wcd938x->dev_up) {
  1721. dev_dbg(component->dev, "%s: enable req %d wcd device down\n",
  1722. __func__, req);
  1723. ret = -ENODEV;
  1724. goto done;
  1725. }
  1726. wcd938x->pullup_ref[micb_index]++;
  1727. if ((wcd938x->pullup_ref[micb_index] == 1) &&
  1728. (wcd938x->micb_ref[micb_index] == 0))
  1729. snd_soc_component_update_bits(component, micb_reg,
  1730. 0xC0, 0x80);
  1731. break;
  1732. case MICB_PULLUP_DISABLE:
  1733. if (wcd938x->pullup_ref[micb_index] > 0)
  1734. wcd938x->pullup_ref[micb_index]--;
  1735. if (!wcd938x->dev_up) {
  1736. dev_dbg(component->dev, "%s: enable req %d wcd device down\n",
  1737. __func__, req);
  1738. ret = -ENODEV;
  1739. goto done;
  1740. }
  1741. if ((wcd938x->pullup_ref[micb_index] == 0) &&
  1742. (wcd938x->micb_ref[micb_index] == 0))
  1743. snd_soc_component_update_bits(component, micb_reg,
  1744. 0xC0, 0x00);
  1745. break;
  1746. case MICB_ENABLE:
  1747. if (!wcd938x->dev_up) {
  1748. dev_dbg(component->dev, "%s: enable req %d wcd device down\n",
  1749. __func__, req);
  1750. ret = -ENODEV;
  1751. goto done;
  1752. }
  1753. wcd938x->micb_ref[micb_index]++;
  1754. if (wcd938x->micb_ref[micb_index] == 1) {
  1755. snd_soc_component_update_bits(component,
  1756. WCD938X_DIGITAL_CDC_DIG_CLK_CTL, 0xF0, 0xF0);
  1757. snd_soc_component_update_bits(component,
  1758. WCD938X_DIGITAL_CDC_ANA_CLK_CTL, 0x10, 0x10);
  1759. snd_soc_component_update_bits(component,
  1760. WCD938X_DIGITAL_CDC_ANA_TX_CLK_CTL, 0x01, 0x01);
  1761. snd_soc_component_update_bits(component,
  1762. WCD938X_MICB1_TEST_CTL_2, 0x01, 0x01);
  1763. snd_soc_component_update_bits(component,
  1764. WCD938X_MICB2_TEST_CTL_2, 0x01, 0x01);
  1765. snd_soc_component_update_bits(component,
  1766. WCD938X_MICB3_TEST_CTL_2, 0x01, 0x01);
  1767. snd_soc_component_update_bits(component,
  1768. WCD938X_MICB4_TEST_CTL_2, 0x01, 0x01);
  1769. snd_soc_component_update_bits(component,
  1770. micb_reg, 0xC0, 0x40);
  1771. if (post_on_event)
  1772. blocking_notifier_call_chain(
  1773. &wcd938x->mbhc->notifier,
  1774. post_on_event,
  1775. &wcd938x->mbhc->wcd_mbhc);
  1776. }
  1777. if (is_dapm && post_dapm_on && wcd938x->mbhc)
  1778. blocking_notifier_call_chain(&wcd938x->mbhc->notifier,
  1779. post_dapm_on,
  1780. &wcd938x->mbhc->wcd_mbhc);
  1781. break;
  1782. case MICB_DISABLE:
  1783. if (wcd938x->micb_ref[micb_index] > 0)
  1784. wcd938x->micb_ref[micb_index]--;
  1785. if (!wcd938x->dev_up) {
  1786. dev_dbg(component->dev, "%s: enable req %d wcd device down\n",
  1787. __func__, req);
  1788. ret = -ENODEV;
  1789. goto done;
  1790. }
  1791. if ((wcd938x->micb_ref[micb_index] == 0) &&
  1792. (wcd938x->pullup_ref[micb_index] > 0))
  1793. snd_soc_component_update_bits(component, micb_reg,
  1794. 0xC0, 0x80);
  1795. else if ((wcd938x->micb_ref[micb_index] == 0) &&
  1796. (wcd938x->pullup_ref[micb_index] == 0)) {
  1797. if (pre_off_event && wcd938x->mbhc)
  1798. blocking_notifier_call_chain(
  1799. &wcd938x->mbhc->notifier,
  1800. pre_off_event,
  1801. &wcd938x->mbhc->wcd_mbhc);
  1802. snd_soc_component_update_bits(component, micb_reg,
  1803. 0xC0, 0x00);
  1804. if (post_off_event && wcd938x->mbhc)
  1805. blocking_notifier_call_chain(
  1806. &wcd938x->mbhc->notifier,
  1807. post_off_event,
  1808. &wcd938x->mbhc->wcd_mbhc);
  1809. }
  1810. if (is_dapm && post_dapm_off && wcd938x->mbhc)
  1811. blocking_notifier_call_chain(&wcd938x->mbhc->notifier,
  1812. post_dapm_off,
  1813. &wcd938x->mbhc->wcd_mbhc);
  1814. break;
  1815. };
  1816. dev_dbg(component->dev,
  1817. "%s: micb_num:%d, micb_ref: %d, pullup_ref: %d\n",
  1818. __func__, micb_num, wcd938x->micb_ref[micb_index],
  1819. wcd938x->pullup_ref[micb_index]);
  1820. done:
  1821. mutex_unlock(&wcd938x->micb_lock);
  1822. return ret;
  1823. }
  1824. EXPORT_SYMBOL(wcd938x_micbias_control);
  1825. static int wcd938x_get_logical_addr(struct swr_device *swr_dev)
  1826. {
  1827. int ret = 0;
  1828. uint8_t devnum = 0;
  1829. int num_retry = NUM_ATTEMPTS;
  1830. do {
  1831. ret = swr_get_logical_dev_num(swr_dev, swr_dev->addr, &devnum);
  1832. if (ret) {
  1833. dev_err(&swr_dev->dev,
  1834. "%s get devnum %d for dev addr %lx failed\n",
  1835. __func__, devnum, swr_dev->addr);
  1836. /* retry after 1ms */
  1837. usleep_range(1000, 1010);
  1838. }
  1839. } while (ret && --num_retry);
  1840. swr_dev->dev_num = devnum;
  1841. return 0;
  1842. }
  1843. static int wcd938x_event_notify(struct notifier_block *block,
  1844. unsigned long val,
  1845. void *data)
  1846. {
  1847. u16 event = (val & 0xffff);
  1848. int ret = 0;
  1849. struct wcd938x_priv *wcd938x = dev_get_drvdata((struct device *)data);
  1850. struct snd_soc_component *component = wcd938x->component;
  1851. struct wcd_mbhc *mbhc;
  1852. switch (event) {
  1853. case BOLERO_WCD_EVT_TX_CH_HOLD_CLEAR:
  1854. if (test_bit(WCD_ADC1, &wcd938x->status_mask)) {
  1855. snd_soc_component_update_bits(component,
  1856. WCD938X_ANA_TX_CH2, 0x40, 0x00);
  1857. clear_bit(WCD_ADC1, &wcd938x->status_mask);
  1858. }
  1859. if (test_bit(WCD_ADC2, &wcd938x->status_mask)) {
  1860. snd_soc_component_update_bits(component,
  1861. WCD938X_ANA_TX_CH2, 0x20, 0x00);
  1862. clear_bit(WCD_ADC2, &wcd938x->status_mask);
  1863. }
  1864. if (test_bit(WCD_ADC3, &wcd938x->status_mask)) {
  1865. snd_soc_component_update_bits(component,
  1866. WCD938X_ANA_TX_CH4, 0x40, 0x00);
  1867. clear_bit(WCD_ADC3, &wcd938x->status_mask);
  1868. }
  1869. if (test_bit(WCD_ADC4, &wcd938x->status_mask)) {
  1870. snd_soc_component_update_bits(component,
  1871. WCD938X_ANA_TX_CH4, 0x20, 0x00);
  1872. clear_bit(WCD_ADC4, &wcd938x->status_mask);
  1873. }
  1874. break;
  1875. case BOLERO_WCD_EVT_PA_OFF_PRE_SSR:
  1876. snd_soc_component_update_bits(component, WCD938X_ANA_HPH,
  1877. 0xC0, 0x00);
  1878. snd_soc_component_update_bits(component, WCD938X_ANA_EAR,
  1879. 0x80, 0x00);
  1880. snd_soc_component_update_bits(component, WCD938X_AUX_AUXPA,
  1881. 0x80, 0x00);
  1882. break;
  1883. case BOLERO_WCD_EVT_SSR_DOWN:
  1884. wcd938x->dev_up = false;
  1885. wcd938x->mbhc->wcd_mbhc.deinit_in_progress = true;
  1886. mbhc = &wcd938x->mbhc->wcd_mbhc;
  1887. wcd938x_mbhc_ssr_down(wcd938x->mbhc, component);
  1888. wcd938x_reset_low(wcd938x->dev);
  1889. break;
  1890. case BOLERO_WCD_EVT_SSR_UP:
  1891. wcd938x_reset(wcd938x->dev);
  1892. /* allow reset to take effect */
  1893. usleep_range(10000, 10010);
  1894. wcd938x_get_logical_addr(wcd938x->tx_swr_dev);
  1895. wcd938x_get_logical_addr(wcd938x->rx_swr_dev);
  1896. wcd938x_init_reg(component);
  1897. regcache_mark_dirty(wcd938x->regmap);
  1898. regcache_sync(wcd938x->regmap);
  1899. /* Initialize MBHC module */
  1900. mbhc = &wcd938x->mbhc->wcd_mbhc;
  1901. ret = wcd938x_mbhc_post_ssr_init(wcd938x->mbhc, component);
  1902. if (ret) {
  1903. dev_err(component->dev, "%s: mbhc initialization failed\n",
  1904. __func__);
  1905. } else {
  1906. wcd938x_mbhc_hs_detect(component, mbhc->mbhc_cfg);
  1907. }
  1908. wcd938x->mbhc->wcd_mbhc.deinit_in_progress = false;
  1909. wcd938x->dev_up = true;
  1910. break;
  1911. case BOLERO_WCD_EVT_CLK_NOTIFY:
  1912. snd_soc_component_update_bits(component,
  1913. WCD938X_DIGITAL_TOP_CLK_CFG, 0x06,
  1914. ((val >> 0x10) << 0x01));
  1915. break;
  1916. default:
  1917. dev_dbg(component->dev, "%s: invalid event %d\n", __func__, event);
  1918. break;
  1919. }
  1920. return 0;
  1921. }
  1922. static int __wcd938x_codec_enable_micbias(struct snd_soc_dapm_widget *w,
  1923. int event)
  1924. {
  1925. struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
  1926. int micb_num;
  1927. dev_dbg(component->dev, "%s: wname: %s, event: %d\n",
  1928. __func__, w->name, event);
  1929. if (strnstr(w->name, "MIC BIAS1", sizeof("MIC BIAS1")))
  1930. micb_num = MIC_BIAS_1;
  1931. else if (strnstr(w->name, "MIC BIAS2", sizeof("MIC BIAS2")))
  1932. micb_num = MIC_BIAS_2;
  1933. else if (strnstr(w->name, "MIC BIAS3", sizeof("MIC BIAS3")))
  1934. micb_num = MIC_BIAS_3;
  1935. else if (strnstr(w->name, "MIC BIAS4", sizeof("MIC BIAS4")))
  1936. micb_num = MIC_BIAS_4;
  1937. else
  1938. return -EINVAL;
  1939. switch (event) {
  1940. case SND_SOC_DAPM_PRE_PMU:
  1941. wcd938x_micbias_control(component, micb_num,
  1942. MICB_ENABLE, true);
  1943. break;
  1944. case SND_SOC_DAPM_POST_PMU:
  1945. /* 1 msec delay as per HW requirement */
  1946. usleep_range(1000, 1100);
  1947. break;
  1948. case SND_SOC_DAPM_POST_PMD:
  1949. wcd938x_micbias_control(component, micb_num,
  1950. MICB_DISABLE, true);
  1951. break;
  1952. };
  1953. return 0;
  1954. }
  1955. static int wcd938x_codec_enable_micbias(struct snd_soc_dapm_widget *w,
  1956. struct snd_kcontrol *kcontrol,
  1957. int event)
  1958. {
  1959. return __wcd938x_codec_enable_micbias(w, event);
  1960. }
  1961. static int __wcd938x_codec_enable_micbias_pullup(struct snd_soc_dapm_widget *w,
  1962. int event)
  1963. {
  1964. struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
  1965. int micb_num;
  1966. dev_dbg(component->dev, "%s: wname: %s, event: %d\n",
  1967. __func__, w->name, event);
  1968. if (strnstr(w->name, "VA MIC BIAS1", sizeof("VA MIC BIAS1")))
  1969. micb_num = MIC_BIAS_1;
  1970. else if (strnstr(w->name, "VA MIC BIAS2", sizeof("VA MIC BIAS2")))
  1971. micb_num = MIC_BIAS_2;
  1972. else if (strnstr(w->name, "VA MIC BIAS3", sizeof("VA MIC BIAS3")))
  1973. micb_num = MIC_BIAS_3;
  1974. else if (strnstr(w->name, "VA MIC BIAS4", sizeof("VA MIC BIAS4")))
  1975. micb_num = MIC_BIAS_4;
  1976. else
  1977. return -EINVAL;
  1978. switch (event) {
  1979. case SND_SOC_DAPM_PRE_PMU:
  1980. wcd938x_micbias_control(component, micb_num,
  1981. MICB_PULLUP_ENABLE, true);
  1982. break;
  1983. case SND_SOC_DAPM_POST_PMU:
  1984. /* 1 msec delay as per HW requirement */
  1985. usleep_range(1000, 1100);
  1986. break;
  1987. case SND_SOC_DAPM_POST_PMD:
  1988. wcd938x_micbias_control(component, micb_num,
  1989. MICB_PULLUP_DISABLE, true);
  1990. break;
  1991. };
  1992. return 0;
  1993. }
  1994. static int wcd938x_codec_enable_micbias_pullup(struct snd_soc_dapm_widget *w,
  1995. struct snd_kcontrol *kcontrol,
  1996. int event)
  1997. {
  1998. return __wcd938x_codec_enable_micbias_pullup(w, event);
  1999. }
  2000. static int wcd938x_wakeup(void *handle, bool enable)
  2001. {
  2002. struct wcd938x_priv *priv;
  2003. int ret = 0;
  2004. if (!handle) {
  2005. pr_err("%s: NULL handle\n", __func__);
  2006. return -EINVAL;
  2007. }
  2008. priv = (struct wcd938x_priv *)handle;
  2009. if (!priv->tx_swr_dev) {
  2010. pr_err("%s: tx swr dev is NULL\n", __func__);
  2011. return -EINVAL;
  2012. }
  2013. mutex_lock(&priv->wakeup_lock);
  2014. if (enable)
  2015. ret = swr_device_wakeup_vote(priv->tx_swr_dev);
  2016. else
  2017. ret = swr_device_wakeup_unvote(priv->tx_swr_dev);
  2018. mutex_unlock(&priv->wakeup_lock);
  2019. return ret;
  2020. }
  2021. static int wcd938x_codec_force_enable_micbias(struct snd_soc_dapm_widget *w,
  2022. struct snd_kcontrol *kcontrol,
  2023. int event)
  2024. {
  2025. int ret = 0;
  2026. struct snd_soc_component *component =
  2027. snd_soc_dapm_to_component(w->dapm);
  2028. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  2029. switch (event) {
  2030. case SND_SOC_DAPM_PRE_PMU:
  2031. wcd938x_wakeup(wcd938x, true);
  2032. ret = __wcd938x_codec_enable_micbias(w, SND_SOC_DAPM_PRE_PMU);
  2033. wcd938x_wakeup(wcd938x, false);
  2034. break;
  2035. case SND_SOC_DAPM_POST_PMD:
  2036. wcd938x_wakeup(wcd938x, true);
  2037. ret = __wcd938x_codec_enable_micbias(w, SND_SOC_DAPM_POST_PMD);
  2038. wcd938x_wakeup(wcd938x, false);
  2039. break;
  2040. }
  2041. return ret;
  2042. }
  2043. static int wcd938x_enable_micbias(struct wcd938x_priv *wcd938x,
  2044. int micb_num, int req)
  2045. {
  2046. int micb_index = micb_num - 1;
  2047. u16 micb_reg;
  2048. if (NULL == wcd938x) {
  2049. pr_err("%s: wcd938x private data is NULL\n", __func__);
  2050. return -EINVAL;
  2051. }
  2052. switch (micb_num) {
  2053. case MIC_BIAS_1:
  2054. micb_reg = WCD938X_ANA_MICB1;
  2055. break;
  2056. case MIC_BIAS_2:
  2057. micb_reg = WCD938X_ANA_MICB2;
  2058. break;
  2059. case MIC_BIAS_3:
  2060. micb_reg = WCD938X_ANA_MICB3;
  2061. break;
  2062. case MIC_BIAS_4:
  2063. micb_reg = WCD938X_ANA_MICB4;
  2064. break;
  2065. default:
  2066. pr_err("%s: Invalid micbias number: %d\n", __func__, micb_num);
  2067. return -EINVAL;
  2068. };
  2069. mutex_lock(&wcd938x->micb_lock);
  2070. switch (req) {
  2071. case MICB_ENABLE:
  2072. wcd938x->micb_ref[micb_index]++;
  2073. if (wcd938x->micb_ref[micb_index] == 1) {
  2074. regmap_update_bits(wcd938x->regmap,
  2075. WCD938X_DIGITAL_CDC_DIG_CLK_CTL, 0xE0, 0xE0);
  2076. regmap_update_bits(wcd938x->regmap,
  2077. WCD938X_DIGITAL_CDC_ANA_CLK_CTL, 0x10, 0x10);
  2078. regmap_update_bits(wcd938x->regmap,
  2079. WCD938X_DIGITAL_CDC_ANA_TX_CLK_CTL, 0x01, 0x01);
  2080. regmap_update_bits(wcd938x->regmap,
  2081. WCD938X_MICB1_TEST_CTL_2, 0x01, 0x01);
  2082. regmap_update_bits(wcd938x->regmap,
  2083. WCD938X_MICB2_TEST_CTL_2, 0x01, 0x01);
  2084. regmap_update_bits(wcd938x->regmap,
  2085. WCD938X_MICB3_TEST_CTL_2, 0x01, 0x01);
  2086. regmap_update_bits(wcd938x->regmap,
  2087. WCD938X_MICB4_TEST_CTL_2, 0x01, 0x01);
  2088. regmap_update_bits(wcd938x->regmap,
  2089. micb_reg, 0xC0, 0x40);
  2090. regmap_update_bits(wcd938x->regmap, micb_reg, 0x3F, 0x10);
  2091. }
  2092. break;
  2093. case MICB_PULLUP_ENABLE:
  2094. wcd938x->pullup_ref[micb_index]++;
  2095. if ((wcd938x->pullup_ref[micb_index] == 1) &&
  2096. (wcd938x->micb_ref[micb_index] == 0))
  2097. regmap_update_bits(wcd938x->regmap, micb_reg,
  2098. 0xC0, 0x80);
  2099. break;
  2100. case MICB_PULLUP_DISABLE:
  2101. if (wcd938x->pullup_ref[micb_index] > 0)
  2102. wcd938x->pullup_ref[micb_index]--;
  2103. if ((wcd938x->pullup_ref[micb_index] == 0) &&
  2104. (wcd938x->micb_ref[micb_index] == 0))
  2105. regmap_update_bits(wcd938x->regmap, micb_reg,
  2106. 0xC0, 0x00);
  2107. break;
  2108. case MICB_DISABLE:
  2109. if (wcd938x->micb_ref[micb_index] > 0)
  2110. wcd938x->micb_ref[micb_index]--;
  2111. if ((wcd938x->micb_ref[micb_index] == 0) &&
  2112. (wcd938x->pullup_ref[micb_index] > 0))
  2113. regmap_update_bits(wcd938x->regmap, micb_reg,
  2114. 0xC0, 0x80);
  2115. else if ((wcd938x->micb_ref[micb_index] == 0) &&
  2116. (wcd938x->pullup_ref[micb_index] == 0))
  2117. regmap_update_bits(wcd938x->regmap, micb_reg,
  2118. 0xC0, 0x00);
  2119. break;
  2120. };
  2121. mutex_unlock(&wcd938x->micb_lock);
  2122. return 0;
  2123. }
  2124. int wcd938x_codec_force_enable_micbias_v2(struct snd_soc_component *component,
  2125. int event, int micb_num)
  2126. {
  2127. struct wcd938x_priv *wcd938x_priv = NULL;
  2128. if(NULL == component) {
  2129. pr_err("%s: wcd938x component is NULL\n", __func__);
  2130. return -EINVAL;
  2131. }
  2132. if(event != SND_SOC_DAPM_PRE_PMU && event != SND_SOC_DAPM_POST_PMD) {
  2133. pr_err("%s: invalid event: %d\n", __func__, event);
  2134. return -EINVAL;
  2135. }
  2136. if(micb_num < MIC_BIAS_1 || micb_num > MIC_BIAS_4) {
  2137. pr_err("%s: invalid mic bias num: %d\n", __func__, micb_num);
  2138. return -EINVAL;
  2139. }
  2140. wcd938x_priv = snd_soc_component_get_drvdata(component);
  2141. switch (event) {
  2142. case SND_SOC_DAPM_PRE_PMU:
  2143. wcd938x_wakeup(wcd938x_priv, true);
  2144. wcd938x_enable_micbias(wcd938x_priv, micb_num, MICB_PULLUP_ENABLE);
  2145. wcd938x_wakeup(wcd938x_priv, false);
  2146. break;
  2147. case SND_SOC_DAPM_POST_PMD:
  2148. wcd938x_wakeup(wcd938x_priv, true);
  2149. wcd938x_enable_micbias(wcd938x_priv, micb_num, MICB_PULLUP_DISABLE);
  2150. wcd938x_wakeup(wcd938x_priv, false);
  2151. break;
  2152. }
  2153. return 0;
  2154. }
  2155. EXPORT_SYMBOL(wcd938x_codec_force_enable_micbias_v2);
  2156. static inline int wcd938x_tx_path_get(const char *wname,
  2157. unsigned int *path_num)
  2158. {
  2159. int ret = 0;
  2160. char *widget_name = NULL;
  2161. char *w_name = NULL;
  2162. char *path_num_char = NULL;
  2163. char *path_name = NULL;
  2164. widget_name = kstrndup(wname, 9, GFP_KERNEL);
  2165. if (!widget_name)
  2166. return -EINVAL;
  2167. w_name = widget_name;
  2168. path_name = strsep(&widget_name, " ");
  2169. if (!path_name) {
  2170. pr_err("%s: Invalid widget name = %s\n",
  2171. __func__, widget_name);
  2172. ret = -EINVAL;
  2173. goto err;
  2174. }
  2175. path_num_char = strpbrk(path_name, "0123");
  2176. if (!path_num_char) {
  2177. pr_err("%s: tx path index not found\n",
  2178. __func__);
  2179. ret = -EINVAL;
  2180. goto err;
  2181. }
  2182. ret = kstrtouint(path_num_char, 10, path_num);
  2183. if (ret < 0)
  2184. pr_err("%s: Invalid tx path = %s\n",
  2185. __func__, w_name);
  2186. err:
  2187. kfree(w_name);
  2188. return ret;
  2189. }
  2190. static int wcd938x_tx_mode_get(struct snd_kcontrol *kcontrol,
  2191. struct snd_ctl_elem_value *ucontrol)
  2192. {
  2193. struct snd_soc_component *component =
  2194. snd_soc_kcontrol_component(kcontrol);
  2195. struct wcd938x_priv *wcd938x = NULL;
  2196. int ret = 0;
  2197. unsigned int path = 0;
  2198. if (!component)
  2199. return -EINVAL;
  2200. wcd938x = snd_soc_component_get_drvdata(component);
  2201. if (!wcd938x)
  2202. return -EINVAL;
  2203. ret = wcd938x_tx_path_get(kcontrol->id.name, &path);
  2204. if (ret < 0)
  2205. return ret;
  2206. ucontrol->value.integer.value[0] = wcd938x->tx_mode[path];
  2207. return 0;
  2208. }
  2209. static int wcd938x_tx_mode_put(struct snd_kcontrol *kcontrol,
  2210. struct snd_ctl_elem_value *ucontrol)
  2211. {
  2212. struct snd_soc_component *component =
  2213. snd_soc_kcontrol_component(kcontrol);
  2214. struct wcd938x_priv *wcd938x = NULL;
  2215. u32 mode_val;
  2216. unsigned int path = 0;
  2217. int ret = 0;
  2218. if (!component)
  2219. return -EINVAL;
  2220. wcd938x = snd_soc_component_get_drvdata(component);
  2221. if (!wcd938x)
  2222. return -EINVAL;
  2223. ret = wcd938x_tx_path_get(kcontrol->id.name, &path);
  2224. if (ret)
  2225. return ret;
  2226. mode_val = ucontrol->value.enumerated.item[0];
  2227. dev_dbg(component->dev, "%s: mode: %d\n", __func__, mode_val);
  2228. wcd938x->tx_mode[path] = mode_val;
  2229. return 0;
  2230. }
  2231. static int wcd938x_rx_hph_mode_get(struct snd_kcontrol *kcontrol,
  2232. struct snd_ctl_elem_value *ucontrol)
  2233. {
  2234. struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
  2235. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  2236. ucontrol->value.integer.value[0] = wcd938x->hph_mode;
  2237. return 0;
  2238. }
  2239. static int wcd938x_rx_hph_mode_put(struct snd_kcontrol *kcontrol,
  2240. struct snd_ctl_elem_value *ucontrol)
  2241. {
  2242. struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
  2243. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  2244. u32 mode_val;
  2245. mode_val = ucontrol->value.enumerated.item[0];
  2246. dev_dbg(component->dev, "%s: mode: %d\n", __func__, mode_val);
  2247. if (wcd938x->variant == WCD9380) {
  2248. if (mode_val == CLS_H_HIFI || mode_val == CLS_AB_HIFI) {
  2249. dev_info(component->dev,
  2250. "%s:Invalid HPH Mode, default to CLS_H_ULP\n",
  2251. __func__);
  2252. mode_val = CLS_H_ULP;
  2253. }
  2254. }
  2255. if (mode_val == CLS_H_NORMAL) {
  2256. dev_info(component->dev,
  2257. "%s:Invalid HPH Mode, default to class_AB\n",
  2258. __func__);
  2259. mode_val = CLS_H_ULP;
  2260. }
  2261. wcd938x->hph_mode = mode_val;
  2262. return 0;
  2263. }
  2264. static int wcd938x_ear_pa_gain_get(struct snd_kcontrol *kcontrol,
  2265. struct snd_ctl_elem_value *ucontrol)
  2266. {
  2267. u8 ear_pa_gain = 0;
  2268. struct snd_soc_component *component =
  2269. snd_soc_kcontrol_component(kcontrol);
  2270. ear_pa_gain = snd_soc_component_read32(component,
  2271. WCD938X_ANA_EAR_COMPANDER_CTL);
  2272. ear_pa_gain = (ear_pa_gain & 0x7C) >> 2;
  2273. ucontrol->value.integer.value[0] = ear_pa_gain;
  2274. dev_dbg(component->dev, "%s: ear_pa_gain = 0x%x\n", __func__,
  2275. ear_pa_gain);
  2276. return 0;
  2277. }
  2278. static int wcd938x_ear_pa_gain_put(struct snd_kcontrol *kcontrol,
  2279. struct snd_ctl_elem_value *ucontrol)
  2280. {
  2281. u8 ear_pa_gain = 0;
  2282. struct snd_soc_component *component =
  2283. snd_soc_kcontrol_component(kcontrol);
  2284. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  2285. dev_dbg(component->dev, "%s: ucontrol->value.integer.value[0] = %ld\n",
  2286. __func__, ucontrol->value.integer.value[0]);
  2287. ear_pa_gain = ucontrol->value.integer.value[0] << 2;
  2288. if (!wcd938x->comp1_enable) {
  2289. snd_soc_component_update_bits(component,
  2290. WCD938X_ANA_EAR_COMPANDER_CTL,
  2291. 0x7C, ear_pa_gain);
  2292. }
  2293. return 0;
  2294. }
  2295. static int wcd938x_get_compander(struct snd_kcontrol *kcontrol,
  2296. struct snd_ctl_elem_value *ucontrol)
  2297. {
  2298. struct snd_soc_component *component =
  2299. snd_soc_kcontrol_component(kcontrol);
  2300. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  2301. bool hphr;
  2302. struct soc_multi_mixer_control *mc;
  2303. mc = (struct soc_multi_mixer_control *)(kcontrol->private_value);
  2304. hphr = mc->shift;
  2305. ucontrol->value.integer.value[0] = hphr ? wcd938x->comp2_enable :
  2306. wcd938x->comp1_enable;
  2307. return 0;
  2308. }
  2309. static int wcd938x_set_compander(struct snd_kcontrol *kcontrol,
  2310. struct snd_ctl_elem_value *ucontrol)
  2311. {
  2312. struct snd_soc_component *component =
  2313. snd_soc_kcontrol_component(kcontrol);
  2314. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  2315. int value = ucontrol->value.integer.value[0];
  2316. bool hphr;
  2317. struct soc_multi_mixer_control *mc;
  2318. mc = (struct soc_multi_mixer_control *)(kcontrol->private_value);
  2319. hphr = mc->shift;
  2320. if (hphr)
  2321. wcd938x->comp2_enable = value;
  2322. else
  2323. wcd938x->comp1_enable = value;
  2324. return 0;
  2325. }
  2326. static int wcd938x_ldoh_get(struct snd_kcontrol *kcontrol,
  2327. struct snd_ctl_elem_value *ucontrol)
  2328. {
  2329. struct snd_soc_component *component =
  2330. snd_soc_kcontrol_component(kcontrol);
  2331. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  2332. ucontrol->value.integer.value[0] = wcd938x->ldoh;
  2333. return 0;
  2334. }
  2335. static int wcd938x_ldoh_put(struct snd_kcontrol *kcontrol,
  2336. struct snd_ctl_elem_value *ucontrol)
  2337. {
  2338. struct snd_soc_component *component =
  2339. snd_soc_kcontrol_component(kcontrol);
  2340. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  2341. wcd938x->ldoh = ucontrol->value.integer.value[0];
  2342. return 0;
  2343. }
  2344. const char * const tx_master_ch_text[] = {
  2345. "ZERO", "SWRM_TX1_CH1", "SWRM_TX1_CH2", "SWRM_TX1_CH3", "SWRM_TX1_CH4",
  2346. "SWRM_TX2_CH1", "SWRM_TX2_CH2", "SWRM_TX2_CH3", "SWRM_TX2_CH4",
  2347. "SWRM_TX3_CH1", "SWRM_TX3_CH2", "SWRM_TX3_CH3", "SWRM_TX3_CH4",
  2348. "SWRM_PCM_IN",
  2349. };
  2350. const struct soc_enum tx_master_ch_enum =
  2351. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(tx_master_ch_text),
  2352. tx_master_ch_text);
  2353. static void wcd938x_tx_get_slave_ch_type_idx(const char *wname, int *ch_idx)
  2354. {
  2355. u8 ch_type = 0;
  2356. if (strnstr(wname, "ADC1", sizeof("ADC1")))
  2357. ch_type = ADC1;
  2358. else if (strnstr(wname, "ADC2", sizeof("ADC2")))
  2359. ch_type = ADC2;
  2360. else if (strnstr(wname, "ADC3", sizeof("ADC3")))
  2361. ch_type = ADC3;
  2362. else if (strnstr(wname, "ADC4", sizeof("ADC4")))
  2363. ch_type = ADC4;
  2364. else if (strnstr(wname, "DMIC0", sizeof("DMIC0")))
  2365. ch_type = DMIC0;
  2366. else if (strnstr(wname, "DMIC1", sizeof("DMIC1")))
  2367. ch_type = DMIC1;
  2368. else if (strnstr(wname, "MBHC", sizeof("MBHC")))
  2369. ch_type = MBHC;
  2370. else if (strnstr(wname, "DMIC2", sizeof("DMIC2")))
  2371. ch_type = DMIC2;
  2372. else if (strnstr(wname, "DMIC3", sizeof("DMIC3")))
  2373. ch_type = DMIC3;
  2374. else if (strnstr(wname, "DMIC4", sizeof("DMIC4")))
  2375. ch_type = DMIC4;
  2376. else if (strnstr(wname, "DMIC5", sizeof("DMIC5")))
  2377. ch_type = DMIC5;
  2378. else if (strnstr(wname, "DMIC6", sizeof("DMIC6")))
  2379. ch_type = DMIC6;
  2380. else if (strnstr(wname, "DMIC7", sizeof("DMIC7")))
  2381. ch_type = DMIC7;
  2382. else
  2383. pr_err("%s: port name: %s is not listed\n", __func__, wname);
  2384. if (ch_type)
  2385. *ch_idx = wcd938x_slave_get_slave_ch_val(ch_type);
  2386. else
  2387. *ch_idx = -EINVAL;
  2388. }
  2389. static int wcd938x_tx_master_ch_get(struct snd_kcontrol *kcontrol,
  2390. struct snd_ctl_elem_value *ucontrol)
  2391. {
  2392. struct snd_soc_component *component =
  2393. snd_soc_kcontrol_component(kcontrol);
  2394. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  2395. int slave_ch_idx;
  2396. wcd938x_tx_get_slave_ch_type_idx(kcontrol->id.name, &slave_ch_idx);
  2397. if (slave_ch_idx != -EINVAL)
  2398. ucontrol->value.integer.value[0] =
  2399. wcd938x_slave_get_master_ch_val(
  2400. wcd938x->tx_master_ch_map[slave_ch_idx]);
  2401. return 0;
  2402. }
  2403. static int wcd938x_tx_master_ch_put(struct snd_kcontrol *kcontrol,
  2404. struct snd_ctl_elem_value *ucontrol)
  2405. {
  2406. struct snd_soc_component *component =
  2407. snd_soc_kcontrol_component(kcontrol);
  2408. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  2409. int slave_ch_idx;
  2410. wcd938x_tx_get_slave_ch_type_idx(kcontrol->id.name, &slave_ch_idx);
  2411. dev_dbg(component->dev, "%s: slave_ch_idx: %d", __func__, slave_ch_idx);
  2412. dev_dbg(component->dev, "%s: ucontrol->value.enumerated.item[0] = %ld\n",
  2413. __func__, ucontrol->value.enumerated.item[0]);
  2414. if (slave_ch_idx != -EINVAL)
  2415. wcd938x->tx_master_ch_map[slave_ch_idx] =
  2416. wcd938x_slave_get_master_ch(
  2417. ucontrol->value.enumerated.item[0]);
  2418. return 0;
  2419. }
  2420. static int wcd938x_bcs_get(struct snd_kcontrol *kcontrol,
  2421. struct snd_ctl_elem_value *ucontrol)
  2422. {
  2423. struct snd_soc_component *component =
  2424. snd_soc_kcontrol_component(kcontrol);
  2425. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  2426. ucontrol->value.integer.value[0] = wcd938x->bcs_dis;
  2427. return 0;
  2428. }
  2429. static int wcd938x_bcs_put(struct snd_kcontrol *kcontrol,
  2430. struct snd_ctl_elem_value *ucontrol)
  2431. {
  2432. struct snd_soc_component *component =
  2433. snd_soc_kcontrol_component(kcontrol);
  2434. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  2435. wcd938x->bcs_dis = ucontrol->value.integer.value[0];
  2436. return 0;
  2437. }
  2438. static const char * const tx_mode_mux_text_wcd9380[] = {
  2439. "ADC_INVALID", "ADC_HIFI", "ADC_LO_HIF", "ADC_NORMAL", "ADC_LP",
  2440. };
  2441. static const struct soc_enum tx_mode_mux_enum_wcd9380 =
  2442. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(tx_mode_mux_text_wcd9380),
  2443. tx_mode_mux_text_wcd9380);
  2444. static const char * const tx_mode_mux_text[] = {
  2445. "ADC_INVALID", "ADC_HIFI", "ADC_LO_HIF", "ADC_NORMAL", "ADC_LP",
  2446. "ADC_ULP1", "ADC_ULP2",
  2447. };
  2448. static const struct soc_enum tx_mode_mux_enum =
  2449. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(tx_mode_mux_text),
  2450. tx_mode_mux_text);
  2451. static const char * const rx_hph_mode_mux_text_wcd9380[] = {
  2452. "CLS_H_INVALID", "CLS_H_INVALID_1", "CLS_H_LP", "CLS_AB",
  2453. "CLS_H_LOHIFI", "CLS_H_ULP", "CLS_H_INVALID_2", "CLS_AB_LP",
  2454. "CLS_AB_LOHIFI",
  2455. };
  2456. static const char * const wcd938x_ear_pa_gain_text[] = {
  2457. "G_6_DB", "G_4P5_DB", "G_3_DB", "G_1P5_DB", "G_0_DB",
  2458. "G_M1P5_DB", "G_M3_DB", "G_M4P5_DB",
  2459. "G_M6_DB", "G_7P5_DB", "G_M9_DB",
  2460. "G_M10P5_DB", "G_M12_DB", "G_M13P5_DB",
  2461. "G_M15_DB", "G_M16P5_DB", "G_M18_DB",
  2462. };
  2463. static const struct soc_enum rx_hph_mode_mux_enum_wcd9380 =
  2464. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(rx_hph_mode_mux_text_wcd9380),
  2465. rx_hph_mode_mux_text_wcd9380);
  2466. static SOC_ENUM_SINGLE_EXT_DECL(wcd938x_ear_pa_gain_enum,
  2467. wcd938x_ear_pa_gain_text);
  2468. static const char * const rx_hph_mode_mux_text[] = {
  2469. "CLS_H_INVALID", "CLS_H_HIFI", "CLS_H_LP", "CLS_AB", "CLS_H_LOHIFI",
  2470. "CLS_H_ULP", "CLS_AB_HIFI", "CLS_AB_LP", "CLS_AB_LOHIFI",
  2471. };
  2472. static const struct soc_enum rx_hph_mode_mux_enum =
  2473. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(rx_hph_mode_mux_text),
  2474. rx_hph_mode_mux_text);
  2475. static const struct snd_kcontrol_new wcd9380_snd_controls[] = {
  2476. SOC_ENUM_EXT("EAR PA GAIN", wcd938x_ear_pa_gain_enum,
  2477. wcd938x_ear_pa_gain_get, wcd938x_ear_pa_gain_put),
  2478. SOC_ENUM_EXT("RX HPH Mode", rx_hph_mode_mux_enum_wcd9380,
  2479. wcd938x_rx_hph_mode_get, wcd938x_rx_hph_mode_put),
  2480. SOC_ENUM_EXT("TX0 MODE", tx_mode_mux_enum_wcd9380,
  2481. wcd938x_tx_mode_get, wcd938x_tx_mode_put),
  2482. SOC_ENUM_EXT("TX1 MODE", tx_mode_mux_enum_wcd9380,
  2483. wcd938x_tx_mode_get, wcd938x_tx_mode_put),
  2484. SOC_ENUM_EXT("TX2 MODE", tx_mode_mux_enum_wcd9380,
  2485. wcd938x_tx_mode_get, wcd938x_tx_mode_put),
  2486. SOC_ENUM_EXT("TX3 MODE", tx_mode_mux_enum_wcd9380,
  2487. wcd938x_tx_mode_get, wcd938x_tx_mode_put),
  2488. };
  2489. static const struct snd_kcontrol_new wcd9385_snd_controls[] = {
  2490. SOC_ENUM_EXT("RX HPH Mode", rx_hph_mode_mux_enum,
  2491. wcd938x_rx_hph_mode_get, wcd938x_rx_hph_mode_put),
  2492. SOC_ENUM_EXT("TX0 MODE", tx_mode_mux_enum,
  2493. wcd938x_tx_mode_get, wcd938x_tx_mode_put),
  2494. SOC_ENUM_EXT("TX1 MODE", tx_mode_mux_enum,
  2495. wcd938x_tx_mode_get, wcd938x_tx_mode_put),
  2496. SOC_ENUM_EXT("TX2 MODE", tx_mode_mux_enum,
  2497. wcd938x_tx_mode_get, wcd938x_tx_mode_put),
  2498. SOC_ENUM_EXT("TX3 MODE", tx_mode_mux_enum,
  2499. wcd938x_tx_mode_get, wcd938x_tx_mode_put),
  2500. };
  2501. static const struct snd_kcontrol_new wcd938x_snd_controls[] = {
  2502. SOC_SINGLE_EXT("HPHL_COMP Switch", SND_SOC_NOPM, 0, 1, 0,
  2503. wcd938x_get_compander, wcd938x_set_compander),
  2504. SOC_SINGLE_EXT("HPHR_COMP Switch", SND_SOC_NOPM, 1, 1, 0,
  2505. wcd938x_get_compander, wcd938x_set_compander),
  2506. SOC_SINGLE_EXT("LDOH Enable", SND_SOC_NOPM, 0, 1, 0,
  2507. wcd938x_ldoh_get, wcd938x_ldoh_put),
  2508. SOC_SINGLE_EXT("ADC2_BCS Disable", SND_SOC_NOPM, 0, 1, 0,
  2509. wcd938x_bcs_get, wcd938x_bcs_put),
  2510. SOC_SINGLE_TLV("HPHL Volume", WCD938X_HPH_L_EN, 0, 20, 1, line_gain),
  2511. SOC_SINGLE_TLV("HPHR Volume", WCD938X_HPH_R_EN, 0, 20, 1, line_gain),
  2512. SOC_SINGLE_TLV("ADC1 Volume", WCD938X_ANA_TX_CH1, 0, 20, 0,
  2513. analog_gain),
  2514. SOC_SINGLE_TLV("ADC2 Volume", WCD938X_ANA_TX_CH2, 0, 20, 0,
  2515. analog_gain),
  2516. SOC_SINGLE_TLV("ADC3 Volume", WCD938X_ANA_TX_CH3, 0, 20, 0,
  2517. analog_gain),
  2518. SOC_SINGLE_TLV("ADC4 Volume", WCD938X_ANA_TX_CH4, 0, 20, 0,
  2519. analog_gain),
  2520. SOC_ENUM_EXT("ADC1 ChMap", tx_master_ch_enum,
  2521. wcd938x_tx_master_ch_get, wcd938x_tx_master_ch_put),
  2522. SOC_ENUM_EXT("ADC2 ChMap", tx_master_ch_enum,
  2523. wcd938x_tx_master_ch_get, wcd938x_tx_master_ch_put),
  2524. SOC_ENUM_EXT("ADC3 ChMap", tx_master_ch_enum,
  2525. wcd938x_tx_master_ch_get, wcd938x_tx_master_ch_put),
  2526. SOC_ENUM_EXT("ADC4 ChMap", tx_master_ch_enum,
  2527. wcd938x_tx_master_ch_get, wcd938x_tx_master_ch_put),
  2528. SOC_ENUM_EXT("DMIC0 ChMap", tx_master_ch_enum,
  2529. wcd938x_tx_master_ch_get, wcd938x_tx_master_ch_put),
  2530. SOC_ENUM_EXT("DMIC1 ChMap", tx_master_ch_enum,
  2531. wcd938x_tx_master_ch_get, wcd938x_tx_master_ch_put),
  2532. SOC_ENUM_EXT("MBHC ChMap", tx_master_ch_enum,
  2533. wcd938x_tx_master_ch_get, wcd938x_tx_master_ch_put),
  2534. SOC_ENUM_EXT("DMIC2 ChMap", tx_master_ch_enum,
  2535. wcd938x_tx_master_ch_get, wcd938x_tx_master_ch_put),
  2536. SOC_ENUM_EXT("DMIC3 ChMap", tx_master_ch_enum,
  2537. wcd938x_tx_master_ch_get, wcd938x_tx_master_ch_put),
  2538. SOC_ENUM_EXT("DMIC4 ChMap", tx_master_ch_enum,
  2539. wcd938x_tx_master_ch_get, wcd938x_tx_master_ch_put),
  2540. SOC_ENUM_EXT("DMIC5 ChMap", tx_master_ch_enum,
  2541. wcd938x_tx_master_ch_get, wcd938x_tx_master_ch_put),
  2542. SOC_ENUM_EXT("DMIC6 ChMap", tx_master_ch_enum,
  2543. wcd938x_tx_master_ch_get, wcd938x_tx_master_ch_put),
  2544. SOC_ENUM_EXT("DMIC7 ChMap", tx_master_ch_enum,
  2545. wcd938x_tx_master_ch_get, wcd938x_tx_master_ch_put),
  2546. };
  2547. static const struct snd_kcontrol_new adc1_switch[] = {
  2548. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  2549. };
  2550. static const struct snd_kcontrol_new adc2_switch[] = {
  2551. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  2552. };
  2553. static const struct snd_kcontrol_new adc3_switch[] = {
  2554. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  2555. };
  2556. static const struct snd_kcontrol_new adc4_switch[] = {
  2557. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  2558. };
  2559. static const struct snd_kcontrol_new dmic1_switch[] = {
  2560. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  2561. };
  2562. static const struct snd_kcontrol_new dmic2_switch[] = {
  2563. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  2564. };
  2565. static const struct snd_kcontrol_new dmic3_switch[] = {
  2566. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  2567. };
  2568. static const struct snd_kcontrol_new dmic4_switch[] = {
  2569. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  2570. };
  2571. static const struct snd_kcontrol_new dmic5_switch[] = {
  2572. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  2573. };
  2574. static const struct snd_kcontrol_new dmic6_switch[] = {
  2575. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  2576. };
  2577. static const struct snd_kcontrol_new dmic7_switch[] = {
  2578. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  2579. };
  2580. static const struct snd_kcontrol_new dmic8_switch[] = {
  2581. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  2582. };
  2583. static const struct snd_kcontrol_new ear_rdac_switch[] = {
  2584. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  2585. };
  2586. static const struct snd_kcontrol_new aux_rdac_switch[] = {
  2587. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  2588. };
  2589. static const struct snd_kcontrol_new hphl_rdac_switch[] = {
  2590. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  2591. };
  2592. static const struct snd_kcontrol_new hphr_rdac_switch[] = {
  2593. SOC_DAPM_SINGLE("Switch", SND_SOC_NOPM, 0, 1, 0)
  2594. };
  2595. static const char * const adc2_mux_text[] = {
  2596. "INP2", "INP3"
  2597. };
  2598. static const struct soc_enum adc2_enum =
  2599. SOC_ENUM_SINGLE(WCD938X_TX_NEW_AMIC_MUX_CFG, 7,
  2600. ARRAY_SIZE(adc2_mux_text), adc2_mux_text);
  2601. static const struct snd_kcontrol_new tx_adc2_mux =
  2602. SOC_DAPM_ENUM("ADC2 MUX Mux", adc2_enum);
  2603. static const char * const adc3_mux_text[] = {
  2604. "INP4", "INP6"
  2605. };
  2606. static const struct soc_enum adc3_enum =
  2607. SOC_ENUM_SINGLE(WCD938X_TX_NEW_AMIC_MUX_CFG, 6,
  2608. ARRAY_SIZE(adc3_mux_text), adc3_mux_text);
  2609. static const struct snd_kcontrol_new tx_adc3_mux =
  2610. SOC_DAPM_ENUM("ADC3 MUX Mux", adc3_enum);
  2611. static const char * const adc4_mux_text[] = {
  2612. "INP5", "INP7"
  2613. };
  2614. static const struct soc_enum adc4_enum =
  2615. SOC_ENUM_SINGLE(WCD938X_TX_NEW_AMIC_MUX_CFG, 5,
  2616. ARRAY_SIZE(adc4_mux_text), adc4_mux_text);
  2617. static const struct snd_kcontrol_new tx_adc4_mux =
  2618. SOC_DAPM_ENUM("ADC4 MUX Mux", adc4_enum);
  2619. static const char * const rdac3_mux_text[] = {
  2620. "RX1", "RX3"
  2621. };
  2622. static const char * const hdr12_mux_text[] = {
  2623. "NO_HDR12", "HDR12"
  2624. };
  2625. static const struct soc_enum hdr12_enum =
  2626. SOC_ENUM_SINGLE(WCD938X_TX_NEW_AMIC_MUX_CFG, 4,
  2627. ARRAY_SIZE(hdr12_mux_text), hdr12_mux_text);
  2628. static const struct snd_kcontrol_new tx_hdr12_mux =
  2629. SOC_DAPM_ENUM("HDR12 MUX Mux", hdr12_enum);
  2630. static const char * const hdr34_mux_text[] = {
  2631. "NO_HDR34", "HDR34"
  2632. };
  2633. static const struct soc_enum hdr34_enum =
  2634. SOC_ENUM_SINGLE(WCD938X_TX_NEW_AMIC_MUX_CFG, 3,
  2635. ARRAY_SIZE(hdr34_mux_text), hdr34_mux_text);
  2636. static const struct snd_kcontrol_new tx_hdr34_mux =
  2637. SOC_DAPM_ENUM("HDR34 MUX Mux", hdr34_enum);
  2638. static const struct soc_enum rdac3_enum =
  2639. SOC_ENUM_SINGLE(WCD938X_DIGITAL_CDC_EAR_PATH_CTL, 0,
  2640. ARRAY_SIZE(rdac3_mux_text), rdac3_mux_text);
  2641. static const struct snd_kcontrol_new rx_rdac3_mux =
  2642. SOC_DAPM_ENUM("RDAC3_MUX Mux", rdac3_enum);
  2643. static const struct snd_soc_dapm_widget wcd938x_dapm_widgets[] = {
  2644. /*input widgets*/
  2645. SND_SOC_DAPM_INPUT("AMIC1"),
  2646. SND_SOC_DAPM_INPUT("AMIC2"),
  2647. SND_SOC_DAPM_INPUT("AMIC3"),
  2648. SND_SOC_DAPM_INPUT("AMIC4"),
  2649. SND_SOC_DAPM_INPUT("AMIC5"),
  2650. SND_SOC_DAPM_INPUT("AMIC6"),
  2651. SND_SOC_DAPM_INPUT("AMIC7"),
  2652. SND_SOC_DAPM_INPUT("IN1_HPHL"),
  2653. SND_SOC_DAPM_INPUT("IN2_HPHR"),
  2654. SND_SOC_DAPM_INPUT("IN3_AUX"),
  2655. /*tx widgets*/
  2656. SND_SOC_DAPM_ADC_E("ADC1", NULL, SND_SOC_NOPM, 0, 0,
  2657. wcd938x_codec_enable_adc,
  2658. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  2659. SND_SOC_DAPM_ADC_E("ADC2", NULL, SND_SOC_NOPM, 1, 0,
  2660. wcd938x_codec_enable_adc,
  2661. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  2662. SND_SOC_DAPM_ADC_E("ADC3", NULL, SND_SOC_NOPM, 2, 0,
  2663. wcd938x_codec_enable_adc,
  2664. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  2665. SND_SOC_DAPM_ADC_E("ADC4", NULL, SND_SOC_NOPM, 3, 0,
  2666. wcd938x_codec_enable_adc,
  2667. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  2668. SND_SOC_DAPM_ADC_E("DMIC1", NULL, SND_SOC_NOPM, 0, 0,
  2669. wcd938x_codec_enable_dmic,
  2670. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  2671. SND_SOC_DAPM_ADC_E("DMIC2", NULL, SND_SOC_NOPM, 1, 0,
  2672. wcd938x_codec_enable_dmic,
  2673. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  2674. SND_SOC_DAPM_ADC_E("DMIC3", NULL, SND_SOC_NOPM, 2, 0,
  2675. wcd938x_codec_enable_dmic,
  2676. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  2677. SND_SOC_DAPM_ADC_E("DMIC4", NULL, SND_SOC_NOPM, 3, 0,
  2678. wcd938x_codec_enable_dmic,
  2679. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  2680. SND_SOC_DAPM_ADC_E("DMIC5", NULL, SND_SOC_NOPM, 4, 0,
  2681. wcd938x_codec_enable_dmic,
  2682. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  2683. SND_SOC_DAPM_ADC_E("DMIC6", NULL, SND_SOC_NOPM, 5, 0,
  2684. wcd938x_codec_enable_dmic,
  2685. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  2686. SND_SOC_DAPM_ADC_E("DMIC7", NULL, SND_SOC_NOPM, 6, 0,
  2687. wcd938x_codec_enable_dmic,
  2688. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  2689. SND_SOC_DAPM_ADC_E("DMIC8", NULL, SND_SOC_NOPM, 7, 0,
  2690. wcd938x_codec_enable_dmic,
  2691. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  2692. SND_SOC_DAPM_MIXER_E("ADC1 REQ", SND_SOC_NOPM, 0, 0,
  2693. NULL, 0, wcd938x_enable_req,
  2694. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  2695. SND_SOC_DAPM_MIXER_E("ADC2 REQ", SND_SOC_NOPM, 1, 0,
  2696. NULL, 0, wcd938x_enable_req,
  2697. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  2698. SND_SOC_DAPM_MIXER_E("ADC3 REQ", SND_SOC_NOPM, 2, 0,
  2699. NULL, 0, wcd938x_enable_req,
  2700. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  2701. SND_SOC_DAPM_MIXER_E("ADC4 REQ", SND_SOC_NOPM, 3, 0,
  2702. NULL, 0, wcd938x_enable_req,
  2703. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  2704. SND_SOC_DAPM_MUX("ADC2 MUX", SND_SOC_NOPM, 0, 0,
  2705. &tx_adc2_mux),
  2706. SND_SOC_DAPM_MUX("ADC3 MUX", SND_SOC_NOPM, 0, 0,
  2707. &tx_adc3_mux),
  2708. SND_SOC_DAPM_MUX("ADC4 MUX", SND_SOC_NOPM, 0, 0,
  2709. &tx_adc4_mux),
  2710. SND_SOC_DAPM_MUX("HDR12 MUX", SND_SOC_NOPM, 0, 0,
  2711. &tx_hdr12_mux),
  2712. SND_SOC_DAPM_MUX("HDR34 MUX", SND_SOC_NOPM, 0, 0,
  2713. &tx_hdr34_mux),
  2714. /*tx mixers*/
  2715. SND_SOC_DAPM_MIXER_E("ADC1_MIXER", SND_SOC_NOPM, 0, 0,
  2716. adc1_switch, ARRAY_SIZE(adc1_switch),
  2717. wcd938x_tx_swr_ctrl, SND_SOC_DAPM_PRE_PMU |
  2718. SND_SOC_DAPM_POST_PMD),
  2719. SND_SOC_DAPM_MIXER_E("ADC2_MIXER", SND_SOC_NOPM, 0, 0,
  2720. adc2_switch, ARRAY_SIZE(adc2_switch),
  2721. wcd938x_tx_swr_ctrl, SND_SOC_DAPM_PRE_PMU |
  2722. SND_SOC_DAPM_POST_PMD),
  2723. SND_SOC_DAPM_MIXER_E("ADC3_MIXER", SND_SOC_NOPM, 0, 0, adc3_switch,
  2724. ARRAY_SIZE(adc3_switch), wcd938x_tx_swr_ctrl,
  2725. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  2726. SND_SOC_DAPM_MIXER_E("ADC4_MIXER", SND_SOC_NOPM, 0, 0, adc4_switch,
  2727. ARRAY_SIZE(adc4_switch), wcd938x_tx_swr_ctrl,
  2728. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  2729. SND_SOC_DAPM_MIXER_E("DMIC1_MIXER", SND_SOC_NOPM, 0,
  2730. 0, dmic1_switch, ARRAY_SIZE(dmic1_switch),
  2731. wcd938x_tx_swr_ctrl, SND_SOC_DAPM_PRE_PMU |
  2732. SND_SOC_DAPM_POST_PMD),
  2733. SND_SOC_DAPM_MIXER_E("DMIC2_MIXER", SND_SOC_NOPM, 0,
  2734. 0, dmic2_switch, ARRAY_SIZE(dmic2_switch),
  2735. wcd938x_tx_swr_ctrl, SND_SOC_DAPM_PRE_PMU |
  2736. SND_SOC_DAPM_POST_PMD),
  2737. SND_SOC_DAPM_MIXER_E("DMIC3_MIXER", SND_SOC_NOPM, 0,
  2738. 0, dmic3_switch, ARRAY_SIZE(dmic3_switch),
  2739. wcd938x_tx_swr_ctrl, SND_SOC_DAPM_PRE_PMU |
  2740. SND_SOC_DAPM_POST_PMD),
  2741. SND_SOC_DAPM_MIXER_E("DMIC4_MIXER", SND_SOC_NOPM, 0,
  2742. 0, dmic4_switch, ARRAY_SIZE(dmic4_switch),
  2743. wcd938x_tx_swr_ctrl, SND_SOC_DAPM_PRE_PMU |
  2744. SND_SOC_DAPM_POST_PMD),
  2745. SND_SOC_DAPM_MIXER_E("DMIC5_MIXER", SND_SOC_NOPM, 0,
  2746. 0, dmic5_switch, ARRAY_SIZE(dmic5_switch),
  2747. wcd938x_tx_swr_ctrl, SND_SOC_DAPM_PRE_PMU |
  2748. SND_SOC_DAPM_POST_PMD),
  2749. SND_SOC_DAPM_MIXER_E("DMIC6_MIXER", SND_SOC_NOPM, 0,
  2750. 0, dmic6_switch, ARRAY_SIZE(dmic6_switch),
  2751. wcd938x_tx_swr_ctrl, SND_SOC_DAPM_PRE_PMU |
  2752. SND_SOC_DAPM_POST_PMD),
  2753. SND_SOC_DAPM_MIXER_E("DMIC7_MIXER", SND_SOC_NOPM, 0,
  2754. 0, dmic7_switch, ARRAY_SIZE(dmic7_switch),
  2755. wcd938x_tx_swr_ctrl, SND_SOC_DAPM_PRE_PMU |
  2756. SND_SOC_DAPM_POST_PMD),
  2757. SND_SOC_DAPM_MIXER_E("DMIC8_MIXER", SND_SOC_NOPM, 0,
  2758. 0, dmic8_switch, ARRAY_SIZE(dmic8_switch),
  2759. wcd938x_tx_swr_ctrl, SND_SOC_DAPM_PRE_PMU |
  2760. SND_SOC_DAPM_POST_PMD),
  2761. /* micbias widgets*/
  2762. SND_SOC_DAPM_SUPPLY("MIC BIAS1", SND_SOC_NOPM, 0, 0,
  2763. wcd938x_codec_enable_micbias,
  2764. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  2765. SND_SOC_DAPM_POST_PMD),
  2766. SND_SOC_DAPM_SUPPLY("MIC BIAS2", SND_SOC_NOPM, 0, 0,
  2767. wcd938x_codec_enable_micbias,
  2768. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  2769. SND_SOC_DAPM_POST_PMD),
  2770. SND_SOC_DAPM_SUPPLY("MIC BIAS3", SND_SOC_NOPM, 0, 0,
  2771. wcd938x_codec_enable_micbias,
  2772. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  2773. SND_SOC_DAPM_POST_PMD),
  2774. SND_SOC_DAPM_SUPPLY("MIC BIAS4", SND_SOC_NOPM, 0, 0,
  2775. wcd938x_codec_enable_micbias,
  2776. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  2777. SND_SOC_DAPM_POST_PMD),
  2778. SND_SOC_DAPM_SUPPLY(DAPM_MICBIAS1_STANDALONE, SND_SOC_NOPM, 0, 0,
  2779. wcd938x_codec_force_enable_micbias,
  2780. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  2781. SND_SOC_DAPM_SUPPLY(DAPM_MICBIAS2_STANDALONE, SND_SOC_NOPM, 0, 0,
  2782. wcd938x_codec_force_enable_micbias,
  2783. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  2784. SND_SOC_DAPM_SUPPLY(DAPM_MICBIAS3_STANDALONE, SND_SOC_NOPM, 0, 0,
  2785. wcd938x_codec_force_enable_micbias,
  2786. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  2787. SND_SOC_DAPM_SUPPLY(DAPM_MICBIAS4_STANDALONE, SND_SOC_NOPM, 0, 0,
  2788. wcd938x_codec_force_enable_micbias,
  2789. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  2790. SND_SOC_DAPM_SUPPLY_S("CLS_H_PORT", 1, SND_SOC_NOPM, 0, 0,
  2791. wcd938x_enable_clsh,
  2792. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  2793. /*rx widgets*/
  2794. SND_SOC_DAPM_PGA_E("EAR PGA", WCD938X_ANA_EAR, 7, 0, NULL, 0,
  2795. wcd938x_codec_enable_ear_pa,
  2796. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  2797. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  2798. SND_SOC_DAPM_PGA_E("AUX PGA", WCD938X_AUX_AUXPA, 7, 0, NULL, 0,
  2799. wcd938x_codec_enable_aux_pa,
  2800. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  2801. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  2802. SND_SOC_DAPM_PGA_E("HPHL PGA", WCD938X_ANA_HPH, 7, 0, NULL, 0,
  2803. wcd938x_codec_enable_hphl_pa,
  2804. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  2805. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  2806. SND_SOC_DAPM_PGA_E("HPHR PGA", WCD938X_ANA_HPH, 6, 0, NULL, 0,
  2807. wcd938x_codec_enable_hphr_pa,
  2808. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  2809. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  2810. SND_SOC_DAPM_DAC_E("RDAC1", NULL, SND_SOC_NOPM, 0, 0,
  2811. wcd938x_codec_hphl_dac_event,
  2812. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  2813. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  2814. SND_SOC_DAPM_DAC_E("RDAC2", NULL, SND_SOC_NOPM, 0, 0,
  2815. wcd938x_codec_hphr_dac_event,
  2816. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  2817. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  2818. SND_SOC_DAPM_DAC_E("RDAC3", NULL, SND_SOC_NOPM, 0, 0,
  2819. wcd938x_codec_ear_dac_event,
  2820. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  2821. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  2822. SND_SOC_DAPM_DAC_E("RDAC4", NULL, SND_SOC_NOPM, 0, 0,
  2823. wcd938x_codec_aux_dac_event,
  2824. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  2825. SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
  2826. SND_SOC_DAPM_MUX("RDAC3_MUX", SND_SOC_NOPM, 0, 0, &rx_rdac3_mux),
  2827. SND_SOC_DAPM_MIXER_E("RX1", SND_SOC_NOPM, 0, 0, NULL, 0,
  2828. wcd938x_enable_rx1, SND_SOC_DAPM_PRE_PMU |
  2829. SND_SOC_DAPM_POST_PMD),
  2830. SND_SOC_DAPM_MIXER_E("RX2", SND_SOC_NOPM, 0, 0, NULL, 0,
  2831. wcd938x_enable_rx2, SND_SOC_DAPM_PRE_PMU |
  2832. SND_SOC_DAPM_POST_PMD),
  2833. SND_SOC_DAPM_MIXER_E("RX3", SND_SOC_NOPM, 0, 0, NULL, 0,
  2834. wcd938x_enable_rx3, SND_SOC_DAPM_PRE_PMU |
  2835. SND_SOC_DAPM_POST_PMD),
  2836. /* rx mixer widgets*/
  2837. SND_SOC_DAPM_MIXER("EAR_RDAC", SND_SOC_NOPM, 0, 0,
  2838. ear_rdac_switch, ARRAY_SIZE(ear_rdac_switch)),
  2839. SND_SOC_DAPM_MIXER("AUX_RDAC", SND_SOC_NOPM, 0, 0,
  2840. aux_rdac_switch, ARRAY_SIZE(aux_rdac_switch)),
  2841. SND_SOC_DAPM_MIXER("HPHL_RDAC", SND_SOC_NOPM, 0, 0,
  2842. hphl_rdac_switch, ARRAY_SIZE(hphl_rdac_switch)),
  2843. SND_SOC_DAPM_MIXER("HPHR_RDAC", SND_SOC_NOPM, 0, 0,
  2844. hphr_rdac_switch, ARRAY_SIZE(hphr_rdac_switch)),
  2845. /*output widgets tx*/
  2846. SND_SOC_DAPM_OUTPUT("WCD_TX_OUTPUT"),
  2847. /*output widgets rx*/
  2848. SND_SOC_DAPM_OUTPUT("EAR"),
  2849. SND_SOC_DAPM_OUTPUT("AUX"),
  2850. SND_SOC_DAPM_OUTPUT("HPHL"),
  2851. SND_SOC_DAPM_OUTPUT("HPHR"),
  2852. /* micbias pull up widgets*/
  2853. SND_SOC_DAPM_SUPPLY("VA MIC BIAS1", SND_SOC_NOPM, 0, 0,
  2854. wcd938x_codec_enable_micbias_pullup,
  2855. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  2856. SND_SOC_DAPM_POST_PMD),
  2857. SND_SOC_DAPM_SUPPLY("VA MIC BIAS2", SND_SOC_NOPM, 0, 0,
  2858. wcd938x_codec_enable_micbias_pullup,
  2859. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  2860. SND_SOC_DAPM_POST_PMD),
  2861. SND_SOC_DAPM_SUPPLY("VA MIC BIAS3", SND_SOC_NOPM, 0, 0,
  2862. wcd938x_codec_enable_micbias_pullup,
  2863. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  2864. SND_SOC_DAPM_POST_PMD),
  2865. SND_SOC_DAPM_SUPPLY("VA MIC BIAS4", SND_SOC_NOPM, 0, 0,
  2866. wcd938x_codec_enable_micbias_pullup,
  2867. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  2868. SND_SOC_DAPM_POST_PMD),
  2869. };
  2870. static const struct snd_soc_dapm_route wcd938x_audio_map[] = {
  2871. {"WCD_TX_OUTPUT", NULL, "ADC1_MIXER"},
  2872. {"ADC1_MIXER", "Switch", "ADC1 REQ"},
  2873. {"ADC1 REQ", NULL, "ADC1"},
  2874. {"ADC1", NULL, "AMIC1"},
  2875. {"WCD_TX_OUTPUT", NULL, "ADC2_MIXER"},
  2876. {"ADC2_MIXER", "Switch", "ADC2 REQ"},
  2877. {"ADC2 REQ", NULL, "ADC2"},
  2878. {"ADC2", NULL, "HDR12 MUX"},
  2879. {"HDR12 MUX", "NO_HDR12", "ADC2 MUX"},
  2880. {"HDR12 MUX", "HDR12", "AMIC1"},
  2881. {"ADC2 MUX", "INP3", "AMIC3"},
  2882. {"ADC2 MUX", "INP2", "AMIC2"},
  2883. {"WCD_TX_OUTPUT", NULL, "ADC3_MIXER"},
  2884. {"ADC3_MIXER", "Switch", "ADC3 REQ"},
  2885. {"ADC3 REQ", NULL, "ADC3"},
  2886. {"ADC3", NULL, "HDR34 MUX"},
  2887. {"HDR34 MUX", "NO_HDR34", "ADC3 MUX"},
  2888. {"HDR34 MUX", "HDR34", "AMIC5"},
  2889. {"ADC3 MUX", "INP4", "AMIC4"},
  2890. {"ADC3 MUX", "INP6", "AMIC6"},
  2891. {"WCD_TX_OUTPUT", NULL, "ADC4_MIXER"},
  2892. {"ADC4_MIXER", "Switch", "ADC4 REQ"},
  2893. {"ADC4 REQ", NULL, "ADC4"},
  2894. {"ADC4", NULL, "ADC4 MUX"},
  2895. {"ADC4 MUX", "INP5", "AMIC5"},
  2896. {"ADC4 MUX", "INP7", "AMIC7"},
  2897. {"WCD_TX_OUTPUT", NULL, "DMIC1_MIXER"},
  2898. {"DMIC1_MIXER", "Switch", "DMIC1"},
  2899. {"WCD_TX_OUTPUT", NULL, "DMIC2_MIXER"},
  2900. {"DMIC2_MIXER", "Switch", "DMIC2"},
  2901. {"WCD_TX_OUTPUT", NULL, "DMIC3_MIXER"},
  2902. {"DMIC3_MIXER", "Switch", "DMIC3"},
  2903. {"WCD_TX_OUTPUT", NULL, "DMIC4_MIXER"},
  2904. {"DMIC4_MIXER", "Switch", "DMIC4"},
  2905. {"WCD_TX_OUTPUT", NULL, "DMIC5_MIXER"},
  2906. {"DMIC5_MIXER", "Switch", "DMIC5"},
  2907. {"WCD_TX_OUTPUT", NULL, "DMIC6_MIXER"},
  2908. {"DMIC6_MIXER", "Switch", "DMIC6"},
  2909. {"WCD_TX_OUTPUT", NULL, "DMIC7_MIXER"},
  2910. {"DMIC7_MIXER", "Switch", "DMIC7"},
  2911. {"WCD_TX_OUTPUT", NULL, "DMIC8_MIXER"},
  2912. {"DMIC8_MIXER", "Switch", "DMIC8"},
  2913. {"IN1_HPHL", NULL, "CLS_H_PORT"},
  2914. {"RX1", NULL, "IN1_HPHL"},
  2915. {"RDAC1", NULL, "RX1"},
  2916. {"HPHL_RDAC", "Switch", "RDAC1"},
  2917. {"HPHL PGA", NULL, "HPHL_RDAC"},
  2918. {"HPHL", NULL, "HPHL PGA"},
  2919. {"IN2_HPHR", NULL, "CLS_H_PORT"},
  2920. {"RX2", NULL, "IN2_HPHR"},
  2921. {"RDAC2", NULL, "RX2"},
  2922. {"HPHR_RDAC", "Switch", "RDAC2"},
  2923. {"HPHR PGA", NULL, "HPHR_RDAC"},
  2924. {"HPHR", NULL, "HPHR PGA"},
  2925. {"IN3_AUX", NULL, "CLS_H_PORT"},
  2926. {"RX3", NULL, "IN3_AUX"},
  2927. {"RDAC4", NULL, "RX3"},
  2928. {"AUX_RDAC", "Switch", "RDAC4"},
  2929. {"AUX PGA", NULL, "AUX_RDAC"},
  2930. {"AUX", NULL, "AUX PGA"},
  2931. {"RDAC3_MUX", "RX3", "RX3"},
  2932. {"RDAC3_MUX", "RX1", "RX1"},
  2933. {"RDAC3", NULL, "RDAC3_MUX"},
  2934. {"EAR_RDAC", "Switch", "RDAC3"},
  2935. {"EAR PGA", NULL, "EAR_RDAC"},
  2936. {"EAR", NULL, "EAR PGA"},
  2937. };
  2938. static ssize_t wcd938x_version_read(struct snd_info_entry *entry,
  2939. void *file_private_data,
  2940. struct file *file,
  2941. char __user *buf, size_t count,
  2942. loff_t pos)
  2943. {
  2944. struct wcd938x_priv *priv;
  2945. char buffer[WCD938X_VERSION_ENTRY_SIZE];
  2946. int len = 0;
  2947. priv = (struct wcd938x_priv *) entry->private_data;
  2948. if (!priv) {
  2949. pr_err("%s: wcd938x priv is null\n", __func__);
  2950. return -EINVAL;
  2951. }
  2952. switch (priv->version) {
  2953. case WCD938X_VERSION_1_0:
  2954. len = snprintf(buffer, sizeof(buffer), "WCD938X_1_0\n");
  2955. break;
  2956. default:
  2957. len = snprintf(buffer, sizeof(buffer), "VER_UNDEFINED\n");
  2958. }
  2959. return simple_read_from_buffer(buf, count, &pos, buffer, len);
  2960. }
  2961. static struct snd_info_entry_ops wcd938x_info_ops = {
  2962. .read = wcd938x_version_read,
  2963. };
  2964. static ssize_t wcd938x_variant_read(struct snd_info_entry *entry,
  2965. void *file_private_data,
  2966. struct file *file,
  2967. char __user *buf, size_t count,
  2968. loff_t pos)
  2969. {
  2970. struct wcd938x_priv *priv;
  2971. char buffer[WCD938X_VARIANT_ENTRY_SIZE];
  2972. int len = 0;
  2973. priv = (struct wcd938x_priv *) entry->private_data;
  2974. if (!priv) {
  2975. pr_err("%s: wcd938x priv is null\n", __func__);
  2976. return -EINVAL;
  2977. }
  2978. switch (priv->variant) {
  2979. case WCD9380:
  2980. len = snprintf(buffer, sizeof(buffer), "WCD9380\n");
  2981. break;
  2982. case WCD9385:
  2983. len = snprintf(buffer, sizeof(buffer), "WCD9385\n");
  2984. break;
  2985. default:
  2986. len = snprintf(buffer, sizeof(buffer), "VER_UNDEFINED\n");
  2987. }
  2988. return simple_read_from_buffer(buf, count, &pos, buffer, len);
  2989. }
  2990. static struct snd_info_entry_ops wcd938x_variant_ops = {
  2991. .read = wcd938x_variant_read,
  2992. };
  2993. /*
  2994. * wcd938x_get_codec_variant
  2995. * @component: component instance
  2996. *
  2997. * Return: codec variant or -EINVAL in error.
  2998. */
  2999. int wcd938x_get_codec_variant(struct snd_soc_component *component)
  3000. {
  3001. struct wcd938x_priv *priv = NULL;
  3002. if (!component)
  3003. return -EINVAL;
  3004. priv = snd_soc_component_get_drvdata(component);
  3005. if (!priv) {
  3006. dev_err(component->dev,
  3007. "%s:wcd938x not probed\n", __func__);
  3008. return 0;
  3009. }
  3010. return priv->variant;
  3011. }
  3012. EXPORT_SYMBOL(wcd938x_get_codec_variant);
  3013. /*
  3014. * wcd938x_info_create_codec_entry - creates wcd938x module
  3015. * @codec_root: The parent directory
  3016. * @component: component instance
  3017. *
  3018. * Creates wcd938x module, variant and version entry under the given
  3019. * parent directory.
  3020. *
  3021. * Return: 0 on success or negative error code on failure.
  3022. */
  3023. int wcd938x_info_create_codec_entry(struct snd_info_entry *codec_root,
  3024. struct snd_soc_component *component)
  3025. {
  3026. struct snd_info_entry *version_entry;
  3027. struct snd_info_entry *variant_entry;
  3028. struct wcd938x_priv *priv;
  3029. struct snd_soc_card *card;
  3030. if (!codec_root || !component)
  3031. return -EINVAL;
  3032. priv = snd_soc_component_get_drvdata(component);
  3033. if (priv->entry) {
  3034. dev_dbg(priv->dev,
  3035. "%s:wcd938x module already created\n", __func__);
  3036. return 0;
  3037. }
  3038. card = component->card;
  3039. priv->entry = snd_info_create_module_entry(codec_root->module,
  3040. "wcd938x", codec_root);
  3041. if (!priv->entry) {
  3042. dev_dbg(component->dev, "%s: failed to create wcd938x entry\n",
  3043. __func__);
  3044. return -ENOMEM;
  3045. }
  3046. priv->entry->mode = S_IFDIR | 0555;
  3047. if (snd_info_register(priv->entry) < 0) {
  3048. snd_info_free_entry(priv->entry);
  3049. return -ENOMEM;
  3050. }
  3051. version_entry = snd_info_create_card_entry(card->snd_card,
  3052. "version",
  3053. priv->entry);
  3054. if (!version_entry) {
  3055. dev_dbg(component->dev, "%s: failed to create wcd938x version entry\n",
  3056. __func__);
  3057. snd_info_free_entry(priv->entry);
  3058. return -ENOMEM;
  3059. }
  3060. version_entry->private_data = priv;
  3061. version_entry->size = WCD938X_VERSION_ENTRY_SIZE;
  3062. version_entry->content = SNDRV_INFO_CONTENT_DATA;
  3063. version_entry->c.ops = &wcd938x_info_ops;
  3064. if (snd_info_register(version_entry) < 0) {
  3065. snd_info_free_entry(version_entry);
  3066. snd_info_free_entry(priv->entry);
  3067. return -ENOMEM;
  3068. }
  3069. priv->version_entry = version_entry;
  3070. variant_entry = snd_info_create_card_entry(card->snd_card,
  3071. "variant",
  3072. priv->entry);
  3073. if (!variant_entry) {
  3074. dev_dbg(component->dev, "%s: failed to create wcd938x variant entry\n",
  3075. __func__);
  3076. snd_info_free_entry(version_entry);
  3077. snd_info_free_entry(priv->entry);
  3078. return -ENOMEM;
  3079. }
  3080. variant_entry->private_data = priv;
  3081. variant_entry->size = WCD938X_VARIANT_ENTRY_SIZE;
  3082. variant_entry->content = SNDRV_INFO_CONTENT_DATA;
  3083. variant_entry->c.ops = &wcd938x_variant_ops;
  3084. if (snd_info_register(variant_entry) < 0) {
  3085. snd_info_free_entry(variant_entry);
  3086. snd_info_free_entry(version_entry);
  3087. snd_info_free_entry(priv->entry);
  3088. return -ENOMEM;
  3089. }
  3090. priv->variant_entry = variant_entry;
  3091. return 0;
  3092. }
  3093. EXPORT_SYMBOL(wcd938x_info_create_codec_entry);
  3094. static int wcd938x_set_micbias_data(struct wcd938x_priv *wcd938x,
  3095. struct wcd938x_pdata *pdata)
  3096. {
  3097. int vout_ctl_1 = 0, vout_ctl_2 = 0, vout_ctl_3 = 0, vout_ctl_4 = 0;
  3098. int rc = 0;
  3099. if (!pdata) {
  3100. dev_err(wcd938x->dev, "%s: NULL pdata\n", __func__);
  3101. return -ENODEV;
  3102. }
  3103. /* set micbias voltage */
  3104. vout_ctl_1 = wcd938x_get_micb_vout_ctl_val(pdata->micbias.micb1_mv);
  3105. vout_ctl_2 = wcd938x_get_micb_vout_ctl_val(pdata->micbias.micb2_mv);
  3106. vout_ctl_3 = wcd938x_get_micb_vout_ctl_val(pdata->micbias.micb3_mv);
  3107. vout_ctl_4 = wcd938x_get_micb_vout_ctl_val(pdata->micbias.micb4_mv);
  3108. if (vout_ctl_1 < 0 || vout_ctl_2 < 0 || vout_ctl_3 < 0 ||
  3109. vout_ctl_4 < 0) {
  3110. rc = -EINVAL;
  3111. goto done;
  3112. }
  3113. regmap_update_bits(wcd938x->regmap, WCD938X_ANA_MICB1, 0x3F,
  3114. vout_ctl_1);
  3115. regmap_update_bits(wcd938x->regmap, WCD938X_ANA_MICB2, 0x3F,
  3116. vout_ctl_2);
  3117. regmap_update_bits(wcd938x->regmap, WCD938X_ANA_MICB3, 0x3F,
  3118. vout_ctl_3);
  3119. regmap_update_bits(wcd938x->regmap, WCD938X_ANA_MICB4, 0x3F,
  3120. vout_ctl_4);
  3121. done:
  3122. return rc;
  3123. }
  3124. static int wcd938x_soc_codec_probe(struct snd_soc_component *component)
  3125. {
  3126. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  3127. struct snd_soc_dapm_context *dapm =
  3128. snd_soc_component_get_dapm(component);
  3129. int variant;
  3130. int ret = -EINVAL;
  3131. dev_info(component->dev, "%s()\n", __func__);
  3132. wcd938x = snd_soc_component_get_drvdata(component);
  3133. if (!wcd938x)
  3134. return -EINVAL;
  3135. wcd938x->component = component;
  3136. snd_soc_component_init_regmap(component, wcd938x->regmap);
  3137. variant = (snd_soc_component_read32(component,
  3138. WCD938X_DIGITAL_EFUSE_REG_0) & 0x1E) >> 1;
  3139. wcd938x->variant = variant;
  3140. wcd938x->fw_data = devm_kzalloc(component->dev,
  3141. sizeof(*(wcd938x->fw_data)),
  3142. GFP_KERNEL);
  3143. if (!wcd938x->fw_data) {
  3144. dev_err(component->dev, "Failed to allocate fw_data\n");
  3145. ret = -ENOMEM;
  3146. goto err;
  3147. }
  3148. set_bit(WCD9XXX_MBHC_CAL, wcd938x->fw_data->cal_bit);
  3149. ret = wcd_cal_create_hwdep(wcd938x->fw_data,
  3150. WCD9XXX_CODEC_HWDEP_NODE, component);
  3151. if (ret < 0) {
  3152. dev_err(component->dev, "%s hwdep failed %d\n", __func__, ret);
  3153. goto err_hwdep;
  3154. }
  3155. ret = wcd938x_mbhc_init(&wcd938x->mbhc, component, wcd938x->fw_data);
  3156. if (ret) {
  3157. pr_err("%s: mbhc initialization failed\n", __func__);
  3158. goto err_hwdep;
  3159. }
  3160. snd_soc_dapm_ignore_suspend(dapm, "AMIC1");
  3161. snd_soc_dapm_ignore_suspend(dapm, "AMIC2");
  3162. snd_soc_dapm_ignore_suspend(dapm, "AMIC3");
  3163. snd_soc_dapm_ignore_suspend(dapm, "AMIC4");
  3164. snd_soc_dapm_ignore_suspend(dapm, "AMIC5");
  3165. snd_soc_dapm_ignore_suspend(dapm, "AMIC6");
  3166. snd_soc_dapm_ignore_suspend(dapm, "AMIC7");
  3167. snd_soc_dapm_ignore_suspend(dapm, "WCD_TX_OUTPUT");
  3168. snd_soc_dapm_ignore_suspend(dapm, "IN1_HPHL");
  3169. snd_soc_dapm_ignore_suspend(dapm, "IN2_HPHR");
  3170. snd_soc_dapm_ignore_suspend(dapm, "IN3_AUX");
  3171. snd_soc_dapm_ignore_suspend(dapm, "EAR");
  3172. snd_soc_dapm_ignore_suspend(dapm, "AUX");
  3173. snd_soc_dapm_ignore_suspend(dapm, "HPHL");
  3174. snd_soc_dapm_ignore_suspend(dapm, "HPHR");
  3175. snd_soc_dapm_sync(dapm);
  3176. wcd_cls_h_init(&wcd938x->clsh_info);
  3177. wcd938x_init_reg(component);
  3178. if (wcd938x->variant == WCD9380) {
  3179. ret = snd_soc_add_component_controls(component, wcd9380_snd_controls,
  3180. ARRAY_SIZE(wcd9380_snd_controls));
  3181. if (ret < 0) {
  3182. dev_err(component->dev,
  3183. "%s: Failed to add snd ctrls for variant: %d\n",
  3184. __func__, wcd938x->variant);
  3185. goto err_hwdep;
  3186. }
  3187. }
  3188. if (wcd938x->variant == WCD9385) {
  3189. ret = snd_soc_add_component_controls(component, wcd9385_snd_controls,
  3190. ARRAY_SIZE(wcd9385_snd_controls));
  3191. if (ret < 0) {
  3192. dev_err(component->dev,
  3193. "%s: Failed to add snd ctrls for variant: %d\n",
  3194. __func__, wcd938x->variant);
  3195. goto err_hwdep;
  3196. }
  3197. }
  3198. wcd938x->version = WCD938X_VERSION_1_0;
  3199. /* Register event notifier */
  3200. wcd938x->nblock.notifier_call = wcd938x_event_notify;
  3201. if (wcd938x->register_notifier) {
  3202. ret = wcd938x->register_notifier(wcd938x->handle,
  3203. &wcd938x->nblock,
  3204. true);
  3205. if (ret) {
  3206. dev_err(component->dev,
  3207. "%s: Failed to register notifier %d\n",
  3208. __func__, ret);
  3209. return ret;
  3210. }
  3211. }
  3212. wcd938x->dev_up = true;
  3213. return ret;
  3214. err_hwdep:
  3215. wcd938x->fw_data = NULL;
  3216. err:
  3217. return ret;
  3218. }
  3219. static void wcd938x_soc_codec_remove(struct snd_soc_component *component)
  3220. {
  3221. struct wcd938x_priv *wcd938x = snd_soc_component_get_drvdata(component);
  3222. if (!wcd938x) {
  3223. dev_err(component->dev, "%s: wcd938x is already NULL\n",
  3224. __func__);
  3225. return;
  3226. }
  3227. if (wcd938x->register_notifier)
  3228. wcd938x->register_notifier(wcd938x->handle,
  3229. &wcd938x->nblock,
  3230. false);
  3231. }
  3232. static struct snd_soc_component_driver soc_codec_dev_wcd938x = {
  3233. .name = WCD938X_DRV_NAME,
  3234. .probe = wcd938x_soc_codec_probe,
  3235. .remove = wcd938x_soc_codec_remove,
  3236. .controls = wcd938x_snd_controls,
  3237. .num_controls = ARRAY_SIZE(wcd938x_snd_controls),
  3238. .dapm_widgets = wcd938x_dapm_widgets,
  3239. .num_dapm_widgets = ARRAY_SIZE(wcd938x_dapm_widgets),
  3240. .dapm_routes = wcd938x_audio_map,
  3241. .num_dapm_routes = ARRAY_SIZE(wcd938x_audio_map),
  3242. };
  3243. static int wcd938x_reset(struct device *dev)
  3244. {
  3245. struct wcd938x_priv *wcd938x = NULL;
  3246. int rc = 0;
  3247. int value = 0;
  3248. if (!dev)
  3249. return -ENODEV;
  3250. wcd938x = dev_get_drvdata(dev);
  3251. if (!wcd938x)
  3252. return -EINVAL;
  3253. if (!wcd938x->rst_np) {
  3254. dev_err(dev, "%s: reset gpio device node not specified\n",
  3255. __func__);
  3256. return -EINVAL;
  3257. }
  3258. value = msm_cdc_pinctrl_get_state(wcd938x->rst_np);
  3259. if (value > 0)
  3260. return 0;
  3261. rc = msm_cdc_pinctrl_select_sleep_state(wcd938x->rst_np);
  3262. if (rc) {
  3263. dev_err(dev, "%s: wcd sleep state request fail!\n",
  3264. __func__);
  3265. return rc;
  3266. }
  3267. /* 20us sleep required after pulling the reset gpio to LOW */
  3268. usleep_range(20, 30);
  3269. rc = msm_cdc_pinctrl_select_active_state(wcd938x->rst_np);
  3270. if (rc) {
  3271. dev_err(dev, "%s: wcd active state request fail!\n",
  3272. __func__);
  3273. return rc;
  3274. }
  3275. /* 20us sleep required after pulling the reset gpio to HIGH */
  3276. usleep_range(20, 30);
  3277. return rc;
  3278. }
  3279. static int wcd938x_read_of_property_u32(struct device *dev, const char *name,
  3280. u32 *val)
  3281. {
  3282. int rc = 0;
  3283. rc = of_property_read_u32(dev->of_node, name, val);
  3284. if (rc)
  3285. dev_err(dev, "%s: Looking up %s property in node %s failed\n",
  3286. __func__, name, dev->of_node->full_name);
  3287. return rc;
  3288. }
  3289. static void wcd938x_dt_parse_micbias_info(struct device *dev,
  3290. struct wcd938x_micbias_setting *mb)
  3291. {
  3292. u32 prop_val = 0;
  3293. int rc = 0;
  3294. /* MB1 */
  3295. if (of_find_property(dev->of_node, "qcom,cdc-micbias1-mv",
  3296. NULL)) {
  3297. rc = wcd938x_read_of_property_u32(dev,
  3298. "qcom,cdc-micbias1-mv",
  3299. &prop_val);
  3300. if (!rc)
  3301. mb->micb1_mv = prop_val;
  3302. } else {
  3303. dev_info(dev, "%s: Micbias1 DT property not found\n",
  3304. __func__);
  3305. }
  3306. /* MB2 */
  3307. if (of_find_property(dev->of_node, "qcom,cdc-micbias2-mv",
  3308. NULL)) {
  3309. rc = wcd938x_read_of_property_u32(dev,
  3310. "qcom,cdc-micbias2-mv",
  3311. &prop_val);
  3312. if (!rc)
  3313. mb->micb2_mv = prop_val;
  3314. } else {
  3315. dev_info(dev, "%s: Micbias2 DT property not found\n",
  3316. __func__);
  3317. }
  3318. /* MB3 */
  3319. if (of_find_property(dev->of_node, "qcom,cdc-micbias3-mv",
  3320. NULL)) {
  3321. rc = wcd938x_read_of_property_u32(dev,
  3322. "qcom,cdc-micbias3-mv",
  3323. &prop_val);
  3324. if (!rc)
  3325. mb->micb3_mv = prop_val;
  3326. } else {
  3327. dev_info(dev, "%s: Micbias3 DT property not found\n",
  3328. __func__);
  3329. }
  3330. /* MB4 */
  3331. if (of_find_property(dev->of_node, "qcom,cdc-micbias4-mv",
  3332. NULL)) {
  3333. rc = wcd938x_read_of_property_u32(dev,
  3334. "qcom,cdc-micbias4-mv",
  3335. &prop_val);
  3336. if (!rc)
  3337. mb->micb4_mv = prop_val;
  3338. } else {
  3339. dev_info(dev, "%s: Micbias4 DT property not found\n",
  3340. __func__);
  3341. }
  3342. }
  3343. static int wcd938x_reset_low(struct device *dev)
  3344. {
  3345. struct wcd938x_priv *wcd938x = NULL;
  3346. int rc = 0;
  3347. if (!dev)
  3348. return -ENODEV;
  3349. wcd938x = dev_get_drvdata(dev);
  3350. if (!wcd938x)
  3351. return -EINVAL;
  3352. if (!wcd938x->rst_np) {
  3353. dev_err(dev, "%s: reset gpio device node not specified\n",
  3354. __func__);
  3355. return -EINVAL;
  3356. }
  3357. rc = msm_cdc_pinctrl_select_sleep_state(wcd938x->rst_np);
  3358. if (rc) {
  3359. dev_err(dev, "%s: wcd sleep state request fail!\n",
  3360. __func__);
  3361. return rc;
  3362. }
  3363. /* 20us sleep required after pulling the reset gpio to LOW */
  3364. usleep_range(20, 30);
  3365. return rc;
  3366. }
  3367. struct wcd938x_pdata *wcd938x_populate_dt_data(struct device *dev)
  3368. {
  3369. struct wcd938x_pdata *pdata = NULL;
  3370. pdata = devm_kzalloc(dev, sizeof(struct wcd938x_pdata),
  3371. GFP_KERNEL);
  3372. if (!pdata)
  3373. return NULL;
  3374. pdata->rst_np = of_parse_phandle(dev->of_node,
  3375. "qcom,wcd-rst-gpio-node", 0);
  3376. if (!pdata->rst_np) {
  3377. dev_err(dev, "%s: Looking up %s property in node %s failed\n",
  3378. __func__, "qcom,wcd-rst-gpio-node",
  3379. dev->of_node->full_name);
  3380. return NULL;
  3381. }
  3382. /* Parse power supplies */
  3383. msm_cdc_get_power_supplies(dev, &pdata->regulator,
  3384. &pdata->num_supplies);
  3385. if (!pdata->regulator || (pdata->num_supplies <= 0)) {
  3386. dev_err(dev, "%s: no power supplies defined for codec\n",
  3387. __func__);
  3388. return NULL;
  3389. }
  3390. pdata->rx_slave = of_parse_phandle(dev->of_node, "qcom,rx-slave", 0);
  3391. pdata->tx_slave = of_parse_phandle(dev->of_node, "qcom,tx-slave", 0);
  3392. wcd938x_dt_parse_micbias_info(dev, &pdata->micbias);
  3393. return pdata;
  3394. }
  3395. static irqreturn_t wcd938x_wd_handle_irq(int irq, void *data)
  3396. {
  3397. pr_err_ratelimited("%s: Watchdog interrupt for irq =%d triggered\n",
  3398. __func__, irq);
  3399. return IRQ_HANDLED;
  3400. }
  3401. static struct snd_soc_dai_driver wcd938x_dai[] = {
  3402. {
  3403. .name = "wcd938x_cdc",
  3404. .playback = {
  3405. .stream_name = "WCD938X_AIF Playback",
  3406. .rates = WCD938X_RATES | WCD938X_FRAC_RATES,
  3407. .formats = WCD938X_FORMATS,
  3408. .rate_max = 192000,
  3409. .rate_min = 8000,
  3410. .channels_min = 1,
  3411. .channels_max = 4,
  3412. },
  3413. .capture = {
  3414. .stream_name = "WCD938X_AIF Capture",
  3415. .rates = WCD938X_RATES | WCD938X_FRAC_RATES,
  3416. .formats = WCD938X_FORMATS,
  3417. .rate_max = 192000,
  3418. .rate_min = 8000,
  3419. .channels_min = 1,
  3420. .channels_max = 4,
  3421. },
  3422. },
  3423. };
  3424. static int wcd938x_bind(struct device *dev)
  3425. {
  3426. int ret = 0, i = 0;
  3427. struct wcd938x_pdata *pdata = dev_get_platdata(dev);
  3428. struct wcd938x_priv *wcd938x = dev_get_drvdata(dev);
  3429. /*
  3430. * Add 5msec delay to provide sufficient time for
  3431. * soundwire auto enumeration of slave devices as
  3432. * as per HW requirement.
  3433. */
  3434. usleep_range(5000, 5010);
  3435. ret = component_bind_all(dev, wcd938x);
  3436. if (ret) {
  3437. dev_err(dev, "%s: Slave bind failed, ret = %d\n",
  3438. __func__, ret);
  3439. return ret;
  3440. }
  3441. wcd938x->rx_swr_dev = get_matching_swr_slave_device(pdata->rx_slave);
  3442. if (!wcd938x->rx_swr_dev) {
  3443. dev_err(dev, "%s: Could not find RX swr slave device\n",
  3444. __func__);
  3445. ret = -ENODEV;
  3446. goto err;
  3447. }
  3448. wcd938x->tx_swr_dev = get_matching_swr_slave_device(pdata->tx_slave);
  3449. if (!wcd938x->tx_swr_dev) {
  3450. dev_err(dev, "%s: Could not find TX swr slave device\n",
  3451. __func__);
  3452. ret = -ENODEV;
  3453. goto err;
  3454. }
  3455. wcd938x->regmap = devm_regmap_init_swr(wcd938x->tx_swr_dev,
  3456. &wcd938x_regmap_config);
  3457. if (!wcd938x->regmap) {
  3458. dev_err(dev, "%s: Regmap init failed\n",
  3459. __func__);
  3460. goto err;
  3461. }
  3462. /* Set all interupts as edge triggered */
  3463. for (i = 0; i < wcd938x_regmap_irq_chip.num_regs; i++)
  3464. regmap_write(wcd938x->regmap,
  3465. (WCD938X_DIGITAL_INTR_LEVEL_0 + i), 0);
  3466. wcd938x_regmap_irq_chip.irq_drv_data = wcd938x;
  3467. wcd938x->irq_info.wcd_regmap_irq_chip = &wcd938x_regmap_irq_chip;
  3468. wcd938x->irq_info.codec_name = "WCD938X";
  3469. wcd938x->irq_info.regmap = wcd938x->regmap;
  3470. wcd938x->irq_info.dev = dev;
  3471. ret = wcd_irq_init(&wcd938x->irq_info, &wcd938x->virq);
  3472. if (ret) {
  3473. dev_err(wcd938x->dev, "%s: IRQ init failed: %d\n",
  3474. __func__, ret);
  3475. goto err;
  3476. }
  3477. wcd938x->tx_swr_dev->slave_irq = wcd938x->virq;
  3478. ret = wcd938x_set_micbias_data(wcd938x, pdata);
  3479. if (ret < 0) {
  3480. dev_err(dev, "%s: bad micbias pdata\n", __func__);
  3481. goto err_irq;
  3482. }
  3483. /* Request for watchdog interrupt */
  3484. wcd_request_irq(&wcd938x->irq_info, WCD938X_IRQ_HPHR_PDM_WD_INT,
  3485. "HPHR PDM WD INT", wcd938x_wd_handle_irq, NULL);
  3486. wcd_request_irq(&wcd938x->irq_info, WCD938X_IRQ_HPHL_PDM_WD_INT,
  3487. "HPHL PDM WD INT", wcd938x_wd_handle_irq, NULL);
  3488. wcd_request_irq(&wcd938x->irq_info, WCD938X_IRQ_AUX_PDM_WD_INT,
  3489. "AUX PDM WD INT", wcd938x_wd_handle_irq, NULL);
  3490. /* Disable watchdog interrupt for HPH and AUX */
  3491. wcd_disable_irq(&wcd938x->irq_info, WCD938X_IRQ_HPHR_PDM_WD_INT);
  3492. wcd_disable_irq(&wcd938x->irq_info, WCD938X_IRQ_HPHL_PDM_WD_INT);
  3493. wcd_disable_irq(&wcd938x->irq_info, WCD938X_IRQ_AUX_PDM_WD_INT);
  3494. ret = snd_soc_register_component(dev, &soc_codec_dev_wcd938x,
  3495. wcd938x_dai, ARRAY_SIZE(wcd938x_dai));
  3496. if (ret) {
  3497. dev_err(dev, "%s: Codec registration failed\n",
  3498. __func__);
  3499. goto err_irq;
  3500. }
  3501. return ret;
  3502. err_irq:
  3503. wcd_irq_exit(&wcd938x->irq_info, wcd938x->virq);
  3504. err:
  3505. component_unbind_all(dev, wcd938x);
  3506. return ret;
  3507. }
  3508. static void wcd938x_unbind(struct device *dev)
  3509. {
  3510. struct wcd938x_priv *wcd938x = dev_get_drvdata(dev);
  3511. wcd_free_irq(&wcd938x->irq_info, WCD938X_IRQ_HPHR_PDM_WD_INT, NULL);
  3512. wcd_free_irq(&wcd938x->irq_info, WCD938X_IRQ_HPHL_PDM_WD_INT, NULL);
  3513. wcd_free_irq(&wcd938x->irq_info, WCD938X_IRQ_AUX_PDM_WD_INT, NULL);
  3514. wcd_irq_exit(&wcd938x->irq_info, wcd938x->virq);
  3515. snd_soc_unregister_component(dev);
  3516. component_unbind_all(dev, wcd938x);
  3517. }
  3518. static const struct of_device_id wcd938x_dt_match[] = {
  3519. { .compatible = "qcom,wcd938x-codec", .data = "wcd938x"},
  3520. {}
  3521. };
  3522. static const struct component_master_ops wcd938x_comp_ops = {
  3523. .bind = wcd938x_bind,
  3524. .unbind = wcd938x_unbind,
  3525. };
  3526. static int wcd938x_compare_of(struct device *dev, void *data)
  3527. {
  3528. return dev->of_node == data;
  3529. }
  3530. static void wcd938x_release_of(struct device *dev, void *data)
  3531. {
  3532. of_node_put(data);
  3533. }
  3534. static int wcd938x_add_slave_components(struct device *dev,
  3535. struct component_match **matchptr)
  3536. {
  3537. struct device_node *np, *rx_node, *tx_node;
  3538. np = dev->of_node;
  3539. rx_node = of_parse_phandle(np, "qcom,rx-slave", 0);
  3540. if (!rx_node) {
  3541. dev_err(dev, "%s: Rx-slave node not defined\n", __func__);
  3542. return -ENODEV;
  3543. }
  3544. of_node_get(rx_node);
  3545. component_match_add_release(dev, matchptr,
  3546. wcd938x_release_of,
  3547. wcd938x_compare_of,
  3548. rx_node);
  3549. tx_node = of_parse_phandle(np, "qcom,tx-slave", 0);
  3550. if (!tx_node) {
  3551. dev_err(dev, "%s: Tx-slave node not defined\n", __func__);
  3552. return -ENODEV;
  3553. }
  3554. of_node_get(tx_node);
  3555. component_match_add_release(dev, matchptr,
  3556. wcd938x_release_of,
  3557. wcd938x_compare_of,
  3558. tx_node);
  3559. return 0;
  3560. }
  3561. static int wcd938x_probe(struct platform_device *pdev)
  3562. {
  3563. struct component_match *match = NULL;
  3564. struct wcd938x_priv *wcd938x = NULL;
  3565. struct wcd938x_pdata *pdata = NULL;
  3566. struct wcd_ctrl_platform_data *plat_data = NULL;
  3567. struct device *dev = &pdev->dev;
  3568. int ret;
  3569. wcd938x = devm_kzalloc(dev, sizeof(struct wcd938x_priv),
  3570. GFP_KERNEL);
  3571. if (!wcd938x)
  3572. return -ENOMEM;
  3573. dev_set_drvdata(dev, wcd938x);
  3574. wcd938x->dev = dev;
  3575. pdata = wcd938x_populate_dt_data(dev);
  3576. if (!pdata) {
  3577. dev_err(dev, "%s: Fail to obtain platform data\n", __func__);
  3578. return -EINVAL;
  3579. }
  3580. dev->platform_data = pdata;
  3581. wcd938x->rst_np = pdata->rst_np;
  3582. ret = msm_cdc_init_supplies(dev, &wcd938x->supplies,
  3583. pdata->regulator, pdata->num_supplies);
  3584. if (!wcd938x->supplies) {
  3585. dev_err(dev, "%s: Cannot init wcd supplies\n",
  3586. __func__);
  3587. return ret;
  3588. }
  3589. plat_data = dev_get_platdata(dev->parent);
  3590. if (!plat_data) {
  3591. dev_err(dev, "%s: platform data from parent is NULL\n",
  3592. __func__);
  3593. return -EINVAL;
  3594. }
  3595. wcd938x->handle = (void *)plat_data->handle;
  3596. if (!wcd938x->handle) {
  3597. dev_err(dev, "%s: handle is NULL\n", __func__);
  3598. return -EINVAL;
  3599. }
  3600. wcd938x->update_wcd_event = plat_data->update_wcd_event;
  3601. if (!wcd938x->update_wcd_event) {
  3602. dev_err(dev, "%s: update_wcd_event api is null!\n",
  3603. __func__);
  3604. return -EINVAL;
  3605. }
  3606. wcd938x->register_notifier = plat_data->register_notifier;
  3607. if (!wcd938x->register_notifier) {
  3608. dev_err(dev, "%s: register_notifier api is null!\n",
  3609. __func__);
  3610. return -EINVAL;
  3611. }
  3612. ret = msm_cdc_enable_static_supplies(&pdev->dev, wcd938x->supplies,
  3613. pdata->regulator,
  3614. pdata->num_supplies);
  3615. if (ret) {
  3616. dev_err(dev, "%s: wcd static supply enable failed!\n",
  3617. __func__);
  3618. return ret;
  3619. }
  3620. ret = wcd938x_parse_port_mapping(dev, "qcom,rx_swr_ch_map",
  3621. CODEC_RX);
  3622. ret |= wcd938x_parse_port_mapping(dev, "qcom,tx_swr_ch_map",
  3623. CODEC_TX);
  3624. if (ret) {
  3625. dev_err(dev, "Failed to read port mapping\n");
  3626. goto err;
  3627. }
  3628. mutex_init(&wcd938x->wakeup_lock);
  3629. mutex_init(&wcd938x->micb_lock);
  3630. ret = wcd938x_add_slave_components(dev, &match);
  3631. if (ret)
  3632. goto err_lock_init;
  3633. wcd938x_reset(dev);
  3634. wcd938x->wakeup = wcd938x_wakeup;
  3635. return component_master_add_with_match(dev,
  3636. &wcd938x_comp_ops, match);
  3637. err_lock_init:
  3638. mutex_destroy(&wcd938x->micb_lock);
  3639. mutex_destroy(&wcd938x->wakeup_lock);
  3640. err:
  3641. return ret;
  3642. }
  3643. static int wcd938x_remove(struct platform_device *pdev)
  3644. {
  3645. struct wcd938x_priv *wcd938x = NULL;
  3646. wcd938x = platform_get_drvdata(pdev);
  3647. component_master_del(&pdev->dev, &wcd938x_comp_ops);
  3648. mutex_destroy(&wcd938x->micb_lock);
  3649. mutex_destroy(&wcd938x->wakeup_lock);
  3650. dev_set_drvdata(&pdev->dev, NULL);
  3651. return 0;
  3652. }
  3653. #ifdef CONFIG_PM_SLEEP
  3654. static int wcd938x_suspend(struct device *dev)
  3655. {
  3656. return 0;
  3657. }
  3658. static int wcd938x_resume(struct device *dev)
  3659. {
  3660. return 0;
  3661. }
  3662. static const struct dev_pm_ops wcd938x_dev_pm_ops = {
  3663. SET_SYSTEM_SLEEP_PM_OPS(
  3664. wcd938x_suspend,
  3665. wcd938x_resume
  3666. )
  3667. };
  3668. #endif
  3669. static struct platform_driver wcd938x_codec_driver = {
  3670. .probe = wcd938x_probe,
  3671. .remove = wcd938x_remove,
  3672. .driver = {
  3673. .name = "wcd938x_codec",
  3674. .owner = THIS_MODULE,
  3675. .of_match_table = of_match_ptr(wcd938x_dt_match),
  3676. #ifdef CONFIG_PM_SLEEP
  3677. .pm = &wcd938x_dev_pm_ops,
  3678. #endif
  3679. .suppress_bind_attrs = true,
  3680. },
  3681. };
  3682. module_platform_driver(wcd938x_codec_driver);
  3683. MODULE_DESCRIPTION("WCD938X Codec driver");
  3684. MODULE_LICENSE("GPL v2");