q6adm.c 131 KB

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