phy_audio_adc.c 109 KB

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