msm-pcm-loopback-v2.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2013-2020, 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/platform_device.h>
  8. #include <linux/slab.h>
  9. #include <linux/dma-mapping.h>
  10. #include <sound/core.h>
  11. #include <sound/soc.h>
  12. #include <sound/pcm.h>
  13. #include <sound/initval.h>
  14. #include <sound/control.h>
  15. #include <sound/tlv.h>
  16. #include <asm/dma.h>
  17. #include <dsp/apr_audio-v2.h>
  18. #include <dsp/q6audio-v2.h>
  19. #include <dsp/q6asm-v2.h>
  20. #include "msm-pcm-routing-v2.h"
  21. #include "msm-qti-pp-config.h"
  22. #define DRV_NAME "msm-pcm-loopback-v2"
  23. #define LOOPBACK_VOL_MAX_STEPS 0x2000
  24. #define LOOPBACK_SESSION_MAX 4
  25. static DEFINE_MUTEX(loopback_session_lock);
  26. static const DECLARE_TLV_DB_LINEAR(loopback_rx_vol_gain, 0,
  27. LOOPBACK_VOL_MAX_STEPS);
  28. struct msm_pcm_loopback {
  29. struct snd_pcm_substream *playback_substream;
  30. struct snd_pcm_substream *capture_substream;
  31. int instance;
  32. struct mutex lock;
  33. uint32_t samp_rate;
  34. uint32_t channel_mode;
  35. int playback_start;
  36. int capture_start;
  37. int session_id;
  38. struct audio_client *audio_client;
  39. uint32_t volume;
  40. };
  41. struct fe_dai_session_map {
  42. char stream_name[32];
  43. struct msm_pcm_loopback *loopback_priv;
  44. };
  45. static struct fe_dai_session_map session_map[LOOPBACK_SESSION_MAX] = {
  46. { {}, NULL},
  47. { {}, NULL},
  48. { {}, NULL},
  49. { {}, NULL},
  50. };
  51. static u32 hfp_tx_mute;
  52. struct msm_pcm_pdata {
  53. int perf_mode;
  54. struct snd_pcm *pcm_device[MSM_FRONTEND_DAI_MM_SIZE];
  55. struct msm_pcm_channel_mixer *chmixer_pspd[MSM_FRONTEND_DAI_MM_SIZE][2];
  56. };
  57. static void stop_pcm(struct msm_pcm_loopback *pcm);
  58. static int msm_pcm_loopback_get_session(struct snd_soc_pcm_runtime *rtd,
  59. struct msm_pcm_loopback **pcm);
  60. static void msm_pcm_route_event_handler(enum msm_pcm_routing_event event,
  61. void *priv_data)
  62. {
  63. struct msm_pcm_loopback *pcm = priv_data;
  64. WARN_ON(!pcm);
  65. pr_debug("%s: event 0x%x\n", __func__, event);
  66. switch (event) {
  67. case MSM_PCM_RT_EVT_DEVSWITCH:
  68. q6asm_cmd(pcm->audio_client, CMD_PAUSE);
  69. q6asm_cmd(pcm->audio_client, CMD_FLUSH);
  70. q6asm_run(pcm->audio_client, 0, 0, 0);
  71. /* fallthrough */
  72. default:
  73. pr_err("%s: default event 0x%x\n", __func__, event);
  74. break;
  75. }
  76. }
  77. static void msm_pcm_loopback_event_handler(uint32_t opcode, uint32_t token,
  78. uint32_t *payload, void *priv)
  79. {
  80. struct msm_pcm_loopback *pcm = priv;
  81. struct snd_pcm_substream *substream = NULL;
  82. struct snd_soc_pcm_runtime *rtd;
  83. int ret = 0;
  84. pr_debug("%s:\n", __func__);
  85. if (pcm->playback_substream != NULL)
  86. substream = pcm->playback_substream;
  87. else if (pcm->capture_substream != NULL)
  88. substream = pcm->capture_substream;
  89. switch (opcode) {
  90. case ASM_STREAM_PP_EVENT: {
  91. pr_debug("%s: ASM_STREAM_EVENT (0x%x)\n", __func__, opcode);
  92. if (!substream) {
  93. pr_err("%s: substream is NULL.\n", __func__);
  94. return;
  95. }
  96. rtd = substream->private_data;
  97. if (!rtd) {
  98. pr_err("%s: rtd is NULL\n", __func__);
  99. return;
  100. }
  101. ret = msm_adsp_inform_mixer_ctl(rtd, payload);
  102. if (ret) {
  103. pr_err("%s: failed to inform mixer ctl. err = %d\n",
  104. __func__, ret);
  105. return;
  106. }
  107. break;
  108. }
  109. case APR_BASIC_RSP_RESULT: {
  110. switch (payload[0]) {
  111. case ASM_STREAM_CMD_REGISTER_PP_EVENTS:
  112. pr_debug("%s: ASM_STREAM_CMD_REGISTER_PP_EVENTS:\n",
  113. __func__);
  114. break;
  115. default:
  116. break;
  117. }
  118. break;
  119. }
  120. default:
  121. pr_err("%s: Not Supported Event opcode[0x%x]\n",
  122. __func__, opcode);
  123. break;
  124. }
  125. }
  126. static int msm_loopback_session_mute_get(struct snd_kcontrol *kcontrol,
  127. struct snd_ctl_elem_value *ucontrol)
  128. {
  129. ucontrol->value.integer.value[0] = hfp_tx_mute;
  130. return 0;
  131. }
  132. static int msm_loopback_session_mute_put(struct snd_kcontrol *kcontrol,
  133. struct snd_ctl_elem_value *ucontrol)
  134. {
  135. int ret = 0, n = 0;
  136. int mute = ucontrol->value.integer.value[0];
  137. struct msm_pcm_loopback *pcm = NULL;
  138. if ((mute < 0) || (mute > 1)) {
  139. pr_err(" %s Invalid arguments", __func__);
  140. ret = -EINVAL;
  141. goto done;
  142. }
  143. mutex_lock(&loopback_session_lock);
  144. pr_debug("%s: mute=%d\n", __func__, mute);
  145. hfp_tx_mute = mute;
  146. for (n = 0; n < LOOPBACK_SESSION_MAX; n++) {
  147. if (!strcmp(session_map[n].stream_name, "MultiMedia6"))
  148. pcm = session_map[n].loopback_priv;
  149. }
  150. if (pcm && pcm->audio_client) {
  151. ret = q6asm_set_mute(pcm->audio_client, mute);
  152. if (ret < 0)
  153. pr_err("%s: Send mute command failed rc=%d\n",
  154. __func__, ret);
  155. }
  156. mutex_unlock(&loopback_session_lock);
  157. done:
  158. return ret;
  159. }
  160. static struct snd_kcontrol_new msm_loopback_controls[] = {
  161. SOC_SINGLE_EXT("HFP TX Mute", SND_SOC_NOPM, 0, 1, 0,
  162. msm_loopback_session_mute_get,
  163. msm_loopback_session_mute_put),
  164. };
  165. static int msm_pcm_loopback_probe(struct snd_soc_component *component)
  166. {
  167. snd_soc_add_component_controls(component, msm_loopback_controls,
  168. ARRAY_SIZE(msm_loopback_controls));
  169. return 0;
  170. }
  171. static int pcm_loopback_set_volume(struct msm_pcm_loopback *prtd,
  172. uint32_t volume)
  173. {
  174. int rc = -EINVAL;
  175. pr_debug("%s: Setting volume 0x%x\n", __func__, volume);
  176. if (prtd && prtd->audio_client) {
  177. rc = q6asm_set_volume(prtd->audio_client, volume);
  178. if (rc < 0) {
  179. pr_err("%s: Send Volume command failed rc = %d\n",
  180. __func__, rc);
  181. return rc;
  182. }
  183. prtd->volume = volume;
  184. }
  185. return rc;
  186. }
  187. static int msm_pcm_loopback_get_session(struct snd_soc_pcm_runtime *rtd,
  188. struct msm_pcm_loopback **pcm)
  189. {
  190. struct snd_soc_component *component =
  191. snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  192. int ret = 0;
  193. int n, index = -1;
  194. if (!component) {
  195. pr_err("%s: component is NULL\n", __func__);
  196. return -EINVAL;
  197. }
  198. dev_dbg(component->dev, "%s: stream %s\n", __func__,
  199. rtd->dai_link->stream_name);
  200. mutex_lock(&loopback_session_lock);
  201. for (n = 0; n < LOOPBACK_SESSION_MAX; n++) {
  202. if (!strcmp(rtd->dai_link->stream_name,
  203. session_map[n].stream_name)) {
  204. *pcm = session_map[n].loopback_priv;
  205. goto exit;
  206. }
  207. /*
  208. * Store the min index value for allocating a new session.
  209. * Here, if session stream name is not found in the
  210. * existing entries after the loop iteration, then this
  211. * index will be used to allocate the new session.
  212. * This index variable is expected to point to the topmost
  213. * available free session.
  214. */
  215. if (!(session_map[n].stream_name[0]) && (index < 0))
  216. index = n;
  217. }
  218. if (index < 0) {
  219. dev_err(component->dev, "%s: Max Sessions allocated\n",
  220. __func__);
  221. ret = -EAGAIN;
  222. goto exit;
  223. }
  224. session_map[index].loopback_priv = kzalloc(
  225. sizeof(struct msm_pcm_loopback), GFP_KERNEL);
  226. if (!session_map[index].loopback_priv) {
  227. ret = -ENOMEM;
  228. goto exit;
  229. }
  230. strlcpy(session_map[index].stream_name,
  231. rtd->dai_link->stream_name,
  232. sizeof(session_map[index].stream_name));
  233. dev_dbg(component->dev, "%s: stream %s index %d\n",
  234. __func__, session_map[index].stream_name, index);
  235. mutex_init(&session_map[index].loopback_priv->lock);
  236. *pcm = session_map[index].loopback_priv;
  237. exit:
  238. mutex_unlock(&loopback_session_lock);
  239. return ret;
  240. }
  241. static int msm_pcm_open(struct snd_pcm_substream *substream)
  242. {
  243. struct snd_pcm_runtime *runtime = substream->runtime;
  244. struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
  245. struct snd_soc_component *component =
  246. snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  247. struct msm_pcm_loopback *pcm = NULL;
  248. int ret = 0;
  249. uint16_t bits_per_sample = 16;
  250. struct asm_session_mtmx_strtr_param_window_v2_t asm_mtmx_strtr_window;
  251. uint32_t param_id;
  252. struct msm_pcm_pdata *pdata;
  253. if (!component) {
  254. pr_err("%s: component is NULL\n", __func__);
  255. return -EINVAL;
  256. }
  257. ret = msm_pcm_loopback_get_session(rtd, &pcm);
  258. if (ret)
  259. return ret;
  260. mutex_lock(&pcm->lock);
  261. pcm->volume = 0x2000;
  262. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  263. pcm->playback_substream = substream;
  264. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  265. pcm->capture_substream = substream;
  266. pcm->instance++;
  267. dev_dbg(component->dev, "%s: pcm out open: %d,%d\n", __func__,
  268. pcm->instance, substream->stream);
  269. if (pcm->instance == 2) {
  270. if (pcm->audio_client != NULL)
  271. stop_pcm(pcm);
  272. pdata = (struct msm_pcm_pdata *)
  273. dev_get_drvdata(component->dev);
  274. if (!pdata) {
  275. dev_err(component->dev,
  276. "%s: platform data not populated\n", __func__);
  277. mutex_unlock(&pcm->lock);
  278. return -EINVAL;
  279. }
  280. pcm->audio_client = q6asm_audio_client_alloc(
  281. (app_cb)msm_pcm_loopback_event_handler, pcm);
  282. if (!pcm->audio_client) {
  283. dev_err(component->dev,
  284. "%s: Could not allocate memory\n", __func__);
  285. mutex_unlock(&pcm->lock);
  286. return -ENOMEM;
  287. }
  288. pcm->session_id = pcm->audio_client->session;
  289. pcm->audio_client->perf_mode = pdata->perf_mode;
  290. ret = q6asm_open_loopback_v2(pcm->audio_client,
  291. bits_per_sample);
  292. if (ret < 0) {
  293. dev_err(component->dev,
  294. "%s: pcm out open failed\n", __func__);
  295. q6asm_audio_client_free(pcm->audio_client);
  296. pcm->audio_client = NULL;
  297. mutex_unlock(&pcm->lock);
  298. return -ENOMEM;
  299. }
  300. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  301. pcm->playback_substream = substream;
  302. ret = pcm_loopback_set_volume(pcm, pcm->volume);
  303. if (ret < 0)
  304. dev_err(component->dev,
  305. "Error %d setting volume", ret);
  306. }
  307. /* Set to largest negative value */
  308. asm_mtmx_strtr_window.window_lsw = 0x00000000;
  309. asm_mtmx_strtr_window.window_msw = 0x80000000;
  310. param_id = ASM_SESSION_MTMX_STRTR_PARAM_RENDER_WINDOW_START_V2;
  311. q6asm_send_mtmx_strtr_window(pcm->audio_client,
  312. &asm_mtmx_strtr_window,
  313. param_id);
  314. /* Set to largest positive value */
  315. asm_mtmx_strtr_window.window_lsw = 0xffffffff;
  316. asm_mtmx_strtr_window.window_msw = 0x7fffffff;
  317. param_id = ASM_SESSION_MTMX_STRTR_PARAM_RENDER_WINDOW_END_V2;
  318. q6asm_send_mtmx_strtr_window(pcm->audio_client,
  319. &asm_mtmx_strtr_window,
  320. param_id);
  321. }
  322. dev_info(component->dev, "%s: Instance = %d, Stream ID = %s\n",
  323. __func__, pcm->instance, substream->pcm->id);
  324. runtime->private_data = pcm;
  325. msm_adsp_init_mixer_ctl_pp_event_queue(substream->private_data);
  326. mutex_unlock(&pcm->lock);
  327. return 0;
  328. }
  329. static void stop_pcm(struct msm_pcm_loopback *pcm)
  330. {
  331. struct snd_soc_pcm_runtime *soc_pcm_rx;
  332. struct snd_soc_pcm_runtime *soc_pcm_tx;
  333. if (pcm->audio_client == NULL) {
  334. pr_err("%s: audio client freed\n", __func__);
  335. return;
  336. }
  337. mutex_lock(&loopback_session_lock);
  338. q6asm_cmd(pcm->audio_client, CMD_CLOSE);
  339. if (pcm->playback_substream != NULL) {
  340. soc_pcm_rx = pcm->playback_substream->private_data;
  341. msm_pcm_routing_dereg_phy_stream(soc_pcm_rx->dai_link->id,
  342. SNDRV_PCM_STREAM_PLAYBACK);
  343. }
  344. if (pcm->capture_substream != NULL) {
  345. soc_pcm_tx = pcm->capture_substream->private_data;
  346. msm_pcm_routing_dereg_phy_stream(soc_pcm_tx->dai_link->id,
  347. SNDRV_PCM_STREAM_CAPTURE);
  348. }
  349. q6asm_audio_client_free(pcm->audio_client);
  350. pcm->audio_client = NULL;
  351. mutex_unlock(&loopback_session_lock);
  352. }
  353. static int msm_pcm_close(struct snd_pcm_substream *substream)
  354. {
  355. struct snd_pcm_runtime *runtime = substream->runtime;
  356. struct msm_pcm_loopback *pcm = runtime->private_data;
  357. struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
  358. struct snd_soc_component *component =
  359. snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  360. int ret = 0, n;
  361. bool found = false;
  362. if (!component) {
  363. pr_err("%s: component is NULL\n", __func__);
  364. return -EINVAL;
  365. }
  366. mutex_lock(&pcm->lock);
  367. dev_dbg(component->dev, "%s: end pcm call:%d\n",
  368. __func__, substream->stream);
  369. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  370. pcm->playback_start = 0;
  371. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  372. pcm->capture_start = 0;
  373. pcm->instance--;
  374. if (!pcm->playback_start || !pcm->capture_start) {
  375. dev_dbg(component->dev, "%s: end pcm call\n", __func__);
  376. stop_pcm(pcm);
  377. }
  378. if (!pcm->instance) {
  379. mutex_lock(&loopback_session_lock);
  380. for (n = 0; n < LOOPBACK_SESSION_MAX; n++) {
  381. if (!strcmp(rtd->dai_link->stream_name,
  382. session_map[n].stream_name)) {
  383. found = true;
  384. break;
  385. }
  386. }
  387. if (found) {
  388. memset(session_map[n].stream_name, 0,
  389. sizeof(session_map[n].stream_name));
  390. mutex_unlock(&pcm->lock);
  391. mutex_destroy(&session_map[n].loopback_priv->lock);
  392. session_map[n].loopback_priv = NULL;
  393. kfree(pcm);
  394. dev_dbg(component->dev, "%s: stream freed %s\n",
  395. __func__, rtd->dai_link->stream_name);
  396. mutex_unlock(&loopback_session_lock);
  397. return 0;
  398. }
  399. mutex_unlock(&loopback_session_lock);
  400. }
  401. msm_adsp_clean_mixer_ctl_pp_event_queue(substream->private_data);
  402. mutex_unlock(&pcm->lock);
  403. return ret;
  404. }
  405. static int msm_pcm_prepare(struct snd_pcm_substream *substream)
  406. {
  407. int ret = 0;
  408. struct snd_pcm_runtime *runtime = substream->runtime;
  409. struct msm_pcm_loopback *pcm = runtime->private_data;
  410. struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
  411. struct snd_soc_component *component =
  412. snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  413. struct msm_pcm_routing_evt event;
  414. memset(&event, 0, sizeof(event));
  415. if (!component) {
  416. pr_err("%s: component is NULL\n", __func__);
  417. return -EINVAL;
  418. }
  419. mutex_lock(&pcm->lock);
  420. dev_dbg(component->dev, "%s: ASM loopback stream:%d\n",
  421. __func__, substream->stream);
  422. if (pcm->playback_start && pcm->capture_start) {
  423. mutex_unlock(&pcm->lock);
  424. return ret;
  425. }
  426. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  427. if (!pcm->playback_start)
  428. pcm->playback_start = 1;
  429. } else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  430. if (!pcm->capture_start)
  431. pcm->capture_start = 1;
  432. }
  433. if (pcm->playback_start && pcm->capture_start) {
  434. struct snd_soc_pcm_runtime *soc_pcm_rx =
  435. pcm->playback_substream->private_data;
  436. struct snd_soc_pcm_runtime *soc_pcm_tx =
  437. pcm->capture_substream->private_data;
  438. event.event_func = msm_pcm_route_event_handler;
  439. event.priv_data = (void *) pcm;
  440. if (!pcm->audio_client) {
  441. mutex_unlock(&pcm->lock);
  442. pr_err("%s: audio client freed\n", __func__);
  443. return -EINVAL;
  444. }
  445. msm_pcm_routing_reg_phy_stream(soc_pcm_tx->dai_link->id,
  446. pcm->audio_client->perf_mode,
  447. pcm->session_id, pcm->capture_substream->stream);
  448. msm_pcm_routing_reg_phy_stream_v2(soc_pcm_rx->dai_link->id,
  449. pcm->audio_client->perf_mode,
  450. pcm->session_id, pcm->playback_substream->stream,
  451. event);
  452. }
  453. mutex_unlock(&pcm->lock);
  454. return ret;
  455. }
  456. static int msm_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  457. {
  458. struct snd_pcm_runtime *runtime = substream->runtime;
  459. struct msm_pcm_loopback *pcm = runtime->private_data;
  460. struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
  461. struct snd_soc_component *component =
  462. snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  463. if (!component) {
  464. pr_err("%s: component is NULL\n", __func__);
  465. return -EINVAL;
  466. }
  467. switch (cmd) {
  468. case SNDRV_PCM_TRIGGER_START:
  469. case SNDRV_PCM_TRIGGER_RESUME:
  470. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  471. dev_dbg(component->dev,
  472. "%s: playback_start:%d,capture_start:%d\n", __func__,
  473. pcm->playback_start, pcm->capture_start);
  474. if (pcm->playback_start && pcm->capture_start)
  475. q6asm_run_nowait(pcm->audio_client, 0, 0, 0);
  476. break;
  477. case SNDRV_PCM_TRIGGER_SUSPEND:
  478. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  479. case SNDRV_PCM_TRIGGER_STOP:
  480. dev_dbg(component->dev,
  481. "%s:Pause/Stop - playback_start:%d,capture_start:%d\n",
  482. __func__, pcm->playback_start, pcm->capture_start);
  483. if (pcm->playback_start && pcm->capture_start)
  484. q6asm_cmd_nowait(pcm->audio_client, CMD_PAUSE);
  485. break;
  486. default:
  487. pr_err("%s: default cmd %d\n", __func__, cmd);
  488. break;
  489. }
  490. return 0;
  491. }
  492. static const struct snd_pcm_ops msm_pcm_ops = {
  493. .open = msm_pcm_open,
  494. .close = msm_pcm_close,
  495. .prepare = msm_pcm_prepare,
  496. .trigger = msm_pcm_trigger,
  497. };
  498. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  499. static int msm_pcm_volume_ctl_put(struct snd_kcontrol *kcontrol,
  500. struct snd_ctl_elem_value *ucontrol)
  501. {
  502. int rc = 0;
  503. struct snd_pcm_volume *vol = kcontrol->private_data;
  504. struct snd_pcm_substream *substream = vol->pcm->streams[0].substream;
  505. struct msm_pcm_loopback *prtd;
  506. int volume = ucontrol->value.integer.value[0];
  507. pr_debug("%s: volume : 0x%x\n", __func__, volume);
  508. if ((!substream) || (!substream->runtime)) {
  509. pr_err("%s substream or runtime not found\n", __func__);
  510. rc = -ENODEV;
  511. goto exit;
  512. }
  513. mutex_lock(&loopback_session_lock);
  514. if (substream->ref_count > 0) {
  515. prtd = substream->runtime->private_data;
  516. if (!prtd) {
  517. rc = -ENODEV;
  518. mutex_unlock(&loopback_session_lock);
  519. goto exit;
  520. }
  521. rc = pcm_loopback_set_volume(prtd, volume);
  522. }
  523. mutex_unlock(&loopback_session_lock);
  524. exit:
  525. return rc;
  526. }
  527. static int msm_pcm_volume_ctl_get(struct snd_kcontrol *kcontrol,
  528. struct snd_ctl_elem_value *ucontrol)
  529. {
  530. int rc = 0;
  531. struct snd_pcm_volume *vol = snd_kcontrol_chip(kcontrol);
  532. struct snd_pcm_substream *substream = NULL;
  533. struct msm_pcm_loopback *prtd;
  534. pr_debug("%s\n", __func__);
  535. if (!vol) {
  536. pr_err("%s: vol is NULL\n", __func__);
  537. return -ENODEV;
  538. }
  539. substream = vol->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
  540. if ((!substream) || (!substream->runtime)) {
  541. pr_debug("%s substream or runtime not found\n", __func__);
  542. rc = -ENODEV;
  543. goto exit;
  544. }
  545. mutex_lock(&loopback_session_lock);
  546. if (substream->ref_count > 0) {
  547. prtd = substream->runtime->private_data;
  548. if (!prtd) {
  549. rc = -ENODEV;
  550. mutex_unlock(&loopback_session_lock);
  551. goto exit;
  552. }
  553. ucontrol->value.integer.value[0] = prtd->volume;
  554. }
  555. mutex_unlock(&loopback_session_lock);
  556. exit:
  557. return rc;
  558. }
  559. static int msm_pcm_add_volume_controls(struct snd_soc_pcm_runtime *rtd)
  560. {
  561. struct snd_pcm *pcm = rtd->pcm->streams[0].pcm;
  562. struct snd_pcm_volume *volume_info;
  563. struct snd_kcontrol *kctl;
  564. int ret = 0;
  565. dev_dbg(rtd->dev, "%s, Volume cntrl add\n", __func__);
  566. ret = snd_pcm_add_volume_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  567. NULL, 1,
  568. rtd->dai_link->id,
  569. &volume_info);
  570. if (ret < 0)
  571. return ret;
  572. kctl = volume_info->kctl;
  573. kctl->put = msm_pcm_volume_ctl_put;
  574. kctl->get = msm_pcm_volume_ctl_get;
  575. kctl->tlv.p = loopback_rx_vol_gain;
  576. return 0;
  577. }
  578. static int msm_pcm_playback_app_type_cfg_ctl_put(struct snd_kcontrol *kcontrol,
  579. struct snd_ctl_elem_value *ucontrol)
  580. {
  581. u64 fe_id = kcontrol->private_value;
  582. int session_type = SESSION_TYPE_RX;
  583. int be_id = ucontrol->value.integer.value[3];
  584. struct msm_pcm_stream_app_type_cfg cfg_data = {0, 0, 48000, 0};
  585. int ret = 0;
  586. cfg_data.app_type = ucontrol->value.integer.value[0];
  587. cfg_data.acdb_dev_id = ucontrol->value.integer.value[1];
  588. if (ucontrol->value.integer.value[2] != 0)
  589. cfg_data.sample_rate = ucontrol->value.integer.value[2];
  590. if (ucontrol->value.integer.value[4] != 0)
  591. cfg_data.copp_token = ucontrol->value.integer.value[4];
  592. pr_debug("%s: fe_id- %llu session_type- %d be_id- %d app_type- %d acdb_dev_id- %d sample_rate- %d copp_token %d\n",
  593. __func__, fe_id, session_type, be_id,
  594. cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate,
  595. cfg_data.copp_token);
  596. ret = msm_pcm_routing_reg_stream_app_type_cfg(fe_id, session_type,
  597. be_id, &cfg_data);
  598. if (ret < 0)
  599. pr_err("%s: msm_pcm_routing_reg_stream_app_type_cfg failed returned %d\n",
  600. __func__, ret);
  601. return ret;
  602. }
  603. static int msm_pcm_playback_app_type_cfg_ctl_get(struct snd_kcontrol *kcontrol,
  604. struct snd_ctl_elem_value *ucontrol)
  605. {
  606. u64 fe_id = kcontrol->private_value;
  607. int session_type = SESSION_TYPE_RX;
  608. int be_id = 0;
  609. struct msm_pcm_stream_app_type_cfg cfg_data = {0};
  610. int ret = 0;
  611. ret = msm_pcm_routing_get_stream_app_type_cfg(fe_id, session_type,
  612. &be_id, &cfg_data);
  613. if (ret < 0) {
  614. pr_err("%s: msm_pcm_routing_get_stream_app_type_cfg failed returned %d\n",
  615. __func__, ret);
  616. goto done;
  617. }
  618. ucontrol->value.integer.value[0] = cfg_data.app_type;
  619. ucontrol->value.integer.value[1] = cfg_data.acdb_dev_id;
  620. ucontrol->value.integer.value[2] = cfg_data.sample_rate;
  621. ucontrol->value.integer.value[3] = be_id;
  622. ucontrol->value.integer.value[4] = cfg_data.copp_token;
  623. pr_debug("%s: fe_id- %llu session_type- %d be_id- %d app_type- %d acdb_dev_id- %d sample_rate- %d copp_token %d\n",
  624. __func__, fe_id, session_type, be_id,
  625. cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate,
  626. cfg_data.copp_token);
  627. done:
  628. return ret;
  629. }
  630. static int msm_pcm_capture_app_type_cfg_ctl_put(struct snd_kcontrol *kcontrol,
  631. struct snd_ctl_elem_value *ucontrol)
  632. {
  633. u64 fe_id = kcontrol->private_value;
  634. int session_type = SESSION_TYPE_TX;
  635. int be_id = ucontrol->value.integer.value[3];
  636. struct msm_pcm_stream_app_type_cfg cfg_data = {0, 0, 48000, 0};
  637. int ret = 0;
  638. cfg_data.app_type = ucontrol->value.integer.value[0];
  639. cfg_data.acdb_dev_id = ucontrol->value.integer.value[1];
  640. if (ucontrol->value.integer.value[2] != 0)
  641. cfg_data.sample_rate = ucontrol->value.integer.value[2];
  642. if (ucontrol->value.integer.value[4] != 0)
  643. cfg_data.copp_token = ucontrol->value.integer.value[4];
  644. pr_debug("%s: fe_id- %llu session_type- %d be_id- %d app_type- %d acdb_dev_id- %d sample_rate- %d copp_token %d\n",
  645. __func__, fe_id, session_type, be_id,
  646. cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate,
  647. cfg_data.copp_token);
  648. ret = msm_pcm_routing_reg_stream_app_type_cfg(fe_id, session_type,
  649. be_id, &cfg_data);
  650. if (ret < 0)
  651. pr_err("%s: msm_pcm_routing_reg_stream_app_type_cfg failed returned %d\n",
  652. __func__, ret);
  653. return ret;
  654. }
  655. static int msm_pcm_capture_app_type_cfg_ctl_get(struct snd_kcontrol *kcontrol,
  656. struct snd_ctl_elem_value *ucontrol)
  657. {
  658. u64 fe_id = kcontrol->private_value;
  659. int session_type = SESSION_TYPE_TX;
  660. int be_id = 0;
  661. struct msm_pcm_stream_app_type_cfg cfg_data = {0};
  662. int ret = 0;
  663. ret = msm_pcm_routing_get_stream_app_type_cfg(fe_id, session_type,
  664. &be_id, &cfg_data);
  665. if (ret < 0) {
  666. pr_err("%s: msm_pcm_routing_get_stream_app_type_cfg failed returned %d\n",
  667. __func__, ret);
  668. goto done;
  669. }
  670. ucontrol->value.integer.value[0] = cfg_data.app_type;
  671. ucontrol->value.integer.value[1] = cfg_data.acdb_dev_id;
  672. ucontrol->value.integer.value[2] = cfg_data.sample_rate;
  673. ucontrol->value.integer.value[3] = be_id;
  674. ucontrol->value.integer.value[4] = cfg_data.copp_token;
  675. pr_debug("%s: fe_id- %llu session_type- %d be_id- %d app_type- %d acdb_dev_id- %d sample_rate- %d copp_token %d\n",
  676. __func__, fe_id, session_type, be_id,
  677. cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate,
  678. cfg_data.copp_token);
  679. done:
  680. return ret;
  681. }
  682. static int msm_pcm_add_app_type_controls(struct snd_soc_pcm_runtime *rtd)
  683. {
  684. struct snd_pcm *pcm = rtd->pcm->streams[0].pcm;
  685. struct snd_pcm_usr *app_type_info;
  686. struct snd_kcontrol *kctl;
  687. const char *playback_mixer_ctl_name = "Audio Stream";
  688. const char *capture_mixer_ctl_name = "Audio Stream Capture";
  689. const char *deviceNo = "NN";
  690. const char *suffix = "App Type Cfg";
  691. int ctl_len, ret = 0;
  692. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
  693. ctl_len = strlen(playback_mixer_ctl_name) + 1 +
  694. strlen(deviceNo) + 1 + strlen(suffix) + 1;
  695. pr_debug("%s: Playback app type cntrl add\n", __func__);
  696. ret = snd_pcm_add_usr_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  697. NULL, 1, ctl_len, rtd->dai_link->id,
  698. &app_type_info);
  699. if (ret < 0)
  700. return ret;
  701. kctl = app_type_info->kctl;
  702. snprintf(kctl->id.name, ctl_len, "%s %d %s",
  703. playback_mixer_ctl_name, rtd->pcm->device, suffix);
  704. kctl->put = msm_pcm_playback_app_type_cfg_ctl_put;
  705. kctl->get = msm_pcm_playback_app_type_cfg_ctl_get;
  706. }
  707. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
  708. ctl_len = strlen(capture_mixer_ctl_name) + 1 +
  709. strlen(deviceNo) + 1 + strlen(suffix) + 1;
  710. pr_debug("%s: Capture app type cntrl add\n", __func__);
  711. ret = snd_pcm_add_usr_ctls(pcm, SNDRV_PCM_STREAM_CAPTURE,
  712. NULL, 1, ctl_len, rtd->dai_link->id,
  713. &app_type_info);
  714. if (ret < 0)
  715. return ret;
  716. kctl = app_type_info->kctl;
  717. snprintf(kctl->id.name, ctl_len, "%s %d %s",
  718. capture_mixer_ctl_name, rtd->pcm->device, suffix);
  719. kctl->put = msm_pcm_capture_app_type_cfg_ctl_put;
  720. kctl->get = msm_pcm_capture_app_type_cfg_ctl_get;
  721. }
  722. return 0;
  723. }
  724. #else
  725. static int msm_pcm_add_volume_controls(struct snd_soc_pcm_runtime *rtd)
  726. {
  727. return 0;
  728. }
  729. static int msm_pcm_add_app_type_controls(struct snd_soc_pcm_runtime *rtd)
  730. {
  731. return 0;
  732. }
  733. #endif /* CONFIG_AUDIO_QGKI */
  734. static struct msm_pcm_channel_mixer *msm_pcm_get_chmixer(
  735. struct msm_pcm_pdata *pdata,
  736. u64 fe_id, int session_type)
  737. {
  738. if (!pdata) {
  739. pr_err("%s: missing pdata\n", __func__);
  740. return NULL;
  741. }
  742. if (fe_id >= MSM_FRONTEND_DAI_MM_SIZE) {
  743. pr_err("%s: invalid FE %llu\n", __func__, fe_id);
  744. return NULL;
  745. }
  746. if ((session_type != SESSION_TYPE_TX) &&
  747. (session_type != SESSION_TYPE_RX)) {
  748. pr_err("%s: invalid session type %d\n", __func__, session_type);
  749. return NULL;
  750. }
  751. return pdata->chmixer_pspd[fe_id][session_type];
  752. }
  753. static int msm_pcm_channel_mixer_cfg_ctl_put(struct snd_kcontrol *kcontrol,
  754. struct snd_ctl_elem_value *ucontrol)
  755. {
  756. u64 fe_id = kcontrol->private_value & 0xFF;
  757. int session_type = (kcontrol->private_value >> 8) & 0xFF;
  758. int ret = 0;
  759. int stream_id = 0;
  760. int be_id = 0, i = 0;
  761. struct msm_pcm_loopback *prtd = NULL;
  762. struct snd_soc_component *component =
  763. snd_soc_kcontrol_component(kcontrol);
  764. struct msm_pcm_pdata *pdata = dev_get_drvdata(component->dev);
  765. struct snd_pcm *pcm = NULL;
  766. struct snd_pcm_substream *substream = NULL;
  767. struct msm_pcm_channel_mixer *chmixer_pspd = NULL;
  768. u8 asm_ch_map[PCM_FORMAT_MAX_NUM_CHANNEL_V8] = {0};
  769. bool reset_override_out_ch_map = false;
  770. bool reset_override_in_ch_map = false;
  771. pcm = pdata->pcm_device[fe_id];
  772. if (!pcm) {
  773. pr_err("%s invalid pcm handle for fe_id %llu\n",
  774. __func__, fe_id);
  775. return -EINVAL;
  776. }
  777. if (session_type == SESSION_TYPE_RX)
  778. substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
  779. else
  780. substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
  781. if (!substream) {
  782. pr_err("%s substream not found\n", __func__);
  783. return -EINVAL;
  784. }
  785. chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
  786. if (!chmixer_pspd) {
  787. pr_err("%s: invalid chmixer_pspd in pdata", __func__);
  788. return -EINVAL;
  789. }
  790. chmixer_pspd->enable = ucontrol->value.integer.value[0];
  791. chmixer_pspd->rule = ucontrol->value.integer.value[1];
  792. chmixer_pspd->input_channel = ucontrol->value.integer.value[2];
  793. chmixer_pspd->output_channel = ucontrol->value.integer.value[3];
  794. chmixer_pspd->port_idx = ucontrol->value.integer.value[4];
  795. if (chmixer_pspd->enable) {
  796. if (session_type == SESSION_TYPE_RX &&
  797. !chmixer_pspd->override_in_ch_map) {
  798. if (chmixer_pspd->input_channel > PCM_FORMAT_MAX_NUM_CHANNEL_V8) {
  799. pr_err("%s: Invalid channel count %d\n",
  800. __func__, chmixer_pspd->input_channel);
  801. return -EINVAL;
  802. }
  803. q6asm_map_channels(asm_ch_map,
  804. chmixer_pspd->input_channel, false);
  805. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  806. chmixer_pspd->in_ch_map[i] = asm_ch_map[i];
  807. chmixer_pspd->override_in_ch_map = true;
  808. reset_override_in_ch_map = true;
  809. } else if (session_type == SESSION_TYPE_TX &&
  810. !chmixer_pspd->override_out_ch_map) {
  811. if (chmixer_pspd->output_channel > PCM_FORMAT_MAX_NUM_CHANNEL_V8) {
  812. pr_err("%s: Invalid channel count %d\n",
  813. __func__, chmixer_pspd->output_channel);
  814. return -EINVAL;
  815. }
  816. q6asm_map_channels(asm_ch_map,
  817. chmixer_pspd->output_channel, false);
  818. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  819. chmixer_pspd->out_ch_map[i] = asm_ch_map[i];
  820. chmixer_pspd->override_out_ch_map = true;
  821. reset_override_out_ch_map = true;
  822. }
  823. } else {
  824. chmixer_pspd->override_out_ch_map = false;
  825. chmixer_pspd->override_in_ch_map = false;
  826. }
  827. /* cache value and take effect during adm_open stage */
  828. msm_pcm_routing_set_channel_mixer_cfg(fe_id,
  829. session_type,
  830. chmixer_pspd);
  831. mutex_lock(&loopback_session_lock);
  832. if (substream->ref_count <= 0) {
  833. pr_err_ratelimited("%s: substream ref_count:%d invalid\n",
  834. __func__, substream->ref_count);
  835. mutex_unlock(&loopback_session_lock);
  836. return -EINVAL;
  837. }
  838. if (chmixer_pspd->enable && substream->runtime) {
  839. prtd = substream->runtime->private_data;
  840. if (!prtd) {
  841. pr_err("%s find invalid prtd fail\n", __func__);
  842. ret = -EINVAL;
  843. mutex_unlock(&loopback_session_lock);
  844. goto done;
  845. }
  846. if (prtd->audio_client) {
  847. stream_id = prtd->audio_client->session;
  848. be_id = chmixer_pspd->port_idx;
  849. msm_pcm_routing_set_channel_mixer_runtime(be_id,
  850. stream_id,
  851. session_type,
  852. chmixer_pspd);
  853. }
  854. }
  855. mutex_unlock(&loopback_session_lock);
  856. if (reset_override_out_ch_map)
  857. chmixer_pspd->override_out_ch_map = false;
  858. if (reset_override_in_ch_map)
  859. chmixer_pspd->override_in_ch_map = false;
  860. done:
  861. return ret;
  862. }
  863. static int msm_pcm_channel_mixer_cfg_ctl_get(struct snd_kcontrol *kcontrol,
  864. struct snd_ctl_elem_value *ucontrol)
  865. {
  866. u64 fe_id = kcontrol->private_value & 0xFF;
  867. int session_type = (kcontrol->private_value >> 8) & 0xFF;
  868. struct snd_soc_component *component =
  869. snd_soc_kcontrol_component(kcontrol);
  870. struct msm_pcm_pdata *pdata = dev_get_drvdata(component->dev);
  871. struct msm_pcm_channel_mixer *chmixer_pspd;
  872. chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
  873. if (!chmixer_pspd) {
  874. pr_err("%s: invalid chmixer_pspd in pdata", __func__);
  875. return -EINVAL;
  876. }
  877. ucontrol->value.integer.value[0] = chmixer_pspd->enable;
  878. ucontrol->value.integer.value[1] = chmixer_pspd->rule;
  879. ucontrol->value.integer.value[2] = chmixer_pspd->input_channel;
  880. ucontrol->value.integer.value[3] = chmixer_pspd->output_channel;
  881. ucontrol->value.integer.value[4] = chmixer_pspd->port_idx;
  882. return 0;
  883. }
  884. static int msm_pcm_channel_mixer_output_map_ctl_put(
  885. struct snd_kcontrol *kcontrol,
  886. struct snd_ctl_elem_value *ucontrol)
  887. {
  888. u64 fe_id = kcontrol->private_value & 0xFF;
  889. int session_type = (kcontrol->private_value >> 8) & 0xFF;
  890. int i = 0;
  891. struct snd_soc_component *component =
  892. snd_soc_kcontrol_component(kcontrol);
  893. struct msm_pcm_pdata *pdata = dev_get_drvdata(component->dev);
  894. struct msm_pcm_channel_mixer *chmixer_pspd;
  895. chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
  896. if (!chmixer_pspd) {
  897. pr_err("%s: invalid chmixer_pspd in pdata", __func__);
  898. return -EINVAL;
  899. }
  900. chmixer_pspd->override_out_ch_map = true;
  901. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  902. chmixer_pspd->out_ch_map[i] =
  903. ucontrol->value.integer.value[i];
  904. return 0;
  905. }
  906. static int msm_pcm_channel_mixer_output_map_ctl_get(
  907. struct snd_kcontrol *kcontrol,
  908. struct snd_ctl_elem_value *ucontrol)
  909. {
  910. u64 fe_id = kcontrol->private_value & 0xFF;
  911. int session_type = (kcontrol->private_value >> 8) & 0xFF;
  912. int i = 0;
  913. struct snd_soc_component *component =
  914. snd_soc_kcontrol_component(kcontrol);
  915. struct msm_pcm_pdata *pdata = dev_get_drvdata(component->dev);
  916. struct msm_pcm_channel_mixer *chmixer_pspd;
  917. chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
  918. if (!chmixer_pspd) {
  919. pr_err("%s: invalid chmixer_pspd in pdata", __func__);
  920. return -EINVAL;
  921. }
  922. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  923. ucontrol->value.integer.value[i] =
  924. chmixer_pspd->out_ch_map[i];
  925. return 0;
  926. }
  927. static int msm_pcm_channel_mixer_input_map_ctl_put(
  928. struct snd_kcontrol *kcontrol,
  929. struct snd_ctl_elem_value *ucontrol)
  930. {
  931. u64 fe_id = kcontrol->private_value & 0xFF;
  932. int session_type = (kcontrol->private_value >> 8) & 0xFF;
  933. int i = 0;
  934. struct snd_soc_component *component =
  935. snd_soc_kcontrol_component(kcontrol);
  936. struct msm_pcm_pdata *pdata = dev_get_drvdata(component->dev);
  937. struct msm_pcm_channel_mixer *chmixer_pspd;
  938. chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
  939. if (!chmixer_pspd) {
  940. pr_err("%s: invalid chmixer_pspd in pdata", __func__);
  941. return -EINVAL;
  942. }
  943. chmixer_pspd->override_in_ch_map = true;
  944. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  945. chmixer_pspd->in_ch_map[i] = ucontrol->value.integer.value[i];
  946. return 0;
  947. }
  948. static int msm_pcm_channel_mixer_input_map_ctl_get(
  949. struct snd_kcontrol *kcontrol,
  950. struct snd_ctl_elem_value *ucontrol)
  951. {
  952. u64 fe_id = kcontrol->private_value & 0xFF;
  953. int session_type = (kcontrol->private_value >> 8) & 0xFF;
  954. int i = 0;
  955. struct snd_soc_component *component =
  956. snd_soc_kcontrol_component(kcontrol);
  957. struct msm_pcm_pdata *pdata = dev_get_drvdata(component->dev);
  958. struct msm_pcm_channel_mixer *chmixer_pspd;
  959. chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
  960. if (!chmixer_pspd) {
  961. pr_err("%s: invalid chmixer_pspd in pdata", __func__);
  962. return -EINVAL;
  963. }
  964. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  965. ucontrol->value.integer.value[i] =
  966. chmixer_pspd->in_ch_map[i];
  967. return 0;
  968. }
  969. static int msm_pcm_channel_mixer_weight_ctl_put(
  970. struct snd_kcontrol *kcontrol,
  971. struct snd_ctl_elem_value *ucontrol)
  972. {
  973. u64 fe_id = kcontrol->private_value & 0xFF;
  974. int session_type = (kcontrol->private_value >> 8) & 0xFF;
  975. int channel = (kcontrol->private_value >> 16) & 0xFF;
  976. int i = 0;
  977. struct snd_soc_component *component =
  978. snd_soc_kcontrol_component(kcontrol);
  979. struct msm_pcm_pdata *pdata = dev_get_drvdata(component->dev);
  980. struct msm_pcm_channel_mixer *chmixer_pspd;
  981. chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
  982. if (!chmixer_pspd) {
  983. pr_err("%s: invalid chmixer_pspd in pdata", __func__);
  984. return -EINVAL;
  985. }
  986. if (channel <= 0 || channel > PCM_FORMAT_MAX_NUM_CHANNEL_V8) {
  987. pr_err("%s: invalid channel number %d\n", __func__, channel);
  988. return -EINVAL;
  989. }
  990. channel--;
  991. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  992. chmixer_pspd->channel_weight[channel][i] =
  993. ucontrol->value.integer.value[i];
  994. return 0;
  995. }
  996. static int msm_pcm_channel_mixer_weight_ctl_get(
  997. struct snd_kcontrol *kcontrol,
  998. struct snd_ctl_elem_value *ucontrol)
  999. {
  1000. u64 fe_id = kcontrol->private_value & 0xFF;
  1001. int session_type = (kcontrol->private_value >> 8) & 0xFF;
  1002. int channel = (kcontrol->private_value >> 16) & 0xFF;
  1003. struct snd_soc_component *component =
  1004. snd_soc_kcontrol_component(kcontrol);
  1005. struct msm_pcm_pdata *pdata = dev_get_drvdata(component->dev);
  1006. int i = 0;
  1007. struct msm_pcm_channel_mixer *chmixer_pspd;
  1008. if (channel <= 0 || channel > PCM_FORMAT_MAX_NUM_CHANNEL_V8) {
  1009. pr_err("%s: invalid channel number %d\n", __func__, channel);
  1010. return -EINVAL;
  1011. }
  1012. channel--;
  1013. chmixer_pspd = msm_pcm_get_chmixer(pdata, fe_id, session_type);
  1014. if (!chmixer_pspd) {
  1015. pr_err("%s: invalid chmixer_pspd in pdata", __func__);
  1016. return -EINVAL;
  1017. }
  1018. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++)
  1019. ucontrol->value.integer.value[i] =
  1020. chmixer_pspd->channel_weight[channel][i];
  1021. return 0;
  1022. }
  1023. static int msm_pcm_add_platform_controls(struct snd_kcontrol_new *kctl,
  1024. struct snd_soc_pcm_runtime *rtd, const char *name_prefix,
  1025. const char *name_suffix, int session_type, int channels)
  1026. {
  1027. int ret = -EINVAL;
  1028. char *mixer_name = NULL;
  1029. struct snd_pcm *pcm = rtd->pcm;
  1030. const char *deviceNo = "NN";
  1031. const char *channelNo = "NN";
  1032. int ctl_len = 0;
  1033. struct snd_soc_component *component = NULL;
  1034. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  1035. if (!component) {
  1036. pr_err("%s: component is NULL\n", __func__);
  1037. return -EINVAL;
  1038. }
  1039. ctl_len = strlen(name_prefix) + 1 + strlen(deviceNo) + 1 +
  1040. strlen(channelNo) + 1 + strlen(name_suffix) + 1;
  1041. mixer_name = kzalloc(ctl_len, GFP_KERNEL);
  1042. if (mixer_name == NULL)
  1043. return -ENOMEM;
  1044. if (channels >= 0) {
  1045. snprintf(mixer_name, ctl_len, "%s %d %s %d",
  1046. name_prefix, pcm->device, name_suffix, channels);
  1047. kctl->private_value = (rtd->dai_link->id) | (session_type << 8) |
  1048. (channels << 16);
  1049. } else {
  1050. snprintf(mixer_name, ctl_len, "%s %d %s",
  1051. name_prefix, pcm->device, name_suffix);
  1052. kctl->private_value = (rtd->dai_link->id) | (session_type << 8);
  1053. }
  1054. kctl->name = mixer_name;
  1055. ret = snd_soc_add_component_controls(component, kctl, 1);
  1056. kfree(mixer_name);
  1057. return ret;
  1058. }
  1059. static int msm_pcm_channel_mixer_output_map_info(struct snd_kcontrol *kcontrol,
  1060. struct snd_ctl_elem_info *uinfo)
  1061. {
  1062. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1063. uinfo->count = PCM_FORMAT_MAX_NUM_CHANNEL_V8;
  1064. /* Valid channel map value ranges from 1 to 64 */
  1065. uinfo->value.integer.min = 1;
  1066. uinfo->value.integer.max = 64;
  1067. return 0;
  1068. }
  1069. static int msm_pcm_add_channel_mixer_output_map_controls(
  1070. struct snd_soc_pcm_runtime *rtd)
  1071. {
  1072. struct snd_pcm *pcm = rtd->pcm;
  1073. const char *playback_mixer_ctl_name = "AudStr";
  1074. const char *capture_mixer_ctl_name = "AudStr Capture";
  1075. const char *suffix = "ChMixer Output Map";
  1076. int session_type = 0, ret = 0, channel = -1;
  1077. struct snd_kcontrol_new channel_mixer_output_map_control = {
  1078. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1079. .name = "?",
  1080. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  1081. .info = msm_pcm_channel_mixer_output_map_info,
  1082. .put = msm_pcm_channel_mixer_output_map_ctl_put,
  1083. .get = msm_pcm_channel_mixer_output_map_ctl_get,
  1084. .private_value = 0,
  1085. };
  1086. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream != NULL) {
  1087. session_type = SESSION_TYPE_RX;
  1088. ret = msm_pcm_add_platform_controls(&channel_mixer_output_map_control,
  1089. rtd, playback_mixer_ctl_name, suffix, session_type, channel);
  1090. if (ret < 0)
  1091. goto fail;
  1092. }
  1093. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream != NULL) {
  1094. session_type = SESSION_TYPE_TX;
  1095. ret = msm_pcm_add_platform_controls(&channel_mixer_output_map_control,
  1096. rtd, capture_mixer_ctl_name, suffix, session_type, channel);
  1097. if (ret < 0)
  1098. goto fail;
  1099. }
  1100. return 0;
  1101. fail:
  1102. pr_err("%s: failed add platform ctl, err = %d\n",
  1103. __func__, ret);
  1104. return ret;
  1105. }
  1106. static int msm_pcm_channel_mixer_input_map_info(struct snd_kcontrol *kcontrol,
  1107. struct snd_ctl_elem_info *uinfo)
  1108. {
  1109. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1110. uinfo->count = PCM_FORMAT_MAX_NUM_CHANNEL_V8;
  1111. /* Valid channel map value ranges from 1 to 64 */
  1112. uinfo->value.integer.min = 1;
  1113. uinfo->value.integer.max = 64;
  1114. return 0;
  1115. }
  1116. static int msm_pcm_add_channel_mixer_input_map_controls(
  1117. struct snd_soc_pcm_runtime *rtd)
  1118. {
  1119. struct snd_pcm *pcm = rtd->pcm;
  1120. const char *playback_mixer_ctl_name = "AudStr";
  1121. const char *capture_mixer_ctl_name = "AudStr Capture";
  1122. const char *suffix = "ChMixer Input Map";
  1123. int session_type = 0, ret = 0, channel = -1;
  1124. struct snd_kcontrol_new channel_mixer_input_map_control = {
  1125. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1126. .name = "?",
  1127. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  1128. .info = msm_pcm_channel_mixer_input_map_info,
  1129. .put = msm_pcm_channel_mixer_input_map_ctl_put,
  1130. .get = msm_pcm_channel_mixer_input_map_ctl_get,
  1131. .private_value = 0,
  1132. };
  1133. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream != NULL) {
  1134. session_type = SESSION_TYPE_RX;
  1135. ret = msm_pcm_add_platform_controls(&channel_mixer_input_map_control,
  1136. rtd, playback_mixer_ctl_name, suffix, session_type, channel);
  1137. if (ret < 0)
  1138. goto fail;
  1139. }
  1140. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream != NULL) {
  1141. session_type = SESSION_TYPE_TX;
  1142. ret = msm_pcm_add_platform_controls(&channel_mixer_input_map_control,
  1143. rtd, capture_mixer_ctl_name, suffix, session_type, channel);
  1144. if (ret < 0)
  1145. goto fail;
  1146. }
  1147. return 0;
  1148. fail:
  1149. pr_err("%s: failed add platform ctl, err = %d\n",
  1150. __func__, ret);
  1151. return ret;
  1152. }
  1153. static int msm_pcm_channel_mixer_cfg_info(struct snd_kcontrol *kcontrol,
  1154. struct snd_ctl_elem_info *uinfo)
  1155. {
  1156. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1157. /* five int values: enable, rule, in_channels, out_channels and port_id */
  1158. uinfo->count = 5;
  1159. /* Valid range is all positive values to support above controls */
  1160. uinfo->value.integer.min = 0;
  1161. uinfo->value.integer.max = INT_MAX;
  1162. return 0;
  1163. }
  1164. static int msm_pcm_add_channel_mixer_cfg_controls(
  1165. struct snd_soc_pcm_runtime *rtd)
  1166. {
  1167. struct snd_pcm *pcm = rtd->pcm;
  1168. const char *playback_mixer_ctl_name = "AudStr";
  1169. const char *capture_mixer_ctl_name = "AudStr Capture";
  1170. const char *suffix = "ChMixer Cfg";
  1171. int session_type = 0, ret = 0, channel = -1;
  1172. struct msm_pcm_pdata *pdata = NULL;
  1173. struct snd_soc_component *component = NULL;
  1174. struct snd_kcontrol_new channel_mixer_cfg_control = {
  1175. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1176. .name = "?",
  1177. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  1178. .info = msm_pcm_channel_mixer_cfg_info,
  1179. .put = msm_pcm_channel_mixer_cfg_ctl_put,
  1180. .get = msm_pcm_channel_mixer_cfg_ctl_get,
  1181. .private_value = 0,
  1182. };
  1183. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  1184. if (!component) {
  1185. pr_err("%s: component is NULL\n", __func__);
  1186. return -EINVAL;
  1187. }
  1188. pdata = (struct msm_pcm_pdata *)
  1189. dev_get_drvdata(component->dev);
  1190. if (pdata == NULL) {
  1191. pr_err("%s: platform data not populated\n", __func__);
  1192. return -EINVAL;
  1193. }
  1194. pdata->pcm_device[rtd->dai_link->id] = rtd->pcm;
  1195. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream != NULL) {
  1196. session_type = SESSION_TYPE_RX;
  1197. ret = msm_pcm_add_platform_controls(&channel_mixer_cfg_control,
  1198. rtd, playback_mixer_ctl_name, suffix, session_type, channel);
  1199. if (ret < 0)
  1200. goto fail;
  1201. }
  1202. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream != NULL) {
  1203. session_type = SESSION_TYPE_TX;
  1204. ret = msm_pcm_add_platform_controls(&channel_mixer_cfg_control,
  1205. rtd, capture_mixer_ctl_name, suffix, session_type, channel);
  1206. if (ret < 0)
  1207. goto fail;
  1208. }
  1209. return 0;
  1210. fail:
  1211. pr_err("%s: failed add platform ctl, err = %d\n",
  1212. __func__, ret);
  1213. return ret;
  1214. }
  1215. static int msm_pcm_channel_mixer_weight_info(struct snd_kcontrol *kcontrol,
  1216. struct snd_ctl_elem_info *uinfo)
  1217. {
  1218. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1219. uinfo->count = PCM_FORMAT_MAX_NUM_CHANNEL_V8;
  1220. /* Valid range: 0 to 0x4000(Unity) gain weightage */
  1221. uinfo->value.integer.min = 0;
  1222. uinfo->value.integer.max = 0x4000;
  1223. return 0;
  1224. }
  1225. static int msm_pcm_add_channel_mixer_weight_controls(
  1226. struct snd_soc_pcm_runtime *rtd,
  1227. int channel)
  1228. {
  1229. struct snd_pcm *pcm = rtd->pcm;
  1230. const char *playback_mixer_ctl_name = "AudStr";
  1231. const char *capture_mixer_ctl_name = "AudStr Capture";
  1232. const char *suffix = "ChMixer Weight Ch";
  1233. int session_type = 0, ret = 0;
  1234. struct snd_kcontrol_new channel_mixer_weight_control = {
  1235. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1236. .name = "?",
  1237. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  1238. .info = msm_pcm_channel_mixer_weight_info,
  1239. .put = msm_pcm_channel_mixer_weight_ctl_put,
  1240. .get = msm_pcm_channel_mixer_weight_ctl_get,
  1241. .private_value = 0,
  1242. };
  1243. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream != NULL) {
  1244. session_type = SESSION_TYPE_RX;
  1245. ret = msm_pcm_add_platform_controls(&channel_mixer_weight_control,
  1246. rtd, playback_mixer_ctl_name, suffix, session_type, channel);
  1247. if (ret < 0)
  1248. goto fail;
  1249. }
  1250. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream != NULL) {
  1251. session_type = SESSION_TYPE_TX;
  1252. ret = msm_pcm_add_platform_controls(&channel_mixer_weight_control,
  1253. rtd, capture_mixer_ctl_name, suffix, session_type, channel);
  1254. if (ret < 0)
  1255. goto fail;
  1256. }
  1257. return 0;
  1258. fail:
  1259. pr_err("%s: failed add platform ctl, err = %d\n",
  1260. __func__, ret);
  1261. return ret;
  1262. }
  1263. static int msm_pcm_add_channel_mixer_controls(struct snd_soc_pcm_runtime *rtd)
  1264. {
  1265. int i, ret = 0;
  1266. struct snd_pcm *pcm = NULL;
  1267. struct msm_pcm_pdata *pdata = NULL;
  1268. struct snd_soc_component *component = NULL;
  1269. if (!rtd || !rtd->pcm) {
  1270. pr_err("%s invalid rtd or pcm\n", __func__);
  1271. return -EINVAL;
  1272. }
  1273. pcm = rtd->pcm;
  1274. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  1275. if (!component) {
  1276. pr_err("%s: component is NULL\n", __func__);
  1277. return -EINVAL;
  1278. }
  1279. pdata = (struct msm_pcm_pdata *)
  1280. dev_get_drvdata(component->dev);
  1281. if (!pdata) {
  1282. pr_err("%s: platform data not populated\n", __func__);
  1283. return -EINVAL;
  1284. }
  1285. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream &&
  1286. !pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_RX]) {
  1287. pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_RX] =
  1288. kzalloc(sizeof(struct msm_pcm_channel_mixer), GFP_KERNEL);
  1289. if (!pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_RX]) {
  1290. ret = -ENOMEM;
  1291. goto fail;
  1292. }
  1293. }
  1294. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream &&
  1295. !pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_TX]) {
  1296. pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_TX] =
  1297. kzalloc(sizeof(struct msm_pcm_channel_mixer), GFP_KERNEL);
  1298. if (!pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_TX]) {
  1299. ret = -ENOMEM;
  1300. goto fail;
  1301. }
  1302. }
  1303. ret = msm_pcm_add_channel_mixer_cfg_controls(rtd);
  1304. if (ret) {
  1305. pr_err("%s: pcm add channel mixer cfg controls failed:%d\n",
  1306. __func__, ret);
  1307. goto fail;
  1308. }
  1309. ret = msm_pcm_add_channel_mixer_input_map_controls(rtd);
  1310. if (ret) {
  1311. pr_err("%s: pcm add channel mixer input map controls failed:%d\n",
  1312. __func__, ret);
  1313. goto fail;
  1314. }
  1315. ret = msm_pcm_add_channel_mixer_output_map_controls(rtd);
  1316. if (ret) {
  1317. pr_err("%s: pcm add channel mixer output map controls failed:%d\n",
  1318. __func__, ret);
  1319. goto fail;
  1320. }
  1321. for (i = 1; i <= PCM_FORMAT_MAX_NUM_CHANNEL_V8; i++) {
  1322. ret = msm_pcm_add_channel_mixer_weight_controls(rtd, i);
  1323. if (ret) {
  1324. pr_err("%s: pcm add channel mixer weight controls failed:%d\n",
  1325. __func__, ret);
  1326. goto fail;
  1327. }
  1328. }
  1329. return 0;
  1330. fail:
  1331. kfree(pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_RX]);
  1332. kfree(pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_TX]);
  1333. pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_RX] = NULL;
  1334. pdata->chmixer_pspd[rtd->dai_link->id][SESSION_TYPE_TX] = NULL;
  1335. return ret;
  1336. }
  1337. static int msm_loopback_adsp_stream_cmd_put(struct snd_kcontrol *kcontrol,
  1338. struct snd_ctl_elem_value *ucontrol)
  1339. {
  1340. int ret = 0;
  1341. struct msm_adsp_event_data *event_data = NULL;
  1342. uint64_t actual_payload_len = 0;
  1343. struct audio_client *audio_client = NULL;
  1344. struct msm_pcm_routing_fdai_data fe_dai;
  1345. unsigned long fe_id = kcontrol->private_value;
  1346. if (fe_id >= MSM_FRONTEND_DAI_MAX) {
  1347. pr_err("%s Received invalid fe_id %lu\n", __func__, fe_id);
  1348. ret = -EINVAL;
  1349. goto done;
  1350. }
  1351. msm_pcm_routing_get_fedai_info(fe_id, SESSION_TYPE_RX, &fe_dai);
  1352. audio_client = q6asm_get_audio_client(fe_dai.strm_id);
  1353. event_data = (struct msm_adsp_event_data *)ucontrol->value.bytes.data;
  1354. if ((event_data->event_type < ADSP_STREAM_PP_EVENT) ||
  1355. (event_data->event_type >= ADSP_STREAM_EVENT_MAX)) {
  1356. pr_err("%s: invalid event_type=%d\n",
  1357. __func__, event_data->event_type);
  1358. ret = -EINVAL;
  1359. goto done;
  1360. }
  1361. actual_payload_len = sizeof(struct msm_adsp_event_data) +
  1362. event_data->payload_len;
  1363. if (actual_payload_len >= U32_MAX) {
  1364. pr_err("%s payload length 0x%X exceeds limit\n",
  1365. __func__, event_data->payload_len);
  1366. ret = -EINVAL;
  1367. goto done;
  1368. }
  1369. if (event_data->payload_len > sizeof(ucontrol->value.bytes.data)
  1370. - sizeof(struct msm_adsp_event_data)) {
  1371. pr_err("%s param length=%d exceeds limit\n",
  1372. __func__, event_data->payload_len);
  1373. ret = -EINVAL;
  1374. goto done;
  1375. }
  1376. ret = q6asm_send_stream_cmd(audio_client, event_data);
  1377. if (ret < 0)
  1378. pr_err("%s: failed to send stream event cmd, err = %d\n",
  1379. __func__, ret);
  1380. done:
  1381. return ret;
  1382. }
  1383. static int msm_pcm_add_audio_adsp_stream_cmd_control(
  1384. struct snd_soc_pcm_runtime *rtd)
  1385. {
  1386. const char *mixer_ctl_name = DSP_STREAM_CMD;
  1387. const char *deviceNo = "NN";
  1388. char *mixer_str = NULL;
  1389. int ctl_len = 0, ret = 0;
  1390. struct snd_soc_component *component = NULL;
  1391. struct snd_kcontrol_new fe_audio_adsp_stream_cmd_config_control[1] = {
  1392. {
  1393. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1394. .name = "?",
  1395. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  1396. .info = msm_adsp_stream_cmd_info,
  1397. .put = msm_loopback_adsp_stream_cmd_put,
  1398. .private_value = 0,
  1399. }
  1400. };
  1401. if (!rtd) {
  1402. pr_err("%s rtd is NULL\n", __func__);
  1403. ret = -EINVAL;
  1404. goto done;
  1405. }
  1406. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  1407. if (!component) {
  1408. pr_err("%s: component is NULL\n", __func__);
  1409. return -EINVAL;
  1410. }
  1411. ctl_len = strlen(mixer_ctl_name) + 1 + strlen(deviceNo) + 1;
  1412. mixer_str = kzalloc(ctl_len, GFP_KERNEL);
  1413. if (!mixer_str) {
  1414. ret = -ENOMEM;
  1415. goto done;
  1416. }
  1417. snprintf(mixer_str, ctl_len, "%s %d", mixer_ctl_name, rtd->pcm->device);
  1418. fe_audio_adsp_stream_cmd_config_control[0].name = mixer_str;
  1419. fe_audio_adsp_stream_cmd_config_control[0].private_value =
  1420. rtd->dai_link->id;
  1421. pr_debug("%s Registering new mixer ctl %s\n", __func__, mixer_str);
  1422. ret = snd_soc_add_component_controls(component,
  1423. fe_audio_adsp_stream_cmd_config_control,
  1424. ARRAY_SIZE(fe_audio_adsp_stream_cmd_config_control));
  1425. if (ret < 0)
  1426. pr_err("%s: failed add ctl %s. err = %d\n",
  1427. __func__, mixer_str, ret);
  1428. kfree(mixer_str);
  1429. done:
  1430. return ret;
  1431. }
  1432. static int msm_pcm_add_audio_adsp_stream_callback_control(
  1433. struct snd_soc_pcm_runtime *rtd)
  1434. {
  1435. const char *mixer_ctl_name = DSP_STREAM_CALLBACK;
  1436. const char *deviceNo = "NN";
  1437. char *mixer_str = NULL;
  1438. int ctl_len = 0, ret = 0;
  1439. struct snd_kcontrol *kctl;
  1440. struct snd_soc_component *component = NULL;
  1441. struct snd_kcontrol_new fe_audio_adsp_callback_config_control[1] = {
  1442. {
  1443. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1444. .name = "?",
  1445. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  1446. .info = msm_adsp_stream_callback_info,
  1447. .get = msm_adsp_stream_callback_get,
  1448. .private_value = 0,
  1449. }
  1450. };
  1451. if (!rtd) {
  1452. pr_err("%s NULL rtd\n", __func__);
  1453. ret = -EINVAL;
  1454. goto done;
  1455. }
  1456. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  1457. if (!component) {
  1458. pr_err("%s: component is NULL\n", __func__);
  1459. return -EINVAL;
  1460. }
  1461. pr_debug("%s: added new pcm FE with name %s, id %d, device no %d\n",
  1462. __func__, rtd->dai_link->name, rtd->dai_link->id, rtd->pcm->device);
  1463. ctl_len = strlen(mixer_ctl_name) + 1 + strlen(deviceNo) + 1;
  1464. mixer_str = kzalloc(ctl_len, GFP_KERNEL);
  1465. if (!mixer_str) {
  1466. ret = -ENOMEM;
  1467. goto done;
  1468. }
  1469. snprintf(mixer_str, ctl_len, "%s %d", mixer_ctl_name, rtd->pcm->device);
  1470. fe_audio_adsp_callback_config_control[0].name = mixer_str;
  1471. fe_audio_adsp_callback_config_control[0].private_value =
  1472. rtd->dai_link->id;
  1473. pr_debug("%s: Registering new mixer ctl %s\n", __func__, mixer_str);
  1474. ret = snd_soc_add_component_controls(component,
  1475. fe_audio_adsp_callback_config_control,
  1476. ARRAY_SIZE(fe_audio_adsp_callback_config_control));
  1477. if (ret < 0) {
  1478. pr_err("%s: failed to add ctl %s. err = %d\n",
  1479. __func__, mixer_str, ret);
  1480. ret = -EINVAL;
  1481. goto free_mixer_str;
  1482. }
  1483. kctl = snd_soc_card_get_kcontrol(rtd->card, mixer_str);
  1484. if (!kctl) {
  1485. pr_err("%s: failed to get kctl %s.\n", __func__, mixer_str);
  1486. ret = -EINVAL;
  1487. goto free_mixer_str;
  1488. }
  1489. kctl->private_data = NULL;
  1490. free_mixer_str:
  1491. kfree(mixer_str);
  1492. done:
  1493. return ret;
  1494. }
  1495. static int msm_pcm_add_controls(struct snd_soc_pcm_runtime *rtd)
  1496. {
  1497. int ret = 0;
  1498. pr_debug("%s\n", __func__);
  1499. ret = msm_pcm_add_volume_controls(rtd);
  1500. if (ret)
  1501. pr_err("%s: pcm add volume controls failed:%d\n",
  1502. __func__, ret);
  1503. ret = msm_pcm_add_app_type_controls(rtd);
  1504. if (ret)
  1505. pr_err("%s: pcm add app type controls failed:%d\n",
  1506. __func__, ret);
  1507. ret = msm_pcm_add_channel_mixer_controls(rtd);
  1508. if (ret)
  1509. pr_err("%s: pcm add channel mixer controls failed:%d\n",
  1510. __func__, ret);
  1511. ret = msm_pcm_add_audio_adsp_stream_cmd_control(rtd);
  1512. if (ret)
  1513. pr_err("%s: Could not add pcm ADSP Stream Cmd Control\n",
  1514. __func__);
  1515. ret = msm_pcm_add_audio_adsp_stream_callback_control(rtd);
  1516. if (ret)
  1517. pr_err("%s: Could not add pcm ADSP Stream Callback Control\n",
  1518. __func__);
  1519. return ret;
  1520. }
  1521. static int msm_asoc_pcm_new(struct snd_soc_pcm_runtime *rtd)
  1522. {
  1523. struct snd_card *card = rtd->card->snd_card;
  1524. int ret = 0;
  1525. if (!card->dev->coherent_dma_mask)
  1526. card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
  1527. ret = msm_pcm_add_controls(rtd);
  1528. if (ret)
  1529. dev_err(rtd->dev, "%s, kctl add failed\n", __func__);
  1530. return ret;
  1531. }
  1532. static struct snd_soc_component_driver msm_soc_component = {
  1533. .name = DRV_NAME,
  1534. .ops = &msm_pcm_ops,
  1535. .pcm_new = msm_asoc_pcm_new,
  1536. .probe = msm_pcm_loopback_probe,
  1537. };
  1538. static int msm_pcm_probe(struct platform_device *pdev)
  1539. {
  1540. struct msm_pcm_pdata *pdata;
  1541. dev_dbg(&pdev->dev, "%s: dev name %s\n",
  1542. __func__, dev_name(&pdev->dev));
  1543. pdata = kzalloc(sizeof(struct msm_pcm_pdata), GFP_KERNEL);
  1544. if (!pdata)
  1545. return -ENOMEM;
  1546. if (of_property_read_bool(pdev->dev.of_node,
  1547. "qcom,msm-pcm-loopback-low-latency"))
  1548. pdata->perf_mode = LOW_LATENCY_PCM_MODE;
  1549. else
  1550. pdata->perf_mode = LEGACY_PCM_MODE;
  1551. dev_set_drvdata(&pdev->dev, pdata);
  1552. return snd_soc_register_component(&pdev->dev,
  1553. &msm_soc_component,
  1554. NULL, 0);
  1555. }
  1556. static int msm_pcm_remove(struct platform_device *pdev)
  1557. {
  1558. struct msm_pcm_pdata *pdata;
  1559. int i = 0;
  1560. pdata = dev_get_drvdata(&pdev->dev);
  1561. if (pdata) {
  1562. for (i = 0; i < MSM_FRONTEND_DAI_MM_SIZE; i++) {
  1563. kfree(pdata->chmixer_pspd[i][SESSION_TYPE_RX]);
  1564. kfree(pdata->chmixer_pspd[i][SESSION_TYPE_TX]);
  1565. }
  1566. }
  1567. kfree(pdata);
  1568. snd_soc_unregister_component(&pdev->dev);
  1569. return 0;
  1570. }
  1571. static const struct of_device_id msm_pcm_loopback_dt_match[] = {
  1572. {.compatible = "qcom,msm-pcm-loopback"},
  1573. {}
  1574. };
  1575. static struct platform_driver msm_pcm_driver = {
  1576. .driver = {
  1577. .name = "msm-pcm-loopback",
  1578. .owner = THIS_MODULE,
  1579. .of_match_table = msm_pcm_loopback_dt_match,
  1580. .suppress_bind_attrs = true,
  1581. },
  1582. .probe = msm_pcm_probe,
  1583. .remove = msm_pcm_remove,
  1584. };
  1585. int __init msm_pcm_loopback_init(void)
  1586. {
  1587. return platform_driver_register(&msm_pcm_driver);
  1588. }
  1589. void msm_pcm_loopback_exit(void)
  1590. {
  1591. platform_driver_unregister(&msm_pcm_driver);
  1592. }
  1593. MODULE_DESCRIPTION("PCM loopback platform driver");
  1594. MODULE_LICENSE("GPL v2");