wcd938x.c 104 KB

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