q6adm.c 132 KB

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