msm-transcode-loopback-q6-v2.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2017-2019, The Linux Foundation. All rights reserved.
  3. */
  4. #include <linux/init.h>
  5. #include <linux/err.h>
  6. #include <linux/module.h>
  7. #include <linux/moduleparam.h>
  8. #include <linux/time.h>
  9. #include <linux/math64.h>
  10. #include <linux/wait.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/slab.h>
  13. #include <sound/core.h>
  14. #include <sound/soc.h>
  15. #include <sound/soc-dapm.h>
  16. #include <sound/pcm.h>
  17. #include <sound/initval.h>
  18. #include <sound/control.h>
  19. #include <sound/audio_effects.h>
  20. #include <sound/pcm_params.h>
  21. #include <sound/timer.h>
  22. #include <sound/tlv.h>
  23. #include <sound/compress_params.h>
  24. #include <sound/compress_offload.h>
  25. #include <sound/compress_driver.h>
  26. #include <dsp/msm_audio_ion.h>
  27. #include <dsp/apr_audio-v2.h>
  28. #include <dsp/q6asm-v2.h>
  29. #include <dsp/q6audio-v2.h>
  30. #include <dsp/msm-audio-effects-q6-v2.h>
  31. #include "msm-pcm-routing-v2.h"
  32. #include "msm-qti-pp-config.h"
  33. #define DRV_NAME "msm-transcode-loopback-v2"
  34. #define LOOPBACK_SESSION_MAX_NUM_STREAMS 2
  35. /* Max volume corresponding to 24dB */
  36. #define TRANSCODE_LR_VOL_MAX_DB 0xFFFF
  37. #define APP_TYPE_CONFIG_IDX_APP_TYPE 0
  38. #define APP_TYPE_CONFIG_IDX_ACDB_ID 1
  39. #define APP_TYPE_CONFIG_IDX_SAMPLE_RATE 2
  40. #define APP_TYPE_CONFIG_IDX_BE_ID 3
  41. static DEFINE_MUTEX(transcode_loopback_session_lock);
  42. struct msm_transcode_audio_effects {
  43. struct bass_boost_params bass_boost;
  44. struct pbe_params pbe;
  45. struct virtualizer_params virtualizer;
  46. struct reverb_params reverb;
  47. struct eq_params equalizer;
  48. struct soft_volume_params volume;
  49. };
  50. struct trans_loopback_pdata {
  51. struct snd_compr_stream *cstream[MSM_FRONTEND_DAI_MAX];
  52. uint32_t master_gain;
  53. int perf_mode[MSM_FRONTEND_DAI_MAX];
  54. struct msm_transcode_audio_effects *audio_effects[MSM_FRONTEND_DAI_MAX];
  55. };
  56. struct loopback_stream {
  57. struct snd_compr_stream *cstream;
  58. uint32_t codec_format;
  59. bool start;
  60. int perf_mode;
  61. };
  62. enum loopback_session_state {
  63. /* One or both streams not opened */
  64. LOOPBACK_SESSION_CLOSE = 0,
  65. /* Loopback streams opened */
  66. LOOPBACK_SESSION_READY,
  67. /* Loopback streams opened and formats configured */
  68. LOOPBACK_SESSION_START,
  69. /* Trigger issued on either of streams when in START state */
  70. LOOPBACK_SESSION_RUN
  71. };
  72. struct msm_transcode_loopback {
  73. struct loopback_stream source;
  74. struct loopback_stream sink;
  75. struct snd_compr_caps source_compr_cap;
  76. struct snd_compr_caps sink_compr_cap;
  77. uint32_t instance;
  78. uint32_t num_streams;
  79. int session_state;
  80. struct mutex lock;
  81. int session_id;
  82. struct audio_client *audio_client;
  83. };
  84. /* Transcode loopback global info struct */
  85. static struct msm_transcode_loopback transcode_info;
  86. static void loopback_event_handler(uint32_t opcode,
  87. uint32_t token, uint32_t *payload, void *priv)
  88. {
  89. struct msm_transcode_loopback *trans =
  90. (struct msm_transcode_loopback *)priv;
  91. struct snd_soc_pcm_runtime *rtd;
  92. struct snd_compr_stream *cstream;
  93. struct audio_client *ac;
  94. int stream_id;
  95. int ret;
  96. if (!trans || !payload) {
  97. pr_err("%s: rtd or payload is NULL\n", __func__);
  98. return;
  99. }
  100. cstream = trans->sink.cstream;
  101. ac = trans->audio_client;
  102. /*
  103. * Token for rest of the compressed commands use to set
  104. * session id, stream id, dir etc.
  105. */
  106. stream_id = q6asm_get_stream_id_from_token(token);
  107. switch (opcode) {
  108. case ASM_STREAM_CMD_ENCDEC_EVENTS:
  109. case ASM_IEC_61937_MEDIA_FMT_EVENT:
  110. pr_debug("%s: Handling stream event : 0X%x\n",
  111. __func__, opcode);
  112. rtd = cstream->private_data;
  113. if (!rtd) {
  114. pr_err("%s: rtd is NULL\n", __func__);
  115. return;
  116. }
  117. ret = msm_adsp_inform_mixer_ctl(rtd, payload);
  118. if (ret) {
  119. pr_err("%s: failed to inform mixer ctrl. err = %d\n",
  120. __func__, ret);
  121. return;
  122. }
  123. break;
  124. case APR_BASIC_RSP_RESULT: {
  125. switch (payload[0]) {
  126. case ASM_SESSION_CMD_RUN_V2:
  127. pr_debug("%s: ASM_SESSION_CMD_RUN_V2:", __func__);
  128. pr_debug("token 0x%x, stream id %d\n", token,
  129. stream_id);
  130. break;
  131. case ASM_STREAM_CMD_CLOSE:
  132. pr_debug("%s: ASM_DATA_CMD_CLOSE:", __func__);
  133. pr_debug("token 0x%x, stream id %d\n", token,
  134. stream_id);
  135. break;
  136. default:
  137. break;
  138. }
  139. break;
  140. }
  141. default:
  142. pr_debug("%s: Not Supported Event opcode[0x%x]\n",
  143. __func__, opcode);
  144. break;
  145. }
  146. }
  147. static void populate_codec_list(struct msm_transcode_loopback *trans,
  148. struct snd_compr_stream *cstream)
  149. {
  150. struct snd_compr_caps compr_cap;
  151. pr_debug("%s\n", __func__);
  152. memset(&compr_cap, 0, sizeof(struct snd_compr_caps));
  153. if (cstream->direction == SND_COMPRESS_CAPTURE) {
  154. compr_cap.direction = SND_COMPRESS_CAPTURE;
  155. compr_cap.num_codecs = 3;
  156. compr_cap.codecs[0] = SND_AUDIOCODEC_PCM;
  157. compr_cap.codecs[1] = SND_AUDIOCODEC_AC3;
  158. compr_cap.codecs[2] = SND_AUDIOCODEC_EAC3;
  159. memcpy(&trans->source_compr_cap, &compr_cap,
  160. sizeof(struct snd_compr_caps));
  161. }
  162. if (cstream->direction == SND_COMPRESS_PLAYBACK) {
  163. compr_cap.direction = SND_COMPRESS_PLAYBACK;
  164. compr_cap.num_codecs = 1;
  165. compr_cap.codecs[0] = SND_AUDIOCODEC_PCM;
  166. memcpy(&trans->sink_compr_cap, &compr_cap,
  167. sizeof(struct snd_compr_caps));
  168. }
  169. }
  170. static int msm_transcode_loopback_open(struct snd_compr_stream *cstream)
  171. {
  172. int ret = 0;
  173. struct snd_compr_runtime *runtime;
  174. struct snd_soc_pcm_runtime *rtd;
  175. struct msm_transcode_loopback *trans = &transcode_info;
  176. struct trans_loopback_pdata *pdata;
  177. struct snd_soc_component *component;
  178. if (cstream == NULL) {
  179. pr_err("%s: Invalid substream\n", __func__);
  180. return -EINVAL;
  181. }
  182. runtime = cstream->runtime;
  183. rtd = snd_pcm_substream_chip(cstream);
  184. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  185. if (!component) {
  186. pr_err("%s: component is NULL\n", __func__);
  187. return -EINVAL;
  188. }
  189. pdata = snd_soc_component_get_drvdata(component);
  190. pdata->cstream[rtd->dai_link->id] = cstream;
  191. pdata->audio_effects[rtd->dai_link->id] =
  192. kzalloc(sizeof(struct msm_transcode_audio_effects), GFP_KERNEL);
  193. if (pdata->audio_effects[rtd->dai_link->id] == NULL) {
  194. ret = -ENOMEM;
  195. goto effect_error;
  196. }
  197. mutex_lock(&trans->lock);
  198. if (trans->num_streams > LOOPBACK_SESSION_MAX_NUM_STREAMS) {
  199. pr_err("msm_transcode_open failed..invalid stream\n");
  200. ret = -EINVAL;
  201. goto exit;
  202. }
  203. if (cstream->direction == SND_COMPRESS_CAPTURE) {
  204. if (trans->source.cstream == NULL) {
  205. trans->source.cstream = cstream;
  206. trans->num_streams++;
  207. } else {
  208. pr_err("%s: capture stream already opened\n",
  209. __func__);
  210. ret = -EINVAL;
  211. goto exit;
  212. }
  213. } else if (cstream->direction == SND_COMPRESS_PLAYBACK) {
  214. if (trans->sink.cstream == NULL) {
  215. trans->sink.cstream = cstream;
  216. trans->num_streams++;
  217. } else {
  218. pr_debug("%s: playback stream already opened\n",
  219. __func__);
  220. ret = -EINVAL;
  221. goto exit;
  222. }
  223. msm_adsp_init_mixer_ctl_pp_event_queue(rtd);
  224. }
  225. pr_debug("%s: num stream%d, stream name %s\n", __func__,
  226. trans->num_streams, cstream->name);
  227. populate_codec_list(trans, cstream);
  228. if (trans->num_streams == LOOPBACK_SESSION_MAX_NUM_STREAMS) {
  229. pr_debug("%s: Moving loopback session to READY state %d\n",
  230. __func__, trans->session_state);
  231. trans->session_state = LOOPBACK_SESSION_READY;
  232. }
  233. runtime->private_data = trans;
  234. exit:
  235. mutex_unlock(&trans->lock);
  236. if ((pdata->audio_effects[rtd->dai_link->id] != NULL) && (ret < 0)) {
  237. kfree(pdata->audio_effects[rtd->dai_link->id]);
  238. pdata->audio_effects[rtd->dai_link->id] = NULL;
  239. }
  240. effect_error:
  241. return ret;
  242. }
  243. static void stop_transcoding(struct msm_transcode_loopback *trans)
  244. {
  245. struct snd_soc_pcm_runtime *soc_pcm_rx;
  246. struct snd_soc_pcm_runtime *soc_pcm_tx;
  247. if (trans->audio_client != NULL) {
  248. q6asm_cmd(trans->audio_client, CMD_CLOSE);
  249. if (trans->sink.cstream != NULL) {
  250. soc_pcm_rx = trans->sink.cstream->private_data;
  251. msm_pcm_routing_dereg_phy_stream(
  252. soc_pcm_rx->dai_link->id,
  253. SND_COMPRESS_PLAYBACK);
  254. }
  255. if (trans->source.cstream != NULL) {
  256. soc_pcm_tx = trans->source.cstream->private_data;
  257. msm_pcm_routing_dereg_phy_stream(
  258. soc_pcm_tx->dai_link->id,
  259. SND_COMPRESS_CAPTURE);
  260. }
  261. q6asm_audio_client_free(trans->audio_client);
  262. trans->audio_client = NULL;
  263. }
  264. }
  265. static int msm_transcode_loopback_free(struct snd_compr_stream *cstream)
  266. {
  267. struct snd_compr_runtime *runtime = cstream->runtime;
  268. struct msm_transcode_loopback *trans = runtime->private_data;
  269. struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(cstream);
  270. struct snd_soc_component *component;
  271. struct trans_loopback_pdata *pdata;
  272. int ret = 0;
  273. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  274. if (!component) {
  275. pr_err("%s: component is NULL\n", __func__);
  276. return -EINVAL;
  277. }
  278. pdata = snd_soc_component_get_drvdata(component);
  279. mutex_lock(&trans->lock);
  280. if (pdata->audio_effects[rtd->dai_link->id] != NULL) {
  281. kfree(pdata->audio_effects[rtd->dai_link->id]);
  282. pdata->audio_effects[rtd->dai_link->id] = NULL;
  283. }
  284. pr_debug("%s: Transcode loopback end:%d, streams %d\n", __func__,
  285. cstream->direction, trans->num_streams);
  286. trans->num_streams--;
  287. stop_transcoding(trans);
  288. if (cstream->direction == SND_COMPRESS_PLAYBACK) {
  289. memset(&trans->sink, 0, sizeof(struct loopback_stream));
  290. msm_adsp_clean_mixer_ctl_pp_event_queue(rtd);
  291. } else if (cstream->direction == SND_COMPRESS_CAPTURE) {
  292. memset(&trans->source, 0, sizeof(struct loopback_stream));
  293. }
  294. trans->session_state = LOOPBACK_SESSION_CLOSE;
  295. mutex_unlock(&trans->lock);
  296. return ret;
  297. }
  298. static int msm_transcode_loopback_trigger(struct snd_compr_stream *cstream,
  299. int cmd)
  300. {
  301. struct snd_compr_runtime *runtime = cstream->runtime;
  302. struct msm_transcode_loopback *trans = runtime->private_data;
  303. switch (cmd) {
  304. case SNDRV_PCM_TRIGGER_START:
  305. case SNDRV_PCM_TRIGGER_RESUME:
  306. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  307. if (trans->session_state == LOOPBACK_SESSION_START) {
  308. pr_debug("%s: Issue Loopback session %d RUN\n",
  309. __func__, trans->instance);
  310. q6asm_run_nowait(trans->audio_client, 0, 0, 0);
  311. trans->session_state = LOOPBACK_SESSION_RUN;
  312. }
  313. break;
  314. case SNDRV_PCM_TRIGGER_SUSPEND:
  315. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  316. case SNDRV_PCM_TRIGGER_STOP:
  317. pr_debug("%s: Issue Loopback session %d STOP\n", __func__,
  318. trans->instance);
  319. if (trans->session_state == LOOPBACK_SESSION_RUN)
  320. q6asm_cmd_nowait(trans->audio_client, CMD_PAUSE);
  321. trans->session_state = LOOPBACK_SESSION_START;
  322. break;
  323. default:
  324. break;
  325. }
  326. return 0;
  327. }
  328. static int msm_transcode_set_render_window(struct audio_client *ac,
  329. uint32_t ws_lsw, uint32_t ws_msw,
  330. uint32_t we_lsw, uint32_t we_msw)
  331. {
  332. int ret = -EINVAL;
  333. struct asm_session_mtmx_strtr_param_window_v2_t asm_mtmx_strtr_window;
  334. uint32_t param_id;
  335. pr_debug("%s, ws_lsw 0x%x ws_msw 0x%x we_lsw 0x%x we_msw 0x%x\n",
  336. __func__, ws_lsw, ws_msw, we_lsw, we_msw);
  337. memset(&asm_mtmx_strtr_window, 0,
  338. sizeof(struct asm_session_mtmx_strtr_param_window_v2_t));
  339. asm_mtmx_strtr_window.window_lsw = ws_lsw;
  340. asm_mtmx_strtr_window.window_msw = ws_msw;
  341. param_id = ASM_SESSION_MTMX_STRTR_PARAM_RENDER_WINDOW_START_V2;
  342. ret = q6asm_send_mtmx_strtr_window(ac, &asm_mtmx_strtr_window, param_id);
  343. if (ret) {
  344. pr_err("%s, start window can't be set error %d\n", __func__, ret);
  345. goto exit;
  346. }
  347. asm_mtmx_strtr_window.window_lsw = we_lsw;
  348. asm_mtmx_strtr_window.window_msw = we_msw;
  349. param_id = ASM_SESSION_MTMX_STRTR_PARAM_RENDER_WINDOW_END_V2;
  350. ret = q6asm_send_mtmx_strtr_window(ac, &asm_mtmx_strtr_window, param_id);
  351. if (ret)
  352. pr_err("%s, end window can't be set error %d\n", __func__, ret);
  353. exit:
  354. return ret;
  355. }
  356. static int msm_transcode_loopback_set_params(struct snd_compr_stream *cstream,
  357. struct snd_compr_params *codec_param)
  358. {
  359. struct snd_compr_runtime *runtime = cstream->runtime;
  360. struct msm_transcode_loopback *trans = runtime->private_data;
  361. struct snd_soc_pcm_runtime *soc_pcm_rx;
  362. struct snd_soc_pcm_runtime *soc_pcm_tx;
  363. struct snd_soc_pcm_runtime *rtd;
  364. struct snd_soc_component *component;
  365. struct trans_loopback_pdata *pdata;
  366. uint32_t bit_width = 16;
  367. int ret = 0;
  368. if (trans == NULL) {
  369. pr_err("%s: Invalid param\n", __func__);
  370. return -EINVAL;
  371. }
  372. mutex_lock(&trans->lock);
  373. rtd = snd_pcm_substream_chip(cstream);
  374. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  375. pdata = snd_soc_component_get_drvdata(component);
  376. if (cstream->direction == SND_COMPRESS_PLAYBACK) {
  377. if (codec_param->codec.id == SND_AUDIOCODEC_PCM) {
  378. trans->sink.codec_format =
  379. FORMAT_LINEAR_PCM;
  380. switch (codec_param->codec.format) {
  381. case SNDRV_PCM_FORMAT_S32_LE:
  382. bit_width = 32;
  383. break;
  384. case SNDRV_PCM_FORMAT_S24_LE:
  385. bit_width = 24;
  386. break;
  387. case SNDRV_PCM_FORMAT_S24_3LE:
  388. bit_width = 24;
  389. break;
  390. case SNDRV_PCM_FORMAT_S16_LE:
  391. default:
  392. bit_width = 16;
  393. break;
  394. }
  395. } else {
  396. pr_debug("%s: unknown sink codec\n", __func__);
  397. ret = -EINVAL;
  398. goto exit;
  399. }
  400. trans->sink.start = true;
  401. trans->sink.perf_mode = pdata->perf_mode[rtd->dai_link->id];
  402. }
  403. if (cstream->direction == SND_COMPRESS_CAPTURE) {
  404. switch (codec_param->codec.id) {
  405. case SND_AUDIOCODEC_PCM:
  406. pr_debug("Source SND_AUDIOCODEC_PCM\n");
  407. trans->source.codec_format =
  408. FORMAT_LINEAR_PCM;
  409. break;
  410. case SND_AUDIOCODEC_AC3:
  411. pr_debug("Source SND_AUDIOCODEC_AC3\n");
  412. trans->source.codec_format =
  413. FORMAT_AC3;
  414. break;
  415. case SND_AUDIOCODEC_EAC3:
  416. pr_debug("Source SND_AUDIOCODEC_EAC3\n");
  417. trans->source.codec_format =
  418. FORMAT_EAC3;
  419. break;
  420. default:
  421. pr_debug("%s: unknown source codec\n", __func__);
  422. ret = -EINVAL;
  423. goto exit;
  424. }
  425. trans->source.start = true;
  426. trans->source.perf_mode = pdata->perf_mode[rtd->dai_link->id];
  427. }
  428. pr_debug("%s: trans->source.start %d trans->sink.start %d trans->source.cstream %pK trans->sink.cstream %pK trans->session_state %d\n",
  429. __func__, trans->source.start, trans->sink.start,
  430. trans->source.cstream, trans->sink.cstream,
  431. trans->session_state);
  432. if ((trans->session_state == LOOPBACK_SESSION_READY) &&
  433. trans->source.start && trans->sink.start) {
  434. pr_debug("%s: Moving loopback session to start state\n",
  435. __func__);
  436. trans->session_state = LOOPBACK_SESSION_START;
  437. }
  438. if (trans->session_state == LOOPBACK_SESSION_START) {
  439. if (trans->audio_client != NULL) {
  440. pr_debug("%s: ASM client already opened, closing\n",
  441. __func__);
  442. stop_transcoding(trans);
  443. }
  444. trans->audio_client = q6asm_audio_client_alloc(
  445. (app_cb)loopback_event_handler, trans);
  446. if (!trans->audio_client) {
  447. pr_err("%s: Could not allocate memory\n", __func__);
  448. ret = -EINVAL;
  449. goto exit;
  450. }
  451. pr_debug("%s: ASM client allocated, callback %pK\n", __func__,
  452. loopback_event_handler);
  453. trans->session_id = trans->audio_client->session;
  454. trans->audio_client->perf_mode = trans->sink.perf_mode;
  455. ret = q6asm_open_transcode_loopback(trans->audio_client,
  456. bit_width,
  457. trans->source.codec_format,
  458. trans->sink.codec_format);
  459. if (ret < 0) {
  460. pr_err("%s: Session transcode loopback open failed\n",
  461. __func__);
  462. q6asm_audio_client_free(trans->audio_client);
  463. trans->audio_client = NULL;
  464. goto exit;
  465. }
  466. pr_debug("%s: Starting ADM open for loopback\n", __func__);
  467. soc_pcm_rx = trans->sink.cstream->private_data;
  468. soc_pcm_tx = trans->source.cstream->private_data;
  469. if (trans->source.codec_format != FORMAT_LINEAR_PCM)
  470. msm_pcm_routing_reg_phy_compr_stream(
  471. soc_pcm_tx->dai_link->id,
  472. LEGACY_PCM_MODE,
  473. trans->session_id,
  474. SNDRV_PCM_STREAM_CAPTURE,
  475. COMPRESSED_PASSTHROUGH_GEN);
  476. else
  477. msm_pcm_routing_reg_phy_stream(
  478. soc_pcm_tx->dai_link->id,
  479. trans->source.perf_mode,
  480. trans->session_id,
  481. SNDRV_PCM_STREAM_CAPTURE);
  482. /* Opening Rx ADM in LOW_LATENCY mode by default */
  483. msm_pcm_routing_reg_phy_stream(
  484. soc_pcm_rx->dai_link->id,
  485. trans->sink.perf_mode,
  486. trans->session_id,
  487. SNDRV_PCM_STREAM_PLAYBACK);
  488. pr_debug("%s: Successfully opened ADM sessions\n", __func__);
  489. }
  490. exit:
  491. mutex_unlock(&trans->lock);
  492. return ret;
  493. }
  494. static int msm_transcode_loopback_get_caps(struct snd_compr_stream *cstream,
  495. struct snd_compr_caps *arg)
  496. {
  497. struct snd_compr_runtime *runtime;
  498. struct msm_transcode_loopback *trans;
  499. if (!arg || !cstream) {
  500. pr_err("%s: Invalid arguments\n", __func__);
  501. return -EINVAL;
  502. }
  503. runtime = cstream->runtime;
  504. trans = runtime->private_data;
  505. pr_debug("%s\n", __func__);
  506. if (cstream->direction == SND_COMPRESS_CAPTURE)
  507. memcpy(arg, &trans->source_compr_cap,
  508. sizeof(struct snd_compr_caps));
  509. else
  510. memcpy(arg, &trans->sink_compr_cap,
  511. sizeof(struct snd_compr_caps));
  512. return 0;
  513. }
  514. static int msm_transcode_loopback_set_metadata(struct snd_compr_stream *cstream,
  515. struct snd_compr_metadata *metadata)
  516. {
  517. struct snd_soc_pcm_runtime *rtd;
  518. struct trans_loopback_pdata *pdata;
  519. struct msm_transcode_loopback *prtd = NULL;
  520. struct snd_soc_component *component;
  521. struct audio_client *ac = NULL;
  522. if (!metadata || !cstream) {
  523. pr_err("%s: Invalid arguments\n", __func__);
  524. return -EINVAL;
  525. }
  526. rtd = snd_pcm_substream_chip(cstream);
  527. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  528. pdata = snd_soc_component_get_drvdata(component);
  529. prtd = cstream->runtime->private_data;
  530. if (!prtd || !prtd->audio_client) {
  531. pr_err("%s: prtd or audio client is NULL\n", __func__);
  532. return -EINVAL;
  533. }
  534. ac = prtd->audio_client;
  535. switch (metadata->key) {
  536. case SNDRV_COMPRESS_LATENCY_MODE:
  537. {
  538. switch (metadata->value[0]) {
  539. case SNDRV_COMPRESS_LEGACY_LATENCY_MODE:
  540. pdata->perf_mode[rtd->dai_link->id] = LEGACY_PCM_MODE;
  541. break;
  542. case SNDRV_COMPRESS_LOW_LATENCY_MODE:
  543. pdata->perf_mode[rtd->dai_link->id] =
  544. LOW_LATENCY_PCM_MODE;
  545. break;
  546. default:
  547. pr_debug("%s: Unsupported latency mode %d, default to Legacy\n",
  548. __func__, metadata->value[0]);
  549. pdata->perf_mode[rtd->dai_link->id] = LEGACY_PCM_MODE;
  550. break;
  551. }
  552. break;
  553. }
  554. case SNDRV_COMPRESS_RENDER_WINDOW:
  555. {
  556. return msm_transcode_set_render_window(
  557. ac,
  558. metadata->value[0],
  559. metadata->value[1],
  560. metadata->value[2],
  561. metadata->value[3]);
  562. }
  563. default:
  564. pr_debug("%s: Unsupported metadata %d\n",
  565. __func__, metadata->key);
  566. break;
  567. }
  568. return 0;
  569. }
  570. static int msm_transcode_stream_cmd_put(struct snd_kcontrol *kcontrol,
  571. struct snd_ctl_elem_value *ucontrol)
  572. {
  573. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  574. unsigned long fe_id = kcontrol->private_value;
  575. struct trans_loopback_pdata *pdata = (struct trans_loopback_pdata *)
  576. snd_soc_component_get_drvdata(comp);
  577. struct snd_compr_stream *cstream = NULL;
  578. struct msm_transcode_loopback *prtd;
  579. int ret = 0;
  580. struct msm_adsp_event_data *event_data = NULL;
  581. if (fe_id >= MSM_FRONTEND_DAI_MAX) {
  582. pr_err("%s Received invalid fe_id %lu\n",
  583. __func__, fe_id);
  584. ret = -EINVAL;
  585. goto done;
  586. }
  587. cstream = pdata->cstream[fe_id];
  588. if (cstream == NULL) {
  589. pr_err("%s cstream is null.\n", __func__);
  590. ret = -EINVAL;
  591. goto done;
  592. }
  593. prtd = cstream->runtime->private_data;
  594. if (!prtd) {
  595. pr_err("%s: prtd is null.\n", __func__);
  596. ret = -EINVAL;
  597. goto done;
  598. }
  599. if (prtd->audio_client == NULL) {
  600. pr_err("%s: audio_client is null.\n", __func__);
  601. ret = -EINVAL;
  602. goto done;
  603. }
  604. event_data = (struct msm_adsp_event_data *)ucontrol->value.bytes.data;
  605. if ((event_data->event_type < ADSP_STREAM_PP_EVENT) ||
  606. (event_data->event_type >= ADSP_STREAM_EVENT_MAX)) {
  607. pr_err("%s: invalid event_type=%d",
  608. __func__, event_data->event_type);
  609. ret = -EINVAL;
  610. goto done;
  611. }
  612. if (event_data->payload_len > sizeof(ucontrol->value.bytes.data)
  613. - sizeof(struct msm_adsp_event_data)) {
  614. pr_err("%s param length=%d exceeds limit",
  615. __func__, event_data->payload_len);
  616. ret = -EINVAL;
  617. goto done;
  618. }
  619. ret = q6asm_send_stream_cmd(prtd->audio_client, event_data);
  620. if (ret < 0)
  621. pr_err("%s: failed to send stream event cmd, err = %d\n",
  622. __func__, ret);
  623. done:
  624. return ret;
  625. }
  626. static int msm_transcode_ion_fd_map_put(struct snd_kcontrol *kcontrol,
  627. struct snd_ctl_elem_value *ucontrol)
  628. {
  629. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  630. unsigned long fe_id = kcontrol->private_value;
  631. struct trans_loopback_pdata *pdata = (struct trans_loopback_pdata *)
  632. snd_soc_component_get_drvdata(comp);
  633. struct snd_compr_stream *cstream = NULL;
  634. struct msm_transcode_loopback *prtd;
  635. int fd;
  636. int ret = 0;
  637. if (fe_id >= MSM_FRONTEND_DAI_MAX) {
  638. pr_err("%s Received out of bounds invalid fe_id %lu\n",
  639. __func__, fe_id);
  640. ret = -EINVAL;
  641. goto done;
  642. }
  643. cstream = pdata->cstream[fe_id];
  644. if (cstream == NULL) {
  645. pr_err("%s cstream is null\n", __func__);
  646. ret = -EINVAL;
  647. goto done;
  648. }
  649. prtd = cstream->runtime->private_data;
  650. if (!prtd) {
  651. pr_err("%s: prtd is null\n", __func__);
  652. ret = -EINVAL;
  653. goto done;
  654. }
  655. if (prtd->audio_client == NULL) {
  656. pr_err("%s: audio_client is null\n", __func__);
  657. ret = -EINVAL;
  658. goto done;
  659. }
  660. memcpy(&fd, ucontrol->value.bytes.data, sizeof(fd));
  661. ret = q6asm_send_ion_fd(prtd->audio_client, fd);
  662. if (ret < 0)
  663. pr_err("%s: failed to register ion fd\n", __func__);
  664. done:
  665. return ret;
  666. }
  667. static int msm_transcode_rtic_event_ack_put(struct snd_kcontrol *kcontrol,
  668. struct snd_ctl_elem_value *ucontrol)
  669. {
  670. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  671. unsigned long fe_id = kcontrol->private_value;
  672. struct trans_loopback_pdata *pdata = (struct trans_loopback_pdata *)
  673. snd_soc_component_get_drvdata(comp);
  674. struct snd_compr_stream *cstream = NULL;
  675. struct msm_transcode_loopback *prtd;
  676. int ret = 0;
  677. int param_length = 0;
  678. if (fe_id >= MSM_FRONTEND_DAI_MAX) {
  679. pr_err("%s Received invalid fe_id %lu\n",
  680. __func__, fe_id);
  681. ret = -EINVAL;
  682. goto done;
  683. }
  684. cstream = pdata->cstream[fe_id];
  685. if (cstream == NULL) {
  686. pr_err("%s cstream is null\n", __func__);
  687. ret = -EINVAL;
  688. goto done;
  689. }
  690. prtd = cstream->runtime->private_data;
  691. if (!prtd) {
  692. pr_err("%s: prtd is null\n", __func__);
  693. ret = -EINVAL;
  694. goto done;
  695. }
  696. if (prtd->audio_client == NULL) {
  697. pr_err("%s: audio_client is null\n", __func__);
  698. ret = -EINVAL;
  699. goto done;
  700. }
  701. memcpy(&param_length, ucontrol->value.bytes.data,
  702. sizeof(param_length));
  703. if ((param_length + sizeof(param_length))
  704. >= sizeof(ucontrol->value.bytes.data)) {
  705. pr_err("%s param length=%d exceeds limit",
  706. __func__, param_length);
  707. ret = -EINVAL;
  708. goto done;
  709. }
  710. ret = q6asm_send_rtic_event_ack(prtd->audio_client,
  711. ucontrol->value.bytes.data + sizeof(param_length),
  712. param_length);
  713. if (ret < 0)
  714. pr_err("%s: failed to send rtic event ack, err = %d\n",
  715. __func__, ret);
  716. done:
  717. return ret;
  718. }
  719. static int msm_transcode_playback_app_type_cfg_put(
  720. struct snd_kcontrol *kcontrol,
  721. struct snd_ctl_elem_value *ucontrol)
  722. {
  723. u64 fe_id = kcontrol->private_value;
  724. int session_type = SESSION_TYPE_RX;
  725. int be_id = ucontrol->value.integer.value[APP_TYPE_CONFIG_IDX_BE_ID];
  726. struct msm_pcm_stream_app_type_cfg cfg_data = {0, 0, 48000};
  727. int ret = 0;
  728. cfg_data.app_type = ucontrol->value.integer.value[
  729. APP_TYPE_CONFIG_IDX_APP_TYPE];
  730. cfg_data.acdb_dev_id = ucontrol->value.integer.value[
  731. APP_TYPE_CONFIG_IDX_ACDB_ID];
  732. if (ucontrol->value.integer.value[APP_TYPE_CONFIG_IDX_SAMPLE_RATE] != 0)
  733. cfg_data.sample_rate = ucontrol->value.integer.value[
  734. APP_TYPE_CONFIG_IDX_SAMPLE_RATE];
  735. pr_debug("%s: fe_id %llu session_type %d be_id %d app_type %d acdb_dev_id %d sample_rate- %d\n",
  736. __func__, fe_id, session_type, be_id,
  737. cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
  738. ret = msm_pcm_routing_reg_stream_app_type_cfg(fe_id, session_type,
  739. be_id, &cfg_data);
  740. if (ret < 0)
  741. pr_err("%s: msm_transcode_playback_stream_app_type_cfg set failed returned %d\n",
  742. __func__, ret);
  743. return ret;
  744. }
  745. static int msm_transcode_playback_app_type_cfg_get(
  746. struct snd_kcontrol *kcontrol,
  747. struct snd_ctl_elem_value *ucontrol)
  748. {
  749. u64 fe_id = kcontrol->private_value;
  750. int session_type = SESSION_TYPE_RX;
  751. int be_id = 0;
  752. struct msm_pcm_stream_app_type_cfg cfg_data = {0};
  753. int ret = 0;
  754. ret = msm_pcm_routing_get_stream_app_type_cfg(fe_id, session_type,
  755. &be_id, &cfg_data);
  756. if (ret < 0) {
  757. pr_err("%s: msm_transcode_playback_stream_app_type_cfg get failed returned %d\n",
  758. __func__, ret);
  759. goto done;
  760. }
  761. ucontrol->value.integer.value[APP_TYPE_CONFIG_IDX_APP_TYPE] =
  762. cfg_data.app_type;
  763. ucontrol->value.integer.value[APP_TYPE_CONFIG_IDX_ACDB_ID] =
  764. cfg_data.acdb_dev_id;
  765. ucontrol->value.integer.value[APP_TYPE_CONFIG_IDX_SAMPLE_RATE] =
  766. cfg_data.sample_rate;
  767. ucontrol->value.integer.value[APP_TYPE_CONFIG_IDX_BE_ID] = be_id;
  768. pr_debug("%s: fedai_id %llu, session_type %d, be_id %d, app_type %d, acdb_dev_id %d, sample_rate %d\n",
  769. __func__, fe_id, session_type, be_id,
  770. cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
  771. done:
  772. return ret;
  773. }
  774. static int msm_transcode_set_volume(struct snd_compr_stream *cstream,
  775. uint32_t master_gain)
  776. {
  777. int rc = 0;
  778. struct msm_transcode_loopback *prtd;
  779. struct snd_soc_pcm_runtime *rtd;
  780. pr_debug("%s: master_gain %d\n", __func__, master_gain);
  781. if (!cstream || !cstream->runtime) {
  782. pr_err("%s: session not active\n", __func__);
  783. return -EINVAL;
  784. }
  785. rtd = cstream->private_data;
  786. prtd = cstream->runtime->private_data;
  787. if (!rtd || !prtd || !prtd->audio_client) {
  788. pr_err("%s: invalid rtd, prtd or audio client", __func__);
  789. return -EINVAL;
  790. }
  791. rc = q6asm_set_volume(prtd->audio_client, master_gain);
  792. if (rc < 0)
  793. pr_err("%s: Send vol gain command failed rc=%d\n",
  794. __func__, rc);
  795. return rc;
  796. }
  797. static int msm_transcode_volume_put(struct snd_kcontrol *kcontrol,
  798. struct snd_ctl_elem_value *ucontrol)
  799. {
  800. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  801. unsigned long fe_id = kcontrol->private_value;
  802. struct trans_loopback_pdata *pdata = (struct trans_loopback_pdata *)
  803. snd_soc_component_get_drvdata(comp);
  804. struct snd_compr_stream *cstream = NULL;
  805. uint32_t ret = 0;
  806. if (fe_id >= MSM_FRONTEND_DAI_MAX) {
  807. pr_err("%s Received out of bounds fe_id %lu\n",
  808. __func__, fe_id);
  809. return -EINVAL;
  810. }
  811. cstream = pdata->cstream[fe_id];
  812. pdata->master_gain = ucontrol->value.integer.value[0];
  813. pr_debug("%s: fe_id %lu master_gain %d\n",
  814. __func__, fe_id, pdata->master_gain);
  815. if (cstream)
  816. ret = msm_transcode_set_volume(cstream, pdata->master_gain);
  817. return ret;
  818. }
  819. static int msm_transcode_volume_get(struct snd_kcontrol *kcontrol,
  820. struct snd_ctl_elem_value *ucontrol)
  821. {
  822. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  823. unsigned long fe_id = kcontrol->private_value;
  824. struct trans_loopback_pdata *pdata = (struct trans_loopback_pdata *)
  825. snd_soc_component_get_drvdata(comp);
  826. if (fe_id >= MSM_FRONTEND_DAI_MAX) {
  827. pr_err("%s Received out of bound fe_id %lu\n", __func__, fe_id);
  828. return -EINVAL;
  829. }
  830. pr_debug("%s: fe_id %lu\n", __func__, fe_id);
  831. ucontrol->value.integer.value[0] = pdata->master_gain;
  832. return 0;
  833. }
  834. static int msm_transcode_audio_effects_config_info(struct snd_kcontrol *kcontrol,
  835. struct snd_ctl_elem_info *uinfo)
  836. {
  837. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  838. uinfo->count = MAX_PP_PARAMS_SZ;
  839. uinfo->value.integer.min = 0;
  840. uinfo->value.integer.max = 0xFFFFFFFF;
  841. return 0;
  842. }
  843. static int msm_transcode_audio_effects_config_get(struct snd_kcontrol *kcontrol,
  844. struct snd_ctl_elem_value *ucontrol)
  845. {
  846. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  847. unsigned long fe_id = kcontrol->private_value;
  848. struct trans_loopback_pdata *pdata = (struct trans_loopback_pdata *)
  849. snd_soc_component_get_drvdata(comp);
  850. struct msm_transcode_audio_effects *audio_effects = NULL;
  851. struct snd_compr_stream *cstream = NULL;
  852. pr_debug("%s: fe_id: %lu\n", __func__, fe_id);
  853. if (fe_id >= MSM_FRONTEND_DAI_MAX) {
  854. pr_err("%s Received out of bounds fe_id %lu\n",
  855. __func__, fe_id);
  856. return -EINVAL;
  857. }
  858. cstream = pdata->cstream[fe_id];
  859. audio_effects = pdata->audio_effects[fe_id];
  860. if (!cstream || !audio_effects) {
  861. pr_err("%s: stream or effects inactive\n", __func__);
  862. return -EINVAL;
  863. }
  864. return 0;
  865. }
  866. static int msm_transcode_audio_effects_config_put(struct snd_kcontrol *kcontrol,
  867. struct snd_ctl_elem_value *ucontrol)
  868. {
  869. struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
  870. unsigned long fe_id = kcontrol->private_value;
  871. struct trans_loopback_pdata *pdata = (struct trans_loopback_pdata *)
  872. snd_soc_component_get_drvdata(comp);
  873. struct msm_transcode_audio_effects *audio_effects = NULL;
  874. struct snd_compr_stream *cstream = NULL;
  875. struct msm_transcode_loopback *prtd = NULL;
  876. long *values = &(ucontrol->value.integer.value[0]);
  877. int effects_module;
  878. int ret = 0;
  879. pr_debug("%s: fe_id: %lu\n", __func__, fe_id);
  880. if (fe_id >= MSM_FRONTEND_DAI_MAX) {
  881. pr_err("%s Received out of bounds fe_id %lu\n",
  882. __func__, fe_id);
  883. ret = -EINVAL;
  884. goto exit;
  885. }
  886. cstream = pdata->cstream[fe_id];
  887. audio_effects = pdata->audio_effects[fe_id];
  888. if (!cstream || !audio_effects) {
  889. pr_err("%s: stream or effects inactive\n", __func__);
  890. ret = -EINVAL;
  891. goto exit;
  892. }
  893. prtd = cstream->runtime->private_data;
  894. if (!prtd) {
  895. pr_err("%s: cannot set audio effects\n", __func__);
  896. ret = -EINVAL;
  897. goto exit;
  898. }
  899. effects_module = *values++;
  900. switch (effects_module) {
  901. case VIRTUALIZER_MODULE:
  902. pr_debug("%s: VIRTUALIZER_MODULE\n", __func__);
  903. if (msm_audio_effects_is_effmodule_supp_in_top(effects_module,
  904. prtd->audio_client->topology))
  905. ret = msm_audio_effects_virtualizer_handler(
  906. prtd->audio_client,
  907. &(audio_effects->virtualizer),
  908. values);
  909. break;
  910. case REVERB_MODULE:
  911. pr_debug("%s: REVERB_MODULE\n", __func__);
  912. if (msm_audio_effects_is_effmodule_supp_in_top(effects_module,
  913. prtd->audio_client->topology))
  914. ret = msm_audio_effects_reverb_handler(prtd->audio_client,
  915. &(audio_effects->reverb),
  916. values);
  917. break;
  918. case BASS_BOOST_MODULE:
  919. pr_debug("%s: BASS_BOOST_MODULE\n", __func__);
  920. if (msm_audio_effects_is_effmodule_supp_in_top(effects_module,
  921. prtd->audio_client->topology))
  922. ret = msm_audio_effects_bass_boost_handler(prtd->audio_client,
  923. &(audio_effects->bass_boost),
  924. values);
  925. break;
  926. case PBE_MODULE:
  927. pr_debug("%s: PBE_MODULE\n", __func__);
  928. if (msm_audio_effects_is_effmodule_supp_in_top(effects_module,
  929. prtd->audio_client->topology))
  930. ret = msm_audio_effects_pbe_handler(prtd->audio_client,
  931. &(audio_effects->pbe),
  932. values);
  933. break;
  934. case EQ_MODULE:
  935. pr_debug("%s: EQ_MODULE\n", __func__);
  936. if (msm_audio_effects_is_effmodule_supp_in_top(effects_module,
  937. prtd->audio_client->topology))
  938. ret = msm_audio_effects_popless_eq_handler(prtd->audio_client,
  939. &(audio_effects->equalizer),
  940. values);
  941. break;
  942. case SOFT_VOLUME_MODULE:
  943. pr_debug("%s: SOFT_VOLUME_MODULE\n", __func__);
  944. break;
  945. case SOFT_VOLUME2_MODULE:
  946. pr_debug("%s: SOFT_VOLUME2_MODULE\n", __func__);
  947. if (msm_audio_effects_is_effmodule_supp_in_top(effects_module,
  948. prtd->audio_client->topology))
  949. ret = msm_audio_effects_volume_handler_v2(prtd->audio_client,
  950. &(audio_effects->volume),
  951. values, SOFT_VOLUME_INSTANCE_2);
  952. break;
  953. default:
  954. pr_err("%s Invalid effects config module\n", __func__);
  955. ret = -EINVAL;
  956. }
  957. exit:
  958. return ret;
  959. }
  960. static int msm_transcode_add_audio_effects_control(struct snd_soc_pcm_runtime *rtd)
  961. {
  962. struct snd_soc_component *component = NULL;
  963. const char *mixer_ctl_name = "Audio Effects Config";
  964. const char *deviceNo = "NN";
  965. char *mixer_str = NULL;
  966. int ctl_len = 0;
  967. int ret = 0;
  968. struct snd_kcontrol_new fe_audio_effects_config_control[1] = {
  969. {
  970. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  971. .name = "?",
  972. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  973. .info = msm_transcode_audio_effects_config_info,
  974. .get = msm_transcode_audio_effects_config_get,
  975. .put = msm_transcode_audio_effects_config_put,
  976. .private_value = 0,
  977. }
  978. };
  979. if (!rtd) {
  980. pr_err("%s NULL rtd\n", __func__);
  981. ret = -EINVAL;
  982. goto done;
  983. }
  984. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  985. if (!component) {
  986. pr_err("%s: component is NULL\n", __func__);
  987. return -EINVAL;
  988. }
  989. pr_debug("%s: added new compr FE with name %s, id %d, cpu dai %s, device no %d\n", __func__,
  990. rtd->dai_link->name, rtd->dai_link->id,
  991. rtd->dai_link->cpu_dai_name, rtd->pcm->device);
  992. ctl_len = strlen(mixer_ctl_name) + 1 + strlen(deviceNo) + 1;
  993. mixer_str = kzalloc(ctl_len, GFP_KERNEL);
  994. if (!mixer_str) {
  995. ret = -ENOMEM;
  996. goto done;
  997. }
  998. snprintf(mixer_str, ctl_len, "%s %d", mixer_ctl_name, rtd->pcm->device);
  999. fe_audio_effects_config_control[0].name = mixer_str;
  1000. fe_audio_effects_config_control[0].private_value = rtd->dai_link->id;
  1001. ret = snd_soc_add_component_controls(component,
  1002. fe_audio_effects_config_control,
  1003. ARRAY_SIZE(fe_audio_effects_config_control));
  1004. if (ret < 0)
  1005. pr_err("%s: failed to add ctl %s. err = %d\n", __func__, mixer_str, ret);
  1006. kfree(mixer_str);
  1007. done:
  1008. return ret;
  1009. }
  1010. static int msm_transcode_stream_cmd_control(
  1011. struct snd_soc_pcm_runtime *rtd)
  1012. {
  1013. struct snd_soc_component *component = NULL;
  1014. const char *mixer_ctl_name = DSP_STREAM_CMD;
  1015. const char *deviceNo = "NN";
  1016. char *mixer_str = NULL;
  1017. int ctl_len = 0, ret = 0;
  1018. struct snd_kcontrol_new fe_loopback_stream_cmd_config_control[1] = {
  1019. {
  1020. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1021. .name = "?",
  1022. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  1023. .info = msm_adsp_stream_cmd_info,
  1024. .put = msm_transcode_stream_cmd_put,
  1025. .private_value = 0,
  1026. }
  1027. };
  1028. if (!rtd) {
  1029. pr_err("%s NULL rtd\n", __func__);
  1030. ret = -EINVAL;
  1031. goto done;
  1032. }
  1033. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  1034. if (!component) {
  1035. pr_err("%s: component is NULL\n", __func__);
  1036. return -EINVAL;
  1037. }
  1038. ctl_len = strlen(mixer_ctl_name) + 1 + strlen(deviceNo) + 1;
  1039. mixer_str = kzalloc(ctl_len, GFP_KERNEL);
  1040. if (!mixer_str) {
  1041. ret = -ENOMEM;
  1042. goto done;
  1043. }
  1044. snprintf(mixer_str, ctl_len, "%s %d", mixer_ctl_name, rtd->pcm->device);
  1045. fe_loopback_stream_cmd_config_control[0].name = mixer_str;
  1046. fe_loopback_stream_cmd_config_control[0].private_value =
  1047. rtd->dai_link->id;
  1048. pr_debug("%s: Registering new mixer ctl %s\n", __func__, mixer_str);
  1049. ret = snd_soc_add_component_controls(component,
  1050. fe_loopback_stream_cmd_config_control,
  1051. ARRAY_SIZE(fe_loopback_stream_cmd_config_control));
  1052. if (ret < 0)
  1053. pr_err("%s: failed to add ctl %s. err = %d\n",
  1054. __func__, mixer_str, ret);
  1055. kfree(mixer_str);
  1056. done:
  1057. return ret;
  1058. }
  1059. static int msm_transcode_stream_callback_control(
  1060. struct snd_soc_pcm_runtime *rtd)
  1061. {
  1062. struct snd_soc_component *component = NULL;
  1063. const char *mixer_ctl_name = DSP_STREAM_CALLBACK;
  1064. const char *deviceNo = "NN";
  1065. char *mixer_str = NULL;
  1066. int ctl_len = 0, ret = 0;
  1067. struct snd_kcontrol *kctl;
  1068. struct snd_kcontrol_new fe_loopback_callback_config_control[1] = {
  1069. {
  1070. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1071. .name = "?",
  1072. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  1073. .info = msm_adsp_stream_callback_info,
  1074. .get = msm_adsp_stream_callback_get,
  1075. .private_value = 0,
  1076. }
  1077. };
  1078. if (!rtd) {
  1079. pr_err("%s: rtd is NULL\n", __func__);
  1080. ret = -EINVAL;
  1081. goto done;
  1082. }
  1083. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  1084. if (!component) {
  1085. pr_err("%s: component is NULL\n", __func__);
  1086. return -EINVAL;
  1087. }
  1088. ctl_len = strlen(mixer_ctl_name) + 1 + strlen(deviceNo) + 1;
  1089. mixer_str = kzalloc(ctl_len, GFP_KERNEL);
  1090. if (!mixer_str) {
  1091. ret = -ENOMEM;
  1092. goto done;
  1093. }
  1094. snprintf(mixer_str, ctl_len, "%s %d", mixer_ctl_name, rtd->pcm->device);
  1095. fe_loopback_callback_config_control[0].name = mixer_str;
  1096. fe_loopback_callback_config_control[0].private_value =
  1097. rtd->dai_link->id;
  1098. pr_debug("%s: Registering new mixer ctl %s\n", __func__, mixer_str);
  1099. ret = snd_soc_add_component_controls(component,
  1100. fe_loopback_callback_config_control,
  1101. ARRAY_SIZE(fe_loopback_callback_config_control));
  1102. if (ret < 0) {
  1103. pr_err("%s: failed to add ctl %s. err = %d\n",
  1104. __func__, mixer_str, ret);
  1105. ret = -EINVAL;
  1106. goto free_mixer_str;
  1107. }
  1108. kctl = snd_soc_card_get_kcontrol(rtd->card, mixer_str);
  1109. if (!kctl) {
  1110. pr_err("%s: failed to get kctl %s.\n", __func__, mixer_str);
  1111. ret = -EINVAL;
  1112. goto free_mixer_str;
  1113. }
  1114. kctl->private_data = NULL;
  1115. free_mixer_str:
  1116. kfree(mixer_str);
  1117. done:
  1118. return ret;
  1119. }
  1120. static int msm_transcode_add_ion_fd_cmd_control(struct snd_soc_pcm_runtime *rtd)
  1121. {
  1122. struct snd_soc_component *component = NULL;
  1123. const char *mixer_ctl_name = "Playback ION FD";
  1124. const char *deviceNo = "NN";
  1125. char *mixer_str = NULL;
  1126. int ctl_len = 0, ret = 0;
  1127. struct snd_kcontrol_new fe_ion_fd_config_control[1] = {
  1128. {
  1129. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1130. .name = "?",
  1131. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  1132. .info = msm_adsp_stream_cmd_info,
  1133. .put = msm_transcode_ion_fd_map_put,
  1134. .private_value = 0,
  1135. }
  1136. };
  1137. if (!rtd) {
  1138. pr_err("%s NULL rtd\n", __func__);
  1139. ret = -EINVAL;
  1140. goto done;
  1141. }
  1142. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  1143. if (!component) {
  1144. pr_err("%s: component is NULL\n", __func__);
  1145. return -EINVAL;
  1146. }
  1147. ctl_len = strlen(mixer_ctl_name) + 1 + strlen(deviceNo) + 1;
  1148. mixer_str = kzalloc(ctl_len, GFP_KERNEL);
  1149. if (!mixer_str) {
  1150. ret = -ENOMEM;
  1151. goto done;
  1152. }
  1153. snprintf(mixer_str, ctl_len, "%s %d", mixer_ctl_name, rtd->pcm->device);
  1154. fe_ion_fd_config_control[0].name = mixer_str;
  1155. fe_ion_fd_config_control[0].private_value = rtd->dai_link->id;
  1156. pr_debug("%s: Registering new mixer ctl %s\n", __func__, mixer_str);
  1157. ret = snd_soc_add_component_controls(component,
  1158. fe_ion_fd_config_control,
  1159. ARRAY_SIZE(fe_ion_fd_config_control));
  1160. if (ret < 0)
  1161. pr_err("%s: failed to add ctl %s\n", __func__, mixer_str);
  1162. kfree(mixer_str);
  1163. done:
  1164. return ret;
  1165. }
  1166. static int msm_transcode_add_event_ack_cmd_control(
  1167. struct snd_soc_pcm_runtime *rtd)
  1168. {
  1169. struct snd_soc_component *component = NULL;
  1170. const char *mixer_ctl_name = "Playback Event Ack";
  1171. const char *deviceNo = "NN";
  1172. char *mixer_str = NULL;
  1173. int ctl_len = 0, ret = 0;
  1174. struct snd_kcontrol_new fe_event_ack_config_control[1] = {
  1175. {
  1176. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1177. .name = "?",
  1178. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  1179. .info = msm_adsp_stream_cmd_info,
  1180. .put = msm_transcode_rtic_event_ack_put,
  1181. .private_value = 0,
  1182. }
  1183. };
  1184. if (!rtd) {
  1185. pr_err("%s NULL rtd\n", __func__);
  1186. ret = -EINVAL;
  1187. goto done;
  1188. }
  1189. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  1190. if (!component) {
  1191. pr_err("%s: component is NULL\n", __func__);
  1192. return -EINVAL;
  1193. }
  1194. ctl_len = strlen(mixer_ctl_name) + 1 + strlen(deviceNo) + 1;
  1195. mixer_str = kzalloc(ctl_len, GFP_KERNEL);
  1196. if (!mixer_str) {
  1197. ret = -ENOMEM;
  1198. goto done;
  1199. }
  1200. snprintf(mixer_str, ctl_len, "%s %d", mixer_ctl_name, rtd->pcm->device);
  1201. fe_event_ack_config_control[0].name = mixer_str;
  1202. fe_event_ack_config_control[0].private_value = rtd->dai_link->id;
  1203. pr_debug("%s: Registering new mixer ctl %s\n", __func__, mixer_str);
  1204. ret = snd_soc_add_component_controls(component,
  1205. fe_event_ack_config_control,
  1206. ARRAY_SIZE(fe_event_ack_config_control));
  1207. if (ret < 0)
  1208. pr_err("%s: failed to add ctl %s\n", __func__, mixer_str);
  1209. kfree(mixer_str);
  1210. done:
  1211. return ret;
  1212. }
  1213. static int msm_transcode_app_type_cfg_info(struct snd_kcontrol *kcontrol,
  1214. struct snd_ctl_elem_info *uinfo)
  1215. {
  1216. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1217. uinfo->count = 5;
  1218. uinfo->value.integer.min = 0;
  1219. uinfo->value.integer.max = 0xFFFFFFFF;
  1220. return 0;
  1221. }
  1222. static int msm_transcode_add_app_type_cfg_control(
  1223. struct snd_soc_pcm_runtime *rtd)
  1224. {
  1225. struct snd_soc_component *component = NULL;
  1226. char mixer_str[32];
  1227. struct snd_kcontrol_new fe_app_type_cfg_control[1] = {
  1228. {
  1229. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1230. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  1231. .info = msm_transcode_app_type_cfg_info,
  1232. .put = msm_transcode_playback_app_type_cfg_put,
  1233. .get = msm_transcode_playback_app_type_cfg_get,
  1234. .private_value = 0,
  1235. }
  1236. };
  1237. if (!rtd) {
  1238. pr_err("%s NULL rtd\n", __func__);
  1239. return -EINVAL;
  1240. }
  1241. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  1242. if (!component) {
  1243. pr_err("%s: component is NULL\n", __func__);
  1244. return -EINVAL;
  1245. }
  1246. if (rtd->compr->direction == SND_COMPRESS_PLAYBACK) {
  1247. snprintf(mixer_str, sizeof(mixer_str),
  1248. "Audio Stream %d App Type Cfg",
  1249. rtd->pcm->device);
  1250. fe_app_type_cfg_control[0].name = mixer_str;
  1251. fe_app_type_cfg_control[0].private_value = rtd->dai_link->id;
  1252. fe_app_type_cfg_control[0].put =
  1253. msm_transcode_playback_app_type_cfg_put;
  1254. fe_app_type_cfg_control[0].get =
  1255. msm_transcode_playback_app_type_cfg_get;
  1256. pr_debug("Registering new mixer ctl %s", mixer_str);
  1257. snd_soc_add_component_controls(component,
  1258. fe_app_type_cfg_control,
  1259. ARRAY_SIZE(fe_app_type_cfg_control));
  1260. }
  1261. return 0;
  1262. }
  1263. static int msm_transcode_volume_info(struct snd_kcontrol *kcontrol,
  1264. struct snd_ctl_elem_info *uinfo)
  1265. {
  1266. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1267. uinfo->count = 1;
  1268. uinfo->value.integer.min = 0;
  1269. uinfo->value.integer.max = TRANSCODE_LR_VOL_MAX_DB;
  1270. return 0;
  1271. }
  1272. static int msm_transcode_add_volume_control(struct snd_soc_pcm_runtime *rtd)
  1273. {
  1274. struct snd_soc_component *component = NULL;
  1275. struct snd_kcontrol_new fe_volume_control[1] = {
  1276. {
  1277. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1278. .name = "Transcode Loopback Rx Volume",
  1279. .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |
  1280. SNDRV_CTL_ELEM_ACCESS_READWRITE,
  1281. .info = msm_transcode_volume_info,
  1282. .get = msm_transcode_volume_get,
  1283. .put = msm_transcode_volume_put,
  1284. .private_value = 0,
  1285. }
  1286. };
  1287. if (!rtd) {
  1288. pr_err("%s NULL rtd\n", __func__);
  1289. return -EINVAL;
  1290. }
  1291. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  1292. if (!component) {
  1293. pr_err("%s: component is NULL\n", __func__);
  1294. return -EINVAL;
  1295. }
  1296. if (rtd->compr->direction == SND_COMPRESS_PLAYBACK) {
  1297. fe_volume_control[0].private_value = rtd->dai_link->id;
  1298. pr_debug("Registering new mixer ctl %s",
  1299. fe_volume_control[0].name);
  1300. snd_soc_add_component_controls(component, fe_volume_control,
  1301. ARRAY_SIZE(fe_volume_control));
  1302. }
  1303. return 0;
  1304. }
  1305. static int msm_transcode_loopback_new(struct snd_soc_pcm_runtime *rtd)
  1306. {
  1307. int rc;
  1308. rc = msm_transcode_add_audio_effects_control(rtd);
  1309. if (rc)
  1310. pr_err("%s: Could not add Compr Audio Effects Control\n",
  1311. __func__);
  1312. rc = msm_transcode_stream_cmd_control(rtd);
  1313. if (rc)
  1314. pr_err("%s: ADSP Stream Cmd Control open failed\n", __func__);
  1315. rc = msm_transcode_stream_callback_control(rtd);
  1316. if (rc)
  1317. pr_err("%s: ADSP Stream callback Control open failed\n",
  1318. __func__);
  1319. rc = msm_transcode_add_ion_fd_cmd_control(rtd);
  1320. if (rc)
  1321. pr_err("%s: Could not add transcode ion fd Control\n",
  1322. __func__);
  1323. rc = msm_transcode_add_event_ack_cmd_control(rtd);
  1324. if (rc)
  1325. pr_err("%s: Could not add transcode event ack Control\n",
  1326. __func__);
  1327. rc = msm_transcode_add_app_type_cfg_control(rtd);
  1328. if (rc)
  1329. pr_err("%s: Could not add Compr App Type Cfg Control\n",
  1330. __func__);
  1331. rc = msm_transcode_add_volume_control(rtd);
  1332. if (rc)
  1333. pr_err("%s: Could not add transcode volume Control\n",
  1334. __func__);
  1335. return 0;
  1336. }
  1337. static struct snd_compr_ops msm_transcode_loopback_ops = {
  1338. .open = msm_transcode_loopback_open,
  1339. .free = msm_transcode_loopback_free,
  1340. .trigger = msm_transcode_loopback_trigger,
  1341. .set_params = msm_transcode_loopback_set_params,
  1342. .get_caps = msm_transcode_loopback_get_caps,
  1343. .set_metadata = msm_transcode_loopback_set_metadata,
  1344. };
  1345. static int msm_transcode_loopback_probe(struct snd_soc_component *component)
  1346. {
  1347. struct trans_loopback_pdata *pdata = NULL;
  1348. int i;
  1349. pr_debug("%s\n", __func__);
  1350. pdata = (struct trans_loopback_pdata *)
  1351. kzalloc(sizeof(struct trans_loopback_pdata),
  1352. GFP_KERNEL);
  1353. if (!pdata)
  1354. return -ENOMEM;
  1355. for (i = 0; i < MSM_FRONTEND_DAI_MAX; i++) {
  1356. pdata->audio_effects[i] = NULL;
  1357. pdata->perf_mode[i] = LOW_LATENCY_PCM_MODE;
  1358. }
  1359. snd_soc_component_set_drvdata(component, pdata);
  1360. return 0;
  1361. }
  1362. static void msm_transcode_loopback_remove(struct snd_soc_component *component)
  1363. {
  1364. struct trans_loopback_pdata *pdata = NULL;
  1365. pdata = (struct trans_loopback_pdata *)
  1366. snd_soc_component_get_drvdata(component);
  1367. kfree(pdata);
  1368. return;
  1369. }
  1370. static struct snd_soc_component_driver msm_soc_component = {
  1371. .name = DRV_NAME,
  1372. .probe = msm_transcode_loopback_probe,
  1373. .compr_ops = &msm_transcode_loopback_ops,
  1374. .pcm_new = msm_transcode_loopback_new,
  1375. .remove = msm_transcode_loopback_remove,
  1376. };
  1377. static int msm_transcode_dev_probe(struct platform_device *pdev)
  1378. {
  1379. pr_debug("%s: dev name %s\n", __func__, dev_name(&pdev->dev));
  1380. if (pdev->dev.of_node)
  1381. dev_set_name(&pdev->dev, "%s", "msm-transcode-loopback");
  1382. return snd_soc_register_component(&pdev->dev,
  1383. &msm_soc_component,
  1384. NULL, 0);
  1385. }
  1386. static int msm_transcode_remove(struct platform_device *pdev)
  1387. {
  1388. snd_soc_unregister_component(&pdev->dev);
  1389. return 0;
  1390. }
  1391. static const struct of_device_id msm_transcode_loopback_dt_match[] = {
  1392. {.compatible = "qcom,msm-transcode-loopback"},
  1393. {}
  1394. };
  1395. MODULE_DEVICE_TABLE(of, msm_transcode_loopback_dt_match);
  1396. static struct platform_driver msm_transcode_loopback_driver = {
  1397. .driver = {
  1398. .name = "msm-transcode-loopback",
  1399. .owner = THIS_MODULE,
  1400. .of_match_table = msm_transcode_loopback_dt_match,
  1401. },
  1402. .probe = msm_transcode_dev_probe,
  1403. .remove = msm_transcode_remove,
  1404. };
  1405. int __init msm_transcode_loopback_init(void)
  1406. {
  1407. memset(&transcode_info, 0, sizeof(struct msm_transcode_loopback));
  1408. mutex_init(&transcode_info.lock);
  1409. return platform_driver_register(&msm_transcode_loopback_driver);
  1410. }
  1411. void msm_transcode_loopback_exit(void)
  1412. {
  1413. mutex_destroy(&transcode_info.lock);
  1414. platform_driver_unregister(&msm_transcode_loopback_driver);
  1415. }
  1416. MODULE_DESCRIPTION("Transcode loopback platform driver");
  1417. MODULE_LICENSE("GPL v2");