wcd938x.c 127 KB

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