phy_audio_dac.c 130 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804
  1. /**
  2. * Copyright (c) 2020 Actions Semiconductor Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file
  8. * @brief Audio DAC physical channel implementation
  9. */
  10. /*
  11. * Features:
  12. * - Build in a 24 bits input sigma-delta DAC.
  13. * - 16 * 2 * 24 bits FIFO.
  14. * - Support digital volume with zero cross detection.
  15. * - Sample rate support 8k/12k/11.025k/16k/22.05k/24k/32k/44.1k/48k/88.2k/96k.
  16. * - Support antipop to restrain noise.
  17. */
  18. /*
  19. * Signal List
  20. * - AVCC: Analog power
  21. * - AGND: Analog ground
  22. * - PAGND: Ground for PA
  23. * - AOUTL/AOUTLP: Left output of PA / Left Positive output of PA
  24. * - AOUTR/AOUTLN: Right output of PA / Left Negative output of PA
  25. * - AOUTRP/VRO: Right Positive output of PA / Virtual Ground for PA
  26. * - AOUTRN/VROS: Right Negative output of PA / VRO Sense for PA
  27. */
  28. #include <kernel.h>
  29. #include <device.h>
  30. #include <ksched.h>
  31. #include <string.h>
  32. #include <errno.h>
  33. #include <soc.h>
  34. #include <board_cfg.h>
  35. #include "../phy_audio_common.h"
  36. #include <drivers/audio/audio_out.h>
  37. #ifdef CONFIG_CFG_DRV
  38. #include <config.h>
  39. #include <drivers/cfg_drv/driver_config.h>
  40. #include <drivers/gpio.h>
  41. #endif
  42. #include <logging/log.h>
  43. LOG_MODULE_REGISTER(dac0, LOG_LEVEL_DBG);
  44. /***************************************************************************************************
  45. * DAC_DIGCTL
  46. */
  47. #define DAC_DIGCTL_FIR_MODE_SHIFT (30)
  48. #define DAC_DIGCTL_FIR_MODE_MASK (0x3 << DAC_DIGCTL_FIR_MODE_SHIFT)
  49. #define DAC_DIGCTL_FIR_MODE(x) ((x) << DAC_DIGCTL_FIR_MODE_SHIFT)
  50. #define DAC_DIGCTL_OVDT_PD (27) /* */
  51. #define DAC_DIGCTL_OVDT_IRQ (26) /* */
  52. #define DAC_DIGCTL_AUMUTE_CTL BIT(25)
  53. #define DAC_DIGCTL_SDM_DITH_EN BIT(24) /* MIX a DC reference to SDM input */
  54. #define DAC_DIGCTL_DAF0M2DAEN BIT(17) /* DAC FIFO0 MIX to PCMBUF */
  55. #define DAC_DIGCTL_DADEN BIT(15) /* DAC analog DEBUG enable */
  56. #define DAC_DIGCTL_DDDEN BIT(14) /* DAC digital DEBUG enable */
  57. #define DAC_DIGCTL_AD2DALPEN_L BIT(12) /* ADC0 loop back to DAC Left channel */
  58. #define DAC_DIGCTL_AD2DALPEN_R BIT(11) /* ADC1 loop back to DAC right channel */
  59. #define DAC_DIGCTL_ADC01MIX BIT(10) /* 0: ADCL/R loopback to DACL/R; 1: ADCL MIX ADCR and then loopback to DACL/R */
  60. #define DAC_DIGCTL_DACHNUM BIT(8)
  61. #define DAC_DIGCTL_SR_SHIFT (4) /* FIFO1 IRQ/DRQ level and max to 30 levels*/
  62. #define DAC_DIGCTL_SR_MASK (0xF << DAC_DIGCTL_SR_SHIFT)
  63. #define DAC_DIGCTL_SR(x) ((x) << DAC_DIGCTL_SR_SHIFT)
  64. #define DAC_DIGCTL_CIC_RATE BIT(3)
  65. #define DAC_DIGCTL_ENDITH BIT(2) /* DAC dither enable */
  66. #define DAC_DIGCTL_DDEN BIT(0) /* DAC digital enable */
  67. /***************************************************************************************************
  68. * DAC_FIFOCTL
  69. */
  70. #define DAC_FIFOCTL_DRQ1_LEVEL_SHIFT (28) /* FIFO1 IRQ/DRQ level and max to 30 levels*/
  71. #define DAC_FIFOCTL_DRQ1_LEVEL_MASK (0xF << DAC_FIFOCTL_DRQ1_LEVEL_SHIFT)
  72. #define DAC_FIFOCTL_DRQ1_LEVEL(x) ((x) << DAC_FIFOCTL_DRQ1_LEVEL_SHIFT)
  73. #define DAC_FIFOCTL_FIFO1_VOL_SHIFT (24)
  74. #define DAC_FIFOCTL_FIFO1_VOL_MASK (0xF << DAC_FIFOCTL_FIFO1_VOL_SHIFT)
  75. #define DAC_FIFOCTL_FIFO1_VOL(x) ((x) << DAC_FIFOCTL_FIFO1_VOL_SHIFT)
  76. #define DAC_FIFOCTL_DACFIFO1_DMAWIDTH BIT(23) /* 0: 32bits; 1: 16bits */
  77. #define DAC_FIFOCTL_DAF1IS_SHIFT (20)
  78. #define DAC_FIFOCTL_DAF1IS_MASK (0x3 << DAC_FIFOCTL_DAF1IS_SHIFT)
  79. #define DAC_FIFOCTL_DAF1IS(x) ((x) << DAC_FIFOCTL_DAF1IS_SHIFT)
  80. #define DAC_FIFOCTL_DAF1EIE BIT(18) /* DAC FIFO1 Half-Empty IRQ enable */
  81. #define DAC_FIFOCTL_DAF1EDE BIT(17) /* DAC FIFO1 Half-Empty DRQ enable */
  82. #define DAC_FIFOCTL_DAF1RT BIT(16) /* DAC FIFO1 reset */
  83. #define DAC_FIFOCTL_DRQ0_LEVEL_SHIFT (12)
  84. #define DAC_FIFOCTL_DRQ0_LEVEL_MASK (0xF << DAC_FIFOCTL_DRQ0_LEVEL_SHIFT)
  85. #define DAC_FIFOCTL_DRQ0_LEVEL(x) ((x) << DAC_FIFOCTL_DRQ0_LEVEL_SHIFT)
  86. #define DAC_FIFOCTL_DACFIFO0_DMAWIDTH BIT(7)
  87. #define DAC_FIFOCTL_DAF0IS_SHIFT (4)
  88. #define DAC_FIFOCTL_DAF0IS_MASK (0x7 << DAC_FIFOCTL_DAF0IS_SHIFT)
  89. #define DAC_FIFOCTL_DAF0IS(x) ((x) << DAC_FIFOCTL_DAF0IS_SHIFT)
  90. #define DAC_FIFOCTL_DAF0_DSPDMA_EN BIT(3)
  91. #define DAC_FIFOCTL_DAF0EIE BIT(2)
  92. #define DAC_FIFOCTL_DAF0EDE BIT(1)
  93. #define DAC_FIFOCTL_DAF0RT BIT(0)
  94. /***************************************************************************************************
  95. * DAC_STAT
  96. */
  97. #define DAC_STAT_FIFO1_ER BIT(8) /* FIFO1 ERROR */
  98. #define DAC_STAT_DAF0EIP BIT(7)
  99. #define DAC_STAT_DAF0F BIT(6)
  100. #define DAC_STAT_DAF0S_SHIFT (0)
  101. #define DAC_STAT_DAF0S_MASK (0x1F << DAC_STAT_DAF0S_SHIFT)
  102. #define DAC_STAT_DAF0S(x) (x<<DAC_STAT_DAF0S_SHIFT)
  103. /***************************************************************************************************
  104. * DAC_DAT_FIFO0
  105. */
  106. #define DAC_DAT_FIFO0_DAFDAT_SHIFT (8)
  107. #define DAC_DAT_FIFO0_DAFDAT_MASK (0xFFFFFF << DAC_DAT_FIFO0_DAFDAT_SHIFT)
  108. #define DAC_DAT_FIFO0_DAFDAT(x) (((x) & DAC_DAT_FIFO0_DAFDAT_MASK) >> DAC_DAT_FIFO0_DAFDAT_SHIFT)
  109. /***************************************************************************************************
  110. * DAC_DAT_FIFO1
  111. */
  112. #define DAC_DAT_FIFO1_DAFDAT_SHIFT (8)
  113. #define DAC_DAT_FIFO1_DAFDAT_MASK (0xFFFFFF << DAC_DAT_FIFO1_DAFDAT_SHIFT)
  114. #define DAC_DAT_FIFO1_DAFDAT(x) (((x) & DAC_DAT_FIFO1_DAFDAT_MASK) >> DAC_DAT_FIFO1_DAFDAT_SHIFT)
  115. /***************************************************************************************************
  116. * PCM_BUF_CTL
  117. */
  118. #define PCM_BUF_CTL_PCMBEPIE BIT(7) /* PCMBUF empty IRQ enable */
  119. #define PCM_BUF_CTL_PCMBFUIE BIT(6) /* PCMBUF full IRQ enable */
  120. #define PCM_BUF_CTL_PCMBHEIE BIT(5) /* PCMBUF half empty IRQ enable */
  121. #define PCM_BUF_CTL_PCMBHFIE BIT(4) /* PCMBUF half full IRQ enable */
  122. #define PCM_BUF_CTL_IRQ_MASK (0xF << 4)
  123. /***************************************************************************************************
  124. * PCM_BUF_STAT
  125. */
  126. #define PCM_BUF_STAT_PCMBEIP BIT(19) /* PCMBUF empty IRQ pending */
  127. #define PCM_BUF_STAT_PCMBFIP BIT(18) /* PCMBUF full IRQ pending */
  128. #define PCM_BUF_STAT_PCMBHEIP BIT(17) /* PCMBUF half empty IRQ pending */
  129. #define PCM_BUF_STAT_PCMBHFIP BIT(16) /* PCMBUF half full IRQ pending */
  130. #define PCM_BUF_STAT_IRQ_MASK (0xF << 16)
  131. #define PCM_BUF_STAT_PCMBS_SHIFT (0)
  132. #define PCM_BUF_STAT_PCMBS_MASK (0x7FF << PCM_BUF_STAT_PCMBS_SHIFT) /* indicates the available samples to fill */
  133. #define PCM_BUF_STAT_PCMBS(x) (x<<PCM_BUF_STAT_PCMBS_SHIFT)
  134. /***************************************************************************************************
  135. * PCM_BUF_THRES_HE
  136. */
  137. #define PCM_BUF_THRES_HE_THRESHOLD_SHIFT (0)
  138. #define PCM_BUF_THRES_HE_THRESHOLD_MASK (0xFFF << PCM_BUF_THRES_HE_THRESHOLD_SHIFT)
  139. /***************************************************************************************************
  140. * PCM_BUF_THRES_HF
  141. */
  142. #define PCM_BUF_THRES_HF_THRESHOLD_SHIFT (0)
  143. #define PCM_BUF_THRES_HF_THRESHOLD_MASK (0xFFF << PCM_BUF_THRES_HF_THRESHOLD_SHIFT)
  144. /***************************************************************************************************
  145. * SDM_RESET_CTL
  146. */
  147. #define SDM_RESET_CTL_SDMCNT_SHIFT (16) /* SDM mute counter */
  148. #define SDM_RESET_CTL_SDMCNT_MASK (0xFFFF << SDM_RESET_CTL_SDMCNT_SHIFT)
  149. #define SDM_RESET_CTL_SDMCNT(x) ((x) << SDM_RESET_CTL_SDMCNT_SHIFT)
  150. #define SDM_RESET_CTL_SDMNDTH_SHIFT (4) /* SDM noise detection threshold */
  151. #define SDM_RESET_CTL_SDMNDTH_MASK (0xFFF << SDM_RESET_CTL_SDMNDTH_SHIFT)
  152. #define SDM_RESET_CTL_SDMNDTH(x) ((x) << SDM_RESET_CTL_SDMNDTH_SHIFT)
  153. #define SDM_RESET_CTL_SDMRDS_L BIT(1) /* 1: SDM detect L valid */
  154. #define SDM_RESET_CTL_SDMREEN BIT(0) /* reset SDM when has detected noise to avoid IDLE TONE; enable when sample rate 8K/11K/12K/16K */
  155. /***************************************************************************************************
  156. * AUTO_MUTE_CTL
  157. */
  158. #define AUTO_MUTE_CTL_AMCNT_SHIFT (16) /* auto mute counter */
  159. #define AUTO_MUTE_CTL_AMCNT_MASK (0xFFFF << AUTO_MUTE_CTL_AMCNT_SHIFT)
  160. #define AUTO_MUTE_CTL_AMCNT(x) ((x) << AUTO_MUTE_CTL_AMCNT_SHIFT)
  161. #define AUTO_MUTE_CTL_AMTH_SHIFT (4) /* auto mute threshold */
  162. #define AUTO_MUTE_CTL_AMTH_MASK (0xFFF << AUTO_MUTE_CTL_AMTH_SHIFT)
  163. #define AUTO_MUTE_CTL_AMTH(x) ((x) << AUTO_MUTE_CTL_AMTH_SHIFT)
  164. #define AUTO_MUTE_CTL_AMPD_OUT BIT(3)
  165. #define AUTO_MUTE_CTL_AMPD_IN BIT(2)
  166. #define AUTO_MUTE_CTL_AM_IRQ_EN BIT(1) /* auto mute IRQ enable */
  167. #define AUTO_MUTE_CTL_AMEN BIT(0) /* auto mute function enable */
  168. /***************************************************************************************************
  169. * VOL_LCH
  170. */
  171. #define VOL_LCH_DONE_PD BIT(22)
  172. #define VOL_LCH_VOLL_IRQ_EN BIT(21)
  173. #define VOL_LCH_TO_CNT BIT(20)
  174. #define VOL_LCH_ADJ_CNT_SHIFT (12)
  175. #define VOL_LCH_ADJ_CNT_MASK (0xFF << VOL_LCH_ADJ_CNT_SHIFT) /* the same as sample rate */
  176. #define VOL_LCH_ADJ_CNT(x) ((x) << VOL_LCH_ADJ_CNT_SHIFT)
  177. #define VOL_LCH_DONE_STA BIT(11) /* If 1 to indicate that DAC volume left channel soft stepping gain reach target done */
  178. #define VOL_LCH_SOFT_STEP_EN BIT(10)
  179. #define VOL_LCH_VOLLZCTOEN BIT(9)
  180. #define VOL_LCH_VOLLZCEN BIT(8)
  181. #define VOL_LCH_VOLL_SHIFT (0)
  182. #define VOL_LCH_VOLL_MASK (0xFF << VOL_LCH_VOLL_SHIFT)
  183. #define VOL_LCH_VOLL(x) ((x) << VOL_LCH_VOLL_SHIFT)
  184. #define VOL_LCH_SOFT_CFG_DEFAULT (VOL_LCH_VOLLZCEN | VOL_LCH_VOLLZCTOEN | VOL_LCH_SOFT_STEP_EN)
  185. /***************************************************************************************************
  186. * VOL_RCH
  187. */
  188. #define VOL_RCH_DONE_PD BIT(22)
  189. #define VOL_RCH_VOLR_IRQ_EN BIT(21)
  190. #define VOL_RCH_TO_CNT BIT(20)
  191. #define VOL_RCH_ADJ_CNT_SHIFT (12)
  192. #define VOL_RCH_ADJ_CNT_MASK (0xFF << VOL_RCH_ADJ_CNT_SHIFT)
  193. #define VOL_RCH_ADJ_CNT(x) ((x) << VOL_RCH_ADJ_CNT_SHIFT)
  194. #define VOL_RCH_DONE_STA BIT(11)
  195. #define VOL_RCH_SOFT_STEP_EN BIT(10)
  196. #define VOL_RCH_VOLRZCTOEN BIT(9)
  197. #define VOL_RCH_VOLRZCEN BIT(8)
  198. #define VOL_RCH_VOLR_SHIFT (0)
  199. #define VOL_RCH_VOLR_MASK (0xFF << VOL_RCH_VOLR_SHIFT)
  200. #define VOL_RCH_VOLR(x) ((x) << VOL_RCH_VOLR_SHIFT)
  201. #define VOL_RCH_SOFT_CFG_DEFAULT (VOL_RCH_VOLRZCEN | VOL_RCH_VOLRZCTOEN | VOL_RCH_SOFT_STEP_EN)
  202. /***************************************************************************************************
  203. * PCM_BUF_CNT
  204. */
  205. #define PCM_BUF_CNT_IP BIT(18) /* overflow pending */
  206. #define PCM_BUF_CNT_IE BIT(17)
  207. #define PCM_BUF_CNT_EN BIT(16)
  208. #define PCM_BUF_CNT_CNT_SHIFT (0)
  209. #define PCM_BUF_CNT_CNT_MASK (0xFFFF << PCM_BUF_CNT_CNT_SHIFT)
  210. /***************************************************************************************************
  211. * DAC_ANACTL0
  212. */
  213. #define DAC_ANACTL0_OVDTOUT BIT(23) /* pa/vro over load state (readonly) */
  214. #define DAC_ANACTL0_OVCDB BIT(22)
  215. #define DAC_ANACTL0_OVCSELH_SHIFT (17)
  216. #define DAC_ANACTL0_OVCSELH_MASK (0x1f<<DAC_ANACTL0_OVCSELH_SHIFT)
  217. #define DAC_ANACTL0_OVCSELH(x) (x<<DAC_ANACTL0_OVCSELH_SHIFT)
  218. #define DAC_ANACTL0_OVDTEN BIT(16)
  219. #define DAC_ANACTL0_SEL_FBCAP BIT(13)
  220. #define DAC_ANACTL0_DFCEN BIT(12)
  221. #define DAC_ANACTL0_PAVOL_SHIFT (9)
  222. #define DAC_ANACTL0_PAVOL_MASK (0x7<<DAC_ANACTL0_PAVOL_SHIFT)
  223. #define DAC_ANACTL0_PAVOL(x) (x<<DAC_ANACTL0_PAVOL_SHIFT)
  224. #define DAC_ANACTL0_DARSET BIT(8)
  225. #define DAC_ANACTL0_PAOSEN BIT(5)
  226. #define DAC_ANACTL0_PAEN BIT(4)
  227. #define DAC_ANACTL0_ZERODT BIT(3)
  228. #define DAC_ANACTL0_HOLDEBUGEN BIT(2)
  229. #define DAC_ANACTL0_DAENL BIT(1) /* DAC enable */
  230. #define DAC_ANACTL0_BIASEN BIT(0) /* DAC + PA current bias enable */
  231. /***************************************************************************************************
  232. * DAC_ANACTL1
  233. */
  234. #define DAC_ANACTL1_RAMDDEBUG (26)
  235. #define DAC_ANACTL1_RAMPDSTEP_SHIFT (24)
  236. #define DAC_ANACTL1_RAMPDSTEP_MASK (0x3 << DAC_ANACTL1_RAMPDSTEP_SHIFT)
  237. #define DAC_ANACTL1_RAMPDSTEP(x) (x<<DAC_ANACTL1_RAMPDSTEP_SHIFT)
  238. #define DAC_ANACTL1_RAMPDINI (23)
  239. #define DAC_ANACTL1_RAMPVOL_SHIFT (20)
  240. #define DAC_ANACTL1_RAMPVOL_MASK (0x3 << DAC_ANACTL1_RAMPVOL_SHIFT)
  241. #define DAC_ANACTL1_RAMPVOL(x) (x<<DAC_ANACTL1_RAMPVOL_SHIFT)
  242. #define DAC_ANACTL1_SMCCKS_SHIFT (18)
  243. #define DAC_ANACTL1_RAMPCLKSEL_MASK (0x3 << DAC_ANACTL1_SMCCKS_SHIFT)
  244. #define DAC_ANACTL1_RAMPCLKSEL(x) BIT(x<<DAC_ANACTL1_RAMPCLKSEL_SHIFT)
  245. #define DAC_ANACTL1_RAMPOPEN BIT(17)
  246. #define DAC_ANACTL1_RAMPDEN BIT(16)
  247. #define DAC_ANACTL1_ATPRPD BIT(6)
  248. #define DAC_ANACTL1_ATPSW2 BIT(5)
  249. #define DAC_ANACTL1_ATPSW1 BIT(4)
  250. #define DAC_ANACTL1_BCDISCH BIT(3)
  251. #define DAC_ANACTL1_ATPRC2EN BIT(2)
  252. #define DAC_ANACTL1_ATPRCEN BIT(1)
  253. #define DAC_ANACTL1_L2PEN BIT(0)
  254. /***************************************************************************************************
  255. * DAC_ANACTL2
  256. */
  257. #define DAC_ANACTL2_SHDEBUGEN BIT(20)
  258. #define DAC_ANACTL2_SHCL_SET_SHIFT (12) /* DAC SH clock divisor setting step2 */
  259. #define DAC_ANACTL2_SHCL_SET_MASK (0xFF << DAC_ANACTL2_SHCL_SET_SHIFT)
  260. #define DAC_ANACTL2_SHCL_SET(x) ((x) << DAC_ANACTL2_SHCL_SET_SHIFT)
  261. #define DAC_ANACTL2_SHCL_PW_SHIFT (4) /* DAC SH clock divisor setting step1 */
  262. #define DAC_ANACTL2_SHCL_PW_MASK (0xFF << DAC_ANACTL2_SHCL_PW_SHIFT)
  263. #define DAC_ANACTL2_SHCL_PW(x) ((x) << DAC_ANACTL2_SHCL_PW_SHIFT)
  264. #define DAC_ANACTL2_SHCL_SEL_SHIFT (2)
  265. #define DAC_ANACTL2_SHCL_SEL_MASK (0x3 << DAC_ANACTL2_SHCL_SEL_SHIFT)
  266. #define DAC_ANACTL2_SHCL_SEL(x) ((x) << DAC_ANACTL2_SHCL_SEL_SHIFT)
  267. #define DAC_ANACTL2_SH_CLKEN BIT(0) /* DAC SH clock enable for THD+N */
  268. /***************************************************************************************************
  269. * SDM_SAMPLES_CNT
  270. */
  271. #define SDM_SAMPLES_CNT_IP BIT(30) /* SDM sample counter overflow irq pending */
  272. #define SDM_SAMPLES_CNT_IE BIT(29) /* SDM sample counter overflow irq enable */
  273. #define SDM_SAMPLES_CNT_EN BIT(28) /* SDM sample counter enable */
  274. #define SDM_SAMPLES_CNT_CNT_SHIFT (0) /* SDM sample counter */
  275. #define SDM_SAMPLES_CNT_MASK (0xFFFFFFF << SDM_SAMPLES_CNT_CNT_SHIFT)
  276. /***************************************************************************************************
  277. * SDM_SAMPLES_NUM
  278. */
  279. #define SDM_SAMPLES_NUM_CNT_SHIFT (0) /* backup DAC_SDM_SAMPLES_CNT when tws/timer irq occured */
  280. #define SDM_SAMPLES_NUM_CNT_MASK (0xFFFFFFF << SDM_SAMPLES_NUM_CNT_SHIFT)
  281. /***************************************************************************************************
  282. * HW_TRIGGER_DAC_CTL
  283. */
  284. #define HW_TRIGGER_DAC_CTL_DSPDMA_DRQ_EN BIT(8)
  285. #define HW_TRIGGER_DAC_CTL_INT_TO_SDMCNT_EN BIT(7) /* if 1 to enable SDM counter trigger */
  286. #define HW_TRIGGER_DAC_CTL_INT_TO_DACFIFO_EN BIT(6) /* if 1 to enable external signal trigger DAC FIFO */
  287. #define HW_TRIGGER_DAC_CTL_INT_TO_SDM_CNT BIT(5) /* enable to backup DAC_SDM_SAMPLES_CNT */
  288. #define HW_TRIGGER_DAC_CTL_INT_TO_DAC_EN BIT(4) /* enable external irq signals to start DAC digital */
  289. #define HW_TRIGGER_DAC_CTL_TRIGGER_SRC_SEL_SHIFT (0) /* external irq source selection */
  290. #define HW_TRIGGER_DAC_CTL_TRIGGER_SRC_SRL_MASK (0xF << HW_TRIGGER_DAC_CTL_TRIGGER_SRC_SEL_SHIFT)
  291. #define HW_TRIGGER_DAC_CTL_TRIGGER_SRC_SRL(x) ((x) << HW_TRIGGER_DAC_CTL_TRIGGER_SRC_SEL_SHIFT)
  292. /***************************************************************************************************
  293. * DC_REF_DAT
  294. */
  295. #define DC_REF_DAT_DC_DAT_SEL_SHIFT (8)
  296. #define DC_REF_DAT_DC_DAT_SEL_MASK (0xFFFFFF<<DC_REF_DAT_DC_DAT_SEL_SHIFT)
  297. #define DC_REF_DAT_DC_DAT(X) ((X)<<DC_REF_DAT_DC_DAT_SEL_SHIFT)
  298. /***************************************************************************************************
  299. * DAC_OSCTL
  300. */
  301. #define DAC_OSCTL_DAOS_L_SEL_SHIFT (0)
  302. #define DAC_OSCTL_DAOS_L_SEL_MASK (0xFF<<DAC_OSCTL_DAOS_L_SEL_SHIFT)
  303. #define DAC_OSCTL_DAOS_L(X) ((X)<<DAC_OSCTL_DAOS_L_SEL_SHIFT)
  304. /***************************************************************************************************
  305. * DAC_DEBUG
  306. */
  307. #define DEBUGSEL (0x40068410)
  308. #define DEBUGIE0 (0x40068420)
  309. #define DEBUGOE0 (0x40068430)
  310. #define DEBUGOE1 (0x40068434)
  311. #define DEBUGSEL_DBGSE_SHIFT (0)
  312. #define DEBUGSEL_DBGSE_MASK (0x7F << DEBUGSEL_DBGSE_SHIFT)
  313. #define DEBUGSEL_DBGSE(x) ((x) << DEBUGSEL_DBGSE_SHIFT)
  314. #define DBGSE_DAC (0xc)
  315. /***************************************************************************************************
  316. * ADC_REF_LDO_CTL
  317. */
  318. #define ADC_REF_LDO_CTL_BASE (0x4005c130)
  319. #define ADC_REF_LDO_CTL_AULDO_PD_CTL_SHIFT (22) /* AULDO pull down current control. 0: small; 3: large */
  320. #define ADC_REF_LDO_CTL_AULDO_PD_CTL_MASK (0x3 << ADC_REF_LDO_CTL_AULDO_PD_CTL_SHIFT)
  321. #define ADC_REF_LDO_CTL_AULDO_PD_CTL(x) ((x) << ADC_REF_LDO_CTL_AULDO_PD_CTL_SHIFT)
  322. #define ADC_REF_LDO_CTL_AULDO_EN_SHIFT (18) /* AULDO enable for ADC */
  323. #define ADC_REF_LDO_CTL_AULDO_EN_MASK (0x3 << ADC_REF_LDO_CTL_AULDO_EN_SHIFT)
  324. #define ADC_REF_LDO_CTL_AULDO_EN(x) ((x) << ADC_REF_LDO_CTL_AULDO_EN_SHIFT)
  325. #define ADC_REF_LDO_CTL_DALDO_EN_SHIFT (10) /* DALDO enable for DAC */
  326. #define ADC_REF_LDO_CTL_DALDO_EN_MASK (0x3 << ADC_REF_LDO_CTL_DALDO_EN_SHIFT)
  327. #define ADC_REF_LDO_CTL_DALDO_EN(x) ((x) << ADC_REF_LDO_CTL_DALDO_EN_SHIFT)
  328. #define ADC_REF_LDO_CTL_VREF_RSEL_SHIFT (2) /* VREF voltage divide res control */
  329. #define ADC_REF_LDO_CTL_VREF_RSEL_MASK (0x3 << ADC_REF_LDO_CTL_VREF_RSEL_SHIFT)
  330. #define ADC_REF_LDO_CTL_VREF_RSEL(x) ((x) << ADC_REF_LDO_CTL_VREF_RSEL_SHIFT)
  331. #define ADC_REF_LDO_CTL_VREF_FU BIT(1) /* VREF fastup control */
  332. #define ADC_REF_LDO_CTL_VREF_EN BIT(0) /* VREF enable control */
  333. /***************************************************************************************************
  334. * ADC_DIGCTL
  335. */
  336. #define ADC_DIGCTL_BASE (0x4005c100)
  337. #define ADC_DIGCTL_ADC_DIG_SHIFT (12)
  338. #define ADC_DIGCTL_ADC_DIG_MASK ((0xF) << ADC_DIGCTL_ADC_DIG_SHIFT)
  339. /***************************************************************************************************
  340. * DAC FEATURES CONGIURATION
  341. */
  342. #define DAC_FIFO_MAX_DRQ_LEVEL (0xE)//reg里没有写对应值,是否为这个?
  343. #define DAC_FIFO_DRQ_LEVEL_DEFAULT (0x8) /* 16 level */
  344. #define DAC_FIFO_MAX_VOL_LEVEL (0xF)
  345. #define DAC_FIFO_VOL_LEVEL_DEFAULT (0x3) /* 0db */
  346. /* DAC volume soft step to_cnt default setting(0 : 8x; 1 : 128x). */
  347. #define DAC_VOL_TO_CNT_DEFAULT (0)
  348. /* The minimal volume value to mute automatically */
  349. #define VOL_MUTE_MIN_DB (-800000)
  350. #define VOL_DB_TO_INDEX(x) (((x) + 374) / 375)
  351. #define VOL_INDEX_TO_DB(x) ((x) * 375)
  352. #define VOL_LEVEL_0DB (0xBE)
  353. #define DAC_FIFO_INVALID_INDEX(x) (((x) != AOUT_FIFO_DAC0) && ((x) != AOUT_FIFO_DAC1))
  354. #define DAC_FIFO_MAX_LEVEL (16)
  355. #define DAC_WAIT_FIFO_EMPTY_TIMEOUT_MS (130) /* PCMBUF 2k samples spends 62.5ms in 16Kfs */
  356. #define DAC_PCMBUF_MAX_CNT (0x400)
  357. #define DAC_PCMBUF_DEFAULT_IRQ (PCM_BUF_CTL_PCMBHEIE)
  358. #define DAC_CHANNEL_NUM_MAX (4)
  359. #define DAC_FIFO_CNT_MAX_SAME_SAMPLES_TIME_US (100000)
  360. #define DAC_FIFO_CNT_CLEAR_PENDING_TIME_US (200)
  361. //#define DAC_ANALOG_DEBUG_IN_ENABLE
  362. #ifdef CONFIG_SOC_SERIES_LEOPARD_FPGA
  363. #define DAC_DIGITAL_DEBUG_OUT_ENABLE
  364. #endif
  365. #define DAC_DIGITAL_DEBUG_OUT_CHANNEL_SEL (2) /* 1: debug left channel; others: debug right channel */
  366. #define DAC_LDO_CAPACITOR_CHARGE_TIME_MS (10) /* Wait time for AOUT L/R capacitor charge full */
  367. #define DAC_HIGH_PERFORMANCE_WAIT_SH_TIME_MS (3) /* Wait time for SH establish stable state */
  368. #define ANC_MIX_TO_DAC_WAIT_STABLE_TIME_MS (3) /* Wait time for DAC interpolation and OSR stable for ANC */
  369. #define FIR_MODE_A 0x0
  370. #define FIR_MODE_B 0x1
  371. #define FIR_MODE_C 0x2
  372. #define FIR_MODE_R 0x3
  373. /*
  374. * @struct acts_audio_dac
  375. * @brief DAC controller hardware register
  376. */
  377. struct acts_audio_dac {
  378. volatile uint32_t digctl; /* DAC digital and control */
  379. volatile uint32_t fifoctl; /* DAC FIFO control */
  380. volatile uint32_t stat; /* DAC state */
  381. volatile uint32_t fifo0_dat; /* DAC FIFO0 data */
  382. volatile uint32_t pcm_buf_ctl; /* PCM buffer control */
  383. volatile uint32_t pcm_buf_stat; /* PCM buffer state */
  384. volatile uint32_t pcm_buf_thres_he; /* PCM buffer half-empty threshold */
  385. volatile uint32_t pcm_buf_thres_hf; /* PCM buffer half-full threshold */
  386. volatile uint32_t sdm_reset_ctl; /* SDM reset control */
  387. volatile uint32_t auto_mute_ctl; /* Auto mute control */
  388. volatile uint32_t vol_lch; /* volume left channel control */
  389. volatile uint32_t vol_rch; /* volume right channel control */
  390. volatile uint32_t pcm_buf_cnt; /* PCM buffer counter */
  391. volatile uint32_t anactl0; /* DAC analog control register 0 */
  392. volatile uint32_t anactl1; /* DAC analog control register 1 */
  393. volatile uint32_t anactl2; /* DAC analog control register 2 */
  394. volatile uint32_t bias; /* DAC bias control */
  395. volatile uint32_t sdm_samples_cnt; /* SDM samples counter */
  396. volatile uint32_t sdm_samples_num; /* SDM sample number */
  397. volatile uint32_t hw_trigger_dac_ctl; /* HW IRQ trigger DAC control */
  398. volatile uint32_t dc_ref_dat;/*dac reference data*/
  399. volatile uint32_t dac_osctl;/*dac offset register*/
  400. };
  401. struct phy_dac_channel {
  402. uint32_t fifo_cnt; /* DAC FIFO hardware counter max value is 0xFFFF */
  403. uint32_t fifo_cnt_timestamp; /* Record the timestamp of DAC FIFO counter overflow irq */
  404. int (*callback)(void *cb_data, u32_t reason); /* PCM Buffer IRQs callback */
  405. void *cb_data; /* callback user data */
  406. };
  407. #ifdef CONFIG_CFG_DRV
  408. /**
  409. * struct phy_dac_external_config
  410. * @brief The DAC external configuration which generated by configuration tool
  411. */
  412. struct phy_dac_external_config {
  413. cfg_uint8 Out_Mode; /* CFG_TYPE_AUDIO_OUT_MODE */
  414. cfg_uint32 DAC_Bias_Setting; /* DAC bias setting */
  415. cfg_uint8 Keep_DA_Enabled_When_Play_Pause; /* always enable DAC analog */
  416. CFG_Type_Extern_PA_Control Extern_PA_Control[2]; /* GPIO pins to control external PA */
  417. cfg_uint8 AntiPOP_Process_Disable; /* forbidden antipop process */
  418. cfg_uint8 Enable_large_current_protect; /* enable large current protect */
  419. cfg_uint8 Pa_Vol; /* PA gain selection */
  420. };
  421. #endif
  422. /**
  423. * struct phy_dac_drv_data
  424. * @brief The software related data that used by physical dac driver.
  425. */
  426. struct phy_dac_drv_data {
  427. struct phy_dac_channel ch[DAC_CHANNEL_NUM_MAX]; /* dac channels infomation */
  428. uint32_t sdm_cnt; /* SDM samples counter */
  429. uint32_t sdm_cnt_timestamp; /* Record the timestamp of SDM counter by overflow irq */
  430. uint8_t sample_rate; /* The sample rate setting refer to enum audio_sr_sel_e */
  431. uint8_t lr_sel; /* left and right channel selection to enable, refer to enum a_lr_chl_e */
  432. uint8_t layout; /* DAC hardware layout */
  433. #ifdef CONFIG_CFG_DRV
  434. struct phy_dac_external_config external_config; /* DAC external configuration */
  435. #endif
  436. atomic_t refcount; /* DAC resources reference counter */
  437. uint8_t ch_fifo0_start : 1; /* The fifo0 channel start indicator */
  438. uint8_t ch_fifo1_start : 1; /* The fifo1 channel start indicator */
  439. uint8_t vol_set_mute : 1; /* The flag of the volume setting less than #VOL_MUTE_MIN_DB event*/
  440. uint8_t is_anc_enable : 1; /* If 1 to indicate that ANC has enabled */
  441. uint8_t audio_pll_index : 1; /* The index of audio pll */
  442. void (*dsp_audio_set_param)(uint8_t id, uint32_t param1, uint32_t param2);
  443. };
  444. /**
  445. * union phy_dac_features
  446. * @brief The infomation from DTS to control the DAC features to enable or nor.
  447. */
  448. typedef union {
  449. uint32_t raw;
  450. struct {
  451. uint32_t layout : 2; /* DAC working layout(0: single-end non-direct; 1: single-end direct(VRO); 2 differencial) */
  452. uint32_t dac_lr_mix : 1; /* DAC left and right channels MIX */
  453. uint32_t noise_detect_mute : 1; /* noise detect mute */
  454. uint32_t automute : 1; /* auto-mute */
  455. uint32_t loopback : 1; /* ADC => DAC loopback */
  456. uint32_t left_mute : 1; /* DAC left mute */
  457. uint32_t right_mute : 1; /* DAC left mute */
  458. uint32_t pa_vol : 3; /* DAC PA gain config */
  459. uint32_t am_irq : 1; /* if 1 to enable auto mute irq */
  460. } v;
  461. } phy_dac_features;
  462. /**
  463. * struct phy_dac_config_data
  464. * @brief The hardware related data that used by physical dac driver.
  465. */
  466. struct phy_dac_config_data {
  467. uint32_t reg_base; /* DAC controller register base address */
  468. struct audio_dma_dt dma_fifo0; /* DMA resource for FIFO0 */
  469. struct audio_dma_dt dma_fifo1; /* DMA resource for FIFO1 */
  470. uint8_t clk_id; /* DAC devclk id */
  471. uint8_t rst_id; /* DAC reset id */
  472. void (*irq_config)(void); /* IRQ configuration function */
  473. phy_dac_features features; /* DAC features */
  474. };
  475. /*
  476. * enum a_dac_fifo_e
  477. * @brief DAC fifo index selection
  478. */
  479. typedef enum {
  480. DAC_FIFO_0 = 0,
  481. DAC_FIFO_1
  482. } a_dac_fifo_e;
  483. /*
  484. * enum a_dac_sr_e
  485. * @brief DAC over sample rate
  486. */
  487. typedef enum {
  488. DAC_SR_8k = 0,
  489. DAC_SR_11k,//11.25k
  490. DAC_SR_12K,
  491. DAC_SR_16K,
  492. DAC_SR_22k,//22.05k
  493. DAC_SR_24K,
  494. DAC_SR_32K,
  495. DAC_SR_44k, //44.1k
  496. DAC_SR_48k,
  497. DAC_SR_88k, //88.2k
  498. DAC_SR_96k,
  499. } a_dac_sr_e;
  500. /*
  501. * enum a_layout_e
  502. * @brief The DAC working layout
  503. */
  504. typedef enum {
  505. SINGLE_END_MODE = 0,
  506. SINGLE_END_VOR_MODE,
  507. DIFFERENTIAL_MODE
  508. } a_layout_e;
  509. /* @brief get the base address of DAC register */
  510. static inline struct acts_audio_dac *get_dac_reg_base(struct device *dev)
  511. {
  512. const struct phy_dac_config_data *cfg = dev->config;
  513. return (struct acts_audio_dac *)cfg->reg_base;
  514. }
  515. /* @brief dump dac controller register */
  516. static void dac_dump_register(struct device *dev)
  517. {
  518. struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  519. LOG_INF("** dac contoller regster **");
  520. LOG_INF(" BASE: %08x", (uint32_t)dac_reg);
  521. LOG_INF(" DAC_DIGCTL: %08x", dac_reg->digctl);
  522. LOG_INF(" DAC_FIFOCTL: %08x", dac_reg->fifoctl);
  523. LOG_INF(" DAC_STAT: %08x", dac_reg->stat);
  524. LOG_INF(" FIFO0_DAT: %08x", dac_reg->fifo0_dat);
  525. LOG_INF(" PCM_BUF_CTL: %08x", dac_reg->pcm_buf_ctl);
  526. LOG_INF(" PCM_BUF_STAT: %08x", dac_reg->pcm_buf_stat);
  527. LOG_INF(" PCM_BUF_THRES_HE: %08x", dac_reg->pcm_buf_thres_he);
  528. LOG_INF(" PCM_BUF_THRES_HF: %08x", dac_reg->pcm_buf_thres_hf);
  529. LOG_INF(" SDM_RESET_CTL: %08x", dac_reg->sdm_reset_ctl);
  530. LOG_INF(" AUTO_MUTE_CTL: %08x", dac_reg->auto_mute_ctl);
  531. LOG_INF(" VOL_LCH: %08x", dac_reg->vol_lch);
  532. LOG_INF(" VOL_RCH: %08x", dac_reg->vol_rch);
  533. LOG_INF(" PCM_BUF_CNT: %08x", dac_reg->pcm_buf_cnt);
  534. LOG_INF(" DAC_ANALOG0: %08x", dac_reg->anactl0);
  535. LOG_INF(" DAC_ANALOG1: %08x", dac_reg->anactl1);
  536. LOG_INF(" DAC_ANALOG2: %08x", dac_reg->anactl2);
  537. LOG_INF(" DAC_BIAS: %08x", dac_reg->bias);
  538. LOG_INF(" SDM_SAMPLES_CNT: %08x", dac_reg->sdm_samples_cnt);
  539. LOG_INF(" SDM_SAMPLES_NUM: %08x", dac_reg->sdm_samples_num);
  540. LOG_INF(" HW_TRIGGER_CTL: %08x", dac_reg->hw_trigger_dac_ctl);
  541. LOG_INF(" AUDIOPLL0_CTL: %08x", sys_read32(AUDIO_PLL0_CTL));
  542. LOG_INF(" CMU_DEVCLKEN1: %08x", sys_read32(CMU_DEVCLKEN1));
  543. LOG_INF(" CMU_DACCLK: %08x", sys_read32(CMU_DACCLK));
  544. LOG_INF(" DMA7_CTL: %08x", sys_read32(0x4001c800));
  545. LOG_INF(" DMA7_START: %08x", sys_read32(0x4001c804));
  546. LOG_INF(" DMA7_sadd0: %08x", sys_read32(0x4001c808));
  547. LOG_INF(" DMA7_sadd1: %08x", sys_read32(0x4001c80c));
  548. LOG_INF(" DMA7_dadd0: %08x", sys_read32(0x4001c810));
  549. LOG_INF(" DMA7_dadd1: %08x", sys_read32(0x4001c814));
  550. LOG_INF(" DMA7_bc: %08x", sys_read32(0x4001c818));
  551. LOG_INF(" DMA7_rc: %08x", sys_read32(0x4001c81c));
  552. LOG_INF(" DEBUGSEL: %08x", sys_read32(DEBUGSEL));
  553. LOG_INF(" DEBUGOE0: %08x", sys_read32(DEBUGOE0));
  554. LOG_INF(" DEBUGOE1: %08x", sys_read32(DEBUGOE1));
  555. }
  556. /* @brief disable DAC FIFO by specified FIFO index */
  557. static void __dac_fifo_disable(struct device *dev, a_dac_fifo_e idx)
  558. {
  559. struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  560. /**
  561. * CMU_DACCLK_DACFIFO0CLKEN/CMU_DACCLK_DACFIFO1CLKEN is a clock gate for DAC FIFO read/write for power consumption.
  562. * When to access DAC_VOL or PA_VOL, shall enable those bits.
  563. */
  564. if (DAC_FIFO_0 == idx) {
  565. dac_reg->fifoctl &= ~(DAC_FIFOCTL_DAF0RT | DAC_FIFOCTL_DAF0EDE);
  566. /* disable DAC FIFO0 to access clock */
  567. sys_write32(sys_read32(CMU_DACCLK) & ~CMU_DACCLK_DACFIFO0CLKEN, CMU_DACCLK);
  568. } else
  569. LOG_ERR("invalid fifo sel");
  570. }
  571. /* @brief enable DAC FIFO0/FIFO1 */
  572. static int __dac_fifo_enable(struct device *dev, audio_fifouse_sel_e sel,
  573. audio_dma_width_e wd, uint8_t drq_level,
  574. uint8_t fifo_vol, a_dac_fifo_e idx, bool fifo1_mix_en)
  575. {
  576. struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  577. uint32_t reg = dac_reg->fifoctl;
  578. if (drq_level > DAC_FIFO_MAX_DRQ_LEVEL)
  579. drq_level = DAC_FIFO_MAX_DRQ_LEVEL;
  580. if (fifo_vol > DAC_FIFO_MAX_VOL_LEVEL)
  581. fifo_vol = DAC_FIFO_MAX_VOL_LEVEL;
  582. if (FIFO_SEL_ASRC == sel) {
  583. LOG_ERR("invalid fifo sel %d", sel);
  584. return -EINVAL;
  585. }
  586. if (DAC_FIFO_0 == idx) {
  587. reg &= ~0xFFFF; /* clear all FIFO0 fields */
  588. if (FIFO_SEL_CPU == sel)
  589. reg |= DAC_FIFOCTL_DAF0EIE; /* enable irq */
  590. else if (FIFO_SEL_DMA == sel)
  591. reg |= DAC_FIFOCTL_DAF0EDE; /* enable drq */
  592. reg |= DAC_FIFOCTL_DAF0IS(sel);
  593. if (DMA_WIDTH_16BITS == wd)
  594. reg |= DAC_FIFOCTL_DACFIFO0_DMAWIDTH;
  595. reg |= DAC_FIFOCTL_DRQ0_LEVEL(drq_level);
  596. // reg |= DAC_FIFOCTL_FIFO0_VOL(fifo_vol);
  597. reg |= DAC_FIFOCTL_DAF0RT;
  598. dac_reg->fifoctl = reg;
  599. /* DAC FIFO0 MIX to DAC enable */
  600. dac_reg->digctl |= DAC_DIGCTL_DAF0M2DAEN;
  601. sys_write32(sys_read32(CMU_DACCLK) | CMU_DACCLK_DACFIFO0CLKEN, CMU_DACCLK);
  602. } else
  603. LOG_ERR("invalid fifo sel %d", sel);
  604. return 0;
  605. }
  606. static bool __dac_fifosrc_is_dsp(struct device *dev)
  607. {
  608. struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  609. uint32_t reg = dac_reg->fifoctl;
  610. if ((reg & DAC_FIFOCTL_DAF0IS_MASK) == (FIFO_SEL_DSP << DAC_FIFOCTL_DAF0IS_SHIFT)) {
  611. return true;
  612. } else {
  613. return false;
  614. }
  615. }
  616. /* @brief update DAC FIFO0/FIFO1 src */
  617. static int __dac_fifo_update_src(struct device *dev, dac_fifosrc_setting_t *fifosrc)
  618. {
  619. struct phy_dac_drv_data *data = dev->data;
  620. struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  621. uint32_t reg = dac_reg->fifoctl;
  622. audio_fifouse_sel_e sel = (fifosrc->fifo_from_dsp)? (FIFO_SEL_DSP): (FIFO_SEL_DMA);
  623. a_dac_fifo_e idx = fifosrc->fifo_idx;
  624. LOG_INF("sel:%d, idx:%d\n", sel, idx);
  625. data->dsp_audio_set_param = fifosrc->dsp_audio_set_param;
  626. if (DAC_FIFO_0 == idx) {
  627. reg &= ~DAC_FIFOCTL_DAF0IS_MASK;
  628. reg |= DAC_FIFOCTL_DAF0IS(sel);
  629. if (sel == FIFO_SEL_DSP) {
  630. reg &= ~DAC_FIFOCTL_DAF0EIE; /* disable irq */
  631. reg &= ~DAC_FIFOCTL_DAF0EDE; /* disable drq */
  632. reg &= ~DAC_FIFOCTL_DAF0_DSPDMA_EN; /* disable DSP_DMA drq*/
  633. } else if (FIFO_SEL_CPU == sel){
  634. reg |= DAC_FIFOCTL_DAF0EIE; /* enable irq */
  635. } else if (FIFO_SEL_DMA == sel){
  636. reg |= DAC_FIFOCTL_DAF0EDE; /* enable drq */
  637. }else if (FIFO_SEL_DSP_DMA == sel){
  638. reg &= ~DAC_FIFOCTL_DAF0EIE; /* disable irq */
  639. reg &= ~DAC_FIFOCTL_DAF0EDE; /* disable drq */
  640. }
  641. } else if (DAC_FIFO_1 == idx) {
  642. LOG_ERR("%s dac_fifo err :leopard no fifo_1\n",__func__);
  643. }
  644. dac_reg->fifoctl = reg;
  645. if (sel == FIFO_SEL_DSP) {
  646. irq_disable(IRQ_ID_DACFIFO);
  647. irq_disable(IRQ_ID_DAC);
  648. } else {
  649. irq_enable(IRQ_ID_DACFIFO);
  650. irq_enable(IRQ_ID_DAC);
  651. }
  652. return 0;
  653. }
  654. /*
  655. * @brief Check and wait the DAC FIFO is empty or not
  656. */
  657. static void __wait_dac_fifo_empty(struct device *dev, a_dac_fifo_e idx)
  658. {
  659. struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  660. if (DAC_FIFO_0 == idx) {
  661. while (((dac_reg->stat & DAC_STAT_DAF0S_MASK) >> DAC_STAT_DAF0S_SHIFT)
  662. != DAC_FIFO_MAX_LEVEL) {
  663. ;
  664. }
  665. } else if (DAC_FIFO_1 == idx) {
  666. LOG_ERR("%s dac_fifo err :leopard no fifo_1\n",__func__);
  667. }
  668. }
  669. /* @brief check if the specified FIFO is working or not */
  670. static bool __is_dac_fifo_working(struct device *dev, a_dac_fifo_e idx)
  671. {
  672. struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  673. if (DAC_FIFO_0 == idx) {
  674. if (dac_reg->fifoctl & DAC_FIFOCTL_DAF0RT)
  675. return true;
  676. } else if (DAC_FIFO_1 == idx) {
  677. LOG_ERR("%s dac_fifo err :leopard no fifo_1\n",__func__);
  678. return false;
  679. }
  680. return false;
  681. }
  682. /* @brief get the available samples to fill into PCM buffer */
  683. static uint32_t __get_pcmbuf_avail_length(struct device *dev)
  684. {
  685. struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  686. uint32_t key, avail;
  687. key = irq_lock();
  688. avail = (dac_reg->pcm_buf_stat & PCM_BUF_STAT_PCMBS_MASK) >> PCM_BUF_STAT_PCMBS_SHIFT;
  689. irq_unlock(key);
  690. LOG_DBG("PCMBUF free space 0x%x samples", avail);
  691. return avail;
  692. }
  693. /* @brief check if dac fifo empty */
  694. static bool __is_dac_fifo_empty(struct device *dev, a_dac_fifo_e idx)
  695. {
  696. // struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  697. // uint32_t fifo_status = 0;
  698. if (DAC_FIFO_0 == idx) {
  699. /* DAC FIFO0 connects with PCMBUF */
  700. if (DAC_PCMBUF_MAX_CNT == __get_pcmbuf_avail_length(dev))
  701. return true;
  702. } else if (DAC_FIFO_1 == idx) {
  703. LOG_ERR("%s dac_fifo err :leopard no fifo_1\n",__func__);
  704. return false;
  705. }
  706. return false;
  707. }
  708. /* @brief check if all DAC FIFO resources are free */
  709. static bool __is_dac_fifo_all_free(struct device *dev, bool check_mix)
  710. {
  711. struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  712. if (check_mix) {
  713. if (dac_reg->digctl & DAC_DIGCTL_DAF0M2DAEN)
  714. return false;
  715. } else {
  716. if (__is_dac_fifo_working(dev, DAC_FIFO_0))
  717. return false;
  718. }
  719. return true;
  720. }
  721. /* @brief check if there is error happened in given fifo index */
  722. static bool __check_dac_fifo_error(struct device *dev, a_dac_fifo_e idx)
  723. {
  724. // struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  725. /* only in case of SPDIF use DAC_FIFO1 */
  726. if (DAC_FIFO_1 == idx) {
  727. LOG_ERR("%s dac_fifo err :leopard no fifo_1\n",__func__);
  728. return false;
  729. }
  730. return false;
  731. }
  732. /* @brief clear fifo error status */
  733. static void __dac_clear_fifo_error(struct device *dev, a_dac_fifo_e idx)
  734. {
  735. // struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  736. if (DAC_FIFO_1 == idx) {
  737. LOG_ERR("%s dac_fifo err :leopard no fifo_1\n",__func__);
  738. }
  739. }
  740. /* @brief enable the FIFO sample counter function and by default to enable overflow irq */
  741. static void __dac_fifo_counter_enable(struct device *dev, a_dac_fifo_e idx)
  742. {
  743. struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  744. if (DAC_FIFO_0 == idx) {
  745. dac_reg->pcm_buf_cnt |= PCM_BUF_CNT_EN;
  746. /* By default to enbale pcm buf counter overflow IRQ */
  747. dac_reg->pcm_buf_cnt |= PCM_BUF_CNT_IE;
  748. /* clear sample counter irq pending */
  749. dac_reg->pcm_buf_cnt |= PCM_BUF_CNT_IP;
  750. } else if (DAC_FIFO_1 == idx) {
  751. LOG_ERR("%s dac_fifo err :leopard no fifo_1\n",__func__);
  752. }
  753. }
  754. /* @brief disable the FIFO sample counter function */
  755. static void __dac_fifo_counter_disable(struct device *dev, a_dac_fifo_e idx)
  756. {
  757. struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  758. if (DAC_FIFO_0 == idx) {
  759. dac_reg->pcm_buf_cnt &= ~(PCM_BUF_CNT_EN | PCM_BUF_CNT_IE);
  760. } else if (DAC_FIFO_1 == idx) {
  761. LOG_ERR("%s dac_fifo err :leopard no fifo_1\n",__func__);
  762. }
  763. }
  764. /* @brief reset the FIFO sample counter function */
  765. static void __dac_fifo_counter_reset(struct device *dev, a_dac_fifo_e idx)
  766. {
  767. struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  768. if (DAC_FIFO_0 == idx) {
  769. dac_reg->pcm_buf_cnt &= ~(PCM_BUF_CNT_EN | PCM_BUF_CNT_IE);
  770. dac_reg->pcm_buf_cnt |= PCM_BUF_CNT_EN;
  771. dac_reg->pcm_buf_cnt |= PCM_BUF_CNT_IE;
  772. } else if (DAC_FIFO_1 == idx) {
  773. LOG_ERR("%s dac_fifo err :leopard no fifo_1\n",__func__);
  774. }
  775. }
  776. /* @brief enable the DAC SDM sample counter function */
  777. static void __dac_sdm_counter_enable(struct device *dev)
  778. {
  779. struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  780. dac_reg->sdm_samples_cnt |= SDM_SAMPLES_CNT_EN;
  781. dac_reg->sdm_samples_cnt |= SDM_SAMPLES_CNT_IE;
  782. dac_reg->sdm_samples_cnt |= SDM_SAMPLES_CNT_IP;
  783. }
  784. /* @brief disable DAC SDM sample counter function */
  785. static void __dac_sdm_counter_disable(struct device *dev)
  786. {
  787. struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  788. dac_reg->sdm_samples_cnt &= ~(SDM_SAMPLES_CNT_EN | SDM_SAMPLES_CNT_IE);
  789. }
  790. /* @brief reset the DAC SDM sample counter function */
  791. static void __dac_sdm_counter_reset(struct device *dev)
  792. {
  793. struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  794. dac_reg->sdm_samples_cnt &= ~(SDM_SAMPLES_CNT_EN | SDM_SAMPLES_CNT_IE);
  795. dac_reg->sdm_samples_cnt |= SDM_SAMPLES_CNT_EN;
  796. dac_reg->sdm_samples_cnt |= SDM_SAMPLES_CNT_IE;
  797. }
  798. /* @brief read the DAC SDM sample counter */
  799. static uint32_t __dac_read_sdm_counter(struct device *dev)
  800. {
  801. struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  802. uint32_t val = 0;
  803. val = dac_reg->sdm_samples_cnt & SDM_SAMPLES_CNT_MASK;
  804. return val;
  805. }
  806. /* @brief read the DAC SDM sample stable counter */
  807. static uint32_t __dac_read_sdm_stable_counter(struct device *dev)
  808. {
  809. struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  810. uint32_t val = 0;
  811. val = dac_reg->sdm_samples_num & SDM_SAMPLES_NUM_CNT_MASK;
  812. return val;
  813. }
  814. /* @brief set the DAC FIFO DRQ level */
  815. static int __dac_fifo_drq_level_set(struct device *dev, a_dac_fifo_e idx, uint8_t level)
  816. {
  817. struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  818. uint32_t reg = dac_reg->fifoctl;
  819. if (level > DAC_FIFO_MAX_DRQ_LEVEL)
  820. return -EINVAL;
  821. if (DAC_FIFO_0 == idx) {
  822. reg &= ~DAC_FIFOCTL_DRQ0_LEVEL_MASK;
  823. reg |= DAC_FIFOCTL_DRQ0_LEVEL(level);
  824. } else if (DAC_FIFO_1 == idx) {
  825. LOG_ERR("%s dac_fifo err :leopard no fifo_1\n",__func__);
  826. } else {
  827. return -EINVAL;
  828. }
  829. dac_reg->fifoctl = reg;
  830. return 0;
  831. }
  832. /* @brief get the DAC FIFO DRQ level */
  833. static int __dac_fifo_drq_level_get(struct device *dev, a_dac_fifo_e idx)
  834. {
  835. struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  836. uint32_t reg = dac_reg->fifoctl;
  837. int level = 0;
  838. if (DAC_FIFO_0 == idx) {
  839. level = (reg & DAC_FIFOCTL_DRQ0_LEVEL_MASK) >> DAC_FIFOCTL_DRQ0_LEVEL_SHIFT;
  840. } else if (DAC_FIFO_1 == idx) {
  841. LOG_ERR("%s dac_fifo err :leopard no fifo_1\n",__func__);
  842. } else {
  843. level = -EINVAL;
  844. }
  845. return level;
  846. }
  847. /* @brief read the FIFO sample counter by specified FIFO index */
  848. static uint32_t __dac_read_fifo_counter(struct device *dev, a_dac_fifo_e idx)
  849. {
  850. struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  851. uint32_t val = 0;
  852. if (DAC_FIFO_0 == idx) {
  853. val = dac_reg->pcm_buf_cnt & PCM_BUF_CNT_CNT_MASK;
  854. } else if (DAC_FIFO_1 == idx) {
  855. LOG_ERR("%s dac_fifo err :leopard no fifo_1\n",__func__);
  856. }
  857. return val;
  858. }
  859. /* @brief PCM BUF configuration */
  860. static int __dac_pcmbuf_config(struct device *dev)
  861. {
  862. struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  863. uint32_t reg = dac_reg->pcm_buf_ctl;
  864. #if (DT_INST_PROP(0, pcmbuf_he_thres) >= DAC_PCMBUF_MAX_CNT)
  865. #error "Error on PCMBUF HE threshold setting"
  866. #endif
  867. #if (DT_INST_PROP(0, pcmbuf_hf_thres) >= DAC_PCMBUF_MAX_CNT)
  868. #error "Error on PCMBUF HF threshold setting"
  869. #endif
  870. reg &= ~PCM_BUF_CTL_IRQ_MASK;
  871. /* By default to enable PCMBUF half empty IRQs */
  872. reg |= DAC_PCMBUF_DEFAULT_IRQ;
  873. dac_reg->pcm_buf_ctl = reg;
  874. dac_reg->pcm_buf_thres_he = CONFIG_AUDIO_DAC_0_PCMBUF_HE_THRES;
  875. dac_reg->pcm_buf_thres_hf = CONFIG_AUDIO_DAC_0_PCMBUF_HF_THRES;
  876. /* Clean all pcm buf irqs pending */
  877. dac_reg->pcm_buf_stat |= PCM_BUF_STAT_IRQ_MASK;
  878. LOG_DBG("ctl:0x%x, thres_he:0x%x thres_hf:0x%x",
  879. dac_reg->pcm_buf_ctl, dac_reg->pcm_buf_thres_he,
  880. dac_reg->pcm_buf_thres_hf);
  881. return 0;
  882. }
  883. static int __dac_pcmbuf_threshold_update(struct device *dev, dac_threshold_setting_t *thres)
  884. {
  885. struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  886. if (!thres)
  887. return -EINVAL;
  888. if ((thres->hf_thres >= DAC_PCMBUF_MAX_CNT)
  889. || (thres->he_thres > thres->hf_thres)) {
  890. LOG_ERR("Invalid threshold hf:%d he:%d",
  891. thres->hf_thres, thres->he_thres);
  892. return -ENOEXEC;
  893. }
  894. dac_reg->pcm_buf_thres_he = thres->he_thres;
  895. dac_reg->pcm_buf_thres_hf = thres->hf_thres;
  896. LOG_INF("new dac threshold => he:0x%x hf:0x%x",
  897. dac_reg->pcm_buf_thres_he, dac_reg->pcm_buf_thres_hf);
  898. return 0;
  899. }
  900. /* @brief set the external trigger source for DAC digital start */
  901. static int __dac_external_trigger_enable(struct device *dev, uint8_t trigger_src)
  902. {
  903. struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  904. if (trigger_src > 7) {
  905. LOG_ERR("Invalid DAC trigger source %d", trigger_src);
  906. return -EINVAL;
  907. }
  908. dac_reg->hw_trigger_dac_ctl &= ~ HW_TRIGGER_DAC_CTL_TRIGGER_SRC_SRL_MASK;
  909. dac_reg->hw_trigger_dac_ctl |= HW_TRIGGER_DAC_CTL_TRIGGER_SRC_SRL(trigger_src);
  910. dac_reg->hw_trigger_dac_ctl |= HW_TRIGGER_DAC_CTL_INT_TO_DAC_EN;
  911. LOG_INF("set DAC external trigger_src:%d", trigger_src);
  912. return 0;
  913. }
  914. /* @breif control the DAC functions that can be triggered by external signals */
  915. static int __dac_external_trigger_control(struct device *dev, dac_ext_trigger_ctl_t *trigger_ctl)
  916. {
  917. struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  918. uint32_t reg = dac_reg->hw_trigger_dac_ctl;
  919. bool valid = false;
  920. if (!trigger_ctl) {
  921. LOG_ERR("Invalid parameter");
  922. return -EINVAL;
  923. }
  924. LOG_INF("extern trigger ctl:0x%x",trigger_ctl->trigger_ctl);
  925. if (trigger_ctl->t.sdm_cnt_trigger_en) {
  926. /* disable SDM CNT until external IRQ to trigger enable */
  927. if (dac_reg->sdm_samples_cnt & SDM_SAMPLES_CNT_EN)
  928. __dac_sdm_counter_disable(dev);
  929. reg |= HW_TRIGGER_DAC_CTL_INT_TO_SDMCNT_EN;
  930. valid = true;
  931. LOG_INF("enable external trigger DAC SDM_CNT enable");
  932. }
  933. if (trigger_ctl->t.sdm_cnt_lock_en) {
  934. reg |= HW_TRIGGER_DAC_CTL_INT_TO_SDM_CNT;
  935. valid = true;
  936. LOG_INF("enable external trigger DAC lock SDM_CNT");
  937. }
  938. if (trigger_ctl->t.dac_fifo_trigger_en) {
  939. /**
  940. * FIXME: HW ISSUE
  941. * DMA always work when DRQ enable regardless of FIFO enable.
  942. */
  943. #if 0
  944. if (dac_reg->fifoctl & DAC_FIFOCTL_DAF0RT) {
  945. dac_reg->fifoctl &= ~(DAC_FIFOCTL_DAF0RT);
  946. }
  947. reg |= HW_TRIGGER_DAC_CTL_INT_TO_DACFIFO_EN;
  948. valid = true;
  949. LOG_INF("enable external trigger DAC FIFO enable");
  950. #endif
  951. }
  952. if (trigger_ctl->t.dac_digital_trigger_en) {
  953. /* disable DAC digital until external IRQ to trigger start */
  954. if (dac_reg->digctl & DAC_DIGCTL_DDEN)
  955. dac_reg->digctl &= ~DAC_DIGCTL_DDEN;
  956. if (dac_reg->sdm_samples_cnt & SDM_SAMPLES_CNT_EN)
  957. __dac_sdm_counter_reset(dev);
  958. reg |= HW_TRIGGER_DAC_CTL_INT_TO_DAC_EN;
  959. valid = true;
  960. LOG_INF("enable external trigger DAC digital start");
  961. }
  962. if (valid)
  963. dac_reg->hw_trigger_dac_ctl = reg;
  964. return 0;
  965. }
  966. /* @brief disable the external irq signal to start DAC digital function */
  967. static void __dac_external_trigger_disable(struct device *dev)
  968. {
  969. struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  970. if (dac_reg->hw_trigger_dac_ctl & HW_TRIGGER_DAC_CTL_INT_TO_SDM_CNT)
  971. dac_reg->hw_trigger_dac_ctl &= ~HW_TRIGGER_DAC_CTL_INT_TO_SDM_CNT;
  972. if (dac_reg->hw_trigger_dac_ctl & HW_TRIGGER_DAC_CTL_INT_TO_DAC_EN)
  973. dac_reg->hw_trigger_dac_ctl &= ~HW_TRIGGER_DAC_CTL_INT_TO_DAC_EN;
  974. }
  975. /* @brief force DAC digital module to start */
  976. static void __dac_digital_force_start(struct device *dev, dac_ext_trigger_ctl_t *trigger_ctl)
  977. {
  978. struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  979. if (trigger_ctl->t.dac_fifo_trigger_en) {
  980. dac_reg->fifoctl |= DAC_FIFOCTL_DAF0RT;
  981. LOG_INF("force enable DAC FIFO");
  982. }
  983. if (trigger_ctl->t.dac_digital_trigger_en) {
  984. dac_reg->digctl |= DAC_DIGCTL_DDEN;
  985. LOG_INF("force start DAC digital");
  986. }
  987. if (trigger_ctl->t.sdm_cnt_trigger_en) {
  988. __dac_sdm_counter_reset(dev);
  989. LOG_INF("force start DAC SDM_CNT");
  990. }
  991. }
  992. /* @brief enable DAC mono mode */
  993. static void __dac_digital_enable_mono(struct device *dev)
  994. {
  995. struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  996. dac_reg->digctl |= DAC_DIGCTL_DACHNUM;
  997. }
  998. /* @brief enable DAC digital function */
  999. static int __dac_digital_enable(struct device *dev, a_dac_sr_e sr,
  1000. audio_ch_mode_e type, uint8_t channel_type)
  1001. {
  1002. struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  1003. // const struct phy_dac_config_data *cfg = dev->config;
  1004. uint32_t reg = dac_reg->digctl;
  1005. /* clear interpolation/OSR/digital_en etc. */
  1006. reg &= ~(DAC_DIGCTL_DDEN | DAC_DIGCTL_ENDITH
  1007. | DAC_DIGCTL_DACHNUM | DAC_DIGCTL_SR_MASK
  1008. | DAC_DIGCTL_FIR_MODE_MASK);
  1009. if ((STEREO_MODE != type) && (MONO_MODE != type))
  1010. return -EINVAL;
  1011. reg |= DAC_DIGCTL_SR(sr);
  1012. /* select fir mode*/
  1013. switch (sr)
  1014. {
  1015. case DAC_SR_11k:
  1016. case DAC_SR_22k:
  1017. case DAC_SR_44k:
  1018. case DAC_SR_88k:
  1019. reg |= DAC_DIGCTL_FIR_MODE(FIR_MODE_B);
  1020. break;
  1021. case DAC_SR_16K:
  1022. reg |= DAC_DIGCTL_FIR_MODE(FIR_MODE_C);
  1023. default:
  1024. reg |= DAC_DIGCTL_FIR_MODE(FIR_MODE_A);
  1025. break;
  1026. }
  1027. /* leopard dac only support mono*/
  1028. reg |= DAC_DIGCTL_DACHNUM;
  1029. /* digital and dith enable */
  1030. reg |= (DAC_DIGCTL_DDEN | DAC_DIGCTL_ENDITH);
  1031. dac_reg->digctl = reg;
  1032. /* disable left/right channel volume soft step function */
  1033. dac_reg->vol_lch &= ~VOL_LCH_SOFT_CFG_DEFAULT;
  1034. dac_reg->vol_rch &= ~VOL_RCH_SOFT_CFG_DEFAULT;
  1035. return 0;
  1036. }
  1037. /* @brief disable digital fifo usage */
  1038. static void __dac_digital_disable_fifo(struct device *dev, a_dac_fifo_e idx)
  1039. {
  1040. struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  1041. if (DAC_FIFO_0 == idx) {
  1042. dac_reg->digctl &= ~DAC_DIGCTL_DAF0M2DAEN; /* disable DAC_FIFO0 MIX to PCMBUF */
  1043. } else if (DAC_FIFO_1 == idx) {
  1044. LOG_ERR("%s dac_fifo err :leopard no fifo_1\n",__func__);
  1045. }
  1046. }
  1047. /* @brief disable DAC digital function */
  1048. static void __dac_digital_disable(struct device *dev)
  1049. {
  1050. struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  1051. const struct phy_dac_config_data *cfg = dev->config;
  1052. /* disable SDM reset function */
  1053. if (PHY_DEV_FEATURE(noise_detect_mute)) {
  1054. if (dac_reg->sdm_reset_ctl & SDM_RESET_CTL_SDMREEN)
  1055. dac_reg->sdm_reset_ctl &= ~SDM_RESET_CTL_SDMREEN;
  1056. }
  1057. /* disable external irq signal to start DAC */
  1058. if (dac_reg->hw_trigger_dac_ctl & HW_TRIGGER_DAC_CTL_INT_TO_DAC_EN) {
  1059. dac_reg->hw_trigger_dac_ctl &= ~(HW_TRIGGER_DAC_CTL_INT_TO_DAC_EN
  1060. | HW_TRIGGER_DAC_CTL_INT_TO_SDM_CNT);
  1061. }
  1062. dac_reg->digctl &= ~(DAC_DIGCTL_ENDITH | DAC_DIGCTL_DDEN);
  1063. }
  1064. /* @brief check if the DAC digital function is working */
  1065. static bool __dac_is_digital_working(struct device *dev)
  1066. {
  1067. struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  1068. if (dac_reg->digctl & DAC_DIGCTL_DDEN)
  1069. return true;
  1070. return false;
  1071. }
  1072. /* @brief DAC L/R channel volume setting */
  1073. static void __dac_volume_set(struct device *dev, uint8_t lr_sel, uint8_t left_v, uint8_t right_v)
  1074. {
  1075. struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  1076. uint32_t reg_l, reg_l_old, reg_r, reg_r_old;
  1077. /* DAC left channel volume setting */
  1078. if (lr_sel & LEFT_CHANNEL_SEL) {
  1079. reg_l_old = reg_l = dac_reg->vol_lch;
  1080. reg_l &= ~VOL_LCH_VOLL_MASK;
  1081. reg_l |= VOL_LCH_VOLL(left_v);
  1082. dac_reg->vol_lch = reg_l;
  1083. LOG_INF("left volume: 0x%x => 0x%x", reg_l_old & 0xFF, left_v);
  1084. }
  1085. if (lr_sel & RIGHT_CHANNEL_SEL) {
  1086. reg_r_old = reg_r = dac_reg->vol_rch;
  1087. reg_r &= ~VOL_RCH_VOLR_MASK;
  1088. reg_r |= VOL_RCH_VOLR(right_v);
  1089. dac_reg->vol_rch = reg_r;
  1090. LOG_INF("right volume: 0x%x => 0x%x", reg_r_old & 0xFF, right_v);
  1091. }
  1092. }
  1093. /* @brief get the current dac L/R volume setting */
  1094. static uint8_t __dac_volume_get(struct device *dev, uint8_t lr_sel)
  1095. {
  1096. struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  1097. uint8_t vol;
  1098. if (lr_sel & LEFT_CHANNEL_SEL)
  1099. vol = dac_reg->vol_lch & VOL_LCH_VOLL_MASK;
  1100. else
  1101. vol = dac_reg->vol_rch & VOL_RCH_VOLR_MASK;
  1102. return vol;
  1103. }
  1104. /* @brief DAC SDM(noise detect mute) configuration */
  1105. static void __dac_sdm_mute_cfg(struct device *dev, uint8_t sr)
  1106. {
  1107. struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  1108. uint32_t reg = dac_reg->sdm_reset_ctl;
  1109. uint16_t sdm_cnt = CONFIG_AUDIO_DAC_0_SDM_CNT;
  1110. uint16_t sdm_thres = CONFIG_AUDIO_DAC_0_SDM_THRES;
  1111. reg &= ~(SDM_RESET_CTL_SDMNDTH_MASK | SDM_RESET_CTL_SDMCNT_MASK);
  1112. reg |= SDM_RESET_CTL_SDMCNT(sdm_cnt);
  1113. reg |= SDM_RESET_CTL_SDMNDTH(sdm_thres);
  1114. /* Reset SDM after has detected noise
  1115. * NOTE: When sample rate are 8k/11k/12k/16k shall enable this bit
  1116. */
  1117. reg |= SDM_RESET_CTL_SDMREEN;
  1118. dac_reg->sdm_reset_ctl = reg;
  1119. LOG_INF("DAC SDM function enable");
  1120. }
  1121. /* @brief DAC automute function configuration */
  1122. static void __dac_automute_cfg(struct device *dev)
  1123. {
  1124. struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  1125. const struct phy_dac_config_data *cfg = dev->config;
  1126. uint32_t reg = dac_reg->auto_mute_ctl;
  1127. uint16_t am_cnt = CONFIG_AUDIO_DAC_0_AM_CNT;
  1128. uint16_t am_thres = CONFIG_AUDIO_DAC_0_AM_THRES;
  1129. reg &= ~(AUTO_MUTE_CTL_AMCNT_MASK | AUTO_MUTE_CTL_AMTH_MASK);
  1130. reg |= AUTO_MUTE_CTL_AMCNT(am_cnt);
  1131. reg |= AUTO_MUTE_CTL_AMTH(am_thres);
  1132. if (PHY_DEV_FEATURE(am_irq))
  1133. reg |= AUTO_MUTE_CTL_AM_IRQ_EN; /* enable auto mute IRQ */
  1134. /* Auto mute enable */
  1135. reg |= AUTO_MUTE_CTL_AMEN;
  1136. dac_reg->auto_mute_ctl = reg;
  1137. LOG_INF("DAC automute function enable");
  1138. }
  1139. /* @brief ADC loopback to DAC function configuration */
  1140. static void __dac_loopback_cfg(struct device *dev, uint8_t lr_sel, bool dac_lr_mix)
  1141. {
  1142. struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  1143. uint32_t reg = dac_reg->digctl;
  1144. /* Clear L/R mix, ADCR loopback to DAC, ADCL loopback to DAC */
  1145. reg &= ~(DAC_DIGCTL_ADC01MIX | DAC_DIGCTL_AD2DALPEN_R | DAC_DIGCTL_AD2DALPEN_L);
  1146. /* ADC0 L to DAC L loopback */
  1147. if (lr_sel & LEFT_CHANNEL_SEL)
  1148. reg |= DAC_DIGCTL_AD2DALPEN_L;
  1149. /* ADC1 R to DAC R loopback */
  1150. if (lr_sel & RIGHT_CHANNEL_SEL)
  1151. reg |= DAC_DIGCTL_AD2DALPEN_R;
  1152. if (dac_lr_mix)
  1153. reg |= DAC_DIGCTL_ADC01MIX;
  1154. else
  1155. reg &= ~DAC_DIGCTL_ADC01MIX;
  1156. dac_reg->digctl = reg;
  1157. LOG_INF("ADDA loopback(lr:%d mix:%d) enable", lr_sel, dac_lr_mix);
  1158. }
  1159. /* @brief dac left volume soft step setting and 'adj_cnt' is the same as 'sample_rate' */
  1160. static void __dac_volume_left_softstep(struct device *dev, uint8_t adj_cnt, bool irq_flag)
  1161. {
  1162. struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  1163. uint32_t reg;
  1164. /* Clear soft step done IRQ pending */
  1165. if (dac_reg->vol_lch & VOL_LCH_DONE_PD)
  1166. dac_reg->vol_lch |= VOL_LCH_DONE_PD;
  1167. reg = dac_reg->vol_lch;
  1168. /* Clear all VOL_LCH exclude VOLL */
  1169. reg &= ~0x3FFF00;
  1170. reg |= VOL_LCH_ADJ_CNT(adj_cnt);
  1171. /* to_cnt setting */
  1172. if (DAC_VOL_TO_CNT_DEFAULT)
  1173. reg |= VOL_LCH_TO_CNT;
  1174. if (irq_flag)
  1175. reg |= VOL_LCH_VOLL_IRQ_EN;
  1176. else
  1177. reg &= ~VOL_LCH_VOLL_IRQ_EN;
  1178. reg |= VOL_LCH_SOFT_CFG_DEFAULT;
  1179. dac_reg->vol_lch = reg;
  1180. }
  1181. /* @brief dac right volume soft step setting and 'adj_cnt' is the same as 'sample_rate' */
  1182. static void __dac_volume_right_softstep(struct device *dev, uint8_t adj_cnt, bool irq_flag)
  1183. {
  1184. struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  1185. uint32_t reg;
  1186. /* Clear soft step done IRQ pending */
  1187. if (dac_reg->vol_rch & VOL_RCH_DONE_PD)
  1188. dac_reg->vol_rch |= VOL_RCH_DONE_PD;
  1189. reg = dac_reg->vol_rch;
  1190. /* Clear all VOL_RCH exclude VOLR */
  1191. reg &= ~0x3FFF00;
  1192. reg |= VOL_RCH_ADJ_CNT(adj_cnt);
  1193. /* to_cnt setting */
  1194. if (DAC_VOL_TO_CNT_DEFAULT)
  1195. reg |= VOL_RCH_TO_CNT;
  1196. if (irq_flag)
  1197. reg |= VOL_RCH_VOLR_IRQ_EN;
  1198. else
  1199. reg &= ~VOL_RCH_VOLR_IRQ_EN;
  1200. reg |= VOL_RCH_SOFT_CFG_DEFAULT;
  1201. dac_reg->vol_rch = reg;
  1202. }
  1203. /* @brief DAC enable mute or disable mute control. */
  1204. static void __dac_mute_control(struct device *dev, bool mute_en)
  1205. {
  1206. struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  1207. if (mute_en)
  1208. dac_reg->anactl0 |= DAC_ANACTL0_ZERODT; /* DAC L/R channel will output zero data */
  1209. else
  1210. dac_reg->anactl0 &= ~DAC_ANACTL0_ZERODT;
  1211. }
  1212. #ifdef CONFIG_CFG_DRV
  1213. /* @brief Disable DAC analog by specified channels */
  1214. static int __dac_analog_disable(struct device *dev, uint8_t lr_sel)
  1215. {
  1216. struct phy_dac_drv_data *data = dev->data;
  1217. struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  1218. dac_reg->anactl0 &= ~DAC_ANACTL0_DAENL;
  1219. return 0;
  1220. }
  1221. #endif
  1222. /* @brief DAC works in differencial layout */
  1223. static int __dac_analog_diff_cfg(struct device *dev)
  1224. {
  1225. const struct phy_dac_config_data *cfg = dev->config;
  1226. struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  1227. uint32_t reg = 0;
  1228. // act_or(CMU_DEVCLKEN1, 1 << CMU_DEVCLKEN1_DACANACLKEN);
  1229. reg |= DAC_ANACTL0_BIASEN;//en all bias
  1230. reg |= DAC_ANACTL0_DAENL; //en dac
  1231. reg |= DAC_ANACTL0_PAEN; //en pa
  1232. reg |= DAC_ANACTL0_PAOSEN; //en pa output stage
  1233. reg |= DAC_ANACTL0_PAVOL(PHY_DEV_FEATURE(pa_vol));//pa swing
  1234. reg |= DAC_ANACTL0_DARSET;
  1235. reg |= DAC_ANACTL0_DFCEN;
  1236. dac_reg->anactl0 = reg;
  1237. dac_reg->anactl1 |= DAC_ANACTL1_ATPSW1;
  1238. return 0;
  1239. }
  1240. #ifdef DAC_ANALOG_DEBUG_IN_ENABLE
  1241. static void __dac_analog_dbgi(struct device *dev)
  1242. {
  1243. struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  1244. uint32_t reg = sys_read32(DEBUGSEL) & ~DEBUGSEL_DBGSE_MASK;
  1245. reg |= DEBUGSEL_DBGSE(DBGSE_DAC);
  1246. sys_write32(reg, DEBUGSEL);
  1247. /* debug GPIO input pin13 ~ pin22 */
  1248. sys_write32(0x7fe000, DEBUGIE0);
  1249. reg = dac_reg->digctl & ~DAC_DIGCTL_DADEN;
  1250. reg |= DAC_DIGCTL_DADEN;
  1251. dac_reg->digctl = reg;
  1252. }
  1253. #endif
  1254. #ifdef DAC_DIGITAL_DEBUG_OUT_ENABLE
  1255. static void __dac_digital_dbgo(struct device *dev, uint8_t lr_sel)
  1256. {
  1257. struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  1258. uint32_t reg = sys_read32(DEBUGSEL) & ~DEBUGSEL_DBGSE_MASK;
  1259. reg |= DEBUGSEL_DBGSE(DBGSE_DAC);
  1260. sys_write32(reg, DEBUGSEL);
  1261. /* debug GPIO output pin 14-21,24-27,32-33 */
  1262. sys_write32(0x0f3fc000, DEBUGOE0);
  1263. sys_write32(0x00000003, DEBUGOE1);
  1264. reg = dac_reg->digctl & ~DAC_DIGCTL_DDDEN;
  1265. reg |= DAC_DIGCTL_DDDEN;
  1266. dac_reg->digctl = reg;
  1267. }
  1268. #endif
  1269. #ifdef CONFIG_CFG_DRV
  1270. /* @brief Enable PA/VRO output over load detection. */
  1271. static void dac_enable_pa_overload_detect(struct device *dev)
  1272. {
  1273. struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  1274. /* PA/VRO over load protection function needs enable DACANCCLK */
  1275. acts_clock_peripheral_enable(CLOCK_ID_DACANACLK);
  1276. dac_reg->anactl0 |= DAC_ANACTL0_OVDTEN;
  1277. }
  1278. #endif
  1279. /* @brief Power control(enable or disable) by DAC LDO */
  1280. static void dac_ldo_power_control(struct device *dev, bool enable)
  1281. {
  1282. ARG_UNUSED(dev);
  1283. uint32_t reg = sys_read32(ADC_REF_LDO_CTL_BASE);
  1284. if (enable) {
  1285. /** FIXME: HW issue
  1286. * ADC LDO shall be enabled when use DAC individually, otherwise VREF_ADD will get low voltage.
  1287. */
  1288. adc_reset_control(true);
  1289. acts_clock_peripheral_enable(CLOCK_ID_ADC);
  1290. /* AULDO pull down current control */
  1291. reg &= ~ADC_REF_LDO_CTL_AULDO_PD_CTL_MASK;
  1292. reg |= ADC_REF_LDO_CTL_AULDO_PD_CTL(2);
  1293. /* VREF voltage divide res control */
  1294. reg &= ~ADC_REF_LDO_CTL_VREF_RSEL_MASK;
  1295. reg |= ADC_REF_LDO_CTL_VREF_RSEL(0);
  1296. reg |= (ADC_REF_LDO_CTL_AULDO_EN(3) | ADC_REF_LDO_CTL_DALDO_EN(3));
  1297. sys_write32(reg, ADC_REF_LDO_CTL_BASE);
  1298. /* ADC/DAC VREF voltage enable */
  1299. sys_write32(sys_read32(ADC_REF_LDO_CTL_BASE) \
  1300. | ADC_REF_LDO_CTL_VREF_EN, ADC_REF_LDO_CTL_BASE);
  1301. if (!(reg & ADC_REF_LDO_CTL_VREF_EN)) {
  1302. LOG_INF("DAC wait for capacitor charge full");
  1303. sys_write32(sys_read32(ADC_REF_LDO_CTL_BASE) | ADC_REF_LDO_CTL_VREF_FU,
  1304. ADC_REF_LDO_CTL_BASE);
  1305. if (!z_is_idle_thread_object(_current))
  1306. k_sleep(K_MSEC(DAC_LDO_CAPACITOR_CHARGE_TIME_MS));
  1307. else
  1308. k_busy_wait(DAC_LDO_CAPACITOR_CHARGE_TIME_MS * 1000UL);
  1309. /* disable LDO fast charge */
  1310. sys_write32(sys_read32(ADC_REF_LDO_CTL_BASE) & ~ADC_REF_LDO_CTL_VREF_FU,
  1311. ADC_REF_LDO_CTL_BASE);
  1312. }
  1313. /* Wait for AULDO stable */
  1314. if (!z_is_idle_thread_object(_current))
  1315. k_sleep(K_MSEC(1));
  1316. else
  1317. k_busy_wait(1000);
  1318. /* reduce AULDO static power consume */
  1319. uint32_t reg1 = sys_read32(ADC_REF_LDO_CTL_BASE);
  1320. reg1 &= ~ADC_REF_LDO_CTL_AULDO_PD_CTL_MASK;
  1321. reg1 &= ~ADC_REF_LDO_CTL_VREF_RSEL_MASK;
  1322. reg1 |= ADC_REF_LDO_CTL_VREF_RSEL(3);
  1323. sys_write32(reg1, ADC_REF_LDO_CTL_BASE);
  1324. } else {
  1325. reg &= ~ADC_REF_LDO_CTL_DALDO_EN_MASK;
  1326. uint8_t is_busy = 0;
  1327. struct device *adc_dev = (struct device *)device_get_binding(CONFIG_AUDIO_ADC_0_NAME);
  1328. if (!adc_dev)
  1329. LOG_ERR("failed to bind adc device:%s", CONFIG_AUDIO_ADC_0_NAME);
  1330. uint32_t key = irq_lock();
  1331. /* check ADC is busy */
  1332. phy_audio_control(adc_dev, PHY_CMD_IS_ADC_BUSY, &is_busy);
  1333. if (is_busy)
  1334. LOG_INF("ADC current is using");
  1335. /* If ADC is idle to disable ADC LDO and VREF */
  1336. if (adc_dev && !is_busy) {
  1337. reg &= ~ADC_REF_LDO_CTL_AULDO_EN_MASK;
  1338. reg &= ~ADC_REF_LDO_CTL_VREF_EN;
  1339. }
  1340. irq_unlock(key);
  1341. sys_write32(reg, ADC_REF_LDO_CTL_BASE);
  1342. }
  1343. }
  1344. /* @brief Translate the volume in db format to DAC hardware volume level. */
  1345. static uint16_t dac_volume_db_to_level(int32_t vol)
  1346. {
  1347. uint32_t level = 0;
  1348. if (vol < 0) {
  1349. vol = -vol;
  1350. level = VOL_DB_TO_INDEX(vol);
  1351. if (level > VOL_LEVEL_0DB)
  1352. level = 0;
  1353. else
  1354. level = VOL_LEVEL_0DB - level;
  1355. } else {
  1356. level = VOL_DB_TO_INDEX(vol);
  1357. if (level > 0x40)
  1358. level = 0xFF;
  1359. else
  1360. level = VOL_LEVEL_0DB + level;
  1361. }
  1362. return level;
  1363. }
  1364. /* @brief Translate the DAC hardware volume level to volume in db format. */
  1365. static int32_t dac_volume_level_to_db(uint16_t level)
  1366. {
  1367. int32_t vol = 0;
  1368. if (level < VOL_LEVEL_0DB) {
  1369. level = VOL_LEVEL_0DB - level;
  1370. vol = VOL_INDEX_TO_DB(level);
  1371. vol = -vol;
  1372. } else {
  1373. level = level - VOL_LEVEL_0DB;
  1374. vol = VOL_INDEX_TO_DB(level);
  1375. }
  1376. return vol;
  1377. }
  1378. /* @brief DAC left/right channel volume setting */
  1379. static int dac_volume_set(struct device *dev, int32_t left_vol, int32_t right_vol, uint8_t sr, int32_t fade)
  1380. {
  1381. const struct phy_dac_config_data *cfg = dev->config;
  1382. struct phy_dac_drv_data *data = dev->data;
  1383. uint8_t lr_sel = 0;
  1384. uint16_t cur_vol_level, l_vol_level, r_vol_level;
  1385. /* check left channel not mute and volume is valid */
  1386. if (!PHY_DEV_FEATURE(left_mute) && (left_vol != AOUT_VOLUME_INVALID))
  1387. lr_sel |= LEFT_CHANNEL_SEL;
  1388. /* check right channel not mute and volume is valid */
  1389. if (!PHY_DEV_FEATURE(right_mute) && (right_vol != AOUT_VOLUME_INVALID))
  1390. lr_sel |= RIGHT_CHANNEL_SEL;
  1391. if ((left_vol <= VOL_MUTE_MIN_DB) || (right_vol <= VOL_MUTE_MIN_DB)) {
  1392. LOG_INF("volume [%d, %d] less than mute level %d",
  1393. left_vol, right_vol, VOL_MUTE_MIN_DB);
  1394. __dac_mute_control(dev, true);
  1395. data->vol_set_mute = 1;
  1396. } else {
  1397. /* disable mute when volume become normal */
  1398. if (data->vol_set_mute) {
  1399. __dac_mute_control(dev, false);
  1400. data->vol_set_mute = 0;
  1401. }
  1402. }
  1403. l_vol_level = dac_volume_db_to_level(left_vol);
  1404. cur_vol_level = __dac_volume_get(dev, LEFT_CHANNEL_SEL);
  1405. if (cur_vol_level == l_vol_level) {
  1406. LOG_DBG("ignore same left volume:%d", cur_vol_level);
  1407. lr_sel &= ~LEFT_CHANNEL_SEL;
  1408. }
  1409. r_vol_level = dac_volume_db_to_level(right_vol);
  1410. cur_vol_level = __dac_volume_get(dev, RIGHT_CHANNEL_SEL);
  1411. if (cur_vol_level == r_vol_level) {
  1412. LOG_DBG("ignore same right volume:%d", cur_vol_level);
  1413. lr_sel &= ~RIGHT_CHANNEL_SEL;
  1414. }
  1415. __dac_volume_set(dev, lr_sel, l_vol_level, r_vol_level);
  1416. if (__dac_fifosrc_is_dsp(dev) && data->dsp_audio_set_param) {
  1417. data->dsp_audio_set_param(DSP_AUDIO_SET_VOLUME, l_vol_level, r_vol_level);
  1418. }
  1419. LOG_INF("set volume {db:[%d, %d] level:[%x, %x]}",
  1420. left_vol, right_vol, l_vol_level, r_vol_level);
  1421. if (__get_pcmbuf_avail_length(dev) >= DAC_PCMBUF_MAX_CNT) {
  1422. LOG_DBG("no data in pcmbuf can not enable soft step volume");
  1423. return 0;
  1424. }
  1425. if(fade) {
  1426. if (lr_sel & LEFT_CHANNEL_SEL)
  1427. __dac_volume_left_softstep(dev, sr, false);
  1428. if (lr_sel & RIGHT_CHANNEL_SEL)
  1429. __dac_volume_right_softstep(dev, sr, false);
  1430. }
  1431. return 0;
  1432. }
  1433. /* @brief Configure the physical layout within DAC */
  1434. static int dac_physical_layout_cfg(struct device *dev)
  1435. {
  1436. const struct phy_dac_config_data *cfg = dev->config;
  1437. // struct phy_dac_drv_data *data = dev->data;
  1438. int ret = -1;
  1439. uint8_t layout = PHY_DEV_FEATURE(layout);
  1440. if(layout == DIFFERENTIAL_MODE )
  1441. {
  1442. ret = __dac_analog_diff_cfg(dev);
  1443. }
  1444. else
  1445. {
  1446. LOG_ERR("leapard can't support sing mode");
  1447. ret = -1;
  1448. }
  1449. return ret;
  1450. }
  1451. /* @brief Enable the features that supported by DAC */
  1452. static int dac_enable_features(struct device *dev, uint8_t sr)
  1453. {
  1454. const struct phy_dac_config_data *cfg = dev->config;
  1455. if (PHY_DEV_FEATURE(automute))
  1456. __dac_automute_cfg(dev);
  1457. if (PHY_DEV_FEATURE(noise_detect_mute))
  1458. __dac_sdm_mute_cfg(dev, sr);
  1459. if (PHY_DEV_FEATURE(loopback))
  1460. __dac_loopback_cfg(dev, LEFT_CHANNEL_SEL | RIGHT_CHANNEL_SEL, false);
  1461. return dac_physical_layout_cfg(dev);
  1462. return 0;
  1463. }
  1464. /* @brief DAC SR selection according to the sample rate */
  1465. static int dac_sample_rate_to_osr(struct device *dev, audio_sr_sel_e sample_rate)
  1466. {
  1467. int sr = -1;
  1468. ARG_UNUSED(dev);
  1469. switch (sample_rate) {
  1470. case SAMPLE_RATE_8KHZ:
  1471. sr = DAC_SR_8k;
  1472. break;
  1473. case SAMPLE_RATE_11KHZ:
  1474. sr = DAC_SR_11k;
  1475. break;
  1476. case SAMPLE_RATE_12KHZ:
  1477. sr = DAC_SR_12K;
  1478. break;
  1479. case SAMPLE_RATE_16KHZ:
  1480. sr = DAC_SR_16K;
  1481. break;
  1482. case SAMPLE_RATE_22KHZ:
  1483. sr = DAC_SR_22k;
  1484. break;
  1485. case SAMPLE_RATE_24KHZ:
  1486. sr = DAC_SR_24K;
  1487. break;
  1488. case SAMPLE_RATE_32KHZ:
  1489. sr = DAC_SR_32K;
  1490. break;
  1491. case SAMPLE_RATE_44KHZ:
  1492. sr = DAC_SR_44k;
  1493. break;
  1494. case SAMPLE_RATE_48KHZ:
  1495. sr = DAC_SR_48k;
  1496. break;
  1497. case SAMPLE_RATE_64KHZ:
  1498. sr = DAC_SR_11k;
  1499. break;
  1500. case SAMPLE_RATE_88KHZ:
  1501. sr = DAC_SR_88k;
  1502. break;
  1503. case SAMPLE_RATE_96KHZ:
  1504. sr = DAC_SR_96k;
  1505. break;
  1506. default:
  1507. sr = DAC_SR_44k;
  1508. break;
  1509. }
  1510. return sr;
  1511. }
  1512. /* @brief DAC sample rate config */
  1513. static int dac_sample_rate_set(struct device *dev, audio_sr_sel_e sr_khz)
  1514. {
  1515. struct phy_dac_drv_data *data = dev->data;
  1516. int ret;
  1517. uint8_t div,fir_div, fir2x_div, cic_div,series,pll_index;
  1518. uint32_t reg;
  1519. ARG_UNUSED(dev);
  1520. /* Get audio dac PLL setting */
  1521. ret = audio_get_pll_setting_dac(sr_khz, &div, &fir_div, &fir2x_div, &cic_div, &series);
  1522. if (ret) {
  1523. LOG_DBG("get pll setting error:%d", ret);
  1524. return ret;
  1525. }
  1526. /* Check the pll usage and then config */
  1527. ret = audio_pll_check_config(series, &pll_index);
  1528. if (ret) {
  1529. LOG_DBG("check pll config error:%d", ret);
  1530. return ret;
  1531. }
  1532. reg = sys_read32(CMU_DACCLK) & ~0x1FF;
  1533. /*enable dac audio*/
  1534. reg |= (CMU_DACCLK_DACSDMCLOCK | CMU_DACCLK_DACCICFIRCLOCK | CMU_DACCLK_DACFIFO0CLKEN);
  1535. /* Select audio_clk_div*/
  1536. reg |= (pll_index & 0x1) << CMU_DACCLK_DACCLKSRC;
  1537. /* set clk */
  1538. reg &= (~CMU_DACCLK_DACFIR2XCLKDIV_MASK);
  1539. reg &= (~CMU_DACCLK_DACCLKDIV_MASK);
  1540. reg &= ~(1 <<CMU_DACCLK_DACFIRCLKDIV);
  1541. reg &= ~(1 <<CMU_DACCLK_DACCICCLCKDIV);
  1542. reg |= (CMU_DACCLK_DACCLKDIV(div)| (fir_div << CMU_DACCLK_DACFIRCLKDIV)
  1543. | CMU_DACCLK_DACFIR2XCLKDIV(fir2x_div) | (cic_div << CMU_DACCLK_DACCICCLCKDIV));
  1544. reg |= CMU_DACCLK_DACHUMDIV;
  1545. data->audio_pll_index = pll_index;
  1546. sys_write32(reg, CMU_DACCLK);
  1547. return 0;
  1548. }
  1549. /* @brief Get the sample rate from the DAC config */
  1550. static int dac_sample_rate_get(struct device *dev)
  1551. {
  1552. uint8_t div,fir_div, fir2x_div, cic_div;
  1553. uint32_t reg = sys_read32(CMU_DACCLK);
  1554. ARG_UNUSED(dev);
  1555. div = (reg & CMU_DACCLK_DACCLKDIV_MASK);
  1556. fir_div = (reg & (1<<CMU_DACCLK_DACFIRCLKDIV)>>CMU_DACCLK_DACFIRCLKDIV);
  1557. fir2x_div = (reg & CMU_DACCLK_DACFIR2XCLKDIV_MASK)>>CMU_DACCLK_DACFIR2XCLKDIV_SHIFT;
  1558. cic_div = (reg & (1<<CMU_DACCLK_DACCICCLCKDIV)>>CMU_DACCLK_DACCICCLCKDIV);
  1559. return audio_get_pll_sample_rate_dac(div, fir_div, fir2x_div, cic_div, 0);
  1560. }
  1561. /* @brief Get the AUDIO_PLL APS used by DAC */
  1562. static int dac_get_pll_aps(struct device *dev)
  1563. {
  1564. uint32_t reg;
  1565. uint8_t pll_index;
  1566. ARG_UNUSED(dev);
  1567. reg = sys_read32(CMU_DACCLK);
  1568. pll_index = (reg & (1 << CMU_DACCLK_DACCLKSRC)) >> CMU_DACCLK_DACCLKSRC;
  1569. return audio_pll_get_aps((a_pll_type_e)pll_index);
  1570. }
  1571. /* @brief Set the AUDIO_PLL APS used by DAC */
  1572. static int dac_set_pll_aps(struct device *dev, audio_aps_level_e level)
  1573. {
  1574. uint32_t reg;
  1575. uint8_t pll_index;
  1576. ARG_UNUSED(dev);
  1577. reg = sys_read32(CMU_DACCLK);
  1578. pll_index = (reg & (1 << CMU_DACCLK_DACCLKSRC)) >> CMU_DACCLK_DACCLKSRC;
  1579. return audio_pll_set_aps((a_pll_type_e)pll_index, level);
  1580. }
  1581. /* @brief Get the DAC DMA information */
  1582. static int dac_get_dma_info(struct device *dev, struct audio_out_dma_info *info)
  1583. {
  1584. const struct phy_dac_config_data *cfg = dev->config;
  1585. if (AOUT_FIFO_DAC0 == info->fifo_type) {
  1586. info->dma_info.dma_chan = cfg->dma_fifo0.dma_chan;
  1587. info->dma_info.dma_dev_name = cfg->dma_fifo0.dma_dev_name;
  1588. info->dma_info.dma_id = cfg->dma_fifo0.dma_id;
  1589. } else {
  1590. return -ENOENT;
  1591. }
  1592. return 0;
  1593. }
  1594. #ifdef CONFIG_CFG_DRV
  1595. /* @brief DAC external PA control */
  1596. static int dac_external_pa_ctl(struct device *dev, uint8_t ctrl_func)
  1597. {
  1598. struct phy_dac_drv_data *data = dev->data;
  1599. uint8_t i, pa_func, enable;
  1600. gpio_flags_t flags = GPIO_OUTPUT;
  1601. const struct device *gpio_dev = NULL;
  1602. if ((ctrl_func != EXTERNAL_PA_ENABLE)
  1603. && (ctrl_func != EXTERNAL_PA_DISABLE)
  1604. && (ctrl_func != EXTERNAL_PA_MUTE)
  1605. && (ctrl_func != EXTERNAL_PA_UNMUTE)) {
  1606. LOG_ERR("invalid external pa ctrl:%d", ctrl_func);
  1607. return -EINVAL;
  1608. }
  1609. if (ctrl_func == EXTERNAL_PA_ENABLE) {
  1610. pa_func = EXTERN_PA_ENABLE;
  1611. enable = true;
  1612. } else if (ctrl_func == EXTERNAL_PA_DISABLE) {
  1613. pa_func = EXTERN_PA_ENABLE;
  1614. enable = false;
  1615. } else if (ctrl_func == EXTERNAL_PA_MUTE) {
  1616. pa_func = EXTERN_PA_MUTE;
  1617. enable = true;
  1618. } else {
  1619. pa_func = EXTERN_PA_MUTE;
  1620. pa_func = EXTERN_PA_MUTE;
  1621. enable = false;
  1622. }
  1623. for (i = 0; i < ARRAY_SIZE(data->external_config.Extern_PA_Control); i++) {
  1624. CFG_Type_Extern_PA_Control *cfg = &data->external_config.Extern_PA_Control[i];
  1625. if (cfg->PA_Function == pa_func && cfg->GPIO_Pin != GPIO_NONE) {
  1626. if (cfg->Pull_Up_Down != CFG_GPIO_PULL_NONE) {
  1627. if (cfg->Pull_Up_Down == CFG_GPIO_PULL_DOWN)
  1628. flags |= GPIO_PULL_DOWN;
  1629. else
  1630. flags |= GPIO_CTL_PULLUP;
  1631. }
  1632. gpio_dev = device_get_binding(CONFIG_GPIO_PIN2NAME(cfg->GPIO_Pin));
  1633. if (!gpio_dev) {
  1634. LOG_ERR("failed to bind GPIO(%d) device", cfg->GPIO_Pin);
  1635. return -ENODEV;
  1636. }
  1637. gpio_pin_configure(gpio_dev, cfg->GPIO_Pin % 32, flags);
  1638. if (enable)
  1639. gpio_pin_set(gpio_dev, cfg->GPIO_Pin % 32, cfg->Active_Level);
  1640. else
  1641. gpio_pin_set(gpio_dev, cfg->GPIO_Pin % 32, !cfg->Active_Level);
  1642. }
  1643. }
  1644. return 0;
  1645. }
  1646. #endif
  1647. /* @brief prepare dac runtime resources such as clock etc. */
  1648. static int phy_dac_prepare_enable(struct device *dev, aout_param_t *out_param)
  1649. {
  1650. dac_setting_t *dac_setting = NULL;
  1651. const struct phy_dac_config_data *cfg = dev->config;
  1652. if ((!out_param) || (!out_param->dac_setting)
  1653. || (!out_param->sample_rate)) {
  1654. LOG_ERR("Invalid parameters");
  1655. return -EINVAL;
  1656. }
  1657. dac_setting = out_param->dac_setting;
  1658. if(dac_setting== NULL)
  1659. {
  1660. LOG_ERR("dac_setting is NULL");
  1661. return -EINVAL;
  1662. }
  1663. if (dac_setting->channel_mode != MONO_MODE) {
  1664. LOG_ERR("Invalid channel mode %d", dac_setting->channel_mode);
  1665. return -EINVAL;
  1666. }
  1667. if (!(out_param->channel_type & AUDIO_CHANNEL_DAC)) {
  1668. LOG_ERR("Invalid channel type %d", out_param->channel_type);
  1669. return -EINVAL;
  1670. }
  1671. if ((out_param->outfifo_type != AOUT_FIFO_DAC0)
  1672. && (out_param->outfifo_type != AOUT_FIFO_DAC1)) {
  1673. LOG_ERR("Invalid FIFO type %d", out_param->outfifo_type);
  1674. return -EINVAL;
  1675. }
  1676. if ((out_param->outfifo_type == AOUT_FIFO_DAC1)
  1677. && !__is_dac_fifo_working(dev, DAC_FIFO_0)) {
  1678. LOG_ERR("DAC FIFO1 depends on DAC FIFO0 enabled");
  1679. return -EPERM;
  1680. }
  1681. if (out_param->reload_setting) {
  1682. LOG_ERR("DAC FIFO does not support reload mode");
  1683. return -EINVAL;
  1684. }
  1685. /* Enable DAC clock gate */
  1686. acts_clock_peripheral_enable(cfg->clk_id);
  1687. /* DAC main clock source is alway 256FS */
  1688. if (dac_sample_rate_set(dev, out_param->sample_rate)) {
  1689. LOG_ERR("Failed to config sample rate %d", out_param->sample_rate);
  1690. return -ESRCH;
  1691. }
  1692. return 0;
  1693. }
  1694. static int phy_dac_disable_pa(struct device *dev)
  1695. {
  1696. struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  1697. #ifdef CONFIG_CFG_DRV
  1698. dac_external_pa_ctl((struct device *)dev, EXTERNAL_PA_DISABLE);
  1699. #endif
  1700. dac_reg->anactl0 = 0;
  1701. dac_reg->anactl1 = 0;
  1702. dac_reg->anactl2 = 0;
  1703. dac_ldo_power_control(dev, false);
  1704. acts_clock_peripheral_disable(CMU_DACCLK);
  1705. return 0;
  1706. }
  1707. /* @brief ADC BIAS setting for power saving */
  1708. static void dac_bias_setting(struct device *dev)
  1709. {
  1710. #ifdef CONFIG_CFG_DRV
  1711. struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  1712. struct phy_dac_drv_data *data = dev->data;
  1713. dac_reg->bias = data->external_config.DAC_Bias_Setting;
  1714. #endif
  1715. }
  1716. /* @brief Wait the DAC FIFO empty */
  1717. static int dac_wait_fifo_empty(struct device *dev, a_dac_fifo_e idx, uint32_t timeout_ms)
  1718. {
  1719. uint32_t start_time;
  1720. struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  1721. start_time = k_cycle_get_32();
  1722. LOG_DBG("wait DAC FIFO empty start time:%d", start_time);
  1723. /* disable PCMBUF irqs to avoid user continuously writing data */
  1724. if (DAC_FIFO_0 == idx) {
  1725. dac_reg->pcm_buf_ctl &= ~DAC_PCMBUF_DEFAULT_IRQ;
  1726. }
  1727. while (!__is_dac_fifo_empty(dev, idx)) {
  1728. if (k_cyc_to_us_floor32(k_cycle_get_32() - start_time)
  1729. >= (timeout_ms * 1000)) {
  1730. LOG_ERR("wait dac fifo(%d) empty(0x%x) timeout",
  1731. idx, __get_pcmbuf_avail_length(dev));
  1732. return -ETIMEDOUT;
  1733. }
  1734. /* PM works in IDLE thread and not allow to sleep */
  1735. if (!z_is_idle_thread_object(_current))
  1736. k_sleep(K_MSEC(1));
  1737. }
  1738. LOG_DBG("wait DAC FIFO empty end time:%d and total use %dus",
  1739. k_cycle_get_32(), k_cycle_get_32() - start_time);
  1740. return 0;
  1741. }
  1742. static int phy_dac_enable(struct device *dev, void *param)
  1743. {
  1744. struct phy_dac_drv_data *data = dev->data;
  1745. const struct phy_dac_config_data *cfg = dev->config;
  1746. aout_param_t *out_param = (aout_param_t *)param;
  1747. dac_setting_t *dac_setting = out_param->dac_setting;
  1748. int ret;
  1749. uint8_t fifo_idx;
  1750. int8_t sr = DAC_SR_44k;
  1751. /* reset DAC controller */
  1752. acts_reset_peripheral(cfg->rst_id);
  1753. soc_powergate_set(POWERGATE_DSP_AU_PG_DEV, true);
  1754. /* enable DAC LDO */
  1755. dac_ldo_power_control(dev, true);
  1756. ret = phy_dac_prepare_enable(dev, out_param);
  1757. if (ret) {
  1758. LOG_ERR("Failed to prepare enable dac err=%d", ret);
  1759. acts_reset_peripheral_assert(cfg->rst_id);
  1760. soc_powergate_set(POWERGATE_DSP_AU_PG_DEV, false);
  1761. return ret;
  1762. }
  1763. fifo_idx = out_param->outfifo_type;
  1764. #if 0 /* Don't check fifo status for ANC */
  1765. if (__is_dac_fifo_working(dev, fifo_idx)) {
  1766. LOG_ERR("The DAC FIFO@%d now is using", out_param->outfifo_type);
  1767. return -EACCES;
  1768. }
  1769. #endif
  1770. ret = __dac_fifo_enable(dev, FIFO_SEL_DMA,
  1771. (out_param->channel_width == CHANNEL_WIDTH_16BITS)
  1772. ? DMA_WIDTH_16BITS : DMA_WIDTH_32BITS,
  1773. DAC_FIFO_DRQ_LEVEL_DEFAULT,
  1774. DAC_FIFO_VOL_LEVEL_DEFAULT,
  1775. fifo_idx, true);
  1776. if (ret)
  1777. {
  1778. acts_reset_peripheral_assert(cfg->rst_id);
  1779. soc_powergate_set(POWERGATE_DSP_AU_PG_DEV, false);
  1780. return ret;
  1781. }
  1782. __dac_pcmbuf_config(dev);
  1783. sr = dac_sample_rate_to_osr(dev, out_param->sample_rate);
  1784. if (sr < 0) {
  1785. LOG_ERR("mapping sample rate:%d to osr error", out_param->sample_rate);
  1786. ret = -EFAULT;
  1787. goto err;
  1788. }
  1789. // ret_anc = anc_disable_for_interpolation_x3(dev, out_param->channel_type);
  1790. ret = __dac_digital_enable(dev, sr,
  1791. dac_setting->channel_mode, out_param->channel_type);
  1792. if (ret) {
  1793. LOG_ERR("Failed to enable DAC digital err=%d", ret);
  1794. goto err;
  1795. }
  1796. // if (ret_anc == 1)
  1797. // anc_enable_for_interpolation_x3(dev);
  1798. dac_bias_setting(dev);
  1799. ret = dac_enable_features(dev, out_param->sample_rate);
  1800. if (ret) {
  1801. LOG_ERR("DAC enable features error %d", ret);
  1802. goto err;
  1803. }
  1804. data->sample_rate = out_param->sample_rate;
  1805. /* Record the PCM BUF data callback */
  1806. data->ch[fifo_idx].callback = out_param->callback;
  1807. data->ch[fifo_idx].cb_data = out_param->cb_data;
  1808. LOG_DBG("DAC ch@%d register callback %p cb_data %p",
  1809. fifo_idx, data->ch[fifo_idx].callback, data->ch[fifo_idx].cb_data);
  1810. /* Clear FIFO ERROR */
  1811. __dac_clear_fifo_error(dev, fifo_idx);
  1812. #ifdef DAC_ANALOG_DEBUG_IN_ENABLE
  1813. __dac_analog_dbgi(dev);
  1814. #endif
  1815. #ifdef DAC_DIGITAL_DEBUG_OUT_ENABLE
  1816. __dac_digital_dbgo(dev, DAC_DIGITAL_DEBUG_OUT_CHANNEL_SEL);
  1817. #endif
  1818. ret = dac_volume_set(dev, dac_setting->volume.left_volume,
  1819. dac_setting->volume.right_volume,
  1820. out_param->sample_rate, 0);
  1821. if (ret)
  1822. goto err;
  1823. uint32_t key = irq_lock();
  1824. /* set channel start flag */
  1825. if (DAC_FIFO_0 == fifo_idx)
  1826. data->ch_fifo0_start = 1;
  1827. else if (DAC_FIFO_1 == fifo_idx)
  1828. data->ch_fifo1_start = 1;
  1829. atomic_inc(&data->refcount);
  1830. irq_unlock(key);
  1831. return ret;
  1832. err:
  1833. __dac_fifo_disable(dev, fifo_idx);
  1834. acts_reset_peripheral_assert(cfg->rst_id);
  1835. soc_powergate_set(POWERGATE_DSP_AU_PG_DEV, false);
  1836. return ret;
  1837. }
  1838. // #ifdef CONFIG_AUDIO_ANTIPOP_PROCESS
  1839. // #if (CONFIG_AUDIO_DAC_0_LAYOUT == 1) //direct driver
  1840. // static void dac_single_end_on_antipop(struct device *dev)
  1841. // {
  1842. // int ramp_data = 0, max_pcm = 0x7FFFFF, min_pcm = -8388607, i;
  1843. // uint32_t ramp_step = 3000, dac_fifoctl;
  1844. // struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  1845. // /* enable DAC clock gate */
  1846. // acts_clock_peripheral_enable(CLOCK_ID_DAC);
  1847. // /* set sample rate 48K */
  1848. // dac_sample_rate_set(dev, SAMPLE_RATE_48KHZ);
  1849. // /* ldo enable */
  1850. // dac_ldo_power_control(dev, true);
  1851. // dac_fifoctl = dac_reg->fifoctl;
  1852. // /* enable DAC digital function */
  1853. // __dac_digital_enable(dev, DAC_OSR_64X, STEREO_MODE, AUDIO_CHANNEL_DAC);
  1854. // /* switch to old plan and set to 0 db */
  1855. // dac_reg->anactl0 &= ~DAC_ANACTL0_SEL_PLAN;
  1856. // dac_reg->anactl0 &= ~DAC_ANACTL0_SEL_CUR_MASK;
  1857. // /* 1.playback mute, all bias en, dac ana/dac dig/pa en, and set to diff mode */
  1858. // sys_write32(sys_read32(CMU_DACCLK) | CMU_DACCLK_DACFIFO0CLKEN, CMU_DACCLK);
  1859. // /* DAC FIFO0 MIX to DAC enable and disable DAC FIFO1 MIX */
  1860. // dac_reg->digctl |= DAC_DIGCTL_DAF0M2DAEN;
  1861. // // dac_reg->digctl &= ~DAC_DIGCTL_DAF1M2DAEN;
  1862. // /* enable PA DFC option */
  1863. // dac_reg->anactl0 |= DAC_ANACTL0_DFCEN;
  1864. // /* enable DAC + PA bias */
  1865. // dac_reg->anactl0 |= DAC_ANACTL0_BIASEN;
  1866. // /* disable LN/LP play back mute */
  1867. // dac_reg->anactl1 &= ~(DAC_ANACTL1_DPBMLP | DAC_ANACTL1_DPBMLN);
  1868. // /* disable DAC R play back mute */
  1869. // dac_reg->anactl1 &= ~DAC_ANACTL1_DPBMR;
  1870. // /* set differential mode */
  1871. // dac_reg->anactl0 |= DAC_ANACTL0_DIFFM;
  1872. // /* left/right channels enable */
  1873. // dac_reg->anactl0 |= (DAC_ANACTL0_DAENL | DAC_ANACTL0_DAENR);
  1874. // /* LN/LP OP enable */
  1875. // dac_reg->anactl0 |= (DAC_ANACTL0_PALNEN | DAC_ANACTL0_PALPEN);
  1876. // /* RN/RP OP enable */
  1877. // dac_reg->anactl0 |= (DAC_ANACTL0_PARNEN | DAC_ANACTL0_PARPEN);
  1878. // /* output stage enable */
  1879. // dac_reg->anactl0 |= (DAC_ANACTL0_PALNOSEN | DAC_ANACTL0_PARNOSEN);
  1880. // dac_reg->anactl0 |= (DAC_ANACTL0_SW1LN | DAC_ANACTL0_SW1RN);
  1881. // /* PA VOL set 0db */
  1882. // dac_reg->anactl0 |= DAC_ANACTL0_PAVOL(7);
  1883. // /* clear left and right channels soft step volume done pending */
  1884. // while (dac_reg->vol_lch & VOL_LCH_DONE_PD) {
  1885. // dac_reg->vol_lch |= VOL_LCH_DONE_PD;
  1886. // }
  1887. // while (dac_reg->vol_rch & VOL_RCH_DONE_PD) {
  1888. // dac_reg->vol_rch |= VOL_RCH_DONE_PD;
  1889. // }
  1890. // /* left and right channels volume set as 0db */
  1891. // //dac_reg->vol_lch |= VOL_LCH_SOFT_STEP_EN;
  1892. // //dac_reg->vol_rch |= VOL_RCH_SOFT_STEP_EN;
  1893. // if ((dac_reg->vol_lch & VOL_LCH_VOLL_MASK) != 0xBF) {
  1894. // dac_reg->vol_lch = (dac_reg->vol_lch & ~VOL_LCH_VOLL_MASK) | 0xBF;
  1895. // while (!(dac_reg->vol_lch & VOL_LCH_DONE_PD));
  1896. // dac_reg->vol_lch |= VOL_LCH_DONE_PD;
  1897. // }
  1898. // if ((dac_reg->vol_rch & VOL_RCH_VOLR_MASK) != 0xBF) {
  1899. // dac_reg->vol_rch = (dac_reg->vol_rch & ~VOL_RCH_VOLR_MASK) | 0xBF;
  1900. // while (!(dac_reg->vol_rch & VOL_RCH_DONE_PD));
  1901. // dac_reg->vol_rch |= VOL_RCH_DONE_PD;
  1902. // }
  1903. // /* set DACFIFO source from CPU */
  1904. // dac_reg->fifoctl = 0x7301;
  1905. // /* left/right channel antipop ramp data compensation */
  1906. // dac_reg->anactl1 |= DAC_ANACTL1_ATP2RCENL;
  1907. // dac_reg->anactl1 |= DAC_ANACTL1_ATP2RCENR;
  1908. // /* send max pcm data to DAC FIFO0 and delay 2ms */
  1909. // for (i = 0; i < 4; i++) {
  1910. // dac_reg->fifo0_dat = max_pcm << 8;
  1911. // dac_reg->fifo0_dat = max_pcm << 8;
  1912. // }
  1913. // if (!z_is_idle_thread_object(_current))
  1914. // k_sleep(K_MSEC(2));
  1915. // else
  1916. // k_busy_wait(2000UL);
  1917. // /* enable loop2 and SW2 connect for LP/RP */
  1918. // dac_reg->anactl1 |= (DAC_ANACTL1_LP2LPEN | DAC_ANACTL1_LP2RPEN);
  1919. // dac_reg->anactl1 |= (DAC_ANACTL1_ATPSW2LP | DAC_ANACTL1_ATPSW2RP);
  1920. // for (i = 0; i < 4; i++) {
  1921. // dac_reg->fifo0_dat = max_pcm << 8;
  1922. // dac_reg->fifo0_dat = max_pcm << 8;
  1923. // }
  1924. // if (!z_is_idle_thread_object(_current))
  1925. // k_sleep(K_MSEC(2));
  1926. // else
  1927. // k_busy_wait(2000UL);
  1928. // /* antipop ramp connect enable for PA */
  1929. // dac_reg->anactl1 |= (DAC_ANACTL1_ATPRCEN_LP | DAC_ANACTL1_ATPRCEN_RP);
  1930. // dac_reg->anactl1 |= (DAC_ANACTL1_ATPRC2EN_LP | DAC_ANACTL1_ATPRC2EN_RP);
  1931. // ramp_data = 0;
  1932. // ramp_step = 8000;
  1933. // /* send ramp data */
  1934. // while (ramp_data > min_pcm) {
  1935. // /* wait pcmbuf not full */
  1936. // while ((dac_reg->pcm_buf_stat & PCM_BUF_STAT_PCMBS_MASK) < 2);
  1937. // /* wait dacfifo not full */
  1938. // while ((dac_reg->stat & DAC_STAT_DAF0S_MASK) < 2);
  1939. // dac_reg->fifo0_dat = ramp_data << 8;
  1940. // dac_reg->fifo0_dat = ramp_data << 8;
  1941. // ramp_data -= ramp_step;
  1942. // }
  1943. // /* wait pcmbuf empty */
  1944. // while ((dac_reg->pcm_buf_stat & PCM_BUF_STAT_PCMBS_MASK) != 0x800);
  1945. // for (i = 0; i < 4; i++)
  1946. // dac_reg->fifo0_dat = min_pcm << 8;
  1947. // if (!z_is_idle_thread_object(_current))
  1948. // k_sleep(K_MSEC(2));
  1949. // else
  1950. // k_busy_wait(2000UL);
  1951. // /* enable sw1 and pa outputstage, disable sw2 */
  1952. // dac_reg->anactl0 |= (DAC_ANACTL0_SW1LP | DAC_ANACTL0_SW1RP);
  1953. // dac_reg->anactl0 |= (DAC_ANACTL0_PALPOSEN | DAC_ANACTL0_PARPOSEN);
  1954. // dac_reg->anactl1 &= ~(DAC_ANACTL1_ATPSW2LP | DAC_ANACTL1_ATPSW2RP);
  1955. // /* send ramp data for disable atprcen */
  1956. // ramp_data = 0;
  1957. // ramp_step = 4000;
  1958. // while (ramp_data < max_pcm) {
  1959. // /* wait pcmbuf not full */
  1960. // while ((dac_reg->pcm_buf_stat & PCM_BUF_STAT_PCMBS_MASK) < 2);
  1961. // /* wait dacfifo not full */
  1962. // while ((dac_reg->stat & DAC_STAT_DAF0S_MASK) < 2);
  1963. // dac_reg->fifo0_dat = ramp_data << 8;
  1964. // dac_reg->fifo0_dat = ramp_data << 8;
  1965. // ramp_data += ramp_step;
  1966. // }
  1967. // /* wait pcmbuf empty */
  1968. // while ((dac_reg->pcm_buf_stat & PCM_BUF_STAT_PCMBS_MASK) != 0x800);
  1969. // for (i = 0; i < 4; i++)
  1970. // dac_reg->fifo0_dat = max_pcm << 8;
  1971. // if (!z_is_idle_thread_object(_current))
  1972. // k_sleep(K_MSEC(2));
  1973. // else
  1974. // k_busy_wait(2000UL);
  1975. // /* disable loop2 and atprcen and atprcen2 */
  1976. // dac_reg->anactl1 &= ~(DAC_ANACTL1_LP2LPEN | DAC_ANACTL1_LP2RPEN);
  1977. // dac_reg->anactl1 &= ~(DAC_ANACTL1_ATPSW2LP | DAC_ANACTL1_ATPSW2RP);
  1978. // dac_reg->anactl1 &= ~(DAC_ANACTL1_ATPRC2EN_LP | DAC_ANACTL1_ATPRC2EN_RP);
  1979. // dac_reg->anactl1 &= ~(DAC_ANACTL1_ATPRCEN_LP | DAC_ANACTL1_ATPRCEN_RP);
  1980. // dac_reg->anactl1 &= ~(DAC_ANACTL1_ATP2RCENL);
  1981. // dac_reg->anactl1 &= ~(DAC_ANACTL1_ATP2RCENR);
  1982. // for (i = 0; i < 4; i++)
  1983. // dac_reg->fifo0_dat = 0;
  1984. // if (!z_is_idle_thread_object(_current))
  1985. // k_sleep(K_MSEC(2));
  1986. // else
  1987. // k_busy_wait(2000UL);
  1988. // dac_reg->fifoctl = dac_fifoctl;
  1989. // dac_reg->anactl0 |= DAC_ANACTL0_ZERODT;
  1990. // //dac_reg->anactl0 |= DAC_ANACTL0_SEL_PLAN;
  1991. // //dac_reg->anactl0 |= DAC_ANACTL0_SEL_CUR_MASK;
  1992. // dac_reg->anactl0 &= ~(1 << 22);
  1993. // if (!z_is_idle_thread_object(_current))
  1994. // k_sleep(K_MSEC(2));
  1995. // else
  1996. // k_busy_wait(2000UL);
  1997. // #if (CONFIG_AUDIO_DAC_HIGH_PERFORMACE_DIFF_EN == 1)
  1998. // __dac_analog_diff_sh_cfg(dev, 20, 100, 0);
  1999. // #endif
  2000. // dac_reg->anactl0 &= ~DAC_ANACTL0_ZERODT;
  2001. // }
  2002. // static void dac_single_end_off_antipop(struct device *dev)
  2003. // {
  2004. // int ramp_data = 0, max_pcm = 8388607, min_pcm = -8388607, i;
  2005. // uint32_t ramp_step;
  2006. // struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  2007. // /* enable DAC clock gate */
  2008. // acts_clock_peripheral_enable(CLOCK_ID_DAC);
  2009. // /* set sample rate 48K */
  2010. // dac_sample_rate_set(dev, SAMPLE_RATE_48KHZ);
  2011. // /* ldo enable */
  2012. // dac_ldo_power_control(dev, true);
  2013. // /* enable DAC digital function */
  2014. // __dac_digital_enable(dev, DAC_OSR_64X, STEREO_MODE, AUDIO_CHANNEL_DAC);
  2015. // /* set DACFIFO source from CPU */
  2016. // dac_reg->fifoctl = 0x7301;
  2017. // /* enable zero data */
  2018. // dac_reg->anactl0 |= DAC_ANACTL0_ZERODT;
  2019. // /* switch to old plan */
  2020. // dac_reg->anactl2 = 0;
  2021. // dac_reg->anactl0 &= ~DAC_ANACTL0_SEL_CUR_MASK;
  2022. // dac_reg->anactl0 &= ~DAC_ANACTL0_SEL_PLAN;
  2023. // dac_reg->anactl0 |= DAC_ANACTL0_PAVOL(7);
  2024. // /* enable PA DFC option */
  2025. // dac_reg->anactl0 |= DAC_ANACTL0_DFCEN;
  2026. // /* enable DAC + PA bias */
  2027. // dac_reg->anactl0 |= DAC_ANACTL0_BIASEN;
  2028. // /* disable LN/LP play back mute */
  2029. // dac_reg->anactl1 &= ~(DAC_ANACTL1_DPBMLP | DAC_ANACTL1_DPBMLN);
  2030. // /* disable DAC R play back mute */
  2031. // dac_reg->anactl1 &= ~DAC_ANACTL1_DPBMR;
  2032. // /* set differential mode */
  2033. // dac_reg->anactl0 |= DAC_ANACTL0_DIFFM;
  2034. // /* left/right channels enable */
  2035. // dac_reg->anactl0 |= (DAC_ANACTL0_DAENL | DAC_ANACTL0_DAENR);
  2036. // /* LN/LP OP enable */
  2037. // dac_reg->anactl0 |= (DAC_ANACTL0_PALNEN | DAC_ANACTL0_PALPEN);
  2038. // /* RN/RP OP enable */
  2039. // dac_reg->anactl0 |= (DAC_ANACTL0_PARNEN | DAC_ANACTL0_PARPEN);
  2040. // /* output stage enable */
  2041. // dac_reg->anactl0 |= (DAC_ANACTL0_PALNOSEN | DAC_ANACTL0_PARNOSEN);
  2042. // dac_reg->anactl0 |= (DAC_ANACTL0_SW1LN | DAC_ANACTL0_SW1RN);
  2043. // if (!z_is_idle_thread_object(_current))
  2044. // k_sleep(K_MSEC(3));
  2045. // else
  2046. // k_busy_wait(3000UL);
  2047. // /* disable zero data */
  2048. // dac_reg->anactl0 &= ~DAC_ANACTL0_ZERODT;
  2049. // /* enable DAC FIFO clock */
  2050. // sys_write32(sys_read32(CMU_DACCLK) | CMU_DACCLK_DACFIFO0CLKEN, CMU_DACCLK);
  2051. // dac_reg->vol_lch = (dac_reg->vol_lch & ~VOL_LCH_VOLL_MASK) | 0xBF;
  2052. // while ((dac_reg->vol_lch & VOL_LCH_VOLL_MASK) != 0xBF)
  2053. // {
  2054. // ;
  2055. // }
  2056. // dac_reg->vol_rch = (dac_reg->vol_rch & ~VOL_RCH_VOLR_MASK) | 0xBF;
  2057. // while ((dac_reg->vol_rch & VOL_RCH_VOLR_MASK) != 0xBF)
  2058. // {
  2059. // ;
  2060. // }
  2061. // dac_reg->digctl |= DAC_DIGCTL_DAF0M2DAEN;
  2062. // // dac_reg->digctl &= ~DAC_DIGCTL_DAF1M2DAEN;
  2063. // /* enable DAC ANA clock */
  2064. // acts_clock_peripheral_enable(CLOCK_ID_DACANACLK);
  2065. // dac_reg->anactl1 |= DAC_ANACTL1_ATP2RCENL;
  2066. // dac_reg->anactl1 |= DAC_ANACTL1_ATP2RCENR;
  2067. // //dac_reg->fifoctl = 0x7301;
  2068. // /* send max pcm data to DAC FIFO0 and delay 2ms */
  2069. // for (i = 0; i < 4; i++) {
  2070. // dac_reg->fifo0_dat = max_pcm << 8;
  2071. // }
  2072. // if (!z_is_idle_thread_object(_current))
  2073. // k_sleep(K_MSEC(2));
  2074. // else
  2075. // k_busy_wait(2000UL);
  2076. // /* enable loop2/atprcen/atprc2en */
  2077. // dac_reg->anactl1 |= (DAC_ANACTL1_ATPRCEN_LP | DAC_ANACTL1_ATPRCEN_RP);
  2078. // dac_reg->anactl1 |= (DAC_ANACTL1_ATPRC2EN_LP | DAC_ANACTL1_ATPRC2EN_RP);
  2079. // dac_reg->anactl1 |= (DAC_ANACTL1_LP2LPEN | DAC_ANACTL1_LP2RPEN);
  2080. // /* send data from max ramp-data to min pcm data */
  2081. // ramp_data = 0;
  2082. // ramp_step = 10000;
  2083. // while (ramp_data > min_pcm) {
  2084. // /* wait pcmbuf not full */
  2085. // while ((dac_reg->pcm_buf_stat & PCM_BUF_STAT_PCMBS_MASK) < 2)
  2086. // {
  2087. // ;
  2088. // }
  2089. // /* wait dacfifo not full */
  2090. // while ((dac_reg->stat & DAC_STAT_DAF0S_MASK) < 2)
  2091. // {
  2092. // ;
  2093. // }
  2094. // dac_reg->fifo0_dat = ramp_data << 8;
  2095. // dac_reg->fifo0_dat = ramp_data << 8;
  2096. // ramp_data -= ramp_step;
  2097. // }
  2098. // /* wait pcmbuf empty */
  2099. // while ((dac_reg->pcm_buf_stat & PCM_BUF_STAT_PCMBS_MASK) != 0x800)
  2100. // {
  2101. // ;
  2102. // }
  2103. // /* enable sw2 */
  2104. // dac_reg->anactl1 |= (DAC_ANACTL1_ATPSW2LP | DAC_ANACTL1_ATPSW2RP);
  2105. // for (i = 0; i < 4; i++)
  2106. // dac_reg->fifo0_dat = min_pcm << 8;
  2107. // if (!z_is_idle_thread_object(_current))
  2108. // k_sleep(K_MSEC(2));
  2109. // else
  2110. // k_busy_wait(2000UL);
  2111. // /* disable sw1 and positive end paosen */
  2112. // dac_reg->anactl0 &= ~(DAC_ANACTL0_SW1LP | DAC_ANACTL0_SW1RP);
  2113. // dac_reg->anactl0 &= ~(DAC_ANACTL0_PALPOSEN | DAC_ANACTL0_PARPOSEN);
  2114. // /* send data from max ramp-data to min pcm data */
  2115. // ramp_data = 0;
  2116. // ramp_step = 10000;
  2117. // while (ramp_data < max_pcm) {
  2118. // /* wait pcmbuf not full */
  2119. // while ((dac_reg->pcm_buf_stat & PCM_BUF_STAT_PCMBS_MASK) < 2)
  2120. // {
  2121. // ;
  2122. // }
  2123. // /* wait dacfifo not full */
  2124. // while ((dac_reg->stat & DAC_STAT_DAF0S_MASK) < 2)
  2125. // {
  2126. // ;
  2127. // }
  2128. // dac_reg->fifo0_dat = ramp_data << 8;
  2129. // dac_reg->fifo0_dat = ramp_data << 8;
  2130. // ramp_data += ramp_step;
  2131. // }
  2132. // /* wait pcmbuf empty */
  2133. // while ((dac_reg->pcm_buf_stat & PCM_BUF_STAT_PCMBS_MASK) != 0x800)
  2134. // {
  2135. // ;
  2136. // }
  2137. // /* disable atprcen, atprcen2, loop2en, sw2, da, pa,negatve end pa and paost */
  2138. // dac_reg->anactl1 &= ~(DAC_ANACTL1_ATPRCEN_LP | DAC_ANACTL1_ATPRCEN_RP);
  2139. // dac_reg->anactl1 &= ~(DAC_ANACTL1_ATPRC2EN_LP | DAC_ANACTL1_ATPRC2EN_RP);
  2140. // dac_reg->anactl1 &= ~(DAC_ANACTL1_LP2LPEN | DAC_ANACTL1_LP2RPEN);
  2141. // dac_reg->anactl1 &= ~(DAC_ANACTL1_ATPSW2LP | DAC_ANACTL1_ATPSW2RP);
  2142. // dac_reg->anactl1 &= ~DAC_ANACTL1_ATP2RCENL;
  2143. // dac_reg->anactl1 &= ~DAC_ANACTL1_ATP2RCENR;
  2144. // dac_reg->anactl0 &= ~(DAC_ANACTL0_DAENL | DAC_ANACTL0_DAENR);
  2145. // dac_reg->anactl0 &= ~(DAC_ANACTL0_PALPEN | DAC_ANACTL0_PARPEN);
  2146. // dac_reg->anactl0 &= ~(DAC_ANACTL0_PALNEN | DAC_ANACTL0_PARNEN);
  2147. // dac_reg->anactl0 &= ~(DAC_ANACTL0_PALNOSEN | DAC_ANACTL0_PARNOSEN);
  2148. // dac_reg->anactl0 &= ~(DAC_ANACTL0_SW1LN | DAC_ANACTL0_SW1RN);
  2149. // dac_reg->digctl &= ~DAC_DIGCTL_DDEN;
  2150. // dac_reg->anactl0 &= ~DAC_ANACTL0_BIASEN;
  2151. // dac_reg->fifoctl &= ~DAC_FIFOCTL_DAF0RT;
  2152. // sys_write32(sys_read32(CMU_DACCLK) & ~CMU_DACCLK_DACFIFO0CLKEN, CMU_DACCLK);
  2153. // dac_reg->digctl &= ~DAC_DIGCTL_DAF0M2DAEN;
  2154. // }
  2155. // #else
  2156. // static void dac_single_end_on_antipop(struct device *dev)
  2157. // {
  2158. // int ramp_data, max_pcm, min_pcm, data_cnt, i;
  2159. // uint32_t ramp_step;
  2160. // max_pcm = 0x7fffffff;//524287
  2161. // min_pcm = 0x80000001;//-524287
  2162. // data_cnt = 4800*3;//9600 samples each channel equals 200ms under 48kfs
  2163. // ramp_step = 0xffffffff / data_cnt;
  2164. // uint32_t dac_fifoctl;
  2165. // struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  2166. // /* enable DAC clock gate */
  2167. // acts_clock_peripheral_enable(CLOCK_ID_DAC);
  2168. // /* set sample rate 48K */
  2169. // dac_sample_rate_set(dev, SAMPLE_RATE_48KHZ);
  2170. // /* ldo enable */
  2171. // dac_ldo_power_control(dev, true);
  2172. // /* enable DAC digital function */
  2173. // __dac_digital_enable(dev, DAC_OSR_64X, STEREO_MODE, AUDIO_CHANNEL_DAC);
  2174. // sys_write32(sys_read32(CMU_DACCLK) | CMU_DACCLK_DACFIFO0CLKEN, CMU_DACCLK);
  2175. // /* DAC FIFO0 MIX to DAC enable and disable DAC FIFO1 MIX */
  2176. // dac_reg->digctl |= DAC_DIGCTL_DAF0M2DAEN;
  2177. // // dac_reg->digctl &= ~DAC_DIGCTL_DAF1M2DAEN;
  2178. // dac_reg->anactl0 &= ~DAC_ANACTL0_SEL_PLAN;
  2179. // dac_reg->anactl0 &= ~DAC_ANACTL0_SEL_CUR_MASK;
  2180. // dac_reg->anactl0 &= ~DAC_ANACTL0_PAVOL_MASK;
  2181. // dac_reg->anactl0 |= DAC_ANACTL0_PAVOL(0x7); //pavol 0db
  2182. // dac_reg->anactl0 |= DAC_ANACTL0_DFCEN; //enable dfc
  2183. // dac_reg->anactl0 |= DAC_ANACTL0_BIASEN; //enable bias
  2184. // //1.playback mute, sw1/dac ana/dac dig/pa en
  2185. // dac_reg->anactl1 &= ~(DAC_ANACTL1_DPBMLP | DAC_ANACTL1_DPBMLN);
  2186. // dac_reg->anactl0 |= (DAC_ANACTL0_SW1LP | DAC_ANACTL0_SW1LN);
  2187. // dac_reg->anactl0 |= (DAC_ANACTL0_DAENL | DAC_ANACTL0_DAENR); //en dac lr
  2188. // dac_reg->anactl0 |= (DAC_ANACTL0_DAINVENL | DAC_ANACTL0_DAINVENR); //en dac INV lr
  2189. // dac_reg->anactl0 |= (DAC_ANACTL0_PALPEN | DAC_ANACTL0_PALNEN);
  2190. // dac_reg->digctl |= DAC_DIGCTL_DDEN;
  2191. // //2.switch dacfifo0 input to cpu
  2192. // dac_fifoctl = dac_reg->fifoctl;
  2193. // //enable fifo0
  2194. // //disable drq irq
  2195. // //set to cpu
  2196. // dac_reg->fifoctl = 0x7301;
  2197. // //3.send max pcm data to fifo0 and delay2ms
  2198. // for (i = 0; i < 8; i++) {
  2199. // dac_reg->fifo0_dat = max_pcm;
  2200. // }
  2201. // if (!z_is_idle_thread_object(_current))
  2202. // k_sleep(K_MSEC(2));
  2203. // else
  2204. // k_busy_wait(2000UL);
  2205. // //4.loop2/atprcen enable
  2206. // dac_reg->anactl1 |= (DAC_ANACTL1_ATP2RCENL | DAC_ANACTL1_ATP2RCENR);
  2207. // dac_reg->anactl1 |= (DAC_ANACTL1_LP2LPEN | DAC_ANACTL1_LP2LNEN);
  2208. // dac_reg->anactl1 |= (DAC_ANACTL1_ATPRCEN_LP | DAC_ANACTL1_ATPRCEN_LN);
  2209. // // dac_dump_register(dev);
  2210. // //5.dac digital begain send max ramp-data untill min
  2211. // ramp_data = max_pcm;
  2212. // while(data_cnt > 0)
  2213. // {
  2214. // while( (dac_reg->pcm_buf_stat & PCM_BUF_STAT_PCMBS_MASK ) < 0x7fe );
  2215. // dac_reg->fifo0_dat = ramp_data; //effective bit is 20,L
  2216. // dac_reg->fifo0_dat = ramp_data; //R
  2217. // ramp_data -= ramp_step;
  2218. // if(data_cnt < 480)
  2219. // ramp_data = min_pcm;
  2220. // data_cnt--;
  2221. // }
  2222. // //6.after delay 200ms, enable pa outputstage
  2223. // //ramp_data += ramp_step; //The while structure upside minus ramp_data one more
  2224. // if (!z_is_idle_thread_object(_current))
  2225. // k_sleep(K_MSEC(200));
  2226. // else
  2227. // k_busy_wait(200000UL);
  2228. // dac_reg->anactl0 |= (DAC_ANACTL0_PALPOSEN | DAC_ANACTL0_PALNOSEN);
  2229. // if (!z_is_idle_thread_object(_current))
  2230. // k_sleep(K_MSEC(5));
  2231. // else
  2232. // k_busy_wait(5000UL);
  2233. // //7.after delay 2ms,disable loop2 and atprcen
  2234. // dac_reg->anactl1 &= ~(DAC_ANACTL1_LP2LPEN | DAC_ANACTL1_LP2LNEN);
  2235. // dac_reg->anactl1 &= ~(DAC_ANACTL1_ATPRCEN_LP | DAC_ANACTL1_ATPRCEN_LN);
  2236. // dac_reg->anactl1 &= ~(DAC_ANACTL1_ATP2RCENL | DAC_ANACTL1_ATP2RCENR);
  2237. // //8.dac digital send 0, after delay 2ms, star soft-mute to enable playback
  2238. // for (i = 0; i < 8; i++) {
  2239. // dac_reg->fifo0_dat = 0;
  2240. // }
  2241. // if (!z_is_idle_thread_object(_current))
  2242. // k_sleep(K_MSEC(2));
  2243. // else
  2244. // k_busy_wait(2000UL);
  2245. // sys_write32(sys_read32(CMU_DEVCLKEN1) | (1 << 15), CMU_DEVCLKEN1); //DACANACLKEN
  2246. // dac_reg->anactl1 &= ~(DAC_ANACTL1_SMCCKS_MASK);
  2247. // dac_reg->anactl1 |= DAC_ANACTL1_SMCCKS(0x1); //250hz
  2248. // dac_reg->anactl1 |= DAC_ANACTL1_SMCEN;
  2249. // dac_reg->anactl1 |= (DAC_ANACTL1_DPBMLP | DAC_ANACTL1_DPBMLN);
  2250. // if (!z_is_idle_thread_object(_current))
  2251. // k_sleep(K_MSEC(50));
  2252. // else
  2253. // k_busy_wait(50000UL);
  2254. // dac_reg->anactl1 &= ~(DAC_ANACTL1_SMCEN);
  2255. // dac_reg->fifoctl = dac_fifoctl;
  2256. // }
  2257. // static void dac_single_end_off_antipop(struct device *dev)
  2258. // {
  2259. // int ramp_data, max_pcm, min_pcm, data_cnt, i;
  2260. // uint32_t ramp_step;
  2261. // max_pcm = 0x7fffffff;//524287
  2262. // min_pcm = 0x80000001;//-524287
  2263. // data_cnt = 4800*3;//9600 samples each channel equals 200ms under 48kfs
  2264. // ramp_step = ((uint32_t)(0xffffffff)) / data_cnt;
  2265. // ramp_data = max_pcm;
  2266. // struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  2267. // uint32_t dac_fifoctl;
  2268. // /* enable DAC clock gate */
  2269. // acts_clock_peripheral_enable(CLOCK_ID_DAC);
  2270. // /* set sample rate 48K */
  2271. // dac_sample_rate_set(dev, SAMPLE_RATE_48KHZ);
  2272. // /* ldo enable */
  2273. // dac_ldo_power_control(dev, true);
  2274. // /* DAC FIFO0 MIX to DAC enable and disable DAC FIFO1 MIX */
  2275. // dac_reg->digctl |= DAC_DIGCTL_DAF0M2DAEN;
  2276. // // dac_reg->digctl &= ~DAC_DIGCTL_DAF1M2DAEN;
  2277. // //1.after delay 2ms,disable playback with soft-mute
  2278. // if (!z_is_idle_thread_object(_current))
  2279. // k_sleep(K_MSEC(2));
  2280. // else
  2281. // k_busy_wait(2000UL);
  2282. // sys_write32(sys_read32(CMU_DEVCLKEN1) | (1 << 15), CMU_DEVCLKEN1); //DACANACLKEN
  2283. // dac_reg->anactl1 &= ~(DAC_ANACTL1_SMCCKS_MASK);
  2284. // dac_reg->anactl1 |= DAC_ANACTL1_SMCCKS(0x2); //1khz
  2285. // dac_reg->anactl1 |= DAC_ANACTL1_SMCEN;
  2286. // if (!z_is_idle_thread_object(_current))
  2287. // k_sleep(K_MSEC(2));
  2288. // else
  2289. // k_busy_wait(2000UL);
  2290. // dac_reg->anactl1 &= ~(DAC_ANACTL1_DPBMLP | DAC_ANACTL1_DPBMLN);
  2291. // if (!z_is_idle_thread_object(_current))
  2292. // k_sleep(K_MSEC(30));
  2293. // else
  2294. // k_busy_wait(30000UL);
  2295. // dac_reg->digctl|= DAC_DIGCTL_DDEN;
  2296. // dac_fifoctl = dac_reg->fifoctl;
  2297. // /* set DACFIFO source from CPU */
  2298. // dac_reg->fifoctl = 0x7301;
  2299. // //3.disable paosen, enable bcdisch and atprcen
  2300. // dac_reg->anactl0 &= ~(DAC_ANACTL0_PALPOSEN | DAC_ANACTL0_PALNOSEN);
  2301. // dac_reg->anactl0 &= ~(DAC_ANACTL0_SW1LP | DAC_ANACTL0_SW1LN);
  2302. // for (i = 0; i < 10; i++) {
  2303. // dac_reg->fifo0_dat = max_pcm;
  2304. // }
  2305. // if (!z_is_idle_thread_object(_current))
  2306. // k_sleep(K_MSEC(5));
  2307. // else
  2308. // k_busy_wait(5000UL);
  2309. // dac_reg->anactl1 |= (DAC_ANACTL1_ATPRCEN_LP | DAC_ANACTL1_ATPRCEN_LN);
  2310. // dac_reg->anactl1 |= (DAC_ANACTL1_BCDISCH_LP | DAC_ANACTL1_BCDISCH_LN);
  2311. // if (!z_is_idle_thread_object(_current))
  2312. // k_sleep(K_MSEC(2));
  2313. // else
  2314. // k_busy_wait(2000UL);
  2315. // //4.dac digital begain send max ramp-data untill min
  2316. // //jump_cnt = 4800 * 1;
  2317. // //ramp_data = max_pcm - (ramp_step * jump_cnt);
  2318. // //data_cnt -= jump_cnt;
  2319. // //while( data_cnt > (4800 * 5 ))
  2320. // while(data_cnt > 0)
  2321. // {
  2322. // while( (dac_reg->pcm_buf_stat & PCM_BUF_STAT_PCMBS_MASK ) < 0x7fe ); //wait IRQ PD
  2323. // dac_reg->fifo0_dat = ramp_data; //effective bit is 20,L
  2324. // dac_reg->fifo0_dat = ramp_data; //R
  2325. // ramp_data -= ramp_step;
  2326. // data_cnt--;
  2327. // }
  2328. // //5.delay 100ms
  2329. // //ramp_data += ramp_step;
  2330. // if (!z_is_idle_thread_object(_current))
  2331. // k_sleep(K_MSEC(100));
  2332. // else
  2333. // k_busy_wait(100000UL);
  2334. // //6.disable pa,bcdisch,atprcen,sw1,dac ana,dac dig
  2335. // dac_reg->anactl0 &= ~(DAC_ANACTL0_PALPEN | DAC_ANACTL0_PALNEN);
  2336. // dac_reg->anactl0 &= ~(DAC_ANACTL0_DAENR | DAC_ANACTL0_DAENL);
  2337. // dac_reg->fifoctl = dac_fifoctl;
  2338. // dac_reg->anactl0 &= ~DAC_DIGCTL_DDEN;
  2339. // dac_reg->anactl0 &= ~DAC_ANACTL0_BIASEN;
  2340. // dac_reg->anactl1 &= ~(DAC_ANACTL1_BCDISCH_LP | DAC_ANACTL1_BCDISCH_LN);
  2341. // dac_reg->anactl1 &= ~(DAC_ANACTL1_ATPRCEN_LP | DAC_ANACTL1_ATPRCEN_LN);
  2342. // }
  2343. // #endif
  2344. // /* @brief DAC antipop process when system power up */
  2345. // static void dac_poweron_antipop_process(struct device *dev)
  2346. // {
  2347. // u32_t start_time;
  2348. // start_time = k_cycle_get_32();
  2349. // dac_single_end_on_antipop(dev);
  2350. // LOG_INF("poweron antipop process take %dus",
  2351. // k_cyc_to_us_floor32(k_cycle_get_32() - start_time));
  2352. // }
  2353. // /* @brief DAC antipop process when system power off */
  2354. // static void dac_poweroff_antipop_process(struct device *dev)
  2355. // {
  2356. // u32_t start_time;
  2357. // start_time = k_cycle_get_32();
  2358. // dac_single_end_off_antipop(dev);
  2359. // LOG_INF("poweroff antipop process take %dus",
  2360. // k_cyc_to_us_floor32(k_cycle_get_32() - start_time));
  2361. // }
  2362. // #endif
  2363. static int phy_dac_disable(struct device *dev, void *param);
  2364. static int phy_dac_ioctl(struct device *dev, uint32_t cmd, void *param)
  2365. {
  2366. const struct phy_dac_config_data *cfg = dev->config;
  2367. struct phy_dac_drv_data *data = dev->data;
  2368. int ret = 0;
  2369. switch (cmd) {
  2370. case PHY_CMD_DUMP_REGS:
  2371. {
  2372. dac_dump_register(dev);
  2373. break;
  2374. }
  2375. case AOUT_CMD_GET_SAMPLERATE:
  2376. {
  2377. *(audio_sr_sel_e *)param = dac_sample_rate_get(dev);
  2378. break;
  2379. }
  2380. case AOUT_CMD_SET_SAMPLERATE:
  2381. {
  2382. audio_sr_sel_e val = *(audio_sr_sel_e *)param;
  2383. ret = dac_sample_rate_set(dev, val);
  2384. if (ret) {
  2385. LOG_ERR("Failed to set DAC sample rate err=%d", ret);
  2386. return ret;
  2387. }
  2388. break;
  2389. }
  2390. case AOUT_CMD_OPEN_PA:
  2391. {
  2392. // #ifdef CONFIG_AUDIO_ANTIPOP_PROCESS
  2393. // dac_poweron_antipop_process(dev);
  2394. // #endif
  2395. #ifdef CONFIG_CFG_DRV
  2396. dac_external_pa_ctl((struct device *)dev, EXTERNAL_PA_ENABLE);
  2397. #endif
  2398. break;
  2399. }
  2400. case AOUT_CMD_CLOSE_PA:
  2401. {
  2402. // #ifdef CONFIG_AUDIO_ANTIPOP_PROCESS
  2403. // dac_poweroff_antipop_process(dev);
  2404. // #endif
  2405. ret = phy_dac_disable_pa(dev);
  2406. break;
  2407. }
  2408. case AOUT_CMD_OUT_MUTE:
  2409. {
  2410. uint8_t flag = *(uint8_t *)param;
  2411. if (flag)
  2412. __dac_mute_control(dev, true);
  2413. else
  2414. __dac_mute_control(dev, false);
  2415. break;
  2416. }
  2417. case AOUT_CMD_GET_VOLUME:
  2418. {
  2419. uint16_t level;
  2420. volume_setting_t *volume = (volume_setting_t *)param;
  2421. level = __dac_volume_get(dev, LEFT_CHANNEL_SEL);
  2422. volume->left_volume = dac_volume_level_to_db(level);
  2423. level = __dac_volume_get(dev, RIGHT_CHANNEL_SEL);
  2424. volume->right_volume = dac_volume_level_to_db(level);
  2425. LOG_INF("Get volume [%d, %d]", volume->left_volume, volume->right_volume);
  2426. break;
  2427. }
  2428. case AOUT_CMD_SET_VOLUME:
  2429. {
  2430. volume_setting_t *volume = (volume_setting_t *)param;
  2431. ret = dac_volume_set(dev, volume->left_volume,
  2432. volume->right_volume, data->sample_rate, 1);
  2433. if (ret) {
  2434. LOG_ERR("Volume set[%d, %d] error:%d",
  2435. volume->left_volume, volume->right_volume, ret);
  2436. return ret;
  2437. }
  2438. break;
  2439. }
  2440. case AOUT_CMD_GET_CHANNEL_STATUS:
  2441. {
  2442. uint8_t idx = *(uint8_t *)param;
  2443. if (DAC_FIFO_INVALID_INDEX(idx)) {
  2444. LOG_ERR("invalid fifo index %d", idx);
  2445. return -EINVAL;
  2446. }
  2447. if (__check_dac_fifo_error(dev, idx))
  2448. *(uint8_t *)param |= AUDIO_CHANNEL_STATUS_ERROR;
  2449. if (!__is_dac_fifo_empty(dev, idx))
  2450. *(uint8_t *)param |= AUDIO_CHANNEL_STATUS_BUSY;
  2451. *(uint8_t *)param = 0;
  2452. break;
  2453. }
  2454. case AOUT_CMD_GET_FIFO_LEN:
  2455. {
  2456. *(uint32_t *)param = DAC_PCMBUF_MAX_CNT;
  2457. break;
  2458. }
  2459. case AOUT_CMD_GET_FIFO_AVAILABLE_LEN:
  2460. {
  2461. *(uint32_t *)param = __get_pcmbuf_avail_length(dev);
  2462. break;
  2463. }
  2464. case AOUT_CMD_GET_APS:
  2465. {
  2466. ret = dac_get_pll_aps(dev);
  2467. if (ret < 0) {
  2468. LOG_ERR("Failed to get audio pll APS err=%d", ret);
  2469. return ret;
  2470. }
  2471. *(audio_aps_level_e *)param = (audio_aps_level_e)ret;
  2472. ret = 0;
  2473. break;
  2474. }
  2475. case AOUT_CMD_SET_APS:
  2476. {
  2477. audio_aps_level_e level = *(audio_aps_level_e *)param;
  2478. ret = dac_set_pll_aps(dev, level);
  2479. if (ret) {
  2480. LOG_ERR("Failed to set audio pll APS err=%d", ret);
  2481. return ret;
  2482. }
  2483. LOG_DBG("set new aps level %d", level);
  2484. break;
  2485. }
  2486. case PHY_CMD_FIFO_GET:
  2487. {
  2488. aout_param_t *out_param = (aout_param_t *)param;
  2489. uint8_t fifo_type;
  2490. bool fifo_mix_en = true;
  2491. if (!out_param)
  2492. return -EINVAL;
  2493. fifo_type = out_param->outfifo_type;
  2494. if ((fifo_type != AOUT_FIFO_DAC0)
  2495. && (fifo_type != AOUT_FIFO_DAC1)
  2496. && (fifo_type != AOUT_FIFO_DAC1_ONLY_SPDIF)) {
  2497. LOG_ERR("Invalid FIFO type %d", fifo_type);
  2498. return -EINVAL;
  2499. }
  2500. if ((fifo_type == AOUT_FIFO_DAC1)
  2501. && !__is_dac_fifo_working(dev, DAC_FIFO_0)) {
  2502. LOG_ERR("DAC FIFO1 depends on DAC FIFO0 enabled");
  2503. return -EPERM;
  2504. }
  2505. if (fifo_type == AOUT_FIFO_DAC1_ONLY_SPDIF) {
  2506. fifo_type = AOUT_FIFO_DAC1;
  2507. fifo_mix_en = false;
  2508. }
  2509. if (__is_dac_fifo_working(dev, fifo_type)) {
  2510. LOG_ERR("DAC FIFO(%d) now is using", out_param->outfifo_type);
  2511. return -EBUSY;
  2512. }
  2513. /* reset dac module */
  2514. if (fifo_type == AOUT_FIFO_DAC0)
  2515. acts_reset_peripheral(cfg->rst_id);
  2516. /* enable dac clock */
  2517. acts_clock_peripheral_enable(cfg->clk_id);
  2518. __dac_fifo_enable(dev, FIFO_SEL_DMA,
  2519. (out_param->channel_width == CHANNEL_WIDTH_16BITS)
  2520. ? DMA_WIDTH_16BITS : DMA_WIDTH_32BITS,
  2521. DAC_FIFO_DRQ_LEVEL_DEFAULT,
  2522. DAC_FIFO_VOL_LEVEL_DEFAULT,
  2523. fifo_type, fifo_mix_en);
  2524. if ((AOUT_FIFO_DAC0 == out_param->outfifo_type) ||
  2525. (AOUT_FIFO_DAC1 == out_param->outfifo_type)) {
  2526. __dac_pcmbuf_config(dev);
  2527. data->ch[fifo_type].fifo_cnt = 0;
  2528. data->ch[fifo_type].fifo_cnt_timestamp = 0;
  2529. /* Record the PCM BUF data callback */
  2530. data->ch[fifo_type].callback = out_param->callback;
  2531. data->ch[fifo_type].cb_data = out_param->cb_data;
  2532. LOG_DBG("Enable PCMBUF callback:%p", data->ch[fifo_type].callback);
  2533. if (AOUT_FIFO_DAC0 == out_param->outfifo_type)
  2534. data->ch_fifo0_start = 1;
  2535. else
  2536. data->ch_fifo1_start = 1;
  2537. }
  2538. dac_setting_t *dac_setting = out_param->dac_setting;
  2539. if (dac_setting) {
  2540. if (dac_setting->channel_mode == MONO_MODE)
  2541. __dac_digital_enable_mono(dev);
  2542. ret = dac_volume_set(dev, dac_setting->volume.left_volume,
  2543. dac_setting->volume.right_volume, out_param->sample_rate, 1);
  2544. }
  2545. break;
  2546. }
  2547. case PHY_CMD_FIFO_PUT:
  2548. {
  2549. uint8_t idx = *(uint8_t *)param;
  2550. if (idx == AOUT_FIFO_DAC1_ONLY_SPDIF)
  2551. idx = AOUT_FIFO_DAC1;
  2552. if (__is_dac_fifo_working(dev, idx)) {
  2553. dac_wait_fifo_empty(dev, (a_dac_fifo_e)idx,
  2554. DAC_WAIT_FIFO_EMPTY_TIMEOUT_MS);
  2555. __dac_fifo_disable(dev, idx);
  2556. __dac_digital_disable_fifo(dev, idx);
  2557. if (AOUT_FIFO_DAC0 == idx)
  2558. data->ch_fifo0_start = 0;
  2559. else
  2560. data->ch_fifo1_start = 0;
  2561. }
  2562. break;
  2563. }
  2564. case PHY_CMD_DAC_FIFO_GET_SAMPLE_CNT:
  2565. {
  2566. uint32_t val;
  2567. uint32_t idx = *(uint32_t *)param;
  2568. if (DAC_FIFO_INVALID_INDEX(idx)) {
  2569. LOG_ERR("invalid fifo index %d", idx);
  2570. return -EINVAL;
  2571. }
  2572. val = __dac_read_fifo_counter(dev, idx);
  2573. if (AOUT_FIFO_DAC0 == idx)
  2574. *(uint32_t *)param = val + data->ch[0].fifo_cnt;
  2575. else
  2576. *(uint32_t *)param = val + data->ch[1].fifo_cnt;
  2577. LOG_DBG("DAC FIFO counter: %d", *(uint32_t *)param);
  2578. break;
  2579. }
  2580. case PHY_CMD_DAC_FIFO_RESET_SAMPLE_CNT:
  2581. {
  2582. uint8_t idx = *(uint8_t *)param;
  2583. if (DAC_FIFO_INVALID_INDEX(idx)) {
  2584. LOG_ERR("invalid fifo index %d", idx);
  2585. return -EINVAL;
  2586. }
  2587. uint32_t key = irq_lock();
  2588. __dac_fifo_counter_reset(dev, idx);
  2589. if (AOUT_FIFO_DAC0 == idx) {
  2590. data->ch[0].fifo_cnt = 0;
  2591. data->ch[0].fifo_cnt_timestamp = 0;
  2592. } else {
  2593. data->ch[1].fifo_cnt = 0;
  2594. data->ch[1].fifo_cnt_timestamp = 0;
  2595. }
  2596. irq_unlock(key);
  2597. break;
  2598. }
  2599. case PHY_CMD_DAC_FIFO_ENABLE_SAMPLE_CNT:
  2600. {
  2601. uint8_t idx = *(uint8_t *)param;
  2602. if (DAC_FIFO_INVALID_INDEX(idx)) {
  2603. LOG_ERR("invalid fifo index %d", idx);
  2604. return -EINVAL;
  2605. }
  2606. uint32_t key = irq_lock();
  2607. __dac_fifo_counter_enable(dev, idx);
  2608. irq_unlock(key);
  2609. break;
  2610. }
  2611. case PHY_CMD_DAC_FIFO_DISABLE_SAMPLE_CNT:
  2612. {
  2613. uint8_t idx = *(uint8_t *)param;
  2614. if (DAC_FIFO_INVALID_INDEX(idx)) {
  2615. LOG_ERR("invalid fifo index %d", idx);
  2616. return -EINVAL;
  2617. }
  2618. uint32_t key = irq_lock();
  2619. __dac_fifo_counter_disable(dev, idx);
  2620. if (AOUT_FIFO_DAC0 == idx) {
  2621. data->ch[0].fifo_cnt = 0;
  2622. data->ch[0].fifo_cnt_timestamp = 0;
  2623. } else {
  2624. data->ch[1].fifo_cnt = 0;
  2625. data->ch[1].fifo_cnt_timestamp = 0;
  2626. }
  2627. irq_unlock(key);
  2628. break;
  2629. }
  2630. // case PHY_CMD_DAC_FIFO_VOLUME_GET:
  2631. // {
  2632. // uint32_t fifo_cmd = *(uint32_t *)param;
  2633. // uint8_t fifo_idx = PHY_GET_FIFO_CMD_INDEX(fifo_cmd);
  2634. // ret = __dac_fifo_volume_get(dev, fifo_idx);
  2635. // if (ret < 0) {
  2636. // LOG_ERR("Get FIFO(%d) volume error", fifo_idx);
  2637. // return ret;
  2638. // }
  2639. // *(uint32_t *)param = PHY_FIFO_CMD(fifo_idx, ret);
  2640. // ret = 0;
  2641. // break;
  2642. // }
  2643. // case PHY_CMD_DAC_FIFO_VOLUME_SET:
  2644. // {
  2645. // uint32_t fifo_cmd = *(uint32_t *)param;
  2646. // uint8_t fifo_idx = PHY_GET_FIFO_CMD_INDEX(fifo_cmd);
  2647. // uint8_t volume = PHY_GET_FIFO_CMD_VAL(fifo_cmd);
  2648. // ret = __dac_fifo_volume_set(dev, fifo_idx, volume);
  2649. // break;
  2650. // }
  2651. case PHY_CMD_FIFO_DRQ_LEVEL_GET:
  2652. {
  2653. uint32_t fifo_cmd = *(uint32_t *)param;
  2654. uint8_t fifo_idx = PHY_GET_FIFO_CMD_INDEX(fifo_cmd);
  2655. ret = __dac_fifo_drq_level_get(dev, fifo_idx);
  2656. if (ret < 0) {
  2657. LOG_ERR("Get FIFO(%d) drq level error", fifo_idx);
  2658. return ret;
  2659. }
  2660. *(uint32_t *)param = PHY_FIFO_CMD(fifo_idx, ret);
  2661. ret = 0;
  2662. break;
  2663. }
  2664. case PHY_CMD_FIFO_DRQ_LEVEL_SET:
  2665. {
  2666. uint32_t fifo_cmd = *(uint32_t *)param;
  2667. uint8_t fifo_idx = PHY_GET_FIFO_CMD_INDEX(fifo_cmd);
  2668. uint8_t level = PHY_GET_FIFO_CMD_VAL(fifo_cmd);
  2669. ret = __dac_fifo_drq_level_set(dev, fifo_idx, level);
  2670. break;
  2671. }
  2672. case PHY_CMD_DAC_WAIT_EMPTY:
  2673. {
  2674. uint8_t fifo_idx = *(uint8_t *)param;
  2675. __wait_dac_fifo_empty(dev, fifo_idx);
  2676. break;
  2677. }
  2678. // case PHY_CMD_CLAIM_WITH_128FS:
  2679. // {
  2680. // acts_clock_peripheral_enable(cfg->clk_id);
  2681. // __dac_digital_claim_128fs(dev, true);
  2682. // break;
  2683. // }
  2684. // case PHY_CMD_CLAIM_WITHOUT_128FS:
  2685. // {
  2686. // __dac_digital_claim_128fs(dev, false);
  2687. // break;
  2688. // }
  2689. case PHY_CMD_GET_AOUT_DMA_INFO:
  2690. {
  2691. ret = dac_get_dma_info(dev, (struct audio_out_dma_info *)param);
  2692. break;
  2693. }
  2694. case AOUT_CMD_SET_DAC_THRESHOLD:
  2695. {
  2696. dac_threshold_setting_t *thres = (dac_threshold_setting_t *)param;
  2697. ret = __dac_pcmbuf_threshold_update(dev, thres);
  2698. break;
  2699. }
  2700. case AOUT_CMD_GET_DAC_SDM_SAMPLE_CNT:
  2701. {
  2702. uint32_t val = __dac_read_sdm_counter(dev);
  2703. *(uint32_t *)param = data->sdm_cnt + val;
  2704. break;
  2705. }
  2706. case AOUT_CMD_RESET_DAC_SDM_SAMPLE_CNT:
  2707. {
  2708. uint32_t key = irq_lock();
  2709. __dac_sdm_counter_reset(dev);
  2710. data->sdm_cnt = 0;
  2711. data->sdm_cnt_timestamp = 0;
  2712. irq_unlock(key);
  2713. break;
  2714. }
  2715. case AOUT_CMD_ENABLE_DAC_SDM_SAMPLE_CNT:
  2716. {
  2717. uint32_t key = irq_lock();
  2718. __dac_sdm_counter_enable(dev);
  2719. irq_unlock(key);
  2720. break;
  2721. }
  2722. case AOUT_CMD_DISABLE_DAC_SDM_SAMPLE_CNT:
  2723. {
  2724. uint32_t key = irq_lock();
  2725. __dac_sdm_counter_disable(dev);
  2726. data->sdm_cnt = 0;
  2727. data->sdm_cnt_timestamp = 0;
  2728. irq_unlock(key);
  2729. break;
  2730. }
  2731. case AOUT_CMD_GET_DAC_SDM_STABLE_SAMPLE_CNT:
  2732. {
  2733. *(uint32_t *)param = __dac_read_sdm_stable_counter(dev);
  2734. break;
  2735. }
  2736. case AOUT_CMD_SET_DAC_TRIGGER_SRC:
  2737. {
  2738. uint8_t src = *(uint8_t *)param;
  2739. ret = __dac_external_trigger_enable(dev, src);
  2740. break;
  2741. }
  2742. // case AOUT_CMD_SELECT_DAC_ENABLE_CHANNEL:
  2743. // {
  2744. // uint8_t lr_sel = *(uint8_t *)param;
  2745. // /* Select both left and right channels to enable */
  2746. // if (lr_sel == (LEFT_CHANNEL_SEL | RIGHT_CHANNEL_SEL)) {
  2747. // if (data->lr_sel != lr_sel) {
  2748. // LOG_ERR("DAC DTS lr sel:%d conflict", data->lr_sel);
  2749. // ret = -EPERM;
  2750. // }
  2751. // } else if (lr_sel == LEFT_CHANNEL_SEL) { /* Only select left channel to enable */
  2752. // if (data->lr_sel & RIGHT_CHANNEL_SEL)
  2753. // ret = __dac_analog_disable(dev, RIGHT_CHANNEL_SEL);
  2754. // } else if (lr_sel == RIGHT_CHANNEL_SEL) { /* Only select right channel to enable */
  2755. // if (data->lr_sel & LEFT_CHANNEL_SEL)
  2756. // ret = __dac_analog_disable(dev, LEFT_CHANNEL_SEL);
  2757. // } else {
  2758. // LOG_ERR("invalid lr sel:%d", lr_sel);
  2759. // ret = -EINVAL;
  2760. // }
  2761. // break;
  2762. // }
  2763. case AOUT_CMD_DAC_FORCE_START:
  2764. {
  2765. dac_ext_trigger_ctl_t *trigger_ctl = (dac_ext_trigger_ctl_t *)param;
  2766. __dac_digital_force_start(dev, trigger_ctl);
  2767. break;
  2768. }
  2769. case AOUT_CMD_EXTERNAL_PA_CONTROL:
  2770. {
  2771. #ifdef CONFIG_CFG_DRV
  2772. uint8_t ctrl_func = *(uint8_t *)param;
  2773. ret = dac_external_pa_ctl(dev, ctrl_func);
  2774. #else
  2775. ret = -ENOTSUP;
  2776. #endif
  2777. break;
  2778. }
  2779. case AOUT_CMD_SET_FIFO_SRC:
  2780. {
  2781. dac_fifosrc_setting_t *fifosrc = (dac_fifosrc_setting_t *)param;
  2782. ret = __dac_fifo_update_src(dev, fifosrc);
  2783. break;
  2784. }
  2785. case AOUT_CMD_DAC_TRIGGER_CONTROL:
  2786. {
  2787. dac_ext_trigger_ctl_t *trigger_ctl = (dac_ext_trigger_ctl_t *)param;
  2788. ret = __dac_external_trigger_control(dev, trigger_ctl);
  2789. break;
  2790. }
  2791. case AOUT_CMD_ANC_CONTROL:
  2792. {
  2793. dac_anc_ctl_t *anc_ctl = (dac_anc_ctl_t *)param;
  2794. if (!anc_ctl) {
  2795. LOG_ERR("invalid anc ctl");
  2796. return -EINVAL;
  2797. }
  2798. if (anc_ctl->is_open_anc) {
  2799. if (data->is_anc_enable) {
  2800. LOG_ERR("DAC ANC already enabled");
  2801. return -EACCES;
  2802. }
  2803. data->is_anc_enable = 1;
  2804. /* check if DAC session has been opened normally */
  2805. uint32_t key = irq_lock();
  2806. if (!__dac_is_digital_working(dev)) {
  2807. irq_unlock(key);
  2808. aout_param_t aout_setting = {0};
  2809. dac_setting_t dac_setting = {0};
  2810. aout_setting.sample_rate = SAMPLE_RATE_48KHZ;
  2811. aout_setting.channel_type = AUDIO_CHANNEL_DAC;
  2812. aout_setting.channel_width = CHANNEL_WIDTH_16BITS;
  2813. aout_setting.outfifo_type = AOUT_FIFO_DAC0;
  2814. dac_setting.channel_mode = STEREO_MODE;
  2815. aout_setting.dac_setting = &dac_setting;
  2816. ret = phy_dac_enable(dev, &aout_setting);
  2817. } else {
  2818. atomic_inc(&data->refcount);
  2819. irq_unlock(key);
  2820. }
  2821. LOG_INF("Enable ANC<=>DAC");
  2822. } else {
  2823. if (!data->is_anc_enable) {
  2824. LOG_ERR("DAC does not enable yet");
  2825. return -EACCES;
  2826. }
  2827. data->is_anc_enable = 0;
  2828. uint8_t fifo_idx = AOUT_FIFO_DAC0;
  2829. ret = phy_dac_disable(dev, &fifo_idx);
  2830. LOG_INF("Disable ANC<=>DAC");
  2831. }
  2832. break;
  2833. }
  2834. default:
  2835. LOG_ERR("Unsupport command %d", cmd);
  2836. ret = -ENOTSUP;
  2837. }
  2838. return ret;
  2839. }
  2840. static int phy_dac_disable(struct device *dev, void *param)
  2841. {
  2842. const struct phy_dac_config_data *cfg = dev->config;
  2843. struct phy_dac_drv_data *data = dev->data;
  2844. uint8_t fifo_idx = *(uint8_t *)param;
  2845. if ((fifo_idx != AOUT_FIFO_DAC0) && (fifo_idx != AOUT_FIFO_DAC1)) {
  2846. LOG_ERR("Invalid FIFO index %d", fifo_idx);
  2847. return -EINVAL;
  2848. }
  2849. uint32_t key = irq_lock();
  2850. /* set channel stop flag and DAC FIFO0 is the main control channel */
  2851. if (fifo_idx == AOUT_FIFO_DAC0)
  2852. data->ch_fifo0_start = 0;
  2853. else if (fifo_idx == AOUT_FIFO_DAC1)
  2854. data->ch_fifo1_start = 0;
  2855. atomic_dec(&data->refcount);
  2856. if (atomic_get(&data->refcount) != 1) {
  2857. LOG_INF("DAC disable refcount:%d", data->refcount);
  2858. irq_unlock(key);
  2859. return 0;
  2860. }
  2861. irq_unlock(key);
  2862. irq_disable(IRQ_ID_DAC);
  2863. irq_disable(IRQ_ID_DACFIFO);
  2864. /* Timeout to wait DAC FIFO empty */
  2865. if ((fifo_idx == AOUT_FIFO_DAC0) && __dac_is_digital_working(dev))
  2866. dac_wait_fifo_empty(dev, fifo_idx, DAC_WAIT_FIFO_EMPTY_TIMEOUT_MS);
  2867. if (AOUT_FIFO_DAC0 == fifo_idx) {
  2868. __dac_digital_disable_fifo(dev, DAC_FIFO_0);
  2869. __dac_fifo_disable(dev, DAC_FIFO_0);
  2870. __dac_fifo_counter_disable(dev, DAC_FIFO_0);
  2871. memset(&data->ch[0], 0, sizeof(struct phy_dac_channel));
  2872. } else {
  2873. __dac_digital_disable_fifo(dev, DAC_FIFO_1);
  2874. __dac_fifo_disable(dev, DAC_FIFO_1);
  2875. __dac_fifo_counter_disable(dev, DAC_FIFO_1);
  2876. memset(&data->ch[1], 0, sizeof(struct phy_dac_channel));
  2877. }
  2878. /* check if all dac fifos are free */
  2879. if (__is_dac_fifo_all_free(dev, true)) {
  2880. data->sample_rate = 0;
  2881. __dac_external_trigger_disable(dev);
  2882. __dac_sdm_counter_disable(dev);
  2883. #ifdef CONFIG_CFG_DRV
  2884. if (data->external_config.Keep_DA_Enabled_When_Play_Pause)
  2885. __dac_mute_control(dev, true);
  2886. else
  2887. __dac_analog_disable(dev, LEFT_CHANNEL_SEL | RIGHT_CHANNEL_SEL);
  2888. #else
  2889. #if (CONFIG_AUDIO_DAC_POWER_PREFERRED == 1)
  2890. struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  2891. dac_reg->anactl0 = 0;
  2892. dac_reg->anactl1 = 0;
  2893. dac_reg->anactl2 = 0;
  2894. dac_ldo_power_control(dev, false);
  2895. acts_clock_peripheral_disable(cfg->clk_id);
  2896. acts_clock_peripheral_disable(CLOCK_ID_DACANACLK);
  2897. struct device *adc_dev = (struct device *)device_get_binding(CONFIG_AUDIO_ADC_0_NAME);
  2898. uint8_t is_busy;
  2899. if (adc_dev) {
  2900. phy_audio_control(adc_dev, PHY_CMD_IS_ADC_BUSY, &is_busy);
  2901. if (!is_busy)
  2902. {
  2903. adc_reset_control(false);
  2904. acts_clock_peripheral_disable(CLOCK_ID_ADC);
  2905. }
  2906. }
  2907. audio_pll_unset(data->audio_pll_index);
  2908. #else
  2909. __dac_mute_control(dev, true);
  2910. #endif
  2911. #endif
  2912. __dac_digital_disable(dev);
  2913. }
  2914. acts_reset_peripheral_assert(cfg->rst_id);
  2915. soc_powergate_set(POWERGATE_DSP_AU_PG_DEV, false);
  2916. return 0;
  2917. }
  2918. const struct phy_audio_driver_api phy_dac_drv_api = {
  2919. .audio_enable = phy_dac_enable,
  2920. .audio_disable = phy_dac_disable,
  2921. .audio_ioctl = phy_dac_ioctl
  2922. };
  2923. /* dump dac device tree infomation */
  2924. static void __dac_dt_dump_info(const struct phy_dac_config_data *cfg)
  2925. {
  2926. #if (PHY_DEV_SHOW_DT_INFO == 1)
  2927. printk("** DAC BASIC INFO **\n");
  2928. printk(" BASE: %08x\n", cfg->reg_base);
  2929. printk(" CLK-ID: %08x\n", cfg->clk_id);
  2930. printk(" RST-ID: %08x\n", cfg->rst_id);
  2931. printk("DMA0-NAME: %s\n", cfg->dma_fifo0.dma_dev_name);
  2932. printk(" DMA0-ID: %08x\n", cfg->dma_fifo0.dma_id);
  2933. printk(" DMA0-CH: %08x\n", cfg->dma_fifo0.dma_chan);
  2934. printk("DMA1-NAME: %s\n", cfg->dma_fifo1.dma_dev_name);
  2935. printk(" DMA1-ID: %08x\n", cfg->dma_fifo1.dma_id);
  2936. printk(" DMA1-CH: %08x\n", cfg->dma_fifo1.dma_chan);
  2937. printk("** DAC FEATURES **\n");
  2938. printk(" LAYOUT: %d\n", PHY_DEV_FEATURE(layout));
  2939. printk(" LR-MIX: %d\n", PHY_DEV_FEATURE(dac_lr_mix));
  2940. printk(" SDM: %d\n", PHY_DEV_FEATURE(noise_detect_mute));
  2941. printk(" AUTOMUTE: %d\n", PHY_DEV_FEATURE(automute));
  2942. printk(" LOOPBACK: %d\n", PHY_DEV_FEATURE(loopback));
  2943. printk(" LEFT-MUTE: %d\n", PHY_DEV_FEATURE(left_mute));
  2944. printk("RIGHT-MUTE: %d\n", PHY_DEV_FEATURE(right_mute));
  2945. printk(" AM-IRQ: %d\n", PHY_DEV_FEATURE(am_irq));
  2946. #endif
  2947. }
  2948. /** @brief DAC digital IRQ routine
  2949. * DAC digital IRQ source as below:
  2950. * - PCMBUF full IRQ/PD
  2951. * - PCMBUF half full IRQ/PD
  2952. * - PCMBUF half empty IRQ/PD
  2953. * - PCMBUF empty IRQ/PD
  2954. * - DACFIFO0 half empty IRQ/PD
  2955. */
  2956. static void phy_dac_fifo_isr(const void *arg)
  2957. {
  2958. struct device *dev = (struct device *)arg;
  2959. struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  2960. struct phy_dac_drv_data *data = dev->data;
  2961. uint32_t stat, pending = 0;
  2962. //audio_debug_trace_start();
  2963. stat = dac_reg->pcm_buf_stat;
  2964. // LOG_DBG("pcmbuf ctl:0x%x stat:0x%x", dac_reg->pcm_buf_ctl, stat);
  2965. /* PCMBUF empty IRQ pending */
  2966. if ((stat & PCM_BUF_STAT_PCMBEIP)
  2967. && (dac_reg->pcm_buf_ctl & PCM_BUF_CTL_PCMBEPIE)) {
  2968. pending |= AOUT_DMA_IRQ_TC;
  2969. dac_reg->pcm_buf_ctl &= ~PCM_BUF_CTL_PCMBEPIE;
  2970. }
  2971. /* PCMBUF half empty IRQ pending */
  2972. if (stat & PCM_BUF_STAT_PCMBHEIP) {
  2973. pending |= AOUT_DMA_IRQ_HF;
  2974. /* Wait until there is half empty irq happen and then start to detect empty irq */
  2975. if (!(dac_reg->pcm_buf_ctl & PCM_BUF_CTL_PCMBEPIE))
  2976. dac_reg->pcm_buf_ctl |= PCM_BUF_CTL_PCMBEPIE;
  2977. }
  2978. if (stat & PCM_BUF_STAT_IRQ_MASK)
  2979. dac_reg->pcm_buf_stat = stat;
  2980. if(dac_reg->stat & DAC_STAT_DAF0EIP)
  2981. dac_reg->stat |= DAC_STAT_DAF0EIP;
  2982. if (pending) {
  2983. if ((dac_reg->digctl & DAC_DIGCTL_DAF0M2DAEN)
  2984. && data->ch[0].callback && data->ch_fifo0_start) {
  2985. data->ch[0].callback(data->ch[0].cb_data, pending);
  2986. }
  2987. }
  2988. //audio_debug_trace_end();
  2989. }
  2990. /** @brief DAC FIFO IRQ routine
  2991. * DAC FIFO IRQ source as below:
  2992. * - VOLL set IRQ/PD
  2993. * - VOLR IRQ/PD
  2994. * - PCMBUF CNT OF IRQ/PD
  2995. * - SDM_SAMPLES CNT OF IRQ/PD
  2996. * - AUTO_MUTE_CTL[0].AMEN IRQ/PD
  2997. * - DAC_DIGCTL[30].SMC IRQ/PD
  2998. * - DAC_DIGCTL_OVDT_PD
  2999. */
  3000. static void phy_dac_digital_isr(const void *arg)
  3001. {
  3002. struct device *dev = (struct device *)arg;
  3003. struct acts_audio_dac *dac_reg = get_dac_reg_base(dev);
  3004. struct phy_dac_drv_data *data = dev->data;
  3005. uint32_t timestamp;
  3006. LOG_DBG("pcmbuf_cnt:0x%x sdm_cnt 0x%x",
  3007. dac_reg->pcm_buf_cnt, dac_reg->sdm_samples_cnt);
  3008. /* DAC auto mute detect pending */
  3009. if (dac_reg->auto_mute_ctl & AUTO_MUTE_CTL_AMPD_OUT)
  3010. dac_reg->auto_mute_ctl |= AUTO_MUTE_CTL_AMPD_OUT;
  3011. if (dac_reg->auto_mute_ctl & AUTO_MUTE_CTL_AMPD_IN)
  3012. dac_reg->auto_mute_ctl |= AUTO_MUTE_CTL_AMPD_IN;
  3013. /* PA over load LOAD pending */
  3014. if (dac_reg->digctl & DAC_DIGCTL_OVDT_PD)
  3015. dac_reg->digctl |= DAC_DIGCTL_OVDT_PD;
  3016. /* PCMBUF sample counter overflow irq pending */
  3017. if (dac_reg->pcm_buf_cnt & PCM_BUF_CNT_IP) {
  3018. data->ch[0].fifo_cnt += (AOUT_FIFO_CNT_MAX + 1);
  3019. dac_reg->pcm_buf_cnt |= PCM_BUF_CNT_IP;
  3020. if (data->ch[0].fifo_cnt_timestamp) {
  3021. if (k_cyc_to_us_floor32(k_cycle_get_32() - data->ch[0].fifo_cnt_timestamp)
  3022. < DAC_FIFO_CNT_MAX_SAME_SAMPLES_TIME_US) {
  3023. __dac_fifo_counter_reset(dev, DAC_FIFO_0);
  3024. data->ch[0].fifo_cnt_timestamp = 0;
  3025. }
  3026. }
  3027. data->ch[0].fifo_cnt_timestamp = k_cycle_get_32();
  3028. timestamp = k_cycle_get_32();
  3029. while (dac_reg->pcm_buf_cnt & PCM_BUF_CNT_IP) {
  3030. dac_reg->pcm_buf_cnt |= PCM_BUF_CNT_IP;
  3031. if (k_cyc_to_us_floor32(k_cycle_get_32() - timestamp)
  3032. > DAC_FIFO_CNT_CLEAR_PENDING_TIME_US) {
  3033. LOG_ERR("failed to clear DAC FIFO0 PD:0x%x", dac_reg->pcm_buf_cnt);
  3034. __dac_fifo_counter_reset(dev, DAC_FIFO_0);
  3035. break;
  3036. }
  3037. }
  3038. }
  3039. /* SDM sample counter overflow irq pending */
  3040. if (dac_reg->sdm_samples_cnt & SDM_SAMPLES_CNT_IP) {
  3041. data->sdm_cnt += (AOUT_SDM_CNT_MAX + 1);
  3042. dac_reg->sdm_samples_cnt |= SDM_SAMPLES_CNT_IP;
  3043. timestamp = k_cycle_get_32();
  3044. while (dac_reg->sdm_samples_cnt & SDM_SAMPLES_CNT_IP) {
  3045. dac_reg->sdm_samples_cnt |= SDM_SAMPLES_CNT_IP;
  3046. if (k_cyc_to_us_floor32(k_cycle_get_32() - timestamp
  3047. > DAC_FIFO_CNT_CLEAR_PENDING_TIME_US)) {
  3048. LOG_ERR("failed to clear SDM CNT:0x%x", dac_reg->sdm_samples_cnt);
  3049. __dac_sdm_counter_reset(dev);
  3050. break;
  3051. }
  3052. }
  3053. if (data->sdm_cnt_timestamp) {
  3054. if (k_cyc_to_us_floor32(k_cycle_get_32() - data->sdm_cnt_timestamp)
  3055. < DAC_FIFO_CNT_MAX_SAME_SAMPLES_TIME_US) {
  3056. __dac_sdm_counter_reset(dev);
  3057. data->sdm_cnt_timestamp = 0;
  3058. }
  3059. }
  3060. data->sdm_cnt_timestamp = k_cycle_get_32();
  3061. }
  3062. }
  3063. #ifdef CONFIG_CFG_DRV
  3064. /* @brief initialize DAC external configuration */
  3065. static int phy_dac_config_init(const struct device *dev)
  3066. {
  3067. struct phy_dac_drv_data *data = dev->data;
  3068. int ret;
  3069. uint8_t i;
  3070. /* CFG_Struct_Audio_Settings */
  3071. PHY_AUDIO_CFG(data->external_config, ITEM_AUDIO_OUT_MODE, Out_Mode);
  3072. PHY_AUDIO_CFG(data->external_config, ITEM_AUDIO_DAC_BIAS_SETTING, DAC_Bias_Setting);
  3073. PHY_AUDIO_CFG(data->external_config, ITEM_AUDIO_KEEP_DA_ENABLED_WHEN_PLAY_PAUSE, Keep_DA_Enabled_When_Play_Pause);
  3074. PHY_AUDIO_CFG(data->external_config, ITEM_AUDIO_ANTIPOP_PROCESS_DISABLE, AntiPOP_Process_Disable);
  3075. /* external PA pins */
  3076. ret = cfg_get_by_key(ITEM_AUDIO_EXTERN_PA_CONTROL,
  3077. &data->external_config.Extern_PA_Control, sizeof(data->external_config.Extern_PA_Control));
  3078. if (ret) {
  3079. for (i = 0; i < ARRAY_SIZE(data->external_config.Extern_PA_Control); i++) {
  3080. LOG_INF("** External PA Pin@%d Info **", i);
  3081. LOG_INF("PA_Function:%d", data->external_config.Extern_PA_Control[i].PA_Function);
  3082. LOG_INF("GPIO_Pin:%d", data->external_config.Extern_PA_Control[i].GPIO_Pin);
  3083. LOG_INF("Pull_Up_Down:%d", data->external_config.Extern_PA_Control[i].Pull_Up_Down);
  3084. LOG_INF("Active_Level:%d", data->external_config.Extern_PA_Control[i].Active_Level);
  3085. }
  3086. dac_external_pa_ctl((struct device *)dev, EXTERNAL_PA_ENABLE);
  3087. }
  3088. return 0;
  3089. }
  3090. #endif
  3091. /* physical dac initialization */
  3092. static int phy_dac_init(const struct device *dev)
  3093. {
  3094. const struct phy_dac_config_data *cfg = dev->config;
  3095. struct phy_dac_drv_data *data = dev->data;
  3096. __dac_dt_dump_info(cfg);
  3097. memset(data, 0, sizeof(struct phy_dac_drv_data));
  3098. atomic_set(&data->refcount, 1);
  3099. #ifdef CONFIG_CFG_DRV
  3100. int ret;
  3101. ret = phy_dac_config_init(dev);
  3102. if (ret)
  3103. LOG_ERR("DAC external config init error:%d", ret);
  3104. #endif
  3105. if (cfg->irq_config)
  3106. cfg->irq_config();
  3107. printk("DAC init successfully\n");
  3108. return 0;
  3109. }
  3110. static void phy_dac_irq_config(void);
  3111. /* physical dac driver data */
  3112. static struct phy_dac_drv_data phy_dac_drv_data0;
  3113. /* physical dac config data */
  3114. static const struct phy_dac_config_data phy_dac_config_data0 = {
  3115. .reg_base = AUDIO_DAC_REG_BASE,
  3116. AUDIO_DMA_FIFO_DEF(DAC, 0),
  3117. AUDIO_DMA_FIFO_DEF(DAC, 1),
  3118. .clk_id = CLOCK_ID_DAC,
  3119. .rst_id = RESET_ID_DAC,
  3120. .irq_config = phy_dac_irq_config,
  3121. PHY_DEV_FEATURE_DEF(layout) = CONFIG_AUDIO_DAC_0_LAYOUT,
  3122. PHY_DEV_FEATURE_DEF(dac_lr_mix) = CONFIG_AUDIO_DAC_0_LR_MIX,
  3123. PHY_DEV_FEATURE_DEF(noise_detect_mute) = CONFIG_AUDIO_DAC_0_NOISE_DETECT_MUTE,
  3124. PHY_DEV_FEATURE_DEF(automute) = CONFIG_AUDIO_DAC_0_AUTOMUTE,
  3125. PHY_DEV_FEATURE_DEF(loopback) = CONFIG_AUDIO_DAC_0_LOOPBACK,
  3126. PHY_DEV_FEATURE_DEF(left_mute) = CONFIG_AUDIO_DAC_0_LEFT_MUTE,
  3127. PHY_DEV_FEATURE_DEF(right_mute) = CONFIG_AUDIO_DAC_0_RIGHT_MUTE,
  3128. PHY_DEV_FEATURE_DEF(pa_vol) = CONFIG_AUDIO_DAC_0_PA_VOL,
  3129. PHY_DEV_FEATURE_DEF(am_irq) = CONFIG_AUDIO_DAC_0_AM_IRQ,
  3130. };
  3131. #if IS_ENABLED(CONFIG_AUDIO_DAC_0)
  3132. DEVICE_DEFINE(dac0, CONFIG_AUDIO_DAC_0_NAME, phy_dac_init, NULL,
  3133. &phy_dac_drv_data0, &phy_dac_config_data0,
  3134. POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_OBJECTS, &phy_dac_drv_api);
  3135. #endif
  3136. static void phy_dac_irq_config(void)
  3137. {
  3138. /* Connect and enable DAC digital IRQ */
  3139. IRQ_CONNECT(IRQ_ID_DAC, CONFIG_AUDIO_DAC_0_IRQ_PRI,
  3140. phy_dac_digital_isr,
  3141. DEVICE_GET(dac0), 0);
  3142. /* Connect and enable DAC FIFO IRQ */
  3143. IRQ_CONNECT(IRQ_ID_DACFIFO, CONFIG_AUDIO_DAC_0_IRQ_PRI,
  3144. phy_dac_fifo_isr,
  3145. DEVICE_GET(dac0), 0);
  3146. }