q6adm.c 131 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523
  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_v1 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_v1) + 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_v1) + 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 || cal_utils_is_cal_stale(cal_block))
  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_utils_is_cal_stale(cal_block))
  1809. continue;
  1810. if (cal_index == ADM_AUDPROC_CAL ||
  1811. cal_index == ADM_LSM_AUDPROC_CAL ||
  1812. cal_index == ADM_LSM_AUDPROC_PERSISTENT_CAL) {
  1813. audproc_cal_info = cal_block->cal_info;
  1814. if ((audproc_cal_info->path == path) &&
  1815. (cal_block->cal_data.size > 0))
  1816. return cal_block;
  1817. } else if (cal_index == ADM_AUDVOL_CAL) {
  1818. audvol_cal_info = cal_block->cal_info;
  1819. if ((audvol_cal_info->path == path) &&
  1820. (cal_block->cal_data.size > 0))
  1821. return cal_block;
  1822. }
  1823. }
  1824. pr_debug("%s: Can't find ADM cal for cal_index %d, path %d\n",
  1825. __func__, cal_index, path);
  1826. return NULL;
  1827. }
  1828. static struct cal_block_data *adm_find_cal_by_app_type(int cal_index, int path,
  1829. int app_type)
  1830. {
  1831. struct list_head *ptr, *next;
  1832. struct cal_block_data *cal_block = NULL;
  1833. struct audio_cal_info_audproc *audproc_cal_info = NULL;
  1834. struct audio_cal_info_audvol *audvol_cal_info = NULL;
  1835. pr_debug("%s\n", __func__);
  1836. list_for_each_safe(ptr, next,
  1837. &this_adm.cal_data[cal_index]->cal_blocks) {
  1838. cal_block = list_entry(ptr,
  1839. struct cal_block_data, list);
  1840. if (cal_utils_is_cal_stale(cal_block))
  1841. continue;
  1842. if (cal_index == ADM_AUDPROC_CAL ||
  1843. cal_index == ADM_LSM_AUDPROC_CAL ||
  1844. cal_index == ADM_LSM_AUDPROC_PERSISTENT_CAL) {
  1845. audproc_cal_info = cal_block->cal_info;
  1846. if ((audproc_cal_info->path == path) &&
  1847. (audproc_cal_info->app_type == app_type) &&
  1848. (cal_block->cal_data.size > 0))
  1849. return cal_block;
  1850. } else if (cal_index == ADM_AUDVOL_CAL) {
  1851. audvol_cal_info = cal_block->cal_info;
  1852. if ((audvol_cal_info->path == path) &&
  1853. (audvol_cal_info->app_type == app_type) &&
  1854. (cal_block->cal_data.size > 0))
  1855. return cal_block;
  1856. }
  1857. }
  1858. pr_debug("%s: Can't find ADM cali for cal_index %d, path %d, app %d, defaulting to search by path\n",
  1859. __func__, cal_index, path, app_type);
  1860. return adm_find_cal_by_path(cal_index, path);
  1861. }
  1862. static struct cal_block_data *adm_find_cal(int cal_index, int path,
  1863. int app_type, int acdb_id,
  1864. int sample_rate)
  1865. {
  1866. struct list_head *ptr, *next;
  1867. struct cal_block_data *cal_block = NULL;
  1868. struct audio_cal_info_audproc *audproc_cal_info = NULL;
  1869. struct audio_cal_info_audvol *audvol_cal_info = NULL;
  1870. pr_debug("%s:\n", __func__);
  1871. list_for_each_safe(ptr, next,
  1872. &this_adm.cal_data[cal_index]->cal_blocks) {
  1873. cal_block = list_entry(ptr,
  1874. struct cal_block_data, list);
  1875. if (cal_utils_is_cal_stale(cal_block))
  1876. continue;
  1877. if (cal_index == ADM_AUDPROC_CAL ||
  1878. cal_index == ADM_LSM_AUDPROC_CAL ||
  1879. cal_index == ADM_LSM_AUDPROC_PERSISTENT_CAL) {
  1880. audproc_cal_info = cal_block->cal_info;
  1881. if ((audproc_cal_info->path == path) &&
  1882. (audproc_cal_info->app_type == app_type) &&
  1883. (audproc_cal_info->acdb_id == acdb_id) &&
  1884. (audproc_cal_info->sample_rate == sample_rate) &&
  1885. (cal_block->cal_data.size > 0))
  1886. return cal_block;
  1887. } else if (cal_index == ADM_AUDVOL_CAL) {
  1888. audvol_cal_info = cal_block->cal_info;
  1889. if ((audvol_cal_info->path == path) &&
  1890. (audvol_cal_info->app_type == app_type) &&
  1891. (audvol_cal_info->acdb_id == acdb_id) &&
  1892. (cal_block->cal_data.size > 0))
  1893. return cal_block;
  1894. }
  1895. }
  1896. 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",
  1897. __func__, cal_index, path, app_type, acdb_id, sample_rate);
  1898. return adm_find_cal_by_app_type(cal_index, path, app_type);
  1899. }
  1900. static int adm_remap_and_send_cal_block(int cal_index, int port_id,
  1901. int copp_idx, struct cal_block_data *cal_block, int perf_mode,
  1902. int app_type, int acdb_id, int sample_rate)
  1903. {
  1904. int ret = 0;
  1905. pr_debug("%s: Sending cal_index cal %d\n", __func__, cal_index);
  1906. ret = remap_cal_data(cal_block, cal_index);
  1907. if (ret) {
  1908. pr_err("%s: Remap_cal_data failed for cal %d!\n",
  1909. __func__, cal_index);
  1910. goto done;
  1911. }
  1912. ret = send_adm_cal_block(port_id, copp_idx, cal_block, perf_mode);
  1913. if (ret < 0)
  1914. pr_debug("%s: No cal sent for cal_index %d, port_id = 0x%x! ret %d sample_rate %d\n",
  1915. __func__, cal_index, port_id, ret, sample_rate);
  1916. done:
  1917. return ret;
  1918. }
  1919. static void send_adm_cal_type(int cal_index, int path, int port_id,
  1920. int copp_idx, int perf_mode, int app_type,
  1921. int acdb_id, int sample_rate)
  1922. {
  1923. struct cal_block_data *cal_block = NULL;
  1924. int ret;
  1925. pr_debug("%s: cal index %d\n", __func__, cal_index);
  1926. if (this_adm.cal_data[cal_index] == NULL) {
  1927. pr_debug("%s: cal_index %d not allocated!\n",
  1928. __func__, cal_index);
  1929. goto done;
  1930. }
  1931. mutex_lock(&this_adm.cal_data[cal_index]->lock);
  1932. cal_block = adm_find_cal(cal_index, path, app_type, acdb_id,
  1933. sample_rate);
  1934. if (cal_block == NULL)
  1935. goto unlock;
  1936. ret = adm_remap_and_send_cal_block(cal_index, port_id, copp_idx,
  1937. cal_block, perf_mode, app_type, acdb_id, sample_rate);
  1938. cal_utils_mark_cal_used(cal_block);
  1939. unlock:
  1940. mutex_unlock(&this_adm.cal_data[cal_index]->lock);
  1941. done:
  1942. return;
  1943. }
  1944. static int get_cal_path(int path)
  1945. {
  1946. if (path == 0x1)
  1947. return RX_DEVICE;
  1948. else
  1949. return TX_DEVICE;
  1950. }
  1951. static void send_adm_cal(int port_id, int copp_idx, int path, int perf_mode,
  1952. int app_type, int acdb_id, int sample_rate,
  1953. int passthr_mode)
  1954. {
  1955. pr_debug("%s: port id 0x%x copp_idx %d\n", __func__, port_id, copp_idx);
  1956. if (passthr_mode != LISTEN) {
  1957. send_adm_cal_type(ADM_AUDPROC_CAL, path, port_id, copp_idx,
  1958. perf_mode, app_type, acdb_id, sample_rate);
  1959. } else {
  1960. send_adm_cal_type(ADM_LSM_AUDPROC_CAL, path, port_id, copp_idx,
  1961. perf_mode, app_type, acdb_id, sample_rate);
  1962. send_adm_cal_type(ADM_LSM_AUDPROC_PERSISTENT_CAL, path,
  1963. port_id, copp_idx, perf_mode, app_type,
  1964. acdb_id, sample_rate);
  1965. }
  1966. send_adm_cal_type(ADM_AUDVOL_CAL, path, port_id, copp_idx, perf_mode,
  1967. app_type, acdb_id, sample_rate);
  1968. }
  1969. /**
  1970. * adm_connect_afe_port -
  1971. * command to send ADM connect AFE port
  1972. *
  1973. * @mode: value of mode for ADM connect AFE
  1974. * @session_id: session active to connect
  1975. * @port_id: Port ID number
  1976. *
  1977. * Returns 0 on success or error on failure
  1978. */
  1979. int adm_connect_afe_port(int mode, int session_id, int port_id)
  1980. {
  1981. struct adm_cmd_connect_afe_port_v5 cmd;
  1982. int ret = 0;
  1983. int port_idx, copp_idx = 0;
  1984. pr_debug("%s: port_id: 0x%x session id:%d mode:%d\n", __func__,
  1985. port_id, session_id, mode);
  1986. port_id = afe_convert_virtual_to_portid(port_id);
  1987. port_idx = adm_validate_and_get_port_index(port_id);
  1988. if (port_idx < 0) {
  1989. pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
  1990. return -EINVAL;
  1991. }
  1992. if (this_adm.apr == NULL) {
  1993. this_adm.apr = apr_register("ADSP", "ADM", adm_callback,
  1994. 0xFFFFFFFF, &this_adm);
  1995. if (this_adm.apr == NULL) {
  1996. pr_err("%s: Unable to register ADM\n", __func__);
  1997. ret = -ENODEV;
  1998. return ret;
  1999. }
  2000. rtac_set_adm_handle(this_adm.apr);
  2001. }
  2002. pr_debug("%s: Port ID 0x%x, index %d\n", __func__, port_id, port_idx);
  2003. cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  2004. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  2005. cmd.hdr.pkt_size = sizeof(cmd);
  2006. cmd.hdr.src_svc = APR_SVC_ADM;
  2007. cmd.hdr.src_domain = APR_DOMAIN_APPS;
  2008. cmd.hdr.src_port = port_id;
  2009. cmd.hdr.dest_svc = APR_SVC_ADM;
  2010. cmd.hdr.dest_domain = APR_DOMAIN_ADSP;
  2011. cmd.hdr.dest_port = 0; /* Ignored */
  2012. cmd.hdr.token = port_idx << 16 | copp_idx;
  2013. cmd.hdr.opcode = ADM_CMD_CONNECT_AFE_PORT_V5;
  2014. cmd.mode = mode;
  2015. cmd.session_id = session_id;
  2016. cmd.afe_port_id = port_id;
  2017. atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
  2018. ret = apr_send_pkt(this_adm.apr, (uint32_t *)&cmd);
  2019. if (ret < 0) {
  2020. pr_err("%s: ADM enable for port_id: 0x%x failed ret %d\n",
  2021. __func__, port_id, ret);
  2022. ret = -EINVAL;
  2023. goto fail_cmd;
  2024. }
  2025. /* Wait for the callback with copp id */
  2026. ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
  2027. atomic_read(&this_adm.copp.stat[port_idx][copp_idx]) >= 0,
  2028. msecs_to_jiffies(TIMEOUT_MS));
  2029. if (!ret) {
  2030. pr_err("%s: ADM connect timedout for port_id: 0x%x\n",
  2031. __func__, port_id);
  2032. ret = -EINVAL;
  2033. goto fail_cmd;
  2034. } else if (atomic_read(&this_adm.copp.stat
  2035. [port_idx][copp_idx]) > 0) {
  2036. pr_err("%s: DSP returned error[%s]\n",
  2037. __func__, adsp_err_get_err_str(
  2038. atomic_read(&this_adm.copp.stat
  2039. [port_idx][copp_idx])));
  2040. ret = adsp_err_get_lnx_err_code(
  2041. atomic_read(&this_adm.copp.stat
  2042. [port_idx][copp_idx]));
  2043. goto fail_cmd;
  2044. }
  2045. atomic_inc(&this_adm.copp.cnt[port_idx][copp_idx]);
  2046. return 0;
  2047. fail_cmd:
  2048. return ret;
  2049. }
  2050. EXPORT_SYMBOL(adm_connect_afe_port);
  2051. int adm_arrange_mch_map(struct adm_cmd_device_open_v5 *open, int path,
  2052. int channel_mode)
  2053. {
  2054. int rc = 0, idx;
  2055. memset(open->dev_channel_mapping, 0, PCM_FORMAT_MAX_NUM_CHANNEL);
  2056. switch (path) {
  2057. case ADM_PATH_PLAYBACK:
  2058. idx = ADM_MCH_MAP_IDX_PLAYBACK;
  2059. break;
  2060. case ADM_PATH_LIVE_REC:
  2061. case ADM_PATH_NONLIVE_REC:
  2062. idx = ADM_MCH_MAP_IDX_REC;
  2063. break;
  2064. default:
  2065. goto non_mch_path;
  2066. };
  2067. if ((open->dev_num_channel > 2) && multi_ch_maps[idx].set_channel_map) {
  2068. memcpy(open->dev_channel_mapping,
  2069. multi_ch_maps[idx].channel_mapping,
  2070. PCM_FORMAT_MAX_NUM_CHANNEL);
  2071. } else {
  2072. if (channel_mode == 1) {
  2073. open->dev_channel_mapping[0] = PCM_CHANNEL_FC;
  2074. } else if (channel_mode == 2) {
  2075. open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
  2076. open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
  2077. } else if (channel_mode == 3) {
  2078. open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
  2079. open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
  2080. open->dev_channel_mapping[2] = PCM_CHANNEL_FC;
  2081. } else if (channel_mode == 4) {
  2082. open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
  2083. open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
  2084. open->dev_channel_mapping[2] = PCM_CHANNEL_LS;
  2085. open->dev_channel_mapping[3] = PCM_CHANNEL_RS;
  2086. } else if (channel_mode == 5) {
  2087. open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
  2088. open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
  2089. open->dev_channel_mapping[2] = PCM_CHANNEL_FC;
  2090. open->dev_channel_mapping[3] = PCM_CHANNEL_LS;
  2091. open->dev_channel_mapping[4] = PCM_CHANNEL_RS;
  2092. } else if (channel_mode == 6) {
  2093. open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
  2094. open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
  2095. open->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
  2096. open->dev_channel_mapping[3] = PCM_CHANNEL_FC;
  2097. open->dev_channel_mapping[4] = PCM_CHANNEL_LS;
  2098. open->dev_channel_mapping[5] = PCM_CHANNEL_RS;
  2099. } else if (channel_mode == 7) {
  2100. open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
  2101. open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
  2102. open->dev_channel_mapping[2] = PCM_CHANNEL_FC;
  2103. open->dev_channel_mapping[3] = PCM_CHANNEL_LFE;
  2104. open->dev_channel_mapping[4] = PCM_CHANNEL_LB;
  2105. open->dev_channel_mapping[5] = PCM_CHANNEL_RB;
  2106. open->dev_channel_mapping[6] = PCM_CHANNEL_CS;
  2107. } else if (channel_mode == 8) {
  2108. open->dev_channel_mapping[0] = PCM_CHANNEL_FL;
  2109. open->dev_channel_mapping[1] = PCM_CHANNEL_FR;
  2110. open->dev_channel_mapping[2] = PCM_CHANNEL_LFE;
  2111. open->dev_channel_mapping[3] = PCM_CHANNEL_FC;
  2112. open->dev_channel_mapping[4] = PCM_CHANNEL_LS;
  2113. open->dev_channel_mapping[5] = PCM_CHANNEL_RS;
  2114. open->dev_channel_mapping[6] = PCM_CHANNEL_LB;
  2115. open->dev_channel_mapping[7] = PCM_CHANNEL_RB;
  2116. } else {
  2117. pr_err("%s: invalid num_chan %d\n", __func__,
  2118. channel_mode);
  2119. rc = -EINVAL;
  2120. goto inval_ch_mod;
  2121. }
  2122. }
  2123. non_mch_path:
  2124. inval_ch_mod:
  2125. return rc;
  2126. }
  2127. int adm_arrange_mch_ep2_map(struct adm_cmd_device_open_v6 *open_v6,
  2128. int channel_mode)
  2129. {
  2130. int rc = 0;
  2131. memset(open_v6->dev_channel_mapping_eid2, 0,
  2132. PCM_FORMAT_MAX_NUM_CHANNEL);
  2133. if (channel_mode == 1) {
  2134. open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FC;
  2135. } else if (channel_mode == 2) {
  2136. open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
  2137. open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
  2138. } else if (channel_mode == 3) {
  2139. open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
  2140. open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
  2141. open_v6->dev_channel_mapping_eid2[2] = PCM_CHANNEL_FC;
  2142. } else if (channel_mode == 4) {
  2143. open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
  2144. open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
  2145. open_v6->dev_channel_mapping_eid2[2] = PCM_CHANNEL_LS;
  2146. open_v6->dev_channel_mapping_eid2[3] = PCM_CHANNEL_RS;
  2147. } else if (channel_mode == 5) {
  2148. open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
  2149. open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
  2150. open_v6->dev_channel_mapping_eid2[2] = PCM_CHANNEL_FC;
  2151. open_v6->dev_channel_mapping_eid2[3] = PCM_CHANNEL_LS;
  2152. open_v6->dev_channel_mapping_eid2[4] = PCM_CHANNEL_RS;
  2153. } else if (channel_mode == 6) {
  2154. open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
  2155. open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
  2156. open_v6->dev_channel_mapping_eid2[2] = PCM_CHANNEL_LFE;
  2157. open_v6->dev_channel_mapping_eid2[3] = PCM_CHANNEL_FC;
  2158. open_v6->dev_channel_mapping_eid2[4] = PCM_CHANNEL_LS;
  2159. open_v6->dev_channel_mapping_eid2[5] = PCM_CHANNEL_RS;
  2160. } else if (channel_mode == 8) {
  2161. open_v6->dev_channel_mapping_eid2[0] = PCM_CHANNEL_FL;
  2162. open_v6->dev_channel_mapping_eid2[1] = PCM_CHANNEL_FR;
  2163. open_v6->dev_channel_mapping_eid2[2] = PCM_CHANNEL_LFE;
  2164. open_v6->dev_channel_mapping_eid2[3] = PCM_CHANNEL_FC;
  2165. open_v6->dev_channel_mapping_eid2[4] = PCM_CHANNEL_LS;
  2166. open_v6->dev_channel_mapping_eid2[5] = PCM_CHANNEL_RS;
  2167. open_v6->dev_channel_mapping_eid2[6] = PCM_CHANNEL_LB;
  2168. open_v6->dev_channel_mapping_eid2[7] = PCM_CHANNEL_RB;
  2169. } else {
  2170. pr_err("%s: invalid num_chan %d\n", __func__,
  2171. channel_mode);
  2172. rc = -EINVAL;
  2173. }
  2174. return rc;
  2175. }
  2176. /**
  2177. * adm_open -
  2178. * command to send ADM open
  2179. *
  2180. * @port_id: port id number
  2181. * @path: direction or ADM path type
  2182. * @rate: sample rate of session
  2183. * @channel_mode: number of channels set
  2184. * @topology: topology active for this session
  2185. * @perf_mode: performance mode like LL/ULL/..
  2186. * @bit_width: bit width to set for copp
  2187. * @app_type: App type used for this session
  2188. * @acdb_id: ACDB ID of this device
  2189. *
  2190. * Returns 0 on success or error on failure
  2191. */
  2192. int adm_open(int port_id, int path, int rate, int channel_mode, int topology,
  2193. int perf_mode, uint16_t bit_width, int app_type, int acdb_id)
  2194. {
  2195. struct adm_cmd_device_open_v5 open;
  2196. struct adm_cmd_device_open_v6 open_v6;
  2197. int ret = 0;
  2198. int port_idx, flags;
  2199. int copp_idx = -1;
  2200. int tmp_port = q6audio_get_port_id(port_id);
  2201. pr_debug("%s:port %#x path:%d rate:%d mode:%d perf_mode:%d,topo_id %d\n",
  2202. __func__, port_id, path, rate, channel_mode, perf_mode,
  2203. topology);
  2204. port_id = q6audio_convert_virtual_to_portid(port_id);
  2205. port_idx = adm_validate_and_get_port_index(port_id);
  2206. if (port_idx < 0) {
  2207. pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
  2208. return -EINVAL;
  2209. }
  2210. if (this_adm.apr == NULL) {
  2211. this_adm.apr = apr_register("ADSP", "ADM", adm_callback,
  2212. 0xFFFFFFFF, &this_adm);
  2213. if (this_adm.apr == NULL) {
  2214. pr_err("%s: Unable to register ADM\n", __func__);
  2215. return -ENODEV;
  2216. }
  2217. rtac_set_adm_handle(this_adm.apr);
  2218. }
  2219. if (perf_mode == ULL_POST_PROCESSING_PCM_MODE) {
  2220. flags = ADM_ULL_POST_PROCESSING_DEVICE_SESSION;
  2221. if ((topology == DOLBY_ADM_COPP_TOPOLOGY_ID) ||
  2222. (topology == DS2_ADM_COPP_TOPOLOGY_ID) ||
  2223. (topology == SRS_TRUMEDIA_TOPOLOGY_ID))
  2224. topology = DEFAULT_COPP_TOPOLOGY;
  2225. } else if (perf_mode == ULTRA_LOW_LATENCY_PCM_MODE) {
  2226. flags = ADM_ULTRA_LOW_LATENCY_DEVICE_SESSION;
  2227. topology = NULL_COPP_TOPOLOGY;
  2228. rate = ULL_SUPPORTED_SAMPLE_RATE;
  2229. bit_width = ULL_SUPPORTED_BITS_PER_SAMPLE;
  2230. } else if (perf_mode == LOW_LATENCY_PCM_MODE) {
  2231. flags = ADM_LOW_LATENCY_DEVICE_SESSION;
  2232. if ((topology == DOLBY_ADM_COPP_TOPOLOGY_ID) ||
  2233. (topology == DS2_ADM_COPP_TOPOLOGY_ID) ||
  2234. (topology == SRS_TRUMEDIA_TOPOLOGY_ID))
  2235. topology = DEFAULT_COPP_TOPOLOGY;
  2236. } else {
  2237. if ((path == ADM_PATH_COMPRESSED_RX) ||
  2238. (path == ADM_PATH_COMPRESSED_TX))
  2239. flags = 0;
  2240. else
  2241. flags = ADM_LEGACY_DEVICE_SESSION;
  2242. }
  2243. if ((topology == VPM_TX_SM_ECNS_V2_COPP_TOPOLOGY) ||
  2244. (topology == VPM_TX_DM_FLUENCE_COPP_TOPOLOGY) ||
  2245. (topology == VPM_TX_DM_RFECNS_COPP_TOPOLOGY))
  2246. rate = 16000;
  2247. /*
  2248. * Routing driver reuses the same adm for streams with the same
  2249. * app_type, sample_rate etc.
  2250. * This isn't allowed for ULL streams as per the DSP interface
  2251. */
  2252. if (perf_mode != ULTRA_LOW_LATENCY_PCM_MODE)
  2253. copp_idx = adm_get_idx_if_copp_exists(port_idx, topology,
  2254. perf_mode,
  2255. rate, bit_width,
  2256. app_type);
  2257. if (copp_idx < 0) {
  2258. copp_idx = adm_get_next_available_copp(port_idx);
  2259. if (copp_idx >= MAX_COPPS_PER_PORT) {
  2260. pr_err("%s: exceeded copp id %d\n",
  2261. __func__, copp_idx);
  2262. return -EINVAL;
  2263. }
  2264. atomic_set(&this_adm.copp.cnt[port_idx][copp_idx], 0);
  2265. atomic_set(&this_adm.copp.topology[port_idx][copp_idx],
  2266. topology);
  2267. atomic_set(&this_adm.copp.mode[port_idx][copp_idx],
  2268. perf_mode);
  2269. atomic_set(&this_adm.copp.rate[port_idx][copp_idx],
  2270. rate);
  2271. atomic_set(&this_adm.copp.channels[port_idx][copp_idx],
  2272. channel_mode);
  2273. atomic_set(&this_adm.copp.bit_width[port_idx][copp_idx],
  2274. bit_width);
  2275. atomic_set(&this_adm.copp.app_type[port_idx][copp_idx],
  2276. app_type);
  2277. atomic_set(&this_adm.copp.acdb_id[port_idx][copp_idx],
  2278. acdb_id);
  2279. set_bit(ADM_STATUS_CALIBRATION_REQUIRED,
  2280. (void *)&this_adm.copp.adm_status[port_idx][copp_idx]);
  2281. if ((path != ADM_PATH_COMPRESSED_RX) &&
  2282. (path != ADM_PATH_COMPRESSED_TX))
  2283. send_adm_custom_topology();
  2284. }
  2285. if (this_adm.copp.adm_delay[port_idx][copp_idx] &&
  2286. perf_mode == LEGACY_PCM_MODE) {
  2287. atomic_set(&this_adm.copp.adm_delay_stat[port_idx][copp_idx],
  2288. 1);
  2289. this_adm.copp.adm_delay[port_idx][copp_idx] = 0;
  2290. wake_up(&this_adm.copp.adm_delay_wait[port_idx][copp_idx]);
  2291. }
  2292. /* Create a COPP if port id are not enabled */
  2293. if (atomic_read(&this_adm.copp.cnt[port_idx][copp_idx]) == 0) {
  2294. pr_debug("%s: open ADM: port_idx: %d, copp_idx: %d\n", __func__,
  2295. port_idx, copp_idx);
  2296. if ((topology == SRS_TRUMEDIA_TOPOLOGY_ID) &&
  2297. perf_mode == LEGACY_PCM_MODE) {
  2298. int res;
  2299. atomic_set(&this_adm.mem_map_index, ADM_SRS_TRUMEDIA);
  2300. msm_dts_srs_tm_ion_memmap(&this_adm.outband_memmap);
  2301. res = adm_memory_map_regions(&this_adm.outband_memmap.paddr, 0,
  2302. (uint32_t *)&this_adm.outband_memmap.size, 1);
  2303. if (res < 0) {
  2304. pr_err("%s: SRS adm_memory_map_regions failed ! addr = 0x%pK, size = %d\n",
  2305. __func__, (void *)this_adm.outband_memmap.paddr,
  2306. (uint32_t)this_adm.outband_memmap.size);
  2307. }
  2308. }
  2309. open.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  2310. APR_HDR_LEN(APR_HDR_SIZE),
  2311. APR_PKT_VER);
  2312. open.hdr.pkt_size = sizeof(open);
  2313. open.hdr.src_svc = APR_SVC_ADM;
  2314. open.hdr.src_domain = APR_DOMAIN_APPS;
  2315. open.hdr.src_port = tmp_port;
  2316. open.hdr.dest_svc = APR_SVC_ADM;
  2317. open.hdr.dest_domain = APR_DOMAIN_ADSP;
  2318. open.hdr.dest_port = tmp_port;
  2319. open.hdr.token = port_idx << 16 | copp_idx;
  2320. open.hdr.opcode = ADM_CMD_DEVICE_OPEN_V5;
  2321. open.flags = flags;
  2322. open.mode_of_operation = path;
  2323. open.endpoint_id_1 = tmp_port;
  2324. open.endpoint_id_2 = 0xFFFF;
  2325. if (this_adm.ec_ref_rx && (path != 1)) {
  2326. open.endpoint_id_2 = this_adm.ec_ref_rx;
  2327. this_adm.ec_ref_rx = -1;
  2328. }
  2329. open.topology_id = topology;
  2330. open.dev_num_channel = channel_mode & 0x00FF;
  2331. open.bit_width = bit_width;
  2332. WARN_ON((perf_mode == ULTRA_LOW_LATENCY_PCM_MODE) &&
  2333. (rate != ULL_SUPPORTED_SAMPLE_RATE));
  2334. open.sample_rate = rate;
  2335. ret = adm_arrange_mch_map(&open, path, channel_mode);
  2336. if (ret)
  2337. return ret;
  2338. pr_debug("%s: port_id=0x%x rate=%d topology_id=0x%X\n",
  2339. __func__, open.endpoint_id_1, open.sample_rate,
  2340. open.topology_id);
  2341. atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
  2342. if ((this_adm.num_ec_ref_rx_chans != 0) && (path != 1) &&
  2343. (open.endpoint_id_2 != 0xFFFF)) {
  2344. memset(&open_v6, 0,
  2345. sizeof(struct adm_cmd_device_open_v6));
  2346. memcpy(&open_v6, &open,
  2347. sizeof(struct adm_cmd_device_open_v5));
  2348. open_v6.hdr.opcode = ADM_CMD_DEVICE_OPEN_V6;
  2349. open_v6.hdr.pkt_size = sizeof(open_v6);
  2350. open_v6.dev_num_channel_eid2 =
  2351. this_adm.num_ec_ref_rx_chans;
  2352. this_adm.num_ec_ref_rx_chans = 0;
  2353. if (this_adm.ec_ref_rx_bit_width != 0) {
  2354. open_v6.bit_width_eid2 =
  2355. this_adm.ec_ref_rx_bit_width;
  2356. this_adm.ec_ref_rx_bit_width = 0;
  2357. } else {
  2358. open_v6.bit_width_eid2 = bit_width;
  2359. }
  2360. if (this_adm.ec_ref_rx_sampling_rate != 0) {
  2361. open_v6.sample_rate_eid2 =
  2362. this_adm.ec_ref_rx_sampling_rate;
  2363. this_adm.ec_ref_rx_sampling_rate = 0;
  2364. } else {
  2365. open_v6.sample_rate_eid2 = rate;
  2366. }
  2367. pr_debug("%s: eid2_channels=%d eid2_bit_width=%d eid2_rate=%d\n",
  2368. __func__, open_v6.dev_num_channel_eid2,
  2369. open_v6.bit_width_eid2,
  2370. open_v6.sample_rate_eid2);
  2371. ret = adm_arrange_mch_ep2_map(&open_v6,
  2372. open_v6.dev_num_channel_eid2);
  2373. if (ret)
  2374. return ret;
  2375. ret = apr_send_pkt(this_adm.apr, (uint32_t *)&open_v6);
  2376. } else {
  2377. ret = apr_send_pkt(this_adm.apr, (uint32_t *)&open);
  2378. }
  2379. if (ret < 0) {
  2380. pr_err("%s: port_id: 0x%x for[0x%x] failed %d\n",
  2381. __func__, tmp_port, port_id, ret);
  2382. return -EINVAL;
  2383. }
  2384. /* Wait for the callback with copp id */
  2385. ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
  2386. atomic_read(&this_adm.copp.stat
  2387. [port_idx][copp_idx]) >= 0,
  2388. msecs_to_jiffies(TIMEOUT_MS));
  2389. if (!ret) {
  2390. pr_err("%s: ADM open timedout for port_id: 0x%x for [0x%x]\n",
  2391. __func__, tmp_port, port_id);
  2392. return -EINVAL;
  2393. } else if (atomic_read(&this_adm.copp.stat
  2394. [port_idx][copp_idx]) > 0) {
  2395. pr_err("%s: DSP returned error[%s]\n",
  2396. __func__, adsp_err_get_err_str(
  2397. atomic_read(&this_adm.copp.stat
  2398. [port_idx][copp_idx])));
  2399. return adsp_err_get_lnx_err_code(
  2400. atomic_read(&this_adm.copp.stat
  2401. [port_idx][copp_idx]));
  2402. }
  2403. }
  2404. atomic_inc(&this_adm.copp.cnt[port_idx][copp_idx]);
  2405. return copp_idx;
  2406. }
  2407. EXPORT_SYMBOL(adm_open);
  2408. /**
  2409. * adm_copp_mfc_cfg -
  2410. * command to send ADM MFC config
  2411. *
  2412. * @port_id: Port ID number
  2413. * @copp_idx: copp index assigned
  2414. * @dst_sample_rate: sink sample rate
  2415. *
  2416. */
  2417. void adm_copp_mfc_cfg(int port_id, int copp_idx, int dst_sample_rate)
  2418. {
  2419. struct audproc_mfc_param_media_fmt mfc_cfg;
  2420. struct adm_cmd_device_open_v5 open;
  2421. struct param_hdr_v3 param_hdr;
  2422. int port_idx;
  2423. int rc = 0;
  2424. int i = 0;
  2425. port_id = q6audio_convert_virtual_to_portid(port_id);
  2426. port_idx = adm_validate_and_get_port_index(port_id);
  2427. if (port_idx < 0) {
  2428. pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
  2429. goto fail_cmd;
  2430. }
  2431. if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
  2432. pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
  2433. goto fail_cmd;
  2434. }
  2435. memset(&mfc_cfg, 0, sizeof(mfc_cfg));
  2436. memset(&open, 0, sizeof(open));
  2437. memset(&param_hdr, 0, sizeof(param_hdr));
  2438. param_hdr.module_id = AUDPROC_MODULE_ID_MFC;
  2439. param_hdr.instance_id = INSTANCE_ID_0;
  2440. param_hdr.param_id = AUDPROC_PARAM_ID_MFC_OUTPUT_MEDIA_FORMAT;
  2441. param_hdr.param_size = sizeof(mfc_cfg);
  2442. mfc_cfg.sampling_rate = dst_sample_rate;
  2443. mfc_cfg.bits_per_sample =
  2444. atomic_read(&this_adm.copp.bit_width[port_idx][copp_idx]);
  2445. open.dev_num_channel = mfc_cfg.num_channels =
  2446. atomic_read(&this_adm.copp.channels[port_idx][copp_idx]);
  2447. rc = adm_arrange_mch_map(&open, ADM_PATH_PLAYBACK,
  2448. mfc_cfg.num_channels);
  2449. if (rc < 0) {
  2450. pr_err("%s: unable to get channal map\n", __func__);
  2451. goto fail_cmd;
  2452. }
  2453. for (i = 0; i < mfc_cfg.num_channels; i++)
  2454. mfc_cfg.channel_type[i] =
  2455. (uint16_t) open.dev_channel_mapping[i];
  2456. atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
  2457. 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",
  2458. __func__, port_idx, copp_idx,
  2459. atomic_read(&this_adm.copp.rate[port_idx][copp_idx]),
  2460. mfc_cfg.bits_per_sample, mfc_cfg.num_channels,
  2461. mfc_cfg.sampling_rate);
  2462. rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
  2463. (uint8_t *) &mfc_cfg);
  2464. if (rc)
  2465. pr_err("%s: Failed to set media format configuration data, err %d\n",
  2466. __func__, rc);
  2467. fail_cmd:
  2468. return;
  2469. }
  2470. EXPORT_SYMBOL(adm_copp_mfc_cfg);
  2471. static void route_set_opcode_matrix_id(
  2472. struct adm_cmd_matrix_map_routings_v5 **route_addr,
  2473. int path, uint32_t passthr_mode)
  2474. {
  2475. struct adm_cmd_matrix_map_routings_v5 *route = *route_addr;
  2476. switch (path) {
  2477. case ADM_PATH_PLAYBACK:
  2478. route->hdr.opcode = ADM_CMD_MATRIX_MAP_ROUTINGS_V5;
  2479. route->matrix_id = ADM_MATRIX_ID_AUDIO_RX;
  2480. break;
  2481. case ADM_PATH_LIVE_REC:
  2482. if (passthr_mode == LISTEN) {
  2483. route->hdr.opcode =
  2484. ADM_CMD_STREAM_DEVICE_MAP_ROUTINGS_V5;
  2485. route->matrix_id = ADM_MATRIX_ID_LISTEN_TX;
  2486. break;
  2487. }
  2488. /* fall through to set matrix id for non-listen case */
  2489. case ADM_PATH_NONLIVE_REC:
  2490. route->hdr.opcode = ADM_CMD_MATRIX_MAP_ROUTINGS_V5;
  2491. route->matrix_id = ADM_MATRIX_ID_AUDIO_TX;
  2492. break;
  2493. case ADM_PATH_COMPRESSED_RX:
  2494. route->hdr.opcode = ADM_CMD_STREAM_DEVICE_MAP_ROUTINGS_V5;
  2495. route->matrix_id = ADM_MATRIX_ID_COMPRESSED_AUDIO_RX;
  2496. break;
  2497. case ADM_PATH_COMPRESSED_TX:
  2498. route->hdr.opcode = ADM_CMD_STREAM_DEVICE_MAP_ROUTINGS_V5;
  2499. route->matrix_id = ADM_MATRIX_ID_COMPRESSED_AUDIO_TX;
  2500. break;
  2501. default:
  2502. pr_err("%s: Wrong path set[%d]\n", __func__, path);
  2503. break;
  2504. }
  2505. pr_debug("%s: opcode 0x%x, matrix id %d\n",
  2506. __func__, route->hdr.opcode, route->matrix_id);
  2507. }
  2508. /**
  2509. * adm_matrix_map -
  2510. * command to send ADM matrix map for ADM copp list
  2511. *
  2512. * @path: direction or ADM path type
  2513. * @payload_map: have info of session id and associated copp_idx/num_copps
  2514. * @perf_mode: performance mode like LL/ULL/..
  2515. * @passthr_mode: flag to indicate passthrough mode
  2516. *
  2517. * Returns 0 on success or error on failure
  2518. */
  2519. int adm_matrix_map(int path, struct route_payload payload_map, int perf_mode,
  2520. uint32_t passthr_mode)
  2521. {
  2522. struct adm_cmd_matrix_map_routings_v5 *route;
  2523. struct adm_session_map_node_v5 *node;
  2524. uint16_t *copps_list;
  2525. int cmd_size = 0;
  2526. int ret = 0, i = 0;
  2527. void *payload = NULL;
  2528. void *matrix_map = NULL;
  2529. int port_idx, copp_idx;
  2530. /* Assumes port_ids have already been validated during adm_open */
  2531. cmd_size = (sizeof(struct adm_cmd_matrix_map_routings_v5) +
  2532. sizeof(struct adm_session_map_node_v5) +
  2533. (sizeof(uint32_t) * payload_map.num_copps));
  2534. matrix_map = kzalloc(cmd_size, GFP_KERNEL);
  2535. if (matrix_map == NULL) {
  2536. pr_err("%s: Mem alloc failed\n", __func__);
  2537. ret = -EINVAL;
  2538. return ret;
  2539. }
  2540. route = (struct adm_cmd_matrix_map_routings_v5 *)matrix_map;
  2541. route->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  2542. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  2543. route->hdr.pkt_size = cmd_size;
  2544. route->hdr.src_svc = 0;
  2545. route->hdr.src_domain = APR_DOMAIN_APPS;
  2546. route->hdr.src_port = 0; /* Ignored */;
  2547. route->hdr.dest_svc = APR_SVC_ADM;
  2548. route->hdr.dest_domain = APR_DOMAIN_ADSP;
  2549. route->hdr.dest_port = 0; /* Ignored */;
  2550. route->hdr.token = 0;
  2551. route->num_sessions = 1;
  2552. route_set_opcode_matrix_id(&route, path, passthr_mode);
  2553. payload = ((u8 *)matrix_map +
  2554. sizeof(struct adm_cmd_matrix_map_routings_v5));
  2555. node = (struct adm_session_map_node_v5 *)payload;
  2556. node->session_id = payload_map.session_id;
  2557. node->num_copps = payload_map.num_copps;
  2558. payload = (u8 *)node + sizeof(struct adm_session_map_node_v5);
  2559. copps_list = (uint16_t *)payload;
  2560. for (i = 0; i < payload_map.num_copps; i++) {
  2561. port_idx =
  2562. adm_validate_and_get_port_index(payload_map.port_id[i]);
  2563. if (port_idx < 0) {
  2564. pr_err("%s: Invalid port_id 0x%x\n", __func__,
  2565. payload_map.port_id[i]);
  2566. ret = -EINVAL;
  2567. goto fail_cmd;
  2568. }
  2569. copp_idx = payload_map.copp_idx[i];
  2570. copps_list[i] = atomic_read(&this_adm.copp.id[port_idx]
  2571. [copp_idx]);
  2572. }
  2573. atomic_set(&this_adm.matrix_map_stat, -1);
  2574. ret = apr_send_pkt(this_adm.apr, (uint32_t *)matrix_map);
  2575. if (ret < 0) {
  2576. pr_err("%s: routing for syream %d failed ret %d\n",
  2577. __func__, payload_map.session_id, ret);
  2578. ret = -EINVAL;
  2579. goto fail_cmd;
  2580. }
  2581. ret = wait_event_timeout(this_adm.matrix_map_wait,
  2582. atomic_read(&this_adm.matrix_map_stat) >= 0,
  2583. msecs_to_jiffies(TIMEOUT_MS));
  2584. if (!ret) {
  2585. pr_err("%s: routing for syream %d failed\n", __func__,
  2586. payload_map.session_id);
  2587. ret = -EINVAL;
  2588. goto fail_cmd;
  2589. } else if (atomic_read(&this_adm.matrix_map_stat) > 0) {
  2590. pr_err("%s: DSP returned error[%s]\n", __func__,
  2591. adsp_err_get_err_str(atomic_read(
  2592. &this_adm.matrix_map_stat)));
  2593. ret = adsp_err_get_lnx_err_code(
  2594. atomic_read(&this_adm.matrix_map_stat));
  2595. goto fail_cmd;
  2596. }
  2597. if ((perf_mode != ULTRA_LOW_LATENCY_PCM_MODE) &&
  2598. (path != ADM_PATH_COMPRESSED_RX)) {
  2599. for (i = 0; i < payload_map.num_copps; i++) {
  2600. port_idx = afe_get_port_index(payload_map.port_id[i]);
  2601. copp_idx = payload_map.copp_idx[i];
  2602. if (port_idx < 0 || copp_idx < 0 ||
  2603. (copp_idx > MAX_COPPS_PER_PORT - 1)) {
  2604. pr_err("%s: Invalid idx port_idx %d copp_idx %d\n",
  2605. __func__, port_idx, copp_idx);
  2606. continue;
  2607. }
  2608. rtac_add_adm_device(payload_map.port_id[i],
  2609. atomic_read(&this_adm.copp.id
  2610. [port_idx][copp_idx]),
  2611. get_cal_path(path),
  2612. payload_map.session_id,
  2613. payload_map.app_type[i],
  2614. payload_map.acdb_dev_id[i]);
  2615. if (!test_bit(ADM_STATUS_CALIBRATION_REQUIRED,
  2616. (void *)&this_adm.copp.adm_status[port_idx]
  2617. [copp_idx])) {
  2618. pr_debug("%s: adm copp[0x%x][%d] already sent",
  2619. __func__, port_idx, copp_idx);
  2620. continue;
  2621. }
  2622. send_adm_cal(payload_map.port_id[i], copp_idx,
  2623. get_cal_path(path), perf_mode,
  2624. payload_map.app_type[i],
  2625. payload_map.acdb_dev_id[i],
  2626. payload_map.sample_rate[i],
  2627. passthr_mode);
  2628. /* ADM COPP calibration is already sent */
  2629. clear_bit(ADM_STATUS_CALIBRATION_REQUIRED,
  2630. (void *)&this_adm.copp.
  2631. adm_status[port_idx][copp_idx]);
  2632. pr_debug("%s: copp_id: %d\n", __func__,
  2633. atomic_read(&this_adm.copp.id[port_idx]
  2634. [copp_idx]));
  2635. }
  2636. }
  2637. fail_cmd:
  2638. kfree(matrix_map);
  2639. return ret;
  2640. }
  2641. EXPORT_SYMBOL(adm_matrix_map);
  2642. /**
  2643. * adm_ec_ref_rx_id -
  2644. * Update EC ref port ID
  2645. *
  2646. */
  2647. void adm_ec_ref_rx_id(int port_id)
  2648. {
  2649. this_adm.ec_ref_rx = port_id;
  2650. pr_debug("%s: ec_ref_rx:%d\n", __func__, this_adm.ec_ref_rx);
  2651. }
  2652. EXPORT_SYMBOL(adm_ec_ref_rx_id);
  2653. /**
  2654. * adm_num_ec_ref_rx_chans -
  2655. * Update EC ref number of channels
  2656. *
  2657. */
  2658. void adm_num_ec_ref_rx_chans(int num_chans)
  2659. {
  2660. this_adm.num_ec_ref_rx_chans = num_chans;
  2661. pr_debug("%s: num_ec_ref_rx_chans:%d\n",
  2662. __func__, this_adm.num_ec_ref_rx_chans);
  2663. }
  2664. EXPORT_SYMBOL(adm_num_ec_ref_rx_chans);
  2665. /**
  2666. * adm_ec_ref_rx_bit_width -
  2667. * Update EC ref bit_width
  2668. *
  2669. */
  2670. void adm_ec_ref_rx_bit_width(int bit_width)
  2671. {
  2672. this_adm.ec_ref_rx_bit_width = bit_width;
  2673. pr_debug("%s: ec_ref_rx_bit_width:%d\n",
  2674. __func__, this_adm.ec_ref_rx_bit_width);
  2675. }
  2676. EXPORT_SYMBOL(adm_ec_ref_rx_bit_width);
  2677. /**
  2678. * adm_ec_ref_rx_sampling_rate -
  2679. * Update EC ref sample rate
  2680. *
  2681. */
  2682. void adm_ec_ref_rx_sampling_rate(int sampling_rate)
  2683. {
  2684. this_adm.ec_ref_rx_sampling_rate = sampling_rate;
  2685. pr_debug("%s: ec_ref_rx_sampling_rate:%d\n",
  2686. __func__, this_adm.ec_ref_rx_sampling_rate);
  2687. }
  2688. EXPORT_SYMBOL(adm_ec_ref_rx_sampling_rate);
  2689. /**
  2690. * adm_close -
  2691. * command to close ADM copp
  2692. *
  2693. * @port_id: Port ID number
  2694. * @perf_mode: performance mode like LL/ULL/..
  2695. * @copp_idx: copp index assigned
  2696. *
  2697. * Returns 0 on success or error on failure
  2698. */
  2699. int adm_close(int port_id, int perf_mode, int copp_idx)
  2700. {
  2701. struct apr_hdr close;
  2702. int ret = 0, port_idx;
  2703. int copp_id = RESET_COPP_ID;
  2704. pr_debug("%s: port_id=0x%x perf_mode: %d copp_idx: %d\n", __func__,
  2705. port_id, perf_mode, copp_idx);
  2706. port_id = q6audio_convert_virtual_to_portid(port_id);
  2707. port_idx = adm_validate_and_get_port_index(port_id);
  2708. if (port_idx < 0) {
  2709. pr_err("%s: Invalid port_id 0x%x\n",
  2710. __func__, port_id);
  2711. return -EINVAL;
  2712. }
  2713. if ((copp_idx < 0) || (copp_idx >= MAX_COPPS_PER_PORT)) {
  2714. pr_err("%s: Invalid copp idx: %d\n", __func__, copp_idx);
  2715. return -EINVAL;
  2716. }
  2717. if (this_adm.copp.adm_delay[port_idx][copp_idx] && perf_mode
  2718. == LEGACY_PCM_MODE) {
  2719. atomic_set(&this_adm.copp.adm_delay_stat[port_idx][copp_idx],
  2720. 1);
  2721. this_adm.copp.adm_delay[port_idx][copp_idx] = 0;
  2722. wake_up(&this_adm.copp.adm_delay_wait[port_idx][copp_idx]);
  2723. }
  2724. atomic_dec(&this_adm.copp.cnt[port_idx][copp_idx]);
  2725. if (!(atomic_read(&this_adm.copp.cnt[port_idx][copp_idx]))) {
  2726. copp_id = adm_get_copp_id(port_idx, copp_idx);
  2727. pr_debug("%s: Closing ADM port_idx:%d copp_idx:%d copp_id:0x%x\n",
  2728. __func__, port_idx, copp_idx, copp_id);
  2729. if ((!perf_mode) && (this_adm.outband_memmap.paddr != 0) &&
  2730. (atomic_read(&this_adm.copp.topology[port_idx][copp_idx]) ==
  2731. SRS_TRUMEDIA_TOPOLOGY_ID)) {
  2732. atomic_set(&this_adm.mem_map_index,
  2733. ADM_SRS_TRUMEDIA);
  2734. ret = adm_memory_unmap_regions();
  2735. if (ret < 0) {
  2736. pr_err("%s: adm mem unmmap err %d",
  2737. __func__, ret);
  2738. } else {
  2739. atomic_set(&this_adm.mem_map_handles
  2740. [ADM_SRS_TRUMEDIA], 0);
  2741. }
  2742. }
  2743. if ((afe_get_port_type(port_id) == MSM_AFE_PORT_TYPE_TX) &&
  2744. this_adm.sourceTrackingData.memmap.paddr) {
  2745. atomic_set(&this_adm.mem_map_index,
  2746. ADM_MEM_MAP_INDEX_SOURCE_TRACKING);
  2747. ret = adm_memory_unmap_regions();
  2748. if (ret < 0) {
  2749. pr_err("%s: adm mem unmmap err %d",
  2750. __func__, ret);
  2751. }
  2752. msm_audio_ion_free(
  2753. this_adm.sourceTrackingData.dma_buf);
  2754. this_adm.sourceTrackingData.dma_buf = NULL;
  2755. this_adm.sourceTrackingData.memmap.size = 0;
  2756. this_adm.sourceTrackingData.memmap.kvaddr = NULL;
  2757. this_adm.sourceTrackingData.memmap.paddr = 0;
  2758. this_adm.sourceTrackingData.apr_cmd_status = -1;
  2759. atomic_set(&this_adm.mem_map_handles[
  2760. ADM_MEM_MAP_INDEX_SOURCE_TRACKING], 0);
  2761. }
  2762. close.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  2763. APR_HDR_LEN(APR_HDR_SIZE),
  2764. APR_PKT_VER);
  2765. close.pkt_size = sizeof(close);
  2766. close.src_svc = APR_SVC_ADM;
  2767. close.src_domain = APR_DOMAIN_APPS;
  2768. close.src_port = port_id;
  2769. close.dest_svc = APR_SVC_ADM;
  2770. close.dest_domain = APR_DOMAIN_ADSP;
  2771. close.dest_port = copp_id;
  2772. close.token = port_idx << 16 | copp_idx;
  2773. close.opcode = ADM_CMD_DEVICE_CLOSE_V5;
  2774. atomic_set(&this_adm.copp.id[port_idx][copp_idx],
  2775. RESET_COPP_ID);
  2776. atomic_set(&this_adm.copp.cnt[port_idx][copp_idx], 0);
  2777. atomic_set(&this_adm.copp.topology[port_idx][copp_idx], 0);
  2778. atomic_set(&this_adm.copp.mode[port_idx][copp_idx], 0);
  2779. atomic_set(&this_adm.copp.stat[port_idx][copp_idx], -1);
  2780. atomic_set(&this_adm.copp.rate[port_idx][copp_idx], 0);
  2781. atomic_set(&this_adm.copp.channels[port_idx][copp_idx], 0);
  2782. atomic_set(&this_adm.copp.bit_width[port_idx][copp_idx], 0);
  2783. atomic_set(&this_adm.copp.app_type[port_idx][copp_idx], 0);
  2784. clear_bit(ADM_STATUS_CALIBRATION_REQUIRED,
  2785. (void *)&this_adm.copp.adm_status[port_idx][copp_idx]);
  2786. ret = apr_send_pkt(this_adm.apr, (uint32_t *)&close);
  2787. if (ret < 0) {
  2788. pr_err("%s: ADM close failed %d\n", __func__, ret);
  2789. return -EINVAL;
  2790. }
  2791. ret = wait_event_timeout(this_adm.copp.wait[port_idx][copp_idx],
  2792. atomic_read(&this_adm.copp.stat
  2793. [port_idx][copp_idx]) >= 0,
  2794. msecs_to_jiffies(TIMEOUT_MS));
  2795. if (!ret) {
  2796. pr_err("%s: ADM cmd Route timedout for port 0x%x\n",
  2797. __func__, port_id);
  2798. return -EINVAL;
  2799. } else if (atomic_read(&this_adm.copp.stat
  2800. [port_idx][copp_idx]) > 0) {
  2801. pr_err("%s: DSP returned error[%s]\n",
  2802. __func__, adsp_err_get_err_str(
  2803. atomic_read(&this_adm.copp.stat
  2804. [port_idx][copp_idx])));
  2805. return adsp_err_get_lnx_err_code(
  2806. atomic_read(&this_adm.copp.stat
  2807. [port_idx][copp_idx]));
  2808. }
  2809. }
  2810. if (perf_mode != ULTRA_LOW_LATENCY_PCM_MODE) {
  2811. pr_debug("%s: remove adm device from rtac\n", __func__);
  2812. rtac_remove_adm_device(port_id, copp_id);
  2813. }
  2814. return 0;
  2815. }
  2816. EXPORT_SYMBOL(adm_close);
  2817. int send_rtac_audvol_cal(void)
  2818. {
  2819. int ret = 0;
  2820. int ret2 = 0;
  2821. int i = 0;
  2822. int copp_idx, port_idx, acdb_id, app_id, path;
  2823. struct cal_block_data *cal_block = NULL;
  2824. struct audio_cal_info_audvol *audvol_cal_info = NULL;
  2825. struct rtac_adm rtac_adm_data;
  2826. mutex_lock(&this_adm.cal_data[ADM_RTAC_AUDVOL_CAL]->lock);
  2827. cal_block = cal_utils_get_only_cal_block(
  2828. this_adm.cal_data[ADM_RTAC_AUDVOL_CAL]);
  2829. if (cal_block == NULL || cal_utils_is_cal_stale(cal_block)) {
  2830. pr_err("%s: can't find cal block!\n", __func__);
  2831. goto unlock;
  2832. }
  2833. audvol_cal_info = cal_block->cal_info;
  2834. if (audvol_cal_info == NULL) {
  2835. pr_err("%s: audvol_cal_info is NULL!\n", __func__);
  2836. goto unlock;
  2837. }
  2838. get_rtac_adm_data(&rtac_adm_data);
  2839. for (; i < rtac_adm_data.num_of_dev; i++) {
  2840. acdb_id = rtac_adm_data.device[i].acdb_dev_id;
  2841. if (acdb_id == 0)
  2842. acdb_id = audvol_cal_info->acdb_id;
  2843. app_id = rtac_adm_data.device[i].app_type;
  2844. if (app_id == 0)
  2845. app_id = audvol_cal_info->app_type;
  2846. path = afe_get_port_type(rtac_adm_data.device[i].afe_port);
  2847. if ((acdb_id == audvol_cal_info->acdb_id) &&
  2848. (app_id == audvol_cal_info->app_type) &&
  2849. (path == audvol_cal_info->path)) {
  2850. if (adm_get_indexes_from_copp_id(rtac_adm_data.
  2851. device[i].copp, &copp_idx, &port_idx) != 0) {
  2852. pr_debug("%s: Copp Id %d is not active\n",
  2853. __func__,
  2854. rtac_adm_data.device[i].copp);
  2855. continue;
  2856. }
  2857. ret2 = adm_remap_and_send_cal_block(ADM_RTAC_AUDVOL_CAL,
  2858. rtac_adm_data.device[i].afe_port,
  2859. copp_idx, cal_block,
  2860. atomic_read(&this_adm.copp.
  2861. mode[port_idx][copp_idx]),
  2862. audvol_cal_info->app_type,
  2863. audvol_cal_info->acdb_id,
  2864. atomic_read(&this_adm.copp.
  2865. rate[port_idx][copp_idx]));
  2866. if (ret2 < 0) {
  2867. pr_debug("%s: remap and send failed for copp Id %d, acdb id %d, app type %d, path %d\n",
  2868. __func__, rtac_adm_data.device[i].copp,
  2869. audvol_cal_info->acdb_id,
  2870. audvol_cal_info->app_type,
  2871. audvol_cal_info->path);
  2872. ret = ret2;
  2873. }
  2874. }
  2875. }
  2876. unlock:
  2877. mutex_unlock(&this_adm.cal_data[ADM_RTAC_AUDVOL_CAL]->lock);
  2878. return ret;
  2879. }
  2880. int adm_map_rtac_block(struct rtac_cal_block_data *cal_block)
  2881. {
  2882. int result = 0;
  2883. pr_debug("%s:\n", __func__);
  2884. if (cal_block == NULL) {
  2885. pr_err("%s: cal_block is NULL!\n",
  2886. __func__);
  2887. result = -EINVAL;
  2888. goto done;
  2889. }
  2890. if (cal_block->cal_data.paddr == 0) {
  2891. pr_debug("%s: No address to map!\n",
  2892. __func__);
  2893. result = -EINVAL;
  2894. goto done;
  2895. }
  2896. if (cal_block->map_data.map_size == 0) {
  2897. pr_debug("%s: map size is 0!\n",
  2898. __func__);
  2899. result = -EINVAL;
  2900. goto done;
  2901. }
  2902. /* valid port ID needed for callback use primary I2S */
  2903. atomic_set(&this_adm.mem_map_index, ADM_RTAC_APR_CAL);
  2904. result = adm_memory_map_regions(&cal_block->cal_data.paddr, 0,
  2905. &cal_block->map_data.map_size, 1);
  2906. if (result < 0) {
  2907. pr_err("%s: RTAC mmap did not work! size = %d result %d\n",
  2908. __func__,
  2909. cal_block->map_data.map_size, result);
  2910. pr_debug("%s: RTAC mmap did not work! addr = 0x%pK, size = %d\n",
  2911. __func__,
  2912. &cal_block->cal_data.paddr,
  2913. cal_block->map_data.map_size);
  2914. goto done;
  2915. }
  2916. cal_block->map_data.map_handle = atomic_read(
  2917. &this_adm.mem_map_handles[ADM_RTAC_APR_CAL]);
  2918. done:
  2919. return result;
  2920. }
  2921. int adm_unmap_rtac_block(uint32_t *mem_map_handle)
  2922. {
  2923. int result = 0;
  2924. pr_debug("%s:\n", __func__);
  2925. if (mem_map_handle == NULL) {
  2926. pr_debug("%s: Map handle is NULL, nothing to unmap\n",
  2927. __func__);
  2928. goto done;
  2929. }
  2930. if (*mem_map_handle == 0) {
  2931. pr_debug("%s: Map handle is 0, nothing to unmap\n",
  2932. __func__);
  2933. goto done;
  2934. }
  2935. if (*mem_map_handle != atomic_read(
  2936. &this_adm.mem_map_handles[ADM_RTAC_APR_CAL])) {
  2937. pr_err("%s: Map handles do not match! Unmapping RTAC, RTAC map 0x%x, ADM map 0x%x\n",
  2938. __func__, *mem_map_handle, atomic_read(
  2939. &this_adm.mem_map_handles[ADM_RTAC_APR_CAL]));
  2940. /* if mismatch use handle passed in to unmap */
  2941. atomic_set(&this_adm.mem_map_handles[ADM_RTAC_APR_CAL],
  2942. *mem_map_handle);
  2943. }
  2944. /* valid port ID needed for callback use primary I2S */
  2945. atomic_set(&this_adm.mem_map_index, ADM_RTAC_APR_CAL);
  2946. result = adm_memory_unmap_regions();
  2947. if (result < 0) {
  2948. pr_debug("%s: adm_memory_unmap_regions failed, error %d\n",
  2949. __func__, result);
  2950. } else {
  2951. atomic_set(&this_adm.mem_map_handles[ADM_RTAC_APR_CAL], 0);
  2952. *mem_map_handle = 0;
  2953. }
  2954. done:
  2955. return result;
  2956. }
  2957. static int get_cal_type_index(int32_t cal_type)
  2958. {
  2959. int ret = -EINVAL;
  2960. switch (cal_type) {
  2961. case ADM_AUDPROC_CAL_TYPE:
  2962. ret = ADM_AUDPROC_CAL;
  2963. break;
  2964. case ADM_LSM_AUDPROC_CAL_TYPE:
  2965. ret = ADM_LSM_AUDPROC_CAL;
  2966. break;
  2967. case ADM_AUDVOL_CAL_TYPE:
  2968. ret = ADM_AUDVOL_CAL;
  2969. break;
  2970. case ADM_CUST_TOPOLOGY_CAL_TYPE:
  2971. ret = ADM_CUSTOM_TOP_CAL;
  2972. break;
  2973. case ADM_RTAC_INFO_CAL_TYPE:
  2974. ret = ADM_RTAC_INFO_CAL;
  2975. break;
  2976. case ADM_RTAC_APR_CAL_TYPE:
  2977. ret = ADM_RTAC_APR_CAL;
  2978. break;
  2979. case ADM_RTAC_AUDVOL_CAL_TYPE:
  2980. ret = ADM_RTAC_AUDVOL_CAL;
  2981. break;
  2982. case ADM_LSM_AUDPROC_PERSISTENT_CAL_TYPE:
  2983. ret = ADM_LSM_AUDPROC_PERSISTENT_CAL;
  2984. break;
  2985. default:
  2986. pr_err("%s: invalid cal type %d!\n", __func__, cal_type);
  2987. }
  2988. return ret;
  2989. }
  2990. static int adm_alloc_cal(int32_t cal_type, size_t data_size, void *data)
  2991. {
  2992. int ret = 0;
  2993. int cal_index;
  2994. pr_debug("%s:\n", __func__);
  2995. cal_index = get_cal_type_index(cal_type);
  2996. if (cal_index < 0) {
  2997. pr_err("%s: could not get cal index %d!\n",
  2998. __func__, cal_index);
  2999. ret = -EINVAL;
  3000. goto done;
  3001. }
  3002. ret = cal_utils_alloc_cal(data_size, data,
  3003. this_adm.cal_data[cal_index], 0, NULL);
  3004. if (ret < 0) {
  3005. pr_err("%s: cal_utils_alloc_block failed, ret = %d, cal type = %d!\n",
  3006. __func__, ret, cal_type);
  3007. ret = -EINVAL;
  3008. goto done;
  3009. }
  3010. done:
  3011. return ret;
  3012. }
  3013. static int adm_dealloc_cal(int32_t cal_type, size_t data_size, void *data)
  3014. {
  3015. int ret = 0;
  3016. int cal_index;
  3017. pr_debug("%s:\n", __func__);
  3018. cal_index = get_cal_type_index(cal_type);
  3019. if (cal_index < 0) {
  3020. pr_err("%s: could not get cal index %d!\n",
  3021. __func__, cal_index);
  3022. ret = -EINVAL;
  3023. goto done;
  3024. }
  3025. ret = cal_utils_dealloc_cal(data_size, data,
  3026. this_adm.cal_data[cal_index]);
  3027. if (ret < 0) {
  3028. pr_err("%s: cal_utils_dealloc_block failed, ret = %d, cal type = %d!\n",
  3029. __func__, ret, cal_type);
  3030. ret = -EINVAL;
  3031. goto done;
  3032. }
  3033. done:
  3034. return ret;
  3035. }
  3036. static int adm_set_cal(int32_t cal_type, size_t data_size, void *data)
  3037. {
  3038. int ret = 0;
  3039. int cal_index;
  3040. pr_debug("%s:\n", __func__);
  3041. cal_index = get_cal_type_index(cal_type);
  3042. if (cal_index < 0) {
  3043. pr_err("%s: could not get cal index %d!\n",
  3044. __func__, cal_index);
  3045. ret = -EINVAL;
  3046. goto done;
  3047. }
  3048. ret = cal_utils_set_cal(data_size, data,
  3049. this_adm.cal_data[cal_index], 0, NULL);
  3050. if (ret < 0) {
  3051. pr_err("%s: cal_utils_set_cal failed, ret = %d, cal type = %d!\n",
  3052. __func__, ret, cal_type);
  3053. ret = -EINVAL;
  3054. goto done;
  3055. }
  3056. if (cal_index == ADM_CUSTOM_TOP_CAL) {
  3057. mutex_lock(&this_adm.cal_data[ADM_CUSTOM_TOP_CAL]->lock);
  3058. this_adm.set_custom_topology = 1;
  3059. mutex_unlock(&this_adm.cal_data[ADM_CUSTOM_TOP_CAL]->lock);
  3060. } else if (cal_index == ADM_RTAC_AUDVOL_CAL) {
  3061. send_rtac_audvol_cal();
  3062. }
  3063. done:
  3064. return ret;
  3065. }
  3066. static int adm_map_cal_data(int32_t cal_type,
  3067. struct cal_block_data *cal_block)
  3068. {
  3069. int ret = 0;
  3070. int cal_index;
  3071. pr_debug("%s:\n", __func__);
  3072. cal_index = get_cal_type_index(cal_type);
  3073. if (cal_index < 0) {
  3074. pr_err("%s: could not get cal index %d!\n",
  3075. __func__, cal_index);
  3076. ret = -EINVAL;
  3077. goto done;
  3078. }
  3079. atomic_set(&this_adm.mem_map_index, cal_index);
  3080. ret = adm_memory_map_regions(&cal_block->cal_data.paddr, 0,
  3081. (uint32_t *)&cal_block->map_data.map_size, 1);
  3082. if (ret < 0) {
  3083. pr_err("%s: map did not work! cal_type %i ret %d\n",
  3084. __func__, cal_index, ret);
  3085. ret = -ENODEV;
  3086. goto done;
  3087. }
  3088. cal_block->map_data.q6map_handle = atomic_read(&this_adm.
  3089. mem_map_handles[cal_index]);
  3090. done:
  3091. return ret;
  3092. }
  3093. static int adm_unmap_cal_data(int32_t cal_type,
  3094. struct cal_block_data *cal_block)
  3095. {
  3096. int ret = 0;
  3097. int cal_index;
  3098. pr_debug("%s:\n", __func__);
  3099. cal_index = get_cal_type_index(cal_type);
  3100. if (cal_index < 0) {
  3101. pr_err("%s: could not get cal index %d!\n",
  3102. __func__, cal_index);
  3103. ret = -EINVAL;
  3104. goto done;
  3105. }
  3106. if (cal_block == NULL) {
  3107. pr_err("%s: Cal block is NULL!\n",
  3108. __func__);
  3109. goto done;
  3110. }
  3111. if (cal_block->map_data.q6map_handle == 0) {
  3112. pr_err("%s: Map handle is NULL, nothing to unmap\n",
  3113. __func__);
  3114. goto done;
  3115. }
  3116. atomic_set(&this_adm.mem_map_handles[cal_index],
  3117. cal_block->map_data.q6map_handle);
  3118. atomic_set(&this_adm.mem_map_index, cal_index);
  3119. ret = adm_memory_unmap_regions();
  3120. if (ret < 0) {
  3121. pr_err("%s: unmap did not work! cal_type %i ret %d\n",
  3122. __func__, cal_index, ret);
  3123. ret = -ENODEV;
  3124. goto done;
  3125. }
  3126. cal_block->map_data.q6map_handle = 0;
  3127. done:
  3128. return ret;
  3129. }
  3130. static void adm_delete_cal_data(void)
  3131. {
  3132. pr_debug("%s:\n", __func__);
  3133. cal_utils_destroy_cal_types(ADM_MAX_CAL_TYPES, this_adm.cal_data);
  3134. }
  3135. static int adm_init_cal_data(void)
  3136. {
  3137. int ret = 0;
  3138. struct cal_type_info cal_type_info[] = {
  3139. {{ADM_CUST_TOPOLOGY_CAL_TYPE,
  3140. {adm_alloc_cal, adm_dealloc_cal, NULL,
  3141. adm_set_cal, NULL, NULL} },
  3142. {adm_map_cal_data, adm_unmap_cal_data,
  3143. cal_utils_match_buf_num} },
  3144. {{ADM_AUDPROC_CAL_TYPE,
  3145. {adm_alloc_cal, adm_dealloc_cal, NULL,
  3146. adm_set_cal, NULL, NULL} },
  3147. {adm_map_cal_data, adm_unmap_cal_data,
  3148. cal_utils_match_buf_num} },
  3149. {{ADM_LSM_AUDPROC_CAL_TYPE,
  3150. {adm_alloc_cal, adm_dealloc_cal, NULL,
  3151. adm_set_cal, NULL, NULL} },
  3152. {adm_map_cal_data, adm_unmap_cal_data,
  3153. cal_utils_match_buf_num} },
  3154. {{ADM_AUDVOL_CAL_TYPE,
  3155. {adm_alloc_cal, adm_dealloc_cal, NULL,
  3156. adm_set_cal, NULL, NULL} },
  3157. {adm_map_cal_data, adm_unmap_cal_data,
  3158. cal_utils_match_buf_num} },
  3159. {{ADM_RTAC_INFO_CAL_TYPE,
  3160. {NULL, NULL, NULL, NULL, NULL, NULL} },
  3161. {NULL, NULL, cal_utils_match_buf_num} },
  3162. {{ADM_RTAC_APR_CAL_TYPE,
  3163. {NULL, NULL, NULL, NULL, NULL, NULL} },
  3164. {NULL, NULL, cal_utils_match_buf_num} },
  3165. {{SRS_TRUMEDIA_CAL_TYPE,
  3166. {NULL, NULL, NULL, NULL, NULL, NULL} },
  3167. {NULL, NULL, cal_utils_match_buf_num} },
  3168. {{ADM_RTAC_AUDVOL_CAL_TYPE,
  3169. {adm_alloc_cal, adm_dealloc_cal, NULL,
  3170. adm_set_cal, NULL, NULL} },
  3171. {adm_map_cal_data, adm_unmap_cal_data,
  3172. cal_utils_match_buf_num} },
  3173. {{ADM_LSM_AUDPROC_PERSISTENT_CAL_TYPE,
  3174. {adm_alloc_cal, adm_dealloc_cal, NULL,
  3175. adm_set_cal, NULL, NULL} },
  3176. {adm_map_cal_data, adm_unmap_cal_data,
  3177. cal_utils_match_buf_num} },
  3178. };
  3179. pr_debug("%s:\n", __func__);
  3180. ret = cal_utils_create_cal_types(ADM_MAX_CAL_TYPES, this_adm.cal_data,
  3181. cal_type_info);
  3182. if (ret < 0) {
  3183. pr_err("%s: could not create cal type! ret %d\n",
  3184. __func__, ret);
  3185. ret = -EINVAL;
  3186. goto err;
  3187. }
  3188. return ret;
  3189. err:
  3190. adm_delete_cal_data();
  3191. return ret;
  3192. }
  3193. /**
  3194. * adm_set_volume -
  3195. * command to set volume on ADM copp
  3196. *
  3197. * @port_id: Port ID number
  3198. * @copp_idx: copp index assigned
  3199. * @volume: gain value to set
  3200. *
  3201. * Returns 0 on success or error on failure
  3202. */
  3203. int adm_set_volume(int port_id, int copp_idx, int volume)
  3204. {
  3205. struct audproc_volume_ctrl_master_gain audproc_vol;
  3206. struct param_hdr_v3 param_hdr;
  3207. int rc = 0;
  3208. pr_debug("%s: port_id %d, volume %d\n", __func__, port_id, volume);
  3209. memset(&audproc_vol, 0, sizeof(audproc_vol));
  3210. memset(&param_hdr, 0, sizeof(param_hdr));
  3211. param_hdr.module_id = AUDPROC_MODULE_ID_VOL_CTRL;
  3212. param_hdr.instance_id = INSTANCE_ID_0;
  3213. param_hdr.param_id = AUDPROC_PARAM_ID_VOL_CTRL_MASTER_GAIN;
  3214. param_hdr.param_size = sizeof(audproc_vol);
  3215. audproc_vol.master_gain = volume;
  3216. rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
  3217. (uint8_t *) &audproc_vol);
  3218. if (rc)
  3219. pr_err("%s: Failed to set volume, err %d\n", __func__, rc);
  3220. return rc;
  3221. }
  3222. EXPORT_SYMBOL(adm_set_volume);
  3223. /**
  3224. * adm_set_softvolume -
  3225. * command to set softvolume
  3226. *
  3227. * @port_id: Port ID number
  3228. * @copp_idx: copp index assigned
  3229. * @softvol_param: Params to set for softvolume
  3230. *
  3231. * Returns 0 on success or error on failure
  3232. */
  3233. int adm_set_softvolume(int port_id, int copp_idx,
  3234. struct audproc_softvolume_params *softvol_param)
  3235. {
  3236. struct audproc_soft_step_volume_params audproc_softvol;
  3237. struct param_hdr_v3 param_hdr;
  3238. int rc = 0;
  3239. pr_debug("%s: period %d step %d curve %d\n", __func__,
  3240. softvol_param->period, softvol_param->step,
  3241. softvol_param->rampingcurve);
  3242. memset(&audproc_softvol, 0, sizeof(audproc_softvol));
  3243. memset(&param_hdr, 0, sizeof(param_hdr));
  3244. param_hdr.module_id = AUDPROC_MODULE_ID_VOL_CTRL;
  3245. param_hdr.instance_id = INSTANCE_ID_0;
  3246. param_hdr.param_id = AUDPROC_PARAM_ID_SOFT_VOL_STEPPING_PARAMETERS;
  3247. param_hdr.param_size = sizeof(audproc_softvol);
  3248. audproc_softvol.period = softvol_param->period;
  3249. audproc_softvol.step = softvol_param->step;
  3250. audproc_softvol.ramping_curve = softvol_param->rampingcurve;
  3251. pr_debug("%s: period %d, step %d, curve %d\n", __func__,
  3252. audproc_softvol.period, audproc_softvol.step,
  3253. audproc_softvol.ramping_curve);
  3254. rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
  3255. (uint8_t *) &audproc_softvol);
  3256. if (rc)
  3257. pr_err("%s: Failed to set soft volume, err %d\n", __func__, rc);
  3258. return rc;
  3259. }
  3260. EXPORT_SYMBOL(adm_set_softvolume);
  3261. /**
  3262. * adm_set_mic_gain -
  3263. * command to set MIC gain
  3264. *
  3265. * @port_id: Port ID number
  3266. * @copp_idx: copp index assigned
  3267. * @volume: gain value to set
  3268. *
  3269. * Returns 0 on success or error on failure
  3270. */
  3271. int adm_set_mic_gain(int port_id, int copp_idx, int volume)
  3272. {
  3273. struct admx_mic_gain mic_gain_params;
  3274. struct param_hdr_v3 param_hdr;
  3275. int rc = 0;
  3276. pr_debug("%s: Setting mic gain to %d at port_id 0x%x\n", __func__,
  3277. volume, port_id);
  3278. memset(&mic_gain_params, 0, sizeof(mic_gain_params));
  3279. memset(&param_hdr, 0, sizeof(param_hdr));
  3280. param_hdr.module_id = ADM_MODULE_IDX_MIC_GAIN_CTRL;
  3281. param_hdr.instance_id = INSTANCE_ID_0;
  3282. param_hdr.param_id = ADM_PARAM_IDX_MIC_GAIN;
  3283. param_hdr.param_size = sizeof(mic_gain_params);
  3284. mic_gain_params.tx_mic_gain = volume;
  3285. rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
  3286. (uint8_t *) &mic_gain_params);
  3287. if (rc)
  3288. pr_err("%s: Failed to set mic gain, err %d\n", __func__, rc);
  3289. return rc;
  3290. }
  3291. EXPORT_SYMBOL(adm_set_mic_gain);
  3292. /**
  3293. * adm_send_set_multichannel_ec_primary_mic_ch -
  3294. * command to set multi-ch EC primary mic
  3295. *
  3296. * @port_id: Port ID number
  3297. * @copp_idx: copp index assigned
  3298. * @primary_mic_ch: channel number of primary mic
  3299. *
  3300. * Returns 0 on success or error on failure
  3301. */
  3302. int adm_send_set_multichannel_ec_primary_mic_ch(int port_id, int copp_idx,
  3303. int primary_mic_ch)
  3304. {
  3305. struct admx_sec_primary_mic_ch sec_primary_ch_params;
  3306. struct param_hdr_v3 param_hdr;
  3307. int rc = 0;
  3308. pr_debug("%s port_id 0x%x, copp_idx 0x%x, primary_mic_ch %d\n",
  3309. __func__, port_id, copp_idx, primary_mic_ch);
  3310. memset(&sec_primary_ch_params, 0, sizeof(sec_primary_ch_params));
  3311. memset(&param_hdr, 0, sizeof(param_hdr));
  3312. param_hdr.module_id = AUDPROC_MODULE_ID_VOICE_TX_SECNS;
  3313. param_hdr.instance_id = INSTANCE_ID_0;
  3314. param_hdr.param_id = AUDPROC_PARAM_IDX_SEC_PRIMARY_MIC_CH;
  3315. param_hdr.param_size = sizeof(sec_primary_ch_params);
  3316. sec_primary_ch_params.version = 0;
  3317. sec_primary_ch_params.sec_primary_mic_ch = primary_mic_ch;
  3318. rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
  3319. (uint8_t *) &sec_primary_ch_params);
  3320. if (rc)
  3321. pr_err("%s: Failed to set primary mic chanel, err %d\n",
  3322. __func__, rc);
  3323. return rc;
  3324. }
  3325. EXPORT_SYMBOL(adm_send_set_multichannel_ec_primary_mic_ch);
  3326. /**
  3327. * adm_param_enable -
  3328. * command to send params to ADM for given module
  3329. *
  3330. * @port_id: Port ID number
  3331. * @copp_idx: copp index assigned
  3332. * @module_id: ADM module
  3333. * @enable: flag to enable or disable module
  3334. *
  3335. * Returns 0 on success or error on failure
  3336. */
  3337. int adm_param_enable(int port_id, int copp_idx, int module_id, int enable)
  3338. {
  3339. struct module_instance_info mod_inst_info;
  3340. memset(&mod_inst_info, 0, sizeof(mod_inst_info));
  3341. mod_inst_info.module_id = module_id;
  3342. mod_inst_info.instance_id = INSTANCE_ID_0;
  3343. return adm_param_enable_v2(port_id, copp_idx, mod_inst_info, enable);
  3344. }
  3345. EXPORT_SYMBOL(adm_param_enable);
  3346. /**
  3347. * adm_param_enable_v2 -
  3348. * command to send params to ADM for given module
  3349. *
  3350. * @port_id: Port ID number
  3351. * @copp_idx: copp index assigned
  3352. * @mod_inst_info: module and instance ID info
  3353. * @enable: flag to enable or disable module
  3354. *
  3355. * Returns 0 on success or error on failure
  3356. */
  3357. int adm_param_enable_v2(int port_id, int copp_idx,
  3358. struct module_instance_info mod_inst_info, int enable)
  3359. {
  3360. uint32_t enable_param;
  3361. struct param_hdr_v3 param_hdr;
  3362. int rc = 0;
  3363. if (enable < 0 || enable > 1) {
  3364. pr_err("%s: Invalid value for enable %d\n", __func__, enable);
  3365. return -EINVAL;
  3366. }
  3367. pr_debug("%s port_id %d, module_id 0x%x, instance_id 0x%x, enable %d\n",
  3368. __func__, port_id, mod_inst_info.module_id,
  3369. mod_inst_info.instance_id, enable);
  3370. memset(&param_hdr, 0, sizeof(param_hdr));
  3371. param_hdr.module_id = mod_inst_info.module_id;
  3372. param_hdr.instance_id = mod_inst_info.instance_id;
  3373. param_hdr.param_id = AUDPROC_PARAM_ID_ENABLE;
  3374. param_hdr.param_size = sizeof(enable_param);
  3375. enable_param = enable;
  3376. rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
  3377. (uint8_t *) &enable_param);
  3378. if (rc)
  3379. pr_err("%s: Failed to set enable of module(%d) instance(%d) to %d, err %d\n",
  3380. __func__, mod_inst_info.module_id,
  3381. mod_inst_info.instance_id, enable, rc);
  3382. return rc;
  3383. }
  3384. EXPORT_SYMBOL(adm_param_enable_v2);
  3385. /**
  3386. * adm_send_calibration -
  3387. * send ADM calibration to DSP
  3388. *
  3389. * @port_id: Port ID number
  3390. * @copp_idx: copp index assigned
  3391. * @path: direction or ADM path type
  3392. * @perf_mode: performance mode like LL/ULL/..
  3393. * @cal_type: calibration type to use
  3394. * @params: pointer with cal data
  3395. * @size: cal size
  3396. *
  3397. * Returns 0 on success or error on failure
  3398. */
  3399. int adm_send_calibration(int port_id, int copp_idx, int path, int perf_mode,
  3400. int cal_type, char *params, int size)
  3401. {
  3402. int rc = 0;
  3403. pr_debug("%s:port_id %d, path %d, perf_mode %d, cal_type %d, size %d\n",
  3404. __func__, port_id, path, perf_mode, cal_type, size);
  3405. /* Maps audio_dev_ctrl path definition to ACDB definition */
  3406. if (get_cal_path(path) != RX_DEVICE) {
  3407. pr_err("%s: acdb_path %d\n", __func__, path);
  3408. rc = -EINVAL;
  3409. goto end;
  3410. }
  3411. rc = adm_set_pp_params(port_id, copp_idx, NULL, (u8 *) params, size);
  3412. end:
  3413. return rc;
  3414. }
  3415. EXPORT_SYMBOL(adm_send_calibration);
  3416. /*
  3417. * adm_update_wait_parameters must be called with routing driver locks.
  3418. * adm_reset_wait_parameters must be called with routing driver locks.
  3419. * set and reset parmeters are separated to make sure it is always called
  3420. * under routing driver lock.
  3421. * adm_wait_timeout is to block until timeout or interrupted. Timeout is
  3422. * not a an error.
  3423. */
  3424. int adm_set_wait_parameters(int port_id, int copp_idx)
  3425. {
  3426. int ret = 0, port_idx;
  3427. pr_debug("%s: port_id 0x%x, copp_idx %d\n", __func__, port_id,
  3428. copp_idx);
  3429. port_id = afe_convert_virtual_to_portid(port_id);
  3430. port_idx = adm_validate_and_get_port_index(port_id);
  3431. if (port_idx < 0) {
  3432. pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
  3433. ret = -EINVAL;
  3434. goto end;
  3435. }
  3436. if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
  3437. pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
  3438. return -EINVAL;
  3439. }
  3440. this_adm.copp.adm_delay[port_idx][copp_idx] = 1;
  3441. atomic_set(&this_adm.copp.adm_delay_stat[port_idx][copp_idx], 0);
  3442. end:
  3443. return ret;
  3444. }
  3445. EXPORT_SYMBOL(adm_set_wait_parameters);
  3446. /**
  3447. * adm_reset_wait_parameters -
  3448. * reset wait parameters or ADM delay value
  3449. *
  3450. * @port_id: Port ID number
  3451. * @copp_idx: copp index assigned
  3452. *
  3453. * Returns 0 on success or error on failure
  3454. */
  3455. int adm_reset_wait_parameters(int port_id, int copp_idx)
  3456. {
  3457. int ret = 0, port_idx;
  3458. pr_debug("%s: port_id 0x%x copp_idx %d\n", __func__, port_id,
  3459. copp_idx);
  3460. port_id = afe_convert_virtual_to_portid(port_id);
  3461. port_idx = adm_validate_and_get_port_index(port_id);
  3462. if (port_idx < 0) {
  3463. pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
  3464. ret = -EINVAL;
  3465. goto end;
  3466. }
  3467. if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
  3468. pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
  3469. return -EINVAL;
  3470. }
  3471. atomic_set(&this_adm.copp.adm_delay_stat[port_idx][copp_idx], 1);
  3472. this_adm.copp.adm_delay[port_idx][copp_idx] = 0;
  3473. end:
  3474. return ret;
  3475. }
  3476. EXPORT_SYMBOL(adm_reset_wait_parameters);
  3477. /**
  3478. * adm_wait_timeout -
  3479. * ADM wait command after command send to DSP
  3480. *
  3481. * @port_id: Port ID number
  3482. * @copp_idx: copp index assigned
  3483. * @wait_time: value in ms for command timeout
  3484. *
  3485. * Returns 0 on success or error on failure
  3486. */
  3487. int adm_wait_timeout(int port_id, int copp_idx, int wait_time)
  3488. {
  3489. int ret = 0, port_idx;
  3490. pr_debug("%s: port_id 0x%x, copp_idx %d, wait_time %d\n", __func__,
  3491. port_id, copp_idx, wait_time);
  3492. port_id = afe_convert_virtual_to_portid(port_id);
  3493. port_idx = adm_validate_and_get_port_index(port_id);
  3494. if (port_idx < 0) {
  3495. pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
  3496. ret = -EINVAL;
  3497. goto end;
  3498. }
  3499. if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
  3500. pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
  3501. return -EINVAL;
  3502. }
  3503. ret = wait_event_timeout(
  3504. this_adm.copp.adm_delay_wait[port_idx][copp_idx],
  3505. atomic_read(&this_adm.copp.adm_delay_stat[port_idx][copp_idx]),
  3506. msecs_to_jiffies(wait_time));
  3507. pr_debug("%s: return %d\n", __func__, ret);
  3508. if (ret != 0)
  3509. ret = -EINTR;
  3510. end:
  3511. pr_debug("%s: return %d--\n", __func__, ret);
  3512. return ret;
  3513. }
  3514. EXPORT_SYMBOL(adm_wait_timeout);
  3515. /**
  3516. * adm_store_cal_data -
  3517. * Retrieve calibration data for ADM copp device
  3518. *
  3519. * @port_id: Port ID number
  3520. * @copp_idx: copp index assigned
  3521. * @path: direction or copp type
  3522. * @perf_mode: performance mode like LL/ULL/..
  3523. * @cal_index: calibration index to use
  3524. * @params: pointer to store cal data
  3525. * @size: pointer to fill with cal size
  3526. *
  3527. * Returns 0 on success or error on failure
  3528. */
  3529. int adm_store_cal_data(int port_id, int copp_idx, int path, int perf_mode,
  3530. int cal_index, char *params, int *size)
  3531. {
  3532. int rc = 0;
  3533. struct cal_block_data *cal_block = NULL;
  3534. int app_type, acdb_id, port_idx, sample_rate;
  3535. if (this_adm.cal_data[cal_index] == NULL) {
  3536. pr_debug("%s: cal_index %d not allocated!\n",
  3537. __func__, cal_index);
  3538. goto end;
  3539. }
  3540. if (get_cal_path(path) != RX_DEVICE) {
  3541. pr_debug("%s: Invalid path to store calibration %d\n",
  3542. __func__, path);
  3543. rc = -EINVAL;
  3544. goto end;
  3545. }
  3546. port_id = afe_convert_virtual_to_portid(port_id);
  3547. port_idx = adm_validate_and_get_port_index(port_id);
  3548. if (port_idx < 0) {
  3549. pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
  3550. rc = -EINVAL;
  3551. goto end;
  3552. }
  3553. if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
  3554. pr_err("%s: Invalid copp_num: %d\n", __func__, copp_idx);
  3555. return -EINVAL;
  3556. }
  3557. acdb_id = atomic_read(&this_adm.copp.acdb_id[port_idx][copp_idx]);
  3558. app_type = atomic_read(&this_adm.copp.app_type[port_idx][copp_idx]);
  3559. sample_rate = atomic_read(&this_adm.copp.rate[port_idx][copp_idx]);
  3560. mutex_lock(&this_adm.cal_data[cal_index]->lock);
  3561. cal_block = adm_find_cal(cal_index, get_cal_path(path), app_type,
  3562. acdb_id, sample_rate);
  3563. if (cal_block == NULL)
  3564. goto unlock;
  3565. if (cal_block->cal_data.size <= 0) {
  3566. pr_debug("%s: No ADM cal send for port_id = 0x%x!\n",
  3567. __func__, port_id);
  3568. rc = -EINVAL;
  3569. goto unlock;
  3570. }
  3571. if (cal_index == ADM_AUDPROC_CAL || cal_index == ADM_LSM_AUDPROC_CAL) {
  3572. if (cal_block->cal_data.size > AUD_PROC_BLOCK_SIZE) {
  3573. pr_err("%s:audproc:invalid size exp/actual[%zd, %d]\n",
  3574. __func__, cal_block->cal_data.size, *size);
  3575. rc = -ENOMEM;
  3576. goto unlock;
  3577. }
  3578. } else if (cal_index == ADM_LSM_AUDPROC_PERSISTENT_CAL) {
  3579. if (cal_block->cal_data.size > AUD_PROC_PERSIST_BLOCK_SIZE) {
  3580. pr_err("%s:persist invalid size exp/actual[%zd, %d]\n",
  3581. __func__, cal_block->cal_data.size, *size);
  3582. rc = -ENOMEM;
  3583. goto unlock;
  3584. }
  3585. } else if (cal_index == ADM_AUDVOL_CAL) {
  3586. if (cal_block->cal_data.size > AUD_VOL_BLOCK_SIZE) {
  3587. pr_err("%s:aud_vol:invalid size exp/actual[%zd, %d]\n",
  3588. __func__, cal_block->cal_data.size, *size);
  3589. rc = -ENOMEM;
  3590. goto unlock;
  3591. }
  3592. } else {
  3593. pr_debug("%s: Not valid calibration for dolby topolgy\n",
  3594. __func__);
  3595. rc = -EINVAL;
  3596. goto unlock;
  3597. }
  3598. memcpy(params, cal_block->cal_data.kvaddr, cal_block->cal_data.size);
  3599. *size = cal_block->cal_data.size;
  3600. pr_debug("%s:port_id %d, copp_idx %d, path %d",
  3601. __func__, port_id, copp_idx, path);
  3602. pr_debug("perf_mode %d, cal_type %d, size %d\n",
  3603. perf_mode, cal_index, *size);
  3604. unlock:
  3605. mutex_unlock(&this_adm.cal_data[cal_index]->lock);
  3606. end:
  3607. return rc;
  3608. }
  3609. EXPORT_SYMBOL(adm_store_cal_data);
  3610. /**
  3611. * adm_send_compressed_device_mute -
  3612. * command to send mute for compressed device
  3613. *
  3614. * @port_id: Port ID number
  3615. * @copp_idx: copp index assigned
  3616. * @mute_on: flag to indicate mute or unmute
  3617. *
  3618. * Returns 0 on success or error on failure
  3619. */
  3620. int adm_send_compressed_device_mute(int port_id, int copp_idx, bool mute_on)
  3621. {
  3622. u32 mute_param = mute_on ? 1 : 0;
  3623. struct param_hdr_v3 param_hdr;
  3624. int ret = 0;
  3625. pr_debug("%s port_id: 0x%x, copp_idx %d, mute_on: %d\n",
  3626. __func__, port_id, copp_idx, mute_on);
  3627. memset(&param_hdr, 0, sizeof(param_hdr));
  3628. param_hdr.module_id = AUDPROC_MODULE_ID_COMPRESSED_MUTE;
  3629. param_hdr.instance_id = INSTANCE_ID_0;
  3630. param_hdr.param_id = AUDPROC_PARAM_ID_COMPRESSED_MUTE;
  3631. param_hdr.param_size = sizeof(mute_param);
  3632. ret = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
  3633. (uint8_t *) &mute_param);
  3634. if (ret)
  3635. pr_err("%s: Failed to set mute, err %d\n", __func__, ret);
  3636. return ret;
  3637. }
  3638. EXPORT_SYMBOL(adm_send_compressed_device_mute);
  3639. /**
  3640. * adm_send_compressed_device_latency -
  3641. * command to send latency for compressed device
  3642. *
  3643. * @port_id: Port ID number
  3644. * @copp_idx: copp index assigned
  3645. * @latency: latency value to pass
  3646. *
  3647. * Returns 0 on success or error on failure
  3648. */
  3649. int adm_send_compressed_device_latency(int port_id, int copp_idx, int latency)
  3650. {
  3651. u32 latency_param;
  3652. struct param_hdr_v3 param_hdr;
  3653. int ret = 0;
  3654. pr_debug("%s port_id: 0x%x, copp_idx %d latency: %d\n", __func__,
  3655. port_id, copp_idx, latency);
  3656. if (latency < 0) {
  3657. pr_err("%s: Invalid value for latency %d", __func__, latency);
  3658. return -EINVAL;
  3659. }
  3660. memset(&param_hdr, 0, sizeof(param_hdr));
  3661. param_hdr.module_id = AUDPROC_MODULE_ID_COMPRESSED_LATENCY;
  3662. param_hdr.instance_id = INSTANCE_ID_0;
  3663. param_hdr.param_id = AUDPROC_PARAM_ID_COMPRESSED_LATENCY;
  3664. param_hdr.param_size = sizeof(latency_param);
  3665. latency_param = latency;
  3666. ret = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
  3667. (uint8_t *) &latency_param);
  3668. if (ret)
  3669. pr_err("%s: Failed to set latency, err %d\n", __func__, ret);
  3670. return ret;
  3671. }
  3672. EXPORT_SYMBOL(adm_send_compressed_device_latency);
  3673. /**
  3674. * adm_swap_speaker_channels
  3675. *
  3676. * Receives port_id, copp_idx, sample rate, spk_swap and
  3677. * send MFC command to swap speaker channel.
  3678. * Return zero on success. On failure returns nonzero.
  3679. *
  3680. * port_id - Passed value, port_id for which channels swap is wanted
  3681. * copp_idx - Passed value, copp_idx for which channels swap is wanted
  3682. * sample_rate - Passed value, sample rate used by app type config
  3683. * spk_swap - Passed value, spk_swap for check if swap flag is set
  3684. */
  3685. int adm_swap_speaker_channels(int port_id, int copp_idx,
  3686. int sample_rate, bool spk_swap)
  3687. {
  3688. struct audproc_mfc_param_media_fmt mfc_cfg;
  3689. struct param_hdr_v3 param_hdr;
  3690. uint16_t num_channels;
  3691. int port_idx = 0;
  3692. int ret = 0;
  3693. pr_debug("%s: Enter, port_id %d, copp_idx %d\n",
  3694. __func__, port_id, copp_idx);
  3695. port_id = q6audio_convert_virtual_to_portid(port_id);
  3696. port_idx = adm_validate_and_get_port_index(port_id);
  3697. if (port_idx < 0 || port_idx >= AFE_MAX_PORTS) {
  3698. pr_err("%s: Invalid port_id %#x\n", __func__, port_id);
  3699. return -EINVAL;
  3700. } else if (copp_idx < 0 || copp_idx >= MAX_COPPS_PER_PORT) {
  3701. pr_err("%s: Invalid copp_idx 0x%x\n", __func__, copp_idx);
  3702. return -EINVAL;
  3703. }
  3704. num_channels = atomic_read(&this_adm.copp.channels[port_idx][copp_idx]);
  3705. if (num_channels != 2) {
  3706. pr_debug("%s: Invalid number of channels: %d\n",
  3707. __func__, num_channels);
  3708. return -EINVAL;
  3709. }
  3710. memset(&mfc_cfg, 0, sizeof(mfc_cfg));
  3711. memset(&param_hdr, 0, sizeof(param_hdr));
  3712. param_hdr.module_id = AUDPROC_MODULE_ID_MFC;
  3713. param_hdr.instance_id = INSTANCE_ID_0;
  3714. param_hdr.param_id = AUDPROC_PARAM_ID_MFC_OUTPUT_MEDIA_FORMAT;
  3715. param_hdr.param_size = sizeof(mfc_cfg);
  3716. mfc_cfg.sampling_rate = sample_rate;
  3717. mfc_cfg.bits_per_sample =
  3718. atomic_read(&this_adm.copp.bit_width[port_idx][copp_idx]);
  3719. mfc_cfg.num_channels = num_channels;
  3720. /* Currently applying speaker swap for only 2 channel use case */
  3721. if (spk_swap) {
  3722. mfc_cfg.channel_type[0] =
  3723. (uint16_t) PCM_CHANNEL_FR;
  3724. mfc_cfg.channel_type[1] =
  3725. (uint16_t) PCM_CHANNEL_FL;
  3726. } else {
  3727. mfc_cfg.channel_type[0] =
  3728. (uint16_t) PCM_CHANNEL_FL;
  3729. mfc_cfg.channel_type[1] =
  3730. (uint16_t) PCM_CHANNEL_FR;
  3731. }
  3732. ret = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
  3733. (u8 *) &mfc_cfg);
  3734. if (ret < 0) {
  3735. pr_err("%s: Failed to set swap speaker channels on port[0x%x] failed %d\n",
  3736. __func__, port_id, ret);
  3737. return ret;
  3738. }
  3739. pr_debug("%s: mfc_cfg Set params returned success", __func__);
  3740. return 0;
  3741. }
  3742. EXPORT_SYMBOL(adm_swap_speaker_channels);
  3743. /**
  3744. * adm_set_sound_focus -
  3745. * Update sound focus info
  3746. *
  3747. * @port_id: Port ID number
  3748. * @copp_idx: copp index assigned
  3749. * @soundFocusData: sound focus data to pass
  3750. *
  3751. * Returns 0 on success or error on failure
  3752. */
  3753. int adm_set_sound_focus(int port_id, int copp_idx,
  3754. struct sound_focus_param soundFocusData)
  3755. {
  3756. struct adm_param_fluence_soundfocus_t soundfocus_params;
  3757. struct param_hdr_v3 param_hdr;
  3758. int ret = 0;
  3759. int i;
  3760. pr_debug("%s: Enter, port_id %d, copp_idx %d\n",
  3761. __func__, port_id, copp_idx);
  3762. memset(&param_hdr, 0, sizeof(param_hdr));
  3763. param_hdr.module_id = VOICEPROC_MODULE_ID_GENERIC_TX;
  3764. param_hdr.instance_id = INSTANCE_ID_0;
  3765. param_hdr.param_id = VOICEPROC_PARAM_ID_FLUENCE_SOUNDFOCUS;
  3766. param_hdr.param_size = sizeof(soundfocus_params);
  3767. memset(&(soundfocus_params), 0xFF, sizeof(soundfocus_params));
  3768. for (i = 0; i < MAX_SECTORS; i++) {
  3769. soundfocus_params.start_angles[i] =
  3770. soundFocusData.start_angle[i];
  3771. soundfocus_params.enables[i] = soundFocusData.enable[i];
  3772. pr_debug("%s: start_angle[%d] = %d\n",
  3773. __func__, i, soundFocusData.start_angle[i]);
  3774. pr_debug("%s: enable[%d] = %d\n",
  3775. __func__, i, soundFocusData.enable[i]);
  3776. }
  3777. soundfocus_params.gain_step = soundFocusData.gain_step;
  3778. pr_debug("%s: gain_step = %d\n", __func__, soundFocusData.gain_step);
  3779. soundfocus_params.reserved = 0;
  3780. ret = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
  3781. (uint8_t *) &soundfocus_params);
  3782. if (ret)
  3783. pr_err("%s: Failed to set sound focus params, err %d\n",
  3784. __func__, ret);
  3785. pr_debug("%s: Exit, ret=%d\n", __func__, ret);
  3786. return ret;
  3787. }
  3788. EXPORT_SYMBOL(adm_set_sound_focus);
  3789. /**
  3790. * adm_get_sound_focus -
  3791. * Retrieve sound focus info
  3792. *
  3793. * @port_id: Port ID number
  3794. * @copp_idx: copp index assigned
  3795. * @soundFocusData: pointer for sound focus data to be updated with
  3796. *
  3797. * Returns 0 on success or error on failure
  3798. */
  3799. int adm_get_sound_focus(int port_id, int copp_idx,
  3800. struct sound_focus_param *soundFocusData)
  3801. {
  3802. int ret = 0, i;
  3803. char *params_value;
  3804. uint32_t max_param_size = 0;
  3805. struct adm_param_fluence_soundfocus_t *soundfocus_params = NULL;
  3806. struct param_hdr_v3 param_hdr;
  3807. pr_debug("%s: Enter, port_id %d, copp_idx %d\n",
  3808. __func__, port_id, copp_idx);
  3809. max_param_size = sizeof(struct adm_param_fluence_soundfocus_t) +
  3810. sizeof(union param_hdrs);
  3811. params_value = kzalloc(max_param_size, GFP_KERNEL);
  3812. if (!params_value)
  3813. return -ENOMEM;
  3814. memset(&param_hdr, 0, sizeof(param_hdr));
  3815. param_hdr.module_id = VOICEPROC_MODULE_ID_GENERIC_TX;
  3816. param_hdr.instance_id = INSTANCE_ID_0;
  3817. param_hdr.param_id = VOICEPROC_PARAM_ID_FLUENCE_SOUNDFOCUS;
  3818. param_hdr.param_size = max_param_size;
  3819. ret = adm_get_pp_params(port_id, copp_idx,
  3820. ADM_CLIENT_ID_SOURCE_TRACKING, NULL, &param_hdr,
  3821. params_value);
  3822. if (ret) {
  3823. pr_err("%s: get parameters failed ret:%d\n", __func__, ret);
  3824. ret = -EINVAL;
  3825. goto done;
  3826. }
  3827. if (this_adm.sourceTrackingData.apr_cmd_status != 0) {
  3828. pr_err("%s - get params returned error [%s]\n",
  3829. __func__, adsp_err_get_err_str(
  3830. this_adm.sourceTrackingData.apr_cmd_status));
  3831. ret = adsp_err_get_lnx_err_code(
  3832. this_adm.sourceTrackingData.apr_cmd_status);
  3833. goto done;
  3834. }
  3835. soundfocus_params = (struct adm_param_fluence_soundfocus_t *)
  3836. params_value;
  3837. for (i = 0; i < MAX_SECTORS; i++) {
  3838. soundFocusData->start_angle[i] =
  3839. soundfocus_params->start_angles[i];
  3840. soundFocusData->enable[i] = soundfocus_params->enables[i];
  3841. pr_debug("%s: start_angle[%d] = %d\n",
  3842. __func__, i, soundFocusData->start_angle[i]);
  3843. pr_debug("%s: enable[%d] = %d\n",
  3844. __func__, i, soundFocusData->enable[i]);
  3845. }
  3846. soundFocusData->gain_step = soundfocus_params->gain_step;
  3847. pr_debug("%s: gain_step = %d\n", __func__, soundFocusData->gain_step);
  3848. done:
  3849. pr_debug("%s: Exit, ret = %d\n", __func__, ret);
  3850. kfree(params_value);
  3851. return ret;
  3852. }
  3853. EXPORT_SYMBOL(adm_get_sound_focus);
  3854. static int adm_source_tracking_alloc_map_memory(void)
  3855. {
  3856. int ret;
  3857. pr_debug("%s: Enter\n", __func__);
  3858. ret = msm_audio_ion_alloc(&this_adm.sourceTrackingData.dma_buf,
  3859. AUD_PROC_BLOCK_SIZE,
  3860. &this_adm.sourceTrackingData.memmap.paddr,
  3861. &this_adm.sourceTrackingData.memmap.size,
  3862. &this_adm.sourceTrackingData.memmap.kvaddr);
  3863. if (ret) {
  3864. pr_err("%s: failed to allocate memory\n", __func__);
  3865. ret = -EINVAL;
  3866. goto done;
  3867. }
  3868. atomic_set(&this_adm.mem_map_index, ADM_MEM_MAP_INDEX_SOURCE_TRACKING);
  3869. ret = adm_memory_map_regions(&this_adm.sourceTrackingData.memmap.paddr,
  3870. 0,
  3871. (uint32_t *)&this_adm.sourceTrackingData.memmap.size,
  3872. 1);
  3873. if (ret < 0) {
  3874. pr_err("%s: failed to map memory, paddr = 0x%pK, size = %d\n",
  3875. __func__,
  3876. (void *)this_adm.sourceTrackingData.memmap.paddr,
  3877. (uint32_t)this_adm.sourceTrackingData.memmap.size);
  3878. msm_audio_ion_free(this_adm.sourceTrackingData.dma_buf);
  3879. this_adm.sourceTrackingData.dma_buf = NULL;
  3880. this_adm.sourceTrackingData.memmap.size = 0;
  3881. this_adm.sourceTrackingData.memmap.kvaddr = NULL;
  3882. this_adm.sourceTrackingData.memmap.paddr = 0;
  3883. this_adm.sourceTrackingData.apr_cmd_status = -1;
  3884. atomic_set(&this_adm.mem_map_handles
  3885. [ADM_MEM_MAP_INDEX_SOURCE_TRACKING], 0);
  3886. ret = -EINVAL;
  3887. goto done;
  3888. }
  3889. ret = 0;
  3890. pr_debug("%s: paddr = 0x%pK, size = %d, mem_map_handle = 0x%x\n",
  3891. __func__, (void *)this_adm.sourceTrackingData.memmap.paddr,
  3892. (uint32_t)this_adm.sourceTrackingData.memmap.size,
  3893. atomic_read(&this_adm.mem_map_handles
  3894. [ADM_MEM_MAP_INDEX_SOURCE_TRACKING]));
  3895. done:
  3896. pr_debug("%s: Exit, ret = %d\n", __func__, ret);
  3897. return ret;
  3898. }
  3899. /**
  3900. * adm_get_source_tracking -
  3901. * Retrieve source tracking info
  3902. *
  3903. * @port_id: Port ID number
  3904. * @copp_idx: copp index assigned
  3905. * @sourceTrackingData: pointer for source track data to be updated with
  3906. *
  3907. * Returns 0 on success or error on failure
  3908. */
  3909. int adm_get_source_tracking(int port_id, int copp_idx,
  3910. struct source_tracking_param *sourceTrackingData)
  3911. {
  3912. struct adm_param_fluence_sourcetracking_t *source_tracking_params =
  3913. NULL;
  3914. struct mem_mapping_hdr mem_hdr;
  3915. struct param_hdr_v3 param_hdr;
  3916. int i = 0;
  3917. int ret = 0;
  3918. pr_debug("%s: Enter, port_id %d, copp_idx %d\n",
  3919. __func__, port_id, copp_idx);
  3920. if (!this_adm.sourceTrackingData.memmap.paddr) {
  3921. /* Allocate and map shared memory for out of band usage */
  3922. ret = adm_source_tracking_alloc_map_memory();
  3923. if (ret != 0) {
  3924. ret = -EINVAL;
  3925. goto done;
  3926. }
  3927. }
  3928. memset(&mem_hdr, 0, sizeof(mem_hdr));
  3929. memset(&param_hdr, 0, sizeof(param_hdr));
  3930. mem_hdr.data_payload_addr_lsw =
  3931. lower_32_bits(this_adm.sourceTrackingData.memmap.paddr);
  3932. mem_hdr.data_payload_addr_msw = msm_audio_populate_upper_32_bits(
  3933. this_adm.sourceTrackingData.memmap.paddr);
  3934. mem_hdr.mem_map_handle = atomic_read(
  3935. &this_adm.mem_map_handles[ADM_MEM_MAP_INDEX_SOURCE_TRACKING]);
  3936. param_hdr.module_id = VOICEPROC_MODULE_ID_GENERIC_TX;
  3937. param_hdr.instance_id = INSTANCE_ID_0;
  3938. param_hdr.param_id = VOICEPROC_PARAM_ID_FLUENCE_SOURCETRACKING;
  3939. /*
  3940. * This size should be the max size of the calibration data + header.
  3941. * Use the union size to ensure max size is used.
  3942. */
  3943. param_hdr.param_size =
  3944. sizeof(struct adm_param_fluence_sourcetracking_t) +
  3945. sizeof(union param_hdrs);
  3946. /*
  3947. * Retrieving parameters out of band, so no need to provide a buffer for
  3948. * the returned parameter data as it will be at the memory location
  3949. * provided.
  3950. */
  3951. ret = adm_get_pp_params(port_id, copp_idx,
  3952. ADM_CLIENT_ID_SOURCE_TRACKING, &mem_hdr,
  3953. &param_hdr, NULL);
  3954. if (ret) {
  3955. pr_err("%s: Failed to get params, error %d\n", __func__, ret);
  3956. goto done;
  3957. }
  3958. if (this_adm.sourceTrackingData.apr_cmd_status != 0) {
  3959. pr_err("%s - get params returned error [%s]\n",
  3960. __func__, adsp_err_get_err_str(
  3961. this_adm.sourceTrackingData.apr_cmd_status));
  3962. ret = adsp_err_get_lnx_err_code(
  3963. this_adm.sourceTrackingData.apr_cmd_status);
  3964. goto done;
  3965. }
  3966. /* How do we know what the param data was retrieved with for hdr size */
  3967. source_tracking_params =
  3968. (struct adm_param_fluence_sourcetracking_t
  3969. *) (this_adm.sourceTrackingData.memmap.kvaddr +
  3970. sizeof(struct param_hdr_v1));
  3971. for (i = 0; i < MAX_SECTORS; i++) {
  3972. sourceTrackingData->vad[i] = source_tracking_params->vad[i];
  3973. pr_debug("%s: vad[%d] = %d\n",
  3974. __func__, i, sourceTrackingData->vad[i]);
  3975. }
  3976. sourceTrackingData->doa_speech = source_tracking_params->doa_speech;
  3977. pr_debug("%s: doa_speech = %d\n",
  3978. __func__, sourceTrackingData->doa_speech);
  3979. for (i = 0; i < MAX_NOISE_SOURCE_INDICATORS; i++) {
  3980. sourceTrackingData->doa_noise[i] =
  3981. source_tracking_params->doa_noise[i];
  3982. pr_debug("%s: doa_noise[%d] = %d\n",
  3983. __func__, i, sourceTrackingData->doa_noise[i]);
  3984. }
  3985. for (i = 0; i < MAX_POLAR_ACTIVITY_INDICATORS; i++) {
  3986. sourceTrackingData->polar_activity[i] =
  3987. source_tracking_params->polar_activity[i];
  3988. pr_debug("%s: polar_activity[%d] = %d\n",
  3989. __func__, i, sourceTrackingData->polar_activity[i]);
  3990. }
  3991. ret = 0;
  3992. done:
  3993. pr_debug("%s: Exit, ret=%d\n", __func__, ret);
  3994. return ret;
  3995. }
  3996. EXPORT_SYMBOL(adm_get_source_tracking);
  3997. int __init adm_init(void)
  3998. {
  3999. int i = 0, j;
  4000. this_adm.ec_ref_rx = -1;
  4001. init_waitqueue_head(&this_adm.matrix_map_wait);
  4002. init_waitqueue_head(&this_adm.adm_wait);
  4003. for (i = 0; i < AFE_MAX_PORTS; i++) {
  4004. for (j = 0; j < MAX_COPPS_PER_PORT; j++) {
  4005. atomic_set(&this_adm.copp.id[i][j], RESET_COPP_ID);
  4006. init_waitqueue_head(&this_adm.copp.wait[i][j]);
  4007. init_waitqueue_head(
  4008. &this_adm.copp.adm_delay_wait[i][j]);
  4009. }
  4010. }
  4011. if (adm_init_cal_data())
  4012. pr_err("%s: could not init cal data!\n", __func__);
  4013. this_adm.sourceTrackingData.dma_buf = NULL;
  4014. this_adm.sourceTrackingData.memmap.size = 0;
  4015. this_adm.sourceTrackingData.memmap.kvaddr = NULL;
  4016. this_adm.sourceTrackingData.memmap.paddr = 0;
  4017. this_adm.sourceTrackingData.apr_cmd_status = -1;
  4018. return 0;
  4019. }
  4020. void adm_exit(void)
  4021. {
  4022. if (this_adm.apr)
  4023. adm_reset_data();
  4024. adm_delete_cal_data();
  4025. }