q6afe.c 99 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636
  1. /* Copyright (c) 2012-2013, 2019 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/slab.h>
  13. #include <linux/debugfs.h>
  14. #include <linux/kernel.h>
  15. #include <linux/kthread.h>
  16. #include <linux/uaccess.h>
  17. #include <linux/wait.h>
  18. #include <linux/jiffies.h>
  19. #include <linux/sched.h>
  20. #include <linux/msm_audio_ion.h>
  21. #include <sound/apr_audio-v2.h>
  22. #include <sound/q6afe-v2.h>
  23. #include <sound/q6audio-v2.h>
  24. #include "msm-pcm-routing-v2.h"
  25. #include "audio_acdb.h"
  26. #ifdef CONFIG_SND_SOC_MAXIM_DSM
  27. #include <sound/maxim_dsm.h>
  28. #ifdef USE_DSM_LOG
  29. #include <linux/file.h>
  30. #include <linux/fs.h>
  31. #endif /* USE_DSM_LOG */
  32. #endif /* CONFIG_SND_SOC_MAXIM_DSM */
  33. enum {
  34. AFE_RX_CAL,
  35. AFE_TX_CAL,
  36. AFE_AANC_TX_CAL,
  37. MAX_AFE_CAL_TYPES
  38. };
  39. struct afe_ctl {
  40. void *apr;
  41. atomic_t state;
  42. atomic_t status;
  43. wait_queue_head_t wait[AFE_MAX_PORTS];
  44. struct task_struct *task;
  45. void (*tx_cb) (uint32_t opcode,
  46. uint32_t token, uint32_t *payload, void *priv);
  47. void (*rx_cb) (uint32_t opcode,
  48. uint32_t token, uint32_t *payload, void *priv);
  49. void *tx_private_data;
  50. void *rx_private_data;
  51. uint32_t mmap_handle;
  52. struct acdb_cal_block afe_cal_addr[MAX_AFE_CAL_TYPES];
  53. atomic_t mem_map_cal_handles[MAX_AFE_CAL_TYPES];
  54. atomic_t mem_map_cal_index;
  55. u16 dtmf_gen_rx_portid;
  56. #ifdef CONFIG_SND_SOC_MAXIM_DSM
  57. struct afe_dsm_spkr_prot_calib_get_resp calib_data;
  58. #else
  59. struct afe_spkr_prot_calib_get_resp calib_data;
  60. #endif /* CONFIG_SND_SOC_MAXIM_DSM */
  61. int vi_tx_port;
  62. int vi_rx_port;
  63. uint32_t afe_sample_rates[AFE_MAX_PORTS];
  64. struct aanc_data aanc_info;
  65. };
  66. static atomic_t afe_ports_mad_type[SLIMBUS_PORT_LAST - SLIMBUS_0_RX];
  67. static unsigned long afe_configured_cmd;
  68. static struct afe_ctl this_afe;
  69. #define TIMEOUT_MS 1000
  70. #define Q6AFE_MAX_VOLUME 0x3FFF
  71. #define Q6AFE_MSM_SPKR_PROCESSING 0
  72. #define Q6AFE_MSM_SPKR_CALIBRATION 1
  73. static int pcm_afe_instance[2];
  74. static int proxy_afe_instance[2];
  75. bool afe_close_done[2] = {true, true};
  76. #define SIZEOF_CFG_CMD(y) \
  77. (sizeof(struct apr_hdr) + sizeof(u16) + (sizeof(struct y)))
  78. void afe_set_aanc_info(struct aanc_data *q6_aanc_info)
  79. {
  80. pr_debug("%s\n", __func__);
  81. this_afe.aanc_info.aanc_active = q6_aanc_info->aanc_active;
  82. this_afe.aanc_info.aanc_rx_port = q6_aanc_info->aanc_rx_port;
  83. this_afe.aanc_info.aanc_tx_port = q6_aanc_info->aanc_tx_port;
  84. pr_debug("aanc active is %d rx port is %d, tx port is %d\n",
  85. this_afe.aanc_info.aanc_active,
  86. this_afe.aanc_info.aanc_rx_port,
  87. this_afe.aanc_info.aanc_tx_port);
  88. }
  89. static bool afe_token_is_valid(uint32_t token)
  90. {
  91. if (token >= AFE_MAX_PORTS) {
  92. pr_err("%s: token %d is invalid.\n", __func__, token);
  93. return false;
  94. }
  95. return true;
  96. }
  97. static int32_t afe_callback(struct apr_client_data *data, void *priv)
  98. {
  99. int i;
  100. if (!data) {
  101. pr_err("%s: Invalid param data\n", __func__);
  102. return -EINVAL;
  103. }
  104. if (data->opcode == RESET_EVENTS) {
  105. pr_debug("q6afe: reset event = %d %d apr[%p]\n",
  106. data->reset_event, data->reset_proc, this_afe.apr);
  107. for (i = 0; i < MAX_AFE_CAL_TYPES; i++) {
  108. this_afe.afe_cal_addr[i].cal_paddr = 0;
  109. this_afe.afe_cal_addr[i].cal_size = 0;
  110. }
  111. if (this_afe.apr) {
  112. apr_reset(this_afe.apr);
  113. atomic_set(&this_afe.state, 0);
  114. this_afe.apr = NULL;
  115. }
  116. /* send info to user */
  117. pr_debug("task_name = %s pid = %d\n",
  118. this_afe.task->comm, this_afe.task->pid);
  119. return 0;
  120. }
  121. pr_debug("%s:opcode = 0x%x cmd = 0x%x status = 0x%x size = %d\n",
  122. __func__, data->opcode,
  123. ((uint32_t *)(data->payload))[0],
  124. ((uint32_t *)(data->payload))[1],
  125. data->payload_size);
  126. if (data->opcode == AFE_PORT_CMDRSP_GET_PARAM_V2) {
  127. u8 *payload = data->payload;
  128. if ((data->payload_size < sizeof(this_afe.calib_data))
  129. || !payload || (data->token >= AFE_MAX_PORTS)) {
  130. pr_err("%s size %d payload %p token %d\n",
  131. __func__, data->payload_size, payload, data->token);
  132. return -EINVAL;
  133. }
  134. memcpy(&this_afe.calib_data, payload,
  135. sizeof(this_afe.calib_data));
  136. if (!this_afe.calib_data.status) {
  137. atomic_set(&this_afe.state, 0);
  138. #ifndef CONFIG_SND_SOC_MAXIM_DSM
  139. pr_err("%s rest %d state %x\n" , __func__
  140. , this_afe.calib_data.res_cfg.r0_cali_q24,
  141. this_afe.calib_data.res_cfg.th_vi_ca_state);
  142. #endif /* CONFIG_SND_SOC_MAXIM_DSM */
  143. } else
  144. atomic_set(&this_afe.state, -1);
  145. if (afe_token_is_valid(data->token))
  146. wake_up(&this_afe.wait[data->token]);
  147. else
  148. return -EINVAL;
  149. } else if (data->payload_size) {
  150. uint32_t *payload;
  151. uint16_t port_id = 0;
  152. payload = data->payload;
  153. pr_debug("%s:opcode = 0x%x cmd = 0x%x status = 0x%x token=%d\n",
  154. __func__, data->opcode,
  155. payload[0], payload[1], data->token);
  156. if (data->opcode == APR_BASIC_RSP_RESULT) {
  157. /* payload[1] contains the error status for response */
  158. if (payload[1] != 0) {
  159. atomic_set(&this_afe.status, -1);
  160. pr_err("%s: cmd = 0x%x returned error = 0x%x\n",
  161. __func__, payload[0], payload[1]);
  162. }
  163. switch (payload[0]) {
  164. case AFE_PORT_CMD_DEVICE_STOP:
  165. case AFE_PORT_CMD_DEVICE_START:
  166. case AFE_PORT_CMD_SET_PARAM_V2:
  167. case AFE_PSEUDOPORT_CMD_START:
  168. case AFE_PSEUDOPORT_CMD_STOP:
  169. case AFE_SERVICE_CMD_SHARED_MEM_MAP_REGIONS:
  170. case AFE_SERVICE_CMD_SHARED_MEM_UNMAP_REGIONS:
  171. case AFE_SERVICE_CMD_UNREGISTER_RT_PORT_DRIVER:
  172. case AFE_PORTS_CMD_DTMF_CTL:
  173. case AFE_SVC_CMD_SET_PARAM:
  174. atomic_set(&this_afe.state, 0);
  175. if (afe_token_is_valid(data->token))
  176. wake_up(&this_afe.wait[data->token]);
  177. else
  178. return -EINVAL;
  179. break;
  180. case AFE_SERVICE_CMD_REGISTER_RT_PORT_DRIVER:
  181. break;
  182. case AFE_PORT_DATA_CMD_RT_PROXY_PORT_WRITE_V2:
  183. port_id = RT_PROXY_PORT_001_TX;
  184. break;
  185. case AFE_PORT_DATA_CMD_RT_PROXY_PORT_READ_V2:
  186. port_id = RT_PROXY_PORT_001_RX;
  187. break;
  188. default:
  189. pr_err("%s:Unknown cmd 0x%x\n", __func__,
  190. payload[0]);
  191. break;
  192. }
  193. } else if (data->opcode ==
  194. AFE_SERVICE_CMDRSP_SHARED_MEM_MAP_REGIONS) {
  195. pr_debug("%s: mmap_handle: 0x%x, cal index %d\n",
  196. __func__, payload[0],
  197. atomic_read(&this_afe.mem_map_cal_index));
  198. if (atomic_read(&this_afe.mem_map_cal_index) != -1)
  199. atomic_set(&this_afe.mem_map_cal_handles[
  200. atomic_read(
  201. &this_afe.mem_map_cal_index)],
  202. (uint32_t)payload[0]);
  203. else
  204. this_afe.mmap_handle = (uint32_t)payload[0];
  205. atomic_set(&this_afe.state, 0);
  206. if (afe_token_is_valid(data->token))
  207. wake_up(&this_afe.wait[data->token]);
  208. else
  209. return -EINVAL;
  210. } else if (data->opcode == AFE_EVENT_RT_PROXY_PORT_STATUS) {
  211. port_id = (uint16_t)(0x0000FFFF & payload[0]);
  212. }
  213. pr_debug("%s:port_id = %x\n", __func__, port_id);
  214. switch (port_id) {
  215. case RT_PROXY_PORT_001_TX: {
  216. if (this_afe.tx_cb) {
  217. this_afe.tx_cb(data->opcode, data->token,
  218. data->payload,
  219. this_afe.tx_private_data);
  220. }
  221. break;
  222. }
  223. case RT_PROXY_PORT_001_RX: {
  224. if (this_afe.rx_cb) {
  225. this_afe.rx_cb(data->opcode, data->token,
  226. data->payload,
  227. this_afe.rx_private_data);
  228. }
  229. break;
  230. }
  231. default:
  232. break;
  233. }
  234. }
  235. return 0;
  236. }
  237. int afe_get_port_type(u16 port_id)
  238. {
  239. int ret;
  240. switch (port_id) {
  241. case PRIMARY_I2S_RX:
  242. case AFE_PORT_ID_PRIMARY_PCM_RX:
  243. case SECONDARY_I2S_RX:
  244. case MI2S_RX:
  245. case HDMI_RX:
  246. case SLIMBUS_0_RX:
  247. case SLIMBUS_1_RX:
  248. case SLIMBUS_2_RX:
  249. case SLIMBUS_3_RX:
  250. case SLIMBUS_4_RX:
  251. case INT_BT_SCO_RX:
  252. case INT_BT_A2DP_RX:
  253. case INT_FM_RX:
  254. case VOICE_PLAYBACK_TX:
  255. case VOICE2_PLAYBACK_TX:
  256. case RT_PROXY_PORT_001_RX:
  257. case AUDIO_PORT_ID_I2S_RX:
  258. case AFE_PORT_ID_PRIMARY_MI2S_RX:
  259. case AFE_PORT_ID_SECONDARY_MI2S_RX:
  260. case AFE_PORT_ID_TERTIARY_MI2S_RX:
  261. case AFE_PORT_ID_QUATERNARY_MI2S_RX:
  262. case AFE_PORT_ID_SECONDARY_PCM_RX:
  263. ret = MSM_AFE_PORT_TYPE_RX;
  264. break;
  265. case PRIMARY_I2S_TX:
  266. case AFE_PORT_ID_PRIMARY_PCM_TX:
  267. case SECONDARY_I2S_TX:
  268. case MI2S_TX:
  269. case DIGI_MIC_TX:
  270. case VOICE_RECORD_TX:
  271. case SLIMBUS_0_TX:
  272. case SLIMBUS_1_TX:
  273. case SLIMBUS_2_TX:
  274. case SLIMBUS_3_TX:
  275. case SLIMBUS_4_TX:
  276. case SLIMBUS_5_TX:
  277. case INT_FM_TX:
  278. case VOICE_RECORD_RX:
  279. case INT_BT_SCO_TX:
  280. case RT_PROXY_PORT_001_TX:
  281. case AFE_PORT_ID_PRIMARY_MI2S_TX:
  282. case AFE_PORT_ID_SECONDARY_MI2S_TX:
  283. case AFE_PORT_ID_TERTIARY_MI2S_TX:
  284. case AFE_PORT_ID_QUATERNARY_MI2S_TX:
  285. case AFE_PORT_ID_SECONDARY_PCM_TX:
  286. ret = MSM_AFE_PORT_TYPE_TX;
  287. break;
  288. default:
  289. WARN_ON(1);
  290. pr_err("%s: invalid port id %#x\n", __func__, port_id);
  291. ret = -EINVAL;
  292. }
  293. return ret;
  294. }
  295. int afe_sizeof_cfg_cmd(u16 port_id)
  296. {
  297. int ret_size;
  298. switch (port_id) {
  299. case PRIMARY_I2S_RX:
  300. case PRIMARY_I2S_TX:
  301. case SECONDARY_I2S_RX:
  302. case SECONDARY_I2S_TX:
  303. case MI2S_RX:
  304. case MI2S_TX:
  305. case AFE_PORT_ID_PRIMARY_MI2S_RX:
  306. ret_size = SIZEOF_CFG_CMD(afe_param_id_i2s_cfg);
  307. break;
  308. case HDMI_RX:
  309. ret_size =
  310. SIZEOF_CFG_CMD(afe_param_id_hdmi_multi_chan_audio_cfg);
  311. break;
  312. case SLIMBUS_0_RX:
  313. case SLIMBUS_0_TX:
  314. case SLIMBUS_1_RX:
  315. case SLIMBUS_1_TX:
  316. case SLIMBUS_2_RX:
  317. case SLIMBUS_2_TX:
  318. ret_size = SIZEOF_CFG_CMD(afe_param_id_slimbus_cfg);
  319. break;
  320. case VOICE_PLAYBACK_TX:
  321. case VOICE2_PLAYBACK_TX:
  322. case VOICE_RECORD_RX:
  323. case VOICE_RECORD_TX:
  324. ret_size = SIZEOF_CFG_CMD(afe_param_id_pseudo_port_cfg);
  325. break;
  326. case RT_PROXY_PORT_001_RX:
  327. case RT_PROXY_PORT_001_TX:
  328. ret_size = SIZEOF_CFG_CMD(afe_param_id_rt_proxy_port_cfg);
  329. break;
  330. case AFE_PORT_ID_PRIMARY_PCM_RX:
  331. case AFE_PORT_ID_PRIMARY_PCM_TX:
  332. case AFE_PORT_ID_SECONDARY_PCM_RX:
  333. case AFE_PORT_ID_SECONDARY_PCM_TX:
  334. default:
  335. ret_size = SIZEOF_CFG_CMD(afe_param_id_pcm_cfg);
  336. break;
  337. }
  338. return ret_size;
  339. }
  340. int afe_q6_interface_prepare(void)
  341. {
  342. int ret = 0;
  343. pr_debug("%s:", __func__);
  344. if (this_afe.apr == NULL) {
  345. this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
  346. 0xFFFFFFFF, &this_afe);
  347. if (this_afe.apr == NULL) {
  348. pr_err("%s: Unable to register AFE\n", __func__);
  349. ret = -ENODEV;
  350. }
  351. }
  352. return ret;
  353. }
  354. /*
  355. * afe_apr_send_pkt : returns 0 on success, negative otherwise.
  356. */
  357. static int afe_apr_send_pkt(void *data, wait_queue_head_t *wait)
  358. {
  359. int ret;
  360. if (wait)
  361. atomic_set(&this_afe.state, 1);
  362. atomic_set(&this_afe.status, 0);
  363. ret = apr_send_pkt(this_afe.apr, data);
  364. if (ret > 0) {
  365. if (wait) {
  366. ret = wait_event_timeout(*wait,
  367. (atomic_read(&this_afe.state) == 0),
  368. msecs_to_jiffies(TIMEOUT_MS));
  369. if (ret)
  370. ret = 0;
  371. else
  372. ret = -ETIMEDOUT;
  373. } else {
  374. ret = 0;
  375. }
  376. } else if (ret == 0) {
  377. /* apr_send_pkt can return 0 when nothing is transmitted */
  378. ret = -EINVAL;
  379. }
  380. pr_debug("%s: leave %d\n", __func__, ret);
  381. return ret;
  382. }
  383. static void afe_send_cal_block(int32_t path, u16 port_id)
  384. {
  385. int result = 0;
  386. int index = 0;
  387. int size = 4096;
  388. struct acdb_cal_block cal_block;
  389. struct afe_audioif_config_command_no_payload afe_cal;
  390. atomic_t *hptr;
  391. u32 handle;
  392. pr_debug("%s: path %d\n", __func__, path);
  393. if (path == AFE_AANC_TX_CAL) {
  394. get_aanc_cal(&cal_block);
  395. } else {
  396. get_afe_cal(path, &cal_block);
  397. }
  398. if (cal_block.cal_size <= 0) {
  399. pr_debug("%s: No AFE cal to send!\n", __func__);
  400. goto done;
  401. }
  402. if ((this_afe.afe_cal_addr[path].cal_paddr != cal_block.cal_paddr) ||
  403. (cal_block.cal_size > this_afe.afe_cal_addr[path].cal_size)) {
  404. atomic_set(&this_afe.mem_map_cal_index, path);
  405. if (this_afe.afe_cal_addr[path].cal_paddr != 0) {
  406. hptr = &this_afe.mem_map_cal_handles[path];
  407. handle = atomic_xchg(hptr, 0);
  408. if (!handle) {
  409. pr_err("%s: invalid NULL handle\n", __func__);
  410. result = -EINVAL;
  411. goto done;
  412. }
  413. result = afe_cmd_memory_unmap(handle);
  414. if (result) {
  415. WARN(1, "%s: AFE memory unmap failed %d, handle 0x%x\n",
  416. __func__, result, handle);
  417. atomic_set(&this_afe.mem_map_cal_index, -1);
  418. goto done;
  419. }
  420. }
  421. result = afe_cmd_memory_map(cal_block.cal_paddr, size);
  422. if (result) {
  423. pr_err("%s: AFE memory map failed\n", __func__);
  424. atomic_set(&this_afe.mem_map_cal_index, -1);
  425. goto done;
  426. }
  427. atomic_set(&this_afe.mem_map_cal_index, -1);
  428. this_afe.afe_cal_addr[path].cal_paddr = cal_block.cal_paddr;
  429. this_afe.afe_cal_addr[path].cal_size = size;
  430. }
  431. index = q6audio_get_port_index(port_id);
  432. if (index < 0 || index > AFE_MAX_PORTS) {
  433. pr_debug("%s: AFE port index[%d] invalid!\n",
  434. __func__, index);
  435. goto done;
  436. }
  437. afe_cal.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  438. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  439. afe_cal.hdr.pkt_size = sizeof(afe_cal);
  440. afe_cal.hdr.src_port = 0;
  441. afe_cal.hdr.dest_port = 0;
  442. afe_cal.hdr.token = index;
  443. afe_cal.hdr.opcode = AFE_PORT_CMD_SET_PARAM_V2;
  444. afe_cal.param.port_id = port_id;
  445. afe_cal.param.payload_size = cal_block.cal_size;
  446. afe_cal.param.payload_address_lsw = cal_block.cal_paddr;
  447. afe_cal.param.payload_address_msw = 0;
  448. afe_cal.param.mem_map_handle =
  449. atomic_read(&this_afe.mem_map_cal_handles[path]);
  450. pr_debug("%s: AFE cal sent for device port = %d, path = %d, cal size = %d, cal addr = 0x%x\n",
  451. __func__, port_id, path,
  452. cal_block.cal_size, cal_block.cal_paddr);
  453. result = afe_apr_send_pkt(&afe_cal, &this_afe.wait[index]);
  454. if (result)
  455. pr_err("%s: AFE cal for port %d failed %d\n",
  456. __func__, port_id, result);
  457. else
  458. pr_debug("%s: AFE cal sent for path %d device!\n", __func__,
  459. path);
  460. done:
  461. return;
  462. }
  463. int afe_unmap_cal_blocks(void)
  464. {
  465. int i;
  466. int result = 0;
  467. int result2 = 0;
  468. for (i = 0; i < MAX_AFE_CAL_TYPES; i++) {
  469. if (atomic_read(&this_afe.mem_map_cal_handles[i]) != 0) {
  470. atomic_set(&this_afe.mem_map_cal_index, i);
  471. result2 = afe_cmd_memory_unmap(atomic_read(
  472. &this_afe.mem_map_cal_handles[i]));
  473. if (result2 < 0) {
  474. pr_err("%s: unmap failed, err %d\n",
  475. __func__, result2);
  476. result = result2;
  477. } else {
  478. atomic_set(&this_afe.mem_map_cal_handles[i],
  479. 0);
  480. }
  481. atomic_set(&this_afe.mem_map_cal_index, -1);
  482. this_afe.afe_cal_addr[i].cal_paddr = 0;
  483. this_afe.afe_cal_addr[i].cal_size = 0;
  484. }
  485. }
  486. return result;
  487. }
  488. static int afe_spk_prot_prepare(int src_port, int dst_port, int param_id,
  489. union afe_spkr_prot_config *prot_config)
  490. {
  491. int ret = -EINVAL;
  492. int index = 0;
  493. struct afe_spkr_prot_config_command config;
  494. memset(&config, 0 , sizeof(config));
  495. if (!prot_config) {
  496. pr_err("%s Invalid params\n", __func__);
  497. goto fail_cmd;
  498. }
  499. ret = q6audio_validate_port(src_port);
  500. if (ret < 0) {
  501. pr_err("%s: Invalid src port 0x%x ret %d",
  502. __func__, src_port, ret);
  503. ret = -EINVAL;
  504. goto fail_cmd;
  505. }
  506. ret = q6audio_validate_port(dst_port);
  507. if (ret < 0) {
  508. pr_err("%s: Invalid dst port 0x%x ret %d", __func__,
  509. dst_port, ret);
  510. ret = -EINVAL;
  511. goto fail_cmd;
  512. }
  513. index = q6audio_get_port_index(src_port);
  514. switch (param_id) {
  515. case AFE_PARAM_ID_FBSP_MODE_RX_CFG:
  516. config.pdata.module_id = AFE_MODULE_FB_SPKR_PROT_RX;
  517. break;
  518. case AFE_PARAM_ID_FEEDBACK_PATH_CFG:
  519. this_afe.vi_tx_port = src_port;
  520. this_afe.vi_rx_port = dst_port;
  521. case AFE_PARAM_ID_SPKR_CALIB_VI_PROC_CFG:
  522. case AFE_PARAM_ID_MODE_VI_PROC_CFG:
  523. config.pdata.module_id = AFE_MODULE_FB_SPKR_PROT_VI_PROC;
  524. break;
  525. default:
  526. goto fail_cmd;
  527. break;
  528. }
  529. config.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  530. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  531. config.hdr.pkt_size = sizeof(config);
  532. config.hdr.src_port = 0;
  533. config.hdr.dest_port = 0;
  534. config.hdr.token = index;
  535. config.hdr.opcode = AFE_PORT_CMD_SET_PARAM_V2;
  536. config.param.port_id = q6audio_get_port_id(src_port);
  537. config.param.payload_size = sizeof(config) - sizeof(config.hdr)
  538. - sizeof(config.param);
  539. config.pdata.param_id = param_id;
  540. config.pdata.param_size = sizeof(config.prot_config);
  541. config.prot_config = *prot_config;
  542. atomic_set(&this_afe.state, 1);
  543. ret = apr_send_pkt(this_afe.apr, (uint32_t *) &config);
  544. if (ret < 0) {
  545. pr_err("%s: port = 0x%x param = 0x%x failed %d\n",
  546. __func__, src_port, param_id, ret);
  547. goto fail_cmd;
  548. }
  549. ret = wait_event_timeout(this_afe.wait[index],
  550. (atomic_read(&this_afe.state) == 0),
  551. msecs_to_jiffies(TIMEOUT_MS));
  552. if (!ret) {
  553. pr_err("%s: wait_event timeout\n", __func__);
  554. ret = -EINVAL;
  555. goto fail_cmd;
  556. }
  557. if (atomic_read(&this_afe.status) != 0) {
  558. pr_err("%s: config cmd failed\n", __func__);
  559. ret = -EINVAL;
  560. goto fail_cmd;
  561. }
  562. ret = 0;
  563. fail_cmd:
  564. pr_debug("%s: config.pdata.param_id 0x%x status %d 0x%x\n",
  565. __func__, config.pdata.param_id, ret, src_port);
  566. return ret;
  567. }
  568. #ifdef CONFIG_SND_SOC_MAXIM_DSM
  569. static int afe_dsm_spk_prot_prepare(int port, int param_id,
  570. union afe_dsm_spkr_prot_config *prot_config)
  571. {
  572. int ret = -EINVAL;
  573. int index = 0;
  574. struct afe_dsm_spkr_prot_config_command config;
  575. memset(&config, 0 , sizeof(config));
  576. if (!prot_config) {
  577. pr_err("%s Invalid params\n", __func__);
  578. goto fail_cmd;
  579. }
  580. if ((q6audio_validate_port(port) < 0)) {
  581. pr_err("%s invalid port %d", __func__, port);
  582. goto fail_cmd;
  583. }
  584. index = q6audio_get_port_index(port);
  585. switch (param_id) {
  586. case AFE_PARAM_ID_FBSP_MODE_RX_CFG:
  587. if(port == maxdsm_get_port_id())
  588. config.pdata.module_id = maxdsm_get_rx_mod_id();
  589. else
  590. config.pdata.module_id = maxdsm_get_tx_mod_id();
  591. break;
  592. case AFE_PARAM_ID_FEEDBACK_PATH_CFG:
  593. this_afe.vi_tx_port = port;
  594. case AFE_PARAM_ID_SPKR_CALIB_VI_PROC_CFG:
  595. case AFE_PARAM_ID_MODE_VI_PROC_CFG:
  596. config.pdata.module_id = AFE_MODULE_FB_SPKR_PROT_VI_PROC;
  597. break;
  598. default:
  599. goto fail_cmd;
  600. break;
  601. }
  602. config.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  603. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  604. config.hdr.pkt_size = sizeof(config);
  605. config.hdr.src_port = 0;
  606. config.hdr.dest_port = 0;
  607. config.hdr.token = index;
  608. config.hdr.opcode = AFE_PORT_CMD_SET_PARAM_V2;
  609. config.param.port_id = q6audio_get_port_id(port);
  610. config.param.payload_size = sizeof(config) - sizeof(config.hdr)
  611. - sizeof(config.param);
  612. config.pdata.param_id = param_id;
  613. config.pdata.param_size = sizeof(config.prot_config);
  614. config.prot_config = *prot_config;
  615. atomic_set(&this_afe.state, 1);
  616. ret = apr_send_pkt(this_afe.apr, (uint32_t *) &config);
  617. if (ret < 0) {
  618. pr_err("%s: Setting param for port %d param[0x%x]failed\n",
  619. __func__, port, param_id);
  620. goto fail_cmd;
  621. }
  622. ret = wait_event_timeout(this_afe.wait[index],
  623. (atomic_read(&this_afe.state) == 0),
  624. msecs_to_jiffies(TIMEOUT_MS));
  625. if (!ret) {
  626. pr_err("%s: wait_event timeout\n", __func__);
  627. ret = -EINVAL;
  628. goto fail_cmd;
  629. }
  630. if (atomic_read(&this_afe.status) != 0) {
  631. pr_err("%s: config cmd failed\n", __func__);
  632. ret = -EINVAL;
  633. goto fail_cmd;
  634. }
  635. ret = 0;
  636. fail_cmd:
  637. pr_debug("%s config.pdata.param_id %x status %d\n",
  638. __func__, config.pdata.param_id, ret);
  639. return ret;
  640. }
  641. #endif /* CONFIG_SND_SOC_MAXIM_DSM */
  642. static void afe_send_cal_spkr_prot_tx(int port_id)
  643. {
  644. struct msm_spk_prot_cfg prot_cfg;
  645. union afe_spkr_prot_config afe_spk_config;
  646. /*Get spkr protection cfg data*/
  647. get_spk_protection_cfg(&prot_cfg);
  648. if ((prot_cfg.mode != MSM_SPKR_PROT_DISABLED) &&
  649. (this_afe.vi_tx_port == port_id)) {
  650. afe_spk_config.mode_rx_cfg.minor_version = 1;
  651. if (prot_cfg.mode == MSM_SPKR_PROT_CALIBRATION_IN_PROGRESS)
  652. afe_spk_config.mode_rx_cfg.mode =
  653. Q6AFE_MSM_SPKR_CALIBRATION;
  654. else
  655. afe_spk_config.mode_rx_cfg.mode =
  656. Q6AFE_MSM_SPKR_PROCESSING;
  657. if (afe_spk_prot_prepare(port_id, 0,
  658. AFE_PARAM_ID_MODE_VI_PROC_CFG,
  659. &afe_spk_config))
  660. pr_err("%s TX VI_PROC_CFG failed\n", __func__);
  661. if (prot_cfg.mode != MSM_SPKR_PROT_NOT_CALIBRATED) {
  662. afe_spk_config.vi_proc_cfg.minor_version = 1;
  663. afe_spk_config.vi_proc_cfg.r0_cali_q24 =
  664. (uint32_t) prot_cfg.r0;
  665. afe_spk_config.vi_proc_cfg.t0_cali_q6 =
  666. (uint32_t) prot_cfg.t0;
  667. if (afe_spk_prot_prepare(port_id, 0,
  668. AFE_PARAM_ID_SPKR_CALIB_VI_PROC_CFG,
  669. &afe_spk_config))
  670. pr_err("%s SPKR_CALIB_VI_PROC_CFG failed\n",
  671. __func__);
  672. }
  673. }
  674. }
  675. static void afe_send_cal_spkr_prot_rx(int port_id)
  676. {
  677. struct msm_spk_prot_cfg prot_cfg;
  678. union afe_spkr_prot_config afe_spk_config;
  679. /*Get spkr protection cfg data*/
  680. get_spk_protection_cfg(&prot_cfg);
  681. if ((prot_cfg.mode != MSM_SPKR_PROT_DISABLED) &&
  682. (this_afe.vi_rx_port == port_id)) {
  683. if (prot_cfg.mode == MSM_SPKR_PROT_CALIBRATION_IN_PROGRESS)
  684. afe_spk_config.mode_rx_cfg.mode =
  685. Q6AFE_MSM_SPKR_CALIBRATION;
  686. else
  687. afe_spk_config.mode_rx_cfg.mode =
  688. Q6AFE_MSM_SPKR_PROCESSING;
  689. afe_spk_config.mode_rx_cfg.minor_version = 1;
  690. if (afe_spk_prot_prepare(port_id, 0,
  691. AFE_PARAM_ID_FBSP_MODE_RX_CFG,
  692. &afe_spk_config))
  693. pr_err("%s RX MODE_VI_PROC_CFG failed\n",
  694. __func__);
  695. }
  696. }
  697. static int afe_send_hw_delay(u16 port_id, u32 rate)
  698. {
  699. struct hw_delay_entry delay_entry;
  700. struct afe_audioif_config_command config;
  701. int index = 0;
  702. int ret = -EINVAL;
  703. pr_debug("%s\n", __func__);
  704. delay_entry.sample_rate = rate;
  705. if (afe_get_port_type(port_id) == MSM_AFE_PORT_TYPE_TX)
  706. ret = get_hw_delay(TX_CAL, &delay_entry);
  707. else if (afe_get_port_type(port_id) == MSM_AFE_PORT_TYPE_RX)
  708. ret = get_hw_delay(RX_CAL, &delay_entry);
  709. if (ret != 0) {
  710. pr_debug("%s: Failed to get hw delay info\n", __func__);
  711. goto fail_cmd;
  712. }
  713. index = q6audio_get_port_index(port_id);
  714. if (index < 0 || index > AFE_MAX_PORTS) {
  715. pr_debug("%s: AFE port index[%d] invalid!\n",
  716. __func__, index);
  717. goto fail_cmd;
  718. }
  719. config.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  720. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  721. config.hdr.pkt_size = sizeof(config);
  722. config.hdr.src_port = 0;
  723. config.hdr.dest_port = 0;
  724. config.hdr.token = index;
  725. config.hdr.opcode = AFE_PORT_CMD_SET_PARAM_V2;
  726. config.param.port_id = q6audio_get_port_id(port_id);
  727. config.param.payload_size = sizeof(config) - sizeof(struct apr_hdr) -
  728. sizeof(config.param);
  729. config.param.payload_address_lsw = 0x00;
  730. config.param.payload_address_msw = 0x00;
  731. config.param.mem_map_handle = 0x00;
  732. config.pdata.module_id = AFE_MODULE_AUDIO_DEV_INTERFACE;
  733. config.pdata.param_id = AFE_PARAM_ID_DEVICE_HW_DELAY;
  734. config.pdata.param_size = sizeof(config.port);
  735. config.port.hw_delay.delay_in_us = delay_entry.delay_usec;
  736. config.port.hw_delay.device_hw_delay_minor_version =
  737. AFE_API_VERSION_DEVICE_HW_DELAY;
  738. ret = afe_apr_send_pkt(&config, &this_afe.wait[index]);
  739. if (ret) {
  740. pr_err("%s: AFE hw delay for port %#x failed\n",
  741. __func__, port_id);
  742. goto fail_cmd;
  743. } else if (atomic_read(&this_afe.status) != 0) {
  744. pr_err("%s: config cmd failed\n", __func__);
  745. ret = -EINVAL;
  746. goto fail_cmd;
  747. }
  748. fail_cmd:
  749. pr_debug("%s port_id %u rate %u delay_usec %d status %d\n",
  750. __func__, port_id, rate, delay_entry.delay_usec, ret);
  751. return ret;
  752. }
  753. void afe_send_cal(u16 port_id)
  754. {
  755. pr_debug("%s: port_id=0x%x\n", __func__, port_id);
  756. if (afe_get_port_type(port_id) == MSM_AFE_PORT_TYPE_TX) {
  757. afe_send_cal_spkr_prot_tx(port_id);
  758. afe_send_cal_block(AFE_TX_CAL, port_id);
  759. } else if (afe_get_port_type(port_id) == MSM_AFE_PORT_TYPE_RX) {
  760. afe_send_cal_spkr_prot_rx(port_id);
  761. afe_send_cal_block(AFE_RX_CAL, port_id);
  762. }
  763. }
  764. int afe_turn_onoff_hw_mad(u16 mad_type, u16 enable)
  765. {
  766. int ret;
  767. struct afe_cmd_hw_mad_ctrl config;
  768. pr_debug("%s: enter\n", __func__);
  769. memset(&config, 0, sizeof(config));
  770. config.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  771. APR_HDR_LEN(APR_HDR_SIZE),
  772. APR_PKT_VER);
  773. config.hdr.pkt_size = sizeof(config);
  774. config.hdr.src_port = 0;
  775. config.hdr.dest_port = 0;
  776. config.hdr.token = IDX_GLOBAL_CFG;
  777. config.hdr.opcode = AFE_PORT_CMD_SET_PARAM_V2;
  778. config.param.port_id = SLIMBUS_5_TX;
  779. config.param.payload_size = sizeof(config) - sizeof(struct apr_hdr) -
  780. sizeof(config.param);
  781. config.param.payload_address_lsw = 0x00;
  782. config.param.payload_address_msw = 0x00;
  783. config.param.mem_map_handle = 0x00;
  784. config.pdata.module_id = AFE_MODULE_HW_MAD;
  785. config.pdata.param_id = AFE_PARAM_ID_HW_MAD_CTRL;
  786. config.pdata.param_size = sizeof(config.payload);
  787. config.payload.minor_version = 1;
  788. config.payload.mad_type = mad_type;
  789. config.payload.mad_enable = enable;
  790. ret = afe_apr_send_pkt(&config, &this_afe.wait[IDX_GLOBAL_CFG]);
  791. if (ret)
  792. pr_err("%s: AFE_PARAM_ID_HW_MAD_CTRL failed %d\n", __func__,
  793. ret);
  794. return ret;
  795. }
  796. static int afe_send_slimbus_slave_cfg(
  797. struct afe_param_cdc_slimbus_slave_cfg *sb_slave_cfg)
  798. {
  799. int ret;
  800. struct afe_svc_cmd_sb_slave_cfg config;
  801. pr_debug("%s: enter\n", __func__);
  802. config.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  803. APR_HDR_LEN(APR_HDR_SIZE),
  804. APR_PKT_VER);
  805. config.hdr.pkt_size = sizeof(config);
  806. config.hdr.src_port = 0;
  807. config.hdr.dest_port = 0;
  808. config.hdr.token = IDX_GLOBAL_CFG;
  809. config.hdr.opcode = AFE_SVC_CMD_SET_PARAM;
  810. config.param.payload_size = sizeof(config) - sizeof(struct apr_hdr) -
  811. sizeof(config.param);
  812. config.param.payload_address_lsw = 0x00;
  813. config.param.payload_address_msw = 0x00;
  814. config.param.mem_map_handle = 0x00;
  815. config.pdata.module_id = AFE_MODULE_CDC_DEV_CFG;
  816. config.pdata.param_id = AFE_PARAM_ID_CDC_SLIMBUS_SLAVE_CFG;
  817. config.pdata.param_size =
  818. sizeof(struct afe_param_cdc_slimbus_slave_cfg);
  819. config.sb_slave_cfg = *sb_slave_cfg;
  820. ret = afe_apr_send_pkt(&config, &this_afe.wait[IDX_GLOBAL_CFG]);
  821. if (ret)
  822. pr_err("%s: AFE_PARAM_ID_CDC_SLIMBUS_SLAVE_CFG failed %d\n",
  823. __func__, ret);
  824. pr_debug("%s: leave %d\n", __func__, ret);
  825. return ret;
  826. }
  827. static int afe_send_codec_reg_config(
  828. struct afe_param_cdc_reg_cfg_data *cdc_reg_cfg)
  829. {
  830. int i, ret;
  831. int pkt_size, payload_size;
  832. struct afe_svc_cmd_cdc_reg_cfg *config;
  833. struct afe_svc_cmd_set_param *param;
  834. pr_debug("%s: enter\n", __func__);
  835. payload_size = sizeof(struct afe_param_cdc_reg_cfg_payload) *
  836. cdc_reg_cfg->num_registers;
  837. pkt_size = sizeof(*config) + payload_size;
  838. pr_debug("%s: pkt_size %d, payload %d\n", __func__, pkt_size,
  839. payload_size);
  840. config = kzalloc(pkt_size, GFP_KERNEL);
  841. if (!config) {
  842. pr_warn("%s: Not enought memory, pkt_size %d\n", __func__,
  843. pkt_size);
  844. return -ENOMEM;
  845. }
  846. config->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  847. APR_HDR_LEN(APR_HDR_SIZE),
  848. APR_PKT_VER);
  849. config->hdr.pkt_size = pkt_size;
  850. config->hdr.src_port = 0;
  851. config->hdr.dest_port = 0;
  852. config->hdr.token = IDX_GLOBAL_CFG;
  853. config->hdr.opcode = AFE_SVC_CMD_SET_PARAM;
  854. param = &config->param;
  855. param->payload_size = payload_size;
  856. param->payload_address_lsw = 0x00;
  857. param->payload_address_msw = 0x00;
  858. param->mem_map_handle = 0x00;
  859. for (i = 0; i < cdc_reg_cfg->num_registers; i++) {
  860. config->reg_data[i].common.module_id = AFE_MODULE_CDC_DEV_CFG;
  861. config->reg_data[i].common.param_id = AFE_PARAM_ID_CDC_REG_CFG;
  862. config->reg_data[i].common.param_size =
  863. sizeof(config->reg_data[i].reg_cfg);
  864. config->reg_data[i].reg_cfg = cdc_reg_cfg->reg_data[i];
  865. }
  866. ret = afe_apr_send_pkt(config, &this_afe.wait[IDX_GLOBAL_CFG]);
  867. if (ret)
  868. pr_err("%s: AFE_PARAM_ID_CDC_REG_CFG failed %d\n", __func__,
  869. ret);
  870. kfree(config);
  871. pr_debug("%s: leave ret %d\n", __func__, ret);
  872. return ret;
  873. }
  874. static int afe_init_cdc_reg_config(void)
  875. {
  876. int ret;
  877. struct afe_svc_cmd_init_cdc_reg_cfg config;
  878. pr_debug("%s: enter\n", __func__);
  879. config.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  880. APR_HDR_LEN(APR_HDR_SIZE),
  881. APR_PKT_VER);
  882. config.hdr.pkt_size = sizeof(config);
  883. config.hdr.src_port = 0;
  884. config.hdr.dest_port = 0;
  885. config.hdr.token = IDX_GLOBAL_CFG;
  886. config.hdr.opcode = AFE_SVC_CMD_SET_PARAM;
  887. config.param.payload_size = sizeof(struct afe_port_param_data_v2);
  888. config.param.payload_address_lsw = 0x00;
  889. config.param.payload_address_msw = 0x00;
  890. config.param.mem_map_handle = 0x00;
  891. config.init.module_id = AFE_MODULE_CDC_DEV_CFG;
  892. config.init.param_id = AFE_PARAM_ID_CDC_REG_CFG_INIT;
  893. config.init.param_size = 0;
  894. ret = afe_apr_send_pkt(&config, &this_afe.wait[IDX_GLOBAL_CFG]);
  895. if (ret) {
  896. pr_err("%s: AFE_PARAM_ID_CDC_INIT_REG_CFG failed %d\n",
  897. __func__, ret);
  898. } else if (atomic_read(&this_afe.status) != 0) {
  899. pr_err("%s: config cmd failed\n", __func__);
  900. ret = -EINVAL;
  901. }
  902. pr_debug("%s: leave ret %d\n", __func__, 0);
  903. return ret;
  904. }
  905. static int afe_send_slimbus_slave_port_cfg(
  906. struct afe_param_slimbus_slave_port_cfg *port_config, u16 port_id)
  907. {
  908. int ret, index;
  909. struct afe_cmd_hw_mad_slimbus_slave_port_cfg config;
  910. pr_debug("%s: enter, port_id %u\n", __func__, port_id);
  911. index = q6audio_get_port_index(port_id);
  912. if (q6audio_validate_port(port_id) < 0) {
  913. pr_err("%s: port id: %#x\n", __func__, port_id);
  914. return -EINVAL;
  915. }
  916. config.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  917. APR_HDR_LEN(APR_HDR_SIZE),
  918. APR_PKT_VER);
  919. config.hdr.pkt_size = sizeof(config);
  920. config.hdr.src_port = 0;
  921. config.hdr.dest_port = 0;
  922. config.hdr.token = index;
  923. config.hdr.opcode = AFE_PORT_CMD_SET_PARAM_V2;
  924. config.param.port_id = port_id;
  925. config.param.payload_size = sizeof(config) - sizeof(struct apr_hdr) -
  926. sizeof(config.param);
  927. config.param.payload_address_lsw = 0x00;
  928. config.param.payload_address_msw = 0x00;
  929. config.param.mem_map_handle = 0x00;
  930. config.pdata.module_id = AFE_MODULE_HW_MAD;
  931. config.pdata.param_id = AFE_PARAM_ID_SLIMBUS_SLAVE_PORT_CFG;
  932. config.pdata.param_size = sizeof(*port_config);
  933. config.sb_port_cfg = *port_config;
  934. ret = afe_apr_send_pkt(&config, &this_afe.wait[index]);
  935. if (ret) {
  936. pr_err("%s: AFE_PARAM_ID_SLIMBUS_SLAVE_PORT_CFG failed %d\n",
  937. __func__, ret);
  938. } else if (atomic_read(&this_afe.status) != 0) {
  939. pr_err("%s: config cmd failed\n", __func__);
  940. ret = -EINVAL;
  941. }
  942. pr_debug("%s: leave %d\n", __func__, ret);
  943. return ret;
  944. }
  945. static int afe_aanc_port_cfg(void *apr, uint16_t tx_port, uint16_t rx_port)
  946. {
  947. struct afe_port_cmd_set_aanc_param cfg;
  948. int ret = 0;
  949. int index = 0;
  950. pr_debug("%s: tx_port %d, rx_port %d\n",
  951. __func__, tx_port, rx_port);
  952. ret = afe_q6_interface_prepare();
  953. if (ret != 0)
  954. return -EINVAL;
  955. index = q6audio_get_port_index(tx_port);
  956. if (q6audio_validate_port(tx_port) < 0) {
  957. pr_err("%s: port id: %#x\n", __func__, tx_port);
  958. return -EINVAL;
  959. }
  960. cfg.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  961. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  962. cfg.hdr.pkt_size = sizeof(cfg);
  963. cfg.hdr.src_port = 0;
  964. cfg.hdr.dest_port = 0;
  965. cfg.hdr.token = index;
  966. cfg.hdr.opcode = AFE_PORT_CMD_SET_PARAM_V2;
  967. cfg.param.port_id = tx_port;
  968. cfg.param.payload_size = sizeof(struct afe_port_param_data_v2) +
  969. sizeof(struct afe_param_aanc_port_cfg);
  970. cfg.param.payload_address_lsw = 0;
  971. cfg.param.payload_address_msw = 0;
  972. cfg.param.mem_map_handle = 0;
  973. cfg.pdata.module_id = AFE_MODULE_AANC;
  974. cfg.pdata.param_id = AFE_PARAM_ID_AANC_PORT_CONFIG;
  975. cfg.pdata.param_size = sizeof(struct afe_param_aanc_port_cfg);
  976. cfg.pdata.reserved = 0;
  977. cfg.data.aanc_port_cfg.aanc_port_cfg_minor_version =
  978. AFE_API_VERSION_AANC_PORT_CONFIG;
  979. cfg.data.aanc_port_cfg.tx_port_sample_rate =
  980. this_afe.aanc_info.aanc_tx_port_sample_rate;
  981. cfg.data.aanc_port_cfg.tx_port_channel_map[0] = AANC_TX_VOICE_MIC;
  982. cfg.data.aanc_port_cfg.tx_port_channel_map[1] = AANC_TX_NOISE_MIC;
  983. cfg.data.aanc_port_cfg.tx_port_channel_map[2] = AANC_TX_ERROR_MIC;
  984. cfg.data.aanc_port_cfg.tx_port_channel_map[3] = AANC_TX_MIC_UNUSED;
  985. cfg.data.aanc_port_cfg.tx_port_channel_map[4] = AANC_TX_MIC_UNUSED;
  986. cfg.data.aanc_port_cfg.tx_port_channel_map[5] = AANC_TX_MIC_UNUSED;
  987. cfg.data.aanc_port_cfg.tx_port_channel_map[6] = AANC_TX_MIC_UNUSED;
  988. cfg.data.aanc_port_cfg.tx_port_channel_map[7] = AANC_TX_MIC_UNUSED;
  989. cfg.data.aanc_port_cfg.tx_port_num_channels = 3;
  990. cfg.data.aanc_port_cfg.rx_path_ref_port_id = rx_port;
  991. cfg.data.aanc_port_cfg.ref_port_sample_rate =
  992. this_afe.aanc_info.aanc_rx_port_sample_rate;
  993. ret = afe_apr_send_pkt((uint32_t *) &cfg, &this_afe.wait[index]);
  994. if (ret) {
  995. pr_err("%s: AFE AANC port config failed for tx_port %d, rx_port %d\n",
  996. __func__, tx_port, rx_port);
  997. } else if (atomic_read(&this_afe.status) != 0) {
  998. pr_err("%s: config cmd failed\n", __func__);
  999. ret = -EINVAL;
  1000. }
  1001. pr_debug("%s: leave %d\n", __func__, ret);
  1002. return ret;
  1003. }
  1004. static int afe_aanc_mod_enable(void *apr, uint16_t tx_port, uint16_t enable)
  1005. {
  1006. struct afe_port_cmd_set_aanc_param cfg;
  1007. int ret = 0;
  1008. int index = 0;
  1009. pr_debug("%s: tx_port %d\n",
  1010. __func__, tx_port);
  1011. ret = afe_q6_interface_prepare();
  1012. if (ret != 0)
  1013. return -EINVAL;
  1014. index = q6audio_get_port_index(tx_port);
  1015. if (q6audio_validate_port(tx_port) < 0) {
  1016. pr_err("%s: port id: %#x\n", __func__, tx_port);
  1017. return -EINVAL;
  1018. }
  1019. cfg.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1020. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  1021. cfg.hdr.pkt_size = sizeof(cfg);
  1022. cfg.hdr.src_port = 0;
  1023. cfg.hdr.dest_port = 0;
  1024. cfg.hdr.token = index;
  1025. cfg.hdr.opcode = AFE_PORT_CMD_SET_PARAM_V2;
  1026. cfg.param.port_id = tx_port;
  1027. cfg.param.payload_size = sizeof(struct afe_port_param_data_v2) +
  1028. sizeof(struct afe_mod_enable_param);
  1029. cfg.param.payload_address_lsw = 0;
  1030. cfg.param.payload_address_lsw = 0;
  1031. cfg.param.mem_map_handle = 0;
  1032. cfg.pdata.module_id = AFE_MODULE_AANC;
  1033. cfg.pdata.param_id = AFE_PARAM_ID_ENABLE;
  1034. cfg.pdata.param_size = sizeof(struct afe_mod_enable_param);
  1035. cfg.pdata.reserved = 0;
  1036. cfg.data.mod_enable.enable = enable;
  1037. cfg.data.mod_enable.reserved = 0;
  1038. ret = afe_apr_send_pkt((uint32_t *) &cfg, &this_afe.wait[index]);
  1039. if (ret) {
  1040. pr_err("%s: AFE AANC enable failed for tx_port %d\n",
  1041. __func__, tx_port);
  1042. } else if (atomic_read(&this_afe.status) != 0) {
  1043. pr_err("%s: config cmd failed\n", __func__);
  1044. ret = -EINVAL;
  1045. }
  1046. pr_debug("%s: leave %d\n", __func__, ret);
  1047. return ret;
  1048. }
  1049. static int afe_send_bank_selection_clip(
  1050. struct afe_param_id_clip_bank_sel *param)
  1051. {
  1052. int ret;
  1053. struct afe_svc_cmd_set_clip_bank_selection config;
  1054. if (!param) {
  1055. pr_err("%s: Invalid params", __func__);
  1056. return -EINVAL;
  1057. }
  1058. config.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1059. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  1060. config.hdr.pkt_size = sizeof(config);
  1061. config.hdr.src_port = 0;
  1062. config.hdr.dest_port = 0;
  1063. config.hdr.token = IDX_GLOBAL_CFG;
  1064. config.hdr.opcode = AFE_SVC_CMD_SET_PARAM;
  1065. config.param.payload_size = sizeof(struct afe_port_param_data_v2) +
  1066. sizeof(struct afe_param_id_clip_bank_sel);
  1067. config.param.payload_address_lsw = 0x00;
  1068. config.param.payload_address_msw = 0x00;
  1069. config.param.mem_map_handle = 0x00;
  1070. config.pdata.module_id = AFE_MODULE_CDC_DEV_CFG;
  1071. config.pdata.param_id = AFE_PARAM_ID_CLIP_BANK_SEL_CFG;
  1072. config.pdata.param_size =
  1073. sizeof(struct afe_param_id_clip_bank_sel);
  1074. config.bank_sel = *param;
  1075. ret = afe_apr_send_pkt(&config, &this_afe.wait[IDX_GLOBAL_CFG]);
  1076. if (ret) {
  1077. pr_err("%s: AFE_PARAM_ID_CLIP_BANK_SEL_CFG failed %d\n",
  1078. __func__, ret);
  1079. } else if (atomic_read(&this_afe.status) != 0) {
  1080. pr_err("%s: config cmd failed\n", __func__);
  1081. ret = -EAGAIN;
  1082. }
  1083. return ret;
  1084. }
  1085. int afe_send_aanc_version(
  1086. struct afe_param_id_cdc_aanc_version *version_cfg)
  1087. {
  1088. int ret;
  1089. struct afe_svc_cmd_cdc_aanc_version config;
  1090. pr_debug("%s: enter\n", __func__);
  1091. config.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1092. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  1093. config.hdr.pkt_size = sizeof(config);
  1094. config.hdr.src_port = 0;
  1095. config.hdr.dest_port = 0;
  1096. config.hdr.token = IDX_GLOBAL_CFG;
  1097. config.hdr.opcode = AFE_SVC_CMD_SET_PARAM;
  1098. config.param.payload_size = sizeof(struct afe_port_param_data_v2) +
  1099. sizeof(struct afe_param_id_cdc_aanc_version);
  1100. config.param.payload_address_lsw = 0x00;
  1101. config.param.payload_address_msw = 0x00;
  1102. config.param.mem_map_handle = 0x00;
  1103. config.pdata.module_id = AFE_MODULE_CDC_DEV_CFG;
  1104. config.pdata.param_id = AFE_PARAM_ID_CDC_AANC_VERSION;
  1105. config.pdata.param_size =
  1106. sizeof(struct afe_param_id_cdc_aanc_version);
  1107. config.version = *version_cfg;
  1108. ret = afe_apr_send_pkt(&config, &this_afe.wait[IDX_GLOBAL_CFG]);
  1109. if (ret) {
  1110. pr_err("%s: AFE_PARAM_ID_CDC_AANC_VERSION failed %d\n",
  1111. __func__, ret);
  1112. } else if (atomic_read(&this_afe.status) != 0) {
  1113. pr_err("%s: config cmd failed\n", __func__);
  1114. ret = -EINVAL;
  1115. }
  1116. pr_debug("%s: leave ret %d\n", __func__, 0);
  1117. return ret;
  1118. }
  1119. int afe_port_set_mad_type(u16 port_id, enum afe_mad_type mad_type)
  1120. {
  1121. int i;
  1122. i = port_id - SLIMBUS_0_RX;
  1123. if (i < 0 || i >= ARRAY_SIZE(afe_ports_mad_type)) {
  1124. pr_err("%s: Invalid port_id 0x%x\n", __func__, port_id);
  1125. return -EINVAL;
  1126. }
  1127. atomic_set(&afe_ports_mad_type[i], mad_type);
  1128. return 0;
  1129. }
  1130. enum afe_mad_type afe_port_get_mad_type(u16 port_id)
  1131. {
  1132. int i;
  1133. i = port_id - SLIMBUS_0_RX;
  1134. if (i < 0 || i >= ARRAY_SIZE(afe_ports_mad_type)) {
  1135. pr_debug("%s: Non Slimbus port_id 0x%x\n", __func__, port_id);
  1136. return MAD_HW_NONE;
  1137. }
  1138. return (enum afe_mad_type) atomic_read(&afe_ports_mad_type[i]);
  1139. }
  1140. int afe_set_config(enum afe_config_type config_type, void *config_data, int arg)
  1141. {
  1142. int ret;
  1143. pr_debug("%s: enter config_type %d\n", __func__, config_type);
  1144. ret = afe_q6_interface_prepare();
  1145. if (ret) {
  1146. pr_err("%s: Q6 interface prepare failed %d\n", __func__, ret);
  1147. return ret;
  1148. }
  1149. switch (config_type) {
  1150. case AFE_SLIMBUS_SLAVE_CONFIG:
  1151. ret = afe_send_slimbus_slave_cfg(config_data);
  1152. if (!ret)
  1153. ret = afe_init_cdc_reg_config();
  1154. else
  1155. pr_err("%s: Sending slimbus slave config failed %d\n",
  1156. __func__, ret);
  1157. break;
  1158. case AFE_CDC_REGISTERS_CONFIG:
  1159. ret = afe_send_codec_reg_config(config_data);
  1160. break;
  1161. case AFE_SLIMBUS_SLAVE_PORT_CONFIG:
  1162. ret = afe_send_slimbus_slave_port_cfg(config_data, arg);
  1163. break;
  1164. case AFE_AANC_VERSION:
  1165. ret = afe_send_aanc_version(config_data);
  1166. break;
  1167. case AFE_CLIP_BANK_SEL:
  1168. ret = afe_send_bank_selection_clip(config_data);
  1169. break;
  1170. case AFE_CDC_CLIP_REGISTERS_CONFIG:
  1171. ret = afe_send_codec_reg_config(config_data);
  1172. break;
  1173. default:
  1174. pr_err("%s: unknown configuration type", __func__);
  1175. ret = -EINVAL;
  1176. }
  1177. if (!ret)
  1178. set_bit(config_type, &afe_configured_cmd);
  1179. pr_debug("%s: leave ret %d\n", __func__, ret);
  1180. return ret;
  1181. }
  1182. /*
  1183. * afe_clear_config - If SSR happens ADSP loses AFE configs, let AFE driver know
  1184. * about the state so client driver can wait until AFE is
  1185. * reconfigured.
  1186. */
  1187. void afe_clear_config(enum afe_config_type config)
  1188. {
  1189. clear_bit(config, &afe_configured_cmd);
  1190. }
  1191. bool afe_has_config(enum afe_config_type config)
  1192. {
  1193. return !!test_bit(config, &afe_configured_cmd);
  1194. }
  1195. static int afe_send_cmd_port_start(u16 port_id)
  1196. {
  1197. struct afe_port_cmd_device_start start;
  1198. int ret, index;
  1199. pr_debug("%s: enter\n", __func__);
  1200. index = q6audio_get_port_index(port_id);
  1201. if (q6audio_validate_port(port_id) < 0) {
  1202. pr_err("%s: port id: %#x\n", __func__, port_id);
  1203. return -EINVAL;
  1204. }
  1205. start.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1206. APR_HDR_LEN(APR_HDR_SIZE),
  1207. APR_PKT_VER);
  1208. start.hdr.pkt_size = sizeof(start);
  1209. start.hdr.src_port = 0;
  1210. start.hdr.dest_port = 0;
  1211. start.hdr.token = index;
  1212. start.hdr.opcode = AFE_PORT_CMD_DEVICE_START;
  1213. start.port_id = q6audio_get_port_id(port_id);
  1214. pr_debug("%s: cmd device start opcode[0x%x] port id[0x%x]\n",
  1215. __func__, start.hdr.opcode, start.port_id);
  1216. ret = afe_apr_send_pkt(&start, &this_afe.wait[index]);
  1217. if (ret) {
  1218. pr_err("%s: AFE enable for port %#x failed %d\n", __func__,
  1219. port_id, ret);
  1220. } else if (this_afe.task != current) {
  1221. this_afe.task = current;
  1222. pr_debug("task_name = %s pid = %d\n",
  1223. this_afe.task->comm, this_afe.task->pid);
  1224. }
  1225. pr_debug("%s: leave %d\n", __func__, ret);
  1226. return ret;
  1227. }
  1228. static int afe_aanc_start(uint16_t tx_port_id, uint16_t rx_port_id)
  1229. {
  1230. int ret;
  1231. pr_debug("%s Tx port is %d, Rx port is %d\n",
  1232. __func__, tx_port_id, rx_port_id);
  1233. ret = afe_aanc_port_cfg(this_afe.apr, tx_port_id, rx_port_id);
  1234. if (ret) {
  1235. pr_err("%s Send AANC Port Config failed %d\n",
  1236. __func__, ret);
  1237. goto fail_cmd;
  1238. }
  1239. afe_send_cal_block(AFE_AANC_TX_CAL, tx_port_id);
  1240. fail_cmd:
  1241. return ret;
  1242. }
  1243. int afe_port_start(u16 port_id, union afe_port_config *afe_config,
  1244. u32 rate) /* This function is no blocking */
  1245. {
  1246. struct afe_audioif_config_command config;
  1247. int ret = 0;
  1248. int cfg_type;
  1249. int index = 0;
  1250. enum afe_mad_type mad_type;
  1251. uint16_t port_index;
  1252. if (!afe_config) {
  1253. pr_err("%s: Error, no configuration data\n", __func__);
  1254. ret = -EINVAL;
  1255. return ret;
  1256. }
  1257. if ((port_id == RT_PROXY_DAI_001_RX) ||
  1258. (port_id == RT_PROXY_DAI_002_TX)) {
  1259. pr_debug("%s: before incrementing pcm_afe_instance %d"\
  1260. " port_id %d\n", __func__,
  1261. pcm_afe_instance[port_id & 0x1], port_id);
  1262. port_id = VIRTUAL_ID_TO_PORTID(port_id);
  1263. pcm_afe_instance[port_id & 0x1]++;
  1264. return 0;
  1265. }
  1266. if ((port_id == RT_PROXY_DAI_002_RX) ||
  1267. (port_id == RT_PROXY_DAI_001_TX)) {
  1268. pr_debug("%s: before incrementing proxy_afe_instance %d"\
  1269. " port_id %d\n", __func__,
  1270. proxy_afe_instance[port_id & 0x1], port_id);
  1271. if (!afe_close_done[port_id & 0x1]) {
  1272. /*close pcm dai corresponding to the proxy dai*/
  1273. afe_close(port_id - 0x10);
  1274. pcm_afe_instance[port_id & 0x1]++;
  1275. pr_debug("%s: reconfigure afe port again\n", __func__);
  1276. }
  1277. proxy_afe_instance[port_id & 0x1]++;
  1278. afe_close_done[port_id & 0x1] = false;
  1279. port_id = VIRTUAL_ID_TO_PORTID(port_id);
  1280. }
  1281. pr_debug("%s: port id: %#x\n", __func__, port_id);
  1282. index = q6audio_get_port_index(port_id);
  1283. if (q6audio_validate_port(port_id) < 0) {
  1284. pr_err("%s: port id: %#x\n", __func__, port_id);
  1285. return -EINVAL;
  1286. }
  1287. ret = afe_q6_interface_prepare();
  1288. if (IS_ERR_VALUE(ret))
  1289. return ret;
  1290. afe_send_cal(port_id);
  1291. afe_send_hw_delay(port_id, rate);
  1292. /* Start SW MAD module */
  1293. mad_type = afe_port_get_mad_type(port_id);
  1294. pr_debug("%s: port_id 0x%x, mad_type %d\n", __func__, port_id,
  1295. mad_type);
  1296. if (mad_type != MAD_HW_NONE && mad_type != MAD_SW_AUDIO) {
  1297. if (!afe_has_config(AFE_CDC_REGISTERS_CONFIG) ||
  1298. !afe_has_config(AFE_SLIMBUS_SLAVE_CONFIG)) {
  1299. pr_err("%s: AFE isn't configured yet for HW MAD\n",
  1300. __func__);
  1301. return -EINVAL;
  1302. }
  1303. ret = afe_turn_onoff_hw_mad(mad_type, true);
  1304. if (ret) {
  1305. pr_err("%s: afe_turn_onoff_hw_mad failed %d\n",
  1306. __func__, ret);
  1307. return ret;
  1308. }
  1309. }
  1310. if ((this_afe.aanc_info.aanc_active) &&
  1311. (this_afe.aanc_info.aanc_tx_port == port_id)) {
  1312. this_afe.aanc_info.aanc_tx_port_sample_rate = rate;
  1313. port_index =
  1314. afe_get_port_index(this_afe.aanc_info.aanc_rx_port);
  1315. if ((port_index >= 0) && (port_index < AFE_MAX_PORTS)) {
  1316. this_afe.aanc_info.aanc_rx_port_sample_rate =
  1317. this_afe.afe_sample_rates[port_index];
  1318. } else {
  1319. ret = -EINVAL;
  1320. goto fail_cmd;
  1321. }
  1322. ret = afe_aanc_start(this_afe.aanc_info.aanc_tx_port,
  1323. this_afe.aanc_info.aanc_rx_port);
  1324. pr_debug("%s afe_aanc_start ret %d\n", __func__, ret);
  1325. }
  1326. config.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1327. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  1328. config.hdr.pkt_size = sizeof(config);
  1329. config.hdr.src_port = 0;
  1330. config.hdr.dest_port = 0;
  1331. config.hdr.token = index;
  1332. switch (port_id) {
  1333. case AFE_PORT_ID_PRIMARY_PCM_RX:
  1334. case AFE_PORT_ID_PRIMARY_PCM_TX:
  1335. case AFE_PORT_ID_SECONDARY_PCM_RX:
  1336. case AFE_PORT_ID_SECONDARY_PCM_TX:
  1337. cfg_type = AFE_PARAM_ID_PCM_CONFIG;
  1338. break;
  1339. case PRIMARY_I2S_RX:
  1340. case PRIMARY_I2S_TX:
  1341. case SECONDARY_I2S_RX:
  1342. case SECONDARY_I2S_TX:
  1343. case MI2S_RX:
  1344. case MI2S_TX:
  1345. case AFE_PORT_ID_PRIMARY_MI2S_RX:
  1346. case AFE_PORT_ID_PRIMARY_MI2S_TX:
  1347. case AFE_PORT_ID_SECONDARY_MI2S_RX:
  1348. case AFE_PORT_ID_SECONDARY_MI2S_TX:
  1349. case AFE_PORT_ID_TERTIARY_MI2S_RX:
  1350. case AFE_PORT_ID_TERTIARY_MI2S_TX:
  1351. case AFE_PORT_ID_QUATERNARY_MI2S_RX:
  1352. case AFE_PORT_ID_QUATERNARY_MI2S_TX:
  1353. cfg_type = AFE_PARAM_ID_I2S_CONFIG;
  1354. break;
  1355. case HDMI_RX:
  1356. cfg_type = AFE_PARAM_ID_HDMI_CONFIG;
  1357. break;
  1358. case VOICE_PLAYBACK_TX:
  1359. case VOICE2_PLAYBACK_TX:
  1360. case VOICE_RECORD_RX:
  1361. case VOICE_RECORD_TX:
  1362. cfg_type = AFE_PARAM_ID_PSEUDO_PORT_CONFIG;
  1363. break;
  1364. case SLIMBUS_0_RX:
  1365. case SLIMBUS_0_TX:
  1366. case SLIMBUS_1_RX:
  1367. case SLIMBUS_1_TX:
  1368. case SLIMBUS_2_RX:
  1369. case SLIMBUS_2_TX:
  1370. case SLIMBUS_3_RX:
  1371. case SLIMBUS_3_TX:
  1372. case SLIMBUS_4_RX:
  1373. case SLIMBUS_4_TX:
  1374. case SLIMBUS_5_TX:
  1375. cfg_type = AFE_PARAM_ID_SLIMBUS_CONFIG;
  1376. break;
  1377. case RT_PROXY_PORT_001_RX:
  1378. case RT_PROXY_PORT_001_TX:
  1379. cfg_type = AFE_PARAM_ID_RT_PROXY_CONFIG;
  1380. break;
  1381. case INT_BT_SCO_RX:
  1382. case INT_BT_SCO_TX:
  1383. case INT_FM_RX:
  1384. case INT_FM_TX:
  1385. cfg_type = AFE_PARAM_ID_INTERNAL_BT_FM_CONFIG;
  1386. break;
  1387. default:
  1388. pr_err("%s: Invalid port id 0x%x\n", __func__, port_id);
  1389. ret = -EINVAL;
  1390. goto fail_cmd;
  1391. }
  1392. config.hdr.opcode = AFE_PORT_CMD_SET_PARAM_V2;
  1393. config.param.port_id = q6audio_get_port_id(port_id);
  1394. config.param.payload_size = sizeof(config) - sizeof(struct apr_hdr) -
  1395. sizeof(config.param);
  1396. config.param.payload_address_lsw = 0x00;
  1397. config.param.payload_address_msw = 0x00;
  1398. config.param.mem_map_handle = 0x00;
  1399. config.pdata.module_id = AFE_MODULE_AUDIO_DEV_INTERFACE;
  1400. config.pdata.param_id = cfg_type;
  1401. config.pdata.param_size = sizeof(config.port);
  1402. config.port = *afe_config;
  1403. ret = afe_apr_send_pkt(&config, &this_afe.wait[index]);
  1404. if (ret) {
  1405. pr_err("%s: AFE enable for port %#x failed\n", __func__,
  1406. port_id);
  1407. goto fail_cmd;
  1408. } else if (atomic_read(&this_afe.status) != 0) {
  1409. pr_err("%s: config cmd failed\n", __func__);
  1410. ret = -EINVAL;
  1411. goto fail_cmd;
  1412. }
  1413. port_index = afe_get_port_index(port_id);
  1414. if ((port_index >= 0) && (port_index < AFE_MAX_PORTS)) {
  1415. this_afe.afe_sample_rates[port_index] = rate;
  1416. } else {
  1417. ret = -EINVAL;
  1418. goto fail_cmd;
  1419. }
  1420. return afe_send_cmd_port_start(port_id);
  1421. fail_cmd:
  1422. return ret;
  1423. }
  1424. int afe_get_port_index(u16 port_id)
  1425. {
  1426. switch (port_id) {
  1427. case PRIMARY_I2S_RX: return IDX_PRIMARY_I2S_RX;
  1428. case PRIMARY_I2S_TX: return IDX_PRIMARY_I2S_TX;
  1429. case AFE_PORT_ID_PRIMARY_PCM_RX:
  1430. return IDX_AFE_PORT_ID_PRIMARY_PCM_RX;
  1431. case AFE_PORT_ID_PRIMARY_PCM_TX:
  1432. return IDX_AFE_PORT_ID_PRIMARY_PCM_TX;
  1433. case AFE_PORT_ID_SECONDARY_PCM_RX:
  1434. return IDX_AFE_PORT_ID_SECONDARY_PCM_RX;
  1435. case AFE_PORT_ID_SECONDARY_PCM_TX:
  1436. return IDX_AFE_PORT_ID_SECONDARY_PCM_TX;
  1437. case SECONDARY_I2S_RX: return IDX_SECONDARY_I2S_RX;
  1438. case SECONDARY_I2S_TX: return IDX_SECONDARY_I2S_TX;
  1439. case MI2S_RX: return IDX_MI2S_RX;
  1440. case MI2S_TX: return IDX_MI2S_TX;
  1441. case HDMI_RX: return IDX_HDMI_RX;
  1442. case RSVD_2: return IDX_RSVD_2;
  1443. case RSVD_3: return IDX_RSVD_3;
  1444. case DIGI_MIC_TX: return IDX_DIGI_MIC_TX;
  1445. case VOICE_RECORD_RX: return IDX_VOICE_RECORD_RX;
  1446. case VOICE_RECORD_TX: return IDX_VOICE_RECORD_TX;
  1447. case VOICE_PLAYBACK_TX: return IDX_VOICE_PLAYBACK_TX;
  1448. case VOICE2_PLAYBACK_TX: return IDX_VOICE2_PLAYBACK_TX;
  1449. case SLIMBUS_0_RX: return IDX_SLIMBUS_0_RX;
  1450. case SLIMBUS_0_TX: return IDX_SLIMBUS_0_TX;
  1451. case SLIMBUS_1_RX: return IDX_SLIMBUS_1_RX;
  1452. case SLIMBUS_1_TX: return IDX_SLIMBUS_1_TX;
  1453. case SLIMBUS_2_RX: return IDX_SLIMBUS_2_RX;
  1454. case SLIMBUS_2_TX: return IDX_SLIMBUS_2_TX;
  1455. case SLIMBUS_3_RX: return IDX_SLIMBUS_3_RX;
  1456. case SLIMBUS_3_TX: return IDX_SLIMBUS_3_TX;
  1457. case INT_BT_SCO_RX: return IDX_INT_BT_SCO_RX;
  1458. case INT_BT_SCO_TX: return IDX_INT_BT_SCO_TX;
  1459. case INT_BT_A2DP_RX: return IDX_INT_BT_A2DP_RX;
  1460. case INT_FM_RX: return IDX_INT_FM_RX;
  1461. case INT_FM_TX: return IDX_INT_FM_TX;
  1462. case RT_PROXY_PORT_001_RX: return IDX_RT_PROXY_PORT_001_RX;
  1463. case RT_PROXY_PORT_001_TX: return IDX_RT_PROXY_PORT_001_TX;
  1464. case SLIMBUS_4_RX: return IDX_SLIMBUS_4_RX;
  1465. case SLIMBUS_4_TX: return IDX_SLIMBUS_4_TX;
  1466. case SLIMBUS_5_TX: return IDX_SLIMBUS_5_TX;
  1467. case AFE_PORT_ID_PRIMARY_MI2S_RX:
  1468. return IDX_AFE_PORT_ID_PRIMARY_MI2S_RX;
  1469. case AFE_PORT_ID_PRIMARY_MI2S_TX:
  1470. return IDX_AFE_PORT_ID_PRIMARY_MI2S_TX;
  1471. case AFE_PORT_ID_QUATERNARY_MI2S_RX:
  1472. return IDX_AFE_PORT_ID_QUATERNARY_MI2S_RX;
  1473. case AFE_PORT_ID_QUATERNARY_MI2S_TX:
  1474. return IDX_AFE_PORT_ID_QUATERNARY_MI2S_TX;
  1475. case AFE_PORT_ID_SECONDARY_MI2S_RX:
  1476. return IDX_AFE_PORT_ID_SECONDARY_MI2S_RX;
  1477. case AFE_PORT_ID_SECONDARY_MI2S_TX:
  1478. return IDX_AFE_PORT_ID_SECONDARY_MI2S_TX;
  1479. case AFE_PORT_ID_TERTIARY_MI2S_RX:
  1480. return IDX_AFE_PORT_ID_TERTIARY_MI2S_RX;
  1481. case AFE_PORT_ID_TERTIARY_MI2S_TX:
  1482. return IDX_AFE_PORT_ID_TERTIARY_MI2S_TX;
  1483. default: return -EINVAL;
  1484. }
  1485. }
  1486. int afe_open(u16 port_id,
  1487. union afe_port_config *afe_config, int rate)
  1488. {
  1489. struct afe_port_cmd_device_start start;
  1490. struct afe_audioif_config_command config;
  1491. int ret = 0;
  1492. int cfg_type;
  1493. int index = 0;
  1494. if (!afe_config) {
  1495. pr_err("%s: Error, no configuration data\n", __func__);
  1496. ret = -EINVAL;
  1497. return ret;
  1498. }
  1499. pr_err("%s: %d %d\n", __func__, port_id, rate);
  1500. index = q6audio_get_port_index(port_id);
  1501. if (q6audio_validate_port(port_id) < 0)
  1502. return -EINVAL;
  1503. if ((port_id == RT_PROXY_DAI_001_RX) ||
  1504. (port_id == RT_PROXY_DAI_002_TX))
  1505. return -EINVAL;
  1506. if ((port_id == RT_PROXY_DAI_002_RX) ||
  1507. (port_id == RT_PROXY_DAI_001_TX))
  1508. port_id = VIRTUAL_ID_TO_PORTID(port_id);
  1509. ret = afe_q6_interface_prepare();
  1510. if (ret != 0)
  1511. return ret;
  1512. if (q6audio_validate_port(port_id) < 0) {
  1513. pr_err("%s: Failed : Invalid Port id = %d\n", __func__,
  1514. port_id);
  1515. ret = -EINVAL;
  1516. goto fail_cmd;
  1517. }
  1518. config.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1519. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  1520. config.hdr.pkt_size = sizeof(config);
  1521. config.hdr.src_port = 0;
  1522. config.hdr.dest_port = 0;
  1523. config.hdr.token = index;
  1524. switch (port_id) {
  1525. case PRIMARY_I2S_RX:
  1526. case PRIMARY_I2S_TX:
  1527. cfg_type = AFE_PARAM_ID_I2S_CONFIG;
  1528. break;
  1529. case AFE_PORT_ID_PRIMARY_PCM_RX:
  1530. case AFE_PORT_ID_PRIMARY_PCM_TX:
  1531. case AFE_PORT_ID_SECONDARY_PCM_RX:
  1532. case AFE_PORT_ID_SECONDARY_PCM_TX:
  1533. cfg_type = AFE_PARAM_ID_PCM_CONFIG;
  1534. break;
  1535. case SECONDARY_I2S_RX:
  1536. case SECONDARY_I2S_TX:
  1537. case AFE_PORT_ID_PRIMARY_MI2S_RX:
  1538. case MI2S_RX:
  1539. case MI2S_TX:
  1540. cfg_type = AFE_PARAM_ID_I2S_CONFIG;
  1541. break;
  1542. case HDMI_RX:
  1543. cfg_type = AFE_PARAM_ID_HDMI_CONFIG;
  1544. break;
  1545. case SLIMBUS_0_RX:
  1546. case SLIMBUS_0_TX:
  1547. case SLIMBUS_1_RX:
  1548. case SLIMBUS_1_TX:
  1549. case SLIMBUS_2_RX:
  1550. case SLIMBUS_2_TX:
  1551. case SLIMBUS_3_RX:
  1552. case SLIMBUS_3_TX:
  1553. case SLIMBUS_4_RX:
  1554. case SLIMBUS_4_TX:
  1555. cfg_type = AFE_PARAM_ID_SLIMBUS_CONFIG;
  1556. break;
  1557. default:
  1558. pr_err("%s: Invalid port id 0x%x\n", __func__, port_id);
  1559. ret = -EINVAL;
  1560. goto fail_cmd;
  1561. }
  1562. config.hdr.opcode = AFE_PORT_CMD_SET_PARAM_V2;
  1563. config.param.port_id = q6audio_get_port_id(port_id);
  1564. config.param.payload_size = sizeof(config) - sizeof(struct apr_hdr)
  1565. - sizeof(config.param);
  1566. config.param.payload_address_lsw = 0x00;
  1567. config.param.payload_address_msw = 0x00;
  1568. config.param.mem_map_handle = 0x00;
  1569. config.pdata.module_id = AFE_MODULE_AUDIO_DEV_INTERFACE;
  1570. config.pdata.param_id = cfg_type;
  1571. config.pdata.param_size = sizeof(config.port);
  1572. config.port = *afe_config;
  1573. pr_debug("%s: param PL size=%d iparam_size[%d][%d %d %d %d] param_id[%x]\n",
  1574. __func__, config.param.payload_size, config.pdata.param_size,
  1575. sizeof(config), sizeof(config.param), sizeof(config.port),
  1576. sizeof(struct apr_hdr), config.pdata.param_id);
  1577. ret = afe_apr_send_pkt(&config, &this_afe.wait[index]);
  1578. if (ret) {
  1579. pr_err("%s: AFE enable for port %d opcode[0x%x]failed\n",
  1580. __func__, port_id, cfg_type);
  1581. goto fail_cmd;
  1582. } else if (atomic_read(&this_afe.status) != 0) {
  1583. pr_err("%s: config cmd failed\n", __func__);
  1584. ret = -EINVAL;
  1585. goto fail_cmd;
  1586. }
  1587. start.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1588. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  1589. start.hdr.pkt_size = sizeof(start);
  1590. start.hdr.src_port = 0;
  1591. start.hdr.dest_port = 0;
  1592. start.hdr.token = index;
  1593. start.hdr.opcode = AFE_PORT_CMD_DEVICE_START;
  1594. start.port_id = q6audio_get_port_id(port_id);
  1595. pr_debug("%s: cmd device start opcode[0x%x] port id[0x%x]\n",
  1596. __func__, start.hdr.opcode, start.port_id);
  1597. ret = afe_apr_send_pkt(&start, &this_afe.wait[index]);
  1598. if (ret) {
  1599. pr_err("%s: AFE enable for port %d failed\n", __func__,
  1600. port_id);
  1601. goto fail_cmd;
  1602. }
  1603. fail_cmd:
  1604. return ret;
  1605. }
  1606. int afe_loopback(u16 enable, u16 rx_port, u16 tx_port)
  1607. {
  1608. struct afe_loopback_cfg_v1 lb_cmd;
  1609. int ret = 0;
  1610. int index = 0;
  1611. if (rx_port == MI2S_RX)
  1612. rx_port = AFE_PORT_ID_PRIMARY_MI2S_RX;
  1613. if (tx_port == MI2S_TX)
  1614. tx_port = AFE_PORT_ID_PRIMARY_MI2S_TX;
  1615. ret = afe_q6_interface_prepare();
  1616. if (ret != 0)
  1617. return ret;
  1618. index = q6audio_get_port_index(rx_port);
  1619. if (q6audio_validate_port(rx_port) < 0)
  1620. return -EINVAL;
  1621. lb_cmd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1622. APR_HDR_LEN(20), APR_PKT_VER);
  1623. lb_cmd.hdr.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  1624. sizeof(lb_cmd) - APR_HDR_SIZE);
  1625. lb_cmd.hdr.src_port = 0;
  1626. lb_cmd.hdr.dest_port = 0;
  1627. lb_cmd.hdr.token = index;
  1628. lb_cmd.hdr.opcode = AFE_PORT_CMD_SET_PARAM_V2;
  1629. lb_cmd.param.port_id = tx_port;
  1630. lb_cmd.param.payload_size = (sizeof(lb_cmd) - sizeof(struct apr_hdr) -
  1631. sizeof(struct afe_port_cmd_set_param_v2));
  1632. lb_cmd.param.payload_address_lsw = 0x00;
  1633. lb_cmd.param.payload_address_msw = 0x00;
  1634. lb_cmd.param.mem_map_handle = 0x00;
  1635. lb_cmd.pdata.module_id = AFE_MODULE_LOOPBACK;
  1636. lb_cmd.pdata.param_id = AFE_PARAM_ID_LOOPBACK_CONFIG;
  1637. lb_cmd.pdata.param_size = lb_cmd.param.payload_size -
  1638. sizeof(struct afe_port_param_data_v2);
  1639. lb_cmd.dst_port_id = rx_port;
  1640. lb_cmd.routing_mode = LB_MODE_DEFAULT;
  1641. lb_cmd.enable = (enable ? 1 : 0);
  1642. lb_cmd.loopback_cfg_minor_version = AFE_API_VERSION_LOOPBACK_CONFIG;
  1643. ret = afe_apr_send_pkt(&lb_cmd, &this_afe.wait[index]);
  1644. if (ret)
  1645. pr_err("%s: AFE loopback failed %d\n", __func__, ret);
  1646. return ret;
  1647. }
  1648. int afe_loopback_gain(u16 port_id, u16 volume)
  1649. {
  1650. struct afe_loopback_gain_per_path_param set_param;
  1651. int ret = 0;
  1652. int index = 0;
  1653. if (this_afe.apr == NULL) {
  1654. this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
  1655. 0xFFFFFFFF, &this_afe);
  1656. pr_debug("%s: Register AFE\n", __func__);
  1657. if (this_afe.apr == NULL) {
  1658. pr_err("%s: Unable to register AFE\n", __func__);
  1659. ret = -ENODEV;
  1660. return ret;
  1661. }
  1662. }
  1663. if (q6audio_validate_port(port_id) < 0) {
  1664. pr_err("%s: Failed : Invalid Port id = %d\n", __func__,
  1665. port_id);
  1666. ret = -EINVAL;
  1667. goto fail_cmd;
  1668. }
  1669. index = q6audio_get_port_index(port_id);
  1670. if (q6audio_validate_port(port_id) < 0)
  1671. return -EINVAL;
  1672. /* RX ports numbers are even .TX ports numbers are odd. */
  1673. if (port_id % 2 == 0) {
  1674. pr_err("%s: Failed : afe loopback gain only for TX ports. port_id %d\n",
  1675. __func__, port_id);
  1676. ret = -EINVAL;
  1677. goto fail_cmd;
  1678. }
  1679. pr_debug("%s: %d %hX\n", __func__, port_id, volume);
  1680. set_param.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1681. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  1682. set_param.hdr.pkt_size = sizeof(set_param);
  1683. set_param.hdr.src_port = 0;
  1684. set_param.hdr.dest_port = 0;
  1685. set_param.hdr.token = index;
  1686. set_param.hdr.opcode = AFE_PORT_CMD_SET_PARAM_V2;
  1687. set_param.param.port_id = port_id;
  1688. set_param.param.payload_size =
  1689. (sizeof(struct afe_loopback_gain_per_path_param) -
  1690. sizeof(struct apr_hdr) - sizeof(struct afe_port_cmd_set_param_v2));
  1691. set_param.param.payload_address_lsw = 0;
  1692. set_param.param.payload_address_msw = 0;
  1693. set_param.param.mem_map_handle = 0;
  1694. set_param.pdata.module_id = AFE_MODULE_LOOPBACK;
  1695. set_param.pdata.param_id = AFE_PARAM_ID_LOOPBACK_GAIN_PER_PATH;
  1696. set_param.pdata.param_size =
  1697. (set_param.param.payload_size -
  1698. sizeof(struct afe_port_param_data_v2));
  1699. set_param.rx_port_id = port_id;
  1700. set_param.gain = volume;
  1701. ret = afe_apr_send_pkt(&set_param, &this_afe.wait[index]);
  1702. if (ret) {
  1703. pr_err("%s: AFE param set failed for port %d\n",
  1704. __func__, port_id);
  1705. goto fail_cmd;
  1706. }
  1707. fail_cmd:
  1708. return ret;
  1709. }
  1710. int afe_pseudo_port_start_nowait(u16 port_id)
  1711. {
  1712. struct afe_pseudoport_start_command start;
  1713. int ret = 0;
  1714. pr_debug("%s: port_id=%d\n", __func__, port_id);
  1715. if (this_afe.apr == NULL) {
  1716. pr_err("%s: AFE APR is not registered\n", __func__);
  1717. return -ENODEV;
  1718. }
  1719. start.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1720. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  1721. start.hdr.pkt_size = sizeof(start);
  1722. start.hdr.src_port = 0;
  1723. start.hdr.dest_port = 0;
  1724. start.hdr.token = 0;
  1725. start.hdr.opcode = AFE_PSEUDOPORT_CMD_START;
  1726. start.port_id = port_id;
  1727. start.timing = 1;
  1728. ret = afe_apr_send_pkt(&start, NULL);
  1729. if (ret) {
  1730. pr_err("%s: AFE enable for port %d failed %d\n",
  1731. __func__, port_id, ret);
  1732. return -EINVAL;
  1733. }
  1734. return 0;
  1735. }
  1736. int afe_start_pseudo_port(u16 port_id)
  1737. {
  1738. int ret = 0;
  1739. struct afe_pseudoport_start_command start;
  1740. int index = 0;
  1741. pr_debug("%s: port_id=%d\n", __func__, port_id);
  1742. ret = afe_q6_interface_prepare();
  1743. if (ret != 0)
  1744. return ret;
  1745. index = q6audio_get_port_index(port_id);
  1746. if (q6audio_validate_port(port_id) < 0)
  1747. return -EINVAL;
  1748. start.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1749. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  1750. start.hdr.pkt_size = sizeof(start);
  1751. start.hdr.src_port = 0;
  1752. start.hdr.dest_port = 0;
  1753. start.hdr.token = 0;
  1754. start.hdr.opcode = AFE_PSEUDOPORT_CMD_START;
  1755. start.port_id = port_id;
  1756. start.timing = 1;
  1757. start.hdr.token = index;
  1758. ret = afe_apr_send_pkt(&start, &this_afe.wait[index]);
  1759. if (ret)
  1760. pr_err("%s: AFE enable for port %d failed %d\n",
  1761. __func__, port_id, ret);
  1762. return ret;
  1763. }
  1764. int afe_pseudo_port_stop_nowait(u16 port_id)
  1765. {
  1766. int ret = 0;
  1767. struct afe_pseudoport_stop_command stop;
  1768. int index = 0;
  1769. pr_debug("%s: port_id=%d\n", __func__, port_id);
  1770. if (this_afe.apr == NULL) {
  1771. pr_err("%s: AFE is already closed\n", __func__);
  1772. return -EINVAL;
  1773. }
  1774. index = q6audio_get_port_index(port_id);
  1775. if (q6audio_validate_port(port_id) < 0)
  1776. return -EINVAL;
  1777. stop.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1778. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  1779. stop.hdr.pkt_size = sizeof(stop);
  1780. stop.hdr.src_port = 0;
  1781. stop.hdr.dest_port = 0;
  1782. stop.hdr.token = 0;
  1783. stop.hdr.opcode = AFE_PSEUDOPORT_CMD_STOP;
  1784. stop.port_id = port_id;
  1785. stop.reserved = 0;
  1786. stop.hdr.token = index;
  1787. ret = afe_apr_send_pkt(&stop, NULL);
  1788. if (ret)
  1789. pr_err("%s: AFE close failed %d\n", __func__, ret);
  1790. return ret;
  1791. }
  1792. int afe_stop_pseudo_port(u16 port_id)
  1793. {
  1794. int ret = 0;
  1795. struct afe_pseudoport_stop_command stop;
  1796. int index = 0;
  1797. pr_debug("%s: port_id=%d\n", __func__, port_id);
  1798. if (this_afe.apr == NULL) {
  1799. pr_err("%s: AFE is already closed\n", __func__);
  1800. return -EINVAL;
  1801. }
  1802. index = q6audio_get_port_index(port_id);
  1803. if (q6audio_validate_port(port_id) < 0)
  1804. return -EINVAL;
  1805. stop.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1806. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  1807. stop.hdr.pkt_size = sizeof(stop);
  1808. stop.hdr.src_port = 0;
  1809. stop.hdr.dest_port = 0;
  1810. stop.hdr.token = 0;
  1811. stop.hdr.opcode = AFE_PSEUDOPORT_CMD_STOP;
  1812. stop.port_id = port_id;
  1813. stop.reserved = 0;
  1814. stop.hdr.token = index;
  1815. ret = afe_apr_send_pkt(&stop, &this_afe.wait[index]);
  1816. if (ret)
  1817. pr_err("%s: AFE close failed %d\n", __func__, ret);
  1818. return ret;
  1819. }
  1820. uint32_t afe_req_mmap_handle(struct afe_audio_client *ac)
  1821. {
  1822. return ac->mem_map_handle;
  1823. }
  1824. struct afe_audio_client *q6afe_audio_client_alloc(void *priv)
  1825. {
  1826. struct afe_audio_client *ac;
  1827. int lcnt = 0;
  1828. ac = kzalloc(sizeof(struct afe_audio_client), GFP_KERNEL);
  1829. if (!ac) {
  1830. pr_err("%s: cannot allocate audio client for afe\n", __func__);
  1831. return NULL;
  1832. }
  1833. ac->priv = priv;
  1834. init_waitqueue_head(&ac->cmd_wait);
  1835. INIT_LIST_HEAD(&ac->port[0].mem_map_handle);
  1836. INIT_LIST_HEAD(&ac->port[1].mem_map_handle);
  1837. pr_debug("%s: mem_map_handle list init'ed\n", __func__);
  1838. mutex_init(&ac->cmd_lock);
  1839. for (lcnt = 0; lcnt <= OUT; lcnt++) {
  1840. mutex_init(&ac->port[lcnt].lock);
  1841. spin_lock_init(&ac->port[lcnt].dsp_lock);
  1842. }
  1843. atomic_set(&ac->cmd_state, 0);
  1844. return ac;
  1845. }
  1846. int q6afe_audio_client_buf_alloc_contiguous(unsigned int dir,
  1847. struct afe_audio_client *ac,
  1848. unsigned int bufsz,
  1849. unsigned int bufcnt)
  1850. {
  1851. int cnt = 0;
  1852. int rc = 0;
  1853. struct afe_audio_buffer *buf;
  1854. int len;
  1855. if (!(ac) || ((dir != IN) && (dir != OUT)))
  1856. return -EINVAL;
  1857. pr_debug("%s: bufsz[%d]bufcnt[%d]\n",
  1858. __func__,
  1859. bufsz, bufcnt);
  1860. if (ac->port[dir].buf) {
  1861. pr_debug("%s: buffer already allocated\n", __func__);
  1862. return 0;
  1863. }
  1864. mutex_lock(&ac->cmd_lock);
  1865. buf = kzalloc(((sizeof(struct afe_audio_buffer))*bufcnt),
  1866. GFP_KERNEL);
  1867. if (!buf) {
  1868. mutex_unlock(&ac->cmd_lock);
  1869. goto fail;
  1870. }
  1871. ac->port[dir].buf = buf;
  1872. rc = msm_audio_ion_alloc("audio_client", &buf[0].client,
  1873. &buf[0].handle, bufsz*bufcnt,
  1874. (ion_phys_addr_t *)&buf[0].phys, (size_t *)&len,
  1875. &buf[0].data);
  1876. if (rc) {
  1877. pr_err("%s: audio ION alloc failed, rc = %d\n",
  1878. __func__, rc);
  1879. mutex_unlock(&ac->cmd_lock);
  1880. goto fail;
  1881. }
  1882. buf[0].used = dir ^ 1;
  1883. buf[0].size = bufsz;
  1884. buf[0].actual_size = bufsz;
  1885. cnt = 1;
  1886. while (cnt < bufcnt) {
  1887. if (bufsz > 0) {
  1888. buf[cnt].data = buf[0].data + (cnt * bufsz);
  1889. buf[cnt].phys = buf[0].phys + (cnt * bufsz);
  1890. if (!buf[cnt].data) {
  1891. pr_err("%s Buf alloc failed\n",
  1892. __func__);
  1893. mutex_unlock(&ac->cmd_lock);
  1894. goto fail;
  1895. }
  1896. buf[cnt].used = dir ^ 1;
  1897. buf[cnt].size = bufsz;
  1898. buf[cnt].actual_size = bufsz;
  1899. pr_debug("%s data[%p]phys[%p][%p]\n", __func__,
  1900. (void *)buf[cnt].data,
  1901. (void *)buf[cnt].phys,
  1902. (void *)&buf[cnt].phys);
  1903. }
  1904. cnt++;
  1905. }
  1906. ac->port[dir].max_buf_cnt = cnt;
  1907. mutex_unlock(&ac->cmd_lock);
  1908. return 0;
  1909. fail:
  1910. q6afe_audio_client_buf_free_contiguous(dir, ac);
  1911. return -EINVAL;
  1912. }
  1913. int afe_memory_map(u32 dma_addr_p, u32 dma_buf_sz, struct afe_audio_client *ac)
  1914. {
  1915. int ret = 0;
  1916. ac->mem_map_handle = 0;
  1917. ret = afe_cmd_memory_map(dma_addr_p, dma_buf_sz);
  1918. if (ret < 0) {
  1919. pr_err("%s: afe_cmd_memory_map failed\n", __func__);
  1920. return ret;
  1921. }
  1922. ac->mem_map_handle = this_afe.mmap_handle;
  1923. return ret;
  1924. }
  1925. int afe_cmd_memory_map(u32 dma_addr_p, u32 dma_buf_sz)
  1926. {
  1927. int ret = 0;
  1928. int cmd_size = 0;
  1929. void *payload = NULL;
  1930. void *mmap_region_cmd = NULL;
  1931. struct afe_service_cmd_shared_mem_map_regions *mregion = NULL;
  1932. struct afe_service_shared_map_region_payload *mregion_pl = NULL;
  1933. int index = 0;
  1934. pr_debug("%s:\n", __func__);
  1935. if (this_afe.apr == NULL) {
  1936. this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
  1937. 0xFFFFFFFF, &this_afe);
  1938. pr_debug("%s: Register AFE\n", __func__);
  1939. if (this_afe.apr == NULL) {
  1940. pr_err("%s: Unable to register AFE\n", __func__);
  1941. ret = -ENODEV;
  1942. return ret;
  1943. }
  1944. }
  1945. cmd_size = sizeof(struct afe_service_cmd_shared_mem_map_regions) \
  1946. + sizeof(struct afe_service_shared_map_region_payload);
  1947. mmap_region_cmd = kzalloc(cmd_size, GFP_KERNEL);
  1948. if (!mmap_region_cmd) {
  1949. pr_err("%s: allocate mmap_region_cmd failed\n", __func__);
  1950. return -ENOMEM;
  1951. }
  1952. mregion = (struct afe_service_cmd_shared_mem_map_regions *)
  1953. mmap_region_cmd;
  1954. mregion->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1955. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  1956. mregion->hdr.pkt_size = cmd_size;
  1957. mregion->hdr.src_port = 0;
  1958. mregion->hdr.dest_port = 0;
  1959. mregion->hdr.token = 0;
  1960. mregion->hdr.opcode = AFE_SERVICE_CMD_SHARED_MEM_MAP_REGIONS;
  1961. mregion->mem_pool_id = ADSP_MEMORY_MAP_SHMEM8_4K_POOL;
  1962. mregion->num_regions = 1;
  1963. mregion->property_flag = 0x00;
  1964. /* Todo */
  1965. index = mregion->hdr.token = IDX_RSVD_2;
  1966. payload = ((u8 *) mmap_region_cmd +
  1967. sizeof(struct afe_service_cmd_shared_mem_map_regions));
  1968. mregion_pl = (struct afe_service_shared_map_region_payload *)payload;
  1969. mregion_pl->shm_addr_lsw = dma_addr_p;
  1970. mregion_pl->shm_addr_msw = 0x00;
  1971. mregion_pl->mem_size_bytes = dma_buf_sz;
  1972. pr_debug("%s: dma_addr_p 0x%x , size %d\n", __func__,
  1973. dma_addr_p, dma_buf_sz);
  1974. atomic_set(&this_afe.state, 1);
  1975. atomic_set(&this_afe.status, 0);
  1976. this_afe.mmap_handle = 0;
  1977. ret = apr_send_pkt(this_afe.apr, (uint32_t *) mmap_region_cmd);
  1978. if (ret < 0) {
  1979. pr_err("%s: AFE memory map cmd failed %d\n",
  1980. __func__, ret);
  1981. ret = -EINVAL;
  1982. goto fail_cmd;
  1983. }
  1984. ret = wait_event_timeout(this_afe.wait[index],
  1985. (atomic_read(&this_afe.state) == 0),
  1986. msecs_to_jiffies(TIMEOUT_MS));
  1987. if (!ret) {
  1988. pr_err("%s: wait_event timeout\n", __func__);
  1989. ret = -EINVAL;
  1990. goto fail_cmd;
  1991. }
  1992. if (atomic_read(&this_afe.status) != 0) {
  1993. pr_err("%s: Memory map cmd failed\n", __func__);
  1994. ret = -EINVAL;
  1995. goto fail_cmd;
  1996. }
  1997. kfree(mmap_region_cmd);
  1998. return 0;
  1999. fail_cmd:
  2000. kfree(mmap_region_cmd);
  2001. return ret;
  2002. }
  2003. int afe_cmd_memory_map_nowait(int port_id, u32 dma_addr_p, u32 dma_buf_sz)
  2004. {
  2005. int ret = 0;
  2006. int cmd_size = 0;
  2007. void *payload = NULL;
  2008. void *mmap_region_cmd = NULL;
  2009. struct afe_service_cmd_shared_mem_map_regions *mregion = NULL;
  2010. struct afe_service_shared_map_region_payload *mregion_pl = NULL;
  2011. int index = 0;
  2012. pr_debug("%s:\n", __func__);
  2013. if (this_afe.apr == NULL) {
  2014. this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
  2015. 0xFFFFFFFF, &this_afe);
  2016. pr_debug("%s: Register AFE\n", __func__);
  2017. if (this_afe.apr == NULL) {
  2018. pr_err("%s: Unable to register AFE\n", __func__);
  2019. ret = -ENODEV;
  2020. return ret;
  2021. }
  2022. }
  2023. index = q6audio_get_port_index(port_id);
  2024. if (q6audio_validate_port(port_id) < 0)
  2025. return -EINVAL;
  2026. cmd_size = sizeof(struct afe_service_cmd_shared_mem_map_regions)
  2027. + sizeof(struct afe_service_shared_map_region_payload);
  2028. mmap_region_cmd = kzalloc(cmd_size, GFP_KERNEL);
  2029. if (!mmap_region_cmd) {
  2030. pr_err("%s: allocate mmap_region_cmd failed\n", __func__);
  2031. return -ENOMEM;
  2032. }
  2033. mregion = (struct afe_service_cmd_shared_mem_map_regions *)
  2034. mmap_region_cmd;
  2035. mregion->hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  2036. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  2037. mregion->hdr.pkt_size = sizeof(mregion);
  2038. mregion->hdr.src_port = 0;
  2039. mregion->hdr.dest_port = 0;
  2040. mregion->hdr.token = 0;
  2041. mregion->hdr.opcode = AFE_SERVICE_CMD_SHARED_MEM_MAP_REGIONS;
  2042. mregion->mem_pool_id = ADSP_MEMORY_MAP_SHMEM8_4K_POOL;
  2043. mregion->num_regions = 1;
  2044. mregion->property_flag = 0x00;
  2045. payload = ((u8 *) mmap_region_cmd +
  2046. sizeof(struct afe_service_cmd_shared_mem_map_regions));
  2047. mregion_pl = (struct afe_service_shared_map_region_payload *)payload;
  2048. mregion_pl->shm_addr_lsw = dma_addr_p;
  2049. mregion_pl->shm_addr_msw = 0x00;
  2050. mregion_pl->mem_size_bytes = dma_buf_sz;
  2051. ret = afe_apr_send_pkt(mmap_region_cmd, NULL);
  2052. if (ret)
  2053. pr_err("%s: AFE memory map cmd failed %d\n",
  2054. __func__, ret);
  2055. kfree(mmap_region_cmd);
  2056. return ret;
  2057. }
  2058. int q6afe_audio_client_buf_free_contiguous(unsigned int dir,
  2059. struct afe_audio_client *ac)
  2060. {
  2061. struct afe_audio_port_data *port;
  2062. int cnt = 0;
  2063. mutex_lock(&ac->cmd_lock);
  2064. port = &ac->port[dir];
  2065. if (!port->buf) {
  2066. mutex_unlock(&ac->cmd_lock);
  2067. return 0;
  2068. }
  2069. cnt = port->max_buf_cnt - 1;
  2070. if (port->buf[0].data) {
  2071. pr_debug("%s:data[%p]phys[%p][%p] , client[%p] handle[%p]\n",
  2072. __func__,
  2073. (void *)port->buf[0].data,
  2074. (void *)port->buf[0].phys,
  2075. (void *)&port->buf[0].phys,
  2076. (void *)port->buf[0].client,
  2077. (void *)port->buf[0].handle);
  2078. msm_audio_ion_free(port->buf[0].client, port->buf[0].handle);
  2079. port->buf[0].client = NULL;
  2080. port->buf[0].handle = NULL;
  2081. }
  2082. while (cnt >= 0) {
  2083. port->buf[cnt].data = NULL;
  2084. port->buf[cnt].phys = 0;
  2085. cnt--;
  2086. }
  2087. port->max_buf_cnt = 0;
  2088. kfree(port->buf);
  2089. port->buf = NULL;
  2090. mutex_unlock(&ac->cmd_lock);
  2091. return 0;
  2092. }
  2093. void q6afe_audio_client_free(struct afe_audio_client *ac)
  2094. {
  2095. int loopcnt;
  2096. struct afe_audio_port_data *port;
  2097. if (!ac)
  2098. return;
  2099. for (loopcnt = 0; loopcnt <= OUT; loopcnt++) {
  2100. port = &ac->port[loopcnt];
  2101. if (!port->buf)
  2102. continue;
  2103. pr_debug("%s:loopcnt = %d\n", __func__, loopcnt);
  2104. q6afe_audio_client_buf_free_contiguous(loopcnt, ac);
  2105. }
  2106. kfree(ac);
  2107. return;
  2108. }
  2109. int afe_cmd_memory_unmap(u32 mem_map_handle)
  2110. {
  2111. int ret = 0;
  2112. struct afe_service_cmd_shared_mem_unmap_regions mregion;
  2113. int index = 0;
  2114. pr_debug("%s: handle 0x%x\n", __func__, mem_map_handle);
  2115. if (this_afe.apr == NULL) {
  2116. this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
  2117. 0xFFFFFFFF, &this_afe);
  2118. pr_debug("%s: Register AFE\n", __func__);
  2119. if (this_afe.apr == NULL) {
  2120. pr_err("%s: Unable to register AFE\n", __func__);
  2121. ret = -ENODEV;
  2122. return ret;
  2123. }
  2124. }
  2125. mregion.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  2126. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  2127. mregion.hdr.pkt_size = sizeof(mregion);
  2128. mregion.hdr.src_port = 0;
  2129. mregion.hdr.dest_port = 0;
  2130. mregion.hdr.token = 0;
  2131. mregion.hdr.opcode = AFE_SERVICE_CMD_SHARED_MEM_UNMAP_REGIONS;
  2132. mregion.mem_map_handle = mem_map_handle;
  2133. /* Todo */
  2134. index = mregion.hdr.token = IDX_RSVD_2;
  2135. atomic_set(&this_afe.status, 0);
  2136. ret = afe_apr_send_pkt(&mregion, &this_afe.wait[index]);
  2137. if (ret)
  2138. pr_err("%s: AFE memory unmap cmd failed %d\n",
  2139. __func__, ret);
  2140. if (atomic_read(&this_afe.status) != 0) {
  2141. pr_err("%s: Memory unmap cmd failed\n", __func__);
  2142. ret = -EINVAL;
  2143. }
  2144. return ret;
  2145. }
  2146. int afe_cmd_memory_unmap_nowait(u32 mem_map_handle)
  2147. {
  2148. int ret = 0;
  2149. struct afe_service_cmd_shared_mem_unmap_regions mregion;
  2150. pr_debug("%s: handle 0x%x\n", __func__, mem_map_handle);
  2151. if (this_afe.apr == NULL) {
  2152. this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
  2153. 0xFFFFFFFF, &this_afe);
  2154. pr_debug("%s: Register AFE\n", __func__);
  2155. if (this_afe.apr == NULL) {
  2156. pr_err("%s: Unable to register AFE\n", __func__);
  2157. ret = -ENODEV;
  2158. return ret;
  2159. }
  2160. }
  2161. mregion.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  2162. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  2163. mregion.hdr.pkt_size = sizeof(mregion);
  2164. mregion.hdr.src_port = 0;
  2165. mregion.hdr.dest_port = 0;
  2166. mregion.hdr.token = 0;
  2167. mregion.hdr.opcode = AFE_SERVICE_CMD_SHARED_MEM_UNMAP_REGIONS;
  2168. mregion.mem_map_handle = mem_map_handle;
  2169. ret = afe_apr_send_pkt(&mregion, NULL);
  2170. if (ret)
  2171. pr_err("%s: AFE memory unmap cmd failed %d\n",
  2172. __func__, ret);
  2173. return ret;
  2174. }
  2175. int afe_register_get_events(u16 port_id,
  2176. void (*cb) (uint32_t opcode,
  2177. uint32_t token, uint32_t *payload, void *priv),
  2178. void *private_data)
  2179. {
  2180. int ret = 0;
  2181. struct afe_service_cmd_register_rt_port_driver rtproxy;
  2182. pr_debug("%s: port_id: %d\n", __func__, port_id);
  2183. if (this_afe.apr == NULL) {
  2184. this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
  2185. 0xFFFFFFFF, &this_afe);
  2186. pr_debug("%s: Register AFE\n", __func__);
  2187. if (this_afe.apr == NULL) {
  2188. pr_err("%s: Unable to register AFE\n", __func__);
  2189. ret = -ENODEV;
  2190. return ret;
  2191. }
  2192. }
  2193. if ((port_id == RT_PROXY_DAI_002_RX) ||
  2194. (port_id == RT_PROXY_DAI_001_TX))
  2195. port_id = VIRTUAL_ID_TO_PORTID(port_id);
  2196. else
  2197. return -EINVAL;
  2198. if (port_id == RT_PROXY_PORT_001_TX) {
  2199. this_afe.tx_cb = cb;
  2200. this_afe.tx_private_data = private_data;
  2201. } else if (port_id == RT_PROXY_PORT_001_RX) {
  2202. this_afe.rx_cb = cb;
  2203. this_afe.rx_private_data = private_data;
  2204. }
  2205. rtproxy.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  2206. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  2207. rtproxy.hdr.pkt_size = sizeof(rtproxy);
  2208. rtproxy.hdr.src_port = 1;
  2209. rtproxy.hdr.dest_port = 1;
  2210. rtproxy.hdr.opcode = AFE_SERVICE_CMD_REGISTER_RT_PORT_DRIVER;
  2211. rtproxy.port_id = port_id;
  2212. rtproxy.reserved = 0;
  2213. ret = afe_apr_send_pkt(&rtproxy, NULL);
  2214. if (ret)
  2215. pr_err("%s: AFE reg. rtproxy_event failed %d\n",
  2216. __func__, ret);
  2217. return ret;
  2218. }
  2219. int afe_unregister_get_events(u16 port_id)
  2220. {
  2221. int ret = 0;
  2222. struct afe_service_cmd_unregister_rt_port_driver rtproxy;
  2223. int index = 0;
  2224. pr_debug("%s:\n", __func__);
  2225. if (this_afe.apr == NULL) {
  2226. this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
  2227. 0xFFFFFFFF, &this_afe);
  2228. pr_debug("%s: Register AFE\n", __func__);
  2229. if (this_afe.apr == NULL) {
  2230. pr_err("%s: Unable to register AFE\n", __func__);
  2231. ret = -ENODEV;
  2232. return ret;
  2233. }
  2234. }
  2235. if ((port_id == RT_PROXY_DAI_002_RX) ||
  2236. (port_id == RT_PROXY_DAI_001_TX))
  2237. port_id = VIRTUAL_ID_TO_PORTID(port_id);
  2238. else
  2239. return -EINVAL;
  2240. index = q6audio_get_port_index(port_id);
  2241. if (q6audio_validate_port(port_id) < 0)
  2242. return -EINVAL;
  2243. rtproxy.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  2244. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  2245. rtproxy.hdr.pkt_size = sizeof(rtproxy);
  2246. rtproxy.hdr.src_port = 0;
  2247. rtproxy.hdr.dest_port = 0;
  2248. rtproxy.hdr.token = 0;
  2249. rtproxy.hdr.opcode = AFE_SERVICE_CMD_UNREGISTER_RT_PORT_DRIVER;
  2250. rtproxy.port_id = port_id;
  2251. rtproxy.reserved = 0;
  2252. rtproxy.hdr.token = index;
  2253. if (port_id == RT_PROXY_PORT_001_TX) {
  2254. this_afe.tx_cb = NULL;
  2255. this_afe.tx_private_data = NULL;
  2256. } else if (port_id == RT_PROXY_PORT_001_RX) {
  2257. this_afe.rx_cb = NULL;
  2258. this_afe.rx_private_data = NULL;
  2259. }
  2260. ret = afe_apr_send_pkt(&rtproxy, &this_afe.wait[index]);
  2261. if (ret)
  2262. pr_err("%s: AFE enable Unreg. rtproxy_event failed %d\n",
  2263. __func__, ret);
  2264. return ret;
  2265. }
  2266. int afe_rt_proxy_port_write(u32 buf_addr_p, u32 mem_map_handle, int bytes)
  2267. {
  2268. int ret = 0;
  2269. struct afe_port_data_cmd_rt_proxy_port_write_v2 afecmd_wr;
  2270. if (this_afe.apr == NULL) {
  2271. pr_err("%s:register to AFE is not done\n", __func__);
  2272. ret = -ENODEV;
  2273. return ret;
  2274. }
  2275. pr_debug("%s: buf_addr_p = 0x%08x bytes = %d\n", __func__,
  2276. buf_addr_p, bytes);
  2277. afecmd_wr.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  2278. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  2279. afecmd_wr.hdr.pkt_size = sizeof(afecmd_wr);
  2280. afecmd_wr.hdr.src_port = 0;
  2281. afecmd_wr.hdr.dest_port = 0;
  2282. afecmd_wr.hdr.token = 0;
  2283. afecmd_wr.hdr.opcode = AFE_PORT_DATA_CMD_RT_PROXY_PORT_WRITE_V2;
  2284. afecmd_wr.port_id = RT_PROXY_PORT_001_TX;
  2285. afecmd_wr.buffer_address_lsw = (uint32_t)buf_addr_p;
  2286. afecmd_wr.buffer_address_msw = 0x00;
  2287. afecmd_wr.mem_map_handle = mem_map_handle;
  2288. afecmd_wr.available_bytes = bytes;
  2289. afecmd_wr.reserved = 0;
  2290. ret = afe_apr_send_pkt(&afecmd_wr, NULL);
  2291. if (ret)
  2292. pr_err("%s: AFE rtproxy write to port 0x%x failed %d\n",
  2293. __func__, afecmd_wr.port_id, ret);
  2294. return ret;
  2295. }
  2296. int afe_rt_proxy_port_read(u32 buf_addr_p, u32 mem_map_handle, int bytes)
  2297. {
  2298. int ret = 0;
  2299. struct afe_port_data_cmd_rt_proxy_port_read_v2 afecmd_rd;
  2300. if (this_afe.apr == NULL) {
  2301. pr_err("%s: register to AFE is not done\n", __func__);
  2302. ret = -ENODEV;
  2303. return ret;
  2304. }
  2305. pr_debug("%s: buf_addr_p = 0x%08x bytes = %d\n", __func__,
  2306. buf_addr_p, bytes);
  2307. afecmd_rd.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  2308. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  2309. afecmd_rd.hdr.pkt_size = sizeof(afecmd_rd);
  2310. afecmd_rd.hdr.src_port = 0;
  2311. afecmd_rd.hdr.dest_port = 0;
  2312. afecmd_rd.hdr.token = 0;
  2313. afecmd_rd.hdr.opcode = AFE_PORT_DATA_CMD_RT_PROXY_PORT_READ_V2;
  2314. afecmd_rd.port_id = RT_PROXY_PORT_001_RX;
  2315. afecmd_rd.buffer_address_lsw = (uint32_t)buf_addr_p;
  2316. afecmd_rd.buffer_address_msw = 0x00;
  2317. afecmd_rd.available_bytes = bytes;
  2318. afecmd_rd.mem_map_handle = mem_map_handle;
  2319. ret = afe_apr_send_pkt(&afecmd_rd, NULL);
  2320. if (ret)
  2321. pr_err("%s: AFE rtproxy read cmd to port 0x%x failed %d\n",
  2322. __func__, afecmd_rd.port_id, ret);
  2323. return ret;
  2324. }
  2325. #ifdef CONFIG_DEBUG_FS
  2326. static struct dentry *debugfs_afelb;
  2327. static struct dentry *debugfs_afelb_gain;
  2328. static int afe_debug_open(struct inode *inode, struct file *file)
  2329. {
  2330. file->private_data = inode->i_private;
  2331. pr_info("debug intf %s\n", (char *) file->private_data);
  2332. return 0;
  2333. }
  2334. static int afe_get_parameters(char *buf, long int *param1, int num_of_par)
  2335. {
  2336. char *token;
  2337. int base, cnt;
  2338. token = strsep(&buf, " ");
  2339. for (cnt = 0; cnt < num_of_par; cnt++) {
  2340. if (token != NULL) {
  2341. if ((token[1] == 'x') || (token[1] == 'X'))
  2342. base = 16;
  2343. else
  2344. base = 10;
  2345. if (kstrtoul(token, base, &param1[cnt]) != 0)
  2346. return -EINVAL;
  2347. token = strsep(&buf, " ");
  2348. } else
  2349. return -EINVAL;
  2350. }
  2351. return 0;
  2352. }
  2353. #define AFE_LOOPBACK_ON (1)
  2354. #define AFE_LOOPBACK_OFF (0)
  2355. static ssize_t afe_debug_write(struct file *filp,
  2356. const char __user *ubuf, size_t cnt, loff_t *ppos)
  2357. {
  2358. char *lb_str = filp->private_data;
  2359. char lbuf[32];
  2360. int rc;
  2361. unsigned long param[5];
  2362. if (cnt > sizeof(lbuf) - 1)
  2363. return -EINVAL;
  2364. rc = copy_from_user(lbuf, ubuf, cnt);
  2365. if (rc)
  2366. return -EFAULT;
  2367. lbuf[cnt] = '\0';
  2368. if (!strncmp(lb_str, "afe_loopback", 12)) {
  2369. rc = afe_get_parameters(lbuf, param, 3);
  2370. if (!rc) {
  2371. pr_info("%s %lu %lu %lu\n", lb_str, param[0], param[1],
  2372. param[2]);
  2373. if ((param[0] != AFE_LOOPBACK_ON) && (param[0] !=
  2374. AFE_LOOPBACK_OFF)) {
  2375. pr_err("%s: Error, parameter 0 incorrect\n",
  2376. __func__);
  2377. rc = -EINVAL;
  2378. goto afe_error;
  2379. }
  2380. if ((q6audio_validate_port(param[1]) < 0) ||
  2381. (q6audio_validate_port(param[2])) < 0) {
  2382. pr_err("%s: Error, invalid afe port\n",
  2383. __func__);
  2384. }
  2385. if (this_afe.apr == NULL) {
  2386. pr_err("%s: Error, AFE not opened\n", __func__);
  2387. rc = -EINVAL;
  2388. } else {
  2389. rc = afe_loopback(param[0], param[1], param[2]);
  2390. }
  2391. } else {
  2392. pr_err("%s: Error, invalid parameters\n", __func__);
  2393. rc = -EINVAL;
  2394. }
  2395. } else if (!strncmp(lb_str, "afe_loopback_gain", 17)) {
  2396. rc = afe_get_parameters(lbuf, param, 2);
  2397. if (!rc) {
  2398. pr_info("%s %lu %lu\n", lb_str, param[0], param[1]);
  2399. if (q6audio_validate_port(param[0]) < 0) {
  2400. pr_err("%s: Error, invalid afe port\n",
  2401. __func__);
  2402. rc = -EINVAL;
  2403. goto afe_error;
  2404. }
  2405. if (param[1] > 100) {
  2406. pr_err("%s: Error, volume shoud be 0 to 100 percentage param = %lu\n",
  2407. __func__, param[1]);
  2408. rc = -EINVAL;
  2409. goto afe_error;
  2410. }
  2411. param[1] = (Q6AFE_MAX_VOLUME * param[1]) / 100;
  2412. if (this_afe.apr == NULL) {
  2413. pr_err("%s: Error, AFE not opened\n", __func__);
  2414. rc = -EINVAL;
  2415. } else {
  2416. rc = afe_loopback_gain(param[0], param[1]);
  2417. }
  2418. } else {
  2419. pr_err("%s: Error, invalid parameters\n", __func__);
  2420. rc = -EINVAL;
  2421. }
  2422. }
  2423. afe_error:
  2424. if (rc == 0)
  2425. rc = cnt;
  2426. else
  2427. pr_err("%s: rc = %d\n", __func__, rc);
  2428. return rc;
  2429. }
  2430. static const struct file_operations afe_debug_fops = {
  2431. .open = afe_debug_open,
  2432. .write = afe_debug_write
  2433. };
  2434. static void config_debug_fs_init(void)
  2435. {
  2436. debugfs_afelb = debugfs_create_file("afe_loopback",
  2437. S_IRUGO | S_IWUSR | S_IWGRP, NULL, (void *) "afe_loopback",
  2438. &afe_debug_fops);
  2439. debugfs_afelb_gain = debugfs_create_file("afe_loopback_gain",
  2440. S_IRUGO | S_IWUSR | S_IWGRP, NULL, (void *) "afe_loopback_gain",
  2441. &afe_debug_fops);
  2442. }
  2443. static void config_debug_fs_exit(void)
  2444. {
  2445. if (debugfs_afelb)
  2446. debugfs_remove(debugfs_afelb);
  2447. if (debugfs_afelb_gain)
  2448. debugfs_remove(debugfs_afelb_gain);
  2449. }
  2450. #else
  2451. static void config_debug_fs_init(void)
  2452. {
  2453. return;
  2454. }
  2455. static void config_debug_fs_exit(void)
  2456. {
  2457. return;
  2458. }
  2459. #endif
  2460. void afe_set_dtmf_gen_rx_portid(u16 port_id, int set)
  2461. {
  2462. if (set)
  2463. this_afe.dtmf_gen_rx_portid = port_id;
  2464. else if (this_afe.dtmf_gen_rx_portid == port_id)
  2465. this_afe.dtmf_gen_rx_portid = -1;
  2466. }
  2467. int afe_dtmf_generate_rx(int64_t duration_in_ms,
  2468. uint16_t high_freq,
  2469. uint16_t low_freq, uint16_t gain)
  2470. {
  2471. int ret = 0;
  2472. int index = 0;
  2473. struct afe_dtmf_generation_command cmd_dtmf;
  2474. pr_debug("%s: DTMF AFE Gen\n", __func__);
  2475. if (afe_validate_port(this_afe.dtmf_gen_rx_portid) < 0) {
  2476. pr_err("%s: Failed : Invalid Port id = %d\n",
  2477. __func__, this_afe.dtmf_gen_rx_portid);
  2478. ret = -EINVAL;
  2479. goto fail_cmd;
  2480. }
  2481. if (this_afe.apr == NULL) {
  2482. this_afe.apr = apr_register("ADSP", "AFE", afe_callback,
  2483. 0xFFFFFFFF, &this_afe);
  2484. pr_debug("%s: Register AFE\n", __func__);
  2485. if (this_afe.apr == NULL) {
  2486. pr_err("%s: Unable to register AFE\n", __func__);
  2487. ret = -ENODEV;
  2488. return ret;
  2489. }
  2490. }
  2491. pr_debug("dur=%lld: hfreq=%d lfreq=%d gain=%d portid=%x\n",
  2492. duration_in_ms, high_freq, low_freq, gain,
  2493. this_afe.dtmf_gen_rx_portid);
  2494. cmd_dtmf.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  2495. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  2496. cmd_dtmf.hdr.pkt_size = sizeof(cmd_dtmf);
  2497. cmd_dtmf.hdr.src_port = 0;
  2498. cmd_dtmf.hdr.dest_port = 0;
  2499. cmd_dtmf.hdr.token = 0;
  2500. cmd_dtmf.hdr.opcode = AFE_PORTS_CMD_DTMF_CTL;
  2501. cmd_dtmf.duration_in_ms = duration_in_ms;
  2502. cmd_dtmf.high_freq = high_freq;
  2503. cmd_dtmf.low_freq = low_freq;
  2504. cmd_dtmf.gain = gain;
  2505. cmd_dtmf.num_ports = 1;
  2506. cmd_dtmf.port_ids = q6audio_get_port_id(this_afe.dtmf_gen_rx_portid);
  2507. atomic_set(&this_afe.state, 1);
  2508. ret = apr_send_pkt(this_afe.apr, (uint32_t *) &cmd_dtmf);
  2509. if (ret < 0) {
  2510. pr_err("%s: AFE DTMF failed for num_ports:%d ids:%x\n",
  2511. __func__, cmd_dtmf.num_ports, cmd_dtmf.port_ids);
  2512. ret = -EINVAL;
  2513. goto fail_cmd;
  2514. }
  2515. index = q6audio_get_port_index(this_afe.dtmf_gen_rx_portid);
  2516. ret = wait_event_timeout(this_afe.wait[index],
  2517. (atomic_read(&this_afe.state) == 0),
  2518. msecs_to_jiffies(TIMEOUT_MS));
  2519. if (!ret) {
  2520. pr_err("%s: wait_event timeout\n", __func__);
  2521. ret = -EINVAL;
  2522. goto fail_cmd;
  2523. }
  2524. return 0;
  2525. fail_cmd:
  2526. return ret;
  2527. }
  2528. int afe_sidetone(u16 tx_port_id, u16 rx_port_id, u16 enable, uint16_t gain)
  2529. {
  2530. struct afe_loopback_cfg_v1 cmd_sidetone;
  2531. int ret = 0;
  2532. int index = 0;
  2533. pr_info("%s: tx_port_id:%d rx_port_id:%d enable:%d gain:%d\n", __func__,
  2534. tx_port_id, rx_port_id, enable, gain);
  2535. index = q6audio_get_port_index(rx_port_id);
  2536. if (q6audio_validate_port(rx_port_id) < 0)
  2537. return -EINVAL;
  2538. cmd_sidetone.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  2539. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  2540. cmd_sidetone.hdr.pkt_size = sizeof(cmd_sidetone);
  2541. cmd_sidetone.hdr.src_port = 0;
  2542. cmd_sidetone.hdr.dest_port = 0;
  2543. cmd_sidetone.hdr.token = 0;
  2544. cmd_sidetone.hdr.opcode = AFE_PORT_CMD_SET_PARAM_V2;
  2545. /* should it be rx or tx port id ?? , bharath*/
  2546. cmd_sidetone.param.port_id = tx_port_id;
  2547. /* size of data param & payload */
  2548. cmd_sidetone.param.payload_size = (sizeof(cmd_sidetone) -
  2549. sizeof(struct apr_hdr) -
  2550. sizeof(struct afe_port_cmd_set_param_v2));
  2551. cmd_sidetone.param.payload_address_lsw = 0x00;
  2552. cmd_sidetone.param.payload_address_msw = 0x00;
  2553. cmd_sidetone.param.mem_map_handle = 0x00;
  2554. cmd_sidetone.pdata.module_id = AFE_MODULE_LOOPBACK;
  2555. cmd_sidetone.pdata.param_id = AFE_PARAM_ID_LOOPBACK_CONFIG;
  2556. /* size of actual payload only */
  2557. cmd_sidetone.pdata.param_size = cmd_sidetone.param.payload_size -
  2558. sizeof(struct afe_port_param_data_v2);
  2559. cmd_sidetone.loopback_cfg_minor_version =
  2560. AFE_API_VERSION_LOOPBACK_CONFIG;
  2561. cmd_sidetone.dst_port_id = rx_port_id;
  2562. cmd_sidetone.routing_mode = LB_MODE_SIDETONE;
  2563. cmd_sidetone.enable = enable;
  2564. ret = afe_apr_send_pkt(&cmd_sidetone, &this_afe.wait[index]);
  2565. if (ret)
  2566. pr_err("%s: AFE sidetone failed for tx_port:%d rx_port:%d\n",
  2567. __func__, tx_port_id, rx_port_id);
  2568. return ret;
  2569. }
  2570. int afe_validate_port(u16 port_id)
  2571. {
  2572. int ret;
  2573. switch (port_id) {
  2574. case PRIMARY_I2S_RX:
  2575. case PRIMARY_I2S_TX:
  2576. case AFE_PORT_ID_PRIMARY_PCM_RX:
  2577. case AFE_PORT_ID_PRIMARY_PCM_TX:
  2578. case AFE_PORT_ID_SECONDARY_PCM_RX:
  2579. case AFE_PORT_ID_SECONDARY_PCM_TX:
  2580. case SECONDARY_I2S_RX:
  2581. case SECONDARY_I2S_TX:
  2582. case MI2S_RX:
  2583. case MI2S_TX:
  2584. case HDMI_RX:
  2585. case RSVD_2:
  2586. case RSVD_3:
  2587. case DIGI_MIC_TX:
  2588. case VOICE_RECORD_RX:
  2589. case VOICE_RECORD_TX:
  2590. case VOICE_PLAYBACK_TX:
  2591. case VOICE2_PLAYBACK_TX:
  2592. case SLIMBUS_0_RX:
  2593. case SLIMBUS_0_TX:
  2594. case SLIMBUS_1_RX:
  2595. case SLIMBUS_1_TX:
  2596. case SLIMBUS_2_RX:
  2597. case SLIMBUS_2_TX:
  2598. case SLIMBUS_3_RX:
  2599. case INT_BT_SCO_RX:
  2600. case INT_BT_SCO_TX:
  2601. case INT_BT_A2DP_RX:
  2602. case INT_FM_RX:
  2603. case INT_FM_TX:
  2604. case RT_PROXY_PORT_001_RX:
  2605. case RT_PROXY_PORT_001_TX:
  2606. case SLIMBUS_4_RX:
  2607. case SLIMBUS_4_TX:
  2608. case AFE_PORT_ID_PRIMARY_MI2S_RX:
  2609. {
  2610. ret = 0;
  2611. break;
  2612. }
  2613. default:
  2614. ret = -EINVAL;
  2615. }
  2616. return ret;
  2617. }
  2618. int afe_convert_virtual_to_portid(u16 port_id)
  2619. {
  2620. int ret;
  2621. /*
  2622. * if port_id is virtual, convert to physical..
  2623. * if port_id is already physical, return physical
  2624. */
  2625. if (afe_validate_port(port_id) < 0) {
  2626. if (port_id == RT_PROXY_DAI_001_RX ||
  2627. port_id == RT_PROXY_DAI_001_TX ||
  2628. port_id == RT_PROXY_DAI_002_RX ||
  2629. port_id == RT_PROXY_DAI_002_TX)
  2630. ret = VIRTUAL_ID_TO_PORTID(port_id);
  2631. else
  2632. ret = -EINVAL;
  2633. } else
  2634. ret = port_id;
  2635. return ret;
  2636. }
  2637. int afe_port_stop_nowait(int port_id)
  2638. {
  2639. struct afe_port_cmd_device_stop stop;
  2640. int ret = 0;
  2641. if (this_afe.apr == NULL) {
  2642. pr_err("AFE is already closed\n");
  2643. ret = -EINVAL;
  2644. goto fail_cmd;
  2645. }
  2646. pr_debug("%s: port_id=%d\n", __func__, port_id);
  2647. port_id = q6audio_convert_virtual_to_portid(port_id);
  2648. stop.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  2649. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  2650. stop.hdr.pkt_size = sizeof(stop);
  2651. stop.hdr.src_port = 0;
  2652. stop.hdr.dest_port = 0;
  2653. stop.hdr.token = 0;
  2654. stop.hdr.opcode = AFE_PORT_CMD_DEVICE_STOP;
  2655. stop.port_id = port_id;
  2656. stop.reserved = 0;
  2657. ret = afe_apr_send_pkt(&stop, NULL);
  2658. if (ret)
  2659. pr_err("%s: AFE close failed\n", __func__);
  2660. fail_cmd:
  2661. return ret;
  2662. }
  2663. int afe_close(int port_id)
  2664. {
  2665. struct afe_port_cmd_device_stop stop;
  2666. enum afe_mad_type mad_type;
  2667. int ret = 0;
  2668. int index = 0;
  2669. uint16_t port_index;
  2670. if (this_afe.apr == NULL) {
  2671. pr_err("%s: AFE is already closed\n", __func__);
  2672. if ((port_id == RT_PROXY_DAI_001_RX) ||
  2673. (port_id == RT_PROXY_DAI_002_TX))
  2674. pcm_afe_instance[port_id & 0x1] = 0;
  2675. if ((port_id == RT_PROXY_DAI_002_RX) ||
  2676. (port_id == RT_PROXY_DAI_001_TX))
  2677. proxy_afe_instance[port_id & 0x1] = 0;
  2678. afe_close_done[port_id & 0x1] = true;
  2679. ret = -EINVAL;
  2680. goto fail_cmd;
  2681. }
  2682. pr_debug("%s: port_id=%d\n", __func__, port_id);
  2683. #if defined(CONFIG_SND_SOC_MAXIM_DSM) && defined(CONFIG_SND_SOC_MAX98506)
  2684. if (maxdsm_get_spk_state()) {
  2685. if (port_id == AFE_PORT_ID_SLIMBUS_MULTI_CHAN_0_RX) {
  2686. maxdsm_set_pilot_signal_state(0);
  2687. pr_info("%s: pilot signal state done %d\n",
  2688. __func__, port_id);
  2689. }
  2690. }
  2691. #endif
  2692. if ((port_id == RT_PROXY_DAI_001_RX) ||
  2693. (port_id == RT_PROXY_DAI_002_TX)) {
  2694. pr_debug("%s: before decrementing pcm_afe_instance %d\n",
  2695. __func__, pcm_afe_instance[port_id & 0x1]);
  2696. port_id = VIRTUAL_ID_TO_PORTID(port_id);
  2697. pcm_afe_instance[port_id & 0x1]--;
  2698. if ((!(pcm_afe_instance[port_id & 0x1] == 0 &&
  2699. proxy_afe_instance[port_id & 0x1] == 0)) ||
  2700. afe_close_done[port_id & 0x1] == true)
  2701. return 0;
  2702. else
  2703. afe_close_done[port_id & 0x1] = true;
  2704. }
  2705. if ((port_id == RT_PROXY_DAI_002_RX) ||
  2706. (port_id == RT_PROXY_DAI_001_TX)) {
  2707. pr_debug("%s: before decrementing proxy_afe_instance %d\n",
  2708. __func__, proxy_afe_instance[port_id & 0x1]);
  2709. port_id = VIRTUAL_ID_TO_PORTID(port_id);
  2710. proxy_afe_instance[port_id & 0x1]--;
  2711. if ((!(pcm_afe_instance[port_id & 0x1] == 0 &&
  2712. proxy_afe_instance[port_id & 0x1] == 0)) ||
  2713. afe_close_done[port_id & 0x1] == true)
  2714. return 0;
  2715. else
  2716. afe_close_done[port_id & 0x1] = true;
  2717. }
  2718. port_id = q6audio_convert_virtual_to_portid(port_id);
  2719. index = q6audio_get_port_index(port_id);
  2720. if (q6audio_validate_port(port_id) < 0) {
  2721. pr_warn("%s: Not a valid port id 0x0%x\n", __func__, port_id);
  2722. return -EINVAL;
  2723. }
  2724. mad_type = afe_port_get_mad_type(port_id);
  2725. pr_debug("%s: port_id 0x%x, mad_type %d\n", __func__, port_id,
  2726. mad_type);
  2727. if (mad_type != MAD_HW_NONE && mad_type != MAD_SW_AUDIO) {
  2728. pr_debug("%s: Turn off MAD\n", __func__);
  2729. ret = afe_turn_onoff_hw_mad(mad_type, false);
  2730. if (ret) {
  2731. pr_err("%s: afe_turn_onoff_hw_mad failed %d\n",
  2732. __func__, ret);
  2733. return ret;
  2734. }
  2735. } else {
  2736. pr_debug("%s: Not a MAD port\n", __func__);
  2737. }
  2738. port_index = afe_get_port_index(port_id);
  2739. if ((port_index >= 0) && (port_index < AFE_MAX_PORTS)) {
  2740. this_afe.afe_sample_rates[port_index] = 0;
  2741. } else {
  2742. ret = -EINVAL;
  2743. goto fail_cmd;
  2744. }
  2745. if ((port_id == this_afe.aanc_info.aanc_tx_port) &&
  2746. (this_afe.aanc_info.aanc_active)) {
  2747. memset(&this_afe.aanc_info, 0x00, sizeof(this_afe.aanc_info));
  2748. ret = afe_aanc_mod_enable(this_afe.apr, port_id, 0);
  2749. if (ret)
  2750. pr_err("%s: AFE mod disable failed %d\n",
  2751. __func__, ret);
  2752. }
  2753. stop.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  2754. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  2755. stop.hdr.pkt_size = sizeof(stop);
  2756. stop.hdr.src_port = 0;
  2757. stop.hdr.dest_port = 0;
  2758. stop.hdr.token = index;
  2759. stop.hdr.opcode = AFE_PORT_CMD_DEVICE_STOP;
  2760. stop.port_id = q6audio_get_port_id(port_id);
  2761. stop.reserved = 0;
  2762. ret = afe_apr_send_pkt(&stop, &this_afe.wait[index]);
  2763. if (ret)
  2764. pr_err("%s: AFE close failed %d\n", __func__, ret);
  2765. fail_cmd:
  2766. return ret;
  2767. }
  2768. int afe_set_lpass_clock(u16 port_id, struct afe_clk_cfg *cfg)
  2769. {
  2770. struct afe_lpass_clk_config_command clk_cfg;
  2771. int index = 0;
  2772. int ret = 0;
  2773. if (!cfg) {
  2774. pr_err("%s: clock cfg is NULL\n", __func__);
  2775. ret = -EINVAL;
  2776. return ret;
  2777. }
  2778. index = q6audio_get_port_index(port_id);
  2779. if (q6audio_is_digital_pcm_interface(port_id) < 0)
  2780. return -EINVAL;
  2781. ret = afe_q6_interface_prepare();
  2782. if (ret != 0) {
  2783. pr_err("%s: Interface prepare failed \n", __func__);
  2784. return ret;
  2785. }
  2786. clk_cfg.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  2787. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  2788. clk_cfg.hdr.pkt_size = sizeof(clk_cfg);
  2789. clk_cfg.hdr.src_port = 0;
  2790. clk_cfg.hdr.dest_port = 0;
  2791. clk_cfg.hdr.token = index;
  2792. clk_cfg.hdr.opcode = AFE_PORT_CMD_SET_PARAM_V2;
  2793. clk_cfg.param.port_id = q6audio_get_port_id(port_id);
  2794. clk_cfg.param.payload_size = sizeof(clk_cfg) - sizeof(struct apr_hdr)
  2795. - sizeof(clk_cfg.param);
  2796. clk_cfg.param.payload_address_lsw = 0x00;
  2797. clk_cfg.param.payload_address_msw = 0x00;
  2798. clk_cfg.param.mem_map_handle = 0x00;
  2799. clk_cfg.pdata.module_id = AFE_MODULE_AUDIO_DEV_INTERFACE;
  2800. clk_cfg.pdata.param_id = AFE_PARAM_ID_LPAIF_CLK_CONFIG;
  2801. clk_cfg.pdata.param_size = sizeof(clk_cfg.clk_cfg);
  2802. clk_cfg.clk_cfg = *cfg;
  2803. pr_info("%s: Minor version =%x clk val1 = %d\n"
  2804. "clk val2 = %d, clk src = %x\n"
  2805. "clk root = %x clk mode = %x resrv = %x\n"
  2806. "port id = %x\n",
  2807. __func__, cfg->i2s_cfg_minor_version,
  2808. cfg->clk_val1, cfg->clk_val2, cfg->clk_src,
  2809. cfg->clk_root, cfg->clk_set_mode,
  2810. cfg->reserved, q6audio_get_port_id(port_id));
  2811. atomic_set(&this_afe.state, 1);
  2812. atomic_set(&this_afe.status, 0);
  2813. ret = apr_send_pkt(this_afe.apr, (uint32_t *) &clk_cfg);
  2814. if (ret < 0) {
  2815. pr_err("%s: AFE enable for port %d\n",
  2816. __func__, port_id);
  2817. ret = -EINVAL;
  2818. goto fail_cmd;
  2819. }
  2820. ret = wait_event_timeout(this_afe.wait[index],
  2821. (atomic_read(&this_afe.state) == 0),
  2822. msecs_to_jiffies(TIMEOUT_MS));
  2823. if (!ret) {
  2824. pr_err("%s: wait_event timeout\n", __func__);
  2825. ret = -EINVAL;
  2826. goto fail_cmd;
  2827. }
  2828. if (atomic_read(&this_afe.status) != 0) {
  2829. pr_err("%s: config cmd failed\n", __func__);
  2830. ret = -EINVAL;
  2831. goto fail_cmd;
  2832. }
  2833. fail_cmd:
  2834. return ret;
  2835. }
  2836. int afe_set_lpass_internal_digital_codec_clock(u16 port_id,
  2837. struct afe_digital_clk_cfg *cfg)
  2838. {
  2839. struct afe_lpass_digital_clk_config_command clk_cfg;
  2840. int index = 0;
  2841. int ret = 0;
  2842. if (!cfg) {
  2843. pr_err("%s: clock cfg is NULL\n", __func__);
  2844. ret = -EINVAL;
  2845. return ret;
  2846. }
  2847. index = q6audio_get_port_index(port_id);
  2848. if (q6audio_is_digital_pcm_interface(port_id) < 0)
  2849. return -EINVAL;
  2850. ret = afe_q6_interface_prepare();
  2851. if (ret != 0)
  2852. return ret;
  2853. clk_cfg.hdr.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  2854. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  2855. clk_cfg.hdr.pkt_size = sizeof(clk_cfg);
  2856. clk_cfg.hdr.src_port = 0;
  2857. clk_cfg.hdr.dest_port = 0;
  2858. clk_cfg.hdr.token = index;
  2859. clk_cfg.hdr.opcode = AFE_PORT_CMD_SET_PARAM_V2;
  2860. clk_cfg.param.port_id = q6audio_get_port_id(port_id);
  2861. clk_cfg.param.payload_size = sizeof(clk_cfg) - sizeof(struct apr_hdr)
  2862. - sizeof(clk_cfg.param);
  2863. clk_cfg.param.payload_address_lsw = 0x00;
  2864. clk_cfg.param.payload_address_msw = 0x00;
  2865. clk_cfg.param.mem_map_handle = 0x00;
  2866. clk_cfg.pdata.module_id = AFE_MODULE_AUDIO_DEV_INTERFACE;
  2867. clk_cfg.pdata.param_id = AFE_PARAM_ID_INTERNAL_DIGIATL_CDC_CLK_CONFIG;
  2868. clk_cfg.pdata.param_size = sizeof(clk_cfg.clk_cfg);
  2869. clk_cfg.clk_cfg = *cfg;
  2870. pr_debug("%s: Minor version =%x clk val = %d\n"
  2871. "clk root = %x resrv = %x port id = %x\n",
  2872. __func__, cfg->i2s_cfg_minor_version,
  2873. cfg->clk_val, cfg->clk_root, cfg->reserved,
  2874. q6audio_get_port_id(port_id));
  2875. atomic_set(&this_afe.state, 1);
  2876. atomic_set(&this_afe.status, 0);
  2877. ret = apr_send_pkt(this_afe.apr, (uint32_t *) &clk_cfg);
  2878. if (ret < 0) {
  2879. pr_err("%s: AFE enable for port %d\n",
  2880. __func__, port_id);
  2881. ret = -EINVAL;
  2882. goto fail_cmd;
  2883. }
  2884. ret = wait_event_timeout(this_afe.wait[index],
  2885. (atomic_read(&this_afe.state) == 0),
  2886. msecs_to_jiffies(TIMEOUT_MS));
  2887. if (!ret) {
  2888. pr_err("%s: wait_event timeout\n", __func__);
  2889. ret = -EINVAL;
  2890. goto fail_cmd;
  2891. }
  2892. if (atomic_read(&this_afe.status) != 0) {
  2893. pr_err("%s: config cmd failed\n", __func__);
  2894. ret = -EINVAL;
  2895. goto fail_cmd;
  2896. }
  2897. fail_cmd:
  2898. return ret;
  2899. }
  2900. int q6afe_check_osr_clk_freq(u32 freq)
  2901. {
  2902. int ret = 0;
  2903. switch (freq) {
  2904. case Q6AFE_LPASS_OSR_CLK_12_P288_MHZ:
  2905. case Q6AFE_LPASS_OSR_CLK_8_P192_MHZ:
  2906. case Q6AFE_LPASS_OSR_CLK_6_P144_MHZ:
  2907. case Q6AFE_LPASS_OSR_CLK_4_P096_MHZ:
  2908. case Q6AFE_LPASS_OSR_CLK_3_P072_MHZ:
  2909. case Q6AFE_LPASS_OSR_CLK_2_P048_MHZ:
  2910. case Q6AFE_LPASS_OSR_CLK_1_P536_MHZ:
  2911. case Q6AFE_LPASS_OSR_CLK_1_P024_MHZ:
  2912. case Q6AFE_LPASS_OSR_CLK_768_kHZ:
  2913. case Q6AFE_LPASS_OSR_CLK_512_kHZ:
  2914. break;
  2915. default:
  2916. ret = -EINVAL;
  2917. }
  2918. return ret;
  2919. }
  2920. int afe_spk_prot_get_calib_data(struct afe_spkr_prot_get_vi_calib *calib_resp)
  2921. {
  2922. int ret = -EINVAL;
  2923. int index = 0, port = SLIMBUS_4_TX;
  2924. if (!calib_resp) {
  2925. pr_err("%s Invalid params\n", __func__);
  2926. goto fail_cmd;
  2927. }
  2928. if ((q6audio_validate_port(port) < 0)) {
  2929. pr_err("%s invalid port %d\n", __func__, port);
  2930. goto fail_cmd;
  2931. }
  2932. index = q6audio_get_port_index(port);
  2933. calib_resp->get_param.hdr.hdr_field =
  2934. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  2935. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  2936. calib_resp->get_param.hdr.pkt_size = sizeof(*calib_resp);
  2937. calib_resp->get_param.hdr.src_port = 0;
  2938. calib_resp->get_param.hdr.dest_port = 0;
  2939. calib_resp->get_param.hdr.token = index;
  2940. calib_resp->get_param.hdr.opcode = AFE_PORT_CMD_GET_PARAM_V2;
  2941. calib_resp->get_param.mem_map_handle = 0;
  2942. calib_resp->get_param.module_id = AFE_MODULE_FB_SPKR_PROT_VI_PROC;
  2943. calib_resp->get_param.param_id = AFE_PARAM_ID_CALIB_RES_CFG;
  2944. calib_resp->get_param.payload_address_lsw = 0;
  2945. calib_resp->get_param.payload_address_msw = 0;
  2946. calib_resp->get_param.payload_size = sizeof(*calib_resp)
  2947. - sizeof(calib_resp->get_param);
  2948. calib_resp->get_param.port_id = q6audio_get_port_id(port);
  2949. calib_resp->pdata.module_id = AFE_MODULE_FB_SPKR_PROT_VI_PROC;
  2950. calib_resp->pdata.param_id = AFE_PARAM_ID_CALIB_RES_CFG;
  2951. calib_resp->pdata.param_size = sizeof(calib_resp->res_cfg);
  2952. atomic_set(&this_afe.state, 1);
  2953. ret = apr_send_pkt(this_afe.apr, (uint32_t *)calib_resp);
  2954. if (ret < 0) {
  2955. pr_err("%s: get param port %d param id[0x%x]failed\n",
  2956. __func__, port, calib_resp->get_param.param_id);
  2957. goto fail_cmd;
  2958. }
  2959. ret = wait_event_timeout(this_afe.wait[index],
  2960. (atomic_read(&this_afe.state) == 0),
  2961. msecs_to_jiffies(TIMEOUT_MS));
  2962. if (!ret) {
  2963. pr_err("%s: wait_event timeout\n", __func__);
  2964. ret = -EINVAL;
  2965. goto fail_cmd;
  2966. }
  2967. if (atomic_read(&this_afe.status) != 0) {
  2968. pr_err("%s: config cmd failed\n", __func__);
  2969. ret = -EINVAL;
  2970. goto fail_cmd;
  2971. }
  2972. memcpy(&calib_resp->res_cfg , &this_afe.calib_data.res_cfg,
  2973. sizeof(this_afe.calib_data.res_cfg));
  2974. pr_debug("%s state %d resistance %d\n", __func__,
  2975. calib_resp->res_cfg.th_vi_ca_state,
  2976. calib_resp->res_cfg.r0_cali_q24);
  2977. ret = 0;
  2978. fail_cmd:
  2979. return ret;
  2980. }
  2981. #ifdef CONFIG_SND_SOC_MAXIM_DSM
  2982. int afe_dsm_spk_prot_get_calib_data(struct afe_dsm_spkr_prot_get_vi_calib *calib_resp)
  2983. {
  2984. int ret = -EINVAL;
  2985. int index = 0, port = maxdsm_get_port_id();
  2986. pr_info("%s: port_id = 0x%x, module_id = 0x%x\n",
  2987. __func__, q6audio_get_port_id(port), maxdsm_get_rx_mod_id());
  2988. if (!calib_resp) {
  2989. pr_err("%s Invalid params\n", __func__);
  2990. goto fail_cmd;
  2991. }
  2992. if ((q6audio_validate_port(port) < 0)) {
  2993. pr_err("%s invalid port %d\n", __func__, port);
  2994. goto fail_cmd;
  2995. }
  2996. index = q6audio_get_port_index(port);
  2997. calib_resp->get_param.hdr.hdr_field =
  2998. APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  2999. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  3000. calib_resp->get_param.hdr.pkt_size = sizeof(*calib_resp);
  3001. calib_resp->get_param.hdr.src_port = 0;
  3002. calib_resp->get_param.hdr.dest_port = 0;
  3003. calib_resp->get_param.hdr.token = index;
  3004. calib_resp->get_param.hdr.opcode = AFE_PORT_CMD_GET_PARAM_V2;
  3005. calib_resp->get_param.mem_map_handle = 0;
  3006. calib_resp->get_param.module_id = maxdsm_get_rx_mod_id();
  3007. calib_resp->get_param.param_id = AFE_PARAM_ID_CALIB_RES_CFG;
  3008. calib_resp->get_param.payload_address_lsw = 0;
  3009. calib_resp->get_param.payload_address_msw = 0;
  3010. calib_resp->get_param.payload_size = sizeof(*calib_resp)
  3011. - sizeof(calib_resp->get_param);
  3012. calib_resp->get_param.port_id = q6audio_get_port_id(port);
  3013. calib_resp->pdata.module_id = maxdsm_get_rx_mod_id();
  3014. calib_resp->pdata.param_id = AFE_PARAM_ID_CALIB_RES_CFG;
  3015. calib_resp->pdata.param_size = sizeof(calib_resp->res_cfg);
  3016. atomic_set(&this_afe.state, 1);
  3017. ret = apr_send_pkt(this_afe.apr, (uint32_t *)calib_resp);
  3018. if (ret < 0) {
  3019. pr_err("%s: get param port %d param id[0x%x]failed\n",
  3020. __func__, port, calib_resp->get_param.param_id);
  3021. goto fail_cmd;
  3022. }
  3023. ret = wait_event_timeout(this_afe.wait[index],
  3024. (atomic_read(&this_afe.state) == 0),
  3025. msecs_to_jiffies(TIMEOUT_MS));
  3026. if (!ret) {
  3027. pr_err("%s: wait_event timeout\n", __func__);
  3028. ret = -EINVAL;
  3029. goto fail_cmd;
  3030. }
  3031. if (atomic_read(&this_afe.status) != 0) {
  3032. pr_err("%s: config cmd failed\n", __func__);
  3033. ret = -EINVAL;
  3034. goto fail_cmd;
  3035. }
  3036. memcpy(&calib_resp->res_cfg , &this_afe.calib_data.res_cfg,
  3037. sizeof(this_afe.calib_data.res_cfg));
  3038. ret = 0;
  3039. fail_cmd:
  3040. return ret;
  3041. }
  3042. #endif /* CONFIG_SND_SOC_MAXIM_DSM */
  3043. int afe_spk_prot_feed_back_cfg(int src_port, int dst_port,
  3044. int l_ch, int r_ch, u32 enable)
  3045. {
  3046. int ret = -EINVAL;
  3047. union afe_spkr_prot_config prot_config;
  3048. int index = 0;
  3049. if (!enable) {
  3050. pr_debug("%s Disable Feedback tx path", __func__);
  3051. this_afe.vi_tx_port = -1;
  3052. this_afe.vi_rx_port = -1;
  3053. return 0;
  3054. }
  3055. if ((q6audio_validate_port(src_port) < 0) ||
  3056. (q6audio_validate_port(dst_port) < 0)) {
  3057. pr_err("%s invalid ports src %d dst %d",
  3058. __func__, src_port, dst_port);
  3059. goto fail_cmd;
  3060. }
  3061. if (!l_ch && !r_ch) {
  3062. pr_err("%s error ch values zero\n", __func__);
  3063. goto fail_cmd;
  3064. }
  3065. pr_debug("%s src_port %x dst_port %x l_ch %d r_ch %d\n",
  3066. __func__, src_port, dst_port, l_ch, r_ch);
  3067. memset(&prot_config, 0, sizeof(prot_config));
  3068. prot_config.feedback_path_cfg.dst_portid =
  3069. q6audio_get_port_id(dst_port);
  3070. if (l_ch) {
  3071. prot_config.feedback_path_cfg.chan_info[index++] = 1;
  3072. prot_config.feedback_path_cfg.chan_info[index++] = 2;
  3073. }
  3074. if (r_ch) {
  3075. prot_config.feedback_path_cfg.chan_info[index++] = 3;
  3076. prot_config.feedback_path_cfg.chan_info[index++] = 4;
  3077. }
  3078. prot_config.feedback_path_cfg.num_channels = index;
  3079. prot_config.feedback_path_cfg.minor_version = 1;
  3080. ret = afe_spk_prot_prepare(src_port, dst_port,
  3081. AFE_PARAM_ID_FEEDBACK_PATH_CFG, &prot_config);
  3082. fail_cmd:
  3083. return ret;
  3084. }
  3085. #ifdef CONFIG_SND_SOC_MAXIM_DSM
  3086. int afe_dsm_spk_prot_feed_back_cfg(int src_port,
  3087. struct afe_dsm_filter_set_params_t *dsm_set_config)
  3088. {
  3089. return afe_dsm_spk_prot_prepare(src_port,
  3090. AFE_PARAM_ID_FBSP_MODE_RX_CFG,
  3091. (union afe_dsm_spkr_prot_config *)dsm_set_config);
  3092. }
  3093. static int dsm_get_afe_params(
  3094. void *param,
  3095. int param_size,
  3096. void *data,
  3097. int index)
  3098. {
  3099. struct maxim_dsm *maxdsm = (struct maxim_dsm*)data;
  3100. unsigned int *p = (unsigned int*)param;
  3101. int idx = index;
  3102. int binfo_idx = 0;
  3103. int i;
  3104. for (i=0;i<param_size;i++) {
  3105. maxdsm->param[idx++] = *(p+i);
  3106. binfo_idx = (idx - 1) >> 1;
  3107. maxdsm->param[idx++] = 1 << maxdsm->binfo[binfo_idx];
  3108. #ifdef USE_DSM_DEBUG
  3109. pr_info("%s: [%d,%d]: 0x%08x, 0x%08x\n",
  3110. __func__,
  3111. idx - 2, idx -1,
  3112. maxdsm->param[idx - 2], maxdsm->param[idx - 1]);
  3113. #endif /* USE_DSM_DEBUG */
  3114. }
  3115. return idx;
  3116. }
  3117. static int dsm_set_afe_params(
  3118. void *param,
  3119. int param_size,
  3120. void *data,
  3121. int index)
  3122. {
  3123. struct maxim_dsm *maxdsm = (struct maxim_dsm*)data;
  3124. unsigned int *p = (unsigned int*)param;
  3125. int idx = index;
  3126. int i;
  3127. for (i=0;i<param_size;i++) {
  3128. *(p+i) = maxdsm->param[idx];
  3129. idx += 2;
  3130. #ifdef USE_DSM_DEBUG
  3131. pr_info("%s: [%d,%d]: 0x%08x / 0x%08x -> 0x%08x\n",
  3132. __func__,
  3133. idx - 2, idx - 1,
  3134. maxdsm->param[idx - 2], maxdsm->param[idx - 1],
  3135. *(p+i));
  3136. #endif /* USE_DSM_DEBUG */
  3137. }
  3138. return idx;
  3139. }
  3140. static int dsm_get_param_size(int version)
  3141. {
  3142. int param_size = 0;
  3143. switch (version) {
  3144. case VERSION_3_0:
  3145. param_size = PARAM_DSM_3_0_MAX;
  3146. break;
  3147. case VERSION_3_5_B:
  3148. param_size = PARAM_DSM_3_5_MAX;
  3149. break;
  3150. case VERSION_4_0_B:
  3151. param_size = PARAM_DSM_4_0_MAX;
  3152. break;
  3153. default:
  3154. param_size = -EINVAL;
  3155. break;
  3156. }
  3157. #ifdef USE_DSM_DEBUG
  3158. pr_info("%s: param_size: %d, version: %d\n",
  3159. __func__, param_size, version);
  3160. #endif /* USE_DSM_DEBUG */
  3161. return param_size;
  3162. }
  3163. int32_t dsm_open(void *data)
  3164. {
  3165. struct afe_dsm_spkr_prot_get_vi_calib calib_resp;
  3166. struct afe_dsm_filter_set_params_t filter_params;
  3167. struct maxim_dsm *maxdsm = (struct maxim_dsm*)data;
  3168. uint32_t dsm_params = maxdsm->filter_set;
  3169. uint32_t version = maxdsm->version;
  3170. int32_t ret = 0;
  3171. pr_info("%s: dsm_params: %d\n", __func__, dsm_params);
  3172. switch (dsm_params) {
  3173. case DSM_ID_FILTER_GET_AFE_PARAMS:
  3174. if (afe_dsm_spk_prot_get_calib_data(&calib_resp)) {
  3175. ret = -EINVAL;
  3176. break;
  3177. }
  3178. if (maxdsm->param && maxdsm->binfo) {
  3179. dsm_get_afe_params(
  3180. &calib_resp.res_cfg.dcResistance,
  3181. (int)(dsm_get_param_size(version) >> 1),
  3182. maxdsm,
  3183. 0);
  3184. #ifdef USE_DSM_LOG
  3185. if (likely(calib_resp.res_cfg.byteLogArray[0] & 0x3)) {
  3186. maxdsm_log_update(calib_resp.res_cfg.byteLogArray,
  3187. calib_resp.res_cfg.intLogArray,
  3188. calib_resp.res_cfg.afterProbByteLogArray,
  3189. calib_resp.res_cfg.afterProbIntLogArray);
  3190. }
  3191. #endif /* USE_DSM_LOG */
  3192. }
  3193. break;
  3194. case DSM_ID_FILTER_SET_AFE_CNTRLS:
  3195. if (!maxdsm->param || !maxdsm->binfo) {
  3196. ret = -EINVAL;
  3197. break;
  3198. }
  3199. dsm_set_afe_params(
  3200. &filter_params.dcResistance,
  3201. (int)(dsm_get_param_size(version) >> 1),
  3202. maxdsm,
  3203. 0);
  3204. ret = afe_dsm_spk_prot_feed_back_cfg(maxdsm_get_port_id(),
  3205. &filter_params);
  3206. break;
  3207. }
  3208. pr_info("%s: ret=%d\n", __func__, ret);
  3209. return ret;
  3210. }
  3211. #endif /* CONFIG_SND_SOC_MAXIM_DSM */
  3212. static int __init afe_init(void)
  3213. {
  3214. int i = 0;
  3215. atomic_set(&this_afe.state, 0);
  3216. atomic_set(&this_afe.status, 0);
  3217. atomic_set(&this_afe.mem_map_cal_index, -1);
  3218. this_afe.apr = NULL;
  3219. this_afe.dtmf_gen_rx_portid = -1;
  3220. this_afe.mmap_handle = 0;
  3221. this_afe.vi_tx_port = -1;
  3222. this_afe.vi_rx_port = -1;
  3223. for (i = 0; i < AFE_MAX_PORTS; i++)
  3224. init_waitqueue_head(&this_afe.wait[i]);
  3225. config_debug_fs_init();
  3226. return 0;
  3227. }
  3228. static void __exit afe_exit(void)
  3229. {
  3230. int i;
  3231. atomic_t *hptr;
  3232. u32 handle;
  3233. config_debug_fs_exit();
  3234. for (i = 0; i < ARRAY_SIZE(this_afe.mem_map_cal_handles); i++) {
  3235. hptr = &this_afe.mem_map_cal_handles[i];
  3236. handle = atomic_xchg(hptr, 0);
  3237. if (handle != 0)
  3238. afe_cmd_memory_unmap_nowait(handle);
  3239. }
  3240. }
  3241. device_initcall(afe_init);
  3242. __exitcall(afe_exit);