q6adm.c 156 KB

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