phy_audio_adc.c 138 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651
  1. /*
  2. * Copyright (c) 2020 Actions Semiconductor Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file
  8. * @brief Audio ADC physical implementation
  9. */
  10. /*
  11. * Features
  12. * - Support 4 independent channels with high performance ADC.
  13. * - ADC0/ADC1 support ANC
  14. * - 1 x FIFOs (ADC0/ADC1 uses FIFO0(4 * 16 level * 24bits))
  15. * - Support single ended and full differential input
  16. * - Support 4 DMIC input
  17. * - Programmable HPF
  18. * - Support 3 different kinds of frequency response curves
  19. * - Sample rate support 8k/12k/11.025k/16k/22.05k/24k/32k/44.1k/48k/88.2k/96k
  20. */
  21. /*
  22. * Signal List
  23. * - AVCC: Analog power
  24. * - AGND: Analog ground
  25. * - INPUT0P: Analog input for ADC0 or differential input ADC0 INPUT0N
  26. * - INPUT0N: Analog input to ADC1 or differential input ADC0 INPUT0P
  27. * - INPUT1P: Analog input to ADC0/1 or differential input ADC1 INPUT1N
  28. * - INPUT1N: Analog input to ADC1 or differential input ADC1 INPUT1P
  29. * - INPUT2P: Analog input to ADC0/2 or differential input ADC2 INPUT2N
  30. * - INPUT2N: Analog input to ADC1/2 or differential input ADC2 INPUT2P
  31. * - INPUT3P: Analog input to ADC3 or differential input ADC3 INPUT3P
  32. * - INPUT3N: Analog input to ADC3 or differential input ADC3 INPUT3P
  33. * - DMIC_CLK: DMIC clk output
  34. * - DMIC_DATA: DMIC data input
  35. */
  36. #include <kernel.h>
  37. #include <device.h>
  38. #include <ksched.h>
  39. #include <string.h>
  40. #include <errno.h>
  41. #include <soc.h>
  42. #include <board_cfg.h>
  43. #include "../phy_audio_common.h"
  44. #include "../audio_acts_utils.h"
  45. #include <drivers/audio/audio_in.h>
  46. #ifdef CONFIG_CFG_DRV
  47. #include <config.h>
  48. #include <drivers/cfg_drv/driver_config.h>
  49. #endif
  50. #include <logging/log.h>
  51. LOG_MODULE_REGISTER(adc0, CONFIG_LOG_DEFAULT_LEVEL);
  52. /***************************************************************************************************
  53. * ADC_DIGCTL
  54. */
  55. #define ADC_DIGCTL_ADC_OVFS_SHIFT (16)
  56. #define ADC_DIGCTL_ADC_OVFS_MASK (0x3 << ADC_DIGCTL_ADC_OVFS_SHIFT)
  57. #define ADC_DIGCTL_ADC_OVFS(x) ((x) << ADC_DIGCTL_ADC_OVFS_SHIFT)
  58. #define ADC_DIGCTL_ADC3_DIG_EN BIT(15) /* ADC3 digital enable */
  59. #define ADC_DIGCTL_ADC2_DIG_EN BIT(14) /* ADC2 digital enable */
  60. #define ADC_DIGCTL_ADC1_DIG_EN BIT(13) /* ADC1 digital enable */
  61. #define ADC_DIGCTL_ADC0_DIG_EN BIT(12) /* ADC0 digital enable */
  62. #define ADC_DIGCTL_ADC_DIG_SHIFT (12)
  63. #define ADC_DIGCTL_ADC_DIG_MASK ((0xF) << ADC_DIGCTL_ADC_DIG_SHIFT)
  64. #define ADC_DIGCTL_DMIC_PRE_GAIN_SHIFT (8)
  65. #define ADC_DIGCTL_DMIC_PRE_GAIN_MASK (0x7 << ADC_DIGCTL_DMIC_PRE_GAIN_SHIFT)
  66. #define ADC_DIGCTL_DMIC_PRE_GAIN(x) ((x) << ADC_DIGCTL_DMIC_PRE_GAIN_SHIFT)
  67. #define ADC_DIGCTL_DMIC23_CHS BIT(7) /* 0: DMIC23 channel select L/R; 1: DMIC23 channel select R/L*/
  68. #define ADC_DIGCTL_DMIC01_CHS BIT(6)
  69. #define ADC_DIGCTL_ADC_FIR_MD_SEL_SHIFT (4)
  70. #define ADC_DIGCTL_ADC_FIR_MD_SEL_MASK (0x3 << ADC_DIGCTL_ADC_FIR_MD_SEL_SHIFT)
  71. #define ADC_DIGCTL_ADC_FIR_MD_SEL(x) ((x) << ADC_DIGCTL_ADC_FIR_MD_SEL_SHIFT)
  72. #define ADC_DIGCTL_ADDEN BIT(3)
  73. #define ADC_DIGCTL_AADEN BIT(2)
  74. /***************************************************************************************************
  75. * CH0_DIGCTL
  76. */
  77. #define CH0_DIGCTL_MIC_SEL BIT(17) /* select ADC analog or digital MIC */
  78. #define CH0_DIGCTL_DAT_OUT_EN BIT(15) /* 0: no output on ADC0 */
  79. #define CH0_DIGCTL_HPF_AS_TS_SHIFT (13)
  80. #define CH0_DIGCTL_HPF_AS_TS_MASK (0x3 << CH0_DIGCTL_HPF_AS_TS_SHIFT)
  81. #define CH0_DIGCTL_HPF_AS_TS(x) ((x) << CH0_DIGCTL_HPF_AS_TS_SHIFT)
  82. #define CH0_DIGCTL_HPF_AS_EN BIT(12) /* HPF auto-set enable */
  83. #define CH0_DIGCTL_HPFEN BIT(11)
  84. #define CH0_DIGCTL_HPF_S BIT(10)
  85. #define CH0_DIGCTL_HPF_N_SHIFT (4)
  86. #define CH0_DIGCTL_HPF_N_MASK (0x3F << CH0_DIGCTL_HPF_N_SHIFT)
  87. #define CH0_DIGCTL_HPF_N(x) ((x) << CH0_DIGCTL_HPF_N_SHIFT)
  88. #define CH0_DIGCTL_ADCGC_SHIFT (0)
  89. #define CH0_DIGCTL_ADCGC_MASK (0xF << CH0_DIGCTL_ADCGC_SHIFT)
  90. #define CH0_DIGCTL_ADCGC(x) ((x) << CH0_DIGCTL_ADCGC_SHIFT)
  91. /***************************************************************************************************
  92. * CH1_DIGCTL
  93. */
  94. #define CH1_DIGCTL_MIC_SEL BIT(17)
  95. #define CH1_DIGCTL_DAT_OUT_EN BIT(15)
  96. #define CH1_DIGCTL_HPF_AS_TS_SHIFT (13)
  97. #define CH1_DIGCTL_HPF_AS_TS_MASK (0x3 << CH1_DIGCTL_HPF_AS_TS_SHIFT)
  98. #define CH1_DIGCTL_HPF_AS_TS(x) ((x) << CH1_DIGCTL_HPF_AS_TS_SHIFT)
  99. #define CH1_DIGCTL_HPF_AS_EN BIT(12)
  100. #define CH1_DIGCTL_HPFEN BIT(11)
  101. #define CH1_DIGCTL_HPF_S BIT(10)
  102. #define CH1_DIGCTL_HPF_N_SHIFT (4)
  103. #define CH1_DIGCTL_HPF_N_MASK (0x3F << CH1_DIGCTL_HPF_N_SHIFT)
  104. #define CH1_DIGCTL_HPF_N(x) ((x) << CH1_DIGCTL_HPF_N_SHIFT)
  105. #define CH1_DIGCTL_ADCGC_SHIFT (0)
  106. #define CH1_DIGCTL_ADCGC_MASK (0xF << CH1_DIGCTL_ADCGC_SHIFT)
  107. #define CH1_DIGCTL_ADCGC(x) ((x) << CH1_DIGCTL_ADCGC_SHIFT)
  108. /***************************************************************************************************
  109. * CH2_DIGCTL
  110. */
  111. #define CH2_DIGCTL_MIC_SEL BIT(17)
  112. #define CH2_DIGCTL_DAT_OUT_EN BIT(15)
  113. #define CH2_DIGCTL_HPF_AS_TS_SHIFT (13)
  114. #define CH2_DIGCTL_HPF_AS_TS_MASK (0x3 << CH2_DIGCTL_HPF_AS_TS_SHIFT)
  115. #define CH2_DIGCTL_HPF_AS_TS(x) ((x) << CH2_DIGCTL_HPF_AS_TS_SHIFT)
  116. #define CH2_DIGCTL_HPF_AS_EN BIT(12)
  117. #define CH2_DIGCTL_HPFEN BIT(11)
  118. #define CH2_DIGCTL_HPF_S BIT(10)
  119. #define CH2_DIGCTL_HPF_N_SHIFT (4)
  120. #define CH2_DIGCTL_HPF_N_MASK (0x3F << CH2_DIGCTL_HPF_N_SHIFT)
  121. #define CH2_DIGCTL_HPF_N(x) ((x) << CH2_DIGCTL_HPF_N_SHIFT)
  122. #define CH2_DIGCTL_ADCGC_SHIFT (0)
  123. #define CH2_DIGCTL_ADCGC_MASK (0xF << CH2_DIGCTL_ADCGC_SHIFT)
  124. #define CH2_DIGCTL_ADCGC(x) ((x) << CH2_DIGCTL_ADCGC_SHIFT)
  125. /***************************************************************************************************
  126. * CH3_DIGCTL
  127. */
  128. #define CH3_DIGCTL_MIC_SEL BIT(17)
  129. #define CH3_DIGCTL_DAT_OUT_EN BIT(15)
  130. #define CH3_DIGCTL_HPF_AS_TS_SHIFT (13)
  131. #define CH3_DIGCTL_HPF_AS_TS_MASK (0x3 << CH3_DIGCTL_HPF_AS_TS_SHIFT)
  132. #define CH3_DIGCTL_HPF_AS_TS(x) ((x) << CH3_DIGCTL_HPF_AS_TS_SHIFT)
  133. #define CH3_DIGCTL_HPF_AS_EN BIT(12)
  134. #define CH3_DIGCTL_HPFEN BIT(11)
  135. #define CH3_DIGCTL_HPF_S BIT(10)
  136. #define CH3_DIGCTL_HPF_N_SHIFT (4)
  137. #define CH3_DIGCTL_HPF_N_MASK (0x3F << CH3_DIGCTL_HPF_N_SHIFT)
  138. #define CH3_DIGCTL_HPF_N(x) ((x) << CH3_DIGCTL_HPF_N_SHIFT)
  139. #define CH3_DIGCTL_ADCGC_SHIFT (0)
  140. #define CH3_DIGCTL_ADCGC_MASK (0xF << CH3_DIGCTL_ADCGC_SHIFT)
  141. #define CH3_DIGCTL_ADCGC(x) ((x) << CH3_DIGCTL_ADCGC_SHIFT)
  142. /***************************************************************************************************
  143. * ADC_FIFOCTL
  144. */
  145. #define ADC_FIFOCTL_DRQ_LEVEL_SHIFT (8)
  146. #define ADC_FIFOCTL_DRQ_LEVEL_MASK (0x3F << ADC_FIFOCTL_DRQ_LEVEL_SHIFT)
  147. #define ADC_FIFOCTL_DRQ_LEVEL(x) ((x) << ADC_FIFOCTL_DRQ_LEVEL_SHIFT)
  148. #define ADC_FIFOCTL_ADCFIFO0_DMAWIDTH BIT(7)
  149. #define ADC_FIFOCTL_ADF0OS_SHIFT (4)
  150. #define ADC_FIFOCTL_ADF0OS_MASK (0x3 << ADC_FIFOCTL_ADF0OS_SHIFT)
  151. #define ADC_FIFOCTL_ADF0OS(x) ((x) << ADC_FIFOCTL_ADF0OS_SHIFT)
  152. #define ADC_FIFOCTL_ADF0FIE BIT(2)
  153. #define ADC_FIFOCTL_ADF0FDE BIT(1)
  154. #define ADC_FIFOCTL_ADF0RT BIT(0)
  155. /***************************************************************************************************
  156. * ADC_STAT
  157. */
  158. #define ADC_STAT_FIFO0_ER BIT(9)
  159. #define ADC_STAT_ADF0EF BIT(8)
  160. #define ADC_STAT_ADF0IP BIT(7)
  161. #define ADC_STAT_ADF0S_SHIFT (0)
  162. #define ADC_STAT_ADF0S_MASK (0x7F << ADC_STAT_ADF0S_SHIFT)
  163. /***************************************************************************************************
  164. * ADC_FIFO0_DAT
  165. */
  166. #define ADC_FIFO0_DAT_ADDAT_SHIFT (8)
  167. #define ADC_FIFO0_DAT_ADDAT_MASK (0xFFFFFF << ADC_FIFO0_DAT_ADDAT_SHIFT)
  168. #define ADC_FIFO0_DAT_ADDAT(x) ((x) << ADC_FIFO0_DAT_ADDAT_SHIFT)
  169. /***************************************************************************************************
  170. * AAL0_CTL
  171. */
  172. #define AAL0_CTL_AAL_EN BIT(17)
  173. #define AAL0_CTL_AAL_RECOVER_MODE BIT(16)
  174. #define AAL0_CTL_AAL_FL_SHIFT (14)
  175. #define AAL0_CTL_AAL_FL_MASK (0x3 << AAL0_CTL_AAL_FL_SHIFT)
  176. #define AAL0_CTL_AAL_FL(x) ((x) << AAL0_CTL_AAL_FL_SHIFT)
  177. #define AAL0_CTL_AAL_VT1_SHIFT (12)
  178. #define AAL0_CTL_AAL_VT1_MASK (0x3 << AAL0_CTL_AAL_VT1_SHIFT)
  179. #define AAL0_CTL_AAL_VT1(x) ((x) << AAL0_CTL_AAL_VT1_SHIFT)
  180. #define AAL0_CTL_AAL_VT0_SHIFT (8)
  181. #define AAL0_CTL_AAL_VT0_MASK (0x7 << AAL0_CTL_AAL_VT0_SHIFT)
  182. #define AAL0_CTL_AAL_VT0(x) ((x) << AAL0_CTL_AAL_VT0_SHIFT)
  183. #define AAL0_CTL_AAL_MAX_SHIFT (4)
  184. #define AAL0_CTL_AAL_MAX_MASK (0xF << AAL0_CTL_AAL_MAX_SHIFT)
  185. #define AAL0_CTL_AAL_MAX(x) ((x) << AAL0_CTL_AAL_MAX_SHIFT)
  186. #define AAL0_CTL_AAL_CNT_SHIFT (0)
  187. #define AAL0_CTL_AAL_CNT_MASK (0xF << AAL0_CTL_AAL_CNT_SHIFT)
  188. #define AAL0_CTL_AAL_CNT(x) ((x) << AAL0_CTL_AAL_CNT_SHIFT)
  189. /***************************************************************************************************
  190. * AAL1_CTL
  191. */
  192. #define AAL1_CTL_AAL_EN BIT(17)
  193. #define AAL1_CTL_AAL_RECOVER_MODE BIT(16)
  194. #define AAL1_CTL_AAL_FL_SHIFT (14)
  195. #define AAL1_CTL_AAL_FL_MASK (0x3 << AAL1_CTL_AAL_FL_SHIFT)
  196. #define AAL1_CTL_AAL_FL(x) ((x) << AAL1_CTL_AAL_FL_SHIFT)
  197. #define AAL1_CTL_AAL_VT1_SHIFT (12)
  198. #define AAL1_CTL_AAL_VT1_MASK (0x3 << AAL1_CTL_AAL_VT1_SHIFT)
  199. #define AAL1_CTL_AAL_VT1(x) ((x) << AAL1_CTL_AAL_VT1_SHIFT)
  200. #define AAL1_CTL_AAL_VT0_SHIFT (8)
  201. #define AAL1_CTL_AAL_VT0_MASK (0x7 << AAL1_CTL_AAL_VT0_SHIFT)
  202. #define AAL1_CTL_AAL_VT0(x) ((x) << AAL1_CTL_AAL_VT0_SHIFT)
  203. #define AAL1_CTL_AAL_MAX_SHIFT (4)
  204. #define AAL1_CTL_AAL_MAX_MASK (0xF << AAL1_CTL_AAL_MAX_SHIFT)
  205. #define AAL1_CTL_AAL_MAX(x) ((x) << AAL1_CTL_AAL_MAX_SHIFT)
  206. #define AAL1_CTL_AAL_CNT_SHIFT (0)
  207. #define AAL1_CTL_AAL_CNT_MASK (0xF << AAL1_CTL_AAL_CNT_SHIFT)
  208. #define AAL1_CTL_AAL_CNT(x) ((x) << AAL1_CTL_AAL_CNT_SHIFT)
  209. /***************************************************************************************************
  210. * AAL2_CTL
  211. */
  212. #define AAL2_CTL_AAL_EN BIT(17)
  213. #define AAL2_CTL_AAL_RECOVER_MODE BIT(16)
  214. #define AAL2_CTL_AAL_FL_SHIFT (14)
  215. #define AAL2_CTL_AAL_FL_MASK (0x3 << AAL2_CTL_AAL_FL_SHIFT)
  216. #define AAL2_CTL_AAL_FL(x) ((x) << AAL2_CTL_AAL_FL_SHIFT)
  217. #define AAL2_CTL_AAL_VT1_SHIFT (12)
  218. #define AAL2_CTL_AAL_VT1_MASK (0x3 << AAL2_CTL_AAL_VT1_SHIFT)
  219. #define AAL2_CTL_AAL_VT0_SHIFT (8)
  220. #define AAL2_CTL_AAL_VT0_MASK (0x7 << AAL2_CTL_AAL_VT0_SHIFT)
  221. #define AAL2_CTL_AAL_VT0(x) ((x) << AAL2_CTL_AAL_VT0_SHIFT)
  222. #define AAL2_CTL_AAL_MAX_SHIFT (4)
  223. #define AAL2_CTL_AAL_MAX_MASK (0xF << AAL2_CTL_AAL_MAX_SHIFT)
  224. #define AAL2_CTL_AAL_MAX(x) ((x) << AAL2_CTL_AAL_MAX_SHIFT)
  225. #define AAL2_CTL_AAL_CNT_SHIFT (0)
  226. #define AAL2_CTL_AAL_CNT_MASK (0xF << AAL2_CTL_AAL_CNT_SHIFT)
  227. #define AAL2_CTL_AAL_CNT(x) ((x) << AAL2_CTL_AAL_CNT_SHIFT)
  228. /***************************************************************************************************
  229. * AAL3_CTL
  230. */
  231. #define AAL3_CTL_AAL_EN BIT(17)
  232. #define AAL3_CTL_AAL_RECOVER_MODE BIT(16)
  233. #define AAL3_CTL_AAL_FL_SHIFT (14)
  234. #define AAL3_CTL_AAL_FL_MASK (0x3 << AAL3_CTL_AAL_FL_SHIFT)
  235. #define AAL3_CTL_AAL_FL(x) ((x) << AAL3_CTL_AAL_FL_SHIFT)
  236. #define AAL3_CTL_AAL_VT1_SHIFT (12)
  237. #define AAL3_CTL_AAL_VT1_MASK (0x3 << AAL3_CTL_AAL_VT1_SHIFT)
  238. #define AAL3_CTL_AAL_VT0_SHIFT (8)
  239. #define AAL3_CTL_AAL_VT0_MASK (0x7 << AAL3_CTL_AAL_VT0_SHIFT)
  240. #define AAL3_CTL_AAL_VT0(x) ((x) << AAL3_CTL_AAL_VT0_SHIFT)
  241. #define AAL3_CTL_AAL_MAX_SHIFT (4)
  242. #define AAL3_CTL_AAL_MAX_MASK (0xF << AAL3_CTL_AAL_MAX_SHIFT)
  243. #define AAL3_CTL_AAL_MAX(x) ((x) << AAL3_CTL_AAL_MAX_SHIFT)
  244. #define AAL3_CTL_AAL_CNT_SHIFT (0)
  245. #define AAL3_CTL_AAL_CNT_MASK (0xF << AAL3_CTL_AAL_CNT_SHIFT)
  246. #define AAL3_CTL_AAL_CNT(x) ((x) << AAL3_CTL_AAL_CNT_SHIFT)
  247. /***************************************************************************************************
  248. * ADC_CTL0
  249. */
  250. #define ADC0_CTL_VRDA0_EN BIT(20) /* VRAD0 enable */
  251. #define ADC0_CTL_ADC0_BINV BIT(19) /* ADC0 channel output phase invert */
  252. #define ADC0_CTL_FDBUF0_IRS_SHIFT (16) /* FDBUF0 input resistor select */
  253. #define ADC0_CTL_FDBUF0_IRS_MASK (0x7 << ADC0_CTL_FDBUF0_IRS_SHIFT)
  254. #define ADC0_CTL_FDBUF0_IRS(x) ((x) << ADC0_CTL_FDBUF0_IRS_SHIFT)
  255. #define ADC0_CTL_PREAM0_PG_SHIFT (12) /* PREAMP0 OP feedback resistor select */
  256. #define ADC0_CTL_PREAM0_PG_MASK (0xF << ADC0_CTL_PREAM0_PG_SHIFT)
  257. #define ADC0_CTL_PREAM0_PG(x) ((x) << ADC0_CTL_PREAM0_PG_SHIFT)
  258. #define ADC0_CTL_ADC0_CAPFC_EN BIT(11) /* input cap to ADC 0 channel fast charge enable */
  259. #define ADC0_CTL_FDBUF0_EN BIT(10) /* FD BUF OP 0 enable */
  260. #define ADC0_CTL_PREOP0_EN BIT(9) /* PREOP 0 enable */
  261. #define ADC0_CTL_ADC0_EN BIT(8) /* ADC0 channel sdm enable */
  262. #define ADC0_CTL_INPUT0N_EN_SHIFT (6) /* INPUT0N pad to ADC0/2 channel input enable */
  263. #define ADC0_CTL_INPUT0N_EN_MASK (0x3 << ADC0_CTL_INPUT0N_EN_SHIFT)
  264. #define ADC0_CTL_INPUT0N_EN(x) ((x) << ADC0_CTL_INPUT0N_EN_SHIFT)
  265. #define ADC0_CTL_INPUT0P_EN_SHIFT (4) /* INPUT0P pad to ADC0 channel input enable */
  266. #define ADC0_CTL_INPUT0P_EN_MASK (0x3 << ADC0_CTL_INPUT0P_EN_SHIFT)
  267. #define ADC0_CTL_INPUT0P_EN(x) ((x) << ADC0_CTL_INPUT0P_EN_SHIFT)
  268. #define ADC0_CTL_INPUT0_IN_MODE BIT(2) /* 0: differential input mode; 1: single end input mode */
  269. #define ADC0_CTL_INPUT0_IRS_SHIFT (0) /* INPUT0 input resistor select */
  270. #define ADC0_CTL_INPUT0_IRS_MASK (0x3 << ADC0_CTL_INPUT0_IRS_SHIFT)
  271. #define ADC0_CTL_INPUT0_IRS(x) ((x) << ADC0_CTL_INPUT0_IRS_SHIFT)
  272. /***************************************************************************************************
  273. * ADC_CTL1
  274. */
  275. #define ADC1_CTL_VRDA1_EN BIT(20) /* VRAD1 enable */
  276. #define ADC1_CTL_ADC1_BINV BIT(19) /* ADC1 channel output phase invert */
  277. #define ADC1_CTL_FDBUF1_IRS_SHIFT (16) /* FDBUF1 input resistor select */
  278. #define ADC1_CTL_FDBUF1_IRS_MASK (0x7 << ADC1_CTL_FDBUF1_IRS_SHIFT)
  279. #define ADC1_CTL_FDBUF1_IRS(x) ((x) << ADC1_CTL_FDBUF1_IRS_SHIFT)
  280. #define ADC1_CTL_PREAM1_PG_SHIFT (12) /* PREAMP1 OP feedback resistor select */
  281. #define ADC1_CTL_PREAM1_PG_MASK (0xF << ADC1_CTL_PREAM1_PG_SHIFT)
  282. #define ADC1_CTL_PREAM1_PG(x) ((x) << ADC1_CTL_PREAM1_PG_SHIFT)
  283. #define ADC1_CTL_ADC1_CAPFC_EN BIT(11) /* input cap to ADC 1 channel fast charge enable */
  284. #define ADC1_CTL_FDBUF1_EN BIT(10) /* FD BUF OP 1 enable */
  285. #define ADC1_CTL_PREOP1_EN BIT(9) /* PREOP 1 enable */
  286. #define ADC1_CTL_ADC1_EN BIT(8) /* ADC1 channel sdm enable */
  287. #define ADC1_CTL_INPUT1N_EN_SHIFT (6) /* INPUT1N pad to ADC1/3 channel input enable */
  288. #define ADC1_CTL_INPUT1N_EN_MASK (0x3 << ADC0_CTL_INPUT0N_EN_SHIFT)
  289. #define ADC1_CTL_INPUT1N_EN(x) ((x) << ADC0_CTL_INPUT0N_EN_SHIFT)
  290. #define ADC1_CTL_INPUT1P_EN_SHIFT (4) /* INPUT1P pad to ADC1 channel input enable */
  291. #define ADC1_CTL_INPUT1P_EN_MASK (0x3 << ADC0_CTL_INPUT0P_EN_SHIFT)
  292. #define ADC1_CTL_INPUT1P_EN(x) ((x) << ADC0_CTL_INPUT0P_EN_SHIFT)
  293. #define ADC1_CTL_INPUT1_IN_MODE BIT(2) /* 0: differential input mode; 1: single end input mode */
  294. #define ADC1_CTL_INPUT1_IRS_SHIFT (0) /* INPUT1 input resistor select */
  295. #define ADC1_CTL_INPUT1_IRS_MASK (0x3 << ADC0_CTL_INPUT0_IRS_SHIFT)
  296. #define ADC1_CTL_INPUT1_IRS(x) ((x) << ADC0_CTL_INPUT0_IRS_SHIFT)
  297. /***************************************************************************************************
  298. * ADC_CTL2
  299. */
  300. #define ADC2_CTL_MIX2AD2FDSE BIT(28) /* mix out to ADC2 input mode select */
  301. #define ADC2_CTL_PAR2AD2_EN BIT(27) /* PA OUTR mix to ADC2 channel */
  302. #define ADC2_CTL_PAL2AD2_EN BIT(26) /* PA OUTL mix to ADC2 channel */
  303. #define ADC2_CTL_PAR2AD2_PD_EN BIT(25) /* PA OUTR mix to ADC2 channel pull down */
  304. #define ADC2_CTL_PAL2AD2_PD_EN BIT(24) /* PA OUTL mix to ADC2 channel pull down */
  305. #define ADC2_CTL_VRDA2_EN BIT(20) /* VRDA2 enable */
  306. #define ADC2_CTL_ADC2_BINV BIT(19) /* ADC2 channel output phase invert */
  307. #define ADC2_CTL_FDBUF2_IRS_SHIFT (16) /* FDBUF2 input resistor select */
  308. #define ADC2_CTL_FDBUF2_IRS_MASK (0x7 << ADC2_CTL_FDBUF2_IRS_SHIFT)
  309. #define ADC2_CTL_FDBUF2_IRS(x) ((x) << ADC2_CTL_FDBUF2_IRS_SHIFT)
  310. #define ADC2_CTL_PREAM2_PG_SHIFT (12) /* PREAMP2 OP feedback resistor select */
  311. #define ADC2_CTL_PREAM2_PG_MASK (0xF << ADC2_CTL_PREAM2_PG_SHIFT)
  312. #define ADC2_CTL_ADC2_CAPFC_EN BIT(11) /* input cap to ADC2 channel fast charge enable */
  313. #define ADC2_CTL_FDBUF2_EN BIT(10) /* FD BUF OP2 enable for SE mode */
  314. #define ADC2_CTL_PREOP2_EN BIT(9) /* PREOP2 enable */
  315. #define ADC2_CTL_ADC2_EN BIT(8) /* ADC2 channel sdm enable */
  316. #define ADC2_CTL_INPUT2N_EN_SHIFT (6) /* INPUT2N pad to ADC2/3 channel input enable */
  317. #define ADC2_CTL_INPUT2N_EN_MASK (0x3 << ADC2_CTL_INPUT2N_EN_SHIFT)
  318. #define ADC2_CTL_INPUT2N_EN(x) ((x) << ADC2_CTL_INPUT2N_EN_SHIFT)
  319. #define ADC2_CTL_INPUT2P_EN_SHIFT (4) /* INPUT2P pad to ADC2 channel input enable */
  320. #define ADC2_CTL_INPUT2P_EN_MASK (0x3 << ADC2_CTL_INPUT2P_EN_SHIFT)
  321. #define ADC2_CTL_INPUT2P_EN(x) ((x) << ADC2_CTL_INPUT2P_EN_SHIFT)
  322. #define ADC2_CTL_INPUT2_IN_MODE BIT(2) /* 0: differential input mode; 1: single end input mode */
  323. #define ADC2_CTL_INPUT2_IRS_SHIFT (0) /* INPUT2 input resistor select */
  324. #define ADC2_CTL_INPUT2_IRS_MASK (0x3 << ADC2_CTL_INPUT2_IRS_SHIFT)
  325. #define ADC2_CTL_INPUT2_IRS(x) ((x) << ADC2_CTL_INPUT2_IRS_SHIFT)
  326. /***************************************************************************************************
  327. * ADC_CTL3
  328. */
  329. #define ADC3_CTL_MIX2AD3FDSE BIT(28)
  330. #define ADC3_CTL_PAR2AD3_EN BIT(27)
  331. #define ADC3_CTL_PAL2AD3_EN BIT(26)
  332. #define ADC3_CTL_PAR2AD3_PD_EN BIT(25)
  333. #define ADC3_CTL_PAL2AD3_PD_EN BIT(24)
  334. #define ADC3_CTL_VRDA3_EN BIT(20)
  335. #define ADC3_CTL_ADC3_BINV BIT(19)
  336. #define ADC3_CTL_FDBUF3_IRS_SHIFT (16)
  337. #define ADC3_CTL_FDBUF3_IRS_MASK (0x7 << ADC3_CTL_FDBUF3_IRS_SHIFT)
  338. #define ADC3_CTL_FDBUF3_IRS(x) ((x) << ADC3_CTL_FDBUF3_IRS_SHIFT)
  339. #define ADC3_CTL_PREAM3_PG_SHIFT (12)
  340. #define ADC3_CTL_PREAM3_PG_MASK (0xF << ADC3_CTL_PREAM3_PG_SHIFT)
  341. #define ADC3_CTL_PREAM3_PG(x) ((x) << ADC3_CTL_PREAM3_PG_SHIFT)
  342. #define ADC3_CTL_ADC3_CAPFC_EN BIT(11)
  343. #define ADC3_CTL_FDBUF3_EN BIT(10)
  344. #define ADC3_CTL_PREOP3_EN BIT(9)
  345. #define ADC3_CTL_ADC3_EN BIT(8)
  346. #define ADC3_CTL_INPUT3N_EN_SHIFT (6) /* INPUT3N pad to ADC2/3 channel input enable */
  347. #define ADC3_CTL_INPUT3N_EN_MASK (0x3 << ADC3_CTL_INPUT3N_EN_SHIFT)
  348. #define ADC3_CTL_INPUT3N_EN(x) ((x) << ADC3_CTL_INPUT3N_EN_SHIFT)
  349. #define ADC3_CTL_INPUT3P_EN_SHIFT (4) /* INPUT3P pad to ADC3 channel input enable */
  350. #define ADC3_CTL_INPUT3P_EN_MASK (0x3 << ADC3_CTL_INPUT3P_EN_SHIFT)
  351. #define ADC3_CTL_INPUT3P_EN(x) ((x) << ADC3_CTL_INPUT3P_EN_SHIFT)
  352. #define ADC3_CTL_INPUT3_IN_MODE BIT(2)
  353. #define ADC3_CTL_INPUT3_IRS_SHIFT (0) /* INPUT3 input resistor select */
  354. #define ADC3_CTL_INPUT3_IRS_MASK (0x3 << ADC3_CTL_INPUT3_IRS_SHIFT)
  355. #define ADC3_CTL_INPUT3_IRS(x) ((x) << ADC3_CTL_INPUT3_IRS_SHIFT)
  356. /***************************************************************************************************
  357. * ADC_BIAS
  358. */
  359. #define ADC_BIAS_BIASEN BIT(28) /* BIAS enable and if use ADC, this bit shall be enabled */
  360. #define ADC_BIAS_BIASSEL_SHIFT (26)
  361. #define ADC_BIAS_BIASSEL_MASK (0x3 << ADC_BIAS_BIASSEL_SHIFT)
  362. #define ADC_BIAS_VRDA_IQS_SHIFT (24)
  363. #define ADC_BIAS_VRDA_IQS_MASK (0x3 << ADC_BIAS_VRDA_IQS_SHIFT)
  364. #define ADC_BIAS_PREOP_ODSC BIT(22)
  365. #define ADC_BIAS_PREOP_IQS_SHIFT (20)
  366. #define ADC_BIAS_PREOP_IQS_MASK (0x3 << ADC_BIAS_PREOP_IQS_SHIFT)
  367. #define ADC_BIAS_OPBUF_ODSC_SHIFT (18)
  368. #define ADC_BIAS_OPBUF_ODSC_MASK (0x3 << ADC_BIAS_OPBUF_ODSC_SHIFT)
  369. #define ADC_BIAS_OPBUF_IQS_SHIFT (16)
  370. #define ADC_BIAS_OPBUF_IQS_MASK (0x3 << ADC_BIAS_OPBUF_IQS_SHIFT)
  371. #define ADC_BIAS_IAD3_SHIFT (14)
  372. #define ADC_BIAS_IAD3_MASK (0x3 << ADC_BIAS_IAD3_SHIFT)
  373. #define ADC_BIAS_IAD2_SHIFT (12)
  374. #define ADC_BIAS_IAD2_MASK (0x3 << ADC_BIAS_IAD2_SHIFT)
  375. #define ADC_BIAS_IAD1_SHIFT (8)
  376. #define ADC_BIAS_IAD1_MASK (0x7 << ADC_BIAS_IAD1_SHIFT)
  377. #define ADC_BIAS_VRDA_IB_SHIFT (5)
  378. #define ADC_BIAS_VRDA_IB_MASK (0x7 << ADC_BIAS_VRDA_IB_SHIFT)
  379. #define ADC_BIAS_OPBUF_IB_SHIFT (3)
  380. #define ADC_BIAS_OPBUF_IB_MASK (0x3 << ADC_BIAS_OPBUF_IB_SHIFT)
  381. #define ADC_BIAS_PREOP_IB_SHIFT (0)
  382. #define ADC_BIAS_PREOP_IB_MASK (0x7 << ADC_BIAS_PREOP_IB_SHIFT)
  383. /***************************************************************************************************
  384. * ADC_VMIC_CTL
  385. */
  386. #define ADC_VMIC_CTL_ISO_VD18 BIT(23) /* isolate the MFP sio of input pin */
  387. #define ADC_VMIC_CTL_ISO_AVCC_AU BIT(22) /* isolation of capless ldo AVCC_AU power */
  388. #define ADC_VMIC_CTL_VMIC2_R_SEL BIT(20) /* divider resistor of VMIC2 control */
  389. #define ADC_VMIC_CTL_VMIC1_R_SEL BIT(19) /* divider resistor of VMIC1 control */
  390. #define ADC_VMIC_CTL_VMIC0_R_SEL BIT(18) /* divider resistor of VIMIC0 control */
  391. #define ADC_VMIC_CTL_VMIC_BIAS_CTL_SHIFT (16) /* VMIC I bias control */
  392. #define ADC_VMIC_CTL_VMIC_BIAS_CTL_MASK (0x3 << ADC_VMIC_CTL_VMIC_BIAS_CTL_SHIFT)
  393. #define ADC_VMIC_CTL_VMIC_BIAS_CTL(x) ((x) << ADC_VMIC_CTL_VMIC_BIAS_CTL_SHIFT)
  394. #define ADC_VMIC_CTL_VMIC_IB_SEL BIT(14) /* VMIC I bias select. 0: from PMU; 1: from VMIC I bias generation */
  395. #define ADC_VMIC_CTL_VMIC_IBGEN_CTL BIT(13) /* VMIC I bias generation current control */
  396. #define ADC_VMIC_CTL_VMIC_IBGEN_EN BIT(12) /* VMIC I bias generation enable */
  397. #define ADC_VMIC_CTL_VMIC2_VOL_SHIFT (10) /* VMIC2 output voltage control */
  398. #define ADC_VMIC_CTL_VMIC2_VOL_MASK (0x3 << ADC_VMIC_CTL_VMIC2_VOL_SHIFT)
  399. #define ADC_VMIC_CTL_VMIC2_VOL(x) ((x) << ADC_VMIC_CTL_VMIC2_VOL_SHIFT)
  400. #define ADC_VMIC_CTL_VMIC2_EN_SHIFT (8) /* VMIC2 enable */
  401. #define ADC_VMIC_CTL_VMIC2_EN_MASK (0x3 << ADC_VMIC_CTL_VMIC2_EN_SHIFT)
  402. #define ADC_VMIC_CTL_VMIC2_EN(x) ((x) << ADC_VMIC_CTL_VMIC2_EN_SHIFT)
  403. #define ADC_VMIC_CTL_VMIC1_VOL_SHIFT (6) /* VMIC1 output voltage control */
  404. #define ADC_VMIC_CTL_VMIC1_VOL_MASK (0x3 << ADC_VMIC_CTL_VMIC1_VOL_SHIFT)
  405. #define ADC_VMIC_CTL_VMIC1_VOL(x) ((x) << ADC_VMIC_CTL_VMIC1_VOL_SHIFT)
  406. #define ADC_VMIC_CTL_VMIC1_EN_SHIFT (4) /* VMIC1 enable. 0x: disable VMIC1 op; 2: bypass VMIC1 op; 3: enable VMIC1 op */
  407. #define ADC_VMIC_CTL_VMIC1_EN_MASK (0x3 << ADC_VMIC_CTL_VMIC1_EN_SHIFT)
  408. #define ADC_VMIC_CTL_VMIC1_EN(x) ((x) << ADC_VMIC_CTL_VMIC1_EN_SHIFT)
  409. #define ADC_VMIC_CTL_VMIC0_VOL_SHIFT (2) /* VMIC0 output voltage control */
  410. #define ADC_VMIC_CTL_VMIC0_VOL_MASK (0x3 << ADC_VMIC_CTL_VMIC0_VOL_SHIFT)
  411. #define ADC_VMIC_CTL_VMIC0_VOL(x) ((x) << ADC_VMIC_CTL_VMIC0_VOL_SHIFT)
  412. #define ADC_VMIC_CTL_VMIC0_EN_SHIFT (0)
  413. #define ADC_VMIC_CTL_VMIC0_EN_MASK (0x3 << ADC_VMIC_CTL_VMIC0_EN_SHIFT)
  414. #define ADC_VMIC_CTL_VMIC0_EN(x) ((x) << ADC_VMIC_CTL_VMIC0_EN_SHIFT)
  415. /***************************************************************************************************
  416. * ADC_REF_LDO_CTL
  417. */
  418. #define ADC_REF_LDO_CTL_AULDO_PD_CTL_SHIFT (18) /* AULDO pull down current control. 0: small; 3: large */
  419. #define ADC_REF_LDO_CTL_AULDO_PD_CTL_MASK (0x3 << ADC_REF_LDO_CTL_AULDO_PD_CTL_SHIFT)
  420. #define ADC_REF_LDO_CTL_AULDO_PD_CTL(x) ((x) << ADC_REF_LDO_CTL_AULDO_PD_CTL_SHIFT)
  421. #define ADC_REF_LDO_CTL_AULDO_BIAS_CTL_SHIFT (16) /* AUDIO I bias control */
  422. #define ADC_REF_LDO_CTL_AULDO_BIAS_CTL_MASK (0x3 << ADC_REF_LDO_CTL_AULDO_BIAS_CTL_SHIFT)
  423. #define ADC_REF_LDO_CTL_AULDO_BIAS_CTL(x) ((x) << ADC_REF_LDO_CTL_AULDO_BIAS_CTL_SHIFT)
  424. #define ADC_REF_LDO_CTL_AULDO_IB_SEL BIT(14) /* AULDO I bias select */
  425. #define ADC_REF_LDO_CTL_AULDO_IBGEN_CTL BIT(13) /* AULDO I bias generation current control */
  426. #define ADC_REF_LDO_CTL_AULDO_IBGEN_EN BIT(12) /* AULDO I bias generation enable */
  427. #define ADC_REF_LDO_CTL_AULDO_VOL_SHIFT (10) /* AULDO output voltage control */
  428. #define ADC_REF_LDO_CTL_AULDO_VOL_MASK (0x3 << ADC_REF_LDO_CTL_AULDO_VOL_SHIFT)
  429. #define ADC_REF_LDO_CTL_AULDO_VOL(x) ((x) << ADC_REF_LDO_CTL_AULDO_VOL_SHIFT)
  430. #define ADC_REF_LDO_CTL_AULDO_EN_SHIFT (8) /* AULDO enable for ADC */
  431. #define ADC_REF_LDO_CTL_AULDO_EN_MASK (0x3 << ADC_REF_LDO_CTL_AULDO_EN_SHIFT)
  432. #define ADC_REF_LDO_CTL_AULDO_EN(x) ((x) << ADC_REF_LDO_CTL_AULDO_EN_SHIFT)
  433. #define ADC_REF_LDO_CTL_DALDO_EN_SHIFT (6) /* DALDO enable for DAC */
  434. #define ADC_REF_LDO_CTL_DALDO_EN_MASK (0x3 << ADC_REF_LDO_CTL_DALDO_EN_SHIFT)
  435. #define ADC_REF_LDO_CTL_DALDO_EN(x) ((x) << ADC_REF_LDO_CTL_DALDO_EN_SHIFT)
  436. #define ADC_REF_LDO_CTL_VREF_RSEL_SHIFT (2) /* VREF voltage divide res control */
  437. #define ADC_REF_LDO_CTL_VREF_RSEL_MASK (0x3 << ADC_REF_LDO_CTL_VREF_RSEL_SHIFT)
  438. #define ADC_REF_LDO_CTL_VREF_RSEL(x) ((x) << ADC_REF_LDO_CTL_VREF_RSEL_SHIFT)
  439. #define ADC_REF_LDO_CTL_VREF_FU BIT(1) /* VREF fastup control */
  440. #define ADC_REF_LDO_CTL_VREF_EN BIT(0) /* VREF enable control */
  441. /***************************************************************************************************
  442. * HW_TRIGGER_ADC_CTL
  443. */
  444. #define HW_TRIGGER_ADC_CTL_INT_TO_ADC3_EN BIT(7)
  445. #define HW_TRIGGER_ADC_CTL_INT_TO_ADC2_EN BIT(6)
  446. #define HW_TRIGGER_ADC_CTL_INT_TO_ADC1_EN BIT(5)
  447. #define HW_TRIGGER_ADC_CTL_INT_TO_ADC0_EN BIT(4)
  448. #define HW_TRIGGER_ADC_CTL_INT_TO_ADC_SHIFT (4)
  449. #define HW_TRIGGER_ADC_CTL_INT_TO_ADC_MASK (0xF << HW_TRIGGER_ADC_CTL_INT_TO_ADC_SHIFT)
  450. #define HW_TRIGGER_ADC_CTL_TRIGGER_SRC_SEL_SHIFT (0)
  451. #define HW_TRIGGER_ADC_CTL_TRIGGER_SRC_SEL_MASK (0xF << HW_TRIGGER_ADC_CTL_TRIGGER_SRC_SEL_SHIFT)
  452. #define HW_TRIGGER_ADC_CTL_TRIGGER_SRC_SEL(x) ((x) << HW_TRIGGER_ADC_CTL_TRIGGER_SRC_SEL_SHIFT)
  453. /***************************************************************************************************
  454. * ADC_DEBUG
  455. */
  456. #define DEBUGSEL (0x40068400)
  457. #define DEBUGIE0 (0x40068410)
  458. #define DEBUGOE0 (0x40068420)
  459. #define DEBUGSEL_DBGSE_SHIFT (0)
  460. #define DEBUGSEL_DBGSE_MASK (0x3F << DEBUGSEL_DBGSE_SHIFT)
  461. #define DEBUGSEL_DBGSE(x) ((x) << DEBUGSEL_DBGSE_SHIFT)
  462. #define DBGSE_ADC (0xd)
  463. /***************************************************************************************************
  464. * ADC FEATURES CONGIURATION
  465. */
  466. #define ADC_FIFO_MAX_NUMBER (1)
  467. #define ADC_FIFO_MAX_DRQ_LEVEL (127)
  468. #define ADC_FIFO_DRQ_LEVEL_DEFAULT (32) /* 32 level */
  469. #define ADC_OSR_DEFAULT (1) /* ADC_OSR_128FS */
  470. #define ADC_DIGITAL_CH_GAIN_MAX (0xF) /* 52.5dB */
  471. #define ADC_DIGITAL_DMIC_GAIN_MAX (0x7) /* 63x */
  472. #define ADC_FEEDBACK_RES_INVALID (0xFF) /* invalid ADC feedback resistor */
  473. #define ADC_MAX_CHANNELS_NUMBER (4) /* max ADC channels number */
  474. #define ADC_HPF_HIGH_FREQ_HZ (500) /* 500Hz for high frequency */
  475. #define ADC_HPF_FAST_STABLE_MS (50) /* 10 milliseconds for HFP fast stable */
  476. #define ADC_FAST_CAP_CHARGE_TIME_MS (80) /* 80 milliseconds for CAP charging */
  477. #define ADC_LDO_CAPACITOR_CHARGE_TIME_MS (5) /* Wait time for ADC input capacitor charge full */
  478. //#define ADC_ANALOG_DEBUG_OUT_ENABLE
  479. #ifdef CONFIG_SOC_SERIES_LARK_FPGA
  480. #define ADC_DIGITAL_DEBUG_IN_ENABLE
  481. #endif
  482. #define ADC_CH2REG(base, x) ((uint32_t)&((base)->ch0_digctl) + ((x) << 2))
  483. #define ADC_CTL2REG(base, x) ((uint32_t)&((base)->adc_ctl0) + ((x) << 2))
  484. /* @brief the macro to configure the ADC channels */
  485. #define ADC_CHANNEL_CFG(n, en) \
  486. { \
  487. if (ADC_CH_DISABLE != ch##n##_input) { \
  488. if (ADC_CH_DMIC == ch##n##_input) { \
  489. adc_hpf_config(dev, n, en); \
  490. __adc_digital_channel_cfg(dev, ADC_CHANNEL_##n, ADC_DMIC, en); \
  491. } else { \
  492. adc_hpf_config(dev, n, en); \
  493. __adc_digital_channel_cfg(dev, ADC_CHANNEL_##n, ADC_AMIC, en); \
  494. } \
  495. } \
  496. }
  497. /* @brief the macro to control ADC channels to enable or disable */
  498. #define ADC_CHANNELS_CTL(en) \
  499. { \
  500. if (en) { \
  501. ADC_CHANNEL_CFG(0, true); \
  502. ADC_CHANNEL_CFG(1, true); \
  503. ADC_CHANNEL_CFG(2, true); \
  504. ADC_CHANNEL_CFG(3, true); \
  505. } else { \
  506. ADC_CHANNEL_CFG(0, false); \
  507. ADC_CHANNEL_CFG(1, false); \
  508. ADC_CHANNEL_CFG(2, false); \
  509. ADC_CHANNEL_CFG(3, false); \
  510. } \
  511. }
  512. /* @brief the macro to set the DMIC gain */
  513. #define ADC_DMIC_GAIN_CFG(n) \
  514. { \
  515. uint8_t dmic_gain; \
  516. if ((ADC_CH_DMIC == ch##n##_input) && (ADC_GAIN_INVALID != ch##n##_gain)) { \
  517. if (adc_dmic_gain_translate(ch##n##_gain, &dmic_gain, &dig_gain)) { \
  518. LOG_DBG("failed to translate dmic ch%d gain %d", n, ch##n##_gain); \
  519. return -EFAULT; \
  520. } \
  521. __adc_digital_gain_set(dev, ADC_CHANNEL_##n, dig_gain); \
  522. __adc_dmic_gain_set(dev, dmic_gain); \
  523. } \
  524. }
  525. /**
  526. * @struct acts_audio_adc
  527. * @brief ADC controller hardware register
  528. */
  529. struct acts_audio_adc {
  530. volatile uint32_t adc_digctl; /* ADC digital control */
  531. volatile uint32_t ch0_digctl; /* channel0 digital control */
  532. volatile uint32_t ch1_digctl; /* channel1 digital control */
  533. volatile uint32_t ch2_digctl; /* channel2 digital control */
  534. volatile uint32_t ch3_digctl; /* channel3 digital control */
  535. volatile uint32_t fifoctl; /* ADC fifo control */
  536. volatile uint32_t stat; /* ADC stat */
  537. volatile uint32_t fifo0_dat; /* ADC FIFO0 data */
  538. volatile uint32_t aal0_ctl; /* ADC0 auto amplitude limit control */
  539. volatile uint32_t aal1_ctl; /* ADC1 auto amplitude limit control */
  540. volatile uint32_t aal2_ctl; /* ADC2 auto amplitude limit control */
  541. volatile uint32_t aal3_ctl; /* ADC3 auto amplitude limit control */
  542. volatile uint32_t adc_ctl0; /* ADC control0 */
  543. volatile uint32_t adc_ctl1; /* ADC control1 */
  544. volatile uint32_t adc_ctl2; /* ADC control2 */
  545. volatile uint32_t adc_ctl3; /* ADC control3 */
  546. volatile uint32_t bias; /* ADC bias */
  547. volatile uint32_t vmic_ctl; /* VMIC control */
  548. volatile uint32_t ref_ldo_ctl; /* ADC reference LDO control */
  549. volatile uint32_t hw_trigger_ctl; /* ADC HW trigger ADC control */
  550. };
  551. #ifdef CONFIG_CFG_DRV
  552. /**
  553. * struct phy_adc_external_config
  554. * @brief The ADC external configuration which generated by configuration tool.
  555. */
  556. struct phy_adc_external_config {
  557. cfg_uint32 ADC_Bias_Setting; /* ADC bias setting */
  558. cfg_uint8 DMIC01_Channel_Aligning; /* DMIC latch policy selection. 0: L/R 1:R/L */
  559. cfg_uint8 DMIC23_Channel_Aligning; /* DMIC latch policy selection. 2: L/R 3:R/L */
  560. CFG_Type_DMIC_Select_GPIO DMIC_Select_GPIO; /* DMIC GPIO pin */
  561. cfg_uint8 Enable_ANC; /* ANC configuration. 0:disable; 1:AUDIO_ANC_FF; 2:AUDIO_ANC_FB; 3:AUDIO_ANC_FY */
  562. CFG_Type_DMIC_Select_GPIO ANCDMIC_Select_GPIO; /* DMIC GPIO pin for ANC */
  563. cfg_uint8 Record_Adc_Select; /* ADC type selection */
  564. cfg_uint8 Enable_VMIC; /* VMIC power supply enable */
  565. cfg_uint8 Hw_Aec_Select; /* Hardware AEC enable */
  566. CFG_Type_Mic_Config Mic_Config[CFG_MAX_ADC_NUM]; /* MIC configuration */
  567. CFG_Type_ADC_Select_INPUT ADC_Select_INPUT; /* ADC input selection */
  568. cfg_int16 ANC_FF_GAIN; /* ANC FF MIC gain */
  569. cfg_int16 ANC_FB_GAIN; /* ANC FB MIC gain */
  570. };
  571. #endif
  572. /**
  573. * struct phy_adc_drv_data
  574. * @brief The software related data that used by physical adc driver.
  575. */
  576. struct phy_adc_drv_data {
  577. #ifdef CONFIG_CFG_DRV
  578. struct phy_adc_external_config external_config; /* ADC external configuration */
  579. uint8_t input_ch0; /* ADC channel0 INPUT selection after parser */
  580. uint8_t input_ch1; /* ADC channel1 INPUT selection after parser */
  581. uint8_t input_ch2; /* ADC channel2 INPUT selection after parser */
  582. uint8_t input_ch3; /* ADC channel3 INPUT selection after parser */
  583. #endif
  584. uint8_t hw_trigger_en : 1; /* If 1 to enable hw IRQ signal to trigger ADC digital start */
  585. uint8_t anc_en : 1; /* If 1 to indicate ANC has been enabled */
  586. uint8_t audio_pll_index : 1; /* The index of audio pll */
  587. };
  588. /**
  589. * union phy_adc_features
  590. * @brief The infomation from DTS to control the ADC features to enable or nor.
  591. */
  592. typedef union {
  593. uint64_t raw;
  594. struct {
  595. uint64_t adc0_hpf_time : 2; /* ADC0 HPF auto-set time */
  596. uint64_t adc1_hpf_time : 2; /* ADC1 HPF auto-set time */
  597. uint64_t adc2_hpf_time : 2; /* ADC2 HPF auto-set time */
  598. uint64_t adc3_hpf_time : 2; /* ADC3 HPF auto-set time */
  599. uint64_t adc0_hpf_fc_high: 1; /* ADC0 HPF use high frequency range */
  600. uint64_t adc1_hpf_fc_high: 1; /* ADC1 HPF use high frequency range */
  601. uint64_t adc2_hpf_fc_high: 1; /* ADC2 HPF use high frequency range */
  602. uint64_t adc3_hpf_fc_high: 1; /* ADC3 HPF use high frequency range */
  603. uint64_t adc0_frequency : 6; /* ADC0 HFP frequency */
  604. uint64_t adc1_frequency : 6; /* ADC1 HFP frequency */
  605. uint64_t adc2_frequency : 6; /* ADC2 HFP frequency */
  606. uint64_t adc3_frequency : 6; /* ADC2 HFP frequency */
  607. uint64_t ldo_voltage : 2; /* AUDIO LDO voltage */
  608. uint64_t fast_cap_charge : 1; /* Fast CAP charge function */
  609. } v;
  610. } phy_adc_features;
  611. #ifndef CONFIG_CFG_DRV
  612. static uint8_t vmic_ctl_array[] = CONFIG_AUDIO_ADC_0_VMIC_CTL_ARRAY;
  613. #else
  614. static uint8_t adc_vmic_index_mapping_array[] = CONFIG_AUDIO_ADC_0_VMIC_MAPPING;
  615. #endif
  616. static uint8_t vmic_voltage_array[] = CONFIG_AUDIO_ADC_0_VMIC_VOLTAGE_ARRAY;
  617. /**
  618. * struct phy_adc_config_data
  619. * @brief The hardware related data that used by physical adc driver.
  620. */
  621. struct phy_adc_config_data {
  622. uint32_t reg_base; /* ADC controller register base address */
  623. struct audio_dma_dt dma_fifo0; /* DMA resource for FIFO0 */
  624. uint8_t clk_id; /* ADC devclk id */
  625. uint8_t rst_id; /* ADC reset id */
  626. phy_adc_features features; /* ADC features */
  627. };
  628. struct adc_amic_aux_gain_setting {
  629. int16_t gain;
  630. uint8_t input_res;
  631. uint8_t feedback_res;
  632. uint8_t digital_gain;
  633. };
  634. struct adc_dmic_gain_setting {
  635. int16_t gain;
  636. uint8_t dmic_pre_gain;
  637. uint8_t digital_gain;
  638. };
  639. struct adc_anc_clk_setting {
  640. uint8_t sample_rate;
  641. uint16_t root_clk_div; /* a_mclk_type_e */
  642. uint8_t ovfs_clk_div; /* 0: div1; 1: div2; 2: div4 */
  643. uint8_t fir_clk_div; /* 0: div1; 1: div3 */
  644. };
  645. /**
  646. * @struct adc_amic_aux_gain_setting
  647. * @brief The gain mapping table of the analog mic and aux.
  648. * @note By the SD suggestion, it is suitable to ajust the analog gain when below 20dB.
  649. * Whereas, it is the same effect to ajust the analog or digital gian when above 20dB.
  650. */
  651. static const struct adc_amic_aux_gain_setting amic_aux_gain_mapping[] = {
  652. {-120, 0, 0, 0},
  653. {-90, 0, 1, 0},
  654. {-60, 0, 2, 0},
  655. {-30, 0, 3, 0},
  656. {0, 0, 4, 0},
  657. {30, 0, 5, 0},
  658. {60, 0, 6, 0},
  659. {75, 0, 7, 0},
  660. {90, 0, 8, 0},
  661. {105, 0, 9, 0},
  662. {120, 0, 10, 0},
  663. {135, 0, 11, 0},
  664. {150, 0, 12, 0},
  665. {165, 0, 13, 0},
  666. {180, 0, 14, 0},
  667. {195, 0, 15, 0},
  668. {210, 1, 12, 0},
  669. {225, 1, 13, 0},
  670. {230, 0, 15, 1},
  671. {240, 1, 14, 0},
  672. {245, 1, 12, 1},
  673. {250, 1, 14, 2},
  674. {255, 1, 15, 0},
  675. {260, 1, 13, 1},
  676. {265, 0, 15, 2},
  677. {270, 2, 12, 0},
  678. {275, 1, 14, 1},
  679. {280, 1, 12, 2},
  680. {285, 2, 13, 0},
  681. {290, 1, 15, 1},
  682. {295, 1, 13, 2},
  683. {300, 2, 14, 0},
  684. {305, 2, 12, 1},
  685. {310, 1, 14, 2},
  686. {315, 2, 15, 0},
  687. {320, 2, 13, 1},
  688. {325, 1, 15, 2},
  689. {330, 1, 13, 3},
  690. {335, 2, 14, 1},
  691. {340, 2, 12, 1},
  692. {345, 1, 14, 3},
  693. {350, 2, 15, 1},
  694. {355, 2, 13, 2},
  695. {360, 1, 15, 3},
  696. {365, 1, 13, 4},
  697. {370, 2, 14, 2},
  698. {375, 2, 12, 3},
  699. {380, 1, 14, 4},
  700. {385, 2, 15, 2},
  701. {390, 2, 13, 3},
  702. {395, 1, 15, 4},
  703. {400, 1, 13, 5},
  704. {405, 2, 14, 3},
  705. {410, 2, 12, 4},
  706. {415, 1, 14, 5},
  707. {420, 2, 15, 3},
  708. {425, 2, 13, 4},
  709. {430, 1, 15, 5},
  710. {435, 1, 13, 6},
  711. {440, 2, 14, 4},
  712. {445, 2, 12, 5},
  713. {450, 1, 14, 6},
  714. {455, 2, 15, 4},
  715. {460, 2, 13, 5},
  716. {465, 1, 15, 6},
  717. {470, 1, 13, 7},
  718. {475, 2, 14, 5},
  719. {480, 2, 12, 6},
  720. {485, 1, 14, 7},
  721. {490, 2, 15, 5},
  722. {495, 2, 13, 6},
  723. {500, 1, 15, 7},
  724. {505, 1, 13, 8},
  725. {510, 2, 14, 6},
  726. {515, 2, 12, 7},
  727. {520, 1, 14, 8},
  728. {525, 2, 15, 6},
  729. {530, 2, 13, 7},
  730. {535, 1, 15, 8},
  731. {540, 1, 13, 9},
  732. {545, 2, 14, 7},
  733. {550, 2, 12, 8},
  734. {555, 1, 14, 9},
  735. {560, 2, 15, 7},
  736. {565, 2, 13, 8},
  737. {570, 1, 15, 9},
  738. {575, 1, 13, 10},
  739. {580, 2, 14, 8},
  740. {585, 2, 12, 9},
  741. {590, 1, 14, 10},
  742. {595, 2, 15, 8},
  743. {600, 2, 13, 9},
  744. {605, 1, 15, 10},
  745. {610, 1, 13, 11},
  746. {615, 2, 14, 9},
  747. {620, 2, 12, 10},
  748. {625, 1, 14, 11},
  749. {630, 2, 15, 9},
  750. {635, 2, 13, 10},
  751. {640, 1, 15, 11},
  752. {645, 1, 13, 12},
  753. {650, 2, 14, 10},
  754. {655, 2, 12, 11},
  755. {660, 1, 14, 12},
  756. {665, 2, 15, 10},
  757. {670, 2, 13, 11},
  758. {675, 1, 15, 12},
  759. {680, 1, 13, 13},
  760. {685, 2, 14, 11},
  761. {690, 2, 12, 12},
  762. {695, 1, 14, 13},
  763. {700, 2, 15, 11},
  764. {705, 2, 13, 12},
  765. {710, 1, 15, 13},
  766. {715, 1, 13, 14},
  767. {720, 2, 14, 12},
  768. {725, 2, 12, 13},
  769. {730, 1, 14, 14},
  770. {735, 2, 15, 12},
  771. {740, 2, 13, 13},
  772. {745, 1, 15, 14},
  773. {750, 1, 13, 15},
  774. {755, 2, 14, 13},
  775. {760, 2, 12, 14},
  776. {765, 1, 14, 15},
  777. {770, 2, 15, 13},
  778. {775, 2, 13, 14},
  779. {780, 1, 15, 15},
  780. {795, 2, 12, 15},
  781. {810, 2, 13, 15},
  782. {825, 2, 14, 15},
  783. {840, 2, 15, 15},
  784. };
  785. #ifdef CONFIG_ADC_DMIC
  786. /* dB = 20 x log(x) */
  787. static const struct adc_dmic_gain_setting dmic_gain_mapping[] = {
  788. {0, 0, 0},
  789. {60, 1, 0},
  790. {120, 2, 0},
  791. {180, 3, 0},
  792. {240, 4, 0},
  793. {300, 5, 0},
  794. {360, 6, 0},
  795. {395, 6, 1},
  796. {430, 6, 2},
  797. {465, 6, 3},
  798. {500, 6, 4},
  799. {535, 6, 5},
  800. {570, 6, 6},
  801. {605, 6, 7},
  802. {640, 6, 8},
  803. {675, 6, 9},
  804. {710, 6, 10},
  805. {745, 6, 11},
  806. {780, 6, 12},
  807. {815, 6, 13},
  808. {850, 6, 14},
  809. {885, 6, 15},
  810. };
  811. #endif
  812. static const struct adc_anc_clk_setting adc_anc_clk_mapping[] = {
  813. {SAMPLE_RATE_16KHZ, MCLK_768FS, 1, 1},
  814. {SAMPLE_RATE_22KHZ, MCLK_256FS, 0, 0},
  815. {SAMPLE_RATE_24KHZ, MCLK_256FS, 0, 0},
  816. {SAMPLE_RATE_32KHZ, MCLK_768FS, 2, 1},
  817. {SAMPLE_RATE_44KHZ, MCLK_256FS, 1, 0},
  818. {SAMPLE_RATE_48KHZ, MCLK_256FS, 1, 0},
  819. };
  820. /**
  821. * enum a_adc_fifo_e
  822. * @brief ADC fifo index selection
  823. */
  824. typedef enum {
  825. ADC_FIFO_0 = 0,
  826. } a_adc_fifo_e;
  827. /**
  828. * enum a_adc_ovfs_e
  829. * @brief ADC CIC over sample rate selection
  830. */
  831. typedef enum {
  832. ADC_OVFS_192FS = 0,
  833. ADC_OVFS_128FS,
  834. ADC_OVFS_96FS,
  835. ADC_OVFS_64FS
  836. } a_adc_ovfs_e;
  837. /**
  838. * enum a_adc_fir_e
  839. * @brief ADC frequency response (FIR) mode selection
  840. */
  841. typedef enum {
  842. ADC_FIR_MODE_A = 0,
  843. ADC_FIR_MODE_B,
  844. ADC_FIR_MODE_C,
  845. } a_adc_fir_e;
  846. /**
  847. * enum a_adc_ch_e
  848. * @beief ADC channels selection
  849. */
  850. typedef enum {
  851. ADC_CHANNEL_0 = 0,
  852. ADC_CHANNEL_1,
  853. ADC_CHANNEL_2,
  854. ADC_CHANNEL_3
  855. } a_adc_ch_e;
  856. typedef enum {
  857. ADC_AMIC = 0, /* analog mic */
  858. ADC_DMIC /* digital mic */
  859. } a_adc_ch_type_e;
  860. /*
  861. * enum a_hpf_time_e
  862. * @brief HPF(High Pass Filter) auto setting time selection
  863. */
  864. typedef enum {
  865. HPF_TIME_0 = 0, /* 1.3ms at 48kfs*/
  866. HPF_TIME_1, /* 5ms at 48kfs */
  867. HPF_TIME_2, /* 10ms at 48kfs*/
  868. HPF_TIME_3 /* 20ms at 48kfs */
  869. } a_hpf_time_e;
  870. /*
  871. * enum a_input_lr_e
  872. * @brief ADC input left and right selection
  873. */
  874. typedef enum {
  875. INPUT_POSITIVE_SEL = (1 << 0), /* INPUTxP selection */
  876. INPUT_NEGATIVE_SEL = (1 << 1) /* INPUTxN selection */
  877. } a_input_lr_e;
  878. typedef struct {
  879. int16_t gain;
  880. uint8_t fb_res;
  881. uint8_t input_res;
  882. } adc_gain_input_t;
  883. /* @brief Get the ADC controller base address */
  884. static inline struct acts_audio_adc *get_adc_reg_base(struct device *dev)
  885. {
  886. const struct phy_adc_config_data *cfg = dev->config;
  887. return (struct acts_audio_adc *)cfg->reg_base;
  888. }
  889. /* @brief Dump the ADC relative registers */
  890. static void adc_dump_register(struct device *dev)
  891. {
  892. struct acts_audio_adc *adc_reg = get_adc_reg_base(dev);
  893. LOG_INF("** adc contoller regster **");
  894. LOG_INF(" BASE: %08x", (uint32_t)adc_reg);
  895. LOG_INF(" ADC_DIGCTL: %08x", adc_reg->adc_digctl);
  896. LOG_INF(" CH0_DIGCTL: %08x", adc_reg->ch0_digctl);
  897. LOG_INF(" CH1_DIGCTL: %08x", adc_reg->ch1_digctl);
  898. LOG_INF(" CH2_DIGCTL: %08x", adc_reg->ch2_digctl);
  899. LOG_INF(" CH3_DIGCTL: %08x", adc_reg->ch3_digctl);
  900. LOG_INF(" ADC_FIFOCTL: %08x", adc_reg->fifoctl);
  901. LOG_INF(" ADC_STAT: %08x", adc_reg->stat);
  902. LOG_INF(" ADC_FIFO0_DAT: %08x", adc_reg->fifo0_dat);
  903. LOG_INF(" AAL0_CTL: %08x", adc_reg->aal0_ctl);
  904. LOG_INF(" AAL1_CTL: %08x", adc_reg->aal1_ctl);
  905. LOG_INF(" AAL2_CTL: %08x", adc_reg->aal2_ctl);
  906. LOG_INF(" AAL3_CTL: %08x", adc_reg->aal3_ctl);
  907. LOG_INF(" ADC_CTL0: %08x", adc_reg->adc_ctl0);
  908. LOG_INF(" ADC_CTL1: %08x", adc_reg->adc_ctl1);
  909. LOG_INF(" ADC_CTL2: %08x", adc_reg->adc_ctl2);
  910. LOG_INF(" ADC_CTL3: %08x", adc_reg->adc_ctl3);
  911. LOG_INF(" ADC_BIAS: %08x", adc_reg->bias);
  912. LOG_INF(" VMIC_CTL: %08x", adc_reg->vmic_ctl);
  913. LOG_INF(" REF_LDO_CTL: %08x", adc_reg->ref_ldo_ctl);
  914. LOG_INF("HW_TRIGGER_CTL: %08x", adc_reg->hw_trigger_ctl);
  915. LOG_INF(" AUDIOPLL0_CTL: %08x", sys_read32(AUDIO_PLL0_CTL));
  916. LOG_INF(" AUDIOPLL1_CTL: %08x", sys_read32(AUDIO_PLL1_CTL));
  917. LOG_INF(" CMU_ADCCLK: %08x", sys_read32(CMU_ADCCLK));
  918. }
  919. /* @brief disable ADC FIFO by specified FIFO index */
  920. static void __adc_fifo_disable(struct device *dev, a_adc_fifo_e idx)
  921. {
  922. struct acts_audio_adc *adc_reg = get_adc_reg_base(dev);
  923. if (ADC_FIFO_0 == idx) {
  924. adc_reg->fifoctl &= ~(ADC_FIFOCTL_ADF0RT | ADC_FIFOCTL_ADF0FDE);
  925. /* disable ADC FIFO0 access clock */
  926. sys_write32(sys_read32(CMU_ADCCLK) & ~CMU_ADCCLK_ADCFIFOCLKEN, CMU_ADCCLK);
  927. }
  928. }
  929. /* @brief check all ADC FIFOs are idle */
  930. static inline bool __adc_check_fifo_all_disable(struct device *dev)
  931. {
  932. struct acts_audio_adc *adc_reg = get_adc_reg_base(dev);
  933. if (adc_reg->fifoctl & ADC_FIFOCTL_ADF0RT)
  934. return false;
  935. return true;
  936. }
  937. /* @brief check whether the ADC FIFO is working now */
  938. static bool __is_adc_fifo_working(struct device *dev, a_adc_fifo_e idx)
  939. {
  940. struct acts_audio_adc *adc_reg = get_adc_reg_base(dev);
  941. if (ADC_FIFO_0 == idx) {
  942. if (adc_reg->fifoctl & ADC_FIFOCTL_ADF0RT)
  943. return true;
  944. }
  945. return false;
  946. }
  947. /* @brief enable ADC FIFO by specified FIFO index */
  948. static int __adc_fifo_enable(struct device *dev, audio_fifouse_sel_e sel,
  949. audio_dma_width_e wd, uint8_t drq_level, a_adc_fifo_e idx)
  950. {
  951. struct acts_audio_adc *adc_reg = get_adc_reg_base(dev);
  952. uint32_t reg = adc_reg->fifoctl;
  953. if ((drq_level > ADC_FIFO_MAX_DRQ_LEVEL) || (drq_level == 0))
  954. drq_level = ADC_FIFO_DRQ_LEVEL_DEFAULT;
  955. if (FIFO_SEL_ASRC == sel) {
  956. LOG_ERR("invalid fifo sel %d", sel);
  957. return -EINVAL;
  958. }
  959. if (ADC_FIFO_0 == idx) {
  960. reg &= ~0x3FFF; /* clear FIFO0 fields */
  961. if (FIFO_SEL_CPU == sel) /* enable IRQ */
  962. reg |= (ADC_FIFOCTL_ADF0FIE);
  963. else if (FIFO_SEL_DMA == sel) /* enable DRQ */
  964. reg |= (ADC_FIFOCTL_ADF0FDE);
  965. reg |= ADC_FIFOCTL_ADF0OS(sel) | ADC_FIFOCTL_ADF0RT;
  966. if (DMA_WIDTH_16BITS == wd) /* width 0:32bits; 1:16bits */
  967. reg |= ADC_FIFOCTL_ADCFIFO0_DMAWIDTH;
  968. reg |= ADC_FIFOCTL_DRQ_LEVEL(drq_level);
  969. adc_reg->fifoctl = reg;
  970. /* enable ADC FIFO to access ADC CLOCK */
  971. sys_write32(sys_read32(CMU_ADCCLK) | CMU_ADCCLK_ADCFIFOCLKEN, CMU_ADCCLK);
  972. }
  973. return 0;
  974. }
  975. /* @brief set the ADC FIFO DRQ level */
  976. static int __adc_fifo_drq_level_set(struct device *dev, a_adc_fifo_e idx, uint8_t level)
  977. {
  978. struct acts_audio_adc *adc_reg = get_adc_reg_base(dev);
  979. uint32_t reg = adc_reg->fifoctl;
  980. if ((level > ADC_FIFO_MAX_DRQ_LEVEL) || (level == 0))
  981. return -EINVAL;
  982. if (ADC_FIFO_0 == idx) {
  983. reg &= ~ADC_FIFOCTL_DRQ_LEVEL_MASK;
  984. reg |= ADC_FIFOCTL_DRQ_LEVEL(level);
  985. } else {
  986. return -EINVAL;
  987. }
  988. adc_reg->fifoctl = reg;
  989. return 0;
  990. }
  991. /* @brief get the ADC FIFO DRQ level */
  992. static int __adc_fifo_drq_level_get(struct device *dev, a_adc_fifo_e idx)
  993. {
  994. struct acts_audio_adc *adc_reg = get_adc_reg_base(dev);
  995. uint32_t reg = adc_reg->fifoctl;
  996. int level;
  997. if (ADC_FIFO_0 == idx) {
  998. level = (reg & ADC_FIFOCTL_DRQ_LEVEL_MASK) >> ADC_FIFOCTL_DRQ_LEVEL_SHIFT;
  999. } else {
  1000. level = -EINVAL;
  1001. }
  1002. return level;
  1003. }
  1004. /* @brief ADC digital CIC over sample rate and FIR mode setting */
  1005. static void __adc_digital_ovfs_fir_cfg(struct device *dev, uint8_t sr)
  1006. {
  1007. struct acts_audio_adc *adc_reg = get_adc_reg_base(dev);
  1008. struct phy_adc_drv_data *data = dev->data;
  1009. uint32_t reg;
  1010. a_adc_fir_e fir;
  1011. /* Configure the Programmable frequency response curve according with the sample rate */
  1012. if (sr <= SAMPLE_RATE_16KHZ) {
  1013. fir = ADC_FIR_MODE_B;
  1014. } else if (sr < SAMPLE_RATE_96KHZ) {
  1015. fir = ADC_FIR_MODE_A;
  1016. } else {
  1017. fir = ADC_FIR_MODE_C;
  1018. }
  1019. reg = adc_reg->adc_digctl;
  1020. reg &= ~(ADC_DIGCTL_ADC_OVFS_MASK | ADC_DIGCTL_ADC_FIR_MD_SEL_MASK);
  1021. /* ADC works without ANC */
  1022. if (sr >= SAMPLE_RATE_96KHZ)
  1023. reg |= ADC_DIGCTL_ADC_OVFS(3); /* 64FS */
  1024. else
  1025. reg |= ADC_DIGCTL_ADC_OVFS(ADC_OSR_DEFAULT); /* 128FS */
  1026. /* ADC works with ANC */
  1027. if (data->anc_en) {
  1028. if (sr == SAMPLE_RATE_16KHZ) {
  1029. reg &= ~ADC_DIGCTL_ADC_OVFS_MASK;
  1030. reg |= ADC_DIGCTL_ADC_OVFS(0); /* ANC sample rate 16K CIC over sample rate select 192fs */
  1031. } else if (sr == SAMPLE_RATE_24KHZ) {
  1032. reg &= ~ADC_DIGCTL_ADC_OVFS_MASK;
  1033. reg |= ADC_DIGCTL_ADC_OVFS(1); /* ANC sample rate 24K CIC over sample rate select 128fs */
  1034. } else if (sr == SAMPLE_RATE_32KHZ) {
  1035. reg &= ~ADC_DIGCTL_ADC_OVFS_MASK;
  1036. reg |= ADC_DIGCTL_ADC_OVFS(2); /* ANC sample rate 32K CIC over sample rate select 96fs */
  1037. } else if (sr == SAMPLE_RATE_48KHZ) {
  1038. reg &= ~ADC_DIGCTL_ADC_OVFS_MASK;
  1039. reg |= ADC_DIGCTL_ADC_OVFS(3); /* ANC sample rate 48K CIC over sample rate select 64fs */
  1040. }
  1041. }
  1042. reg |= ADC_DIGCTL_ADC_FIR_MD_SEL(fir);
  1043. adc_reg->adc_digctl = reg;
  1044. }
  1045. /* @brief ADC channel digital configuration */
  1046. static void __adc_digital_channel_cfg(struct device *dev, a_adc_ch_e ch, a_adc_ch_type_e type, bool out_en)
  1047. {
  1048. struct acts_audio_adc *adc_reg = get_adc_reg_base(dev);
  1049. struct phy_adc_drv_data *data = dev->data;
  1050. uint32_t reg0, reg1;
  1051. uint32_t ch_digital = ADC_CH2REG(adc_reg, ch);
  1052. reg0 = adc_reg->adc_digctl;
  1053. reg1 = sys_read32(ch_digital);
  1054. LOG_DBG("channel:%d type:%d enable:%d", ch, type, out_en);
  1055. if (out_en) {
  1056. /* enable FIR/CIC clock */
  1057. sys_write32(sys_read32(CMU_ADCCLK) | CMU_ADCCLK_ADCCICEN | CMU_ADCCLK_ADCFIREN, CMU_ADCCLK);
  1058. if (ADC_AMIC == type) {
  1059. /* enable ANA clock */
  1060. sys_write32(sys_read32(CMU_ADCCLK) | CMU_ADCCLK_ADCANAEN, CMU_ADCCLK);
  1061. reg1 &= ~CH0_DIGCTL_MIC_SEL; /* enable ADC analog part */
  1062. } else if (ADC_DMIC == type) {
  1063. /* enable DMIC clock */
  1064. sys_write32(sys_read32(CMU_ADCCLK) | CMU_ADCCLK_ADCDMICEN, CMU_ADCCLK);
  1065. reg1 |= CH0_DIGCTL_MIC_SEL; /* enable ADC digital MIC part */
  1066. }
  1067. reg1 |= CH0_DIGCTL_DAT_OUT_EN; /* channel FIFO timing slot enable */
  1068. } else {
  1069. reg1 &= ~CH0_DIGCTL_DAT_OUT_EN;
  1070. reg0 &= ~(1 << (ADC_DIGCTL_ADC_DIG_SHIFT + ch)); /* channel disable */
  1071. adc_reg->adc_digctl = reg0;
  1072. /* check all channels disable */
  1073. if ((!(reg0 & ADC_DIGCTL_ADC_DIG_MASK)) && (!data->anc_en)) {
  1074. sys_write32(sys_read32(CMU_ADCCLK) & ~(CMU_ADCCLK_ADCDMICEN
  1075. | CMU_ADCCLK_ADCANAEN | CMU_ADCCLK_ADCCICEN
  1076. | CMU_ADCCLK_ADCFIREN), CMU_ADCCLK);
  1077. }
  1078. }
  1079. sys_write32(reg1, ch_digital);
  1080. }
  1081. /* @brief ADC channels enable at the same time */
  1082. static void __adc_digital_channels_en(struct device *dev, bool ch0_en, bool ch1_en, bool ch2_en, bool ch3_en)
  1083. {
  1084. struct acts_audio_adc *adc_reg = get_adc_reg_base(dev);
  1085. struct phy_adc_drv_data *data = dev->data;
  1086. uint32_t reg = adc_reg->adc_digctl;
  1087. uint32_t reg1 = adc_reg->hw_trigger_ctl;
  1088. //reg &= ~ADC_DIGCTL_ADC_DIG_MASK;
  1089. reg1 &= ~HW_TRIGGER_ADC_CTL_INT_TO_ADC_MASK;
  1090. if (ch0_en) {
  1091. reg |= ADC_DIGCTL_ADC0_DIG_EN;
  1092. reg1 |= HW_TRIGGER_ADC_CTL_INT_TO_ADC0_EN;
  1093. }
  1094. if (ch1_en) {
  1095. reg |= ADC_DIGCTL_ADC1_DIG_EN;
  1096. reg1 |= HW_TRIGGER_ADC_CTL_INT_TO_ADC1_EN;
  1097. }
  1098. if (ch2_en) {
  1099. reg |= ADC_DIGCTL_ADC2_DIG_EN;
  1100. reg1 |= HW_TRIGGER_ADC_CTL_INT_TO_ADC2_EN;
  1101. }
  1102. if (ch3_en) {
  1103. reg |= ADC_DIGCTL_ADC3_DIG_EN;
  1104. reg1 |= HW_TRIGGER_ADC_CTL_INT_TO_ADC3_EN;
  1105. }
  1106. if (data->hw_trigger_en)
  1107. adc_reg->hw_trigger_ctl = reg1;
  1108. else
  1109. adc_reg->adc_digctl = reg;
  1110. }
  1111. #if 0
  1112. /* @brief ADC HPF(High Pass Filter) audo-set configuration */
  1113. static void __adc_hpf_auto_set(struct device *dev, a_adc_ch_e ch, bool enable)
  1114. {
  1115. struct acts_audio_adc *adc_reg = get_adc_reg_base(dev);
  1116. const struct phy_adc_config_data *cfg = dev->config;
  1117. uint32_t reg, ch_digctl = ADC_CH2REG(adc_reg, ch);
  1118. a_hpf_time_e time = HPF_TIME_2;
  1119. if (!enable) {
  1120. /* disable HPF auto-set function */
  1121. sys_write32(sys_read32(ch_digctl) & ~CH0_DIGCTL_HPF_AS_EN, ch_digctl);
  1122. return ;
  1123. }
  1124. if (ADC_CHANNEL_0 == ch)
  1125. time = PHY_DEV_FEATURE(adc0_hpf_time);
  1126. else if (ADC_CHANNEL_1 == ch)
  1127. time = PHY_DEV_FEATURE(adc1_hpf_time);
  1128. else if (ADC_CHANNEL_2 == ch)
  1129. time = PHY_DEV_FEATURE(adc2_hpf_time);
  1130. else if (ADC_CHANNEL_3 == ch)
  1131. time = PHY_DEV_FEATURE(adc3_hpf_time);
  1132. reg = sys_read32(ch_digctl) & ~CH0_DIGCTL_HPF_AS_TS_MASK;
  1133. reg |= CH0_DIGCTL_HPF_AS_TS(time);
  1134. reg |= CH0_DIGCTL_HPF_AS_EN; /* HPF auto-set enable */
  1135. sys_write32(reg, ch_digctl);
  1136. LOG_DBG("%d ch@%d HPF reg:0x%x", __LINE__, ch, sys_read32(ch_digctl));
  1137. }
  1138. #endif
  1139. /* @brief ADC HPF configuration for fast stable */
  1140. static void __adc_hpf_fast_stable(struct device *dev, uint16_t input_dev, uint8_t sample_rate)
  1141. {
  1142. struct acts_audio_adc *adc_reg = get_adc_reg_base(dev);
  1143. uint8_t i;
  1144. uint32_t reg, ch_digctl = 0, en_flag = 0;
  1145. audio_input_map_t adc_input_map = {.audio_dev = input_dev,};
  1146. #ifdef CONFIG_CFG_DRV
  1147. struct phy_adc_drv_data *data = dev->data;
  1148. adc_input_map.ch0_input = data->input_ch0;
  1149. adc_input_map.ch1_input = data->input_ch1;
  1150. adc_input_map.ch2_input = data->input_ch2;
  1151. adc_input_map.ch3_input = data->input_ch3;
  1152. #else
  1153. if (board_audio_device_mapping(&adc_input_map)) {
  1154. LOG_ERR("invalid input device:0x%x", input_dev);
  1155. return ;
  1156. }
  1157. #endif
  1158. if (adc_input_map.ch0_input != ADC_CH_DISABLE)
  1159. en_flag |= BIT(0);
  1160. if (adc_input_map.ch1_input != ADC_CH_DISABLE)
  1161. en_flag |= BIT(1);
  1162. if (adc_input_map.ch2_input != ADC_CH_DISABLE)
  1163. en_flag |= BIT(2);
  1164. if (adc_input_map.ch3_input != ADC_CH_DISABLE)
  1165. en_flag |= BIT(3);
  1166. for (i = 0; i < ADC_MAX_CHANNELS_NUMBER; i++) {
  1167. if (en_flag & BIT(i)) {
  1168. ch_digctl = ADC_CH2REG(adc_reg, i);
  1169. reg = sys_read32(ch_digctl) & ~(0x7f << CH0_DIGCTL_HPF_N_SHIFT);
  1170. reg |= CH0_DIGCTL_HPF_N(0x03);
  1171. /* enable high frequency range and HPF function */
  1172. reg |= (CH0_DIGCTL_HPF_S | CH0_DIGCTL_HPFEN );
  1173. sys_write32(reg, ch_digctl);
  1174. }
  1175. }
  1176. }
  1177. #if 0
  1178. /* @brief ADC HPF(High Pass Filter) enable */
  1179. static void __adc_hpf_control(struct device *dev, a_adc_ch_e ch, bool enable)
  1180. {
  1181. struct acts_audio_adc *adc_reg = get_adc_reg_base(dev);
  1182. const struct phy_adc_config_data *cfg = dev->config;
  1183. uint32_t reg, ch_digctl = ADC_CH2REG(adc_reg, ch);
  1184. bool is_high;
  1185. uint8_t frequency;
  1186. if (!enable) {
  1187. sys_write32(sys_read32(ch_digctl) & ~CH0_DIGCTL_HPFEN, ch_digctl);
  1188. return ;
  1189. }
  1190. if (ADC_CHANNEL_0 == ch) {
  1191. is_high = PHY_DEV_FEATURE(adc0_hpf_fc_high);
  1192. frequency = PHY_DEV_FEATURE(adc0_frequency);
  1193. } else if (ADC_CHANNEL_1 == ch) {
  1194. is_high = PHY_DEV_FEATURE(adc1_hpf_fc_high);
  1195. frequency = PHY_DEV_FEATURE(adc1_frequency);
  1196. } else if (ADC_CHANNEL_2 == ch){
  1197. is_high = PHY_DEV_FEATURE(adc2_hpf_fc_high);
  1198. frequency = PHY_DEV_FEATURE(adc2_frequency);
  1199. } else {
  1200. is_high = PHY_DEV_FEATURE(adc3_hpf_fc_high);
  1201. frequency = PHY_DEV_FEATURE(adc3_frequency);
  1202. }
  1203. /* clear HPF_S and HPF_N */
  1204. reg = sys_read32(ch_digctl) & ~(0x7f << CH0_DIGCTL_HPF_N_SHIFT);
  1205. reg |= CH0_DIGCTL_HPF_N(frequency);
  1206. if (is_high)
  1207. reg |= CH0_DIGCTL_HPF_S;
  1208. /* enable HPF */
  1209. reg |= CH0_DIGCTL_HPFEN;
  1210. sys_write32(reg, ch_digctl);
  1211. LOG_DBG("%d ch@%d HPF reg:0x%x", __LINE__, ch, sys_read32(ch_digctl));
  1212. }
  1213. #endif
  1214. /* @brief ADC channel digital gain setting */
  1215. static void __adc_digital_gain_set(struct device *dev, a_adc_ch_e ch, uint8_t gain)
  1216. {
  1217. struct acts_audio_adc *adc_reg = get_adc_reg_base(dev);
  1218. uint32_t reg, ch_digctl = ADC_CH2REG(adc_reg, ch);
  1219. if (gain > ADC_DIGITAL_CH_GAIN_MAX)
  1220. gain = ADC_DIGITAL_CH_GAIN_MAX;
  1221. reg = sys_read32(ch_digctl) & ~CH0_DIGCTL_ADCGC_MASK;
  1222. reg |= CH0_DIGCTL_ADCGC(gain);
  1223. sys_write32(reg, ch_digctl);
  1224. }
  1225. #ifdef CONFIG_ADC_DMIC
  1226. /* @brief ADC digital dmic gain setting */
  1227. static void __adc_dmic_gain_set(struct device *dev, uint8_t gain)
  1228. {
  1229. struct acts_audio_adc *adc_reg = get_adc_reg_base(dev);
  1230. uint32_t reg;
  1231. if (gain > ADC_DIGITAL_DMIC_GAIN_MAX)
  1232. gain = ADC_DIGITAL_DMIC_GAIN_MAX;
  1233. reg = adc_reg->adc_digctl & ~ADC_DIGCTL_DMIC_PRE_GAIN_MASK;
  1234. reg |= ADC_DIGCTL_DMIC_PRE_GAIN(gain);
  1235. /* by default DMIC01 latch sequence is L firstly and then R */
  1236. #ifdef CONFIG_ADC_DMIC_RL_SEQUENCE
  1237. reg |= ADC_DIGCTL_DMIC01_CHS;
  1238. #endif
  1239. /* by default DMIC23 latch sequence is L firstly and then R */
  1240. #ifdef CONFIG_ADC_DMIC23_RL_SEQUENCE
  1241. reg |= ADC_DIGCTL_DMIC23_CHS;
  1242. #endif
  1243. #ifdef CONFIG_CFG_DRV
  1244. struct phy_adc_drv_data *data = dev->data;
  1245. if (data->external_config.DMIC01_Channel_Aligning)
  1246. reg |= ADC_DIGCTL_DMIC01_CHS; /* DMIC01 latch sequency: R/L */
  1247. if (data->external_config.DMIC23_Channel_Aligning)
  1248. reg |= ADC_DIGCTL_DMIC23_CHS; /* DMIC23 latch sequency: R/L */
  1249. #endif
  1250. adc_reg->adc_digctl = reg;
  1251. }
  1252. #endif
  1253. /* @brief ADC VMIC control initialization */
  1254. static void __adc_vmic_ctl_init(struct device *dev)
  1255. {
  1256. struct acts_audio_adc *adc_reg = get_adc_reg_base(dev);
  1257. adc_reg->vmic_ctl |= (ADC_VMIC_CTL_ISO_AVCC_AU | ADC_VMIC_CTL_ISO_VD18);
  1258. adc_reg->vmic_ctl &= ~ADC_VMIC_CTL_VMIC0_VOL_MASK;
  1259. adc_reg->vmic_ctl |= ADC_VMIC_CTL_VMIC0_VOL(vmic_voltage_array[0]);
  1260. adc_reg->vmic_ctl &= ~ADC_VMIC_CTL_VMIC1_VOL_MASK;
  1261. adc_reg->vmic_ctl |= ADC_VMIC_CTL_VMIC1_VOL(vmic_voltage_array[1]);
  1262. adc_reg->vmic_ctl &= ~ADC_VMIC_CTL_VMIC2_VOL_MASK;
  1263. adc_reg->vmic_ctl |= ADC_VMIC_CTL_VMIC2_VOL(vmic_voltage_array[2]);
  1264. }
  1265. #ifdef CONFIG_CFG_DRV
  1266. static void __adc_vmic_ctl(struct device *dev, uint8_t adc2vmic_index, bool is_en)
  1267. {
  1268. struct acts_audio_adc *adc_reg = get_adc_reg_base(dev);
  1269. LOG_DBG("adc2vmic index:%d is_en:%d",adc2vmic_index, is_en);
  1270. if (is_en) {
  1271. if (adc2vmic_index == 0)
  1272. adc_reg->vmic_ctl |= ADC_VMIC_CTL_VMIC0_EN(3); /* enable VMIC0 OP */
  1273. else if (adc2vmic_index == 1)
  1274. adc_reg->vmic_ctl |= ADC_VMIC_CTL_VMIC1_EN(3); /* enable VMIC1 OP */
  1275. else
  1276. adc_reg->vmic_ctl |= ADC_VMIC_CTL_VMIC2_EN(3); /* enable VMIC2 OP */
  1277. } else {
  1278. if (adc2vmic_index == 0)
  1279. adc_reg->vmic_ctl &= ~ADC_VMIC_CTL_VMIC0_EN_MASK; /* disable VMIC0 OP */
  1280. else if (adc2vmic_index == 1)
  1281. adc_reg->vmic_ctl &= ~ADC_VMIC_CTL_VMIC1_EN_MASK; /* disable VMIC1 OP */
  1282. else
  1283. adc_reg->vmic_ctl &= ~ADC_VMIC_CTL_VMIC2_EN_MASK; /* disable VMIC2 OP */
  1284. }
  1285. }
  1286. #endif
  1287. /* @brief ADC VMIC control */
  1288. static void __adc_vmic_ctl_enable(struct device *dev, uint16_t input_dev)
  1289. {
  1290. /* power-on MIC voltage */
  1291. #ifndef CONFIG_CFG_DRV
  1292. struct acts_audio_adc *adc_reg = get_adc_reg_base(dev);
  1293. if ((input_dev & AUDIO_DEV_TYPE_AMIC) || (input_dev & AUDIO_DEV_TYPE_DMIC))
  1294. #endif
  1295. {
  1296. /* vmic_ctl_array
  1297. * - 0: disable VMICx OP
  1298. * - 2: bypass VMICx OP
  1299. * - 3: enable VMICx OP
  1300. */
  1301. #ifdef CONFIG_CFG_DRV
  1302. struct phy_adc_drv_data *data = dev->data;
  1303. if (data->external_config.Enable_VMIC & ADC_0)
  1304. #else
  1305. if (vmic_ctl_array[0] <= 3)
  1306. #endif
  1307. {
  1308. #ifdef CONFIG_CFG_DRV
  1309. __adc_vmic_ctl(dev, adc_vmic_index_mapping_array[0], true);
  1310. #else
  1311. adc_reg->vmic_ctl &= ~ADC_VMIC_CTL_VMIC0_EN_MASK;
  1312. adc_reg->vmic_ctl |= ADC_VMIC_CTL_VMIC0_EN(vmic_ctl_array[0]);
  1313. #endif
  1314. }
  1315. #ifdef CONFIG_CFG_DRV
  1316. if (data->external_config.Enable_VMIC & ADC_1)
  1317. #else
  1318. if (vmic_ctl_array[1] <= 3)
  1319. #endif
  1320. {
  1321. #ifdef CONFIG_CFG_DRV
  1322. __adc_vmic_ctl(dev, adc_vmic_index_mapping_array[1], true);
  1323. #else
  1324. adc_reg->vmic_ctl &= ~ADC_VMIC_CTL_VMIC1_EN_MASK;
  1325. adc_reg->vmic_ctl |= ADC_VMIC_CTL_VMIC1_EN(vmic_ctl_array[1]);
  1326. #endif
  1327. }
  1328. #ifdef CONFIG_CFG_DRV
  1329. if (data->external_config.Enable_VMIC & ADC_2)
  1330. #else
  1331. if (vmic_ctl_array[2] <= 3)
  1332. #endif
  1333. {
  1334. #ifdef CONFIG_CFG_DRV
  1335. __adc_vmic_ctl(dev, adc_vmic_index_mapping_array[2], true);
  1336. #else
  1337. adc_reg->vmic_ctl &= ~ADC_VMIC_CTL_VMIC2_EN_MASK;
  1338. adc_reg->vmic_ctl |= ADC_VMIC_CTL_VMIC2_EN(vmic_ctl_array[2]);
  1339. #endif
  1340. }
  1341. k_sleep(K_MSEC(1));
  1342. }
  1343. }
  1344. /* @brief ADC VMIC control */
  1345. static void __adc_vmic_ctl_disable(struct device *dev, uint16_t input_dev)
  1346. {
  1347. /* power-off MIC voltage */
  1348. #ifndef CONFIG_CFG_DRV
  1349. struct acts_audio_adc *adc_reg = get_adc_reg_base(dev);
  1350. if ((input_dev & AUDIO_DEV_TYPE_AMIC) || (input_dev & AUDIO_DEV_TYPE_DMIC))
  1351. #endif
  1352. {
  1353. #ifdef CONFIG_CFG_DRV
  1354. struct phy_adc_drv_data *data = dev->data;
  1355. if (data->external_config.Enable_VMIC & ADC_0)
  1356. #else
  1357. if (vmic_ctl_array[0] <= 3)
  1358. #endif
  1359. {
  1360. #ifdef CONFIG_CFG_DRV
  1361. if (!(data->anc_en && data->external_config.Enable_ANC == ANC_FF)) {
  1362. __adc_vmic_ctl(dev, adc_vmic_index_mapping_array[0], false);
  1363. }
  1364. #else
  1365. adc_reg->vmic_ctl &= ~ADC_VMIC_CTL_VMIC0_EN_MASK; /* disable VMIC0 OP */
  1366. #endif
  1367. }
  1368. #ifdef CONFIG_CFG_DRV
  1369. if (data->external_config.Enable_VMIC & ADC_1)
  1370. #else
  1371. if (vmic_ctl_array[1] <= 3)
  1372. #endif
  1373. {
  1374. #ifdef CONFIG_CFG_DRV
  1375. if (!(data->anc_en && data->external_config.Enable_ANC == ANC_FB)) {
  1376. __adc_vmic_ctl(dev, adc_vmic_index_mapping_array[1], false);
  1377. }
  1378. #else
  1379. adc_reg->vmic_ctl &= ~ADC_VMIC_CTL_VMIC1_EN_MASK; /* disable VMIC1 OP */
  1380. #endif
  1381. }
  1382. #ifdef CONFIG_CFG_DRV
  1383. if (data->external_config.Enable_VMIC & ADC_2)
  1384. #else
  1385. if (vmic_ctl_array[2])
  1386. #endif
  1387. {
  1388. #ifdef CONFIG_CFG_DRV
  1389. __adc_vmic_ctl(dev, adc_vmic_index_mapping_array[2], false);
  1390. #else
  1391. adc_reg->vmic_ctl &= ~ADC_VMIC_CTL_VMIC2_EN_MASK; /* disable VMIC2 OP */
  1392. #endif
  1393. }
  1394. k_sleep(K_MSEC(1));
  1395. }
  1396. }
  1397. /* @brief ADC input analog gain setting */
  1398. static int __adc_input_gain_set(struct device *dev, a_adc_ch_e ch, uint8_t input_res,
  1399. uint8_t feedback_res, bool is_diff, bool update_fb)
  1400. {
  1401. struct acts_audio_adc *adc_reg = get_adc_reg_base(dev);
  1402. uint32_t reg, adc_ctl = ADC_CTL2REG(adc_reg, ch);
  1403. /* Set FDBUFx when in single end mode */
  1404. reg = sys_read32(adc_ctl);
  1405. if (!is_diff) {
  1406. reg &= ~ADC0_CTL_FDBUF0_IRS_MASK;
  1407. reg |= ADC0_CTL_FDBUF0_IRS(input_res);
  1408. reg |= 0x4 << ADC0_CTL_FDBUF0_IRS_SHIFT;
  1409. }
  1410. reg &= ~ADC0_CTL_INPUT0_IRS_MASK;
  1411. reg |= ADC0_CTL_INPUT0_IRS(input_res);
  1412. if (update_fb) {
  1413. reg &= ~ADC0_CTL_PREAM0_PG_MASK;
  1414. reg |= ADC0_CTL_PREAM0_PG(feedback_res);
  1415. }
  1416. sys_write32(reg, adc_ctl);
  1417. return 0;
  1418. }
  1419. /* @brief ADC channel0 analog control */
  1420. static int __adc_ch0_analog_control(struct device *dev, uint8_t inputx)
  1421. {
  1422. struct acts_audio_adc *adc_reg = get_adc_reg_base(dev);
  1423. uint32_t reg = adc_reg->adc_ctl0;
  1424. /* ADC channel disable or DMIC does not need to set ADCx_ctl */
  1425. if ((inputx == ADC_CH_DISABLE) || (inputx == ADC_CH_DMIC))
  1426. return 0;
  1427. if ((inputx != ADC_CH_INPUT0P) && (inputx != ADC_CH_INPUT0NP_DIFF)) {
  1428. LOG_ERR("invalid input:0x%x for ADC0", inputx);
  1429. return -EINVAL;
  1430. }
  1431. reg &= ~ADC0_CTL_INPUT0_IRS_MASK; /* clear input resistor */
  1432. reg &= ~ADC0_CTL_INPUT0_IN_MODE;
  1433. reg &= ~(0xf << ADC0_CTL_INPUT0P_EN_SHIFT); /* clear INPUTN/P pad to ADC channel input enable */
  1434. /* ADC0 channel sdm / PREOP 0 / FD BUF OP 0 diable*/
  1435. reg &= ~(ADC0_CTL_ADC0_EN | ADC0_CTL_PREOP0_EN | ADC0_CTL_FDBUF0_EN);
  1436. reg &= ~ADC0_CTL_PREAM0_PG_MASK;
  1437. reg &= ~ADC0_CTL_FDBUF0_IRS_MASK;
  1438. reg |= ADC0_CTL_VRDA0_EN; /* VRDA0 enable */
  1439. reg |= ADC0_CTL_FDBUF0_EN; /* FD BUF OP 0 enable for SE mode */
  1440. reg |= ADC0_CTL_PREOP0_EN; /* PREOP 0 enable */
  1441. reg |= ADC0_CTL_ADC0_EN; /* ADC0 channel enable */
  1442. /* by default to enable single end input mode: INPUT0P=>ADC0, INPUT0N=>ADC2 */
  1443. reg |= ADC0_CTL_INPUT0_IN_MODE;
  1444. /* input differential mode */
  1445. if (ADC_CH_INPUT0NP_DIFF == inputx) {
  1446. reg &= ~ADC0_CTL_INPUT0_IN_MODE; /* enable differential input mode */
  1447. reg &= ~ADC0_CTL_FDBUF0_EN; /* when sel diff mode, disable buf */
  1448. reg &= ~ADC0_CTL_FDBUF0_IRS_MASK; /* when select diff mode to disable connect buf */
  1449. }
  1450. /* enable INPUT0N pad to ADC0/2 channel */
  1451. if (inputx & ADC_CH_INPUT0N)
  1452. reg |= ADC0_CTL_INPUT0N_EN(3);
  1453. /* enable INPUT0P pad to ADC0 channel */
  1454. if (inputx & ADC_CH_INPUT0P)
  1455. reg |= ADC0_CTL_INPUT0P_EN(3);
  1456. /*input cap to ADC0 channel fast charge enable*/
  1457. reg |= ADC0_CTL_ADC0_CAPFC_EN;
  1458. /* input differential mode */
  1459. if (ADC_CH_INPUT0NP_DIFF == inputx)
  1460. reg |= ADC0_CTL_FDBUF0_IRS(0x0);
  1461. else
  1462. reg |= ADC0_CTL_FDBUF0_IRS(0x7);
  1463. reg |= ADC0_CTL_INPUT0_IRS(0x3);
  1464. adc_reg->adc_ctl0 = reg;
  1465. k_sleep(K_MSEC(50));
  1466. /*disable fast charge*/
  1467. reg &= ~ADC0_CTL_ADC0_CAPFC_EN;
  1468. reg &= ~ADC0_CTL_FDBUF0_IRS_MASK;
  1469. reg &= ~ADC0_CTL_INPUT0_IRS_MASK;
  1470. adc_reg->adc_ctl0 = reg;
  1471. return 0;
  1472. }
  1473. /* @brief ADC channel1 analog control */
  1474. static int __adc_ch1_analog_control(struct device *dev, uint8_t inputx)
  1475. {
  1476. struct acts_audio_adc *adc_reg = get_adc_reg_base(dev);
  1477. uint32_t reg = adc_reg->adc_ctl1;
  1478. /* ADC channel disable or DMIC does not need to set ADCx_ctl */
  1479. if ((inputx == ADC_CH_DISABLE) || (inputx == ADC_CH_DMIC))
  1480. return 0;
  1481. if ((inputx != ADC_CH_INPUT1P) && (inputx != ADC_CH_INPUT1NP_DIFF)) {
  1482. LOG_ERR("invalid input:0x%x for ADC1", inputx);
  1483. return -EINVAL;
  1484. }
  1485. reg &= ~ADC1_CTL_INPUT1_IRS_MASK; /* clear input resistor */
  1486. reg &= ~ADC1_CTL_INPUT1_IN_MODE;
  1487. reg &= ~(0xf << ADC1_CTL_INPUT1P_EN_SHIFT); /* clear INPUTN/P pad to ADC channel input enable */
  1488. /* ADC1 channel sdm / PREOP 1 / FD BUF OP 1 diable*/
  1489. reg &= ~(ADC1_CTL_ADC1_EN | ADC1_CTL_PREOP1_EN | ADC1_CTL_FDBUF1_EN);
  1490. reg &= ~ADC1_CTL_PREAM1_PG_MASK;
  1491. reg &= ~ADC1_CTL_FDBUF1_IRS_MASK;
  1492. reg |= ADC1_CTL_VRDA1_EN; /* VRDA1 enable */
  1493. reg |= ADC1_CTL_FDBUF1_EN; /* FD BUF OP 1 enable for SE mode */
  1494. reg |= ADC1_CTL_PREOP1_EN; /* PREOP 1 enable */
  1495. reg |= ADC1_CTL_ADC1_EN; /* ADC1 channel enable */
  1496. /* by default to enable single end input mode: INPUT1P=>ADC1, INPUT1N=>ADC3 */
  1497. reg |= ADC1_CTL_INPUT1_IN_MODE;
  1498. /* input differential mode */
  1499. if (ADC_CH_INPUT1NP_DIFF == inputx) {
  1500. reg &= ~ADC1_CTL_INPUT1_IN_MODE; /* enable differential input mode */
  1501. reg &= ~ADC1_CTL_FDBUF1_EN; /* when sel diff mode, disable buf */
  1502. reg &= ~ADC1_CTL_FDBUF1_IRS_MASK; /* when select diff mode to disable connect buf */
  1503. }
  1504. /* enable INPUT1N pad to ADC1/3 channel */
  1505. if (inputx & ADC_CH_INPUT1N)
  1506. reg |= ADC1_CTL_INPUT1N_EN(3);
  1507. /* enable INPUT1P pad to ADC1 channel */
  1508. if (inputx & ADC_CH_INPUT1P)
  1509. reg |= ADC1_CTL_INPUT1P_EN(3);
  1510. /*input cap to ADC1 channel fast charge enable*/
  1511. reg |= ADC1_CTL_ADC1_CAPFC_EN;
  1512. /* input differential mode */
  1513. if (ADC_CH_INPUT0NP_DIFF == inputx)
  1514. reg |= ADC1_CTL_FDBUF1_IRS(0x0);
  1515. else
  1516. reg |= ADC1_CTL_FDBUF1_IRS(0x7);
  1517. reg |= ADC1_CTL_INPUT1_IRS(0x3);
  1518. adc_reg->adc_ctl1 = reg;
  1519. /*disable fast charge*/
  1520. k_sleep(K_MSEC(50));
  1521. reg &= ~ADC1_CTL_ADC1_CAPFC_EN;
  1522. reg &= ~ADC1_CTL_FDBUF1_IRS_MASK;
  1523. reg &= ~ADC1_CTL_INPUT1_IRS_MASK;
  1524. adc_reg->adc_ctl1 = reg;
  1525. return 0;
  1526. }
  1527. /* @brief ADC channel2 analog control */
  1528. static int __adc_ch2_analog_control(struct device *dev, uint8_t inputx)
  1529. {
  1530. struct acts_audio_adc *adc_reg = get_adc_reg_base(dev);
  1531. uint32_t reg = adc_reg->adc_ctl2;
  1532. /* ADC channel disable or DMIC does not need to set ADCx_ctl */
  1533. if ((inputx == ADC_CH_DISABLE) || (inputx == ADC_CH_DMIC))
  1534. return 0;
  1535. if ((inputx & ADC_CH_INPUT2N) && !(inputx & ADC_CH_INPUT2P)) {
  1536. LOG_ERR("ADC2 does not support INPUT2N in SE mode");
  1537. return -EINVAL;
  1538. }
  1539. if ((inputx & ADC_CH_INPUT1N) || (inputx & ADC_CH_INPUT3P)) {
  1540. LOG_ERR("Invalid input:0x%x for ADC2", inputx);
  1541. return -EINVAL;
  1542. }
  1543. reg &= ~ADC2_CTL_INPUT2_IRS_MASK; /* clear input resistor */
  1544. reg &= ~ADC2_CTL_INPUT2_IN_MODE;
  1545. reg &= ~(0xf << ADC2_CTL_INPUT2P_EN_SHIFT); /* clear INPUTN/P pad to ADC channel input enable */
  1546. /* ADC2 channel sdm / PREOP 2 / FD BUF OP 2 diable*/
  1547. reg &= ~(ADC2_CTL_ADC2_EN | ADC2_CTL_PREOP2_EN | ADC2_CTL_FDBUF2_EN);
  1548. reg &= ~ADC2_CTL_PREAM2_PG_MASK;
  1549. reg &= ~ADC2_CTL_FDBUF2_IRS_MASK;
  1550. reg |= ADC2_CTL_VRDA2_EN; /* VRDA2 enable */
  1551. reg |= ADC2_CTL_FDBUF2_EN; /* FD BUF OP 2 enable for SE mode */
  1552. reg |= ADC2_CTL_PREOP2_EN; /* PREOP 2 enable */
  1553. reg |= ADC2_CTL_ADC2_EN; /* ADC2 channel enable */
  1554. /* by default to enable single end input mode: INPUT2P=>ADC2, INPUT2N=>ADC3 */
  1555. reg |= ADC2_CTL_INPUT2_IN_MODE;
  1556. /* input differential mode */
  1557. if (ADC_CH_INPUT2NP_DIFF == inputx) {
  1558. reg &= ~ADC2_CTL_INPUT2_IN_MODE; /* enable differential input mode */
  1559. reg &= ~ADC2_CTL_FDBUF2_EN; /* when sel diff mode, disable buf */
  1560. reg &= ~ADC2_CTL_FDBUF2_IRS_MASK; /* when select diff mode to disable connect buf */
  1561. }
  1562. if (inputx & ADC_CH_INPUT2N)
  1563. reg |= ADC2_CTL_INPUT2N_EN(3);
  1564. if (inputx & ADC_CH_INPUT2P)
  1565. reg |= ADC2_CTL_INPUT2P_EN(3);
  1566. /*input cap to ADC2 channel fast charge enable*/
  1567. reg |= ADC2_CTL_ADC2_CAPFC_EN;
  1568. /* input differential mode */
  1569. if (ADC_CH_INPUT0NP_DIFF == inputx)
  1570. reg |= ADC2_CTL_FDBUF2_IRS(0x0);
  1571. else
  1572. reg |= ADC2_CTL_FDBUF2_IRS(0x7);
  1573. reg |= ADC2_CTL_INPUT2_IRS(0x3);
  1574. adc_reg->adc_ctl2 = reg;
  1575. /*disable fast charge*/
  1576. k_sleep(K_MSEC(50));
  1577. reg &= ~ADC2_CTL_ADC2_CAPFC_EN;
  1578. reg &= ~ADC2_CTL_FDBUF2_IRS_MASK;
  1579. reg &= ~ADC2_CTL_INPUT2_IRS_MASK;
  1580. adc_reg->adc_ctl2 = reg;
  1581. /* in case of the INPUT0N pad to ADC2 */
  1582. if (inputx & ADC_CH_INPUT0N) {
  1583. adc_reg->adc_ctl0 |= ADC0_CTL_INPUT0N_EN(3); /* enable INPUT0N to ADC0 channel */
  1584. adc_reg->adc_ctl0 |= ADC0_CTL_INPUT0_IN_MODE; /* enable ADC0 single end mode */
  1585. }
  1586. /* in case of the INPUT3N pad to ADC2 */
  1587. if (inputx & ADC_CH_INPUT3N) {
  1588. adc_reg->adc_ctl3 |= ADC3_CTL_INPUT3N_EN(3); /* enable INPUT3N to ADC3 channel */
  1589. adc_reg->adc_ctl3 |= ADC3_CTL_INPUT3_IN_MODE; /* enable ADC3 single end mode */
  1590. }
  1591. return 0;
  1592. }
  1593. /* @brief ADC channel3 analog control */
  1594. static int __adc_ch3_analog_control(struct device *dev, uint8_t inputx)
  1595. {
  1596. struct acts_audio_adc *adc_reg = get_adc_reg_base(dev);
  1597. uint32_t reg = adc_reg->adc_ctl3;
  1598. /* ADC channel disable or DMIC does not need to set ADCx_ctl */
  1599. if ((inputx == ADC_CH_DISABLE) || (inputx == ADC_CH_DMIC))
  1600. return 0;
  1601. if ((inputx & ADC_CH_INPUT3N) && !(inputx & ADC_CH_INPUT3P)) {
  1602. LOG_ERR("ADC2 does not support INPUT3N in SE mode");
  1603. return -EINVAL;
  1604. }
  1605. if ((inputx & ADC_CH_INPUT0N) || (inputx & ADC_CH_INPUT2P)) {
  1606. LOG_ERR("Invalid input:0x%x for ADC3", inputx);
  1607. return -EINVAL;
  1608. }
  1609. reg &= ~ADC3_CTL_INPUT3_IRS_MASK; /* clear input resistor */
  1610. reg &= ~ADC3_CTL_INPUT3_IN_MODE;
  1611. reg &= ~(0xf << ADC3_CTL_INPUT3P_EN_SHIFT); /* clear INPUTN/P pad to ADC channel input enable */
  1612. /* ADC3 channel sdm / PREOP 3 / FD BUF OP 3 diable*/
  1613. reg &= ~(ADC3_CTL_ADC3_EN | ADC3_CTL_PREOP3_EN | ADC3_CTL_FDBUF3_EN);
  1614. reg &= ~ADC3_CTL_PREAM3_PG_MASK;
  1615. reg &= ~ADC3_CTL_FDBUF3_IRS_MASK;
  1616. reg |= ADC3_CTL_VRDA3_EN; /* VRDA3 enable */
  1617. reg |= ADC3_CTL_FDBUF3_EN; /* FD BUF OP 3 enable for SE mode */
  1618. reg |= ADC3_CTL_PREOP3_EN; /* PREOP 3 enable */
  1619. reg |= ADC3_CTL_ADC3_EN; /* ADC3 channel enable */
  1620. /* by default to enable single end input mode: INPUT3P=>ADC3, INPUT3N=>ADC2 */
  1621. reg |= ADC3_CTL_INPUT3_IN_MODE;
  1622. /* input differential mode */
  1623. if(ADC_CH_INPUT3NP_DIFF == inputx) {
  1624. reg &= ~ADC3_CTL_INPUT3_IN_MODE; /* enable differential input mode */
  1625. reg &= ~ADC3_CTL_FDBUF3_EN; /* when sel diff mode, disable buf */
  1626. reg &= ~ADC3_CTL_FDBUF3_IRS_MASK; /* when select diff mode to disable connect buf */
  1627. }
  1628. if (inputx & ADC_CH_INPUT3N)
  1629. reg |= ADC3_CTL_INPUT3N_EN(3);
  1630. if (inputx & ADC_CH_INPUT3P)
  1631. reg |= ADC3_CTL_INPUT3P_EN(3);
  1632. /*input cap to ADC3 channel fast charge enable*/
  1633. reg |= ADC3_CTL_ADC3_CAPFC_EN;
  1634. /* input differential mode */
  1635. if (ADC_CH_INPUT0NP_DIFF == inputx)
  1636. reg |= ADC3_CTL_FDBUF3_IRS(0x0);
  1637. else
  1638. reg |= ADC3_CTL_FDBUF3_IRS(0x7);
  1639. reg |= ADC3_CTL_INPUT3_IRS(0x3);
  1640. adc_reg->adc_ctl3 = reg;
  1641. /*disable fast charge*/
  1642. k_sleep(K_MSEC(50));
  1643. reg &= ~ADC3_CTL_ADC3_CAPFC_EN;
  1644. reg &= ~ADC3_CTL_FDBUF3_IRS_MASK;
  1645. reg &= ~ADC3_CTL_INPUT3_IRS_MASK;
  1646. adc_reg->adc_ctl3 = reg;
  1647. /* in case of the INPUT1N pad to ADC3 */
  1648. if (ADC_CH_INPUT1N == inputx) {
  1649. adc_reg->adc_ctl1 |= ADC1_CTL_INPUT1N_EN(3); /* enable INPUT1N to ADC0 channel */
  1650. adc_reg->adc_ctl1 |= ADC1_CTL_INPUT1_IN_MODE; /* enable ADC1 single end mode */
  1651. }
  1652. /* in case of the INPUT2N pad to ADC3 */
  1653. if(ADC_CH_INPUT2N == inputx) {
  1654. adc_reg->adc_ctl2 |= ADC2_CTL_INPUT2N_EN(3); /* enable INPUT1N to ADC0 channel */
  1655. adc_reg->adc_ctl2 |= ADC2_CTL_INPUT2_IN_MODE; /* enable ADC1 single end mode */
  1656. }
  1657. return 0;
  1658. }
  1659. #ifdef ADC_DIGITAL_DEBUG_IN_ENABLE
  1660. static void __adc_digital_debug_in(struct device *dev)
  1661. {
  1662. struct acts_audio_adc *adc_reg = get_adc_reg_base(dev);
  1663. /* set ADC clock divisor to '1' */
  1664. sys_write32(sys_read32(CMU_ADCCLK) & (~0x7), CMU_ADCCLK);
  1665. /* switch ADC debug clock to external PAD */
  1666. sys_write32(sys_read32(CMU_ADCCLK) | CMU_ADCCLK_ADCDEBUGEN, CMU_ADCCLK);
  1667. /* ADC Digital debug enable */
  1668. adc_reg->adc_digctl |= ADC_DIGCTL_ADDEN;
  1669. }
  1670. #endif
  1671. #ifdef ADC_ANALOG_DEBUG_OUT_ENABLE
  1672. static void __adc_analog_debug_out(struct device *dev)
  1673. {
  1674. struct acts_audio_adc *adc_reg = get_adc_reg_base(dev);
  1675. uint32_t reg = sys_read32(DEBUGSEL) & ~DEBUGSEL_DBGSE_MASK;
  1676. reg |= DBGSE_ADC << DEBUGSEL_DBGSE_SHIFT;
  1677. sys_write32(reg, DEBUGSEL);
  1678. sys_write32(sys_read32(DEBUGIE0) & (~0x107fe000), DEBUGIE0);
  1679. sys_write32(sys_read32(DEBUGOE0) | 0x107fe000, DEBUGOE0);
  1680. adc_reg->adc_digctl |= ADC_DIGCTL_AADEN;
  1681. }
  1682. #endif
  1683. /* @brief set the external trigger source for DAC digital start */
  1684. static int __adc_external_trigger_enable(struct device *dev, uint8_t trigger_src)
  1685. {
  1686. struct acts_audio_adc *adc_reg = get_adc_reg_base(dev);
  1687. struct phy_adc_drv_data *data = dev->data;
  1688. if (trigger_src > 6) {
  1689. LOG_ERR("Invalid ADC trigger source %d", trigger_src);
  1690. return -EINVAL;
  1691. }
  1692. adc_reg->hw_trigger_ctl &= ~ HW_TRIGGER_ADC_CTL_TRIGGER_SRC_SEL_MASK;
  1693. adc_reg->hw_trigger_ctl |= HW_TRIGGER_ADC_CTL_TRIGGER_SRC_SEL(trigger_src);
  1694. data->hw_trigger_en = 1;
  1695. return 0;
  1696. }
  1697. /* @brief disable the external irq signal to start ADC digital function */
  1698. static void __adc_external_trigger_disable(struct device *dev)
  1699. {
  1700. struct acts_audio_adc *adc_reg = get_adc_reg_base(dev);
  1701. struct phy_adc_drv_data *data = dev->data;
  1702. if (adc_reg->hw_trigger_ctl & HW_TRIGGER_ADC_CTL_INT_TO_ADC_MASK)
  1703. adc_reg->hw_trigger_ctl &= ~HW_TRIGGER_ADC_CTL_INT_TO_ADC_MASK;
  1704. data->hw_trigger_en = 0;
  1705. }
  1706. /* @brief ADC fast capacitor charge function */
  1707. static void adc_fast_cap_charge(struct device *dev, uint16_t input_dev)
  1708. {
  1709. struct acts_audio_adc *adc_reg = get_adc_reg_base(dev);
  1710. const struct phy_adc_config_data *cfg = dev->config;
  1711. audio_input_map_t adc_input_map = {.audio_dev = input_dev,};
  1712. if (!PHY_DEV_FEATURE(fast_cap_charge))
  1713. return ;
  1714. #ifdef CONFIG_CFG_DRV
  1715. struct phy_adc_drv_data *data = dev->data;
  1716. adc_input_map.ch0_input = data->input_ch0;
  1717. adc_input_map.ch1_input = data->input_ch1;
  1718. adc_input_map.ch2_input = data->input_ch2;
  1719. adc_input_map.ch3_input = data->input_ch3;
  1720. #else
  1721. if (board_audio_device_mapping(&adc_input_map)) {
  1722. LOG_ERR("invalid input device:0x%x", input_dev);
  1723. return ;
  1724. }
  1725. #endif
  1726. /* set input resistor to use 3k */
  1727. if (ADC_CH_DISABLE != adc_input_map.ch0_input) {
  1728. /* in single-end mode */
  1729. if ((adc_input_map.ch0_input & ADC_CH_INPUT0NP_DIFF) != ADC_CH_INPUT0NP_DIFF) {
  1730. adc_reg->adc_ctl0 &= ~ADC0_CTL_FDBUF0_IRS_MASK;
  1731. adc_reg->adc_ctl0 |= ADC0_CTL_FDBUF0_IRS(0x7);
  1732. }
  1733. adc_reg->adc_ctl0 &= ~ADC0_CTL_INPUT0_IRS_MASK;
  1734. adc_reg->adc_ctl0 |= ADC0_CTL_INPUT0_IRS(0x3);
  1735. /*Enable fast charge function of input cap to ADC L/R channel 0 */
  1736. adc_reg->adc_ctl0 |= ADC0_CTL_ADC0_CAPFC_EN;
  1737. }
  1738. if (ADC_CH_DISABLE != adc_input_map.ch1_input) {
  1739. /* in single-end mode */
  1740. if ((adc_input_map.ch1_input & ADC_CH_INPUT1NP_DIFF) != ADC_CH_INPUT1NP_DIFF) {
  1741. adc_reg->adc_ctl1 &= ~ADC1_CTL_FDBUF1_IRS_MASK;
  1742. adc_reg->adc_ctl1 |= ADC1_CTL_FDBUF1_IRS(0x7);
  1743. }
  1744. adc_reg->adc_ctl1 &= ~ADC1_CTL_INPUT1_IRS_MASK;
  1745. adc_reg->adc_ctl1 |= ADC1_CTL_INPUT1_IRS(0x3);
  1746. /*Enable fast charge function of input cap to ADC L/R channel 1 */
  1747. adc_reg->adc_ctl1 |= ADC1_CTL_ADC1_CAPFC_EN;
  1748. }
  1749. if (ADC_CH_DISABLE != adc_input_map.ch2_input) {
  1750. /* in single-end mode */
  1751. if ((adc_input_map.ch2_input & ADC_CH_INPUT2NP_DIFF) != ADC_CH_INPUT2NP_DIFF) {
  1752. adc_reg->adc_ctl2 &= ~ADC2_CTL_FDBUF2_IRS_MASK;
  1753. adc_reg->adc_ctl2 |= ADC2_CTL_FDBUF2_IRS(0x7);
  1754. }
  1755. adc_reg->adc_ctl2 &= ~ADC2_CTL_INPUT2_IRS_MASK;
  1756. adc_reg->adc_ctl2 |= ADC2_CTL_INPUT2_IRS(0x3);
  1757. /*Enable fast charge function of input cap to ADC L/R channel 2 */
  1758. adc_reg->adc_ctl2 |= ADC2_CTL_ADC2_CAPFC_EN;
  1759. }
  1760. if (ADC_CH_DISABLE != adc_input_map.ch3_input) {
  1761. /* in single-end mode */
  1762. if ((adc_input_map.ch3_input & ADC_CH_INPUT3NP_DIFF) != ADC_CH_INPUT3NP_DIFF) {
  1763. adc_reg->adc_ctl3 &= ~ADC3_CTL_FDBUF3_IRS_MASK;
  1764. adc_reg->adc_ctl3 |= ADC3_CTL_FDBUF3_IRS(0x7);
  1765. }
  1766. adc_reg->adc_ctl3 &= ~ADC3_CTL_INPUT3_IRS_MASK;
  1767. adc_reg->adc_ctl3 |= ADC3_CTL_INPUT3_IRS(0x3);
  1768. /*Enable fast charge function of input cap to ADC L/R channel 3 */
  1769. adc_reg->adc_ctl3 |= ADC3_CTL_ADC3_CAPFC_EN;
  1770. }
  1771. /* 3. wait for charging */
  1772. k_sleep(K_MSEC(ADC_FAST_CAP_CHARGE_TIME_MS));
  1773. /* disable fast charge of input CAP to ADC channels */
  1774. if (ADC_CH_DISABLE != adc_input_map.ch0_input)
  1775. adc_reg->adc_ctl0 &= ~ADC0_CTL_ADC0_CAPFC_EN;
  1776. if (ADC_CH_DISABLE != adc_input_map.ch1_input)
  1777. adc_reg->adc_ctl1 &= ~ADC1_CTL_ADC1_CAPFC_EN;
  1778. if (ADC_CH_DISABLE != adc_input_map.ch2_input)
  1779. adc_reg->adc_ctl2 &= ~ADC2_CTL_ADC2_CAPFC_EN;
  1780. if (ADC_CH_DISABLE != adc_input_map.ch3_input)
  1781. adc_reg->adc_ctl3 &= ~ADC3_CTL_ADC3_CAPFC_EN;
  1782. }
  1783. /* @brief ADC input channel analog configuration */
  1784. static int adc_input_config(struct device *dev, uint16_t input_dev)
  1785. {
  1786. audio_input_map_t adc_input_map = {.audio_dev = input_dev,};
  1787. int ret;
  1788. #ifdef CONFIG_CFG_DRV
  1789. struct phy_adc_drv_data *data = dev->data;
  1790. adc_input_map.ch0_input = data->input_ch0;
  1791. adc_input_map.ch1_input = data->input_ch1;
  1792. adc_input_map.ch2_input = data->input_ch2;
  1793. adc_input_map.ch3_input = data->input_ch3;
  1794. #else
  1795. if (board_audio_device_mapping(&adc_input_map))
  1796. return -ENOENT;
  1797. #endif
  1798. LOG_INF("ADC channel {dev:0x%x, [0x%x, 0x%x, 0x%x, 0x%x]}",
  1799. input_dev, adc_input_map.ch0_input, adc_input_map.ch1_input,
  1800. adc_input_map.ch2_input, adc_input_map.ch3_input);
  1801. // adc_fast_cap_charge(dev, input_dev);
  1802. ret = __adc_ch0_analog_control(dev, adc_input_map.ch0_input);
  1803. if (ret)
  1804. return ret;
  1805. ret = __adc_ch1_analog_control(dev, adc_input_map.ch1_input);
  1806. if (ret)
  1807. return ret;
  1808. ret = __adc_ch2_analog_control(dev, adc_input_map.ch2_input);
  1809. if (ret)
  1810. return ret;
  1811. ret = __adc_ch3_analog_control(dev, adc_input_map.ch3_input);
  1812. if (ret)
  1813. return ret;
  1814. return 0;
  1815. }
  1816. /* @brief ADC HPF (High Pass Filter) configuration */
  1817. static void adc_hpf_config(struct device *dev, a_adc_ch_e ch, bool enable)
  1818. {
  1819. //__adc_hpf_auto_set(dev, ch, enable);
  1820. // __adc_hpf_control(dev, ch, enable);
  1821. return;
  1822. }
  1823. /* @brief ADC channels enable according to the input audio device */
  1824. static int adc_channels_enable(struct device *dev, uint16_t input_dev)
  1825. {
  1826. uint8_t ch0_input = 0, ch1_input = 0, ch2_input = 0, ch3_input = 0;
  1827. audio_input_map_t adc_input_map = {.audio_dev = input_dev,};
  1828. #ifdef CONFIG_CFG_DRV
  1829. struct phy_adc_drv_data *data = dev->data;
  1830. adc_input_map.ch0_input = data->input_ch0;
  1831. adc_input_map.ch1_input = data->input_ch1;
  1832. adc_input_map.ch2_input = data->input_ch2;
  1833. adc_input_map.ch3_input = data->input_ch3;
  1834. #else
  1835. if (board_audio_device_mapping(&adc_input_map)) {
  1836. LOG_ERR("invalid input device:0x%x", input_dev);
  1837. return -ENOENT;
  1838. }
  1839. #endif
  1840. ch0_input = adc_input_map.ch0_input;
  1841. ch1_input = adc_input_map.ch1_input;
  1842. ch2_input = adc_input_map.ch2_input;
  1843. ch3_input = adc_input_map.ch3_input;
  1844. ADC_CHANNELS_CTL(true);
  1845. return 0;
  1846. }
  1847. /* @brief ADC channels disable according to the input audio device */
  1848. static int adc_channels_disable(struct device *dev, uint16_t input_dev)
  1849. {
  1850. uint8_t ch0_input = 0, ch1_input = 0, ch2_input = 0, ch3_input = 0;
  1851. audio_input_map_t adc_input_map = {.audio_dev = input_dev,};
  1852. #ifdef CONFIG_CFG_DRV
  1853. struct phy_adc_drv_data *data = dev->data;
  1854. adc_input_map.ch0_input = data->input_ch0;
  1855. adc_input_map.ch1_input = data->input_ch1;
  1856. adc_input_map.ch2_input = data->input_ch2;
  1857. adc_input_map.ch3_input = data->input_ch3;
  1858. #else
  1859. if (board_audio_device_mapping(&adc_input_map)) {
  1860. LOG_ERR("invalid input device:0x%x", input_dev);
  1861. return -ENOENT;
  1862. }
  1863. #endif
  1864. ch0_input = adc_input_map.ch0_input;
  1865. ch1_input = adc_input_map.ch1_input;
  1866. ch2_input = adc_input_map.ch2_input;
  1867. ch3_input = adc_input_map.ch3_input;
  1868. ADC_CHANNELS_CTL(false);
  1869. return 0;
  1870. }
  1871. /* @brief Enable the ADC digital function */
  1872. static int adc_digital_enable(struct device *dev, uint16_t input_dev, uint8_t sample_rate)
  1873. {
  1874. int ret;
  1875. /* configure OVFS and FIR */
  1876. __adc_digital_ovfs_fir_cfg(dev, sample_rate);
  1877. adc_fast_cap_charge(dev, input_dev);
  1878. /* set HPF high frequency range for fast stable */
  1879. __adc_hpf_fast_stable(dev, input_dev, sample_rate);
  1880. ret = adc_channels_enable(dev, input_dev);
  1881. return ret;
  1882. }
  1883. /* @brief Disable the ADC digital function */
  1884. static void adc_digital_disable(struct device *dev, uint16_t input_dev)
  1885. {
  1886. adc_channels_disable(dev, input_dev);
  1887. }
  1888. /* @brief Translate the AMIC/AUX gain from dB fromat to hardware register value */
  1889. static int adc_aux_amic_gain_translate(int16_t gain, uint8_t *input_res, uint8_t *fd_res, uint8_t *dig_gain)
  1890. {
  1891. int i;
  1892. for (i = 0; i < ARRAY_SIZE(amic_aux_gain_mapping); i++) {
  1893. if (gain <= amic_aux_gain_mapping[i].gain) {
  1894. *input_res = amic_aux_gain_mapping[i].input_res;
  1895. *fd_res = amic_aux_gain_mapping[i].feedback_res;
  1896. *dig_gain = amic_aux_gain_mapping[i].digital_gain;
  1897. LOG_INF("gain:%d map [%d %d %d]",
  1898. gain, *input_res, *fd_res, *dig_gain);
  1899. break;
  1900. }
  1901. }
  1902. if (i == ARRAY_SIZE(amic_aux_gain_mapping)) {
  1903. LOG_ERR("can not find out gain map %d", gain);
  1904. return -ENOENT;
  1905. }
  1906. return 0;
  1907. }
  1908. #ifdef CONFIG_ADC_DMIC
  1909. /* @brief Translate the DMIC gain from dB fromat to hardware register value */
  1910. static int adc_dmic_gain_translate(int16_t gain, uint8_t *dmic_gain, uint8_t *dig_gain)
  1911. {
  1912. int i;
  1913. for (i = 0; i < ARRAY_SIZE(dmic_gain_mapping); i++) {
  1914. if (gain <= dmic_gain_mapping[i].gain) {
  1915. *dmic_gain = dmic_gain_mapping[i].dmic_pre_gain;
  1916. *dig_gain = dmic_gain_mapping[i].digital_gain;
  1917. LOG_DBG("gain:%d map [%d %d]",
  1918. gain, *dmic_gain, *dig_gain);
  1919. break;
  1920. }
  1921. }
  1922. if (i == ARRAY_SIZE(dmic_gain_mapping)) {
  1923. LOG_ERR("can not find out gain map %d", gain);
  1924. return -ENOENT;
  1925. }
  1926. return 0;
  1927. }
  1928. #endif
  1929. /* @brief ADC config channel0 gain */
  1930. static int adc_ch0_gain_config(struct device *dev, uint8_t ch_input, int16_t ch_gain)
  1931. {
  1932. uint8_t dig_gain, input_res, fd_res;
  1933. if ((ADC_CH_DISABLE != ch_input) && (ADC_CH_DMIC != ch_input)) {
  1934. if (adc_aux_amic_gain_translate(ch_gain, &input_res, &fd_res, &dig_gain)) {
  1935. LOG_ERR("failed to translate amic_aux ch0 gain %d", ch_gain);
  1936. } else {
  1937. __adc_digital_gain_set(dev, ADC_CHANNEL_0, dig_gain);
  1938. if ((ADC_CH_INPUT0NP_DIFF & ch_input) == ADC_CH_INPUT0NP_DIFF)
  1939. __adc_input_gain_set(dev, ADC_CHANNEL_0, input_res, fd_res, true, true);
  1940. else
  1941. __adc_input_gain_set(dev, ADC_CHANNEL_0, input_res, fd_res, false, true);
  1942. }
  1943. }
  1944. return 0;
  1945. }
  1946. /* @brief ADC config channel1 gain */
  1947. static int adc_ch1_gain_config(struct device *dev, uint8_t ch_input, int16_t ch_gain)
  1948. {
  1949. uint8_t dig_gain, input_res, fd_res;
  1950. if ((ADC_CH_DISABLE != ch_input) && (ADC_CH_DMIC != ch_input)) {
  1951. if (adc_aux_amic_gain_translate(ch_gain, &input_res, &fd_res, &dig_gain)) {
  1952. LOG_ERR("failed to translate amic_aux ch1 gain %d", ch_gain);
  1953. } else {
  1954. __adc_digital_gain_set(dev, ADC_CHANNEL_1, dig_gain);
  1955. if ((ADC_CH_INPUT1NP_DIFF & ch_input) == ADC_CH_INPUT1NP_DIFF)
  1956. __adc_input_gain_set(dev, ADC_CHANNEL_1, input_res, fd_res, true, true);
  1957. else
  1958. __adc_input_gain_set(dev, ADC_CHANNEL_1, input_res, fd_res, false, true);
  1959. }
  1960. }
  1961. return 0;
  1962. }
  1963. /* @brief ADC config channel2 gain */
  1964. static int adc_ch2_gain_config(struct device *dev, uint8_t ch_input, int16_t ch_gain)
  1965. {
  1966. struct acts_audio_adc *adc_reg = get_adc_reg_base(dev);
  1967. uint8_t dig_gain, input_res, fd_res;
  1968. if ((ADC_CH_DISABLE != ch_input) && (ADC_CH_DMIC != ch_input)) {
  1969. if (adc_aux_amic_gain_translate(ch_gain, &input_res, &fd_res, &dig_gain)) {
  1970. LOG_ERR("failed to translate amic_aux ch2 gain %d", ch_gain);
  1971. } else {
  1972. __adc_digital_gain_set(dev, ADC_CHANNEL_2, dig_gain);
  1973. if ((ADC_CH_INPUT2NP_DIFF & ch_input) == ADC_CH_INPUT2NP_DIFF)
  1974. __adc_input_gain_set(dev, ADC_CHANNEL_2, input_res, fd_res, true, true);
  1975. else
  1976. __adc_input_gain_set(dev, ADC_CHANNEL_2, input_res, fd_res, false, true);
  1977. /* In case of the INPUT0N pad to ADC2 */
  1978. if (ADC_CH_INPUT0N & ch_input) {
  1979. /* check if INPUT0P is disabled or not */
  1980. if (!(adc_reg->adc_ctl0 & ADC0_CTL_INPUT0P_EN_MASK)) {
  1981. /* if INPUT0P is disabled, update the ADC0 input resistor */
  1982. __adc_input_gain_set(dev, ADC_CHANNEL_0, input_res, fd_res, false, false);
  1983. }
  1984. }
  1985. /* In case of the INPUT0N pad to ADC2 */
  1986. if (ADC_CH_INPUT3N & ch_input) {
  1987. /* check if INPUT3P is disabled or not */
  1988. if (!(adc_reg->adc_ctl3 & ADC3_CTL_INPUT3P_EN_MASK)) {
  1989. /* if INPUT3P is disabled, update the ADC3 input resistor */
  1990. __adc_input_gain_set(dev, ADC_CHANNEL_3, input_res, fd_res, false, false);
  1991. }
  1992. }
  1993. }
  1994. }
  1995. return 0;
  1996. }
  1997. /* @brief ADC config channel3 gain */
  1998. static int adc_ch3_gain_config(struct device *dev, uint8_t ch_input, int16_t ch_gain)
  1999. {
  2000. struct acts_audio_adc *adc_reg = get_adc_reg_base(dev);
  2001. uint8_t dig_gain, input_res, fd_res;
  2002. if ((ADC_CH_DISABLE != ch_input) && (ADC_CH_DMIC != ch_input)) {
  2003. if (adc_aux_amic_gain_translate(ch_gain, &input_res, &fd_res, &dig_gain)) {
  2004. LOG_ERR("failed to translate amic_aux ch3 gain %d", ch_gain);
  2005. } else {
  2006. __adc_digital_gain_set(dev, ADC_CHANNEL_3, dig_gain);
  2007. if ((ADC_CH_INPUT3NP_DIFF & ch_input) == ADC_CH_INPUT3NP_DIFF)
  2008. __adc_input_gain_set(dev, ADC_CHANNEL_3, input_res, fd_res, true, true);
  2009. else
  2010. __adc_input_gain_set(dev, ADC_CHANNEL_3, input_res, fd_res, false, true);
  2011. /* In case of the INPUT1N pad to ADC3 */
  2012. if (ADC_CH_INPUT1N & ch_input) {
  2013. /* check if INPUT1P is disabled or not */
  2014. if (!(adc_reg->adc_ctl1 & ADC1_CTL_INPUT1P_EN_MASK)) {
  2015. /* if INPUT1P is disabled, update the ADC1 input resistor */
  2016. __adc_input_gain_set(dev, ADC_CHANNEL_1, input_res, fd_res, false, false);
  2017. }
  2018. }
  2019. /* In case of the INPUT2N pad to ADC3 */
  2020. if (ADC_CH_INPUT2N & ch_input) {
  2021. /* check if INPUT2P is disabled or not */
  2022. if (!(adc_reg->adc_ctl2 & ADC2_CTL_INPUT2P_EN_MASK)) {
  2023. /* if INPUT2P is disabled, update the ADC2 input resistor */
  2024. __adc_input_gain_set(dev, ADC_CHANNEL_2, input_res, fd_res, false, false);
  2025. }
  2026. }
  2027. }
  2028. }
  2029. return 0;
  2030. }
  2031. /* @brief ADC gain configuration */
  2032. static int adc_gain_config(struct device *dev, uint16_t input_dev, adc_gain *gain)
  2033. {
  2034. uint8_t ch0_input, ch1_input, ch2_input, ch3_input;
  2035. int16_t ch0_gain = 0, ch1_gain = 0, ch2_gain = 0, ch3_gain = 0;
  2036. audio_input_map_t adc_input_map = {.audio_dev = input_dev,};
  2037. #ifdef CONFIG_CFG_DRV
  2038. struct phy_adc_drv_data *data = dev->data;
  2039. adc_input_map.ch0_input = data->input_ch0;
  2040. adc_input_map.ch1_input = data->input_ch1;
  2041. adc_input_map.ch2_input = data->input_ch2;
  2042. adc_input_map.ch3_input = data->input_ch3;
  2043. #else
  2044. if (board_audio_device_mapping(&adc_input_map))
  2045. return -ENOENT;
  2046. #endif
  2047. ch0_input = adc_input_map.ch0_input;
  2048. ch1_input = adc_input_map.ch1_input;
  2049. ch2_input = adc_input_map.ch2_input;
  2050. ch3_input = adc_input_map.ch3_input;
  2051. /* gain set when input channel enabled */
  2052. if (ADC_CH_DISABLE != ch0_input)
  2053. ch0_gain = gain->ch_gain[0];
  2054. if (ADC_CH_DISABLE != ch1_input)
  2055. ch1_gain = gain->ch_gain[1];
  2056. if (ADC_CH_DISABLE != ch2_input)
  2057. ch2_gain = gain->ch_gain[2];
  2058. if (ADC_CH_DISABLE != ch3_input)
  2059. ch3_gain = gain->ch_gain[3];
  2060. LOG_INF("ADC channel gain {%d, %d, %d, %d}",
  2061. ch0_gain, ch1_gain, ch2_gain, ch3_gain);
  2062. #ifdef CONFIG_ADC_DMIC
  2063. ADC_DMIC_GAIN_CFG(0);
  2064. ADC_DMIC_GAIN_CFG(1);
  2065. ADC_DMIC_GAIN_CFG(2);
  2066. ADC_DMIC_GAIN_CFG(3);
  2067. #endif
  2068. /* single-end is 6db minus than diff mode */
  2069. if ((ch0_input & ADC_CH_INPUT0NP_DIFF) != ADC_CH_INPUT0NP_DIFF)
  2070. ch0_gain += 60;
  2071. if ((ch1_input & ADC_CH_INPUT1NP_DIFF) != ADC_CH_INPUT1NP_DIFF)
  2072. ch1_gain += 60;
  2073. if ((ch2_input & ADC_CH_INPUT2NP_DIFF) != ADC_CH_INPUT2NP_DIFF)
  2074. ch2_gain += 60;
  2075. if ((ch3_input & ADC_CH_INPUT3NP_DIFF) != ADC_CH_INPUT3NP_DIFF)
  2076. ch3_gain += 60;
  2077. /* config channel0 gain */
  2078. adc_ch0_gain_config(dev, ch0_input, ch0_gain);
  2079. /* config channel1 gain */
  2080. adc_ch1_gain_config(dev, ch1_input, ch1_gain);
  2081. /* config channel2 gain */
  2082. adc_ch2_gain_config(dev, ch2_input, ch2_gain);
  2083. /* config channel3 gain */
  2084. adc_ch3_gain_config(dev, ch3_input, ch3_gain);
  2085. return 0;
  2086. }
  2087. /* @brief ADC sample rate config */
  2088. static int adc_sample_rate_set(struct device *dev, audio_sr_sel_e sr_khz)
  2089. {
  2090. struct phy_adc_drv_data *data = dev->data;
  2091. int ret;
  2092. uint8_t pre_div, clk_div, series, pll_index;
  2093. uint32_t reg;
  2094. uint16_t mclk = MCLK_256FS;
  2095. uint8_t ovfs_clk_div = 0, fir_clk_div = 0;
  2096. uint8_t i;
  2097. for (i = 0; i < ARRAY_SIZE(adc_anc_clk_mapping); i++) {
  2098. if (adc_anc_clk_mapping[i].sample_rate == sr_khz) {
  2099. mclk = adc_anc_clk_mapping[i].root_clk_div;
  2100. ovfs_clk_div = adc_anc_clk_mapping[i].ovfs_clk_div;
  2101. fir_clk_div = adc_anc_clk_mapping[i].fir_clk_div;
  2102. break;
  2103. }
  2104. }
  2105. /* Get audio PLL setting */
  2106. ret = audio_get_pll_setting(sr_khz, mclk, /* ADC clock source is fixed 256FS */
  2107. &pre_div, &clk_div, &series);
  2108. if (ret) {
  2109. LOG_DBG("get pll setting error:%d", ret);
  2110. return ret;
  2111. }
  2112. /* Check the pll usage and then config */
  2113. ret = audio_pll_check_config(series, &pll_index);
  2114. if (ret) {
  2115. LOG_DBG("check pll config error:%d", ret);
  2116. return ret;
  2117. }
  2118. reg = sys_read32(CMU_ADCCLK) & ~0x1FF;
  2119. /* Select pll0 or pll1 */
  2120. reg |= (pll_index & 0x1) << CMU_ADCCLK_ADCCLKSRC;
  2121. reg |= (pre_div << CMU_ADCCLK_ADCCLKPREDIV) | (clk_div << CMU_ADCCLK_ADCCLKDIV_SHIFT);
  2122. /* ADC OVFS clock divisor */
  2123. reg &= ~CMU_ADCCLK_ADCOVFSCLKDIV_MASK;
  2124. reg |= CMU_ADCCLK_ADCOVFSCLKDIV(ovfs_clk_div);
  2125. /* ADC FIR clock divisor */
  2126. if (fir_clk_div)
  2127. reg |= CMU_ADCCLK_ADCFIRCLKDIV;
  2128. else
  2129. reg &= ~CMU_ADCCLK_ADCFIRCLKDIV;
  2130. LOG_DBG("sr:%d pll_index:%d pre_div:%d clk_div:%d", sr_khz, pll_index, pre_div, clk_div);
  2131. data->audio_pll_index = pll_index;
  2132. sys_write32(reg, CMU_ADCCLK);
  2133. return 0;
  2134. }
  2135. /* @brief Get the sample rate from the ADC config */
  2136. static int adc_sample_rate_get(struct device *dev)
  2137. {
  2138. uint8_t pre_div, clk_div, pll_index;
  2139. uint32_t reg;
  2140. ARG_UNUSED(dev);
  2141. reg = sys_read32(CMU_ADCCLK);
  2142. pll_index = (reg & (1 << CMU_ADCCLK_ADCCLKSRC)) >> CMU_ADCCLK_ADCCLKSRC;
  2143. pre_div = (reg & (1 << CMU_ADCCLK_ADCCLKPREDIV)) >> CMU_ADCCLK_ADCCLKPREDIV;
  2144. clk_div = reg & CMU_ADCCLK_ADCCLKDIV_MASK;
  2145. return audio_get_pll_sample_rate(MCLK_256FS, pre_div, clk_div, pll_index);
  2146. }
  2147. /* @brief Get the AUDIO_PLL APS used by ADC */
  2148. static int adc_get_pll_aps(struct device *dev)
  2149. {
  2150. uint32_t reg;
  2151. uint8_t pll_index;
  2152. ARG_UNUSED(dev);
  2153. reg = sys_read32(CMU_ADCCLK);
  2154. pll_index = (reg & (1 << CMU_ADCCLK_ADCCLKSRC)) >> CMU_ADCCLK_ADCCLKSRC;
  2155. return audio_pll_get_aps((a_pll_type_e)pll_index);
  2156. }
  2157. /* @brief Set the AUDIO_PLL APS used by ADC */
  2158. static int adc_set_pll_aps(struct device *dev, audio_aps_level_e level)
  2159. {
  2160. uint32_t reg;
  2161. uint8_t pll_index;
  2162. ARG_UNUSED(dev);
  2163. reg = sys_read32(CMU_ADCCLK);
  2164. pll_index = (reg & (1 << CMU_ADCCLK_ADCCLKSRC)) >> CMU_ADCCLK_ADCCLKSRC;
  2165. return audio_pll_set_aps((a_pll_type_e)pll_index, level);
  2166. }
  2167. /* @brief Set the AUDIO_PLL index that used by ADC */
  2168. static int adc_get_pll_index(struct device *dev, uint8_t *idx)
  2169. {
  2170. uint32_t reg;
  2171. ARG_UNUSED(dev);
  2172. reg = sys_read32(CMU_ADCCLK);
  2173. *idx = (reg & (1 << CMU_ADCCLK_ADCCLKSRC)) >> CMU_ADCCLK_ADCCLKSRC;
  2174. return 0;
  2175. }
  2176. /* @brief ADC FIFO DRQ level set by input device */
  2177. static int adc_fifo_drq_level_set(struct device *dev, uint8_t level)
  2178. {
  2179. return __adc_fifo_drq_level_set(dev, ADC_FIFO_0, level);
  2180. }
  2181. /* @brief ADC FIFO DRQ level get by input device */
  2182. static int adc_fifo_drq_level_get(struct device *dev, uint8_t *level)
  2183. {
  2184. int ret;
  2185. ret = __adc_fifo_drq_level_get(dev, ADC_FIFO_0);
  2186. if (ret < 0) {
  2187. LOG_ERR("Failed to get ADC FIFO DRQ level err=%d", ret);
  2188. return ret;
  2189. }
  2190. *level = ret;
  2191. LOG_DBG("ADC DRQ level %d", *level);
  2192. return 0;
  2193. }
  2194. /* @brief check the FIFO is busy or not */
  2195. static int adc_check_fifo_busy(struct device *dev)
  2196. {
  2197. if (__is_adc_fifo_working(dev, ADC_FIFO_0)) {
  2198. LOG_INF("ADC FIFO0 now is working");
  2199. return -EBUSY;
  2200. }
  2201. return 0;
  2202. }
  2203. /* @brief ADC FIFO enable on the basic of the input audio device usage */
  2204. static int adc_fifo_enable(struct device *dev, ain_param_t *in_param)
  2205. {
  2206. audio_dma_width_e wd = (in_param->channel_width != CHANNEL_WIDTH_16BITS)
  2207. ? DMA_WIDTH_32BITS : DMA_WIDTH_16BITS;
  2208. __adc_fifo_disable(dev, ADC_FIFO_0);
  2209. return __adc_fifo_enable(dev, FIFO_SEL_DMA, wd,
  2210. ADC_FIFO_DRQ_LEVEL_DEFAULT, ADC_FIFO_0);;
  2211. }
  2212. /* @brief ADC FIFO enable on the basic of the input audio device usage */
  2213. static int adc_fifo_disable(struct device *dev)
  2214. {
  2215. __adc_fifo_disable(dev, ADC_FIFO_0);;
  2216. return 0;
  2217. }
  2218. /* @brief ADC BIAS setting for power saving */
  2219. static void adc_bias_setting(struct device *dev, uint16_t input_dev)
  2220. {
  2221. struct acts_audio_adc *adc_reg = get_adc_reg_base(dev);
  2222. #ifdef CONFIG_CFG_DRV
  2223. struct phy_adc_drv_data *data = dev->data;
  2224. adc_reg->bias = data->external_config.ADC_Bias_Setting;
  2225. #endif
  2226. /* BIAS enable */
  2227. adc_reg->bias |= ADC_BIAS_BIASEN;
  2228. }
  2229. /* @brief AUDIO LDO initialization */
  2230. static void adc_ldo_init(struct device *dev)
  2231. {
  2232. struct acts_audio_adc *adc_reg = get_adc_reg_base(dev);
  2233. const struct phy_adc_config_data *cfg = dev->config;
  2234. /* set the AUDIO LDO output voltage */
  2235. adc_reg->ref_ldo_ctl &= ~ADC_REF_LDO_CTL_AULDO_VOL_MASK;
  2236. adc_reg->ref_ldo_ctl |= ADC_REF_LDO_CTL_AULDO_VOL(PHY_DEV_FEATURE(ldo_voltage));
  2237. }
  2238. /* @brief Power control(enable or disable) by ADC LDO */
  2239. static void adc_ldo_power_control(struct device *dev, bool enable)
  2240. {
  2241. struct acts_audio_adc *adc_reg = get_adc_reg_base(dev);
  2242. uint32_t reg = adc_reg->ref_ldo_ctl;
  2243. if (enable) {
  2244. /** FIXME: HW issue
  2245. * ADC LDO shall be enabled when use DAC individually, otherwise VREF_ADD will get low voltage.
  2246. */
  2247. /* AULDO pull down current control */
  2248. reg &= ~ADC_REF_LDO_CTL_AULDO_PD_CTL_MASK;
  2249. reg |= ADC_REF_LDO_CTL_AULDO_PD_CTL(2);
  2250. /* VREF voltage divide res control */
  2251. reg &= ~ADC_REF_LDO_CTL_VREF_RSEL_MASK;
  2252. reg |= ADC_REF_LDO_CTL_VREF_RSEL(0);
  2253. reg |= ADC_REF_LDO_CTL_AULDO_EN(3);
  2254. adc_reg->ref_ldo_ctl = reg;
  2255. adc_reg->ref_ldo_ctl |= ADC_REF_LDO_CTL_VREF_EN;
  2256. if (!(reg & ADC_REF_LDO_CTL_VREF_EN)) {
  2257. LOG_INF("ADC wait for capacitor charge full");
  2258. /* enable VREF fast charge */
  2259. adc_reg->ref_ldo_ctl |= ADC_REF_LDO_CTL_VREF_FU;
  2260. if (!z_is_idle_thread_object(_current))
  2261. k_sleep(K_MSEC(ADC_LDO_CAPACITOR_CHARGE_TIME_MS));
  2262. else
  2263. k_busy_wait(ADC_LDO_CAPACITOR_CHARGE_TIME_MS * 1000UL);
  2264. /* disable LDO fast charge */
  2265. adc_reg->ref_ldo_ctl &= ~ADC_REF_LDO_CTL_VREF_FU;
  2266. }
  2267. /* Wait for AULDO stable */
  2268. if (!z_is_idle_thread_object(_current))
  2269. k_sleep(K_MSEC(1));
  2270. else
  2271. k_busy_wait(1000);
  2272. /* reduce AULDO static power consume */
  2273. uint32_t reg1 = adc_reg->ref_ldo_ctl;
  2274. reg1 &= ~ADC_REF_LDO_CTL_AULDO_PD_CTL_MASK;
  2275. reg1 &= ~ADC_REF_LDO_CTL_VREF_RSEL_MASK;
  2276. reg1 |= ADC_REF_LDO_CTL_VREF_RSEL(3);
  2277. adc_reg->ref_ldo_ctl = reg1;
  2278. } else {
  2279. /* check DAC LDO status */
  2280. uint32_t key = irq_lock();
  2281. if (!(reg & ADC_REF_LDO_CTL_DALDO_EN_MASK)) {
  2282. reg &= ~ADC_REF_LDO_CTL_AULDO_EN_MASK;
  2283. reg &= ~ADC_REF_LDO_CTL_VREF_EN;
  2284. adc_reg->ref_ldo_ctl = reg;
  2285. }
  2286. irq_unlock(key);
  2287. }
  2288. }
  2289. /* @brief ADC physical level enable procedure */
  2290. static int phy_adc_enable(struct device *dev, void *param)
  2291. {
  2292. const struct phy_adc_config_data *cfg = dev->config;
  2293. ain_param_t *in_param = (ain_param_t *)param;
  2294. adc_setting_t *adc_setting = in_param->adc_setting;
  2295. int ret;
  2296. if ((!in_param) || (!adc_setting)
  2297. || (!in_param->sample_rate)) {
  2298. LOG_ERR("Invalid parameters");
  2299. return -EINVAL;
  2300. }
  2301. if (in_param->channel_type != AUDIO_CHANNEL_ADC) {
  2302. LOG_ERR("Invalid channel type %d", in_param->channel_type);
  2303. return -EINVAL;
  2304. }
  2305. ret = adc_check_fifo_busy(dev);
  2306. if (ret)
  2307. return ret;
  2308. /* enable adc clock */
  2309. acts_clock_peripheral_enable(cfg->clk_id);
  2310. /* enable adc ldo */
  2311. adc_ldo_power_control(dev, true);
  2312. __adc_vmic_ctl_init(dev);
  2313. __adc_vmic_ctl_enable(dev, adc_setting->device);
  2314. /* audio_pll and adc clock setting */
  2315. if (adc_sample_rate_set(dev, in_param->sample_rate)) {
  2316. LOG_ERR("Failed to config sample rate %d", in_param->sample_rate);
  2317. return -ESRCH;
  2318. }
  2319. /* ADC FIFO enable */
  2320. ret = adc_fifo_enable(dev, in_param);
  2321. if (ret)
  2322. return ret;
  2323. /* ADC digital enable */
  2324. ret = adc_digital_enable(dev, adc_setting->device,
  2325. in_param->sample_rate);
  2326. if (ret) {
  2327. LOG_ERR("ADC digital enable error %d", ret);
  2328. goto err;
  2329. }
  2330. #ifdef ADC_DIGITAL_DEBUG_IN_ENABLE
  2331. __adc_digital_debug_in(dev);
  2332. #endif
  2333. #ifdef ADC_ANALOG_DEBUG_OUT_ENABLE
  2334. __adc_analog_debug_out(dev);
  2335. #endif
  2336. /* set ADC BIAS */
  2337. adc_bias_setting(dev, adc_setting->device);
  2338. /* ADC analog input enable */
  2339. ret = adc_input_config(dev, adc_setting->device);
  2340. if (ret) {
  2341. LOG_ERR("ADC input config error %d", ret);
  2342. goto err;
  2343. }
  2344. /* ADC gain setting */
  2345. ret = adc_gain_config(dev, adc_setting->device, &adc_setting->gain);
  2346. if (ret) {
  2347. LOG_ERR("ADC gain config error %d", ret);
  2348. goto err;
  2349. }
  2350. return ret;
  2351. err:
  2352. adc_fifo_disable(dev);
  2353. return ret;
  2354. }
  2355. /* @brief ADC physical level disable procedure */
  2356. static int phy_adc_disable(struct device *dev, void *param)
  2357. {
  2358. const struct phy_adc_config_data *cfg = dev->config;
  2359. struct acts_audio_adc *adc_reg = get_adc_reg_base(dev);
  2360. struct phy_adc_drv_data *data = dev->data;
  2361. uint16_t input_dev = *(uint16_t *)param;
  2362. LOG_INF("disable input device:0x%x", input_dev);
  2363. __adc_external_trigger_disable(dev);
  2364. /* DAC FIFO reset */
  2365. adc_fifo_disable(dev);
  2366. if (__adc_check_fifo_all_disable(dev)) {
  2367. adc_digital_disable(dev, input_dev);
  2368. if (!data->anc_en) {
  2369. adc_reg->adc_ctl0 = 0;
  2370. adc_reg->adc_ctl1 = 0;
  2371. } else {
  2372. #ifdef CONFIG_CFG_DRV
  2373. if (data->external_config.Enable_ANC == ANC_FF)
  2374. adc_reg->adc_ctl1 = 0;
  2375. else if (data->external_config.Enable_ANC == ANC_FB)
  2376. adc_reg->adc_ctl0 = 0;
  2377. #endif
  2378. }
  2379. adc_reg->adc_ctl2 = 0;
  2380. adc_reg->adc_ctl3 = 0;
  2381. /* TODO: check DAC depends on ADC issue */
  2382. /*if (!data->anc_en)
  2383. acts_clock_peripheral_disable(cfg->clk_id);*/
  2384. data->hw_trigger_en = 0;
  2385. /* disable VMIC power */
  2386. if (!data->anc_en) {
  2387. __adc_vmic_ctl_disable(dev, input_dev);
  2388. /* disable ADC LDO */
  2389. adc_ldo_power_control(dev, false);
  2390. acts_clock_peripheral_disable(cfg->clk_id);
  2391. audio_pll_unset(data->audio_pll_index);
  2392. }
  2393. }
  2394. return 0;
  2395. }
  2396. /* @brief Get the ADC DMA information */
  2397. static int adc_get_dma_info(struct device *dev, struct audio_in_dma_info *info)
  2398. {
  2399. const struct phy_adc_config_data *cfg = dev->config;
  2400. /* use ADC FIFO0 */
  2401. info->dma_info.dma_chan = cfg->dma_fifo0.dma_chan;
  2402. info->dma_info.dma_dev_name = cfg->dma_fifo0.dma_dev_name;
  2403. info->dma_info.dma_id = cfg->dma_fifo0.dma_id;
  2404. return 0;
  2405. }
  2406. static int adc_channels_start(struct device *dev, struct aduio_in_adc_en *ctl)
  2407. {
  2408. uint8_t i;
  2409. bool ch0_en = false, ch1_en = false, ch2_en = false, ch3_en = false;
  2410. audio_input_map_t adc_input_map = {0};
  2411. if (ctl->input_dev_num > ADC_FIFO_MAX_NUMBER) {
  2412. LOG_ERR("invalid input device number:%d", ctl->input_dev_num);
  2413. return -EINVAL;
  2414. }
  2415. for (i = 0; i < ctl->input_dev_num; i++) {
  2416. LOG_INF("start audio device 0x%x", ctl->input_dev_array[i]);
  2417. adc_input_map.audio_dev = ctl->input_dev_array[i];
  2418. #ifdef CONFIG_CFG_DRV
  2419. struct phy_adc_drv_data *data = dev->data;
  2420. adc_input_map.ch0_input = data->input_ch0;
  2421. adc_input_map.ch1_input = data->input_ch1;
  2422. adc_input_map.ch2_input = data->input_ch2;
  2423. adc_input_map.ch3_input = data->input_ch3;
  2424. #else
  2425. if (board_audio_device_mapping(&adc_input_map))
  2426. return -ENOENT;
  2427. #endif
  2428. if (ADC_CH_DISABLE != adc_input_map.ch0_input)
  2429. ch0_en = true;
  2430. if (ADC_CH_DISABLE != adc_input_map.ch1_input)
  2431. ch1_en = true;
  2432. if (ADC_CH_DISABLE != adc_input_map.ch2_input)
  2433. ch2_en = true;
  2434. if (ADC_CH_DISABLE != adc_input_map.ch3_input)
  2435. ch3_en = true;
  2436. }
  2437. __adc_digital_channels_en(dev, ch0_en, ch1_en, ch2_en, ch3_en);
  2438. return 0;
  2439. }
  2440. /* @brief check ADC is busy or not */
  2441. static bool adc_is_busy(struct device *dev)
  2442. {
  2443. struct acts_audio_adc *adc_reg = get_adc_reg_base(dev);
  2444. struct phy_adc_drv_data *data = dev->data;
  2445. bool is_busy = false;
  2446. if (data->anc_en)
  2447. is_busy = true;
  2448. if (adc_reg->adc_digctl & ADC_DIGCTL_ADC_DIG_MASK)
  2449. is_busy = true;
  2450. return is_busy;
  2451. }
  2452. /* @brief ADC ANC configuration */
  2453. static int adc_anc_control(struct device *dev, adc_anc_ctl_t *ctl)
  2454. {
  2455. #ifdef CONFIG_CFG_DRV
  2456. const struct phy_adc_config_data *cfg = dev->config;
  2457. struct phy_adc_drv_data *data = dev->data;
  2458. struct acts_audio_adc *adc_reg = get_adc_reg_base(dev);
  2459. if (!ctl) {
  2460. LOG_ERR("adc anc control invalid parameter");
  2461. return -EINVAL;
  2462. }
  2463. acts_clock_peripheral_enable(cfg->clk_id);
  2464. if (ctl->is_open_anc) {
  2465. if (data->anc_en) {
  2466. LOG_ERR("ADC ANC has already enabled");
  2467. return -EACCES;
  2468. }
  2469. data->anc_en = 1;
  2470. /* enable adc ldo */
  2471. adc_ldo_power_control(dev, true);
  2472. __adc_vmic_ctl_init(dev);
  2473. /* set ADC BIAS */
  2474. adc_bias_setting(dev, 0);
  2475. /* audio_pll and adc clock setting */
  2476. if (!(adc_reg->adc_digctl & ADC_DIGCTL_ADC_DIG_MASK)) {
  2477. if (adc_sample_rate_set(dev, SAMPLE_RATE_16KHZ)) {
  2478. LOG_ERR("Failed to config sample rate");
  2479. data->anc_en = 0;
  2480. return -ESRCH;
  2481. }
  2482. }
  2483. /* enable FIR/CIC/ANA clock */
  2484. sys_write32(sys_read32(CMU_ADCCLK) | CMU_ADCCLK_ADCCICEN \
  2485. | CMU_ADCCLK_ADCFIREN | CMU_ADCCLK_ADCANAEN, CMU_ADCCLK);
  2486. uint32_t reg = adc_reg->adc_digctl;
  2487. if (adc_reg->adc_digctl & ADC_DIGCTL_ADC_DIG_MASK) {
  2488. uint8_t sample_rate = adc_sample_rate_get(dev);
  2489. if (sample_rate == SAMPLE_RATE_16KHZ) {
  2490. reg &= ~ADC_DIGCTL_ADC_OVFS_MASK;
  2491. reg |= ADC_DIGCTL_ADC_OVFS(0); /* ANC sample rate 16K CIC over sample rate select 192fs */
  2492. } else if (sample_rate == SAMPLE_RATE_24KHZ) {
  2493. reg &= ~ADC_DIGCTL_ADC_OVFS_MASK;
  2494. reg |= ADC_DIGCTL_ADC_OVFS(1); /* ANC sample rate 24K CIC over sample rate select 128fs */
  2495. } else if (sample_rate == SAMPLE_RATE_32KHZ) {
  2496. reg &= ~ADC_DIGCTL_ADC_OVFS_MASK;
  2497. reg |= ADC_DIGCTL_ADC_OVFS(2); /* ANC sample rate 32K CIC over sample rate select 96fs */
  2498. } else if (sample_rate == SAMPLE_RATE_48KHZ) {
  2499. reg &= ~ADC_DIGCTL_ADC_OVFS_MASK;
  2500. reg |= ADC_DIGCTL_ADC_OVFS(3); /* ANC sample rate 48K CIC over sample rate select 64fs */
  2501. } else {
  2502. LOG_ERR("Current ADC sample rate:%d not support ANC", sample_rate);
  2503. data->anc_en = 0;
  2504. return -ENOTSUP;
  2505. }
  2506. }
  2507. adc_reg->adc_digctl = reg;
  2508. }
  2509. LOG_INF("ANC status:%d mode:%d",
  2510. ctl->is_open_anc, data->external_config.Enable_ANC);
  2511. /**
  2512. * ADC channels have fixed mapping to ANC mode as belown:
  2513. * ADC0 (INPUT0P) <--> ANC FF
  2514. * ADC1 (INPUT1P) <--> ANC FB
  2515. * ADC0/1 (INPUT0P/INPUT1P)<--> ANC FY
  2516. */
  2517. if (data->external_config.Enable_ANC == ANC_FF) {
  2518. if (ctl->is_open_anc) {
  2519. if (data->external_config.Mic_Config[0].Audio_In_Mode
  2520. == AUDIO_IN_MODE_ADC_DIFF) {
  2521. __adc_ch0_analog_control(dev, ADC_CH_INPUT0NP_DIFF);
  2522. adc_ch0_gain_config(dev, ADC_CH_INPUT0NP_DIFF, data->external_config.ANC_FF_GAIN);
  2523. } else {
  2524. __adc_ch0_analog_control(dev, ADC_CH_INPUT0P);
  2525. adc_ch0_gain_config(dev, ADC_CH_INPUT0P, (data->external_config.ANC_FF_GAIN + 60));
  2526. }
  2527. __adc_vmic_ctl(dev, adc_vmic_index_mapping_array[0], true);
  2528. } else {
  2529. if (!(adc_reg->ch0_digctl & ADC_DIGCTL_ADC0_DIG_EN)) {
  2530. adc_reg->adc_ctl0 = 0;
  2531. __adc_vmic_ctl(dev, adc_vmic_index_mapping_array[0], false);
  2532. }
  2533. }
  2534. } else if (data->external_config.Enable_ANC == ANC_FB) {
  2535. if (ctl->is_open_anc) {
  2536. if (data->external_config.Mic_Config[1].Audio_In_Mode
  2537. == AUDIO_IN_MODE_ADC_DIFF) {
  2538. __adc_ch1_analog_control(dev, ADC_CH_INPUT1NP_DIFF);
  2539. adc_ch1_gain_config(dev, ADC_CH_INPUT1NP_DIFF, data->external_config.ANC_FB_GAIN);
  2540. } else {
  2541. __adc_ch1_analog_control(dev, ADC_CH_INPUT1P);
  2542. adc_ch1_gain_config(dev, ADC_CH_INPUT1P, (data->external_config.ANC_FB_GAIN + 60));
  2543. }
  2544. __adc_vmic_ctl(dev, adc_vmic_index_mapping_array[1], true);
  2545. } else {
  2546. if (!(adc_reg->ch0_digctl & ADC_DIGCTL_ADC1_DIG_EN)) {
  2547. adc_reg->adc_ctl1 = 0;
  2548. __adc_vmic_ctl(dev, adc_vmic_index_mapping_array[1], false);
  2549. }
  2550. }
  2551. } else if (data->external_config.Enable_ANC == ANC_HY) {
  2552. if (ctl->is_open_anc) {
  2553. if (data->external_config.Mic_Config[2].Audio_In_Mode
  2554. == AUDIO_IN_MODE_ADC_DIFF) {
  2555. __adc_ch0_analog_control(dev, ADC_CH_INPUT0NP_DIFF);
  2556. __adc_ch1_analog_control(dev, ADC_CH_INPUT1NP_DIFF);
  2557. adc_ch0_gain_config(dev, ADC_CH_INPUT0NP_DIFF, data->external_config.ANC_FF_GAIN);
  2558. adc_ch1_gain_config(dev, ADC_CH_INPUT1NP_DIFF, data->external_config.ANC_FB_GAIN);
  2559. } else {
  2560. __adc_ch0_analog_control(dev, ADC_CH_INPUT0P);
  2561. __adc_ch1_analog_control(dev, ADC_CH_INPUT1P);
  2562. adc_ch0_gain_config(dev, ADC_CH_INPUT0P, (data->external_config.ANC_FF_GAIN + 60));
  2563. adc_ch1_gain_config(dev, ADC_CH_INPUT1P, (data->external_config.ANC_FB_GAIN + 60));
  2564. }
  2565. __adc_vmic_ctl(dev, adc_vmic_index_mapping_array[0], true);
  2566. __adc_vmic_ctl(dev, adc_vmic_index_mapping_array[1], true);
  2567. } else {
  2568. if (!(adc_reg->ch0_digctl & ADC_DIGCTL_ADC0_DIG_EN)) {
  2569. adc_reg->adc_ctl0 = 0;
  2570. __adc_vmic_ctl(dev, adc_vmic_index_mapping_array[0], false);
  2571. }
  2572. if (!(adc_reg->ch0_digctl & ADC_DIGCTL_ADC1_DIG_EN)) {
  2573. adc_reg->adc_ctl1 = 0;
  2574. __adc_vmic_ctl(dev, adc_vmic_index_mapping_array[1], false);
  2575. }
  2576. }
  2577. }
  2578. if (!ctl->is_open_anc) {
  2579. if (!data->anc_en) {
  2580. LOG_ERR("ADC ANC does not enable yet");
  2581. return -EACCES;
  2582. }
  2583. data->anc_en = 0;
  2584. /* TODO: check DAC depends on ADC issue */
  2585. /*if ((adc_reg->adc_digctl & ADC_DIGCTL_ADC_DIG_MASK))
  2586. acts_clock_peripheral_disable(cfg->clk_id);*/
  2587. /* check all ADC channels are disabled */
  2588. if (!(adc_reg->adc_digctl & ADC_DIGCTL_ADC_DIG_MASK)) {
  2589. sys_write32(sys_read32(CMU_ADCCLK) & ~(CMU_ADCCLK_ADCDMICEN
  2590. | CMU_ADCCLK_ADCANAEN | CMU_ADCCLK_ADCCICEN
  2591. | CMU_ADCCLK_ADCFIREN), CMU_ADCCLK);
  2592. /* all ADC channels disable to disable VMIC and ADC LDO */
  2593. __adc_vmic_ctl_disable(dev, AUDIO_DEV_TYPE_AMIC);
  2594. /* disable ADC LDO */
  2595. adc_ldo_power_control(dev, false);
  2596. }
  2597. }
  2598. #endif
  2599. return 0;
  2600. }
  2601. /* @brief ADC AEC configuration */
  2602. static int adc_aec_config(struct device *dev, bool is_en)
  2603. {
  2604. struct acts_audio_adc *adc_reg = get_adc_reg_base(dev);
  2605. uint8_t aec_sel = 0, pull_down = 0;
  2606. #if defined(CONFIG_CFG_DRV)
  2607. struct phy_adc_drv_data *data = dev->data;
  2608. if (data->external_config.Hw_Aec_Select == ADC_2)
  2609. aec_sel = 0;
  2610. else
  2611. aec_sel = 1;
  2612. #elif defined(CONFIG_AUDIO_ADC_0_AEC_SEL)
  2613. aec_sel = CONFIG_AUDIO_ADC_0_AEC_SEL;
  2614. #endif
  2615. #if defined(CONFIG_AUDIO_ADC_0_MIX_PULL_DOWN)
  2616. pull_down = CONFIG_AUDIO_ADC_0_MIX_PULL_DOWN;
  2617. #endif
  2618. if (is_en) {
  2619. /* HW AEC only support ADC2/3 */
  2620. if (aec_sel == 0) {
  2621. adc_hpf_config(dev, ADC_CHANNEL_2, 1);
  2622. __adc_digital_channel_cfg(dev, ADC_CHANNEL_2, ADC_AMIC, 1);
  2623. __adc_ch2_analog_control(dev, ADC_CH_INPUT2NP_DIFF);
  2624. __adc_digital_gain_set(dev, ADC_CHANNEL_2, 0);
  2625. adc_ch2_gain_config(dev, ADC_CH_INPUT2NP_DIFF, 0);
  2626. /* mask input*/
  2627. adc_reg->adc_ctl2 &= ~(ADC2_CTL_INPUT2P_EN_MASK | ADC2_CTL_INPUT2N_EN_MASK);
  2628. adc_reg->adc_ctl2 |= ADC2_CTL_PAL2AD2_EN;
  2629. if (pull_down)
  2630. adc_reg->adc_ctl2 |= ADC2_CTL_PAL2AD2_PD_EN;
  2631. adc_reg->adc_digctl |= ADC_DIGCTL_ADC2_DIG_EN;
  2632. __adc_hpf_fast_stable(dev, ADC_CHANNEL_2, SAMPLE_RATE_16KHZ);
  2633. } else if (aec_sel == 1) {
  2634. adc_hpf_config(dev, ADC_CHANNEL_3, 1);
  2635. __adc_digital_channel_cfg(dev, ADC_CHANNEL_3, ADC_AMIC, 1);
  2636. __adc_ch3_analog_control(dev, ADC_CH_INPUT3NP_DIFF);
  2637. adc_ch2_gain_config(dev, ADC_CH_INPUT3NP_DIFF, 0);
  2638. __adc_digital_gain_set(dev, ADC_CHANNEL_3, 0);
  2639. /* mask input*/
  2640. adc_reg->adc_ctl3 &= ~(ADC3_CTL_INPUT3P_EN_SHIFT | ADC3_CTL_INPUT3N_EN_MASK);
  2641. adc_reg->adc_ctl3 |= ADC3_CTL_PAL2AD3_EN;
  2642. if (pull_down)
  2643. adc_reg->adc_ctl3 |= ADC3_CTL_PAL2AD3_PD_EN;
  2644. adc_reg->adc_digctl |= ADC_DIGCTL_ADC3_DIG_EN;
  2645. __adc_hpf_fast_stable(dev, ADC_CHANNEL_3, SAMPLE_RATE_16KHZ);
  2646. } else {
  2647. LOG_ERR("Invalid HW AEC channel select:%d", aec_sel);
  2648. return -EINVAL;
  2649. }
  2650. } else {
  2651. if (aec_sel == 0) {
  2652. __adc_digital_channel_cfg(dev, ADC_CHANNEL_2, ADC_AMIC, 0);
  2653. adc_reg->adc_ctl2 = 0;
  2654. } else if (aec_sel == 1) {
  2655. __adc_digital_channel_cfg(dev, ADC_CHANNEL_3, ADC_AMIC, 0);
  2656. adc_reg->adc_ctl3 = 0;
  2657. }
  2658. }
  2659. return 0;
  2660. }
  2661. /* @brief ADC ioctl commands */
  2662. static int phy_adc_ioctl(struct device *dev, uint32_t cmd, void *param)
  2663. {
  2664. int ret = 0;
  2665. switch (cmd) {
  2666. case PHY_CMD_DUMP_REGS:
  2667. {
  2668. adc_dump_register(dev);
  2669. break;
  2670. }
  2671. case PHY_CMD_GET_AIN_DMA_INFO:
  2672. {
  2673. ret = adc_get_dma_info(dev, (struct audio_in_dma_info *)param);
  2674. break;
  2675. }
  2676. case PHY_CMD_ADC_DIGITAL_ENABLE:
  2677. {
  2678. ret = adc_channels_start(dev, (struct aduio_in_adc_en *)param);
  2679. break;
  2680. }
  2681. case PHY_CMD_ADC_GAIN_CONFIG:
  2682. {
  2683. adc_setting_t *setting = (adc_setting_t *)param;
  2684. ret = adc_gain_config(dev, setting->device, &setting->gain);
  2685. break;
  2686. }
  2687. case AIN_CMD_GET_SAMPLERATE:
  2688. {
  2689. ret = adc_sample_rate_get(dev);
  2690. if (ret < 0) {
  2691. LOG_ERR("Failed to get ADC sample rate err=%d", ret);
  2692. return ret;
  2693. }
  2694. *(audio_sr_sel_e *)param = (audio_sr_sel_e)ret;
  2695. ret = 0;
  2696. break;
  2697. }
  2698. case AIN_CMD_SET_SAMPLERATE:
  2699. {
  2700. audio_sr_sel_e val = *(audio_sr_sel_e *)param;
  2701. ret = adc_sample_rate_set(dev, val);
  2702. if (ret) {
  2703. LOG_ERR("Failed to set ADC sample rate err=%d", ret);
  2704. return ret;
  2705. }
  2706. break;
  2707. }
  2708. case AIN_CMD_GET_APS:
  2709. {
  2710. ret = adc_get_pll_aps(dev);
  2711. if (ret < 0) {
  2712. LOG_ERR("Failed to get audio pll APS err=%d", ret);
  2713. return ret;
  2714. }
  2715. *(audio_aps_level_e *)param = (audio_aps_level_e)ret;
  2716. ret = 0;
  2717. break;
  2718. }
  2719. case AIN_CMD_SET_APS:
  2720. {
  2721. audio_aps_level_e level = *(audio_aps_level_e *)param;
  2722. ret = adc_set_pll_aps(dev, level);
  2723. if (ret) {
  2724. LOG_ERR("Failed to set audio pll APS err=%d", ret);
  2725. return ret;
  2726. }
  2727. LOG_DBG("set new aps level %d", level);
  2728. break;
  2729. }
  2730. case PHY_CMD_GET_AUDIOPLL_IDX:
  2731. {
  2732. ret = adc_get_pll_index(dev, (uint8_t *)param);
  2733. break;
  2734. }
  2735. case PHY_CMD_FIFO_DRQ_LEVEL_GET:
  2736. {
  2737. uint32_t fifo_cmd = *(uint32_t *)param;
  2738. uint16_t input_dev = PHY_GET_FIFO_CMD_INDEX(fifo_cmd);
  2739. uint8_t level;
  2740. ret = adc_fifo_drq_level_get(dev, &level);
  2741. if (ret < 0)
  2742. return ret;
  2743. *(uint32_t *)param = PHY_FIFO_CMD(input_dev, level);
  2744. ret = 0;
  2745. break;
  2746. }
  2747. case PHY_CMD_FIFO_DRQ_LEVEL_SET:
  2748. {
  2749. uint32_t fifo_cmd = *(uint32_t *)param;
  2750. uint8_t level = PHY_GET_FIFO_CMD_VAL(fifo_cmd);
  2751. ret = adc_fifo_drq_level_set(dev, level);
  2752. break;
  2753. }
  2754. case AIN_CMD_SET_ADC_TRIGGER_SRC:
  2755. {
  2756. uint8_t src = *(uint8_t *)param;
  2757. ret = __adc_external_trigger_enable(dev, src);
  2758. break;
  2759. }
  2760. case AIN_CMD_ANC_CONTROL:
  2761. {
  2762. adc_anc_ctl_t *ctl = (adc_anc_ctl_t *)param;
  2763. ret = adc_anc_control(dev, ctl);
  2764. break;
  2765. }
  2766. case PHY_CMD_IS_ADC_BUSY:
  2767. {
  2768. *(uint8_t *)param = (uint8_t)adc_is_busy(dev);
  2769. break;
  2770. }
  2771. case AIN_CMD_AEC_CONTROL:
  2772. {
  2773. bool en = *(bool *)param;
  2774. ret = adc_aec_config(dev, en);
  2775. break;
  2776. }
  2777. default:
  2778. LOG_ERR("Unsupport command %d", cmd);
  2779. return -ENOTSUP;
  2780. }
  2781. return ret;
  2782. }
  2783. const struct phy_audio_driver_api phy_adc_drv_api = {
  2784. .audio_enable = phy_adc_enable,
  2785. .audio_disable = phy_adc_disable,
  2786. .audio_ioctl = phy_adc_ioctl
  2787. };
  2788. /* dump dac device tree infomation */
  2789. static void __adc_dt_dump_info(const struct phy_adc_config_data *cfg)
  2790. {
  2791. #if (PHY_DEV_SHOW_DT_INFO == 1)
  2792. LOG_INF("** ADC BASIC INFO **");
  2793. LOG_INF(" BASE: %08x", cfg->reg_base);
  2794. LOG_INF(" CLK-ID: %08x", cfg->clk_id);
  2795. LOG_INF(" RST-ID: %08x", cfg->rst_id);
  2796. LOG_INF("DMA0-NAME: %s", cfg->dma_fifo0.dma_dev_name);
  2797. LOG_INF(" DMA0-ID: %08x", cfg->dma_fifo0.dma_id);
  2798. LOG_INF(" DMA0-CH: %08x", cfg->dma_fifo0.dma_chan);
  2799. LOG_INF("** ADC FEATURES **");
  2800. LOG_INF(" ADC0-HPF-TIME: %d", PHY_DEV_FEATURE(adc0_hpf_time));
  2801. LOG_INF("ADC0-HPF-FC-HIGH: %d", PHY_DEV_FEATURE(adc0_hpf_fc_high));
  2802. LOG_INF(" ADC1-HPF-TIME: %d", PHY_DEV_FEATURE(adc1_hpf_time));
  2803. LOG_INF("ADC1-HPF-FC-HIGH: %d", PHY_DEV_FEATURE(adc1_hpf_fc_high));
  2804. LOG_INF(" ADC2-HPF-TIME: %d", PHY_DEV_FEATURE(adc2_hpf_time));
  2805. LOG_INF("ADC2-HPF-FC-HIGH: %d", PHY_DEV_FEATURE(adc2_hpf_fc_high));
  2806. LOG_INF(" ADC3-HPF-TIME: %d", PHY_DEV_FEATURE(adc3_hpf_time));
  2807. LOG_INF("ADC3-HPF-FC-HIGH: %d", PHY_DEV_FEATURE(adc3_hpf_fc_high));
  2808. LOG_INF(" ADC0-FREQUENCY: %d", PHY_DEV_FEATURE(adc0_frequency));
  2809. LOG_INF(" ADC1-FREQUENCY: %d", PHY_DEV_FEATURE(adc1_frequency));
  2810. LOG_INF(" ADC2-FREQUENCY: %d", PHY_DEV_FEATURE(adc2_frequency));
  2811. LOG_INF(" ADC3-FREQUENCY: %d", PHY_DEV_FEATURE(adc3_frequency));
  2812. LOG_INF(" FAST-CAP-CHARGE: %d", PHY_DEV_FEATURE(fast_cap_charge));
  2813. LOG_INF(" LDO-VOLTAGE: %d", PHY_DEV_FEATURE(ldo_voltage));
  2814. LOG_INF(" VMIC-CTL-EN: <%d, %d, %d>",
  2815. vmic_ctl_array[0], vmic_ctl_array[1], vmic_ctl_array[2]);
  2816. LOG_INF(" VMIC-CTL-VOL: <%d, %d>",
  2817. vmic_voltage_array[0], vmic_voltage_array[1]);
  2818. #endif
  2819. }
  2820. #ifdef CONFIG_CFG_DRV
  2821. static int __adc_config_dmic_mfp(struct device *dev, uint16_t clk_pin, uint16_t dat_pin)
  2822. {
  2823. int ret;
  2824. uint16_t pin, mfp;
  2825. pin = PHY_AUDIO_PIN_NUM_CFG(clk_pin);
  2826. mfp = PHY_AUDIO_PIN_MFP_CFG(clk_pin);
  2827. ret = acts_pinmux_set(pin, mfp);
  2828. if (ret) {
  2829. LOG_ERR("pin@%d config mfp error:%d", pin, mfp);
  2830. return ret;
  2831. }
  2832. pin = PHY_AUDIO_PIN_NUM_CFG(dat_pin);
  2833. mfp = PHY_AUDIO_PIN_MFP_CFG(dat_pin);
  2834. ret = acts_pinmux_set(pin, mfp);
  2835. if (ret) {
  2836. LOG_ERR("pin@%d config mfp error:%d", pin, mfp);
  2837. return ret;
  2838. }
  2839. return 0;
  2840. }
  2841. /* @brief Configure PINMUX for DMIC(Digital MIC) */
  2842. static int adc_config_dmic_mfp(struct device *dev, uint8_t ch0_input,
  2843. uint8_t ch1_input, uint8_t ch2_input, uint8_t ch3_input)
  2844. {
  2845. int ret = 0;
  2846. struct phy_adc_drv_data *data = dev->data;
  2847. if ((ADC_CH_DMIC == ch0_input)
  2848. || (ADC_CH_DMIC == ch1_input)) {
  2849. if ((data->external_config.DMIC_Select_GPIO.DMIC01_CLK != GPIO_NONE)
  2850. && (data->external_config.DMIC_Select_GPIO.DMIC01_DAT != GPIO_NONE)) {
  2851. ret = __adc_config_dmic_mfp(dev,
  2852. data->external_config.DMIC_Select_GPIO.DMIC01_CLK,
  2853. data->external_config.DMIC_Select_GPIO.DMIC01_DAT);
  2854. }
  2855. }
  2856. if ((ADC_CH_DMIC == ch2_input)
  2857. || (ADC_CH_DMIC == ch3_input)) {
  2858. if ((data->external_config.DMIC_Select_GPIO.DMIC23_CLK != GPIO_NONE)
  2859. && (data->external_config.DMIC_Select_GPIO.DMIC23_DAT != GPIO_NONE)) {
  2860. ret = __adc_config_dmic_mfp(dev,
  2861. data->external_config.DMIC_Select_GPIO.DMIC23_CLK,
  2862. data->external_config.DMIC_Select_GPIO.DMIC23_DAT);
  2863. }
  2864. }
  2865. return ret;
  2866. }
  2867. /* @brief Configure PINMUX for ANC DMIC */
  2868. static int adc_config_anc_dmic_mfp(struct device *dev)
  2869. {
  2870. int ret = 0;
  2871. struct phy_adc_drv_data *data = dev->data;
  2872. if (data->external_config.Enable_ANC) {
  2873. if ((data->external_config.ANCDMIC_Select_GPIO.DMIC01_CLK != GPIO_NONE)
  2874. && (data->external_config.ANCDMIC_Select_GPIO.DMIC01_DAT != GPIO_NONE)) {
  2875. ret = __adc_config_dmic_mfp(dev,
  2876. data->external_config.ANCDMIC_Select_GPIO.DMIC01_CLK,
  2877. data->external_config.ANCDMIC_Select_GPIO.DMIC01_DAT);
  2878. }
  2879. if ((data->external_config.ANCDMIC_Select_GPIO.DMIC23_CLK != GPIO_NONE)
  2880. && (data->external_config.ANCDMIC_Select_GPIO.DMIC23_DAT != GPIO_NONE)) {
  2881. ret = __adc_config_dmic_mfp(dev,
  2882. data->external_config.ANCDMIC_Select_GPIO.DMIC23_CLK,
  2883. data->external_config.ANCDMIC_Select_GPIO.DMIC23_DAT);
  2884. }
  2885. }
  2886. return ret;
  2887. }
  2888. /* @brief parser ADC INPUT from */
  2889. static int adc_input_parser(const struct device *dev)
  2890. {
  2891. struct phy_adc_drv_data *data = dev->data;
  2892. uint8_t input, i;
  2893. /* validate input channel0 */
  2894. input = data->external_config.ADC_Select_INPUT.ADC_Input_Ch0;
  2895. if ((!(input & ADC_CH_INPUT0P)) &&
  2896. (!(input & ADC_CH_INPUT0NP_DIFF))) {
  2897. LOG_ERR("invalid input ch0:%d", input);
  2898. return -EINVAL;
  2899. }
  2900. /* validate input channel1 */
  2901. input = data->external_config.ADC_Select_INPUT.ADC_Input_Ch1;
  2902. if ((!(input & ADC_CH_INPUT1P)) &&
  2903. (!(input & ADC_CH_INPUT1NP_DIFF))) {
  2904. LOG_ERR("invalid input ch1:%d", input);
  2905. return -EINVAL;
  2906. }
  2907. /* validate input channel2 */
  2908. input = data->external_config.ADC_Select_INPUT.ADC_Input_Ch2;
  2909. if ((!(input & ADC_CH_INPUT0N)) && (!(input & ADC_CH_INPUT2P)) &&
  2910. (!(input & ADC_CH_INPUT3N)) && (!(input & ADC_CH_INPUT2NP_DIFF))) {
  2911. LOG_ERR("invalid input ch1:%d", input);
  2912. return -EINVAL;
  2913. }
  2914. /* validate input channel3 */
  2915. input = data->external_config.ADC_Select_INPUT.ADC_Input_Ch3;
  2916. if ((!(input & ADC_CH_INPUT1N)) && (!(input & ADC_CH_INPUT2N)) &&
  2917. (!(input & ADC_CH_INPUT3P)) && (!(input & ADC_CH_INPUT3NP_DIFF))) {
  2918. LOG_ERR("invalid input ch3:%d", input);
  2919. return -EINVAL;
  2920. }
  2921. /* channel0 input parser */
  2922. if (data->external_config.Record_Adc_Select & ADC_0) {
  2923. for (i = 0; i < CFG_MAX_ADC_NUM; i++) {
  2924. if (data->external_config.Mic_Config[i].Adc_Index == ADC_0) {
  2925. if (data->external_config.Mic_Config[i].Mic_Type == ADC_TYPE_DMIC) {
  2926. data->input_ch0 = ADC_CH_DMIC;
  2927. } else {
  2928. if (data->external_config.Mic_Config[i].Audio_In_Mode == AUDIO_IN_MODE_ADC_DIFF) {
  2929. data->input_ch0 = ADC_CH_INPUT0NP_DIFF;
  2930. } else {
  2931. data->input_ch0 = data->external_config.ADC_Select_INPUT.ADC_Input_Ch0;
  2932. }
  2933. }
  2934. break;
  2935. }
  2936. }
  2937. if (i == CFG_MAX_ADC_NUM) {
  2938. data->input_ch0 = ADC_CH_DISABLE;
  2939. }
  2940. } else {
  2941. data->input_ch0 = ADC_CH_DISABLE;
  2942. }
  2943. /* channel1 input parser */
  2944. if (data->external_config.Record_Adc_Select & ADC_1) {
  2945. for (i = 0; i < CFG_MAX_ADC_NUM; i++) {
  2946. if (data->external_config.Mic_Config[i].Adc_Index == ADC_1) {
  2947. if (data->external_config.Mic_Config[i].Mic_Type == ADC_TYPE_DMIC) {
  2948. data->input_ch1 = ADC_CH_DMIC;
  2949. } else {
  2950. if (data->external_config.Mic_Config[i].Audio_In_Mode == AUDIO_IN_MODE_ADC_DIFF) {
  2951. data->input_ch1 = ADC_CH_INPUT1NP_DIFF;
  2952. } else {
  2953. data->input_ch1 = data->external_config.ADC_Select_INPUT.ADC_Input_Ch1;
  2954. }
  2955. }
  2956. break;
  2957. }
  2958. }
  2959. if (i == CFG_MAX_ADC_NUM)
  2960. data->input_ch1 = ADC_CH_DISABLE;
  2961. } else {
  2962. data->input_ch1 = ADC_CH_DISABLE;
  2963. }
  2964. /* channel2 input parser */
  2965. if (data->external_config.Record_Adc_Select & ADC_2) {
  2966. for (i = 0; i < CFG_MAX_ADC_NUM; i++) {
  2967. if (data->external_config.Mic_Config[i].Adc_Index == ADC_2) {
  2968. if (data->external_config.Mic_Config[i].Mic_Type == ADC_TYPE_DMIC) {
  2969. data->input_ch2 = ADC_CH_DMIC;
  2970. } else {
  2971. if (data->external_config.Mic_Config[i].Audio_In_Mode == AUDIO_IN_MODE_ADC_DIFF) {
  2972. data->input_ch2 = ADC_CH_INPUT2NP_DIFF;
  2973. } else {
  2974. data->input_ch2 = data->external_config.ADC_Select_INPUT.ADC_Input_Ch2;
  2975. }
  2976. }
  2977. break;
  2978. }
  2979. }
  2980. if (i == CFG_MAX_ADC_NUM)
  2981. data->input_ch2 = ADC_CH_DISABLE;
  2982. } else {
  2983. data->input_ch2 = ADC_CH_DISABLE;
  2984. }
  2985. /* channel3 input parser */
  2986. if (data->external_config.Record_Adc_Select & ADC_3) {
  2987. for (i = 0; i < CFG_MAX_ADC_NUM; i++) {
  2988. if (data->external_config.Mic_Config[i].Adc_Index == ADC_3) {
  2989. if (data->external_config.Mic_Config[i].Mic_Type == ADC_TYPE_DMIC) {
  2990. data->input_ch3 = ADC_CH_DMIC;
  2991. } else {
  2992. if (data->external_config.Mic_Config[i].Audio_In_Mode == AUDIO_IN_MODE_ADC_DIFF) {
  2993. data->input_ch3 = ADC_CH_INPUT3NP_DIFF;
  2994. } else {
  2995. data->input_ch3 = data->external_config.ADC_Select_INPUT.ADC_Input_Ch3;
  2996. }
  2997. }
  2998. break;
  2999. }
  3000. }
  3001. if (i == CFG_MAX_ADC_NUM)
  3002. data->input_ch3 = ADC_CH_DISABLE;
  3003. } else {
  3004. data->input_ch3 = ADC_CH_DISABLE;
  3005. }
  3006. if ((data->input_ch0 == ADC_CH_DMIC)
  3007. || (data->input_ch1 == ADC_CH_DMIC)
  3008. || (data->input_ch2 == ADC_CH_DMIC)
  3009. || (data->input_ch3 == ADC_CH_DMIC)) {
  3010. /* configure DMIC MFP */
  3011. if (adc_config_dmic_mfp((struct device *)dev, data->input_ch0,
  3012. data->input_ch1, data->input_ch2, data->input_ch3)) {
  3013. LOG_ERR("DMIC MFP config error");
  3014. return -ENXIO;
  3015. }
  3016. /* configure ANC DMIC MFP */
  3017. if (adc_config_anc_dmic_mfp((struct device *)dev)) {
  3018. LOG_ERR("ANC DMIC MFP config error");
  3019. return -ENXIO;
  3020. }
  3021. }
  3022. LOG_INF("ADC input parser ch0:%d ch1:%d ch2:%d ch3:%d",
  3023. data->input_ch0, data->input_ch1, data->input_ch2, data->input_ch3);
  3024. return 0;
  3025. }
  3026. /* @brief initialize ADC external configuration */
  3027. static int phy_adc_config_init(const struct device *dev)
  3028. {
  3029. struct phy_adc_drv_data *data = dev->data;
  3030. int ret;
  3031. uint8_t i;
  3032. /* CFG_Struct_Audio_Settings */
  3033. PHY_AUDIO_CFG(data->external_config, ITEM_AUDIO_ADC_BIAS_SETTING, ADC_Bias_Setting);
  3034. PHY_AUDIO_CFG(data->external_config, ITEM_AUDIO_DMIC01_CHANNEL_ALIGNING, DMIC01_Channel_Aligning);
  3035. PHY_AUDIO_CFG(data->external_config, ITEM_AUDIO_ENABLE_ANC, Enable_ANC);
  3036. PHY_AUDIO_CFG(data->external_config, ITEM_AUDIO_RECORD_ADC_SELECT, Record_Adc_Select);
  3037. PHY_AUDIO_CFG(data->external_config, ITEM_AUDIO_ENABLE_VMIC, Enable_VMIC);
  3038. PHY_AUDIO_CFG(data->external_config, ITEM_AUDIO_HW_AEC_SELECT, Hw_Aec_Select);
  3039. PHY_AUDIO_CFG(data->external_config, ITEM_AUDIO_ANC_FF_GAIN_SETTING, ANC_FF_GAIN);
  3040. PHY_AUDIO_CFG(data->external_config, ITEM_AUDIO_ANC_FB_GAIN_SETTING, ANC_FB_GAIN);
  3041. /* CFG_Type_DMIC_Select_GPIO */
  3042. ret = cfg_get_by_key(ITEM_AUDIO_DMIC_SELECT_GPIO,
  3043. &data->external_config.DMIC_Select_GPIO, sizeof(data->external_config.DMIC_Select_GPIO));
  3044. if (ret) {
  3045. LOG_DBG("** DMIC PINMUX **");
  3046. LOG_DBG("DMIC01_CLK:%d", data->external_config.DMIC_Select_GPIO.DMIC01_CLK);
  3047. LOG_DBG("DMIC01_DAT:%d", data->external_config.DMIC_Select_GPIO.DMIC01_DAT);
  3048. LOG_DBG("DMIC23_CLK:%d", data->external_config.DMIC_Select_GPIO.DMIC23_CLK);
  3049. LOG_DBG("DMIC23_DAT:%d", data->external_config.DMIC_Select_GPIO.DMIC23_DAT);
  3050. }
  3051. /* CFG_Type_DMIC_Select_GPIO */
  3052. ret = cfg_get_by_key(ITEM_AUDIO_ANCDMIC_SELECT_GPIO,
  3053. &data->external_config.DMIC_Select_GPIO, sizeof(data->external_config.DMIC_Select_GPIO));
  3054. if (ret) {
  3055. LOG_DBG("** ANCDMIC PINMUX **");
  3056. LOG_DBG("DMIC01_CLK:%d", data->external_config.DMIC_Select_GPIO.DMIC01_CLK);
  3057. LOG_DBG("DMIC01_DAT:%d", data->external_config.DMIC_Select_GPIO.DMIC01_DAT);
  3058. LOG_DBG("DMIC23_CLK:%d", data->external_config.DMIC_Select_GPIO.DMIC23_CLK);
  3059. LOG_DBG("DMIC23_DAT:%d", data->external_config.DMIC_Select_GPIO.DMIC23_DAT);
  3060. }
  3061. /* CFG_Type_Mic_Config */
  3062. ret = cfg_get_by_key(ITEM_AUDIO_MIC_CONFIG,
  3063. &data->external_config.Mic_Config, sizeof(data->external_config.Mic_Config));
  3064. if (ret) {
  3065. for (i = 0; i < ARRAY_SIZE(data->external_config.Mic_Config); i++) {
  3066. if (ADC_NONE != data->external_config.Mic_Config[i].Adc_Index) {
  3067. LOG_DBG("** MIC config@%d Info **", i);
  3068. LOG_DBG("Adc_Index:%d", data->external_config.Mic_Config[i].Adc_Index);
  3069. LOG_DBG("Mic_Type:%d", data->external_config.Mic_Config[i].Mic_Type);
  3070. LOG_DBG("Audio_In_Mode:%d", data->external_config.Mic_Config[i].Audio_In_Mode);
  3071. }
  3072. }
  3073. }
  3074. /* CFG_Type_ADC_Select_INPUT */
  3075. ret = cfg_get_by_key(ITEM_AUDIO_ADC_INPUT_SELECT,
  3076. &data->external_config.ADC_Select_INPUT, sizeof(data->external_config.ADC_Select_INPUT));
  3077. if (!ret) {
  3078. LOG_ERR("ADC input selection error");
  3079. return -ENOENT;
  3080. } else {
  3081. LOG_INF("ADC input ch0:%d ch1:%d ch2:%d ch3:%d",
  3082. data->external_config.ADC_Select_INPUT.ADC_Input_Ch0,
  3083. data->external_config.ADC_Select_INPUT.ADC_Input_Ch1,
  3084. data->external_config.ADC_Select_INPUT.ADC_Input_Ch2,
  3085. data->external_config.ADC_Select_INPUT.ADC_Input_Ch3);
  3086. }
  3087. return adc_input_parser(dev);
  3088. }
  3089. #endif
  3090. static int phy_adc_init(const struct device *dev)
  3091. {
  3092. const struct phy_adc_config_data *cfg = dev->config;
  3093. struct phy_adc_drv_data *data = dev->data;
  3094. __adc_dt_dump_info(cfg);
  3095. /* reset ADC controller */
  3096. acts_reset_peripheral(cfg->rst_id);
  3097. memset(data, 0, sizeof(struct phy_adc_drv_data));
  3098. adc_ldo_init((struct device *)dev);
  3099. #ifdef CONFIG_CFG_DRV
  3100. int ret;
  3101. ret = phy_adc_config_init(dev);
  3102. if (ret)
  3103. LOG_ERR("ADC external config init error:%d", ret);
  3104. #endif
  3105. printk("ADC init successfully\n");
  3106. return 0;
  3107. }
  3108. /* physical adc driver data */
  3109. static struct phy_adc_drv_data phy_adc_drv_data0;
  3110. /* physical adc config data */
  3111. static const struct phy_adc_config_data phy_adc_config_data0 = {
  3112. .reg_base = AUDIO_ADC_REG_BASE,
  3113. AUDIO_DMA_FIFO_DEF(ADC, 0),
  3114. .clk_id = CLOCK_ID_ADC,
  3115. .rst_id = RESET_ID_ADC,
  3116. PHY_DEV_FEATURE_DEF(adc0_hpf_time) = CONFIG_AUDIO_ADC_0_CH0_HPF_TIME,
  3117. PHY_DEV_FEATURE_DEF(adc0_hpf_fc_high) = CONFIG_AUDIO_ADC_0_CH0_HPF_FC_HIGH,
  3118. PHY_DEV_FEATURE_DEF(adc1_hpf_time) = CONFIG_AUDIO_ADC_0_CH1_HPF_TIME,
  3119. PHY_DEV_FEATURE_DEF(adc1_hpf_fc_high) = CONFIG_AUDIO_ADC_0_CH1_HPF_FC_HIGH,
  3120. PHY_DEV_FEATURE_DEF(adc2_hpf_time) = CONFIG_AUDIO_ADC_0_CH2_HPF_TIME,
  3121. PHY_DEV_FEATURE_DEF(adc2_hpf_fc_high) = CONFIG_AUDIO_ADC_0_CH2_HPF_FC_HIGH,
  3122. PHY_DEV_FEATURE_DEF(adc3_hpf_time) = CONFIG_AUDIO_ADC_0_CH3_HPF_TIME,
  3123. PHY_DEV_FEATURE_DEF(adc3_hpf_fc_high) = CONFIG_AUDIO_ADC_0_CH3_HPF_FC_HIGH,
  3124. PHY_DEV_FEATURE_DEF(adc0_frequency) = CONFIG_AUDIO_ADC_0_CH0_FREQUENCY,
  3125. PHY_DEV_FEATURE_DEF(adc1_frequency) = CONFIG_AUDIO_ADC_0_CH1_FREQUENCY,
  3126. PHY_DEV_FEATURE_DEF(adc2_frequency) = CONFIG_AUDIO_ADC_0_CH2_FREQUENCY,
  3127. PHY_DEV_FEATURE_DEF(adc3_frequency) = CONFIG_AUDIO_ADC_0_CH3_FREQUENCY,
  3128. PHY_DEV_FEATURE_DEF(ldo_voltage) = CONFIG_AUDIO_ADC_0_LDO_VOLTAGE,
  3129. PHY_DEV_FEATURE_DEF(fast_cap_charge) = CONFIG_AUDIO_ADC_0_FAST_CAP_CHARGE,
  3130. };
  3131. #if IS_ENABLED(CONFIG_AUDIO_ADC_0)
  3132. DEVICE_DEFINE(adc0, CONFIG_AUDIO_ADC_0_NAME, phy_adc_init, NULL,
  3133. &phy_adc_drv_data0, &phy_adc_config_data0,
  3134. POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_OBJECTS, &phy_adc_drv_api);
  3135. #endif