q6adm.c 158 KB

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