q6adm.c 158 KB

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