q6adm.c 130 KB

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