q6adm.c 159 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283
  1. /* Copyright (c) 2012-2017, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/slab.h>
  14. #include <linux/wait.h>
  15. #include <linux/sched.h>
  16. #include <linux/jiffies.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/atomic.h>
  19. #include <linux/wait.h>
  20. #include <sound/asound.h>
  21. #include <dsp/msm-dts-srs-tm-config.h>
  22. #include <dsp/apr_audio-v2.h>
  23. #include <dsp/q6adm-v2.h>
  24. #include <dsp/q6audio-v2.h>
  25. #include <dsp/q6afe-v2.h>
  26. #include <dsp/audio_cal_utils.h>
  27. #include <ipc/apr.h>
  28. #include "adsp_err.h"
  29. #define TIMEOUT_MS 1000
  30. #define RESET_COPP_ID 99
  31. #define INVALID_COPP_ID 0xFF
  32. /* Used for inband payload copy, max size is 4k */
  33. /* 2 is to account for module & param ID in payload */
  34. #define ADM_GET_PARAMETER_LENGTH (4096 - APR_HDR_SIZE - 2 * sizeof(uint32_t))
  35. #define ULL_SUPPORTED_BITS_PER_SAMPLE 16
  36. #define ULL_SUPPORTED_SAMPLE_RATE 48000
  37. #ifndef CONFIG_DOLBY_DAP
  38. #undef DOLBY_ADM_COPP_TOPOLOGY_ID
  39. #define DOLBY_ADM_COPP_TOPOLOGY_ID 0xFFFFFFFE
  40. #endif
  41. #ifndef CONFIG_DOLBY_DS2
  42. #undef DS2_ADM_COPP_TOPOLOGY_ID
  43. #define DS2_ADM_COPP_TOPOLOGY_ID 0xFFFFFFFF
  44. #endif
  45. /* ENUM for adm_status */
  46. enum adm_cal_status {
  47. ADM_STATUS_CALIBRATION_REQUIRED = 0,
  48. ADM_STATUS_MAX,
  49. };
  50. struct adm_copp {
  51. atomic_t id[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
  52. atomic_t cnt[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
  53. atomic_t topology[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
  54. atomic_t mode[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
  55. atomic_t stat[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
  56. atomic_t rate[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
  57. atomic_t bit_width[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
  58. atomic_t channels[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
  59. atomic_t app_type[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
  60. atomic_t acdb_id[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
  61. wait_queue_head_t wait[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
  62. wait_queue_head_t adm_delay_wait[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
  63. atomic_t adm_delay_stat[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
  64. uint32_t adm_delay[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
  65. unsigned long adm_status[AFE_MAX_PORTS][MAX_COPPS_PER_PORT];
  66. };
  67. struct source_tracking_data {
  68. struct ion_client *ion_client;
  69. struct ion_handle *ion_handle;
  70. struct param_outband memmap;
  71. int apr_cmd_status;
  72. };
  73. struct adm_ctl {
  74. void *apr;
  75. struct adm_copp copp;
  76. atomic_t matrix_map_stat;
  77. wait_queue_head_t matrix_map_wait;
  78. atomic_t adm_stat;
  79. wait_queue_head_t adm_wait;
  80. struct cal_type_data *cal_data[ADM_MAX_CAL_TYPES];
  81. atomic_t mem_map_handles[ADM_MEM_MAP_INDEX_MAX];
  82. atomic_t mem_map_index;
  83. struct param_outband outband_memmap;
  84. struct source_tracking_data sourceTrackingData;
  85. int set_custom_topology;
  86. int ec_ref_rx;
  87. int num_ec_ref_rx_chans;
  88. int ec_ref_rx_bit_width;
  89. int ec_ref_rx_sampling_rate;
  90. int lsm_port_id;
  91. };
  92. static struct adm_ctl this_adm;
  93. struct adm_multi_ch_map {
  94. bool set_channel_map;
  95. char channel_mapping[PCM_FORMAT_MAX_NUM_CHANNEL];
  96. };
  97. #define ADM_MCH_MAP_IDX_PLAYBACK 0
  98. #define ADM_MCH_MAP_IDX_REC 1
  99. static struct adm_multi_ch_map multi_ch_maps[2] = {
  100. { false,
  101. {0, 0, 0, 0, 0, 0, 0, 0}
  102. },
  103. { false,
  104. {0, 0, 0, 0, 0, 0, 0, 0}
  105. }
  106. };
  107. static int adm_get_parameters[MAX_COPPS_PER_PORT * ADM_GET_PARAMETER_LENGTH];
  108. static int adm_module_topo_list[
  109. MAX_COPPS_PER_PORT * ADM_GET_TOPO_MODULE_LIST_LENGTH];
  110. static struct mutex dts_srs_lock;
  111. void msm_dts_srs_acquire_lock(void)
  112. {
  113. mutex_lock(&dts_srs_lock);
  114. }
  115. void msm_dts_srs_release_lock(void)
  116. {
  117. mutex_unlock(&dts_srs_lock);
  118. }
  119. /**
  120. * adm_validate_and_get_port_index -
  121. * validate given port id
  122. *
  123. * @port_id: Port ID number
  124. *
  125. * Returns valid index on success or error on failure
  126. */
  127. int adm_validate_and_get_port_index(int port_id)
  128. {
  129. int index;
  130. int ret;
  131. ret = q6audio_validate_port(port_id);
  132. if (ret < 0) {
  133. pr_err("%s: port validation failed id 0x%x ret %d\n",
  134. __func__, port_id, ret);
  135. return -EINVAL;
  136. }
  137. index = afe_get_port_index(port_id);
  138. if (index < 0 || index >= AFE_MAX_PORTS) {
  139. pr_err("%s: Invalid port idx %d port_id 0x%x\n",
  140. __func__, index,
  141. port_id);
  142. return -EINVAL;
  143. }
  144. pr_debug("%s: port_idx- %d\n", __func__, index);
  145. return index;
  146. }
  147. EXPORT_SYMBOL(adm_validate_and_get_port_index);
  148. /**
  149. * adm_get_default_copp_idx -
  150. * retrieve default copp_idx for given port
  151. *
  152. * @port_id: Port ID number
  153. *
  154. * Returns valid value on success or error on failure
  155. */
  156. int adm_get_default_copp_idx(int port_id)
  157. {
  158. int port_idx = adm_validate_and_get_port_index(port_id), idx;
  159. if (port_idx < 0) {
  160. pr_err("%s: Invalid port id: 0x%x", __func__, port_id);
  161. return -EINVAL;
  162. }
  163. pr_debug("%s: port_idx:%d\n", __func__, port_idx);
  164. for (idx = 0; idx < MAX_COPPS_PER_PORT; idx++) {
  165. if (atomic_read(&this_adm.copp.id[port_idx][idx]) !=
  166. RESET_COPP_ID)
  167. return idx;
  168. }
  169. return -EINVAL;
  170. }
  171. EXPORT_SYMBOL(adm_get_default_copp_idx);
  172. int adm_get_topology_for_port_from_copp_id(int port_id, int copp_id)
  173. {
  174. int port_idx = adm_validate_and_get_port_index(port_id), idx;
  175. if (port_idx < 0) {
  176. pr_err("%s: Invalid port id: 0x%x", __func__, port_id);
  177. return 0;
  178. }
  179. for (idx = 0; idx < MAX_COPPS_PER_PORT; idx++)
  180. if (atomic_read(&this_adm.copp.id[port_idx][idx]) == copp_id)
  181. return atomic_read(&this_adm.copp.topology[port_idx]
  182. [idx]);
  183. pr_err("%s: Invalid copp_id %d port_id 0x%x\n",
  184. __func__, copp_id, port_id);
  185. return 0;
  186. }
  187. /**
  188. * adm_get_topology_for_port_copp_idx -
  189. * retrieve topology of given port/copp_idx
  190. *
  191. * @port_id: Port ID number
  192. * @copp_idx: copp index of ADM copp
  193. *
  194. * Returns valid value on success or 0 on failure
  195. */
  196. int adm_get_topology_for_port_copp_idx(int port_id, int copp_idx)
  197. {
  198. int port_idx = adm_validate_and_get_port_index(port_id);
  199. if (port_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
  200. pr_err("%s: Invalid port: 0x%x copp id: 0x%x",
  201. __func__, port_id, copp_idx);
  202. return 0;
  203. }
  204. return atomic_read(&this_adm.copp.topology[port_idx][copp_idx]);
  205. }
  206. EXPORT_SYMBOL(adm_get_topology_for_port_copp_idx);
  207. int adm_get_indexes_from_copp_id(int copp_id, int *copp_idx, int *port_idx)
  208. {
  209. int p_idx, c_idx;
  210. for (p_idx = 0; p_idx < AFE_MAX_PORTS; p_idx++) {
  211. for (c_idx = 0; c_idx < MAX_COPPS_PER_PORT; c_idx++) {
  212. if (atomic_read(&this_adm.copp.id[p_idx][c_idx])
  213. == copp_id) {
  214. if (copp_idx != NULL)
  215. *copp_idx = c_idx;
  216. if (port_idx != NULL)
  217. *port_idx = p_idx;
  218. return 0;
  219. }
  220. }
  221. }
  222. return -EINVAL;
  223. }
  224. static int adm_get_copp_id(int port_idx, int copp_idx)
  225. {
  226. pr_debug("%s: port_idx:%d copp_idx:%d\n", __func__, port_idx, copp_idx);
  227. if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
  228. pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
  229. return -EINVAL;
  230. }
  231. return atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
  232. }
  233. static int adm_get_idx_if_copp_exists(int port_idx, int topology, int mode,
  234. int rate, int bit_width, int app_type)
  235. {
  236. int idx;
  237. pr_debug("%s: port_idx-%d, topology-0x%x, mode-%d, rate-%d, bit_width-%d\n",
  238. __func__, port_idx, topology, mode, rate, bit_width);
  239. for (idx = 0; idx < MAX_COPPS_PER_PORT; idx++)
  240. if ((topology ==
  241. atomic_read(&this_adm.copp.topology[port_idx][idx])) &&
  242. (mode == atomic_read(&this_adm.copp.mode[port_idx][idx])) &&
  243. (rate == atomic_read(&this_adm.copp.rate[port_idx][idx])) &&
  244. (bit_width ==
  245. atomic_read(&this_adm.copp.bit_width[port_idx][idx])) &&
  246. (app_type ==
  247. atomic_read(&this_adm.copp.app_type[port_idx][idx])))
  248. return idx;
  249. return -EINVAL;
  250. }
  251. static int adm_get_next_available_copp(int port_idx)
  252. {
  253. int idx;
  254. pr_debug("%s:\n", __func__);
  255. for (idx = 0; idx < MAX_COPPS_PER_PORT; idx++) {
  256. pr_debug("%s: copp_id:0x%x port_idx:%d idx:%d\n", __func__,
  257. atomic_read(&this_adm.copp.id[port_idx][idx]),
  258. port_idx, idx);
  259. if (atomic_read(&this_adm.copp.id[port_idx][idx]) ==
  260. RESET_COPP_ID)
  261. break;
  262. }
  263. return idx;
  264. }
  265. /**
  266. * srs_trumedia_open -
  267. * command to set SRS trumedia open
  268. *
  269. * @port_id: Port ID number
  270. * @copp_idx: copp index of ADM copp
  271. * @srs_tech_id: SRS tech index
  272. * @srs_params: params pointer
  273. *
  274. * Returns 0 on success or error on failure
  275. */
  276. int srs_trumedia_open(int port_id, int copp_idx, __s32 srs_tech_id,
  277. void *srs_params)
  278. {
  279. struct adm_cmd_set_pp_params_inband_v5 *adm_params = NULL;
  280. struct adm_cmd_set_pp_params_v5 *adm_params_ = NULL;
  281. __s32 sz = 0, param_id, module_id = SRS_TRUMEDIA_MODULE_ID, outband = 0;
  282. int ret = 0, port_idx;
  283. pr_debug("SRS - %s", __func__);
  284. port_id = afe_convert_virtual_to_portid(port_id);
  285. port_idx = adm_validate_and_get_port_index(port_id);
  286. if (port_idx < 0) {
  287. pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
  288. return -EINVAL;
  289. }
  290. switch (srs_tech_id) {
  291. case SRS_ID_GLOBAL: {
  292. struct srs_trumedia_params_GLOBAL *glb_params = NULL;
  293. sz = sizeof(struct adm_cmd_set_pp_params_inband_v5) +
  294. sizeof(struct srs_trumedia_params_GLOBAL);
  295. adm_params = kzalloc(sz, GFP_KERNEL);
  296. if (!adm_params) {
  297. pr_err("%s, adm params memory alloc failed\n",
  298. __func__);
  299. return -ENOMEM;
  300. }
  301. adm_params->payload_size =
  302. sizeof(struct srs_trumedia_params_GLOBAL) +
  303. sizeof(struct adm_param_data_v5);
  304. param_id = SRS_TRUMEDIA_PARAMS;
  305. adm_params->params.param_size =
  306. sizeof(struct srs_trumedia_params_GLOBAL);
  307. glb_params = (struct srs_trumedia_params_GLOBAL *)
  308. ((u8 *)adm_params +
  309. sizeof(struct adm_cmd_set_pp_params_inband_v5));
  310. memcpy(glb_params, srs_params,
  311. sizeof(struct srs_trumedia_params_GLOBAL));
  312. break;
  313. }
  314. case SRS_ID_WOWHD: {
  315. struct srs_trumedia_params_WOWHD *whd_params = NULL;
  316. sz = sizeof(struct adm_cmd_set_pp_params_inband_v5) +
  317. sizeof(struct srs_trumedia_params_WOWHD);
  318. adm_params = kzalloc(sz, GFP_KERNEL);
  319. if (!adm_params) {
  320. pr_err("%s, adm params memory alloc failed\n",
  321. __func__);
  322. return -ENOMEM;
  323. }
  324. adm_params->payload_size =
  325. sizeof(struct srs_trumedia_params_WOWHD) +
  326. sizeof(struct adm_param_data_v5);
  327. param_id = SRS_TRUMEDIA_PARAMS_WOWHD;
  328. adm_params->params.param_size =
  329. sizeof(struct srs_trumedia_params_WOWHD);
  330. whd_params = (struct srs_trumedia_params_WOWHD *)
  331. ((u8 *)adm_params +
  332. sizeof(struct adm_cmd_set_pp_params_inband_v5));
  333. memcpy(whd_params, srs_params,
  334. sizeof(struct srs_trumedia_params_WOWHD));
  335. break;
  336. }
  337. case SRS_ID_CSHP: {
  338. struct srs_trumedia_params_CSHP *chp_params = NULL;
  339. sz = sizeof(struct adm_cmd_set_pp_params_inband_v5) +
  340. sizeof(struct srs_trumedia_params_CSHP);
  341. adm_params = kzalloc(sz, GFP_KERNEL);
  342. if (!adm_params) {
  343. pr_err("%s, adm params memory alloc failed\n",
  344. __func__);
  345. return -ENOMEM;
  346. }
  347. adm_params->payload_size =
  348. sizeof(struct srs_trumedia_params_CSHP) +
  349. sizeof(struct adm_param_data_v5);
  350. param_id = SRS_TRUMEDIA_PARAMS_CSHP;
  351. adm_params->params.param_size =
  352. sizeof(struct srs_trumedia_params_CSHP);
  353. chp_params = (struct srs_trumedia_params_CSHP *)
  354. ((u8 *)adm_params +
  355. sizeof(struct adm_cmd_set_pp_params_inband_v5));
  356. memcpy(chp_params, srs_params,
  357. sizeof(struct srs_trumedia_params_CSHP));
  358. break;
  359. }
  360. case SRS_ID_HPF: {
  361. struct srs_trumedia_params_HPF *hpf_params = NULL;
  362. sz = sizeof(struct adm_cmd_set_pp_params_inband_v5) +
  363. sizeof(struct srs_trumedia_params_HPF);
  364. adm_params = kzalloc(sz, GFP_KERNEL);
  365. if (!adm_params) {
  366. pr_err("%s, adm params memory alloc failed\n",
  367. __func__);
  368. return -ENOMEM;
  369. }
  370. adm_params->payload_size =
  371. sizeof(struct srs_trumedia_params_HPF) +
  372. sizeof(struct adm_param_data_v5);
  373. param_id = SRS_TRUMEDIA_PARAMS_HPF;
  374. adm_params->params.param_size =
  375. sizeof(struct srs_trumedia_params_HPF);
  376. hpf_params = (struct srs_trumedia_params_HPF *)
  377. ((u8 *)adm_params +
  378. sizeof(struct adm_cmd_set_pp_params_inband_v5));
  379. memcpy(hpf_params, srs_params,
  380. sizeof(struct srs_trumedia_params_HPF));
  381. break;
  382. }
  383. case SRS_ID_AEQ: {
  384. int *update_params_ptr = (int *)this_adm.outband_memmap.kvaddr;
  385. outband = 1;
  386. adm_params = kzalloc(sizeof(struct adm_cmd_set_pp_params_v5),
  387. GFP_KERNEL);
  388. adm_params_ = (struct adm_cmd_set_pp_params_v5 *)adm_params;
  389. if (!adm_params_) {
  390. pr_err("%s, adm params memory alloc failed\n",
  391. __func__);
  392. return -ENOMEM;
  393. }
  394. sz = sizeof(struct srs_trumedia_params_AEQ);
  395. if (update_params_ptr == NULL) {
  396. pr_err("ADM_SRS_TRUMEDIA - %s: null memmap for AEQ params\n",
  397. __func__);
  398. ret = -EINVAL;
  399. goto fail_cmd;
  400. }
  401. param_id = SRS_TRUMEDIA_PARAMS_AEQ;
  402. *update_params_ptr++ = module_id;
  403. *update_params_ptr++ = param_id;
  404. *update_params_ptr++ = sz;
  405. memcpy(update_params_ptr, srs_params, sz);
  406. adm_params_->payload_size = sz + 12;
  407. break;
  408. }
  409. case SRS_ID_HL: {
  410. struct srs_trumedia_params_HL *hl_params = NULL;
  411. sz = sizeof(struct adm_cmd_set_pp_params_inband_v5) +
  412. sizeof(struct srs_trumedia_params_HL);
  413. adm_params = kzalloc(sz, GFP_KERNEL);
  414. if (!adm_params) {
  415. pr_err("%s, adm params memory alloc failed\n",
  416. __func__);
  417. return -ENOMEM;
  418. }
  419. adm_params->payload_size =
  420. sizeof(struct srs_trumedia_params_HL) +
  421. sizeof(struct adm_param_data_v5);
  422. param_id = SRS_TRUMEDIA_PARAMS_HL;
  423. adm_params->params.param_size =
  424. sizeof(struct srs_trumedia_params_HL);
  425. hl_params = (struct srs_trumedia_params_HL *)
  426. ((u8 *)adm_params +
  427. sizeof(struct adm_cmd_set_pp_params_inband_v5));
  428. memcpy(hl_params, srs_params,
  429. sizeof(struct srs_trumedia_params_HL));
  430. break;
  431. }
  432. case SRS_ID_GEQ: {
  433. struct srs_trumedia_params_GEQ *geq_params = NULL;
  434. sz = sizeof(struct adm_cmd_set_pp_params_inband_v5) +
  435. sizeof(struct srs_trumedia_params_GEQ);
  436. adm_params = kzalloc(sz, GFP_KERNEL);
  437. if (!adm_params) {
  438. pr_err("%s, adm params memory alloc failed\n",
  439. __func__);
  440. return -ENOMEM;
  441. }
  442. adm_params->payload_size =
  443. sizeof(struct srs_trumedia_params_GEQ) +
  444. sizeof(struct adm_param_data_v5);
  445. param_id = SRS_TRUMEDIA_PARAMS_GEQ;
  446. adm_params->params.param_size =
  447. sizeof(struct srs_trumedia_params_GEQ);
  448. geq_params = (struct srs_trumedia_params_GEQ *)
  449. ((u8 *)adm_params +
  450. sizeof(struct adm_cmd_set_pp_params_inband_v5));
  451. memcpy(geq_params, srs_params,
  452. sizeof(struct srs_trumedia_params_GEQ));
  453. pr_debug("SRS - %s: GEQ params prepared\n", __func__);
  454. break;
  455. }
  456. default:
  457. goto fail_cmd;
  458. }
  459. adm_params->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  460. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  461. adm_params->hdr.src_svc = APR_SVC_ADM;
  462. adm_params->hdr.src_domain = APR_DOMAIN_APPS;
  463. adm_params->hdr.src_port = port_id;
  464. adm_params->hdr.dest_svc = APR_SVC_ADM;
  465. adm_params->hdr.dest_domain = APR_DOMAIN_ADSP;
  466. adm_params->hdr.dest_port =
  467. atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
  468. adm_params->hdr.token = port_idx << 16 | copp_idx;
  469. adm_params->hdr.opcode = ADM_CMD_SET_PP_PARAMS_V5;
  470. if (outband && this_adm.outband_memmap.paddr) {
  471. adm_params->hdr.pkt_size =
  472. sizeof(struct adm_cmd_set_pp_params_v5);
  473. adm_params->payload_addr_lsw = lower_32_bits(
  474. this_adm.outband_memmap.paddr);
  475. adm_params->payload_addr_msw = msm_audio_populate_upper_32_bits(
  476. this_adm.outband_memmap.paddr);
  477. adm_params->mem_map_handle = atomic_read(&this_adm.
  478. mem_map_handles[ADM_SRS_TRUMEDIA]);
  479. } else {
  480. adm_params->hdr.pkt_size = sz;
  481. adm_params->payload_addr_lsw = 0;
  482. adm_params->payload_addr_msw = 0;
  483. adm_params->mem_map_handle = 0;
  484. adm_params->params.module_id = module_id;
  485. adm_params->params.param_id = param_id;
  486. adm_params->params.reserved = 0;
  487. }
  488. pr_debug("SRS - %s: Command was sent now check Q6 - port id = %d, size %d, module id %x, param id %x.\n",
  489. __func__, adm_params->hdr.dest_port,
  490. adm_params->payload_size, module_id, param_id);
  491. atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
  492. ret = apr_send_pkt(this_adm.apr, (uint32_t *)adm_params);
  493. if (ret < 0) {
  494. pr_err("SRS - %s: ADM enable for port %d failed\n", __func__,
  495. port_id);
  496. ret = -EINVAL;
  497. goto fail_cmd;
  498. }
  499. /* Wait for the callback with copp id */
  500. ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
  501. atomic_read(&this_adm.copp.stat
  502. [port_idx][copp_idx]) >= 0,
  503. msecs_to_jiffies(TIMEOUT_MS));
  504. if (!ret) {
  505. pr_err("%s: SRS set params timed out port = %d\n",
  506. __func__, port_id);
  507. ret = -EINVAL;
  508. goto fail_cmd;
  509. } else if (atomic_read(&this_adm.copp.stat
  510. [port_idx][copp_idx]) > 0) {
  511. pr_err("%s: DSP returned error[%s]\n",
  512. __func__, adsp_err_get_err_str(
  513. atomic_read(&this_adm.copp.stat
  514. [port_idx][copp_idx])));
  515. ret = adsp_err_get_lnx_err_code(
  516. atomic_read(&this_adm.copp.stat
  517. [port_idx][copp_idx]));
  518. goto fail_cmd;
  519. }
  520. fail_cmd:
  521. kfree(adm_params);
  522. return ret;
  523. }
  524. EXPORT_SYMBOL(srs_trumedia_open);
  525. static int adm_populate_channel_weight(u16 *ptr,
  526. struct msm_pcm_channel_mixer *ch_mixer,
  527. int channel_index)
  528. {
  529. u16 i, j, start_index = 0;
  530. if (channel_index > ch_mixer->output_channel) {
  531. pr_err("%s: channel index %d is larger than output_channel %d\n",
  532. __func__, channel_index, ch_mixer->output_channel);
  533. return -EINVAL;
  534. }
  535. for (i = 0; i < ch_mixer->output_channel; i++) {
  536. pr_debug("%s: weight for output %d:", __func__, i);
  537. for (j = 0; j < ADM_MAX_CHANNELS; j++)
  538. pr_debug(" %d",
  539. ch_mixer->channel_weight[i][j]);
  540. pr_debug("\n");
  541. }
  542. for (i = 0; i < channel_index; ++i)
  543. start_index += ch_mixer->input_channels[i];
  544. for (i = 0; i < ch_mixer->output_channel; ++i) {
  545. for (j = start_index;
  546. j < start_index +
  547. ch_mixer->input_channels[channel_index]; j++) {
  548. *ptr = ch_mixer->channel_weight[i][j];
  549. pr_debug("%s: ptr[%d][%d] = %d\n",
  550. __func__, i, j, *ptr);
  551. ptr++;
  552. }
  553. }
  554. return 0;
  555. }
  556. /*
  557. * adm_programable_channel_mixer
  558. *
  559. * Receives port_id, copp_idx, session_id, session_type, ch_mixer
  560. * and channel_index to send ADM command to mix COPP data.
  561. *
  562. * port_id - Passed value, port_id for which backend is wanted
  563. * copp_idx - Passed value, copp_idx for which COPP is wanted
  564. * session_id - Passed value, session_id for which session is needed
  565. * session_type - Passed value, session_type for RX or TX
  566. * ch_mixer - Passed value, ch_mixer for which channel mixer config is needed
  567. * channel_index - Passed value, channel_index for which channel is needed
  568. */
  569. int adm_programable_channel_mixer(int port_id, int copp_idx, int session_id,
  570. int session_type,
  571. struct msm_pcm_channel_mixer *ch_mixer,
  572. int channel_index)
  573. {
  574. struct adm_cmd_set_pspd_mtmx_strtr_params_v5 *adm_params = NULL;
  575. struct adm_param_data_v5 data_v5;
  576. int ret = 0, port_idx, sz = 0, param_size = 0;
  577. u16 *adm_pspd_params;
  578. u16 *ptr;
  579. int index = 0;
  580. pr_debug("%s: port_id = %d\n", __func__, port_id);
  581. port_id = afe_convert_virtual_to_portid(port_id);
  582. port_idx = adm_validate_and_get_port_index(port_id);
  583. if (port_idx < 0) {
  584. pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
  585. return -EINVAL;
  586. }
  587. /*
  588. * First 8 bytes are 4 bytes as rule number, 2 bytes as output
  589. * channel and 2 bytes as input channel.
  590. * 2 * ch_mixer->output_channel means output channel mapping.
  591. * 2 * ch_mixer->input_channels[channel_index]) means input
  592. * channel mapping.
  593. * 2 * ch_mixer->input_channels[channel_index] *
  594. * ch_mixer->output_channel) means the channel mixer weighting
  595. * coefficients.
  596. * param_size needs to be a multiple of 4 bytes.
  597. */
  598. param_size = 2 * (4 + ch_mixer->output_channel +
  599. ch_mixer->input_channels[channel_index] +
  600. ch_mixer->input_channels[channel_index] *
  601. ch_mixer->output_channel);
  602. roundup(param_size, 4);
  603. sz = sizeof(struct adm_cmd_set_pspd_mtmx_strtr_params_v5) +
  604. sizeof(struct default_chmixer_param_id_coeff) +
  605. sizeof(struct adm_param_data_v5) + param_size;
  606. pr_debug("%s: sz = %d\n", __func__, sz);
  607. adm_params = kzalloc(sz, GFP_KERNEL);
  608. if (!adm_params)
  609. return -ENOMEM;
  610. adm_params->payload_addr_lsw = 0;
  611. adm_params->payload_addr_msw = 0;
  612. adm_params->mem_map_handle = 0;
  613. adm_params->direction = session_type;
  614. adm_params->sessionid = session_id;
  615. pr_debug("%s: copp_id = %d, session id %d\n", __func__,
  616. atomic_read(&this_adm.copp.id[port_idx][copp_idx]),
  617. session_id);
  618. adm_params->deviceid = atomic_read(
  619. &this_adm.copp.id[port_idx][copp_idx]);
  620. adm_params->reserved = 0;
  621. data_v5.module_id = MTMX_MODULE_ID_DEFAULT_CHMIXER;
  622. data_v5.param_id = DEFAULT_CHMIXER_PARAM_ID_COEFF;
  623. data_v5.reserved = 0;
  624. data_v5.param_size = param_size;
  625. adm_params->payload_size =
  626. sizeof(struct default_chmixer_param_id_coeff) +
  627. sizeof(struct adm_param_data_v5) + data_v5.param_size;
  628. adm_pspd_params = (u16 *)((u8 *)adm_params +
  629. sizeof(struct adm_cmd_set_pspd_mtmx_strtr_params_v5));
  630. memcpy(adm_pspd_params, &data_v5, sizeof(data_v5));
  631. adm_pspd_params = (u16 *)((u8 *)adm_params +
  632. sizeof(struct adm_cmd_set_pspd_mtmx_strtr_params_v5)
  633. + sizeof(data_v5));
  634. adm_pspd_params[0] = ch_mixer->rule;
  635. adm_pspd_params[2] = ch_mixer->output_channel;
  636. adm_pspd_params[3] = ch_mixer->input_channels[channel_index];
  637. index = 4;
  638. if (ch_mixer->output_channel == 1) {
  639. adm_pspd_params[index] = PCM_CHANNEL_FC;
  640. } else if (ch_mixer->output_channel == 2) {
  641. adm_pspd_params[index] = PCM_CHANNEL_FL;
  642. adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
  643. } else if (ch_mixer->output_channel == 3) {
  644. adm_pspd_params[index] = PCM_CHANNEL_FL;
  645. adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
  646. adm_pspd_params[index + 2] = PCM_CHANNEL_FC;
  647. } else if (ch_mixer->output_channel == 4) {
  648. adm_pspd_params[index] = PCM_CHANNEL_FL;
  649. adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
  650. adm_pspd_params[index + 2] = PCM_CHANNEL_LS;
  651. adm_pspd_params[index + 3] = PCM_CHANNEL_RS;
  652. } else if (ch_mixer->output_channel == 5) {
  653. adm_pspd_params[index] = PCM_CHANNEL_FL;
  654. adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
  655. adm_pspd_params[index + 2] = PCM_CHANNEL_FC;
  656. adm_pspd_params[index + 3] = PCM_CHANNEL_LS;
  657. adm_pspd_params[index + 4] = PCM_CHANNEL_RS;
  658. } else if (ch_mixer->output_channel == 6) {
  659. adm_pspd_params[index] = PCM_CHANNEL_FL;
  660. adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
  661. adm_pspd_params[index + 2] = PCM_CHANNEL_LFE;
  662. adm_pspd_params[index + 3] = PCM_CHANNEL_FC;
  663. adm_pspd_params[index + 4] = PCM_CHANNEL_LS;
  664. adm_pspd_params[index + 5] = PCM_CHANNEL_RS;
  665. } else if (ch_mixer->output_channel == 8) {
  666. adm_pspd_params[index] = PCM_CHANNEL_FL;
  667. adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
  668. adm_pspd_params[index + 2] = PCM_CHANNEL_LFE;
  669. adm_pspd_params[index + 3] = PCM_CHANNEL_FC;
  670. adm_pspd_params[index + 4] = PCM_CHANNEL_LS;
  671. adm_pspd_params[index + 5] = PCM_CHANNEL_RS;
  672. adm_pspd_params[index + 6] = PCM_CHANNEL_LB;
  673. adm_pspd_params[index + 7] = PCM_CHANNEL_RB;
  674. }
  675. index = index + ch_mixer->output_channel;
  676. if (ch_mixer->input_channels[channel_index] == 1) {
  677. adm_pspd_params[index] = PCM_CHANNEL_FC;
  678. } else if (ch_mixer->input_channels[channel_index] == 2) {
  679. adm_pspd_params[index] = PCM_CHANNEL_FL;
  680. adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
  681. } else if (ch_mixer->input_channels[channel_index] == 3) {
  682. adm_pspd_params[index] = PCM_CHANNEL_FL;
  683. adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
  684. adm_pspd_params[index + 2] = PCM_CHANNEL_FC;
  685. } else if (ch_mixer->input_channels[channel_index] == 4) {
  686. adm_pspd_params[index] = PCM_CHANNEL_FL;
  687. adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
  688. adm_pspd_params[index + 2] = PCM_CHANNEL_LS;
  689. adm_pspd_params[index + 3] = PCM_CHANNEL_RS;
  690. } else if (ch_mixer->input_channels[channel_index] == 5) {
  691. adm_pspd_params[index] = PCM_CHANNEL_FL;
  692. adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
  693. adm_pspd_params[index + 2] = PCM_CHANNEL_FC;
  694. adm_pspd_params[index + 3] = PCM_CHANNEL_LS;
  695. adm_pspd_params[index + 4] = PCM_CHANNEL_RS;
  696. } else if (ch_mixer->input_channels[channel_index] == 6) {
  697. adm_pspd_params[index] = PCM_CHANNEL_FL;
  698. adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
  699. adm_pspd_params[index + 2] = PCM_CHANNEL_LFE;
  700. adm_pspd_params[index + 3] = PCM_CHANNEL_FC;
  701. adm_pspd_params[index + 4] = PCM_CHANNEL_LS;
  702. adm_pspd_params[index + 5] = PCM_CHANNEL_RS;
  703. } else if (ch_mixer->input_channels[channel_index] == 8) {
  704. adm_pspd_params[index] = PCM_CHANNEL_FL;
  705. adm_pspd_params[index + 1] = PCM_CHANNEL_FR;
  706. adm_pspd_params[index + 2] = PCM_CHANNEL_LFE;
  707. adm_pspd_params[index + 3] = PCM_CHANNEL_FC;
  708. adm_pspd_params[index + 4] = PCM_CHANNEL_LS;
  709. adm_pspd_params[index + 5] = PCM_CHANNEL_RS;
  710. adm_pspd_params[index + 6] = PCM_CHANNEL_LB;
  711. adm_pspd_params[index + 7] = PCM_CHANNEL_RB;
  712. }
  713. index = index + ch_mixer->input_channels[channel_index];
  714. ret = adm_populate_channel_weight(&adm_pspd_params[index],
  715. ch_mixer, channel_index);
  716. if (!ret) {
  717. pr_err("%s: fail to get channel weight with error %d\n",
  718. __func__, ret);
  719. goto fail_cmd;
  720. }
  721. adm_params->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  722. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  723. adm_params->hdr.src_svc = APR_SVC_ADM;
  724. adm_params->hdr.src_domain = APR_DOMAIN_APPS;
  725. adm_params->hdr.src_port = port_id;
  726. adm_params->hdr.dest_svc = APR_SVC_ADM;
  727. adm_params->hdr.dest_domain = APR_DOMAIN_ADSP;
  728. adm_params->hdr.dest_port =
  729. atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
  730. adm_params->hdr.token = port_idx << 16 | copp_idx;
  731. adm_params->hdr.opcode = ADM_CMD_SET_PSPD_MTMX_STRTR_PARAMS_V5;
  732. adm_params->hdr.pkt_size = sz;
  733. adm_params->payload_addr_lsw = 0;
  734. adm_params->payload_addr_msw = 0;
  735. adm_params->mem_map_handle = 0;
  736. adm_params->reserved = 0;
  737. ptr = (u16 *)adm_params;
  738. for (index = 0; index < (sz / 2); index++)
  739. pr_debug("%s: adm_params[%d] = 0x%x\n",
  740. __func__, index, (unsigned int)ptr[index]);
  741. atomic_set(&this_adm.copp.stat[port_idx][copp_idx], 0);
  742. ret = apr_send_pkt(this_adm.apr, (uint32_t *)adm_params);
  743. if (ret < 0) {
  744. pr_err("%s: Set params failed port %d rc %d\n", __func__,
  745. port_id, ret);
  746. ret = -EINVAL;
  747. goto fail_cmd;
  748. }
  749. ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
  750. atomic_read(
  751. &this_adm.copp.stat[port_idx][copp_idx]) >= 0,
  752. msecs_to_jiffies(TIMEOUT_MS));
  753. if (!ret) {
  754. pr_err("%s: set params timed out port = %d\n",
  755. __func__, port_id);
  756. ret = -ETIMEDOUT;
  757. goto fail_cmd;
  758. }
  759. ret = 0;
  760. fail_cmd:
  761. kfree(adm_params);
  762. return ret;
  763. }
  764. EXPORT_SYMBOL(adm_programable_channel_mixer);
  765. /**
  766. * adm_set_stereo_to_custom_stereo -
  767. * command to update custom stereo
  768. *
  769. * @port_id: Port ID number
  770. * @copp_idx: copp index of ADM copp
  771. * @session_id: session id to be updated
  772. * @params: params pointer
  773. * @param_length: length of params
  774. *
  775. * Returns 0 on success or error on failure
  776. */
  777. int adm_set_stereo_to_custom_stereo(int port_id, int copp_idx,
  778. unsigned int session_id, char *params,
  779. uint32_t params_length)
  780. {
  781. struct adm_cmd_set_pspd_mtmx_strtr_params_v5 *adm_params = NULL;
  782. int sz, rc = 0, port_idx;
  783. pr_debug("%s:\n", __func__);
  784. port_id = afe_convert_virtual_to_portid(port_id);
  785. port_idx = adm_validate_and_get_port_index(port_id);
  786. if (port_idx < 0) {
  787. pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
  788. return -EINVAL;
  789. }
  790. sz = sizeof(struct adm_cmd_set_pspd_mtmx_strtr_params_v5) +
  791. params_length;
  792. adm_params = kzalloc(sz, GFP_KERNEL);
  793. if (!adm_params) {
  794. pr_err("%s, adm params memory alloc failed\n", __func__);
  795. return -ENOMEM;
  796. }
  797. memcpy(((u8 *)adm_params +
  798. sizeof(struct adm_cmd_set_pspd_mtmx_strtr_params_v5)),
  799. params, params_length);
  800. adm_params->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  801. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  802. adm_params->hdr.pkt_size = sz;
  803. adm_params->hdr.src_svc = APR_SVC_ADM;
  804. adm_params->hdr.src_domain = APR_DOMAIN_APPS;
  805. adm_params->hdr.src_port = port_id;
  806. adm_params->hdr.dest_svc = APR_SVC_ADM;
  807. adm_params->hdr.dest_domain = APR_DOMAIN_ADSP;
  808. adm_params->hdr.dest_port = 0; /* Ignored */;
  809. adm_params->hdr.token = port_idx << 16 | copp_idx;
  810. adm_params->hdr.opcode = ADM_CMD_SET_PSPD_MTMX_STRTR_PARAMS_V5;
  811. adm_params->payload_addr_lsw = 0;
  812. adm_params->payload_addr_msw = 0;
  813. adm_params->mem_map_handle = 0;
  814. adm_params->payload_size = params_length;
  815. /* direction RX as 0 */
  816. adm_params->direction = ADM_MATRIX_ID_AUDIO_RX;
  817. /* session id for this cmd to be applied on */
  818. adm_params->sessionid = session_id;
  819. adm_params->deviceid =
  820. atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
  821. adm_params->reserved = 0;
  822. pr_debug("%s: deviceid %d, session_id %d, src_port %d, dest_port %d\n",
  823. __func__, adm_params->deviceid, adm_params->sessionid,
  824. adm_params->hdr.src_port, adm_params->hdr.dest_port);
  825. atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
  826. rc = apr_send_pkt(this_adm.apr, (uint32_t *)adm_params);
  827. if (rc < 0) {
  828. pr_err("%s: Set params failed port = 0x%x rc %d\n",
  829. __func__, port_id, rc);
  830. rc = -EINVAL;
  831. goto set_stereo_to_custom_stereo_return;
  832. }
  833. /* Wait for the callback */
  834. rc = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
  835. atomic_read(&this_adm.copp.stat
  836. [port_idx][copp_idx]) >= 0,
  837. msecs_to_jiffies(TIMEOUT_MS));
  838. if (!rc) {
  839. pr_err("%s: Set params timed out port = 0x%x\n", __func__,
  840. port_id);
  841. rc = -EINVAL;
  842. goto set_stereo_to_custom_stereo_return;
  843. } else if (atomic_read(&this_adm.copp.stat
  844. [port_idx][copp_idx]) > 0) {
  845. pr_err("%s: DSP returned error[%s]\n", __func__,
  846. adsp_err_get_err_str(atomic_read(
  847. &this_adm.copp.stat
  848. [port_idx][copp_idx])));
  849. rc = adsp_err_get_lnx_err_code(
  850. atomic_read(&this_adm.copp.stat
  851. [port_idx][copp_idx]));
  852. goto set_stereo_to_custom_stereo_return;
  853. }
  854. rc = 0;
  855. set_stereo_to_custom_stereo_return:
  856. kfree(adm_params);
  857. return rc;
  858. }
  859. EXPORT_SYMBOL(adm_set_stereo_to_custom_stereo);
  860. /**
  861. * adm_dolby_dap_send_params -
  862. * command to send dolby dap params
  863. *
  864. * @port_id: Port ID number
  865. * @copp_idx: copp index of ADM copp
  866. * @params: params pointer
  867. * @param_length: length of params
  868. *
  869. * Returns 0 on success or error on failure
  870. */
  871. int adm_dolby_dap_send_params(int port_id, int copp_idx, char *params,
  872. uint32_t params_length)
  873. {
  874. struct adm_cmd_set_pp_params_v5 *adm_params = NULL;
  875. int sz, rc = 0;
  876. int port_idx;
  877. pr_debug("%s:\n", __func__);
  878. port_id = afe_convert_virtual_to_portid(port_id);
  879. port_idx = adm_validate_and_get_port_index(port_id);
  880. if (port_idx < 0) {
  881. pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
  882. return -EINVAL;
  883. }
  884. sz = sizeof(struct adm_cmd_set_pp_params_v5) + params_length;
  885. adm_params = kzalloc(sz, GFP_KERNEL);
  886. if (!adm_params) {
  887. pr_err("%s, adm params memory alloc failed", __func__);
  888. return -ENOMEM;
  889. }
  890. memcpy(((u8 *)adm_params + sizeof(struct adm_cmd_set_pp_params_v5)),
  891. params, params_length);
  892. adm_params->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  893. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  894. adm_params->hdr.pkt_size = sz;
  895. adm_params->hdr.src_svc = APR_SVC_ADM;
  896. adm_params->hdr.src_domain = APR_DOMAIN_APPS;
  897. adm_params->hdr.src_port = port_id;
  898. adm_params->hdr.dest_svc = APR_SVC_ADM;
  899. adm_params->hdr.dest_domain = APR_DOMAIN_ADSP;
  900. adm_params->hdr.dest_port =
  901. atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
  902. adm_params->hdr.token = port_idx << 16 | copp_idx;
  903. adm_params->hdr.opcode = ADM_CMD_SET_PP_PARAMS_V5;
  904. adm_params->payload_addr_lsw = 0;
  905. adm_params->payload_addr_msw = 0;
  906. adm_params->mem_map_handle = 0;
  907. adm_params->payload_size = params_length;
  908. atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
  909. rc = apr_send_pkt(this_adm.apr, (uint32_t *)adm_params);
  910. if (rc < 0) {
  911. pr_err("%s: Set params failed port = 0x%x rc %d\n",
  912. __func__, port_id, rc);
  913. rc = -EINVAL;
  914. goto dolby_dap_send_param_return;
  915. }
  916. /* Wait for the callback */
  917. rc = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
  918. atomic_read(&this_adm.copp.stat[port_idx][copp_idx]) >= 0,
  919. msecs_to_jiffies(TIMEOUT_MS));
  920. if (!rc) {
  921. pr_err("%s: Set params timed out port = 0x%x\n",
  922. __func__, port_id);
  923. rc = -EINVAL;
  924. goto dolby_dap_send_param_return;
  925. } else if (atomic_read(&this_adm.copp.stat
  926. [port_idx][copp_idx]) > 0) {
  927. pr_err("%s: DSP returned error[%s]\n",
  928. __func__, adsp_err_get_err_str(
  929. atomic_read(&this_adm.copp.stat
  930. [port_idx][copp_idx])));
  931. rc = adsp_err_get_lnx_err_code(
  932. atomic_read(&this_adm.copp.stat
  933. [port_idx][copp_idx]));
  934. goto dolby_dap_send_param_return;
  935. }
  936. rc = 0;
  937. dolby_dap_send_param_return:
  938. kfree(adm_params);
  939. return rc;
  940. }
  941. EXPORT_SYMBOL(adm_dolby_dap_send_params);
  942. /**
  943. * adm_get_params_v5 -
  944. * command to retrieve ADM params for given module
  945. *
  946. * @port_id: Port ID number
  947. * @copp_idx: copp index of ADM copp
  948. * @params: params pointer
  949. * @param_length: length of params
  950. *
  951. * Returns 0 on success or error on failure
  952. */
  953. int adm_send_params_v5(int port_id, int copp_idx, char *params,
  954. uint32_t params_length)
  955. {
  956. struct adm_cmd_set_pp_params_v5 *adm_params = NULL;
  957. int rc = 0;
  958. int sz, port_idx;
  959. pr_debug("%s:\n", __func__);
  960. port_id = afe_convert_virtual_to_portid(port_id);
  961. port_idx = adm_validate_and_get_port_index(port_id);
  962. if (port_idx < 0) {
  963. pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
  964. return -EINVAL;
  965. }
  966. sz = sizeof(struct adm_cmd_set_pp_params_v5) + params_length;
  967. adm_params = kzalloc(sz, GFP_KERNEL);
  968. if (!adm_params) {
  969. pr_err("%s, adm params memory alloc failed", __func__);
  970. return -ENOMEM;
  971. }
  972. memcpy(((u8 *)adm_params + sizeof(struct adm_cmd_set_pp_params_v5)),
  973. params, params_length);
  974. adm_params->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  975. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  976. adm_params->hdr.pkt_size = sz;
  977. adm_params->hdr.src_svc = APR_SVC_ADM;
  978. adm_params->hdr.src_domain = APR_DOMAIN_APPS;
  979. adm_params->hdr.src_port = port_id;
  980. adm_params->hdr.dest_svc = APR_SVC_ADM;
  981. adm_params->hdr.dest_domain = APR_DOMAIN_ADSP;
  982. adm_params->hdr.dest_port =
  983. atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
  984. adm_params->hdr.token = port_idx << 16 | copp_idx;
  985. adm_params->hdr.opcode = ADM_CMD_SET_PP_PARAMS_V5;
  986. adm_params->payload_addr_lsw = 0;
  987. adm_params->payload_addr_msw = 0;
  988. adm_params->mem_map_handle = 0;
  989. adm_params->payload_size = params_length;
  990. atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
  991. rc = apr_send_pkt(this_adm.apr, (uint32_t *)adm_params);
  992. if (rc < 0) {
  993. pr_err("%s: Set params failed port = 0x%x rc %d\n",
  994. __func__, port_id, rc);
  995. rc = -EINVAL;
  996. goto send_param_return;
  997. }
  998. /* Wait for the callback */
  999. rc = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
  1000. atomic_read(&this_adm.copp.stat[port_idx][copp_idx]) >= 0,
  1001. msecs_to_jiffies(TIMEOUT_MS));
  1002. if (!rc) {
  1003. pr_err("%s: Set params timed out port = 0x%x\n",
  1004. __func__, port_id);
  1005. rc = -EINVAL;
  1006. goto send_param_return;
  1007. } else if (atomic_read(&this_adm.copp.stat
  1008. [port_idx][copp_idx]) > 0) {
  1009. pr_err("%s: DSP returned error[%s]\n",
  1010. __func__, adsp_err_get_err_str(
  1011. atomic_read(&this_adm.copp.stat
  1012. [port_idx][copp_idx])));
  1013. rc = adsp_err_get_lnx_err_code(
  1014. atomic_read(&this_adm.copp.stat
  1015. [port_idx][copp_idx]));
  1016. goto send_param_return;
  1017. }
  1018. rc = 0;
  1019. send_param_return:
  1020. kfree(adm_params);
  1021. return rc;
  1022. }
  1023. EXPORT_SYMBOL(adm_send_params_v5);
  1024. int adm_get_params_v2(int port_id, int copp_idx, uint32_t module_id,
  1025. uint32_t param_id, uint32_t params_length,
  1026. char *params, uint32_t client_id)
  1027. {
  1028. struct adm_cmd_get_pp_params_v5 *adm_params = NULL;
  1029. int rc = 0, i = 0;
  1030. int port_idx, idx;
  1031. int *params_data = (int *)params;
  1032. uint64_t sz = 0;
  1033. port_id = afe_convert_virtual_to_portid(port_id);
  1034. port_idx = adm_validate_and_get_port_index(port_id);
  1035. if (port_idx < 0) {
  1036. pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
  1037. return -EINVAL;
  1038. }
  1039. sz = (uint64_t)sizeof(struct adm_cmd_get_pp_params_v5) +
  1040. (uint64_t)params_length;
  1041. /*
  1042. * Check if the value of "sz" (which is ultimately assigned to
  1043. * "hdr.pkt_size") crosses U16_MAX.
  1044. */
  1045. if (sz > U16_MAX) {
  1046. pr_err("%s: Invalid params_length\n", __func__);
  1047. return -EINVAL;
  1048. }
  1049. adm_params = kzalloc(sz, GFP_KERNEL);
  1050. if (!adm_params) {
  1051. pr_err("%s: adm params memory alloc failed", __func__);
  1052. return -ENOMEM;
  1053. }
  1054. memcpy(((u8 *)adm_params + sizeof(struct adm_cmd_get_pp_params_v5)),
  1055. params, params_length);
  1056. adm_params->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1057. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  1058. adm_params->hdr.pkt_size = sz;
  1059. adm_params->hdr.src_svc = APR_SVC_ADM;
  1060. adm_params->hdr.src_domain = APR_DOMAIN_APPS;
  1061. adm_params->hdr.src_port = port_id;
  1062. adm_params->hdr.dest_svc = APR_SVC_ADM;
  1063. adm_params->hdr.dest_domain = APR_DOMAIN_ADSP;
  1064. adm_params->hdr.dest_port =
  1065. atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
  1066. adm_params->hdr.token = port_idx << 16 | client_id << 8 | copp_idx;
  1067. adm_params->hdr.opcode = ADM_CMD_GET_PP_PARAMS_V5;
  1068. adm_params->data_payload_addr_lsw = 0;
  1069. adm_params->data_payload_addr_msw = 0;
  1070. adm_params->mem_map_handle = 0;
  1071. adm_params->module_id = module_id;
  1072. adm_params->param_id = param_id;
  1073. adm_params->param_max_size = params_length;
  1074. adm_params->reserved = 0;
  1075. atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
  1076. rc = apr_send_pkt(this_adm.apr, (uint32_t *)adm_params);
  1077. if (rc < 0) {
  1078. pr_err("%s: Failed to Get Params on port_id 0x%x %d\n",
  1079. __func__, port_id, rc);
  1080. rc = -EINVAL;
  1081. goto adm_get_param_return;
  1082. }
  1083. /* Wait for the callback with copp id */
  1084. rc = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
  1085. atomic_read(&this_adm.copp.stat[port_idx][copp_idx]) >= 0,
  1086. msecs_to_jiffies(TIMEOUT_MS));
  1087. if (!rc) {
  1088. pr_err("%s: get params timed out port_id = 0x%x\n", __func__,
  1089. port_id);
  1090. rc = -EINVAL;
  1091. goto adm_get_param_return;
  1092. } else if (atomic_read(&this_adm.copp.stat
  1093. [port_idx][copp_idx]) > 0) {
  1094. pr_err("%s: DSP returned error[%s]\n",
  1095. __func__, adsp_err_get_err_str(
  1096. atomic_read(&this_adm.copp.stat
  1097. [port_idx][copp_idx])));
  1098. rc = adsp_err_get_lnx_err_code(
  1099. atomic_read(&this_adm.copp.stat
  1100. [port_idx][copp_idx]));
  1101. goto adm_get_param_return;
  1102. }
  1103. idx = ADM_GET_PARAMETER_LENGTH * copp_idx;
  1104. if (adm_get_parameters[idx] < 0) {
  1105. pr_err("%s: Size is invalid %d\n", __func__,
  1106. adm_get_parameters[idx]);
  1107. rc = -EINVAL;
  1108. goto adm_get_param_return;
  1109. }
  1110. if ((params_data) &&
  1111. (ARRAY_SIZE(adm_get_parameters) >
  1112. idx) &&
  1113. (ARRAY_SIZE(adm_get_parameters) >=
  1114. 1+adm_get_parameters[idx]+idx) &&
  1115. (params_length/sizeof(uint32_t) >=
  1116. adm_get_parameters[idx])) {
  1117. for (i = 0; i < adm_get_parameters[idx]; i++)
  1118. params_data[i] = adm_get_parameters[1+i+idx];
  1119. } else {
  1120. pr_err("%s: Get param data not copied! get_param array size %zd, index %d, params array size %zd, index %d\n",
  1121. __func__, ARRAY_SIZE(adm_get_parameters),
  1122. (1+adm_get_parameters[idx]+idx),
  1123. params_length/sizeof(int),
  1124. adm_get_parameters[idx]);
  1125. }
  1126. rc = 0;
  1127. adm_get_param_return:
  1128. kfree(adm_params);
  1129. return rc;
  1130. }
  1131. /**
  1132. * adm_get_params -
  1133. * command to retrieve ADM params for given module
  1134. *
  1135. * @port_id: Port ID number
  1136. * @copp_idx: copp index of ADM copp
  1137. * @module_id: module ID
  1138. * @param_id: Param index
  1139. * @param_length: length of params
  1140. * @params: params pointer
  1141. *
  1142. * Returns 0 on success or error on failure
  1143. */
  1144. int adm_get_params(int port_id, int copp_idx, uint32_t module_id,
  1145. uint32_t param_id, uint32_t params_length, char *params)
  1146. {
  1147. return adm_get_params_v2(port_id, copp_idx, module_id, param_id,
  1148. params_length, params, 0);
  1149. }
  1150. EXPORT_SYMBOL(adm_get_params);
  1151. /**
  1152. * adm_get_pp_topo_module_list -
  1153. * command to update PP top module list
  1154. *
  1155. * @port_id: Port ID number
  1156. * @copp_idx: copp index of ADM copp
  1157. * @param_length: length of params
  1158. * @params: pointer with PP top module params
  1159. *
  1160. * Returns 0 on success or error on failure
  1161. */
  1162. int adm_get_pp_topo_module_list(int port_id, int copp_idx, int32_t param_length,
  1163. char *params)
  1164. {
  1165. struct adm_cmd_get_pp_topo_module_list_t *adm_pp_module_list = NULL;
  1166. int sz, rc = 0, i = 0;
  1167. int port_idx, idx;
  1168. int32_t *params_data = (int32_t *)params;
  1169. int *topo_list;
  1170. pr_debug("%s : port_id %x", __func__, port_id);
  1171. port_id = afe_convert_virtual_to_portid(port_id);
  1172. port_idx = adm_validate_and_get_port_index(port_id);
  1173. if (port_idx < 0) {
  1174. pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
  1175. return -EINVAL;
  1176. }
  1177. if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
  1178. pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
  1179. return -EINVAL;
  1180. }
  1181. sz = sizeof(struct adm_cmd_get_pp_topo_module_list_t) + param_length;
  1182. adm_pp_module_list = kzalloc(sz, GFP_KERNEL);
  1183. if (!adm_pp_module_list) {
  1184. pr_err("%s, adm params memory alloc failed", __func__);
  1185. return -ENOMEM;
  1186. }
  1187. memcpy(((u8 *)adm_pp_module_list +
  1188. sizeof(struct adm_cmd_get_pp_topo_module_list_t)),
  1189. params, param_length);
  1190. adm_pp_module_list->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1191. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  1192. adm_pp_module_list->hdr.pkt_size = sz;
  1193. adm_pp_module_list->hdr.src_svc = APR_SVC_ADM;
  1194. adm_pp_module_list->hdr.src_domain = APR_DOMAIN_APPS;
  1195. adm_pp_module_list->hdr.src_port = port_id;
  1196. adm_pp_module_list->hdr.dest_svc = APR_SVC_ADM;
  1197. adm_pp_module_list->hdr.dest_domain = APR_DOMAIN_ADSP;
  1198. adm_pp_module_list->hdr.dest_port =
  1199. atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
  1200. adm_pp_module_list->hdr.token = port_idx << 16 | copp_idx;
  1201. adm_pp_module_list->hdr.opcode = ADM_CMD_GET_PP_TOPO_MODULE_LIST;
  1202. adm_pp_module_list->param_max_size = param_length;
  1203. /* Payload address and mmap handle set to zero by kzalloc */
  1204. atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
  1205. rc = apr_send_pkt(this_adm.apr, (uint32_t *)adm_pp_module_list);
  1206. if (rc < 0) {
  1207. pr_err("%s: Failed to Get Params on port %d\n", __func__,
  1208. port_id);
  1209. rc = -EINVAL;
  1210. goto adm_pp_module_list_l;
  1211. }
  1212. /* Wait for the callback with copp id */
  1213. rc = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
  1214. atomic_read(&this_adm.copp.stat[port_idx][copp_idx]) >= 0,
  1215. msecs_to_jiffies(TIMEOUT_MS));
  1216. if (!rc) {
  1217. pr_err("%s: get params timed out port = %d\n", __func__,
  1218. port_id);
  1219. rc = -EINVAL;
  1220. goto adm_pp_module_list_l;
  1221. } else if (atomic_read(&this_adm.copp.stat
  1222. [port_idx][copp_idx]) > 0) {
  1223. pr_err("%s: DSP returned error[%s]\n",
  1224. __func__, adsp_err_get_err_str(
  1225. atomic_read(&this_adm.copp.stat
  1226. [port_idx][copp_idx])));
  1227. rc = adsp_err_get_lnx_err_code(
  1228. atomic_read(&this_adm.copp.stat
  1229. [port_idx][copp_idx]));
  1230. goto adm_pp_module_list_l;
  1231. }
  1232. if (params_data) {
  1233. idx = ADM_GET_TOPO_MODULE_LIST_LENGTH * copp_idx;
  1234. topo_list = (int *)(adm_module_topo_list + idx);
  1235. if (param_length <= ADM_GET_TOPO_MODULE_LIST_LENGTH &&
  1236. idx <
  1237. (MAX_COPPS_PER_PORT * ADM_GET_TOPO_MODULE_LIST_LENGTH))
  1238. memcpy(params_data, topo_list, param_length);
  1239. else
  1240. pr_debug("%s: i/p size:%d > MAX param size:%d\n",
  1241. __func__, param_length,
  1242. (int)ADM_GET_TOPO_MODULE_LIST_LENGTH);
  1243. for (i = 1; i <= params_data[0]; i++)
  1244. pr_debug("module = 0x%x\n", params_data[i]);
  1245. }
  1246. rc = 0;
  1247. adm_pp_module_list_l:
  1248. kfree(adm_pp_module_list);
  1249. pr_debug("%s : rc = %d ", __func__, rc);
  1250. return rc;
  1251. }
  1252. EXPORT_SYMBOL(adm_get_pp_topo_module_list);
  1253. static void adm_callback_debug_print(struct apr_client_data *data)
  1254. {
  1255. uint32_t *payload;
  1256. payload = data->payload;
  1257. if (data->payload_size >= 8)
  1258. pr_debug("%s: code = 0x%x PL#0[0x%x], PL#1[0x%x], size = %d\n",
  1259. __func__, data->opcode, payload[0], payload[1],
  1260. data->payload_size);
  1261. else if (data->payload_size >= 4)
  1262. pr_debug("%s: code = 0x%x PL#0[0x%x], size = %d\n",
  1263. __func__, data->opcode, payload[0],
  1264. data->payload_size);
  1265. else
  1266. pr_debug("%s: code = 0x%x, size = %d\n",
  1267. __func__, data->opcode, data->payload_size);
  1268. }
  1269. /**
  1270. * adm_set_multi_ch_map -
  1271. * Update multi channel map info
  1272. *
  1273. * @channel_map: pointer with channel map info
  1274. * @path: direction or ADM path type
  1275. *
  1276. * Returns 0 on success or error on failure
  1277. */
  1278. int adm_set_multi_ch_map(char *channel_map, int path)
  1279. {
  1280. int idx;
  1281. if (path == ADM_PATH_PLAYBACK) {
  1282. idx = ADM_MCH_MAP_IDX_PLAYBACK;
  1283. } else if (path == ADM_PATH_LIVE_REC) {
  1284. idx = ADM_MCH_MAP_IDX_REC;
  1285. } else {
  1286. pr_err("%s: invalid attempt to set path %d\n", __func__, path);
  1287. return -EINVAL;
  1288. }
  1289. memcpy(multi_ch_maps[idx].channel_mapping, channel_map,
  1290. PCM_FORMAT_MAX_NUM_CHANNEL);
  1291. multi_ch_maps[idx].set_channel_map = true;
  1292. return 0;
  1293. }
  1294. EXPORT_SYMBOL(adm_set_multi_ch_map);
  1295. /**
  1296. * adm_get_multi_ch_map -
  1297. * Retrieves multi channel map info
  1298. *
  1299. * @channel_map: pointer to be updated with channel map
  1300. * @path: direction or ADM path type
  1301. *
  1302. * Returns 0 on success or error on failure
  1303. */
  1304. int adm_get_multi_ch_map(char *channel_map, int path)
  1305. {
  1306. int idx;
  1307. if (path == ADM_PATH_PLAYBACK) {
  1308. idx = ADM_MCH_MAP_IDX_PLAYBACK;
  1309. } else if (path == ADM_PATH_LIVE_REC) {
  1310. idx = ADM_MCH_MAP_IDX_REC;
  1311. } else {
  1312. pr_err("%s: invalid attempt to get path %d\n", __func__, path);
  1313. return -EINVAL;
  1314. }
  1315. if (multi_ch_maps[idx].set_channel_map) {
  1316. memcpy(channel_map, multi_ch_maps[idx].channel_mapping,
  1317. PCM_FORMAT_MAX_NUM_CHANNEL);
  1318. }
  1319. return 0;
  1320. }
  1321. EXPORT_SYMBOL(adm_get_multi_ch_map);
  1322. static int32_t adm_callback(struct apr_client_data *data, void *priv)
  1323. {
  1324. uint32_t *payload;
  1325. int i, j, port_idx, copp_idx, idx, client_id;
  1326. if (data == NULL) {
  1327. pr_err("%s: data parameter is null\n", __func__);
  1328. return -EINVAL;
  1329. }
  1330. payload = data->payload;
  1331. if (data->opcode == RESET_EVENTS) {
  1332. pr_debug("%s: Reset event is received: %d %d apr[%pK]\n",
  1333. __func__,
  1334. data->reset_event, data->reset_proc, this_adm.apr);
  1335. if (this_adm.apr) {
  1336. apr_reset(this_adm.apr);
  1337. for (i = 0; i < AFE_MAX_PORTS; i++) {
  1338. for (j = 0; j < MAX_COPPS_PER_PORT; j++) {
  1339. atomic_set(&this_adm.copp.id[i][j],
  1340. RESET_COPP_ID);
  1341. atomic_set(&this_adm.copp.cnt[i][j], 0);
  1342. atomic_set(
  1343. &this_adm.copp.topology[i][j], 0);
  1344. atomic_set(&this_adm.copp.mode[i][j],
  1345. 0);
  1346. atomic_set(&this_adm.copp.stat[i][j],
  1347. 0);
  1348. atomic_set(&this_adm.copp.rate[i][j],
  1349. 0);
  1350. atomic_set(
  1351. &this_adm.copp.channels[i][j],
  1352. 0);
  1353. atomic_set(
  1354. &this_adm.copp.bit_width[i][j], 0);
  1355. atomic_set(
  1356. &this_adm.copp.app_type[i][j], 0);
  1357. atomic_set(
  1358. &this_adm.copp.acdb_id[i][j], 0);
  1359. this_adm.copp.adm_status[i][j] =
  1360. ADM_STATUS_CALIBRATION_REQUIRED;
  1361. }
  1362. }
  1363. this_adm.apr = NULL;
  1364. cal_utils_clear_cal_block_q6maps(ADM_MAX_CAL_TYPES,
  1365. this_adm.cal_data);
  1366. mutex_lock(&this_adm.cal_data
  1367. [ADM_CUSTOM_TOP_CAL]->lock);
  1368. this_adm.set_custom_topology = 1;
  1369. mutex_unlock(&this_adm.cal_data[
  1370. ADM_CUSTOM_TOP_CAL]->lock);
  1371. rtac_clear_mapping(ADM_RTAC_CAL);
  1372. /*
  1373. * Free the ION memory and clear the map handles
  1374. * for Source Tracking
  1375. */
  1376. if (this_adm.sourceTrackingData.memmap.paddr != 0) {
  1377. msm_audio_ion_free(
  1378. this_adm.sourceTrackingData.ion_client,
  1379. this_adm.sourceTrackingData.ion_handle);
  1380. this_adm.sourceTrackingData.ion_client = NULL;
  1381. this_adm.sourceTrackingData.ion_handle = NULL;
  1382. this_adm.sourceTrackingData.memmap.size = 0;
  1383. this_adm.sourceTrackingData.memmap.kvaddr =
  1384. NULL;
  1385. this_adm.sourceTrackingData.memmap.paddr = 0;
  1386. this_adm.sourceTrackingData.apr_cmd_status = -1;
  1387. atomic_set(&this_adm.mem_map_handles[
  1388. ADM_MEM_MAP_INDEX_SOURCE_TRACKING], 0);
  1389. }
  1390. }
  1391. return 0;
  1392. }
  1393. adm_callback_debug_print(data);
  1394. if (data->payload_size) {
  1395. copp_idx = (data->token) & 0XFF;
  1396. port_idx = ((data->token) >> 16) & 0xFF;
  1397. client_id = ((data->token) >> 8) & 0xFF;
  1398. if (port_idx < 0 || port_idx >= AFE_MAX_PORTS) {
  1399. pr_err("%s: Invalid port idx %d token %d\n",
  1400. __func__, port_idx, data->token);
  1401. return 0;
  1402. }
  1403. if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
  1404. pr_err("%s: Invalid copp idx %d token %d\n",
  1405. __func__, copp_idx, data->token);
  1406. return 0;
  1407. }
  1408. if (client_id < 0 || client_id >= ADM_CLIENT_ID_MAX) {
  1409. pr_err("%s: Invalid client id %d\n", __func__,
  1410. client_id);
  1411. return 0;
  1412. }
  1413. if (data->opcode == APR_BASIC_RSP_RESULT) {
  1414. pr_debug("%s: APR_BASIC_RSP_RESULT id 0x%x\n",
  1415. __func__, payload[0]);
  1416. if (payload[1] != 0) {
  1417. pr_err("%s: cmd = 0x%x returned error = 0x%x\n",
  1418. __func__, payload[0], payload[1]);
  1419. }
  1420. switch (payload[0]) {
  1421. case ADM_CMD_SET_PP_PARAMS_V5:
  1422. pr_debug("%s: ADM_CMD_SET_PP_PARAMS_V5\n",
  1423. __func__);
  1424. if (client_id == ADM_CLIENT_ID_SOURCE_TRACKING)
  1425. this_adm.sourceTrackingData.
  1426. apr_cmd_status = payload[1];
  1427. else if (rtac_make_adm_callback(payload,
  1428. data->payload_size))
  1429. break;
  1430. /*
  1431. * if soft volume is called and already
  1432. * interrupted break out of the sequence here
  1433. */
  1434. case ADM_CMD_DEVICE_OPEN_V5:
  1435. case ADM_CMD_DEVICE_CLOSE_V5:
  1436. case ADM_CMD_DEVICE_OPEN_V6:
  1437. pr_debug("%s: Basic callback received, wake up.\n",
  1438. __func__);
  1439. atomic_set(&this_adm.copp.stat[port_idx]
  1440. [copp_idx], payload[1]);
  1441. wake_up(
  1442. &this_adm.copp.wait[port_idx][copp_idx]);
  1443. break;
  1444. case ADM_CMD_ADD_TOPOLOGIES:
  1445. pr_debug("%s: callback received, ADM_CMD_ADD_TOPOLOGIES.\n",
  1446. __func__);
  1447. atomic_set(&this_adm.adm_stat, payload[1]);
  1448. wake_up(&this_adm.adm_wait);
  1449. break;
  1450. case ADM_CMD_MATRIX_MAP_ROUTINGS_V5:
  1451. case ADM_CMD_STREAM_DEVICE_MAP_ROUTINGS_V5:
  1452. pr_debug("%s: Basic callback received, wake up.\n",
  1453. __func__);
  1454. atomic_set(&this_adm.matrix_map_stat,
  1455. payload[1]);
  1456. wake_up(&this_adm.matrix_map_wait);
  1457. break;
  1458. case ADM_CMD_SHARED_MEM_UNMAP_REGIONS:
  1459. pr_debug("%s: ADM_CMD_SHARED_MEM_UNMAP_REGIONS\n",
  1460. __func__);
  1461. atomic_set(&this_adm.adm_stat, payload[1]);
  1462. wake_up(&this_adm.adm_wait);
  1463. break;
  1464. case ADM_CMD_SHARED_MEM_MAP_REGIONS:
  1465. pr_debug("%s: ADM_CMD_SHARED_MEM_MAP_REGIONS\n",
  1466. __func__);
  1467. /* Should only come here if there is an APR */
  1468. /* error or malformed APR packet. Otherwise */
  1469. /* response will be returned as */
  1470. if (payload[1] != 0) {
  1471. pr_err("%s: ADM map error, resuming\n",
  1472. __func__);
  1473. atomic_set(&this_adm.adm_stat,
  1474. payload[1]);
  1475. wake_up(&this_adm.adm_wait);
  1476. }
  1477. break;
  1478. case ADM_CMD_GET_PP_PARAMS_V5:
  1479. pr_debug("%s: ADM_CMD_GET_PP_PARAMS_V5\n",
  1480. __func__);
  1481. /* Should only come here if there is an APR */
  1482. /* error or malformed APR packet. Otherwise */
  1483. /* response will be returned as */
  1484. /* ADM_CMDRSP_GET_PP_PARAMS_V5 */
  1485. if (client_id ==
  1486. ADM_CLIENT_ID_SOURCE_TRACKING) {
  1487. this_adm.sourceTrackingData.
  1488. apr_cmd_status = payload[1];
  1489. if (payload[1] != 0)
  1490. pr_err("%s: ADM get param error = %d\n",
  1491. __func__, payload[1]);
  1492. atomic_set(&this_adm.copp.stat
  1493. [port_idx][copp_idx],
  1494. payload[1]);
  1495. wake_up(&this_adm.copp.wait
  1496. [port_idx][copp_idx]);
  1497. } else {
  1498. if (payload[1] != 0) {
  1499. pr_err("%s: ADM get param error = %d, resuming\n",
  1500. __func__, payload[1]);
  1501. rtac_make_adm_callback(payload,
  1502. data->payload_size);
  1503. }
  1504. }
  1505. break;
  1506. case ADM_CMD_SET_PSPD_MTMX_STRTR_PARAMS_V5:
  1507. pr_debug("%s: ADM_CMD_SET_PSPD_MTMX_STRTR_PARAMS_V5\n",
  1508. __func__);
  1509. atomic_set(&this_adm.copp.stat[port_idx]
  1510. [copp_idx], payload[1]);
  1511. wake_up(
  1512. &this_adm.copp.wait[port_idx][copp_idx]);
  1513. break;
  1514. case ADM_CMD_GET_PP_TOPO_MODULE_LIST:
  1515. pr_debug("%s:ADM_CMD_GET_PP_TOPO_MODULE_LIST\n",
  1516. __func__);
  1517. if (payload[1] != 0)
  1518. pr_err("%s: ADM get topo list error = %d,\n",
  1519. __func__, payload[1]);
  1520. break;
  1521. default:
  1522. pr_err("%s: Unknown Cmd: 0x%x\n", __func__,
  1523. payload[0]);
  1524. break;
  1525. }
  1526. return 0;
  1527. }
  1528. switch (data->opcode) {
  1529. case ADM_CMDRSP_DEVICE_OPEN_V5:
  1530. case ADM_CMDRSP_DEVICE_OPEN_V6: {
  1531. struct adm_cmd_rsp_device_open_v5 *open =
  1532. (struct adm_cmd_rsp_device_open_v5 *)data->payload;
  1533. if (open->copp_id == INVALID_COPP_ID) {
  1534. pr_err("%s: invalid coppid rxed %d\n",
  1535. __func__, open->copp_id);
  1536. atomic_set(&this_adm.copp.stat[port_idx]
  1537. [copp_idx], ADSP_EBADPARAM);
  1538. wake_up(
  1539. &this_adm.copp.wait[port_idx][copp_idx]);
  1540. break;
  1541. }
  1542. atomic_set(&this_adm.copp.stat
  1543. [port_idx][copp_idx], payload[0]);
  1544. atomic_set(&this_adm.copp.id[port_idx][copp_idx],
  1545. open->copp_id);
  1546. pr_debug("%s: coppid rxed=%d\n", __func__,
  1547. open->copp_id);
  1548. wake_up(&this_adm.copp.wait[port_idx][copp_idx]);
  1549. }
  1550. break;
  1551. case ADM_CMDRSP_GET_PP_PARAMS_V5:
  1552. pr_debug("%s: ADM_CMDRSP_GET_PP_PARAMS_V5\n", __func__);
  1553. if (payload[0] != 0)
  1554. pr_err("%s: ADM_CMDRSP_GET_PP_PARAMS_V5 returned error = 0x%x\n",
  1555. __func__, payload[0]);
  1556. if (client_id == ADM_CLIENT_ID_SOURCE_TRACKING)
  1557. this_adm.sourceTrackingData.apr_cmd_status =
  1558. payload[0];
  1559. else if (rtac_make_adm_callback(payload,
  1560. data->payload_size))
  1561. break;
  1562. idx = ADM_GET_PARAMETER_LENGTH * copp_idx;
  1563. if ((payload[0] == 0) && (data->payload_size >
  1564. (4 * sizeof(*payload))) &&
  1565. (data->payload_size - 4 >=
  1566. payload[3]) &&
  1567. (ARRAY_SIZE(adm_get_parameters) >
  1568. idx) &&
  1569. (ARRAY_SIZE(adm_get_parameters)-idx-1 >=
  1570. payload[3])) {
  1571. adm_get_parameters[idx] = payload[3] /
  1572. sizeof(uint32_t);
  1573. /*
  1574. * payload[3] is param_size which is
  1575. * expressed in number of bytes
  1576. */
  1577. pr_debug("%s: GET_PP PARAM:received parameter length: 0x%x\n",
  1578. __func__, adm_get_parameters[idx]);
  1579. /* storing param size then params */
  1580. for (i = 0; i < payload[3] /
  1581. sizeof(uint32_t); i++)
  1582. adm_get_parameters[idx+1+i] =
  1583. payload[4+i];
  1584. } else if (payload[0] == 0) {
  1585. adm_get_parameters[idx] = -1;
  1586. pr_err("%s: Out of band case, setting size to %d\n",
  1587. __func__, adm_get_parameters[idx]);
  1588. } else {
  1589. adm_get_parameters[idx] = -1;
  1590. pr_err("%s: GET_PP_PARAMS failed, setting size to %d\n",
  1591. __func__, adm_get_parameters[idx]);
  1592. }
  1593. atomic_set(&this_adm.copp.stat
  1594. [port_idx][copp_idx], payload[0]);
  1595. wake_up(&this_adm.copp.wait[port_idx][copp_idx]);
  1596. break;
  1597. case ADM_CMDRSP_GET_PP_TOPO_MODULE_LIST:
  1598. pr_debug("%s: ADM_CMDRSP_GET_PP_TOPO_MODULE_LIST\n",
  1599. __func__);
  1600. if (payload[0] != 0) {
  1601. pr_err("%s: ADM_CMDRSP_GET_PP_TOPO_MODULE_LIST",
  1602. __func__);
  1603. pr_err(":err = 0x%x\n", payload[0]);
  1604. } else if (payload[1] >
  1605. ((ADM_GET_TOPO_MODULE_LIST_LENGTH /
  1606. sizeof(uint32_t)) - 1)) {
  1607. pr_err("%s: ADM_CMDRSP_GET_PP_TOPO_MODULE_LIST",
  1608. __func__);
  1609. pr_err(":size = %d\n", payload[1]);
  1610. } else {
  1611. idx = ADM_GET_TOPO_MODULE_LIST_LENGTH *
  1612. copp_idx;
  1613. pr_debug("%s:Num modules payload[1] %d\n",
  1614. __func__, payload[1]);
  1615. adm_module_topo_list[idx] = payload[1];
  1616. for (i = 1; i <= payload[1]; i++) {
  1617. adm_module_topo_list[idx+i] =
  1618. payload[1+i];
  1619. pr_debug("%s:payload[%d] = %x\n",
  1620. __func__, (i+1), payload[1+i]);
  1621. }
  1622. }
  1623. atomic_set(&this_adm.copp.stat
  1624. [port_idx][copp_idx], payload[0]);
  1625. wake_up(&this_adm.copp.wait[port_idx][copp_idx]);
  1626. break;
  1627. case ADM_CMDRSP_SHARED_MEM_MAP_REGIONS:
  1628. pr_debug("%s: ADM_CMDRSP_SHARED_MEM_MAP_REGIONS\n",
  1629. __func__);
  1630. atomic_set(&this_adm.mem_map_handles[
  1631. atomic_read(&this_adm.mem_map_index)],
  1632. *payload);
  1633. atomic_set(&this_adm.adm_stat, 0);
  1634. wake_up(&this_adm.adm_wait);
  1635. break;
  1636. default:
  1637. pr_err("%s: Unknown cmd:0x%x\n", __func__,
  1638. data->opcode);
  1639. break;
  1640. }
  1641. }
  1642. return 0;
  1643. }
  1644. static int adm_memory_map_regions(phys_addr_t *buf_add, uint32_t mempool_id,
  1645. uint32_t *bufsz, uint32_t bufcnt)
  1646. {
  1647. struct avs_cmd_shared_mem_map_regions *mmap_regions = NULL;
  1648. struct avs_shared_map_region_payload *mregions = NULL;
  1649. void *mmap_region_cmd = NULL;
  1650. void *payload = NULL;
  1651. int ret = 0;
  1652. int i = 0;
  1653. int cmd_size = 0;
  1654. pr_debug("%s:\n", __func__);
  1655. if (this_adm.apr == NULL) {
  1656. this_adm.apr = apr_register("ADSP", "ADM", adm_callback,
  1657. 0xFFFFFFFF, &this_adm);
  1658. if (this_adm.apr == NULL) {
  1659. pr_err("%s: Unable to register ADM\n", __func__);
  1660. ret = -ENODEV;
  1661. return ret;
  1662. }
  1663. rtac_set_adm_handle(this_adm.apr);
  1664. }
  1665. cmd_size = sizeof(struct avs_cmd_shared_mem_map_regions)
  1666. + sizeof(struct avs_shared_map_region_payload)
  1667. * bufcnt;
  1668. mmap_region_cmd = kzalloc(cmd_size, GFP_KERNEL);
  1669. if (!mmap_region_cmd)
  1670. return -ENOMEM;
  1671. mmap_regions = (struct avs_cmd_shared_mem_map_regions *)mmap_region_cmd;
  1672. mmap_regions->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1673. APR_HDR_LEN(APR_HDR_SIZE),
  1674. APR_PKT_VER);
  1675. mmap_regions->hdr.pkt_size = cmd_size;
  1676. mmap_regions->hdr.src_port = 0;
  1677. mmap_regions->hdr.dest_port = 0;
  1678. mmap_regions->hdr.token = 0;
  1679. mmap_regions->hdr.opcode = ADM_CMD_SHARED_MEM_MAP_REGIONS;
  1680. mmap_regions->mem_pool_id = ADSP_MEMORY_MAP_SHMEM8_4K_POOL & 0x00ff;
  1681. mmap_regions->num_regions = bufcnt & 0x00ff;
  1682. mmap_regions->property_flag = 0x00;
  1683. pr_debug("%s: map_regions->num_regions = %d\n", __func__,
  1684. mmap_regions->num_regions);
  1685. payload = ((u8 *) mmap_region_cmd +
  1686. sizeof(struct avs_cmd_shared_mem_map_regions));
  1687. mregions = (struct avs_shared_map_region_payload *)payload;
  1688. for (i = 0; i < bufcnt; i++) {
  1689. mregions->shm_addr_lsw = lower_32_bits(buf_add[i]);
  1690. mregions->shm_addr_msw =
  1691. msm_audio_populate_upper_32_bits(buf_add[i]);
  1692. mregions->mem_size_bytes = bufsz[i];
  1693. ++mregions;
  1694. }
  1695. atomic_set(&this_adm.adm_stat, -1);
  1696. ret = apr_send_pkt(this_adm.apr, (uint32_t *) mmap_region_cmd);
  1697. if (ret < 0) {
  1698. pr_err("%s: mmap_regions op[0x%x]rc[%d]\n", __func__,
  1699. mmap_regions->hdr.opcode, ret);
  1700. ret = -EINVAL;
  1701. goto fail_cmd;
  1702. }
  1703. ret = wait_event_timeout(this_adm.adm_wait,
  1704. atomic_read(&this_adm.adm_stat) >= 0,
  1705. 5 * HZ);
  1706. if (!ret) {
  1707. pr_err("%s: timeout. waited for memory_map\n", __func__);
  1708. ret = -EINVAL;
  1709. goto fail_cmd;
  1710. } else if (atomic_read(&this_adm.adm_stat) > 0) {
  1711. pr_err("%s: DSP returned error[%s]\n",
  1712. __func__, adsp_err_get_err_str(
  1713. atomic_read(&this_adm.adm_stat)));
  1714. ret = adsp_err_get_lnx_err_code(
  1715. atomic_read(&this_adm.adm_stat));
  1716. goto fail_cmd;
  1717. }
  1718. fail_cmd:
  1719. kfree(mmap_region_cmd);
  1720. return ret;
  1721. }
  1722. static int adm_memory_unmap_regions(void)
  1723. {
  1724. struct avs_cmd_shared_mem_unmap_regions unmap_regions;
  1725. int ret = 0;
  1726. pr_debug("%s:\n", __func__);
  1727. if (this_adm.apr == NULL) {
  1728. pr_err("%s: APR handle NULL\n", __func__);
  1729. return -EINVAL;
  1730. }
  1731. unmap_regions.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1732. APR_HDR_LEN(APR_HDR_SIZE),
  1733. APR_PKT_VER);
  1734. unmap_regions.hdr.pkt_size = sizeof(unmap_regions);
  1735. unmap_regions.hdr.src_port = 0;
  1736. unmap_regions.hdr.dest_port = 0;
  1737. unmap_regions.hdr.token = 0;
  1738. unmap_regions.hdr.opcode = ADM_CMD_SHARED_MEM_UNMAP_REGIONS;
  1739. unmap_regions.mem_map_handle = atomic_read(&this_adm.
  1740. mem_map_handles[atomic_read(&this_adm.mem_map_index)]);
  1741. atomic_set(&this_adm.adm_stat, -1);
  1742. ret = apr_send_pkt(this_adm.apr, (uint32_t *) &unmap_regions);
  1743. if (ret < 0) {
  1744. pr_err("%s: mmap_regions op[0x%x]rc[%d]\n", __func__,
  1745. unmap_regions.hdr.opcode, ret);
  1746. ret = -EINVAL;
  1747. goto fail_cmd;
  1748. }
  1749. ret = wait_event_timeout(this_adm.adm_wait,
  1750. atomic_read(&this_adm.adm_stat) >= 0,
  1751. 5 * HZ);
  1752. if (!ret) {
  1753. pr_err("%s: timeout. waited for memory_unmap\n",
  1754. __func__);
  1755. ret = -EINVAL;
  1756. goto fail_cmd;
  1757. } else if (atomic_read(&this_adm.adm_stat) > 0) {
  1758. pr_err("%s: DSP returned error[%s]\n",
  1759. __func__, adsp_err_get_err_str(
  1760. atomic_read(&this_adm.adm_stat)));
  1761. ret = adsp_err_get_lnx_err_code(
  1762. atomic_read(&this_adm.adm_stat));
  1763. goto fail_cmd;
  1764. } else {
  1765. pr_debug("%s: Unmap handle 0x%x succeeded\n", __func__,
  1766. unmap_regions.mem_map_handle);
  1767. }
  1768. fail_cmd:
  1769. return ret;
  1770. }
  1771. static int remap_cal_data(struct cal_block_data *cal_block, int cal_index)
  1772. {
  1773. int ret = 0;
  1774. if (cal_block->map_data.ion_client == NULL) {
  1775. pr_err("%s: No ION allocation for cal index %d!\n",
  1776. __func__, cal_index);
  1777. ret = -EINVAL;
  1778. goto done;
  1779. }
  1780. if ((cal_block->map_data.map_size > 0) &&
  1781. (cal_block->map_data.q6map_handle == 0)) {
  1782. atomic_set(&this_adm.mem_map_index, cal_index);
  1783. ret = adm_memory_map_regions(&cal_block->cal_data.paddr, 0,
  1784. (uint32_t *)&cal_block->map_data.map_size, 1);
  1785. if (ret < 0) {
  1786. pr_err("%s: ADM mmap did not work! size = %zd ret %d\n",
  1787. __func__,
  1788. cal_block->map_data.map_size, ret);
  1789. pr_debug("%s: ADM mmap did not work! addr = 0x%pK, size = %zd ret %d\n",
  1790. __func__,
  1791. &cal_block->cal_data.paddr,
  1792. cal_block->map_data.map_size, ret);
  1793. goto done;
  1794. }
  1795. cal_block->map_data.q6map_handle = atomic_read(&this_adm.
  1796. mem_map_handles[cal_index]);
  1797. }
  1798. done:
  1799. return ret;
  1800. }
  1801. static void send_adm_custom_topology(void)
  1802. {
  1803. struct cal_block_data *cal_block = NULL;
  1804. struct cmd_set_topologies adm_top;
  1805. int cal_index = ADM_CUSTOM_TOP_CAL;
  1806. int result;
  1807. if (this_adm.cal_data[cal_index] == NULL)
  1808. goto done;
  1809. mutex_lock(&this_adm.cal_data[cal_index]->lock);
  1810. if (!this_adm.set_custom_topology)
  1811. goto unlock;
  1812. this_adm.set_custom_topology = 0;
  1813. cal_block = cal_utils_get_only_cal_block(this_adm.cal_data[cal_index]);
  1814. if (cal_block == NULL)
  1815. goto unlock;
  1816. pr_debug("%s: Sending cal_index %d\n", __func__, cal_index);
  1817. result = remap_cal_data(cal_block, cal_index);
  1818. if (result) {
  1819. pr_err("%s: Remap_cal_data failed for cal %d!\n",
  1820. __func__, cal_index);
  1821. goto unlock;
  1822. }
  1823. atomic_set(&this_adm.mem_map_index, cal_index);
  1824. atomic_set(&this_adm.mem_map_handles[cal_index],
  1825. cal_block->map_data.q6map_handle);
  1826. if (cal_block->cal_data.size == 0) {
  1827. pr_debug("%s: No ADM cal to send\n", __func__);
  1828. goto unlock;
  1829. }
  1830. adm_top.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1831. APR_HDR_LEN(20), APR_PKT_VER);
  1832. adm_top.hdr.pkt_size = sizeof(adm_top);
  1833. adm_top.hdr.src_svc = APR_SVC_ADM;
  1834. adm_top.hdr.src_domain = APR_DOMAIN_APPS;
  1835. adm_top.hdr.src_port = 0;
  1836. adm_top.hdr.dest_svc = APR_SVC_ADM;
  1837. adm_top.hdr.dest_domain = APR_DOMAIN_ADSP;
  1838. adm_top.hdr.dest_port = 0;
  1839. adm_top.hdr.token = 0;
  1840. adm_top.hdr.opcode = ADM_CMD_ADD_TOPOLOGIES;
  1841. adm_top.payload_addr_lsw = lower_32_bits(cal_block->cal_data.paddr);
  1842. adm_top.payload_addr_msw = msm_audio_populate_upper_32_bits(
  1843. cal_block->cal_data.paddr);
  1844. adm_top.mem_map_handle = cal_block->map_data.q6map_handle;
  1845. adm_top.payload_size = cal_block->cal_data.size;
  1846. atomic_set(&this_adm.adm_stat, -1);
  1847. pr_debug("%s: Sending ADM_CMD_ADD_TOPOLOGIES payload = 0x%pK, size = %d\n",
  1848. __func__, &cal_block->cal_data.paddr,
  1849. adm_top.payload_size);
  1850. result = apr_send_pkt(this_adm.apr, (uint32_t *)&adm_top);
  1851. if (result < 0) {
  1852. pr_err("%s: Set topologies failed payload size = %zd result %d\n",
  1853. __func__, cal_block->cal_data.size, result);
  1854. goto unlock;
  1855. }
  1856. /* Wait for the callback */
  1857. result = wait_event_timeout(this_adm.adm_wait,
  1858. atomic_read(&this_adm.adm_stat) >= 0,
  1859. msecs_to_jiffies(TIMEOUT_MS));
  1860. if (!result) {
  1861. pr_err("%s: Set topologies timed out payload size = %zd\n",
  1862. __func__, cal_block->cal_data.size);
  1863. goto unlock;
  1864. } else if (atomic_read(&this_adm.adm_stat) > 0) {
  1865. pr_err("%s: DSP returned error[%s]\n",
  1866. __func__, adsp_err_get_err_str(
  1867. atomic_read(&this_adm.adm_stat)));
  1868. result = adsp_err_get_lnx_err_code(
  1869. atomic_read(&this_adm.adm_stat));
  1870. goto unlock;
  1871. }
  1872. unlock:
  1873. mutex_unlock(&this_adm.cal_data[cal_index]->lock);
  1874. done:
  1875. return;
  1876. }
  1877. static int send_adm_cal_block(int port_id, int copp_idx,
  1878. struct cal_block_data *cal_block, int perf_mode,
  1879. int app_type, int acdb_id, int sample_rate)
  1880. {
  1881. s32 result = 0;
  1882. struct adm_cmd_set_pp_params_v5 adm_params;
  1883. int port_idx;
  1884. pr_debug("%s: Port id 0x%x sample_rate %d ,\n", __func__,
  1885. port_id, sample_rate);
  1886. port_id = afe_convert_virtual_to_portid(port_id);
  1887. port_idx = adm_validate_and_get_port_index(port_id);
  1888. if (port_idx < 0) {
  1889. pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
  1890. return -EINVAL;
  1891. }
  1892. if (!cal_block) {
  1893. pr_debug("%s: No ADM cal to send for port_id = 0x%x!\n",
  1894. __func__, port_id);
  1895. result = -EINVAL;
  1896. goto done;
  1897. }
  1898. if (cal_block->cal_data.size <= 0) {
  1899. pr_debug("%s: No ADM cal send for port_id = 0x%x!\n",
  1900. __func__, port_id);
  1901. result = -EINVAL;
  1902. goto done;
  1903. }
  1904. if (perf_mode == LEGACY_PCM_MODE &&
  1905. ((atomic_read(&this_adm.copp.topology[port_idx][copp_idx])) ==
  1906. DS2_ADM_COPP_TOPOLOGY_ID)) {
  1907. pr_err("%s: perf_mode %d, topology 0x%x\n", __func__, perf_mode,
  1908. atomic_read(
  1909. &this_adm.copp.topology[port_idx][copp_idx]));
  1910. goto done;
  1911. }
  1912. adm_params.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1913. APR_HDR_LEN(20), APR_PKT_VER);
  1914. adm_params.hdr.pkt_size = sizeof(adm_params);
  1915. adm_params.hdr.src_svc = APR_SVC_ADM;
  1916. adm_params.hdr.src_domain = APR_DOMAIN_APPS;
  1917. adm_params.hdr.src_port = port_id;
  1918. adm_params.hdr.dest_svc = APR_SVC_ADM;
  1919. adm_params.hdr.dest_domain = APR_DOMAIN_ADSP;
  1920. adm_params.hdr.token = port_idx << 16 | copp_idx;
  1921. adm_params.hdr.dest_port =
  1922. atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
  1923. adm_params.hdr.opcode = ADM_CMD_SET_PP_PARAMS_V5;
  1924. adm_params.payload_addr_lsw = lower_32_bits(cal_block->cal_data.paddr);
  1925. adm_params.payload_addr_msw = msm_audio_populate_upper_32_bits(
  1926. cal_block->cal_data.paddr);
  1927. adm_params.mem_map_handle = cal_block->map_data.q6map_handle;
  1928. adm_params.payload_size = cal_block->cal_data.size;
  1929. atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
  1930. pr_debug("%s: Sending SET_PARAMS payload = 0x%pK, size = %d\n",
  1931. __func__, &cal_block->cal_data.paddr,
  1932. adm_params.payload_size);
  1933. result = apr_send_pkt(this_adm.apr, (uint32_t *)&adm_params);
  1934. if (result < 0) {
  1935. pr_err("%s: Set params failed port 0x%x result %d\n",
  1936. __func__, port_id, result);
  1937. pr_debug("%s: Set params failed port = 0x%x payload = 0x%pK result %d\n",
  1938. __func__, port_id, &cal_block->cal_data.paddr, result);
  1939. result = -EINVAL;
  1940. goto done;
  1941. }
  1942. /* Wait for the callback */
  1943. result = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
  1944. atomic_read(&this_adm.copp.stat[port_idx][copp_idx]) >= 0,
  1945. msecs_to_jiffies(TIMEOUT_MS));
  1946. if (!result) {
  1947. pr_err("%s: Set params timed out port = 0x%x\n",
  1948. __func__, port_id);
  1949. pr_debug("%s: Set params timed out port = 0x%x, payload = 0x%pK\n",
  1950. __func__, port_id, &cal_block->cal_data.paddr);
  1951. result = -EINVAL;
  1952. goto done;
  1953. } else if (atomic_read(&this_adm.copp.stat
  1954. [port_idx][copp_idx]) > 0) {
  1955. pr_err("%s: DSP returned error[%s]\n",
  1956. __func__, adsp_err_get_err_str(
  1957. atomic_read(&this_adm.copp.stat
  1958. [port_idx][copp_idx])));
  1959. result = adsp_err_get_lnx_err_code(
  1960. atomic_read(&this_adm.copp.stat
  1961. [port_idx][copp_idx]));
  1962. goto done;
  1963. }
  1964. done:
  1965. return result;
  1966. }
  1967. static struct cal_block_data *adm_find_cal_by_path(int cal_index, int path)
  1968. {
  1969. struct list_head *ptr, *next;
  1970. struct cal_block_data *cal_block = NULL;
  1971. struct audio_cal_info_audproc *audproc_cal_info = NULL;
  1972. struct audio_cal_info_audvol *audvol_cal_info = NULL;
  1973. pr_debug("%s:\n", __func__);
  1974. list_for_each_safe(ptr, next,
  1975. &this_adm.cal_data[cal_index]->cal_blocks) {
  1976. cal_block = list_entry(ptr,
  1977. struct cal_block_data, list);
  1978. if (cal_index == ADM_AUDPROC_CAL ||
  1979. cal_index == ADM_LSM_AUDPROC_CAL) {
  1980. audproc_cal_info = cal_block->cal_info;
  1981. if ((audproc_cal_info->path == path) &&
  1982. (cal_block->cal_data.size > 0))
  1983. return cal_block;
  1984. } else if (cal_index == ADM_AUDVOL_CAL) {
  1985. audvol_cal_info = cal_block->cal_info;
  1986. if ((audvol_cal_info->path == path) &&
  1987. (cal_block->cal_data.size > 0))
  1988. return cal_block;
  1989. }
  1990. }
  1991. pr_debug("%s: Can't find ADM cal for cal_index %d, path %d\n",
  1992. __func__, cal_index, path);
  1993. return NULL;
  1994. }
  1995. static struct cal_block_data *adm_find_cal_by_app_type(int cal_index, int path,
  1996. int app_type)
  1997. {
  1998. struct list_head *ptr, *next;
  1999. struct cal_block_data *cal_block = NULL;
  2000. struct audio_cal_info_audproc *audproc_cal_info = NULL;
  2001. struct audio_cal_info_audvol *audvol_cal_info = NULL;
  2002. pr_debug("%s\n", __func__);
  2003. list_for_each_safe(ptr, next,
  2004. &this_adm.cal_data[cal_index]->cal_blocks) {
  2005. cal_block = list_entry(ptr,
  2006. struct cal_block_data, list);
  2007. if (cal_index == ADM_AUDPROC_CAL ||
  2008. cal_index == ADM_LSM_AUDPROC_CAL) {
  2009. audproc_cal_info = cal_block->cal_info;
  2010. if ((audproc_cal_info->path == path) &&
  2011. (audproc_cal_info->app_type == app_type) &&
  2012. (cal_block->cal_data.size > 0))
  2013. return cal_block;
  2014. } else if (cal_index == ADM_AUDVOL_CAL) {
  2015. audvol_cal_info = cal_block->cal_info;
  2016. if ((audvol_cal_info->path == path) &&
  2017. (audvol_cal_info->app_type == app_type) &&
  2018. (cal_block->cal_data.size > 0))
  2019. return cal_block;
  2020. }
  2021. }
  2022. pr_debug("%s: Can't find ADM cali for cal_index %d, path %d, app %d, defaulting to search by path\n",
  2023. __func__, cal_index, path, app_type);
  2024. return adm_find_cal_by_path(cal_index, path);
  2025. }
  2026. static struct cal_block_data *adm_find_cal(int cal_index, int path,
  2027. int app_type, int acdb_id,
  2028. int sample_rate)
  2029. {
  2030. struct list_head *ptr, *next;
  2031. struct cal_block_data *cal_block = NULL;
  2032. struct audio_cal_info_audproc *audproc_cal_info = NULL;
  2033. struct audio_cal_info_audvol *audvol_cal_info = NULL;
  2034. pr_debug("%s:\n", __func__);
  2035. list_for_each_safe(ptr, next,
  2036. &this_adm.cal_data[cal_index]->cal_blocks) {
  2037. cal_block = list_entry(ptr,
  2038. struct cal_block_data, list);
  2039. if (cal_index == ADM_AUDPROC_CAL ||
  2040. cal_index == ADM_LSM_AUDPROC_CAL) {
  2041. audproc_cal_info = cal_block->cal_info;
  2042. if ((audproc_cal_info->path == path) &&
  2043. (audproc_cal_info->app_type == app_type) &&
  2044. (audproc_cal_info->acdb_id == acdb_id) &&
  2045. (audproc_cal_info->sample_rate == sample_rate) &&
  2046. (cal_block->cal_data.size > 0))
  2047. return cal_block;
  2048. } else if (cal_index == ADM_AUDVOL_CAL) {
  2049. audvol_cal_info = cal_block->cal_info;
  2050. if ((audvol_cal_info->path == path) &&
  2051. (audvol_cal_info->app_type == app_type) &&
  2052. (audvol_cal_info->acdb_id == acdb_id) &&
  2053. (cal_block->cal_data.size > 0))
  2054. return cal_block;
  2055. }
  2056. }
  2057. pr_debug("%s: Can't find ADM cal for cal_index %d, path %d, app %d, acdb_id %d sample_rate %d defaulting to search by app type\n",
  2058. __func__, cal_index, path, app_type, acdb_id, sample_rate);
  2059. return adm_find_cal_by_app_type(cal_index, path, app_type);
  2060. }
  2061. static int adm_remap_and_send_cal_block(int cal_index, int port_id,
  2062. int copp_idx, struct cal_block_data *cal_block, int perf_mode,
  2063. int app_type, int acdb_id, int sample_rate)
  2064. {
  2065. int ret = 0;
  2066. pr_debug("%s: Sending cal_index cal %d\n", __func__, cal_index);
  2067. ret = remap_cal_data(cal_block, cal_index);
  2068. if (ret) {
  2069. pr_err("%s: Remap_cal_data failed for cal %d!\n",
  2070. __func__, cal_index);
  2071. goto done;
  2072. }
  2073. ret = send_adm_cal_block(port_id, copp_idx, cal_block, perf_mode,
  2074. app_type, acdb_id, sample_rate);
  2075. if (ret < 0)
  2076. pr_debug("%s: No cal sent for cal_index %d, port_id = 0x%x! ret %d sample_rate %d\n",
  2077. __func__, cal_index, port_id, ret, sample_rate);
  2078. done:
  2079. return ret;
  2080. }
  2081. static void send_adm_cal_type(int cal_index, int path, int port_id,
  2082. int copp_idx, int perf_mode, int app_type,
  2083. int acdb_id, int sample_rate)
  2084. {
  2085. struct cal_block_data *cal_block = NULL;
  2086. int ret;
  2087. pr_debug("%s: cal index %d\n", __func__, cal_index);
  2088. if (this_adm.cal_data[cal_index] == NULL) {
  2089. pr_debug("%s: cal_index %d not allocated!\n",
  2090. __func__, cal_index);
  2091. goto done;
  2092. }
  2093. mutex_lock(&this_adm.cal_data[cal_index]->lock);
  2094. cal_block = adm_find_cal(cal_index, path, app_type, acdb_id,
  2095. sample_rate);
  2096. if (cal_block == NULL)
  2097. goto unlock;
  2098. ret = adm_remap_and_send_cal_block(cal_index, port_id, copp_idx,
  2099. cal_block, perf_mode, app_type, acdb_id, sample_rate);
  2100. unlock:
  2101. mutex_unlock(&this_adm.cal_data[cal_index]->lock);
  2102. done:
  2103. return;
  2104. }
  2105. static int get_cal_path(int path)
  2106. {
  2107. if (path == 0x1)
  2108. return RX_DEVICE;
  2109. else
  2110. return TX_DEVICE;
  2111. }
  2112. /*
  2113. * Command to set LSM port ID
  2114. * which is used to send LSM cal
  2115. *
  2116. * @port_id: Port ID number to be set
  2117. *
  2118. */
  2119. void adm_set_lsm_port_id(int port_id)
  2120. {
  2121. this_adm.lsm_port_id = port_id;
  2122. }
  2123. EXPORT_SYMBOL(adm_set_lsm_port_id);
  2124. static void send_adm_cal(int port_id, int copp_idx, int path, int perf_mode,
  2125. int app_type, int acdb_id, int sample_rate)
  2126. {
  2127. pr_debug("%s: port id 0x%x copp_idx %d\n", __func__, port_id, copp_idx);
  2128. if (port_id != this_adm.lsm_port_id)
  2129. send_adm_cal_type(ADM_AUDPROC_CAL, path, port_id, copp_idx,
  2130. perf_mode, app_type, acdb_id, sample_rate);
  2131. else
  2132. send_adm_cal_type(ADM_LSM_AUDPROC_CAL, path, port_id, copp_idx,
  2133. perf_mode, app_type, acdb_id, sample_rate);
  2134. send_adm_cal_type(ADM_AUDVOL_CAL, path, port_id, copp_idx, perf_mode,
  2135. app_type, acdb_id, sample_rate);
  2136. }
  2137. /**
  2138. * adm_connect_afe_port -
  2139. * command to send ADM connect AFE port
  2140. *
  2141. * @mode: value of mode for ADM connect AFE
  2142. * @session_id: session active to connect
  2143. * @port_id: Port ID number
  2144. *
  2145. * Returns 0 on success or error on failure
  2146. */
  2147. int adm_connect_afe_port(int mode, int session_id, int port_id)
  2148. {
  2149. struct adm_cmd_connect_afe_port_v5 cmd;
  2150. int ret = 0;
  2151. int port_idx, copp_idx = 0;
  2152. pr_debug("%s: port_id: 0x%x session id:%d mode:%d\n", __func__,
  2153. port_id, session_id, mode);
  2154. port_id = afe_convert_virtual_to_portid(port_id);
  2155. port_idx = adm_validate_and_get_port_index(port_id);
  2156. if (port_idx < 0) {
  2157. pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
  2158. return -EINVAL;
  2159. }
  2160. if (this_adm.apr == NULL) {
  2161. this_adm.apr = apr_register("ADSP", "ADM", adm_callback,
  2162. 0xFFFFFFFF, &this_adm);
  2163. if (this_adm.apr == NULL) {
  2164. pr_err("%s: Unable to register ADM\n", __func__);
  2165. ret = -ENODEV;
  2166. return ret;
  2167. }
  2168. rtac_set_adm_handle(this_adm.apr);
  2169. }
  2170. pr_debug("%s: Port ID 0x%x, index %d\n", __func__, port_id, port_idx);
  2171. cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  2172. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  2173. cmd.hdr.pkt_size = sizeof(cmd);
  2174. cmd.hdr.src_svc = APR_SVC_ADM;
  2175. cmd.hdr.src_domain = APR_DOMAIN_APPS;
  2176. cmd.hdr.src_port = port_id;
  2177. cmd.hdr.dest_svc = APR_SVC_ADM;
  2178. cmd.hdr.dest_domain = APR_DOMAIN_ADSP;
  2179. cmd.hdr.dest_port = 0; /* Ignored */
  2180. cmd.hdr.token = port_idx << 16 | copp_idx;
  2181. cmd.hdr.opcode = ADM_CMD_CONNECT_AFE_PORT_V5;
  2182. cmd.mode = mode;
  2183. cmd.session_id = session_id;
  2184. cmd.afe_port_id = port_id;
  2185. atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
  2186. ret = apr_send_pkt(this_adm.apr, (uint32_t *)&cmd);
  2187. if (ret < 0) {
  2188. pr_err("%s: ADM enable for port_id: 0x%x failed ret %d\n",
  2189. __func__, port_id, ret);
  2190. ret = -EINVAL;
  2191. goto fail_cmd;
  2192. }
  2193. /* Wait for the callback with copp id */
  2194. ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
  2195. atomic_read(&this_adm.copp.stat[port_idx][copp_idx]) >= 0,
  2196. msecs_to_jiffies(TIMEOUT_MS));
  2197. if (!ret) {
  2198. pr_err("%s: ADM connect timedout for port_id: 0x%x\n",
  2199. __func__, port_id);
  2200. ret = -EINVAL;
  2201. goto fail_cmd;
  2202. } else if (atomic_read(&this_adm.copp.stat
  2203. [port_idx][copp_idx]) > 0) {
  2204. pr_err("%s: DSP returned error[%s]\n",
  2205. __func__, adsp_err_get_err_str(
  2206. atomic_read(&this_adm.copp.stat
  2207. [port_idx][copp_idx])));
  2208. ret = adsp_err_get_lnx_err_code(
  2209. atomic_read(&this_adm.copp.stat
  2210. [port_idx][copp_idx]));
  2211. goto fail_cmd;
  2212. }
  2213. atomic_inc(&this_adm.copp.cnt[port_idx][copp_idx]);
  2214. return 0;
  2215. fail_cmd:
  2216. return ret;
  2217. }
  2218. EXPORT_SYMBOL(adm_connect_afe_port);
  2219. int adm_arrange_mch_map(struct adm_cmd_device_open_v5 *open, int path,
  2220. int channel_mode)
  2221. {
  2222. int rc = 0, idx;
  2223. memset(open->dev_channel_mapping, 0, PCM_FORMAT_MAX_NUM_CHANNEL);
  2224. switch (path) {
  2225. case ADM_PATH_PLAYBACK:
  2226. idx = ADM_MCH_MAP_IDX_PLAYBACK;
  2227. break;
  2228. case ADM_PATH_LIVE_REC:
  2229. case ADM_PATH_NONLIVE_REC:
  2230. idx = ADM_MCH_MAP_IDX_REC;
  2231. break;
  2232. default:
  2233. goto non_mch_path;
  2234. };
  2235. if ((open->dev_num_channel > 2) && multi_ch_maps[idx].set_channel_map) {
  2236. memcpy(open->dev_channel_mapping,
  2237. multi_ch_maps[idx].channel_mapping,
  2238. PCM_FORMAT_MAX_NUM_CHANNEL);
  2239. } else {
  2240. if (channel_mode == 1) {
  2241. open->dev_channel_mapping[0] = PCM_CHANNEL_FC;
  2242. } else if (channel_mode == 2) {
  2243. open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
  2244. open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
  2245. } else if (channel_mode == 3) {
  2246. open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
  2247. open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
  2248. open->dev_channel_mapping[2] = PCM_CHANNEL_FC;
  2249. } else if (channel_mode == 4) {
  2250. open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
  2251. open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
  2252. open->dev_channel_mapping[2] = PCM_CHANNEL_LS;
  2253. open->dev_channel_mapping[3] = PCM_CHANNEL_RS;
  2254. } else if (channel_mode == 5) {
  2255. open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
  2256. open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
  2257. open->dev_channel_mapping[2] = PCM_CHANNEL_FC;
  2258. open->dev_channel_mapping[3] = PCM_CHANNEL_LS;
  2259. open->dev_channel_mapping[4] = PCM_CHANNEL_RS;
  2260. } else if (channel_mode == 6) {
  2261. open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
  2262. open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
  2263. open->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
  2264. open->dev_channel_mapping[3] = PCM_CHANNEL_FC;
  2265. open->dev_channel_mapping[4] = PCM_CHANNEL_LS;
  2266. open->dev_channel_mapping[5] = PCM_CHANNEL_RS;
  2267. } else if (channel_mode == 7) {
  2268. open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
  2269. open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
  2270. open->dev_channel_mapping[2] = PCM_CHANNEL_FC;
  2271. open->dev_channel_mapping[3] = PCM_CHANNEL_LFE;
  2272. open->dev_channel_mapping[4] = PCM_CHANNEL_LB;
  2273. open->dev_channel_mapping[5] = PCM_CHANNEL_RB;
  2274. open->dev_channel_mapping[6] = PCM_CHANNEL_CS;
  2275. } else if (channel_mode == 8) {
  2276. open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
  2277. open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
  2278. open->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
  2279. open->dev_channel_mapping[3] = PCM_CHANNEL_FC;
  2280. open->dev_channel_mapping[4] = PCM_CHANNEL_LS;
  2281. open->dev_channel_mapping[5] = PCM_CHANNEL_RS;
  2282. open->dev_channel_mapping[6] = PCM_CHANNEL_LB;
  2283. open->dev_channel_mapping[7] = PCM_CHANNEL_RB;
  2284. } else {
  2285. pr_err("%s: invalid num_chan %d\n", __func__,
  2286. channel_mode);
  2287. rc = -EINVAL;
  2288. goto inval_ch_mod;
  2289. }
  2290. }
  2291. non_mch_path:
  2292. inval_ch_mod:
  2293. return rc;
  2294. }
  2295. int adm_arrange_mch_ep2_map(struct adm_cmd_device_open_v6 *open_v6,
  2296. int channel_mode)
  2297. {
  2298. int rc = 0;
  2299. memset(open_v6->dev_channel_mapping_eid2, 0,
  2300. PCM_FORMAT_MAX_NUM_CHANNEL);
  2301. if (channel_mode == 1) {
  2302. open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FC;
  2303. } else if (channel_mode == 2) {
  2304. open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
  2305. open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
  2306. } else if (channel_mode == 3) {
  2307. open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
  2308. open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
  2309. open_v6->dev_channel_mapping_eid2[2] = PCM_CHANNEL_FC;
  2310. } else if (channel_mode == 4) {
  2311. open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
  2312. open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
  2313. open_v6->dev_channel_mapping_eid2[2] = PCM_CHANNEL_LS;
  2314. open_v6->dev_channel_mapping_eid2[3] = PCM_CHANNEL_RS;
  2315. } else if (channel_mode == 5) {
  2316. open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
  2317. open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
  2318. open_v6->dev_channel_mapping_eid2[2] = PCM_CHANNEL_FC;
  2319. open_v6->dev_channel_mapping_eid2[3] = PCM_CHANNEL_LS;
  2320. open_v6->dev_channel_mapping_eid2[4] = PCM_CHANNEL_RS;
  2321. } else if (channel_mode == 6) {
  2322. open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
  2323. open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
  2324. open_v6->dev_channel_mapping_eid2[2] = PCM_CHANNEL_LFE;
  2325. open_v6->dev_channel_mapping_eid2[3] = PCM_CHANNEL_FC;
  2326. open_v6->dev_channel_mapping_eid2[4] = PCM_CHANNEL_LS;
  2327. open_v6->dev_channel_mapping_eid2[5] = PCM_CHANNEL_RS;
  2328. } else if (channel_mode == 8) {
  2329. open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
  2330. open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
  2331. open_v6->dev_channel_mapping_eid2[2] = PCM_CHANNEL_LFE;
  2332. open_v6->dev_channel_mapping_eid2[3] = PCM_CHANNEL_FC;
  2333. open_v6->dev_channel_mapping_eid2[4] = PCM_CHANNEL_LS;
  2334. open_v6->dev_channel_mapping_eid2[5] = PCM_CHANNEL_RS;
  2335. open_v6->dev_channel_mapping_eid2[6] = PCM_CHANNEL_LB;
  2336. open_v6->dev_channel_mapping_eid2[7] = PCM_CHANNEL_RB;
  2337. } else {
  2338. pr_err("%s: invalid num_chan %d\n", __func__,
  2339. channel_mode);
  2340. rc = -EINVAL;
  2341. }
  2342. return rc;
  2343. }
  2344. /**
  2345. * adm_open -
  2346. * command to send ADM open
  2347. *
  2348. * @port_id: port id number
  2349. * @path: direction or ADM path type
  2350. * @rate: sample rate of session
  2351. * @channel_mode: number of channels set
  2352. * @topology: topology active for this session
  2353. * @perf_mode: performance mode like LL/ULL/..
  2354. * @bit_width: bit width to set for copp
  2355. * @app_type: App type used for this session
  2356. * @acdb_id: ACDB ID of this device
  2357. *
  2358. * Returns 0 on success or error on failure
  2359. */
  2360. int adm_open(int port_id, int path, int rate, int channel_mode, int topology,
  2361. int perf_mode, uint16_t bit_width, int app_type, int acdb_id)
  2362. {
  2363. struct adm_cmd_device_open_v5 open;
  2364. struct adm_cmd_device_open_v6 open_v6;
  2365. int ret = 0;
  2366. int port_idx, flags;
  2367. int copp_idx = -1;
  2368. int tmp_port = q6audio_get_port_id(port_id);
  2369. pr_debug("%s:port %#x path:%d rate:%d mode:%d perf_mode:%d,topo_id %d\n",
  2370. __func__, port_id, path, rate, channel_mode, perf_mode,
  2371. topology);
  2372. port_id = q6audio_convert_virtual_to_portid(port_id);
  2373. port_idx = adm_validate_and_get_port_index(port_id);
  2374. if (port_idx < 0) {
  2375. pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
  2376. return -EINVAL;
  2377. }
  2378. if (this_adm.apr == NULL) {
  2379. this_adm.apr = apr_register("ADSP", "ADM", adm_callback,
  2380. 0xFFFFFFFF, &this_adm);
  2381. if (this_adm.apr == NULL) {
  2382. pr_err("%s: Unable to register ADM\n", __func__);
  2383. return -ENODEV;
  2384. }
  2385. rtac_set_adm_handle(this_adm.apr);
  2386. }
  2387. if (perf_mode == ULL_POST_PROCESSING_PCM_MODE) {
  2388. flags = ADM_ULL_POST_PROCESSING_DEVICE_SESSION;
  2389. if ((topology == DOLBY_ADM_COPP_TOPOLOGY_ID) ||
  2390. (topology == DS2_ADM_COPP_TOPOLOGY_ID) ||
  2391. (topology == SRS_TRUMEDIA_TOPOLOGY_ID))
  2392. topology = DEFAULT_COPP_TOPOLOGY;
  2393. } else if (perf_mode == ULTRA_LOW_LATENCY_PCM_MODE) {
  2394. flags = ADM_ULTRA_LOW_LATENCY_DEVICE_SESSION;
  2395. topology = NULL_COPP_TOPOLOGY;
  2396. rate = ULL_SUPPORTED_SAMPLE_RATE;
  2397. bit_width = ULL_SUPPORTED_BITS_PER_SAMPLE;
  2398. } else if (perf_mode == LOW_LATENCY_PCM_MODE) {
  2399. flags = ADM_LOW_LATENCY_DEVICE_SESSION;
  2400. if ((topology == DOLBY_ADM_COPP_TOPOLOGY_ID) ||
  2401. (topology == DS2_ADM_COPP_TOPOLOGY_ID) ||
  2402. (topology == SRS_TRUMEDIA_TOPOLOGY_ID))
  2403. topology = DEFAULT_COPP_TOPOLOGY;
  2404. } else {
  2405. if ((path == ADM_PATH_COMPRESSED_RX) ||
  2406. (path == ADM_PATH_COMPRESSED_TX))
  2407. flags = 0;
  2408. else
  2409. flags = ADM_LEGACY_DEVICE_SESSION;
  2410. }
  2411. if ((topology == VPM_TX_SM_ECNS_V2_COPP_TOPOLOGY) ||
  2412. (topology == VPM_TX_DM_FLUENCE_COPP_TOPOLOGY) ||
  2413. (topology == VPM_TX_DM_RFECNS_COPP_TOPOLOGY))
  2414. rate = 16000;
  2415. /*
  2416. * Routing driver reuses the same adm for streams with the same
  2417. * app_type, sample_rate etc.
  2418. * This isn't allowed for ULL streams as per the DSP interface
  2419. */
  2420. if (perf_mode != ULTRA_LOW_LATENCY_PCM_MODE)
  2421. copp_idx = adm_get_idx_if_copp_exists(port_idx, topology,
  2422. perf_mode,
  2423. rate, bit_width,
  2424. app_type);
  2425. if (copp_idx < 0) {
  2426. copp_idx = adm_get_next_available_copp(port_idx);
  2427. if (copp_idx >= MAX_COPPS_PER_PORT) {
  2428. pr_err("%s: exceeded copp id %d\n",
  2429. __func__, copp_idx);
  2430. return -EINVAL;
  2431. }
  2432. atomic_set(&this_adm.copp.cnt[port_idx][copp_idx], 0);
  2433. atomic_set(&this_adm.copp.topology[port_idx][copp_idx],
  2434. topology);
  2435. atomic_set(&this_adm.copp.mode[port_idx][copp_idx],
  2436. perf_mode);
  2437. atomic_set(&this_adm.copp.rate[port_idx][copp_idx],
  2438. rate);
  2439. atomic_set(&this_adm.copp.channels[port_idx][copp_idx],
  2440. channel_mode);
  2441. atomic_set(&this_adm.copp.bit_width[port_idx][copp_idx],
  2442. bit_width);
  2443. atomic_set(&this_adm.copp.app_type[port_idx][copp_idx],
  2444. app_type);
  2445. atomic_set(&this_adm.copp.acdb_id[port_idx][copp_idx],
  2446. acdb_id);
  2447. set_bit(ADM_STATUS_CALIBRATION_REQUIRED,
  2448. (void *)&this_adm.copp.adm_status[port_idx][copp_idx]);
  2449. if ((path != ADM_PATH_COMPRESSED_RX) &&
  2450. (path != ADM_PATH_COMPRESSED_TX))
  2451. send_adm_custom_topology();
  2452. }
  2453. if (this_adm.copp.adm_delay[port_idx][copp_idx] &&
  2454. perf_mode == LEGACY_PCM_MODE) {
  2455. atomic_set(&this_adm.copp.adm_delay_stat[port_idx][copp_idx],
  2456. 1);
  2457. this_adm.copp.adm_delay[port_idx][copp_idx] = 0;
  2458. wake_up(&this_adm.copp.adm_delay_wait[port_idx][copp_idx]);
  2459. }
  2460. /* Create a COPP if port id are not enabled */
  2461. if (atomic_read(&this_adm.copp.cnt[port_idx][copp_idx]) == 0) {
  2462. pr_debug("%s: open ADM: port_idx: %d, copp_idx: %d\n", __func__,
  2463. port_idx, copp_idx);
  2464. if ((topology == SRS_TRUMEDIA_TOPOLOGY_ID) &&
  2465. perf_mode == LEGACY_PCM_MODE) {
  2466. int res;
  2467. atomic_set(&this_adm.mem_map_index, ADM_SRS_TRUMEDIA);
  2468. msm_dts_srs_tm_ion_memmap(&this_adm.outband_memmap);
  2469. res = adm_memory_map_regions(&this_adm.outband_memmap.paddr, 0,
  2470. (uint32_t *)&this_adm.outband_memmap.size, 1);
  2471. if (res < 0) {
  2472. pr_err("%s: SRS adm_memory_map_regions failed ! addr = 0x%pK, size = %d\n",
  2473. __func__, (void *)this_adm.outband_memmap.paddr,
  2474. (uint32_t)this_adm.outband_memmap.size);
  2475. }
  2476. }
  2477. open.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  2478. APR_HDR_LEN(APR_HDR_SIZE),
  2479. APR_PKT_VER);
  2480. open.hdr.pkt_size = sizeof(open);
  2481. open.hdr.src_svc = APR_SVC_ADM;
  2482. open.hdr.src_domain = APR_DOMAIN_APPS;
  2483. open.hdr.src_port = tmp_port;
  2484. open.hdr.dest_svc = APR_SVC_ADM;
  2485. open.hdr.dest_domain = APR_DOMAIN_ADSP;
  2486. open.hdr.dest_port = tmp_port;
  2487. open.hdr.token = port_idx << 16 | copp_idx;
  2488. open.hdr.opcode = ADM_CMD_DEVICE_OPEN_V5;
  2489. open.flags = flags;
  2490. open.mode_of_operation = path;
  2491. open.endpoint_id_1 = tmp_port;
  2492. open.endpoint_id_2 = 0xFFFF;
  2493. if (this_adm.ec_ref_rx && (path != 1)) {
  2494. open.endpoint_id_2 = this_adm.ec_ref_rx;
  2495. this_adm.ec_ref_rx = -1;
  2496. }
  2497. open.topology_id = topology;
  2498. open.dev_num_channel = channel_mode & 0x00FF;
  2499. open.bit_width = bit_width;
  2500. WARN_ON((perf_mode == ULTRA_LOW_LATENCY_PCM_MODE) &&
  2501. (rate != ULL_SUPPORTED_SAMPLE_RATE));
  2502. open.sample_rate = rate;
  2503. ret = adm_arrange_mch_map(&open, path, channel_mode);
  2504. if (ret)
  2505. return ret;
  2506. pr_debug("%s: port_id=0x%x rate=%d topology_id=0x%X\n",
  2507. __func__, open.endpoint_id_1, open.sample_rate,
  2508. open.topology_id);
  2509. atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
  2510. if ((this_adm.num_ec_ref_rx_chans != 0) && (path != 1) &&
  2511. (open.endpoint_id_2 != 0xFFFF)) {
  2512. memset(&open_v6, 0,
  2513. sizeof(struct adm_cmd_device_open_v6));
  2514. memcpy(&open_v6, &open,
  2515. sizeof(struct adm_cmd_device_open_v5));
  2516. open_v6.hdr.opcode = ADM_CMD_DEVICE_OPEN_V6;
  2517. open_v6.hdr.pkt_size = sizeof(open_v6);
  2518. open_v6.dev_num_channel_eid2 =
  2519. this_adm.num_ec_ref_rx_chans;
  2520. this_adm.num_ec_ref_rx_chans = 0;
  2521. if (this_adm.ec_ref_rx_bit_width != 0) {
  2522. open_v6.bit_width_eid2 =
  2523. this_adm.ec_ref_rx_bit_width;
  2524. this_adm.ec_ref_rx_bit_width = 0;
  2525. } else {
  2526. open_v6.bit_width_eid2 = bit_width;
  2527. }
  2528. if (this_adm.ec_ref_rx_sampling_rate != 0) {
  2529. open_v6.sample_rate_eid2 =
  2530. this_adm.ec_ref_rx_sampling_rate;
  2531. this_adm.ec_ref_rx_sampling_rate = 0;
  2532. } else {
  2533. open_v6.sample_rate_eid2 = rate;
  2534. }
  2535. pr_debug("%s: eid2_channels=%d eid2_bit_width=%d eid2_rate=%d\n",
  2536. __func__, open_v6.dev_num_channel_eid2,
  2537. open_v6.bit_width_eid2,
  2538. open_v6.sample_rate_eid2);
  2539. ret = adm_arrange_mch_ep2_map(&open_v6,
  2540. open_v6.dev_num_channel_eid2);
  2541. if (ret)
  2542. return ret;
  2543. ret = apr_send_pkt(this_adm.apr, (uint32_t *)&open_v6);
  2544. } else {
  2545. ret = apr_send_pkt(this_adm.apr, (uint32_t *)&open);
  2546. }
  2547. if (ret < 0) {
  2548. pr_err("%s: port_id: 0x%x for[0x%x] failed %d\n",
  2549. __func__, tmp_port, port_id, ret);
  2550. return -EINVAL;
  2551. }
  2552. /* Wait for the callback with copp id */
  2553. ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
  2554. atomic_read(&this_adm.copp.stat
  2555. [port_idx][copp_idx]) >= 0,
  2556. msecs_to_jiffies(TIMEOUT_MS));
  2557. if (!ret) {
  2558. pr_err("%s: ADM open timedout for port_id: 0x%x for [0x%x]\n",
  2559. __func__, tmp_port, port_id);
  2560. return -EINVAL;
  2561. } else if (atomic_read(&this_adm.copp.stat
  2562. [port_idx][copp_idx]) > 0) {
  2563. pr_err("%s: DSP returned error[%s]\n",
  2564. __func__, adsp_err_get_err_str(
  2565. atomic_read(&this_adm.copp.stat
  2566. [port_idx][copp_idx])));
  2567. return adsp_err_get_lnx_err_code(
  2568. atomic_read(&this_adm.copp.stat
  2569. [port_idx][copp_idx]));
  2570. }
  2571. }
  2572. atomic_inc(&this_adm.copp.cnt[port_idx][copp_idx]);
  2573. return copp_idx;
  2574. }
  2575. EXPORT_SYMBOL(adm_open);
  2576. /**
  2577. * adm_copp_mfc_cfg -
  2578. * command to send ADM MFC config
  2579. *
  2580. * @port_id: Port ID number
  2581. * @copp_idx: copp index assigned
  2582. * @dst_sample_rate: sink sample rate
  2583. *
  2584. */
  2585. void adm_copp_mfc_cfg(int port_id, int copp_idx, int dst_sample_rate)
  2586. {
  2587. struct audproc_mfc_output_media_fmt mfc_cfg;
  2588. struct adm_cmd_device_open_v5 open;
  2589. int port_idx;
  2590. int sz = 0;
  2591. int rc = 0;
  2592. int i = 0;
  2593. port_id = q6audio_convert_virtual_to_portid(port_id);
  2594. port_idx = adm_validate_and_get_port_index(port_id);
  2595. if (port_idx < 0) {
  2596. pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
  2597. goto fail_cmd;
  2598. }
  2599. if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
  2600. pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
  2601. goto fail_cmd;
  2602. }
  2603. sz = sizeof(struct audproc_mfc_output_media_fmt);
  2604. mfc_cfg.params.hdr.hdr_field =
  2605. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  2606. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  2607. mfc_cfg.params.hdr.pkt_size = sz;
  2608. mfc_cfg.params.hdr.src_svc = APR_SVC_ADM;
  2609. mfc_cfg.params.hdr.src_domain = APR_DOMAIN_APPS;
  2610. mfc_cfg.params.hdr.src_port = port_id;
  2611. mfc_cfg.params.hdr.dest_svc = APR_SVC_ADM;
  2612. mfc_cfg.params.hdr.dest_domain = APR_DOMAIN_ADSP;
  2613. mfc_cfg.params.hdr.dest_port =
  2614. atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
  2615. mfc_cfg.params.hdr.token = port_idx << 16 | copp_idx;
  2616. mfc_cfg.params.hdr.opcode = ADM_CMD_SET_PP_PARAMS_V5;
  2617. mfc_cfg.params.payload_addr_lsw = 0;
  2618. mfc_cfg.params.payload_addr_msw = 0;
  2619. mfc_cfg.params.mem_map_handle = 0;
  2620. mfc_cfg.params.payload_size = sizeof(mfc_cfg) -
  2621. sizeof(mfc_cfg.params);
  2622. mfc_cfg.data.module_id = AUDPROC_MODULE_ID_MFC;
  2623. mfc_cfg.data.param_id =
  2624. AUDPROC_PARAM_ID_MFC_OUTPUT_MEDIA_FORMAT;
  2625. mfc_cfg.data.param_size = mfc_cfg.params.payload_size -
  2626. sizeof(mfc_cfg.data);
  2627. mfc_cfg.data.reserved = 0;
  2628. mfc_cfg.sampling_rate = dst_sample_rate;
  2629. mfc_cfg.bits_per_sample =
  2630. atomic_read(&this_adm.copp.bit_width[port_idx][copp_idx]);
  2631. open.dev_num_channel = mfc_cfg.num_channels =
  2632. atomic_read(&this_adm.copp.channels[port_idx][copp_idx]);
  2633. rc = adm_arrange_mch_map(&open, ADM_PATH_PLAYBACK,
  2634. mfc_cfg.num_channels);
  2635. if (rc < 0) {
  2636. pr_err("%s: unable to get channal map\n", __func__);
  2637. goto fail_cmd;
  2638. }
  2639. for (i = 0; i < mfc_cfg.num_channels; i++)
  2640. mfc_cfg.channel_type[i] =
  2641. (uint16_t) open.dev_channel_mapping[i];
  2642. atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
  2643. pr_debug("%s: mfc config: port_idx %d copp_idx %d copp SR %d copp BW %d copp chan %d o/p SR %d\n",
  2644. __func__, port_idx, copp_idx,
  2645. atomic_read(&this_adm.copp.rate[port_idx][copp_idx]),
  2646. mfc_cfg.bits_per_sample, mfc_cfg.num_channels,
  2647. mfc_cfg.sampling_rate);
  2648. rc = apr_send_pkt(this_adm.apr, (uint32_t *)&mfc_cfg);
  2649. if (rc < 0) {
  2650. pr_err("%s: port_id: for[0x%x] failed %d\n",
  2651. __func__, port_id, rc);
  2652. goto fail_cmd;
  2653. }
  2654. /* Wait for the callback with copp id */
  2655. rc = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
  2656. atomic_read(&this_adm.copp.stat
  2657. [port_idx][copp_idx]) >= 0,
  2658. msecs_to_jiffies(TIMEOUT_MS));
  2659. if (!rc) {
  2660. pr_err("%s: mfc_cfg Set params timed out for port_id: for [0x%x]\n",
  2661. __func__, port_id);
  2662. goto fail_cmd;
  2663. } else if (atomic_read(&this_adm.copp.stat
  2664. [port_idx][copp_idx]) > 0) {
  2665. pr_err("%s: DSP returned error[%s]\n",
  2666. __func__, adsp_err_get_err_str(
  2667. atomic_read(&this_adm.copp.stat
  2668. [port_idx][copp_idx])));
  2669. goto fail_cmd;
  2670. }
  2671. rc = 0;
  2672. fail_cmd:
  2673. return;
  2674. }
  2675. EXPORT_SYMBOL(adm_copp_mfc_cfg);
  2676. static void route_set_opcode_matrix_id(
  2677. struct adm_cmd_matrix_map_routings_v5 **route_addr,
  2678. int path, uint32_t passthr_mode)
  2679. {
  2680. struct adm_cmd_matrix_map_routings_v5 *route = *route_addr;
  2681. switch (path) {
  2682. case ADM_PATH_PLAYBACK:
  2683. route->hdr.opcode = ADM_CMD_MATRIX_MAP_ROUTINGS_V5;
  2684. route->matrix_id = ADM_MATRIX_ID_AUDIO_RX;
  2685. break;
  2686. case ADM_PATH_LIVE_REC:
  2687. if (passthr_mode == LISTEN) {
  2688. route->hdr.opcode =
  2689. ADM_CMD_STREAM_DEVICE_MAP_ROUTINGS_V5;
  2690. route->matrix_id = ADM_MATRIX_ID_LISTEN_TX;
  2691. break;
  2692. }
  2693. /* fall through to set matrix id for non-listen case */
  2694. case ADM_PATH_NONLIVE_REC:
  2695. route->hdr.opcode = ADM_CMD_MATRIX_MAP_ROUTINGS_V5;
  2696. route->matrix_id = ADM_MATRIX_ID_AUDIO_TX;
  2697. break;
  2698. case ADM_PATH_COMPRESSED_RX:
  2699. route->hdr.opcode = ADM_CMD_STREAM_DEVICE_MAP_ROUTINGS_V5;
  2700. route->matrix_id = ADM_MATRIX_ID_COMPRESSED_AUDIO_RX;
  2701. break;
  2702. case ADM_PATH_COMPRESSED_TX:
  2703. route->hdr.opcode = ADM_CMD_STREAM_DEVICE_MAP_ROUTINGS_V5;
  2704. route->matrix_id = ADM_MATRIX_ID_COMPRESSED_AUDIO_TX;
  2705. break;
  2706. default:
  2707. pr_err("%s: Wrong path set[%d]\n", __func__, path);
  2708. break;
  2709. }
  2710. pr_debug("%s: opcode 0x%x, matrix id %d\n",
  2711. __func__, route->hdr.opcode, route->matrix_id);
  2712. }
  2713. /**
  2714. * adm_matrix_map -
  2715. * command to send ADM matrix map for ADM copp list
  2716. *
  2717. * @path: direction or ADM path type
  2718. * @payload_map: have info of session id and associated copp_idx/num_copps
  2719. * @perf_mode: performance mode like LL/ULL/..
  2720. * @passthr_mode: flag to indicate passthrough mode
  2721. *
  2722. * Returns 0 on success or error on failure
  2723. */
  2724. int adm_matrix_map(int path, struct route_payload payload_map, int perf_mode,
  2725. uint32_t passthr_mode)
  2726. {
  2727. struct adm_cmd_matrix_map_routings_v5 *route;
  2728. struct adm_session_map_node_v5 *node;
  2729. uint16_t *copps_list;
  2730. int cmd_size = 0;
  2731. int ret = 0, i = 0;
  2732. void *payload = NULL;
  2733. void *matrix_map = NULL;
  2734. int port_idx, copp_idx;
  2735. /* Assumes port_ids have already been validated during adm_open */
  2736. cmd_size = (sizeof(struct adm_cmd_matrix_map_routings_v5) +
  2737. sizeof(struct adm_session_map_node_v5) +
  2738. (sizeof(uint32_t) * payload_map.num_copps));
  2739. matrix_map = kzalloc(cmd_size, GFP_KERNEL);
  2740. if (matrix_map == NULL) {
  2741. pr_err("%s: Mem alloc failed\n", __func__);
  2742. ret = -EINVAL;
  2743. return ret;
  2744. }
  2745. route = (struct adm_cmd_matrix_map_routings_v5 *)matrix_map;
  2746. route->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  2747. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  2748. route->hdr.pkt_size = cmd_size;
  2749. route->hdr.src_svc = 0;
  2750. route->hdr.src_domain = APR_DOMAIN_APPS;
  2751. route->hdr.src_port = 0; /* Ignored */;
  2752. route->hdr.dest_svc = APR_SVC_ADM;
  2753. route->hdr.dest_domain = APR_DOMAIN_ADSP;
  2754. route->hdr.dest_port = 0; /* Ignored */;
  2755. route->hdr.token = 0;
  2756. route->num_sessions = 1;
  2757. route_set_opcode_matrix_id(&route, path, passthr_mode);
  2758. payload = ((u8 *)matrix_map +
  2759. sizeof(struct adm_cmd_matrix_map_routings_v5));
  2760. node = (struct adm_session_map_node_v5 *)payload;
  2761. node->session_id = payload_map.session_id;
  2762. node->num_copps = payload_map.num_copps;
  2763. payload = (u8 *)node + sizeof(struct adm_session_map_node_v5);
  2764. copps_list = (uint16_t *)payload;
  2765. for (i = 0; i < payload_map.num_copps; i++) {
  2766. port_idx =
  2767. adm_validate_and_get_port_index(payload_map.port_id[i]);
  2768. if (port_idx < 0) {
  2769. pr_err("%s: Invalid port_id 0x%x\n", __func__,
  2770. payload_map.port_id[i]);
  2771. ret = -EINVAL;
  2772. goto fail_cmd;
  2773. }
  2774. copp_idx = payload_map.copp_idx[i];
  2775. copps_list[i] = atomic_read(&this_adm.copp.id[port_idx]
  2776. [copp_idx]);
  2777. }
  2778. atomic_set(&this_adm.matrix_map_stat, -1);
  2779. ret = apr_send_pkt(this_adm.apr, (uint32_t *)matrix_map);
  2780. if (ret < 0) {
  2781. pr_err("%s: routing for syream %d failed ret %d\n",
  2782. __func__, payload_map.session_id, ret);
  2783. ret = -EINVAL;
  2784. goto fail_cmd;
  2785. }
  2786. ret = wait_event_timeout(this_adm.matrix_map_wait,
  2787. atomic_read(&this_adm.matrix_map_stat) >= 0,
  2788. msecs_to_jiffies(TIMEOUT_MS));
  2789. if (!ret) {
  2790. pr_err("%s: routing for syream %d failed\n", __func__,
  2791. payload_map.session_id);
  2792. ret = -EINVAL;
  2793. goto fail_cmd;
  2794. } else if (atomic_read(&this_adm.matrix_map_stat) > 0) {
  2795. pr_err("%s: DSP returned error[%s]\n", __func__,
  2796. adsp_err_get_err_str(atomic_read(
  2797. &this_adm.matrix_map_stat)));
  2798. ret = adsp_err_get_lnx_err_code(
  2799. atomic_read(&this_adm.matrix_map_stat));
  2800. goto fail_cmd;
  2801. }
  2802. if ((perf_mode != ULTRA_LOW_LATENCY_PCM_MODE) &&
  2803. (path != ADM_PATH_COMPRESSED_RX)) {
  2804. for (i = 0; i < payload_map.num_copps; i++) {
  2805. port_idx = afe_get_port_index(payload_map.port_id[i]);
  2806. copp_idx = payload_map.copp_idx[i];
  2807. if (port_idx < 0 || copp_idx < 0 ||
  2808. (copp_idx > MAX_COPPS_PER_PORT - 1)) {
  2809. pr_err("%s: Invalid idx port_idx %d copp_idx %d\n",
  2810. __func__, port_idx, copp_idx);
  2811. continue;
  2812. }
  2813. rtac_add_adm_device(payload_map.port_id[i],
  2814. atomic_read(&this_adm.copp.id
  2815. [port_idx][copp_idx]),
  2816. get_cal_path(path),
  2817. payload_map.session_id,
  2818. payload_map.app_type[i],
  2819. payload_map.acdb_dev_id[i]);
  2820. if (!test_bit(ADM_STATUS_CALIBRATION_REQUIRED,
  2821. (void *)&this_adm.copp.adm_status[port_idx]
  2822. [copp_idx])) {
  2823. pr_debug("%s: adm copp[0x%x][%d] already sent",
  2824. __func__, port_idx, copp_idx);
  2825. continue;
  2826. }
  2827. send_adm_cal(payload_map.port_id[i], copp_idx,
  2828. get_cal_path(path), perf_mode,
  2829. payload_map.app_type[i],
  2830. payload_map.acdb_dev_id[i],
  2831. payload_map.sample_rate[i]);
  2832. /* ADM COPP calibration is already sent */
  2833. clear_bit(ADM_STATUS_CALIBRATION_REQUIRED,
  2834. (void *)&this_adm.copp.
  2835. adm_status[port_idx][copp_idx]);
  2836. pr_debug("%s: copp_id: %d\n", __func__,
  2837. atomic_read(&this_adm.copp.id[port_idx]
  2838. [copp_idx]));
  2839. }
  2840. }
  2841. fail_cmd:
  2842. kfree(matrix_map);
  2843. return ret;
  2844. }
  2845. EXPORT_SYMBOL(adm_matrix_map);
  2846. /**
  2847. * adm_ec_ref_rx_id -
  2848. * Update EC ref port ID
  2849. *
  2850. */
  2851. void adm_ec_ref_rx_id(int port_id)
  2852. {
  2853. this_adm.ec_ref_rx = port_id;
  2854. pr_debug("%s: ec_ref_rx:%d\n", __func__, this_adm.ec_ref_rx);
  2855. }
  2856. EXPORT_SYMBOL(adm_ec_ref_rx_id);
  2857. /**
  2858. * adm_num_ec_ref_rx_chans -
  2859. * Update EC ref number of channels
  2860. *
  2861. */
  2862. void adm_num_ec_ref_rx_chans(int num_chans)
  2863. {
  2864. this_adm.num_ec_ref_rx_chans = num_chans;
  2865. pr_debug("%s: num_ec_ref_rx_chans:%d\n",
  2866. __func__, this_adm.num_ec_ref_rx_chans);
  2867. }
  2868. EXPORT_SYMBOL(adm_num_ec_ref_rx_chans);
  2869. /**
  2870. * adm_ec_ref_rx_bit_width -
  2871. * Update EC ref bit_width
  2872. *
  2873. */
  2874. void adm_ec_ref_rx_bit_width(int bit_width)
  2875. {
  2876. this_adm.ec_ref_rx_bit_width = bit_width;
  2877. pr_debug("%s: ec_ref_rx_bit_width:%d\n",
  2878. __func__, this_adm.ec_ref_rx_bit_width);
  2879. }
  2880. EXPORT_SYMBOL(adm_ec_ref_rx_bit_width);
  2881. /**
  2882. * adm_ec_ref_rx_sampling_rate -
  2883. * Update EC ref sample rate
  2884. *
  2885. */
  2886. void adm_ec_ref_rx_sampling_rate(int sampling_rate)
  2887. {
  2888. this_adm.ec_ref_rx_sampling_rate = sampling_rate;
  2889. pr_debug("%s: ec_ref_rx_sampling_rate:%d\n",
  2890. __func__, this_adm.ec_ref_rx_sampling_rate);
  2891. }
  2892. EXPORT_SYMBOL(adm_ec_ref_rx_sampling_rate);
  2893. /**
  2894. * adm_close -
  2895. * command to close ADM copp
  2896. *
  2897. * @port_id: Port ID number
  2898. * @perf_mode: performance mode like LL/ULL/..
  2899. * @copp_idx: copp index assigned
  2900. *
  2901. * Returns 0 on success or error on failure
  2902. */
  2903. int adm_close(int port_id, int perf_mode, int copp_idx)
  2904. {
  2905. struct apr_hdr close;
  2906. int ret = 0, port_idx;
  2907. int copp_id = RESET_COPP_ID;
  2908. pr_debug("%s: port_id=0x%x perf_mode: %d copp_idx: %d\n", __func__,
  2909. port_id, perf_mode, copp_idx);
  2910. port_id = q6audio_convert_virtual_to_portid(port_id);
  2911. port_idx = adm_validate_and_get_port_index(port_id);
  2912. if (port_idx < 0) {
  2913. pr_err("%s: Invalid port_id 0x%x\n",
  2914. __func__, port_id);
  2915. return -EINVAL;
  2916. }
  2917. if ((copp_idx < 0) || (copp_idx >= MAX_COPPS_PER_PORT)) {
  2918. pr_err("%s: Invalid copp idx: %d\n", __func__, copp_idx);
  2919. return -EINVAL;
  2920. }
  2921. if (this_adm.copp.adm_delay[port_idx][copp_idx] && perf_mode
  2922. == LEGACY_PCM_MODE) {
  2923. atomic_set(&this_adm.copp.adm_delay_stat[port_idx][copp_idx],
  2924. 1);
  2925. this_adm.copp.adm_delay[port_idx][copp_idx] = 0;
  2926. wake_up(&this_adm.copp.adm_delay_wait[port_idx][copp_idx]);
  2927. }
  2928. atomic_dec(&this_adm.copp.cnt[port_idx][copp_idx]);
  2929. if (!(atomic_read(&this_adm.copp.cnt[port_idx][copp_idx]))) {
  2930. copp_id = adm_get_copp_id(port_idx, copp_idx);
  2931. pr_debug("%s: Closing ADM port_idx:%d copp_idx:%d copp_id:0x%x\n",
  2932. __func__, port_idx, copp_idx, copp_id);
  2933. if ((!perf_mode) && (this_adm.outband_memmap.paddr != 0) &&
  2934. (atomic_read(&this_adm.copp.topology[port_idx][copp_idx]) ==
  2935. SRS_TRUMEDIA_TOPOLOGY_ID)) {
  2936. atomic_set(&this_adm.mem_map_index,
  2937. ADM_SRS_TRUMEDIA);
  2938. ret = adm_memory_unmap_regions();
  2939. if (ret < 0) {
  2940. pr_err("%s: adm mem unmmap err %d",
  2941. __func__, ret);
  2942. } else {
  2943. atomic_set(&this_adm.mem_map_handles
  2944. [ADM_SRS_TRUMEDIA], 0);
  2945. }
  2946. }
  2947. if ((afe_get_port_type(port_id) == MSM_AFE_PORT_TYPE_TX) &&
  2948. this_adm.sourceTrackingData.memmap.paddr) {
  2949. atomic_set(&this_adm.mem_map_index,
  2950. ADM_MEM_MAP_INDEX_SOURCE_TRACKING);
  2951. ret = adm_memory_unmap_regions();
  2952. if (ret < 0) {
  2953. pr_err("%s: adm mem unmmap err %d",
  2954. __func__, ret);
  2955. }
  2956. msm_audio_ion_free(
  2957. this_adm.sourceTrackingData.ion_client,
  2958. this_adm.sourceTrackingData.ion_handle);
  2959. this_adm.sourceTrackingData.ion_client = NULL;
  2960. this_adm.sourceTrackingData.ion_handle = NULL;
  2961. this_adm.sourceTrackingData.memmap.size = 0;
  2962. this_adm.sourceTrackingData.memmap.kvaddr = NULL;
  2963. this_adm.sourceTrackingData.memmap.paddr = 0;
  2964. this_adm.sourceTrackingData.apr_cmd_status = -1;
  2965. atomic_set(&this_adm.mem_map_handles[
  2966. ADM_MEM_MAP_INDEX_SOURCE_TRACKING], 0);
  2967. }
  2968. close.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  2969. APR_HDR_LEN(APR_HDR_SIZE),
  2970. APR_PKT_VER);
  2971. close.pkt_size = sizeof(close);
  2972. close.src_svc = APR_SVC_ADM;
  2973. close.src_domain = APR_DOMAIN_APPS;
  2974. close.src_port = port_id;
  2975. close.dest_svc = APR_SVC_ADM;
  2976. close.dest_domain = APR_DOMAIN_ADSP;
  2977. close.dest_port = copp_id;
  2978. close.token = port_idx << 16 | copp_idx;
  2979. close.opcode = ADM_CMD_DEVICE_CLOSE_V5;
  2980. atomic_set(&this_adm.copp.id[port_idx][copp_idx],
  2981. RESET_COPP_ID);
  2982. atomic_set(&this_adm.copp.cnt[port_idx][copp_idx], 0);
  2983. atomic_set(&this_adm.copp.topology[port_idx][copp_idx], 0);
  2984. atomic_set(&this_adm.copp.mode[port_idx][copp_idx], 0);
  2985. atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
  2986. atomic_set(&this_adm.copp.rate[port_idx][copp_idx], 0);
  2987. atomic_set(&this_adm.copp.channels[port_idx][copp_idx], 0);
  2988. atomic_set(&this_adm.copp.bit_width[port_idx][copp_idx], 0);
  2989. atomic_set(&this_adm.copp.app_type[port_idx][copp_idx], 0);
  2990. clear_bit(ADM_STATUS_CALIBRATION_REQUIRED,
  2991. (void *)&this_adm.copp.adm_status[port_idx][copp_idx]);
  2992. ret = apr_send_pkt(this_adm.apr, (uint32_t *)&close);
  2993. if (ret < 0) {
  2994. pr_err("%s: ADM close failed %d\n", __func__, ret);
  2995. return -EINVAL;
  2996. }
  2997. ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
  2998. atomic_read(&this_adm.copp.stat
  2999. [port_idx][copp_idx]) >= 0,
  3000. msecs_to_jiffies(TIMEOUT_MS));
  3001. if (!ret) {
  3002. pr_err("%s: ADM cmd Route timedout for port 0x%x\n",
  3003. __func__, port_id);
  3004. return -EINVAL;
  3005. } else if (atomic_read(&this_adm.copp.stat
  3006. [port_idx][copp_idx]) > 0) {
  3007. pr_err("%s: DSP returned error[%s]\n",
  3008. __func__, adsp_err_get_err_str(
  3009. atomic_read(&this_adm.copp.stat
  3010. [port_idx][copp_idx])));
  3011. return adsp_err_get_lnx_err_code(
  3012. atomic_read(&this_adm.copp.stat
  3013. [port_idx][copp_idx]));
  3014. }
  3015. }
  3016. if (perf_mode != ULTRA_LOW_LATENCY_PCM_MODE) {
  3017. pr_debug("%s: remove adm device from rtac\n", __func__);
  3018. rtac_remove_adm_device(port_id, copp_id);
  3019. }
  3020. return 0;
  3021. }
  3022. EXPORT_SYMBOL(adm_close);
  3023. int send_rtac_audvol_cal(void)
  3024. {
  3025. int ret = 0;
  3026. int ret2 = 0;
  3027. int i = 0;
  3028. int copp_idx, port_idx, acdb_id, app_id, path;
  3029. struct cal_block_data *cal_block = NULL;
  3030. struct audio_cal_info_audvol *audvol_cal_info = NULL;
  3031. struct rtac_adm rtac_adm_data;
  3032. mutex_lock(&this_adm.cal_data[ADM_RTAC_AUDVOL_CAL]->lock);
  3033. cal_block = cal_utils_get_only_cal_block(
  3034. this_adm.cal_data[ADM_RTAC_AUDVOL_CAL]);
  3035. if (cal_block == NULL) {
  3036. pr_err("%s: can't find cal block!\n", __func__);
  3037. goto unlock;
  3038. }
  3039. audvol_cal_info = cal_block->cal_info;
  3040. if (audvol_cal_info == NULL) {
  3041. pr_err("%s: audvol_cal_info is NULL!\n", __func__);
  3042. goto unlock;
  3043. }
  3044. get_rtac_adm_data(&rtac_adm_data);
  3045. for (; i < rtac_adm_data.num_of_dev; i++) {
  3046. acdb_id = rtac_adm_data.device[i].acdb_dev_id;
  3047. if (acdb_id == 0)
  3048. acdb_id = audvol_cal_info->acdb_id;
  3049. app_id = rtac_adm_data.device[i].app_type;
  3050. if (app_id == 0)
  3051. app_id = audvol_cal_info->app_type;
  3052. path = afe_get_port_type(rtac_adm_data.device[i].afe_port);
  3053. if ((acdb_id == audvol_cal_info->acdb_id) &&
  3054. (app_id == audvol_cal_info->app_type) &&
  3055. (path == audvol_cal_info->path)) {
  3056. if (adm_get_indexes_from_copp_id(rtac_adm_data.
  3057. device[i].copp, &copp_idx, &port_idx) != 0) {
  3058. pr_debug("%s: Copp Id %d is not active\n",
  3059. __func__,
  3060. rtac_adm_data.device[i].copp);
  3061. continue;
  3062. }
  3063. ret2 = adm_remap_and_send_cal_block(ADM_RTAC_AUDVOL_CAL,
  3064. rtac_adm_data.device[i].afe_port,
  3065. copp_idx, cal_block,
  3066. atomic_read(&this_adm.copp.
  3067. mode[port_idx][copp_idx]),
  3068. audvol_cal_info->app_type,
  3069. audvol_cal_info->acdb_id,
  3070. atomic_read(&this_adm.copp.
  3071. rate[port_idx][copp_idx]));
  3072. if (ret2 < 0) {
  3073. pr_debug("%s: remap and send failed for copp Id %d, acdb id %d, app type %d, path %d\n",
  3074. __func__, rtac_adm_data.device[i].copp,
  3075. audvol_cal_info->acdb_id,
  3076. audvol_cal_info->app_type,
  3077. audvol_cal_info->path);
  3078. ret = ret2;
  3079. }
  3080. }
  3081. }
  3082. unlock:
  3083. mutex_unlock(&this_adm.cal_data[ADM_RTAC_AUDVOL_CAL]->lock);
  3084. return ret;
  3085. }
  3086. int adm_map_rtac_block(struct rtac_cal_block_data *cal_block)
  3087. {
  3088. int result = 0;
  3089. pr_debug("%s:\n", __func__);
  3090. if (cal_block == NULL) {
  3091. pr_err("%s: cal_block is NULL!\n",
  3092. __func__);
  3093. result = -EINVAL;
  3094. goto done;
  3095. }
  3096. if (cal_block->cal_data.paddr == 0) {
  3097. pr_debug("%s: No address to map!\n",
  3098. __func__);
  3099. result = -EINVAL;
  3100. goto done;
  3101. }
  3102. if (cal_block->map_data.map_size == 0) {
  3103. pr_debug("%s: map size is 0!\n",
  3104. __func__);
  3105. result = -EINVAL;
  3106. goto done;
  3107. }
  3108. /* valid port ID needed for callback use primary I2S */
  3109. atomic_set(&this_adm.mem_map_index, ADM_RTAC_APR_CAL);
  3110. result = adm_memory_map_regions(&cal_block->cal_data.paddr, 0,
  3111. &cal_block->map_data.map_size, 1);
  3112. if (result < 0) {
  3113. pr_err("%s: RTAC mmap did not work! size = %d result %d\n",
  3114. __func__,
  3115. cal_block->map_data.map_size, result);
  3116. pr_debug("%s: RTAC mmap did not work! addr = 0x%pK, size = %d\n",
  3117. __func__,
  3118. &cal_block->cal_data.paddr,
  3119. cal_block->map_data.map_size);
  3120. goto done;
  3121. }
  3122. cal_block->map_data.map_handle = atomic_read(
  3123. &this_adm.mem_map_handles[ADM_RTAC_APR_CAL]);
  3124. done:
  3125. return result;
  3126. }
  3127. int adm_unmap_rtac_block(uint32_t *mem_map_handle)
  3128. {
  3129. int result = 0;
  3130. pr_debug("%s:\n", __func__);
  3131. if (mem_map_handle == NULL) {
  3132. pr_debug("%s: Map handle is NULL, nothing to unmap\n",
  3133. __func__);
  3134. goto done;
  3135. }
  3136. if (*mem_map_handle == 0) {
  3137. pr_debug("%s: Map handle is 0, nothing to unmap\n",
  3138. __func__);
  3139. goto done;
  3140. }
  3141. if (*mem_map_handle != atomic_read(
  3142. &this_adm.mem_map_handles[ADM_RTAC_APR_CAL])) {
  3143. pr_err("%s: Map handles do not match! Unmapping RTAC, RTAC map 0x%x, ADM map 0x%x\n",
  3144. __func__, *mem_map_handle, atomic_read(
  3145. &this_adm.mem_map_handles[ADM_RTAC_APR_CAL]));
  3146. /* if mismatch use handle passed in to unmap */
  3147. atomic_set(&this_adm.mem_map_handles[ADM_RTAC_APR_CAL],
  3148. *mem_map_handle);
  3149. }
  3150. /* valid port ID needed for callback use primary I2S */
  3151. atomic_set(&this_adm.mem_map_index, ADM_RTAC_APR_CAL);
  3152. result = adm_memory_unmap_regions();
  3153. if (result < 0) {
  3154. pr_debug("%s: adm_memory_unmap_regions failed, error %d\n",
  3155. __func__, result);
  3156. } else {
  3157. atomic_set(&this_adm.mem_map_handles[ADM_RTAC_APR_CAL], 0);
  3158. *mem_map_handle = 0;
  3159. }
  3160. done:
  3161. return result;
  3162. }
  3163. static int get_cal_type_index(int32_t cal_type)
  3164. {
  3165. int ret = -EINVAL;
  3166. switch (cal_type) {
  3167. case ADM_AUDPROC_CAL_TYPE:
  3168. ret = ADM_AUDPROC_CAL;
  3169. break;
  3170. case ADM_LSM_AUDPROC_CAL_TYPE:
  3171. ret = ADM_LSM_AUDPROC_CAL;
  3172. break;
  3173. case ADM_AUDVOL_CAL_TYPE:
  3174. ret = ADM_AUDVOL_CAL;
  3175. break;
  3176. case ADM_CUST_TOPOLOGY_CAL_TYPE:
  3177. ret = ADM_CUSTOM_TOP_CAL;
  3178. break;
  3179. case ADM_RTAC_INFO_CAL_TYPE:
  3180. ret = ADM_RTAC_INFO_CAL;
  3181. break;
  3182. case ADM_RTAC_APR_CAL_TYPE:
  3183. ret = ADM_RTAC_APR_CAL;
  3184. break;
  3185. case ADM_RTAC_AUDVOL_CAL_TYPE:
  3186. ret = ADM_RTAC_AUDVOL_CAL;
  3187. break;
  3188. default:
  3189. pr_err("%s: invalid cal type %d!\n", __func__, cal_type);
  3190. }
  3191. return ret;
  3192. }
  3193. static int adm_alloc_cal(int32_t cal_type, size_t data_size, void *data)
  3194. {
  3195. int ret = 0;
  3196. int cal_index;
  3197. pr_debug("%s:\n", __func__);
  3198. cal_index = get_cal_type_index(cal_type);
  3199. if (cal_index < 0) {
  3200. pr_err("%s: could not get cal index %d!\n",
  3201. __func__, cal_index);
  3202. ret = -EINVAL;
  3203. goto done;
  3204. }
  3205. ret = cal_utils_alloc_cal(data_size, data,
  3206. this_adm.cal_data[cal_index], 0, NULL);
  3207. if (ret < 0) {
  3208. pr_err("%s: cal_utils_alloc_block failed, ret = %d, cal type = %d!\n",
  3209. __func__, ret, cal_type);
  3210. ret = -EINVAL;
  3211. goto done;
  3212. }
  3213. done:
  3214. return ret;
  3215. }
  3216. static int adm_dealloc_cal(int32_t cal_type, size_t data_size, void *data)
  3217. {
  3218. int ret = 0;
  3219. int cal_index;
  3220. pr_debug("%s:\n", __func__);
  3221. cal_index = get_cal_type_index(cal_type);
  3222. if (cal_index < 0) {
  3223. pr_err("%s: could not get cal index %d!\n",
  3224. __func__, cal_index);
  3225. ret = -EINVAL;
  3226. goto done;
  3227. }
  3228. ret = cal_utils_dealloc_cal(data_size, data,
  3229. this_adm.cal_data[cal_index]);
  3230. if (ret < 0) {
  3231. pr_err("%s: cal_utils_dealloc_block failed, ret = %d, cal type = %d!\n",
  3232. __func__, ret, cal_type);
  3233. ret = -EINVAL;
  3234. goto done;
  3235. }
  3236. done:
  3237. return ret;
  3238. }
  3239. static int adm_set_cal(int32_t cal_type, size_t data_size, void *data)
  3240. {
  3241. int ret = 0;
  3242. int cal_index;
  3243. pr_debug("%s:\n", __func__);
  3244. cal_index = get_cal_type_index(cal_type);
  3245. if (cal_index < 0) {
  3246. pr_err("%s: could not get cal index %d!\n",
  3247. __func__, cal_index);
  3248. ret = -EINVAL;
  3249. goto done;
  3250. }
  3251. ret = cal_utils_set_cal(data_size, data,
  3252. this_adm.cal_data[cal_index], 0, NULL);
  3253. if (ret < 0) {
  3254. pr_err("%s: cal_utils_set_cal failed, ret = %d, cal type = %d!\n",
  3255. __func__, ret, cal_type);
  3256. ret = -EINVAL;
  3257. goto done;
  3258. }
  3259. if (cal_index == ADM_CUSTOM_TOP_CAL) {
  3260. mutex_lock(&this_adm.cal_data[ADM_CUSTOM_TOP_CAL]->lock);
  3261. this_adm.set_custom_topology = 1;
  3262. mutex_unlock(&this_adm.cal_data[ADM_CUSTOM_TOP_CAL]->lock);
  3263. } else if (cal_index == ADM_RTAC_AUDVOL_CAL) {
  3264. send_rtac_audvol_cal();
  3265. }
  3266. done:
  3267. return ret;
  3268. }
  3269. static int adm_map_cal_data(int32_t cal_type,
  3270. struct cal_block_data *cal_block)
  3271. {
  3272. int ret = 0;
  3273. int cal_index;
  3274. pr_debug("%s:\n", __func__);
  3275. cal_index = get_cal_type_index(cal_type);
  3276. if (cal_index < 0) {
  3277. pr_err("%s: could not get cal index %d!\n",
  3278. __func__, cal_index);
  3279. ret = -EINVAL;
  3280. goto done;
  3281. }
  3282. atomic_set(&this_adm.mem_map_index, cal_index);
  3283. ret = adm_memory_map_regions(&cal_block->cal_data.paddr, 0,
  3284. (uint32_t *)&cal_block->map_data.map_size, 1);
  3285. if (ret < 0) {
  3286. pr_err("%s: map did not work! cal_type %i ret %d\n",
  3287. __func__, cal_index, ret);
  3288. ret = -ENODEV;
  3289. goto done;
  3290. }
  3291. cal_block->map_data.q6map_handle = atomic_read(&this_adm.
  3292. mem_map_handles[cal_index]);
  3293. done:
  3294. return ret;
  3295. }
  3296. static int adm_unmap_cal_data(int32_t cal_type,
  3297. struct cal_block_data *cal_block)
  3298. {
  3299. int ret = 0;
  3300. int cal_index;
  3301. pr_debug("%s:\n", __func__);
  3302. cal_index = get_cal_type_index(cal_type);
  3303. if (cal_index < 0) {
  3304. pr_err("%s: could not get cal index %d!\n",
  3305. __func__, cal_index);
  3306. ret = -EINVAL;
  3307. goto done;
  3308. }
  3309. if (cal_block == NULL) {
  3310. pr_err("%s: Cal block is NULL!\n",
  3311. __func__);
  3312. goto done;
  3313. }
  3314. if (cal_block->map_data.q6map_handle == 0) {
  3315. pr_err("%s: Map handle is NULL, nothing to unmap\n",
  3316. __func__);
  3317. goto done;
  3318. }
  3319. atomic_set(&this_adm.mem_map_handles[cal_index],
  3320. cal_block->map_data.q6map_handle);
  3321. atomic_set(&this_adm.mem_map_index, cal_index);
  3322. ret = adm_memory_unmap_regions();
  3323. if (ret < 0) {
  3324. pr_err("%s: unmap did not work! cal_type %i ret %d\n",
  3325. __func__, cal_index, ret);
  3326. ret = -ENODEV;
  3327. goto done;
  3328. }
  3329. cal_block->map_data.q6map_handle = 0;
  3330. done:
  3331. return ret;
  3332. }
  3333. static void adm_delete_cal_data(void)
  3334. {
  3335. pr_debug("%s:\n", __func__);
  3336. cal_utils_destroy_cal_types(ADM_MAX_CAL_TYPES, this_adm.cal_data);
  3337. }
  3338. static int adm_init_cal_data(void)
  3339. {
  3340. int ret = 0;
  3341. struct cal_type_info cal_type_info[] = {
  3342. {{ADM_CUST_TOPOLOGY_CAL_TYPE,
  3343. {adm_alloc_cal, adm_dealloc_cal, NULL,
  3344. adm_set_cal, NULL, NULL} },
  3345. {adm_map_cal_data, adm_unmap_cal_data,
  3346. cal_utils_match_buf_num} },
  3347. {{ADM_AUDPROC_CAL_TYPE,
  3348. {adm_alloc_cal, adm_dealloc_cal, NULL,
  3349. adm_set_cal, NULL, NULL} },
  3350. {adm_map_cal_data, adm_unmap_cal_data,
  3351. cal_utils_match_buf_num} },
  3352. {{ADM_LSM_AUDPROC_CAL_TYPE,
  3353. {adm_alloc_cal, adm_dealloc_cal, NULL,
  3354. adm_set_cal, NULL, NULL} },
  3355. {adm_map_cal_data, adm_unmap_cal_data,
  3356. cal_utils_match_buf_num} },
  3357. {{ADM_AUDVOL_CAL_TYPE,
  3358. {adm_alloc_cal, adm_dealloc_cal, NULL,
  3359. adm_set_cal, NULL, NULL} },
  3360. {adm_map_cal_data, adm_unmap_cal_data,
  3361. cal_utils_match_buf_num} },
  3362. {{ADM_RTAC_INFO_CAL_TYPE,
  3363. {NULL, NULL, NULL, NULL, NULL, NULL} },
  3364. {NULL, NULL, cal_utils_match_buf_num} },
  3365. {{ADM_RTAC_APR_CAL_TYPE,
  3366. {NULL, NULL, NULL, NULL, NULL, NULL} },
  3367. {NULL, NULL, cal_utils_match_buf_num} },
  3368. {{SRS_TRUMEDIA_CAL_TYPE,
  3369. {NULL, NULL, NULL, NULL, NULL, NULL} },
  3370. {NULL, NULL, cal_utils_match_buf_num} },
  3371. {{ADM_RTAC_AUDVOL_CAL_TYPE,
  3372. {adm_alloc_cal, adm_dealloc_cal, NULL,
  3373. adm_set_cal, NULL, NULL} },
  3374. {adm_map_cal_data, adm_unmap_cal_data,
  3375. cal_utils_match_buf_num} },
  3376. };
  3377. pr_debug("%s:\n", __func__);
  3378. ret = cal_utils_create_cal_types(ADM_MAX_CAL_TYPES, this_adm.cal_data,
  3379. cal_type_info);
  3380. if (ret < 0) {
  3381. pr_err("%s: could not create cal type! ret %d\n",
  3382. __func__, ret);
  3383. ret = -EINVAL;
  3384. goto err;
  3385. }
  3386. return ret;
  3387. err:
  3388. adm_delete_cal_data();
  3389. return ret;
  3390. }
  3391. /**
  3392. * adm_set_volume -
  3393. * command to set volume on ADM copp
  3394. *
  3395. * @port_id: Port ID number
  3396. * @copp_idx: copp index assigned
  3397. * @volume: gain value to set
  3398. *
  3399. * Returns 0 on success or error on failure
  3400. */
  3401. int adm_set_volume(int port_id, int copp_idx, int volume)
  3402. {
  3403. struct audproc_volume_ctrl_master_gain audproc_vol;
  3404. int sz = 0;
  3405. int rc = 0;
  3406. int port_idx;
  3407. pr_debug("%s: port_id %d, volume %d\n", __func__, port_id, volume);
  3408. port_id = afe_convert_virtual_to_portid(port_id);
  3409. port_idx = adm_validate_and_get_port_index(port_id);
  3410. if (port_idx < 0) {
  3411. pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
  3412. rc = -EINVAL;
  3413. goto fail_cmd;
  3414. }
  3415. if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
  3416. pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
  3417. return -EINVAL;
  3418. }
  3419. sz = sizeof(struct audproc_volume_ctrl_master_gain);
  3420. audproc_vol.params.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  3421. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  3422. audproc_vol.params.hdr.pkt_size = sz;
  3423. audproc_vol.params.hdr.src_svc = APR_SVC_ADM;
  3424. audproc_vol.params.hdr.src_domain = APR_DOMAIN_APPS;
  3425. audproc_vol.params.hdr.src_port = port_id;
  3426. audproc_vol.params.hdr.dest_svc = APR_SVC_ADM;
  3427. audproc_vol.params.hdr.dest_domain = APR_DOMAIN_ADSP;
  3428. audproc_vol.params.hdr.dest_port =
  3429. atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
  3430. audproc_vol.params.hdr.token = port_idx << 16 | copp_idx;
  3431. audproc_vol.params.hdr.opcode = ADM_CMD_SET_PP_PARAMS_V5;
  3432. audproc_vol.params.payload_addr_lsw = 0;
  3433. audproc_vol.params.payload_addr_msw = 0;
  3434. audproc_vol.params.mem_map_handle = 0;
  3435. audproc_vol.params.payload_size = sizeof(audproc_vol) -
  3436. sizeof(audproc_vol.params);
  3437. audproc_vol.data.module_id = AUDPROC_MODULE_ID_VOL_CTRL;
  3438. audproc_vol.data.param_id = AUDPROC_PARAM_ID_VOL_CTRL_MASTER_GAIN;
  3439. audproc_vol.data.param_size = audproc_vol.params.payload_size -
  3440. sizeof(audproc_vol.data);
  3441. audproc_vol.data.reserved = 0;
  3442. audproc_vol.master_gain = volume;
  3443. atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
  3444. rc = apr_send_pkt(this_adm.apr, (uint32_t *)&audproc_vol);
  3445. if (rc < 0) {
  3446. pr_err("%s: Set params failed port = %#x\n",
  3447. __func__, port_id);
  3448. rc = -EINVAL;
  3449. goto fail_cmd;
  3450. }
  3451. /* Wait for the callback */
  3452. rc = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
  3453. atomic_read(&this_adm.copp.stat[port_idx][copp_idx]) >= 0,
  3454. msecs_to_jiffies(TIMEOUT_MS));
  3455. if (!rc) {
  3456. pr_err("%s: Vol cntrl Set params timed out port = %#x\n",
  3457. __func__, port_id);
  3458. rc = -EINVAL;
  3459. goto fail_cmd;
  3460. } else if (atomic_read(&this_adm.copp.stat
  3461. [port_idx][copp_idx]) > 0) {
  3462. pr_err("%s: DSP returned error[%s]\n",
  3463. __func__, adsp_err_get_err_str(
  3464. atomic_read(&this_adm.copp.stat
  3465. [port_idx][copp_idx])));
  3466. rc = adsp_err_get_lnx_err_code(
  3467. atomic_read(&this_adm.copp.stat
  3468. [port_idx][copp_idx]));
  3469. goto fail_cmd;
  3470. }
  3471. rc = 0;
  3472. fail_cmd:
  3473. return rc;
  3474. }
  3475. EXPORT_SYMBOL(adm_set_volume);
  3476. /**
  3477. * adm_set_softvolume -
  3478. * command to set softvolume
  3479. *
  3480. * @port_id: Port ID number
  3481. * @copp_idx: copp index assigned
  3482. * @softvol_param: Params to set for softvolume
  3483. *
  3484. * Returns 0 on success or error on failure
  3485. */
  3486. int adm_set_softvolume(int port_id, int copp_idx,
  3487. struct audproc_softvolume_params *softvol_param)
  3488. {
  3489. struct audproc_soft_step_volume_params audproc_softvol;
  3490. int sz = 0;
  3491. int rc = 0;
  3492. int port_idx;
  3493. pr_debug("%s: period %d step %d curve %d\n", __func__,
  3494. softvol_param->period, softvol_param->step,
  3495. softvol_param->rampingcurve);
  3496. port_id = afe_convert_virtual_to_portid(port_id);
  3497. port_idx = adm_validate_and_get_port_index(port_id);
  3498. if (port_idx < 0) {
  3499. pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
  3500. rc = -EINVAL;
  3501. goto fail_cmd;
  3502. }
  3503. if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
  3504. pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
  3505. return -EINVAL;
  3506. }
  3507. sz = sizeof(struct audproc_soft_step_volume_params);
  3508. audproc_softvol.params.hdr.hdr_field =
  3509. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  3510. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  3511. audproc_softvol.params.hdr.pkt_size = sz;
  3512. audproc_softvol.params.hdr.src_svc = APR_SVC_ADM;
  3513. audproc_softvol.params.hdr.src_domain = APR_DOMAIN_APPS;
  3514. audproc_softvol.params.hdr.src_port = port_id;
  3515. audproc_softvol.params.hdr.dest_svc = APR_SVC_ADM;
  3516. audproc_softvol.params.hdr.dest_domain = APR_DOMAIN_ADSP;
  3517. audproc_softvol.params.hdr.dest_port =
  3518. atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
  3519. audproc_softvol.params.hdr.token = port_idx << 16 | copp_idx;
  3520. audproc_softvol.params.hdr.opcode = ADM_CMD_SET_PP_PARAMS_V5;
  3521. audproc_softvol.params.payload_addr_lsw = 0;
  3522. audproc_softvol.params.payload_addr_msw = 0;
  3523. audproc_softvol.params.mem_map_handle = 0;
  3524. audproc_softvol.params.payload_size = sizeof(audproc_softvol) -
  3525. sizeof(audproc_softvol.params);
  3526. audproc_softvol.data.module_id = AUDPROC_MODULE_ID_VOL_CTRL;
  3527. audproc_softvol.data.param_id =
  3528. AUDPROC_PARAM_ID_SOFT_VOL_STEPPING_PARAMETERS;
  3529. audproc_softvol.data.param_size = audproc_softvol.params.payload_size -
  3530. sizeof(audproc_softvol.data);
  3531. audproc_softvol.data.reserved = 0;
  3532. audproc_softvol.period = softvol_param->period;
  3533. audproc_softvol.step = softvol_param->step;
  3534. audproc_softvol.ramping_curve = softvol_param->rampingcurve;
  3535. pr_debug("%s: period %d, step %d, curve %d\n", __func__,
  3536. audproc_softvol.period, audproc_softvol.step,
  3537. audproc_softvol.ramping_curve);
  3538. atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
  3539. rc = apr_send_pkt(this_adm.apr, (uint32_t *)&audproc_softvol);
  3540. if (rc < 0) {
  3541. pr_err("%s: Set params failed port = %#x\n",
  3542. __func__, port_id);
  3543. rc = -EINVAL;
  3544. goto fail_cmd;
  3545. }
  3546. /* Wait for the callback */
  3547. rc = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
  3548. atomic_read(&this_adm.copp.stat[port_idx][copp_idx]) >= 0,
  3549. msecs_to_jiffies(TIMEOUT_MS));
  3550. if (!rc) {
  3551. pr_err("%s: Soft volume Set params timed out port = %#x\n",
  3552. __func__, port_id);
  3553. rc = -EINVAL;
  3554. goto fail_cmd;
  3555. } else if (atomic_read(&this_adm.copp.stat
  3556. [port_idx][copp_idx]) > 0) {
  3557. pr_err("%s: DSP returned error[%s]\n",
  3558. __func__, adsp_err_get_err_str(
  3559. atomic_read(&this_adm.copp.stat
  3560. [port_idx][copp_idx])));
  3561. rc = adsp_err_get_lnx_err_code(
  3562. atomic_read(&this_adm.copp.stat
  3563. [port_idx][copp_idx]));
  3564. goto fail_cmd;
  3565. }
  3566. rc = 0;
  3567. fail_cmd:
  3568. return rc;
  3569. }
  3570. EXPORT_SYMBOL(adm_set_softvolume);
  3571. /**
  3572. * adm_set_mic_gain -
  3573. * command to set MIC gain
  3574. *
  3575. * @port_id: Port ID number
  3576. * @copp_idx: copp index assigned
  3577. * @volume: gain value to set
  3578. *
  3579. * Returns 0 on success or error on failure
  3580. */
  3581. int adm_set_mic_gain(int port_id, int copp_idx, int volume)
  3582. {
  3583. struct adm_set_mic_gain_params mic_gain_params;
  3584. int rc = 0;
  3585. int sz, port_idx;
  3586. pr_debug("%s:\n", __func__);
  3587. port_id = afe_convert_virtual_to_portid(port_id);
  3588. port_idx = adm_validate_and_get_port_index(port_id);
  3589. if (port_idx < 0) {
  3590. pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
  3591. return -EINVAL;
  3592. }
  3593. sz = sizeof(struct adm_set_mic_gain_params);
  3594. mic_gain_params.params.hdr.hdr_field =
  3595. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  3596. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  3597. mic_gain_params.params.hdr.pkt_size = sz;
  3598. mic_gain_params.params.hdr.src_svc = APR_SVC_ADM;
  3599. mic_gain_params.params.hdr.src_domain = APR_DOMAIN_APPS;
  3600. mic_gain_params.params.hdr.src_port = port_id;
  3601. mic_gain_params.params.hdr.dest_svc = APR_SVC_ADM;
  3602. mic_gain_params.params.hdr.dest_domain = APR_DOMAIN_ADSP;
  3603. mic_gain_params.params.hdr.dest_port =
  3604. atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
  3605. mic_gain_params.params.hdr.token = port_idx << 16 | copp_idx;
  3606. mic_gain_params.params.hdr.opcode = ADM_CMD_SET_PP_PARAMS_V5;
  3607. mic_gain_params.params.payload_addr_lsw = 0;
  3608. mic_gain_params.params.payload_addr_msw = 0;
  3609. mic_gain_params.params.mem_map_handle = 0;
  3610. mic_gain_params.params.payload_size =
  3611. sizeof(struct adm_param_data_v5) +
  3612. sizeof(struct admx_mic_gain);
  3613. mic_gain_params.data.module_id = ADM_MODULE_IDX_MIC_GAIN_CTRL;
  3614. mic_gain_params.data.param_id = ADM_PARAM_IDX_MIC_GAIN;
  3615. mic_gain_params.data.param_size =
  3616. sizeof(struct admx_mic_gain);
  3617. mic_gain_params.data.reserved = 0;
  3618. mic_gain_params.mic_gain_data.tx_mic_gain = volume;
  3619. mic_gain_params.mic_gain_data.reserved = 0;
  3620. pr_debug("%s: Mic Gain set to %d at port_id 0x%x\n",
  3621. __func__, volume, port_id);
  3622. atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
  3623. rc = apr_send_pkt(this_adm.apr, (uint32_t *)&mic_gain_params);
  3624. if (rc < 0) {
  3625. pr_err("%s: Set params failed port = %#x\n",
  3626. __func__, port_id);
  3627. rc = -EINVAL;
  3628. goto fail_cmd;
  3629. }
  3630. /* Wait for the callback */
  3631. rc = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
  3632. atomic_read(&this_adm.copp.stat[port_idx][copp_idx]) >= 0,
  3633. msecs_to_jiffies(TIMEOUT_MS));
  3634. if (!rc) {
  3635. pr_err("%s: Mic Gain Set params timed out port = %#x\n",
  3636. __func__, port_id);
  3637. rc = -EINVAL;
  3638. goto fail_cmd;
  3639. } else if (atomic_read(&this_adm.copp.stat
  3640. [port_idx][copp_idx]) > 0) {
  3641. pr_err("%s: DSP returned error[%s]\n",
  3642. __func__, adsp_err_get_err_str(
  3643. atomic_read(&this_adm.copp.stat
  3644. [port_idx][copp_idx])));
  3645. rc = adsp_err_get_lnx_err_code(
  3646. atomic_read(&this_adm.copp.stat
  3647. [port_idx][copp_idx]));
  3648. goto fail_cmd;
  3649. }
  3650. rc = 0;
  3651. fail_cmd:
  3652. return rc;
  3653. }
  3654. EXPORT_SYMBOL(adm_set_mic_gain);
  3655. /**
  3656. * adm_send_set_multichannel_ec_primary_mic_ch -
  3657. * command to set multi-ch EC primary mic
  3658. *
  3659. * @port_id: Port ID number
  3660. * @copp_idx: copp index assigned
  3661. * @primary_mic_ch: channel number of primary mic
  3662. *
  3663. * Returns 0 on success or error on failure
  3664. */
  3665. int adm_send_set_multichannel_ec_primary_mic_ch(int port_id, int copp_idx,
  3666. int primary_mic_ch)
  3667. {
  3668. struct adm_set_sec_primary_ch_params sec_primary_ch_params;
  3669. int rc = 0;
  3670. int sz, port_idx;
  3671. pr_debug("%s port_id 0x%x, copp_idx 0x%x, primary_mic_ch %d\n",
  3672. __func__, port_id, copp_idx, primary_mic_ch);
  3673. port_id = afe_convert_virtual_to_portid(port_id);
  3674. port_idx = adm_validate_and_get_port_index(port_id);
  3675. if (port_idx < 0) {
  3676. pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
  3677. return -EINVAL;
  3678. }
  3679. if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
  3680. pr_err("%s: Invalid copp_idx 0x%x\n", __func__, copp_idx);
  3681. return -EINVAL;
  3682. }
  3683. sz = sizeof(struct adm_set_sec_primary_ch_params);
  3684. sec_primary_ch_params.params.hdr.hdr_field =
  3685. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  3686. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  3687. sec_primary_ch_params.params.hdr.pkt_size = sz;
  3688. sec_primary_ch_params.params.hdr.src_svc = APR_SVC_ADM;
  3689. sec_primary_ch_params.params.hdr.src_domain = APR_DOMAIN_APPS;
  3690. sec_primary_ch_params.params.hdr.src_port = port_id;
  3691. sec_primary_ch_params.params.hdr.dest_svc = APR_SVC_ADM;
  3692. sec_primary_ch_params.params.hdr.dest_domain = APR_DOMAIN_ADSP;
  3693. sec_primary_ch_params.params.hdr.dest_port =
  3694. atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
  3695. sec_primary_ch_params.params.hdr.token = port_idx << 16 | copp_idx;
  3696. sec_primary_ch_params.params.hdr.opcode = ADM_CMD_SET_PP_PARAMS_V5;
  3697. sec_primary_ch_params.params.payload_addr_lsw = 0;
  3698. sec_primary_ch_params.params.payload_addr_msw = 0;
  3699. sec_primary_ch_params.params.mem_map_handle = 0;
  3700. sec_primary_ch_params.params.payload_size =
  3701. sizeof(struct adm_param_data_v5) +
  3702. sizeof(struct admx_sec_primary_mic_ch);
  3703. sec_primary_ch_params.data.module_id =
  3704. AUDPROC_MODULE_ID_VOICE_TX_SECNS;
  3705. sec_primary_ch_params.data.param_id =
  3706. AUDPROC_PARAM_IDX_SEC_PRIMARY_MIC_CH;
  3707. sec_primary_ch_params.data.param_size =
  3708. sizeof(struct admx_sec_primary_mic_ch);
  3709. sec_primary_ch_params.data.reserved = 0;
  3710. sec_primary_ch_params.sec_primary_mic_ch_data.version = 0;
  3711. sec_primary_ch_params.sec_primary_mic_ch_data.reserved = 0;
  3712. sec_primary_ch_params.sec_primary_mic_ch_data.sec_primary_mic_ch =
  3713. primary_mic_ch;
  3714. sec_primary_ch_params.sec_primary_mic_ch_data.reserved1 = 0;
  3715. atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
  3716. rc = apr_send_pkt(this_adm.apr, (uint32_t *)&sec_primary_ch_params);
  3717. if (rc < 0) {
  3718. pr_err("%s: Set params failed port = %#x\n",
  3719. __func__, port_id);
  3720. rc = -EINVAL;
  3721. goto fail_cmd;
  3722. }
  3723. /* Wait for the callback */
  3724. rc = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
  3725. atomic_read(&this_adm.copp.stat[port_idx][copp_idx]) >= 0,
  3726. msecs_to_jiffies(TIMEOUT_MS));
  3727. if (!rc) {
  3728. pr_err("%s: Mic Set params timed out port = %#x\n",
  3729. __func__, port_id);
  3730. rc = -EINVAL;
  3731. goto fail_cmd;
  3732. } else if (atomic_read(&this_adm.copp.stat
  3733. [port_idx][copp_idx]) > 0) {
  3734. pr_err("%s: DSP returned error[%s]\n",
  3735. __func__, adsp_err_get_err_str(
  3736. atomic_read(&this_adm.copp.stat
  3737. [port_idx][copp_idx])));
  3738. rc = adsp_err_get_lnx_err_code(
  3739. atomic_read(&this_adm.copp.stat
  3740. [port_idx][copp_idx]));
  3741. goto fail_cmd;
  3742. }
  3743. rc = 0;
  3744. fail_cmd:
  3745. return rc;
  3746. }
  3747. EXPORT_SYMBOL(adm_send_set_multichannel_ec_primary_mic_ch);
  3748. /**
  3749. * adm_param_enable -
  3750. * command to send params to ADM for given module
  3751. *
  3752. * @port_id: Port ID number
  3753. * @copp_idx: copp index assigned
  3754. * @module_id: ADM module
  3755. * @enable: flag to enable or disable module
  3756. *
  3757. * Returns 0 on success or error on failure
  3758. */
  3759. int adm_param_enable(int port_id, int copp_idx, int module_id, int enable)
  3760. {
  3761. struct audproc_enable_param_t adm_mod_enable;
  3762. int sz = 0;
  3763. int rc = 0;
  3764. int port_idx;
  3765. pr_debug("%s port_id %d, module_id 0x%x, enable %d\n",
  3766. __func__, port_id, module_id, enable);
  3767. port_id = afe_convert_virtual_to_portid(port_id);
  3768. port_idx = adm_validate_and_get_port_index(port_id);
  3769. if (port_idx < 0) {
  3770. pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
  3771. rc = -EINVAL;
  3772. goto fail_cmd;
  3773. }
  3774. if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
  3775. pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
  3776. return -EINVAL;
  3777. }
  3778. sz = sizeof(struct audproc_enable_param_t);
  3779. adm_mod_enable.pp_params.hdr.hdr_field =
  3780. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  3781. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  3782. adm_mod_enable.pp_params.hdr.pkt_size = sz;
  3783. adm_mod_enable.pp_params.hdr.src_svc = APR_SVC_ADM;
  3784. adm_mod_enable.pp_params.hdr.src_domain = APR_DOMAIN_APPS;
  3785. adm_mod_enable.pp_params.hdr.src_port = port_id;
  3786. adm_mod_enable.pp_params.hdr.dest_svc = APR_SVC_ADM;
  3787. adm_mod_enable.pp_params.hdr.dest_domain = APR_DOMAIN_ADSP;
  3788. adm_mod_enable.pp_params.hdr.dest_port =
  3789. atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
  3790. adm_mod_enable.pp_params.hdr.token = port_idx << 16 | copp_idx;
  3791. adm_mod_enable.pp_params.hdr.opcode = ADM_CMD_SET_PP_PARAMS_V5;
  3792. adm_mod_enable.pp_params.payload_addr_lsw = 0;
  3793. adm_mod_enable.pp_params.payload_addr_msw = 0;
  3794. adm_mod_enable.pp_params.mem_map_handle = 0;
  3795. adm_mod_enable.pp_params.payload_size = sizeof(adm_mod_enable) -
  3796. sizeof(adm_mod_enable.pp_params) +
  3797. sizeof(adm_mod_enable.pp_params.params);
  3798. adm_mod_enable.pp_params.params.module_id = module_id;
  3799. adm_mod_enable.pp_params.params.param_id = AUDPROC_PARAM_ID_ENABLE;
  3800. adm_mod_enable.pp_params.params.param_size =
  3801. adm_mod_enable.pp_params.payload_size -
  3802. sizeof(adm_mod_enable.pp_params.params);
  3803. adm_mod_enable.pp_params.params.reserved = 0;
  3804. adm_mod_enable.enable = enable;
  3805. atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
  3806. rc = apr_send_pkt(this_adm.apr, (uint32_t *)&adm_mod_enable);
  3807. if (rc < 0) {
  3808. pr_err("%s: Set params failed port = %#x\n",
  3809. __func__, port_id);
  3810. rc = -EINVAL;
  3811. goto fail_cmd;
  3812. }
  3813. /* Wait for the callback */
  3814. rc = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
  3815. atomic_read(&this_adm.copp.stat[port_idx][copp_idx]) >= 0,
  3816. msecs_to_jiffies(TIMEOUT_MS));
  3817. if (!rc) {
  3818. pr_err("%s: module %x enable %d timed out on port = %#x\n",
  3819. __func__, module_id, enable, port_id);
  3820. rc = -EINVAL;
  3821. goto fail_cmd;
  3822. } else if (atomic_read(&this_adm.copp.stat
  3823. [port_idx][copp_idx]) > 0) {
  3824. pr_err("%s: DSP returned error[%s]\n",
  3825. __func__, adsp_err_get_err_str(
  3826. atomic_read(&this_adm.copp.stat
  3827. [port_idx][copp_idx])));
  3828. rc = adsp_err_get_lnx_err_code(
  3829. atomic_read(&this_adm.copp.stat
  3830. [port_idx][copp_idx]));
  3831. goto fail_cmd;
  3832. }
  3833. rc = 0;
  3834. fail_cmd:
  3835. return rc;
  3836. }
  3837. EXPORT_SYMBOL(adm_param_enable);
  3838. /**
  3839. * adm_send_calibration -
  3840. * send ADM calibration to DSP
  3841. *
  3842. * @port_id: Port ID number
  3843. * @copp_idx: copp index assigned
  3844. * @path: direction or ADM path type
  3845. * @perf_mode: performance mode like LL/ULL/..
  3846. * @cal_type: calibration type to use
  3847. * @params: pointer with cal data
  3848. * @size: cal size
  3849. *
  3850. * Returns 0 on success or error on failure
  3851. */
  3852. int adm_send_calibration(int port_id, int copp_idx, int path, int perf_mode,
  3853. int cal_type, char *params, int size)
  3854. {
  3855. struct adm_cmd_set_pp_params_v5 *adm_params = NULL;
  3856. int sz, rc = 0;
  3857. int port_idx;
  3858. pr_debug("%s:port_id %d, path %d, perf_mode %d, cal_type %d, size %d\n",
  3859. __func__, port_id, path, perf_mode, cal_type, size);
  3860. port_id = afe_convert_virtual_to_portid(port_id);
  3861. port_idx = adm_validate_and_get_port_index(port_id);
  3862. if (port_idx < 0) {
  3863. pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
  3864. rc = -EINVAL;
  3865. goto end;
  3866. }
  3867. if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
  3868. pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
  3869. return -EINVAL;
  3870. }
  3871. /* Maps audio_dev_ctrl path definition to ACDB definition */
  3872. if (get_cal_path(path) != RX_DEVICE) {
  3873. pr_err("%s: acdb_path %d\n", __func__, path);
  3874. rc = -EINVAL;
  3875. goto end;
  3876. }
  3877. sz = sizeof(struct adm_cmd_set_pp_params_v5) + size;
  3878. adm_params = kzalloc(sz, GFP_KERNEL);
  3879. if (!adm_params) {
  3880. pr_err("%s, adm params memory alloc failed", __func__);
  3881. rc = -ENOMEM;
  3882. goto end;
  3883. }
  3884. memcpy(((u8 *)adm_params + sizeof(struct adm_cmd_set_pp_params_v5)),
  3885. params, size);
  3886. adm_params->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  3887. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  3888. adm_params->hdr.pkt_size = sz;
  3889. adm_params->hdr.src_svc = APR_SVC_ADM;
  3890. adm_params->hdr.src_domain = APR_DOMAIN_APPS;
  3891. adm_params->hdr.src_port = port_id;
  3892. adm_params->hdr.dest_svc = APR_SVC_ADM;
  3893. adm_params->hdr.dest_domain = APR_DOMAIN_ADSP;
  3894. adm_params->hdr.dest_port =
  3895. atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
  3896. adm_params->hdr.token = port_idx << 16 | copp_idx;
  3897. adm_params->hdr.opcode = ADM_CMD_SET_PP_PARAMS_V5;
  3898. /* payload address and mmap handle initialized to zero by kzalloc */
  3899. adm_params->payload_size = size;
  3900. atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
  3901. rc = apr_send_pkt(this_adm.apr, (uint32_t *)adm_params);
  3902. if (rc < 0) {
  3903. pr_err("%s: Set params failed port = %#x\n",
  3904. __func__, port_id);
  3905. rc = -EINVAL;
  3906. goto end;
  3907. }
  3908. /* Wait for the callback */
  3909. rc = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
  3910. atomic_read(&this_adm.copp.stat[port_idx][copp_idx]) >= 0,
  3911. msecs_to_jiffies(TIMEOUT_MS));
  3912. if (!rc) {
  3913. pr_err("%s: Set params timed out port = %#x\n",
  3914. __func__, port_id);
  3915. rc = -EINVAL;
  3916. goto end;
  3917. } else if (atomic_read(&this_adm.copp.stat
  3918. [port_idx][copp_idx]) > 0) {
  3919. pr_err("%s: DSP returned error[%s]\n",
  3920. __func__, adsp_err_get_err_str(
  3921. atomic_read(&this_adm.copp.stat
  3922. [port_idx][copp_idx])));
  3923. rc = adsp_err_get_lnx_err_code(
  3924. atomic_read(&this_adm.copp.stat
  3925. [port_idx][copp_idx]));
  3926. goto end;
  3927. }
  3928. rc = 0;
  3929. end:
  3930. kfree(adm_params);
  3931. return rc;
  3932. }
  3933. EXPORT_SYMBOL(adm_send_calibration);
  3934. /*
  3935. * adm_update_wait_parameters must be called with routing driver locks.
  3936. * adm_reset_wait_parameters must be called with routing driver locks.
  3937. * set and reset parmeters are separated to make sure it is always called
  3938. * under routing driver lock.
  3939. * adm_wait_timeout is to block until timeout or interrupted. Timeout is
  3940. * not a an error.
  3941. */
  3942. int adm_set_wait_parameters(int port_id, int copp_idx)
  3943. {
  3944. int ret = 0, port_idx;
  3945. pr_debug("%s: port_id 0x%x, copp_idx %d\n", __func__, port_id,
  3946. copp_idx);
  3947. port_id = afe_convert_virtual_to_portid(port_id);
  3948. port_idx = adm_validate_and_get_port_index(port_id);
  3949. if (port_idx < 0) {
  3950. pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
  3951. ret = -EINVAL;
  3952. goto end;
  3953. }
  3954. if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
  3955. pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
  3956. return -EINVAL;
  3957. }
  3958. this_adm.copp.adm_delay[port_idx][copp_idx] = 1;
  3959. atomic_set(&this_adm.copp.adm_delay_stat[port_idx][copp_idx], 0);
  3960. end:
  3961. return ret;
  3962. }
  3963. EXPORT_SYMBOL(adm_set_wait_parameters);
  3964. /**
  3965. * adm_reset_wait_parameters -
  3966. * reset wait parameters or ADM delay value
  3967. *
  3968. * @port_id: Port ID number
  3969. * @copp_idx: copp index assigned
  3970. *
  3971. * Returns 0 on success or error on failure
  3972. */
  3973. int adm_reset_wait_parameters(int port_id, int copp_idx)
  3974. {
  3975. int ret = 0, port_idx;
  3976. pr_debug("%s: port_id 0x%x copp_idx %d\n", __func__, port_id,
  3977. copp_idx);
  3978. port_id = afe_convert_virtual_to_portid(port_id);
  3979. port_idx = adm_validate_and_get_port_index(port_id);
  3980. if (port_idx < 0) {
  3981. pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
  3982. ret = -EINVAL;
  3983. goto end;
  3984. }
  3985. if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
  3986. pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
  3987. return -EINVAL;
  3988. }
  3989. atomic_set(&this_adm.copp.adm_delay_stat[port_idx][copp_idx], 1);
  3990. this_adm.copp.adm_delay[port_idx][copp_idx] = 0;
  3991. end:
  3992. return ret;
  3993. }
  3994. EXPORT_SYMBOL(adm_reset_wait_parameters);
  3995. /**
  3996. * adm_wait_timeout -
  3997. * ADM wait command after command send to DSP
  3998. *
  3999. * @port_id: Port ID number
  4000. * @copp_idx: copp index assigned
  4001. * @wait_time: value in ms for command timeout
  4002. *
  4003. * Returns 0 on success or error on failure
  4004. */
  4005. int adm_wait_timeout(int port_id, int copp_idx, int wait_time)
  4006. {
  4007. int ret = 0, port_idx;
  4008. pr_debug("%s: port_id 0x%x, copp_idx %d, wait_time %d\n", __func__,
  4009. port_id, copp_idx, wait_time);
  4010. port_id = afe_convert_virtual_to_portid(port_id);
  4011. port_idx = adm_validate_and_get_port_index(port_id);
  4012. if (port_idx < 0) {
  4013. pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
  4014. ret = -EINVAL;
  4015. goto end;
  4016. }
  4017. if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
  4018. pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
  4019. return -EINVAL;
  4020. }
  4021. ret = wait_event_timeout(
  4022. this_adm.copp.adm_delay_wait[port_idx][copp_idx],
  4023. atomic_read(&this_adm.copp.adm_delay_stat[port_idx][copp_idx]),
  4024. msecs_to_jiffies(wait_time));
  4025. pr_debug("%s: return %d\n", __func__, ret);
  4026. if (ret != 0)
  4027. ret = -EINTR;
  4028. end:
  4029. pr_debug("%s: return %d--\n", __func__, ret);
  4030. return ret;
  4031. }
  4032. EXPORT_SYMBOL(adm_wait_timeout);
  4033. /**
  4034. * adm_store_cal_data -
  4035. * Retrieve calibration data for ADM copp device
  4036. *
  4037. * @port_id: Port ID number
  4038. * @copp_idx: copp index assigned
  4039. * @path: direction or copp type
  4040. * @perf_mode: performance mode like LL/ULL/..
  4041. * @cal_index: calibration index to use
  4042. * @params: pointer to store cal data
  4043. * @size: pointer to fill with cal size
  4044. *
  4045. * Returns 0 on success or error on failure
  4046. */
  4047. int adm_store_cal_data(int port_id, int copp_idx, int path, int perf_mode,
  4048. int cal_index, char *params, int *size)
  4049. {
  4050. int rc = 0;
  4051. struct cal_block_data *cal_block = NULL;
  4052. int app_type, acdb_id, port_idx, sample_rate;
  4053. if (this_adm.cal_data[cal_index] == NULL) {
  4054. pr_debug("%s: cal_index %d not allocated!\n",
  4055. __func__, cal_index);
  4056. goto end;
  4057. }
  4058. if (get_cal_path(path) != RX_DEVICE) {
  4059. pr_debug("%s: Invalid path to store calibration %d\n",
  4060. __func__, path);
  4061. rc = -EINVAL;
  4062. goto end;
  4063. }
  4064. port_id = afe_convert_virtual_to_portid(port_id);
  4065. port_idx = adm_validate_and_get_port_index(port_id);
  4066. if (port_idx < 0) {
  4067. pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
  4068. rc = -EINVAL;
  4069. goto end;
  4070. }
  4071. if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
  4072. pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
  4073. return -EINVAL;
  4074. }
  4075. acdb_id = atomic_read(&this_adm.copp.acdb_id[port_idx][copp_idx]);
  4076. app_type = atomic_read(&this_adm.copp.app_type[port_idx][copp_idx]);
  4077. sample_rate = atomic_read(&this_adm.copp.rate[port_idx][copp_idx]);
  4078. mutex_lock(&this_adm.cal_data[cal_index]->lock);
  4079. cal_block = adm_find_cal(cal_index, get_cal_path(path), app_type,
  4080. acdb_id, sample_rate);
  4081. if (cal_block == NULL)
  4082. goto unlock;
  4083. if (cal_block->cal_data.size <= 0) {
  4084. pr_debug("%s: No ADM cal send for port_id = 0x%x!\n",
  4085. __func__, port_id);
  4086. rc = -EINVAL;
  4087. goto unlock;
  4088. }
  4089. if (cal_index == ADM_AUDPROC_CAL || cal_index == ADM_LSM_AUDPROC_CAL) {
  4090. if (cal_block->cal_data.size > AUD_PROC_BLOCK_SIZE) {
  4091. pr_err("%s:audproc:invalid size exp/actual[%zd, %d]\n",
  4092. __func__, cal_block->cal_data.size, *size);
  4093. rc = -ENOMEM;
  4094. goto unlock;
  4095. }
  4096. } else if (cal_index == ADM_AUDVOL_CAL) {
  4097. if (cal_block->cal_data.size > AUD_VOL_BLOCK_SIZE) {
  4098. pr_err("%s:aud_vol:invalid size exp/actual[%zd, %d]\n",
  4099. __func__, cal_block->cal_data.size, *size);
  4100. rc = -ENOMEM;
  4101. goto unlock;
  4102. }
  4103. } else {
  4104. pr_debug("%s: Not valid calibration for dolby topolgy\n",
  4105. __func__);
  4106. rc = -EINVAL;
  4107. goto unlock;
  4108. }
  4109. memcpy(params, cal_block->cal_data.kvaddr, cal_block->cal_data.size);
  4110. *size = cal_block->cal_data.size;
  4111. pr_debug("%s:port_id %d, copp_idx %d, path %d",
  4112. __func__, port_id, copp_idx, path);
  4113. pr_debug("perf_mode %d, cal_type %d, size %d\n",
  4114. perf_mode, cal_index, *size);
  4115. unlock:
  4116. mutex_unlock(&this_adm.cal_data[cal_index]->lock);
  4117. end:
  4118. return rc;
  4119. }
  4120. EXPORT_SYMBOL(adm_store_cal_data);
  4121. /**
  4122. * adm_send_compressed_device_mute -
  4123. * command to send mute for compressed device
  4124. *
  4125. * @port_id: Port ID number
  4126. * @copp_idx: copp index assigned
  4127. * @mute_on: flag to indicate mute or unmute
  4128. *
  4129. * Returns 0 on success or error on failure
  4130. */
  4131. int adm_send_compressed_device_mute(int port_id, int copp_idx, bool mute_on)
  4132. {
  4133. struct adm_set_compressed_device_mute mute_params;
  4134. int ret = 0;
  4135. int port_idx;
  4136. pr_debug("%s port_id: 0x%x, copp_idx %d, mute_on: %d\n",
  4137. __func__, port_id, copp_idx, mute_on);
  4138. port_id = afe_convert_virtual_to_portid(port_id);
  4139. port_idx = adm_validate_and_get_port_index(port_id);
  4140. if (port_idx < 0 || port_idx >= AFE_MAX_PORTS) {
  4141. pr_err("%s: Invalid port_id %#x copp_idx %d\n",
  4142. __func__, port_id, copp_idx);
  4143. ret = -EINVAL;
  4144. goto end;
  4145. }
  4146. mute_params.command.hdr.hdr_field =
  4147. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  4148. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  4149. mute_params.command.hdr.pkt_size =
  4150. sizeof(struct adm_set_compressed_device_mute);
  4151. mute_params.command.hdr.src_svc = APR_SVC_ADM;
  4152. mute_params.command.hdr.src_domain = APR_DOMAIN_APPS;
  4153. mute_params.command.hdr.src_port = port_id;
  4154. mute_params.command.hdr.dest_svc = APR_SVC_ADM;
  4155. mute_params.command.hdr.dest_domain = APR_DOMAIN_ADSP;
  4156. mute_params.command.hdr.dest_port =
  4157. atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
  4158. mute_params.command.hdr.token = port_idx << 16 | copp_idx;
  4159. mute_params.command.hdr.opcode = ADM_CMD_SET_PP_PARAMS_V5;
  4160. mute_params.command.payload_addr_lsw = 0;
  4161. mute_params.command.payload_addr_msw = 0;
  4162. mute_params.command.mem_map_handle = 0;
  4163. mute_params.command.payload_size = sizeof(mute_params) -
  4164. sizeof(mute_params.command);
  4165. mute_params.params.module_id = AUDPROC_MODULE_ID_COMPRESSED_MUTE;
  4166. mute_params.params.param_id = AUDPROC_PARAM_ID_COMPRESSED_MUTE;
  4167. mute_params.params.param_size = mute_params.command.payload_size -
  4168. sizeof(mute_params.params);
  4169. mute_params.params.reserved = 0;
  4170. mute_params.mute_on = mute_on;
  4171. atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
  4172. ret = apr_send_pkt(this_adm.apr, (uint32_t *)&mute_params);
  4173. if (ret < 0) {
  4174. pr_err("%s: device mute for port %d copp %d failed, ret %d\n",
  4175. __func__, port_id, copp_idx, ret);
  4176. ret = -EINVAL;
  4177. goto end;
  4178. }
  4179. /* Wait for the callback */
  4180. ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
  4181. atomic_read(&this_adm.copp.stat[port_idx][copp_idx]) >= 0,
  4182. msecs_to_jiffies(TIMEOUT_MS));
  4183. if (!ret) {
  4184. pr_err("%s: send device mute for port %d copp %d failed\n",
  4185. __func__, port_id, copp_idx);
  4186. ret = -EINVAL;
  4187. goto end;
  4188. } else if (atomic_read(&this_adm.copp.stat
  4189. [port_idx][copp_idx]) > 0) {
  4190. pr_err("%s: DSP returned error[%s]\n",
  4191. __func__, adsp_err_get_err_str(
  4192. atomic_read(&this_adm.copp.stat
  4193. [port_idx][copp_idx])));
  4194. ret = adsp_err_get_lnx_err_code(
  4195. atomic_read(&this_adm.copp.stat
  4196. [port_idx][copp_idx]));
  4197. goto end;
  4198. }
  4199. ret = 0;
  4200. end:
  4201. return ret;
  4202. }
  4203. EXPORT_SYMBOL(adm_send_compressed_device_mute);
  4204. /**
  4205. * adm_send_compressed_device_latency -
  4206. * command to send latency for compressed device
  4207. *
  4208. * @port_id: Port ID number
  4209. * @copp_idx: copp index assigned
  4210. * @latency: latency value to pass
  4211. *
  4212. * Returns 0 on success or error on failure
  4213. */
  4214. int adm_send_compressed_device_latency(int port_id, int copp_idx, int latency)
  4215. {
  4216. struct adm_set_compressed_device_latency latency_params;
  4217. int port_idx;
  4218. int ret = 0;
  4219. pr_debug("%s port_id: 0x%x, copp_idx %d latency: %d\n", __func__,
  4220. port_id, copp_idx, latency);
  4221. port_id = afe_convert_virtual_to_portid(port_id);
  4222. port_idx = adm_validate_and_get_port_index(port_id);
  4223. if (port_idx < 0 || port_idx >= AFE_MAX_PORTS) {
  4224. pr_err("%s: Invalid port_id %#x copp_idx %d\n",
  4225. __func__, port_id, copp_idx);
  4226. ret = -EINVAL;
  4227. goto end;
  4228. }
  4229. latency_params.command.hdr.hdr_field =
  4230. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  4231. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  4232. latency_params.command.hdr.pkt_size =
  4233. sizeof(struct adm_set_compressed_device_latency);
  4234. latency_params.command.hdr.src_svc = APR_SVC_ADM;
  4235. latency_params.command.hdr.src_domain = APR_DOMAIN_APPS;
  4236. latency_params.command.hdr.src_port = port_id;
  4237. latency_params.command.hdr.dest_svc = APR_SVC_ADM;
  4238. latency_params.command.hdr.dest_domain = APR_DOMAIN_ADSP;
  4239. latency_params.command.hdr.dest_port =
  4240. atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
  4241. latency_params.command.hdr.token = port_idx << 16 | copp_idx;
  4242. latency_params.command.hdr.opcode = ADM_CMD_SET_PP_PARAMS_V5;
  4243. latency_params.command.payload_addr_lsw = 0;
  4244. latency_params.command.payload_addr_msw = 0;
  4245. latency_params.command.mem_map_handle = 0;
  4246. latency_params.command.payload_size = sizeof(latency_params) -
  4247. sizeof(latency_params.command);
  4248. latency_params.params.module_id = AUDPROC_MODULE_ID_COMPRESSED_LATENCY;
  4249. latency_params.params.param_id = AUDPROC_PARAM_ID_COMPRESSED_LATENCY;
  4250. latency_params.params.param_size = latency_params.command.payload_size -
  4251. sizeof(latency_params.params);
  4252. latency_params.params.reserved = 0;
  4253. latency_params.latency = latency;
  4254. atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
  4255. ret = apr_send_pkt(this_adm.apr, (uint32_t *)&latency_params);
  4256. if (ret < 0) {
  4257. pr_err("%s: send device latency err %d for port %d copp %d\n",
  4258. __func__, port_id, copp_idx, ret);
  4259. ret = -EINVAL;
  4260. goto end;
  4261. }
  4262. /* Wait for the callback */
  4263. ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
  4264. atomic_read(&this_adm.copp.stat[port_idx][copp_idx]) >= 0,
  4265. msecs_to_jiffies(TIMEOUT_MS));
  4266. if (!ret) {
  4267. pr_err("%s: send device latency for port %d failed\n", __func__,
  4268. port_id);
  4269. ret = -EINVAL;
  4270. goto end;
  4271. } else if (atomic_read(&this_adm.copp.stat
  4272. [port_idx][copp_idx]) > 0) {
  4273. pr_err("%s: DSP returned error[%s]\n",
  4274. __func__, adsp_err_get_err_str(
  4275. atomic_read(&this_adm.copp.stat
  4276. [port_idx][copp_idx])));
  4277. ret = adsp_err_get_lnx_err_code(
  4278. atomic_read(&this_adm.copp.stat
  4279. [port_idx][copp_idx]));
  4280. goto end;
  4281. }
  4282. ret = 0;
  4283. end:
  4284. return ret;
  4285. }
  4286. EXPORT_SYMBOL(adm_send_compressed_device_latency);
  4287. /**
  4288. * adm_swap_speaker_channels
  4289. *
  4290. * Receives port_id, copp_idx, sample rate, spk_swap and
  4291. * send MFC command to swap speaker channel.
  4292. * Return zero on success. On failure returns nonzero.
  4293. *
  4294. * port_id - Passed value, port_id for which channels swap is wanted
  4295. * copp_idx - Passed value, copp_idx for which channels swap is wanted
  4296. * sample_rate - Passed value, sample rate used by app type config
  4297. * spk_swap - Passed value, spk_swap for check if swap flag is set
  4298. */
  4299. int adm_swap_speaker_channels(int port_id, int copp_idx,
  4300. int sample_rate, bool spk_swap)
  4301. {
  4302. struct audproc_mfc_output_media_fmt mfc_cfg;
  4303. uint16_t num_channels;
  4304. int port_idx;
  4305. int ret = 0;
  4306. pr_debug("%s: Enter, port_id %d, copp_idx %d\n",
  4307. __func__, port_id, copp_idx);
  4308. port_id = q6audio_convert_virtual_to_portid(port_id);
  4309. port_idx = adm_validate_and_get_port_index(port_id);
  4310. if (port_idx < 0 || port_idx >= AFE_MAX_PORTS) {
  4311. pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
  4312. ret = -EINVAL;
  4313. goto done;
  4314. }
  4315. if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
  4316. pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
  4317. ret = -EINVAL;
  4318. goto done;
  4319. }
  4320. num_channels = atomic_read(
  4321. &this_adm.copp.channels[port_idx][copp_idx]);
  4322. if (num_channels != 2) {
  4323. pr_debug("%s: Invalid number of channels: %d\n",
  4324. __func__, num_channels);
  4325. ret = -EINVAL;
  4326. goto done;
  4327. }
  4328. memset(&mfc_cfg, 0, sizeof(mfc_cfg));
  4329. mfc_cfg.params.hdr.hdr_field =
  4330. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  4331. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  4332. mfc_cfg.params.hdr.pkt_size =
  4333. sizeof(mfc_cfg);
  4334. mfc_cfg.params.hdr.src_svc = APR_SVC_ADM;
  4335. mfc_cfg.params.hdr.src_domain = APR_DOMAIN_APPS;
  4336. mfc_cfg.params.hdr.src_port = port_id;
  4337. mfc_cfg.params.hdr.dest_svc = APR_SVC_ADM;
  4338. mfc_cfg.params.hdr.dest_domain = APR_DOMAIN_ADSP;
  4339. mfc_cfg.params.hdr.dest_port =
  4340. atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
  4341. mfc_cfg.params.hdr.token = port_idx << 16 | copp_idx;
  4342. mfc_cfg.params.hdr.opcode = ADM_CMD_SET_PP_PARAMS_V5;
  4343. mfc_cfg.params.payload_addr_lsw = 0;
  4344. mfc_cfg.params.payload_addr_msw = 0;
  4345. mfc_cfg.params.mem_map_handle = 0;
  4346. mfc_cfg.params.payload_size = sizeof(mfc_cfg) -
  4347. sizeof(mfc_cfg.params);
  4348. mfc_cfg.data.module_id = AUDPROC_MODULE_ID_MFC;
  4349. mfc_cfg.data.param_id = AUDPROC_PARAM_ID_MFC_OUTPUT_MEDIA_FORMAT;
  4350. mfc_cfg.data.param_size = mfc_cfg.params.payload_size -
  4351. sizeof(mfc_cfg.data);
  4352. mfc_cfg.data.reserved = 0;
  4353. mfc_cfg.sampling_rate = sample_rate;
  4354. mfc_cfg.bits_per_sample =
  4355. atomic_read(&this_adm.copp.bit_width[port_idx][copp_idx]);
  4356. mfc_cfg.num_channels = num_channels;
  4357. /* Currently applying speaker swap for only 2 channel use case */
  4358. if (spk_swap) {
  4359. mfc_cfg.channel_type[0] =
  4360. (uint16_t) PCM_CHANNEL_FR;
  4361. mfc_cfg.channel_type[1] =
  4362. (uint16_t) PCM_CHANNEL_FL;
  4363. } else {
  4364. mfc_cfg.channel_type[0] =
  4365. (uint16_t) PCM_CHANNEL_FL;
  4366. mfc_cfg.channel_type[1] =
  4367. (uint16_t) PCM_CHANNEL_FR;
  4368. }
  4369. atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
  4370. pr_debug("%s: mfc config: port_idx %d copp_idx %d copp SR %d copp BW %d copp chan %d\n",
  4371. __func__, port_idx, copp_idx, mfc_cfg.sampling_rate,
  4372. mfc_cfg.bits_per_sample, mfc_cfg.num_channels);
  4373. ret = apr_send_pkt(this_adm.apr, (uint32_t *)&mfc_cfg);
  4374. if (ret < 0) {
  4375. pr_err("%s: port_id: for[0x%x] failed %d\n",
  4376. __func__, port_id, ret);
  4377. goto done;
  4378. }
  4379. /* Wait for the callback with copp id */
  4380. ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
  4381. atomic_read(&this_adm.copp.stat
  4382. [port_idx][copp_idx]) >= 0,
  4383. msecs_to_jiffies(TIMEOUT_MS));
  4384. if (!ret) {
  4385. pr_err("%s: mfc_cfg Set params timed out for port_id: for [0x%x]\n",
  4386. __func__, port_id);
  4387. ret = -ETIMEDOUT;
  4388. goto done;
  4389. }
  4390. if (atomic_read(&this_adm.copp.stat[port_idx][copp_idx]) > 0) {
  4391. pr_err("%s: DSP returned error[%s]\n",
  4392. __func__, adsp_err_get_err_str(
  4393. atomic_read(&this_adm.copp.stat
  4394. [port_idx][copp_idx])));
  4395. ret = adsp_err_get_lnx_err_code(
  4396. atomic_read(&this_adm.copp.stat
  4397. [port_idx][copp_idx]));
  4398. goto done;
  4399. }
  4400. pr_debug("%s: mfc_cfg Set params returned success", __func__);
  4401. ret = 0;
  4402. done:
  4403. return ret;
  4404. }
  4405. EXPORT_SYMBOL(adm_swap_speaker_channels);
  4406. /**
  4407. * adm_set_sound_focus -
  4408. * Update sound focus info
  4409. *
  4410. * @port_id: Port ID number
  4411. * @copp_idx: copp index assigned
  4412. * @soundFocusData: sound focus data to pass
  4413. *
  4414. * Returns 0 on success or error on failure
  4415. */
  4416. int adm_set_sound_focus(int port_id, int copp_idx,
  4417. struct sound_focus_param soundFocusData)
  4418. {
  4419. struct adm_set_fluence_soundfocus_param soundfocus_params;
  4420. int sz = 0;
  4421. int ret = 0;
  4422. int port_idx;
  4423. int i;
  4424. pr_debug("%s: Enter, port_id %d, copp_idx %d\n",
  4425. __func__, port_id, copp_idx);
  4426. port_id = afe_convert_virtual_to_portid(port_id);
  4427. port_idx = adm_validate_and_get_port_index(port_id);
  4428. if (port_idx < 0) {
  4429. pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
  4430. ret = -EINVAL;
  4431. goto done;
  4432. }
  4433. if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
  4434. pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
  4435. ret = -EINVAL;
  4436. goto done;
  4437. }
  4438. sz = sizeof(struct adm_set_fluence_soundfocus_param);
  4439. soundfocus_params.params.hdr.hdr_field =
  4440. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD, APR_HDR_LEN(APR_HDR_SIZE),
  4441. APR_PKT_VER);
  4442. soundfocus_params.params.hdr.pkt_size = sz;
  4443. soundfocus_params.params.hdr.src_svc = APR_SVC_ADM;
  4444. soundfocus_params.params.hdr.src_domain = APR_DOMAIN_APPS;
  4445. soundfocus_params.params.hdr.src_port = port_id;
  4446. soundfocus_params.params.hdr.dest_svc = APR_SVC_ADM;
  4447. soundfocus_params.params.hdr.dest_domain = APR_DOMAIN_ADSP;
  4448. soundfocus_params.params.hdr.dest_port =
  4449. atomic_read(&this_adm.copp.id[port_idx][copp_idx]);
  4450. soundfocus_params.params.hdr.token = port_idx << 16 |
  4451. ADM_CLIENT_ID_SOURCE_TRACKING << 8 | copp_idx;
  4452. soundfocus_params.params.hdr.opcode = ADM_CMD_SET_PP_PARAMS_V5;
  4453. soundfocus_params.params.payload_addr_lsw = 0;
  4454. soundfocus_params.params.payload_addr_msw = 0;
  4455. soundfocus_params.params.mem_map_handle = 0;
  4456. soundfocus_params.params.payload_size = sizeof(soundfocus_params) -
  4457. sizeof(soundfocus_params.params);
  4458. soundfocus_params.data.module_id = VOICEPROC_MODULE_ID_GENERIC_TX;
  4459. soundfocus_params.data.param_id = VOICEPROC_PARAM_ID_FLUENCE_SOUNDFOCUS;
  4460. soundfocus_params.data.param_size =
  4461. soundfocus_params.params.payload_size -
  4462. sizeof(soundfocus_params.data);
  4463. soundfocus_params.data.reserved = 0;
  4464. memset(&(soundfocus_params.soundfocus_data), 0xFF,
  4465. sizeof(struct adm_param_fluence_soundfocus_t));
  4466. for (i = 0; i < MAX_SECTORS; i++) {
  4467. soundfocus_params.soundfocus_data.start_angles[i] =
  4468. soundFocusData.start_angle[i];
  4469. soundfocus_params.soundfocus_data.enables[i] =
  4470. soundFocusData.enable[i];
  4471. pr_debug("%s: start_angle[%d] = %d\n",
  4472. __func__, i, soundFocusData.start_angle[i]);
  4473. pr_debug("%s: enable[%d] = %d\n",
  4474. __func__, i, soundFocusData.enable[i]);
  4475. }
  4476. soundfocus_params.soundfocus_data.gain_step =
  4477. soundFocusData.gain_step;
  4478. pr_debug("%s: gain_step = %d\n", __func__, soundFocusData.gain_step);
  4479. soundfocus_params.soundfocus_data.reserved = 0;
  4480. atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
  4481. ret = apr_send_pkt(this_adm.apr, (uint32_t *)&soundfocus_params);
  4482. if (ret < 0) {
  4483. pr_err("%s: Set params failed\n", __func__);
  4484. ret = -EINVAL;
  4485. goto done;
  4486. }
  4487. /* Wait for the callback */
  4488. ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
  4489. atomic_read(&this_adm.copp.stat[port_idx][copp_idx]) >= 0,
  4490. msecs_to_jiffies(TIMEOUT_MS));
  4491. if (!ret) {
  4492. pr_err("%s: Set params timed out\n", __func__);
  4493. ret = -EINVAL;
  4494. goto done;
  4495. }
  4496. if (this_adm.sourceTrackingData.apr_cmd_status != 0) {
  4497. pr_err("%s - set params returned error [%s]\n",
  4498. __func__, adsp_err_get_err_str(
  4499. this_adm.sourceTrackingData.apr_cmd_status));
  4500. ret = adsp_err_get_lnx_err_code(
  4501. this_adm.sourceTrackingData.apr_cmd_status);
  4502. goto done;
  4503. }
  4504. ret = 0;
  4505. done:
  4506. pr_debug("%s: Exit, ret=%d\n", __func__, ret);
  4507. return ret;
  4508. }
  4509. EXPORT_SYMBOL(adm_set_sound_focus);
  4510. /**
  4511. * adm_get_sound_focus -
  4512. * Retrieve sound focus info
  4513. *
  4514. * @port_id: Port ID number
  4515. * @copp_idx: copp index assigned
  4516. * @soundFocusData: pointer for sound focus data to be updated with
  4517. *
  4518. * Returns 0 on success or error on failure
  4519. */
  4520. int adm_get_sound_focus(int port_id, int copp_idx,
  4521. struct sound_focus_param *soundFocusData)
  4522. {
  4523. int ret = 0, i;
  4524. char *params_value;
  4525. uint32_t param_payload_len = sizeof(struct adm_param_data_v5) +
  4526. sizeof(struct adm_param_fluence_soundfocus_t);
  4527. struct adm_param_fluence_soundfocus_t *soundfocus_params;
  4528. pr_debug("%s: Enter, port_id %d, copp_idx %d\n",
  4529. __func__, port_id, copp_idx);
  4530. params_value = kzalloc(param_payload_len, GFP_KERNEL);
  4531. if (!params_value) {
  4532. ret = -ENOMEM;
  4533. goto done;
  4534. }
  4535. ret = adm_get_params_v2(port_id, copp_idx,
  4536. VOICEPROC_MODULE_ID_GENERIC_TX,
  4537. VOICEPROC_PARAM_ID_FLUENCE_SOUNDFOCUS,
  4538. param_payload_len,
  4539. params_value,
  4540. ADM_CLIENT_ID_SOURCE_TRACKING);
  4541. if (ret) {
  4542. pr_err("%s: get parameters failed ret:%d\n", __func__, ret);
  4543. kfree(params_value);
  4544. ret = -EINVAL;
  4545. goto done;
  4546. }
  4547. if (this_adm.sourceTrackingData.apr_cmd_status != 0) {
  4548. pr_err("%s - get params returned error [%s]\n",
  4549. __func__, adsp_err_get_err_str(
  4550. this_adm.sourceTrackingData.apr_cmd_status));
  4551. kfree(params_value);
  4552. ret = adsp_err_get_lnx_err_code(
  4553. this_adm.sourceTrackingData.apr_cmd_status);
  4554. goto done;
  4555. }
  4556. soundfocus_params = (struct adm_param_fluence_soundfocus_t *)
  4557. params_value;
  4558. for (i = 0; i < MAX_SECTORS; i++) {
  4559. soundFocusData->start_angle[i] =
  4560. soundfocus_params->start_angles[i];
  4561. soundFocusData->enable[i] = soundfocus_params->enables[i];
  4562. pr_debug("%s: start_angle[%d] = %d\n",
  4563. __func__, i, soundFocusData->start_angle[i]);
  4564. pr_debug("%s: enable[%d] = %d\n",
  4565. __func__, i, soundFocusData->enable[i]);
  4566. }
  4567. soundFocusData->gain_step = soundfocus_params->gain_step;
  4568. pr_debug("%s: gain_step = %d\n", __func__, soundFocusData->gain_step);
  4569. kfree(params_value);
  4570. done:
  4571. pr_debug("%s: Exit, ret = %d\n", __func__, ret);
  4572. return ret;
  4573. }
  4574. EXPORT_SYMBOL(adm_get_sound_focus);
  4575. static int adm_source_tracking_alloc_map_memory(void)
  4576. {
  4577. int ret;
  4578. pr_debug("%s: Enter\n", __func__);
  4579. ret = msm_audio_ion_alloc("SOURCE_TRACKING",
  4580. &this_adm.sourceTrackingData.ion_client,
  4581. &this_adm.sourceTrackingData.ion_handle,
  4582. AUD_PROC_BLOCK_SIZE,
  4583. &this_adm.sourceTrackingData.memmap.paddr,
  4584. &this_adm.sourceTrackingData.memmap.size,
  4585. &this_adm.sourceTrackingData.memmap.kvaddr);
  4586. if (ret) {
  4587. pr_err("%s: failed to allocate memory\n", __func__);
  4588. ret = -EINVAL;
  4589. goto done;
  4590. }
  4591. atomic_set(&this_adm.mem_map_index, ADM_MEM_MAP_INDEX_SOURCE_TRACKING);
  4592. ret = adm_memory_map_regions(&this_adm.sourceTrackingData.memmap.paddr,
  4593. 0,
  4594. (uint32_t *)&this_adm.sourceTrackingData.memmap.size,
  4595. 1);
  4596. if (ret < 0) {
  4597. pr_err("%s: failed to map memory, paddr = 0x%pK, size = %d\n",
  4598. __func__,
  4599. (void *)this_adm.sourceTrackingData.memmap.paddr,
  4600. (uint32_t)this_adm.sourceTrackingData.memmap.size);
  4601. msm_audio_ion_free(this_adm.sourceTrackingData.ion_client,
  4602. this_adm.sourceTrackingData.ion_handle);
  4603. this_adm.sourceTrackingData.ion_client = NULL;
  4604. this_adm.sourceTrackingData.ion_handle = NULL;
  4605. this_adm.sourceTrackingData.memmap.size = 0;
  4606. this_adm.sourceTrackingData.memmap.kvaddr = NULL;
  4607. this_adm.sourceTrackingData.memmap.paddr = 0;
  4608. this_adm.sourceTrackingData.apr_cmd_status = -1;
  4609. atomic_set(&this_adm.mem_map_handles
  4610. [ADM_MEM_MAP_INDEX_SOURCE_TRACKING], 0);
  4611. ret = -EINVAL;
  4612. goto done;
  4613. }
  4614. ret = 0;
  4615. pr_debug("%s: paddr = 0x%pK, size = %d, mem_map_handle = 0x%x\n",
  4616. __func__, (void *)this_adm.sourceTrackingData.memmap.paddr,
  4617. (uint32_t)this_adm.sourceTrackingData.memmap.size,
  4618. atomic_read(&this_adm.mem_map_handles
  4619. [ADM_MEM_MAP_INDEX_SOURCE_TRACKING]));
  4620. done:
  4621. pr_debug("%s: Exit, ret = %d\n", __func__, ret);
  4622. return ret;
  4623. }
  4624. /**
  4625. * adm_get_source_tracking -
  4626. * Retrieve source tracking info
  4627. *
  4628. * @port_id: Port ID number
  4629. * @copp_idx: copp index assigned
  4630. * @sourceTrackingData: pointer for source track data to be updated with
  4631. *
  4632. * Returns 0 on success or error on failure
  4633. */
  4634. int adm_get_source_tracking(int port_id, int copp_idx,
  4635. struct source_tracking_param *sourceTrackingData)
  4636. {
  4637. struct adm_cmd_get_pp_params_v5 admp;
  4638. int p_idx, ret = 0, i;
  4639. struct adm_param_fluence_sourcetracking_t *source_tracking_params;
  4640. pr_debug("%s: Enter, port_id %d, copp_idx %d\n",
  4641. __func__, port_id, copp_idx);
  4642. if (!this_adm.sourceTrackingData.memmap.paddr) {
  4643. /* Allocate and map shared memory for out of band usage */
  4644. ret = adm_source_tracking_alloc_map_memory();
  4645. if (ret != 0) {
  4646. ret = -EINVAL;
  4647. goto done;
  4648. }
  4649. }
  4650. port_id = afe_convert_virtual_to_portid(port_id);
  4651. p_idx = adm_validate_and_get_port_index(port_id);
  4652. if (p_idx < 0) {
  4653. pr_err("%s - invalid port index %i, port id %i, copp idx %i\n",
  4654. __func__, p_idx, port_id, copp_idx);
  4655. ret = -EINVAL;
  4656. goto done;
  4657. }
  4658. admp.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  4659. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  4660. admp.hdr.pkt_size = sizeof(admp);
  4661. admp.hdr.src_svc = APR_SVC_ADM;
  4662. admp.hdr.src_domain = APR_DOMAIN_APPS;
  4663. admp.hdr.src_port = port_id;
  4664. admp.hdr.dest_svc = APR_SVC_ADM;
  4665. admp.hdr.dest_domain = APR_DOMAIN_ADSP;
  4666. admp.hdr.dest_port = atomic_read(&this_adm.copp.id[p_idx][copp_idx]);
  4667. admp.hdr.token = p_idx << 16 | ADM_CLIENT_ID_SOURCE_TRACKING << 8 |
  4668. copp_idx;
  4669. admp.hdr.opcode = ADM_CMD_GET_PP_PARAMS_V5;
  4670. admp.data_payload_addr_lsw =
  4671. lower_32_bits(this_adm.sourceTrackingData.memmap.paddr);
  4672. admp.data_payload_addr_msw =
  4673. msm_audio_populate_upper_32_bits(
  4674. this_adm.sourceTrackingData.memmap.paddr);
  4675. admp.mem_map_handle = atomic_read(&this_adm.mem_map_handles[
  4676. ADM_MEM_MAP_INDEX_SOURCE_TRACKING]);
  4677. admp.module_id = VOICEPROC_MODULE_ID_GENERIC_TX;
  4678. admp.param_id = VOICEPROC_PARAM_ID_FLUENCE_SOURCETRACKING;
  4679. admp.param_max_size = sizeof(struct adm_param_fluence_sourcetracking_t)
  4680. + sizeof(struct adm_param_data_v5);
  4681. admp.reserved = 0;
  4682. atomic_set(&this_adm.copp.stat[p_idx][copp_idx], -1);
  4683. ret = apr_send_pkt(this_adm.apr, (uint32_t *)&admp);
  4684. if (ret < 0) {
  4685. pr_err("%s - failed to get Source Tracking Params\n",
  4686. __func__);
  4687. ret = -EINVAL;
  4688. goto done;
  4689. }
  4690. ret = wait_event_timeout(this_adm.copp.wait[p_idx][copp_idx],
  4691. atomic_read(&this_adm.copp.stat[p_idx][copp_idx]) >= 0,
  4692. msecs_to_jiffies(TIMEOUT_MS));
  4693. if (!ret) {
  4694. pr_err("%s - get params timed out\n", __func__);
  4695. ret = -EINVAL;
  4696. goto done;
  4697. } else if (atomic_read(&this_adm.copp.stat
  4698. [p_idx][copp_idx]) > 0) {
  4699. pr_err("%s: DSP returned error[%s]\n",
  4700. __func__, adsp_err_get_err_str(
  4701. atomic_read(&this_adm.copp.stat
  4702. [p_idx][copp_idx])));
  4703. ret = adsp_err_get_lnx_err_code(
  4704. atomic_read(&this_adm.copp.stat
  4705. [p_idx][copp_idx]));
  4706. goto done;
  4707. }
  4708. if (this_adm.sourceTrackingData.apr_cmd_status != 0) {
  4709. pr_err("%s - get params returned error [%s]\n",
  4710. __func__, adsp_err_get_err_str(
  4711. this_adm.sourceTrackingData.apr_cmd_status));
  4712. ret = adsp_err_get_lnx_err_code(
  4713. this_adm.sourceTrackingData.apr_cmd_status);
  4714. goto done;
  4715. }
  4716. source_tracking_params = (struct adm_param_fluence_sourcetracking_t *)
  4717. (this_adm.sourceTrackingData.memmap.kvaddr +
  4718. sizeof(struct adm_param_data_v5));
  4719. for (i = 0; i < MAX_SECTORS; i++) {
  4720. sourceTrackingData->vad[i] = source_tracking_params->vad[i];
  4721. pr_debug("%s: vad[%d] = %d\n",
  4722. __func__, i, sourceTrackingData->vad[i]);
  4723. }
  4724. sourceTrackingData->doa_speech = source_tracking_params->doa_speech;
  4725. pr_debug("%s: doa_speech = %d\n",
  4726. __func__, sourceTrackingData->doa_speech);
  4727. for (i = 0; i < MAX_NOISE_SOURCE_INDICATORS; i++) {
  4728. sourceTrackingData->doa_noise[i] =
  4729. source_tracking_params->doa_noise[i];
  4730. pr_debug("%s: doa_noise[%d] = %d\n",
  4731. __func__, i, sourceTrackingData->doa_noise[i]);
  4732. }
  4733. for (i = 0; i < MAX_POLAR_ACTIVITY_INDICATORS; i++) {
  4734. sourceTrackingData->polar_activity[i] =
  4735. source_tracking_params->polar_activity[i];
  4736. pr_debug("%s: polar_activity[%d] = %d\n",
  4737. __func__, i, sourceTrackingData->polar_activity[i]);
  4738. }
  4739. ret = 0;
  4740. done:
  4741. pr_debug("%s: Exit, ret=%d\n", __func__, ret);
  4742. return ret;
  4743. }
  4744. EXPORT_SYMBOL(adm_get_source_tracking);
  4745. int __init adm_init(void)
  4746. {
  4747. int i = 0, j;
  4748. this_adm.apr = NULL;
  4749. this_adm.ec_ref_rx = -1;
  4750. this_adm.num_ec_ref_rx_chans = 0;
  4751. this_adm.ec_ref_rx_bit_width = 0;
  4752. this_adm.ec_ref_rx_sampling_rate = 0;
  4753. atomic_set(&this_adm.matrix_map_stat, 0);
  4754. init_waitqueue_head(&this_adm.matrix_map_wait);
  4755. atomic_set(&this_adm.adm_stat, 0);
  4756. init_waitqueue_head(&this_adm.adm_wait);
  4757. for (i = 0; i < AFE_MAX_PORTS; i++) {
  4758. for (j = 0; j < MAX_COPPS_PER_PORT; j++) {
  4759. atomic_set(&this_adm.copp.id[i][j], RESET_COPP_ID);
  4760. atomic_set(&this_adm.copp.cnt[i][j], 0);
  4761. atomic_set(&this_adm.copp.topology[i][j], 0);
  4762. atomic_set(&this_adm.copp.mode[i][j], 0);
  4763. atomic_set(&this_adm.copp.stat[i][j], 0);
  4764. atomic_set(&this_adm.copp.rate[i][j], 0);
  4765. atomic_set(&this_adm.copp.channels[i][j], 0);
  4766. atomic_set(&this_adm.copp.bit_width[i][j], 0);
  4767. atomic_set(&this_adm.copp.app_type[i][j], 0);
  4768. atomic_set(&this_adm.copp.acdb_id[i][j], 0);
  4769. init_waitqueue_head(&this_adm.copp.wait[i][j]);
  4770. atomic_set(&this_adm.copp.adm_delay_stat[i][j], 0);
  4771. init_waitqueue_head(
  4772. &this_adm.copp.adm_delay_wait[i][j]);
  4773. atomic_set(&this_adm.copp.topology[i][j], 0);
  4774. this_adm.copp.adm_delay[i][j] = 0;
  4775. this_adm.copp.adm_status[i][j] =
  4776. ADM_STATUS_CALIBRATION_REQUIRED;
  4777. }
  4778. }
  4779. if (adm_init_cal_data())
  4780. pr_err("%s: could not init cal data!\n", __func__);
  4781. this_adm.sourceTrackingData.ion_client = NULL;
  4782. this_adm.sourceTrackingData.ion_handle = NULL;
  4783. this_adm.sourceTrackingData.memmap.size = 0;
  4784. this_adm.sourceTrackingData.memmap.kvaddr = NULL;
  4785. this_adm.sourceTrackingData.memmap.paddr = 0;
  4786. this_adm.sourceTrackingData.apr_cmd_status = -1;
  4787. atomic_set(&this_adm.mem_map_handles[ADM_MEM_MAP_INDEX_SOURCE_TRACKING],
  4788. 0);
  4789. mutex_init(&dts_srs_lock);
  4790. return 0;
  4791. }
  4792. void adm_exit(void)
  4793. {
  4794. mutex_destroy(&dts_srs_lock);
  4795. adm_delete_cal_data();
  4796. }