msm-ds2-dap-config.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204
  1. /*
  2. * Copyright (c) 2013-2018, The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/err.h>
  14. #include <linux/module.h>
  15. #include <linux/bitops.h>
  16. #include <sound/control.h>
  17. #include <dsp/q6adm-v2.h>
  18. #include <dsp/q6core.h>
  19. #include <dsp/q6common.h>
  20. #include "msm-ds2-dap-config.h"
  21. #include "msm-pcm-routing-v2.h"
  22. /* ramp up/down for 30ms */
  23. #define DOLBY_SOFT_VOLUME_PERIOD 40
  24. /* Step value 0ms or 0us */
  25. #define DOLBY_SOFT_VOLUME_STEP 1000
  26. #define DOLBY_ADDITIONAL_RAMP_WAIT 10
  27. #define SOFT_VOLUME_PARAM_SIZE 3
  28. #define PARAM_PAYLOAD_SIZE 3
  29. enum {
  30. DOLBY_SOFT_VOLUME_CURVE_LINEAR = 0,
  31. DOLBY_SOFT_VOLUME_CURVE_EXP,
  32. DOLBY_SOFT_VOLUME_CURVE_LOG,
  33. };
  34. #define VOLUME_ZERO_GAIN 0x0
  35. #define VOLUME_UNITY_GAIN 0x2000
  36. /* Wait time for module enable/disble */
  37. #define DOLBY_MODULE_ENABLE_PERIOD 50
  38. /* DOLBY device definitions end */
  39. enum {
  40. DOLBY_OFF_CACHE = 0,
  41. DOLBY_SPEAKER_CACHE,
  42. DOLBY_HEADPHONE_CACHE,
  43. DOLBY_HDMI_CACHE,
  44. DOLBY_WFD_CACHE,
  45. DOLBY_FM_CACHE,
  46. DOLBY_MAX_CACHE,
  47. };
  48. enum {
  49. DAP_SOFT_BYPASS = 0,
  50. DAP_HARD_BYPASS,
  51. };
  52. enum {
  53. MODULE_DISABLE = 0,
  54. MODULE_ENABLE,
  55. };
  56. /* dolby param ids to/from dsp */
  57. static uint32_t ds2_dap_params_id[MAX_DS2_PARAMS] = {
  58. DOLBY_PARAM_ID_VDHE, DOLBY_PARAM_ID_VSPE, DOLBY_PARAM_ID_DSSF,
  59. DOLBY_PARAM_ID_DVLI, DOLBY_PARAM_ID_DVLO, DOLBY_PARAM_ID_DVLE,
  60. DOLBY_PARAM_ID_DVMC, DOLBY_PARAM_ID_DVME, DOLBY_PARAM_ID_IENB,
  61. DOLBY_PARAM_ID_IEBF, DOLBY_PARAM_ID_IEON, DOLBY_PARAM_ID_DEON,
  62. DOLBY_PARAM_ID_NGON, DOLBY_PARAM_ID_GEON, DOLBY_PARAM_ID_GENB,
  63. DOLBY_PARAM_ID_GEBF, DOLBY_PARAM_ID_AONB, DOLBY_PARAM_ID_AOBF,
  64. DOLBY_PARAM_ID_AOBG, DOLBY_PARAM_ID_AOON, DOLBY_PARAM_ID_ARNB,
  65. DOLBY_PARAM_ID_ARBF, DOLBY_PARAM_ID_PLB, DOLBY_PARAM_ID_PLMD,
  66. DOLBY_PARAM_ID_DHSB, DOLBY_PARAM_ID_DHRG, DOLBY_PARAM_ID_DSSB,
  67. DOLBY_PARAM_ID_DSSA, DOLBY_PARAM_ID_DVLA, DOLBY_PARAM_ID_IEBT,
  68. DOLBY_PARAM_ID_IEA, DOLBY_PARAM_ID_DEA, DOLBY_PARAM_ID_DED,
  69. DOLBY_PARAM_ID_GEBG, DOLBY_PARAM_ID_AOCC, DOLBY_PARAM_ID_ARBI,
  70. DOLBY_PARAM_ID_ARBL, DOLBY_PARAM_ID_ARBH, DOLBY_PARAM_ID_AROD,
  71. DOLBY_PARAM_ID_ARTP, DOLBY_PARAM_ID_VMON, DOLBY_PARAM_ID_VMB,
  72. DOLBY_PARAM_ID_VCNB, DOLBY_PARAM_ID_VCBF, DOLBY_PARAM_ID_PREG,
  73. DOLBY_PARAM_ID_VEN, DOLBY_PARAM_ID_PSTG, DOLBY_PARAM_ID_INIT_ENDP,
  74. };
  75. /* modifed state: 0x00000000 - Not updated
  76. * > 0x00000000 && < 0x00010000
  77. * Updated and not committed to DSP
  78. * 0x00010001 - Updated and committed to DSP
  79. * > 0x00010001 - Modified the committed value
  80. */
  81. /* param offset */
  82. static uint32_t ds2_dap_params_offset[MAX_DS2_PARAMS] = {
  83. DOLBY_PARAM_VDHE_OFFSET, DOLBY_PARAM_VSPE_OFFSET,
  84. DOLBY_PARAM_DSSF_OFFSET, DOLBY_PARAM_DVLI_OFFSET,
  85. DOLBY_PARAM_DVLO_OFFSET, DOLBY_PARAM_DVLE_OFFSET,
  86. DOLBY_PARAM_DVMC_OFFSET, DOLBY_PARAM_DVME_OFFSET,
  87. DOLBY_PARAM_IENB_OFFSET, DOLBY_PARAM_IEBF_OFFSET,
  88. DOLBY_PARAM_IEON_OFFSET, DOLBY_PARAM_DEON_OFFSET,
  89. DOLBY_PARAM_NGON_OFFSET, DOLBY_PARAM_GEON_OFFSET,
  90. DOLBY_PARAM_GENB_OFFSET, DOLBY_PARAM_GEBF_OFFSET,
  91. DOLBY_PARAM_AONB_OFFSET, DOLBY_PARAM_AOBF_OFFSET,
  92. DOLBY_PARAM_AOBG_OFFSET, DOLBY_PARAM_AOON_OFFSET,
  93. DOLBY_PARAM_ARNB_OFFSET, DOLBY_PARAM_ARBF_OFFSET,
  94. DOLBY_PARAM_PLB_OFFSET, DOLBY_PARAM_PLMD_OFFSET,
  95. DOLBY_PARAM_DHSB_OFFSET, DOLBY_PARAM_DHRG_OFFSET,
  96. DOLBY_PARAM_DSSB_OFFSET, DOLBY_PARAM_DSSA_OFFSET,
  97. DOLBY_PARAM_DVLA_OFFSET, DOLBY_PARAM_IEBT_OFFSET,
  98. DOLBY_PARAM_IEA_OFFSET, DOLBY_PARAM_DEA_OFFSET,
  99. DOLBY_PARAM_DED_OFFSET, DOLBY_PARAM_GEBG_OFFSET,
  100. DOLBY_PARAM_AOCC_OFFSET, DOLBY_PARAM_ARBI_OFFSET,
  101. DOLBY_PARAM_ARBL_OFFSET, DOLBY_PARAM_ARBH_OFFSET,
  102. DOLBY_PARAM_AROD_OFFSET, DOLBY_PARAM_ARTP_OFFSET,
  103. DOLBY_PARAM_VMON_OFFSET, DOLBY_PARAM_VMB_OFFSET,
  104. DOLBY_PARAM_VCNB_OFFSET, DOLBY_PARAM_VCBF_OFFSET,
  105. DOLBY_PARAM_PREG_OFFSET, DOLBY_PARAM_VEN_OFFSET,
  106. DOLBY_PARAM_PSTG_OFFSET, DOLBY_PARAM_INT_ENDP_OFFSET,
  107. };
  108. /* param_length */
  109. static uint32_t ds2_dap_params_length[MAX_DS2_PARAMS] = {
  110. DOLBY_PARAM_VDHE_LENGTH, DOLBY_PARAM_VSPE_LENGTH,
  111. DOLBY_PARAM_DSSF_LENGTH, DOLBY_PARAM_DVLI_LENGTH,
  112. DOLBY_PARAM_DVLO_LENGTH, DOLBY_PARAM_DVLE_LENGTH,
  113. DOLBY_PARAM_DVMC_LENGTH, DOLBY_PARAM_DVME_LENGTH,
  114. DOLBY_PARAM_IENB_LENGTH, DOLBY_PARAM_IEBF_LENGTH,
  115. DOLBY_PARAM_IEON_LENGTH, DOLBY_PARAM_DEON_LENGTH,
  116. DOLBY_PARAM_NGON_LENGTH, DOLBY_PARAM_GEON_LENGTH,
  117. DOLBY_PARAM_GENB_LENGTH, DOLBY_PARAM_GEBF_LENGTH,
  118. DOLBY_PARAM_AONB_LENGTH, DOLBY_PARAM_AOBF_LENGTH,
  119. DOLBY_PARAM_AOBG_LENGTH, DOLBY_PARAM_AOON_LENGTH,
  120. DOLBY_PARAM_ARNB_LENGTH, DOLBY_PARAM_ARBF_LENGTH,
  121. DOLBY_PARAM_PLB_LENGTH, DOLBY_PARAM_PLMD_LENGTH,
  122. DOLBY_PARAM_DHSB_LENGTH, DOLBY_PARAM_DHRG_LENGTH,
  123. DOLBY_PARAM_DSSB_LENGTH, DOLBY_PARAM_DSSA_LENGTH,
  124. DOLBY_PARAM_DVLA_LENGTH, DOLBY_PARAM_IEBT_LENGTH,
  125. DOLBY_PARAM_IEA_LENGTH, DOLBY_PARAM_DEA_LENGTH,
  126. DOLBY_PARAM_DED_LENGTH, DOLBY_PARAM_GEBG_LENGTH,
  127. DOLBY_PARAM_AOCC_LENGTH, DOLBY_PARAM_ARBI_LENGTH,
  128. DOLBY_PARAM_ARBL_LENGTH, DOLBY_PARAM_ARBH_LENGTH,
  129. DOLBY_PARAM_AROD_LENGTH, DOLBY_PARAM_ARTP_LENGTH,
  130. DOLBY_PARAM_VMON_LENGTH, DOLBY_PARAM_VMB_LENGTH,
  131. DOLBY_PARAM_VCNB_LENGTH, DOLBY_PARAM_VCBF_LENGTH,
  132. DOLBY_PARAM_PREG_LENGTH, DOLBY_PARAM_VEN_LENGTH,
  133. DOLBY_PARAM_PSTG_LENGTH, DOLBY_PARAM_INT_ENDP_LENGTH,
  134. };
  135. struct ds2_dap_params_s {
  136. int32_t params_val[TOTAL_LENGTH_DS2_PARAM];
  137. int32_t dap_params_modified[MAX_DS2_PARAMS];
  138. };
  139. struct audio_rx_cal_data {
  140. char aud_proc_data[AUD_PROC_BLOCK_SIZE];
  141. int32_t aud_proc_size;
  142. char aud_vol_data[AUD_VOL_BLOCK_SIZE];
  143. int32_t aud_vol_size;
  144. };
  145. static struct ds2_dap_params_s ds2_dap_params[DOLBY_MAX_CACHE];
  146. struct ds2_device_mapping {
  147. int32_t device_id; /* audio_out_... */
  148. int port_id; /* afe port. constant for a target variant. routing-v2*/
  149. /*Only one Dolby COPP for a specific port*/
  150. int copp_idx; /* idx for the copp port on which ds2 is active */
  151. int cache_dev; /* idx to a shared parameter array dependent on device*/
  152. uint32_t stream_ref_count;
  153. bool active;
  154. void *cal_data;
  155. };
  156. static struct ds2_device_mapping dev_map[DS2_DEVICES_ALL];
  157. struct ds2_dap_params_states_s {
  158. bool use_cache;
  159. bool dap_bypass;
  160. bool dap_bypass_type;
  161. bool node_opened;
  162. int32_t device;
  163. bool custom_stereo_onoff;
  164. };
  165. static struct ds2_dap_params_states_s ds2_dap_params_states = {true, false,
  166. false, DEVICE_NONE};
  167. static int all_supported_devices = EARPIECE|SPEAKER|WIRED_HEADSET|
  168. WIRED_HEADPHONE|BLUETOOTH_SCO|AUX_DIGITAL|
  169. ANLG_DOCK_HEADSET|DGTL_DOCK_HEADSET|
  170. REMOTE_SUBMIX|ANC_HEADSET|ANC_HEADPHONE|
  171. PROXY|FM|FM_TX|DEVICE_NONE|
  172. BLUETOOTH_SCO_HEADSET|BLUETOOTH_SCO_CARKIT;
  173. static void msm_ds2_dap_check_and_update_ramp_wait(int port_id, int copp_idx,
  174. int *ramp_wait)
  175. {
  176. int32_t *update_params_value = NULL;
  177. uint32_t params_length = SOFT_VOLUME_PARAM_SIZE * sizeof(uint32_t);
  178. uint32_t param_payload_len = PARAM_PAYLOAD_SIZE * sizeof(uint32_t);
  179. struct param_hdr_v3 param_hdr;
  180. int rc = 0;
  181. update_params_value = kzalloc(params_length + param_payload_len,
  182. GFP_KERNEL);
  183. if (!update_params_value) {
  184. pr_err("%s: params memory alloc failed\n", __func__);
  185. goto end;
  186. }
  187. memset(&param_hdr, 0, sizeof(param_hdr));
  188. param_hdr.module_id = AUDPROC_MODULE_ID_VOL_CTRL;
  189. param_hdr.instance_id = INSTANCE_ID_0;
  190. param_hdr.param_id = AUDPROC_PARAM_ID_SOFT_VOL_STEPPING_PARAMETERS;
  191. param_hdr.param_size = params_length + param_payload_len;
  192. rc = adm_get_pp_params(port_id, copp_idx, ADM_CLIENT_ID_DEFAULT, NULL,
  193. &param_hdr, (char *) update_params_value);
  194. if (rc == 0) {
  195. pr_debug("%s: params_value [0x%x, 0x%x, 0x%x]\n",
  196. __func__, update_params_value[0],
  197. update_params_value[1],
  198. update_params_value[2]);
  199. *ramp_wait = update_params_value[0];
  200. }
  201. end:
  202. kfree(update_params_value);
  203. /*
  204. * No error returned as we do not need to error out from dap on/dap
  205. * bypass. The default ramp parameter will be used to wait during
  206. * ramp down.
  207. */
  208. }
  209. static int msm_ds2_dap_set_vspe_vdhe(int dev_map_idx,
  210. bool is_custom_stereo_enabled)
  211. {
  212. u8 *packed_param_data = NULL;
  213. u8 *param_data = NULL;
  214. struct param_hdr_v3 param_hdr;
  215. u32 packed_param_size = 0;
  216. u32 param_size = 0;
  217. int cdev;
  218. int rc = 0;
  219. if (dev_map_idx < 0 || dev_map_idx >= DS2_DEVICES_ALL) {
  220. pr_err("%s: invalid dev map index %d\n", __func__, dev_map_idx);
  221. rc = -EINVAL;
  222. goto end;
  223. }
  224. if (dev_map[dev_map_idx].port_id == DOLBY_INVALID_PORT_ID) {
  225. pr_err("%s: Invalid port id\n", __func__);
  226. rc = -EINVAL;
  227. goto end;
  228. }
  229. if ((dev_map[dev_map_idx].copp_idx < 0) ||
  230. (dev_map[dev_map_idx].copp_idx >= MAX_COPPS_PER_PORT)) {
  231. pr_err("%s: Invalid copp_idx\n", __func__);
  232. rc = -EINVAL;
  233. goto end;
  234. }
  235. if ((dev_map[dev_map_idx].port_id != SLIMBUS_0_RX) &&
  236. (dev_map[dev_map_idx].port_id != RT_PROXY_PORT_001_RX)) {
  237. pr_debug("%s:No Custom stereo for port:0x%x\n",
  238. __func__, dev_map[dev_map_idx].port_id);
  239. goto end;
  240. }
  241. /* Allocate the max space needed */
  242. packed_param_size = (TOTAL_LENGTH_DOLBY_PARAM * sizeof(uint32_t)) +
  243. (2 * sizeof(union param_hdrs));
  244. packed_param_data = kzalloc(packed_param_size, GFP_KERNEL);
  245. if (!packed_param_data)
  246. return -ENOMEM;
  247. packed_param_size = 0;
  248. memset(&param_hdr, 0, sizeof(param_hdr));
  249. /* Set common values */
  250. cdev = dev_map[dev_map_idx].cache_dev;
  251. param_hdr.module_id = DOLBY_BUNDLE_MODULE_ID;
  252. param_hdr.instance_id = INSTANCE_ID_0;
  253. /* Pack VDHE header + data */
  254. param_hdr.param_id = DOLBY_PARAM_ID_VDHE;
  255. param_size = DOLBY_PARAM_VDHE_LENGTH * sizeof(uint32_t);
  256. param_hdr.param_size = param_size;
  257. if (is_custom_stereo_enabled)
  258. param_data = NULL;
  259. else
  260. param_data = (u8 *) &ds2_dap_params[cdev]
  261. .params_val[DOLBY_PARAM_VDHE_OFFSET];
  262. rc = q6common_pack_pp_params(packed_param_data, &param_hdr, param_data,
  263. &param_size);
  264. if (rc) {
  265. pr_err("%s: Failed to pack params for dolby vdhe, error %d\n",
  266. __func__, rc);
  267. goto end;
  268. }
  269. packed_param_size += param_size;
  270. /* Pack VSPE header + data */
  271. param_hdr.param_id = DOLBY_PARAM_ID_VSPE;
  272. param_size = DOLBY_PARAM_VSPE_LENGTH * sizeof(uint32_t);
  273. param_hdr.param_size = param_size;
  274. if (is_custom_stereo_enabled)
  275. param_data = NULL;
  276. else
  277. param_data = (u8 *) &ds2_dap_params[cdev]
  278. .params_val[DOLBY_PARAM_VSPE_OFFSET];
  279. rc = q6common_pack_pp_params(packed_param_data + packed_param_size,
  280. &param_hdr, param_data, &param_size);
  281. if (rc) {
  282. pr_err("%s: Failed to pack params for dolby vspe, error %d\n",
  283. __func__, rc);
  284. goto end;
  285. }
  286. packed_param_size += param_size;
  287. rc = adm_set_pp_params(dev_map[dev_map_idx].port_id,
  288. dev_map[dev_map_idx].copp_idx, NULL,
  289. packed_param_data, packed_param_size);
  290. if (rc) {
  291. pr_err("%s: send vdhe/vspe params failed with rc=%d\n",
  292. __func__, rc);
  293. rc = -EINVAL;
  294. goto end;
  295. }
  296. end:
  297. kfree(packed_param_data);
  298. return rc;
  299. }
  300. int qti_set_custom_stereo_on(int port_id, int copp_idx,
  301. bool is_custom_stereo_on)
  302. {
  303. struct custom_stereo_param custom_stereo;
  304. struct param_hdr_v3 param_hdr;
  305. uint16_t op_FL_ip_FL_weight;
  306. uint16_t op_FL_ip_FR_weight;
  307. uint16_t op_FR_ip_FL_weight;
  308. uint16_t op_FR_ip_FR_weight;
  309. int rc = 0;
  310. if ((port_id != SLIMBUS_0_RX) &&
  311. (port_id != RT_PROXY_PORT_001_RX)) {
  312. pr_debug("%s:No Custom stereo for port:0x%x\n",
  313. __func__, port_id);
  314. return 0;
  315. }
  316. memset(&custom_stereo, 0, sizeof(custom_stereo));
  317. memset(&param_hdr, 0, sizeof(param_hdr));
  318. pr_debug("%s: port 0x%x, copp_idx %d, is_custom_stereo_on %d\n",
  319. __func__, port_id, copp_idx, is_custom_stereo_on);
  320. if (is_custom_stereo_on) {
  321. op_FL_ip_FL_weight =
  322. Q14_GAIN_ZERO_POINT_FIVE;
  323. op_FL_ip_FR_weight =
  324. Q14_GAIN_ZERO_POINT_FIVE;
  325. op_FR_ip_FL_weight =
  326. Q14_GAIN_ZERO_POINT_FIVE;
  327. op_FR_ip_FR_weight =
  328. Q14_GAIN_ZERO_POINT_FIVE;
  329. } else {
  330. op_FL_ip_FL_weight = Q14_GAIN_UNITY;
  331. op_FL_ip_FR_weight = 0;
  332. op_FR_ip_FL_weight = 0;
  333. op_FR_ip_FR_weight = Q14_GAIN_UNITY;
  334. }
  335. param_hdr.module_id = MTMX_MODULE_ID_DEFAULT_CHMIXER;
  336. param_hdr.instance_id = INSTANCE_ID_0;
  337. param_hdr.param_id = DEFAULT_CHMIXER_PARAM_ID_COEFF;
  338. param_hdr.param_size = sizeof(struct custom_stereo_param);
  339. /* index is 32-bit param in little endian*/
  340. custom_stereo.index = CUSTOM_STEREO_INDEX_PARAM;
  341. custom_stereo.reserved = 0;
  342. /* for stereo mixing num out ch*/
  343. custom_stereo.num_out_ch = CUSTOM_STEREO_NUM_OUT_CH;
  344. /* for stereo mixing num in ch*/
  345. custom_stereo.num_in_ch = CUSTOM_STEREO_NUM_IN_CH;
  346. /* Out ch map FL/FR*/
  347. custom_stereo.out_fl = PCM_CHANNEL_FL;
  348. custom_stereo.out_fr = PCM_CHANNEL_FR;
  349. /* In ch map FL/FR*/
  350. custom_stereo.in_fl = PCM_CHANNEL_FL;
  351. custom_stereo.in_fr = PCM_CHANNEL_FR;
  352. /* weighting coefficients as name suggests,
  353. * mixing will be done according to these coefficients
  354. */
  355. custom_stereo.op_FL_ip_FL_weight = op_FL_ip_FL_weight;
  356. custom_stereo.op_FL_ip_FR_weight = op_FL_ip_FR_weight;
  357. custom_stereo.op_FR_ip_FL_weight = op_FR_ip_FL_weight;
  358. custom_stereo.op_FR_ip_FR_weight = op_FR_ip_FR_weight;
  359. rc = adm_pack_and_set_one_pp_param(port_id, copp_idx, param_hdr,
  360. (u8 *) &custom_stereo);
  361. if (rc) {
  362. pr_err("%s: send params failed rc=%d\n", __func__, rc);
  363. return -EINVAL;
  364. }
  365. return 0;
  366. }
  367. static int dap_set_custom_stereo_onoff(int dev_map_idx,
  368. bool is_custom_stereo_enabled)
  369. {
  370. uint32_t enable = is_custom_stereo_enabled ? 1 : 0;
  371. struct param_hdr_v3 param_hdr;
  372. int rc = 0;
  373. if ((dev_map[dev_map_idx].port_id != SLIMBUS_0_RX) &&
  374. (dev_map[dev_map_idx].port_id != RT_PROXY_PORT_001_RX)) {
  375. pr_debug("%s:No Custom stereo for port:0x%x\n",
  376. __func__, dev_map[dev_map_idx].port_id);
  377. goto end;
  378. }
  379. if ((dev_map[dev_map_idx].copp_idx < 0) ||
  380. (dev_map[dev_map_idx].copp_idx >= MAX_COPPS_PER_PORT)) {
  381. rc = -EINVAL;
  382. goto end;
  383. }
  384. memset(&param_hdr, 0, sizeof(param_hdr));
  385. /* DAP custom stereo */
  386. msm_ds2_dap_set_vspe_vdhe(dev_map_idx,
  387. is_custom_stereo_enabled);
  388. param_hdr.module_id = DOLBY_BUNDLE_MODULE_ID;
  389. param_hdr.instance_id = INSTANCE_ID_0;
  390. param_hdr.param_id = DOLBY_ENABLE_CUSTOM_STEREO;
  391. param_hdr.param_size = sizeof(enable);
  392. rc = adm_pack_and_set_one_pp_param(dev_map[dev_map_idx].port_id,
  393. dev_map[dev_map_idx].copp_idx,
  394. param_hdr, (u8 *) &enable);
  395. if (rc) {
  396. pr_err("%s: set custom stereo enable failed with rc=%d\n",
  397. __func__, rc);
  398. rc = -EINVAL;
  399. }
  400. end:
  401. return rc;
  402. }
  403. static int set_custom_stereo_onoff(int dev_map_idx,
  404. bool is_custom_stereo_enabled)
  405. {
  406. int rc = 0;
  407. pr_debug("%s: map index %d, custom stereo %d\n", __func__, dev_map_idx,
  408. is_custom_stereo_enabled);
  409. if (dev_map_idx < 0 || dev_map_idx >= DS2_DEVICES_ALL) {
  410. pr_err("%s: invalid dev map index %d\n", __func__, dev_map_idx);
  411. rc = -EINVAL;
  412. goto end;
  413. }
  414. if (dev_map[dev_map_idx].port_id == DOLBY_INVALID_PORT_ID) {
  415. pr_err("%s: invalid port id\n", __func__);
  416. rc = -EINVAL;
  417. goto end;
  418. }
  419. if ((dev_map[dev_map_idx].copp_idx < 0) ||
  420. (dev_map[dev_map_idx].copp_idx >= MAX_COPPS_PER_PORT)) {
  421. pr_err("%s: invalid copp idx\n", __func__);
  422. rc = -EINVAL;
  423. goto end;
  424. }
  425. if (ds2_dap_params_states.dap_bypass == true &&
  426. ds2_dap_params_states.dap_bypass_type == DAP_HARD_BYPASS) {
  427. rc = qti_set_custom_stereo_on(dev_map[dev_map_idx].port_id,
  428. dev_map[dev_map_idx].copp_idx,
  429. is_custom_stereo_enabled);
  430. if (rc < 0) {
  431. pr_err("%s:qti_set_custom_stereo_on_copp failed C.S %d",
  432. __func__, is_custom_stereo_enabled);
  433. }
  434. goto end;
  435. }
  436. if (ds2_dap_params_states.dap_bypass == false) {
  437. rc = dap_set_custom_stereo_onoff(dev_map_idx,
  438. is_custom_stereo_enabled);
  439. if (rc < 0) {
  440. pr_err("%s:qti_set_custom_stereo_on_copp failed C.S %d",
  441. __func__, is_custom_stereo_enabled);
  442. }
  443. goto end;
  444. }
  445. end:
  446. return rc;
  447. }
  448. static int msm_ds2_dap_alloc_and_store_cal_data(int dev_map_idx, int path,
  449. int perf_mode)
  450. {
  451. int rc = 0;
  452. struct audio_rx_cal_data *aud_cal_data;
  453. pr_debug("%s: path %d, perf_mode %d, dev_map_idx %d\n",
  454. __func__, path, perf_mode, dev_map_idx);
  455. if (dev_map_idx < 0 || dev_map_idx >= DS2_DEVICES_ALL) {
  456. pr_err("%s: invalid dev map index %d\n", __func__, dev_map_idx);
  457. rc = -EINVAL;
  458. goto end;
  459. }
  460. aud_cal_data = kzalloc(sizeof(struct audio_rx_cal_data), GFP_KERNEL);
  461. if (!aud_cal_data) {
  462. rc = -ENOMEM;
  463. goto end;
  464. }
  465. rc = adm_store_cal_data(dev_map[dev_map_idx].port_id,
  466. dev_map[dev_map_idx].copp_idx, path, perf_mode,
  467. ADM_AUDPROC_CAL, aud_cal_data->aud_proc_data,
  468. &aud_cal_data->aud_proc_size);
  469. if (rc < 0) {
  470. pr_err("%s: store cal data err %d\n", __func__, rc);
  471. kfree(aud_cal_data);
  472. goto end;
  473. }
  474. rc = adm_store_cal_data(dev_map[dev_map_idx].port_id,
  475. dev_map[dev_map_idx].copp_idx, path, perf_mode,
  476. ADM_AUDVOL_CAL, aud_cal_data->aud_vol_data,
  477. &aud_cal_data->aud_vol_size);
  478. if (rc < 0) {
  479. pr_err("%s: store cal data err %d\n", __func__, rc);
  480. kfree(aud_cal_data);
  481. goto end;
  482. }
  483. dev_map[dev_map_idx].cal_data = (void *)aud_cal_data;
  484. end:
  485. pr_debug("%s: ret %d\n", __func__, rc);
  486. return rc;
  487. }
  488. static int msm_ds2_dap_free_cal_data(int dev_map_idx)
  489. {
  490. int rc = 0;
  491. struct audio_rx_cal_data *aud_cal_data;
  492. pr_debug("%s: dev_map_idx %d\n", __func__, dev_map_idx);
  493. if (dev_map_idx < 0 || dev_map_idx >= DS2_DEVICES_ALL) {
  494. pr_err("%s: invalid dev map index %d\n", __func__, dev_map_idx);
  495. rc = -EINVAL;
  496. goto end;
  497. }
  498. aud_cal_data = (struct audio_rx_cal_data *)
  499. dev_map[dev_map_idx].cal_data;
  500. kfree(aud_cal_data);
  501. dev_map[dev_map_idx].cal_data = NULL;
  502. end:
  503. return rc;
  504. }
  505. static int msm_ds2_dap_send_cal_data(int dev_map_idx)
  506. {
  507. int rc = 0;
  508. struct audio_rx_cal_data *aud_cal_data = NULL;
  509. pr_debug("%s: devmap index %d\n", __func__, dev_map_idx);
  510. if (dev_map_idx < 0 || dev_map_idx >= DS2_DEVICES_ALL) {
  511. pr_err("%s: invalid dev map index %d\n", __func__, dev_map_idx);
  512. rc = -EINVAL;
  513. goto end;
  514. }
  515. if (dev_map[dev_map_idx].cal_data == NULL) {
  516. pr_err("%s: No valid calibration data stored for idx %d\n",
  517. __func__, dev_map_idx);
  518. rc = -EINVAL;
  519. goto end;
  520. }
  521. /* send aud proc cal */
  522. aud_cal_data = (struct audio_rx_cal_data *)
  523. dev_map[dev_map_idx].cal_data;
  524. rc = adm_send_calibration(dev_map[dev_map_idx].port_id,
  525. dev_map[dev_map_idx].copp_idx,
  526. ADM_PATH_PLAYBACK, 0,
  527. ADM_AUDPROC_CAL,
  528. aud_cal_data->aud_proc_data,
  529. aud_cal_data->aud_proc_size);
  530. if (rc < 0) {
  531. pr_err("%s: adm_send_calibration failed %d\n", __func__, rc);
  532. goto end;
  533. }
  534. /* send aud volume cal*/
  535. rc = adm_send_calibration(dev_map[dev_map_idx].port_id,
  536. dev_map[dev_map_idx].copp_idx,
  537. ADM_PATH_PLAYBACK, 0,
  538. ADM_AUDVOL_CAL,
  539. aud_cal_data->aud_vol_data,
  540. aud_cal_data->aud_vol_size);
  541. if (rc < 0)
  542. pr_err("%s: adm_send_calibration failed %d\n", __func__, rc);
  543. end:
  544. pr_debug("%s: return %d\n", __func__, rc);
  545. return rc;
  546. }
  547. static inline int msm_ds2_dap_can_enable_module(int32_t module_id)
  548. {
  549. if (module_id == MTMX_MODULE_ID_DEFAULT_CHMIXER ||
  550. module_id == AUDPROC_MODULE_ID_RESAMPLER ||
  551. module_id == AUDPROC_MODULE_ID_VOL_CTRL) {
  552. return false;
  553. }
  554. return true;
  555. }
  556. static int msm_ds2_dap_init_modules_in_topology(int dev_map_idx)
  557. {
  558. int rc = 0, i = 0, port_id, copp_idx;
  559. /* Account for 32 bit integer allocation */
  560. int32_t param_sz =
  561. (ADM_GET_TOPO_MODULE_INSTANCE_LIST_LENGTH / sizeof(uint32_t));
  562. int32_t *update_param_val = NULL;
  563. struct module_instance_info mod_inst_info;
  564. int mod_inst_info_sz = 0;
  565. if (dev_map_idx < 0 || dev_map_idx >= DS2_DEVICES_ALL) {
  566. pr_err("%s: invalid dev map index %d\n", __func__, dev_map_idx);
  567. rc = -EINVAL;
  568. goto end;
  569. }
  570. memset(&mod_inst_info, 0, sizeof(mod_inst_info));
  571. port_id = dev_map[dev_map_idx].port_id;
  572. copp_idx = dev_map[dev_map_idx].copp_idx;
  573. pr_debug("%s: port_id 0x%x copp_idx %d\n", __func__, port_id, copp_idx);
  574. update_param_val =
  575. kzalloc(ADM_GET_TOPO_MODULE_INSTANCE_LIST_LENGTH, GFP_KERNEL);
  576. if (!update_param_val) {
  577. pr_err("%s, param memory alloc failed\n", __func__);
  578. rc = -ENOMEM;
  579. goto end;
  580. }
  581. if (!ds2_dap_params_states.dap_bypass) {
  582. /* get modules from dsp */
  583. rc = adm_get_pp_topo_module_list_v2(
  584. port_id, copp_idx,
  585. ADM_GET_TOPO_MODULE_INSTANCE_LIST_LENGTH,
  586. update_param_val);
  587. if (rc < 0) {
  588. pr_err("%s:topo list port %d, err %d,copp_idx %d\n",
  589. __func__, port_id, copp_idx, rc);
  590. goto end;
  591. }
  592. if (update_param_val[0] > (param_sz - 1)) {
  593. pr_err("%s:max modules exp/ret [%d: %d]\n",
  594. __func__, (param_sz - 1),
  595. update_param_val[0]);
  596. rc = -EINVAL;
  597. goto end;
  598. }
  599. mod_inst_info_sz = sizeof(struct module_instance_info) /
  600. sizeof(uint32_t);
  601. /* Turn off modules */
  602. for (i = 1; i < update_param_val[0] * mod_inst_info_sz;
  603. i += mod_inst_info_sz) {
  604. if (!msm_ds2_dap_can_enable_module(
  605. update_param_val[i]) ||
  606. (update_param_val[i] == DS2_MODULE_ID)) {
  607. pr_debug("%s: Do not enable/disable %d\n",
  608. __func__, update_param_val[i]);
  609. continue;
  610. }
  611. pr_debug("%s: param disable %d\n",
  612. __func__, update_param_val[i]);
  613. memcpy(&mod_inst_info, &update_param_val[i],
  614. sizeof(mod_inst_info));
  615. adm_param_enable_v2(port_id, copp_idx,
  616. mod_inst_info,
  617. MODULE_DISABLE);
  618. }
  619. } else {
  620. msm_ds2_dap_send_cal_data(dev_map_idx);
  621. }
  622. mod_inst_info.module_id = DS2_MODULE_ID;
  623. mod_inst_info.instance_id = INSTANCE_ID_0;
  624. adm_param_enable_v2(port_id, copp_idx, mod_inst_info,
  625. !ds2_dap_params_states.dap_bypass);
  626. end:
  627. kfree(update_param_val);
  628. return rc;
  629. }
  630. static bool msm_ds2_dap_check_is_param_modified(int32_t *dap_params_modified,
  631. int32_t idx, int32_t commit)
  632. {
  633. if ((dap_params_modified[idx] == 0) ||
  634. (commit &&
  635. ((dap_params_modified[idx] & 0x00010000) &&
  636. ((dap_params_modified[idx] & 0x0000FFFF) <= 1)))) {
  637. pr_debug("%s: not modified at idx %d\n", __func__, idx);
  638. return false;
  639. }
  640. pr_debug("%s: modified at idx %d\n", __func__, idx);
  641. return true;
  642. }
  643. static int msm_ds2_dap_map_device_to_dolby_cache_devices(int32_t device_id)
  644. {
  645. int32_t cache_dev = -1;
  646. switch (device_id) {
  647. case DEVICE_NONE:
  648. cache_dev = DOLBY_OFF_CACHE;
  649. break;
  650. case EARPIECE:
  651. case SPEAKER:
  652. cache_dev = DOLBY_SPEAKER_CACHE;
  653. break;
  654. case WIRED_HEADSET:
  655. case WIRED_HEADPHONE:
  656. case ANLG_DOCK_HEADSET:
  657. case DGTL_DOCK_HEADSET:
  658. case ANC_HEADSET:
  659. case ANC_HEADPHONE:
  660. case BLUETOOTH_SCO:
  661. case BLUETOOTH_SCO_HEADSET:
  662. case BLUETOOTH_SCO_CARKIT:
  663. cache_dev = DOLBY_HEADPHONE_CACHE;
  664. break;
  665. case FM:
  666. case FM_TX:
  667. cache_dev = DOLBY_FM_CACHE;
  668. break;
  669. case AUX_DIGITAL:
  670. cache_dev = DOLBY_HDMI_CACHE;
  671. break;
  672. case PROXY:
  673. case REMOTE_SUBMIX:
  674. cache_dev = DOLBY_WFD_CACHE;
  675. break;
  676. default:
  677. pr_err("%s: invalid cache device\n", __func__);
  678. }
  679. pr_debug("%s: cache device %d\n", __func__, cache_dev);
  680. return cache_dev;
  681. }
  682. static int msm_ds2_dap_update_num_devices(struct dolby_param_data *dolby_data,
  683. int32_t *num_device, int32_t *dev_arr,
  684. int32_t array_size)
  685. {
  686. int32_t idx = 0;
  687. int supported_devices = 0;
  688. if (!array_size) {
  689. pr_err("%s: array size zero\n", __func__);
  690. return -EINVAL;
  691. }
  692. if (dolby_data->device_id == DEVICE_OUT_ALL ||
  693. dolby_data->device_id == DEVICE_OUT_DEFAULT)
  694. supported_devices = all_supported_devices;
  695. else
  696. supported_devices = dolby_data->device_id;
  697. if ((idx < array_size) && (supported_devices & EARPIECE))
  698. dev_arr[idx++] = EARPIECE;
  699. if ((idx < array_size) && (supported_devices & SPEAKER))
  700. dev_arr[idx++] = SPEAKER;
  701. if ((idx < array_size) && (supported_devices & WIRED_HEADSET))
  702. dev_arr[idx++] = WIRED_HEADSET;
  703. if ((idx < array_size) && (supported_devices & WIRED_HEADPHONE))
  704. dev_arr[idx++] = WIRED_HEADPHONE;
  705. if ((idx < array_size) && (supported_devices & BLUETOOTH_SCO))
  706. dev_arr[idx++] = BLUETOOTH_SCO;
  707. if ((idx < array_size) && (supported_devices & BLUETOOTH_SCO_CARKIT))
  708. dev_arr[idx++] = BLUETOOTH_SCO_CARKIT;
  709. if ((idx < array_size) && (supported_devices & BLUETOOTH_SCO_HEADSET))
  710. dev_arr[idx++] = BLUETOOTH_SCO_HEADSET;
  711. if ((idx < array_size) && (supported_devices & AUX_DIGITAL))
  712. dev_arr[idx++] = AUX_DIGITAL;
  713. if ((idx < array_size) && (supported_devices & ANLG_DOCK_HEADSET))
  714. dev_arr[idx++] = ANLG_DOCK_HEADSET;
  715. if ((idx < array_size) && (supported_devices & DGTL_DOCK_HEADSET))
  716. dev_arr[idx++] = DGTL_DOCK_HEADSET;
  717. if ((idx < array_size) && (supported_devices & REMOTE_SUBMIX))
  718. dev_arr[idx++] = REMOTE_SUBMIX;
  719. if ((idx < array_size) && (supported_devices & ANC_HEADSET))
  720. dev_arr[idx++] = ANC_HEADSET;
  721. if ((idx < array_size) && (supported_devices & ANC_HEADPHONE))
  722. dev_arr[idx++] = ANC_HEADPHONE;
  723. if ((idx < array_size) && (supported_devices & PROXY))
  724. dev_arr[idx++] = PROXY;
  725. if ((idx < array_size) && (supported_devices & FM))
  726. dev_arr[idx++] = FM;
  727. if ((idx < array_size) && (supported_devices & FM_TX))
  728. dev_arr[idx++] = FM_TX;
  729. /* CHECK device none separately */
  730. if ((idx < array_size) && (supported_devices == DEVICE_NONE))
  731. dev_arr[idx++] = DEVICE_NONE;
  732. pr_debug("%s: dev id 0x%x, idx %d\n", __func__,
  733. supported_devices, idx);
  734. *num_device = idx;
  735. return 0;
  736. }
  737. static int msm_ds2_dap_get_port_id(
  738. int32_t device_id, int32_t be_id)
  739. {
  740. struct msm_pcm_routing_bdai_data bedais;
  741. int port_id = DOLBY_INVALID_PORT_ID;
  742. int port_type = 0;
  743. if (be_id < 0) {
  744. port_id = -1;
  745. goto end;
  746. }
  747. msm_pcm_routing_get_bedai_info(be_id, &bedais);
  748. pr_debug("%s: be port_id %d\n", __func__, bedais.port_id);
  749. port_id = bedais.port_id;
  750. port_type = afe_get_port_type(bedais.port_id);
  751. if (port_type != MSM_AFE_PORT_TYPE_RX)
  752. port_id = DOLBY_INVALID_PORT_ID;
  753. end:
  754. pr_debug("%s: device_id 0x%x, be_id %d, port_id %d\n",
  755. __func__, device_id, be_id, port_id);
  756. return port_id;
  757. }
  758. static int msm_ds2_dap_update_dev_map_port_id(int32_t device_id, int port_id)
  759. {
  760. int i;
  761. for (i = 0; i < DS2_DEVICES_ALL; i++) {
  762. if (dev_map[i].device_id == device_id)
  763. dev_map[i].port_id = port_id;
  764. }
  765. pr_debug("%s: port_id %d, device_id 0x%x\n",
  766. __func__, port_id, device_id);
  767. return 0;
  768. }
  769. static int msm_ds2_dap_handle_bypass_wait(int port_id, int copp_idx,
  770. int wait_time)
  771. {
  772. int ret = 0;
  773. adm_set_wait_parameters(port_id, copp_idx);
  774. msm_pcm_routing_release_lock();
  775. ret = adm_wait_timeout(port_id, copp_idx, wait_time);
  776. msm_pcm_routing_acquire_lock();
  777. /* Reset the parameters if wait has timed out */
  778. if (ret == 0)
  779. adm_reset_wait_parameters(port_id, copp_idx);
  780. return ret;
  781. }
  782. static int msm_ds2_dap_handle_bypass(struct dolby_param_data *dolby_data)
  783. {
  784. int rc = 0, i = 0, j = 0;
  785. /*Account for 32 bit integer allocation */
  786. int32_t param_sz =
  787. (ADM_GET_TOPO_MODULE_INSTANCE_LIST_LENGTH / sizeof(uint32_t));
  788. int32_t *mod_list = NULL;
  789. int port_id = 0, copp_idx = -1;
  790. bool cs_onoff = ds2_dap_params_states.custom_stereo_onoff;
  791. int ramp_wait = DOLBY_SOFT_VOLUME_PERIOD;
  792. struct module_instance_info mod_inst_info;
  793. int mod_inst_info_sz = 0;
  794. pr_debug("%s: bypass type %d bypass %d custom stereo %d\n", __func__,
  795. ds2_dap_params_states.dap_bypass_type,
  796. ds2_dap_params_states.dap_bypass,
  797. ds2_dap_params_states.custom_stereo_onoff);
  798. memset(&mod_inst_info, 0, sizeof(mod_inst_info));
  799. mod_list =
  800. kzalloc(ADM_GET_TOPO_MODULE_INSTANCE_LIST_LENGTH, GFP_KERNEL);
  801. if (!mod_list) {
  802. pr_err("%s: param memory alloc failed\n", __func__);
  803. rc = -ENOMEM;
  804. goto end;
  805. }
  806. for (i = 0; i < DS2_DEVICES_ALL; i++) {
  807. pr_debug("%s: active dev %d\n", __func__, dev_map[i].active);
  808. if (dev_map[i].active) {
  809. port_id = dev_map[i].port_id;
  810. copp_idx = dev_map[i].copp_idx;
  811. if (port_id == DOLBY_INVALID_PORT_ID) {
  812. pr_err("%s: invalid port\n", __func__);
  813. rc = 0;
  814. goto end;
  815. }
  816. if ((copp_idx < 0) ||
  817. (copp_idx >= MAX_COPPS_PER_PORT)) {
  818. pr_err("%s: Invalid copp_idx\n", __func__);
  819. rc = 0;
  820. goto end;
  821. }
  822. /* getmodules from dsp */
  823. rc = adm_get_pp_topo_module_list_v2(
  824. port_id, copp_idx,
  825. ADM_GET_TOPO_MODULE_INSTANCE_LIST_LENGTH,
  826. mod_list);
  827. if (rc < 0) {
  828. pr_err("%s:adm get topo list port %d",
  829. __func__, port_id);
  830. pr_err("copp_idx %d, err %d\n",
  831. copp_idx, rc);
  832. goto end;
  833. }
  834. if (mod_list[0] > (param_sz - 1)) {
  835. pr_err("%s:max modules exp/ret [%d: %d]\n",
  836. __func__, (param_sz - 1),
  837. mod_list[0]);
  838. rc = -EINVAL;
  839. goto end;
  840. }
  841. /*
  842. * get ramp parameters
  843. * check for change in ramp parameters
  844. * update ramp wait
  845. */
  846. msm_ds2_dap_check_and_update_ramp_wait(port_id,
  847. copp_idx,
  848. &ramp_wait);
  849. /* Mute before switching modules */
  850. rc = adm_set_volume(port_id, copp_idx,
  851. VOLUME_ZERO_GAIN);
  852. if (rc < 0) {
  853. /*
  854. * Not Fatal can continue bypass operations.
  855. * Do not need to block playback
  856. */
  857. pr_info("%s :Set volume port_id %d",
  858. __func__, port_id);
  859. pr_info("copp_idx %d, error %d\n",
  860. copp_idx, rc);
  861. }
  862. rc = msm_ds2_dap_handle_bypass_wait(port_id, copp_idx,
  863. (ramp_wait +
  864. DOLBY_ADDITIONAL_RAMP_WAIT));
  865. if (rc == -EINTR) {
  866. pr_info("%s:bypass interrupted-ignore,port %d",
  867. __func__, port_id);
  868. pr_info("copp_idx %d\n", copp_idx);
  869. rc = 0;
  870. continue;
  871. }
  872. /* if dap bypass is set */
  873. if (ds2_dap_params_states.dap_bypass) {
  874. /* Turn off dap module */
  875. mod_inst_info.module_id = DS2_MODULE_ID;
  876. mod_inst_info.instance_id = INSTANCE_ID_0;
  877. adm_param_enable_v2(port_id, copp_idx,
  878. mod_inst_info,
  879. MODULE_DISABLE);
  880. /*
  881. * If custom stereo is on at the time of bypass,
  882. * switch off custom stereo on dap and turn on
  883. * custom stereo on qti channel mixer.
  884. */
  885. if (cs_onoff) {
  886. rc = dap_set_custom_stereo_onoff(i,
  887. !cs_onoff);
  888. if (rc < 0) {
  889. pr_info("%s:D_CS i %d,rc %d\n",
  890. __func__, i, rc);
  891. }
  892. rc = qti_set_custom_stereo_on(port_id,
  893. copp_idx,
  894. cs_onoff);
  895. if (rc < 0) {
  896. pr_info("%s:Q_CS port id 0x%x",
  897. __func__, port_id);
  898. pr_info("copp idx %d, rc %d\n",
  899. copp_idx, rc);
  900. }
  901. }
  902. mod_inst_info_sz =
  903. sizeof(struct module_instance_info) /
  904. sizeof(uint32_t);
  905. /* Turn on qti modules */
  906. for (j = 1; j < mod_list[0] * mod_inst_info_sz;
  907. j += mod_inst_info_sz) {
  908. if (!msm_ds2_dap_can_enable_module(
  909. mod_list[j]) ||
  910. mod_list[j] ==
  911. DS2_MODULE_ID)
  912. continue;
  913. pr_debug("%s: param enable %d\n",
  914. __func__, mod_list[j]);
  915. memcpy(&mod_inst_info, &mod_list[j],
  916. sizeof(mod_inst_info));
  917. adm_param_enable_v2(port_id, copp_idx,
  918. mod_inst_info,
  919. MODULE_ENABLE);
  920. }
  921. /* Add adm api to resend calibration on port */
  922. rc = msm_ds2_dap_send_cal_data(i);
  923. if (rc < 0) {
  924. /*
  925. * Not fatal,continue bypass operations.
  926. * Do not need to block playback
  927. */
  928. pr_info("%s:send cal err %d index %d\n",
  929. __func__, rc, i);
  930. }
  931. } else {
  932. /* Turn off qti modules */
  933. for (j = 1; j < mod_list[0] * mod_inst_info_sz;
  934. j += mod_inst_info_sz) {
  935. if (!msm_ds2_dap_can_enable_module(
  936. mod_list[j]) ||
  937. mod_list[j] ==
  938. DS2_MODULE_ID)
  939. continue;
  940. pr_debug("%s: param disable %d\n",
  941. __func__, mod_list[j]);
  942. memcpy(&mod_inst_info, &mod_list[j],
  943. sizeof(mod_inst_info));
  944. adm_param_enable_v2(port_id, copp_idx,
  945. mod_inst_info,
  946. MODULE_DISABLE);
  947. }
  948. /* Enable DAP modules */
  949. pr_debug("%s:DS2 param enable\n", __func__);
  950. mod_inst_info.module_id = DS2_MODULE_ID;
  951. mod_inst_info.instance_id = INSTANCE_ID_0;
  952. adm_param_enable_v2(port_id, copp_idx,
  953. mod_inst_info,
  954. MODULE_ENABLE);
  955. /*
  956. * If custom stereo is on at the time of dap on,
  957. * switch off custom stereo on qti channel mixer
  958. * and turn on custom stereo on DAP.
  959. * mixer(qti).
  960. */
  961. if (cs_onoff) {
  962. rc = qti_set_custom_stereo_on(port_id,
  963. copp_idx,
  964. !cs_onoff);
  965. if (rc < 0) {
  966. pr_info("%s:Q_CS port_id 0x%x",
  967. __func__, port_id);
  968. pr_info("copp_idx %d rc %d\n",
  969. copp_idx, rc);
  970. }
  971. rc = dap_set_custom_stereo_onoff(i,
  972. cs_onoff);
  973. if (rc < 0) {
  974. pr_info("%s:D_CS i %d,rc %d\n",
  975. __func__, i, rc);
  976. }
  977. }
  978. }
  979. rc = msm_ds2_dap_handle_bypass_wait(port_id, copp_idx,
  980. DOLBY_MODULE_ENABLE_PERIOD);
  981. if (rc == -EINTR) {
  982. pr_info("%s:bypass interrupted port_id %d copp_idx %d\n",
  983. __func__, port_id, copp_idx);
  984. /* Interrupted ignore bypass */
  985. rc = 0;
  986. continue;
  987. }
  988. /* set volume to unity gain after module on/off */
  989. rc = adm_set_volume(port_id, copp_idx,
  990. VOLUME_UNITY_GAIN);
  991. if (rc < 0) {
  992. /*
  993. * Not Fatal can continue bypass operations.
  994. * Do not need to block playback
  995. */
  996. pr_info("%s: Set vol port %d copp %d, rc %d\n",
  997. __func__, port_id, copp_idx, rc);
  998. rc = 0;
  999. }
  1000. }
  1001. }
  1002. end:
  1003. kfree(mod_list);
  1004. pr_debug("%s:return rc=%d\n", __func__, rc);
  1005. return rc;
  1006. }
  1007. static int msm_ds2_dap_send_end_point(int dev_map_idx, int endp_idx)
  1008. {
  1009. uint32_t offset = 0;
  1010. struct param_hdr_v3 param_hdr;
  1011. int cache_device = 0;
  1012. struct ds2_dap_params_s *ds2_ap_params_obj = NULL;
  1013. int32_t *modified_param = NULL;
  1014. int rc = 0;
  1015. if (dev_map_idx < 0 || dev_map_idx >= DS2_DEVICES_ALL) {
  1016. pr_err("%s: invalid dev map index %d\n", __func__, dev_map_idx);
  1017. rc = -EINVAL;
  1018. goto end;
  1019. }
  1020. memset(&param_hdr, 0, sizeof(param_hdr));
  1021. cache_device = dev_map[dev_map_idx].cache_dev;
  1022. ds2_ap_params_obj = &ds2_dap_params[cache_device];
  1023. pr_debug("%s: cache dev %d, dev_map_idx %d\n", __func__,
  1024. cache_device, dev_map_idx);
  1025. pr_debug("%s: endp - %pK %pK\n", __func__,
  1026. &ds2_dap_params[cache_device], ds2_ap_params_obj);
  1027. if (dev_map[dev_map_idx].port_id == DOLBY_INVALID_PORT_ID) {
  1028. pr_err("%s: invalid port\n", __func__);
  1029. rc = -EINVAL;
  1030. goto end;
  1031. }
  1032. if ((dev_map[dev_map_idx].copp_idx < 0) ||
  1033. (dev_map[dev_map_idx].copp_idx >= MAX_COPPS_PER_PORT)) {
  1034. pr_err("%s: Invalid copp_idx\n", __func__);
  1035. rc = -EINVAL;
  1036. goto end;
  1037. }
  1038. param_hdr.module_id = DOLBY_BUNDLE_MODULE_ID;
  1039. param_hdr.instance_id = INSTANCE_ID_0;
  1040. param_hdr.param_id = DOLBY_PARAM_ID_INIT_ENDP;
  1041. param_hdr.param_size = sizeof(offset);
  1042. offset = ds2_ap_params_obj->params_val[ds2_dap_params_offset[endp_idx]];
  1043. pr_debug("%s: off %d, length %d\n", __func__,
  1044. ds2_dap_params_offset[endp_idx],
  1045. ds2_dap_params_length[endp_idx]);
  1046. pr_debug("%s: param 0x%x, param val %d\n", __func__,
  1047. ds2_dap_params_id[endp_idx], ds2_ap_params_obj->
  1048. params_val[ds2_dap_params_offset[endp_idx]]);
  1049. rc = adm_pack_and_set_one_pp_param(dev_map[dev_map_idx].port_id,
  1050. dev_map[dev_map_idx].copp_idx,
  1051. param_hdr, (u8 *) &offset);
  1052. if (rc) {
  1053. pr_err("%s: send dolby params failed rc %d\n", __func__, rc);
  1054. rc = -EINVAL;
  1055. }
  1056. modified_param = ds2_ap_params_obj->dap_params_modified;
  1057. if (modified_param == NULL) {
  1058. pr_err("%s: modified param structure invalid\n",
  1059. __func__);
  1060. rc = -EINVAL;
  1061. goto end;
  1062. }
  1063. if (msm_ds2_dap_check_is_param_modified(modified_param, endp_idx, 0))
  1064. ds2_ap_params_obj->dap_params_modified[endp_idx] = 0x00010001;
  1065. end:
  1066. return rc;
  1067. }
  1068. static int msm_ds2_dap_send_cached_params(int dev_map_idx,
  1069. int commit)
  1070. {
  1071. uint8_t *packed_params = NULL;
  1072. uint32_t packed_params_size = 0;
  1073. uint32_t param_size = 0;
  1074. struct param_hdr_v3 param_hdr;
  1075. uint32_t idx, i, ret = 0;
  1076. int cache_device = 0;
  1077. struct ds2_dap_params_s *ds2_ap_params_obj = NULL;
  1078. int32_t *modified_param = NULL;
  1079. if (dev_map_idx < 0 || dev_map_idx >= DS2_DEVICES_ALL) {
  1080. pr_err("%s: invalid dev map index %d\n", __func__, dev_map_idx);
  1081. ret = -EINVAL;
  1082. goto end;
  1083. }
  1084. memset(&param_hdr, 0, sizeof(param_hdr));
  1085. cache_device = dev_map[dev_map_idx].cache_dev;
  1086. /* Use off profile cache in only for soft bypass */
  1087. if (ds2_dap_params_states.dap_bypass_type == DAP_SOFT_BYPASS &&
  1088. ds2_dap_params_states.dap_bypass == true) {
  1089. pr_debug("%s: use bypass cache 0\n", __func__);
  1090. cache_device = dev_map[0].cache_dev;
  1091. }
  1092. ds2_ap_params_obj = &ds2_dap_params[cache_device];
  1093. pr_debug("%s: cached param - %pK %pK, cache_device %d\n", __func__,
  1094. &ds2_dap_params[cache_device], ds2_ap_params_obj,
  1095. cache_device);
  1096. /*
  1097. * Allocate the max space needed. This is enough space to hold the
  1098. * header for each param plus the total size of all the params.
  1099. */
  1100. packed_params_size = (sizeof(param_hdr) * (MAX_DS2_PARAMS - 1)) +
  1101. (TOTAL_LENGTH_DOLBY_PARAM * sizeof(uint32_t));
  1102. packed_params = kzalloc(packed_params_size, GFP_KERNEL);
  1103. if (!packed_params)
  1104. return -ENOMEM;
  1105. if (dev_map[dev_map_idx].port_id == DOLBY_INVALID_PORT_ID) {
  1106. pr_err("%s: invalid port id\n", __func__);
  1107. ret = -EINVAL;
  1108. goto end;
  1109. }
  1110. if ((dev_map[dev_map_idx].copp_idx < 0) ||
  1111. (dev_map[dev_map_idx].copp_idx >= MAX_COPPS_PER_PORT)) {
  1112. pr_err("%s: Invalid copp_idx\n", __func__);
  1113. ret = -EINVAL;
  1114. goto end;
  1115. }
  1116. packed_params_size = 0;
  1117. for (i = 0; i < (MAX_DS2_PARAMS-1); i++) {
  1118. /*get the pointer to the param modified array in the cache*/
  1119. modified_param = ds2_ap_params_obj->dap_params_modified;
  1120. if (modified_param == NULL) {
  1121. pr_err("%s: modified param structure invalid\n",
  1122. __func__);
  1123. ret = -EINVAL;
  1124. goto end;
  1125. }
  1126. if (!msm_ds2_dap_check_is_param_modified(modified_param, i,
  1127. commit))
  1128. continue;
  1129. param_hdr.module_id = DOLBY_BUNDLE_MODULE_ID;
  1130. param_hdr.instance_id = INSTANCE_ID_0;
  1131. param_hdr.param_id = ds2_dap_params_id[i];
  1132. param_hdr.param_size =
  1133. ds2_dap_params_length[i] * sizeof(uint32_t);
  1134. idx = ds2_dap_params_offset[i];
  1135. ret = q6common_pack_pp_params(
  1136. packed_params + packed_params_size, &param_hdr,
  1137. (u8 *) &ds2_ap_params_obj->params_val[idx],
  1138. &param_size);
  1139. if (ret) {
  1140. pr_err("%s: Failed to pack params, error %d\n",
  1141. __func__, ret);
  1142. goto end;
  1143. }
  1144. packed_params_size += param_size;
  1145. }
  1146. pr_debug("%s: total packed param length: %d\n", __func__,
  1147. packed_params_size);
  1148. if (packed_params_size) {
  1149. ret = adm_set_pp_params(dev_map[dev_map_idx].port_id,
  1150. dev_map[dev_map_idx].copp_idx, NULL,
  1151. packed_params, packed_params_size);
  1152. if (ret) {
  1153. pr_err("%s: send dolby params failed ret %d\n",
  1154. __func__, ret);
  1155. ret = -EINVAL;
  1156. goto end;
  1157. }
  1158. for (i = 0; i < MAX_DS2_PARAMS-1; i++) {
  1159. /*get pointer to the param modified array in the cache*/
  1160. modified_param = ds2_ap_params_obj->dap_params_modified;
  1161. if (modified_param == NULL) {
  1162. pr_err("%s: modified param struct invalid\n",
  1163. __func__);
  1164. ret = -EINVAL;
  1165. goto end;
  1166. }
  1167. if (!msm_ds2_dap_check_is_param_modified(
  1168. modified_param, i, commit))
  1169. continue;
  1170. ds2_ap_params_obj->dap_params_modified[i] = 0x00010001;
  1171. }
  1172. }
  1173. end:
  1174. kfree(packed_params);
  1175. return ret;
  1176. }
  1177. static int msm_ds2_dap_commit_params(struct dolby_param_data *dolby_data,
  1178. int commit)
  1179. {
  1180. int ret = 0, i, idx;
  1181. struct ds2_dap_params_s *ds2_ap_params_obj = NULL;
  1182. int32_t *modified_param = NULL;
  1183. /* Do not commit params if in hard bypass */
  1184. if (ds2_dap_params_states.dap_bypass_type == DAP_HARD_BYPASS &&
  1185. ds2_dap_params_states.dap_bypass == true) {
  1186. pr_debug("%s: called in bypass", __func__);
  1187. ret = -EINVAL;
  1188. goto end;
  1189. }
  1190. for (idx = 0; idx < MAX_DS2_PARAMS; idx++) {
  1191. if (ds2_dap_params_id[idx] == DOLBY_PARAM_ID_INIT_ENDP)
  1192. break;
  1193. }
  1194. if (idx >= MAX_DS2_PARAMS || idx < 0) {
  1195. pr_err("%s: index of DS2 Param not found idx %d\n",
  1196. __func__, idx);
  1197. ret = -EINVAL;
  1198. goto end;
  1199. }
  1200. pr_debug("%s: found endp - idx %d 0x%x\n", __func__, idx,
  1201. ds2_dap_params_id[idx]);
  1202. for (i = 0; i < DS2_DEVICES_ALL; i++) {
  1203. pr_debug("%s:dev[0x%x,0x%x],i:%d,active:%d,bypass:%d,type:%d\n",
  1204. __func__, dolby_data->device_id, dev_map[i].device_id,
  1205. i, dev_map[i].active, ds2_dap_params_states.dap_bypass,
  1206. ds2_dap_params_states.dap_bypass_type);
  1207. if (((dev_map[i].device_id & ds2_dap_params_states.device) ||
  1208. ((ds2_dap_params_states.dap_bypass_type ==
  1209. DAP_SOFT_BYPASS) &&
  1210. (ds2_dap_params_states.dap_bypass == true))) &&
  1211. (dev_map[i].active == true)) {
  1212. /*get ptr to the cache storing the params for device*/
  1213. if ((ds2_dap_params_states.dap_bypass_type ==
  1214. DAP_SOFT_BYPASS) &&
  1215. (ds2_dap_params_states.dap_bypass == true))
  1216. ds2_ap_params_obj =
  1217. &ds2_dap_params[dev_map[0].cache_dev];
  1218. else
  1219. ds2_ap_params_obj =
  1220. &ds2_dap_params[dev_map[i].cache_dev];
  1221. /*get the pointer to the param modified array in cache*/
  1222. modified_param = ds2_ap_params_obj->dap_params_modified;
  1223. if (modified_param == NULL) {
  1224. pr_err("%s: modified_param NULL\n", __func__);
  1225. ret = -EINVAL;
  1226. goto end;
  1227. }
  1228. /*
  1229. * Send the endp param if use cache is set
  1230. * or if param is modified
  1231. */
  1232. if (!commit || msm_ds2_dap_check_is_param_modified(
  1233. modified_param, idx, commit)) {
  1234. msm_ds2_dap_send_end_point(i, idx);
  1235. commit = 0;
  1236. }
  1237. ret = msm_ds2_dap_send_cached_params(i, commit);
  1238. if (ret < 0) {
  1239. pr_err("%s: send cached param %d\n",
  1240. __func__, ret);
  1241. goto end;
  1242. }
  1243. }
  1244. }
  1245. end:
  1246. return ret;
  1247. }
  1248. static int msm_ds2_dap_handle_commands(u32 cmd, void *arg)
  1249. {
  1250. int ret = 0, port_id = 0;
  1251. int32_t data;
  1252. struct dolby_param_data *dolby_data = (struct dolby_param_data *)arg;
  1253. if (get_user(data, &dolby_data->data[0])) {
  1254. pr_debug("%s error getting data\n", __func__);
  1255. ret = -EFAULT;
  1256. goto end;
  1257. }
  1258. pr_debug("%s: param_id %d,be_id %d,device_id 0x%x,length %d,data %d\n",
  1259. __func__, dolby_data->param_id, dolby_data->be_id,
  1260. dolby_data->device_id, dolby_data->length, data);
  1261. switch (dolby_data->param_id) {
  1262. case DAP_CMD_COMMIT_ALL:
  1263. msm_ds2_dap_commit_params(dolby_data, 0);
  1264. break;
  1265. case DAP_CMD_COMMIT_CHANGED:
  1266. msm_ds2_dap_commit_params(dolby_data, 1);
  1267. break;
  1268. case DAP_CMD_USE_CACHE_FOR_INIT:
  1269. ds2_dap_params_states.use_cache = data;
  1270. break;
  1271. case DAP_CMD_SET_BYPASS:
  1272. pr_debug("%s: bypass %d bypass type %d, data %d\n", __func__,
  1273. ds2_dap_params_states.dap_bypass,
  1274. ds2_dap_params_states.dap_bypass_type,
  1275. data);
  1276. /* Do not perform bypass operation if bypass state is same*/
  1277. if (ds2_dap_params_states.dap_bypass == data)
  1278. break;
  1279. ds2_dap_params_states.dap_bypass = data;
  1280. /* hard bypass */
  1281. if (ds2_dap_params_states.dap_bypass_type == DAP_HARD_BYPASS)
  1282. msm_ds2_dap_handle_bypass(dolby_data);
  1283. /* soft bypass */
  1284. msm_ds2_dap_commit_params(dolby_data, 0);
  1285. break;
  1286. case DAP_CMD_SET_BYPASS_TYPE:
  1287. if (data == true)
  1288. ds2_dap_params_states.dap_bypass_type =
  1289. DAP_HARD_BYPASS;
  1290. else
  1291. ds2_dap_params_states.dap_bypass_type =
  1292. DAP_SOFT_BYPASS;
  1293. pr_debug("%s: bypass type %d", __func__,
  1294. ds2_dap_params_states.dap_bypass_type);
  1295. break;
  1296. case DAP_CMD_SET_ACTIVE_DEVICE:
  1297. pr_debug("%s: DAP_CMD_SET_ACTIVE_DEVICE length %d\n",
  1298. __func__, dolby_data->length);
  1299. /* TODO: need to handle multiple instance*/
  1300. ds2_dap_params_states.device |= dolby_data->device_id;
  1301. port_id = msm_ds2_dap_get_port_id(
  1302. dolby_data->device_id,
  1303. dolby_data->be_id);
  1304. pr_debug("%s: device id 0x%x all_dev 0x%x port_id %d\n",
  1305. __func__, dolby_data->device_id,
  1306. ds2_dap_params_states.device, port_id);
  1307. msm_ds2_dap_update_dev_map_port_id(dolby_data->device_id,
  1308. port_id);
  1309. if (port_id == DOLBY_INVALID_PORT_ID) {
  1310. pr_err("%s: invalid port id %d\n", __func__, port_id);
  1311. ret = -EINVAL;
  1312. goto end;
  1313. }
  1314. break;
  1315. }
  1316. end:
  1317. return ret;
  1318. }
  1319. static int msm_ds2_dap_set_param(u32 cmd, void *arg)
  1320. {
  1321. int rc = 0, idx, i, j, off, port_id = 0, cdev = 0;
  1322. int32_t num_device = 0;
  1323. int32_t data = 0;
  1324. int32_t dev_arr[DS2_DSP_SUPPORTED_ENDP_DEVICE] = {0};
  1325. struct dolby_param_data *dolby_data = (struct dolby_param_data *)arg;
  1326. rc = msm_ds2_dap_update_num_devices(dolby_data, &num_device, dev_arr,
  1327. DS2_DSP_SUPPORTED_ENDP_DEVICE);
  1328. if (num_device == 0 || rc < 0) {
  1329. pr_err("%s: num devices 0\n", __func__);
  1330. rc = -EINVAL;
  1331. goto end;
  1332. }
  1333. for (i = 0; i < num_device; i++) {
  1334. port_id = msm_ds2_dap_get_port_id(dev_arr[i],
  1335. dolby_data->be_id);
  1336. if (port_id != DOLBY_INVALID_PORT_ID)
  1337. msm_ds2_dap_update_dev_map_port_id(dev_arr[i], port_id);
  1338. cdev = msm_ds2_dap_map_device_to_dolby_cache_devices(
  1339. dev_arr[i]);
  1340. if (cdev < 0 || cdev >= DOLBY_MAX_CACHE) {
  1341. pr_err("%s: Invalid cache device %d for device 0x%x\n",
  1342. __func__, cdev, dev_arr[i]);
  1343. rc = -EINVAL;
  1344. goto end;
  1345. }
  1346. pr_debug("%s:port:%d,be:%d,dev:0x%x,cdev:%d,param:0x%x,len:%d\n"
  1347. , __func__, port_id, dolby_data->be_id, dev_arr[i],
  1348. cdev, dolby_data->param_id, dolby_data->length);
  1349. for (idx = 0; idx < MAX_DS2_PARAMS; idx++) {
  1350. /*paramid from user space*/
  1351. if (dolby_data->param_id == ds2_dap_params_id[idx])
  1352. break;
  1353. }
  1354. if (idx > MAX_DS2_PARAMS-1) {
  1355. pr_err("%s: invalid param id 0x%x at idx %d\n",
  1356. __func__, dolby_data->param_id, idx);
  1357. rc = -EINVAL;
  1358. goto end;
  1359. }
  1360. off = ds2_dap_params_offset[idx];
  1361. if ((dolby_data->length <= 0) ||
  1362. (dolby_data->length > TOTAL_LENGTH_DS2_PARAM - off)) {
  1363. pr_err("%s: invalid length %d at idx %d\n",
  1364. __func__, dolby_data->length, idx);
  1365. rc = -EINVAL;
  1366. goto end;
  1367. }
  1368. /* cache the parameters */
  1369. ds2_dap_params[cdev].dap_params_modified[idx] += 1;
  1370. for (j = 0; j < dolby_data->length; j++) {
  1371. if (get_user(data, &dolby_data->data[j])) {
  1372. pr_debug("%s:error getting data\n", __func__);
  1373. rc = -EFAULT;
  1374. goto end;
  1375. }
  1376. ds2_dap_params[cdev].params_val[off + j] = data;
  1377. pr_debug("%s:off %d,val[i/p:o/p]-[%d / %d]\n",
  1378. __func__, off, data,
  1379. ds2_dap_params[cdev].
  1380. params_val[off + j]);
  1381. }
  1382. }
  1383. end:
  1384. return rc;
  1385. }
  1386. static int msm_ds2_dap_get_param(u32 cmd, void *arg)
  1387. {
  1388. int rc = 0, i, port_id = 0, copp_idx = -1;
  1389. struct dolby_param_data *dolby_data = (struct dolby_param_data *)arg;
  1390. int32_t *params_value = NULL;
  1391. uint32_t params_length = DOLBY_MAX_LENGTH_INDIVIDUAL_PARAM *
  1392. sizeof(uint32_t);
  1393. uint32_t param_payload_len =
  1394. DOLBY_PARAM_PAYLOAD_SIZE * sizeof(uint32_t);
  1395. struct param_hdr_v3 param_hdr;
  1396. /* Return error on get param in soft or hard bypass */
  1397. if (ds2_dap_params_states.dap_bypass == true) {
  1398. pr_err("%s: called in bypass_type %d bypass %d\n", __func__,
  1399. ds2_dap_params_states.dap_bypass_type,
  1400. ds2_dap_params_states.dap_bypass);
  1401. rc = -EINVAL;
  1402. goto end;
  1403. }
  1404. /* Return if invalid length */
  1405. if ((dolby_data->length >
  1406. (DOLBY_MAX_LENGTH_INDIVIDUAL_PARAM - DOLBY_PARAM_PAYLOAD_SIZE)) ||
  1407. (dolby_data->length <= 0)) {
  1408. pr_err("Invalid length %d", dolby_data->length);
  1409. rc = -EINVAL;
  1410. goto end;
  1411. }
  1412. for (i = 0; i < DS2_DEVICES_ALL; i++) {
  1413. if ((dev_map[i].active) &&
  1414. (dev_map[i].device_id & dolby_data->device_id)) {
  1415. port_id = dev_map[i].port_id;
  1416. copp_idx = dev_map[i].copp_idx;
  1417. break;
  1418. }
  1419. }
  1420. if (port_id == DOLBY_INVALID_PORT_ID) {
  1421. pr_err("%s: Invalid port\n", __func__);
  1422. rc = -EINVAL;
  1423. goto end;
  1424. }
  1425. if ((copp_idx < 0) || (copp_idx >= MAX_COPPS_PER_PORT)) {
  1426. pr_err("%s: Invalid copp_idx\n", __func__);
  1427. rc = -EINVAL;
  1428. goto end;
  1429. }
  1430. pr_debug("%s: port_id 0x%x, copp_idx %d, dev_map[i].device_id %x\n",
  1431. __func__, port_id, copp_idx, dev_map[i].device_id);
  1432. params_value = kzalloc(params_length + param_payload_len,
  1433. GFP_KERNEL);
  1434. if (!params_value)
  1435. return -ENOMEM;
  1436. memset(&param_hdr, 0, sizeof(param_hdr));
  1437. if (dolby_data->param_id == DOLBY_PARAM_ID_VER) {
  1438. param_hdr.module_id = DOLBY_BUNDLE_MODULE_ID;
  1439. param_hdr.instance_id = INSTANCE_ID_0;
  1440. param_hdr.param_id = DOLBY_PARAM_ID_VER;
  1441. param_hdr.param_size = params_length + param_payload_len;
  1442. } else {
  1443. for (i = 0; i < MAX_DS2_PARAMS; i++)
  1444. if (ds2_dap_params_id[i] ==
  1445. dolby_data->param_id)
  1446. break;
  1447. if (i > MAX_DS2_PARAMS-1) {
  1448. pr_err("%s: invalid param id 0x%x at id %d\n", __func__,
  1449. dolby_data->param_id, i);
  1450. rc = -EINVAL;
  1451. goto end;
  1452. } else {
  1453. params_length =
  1454. ds2_dap_params_length[i] * sizeof(uint32_t);
  1455. param_hdr.module_id = DOLBY_BUNDLE_MODULE_ID;
  1456. param_hdr.instance_id = INSTANCE_ID_0;
  1457. param_hdr.param_id = ds2_dap_params_id[i];
  1458. param_hdr.param_size =
  1459. params_length + param_payload_len;
  1460. }
  1461. }
  1462. rc = adm_get_pp_params(port_id, copp_idx, ADM_CLIENT_ID_DEFAULT, NULL,
  1463. &param_hdr, (u8 *) params_value);
  1464. if (rc) {
  1465. pr_err("%s: get parameters failed rc %d\n", __func__, rc);
  1466. rc = -EINVAL;
  1467. goto end;
  1468. }
  1469. if (copy_to_user((void __user *) dolby_data->data,
  1470. &params_value[DOLBY_PARAM_PAYLOAD_SIZE],
  1471. (dolby_data->length * sizeof(uint32_t)))) {
  1472. pr_err("%s: error getting param\n", __func__);
  1473. rc = -EFAULT;
  1474. goto end;
  1475. }
  1476. end:
  1477. kfree(params_value);
  1478. return rc;
  1479. }
  1480. static int msm_ds2_dap_param_visualizer_control_get(u32 cmd, void *arg)
  1481. {
  1482. int32_t *visualizer_data = NULL;
  1483. int i = 0, ret = 0, port_id = -1, cache_dev = -1, copp_idx = -1;
  1484. int32_t *update_visualizer_data = NULL;
  1485. struct dolby_param_data *dolby_data = (struct dolby_param_data *)arg;
  1486. uint32_t offset, length, params_length;
  1487. uint32_t param_payload_len =
  1488. DOLBY_PARAM_PAYLOAD_SIZE * sizeof(uint32_t);
  1489. struct param_hdr_v3 param_hdr;
  1490. for (i = 0; i < DS2_DEVICES_ALL; i++) {
  1491. if ((dev_map[i].active)) {
  1492. port_id = dev_map[i].port_id;
  1493. cache_dev = dev_map[i].cache_dev;
  1494. copp_idx = dev_map[i].copp_idx;
  1495. break;
  1496. }
  1497. }
  1498. if (port_id == DOLBY_INVALID_PORT_ID ||
  1499. (copp_idx < 0) || (copp_idx >= MAX_COPPS_PER_PORT)) {
  1500. ret = 0;
  1501. dolby_data->length = 0;
  1502. pr_err("%s: no device active\n", __func__);
  1503. goto end;
  1504. }
  1505. memset(&param_hdr, 0, sizeof(param_hdr));
  1506. length = ds2_dap_params[cache_dev].params_val[DOLBY_PARAM_VCNB_OFFSET];
  1507. if (length > DOLBY_PARAM_VCNB_MAX_LENGTH || length <= 0) {
  1508. ret = 0;
  1509. dolby_data->length = 0;
  1510. pr_err("%s Incorrect VCNB length", __func__);
  1511. return -EINVAL;
  1512. }
  1513. params_length = (2*length + DOLBY_VIS_PARAM_HEADER_SIZE) *
  1514. sizeof(uint32_t);
  1515. visualizer_data = kzalloc(params_length, GFP_KERNEL);
  1516. if (!visualizer_data) {
  1517. pr_err("%s: params memory alloc failed\n", __func__);
  1518. ret = -ENOMEM;
  1519. dolby_data->length = 0;
  1520. goto end;
  1521. }
  1522. memset(visualizer_data, 0x0, params_length);
  1523. /* Return error on get param in soft or hard bypass */
  1524. if (ds2_dap_params_states.dap_bypass == true) {
  1525. pr_debug("%s: visualizer called in bypass, return 0\n",
  1526. __func__);
  1527. ret = 0;
  1528. dolby_data->length = 0;
  1529. goto end;
  1530. }
  1531. offset = 0;
  1532. params_length = length * sizeof(uint32_t);
  1533. param_hdr.module_id = DOLBY_BUNDLE_MODULE_ID;
  1534. param_hdr.instance_id = INSTANCE_ID_0;
  1535. param_hdr.param_id = DOLBY_PARAM_ID_VCBG;
  1536. param_hdr.param_size = length * sizeof(uint32_t) + param_payload_len;
  1537. ret = adm_get_pp_params(port_id, copp_idx, ADM_CLIENT_ID_DEFAULT, NULL,
  1538. &param_hdr,
  1539. (((char *) (visualizer_data)) + offset));
  1540. if (ret) {
  1541. pr_err("%s: get parameters failed ret %d\n", __func__, ret);
  1542. ret = -EINVAL;
  1543. dolby_data->length = 0;
  1544. goto end;
  1545. }
  1546. offset = length * sizeof(uint32_t);
  1547. param_hdr.module_id = DOLBY_BUNDLE_MODULE_ID;
  1548. param_hdr.instance_id = INSTANCE_ID_0;
  1549. param_hdr.param_id = DOLBY_PARAM_ID_VCBE;
  1550. param_hdr.param_size = length * sizeof(uint32_t) + param_payload_len;
  1551. ret = adm_get_pp_params(port_id, copp_idx, ADM_CLIENT_ID_DEFAULT, NULL,
  1552. &param_hdr,
  1553. (((char *) (visualizer_data)) + offset));
  1554. if (ret) {
  1555. pr_err("%s: get parameters failed ret %d\n", __func__, ret);
  1556. ret = -EINVAL;
  1557. dolby_data->length = 0;
  1558. goto end;
  1559. }
  1560. update_visualizer_data = visualizer_data;
  1561. dolby_data->length = 2 * length;
  1562. if (copy_to_user((void *)dolby_data->data,
  1563. (void *)update_visualizer_data,
  1564. (dolby_data->length * sizeof(uint32_t)))) {
  1565. pr_err("%s: copy to user failed for data\n", __func__);
  1566. dolby_data->length = 0;
  1567. ret = -EFAULT;
  1568. goto end;
  1569. }
  1570. end:
  1571. kfree(visualizer_data);
  1572. return ret;
  1573. }
  1574. int msm_ds2_dap_set_security_control(u32 cmd, void *arg)
  1575. {
  1576. struct dolby_param_license *dolby_license =
  1577. ((struct dolby_param_license *)arg);
  1578. pr_debug("%s: dmid %d license key %d\n", __func__,
  1579. dolby_license->dmid, dolby_license->license_key);
  1580. core_set_dolby_manufacturer_id(dolby_license->dmid);
  1581. core_set_license(dolby_license->license_key, DOLBY_DS1_LICENSE_ID);
  1582. return 0;
  1583. }
  1584. int msm_ds2_dap_update_port_parameters(struct snd_hwdep *hw, struct file *file,
  1585. bool open)
  1586. {
  1587. int i = 0, dev_id = 0;
  1588. pr_debug("%s: open %d\n", __func__, open);
  1589. ds2_dap_params_states.node_opened = open;
  1590. ds2_dap_params_states.dap_bypass = true;
  1591. ds2_dap_params_states.dap_bypass_type = 0;
  1592. ds2_dap_params_states.use_cache = 0;
  1593. ds2_dap_params_states.device = 0;
  1594. ds2_dap_params_states.custom_stereo_onoff = 0;
  1595. for (i = 0; i < DS2_DEVICES_ALL; i++) {
  1596. if (i == 0)
  1597. dev_map[i].device_id = 0;
  1598. else {
  1599. dev_id = (1 << (i-1));
  1600. if (all_supported_devices & dev_id)
  1601. dev_map[i].device_id = dev_id;
  1602. else
  1603. continue;
  1604. }
  1605. dev_map[i].cache_dev =
  1606. msm_ds2_dap_map_device_to_dolby_cache_devices(
  1607. dev_map[i].device_id);
  1608. if (dev_map[i].cache_dev < 0 ||
  1609. dev_map[i].cache_dev >= DOLBY_MAX_CACHE)
  1610. pr_err("%s: Invalid cache device %d for device 0x%x\n",
  1611. __func__,
  1612. dev_map[i].cache_dev,
  1613. dev_map[i].device_id);
  1614. dev_map[i].port_id = -1;
  1615. dev_map[i].active = false;
  1616. dev_map[i].stream_ref_count = 0;
  1617. dev_map[i].cal_data = NULL;
  1618. dev_map[i].copp_idx = -1;
  1619. pr_debug("%s: device_id 0x%x, cache_dev %d act %d\n", __func__,
  1620. dev_map[i].device_id, dev_map[i].cache_dev,
  1621. dev_map[i].active);
  1622. }
  1623. return 0;
  1624. }
  1625. int msm_ds2_dap_ioctl_shared(struct snd_hwdep *hw, struct file *file,
  1626. u32 cmd, void *arg)
  1627. {
  1628. int ret = 0;
  1629. pr_debug("%s: cmd: 0x%x\n", __func__, cmd);
  1630. switch (cmd) {
  1631. case SNDRV_DEVDEP_DAP_IOCTL_SET_PARAM:
  1632. ret = msm_ds2_dap_set_param(cmd, arg);
  1633. break;
  1634. case SNDRV_DEVDEP_DAP_IOCTL_GET_PARAM:
  1635. ret = msm_ds2_dap_get_param(cmd, arg);
  1636. break;
  1637. case SNDRV_DEVDEP_DAP_IOCTL_DAP_COMMAND:
  1638. ret = msm_ds2_dap_handle_commands(cmd, arg);
  1639. break;
  1640. case SNDRV_DEVDEP_DAP_IOCTL_DAP_LICENSE:
  1641. ret = msm_ds2_dap_set_security_control(cmd, arg);
  1642. break;
  1643. case SNDRV_DEVDEP_DAP_IOCTL_GET_VISUALIZER:
  1644. ret = msm_ds2_dap_param_visualizer_control_get(cmd, arg);
  1645. break;
  1646. default:
  1647. pr_err("%s: called with invalid control 0x%x\n", __func__, cmd);
  1648. ret = -EINVAL;
  1649. }
  1650. return ret;
  1651. }
  1652. int msm_ds2_dap_ioctl(struct snd_hwdep *hw, struct file *file,
  1653. u32 cmd, void *arg)
  1654. {
  1655. int ret = 0;
  1656. pr_debug("%s: cmd: 0x%x\n", __func__, cmd);
  1657. if (!arg) {
  1658. pr_err("%s: Invalid params event status\n", __func__);
  1659. ret = -EINVAL;
  1660. goto end;
  1661. }
  1662. switch (cmd) {
  1663. case SNDRV_DEVDEP_DAP_IOCTL_SET_PARAM:
  1664. case SNDRV_DEVDEP_DAP_IOCTL_DAP_COMMAND: {
  1665. struct dolby_param_data dolby_data;
  1666. if (copy_from_user((void *)&dolby_data, (void *)arg,
  1667. sizeof(struct dolby_param_data))) {
  1668. pr_err("%s: Copy from user failed\n", __func__);
  1669. ret = -EFAULT;
  1670. goto end;
  1671. }
  1672. ret = msm_ds2_dap_ioctl_shared(hw, file, cmd, &dolby_data);
  1673. break;
  1674. }
  1675. case SNDRV_DEVDEP_DAP_IOCTL_DAP_LICENSE: {
  1676. struct dolby_param_license dolby_license;
  1677. if (copy_from_user((void *)&dolby_license, (void *)arg,
  1678. sizeof(struct dolby_param_license))) {
  1679. pr_err("%s: Copy from user failed\n", __func__);
  1680. ret = -EFAULT;
  1681. goto end;
  1682. }
  1683. ret = msm_ds2_dap_ioctl_shared(hw, file, cmd, &dolby_license);
  1684. break;
  1685. }
  1686. case SNDRV_DEVDEP_DAP_IOCTL_GET_PARAM:
  1687. case SNDRV_DEVDEP_DAP_IOCTL_GET_VISUALIZER: {
  1688. struct dolby_param_data dolby_data;
  1689. if (copy_from_user((void *)&dolby_data, (void *)arg,
  1690. sizeof(struct dolby_param_data))) {
  1691. pr_err("%s: Copy from user failed\n", __func__);
  1692. ret = -EFAULT;
  1693. goto end;
  1694. }
  1695. ret = msm_ds2_dap_ioctl_shared(hw, file, cmd, &dolby_data);
  1696. if (ret < 0)
  1697. pr_err("%s: ioctl cmd %d returned err %d\n",
  1698. __func__, cmd, ret);
  1699. if (copy_to_user((void *)arg, &dolby_data,
  1700. sizeof(struct dolby_param_data))) {
  1701. pr_err("%s: Copy to user failed\n", __func__);
  1702. ret = -EFAULT;
  1703. goto end;
  1704. }
  1705. break;
  1706. }
  1707. default:
  1708. pr_err("%s: called with invalid control 0x%x\n", __func__, cmd);
  1709. ret = -EINVAL;
  1710. }
  1711. end:
  1712. return ret;
  1713. }
  1714. #ifdef CONFIG_COMPAT
  1715. int msm_ds2_dap_compat_ioctl(struct snd_hwdep *hw, struct file *file,
  1716. u32 cmd, void *arg)
  1717. {
  1718. int ret = 0;
  1719. pr_debug("%s: cmd: 0x%x\n", __func__, cmd);
  1720. switch (cmd) {
  1721. case SNDRV_DEVDEP_DAP_IOCTL_SET_PARAM32:
  1722. cmd = SNDRV_DEVDEP_DAP_IOCTL_SET_PARAM;
  1723. goto handle_set_ioctl;
  1724. case SNDRV_DEVDEP_DAP_IOCTL_DAP_COMMAND32:
  1725. cmd = SNDRV_DEVDEP_DAP_IOCTL_DAP_COMMAND;
  1726. handle_set_ioctl:
  1727. {
  1728. struct dolby_param_data32 dolby_data32;
  1729. struct dolby_param_data dolby_data;
  1730. memset(&dolby_data32, 0, sizeof(dolby_data32));
  1731. memset(&dolby_data, 0, sizeof(dolby_data));
  1732. if (copy_from_user(&dolby_data32, (void *)arg,
  1733. sizeof(struct dolby_param_data32))) {
  1734. pr_err("%s: Copy from user failed\n", __func__);
  1735. ret = -EFAULT;
  1736. goto end;
  1737. }
  1738. dolby_data.version = dolby_data32.version;
  1739. dolby_data.device_id = dolby_data32.device_id;
  1740. dolby_data.be_id = dolby_data32.be_id;
  1741. dolby_data.param_id = dolby_data32.param_id;
  1742. dolby_data.length = dolby_data32.length;
  1743. dolby_data.data = compat_ptr(dolby_data32.data);
  1744. ret = msm_ds2_dap_ioctl_shared(hw, file, cmd, &dolby_data);
  1745. break;
  1746. }
  1747. case SNDRV_DEVDEP_DAP_IOCTL_GET_PARAM32:
  1748. cmd = SNDRV_DEVDEP_DAP_IOCTL_GET_PARAM;
  1749. goto handle_get_ioctl;
  1750. case SNDRV_DEVDEP_DAP_IOCTL_GET_VISUALIZER32:
  1751. cmd = SNDRV_DEVDEP_DAP_IOCTL_GET_VISUALIZER;
  1752. handle_get_ioctl:
  1753. {
  1754. struct dolby_param_data32 dolby_data32;
  1755. struct dolby_param_data dolby_data;
  1756. memset(&dolby_data32, 0, sizeof(dolby_data32));
  1757. memset(&dolby_data, 0, sizeof(dolby_data));
  1758. if (copy_from_user(&dolby_data32, (void *)arg,
  1759. sizeof(struct dolby_param_data32))) {
  1760. pr_err("%s: Copy from user failed\n", __func__);
  1761. ret = -EFAULT;
  1762. goto end;
  1763. }
  1764. dolby_data.version = dolby_data32.version;
  1765. dolby_data.device_id = dolby_data32.device_id;
  1766. dolby_data.be_id = dolby_data32.be_id;
  1767. dolby_data.param_id = dolby_data32.param_id;
  1768. dolby_data.length = dolby_data32.length;
  1769. dolby_data.data = compat_ptr(dolby_data32.data);
  1770. ret = msm_ds2_dap_ioctl_shared(hw, file, cmd, &dolby_data);
  1771. if (ret < 0)
  1772. pr_err("%s: ioctl cmd %d, returned err %d\n",
  1773. __func__, cmd, ret);
  1774. dolby_data32.length = dolby_data.length;
  1775. if (copy_to_user((void *)arg, &dolby_data32,
  1776. sizeof(struct dolby_param_data32))) {
  1777. pr_err("%s: Copy to user failed\n", __func__);
  1778. ret = -EFAULT;
  1779. goto end;
  1780. }
  1781. break;
  1782. }
  1783. case SNDRV_DEVDEP_DAP_IOCTL_DAP_LICENSE32: {
  1784. struct dolby_param_license32 dolby_license32;
  1785. struct dolby_param_license dolby_license;
  1786. cmd = SNDRV_DEVDEP_DAP_IOCTL_DAP_LICENSE;
  1787. if (copy_from_user((void *)&dolby_license32, (void *)arg,
  1788. sizeof(struct dolby_param_license32))) {
  1789. pr_err("%s: Copy from user failed\n", __func__);
  1790. ret = -EFAULT;
  1791. goto end;
  1792. }
  1793. dolby_license.dmid = dolby_license32.dmid;
  1794. dolby_license.license_key = dolby_license32.license_key;
  1795. ret = msm_ds2_dap_ioctl_shared(hw, file, cmd, &dolby_license);
  1796. break;
  1797. }
  1798. default:
  1799. pr_err("%s: called with invalid control 0x%x\n",
  1800. __func__, cmd);
  1801. ret = -EINVAL;
  1802. }
  1803. end:
  1804. return ret;
  1805. }
  1806. #endif
  1807. int msm_ds2_dap_init(int port_id, int copp_idx, int channels,
  1808. bool is_custom_stereo_on)
  1809. {
  1810. int ret = 0, idx = -1, i;
  1811. struct dolby_param_data dolby_data;
  1812. struct audproc_softvolume_params softvol = {
  1813. .period = DOLBY_SOFT_VOLUME_PERIOD,
  1814. .step = DOLBY_SOFT_VOLUME_STEP,
  1815. .rampingcurve = DOLBY_SOFT_VOLUME_CURVE_EXP,
  1816. };
  1817. pr_debug("%s: port id %d, copp_idx %d\n", __func__, port_id, copp_idx);
  1818. if (port_id != DOLBY_INVALID_PORT_ID) {
  1819. for (i = 0; i < DS2_DEVICES_ALL; i++) {
  1820. if ((dev_map[i].port_id == port_id) &&
  1821. /* device part of active device */
  1822. (dev_map[i].device_id &
  1823. ds2_dap_params_states.device)) {
  1824. idx = i;
  1825. /* Give priority to headset in case of
  1826. * combo device
  1827. */
  1828. if (dev_map[i].device_id == SPEAKER)
  1829. continue;
  1830. else
  1831. break;
  1832. }
  1833. }
  1834. if (idx < 0) {
  1835. pr_err("%s: invalid index for port %d\n",
  1836. __func__, port_id);
  1837. ret = -EINVAL;
  1838. goto end;
  1839. }
  1840. pr_debug("%s:index %d, dev[0x%x,0x%x]\n", __func__, idx,
  1841. dev_map[idx].device_id, ds2_dap_params_states.device);
  1842. dev_map[idx].active = true;
  1843. dev_map[idx].copp_idx = copp_idx;
  1844. dolby_data.param_id = DOLBY_COMMIT_ALL_TO_DSP;
  1845. dolby_data.length = 0;
  1846. dolby_data.data = NULL;
  1847. dolby_data.device_id = dev_map[idx].device_id;
  1848. pr_debug("%s: idx %d, active %d, dev id 0x%x, ref count %d\n",
  1849. __func__, idx, dev_map[idx].active,
  1850. dev_map[idx].device_id,
  1851. dev_map[idx].stream_ref_count);
  1852. if (dev_map[idx].stream_ref_count == 0) {
  1853. /*perform next 3 func only if hard bypass enabled*/
  1854. if (ds2_dap_params_states.dap_bypass_type ==
  1855. DAP_HARD_BYPASS) {
  1856. ret = msm_ds2_dap_alloc_and_store_cal_data(idx,
  1857. ADM_PATH_PLAYBACK, 0);
  1858. if (ret < 0) {
  1859. pr_err("%s: Failed to alloc and store cal data for idx %d, device %d, copp_idx %d",
  1860. __func__,
  1861. idx, dev_map[idx].device_id,
  1862. dev_map[idx].copp_idx);
  1863. dev_map[idx].active = false;
  1864. dev_map[idx].copp_idx = -1;
  1865. goto end;
  1866. }
  1867. ret = adm_set_softvolume(port_id, copp_idx,
  1868. &softvol);
  1869. if (ret < 0) {
  1870. pr_err("%s: Soft volume ret error %d\n",
  1871. __func__, ret);
  1872. dev_map[idx].active = false;
  1873. dev_map[idx].copp_idx = -1;
  1874. goto end;
  1875. }
  1876. ret = msm_ds2_dap_init_modules_in_topology(
  1877. idx);
  1878. if (ret < 0) {
  1879. pr_err("%s: Failed to init modules in topolofy for idx %d, device %d, copp_idx %d\n",
  1880. __func__, idx,
  1881. dev_map[idx].device_id,
  1882. dev_map[idx].copp_idx);
  1883. dev_map[idx].active = false;
  1884. dev_map[idx].copp_idx = -1;
  1885. goto end;
  1886. }
  1887. }
  1888. ret = msm_ds2_dap_commit_params(&dolby_data, 0);
  1889. if (ret < 0) {
  1890. pr_debug("%s: commit params ret %d\n",
  1891. __func__, ret);
  1892. ret = 0;
  1893. }
  1894. }
  1895. dev_map[idx].stream_ref_count++;
  1896. if (is_custom_stereo_on) {
  1897. ds2_dap_params_states.custom_stereo_onoff =
  1898. is_custom_stereo_on;
  1899. set_custom_stereo_onoff(idx,
  1900. is_custom_stereo_on);
  1901. }
  1902. }
  1903. end:
  1904. return ret;
  1905. }
  1906. void msm_ds2_dap_deinit(int port_id)
  1907. {
  1908. /*
  1909. * Get the active port corrresponding to the active device
  1910. * Check if this is same as incoming port
  1911. * Set it to invalid
  1912. */
  1913. int idx = -1, i;
  1914. pr_debug("%s: port_id %d\n", __func__, port_id);
  1915. if (port_id != DOLBY_INVALID_PORT_ID) {
  1916. for (i = 0; i < DS2_DEVICES_ALL; i++) {
  1917. /* Active port */
  1918. if ((dev_map[i].port_id == port_id) &&
  1919. /* device part of active device */
  1920. (dev_map[i].device_id &
  1921. ds2_dap_params_states.device) &&
  1922. /*
  1923. * Need this check to avoid race condition of
  1924. * active device being set and playback
  1925. * instance opened
  1926. */
  1927. /* active device*/
  1928. dev_map[i].active) {
  1929. idx = i;
  1930. if (dev_map[i].device_id == SPEAKER)
  1931. continue;
  1932. else
  1933. break;
  1934. }
  1935. }
  1936. if (idx < 0) {
  1937. pr_err("%s: invalid index for port %d\n",
  1938. __func__, port_id);
  1939. return;
  1940. }
  1941. pr_debug("%s:index %d, dev [0x%x, 0x%x]\n", __func__, idx,
  1942. dev_map[idx].device_id, ds2_dap_params_states.device);
  1943. dev_map[idx].stream_ref_count--;
  1944. if (dev_map[idx].stream_ref_count == 0) {
  1945. /*perform next func only if hard bypass enabled*/
  1946. if (ds2_dap_params_states.dap_bypass_type ==
  1947. DAP_HARD_BYPASS) {
  1948. msm_ds2_dap_free_cal_data(idx);
  1949. }
  1950. ds2_dap_params_states.device &= ~dev_map[idx].device_id;
  1951. dev_map[idx].active = false;
  1952. dev_map[idx].copp_idx = -1;
  1953. }
  1954. pr_debug("%s:idx %d, active %d, dev id 0x%x ref count %d\n",
  1955. __func__, idx, dev_map[idx].active,
  1956. dev_map[idx].device_id, dev_map[idx].stream_ref_count);
  1957. }
  1958. }
  1959. int msm_ds2_dap_set_custom_stereo_onoff(int port_id, int copp_idx,
  1960. bool is_custom_stereo_enabled)
  1961. {
  1962. int idx = -1, rc = 0, i;
  1963. pr_debug("%s: port_id %d\n", __func__, port_id);
  1964. if (port_id != DOLBY_INVALID_PORT_ID) {
  1965. for (i = 0; i < DS2_DEVICES_ALL; i++) {
  1966. if ((dev_map[i].port_id == port_id) &&
  1967. /* device part of active device */
  1968. (dev_map[i].device_id &
  1969. ds2_dap_params_states.device)) {
  1970. idx = i;
  1971. if (dev_map[i].device_id == SPEAKER)
  1972. continue;
  1973. else
  1974. break;
  1975. }
  1976. }
  1977. if (idx < 0) {
  1978. pr_err("%s: invalid index for port %d\n",
  1979. __func__, port_id);
  1980. return rc;
  1981. }
  1982. ds2_dap_params_states.custom_stereo_onoff =
  1983. is_custom_stereo_enabled;
  1984. rc = set_custom_stereo_onoff(idx,
  1985. is_custom_stereo_enabled);
  1986. if (rc < 0) {
  1987. pr_err("%s: Custom stereo err %d on port %d\n",
  1988. __func__, rc, port_id);
  1989. }
  1990. }
  1991. return rc;
  1992. }