msm-pcm-q6-noirq.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2016-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/wait.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/slab.h>
  12. #include <linux/of_device.h>
  13. #include <linux/dma-mapping.h>
  14. #include <linux/dma-buf.h>
  15. #include <sound/core.h>
  16. #include <sound/soc.h>
  17. #include <sound/soc-dapm.h>
  18. #include <sound/pcm.h>
  19. #include <sound/initval.h>
  20. #include <sound/control.h>
  21. #include <sound/timer.h>
  22. #include <sound/hwdep.h>
  23. #include <asm/dma.h>
  24. #include <sound/tlv.h>
  25. #include <sound/pcm_params.h>
  26. #include <sound/devdep_params.h>
  27. #include <dsp/msm_audio_ion.h>
  28. #include <dsp/q6audio-v2.h>
  29. #include "msm-pcm-q6-v2.h"
  30. #include "msm-pcm-routing-v2.h"
  31. #define DRV_NAME "msm-pcm-q6-noirq"
  32. #define PCM_MASTER_VOL_MAX_STEPS 0x2000
  33. static const DECLARE_TLV_DB_LINEAR(msm_pcm_vol_gain, 0,
  34. PCM_MASTER_VOL_MAX_STEPS);
  35. struct snd_msm {
  36. struct snd_card *card;
  37. struct snd_pcm *pcm;
  38. };
  39. #define CMD_EOS_MIN_TIMEOUT_LENGTH 50
  40. #define CMD_EOS_TIMEOUT_MULTIPLIER (HZ * 50)
  41. #define ATRACE_END() \
  42. trace_printk("tracing_mark_write: E\n")
  43. #define ATRACE_BEGIN(name) \
  44. trace_printk("tracing_mark_write: B|%d|%s\n", current->tgid, name)
  45. #define ATRACE_FUNC() ATRACE_BEGIN(__func__)
  46. #define ATRACE_INT(name, value) \
  47. trace_printk("tracing_mark_write: C|%d|%s|%d\n", \
  48. current->tgid, name, (int)(value))
  49. #define SIO_PLAYBACK_MAX_PERIOD_SIZE PLAYBACK_MAX_PERIOD_SIZE
  50. #define SIO_PLAYBACK_MIN_PERIOD_SIZE 48
  51. #define SIO_PLAYBACK_MAX_NUM_PERIODS 512
  52. #define SIO_PLAYBACK_MIN_NUM_PERIODS PLAYBACK_MIN_NUM_PERIODS
  53. #define SIO_PLAYBACK_MIN_BYTES (SIO_PLAYBACK_MIN_NUM_PERIODS * \
  54. SIO_PLAYBACK_MIN_PERIOD_SIZE)
  55. #define SIO_PLAYBACK_MAX_BYTES ((SIO_PLAYBACK_MAX_NUM_PERIODS) * \
  56. (SIO_PLAYBACK_MAX_PERIOD_SIZE))
  57. #define SIO_CAPTURE_MAX_PERIOD_SIZE CAPTURE_MAX_PERIOD_SIZE
  58. #define SIO_CAPTURE_MIN_PERIOD_SIZE 48
  59. #define SIO_CAPTURE_MAX_NUM_PERIODS 512
  60. #define SIO_CAPTURE_MIN_NUM_PERIODS CAPTURE_MIN_NUM_PERIODS
  61. #define SIO_CAPTURE_MIN_BYTES (SIO_CAPTURE_MIN_NUM_PERIODS * \
  62. SIO_CAPTURE_MIN_PERIOD_SIZE)
  63. #define SIO_CAPTURE_MAX_BYTES (SIO_CAPTURE_MAX_NUM_PERIODS * \
  64. SIO_CAPTURE_MAX_PERIOD_SIZE)
  65. static struct snd_pcm_hardware msm_pcm_hardware_playback = {
  66. .info = (SNDRV_PCM_INFO_MMAP |
  67. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  68. SNDRV_PCM_INFO_MMAP_VALID |
  69. SNDRV_PCM_INFO_INTERLEAVED |
  70. SNDRV_PCM_INFO_NO_PERIOD_WAKEUP |
  71. SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME),
  72. .formats = (SNDRV_PCM_FMTBIT_S16_LE |
  73. SNDRV_PCM_FMTBIT_S24_LE |
  74. SNDRV_PCM_FMTBIT_S24_3LE),
  75. .rates = SNDRV_PCM_RATE_8000_192000,
  76. .rate_min = 8000,
  77. .rate_max = 192000,
  78. .channels_min = 1,
  79. .channels_max = 8,
  80. .buffer_bytes_max = SIO_PLAYBACK_MAX_NUM_PERIODS *
  81. SIO_PLAYBACK_MAX_PERIOD_SIZE,
  82. .period_bytes_min = SIO_PLAYBACK_MIN_PERIOD_SIZE,
  83. .period_bytes_max = SIO_PLAYBACK_MAX_PERIOD_SIZE,
  84. .periods_min = SIO_PLAYBACK_MIN_NUM_PERIODS,
  85. .periods_max = SIO_PLAYBACK_MAX_NUM_PERIODS,
  86. .fifo_size = 0,
  87. };
  88. static struct snd_pcm_hardware msm_pcm_hardware_capture = {
  89. .info = (SNDRV_PCM_INFO_MMAP |
  90. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  91. SNDRV_PCM_INFO_MMAP_VALID |
  92. SNDRV_PCM_INFO_INTERLEAVED |
  93. SNDRV_PCM_INFO_NO_PERIOD_WAKEUP |
  94. SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME),
  95. .formats = (SNDRV_PCM_FMTBIT_S16_LE |
  96. SNDRV_PCM_FMTBIT_S24_LE |
  97. SNDRV_PCM_FMTBIT_S24_3LE),
  98. .rates = SNDRV_PCM_RATE_8000_48000,
  99. .rate_min = 8000,
  100. .rate_max = 48000,
  101. .channels_min = 1,
  102. .channels_max = 4,
  103. .buffer_bytes_max = SIO_CAPTURE_MAX_NUM_PERIODS *
  104. SIO_CAPTURE_MAX_PERIOD_SIZE,
  105. .period_bytes_min = SIO_CAPTURE_MIN_PERIOD_SIZE,
  106. .period_bytes_max = SIO_CAPTURE_MAX_PERIOD_SIZE,
  107. .periods_min = SIO_CAPTURE_MIN_NUM_PERIODS,
  108. .periods_max = SIO_CAPTURE_MAX_NUM_PERIODS,
  109. .fifo_size = 0,
  110. };
  111. /* Conventional and unconventional sample rate supported */
  112. static unsigned int supported_sample_rates[] = {
  113. 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000,
  114. 88200, 96000, 176400, 192000
  115. };
  116. static struct snd_pcm_hw_constraint_list constraints_sample_rates = {
  117. .count = ARRAY_SIZE(supported_sample_rates),
  118. .list = supported_sample_rates,
  119. .mask = 0,
  120. };
  121. static unsigned long msm_pcm_fe_topology[MSM_FRONTEND_DAI_MAX];
  122. /* default value is DTS (i.e read from device tree) */
  123. static char const *msm_pcm_fe_topology_text[] = {
  124. "DTS", "ULL", "ULL_PP", "LL" };
  125. static const struct soc_enum msm_pcm_fe_topology_enum[] = {
  126. SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(msm_pcm_fe_topology_text),
  127. msm_pcm_fe_topology_text),
  128. };
  129. static void event_handler(uint32_t opcode,
  130. uint32_t token, uint32_t *payload, void *priv)
  131. {
  132. uint32_t *ptrmem = (uint32_t *)payload;
  133. switch (opcode) {
  134. case ASM_DATA_EVENT_WATERMARK:
  135. pr_debug("%s: Watermark level = 0x%08x\n", __func__, *ptrmem);
  136. break;
  137. case APR_BASIC_RSP_RESULT:
  138. pr_debug("%s: Payload = [0x%x]stat[0x%x]\n",
  139. __func__, payload[0], payload[1]);
  140. switch (payload[0]) {
  141. case ASM_SESSION_CMD_RUN_V2:
  142. case ASM_SESSION_CMD_PAUSE:
  143. case ASM_STREAM_CMD_FLUSH:
  144. break;
  145. default:
  146. break;
  147. }
  148. break;
  149. default:
  150. pr_debug("Not Supported Event opcode[0x%x]\n", opcode);
  151. break;
  152. }
  153. }
  154. static int msm_pcm_open(struct snd_pcm_substream *substream)
  155. {
  156. struct snd_pcm_runtime *runtime = substream->runtime;
  157. struct msm_audio *prtd;
  158. int ret = 0;
  159. prtd = kzalloc(sizeof(struct msm_audio), GFP_KERNEL);
  160. if (prtd == NULL)
  161. return -ENOMEM;
  162. prtd->substream = substream;
  163. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  164. runtime->hw = msm_pcm_hardware_playback;
  165. else
  166. runtime->hw = msm_pcm_hardware_capture;
  167. ret = snd_pcm_hw_constraint_list(runtime, 0,
  168. SNDRV_PCM_HW_PARAM_RATE,
  169. &constraints_sample_rates);
  170. if (ret)
  171. pr_info("snd_pcm_hw_constraint_list failed\n");
  172. ret = snd_pcm_hw_constraint_integer(runtime,
  173. SNDRV_PCM_HW_PARAM_PERIODS);
  174. if (ret)
  175. pr_info("snd_pcm_hw_constraint_integer failed\n");
  176. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  177. ret = snd_pcm_hw_constraint_minmax(runtime,
  178. SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  179. SIO_PLAYBACK_MIN_BYTES,
  180. SIO_PLAYBACK_MAX_BYTES);
  181. if (ret) {
  182. pr_info("%s: P buffer bytes minmax constraint ret %d\n",
  183. __func__, ret);
  184. }
  185. } else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  186. ret = snd_pcm_hw_constraint_minmax(runtime,
  187. SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  188. SIO_CAPTURE_MIN_BYTES,
  189. SIO_CAPTURE_MAX_BYTES);
  190. if (ret) {
  191. pr_info("%s: C buffer bytes minmax constraint ret %d\n",
  192. __func__, ret);
  193. }
  194. }
  195. ret = snd_pcm_hw_constraint_step(runtime, 0,
  196. SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
  197. if (ret) {
  198. pr_err("%s: Constraint for period bytes step ret = %d\n",
  199. __func__, ret);
  200. }
  201. ret = snd_pcm_hw_constraint_step(runtime, 0,
  202. SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 32);
  203. if (ret) {
  204. pr_err("%s: Constraint for buffer bytes step ret = %d\n",
  205. __func__, ret);
  206. }
  207. prtd->audio_client = q6asm_audio_client_alloc(
  208. (app_cb)event_handler, prtd);
  209. if (!prtd->audio_client) {
  210. pr_err("%s: client alloc failed\n", __func__);
  211. ret = -ENOMEM;
  212. goto fail_cmd;
  213. }
  214. prtd->dsp_cnt = 0;
  215. prtd->set_channel_map = false;
  216. runtime->private_data = prtd;
  217. return 0;
  218. fail_cmd:
  219. kfree(prtd);
  220. return ret;
  221. }
  222. static int msm_pcm_hw_params(struct snd_pcm_substream *substream,
  223. struct snd_pcm_hw_params *params)
  224. {
  225. struct snd_pcm_runtime *runtime = substream->runtime;
  226. struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
  227. struct snd_soc_component *component =
  228. snd_soc_rtdcom_lookup(soc_prtd, DRV_NAME);
  229. struct msm_audio *prtd = runtime->private_data;
  230. struct msm_plat_data *pdata;
  231. struct snd_dma_buffer *dma_buf = &substream->dma_buffer;
  232. struct audio_buffer *buf;
  233. struct shared_io_config config;
  234. uint16_t sample_word_size;
  235. uint16_t bits_per_sample;
  236. int ret;
  237. int dir = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ? IN : OUT;
  238. unsigned long topology;
  239. int perf_mode;
  240. bool use_default_chmap = true;
  241. char *chmap = NULL;
  242. if (!component) {
  243. pr_err("%s: component is NULL\n", __func__);
  244. return -EINVAL;
  245. }
  246. pdata = (struct msm_plat_data *)
  247. dev_get_drvdata(component->dev);
  248. if (!pdata) {
  249. ret = -EINVAL;
  250. pr_err("%s: platform data not populated ret: %d\n", __func__,
  251. ret);
  252. return ret;
  253. }
  254. topology = msm_pcm_fe_topology[soc_prtd->dai_link->id];
  255. if (!strcmp(msm_pcm_fe_topology_text[topology], "ULL_PP"))
  256. perf_mode = ULL_POST_PROCESSING_PCM_MODE;
  257. else if (!strcmp(msm_pcm_fe_topology_text[topology], "ULL"))
  258. perf_mode = ULTRA_LOW_LATENCY_PCM_MODE;
  259. else if (!strcmp(msm_pcm_fe_topology_text[topology], "LL"))
  260. perf_mode = LOW_LATENCY_PCM_MODE;
  261. else
  262. /* use the default from the device tree */
  263. perf_mode = pdata->perf_mode;
  264. /* need to set LOW_LATENCY_PCM_MODE for capture since
  265. * push mode does not support ULL
  266. */
  267. prtd->audio_client->perf_mode = (dir == IN) ?
  268. perf_mode :
  269. LOW_LATENCY_PCM_MODE;
  270. /* rate and channels are sent to audio driver */
  271. prtd->samp_rate = params_rate(params);
  272. prtd->channel_mode = params_channels(params);
  273. if (prtd->enabled)
  274. return 0;
  275. if (pdata->ch_map[soc_prtd->dai_link->id]) {
  276. use_default_chmap =
  277. !(pdata->ch_map[soc_prtd->dai_link->id]->set_ch_map);
  278. chmap =
  279. pdata->ch_map[soc_prtd->dai_link->id]->channel_map;
  280. }
  281. switch (runtime->format) {
  282. case SNDRV_PCM_FORMAT_S24_LE:
  283. bits_per_sample = 24;
  284. sample_word_size = 32;
  285. break;
  286. case SNDRV_PCM_FORMAT_S24_3LE:
  287. bits_per_sample = 24;
  288. sample_word_size = 24;
  289. break;
  290. case SNDRV_PCM_FORMAT_S16_LE:
  291. default:
  292. bits_per_sample = 16;
  293. sample_word_size = 16;
  294. break;
  295. }
  296. config.format = FORMAT_LINEAR_PCM;
  297. config.bits_per_sample = bits_per_sample;
  298. config.rate = params_rate(params);
  299. config.channels = params_channels(params);
  300. config.sample_word_size = sample_word_size;
  301. config.bufsz = params_buffer_bytes(params) / params_periods(params);
  302. config.bufcnt = params_periods(params);
  303. ret = q6asm_open_shared_io(prtd->audio_client, &config, dir,
  304. use_default_chmap, chmap);
  305. if (ret) {
  306. pr_err("%s: q6asm_open_write_shared_io failed ret: %d\n",
  307. __func__, ret);
  308. return ret;
  309. }
  310. prtd->pcm_size = params_buffer_bytes(params);
  311. prtd->pcm_count = params_buffer_bytes(params);
  312. prtd->pcm_irq_pos = 0;
  313. buf = prtd->audio_client->port[dir].buf;
  314. dma_buf->dev.type = SNDRV_DMA_TYPE_DEV;
  315. dma_buf->dev.dev = substream->pcm->card->dev;
  316. dma_buf->private_data = NULL;
  317. dma_buf->area = buf->data;
  318. dma_buf->addr = buf->phys;
  319. dma_buf->bytes = prtd->pcm_size;
  320. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  321. pr_debug("%s: session ID %d, perf %d\n", __func__,
  322. prtd->audio_client->session,
  323. prtd->audio_client->perf_mode);
  324. prtd->session_id = prtd->audio_client->session;
  325. pr_debug("msm_pcm_routing_reg_phy_stream w/ id %d\n",
  326. soc_prtd->dai_link->id);
  327. ret = msm_pcm_routing_reg_phy_stream(soc_prtd->dai_link->id,
  328. prtd->audio_client->perf_mode,
  329. prtd->session_id, substream->stream);
  330. if (ret) {
  331. pr_err("%s: stream reg failed ret:%d\n", __func__, ret);
  332. return ret;
  333. }
  334. atomic_set(&prtd->out_count, runtime->periods);
  335. prtd->enabled = 1;
  336. prtd->cmd_pending = 0;
  337. prtd->cmd_interrupt = 0;
  338. return 0;
  339. }
  340. static int msm_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  341. {
  342. int ret = 0;
  343. struct snd_pcm_runtime *runtime = substream->runtime;
  344. struct msm_audio *prtd = runtime->private_data;
  345. int dir = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ? 0 : 1;
  346. struct audio_buffer *buf;
  347. switch (cmd) {
  348. case SNDRV_PCM_TRIGGER_START:
  349. case SNDRV_PCM_TRIGGER_RESUME:
  350. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  351. pr_debug("%s: %s Trigger start\n", __func__,
  352. dir == 0 ? "P" : "C");
  353. ret = q6asm_run(prtd->audio_client, 0, 0, 0);
  354. if (ret)
  355. break;
  356. atomic_set(&prtd->start, 1);
  357. break;
  358. case SNDRV_PCM_TRIGGER_STOP:
  359. pr_debug("%s: SNDRV_PCM_TRIGGER_STOP\n", __func__);
  360. atomic_set(&prtd->start, 0);
  361. q6asm_cmd(prtd->audio_client, CMD_PAUSE);
  362. q6asm_cmd(prtd->audio_client, CMD_FLUSH);
  363. buf = q6asm_shared_io_buf(prtd->audio_client, dir);
  364. if (buf == NULL) {
  365. pr_err("%s: shared IO buffer is null\n", __func__);
  366. ret = -EINVAL;
  367. break;
  368. }
  369. memset(buf->data, 0, buf->actual_size);
  370. break;
  371. case SNDRV_PCM_TRIGGER_SUSPEND:
  372. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  373. pr_debug("%s: SNDRV_PCM_TRIGGER_PAUSE\n", __func__);
  374. ret = q6asm_cmd_nowait(prtd->audio_client, CMD_PAUSE);
  375. atomic_set(&prtd->start, 0);
  376. break;
  377. default:
  378. ret = -EINVAL;
  379. break;
  380. }
  381. return ret;
  382. }
  383. static int msm_pcm_mmap_fd(struct snd_pcm_substream *substream,
  384. struct snd_pcm_mmap_fd *mmap_fd)
  385. {
  386. struct msm_audio *prtd;
  387. struct audio_port_data *apd;
  388. struct audio_buffer *ab;
  389. int dir = -1;
  390. struct dma_buf *buf = NULL;
  391. int rc = 0;
  392. if (!substream->runtime) {
  393. pr_err("%s substream runtime not found\n", __func__);
  394. return -EFAULT;
  395. }
  396. prtd = substream->runtime->private_data;
  397. if (!prtd || !prtd->audio_client || !prtd->mmap_flag) {
  398. pr_err("%s no audio client or not an mmap session\n", __func__);
  399. return -EINVAL;
  400. }
  401. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  402. dir = IN;
  403. else
  404. dir = OUT;
  405. apd = prtd->audio_client->port;
  406. ab = &(apd[dir].buf[0]);
  407. /*
  408. * Passing O_CLOEXEC as flag passed to fd, to be in sync with
  409. * previous implimentation.
  410. * This was the flag used by previous internal wrapper API, which
  411. * used to call dma_buf_fd internally.
  412. */
  413. mmap_fd->fd = dma_buf_fd(ab->dma_buf, O_CLOEXEC);
  414. if (mmap_fd->fd < 0) {
  415. pr_err("%s: dma_buf_fd failed, fd:%d\n",
  416. __func__, mmap_fd->fd);
  417. rc = -EFAULT;
  418. goto buf_fd_fail;
  419. }
  420. mmap_fd->dir = dir;
  421. mmap_fd->actual_size = ab->actual_size;
  422. mmap_fd->size = ab->size;
  423. buf = dma_buf_get(mmap_fd->fd);
  424. if (IS_ERR_OR_NULL(buf)) {
  425. pr_err("%s: dma_buf_get failed, fd:%d\n",
  426. __func__, mmap_fd->fd);
  427. rc = -EINVAL;
  428. }
  429. buf_fd_fail:
  430. return rc;
  431. }
  432. static int msm_pcm_ioctl(struct snd_pcm_substream *substream,
  433. unsigned int cmd, void *arg)
  434. {
  435. struct snd_pcm_runtime *runtime = substream->runtime;
  436. struct msm_audio *prtd = runtime->private_data;
  437. int dir = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ? 0 : 1;
  438. struct audio_buffer *buf;
  439. switch (cmd) {
  440. case SNDRV_PCM_IOCTL1_RESET:
  441. pr_debug("%s: %s SNDRV_PCM_IOCTL1_RESET\n", __func__,
  442. dir == 0 ? "P" : "C");
  443. buf = q6asm_shared_io_buf(prtd->audio_client, dir);
  444. if (buf && buf->data)
  445. memset(buf->data, 0, buf->actual_size);
  446. break;
  447. default:
  448. break;
  449. }
  450. return snd_pcm_lib_ioctl(substream, cmd, arg);
  451. }
  452. #ifdef CONFIG_COMPAT
  453. static int msm_pcm_compat_ioctl(struct snd_pcm_substream *substream,
  454. unsigned int cmd, void *arg)
  455. {
  456. /* we only handle RESET which is common for both modes */
  457. return msm_pcm_ioctl(substream, cmd, arg);
  458. }
  459. #endif
  460. static snd_pcm_uframes_t msm_pcm_pointer(struct snd_pcm_substream *substream)
  461. {
  462. struct snd_pcm_runtime *runtime = substream->runtime;
  463. uint32_t read_index, wall_clk_msw, wall_clk_lsw;
  464. /*these are offsets, unlike ASoC's full values*/
  465. snd_pcm_sframes_t hw_ptr;
  466. snd_pcm_sframes_t period_size;
  467. int ret;
  468. int retries = 10;
  469. struct msm_audio *prtd = runtime->private_data;
  470. period_size = runtime->period_size;
  471. do {
  472. ret = q6asm_get_shared_pos(prtd->audio_client,
  473. &read_index, &wall_clk_msw,
  474. &wall_clk_lsw);
  475. } while (ret == -EAGAIN && --retries);
  476. if (ret || !period_size) {
  477. pr_err("get_shared_pos error or zero period size\n");
  478. return 0;
  479. }
  480. hw_ptr = bytes_to_frames(substream->runtime,
  481. read_index);
  482. if (runtime->control->appl_ptr == 0) {
  483. pr_debug("ptr(%s): appl(0), hw = %lu read_index = %u\n",
  484. prtd->substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
  485. "P" : "C",
  486. hw_ptr, read_index);
  487. }
  488. return (hw_ptr/period_size) * period_size;
  489. }
  490. static int msm_pcm_copy(struct snd_pcm_substream *substream, int a,
  491. unsigned long hwoff, void __user *buf, unsigned long fbytes)
  492. {
  493. return -EINVAL;
  494. }
  495. static int msm_pcm_mmap(struct snd_pcm_substream *substream,
  496. struct vm_area_struct *vma)
  497. {
  498. struct snd_pcm_runtime *runtime = substream->runtime;
  499. struct msm_audio *prtd = runtime->private_data;
  500. struct audio_client *ac = prtd->audio_client;
  501. struct audio_port_data *apd = ac->port;
  502. struct audio_buffer *ab;
  503. int dir = -1;
  504. int ret;
  505. pr_debug("%s: mmap begin\n", __func__);
  506. prtd->mmap_flag = 1;
  507. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  508. dir = IN;
  509. else
  510. dir = OUT;
  511. ab = &(apd[dir].buf[0]);
  512. ret = msm_audio_ion_mmap(ab, vma);
  513. if (ret)
  514. prtd->mmap_flag = 0;
  515. return ret;
  516. }
  517. static int msm_pcm_prepare(struct snd_pcm_substream *substream)
  518. {
  519. struct snd_pcm_runtime *runtime = substream->runtime;
  520. struct msm_audio *prtd = runtime->private_data;
  521. if (!prtd || !prtd->mmap_flag)
  522. return -EIO;
  523. return 0;
  524. }
  525. static int msm_pcm_close(struct snd_pcm_substream *substream)
  526. {
  527. struct snd_pcm_runtime *runtime = substream->runtime;
  528. struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
  529. struct msm_audio *prtd = runtime->private_data;
  530. struct audio_client *ac = prtd->audio_client;
  531. uint32_t timeout;
  532. int dir = 0;
  533. int ret = 0;
  534. if (ac) {
  535. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  536. dir = IN;
  537. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  538. dir = OUT;
  539. /* determine timeout length */
  540. if (runtime->frame_bits == 0 || runtime->rate == 0) {
  541. timeout = CMD_EOS_MIN_TIMEOUT_LENGTH;
  542. } else {
  543. timeout = (runtime->period_size *
  544. CMD_EOS_TIMEOUT_MULTIPLIER) /
  545. ((runtime->frame_bits / 8) *
  546. runtime->rate);
  547. if (timeout < CMD_EOS_MIN_TIMEOUT_LENGTH)
  548. timeout = CMD_EOS_MIN_TIMEOUT_LENGTH;
  549. }
  550. q6asm_cmd(ac, CMD_CLOSE);
  551. ret = q6asm_shared_io_free(ac, dir);
  552. if (ret) {
  553. pr_err("%s: Failed to close pull mode, ret %d\n",
  554. __func__, ret);
  555. }
  556. q6asm_audio_client_free(ac);
  557. }
  558. msm_pcm_routing_dereg_phy_stream(soc_prtd->dai_link->id,
  559. dir == IN ?
  560. SNDRV_PCM_STREAM_PLAYBACK :
  561. SNDRV_PCM_STREAM_CAPTURE);
  562. kfree(prtd);
  563. runtime->private_data = NULL;
  564. return 0;
  565. }
  566. static int msm_pcm_set_volume(struct msm_audio *prtd, uint32_t volume)
  567. {
  568. int rc = 0;
  569. if (prtd && prtd->audio_client) {
  570. pr_debug("%s: channels %d volume 0x%x\n", __func__,
  571. prtd->channel_mode, volume);
  572. rc = q6asm_set_volume(prtd->audio_client, volume);
  573. if (rc < 0) {
  574. pr_err("%s: Send Volume command failed rc=%d\n",
  575. __func__, rc);
  576. }
  577. }
  578. return rc;
  579. }
  580. static int msm_pcm_volume_ctl_get(struct snd_kcontrol *kcontrol,
  581. struct snd_ctl_elem_value *ucontrol)
  582. {
  583. struct snd_pcm_volume *vol = snd_kcontrol_chip(kcontrol);
  584. struct snd_pcm_substream *substream =
  585. vol->pcm->streams[vol->stream].substream;
  586. struct msm_audio *prtd;
  587. pr_debug("%s\n", __func__);
  588. if (!substream) {
  589. pr_err("%s substream not found\n", __func__);
  590. return -ENODEV;
  591. }
  592. if (!substream->runtime) {
  593. pr_debug("%s substream runtime not found\n", __func__);
  594. return 0;
  595. }
  596. prtd = substream->runtime->private_data;
  597. if (prtd)
  598. ucontrol->value.integer.value[0] = prtd->volume;
  599. return 0;
  600. }
  601. static int msm_pcm_volume_ctl_put(struct snd_kcontrol *kcontrol,
  602. struct snd_ctl_elem_value *ucontrol)
  603. {
  604. int rc = 0;
  605. struct snd_pcm_volume *vol = snd_kcontrol_chip(kcontrol);
  606. struct snd_pcm_substream *substream =
  607. vol->pcm->streams[vol->stream].substream;
  608. struct msm_audio *prtd;
  609. int volume = ucontrol->value.integer.value[0];
  610. pr_debug("%s: volume : 0x%x\n", __func__, volume);
  611. if (!substream) {
  612. pr_err("%s substream not found\n", __func__);
  613. return -ENODEV;
  614. }
  615. if (!substream->runtime) {
  616. pr_err("%s substream runtime not found\n", __func__);
  617. return 0;
  618. }
  619. prtd = substream->runtime->private_data;
  620. if (prtd) {
  621. rc = msm_pcm_set_volume(prtd, volume);
  622. prtd->volume = volume;
  623. }
  624. return rc;
  625. }
  626. static int msm_pcm_add_volume_control(struct snd_soc_pcm_runtime *rtd,
  627. int stream)
  628. {
  629. int ret = 0;
  630. struct snd_pcm *pcm = rtd->pcm;
  631. struct snd_pcm_volume *volume_info;
  632. struct snd_kcontrol *kctl;
  633. dev_dbg(rtd->dev, "%s, volume control add\n", __func__);
  634. ret = snd_pcm_add_volume_ctls(pcm, stream,
  635. NULL, 1, rtd->dai_link->id,
  636. &volume_info);
  637. if (ret < 0) {
  638. pr_err("%s volume control failed ret %d\n", __func__, ret);
  639. return ret;
  640. }
  641. kctl = volume_info->kctl;
  642. kctl->put = msm_pcm_volume_ctl_put;
  643. kctl->get = msm_pcm_volume_ctl_get;
  644. kctl->tlv.p = msm_pcm_vol_gain;
  645. return 0;
  646. }
  647. static int msm_pcm_channel_map_put(struct snd_kcontrol *kcontrol,
  648. struct snd_ctl_elem_value *ucontrol)
  649. {
  650. struct snd_soc_component *pcm = snd_kcontrol_chip(kcontrol);
  651. u64 fe_id = kcontrol->private_value;
  652. struct msm_plat_data *pdata = (struct msm_plat_data *)
  653. snd_soc_component_get_drvdata(pcm);
  654. int rc = 0, i = 0;
  655. pr_debug("%s: fe_id- %llu\n", __func__, fe_id);
  656. if (fe_id >= MSM_FRONTEND_DAI_MAX) {
  657. pr_err("%s Received out of bounds fe_id %llu\n",
  658. __func__, fe_id);
  659. rc = -EINVAL;
  660. goto end;
  661. }
  662. if (pdata->ch_map[fe_id]) {
  663. pdata->ch_map[fe_id]->set_ch_map = true;
  664. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL; i++)
  665. pdata->ch_map[fe_id]->channel_map[i] =
  666. (char)(ucontrol->value.integer.value[i]);
  667. } else {
  668. pr_debug("%s: no memory for ch_map, default will be set\n",
  669. __func__);
  670. }
  671. end:
  672. pr_debug("%s: ret %d\n", __func__, rc);
  673. return rc;
  674. }
  675. static int msm_pcm_channel_map_info(struct snd_kcontrol *kcontrol,
  676. struct snd_ctl_elem_info *uinfo)
  677. {
  678. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  679. uinfo->count = 8;
  680. uinfo->value.integer.min = 0;
  681. uinfo->value.integer.max = 0xFFFFFFFF;
  682. return 0;
  683. }
  684. static int msm_pcm_channel_map_get(struct snd_kcontrol *kcontrol,
  685. struct snd_ctl_elem_value *ucontrol)
  686. {
  687. struct snd_soc_component *pcm = snd_kcontrol_chip(kcontrol);
  688. u64 fe_id = kcontrol->private_value;
  689. struct msm_plat_data *pdata = (struct msm_plat_data *)
  690. snd_soc_component_get_drvdata(pcm);
  691. int rc = 0, i = 0;
  692. pr_debug("%s: fe_id- %llu\n", __func__, fe_id);
  693. if (fe_id >= MSM_FRONTEND_DAI_MAX) {
  694. pr_err("%s: Received out of bounds fe_id %llu\n",
  695. __func__, fe_id);
  696. rc = -EINVAL;
  697. goto end;
  698. }
  699. if (pdata->ch_map[fe_id]) {
  700. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL; i++)
  701. ucontrol->value.integer.value[i] =
  702. pdata->ch_map[fe_id]->channel_map[i];
  703. }
  704. end:
  705. pr_debug("%s: ret %d\n", __func__, rc);
  706. return rc;
  707. }
  708. static int msm_pcm_add_channel_map_control(struct snd_soc_pcm_runtime *rtd)
  709. {
  710. struct snd_soc_component *component = NULL;
  711. const char *mixer_ctl_name = "Playback Channel Map";
  712. const char *deviceNo = "NN";
  713. char *mixer_str = NULL;
  714. struct msm_plat_data *pdata = NULL;
  715. int ctl_len = 0;
  716. struct snd_kcontrol_new fe_channel_map_control[1] = {
  717. {
  718. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  719. .name = "?",
  720. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  721. .info = msm_pcm_channel_map_info,
  722. .get = msm_pcm_channel_map_get,
  723. .put = msm_pcm_channel_map_put,
  724. .private_value = 0,
  725. }
  726. };
  727. if (!rtd) {
  728. pr_err("%s: NULL rtd\n", __func__);
  729. return -EINVAL;
  730. }
  731. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  732. if (!component) {
  733. pr_err("%s: component is NULL\n", __func__);
  734. return -EINVAL;
  735. }
  736. pr_debug("%s: added new pcm FE with name %s, id %d, cpu dai %s, device no %d\n",
  737. __func__, rtd->dai_link->name, rtd->dai_link->id,
  738. rtd->dai_link->cpu_dai_name, rtd->pcm->device);
  739. ctl_len = strlen(mixer_ctl_name) + strlen(deviceNo) + 1;
  740. mixer_str = kzalloc(ctl_len, GFP_KERNEL);
  741. if (!mixer_str)
  742. return -ENOMEM;
  743. snprintf(mixer_str, ctl_len, "%s%d", mixer_ctl_name, rtd->pcm->device);
  744. fe_channel_map_control[0].name = mixer_str;
  745. fe_channel_map_control[0].private_value = rtd->dai_link->id;
  746. pr_debug("%s: Registering new mixer ctl %s\n", __func__, mixer_str);
  747. snd_soc_add_component_controls(component,
  748. fe_channel_map_control,
  749. ARRAY_SIZE(fe_channel_map_control));
  750. pdata = snd_soc_component_get_drvdata(component);
  751. pdata->ch_map[rtd->dai_link->id] =
  752. kzalloc(sizeof(struct msm_pcm_ch_map), GFP_KERNEL);
  753. if (!pdata->ch_map[rtd->dai_link->id]) {
  754. pr_err("%s: Could not allocate memory for channel map\n",
  755. __func__);
  756. kfree(mixer_str);
  757. return -ENOMEM;
  758. }
  759. kfree(mixer_str);
  760. return 0;
  761. }
  762. static int msm_pcm_fe_topology_info(struct snd_kcontrol *kcontrol,
  763. struct snd_ctl_elem_info *uinfo)
  764. {
  765. const struct soc_enum *e = &msm_pcm_fe_topology_enum[0];
  766. return snd_ctl_enum_info(uinfo, 1, e->items, e->texts);
  767. }
  768. static int msm_pcm_fe_topology_get(struct snd_kcontrol *kcontrol,
  769. struct snd_ctl_elem_value *ucontrol)
  770. {
  771. unsigned long fe_id = kcontrol->private_value;
  772. if (fe_id >= MSM_FRONTEND_DAI_MAX) {
  773. pr_err("%s Received out of bound fe_id %lu\n", __func__, fe_id);
  774. return -EINVAL;
  775. }
  776. pr_debug("%s: %lu topology %s\n", __func__, fe_id,
  777. msm_pcm_fe_topology_text[msm_pcm_fe_topology[fe_id]]);
  778. ucontrol->value.enumerated.item[0] = msm_pcm_fe_topology[fe_id];
  779. return 0;
  780. }
  781. static int msm_pcm_fe_topology_put(struct snd_kcontrol *kcontrol,
  782. struct snd_ctl_elem_value *ucontrol)
  783. {
  784. unsigned long fe_id = kcontrol->private_value;
  785. unsigned int item;
  786. if (fe_id >= MSM_FRONTEND_DAI_MAX) {
  787. pr_err("%s Received out of bound fe_id %lu\n", __func__, fe_id);
  788. return -EINVAL;
  789. }
  790. item = ucontrol->value.enumerated.item[0];
  791. if (item >= ARRAY_SIZE(msm_pcm_fe_topology_text)) {
  792. pr_err("%s Received out of bound topology %lu\n", __func__,
  793. fe_id);
  794. return -EINVAL;
  795. }
  796. pr_debug("%s: %lu new topology %s\n", __func__, fe_id,
  797. msm_pcm_fe_topology_text[item]);
  798. msm_pcm_fe_topology[fe_id] = item;
  799. return 0;
  800. }
  801. static int msm_pcm_add_fe_topology_control(struct snd_soc_pcm_runtime *rtd)
  802. {
  803. struct snd_soc_component *component = NULL;
  804. const char *mixer_ctl_name = "PCM_Dev";
  805. const char *deviceNo = "NN";
  806. const char *topo_text = "Topology";
  807. char *mixer_str = NULL;
  808. int ctl_len;
  809. int ret;
  810. struct snd_kcontrol_new topology_control[1] = {
  811. {
  812. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  813. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  814. .name = "?",
  815. .info = msm_pcm_fe_topology_info,
  816. .get = msm_pcm_fe_topology_get,
  817. .put = msm_pcm_fe_topology_put,
  818. .private_value = 0,
  819. },
  820. };
  821. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  822. if (!component) {
  823. pr_err("%s: component is NULL\n", __func__);
  824. return -EINVAL;
  825. }
  826. ctl_len = strlen(mixer_ctl_name) + 1 + strlen(deviceNo) + 1 +
  827. strlen(topo_text) + 1;
  828. mixer_str = kzalloc(ctl_len, GFP_KERNEL);
  829. if (!mixer_str)
  830. return -ENOMEM;
  831. snprintf(mixer_str, ctl_len, "%s %d %s", mixer_ctl_name,
  832. rtd->pcm->device, topo_text);
  833. topology_control[0].name = mixer_str;
  834. topology_control[0].private_value = rtd->dai_link->id;
  835. ret = snd_soc_add_component_controls(component, topology_control,
  836. ARRAY_SIZE(topology_control));
  837. msm_pcm_fe_topology[rtd->dai_link->id] = 0;
  838. kfree(mixer_str);
  839. return ret;
  840. }
  841. static int msm_pcm_playback_app_type_cfg_ctl_put(struct snd_kcontrol *kcontrol,
  842. struct snd_ctl_elem_value *ucontrol)
  843. {
  844. u64 fe_id = kcontrol->private_value;
  845. int session_type = SESSION_TYPE_RX;
  846. int be_id = ucontrol->value.integer.value[3];
  847. struct msm_pcm_stream_app_type_cfg cfg_data = {0, 0, 48000};
  848. int ret = 0;
  849. cfg_data.app_type = ucontrol->value.integer.value[0];
  850. cfg_data.acdb_dev_id = ucontrol->value.integer.value[1];
  851. if (ucontrol->value.integer.value[2] != 0)
  852. cfg_data.sample_rate = ucontrol->value.integer.value[2];
  853. pr_debug("%s: fe_id- %llu session_type- %d be_id- %d app_type- %d acdb_dev_id- %d sample_rate- %d\n",
  854. __func__, fe_id, session_type, be_id,
  855. cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
  856. ret = msm_pcm_routing_reg_stream_app_type_cfg(fe_id, session_type,
  857. be_id, &cfg_data);
  858. if (ret < 0)
  859. pr_err("%s: msm_pcm_routing_reg_stream_app_type_cfg failed returned %d\n",
  860. __func__, ret);
  861. return ret;
  862. }
  863. static int msm_pcm_playback_app_type_cfg_ctl_get(struct snd_kcontrol *kcontrol,
  864. struct snd_ctl_elem_value *ucontrol)
  865. {
  866. u64 fe_id = kcontrol->private_value;
  867. int session_type = SESSION_TYPE_RX;
  868. int be_id = 0;
  869. struct msm_pcm_stream_app_type_cfg cfg_data = {0};
  870. int ret = 0;
  871. ret = msm_pcm_routing_get_stream_app_type_cfg(fe_id, session_type,
  872. &be_id, &cfg_data);
  873. if (ret < 0) {
  874. pr_err("%s: msm_pcm_routing_get_stream_app_type_cfg failed returned %d\n",
  875. __func__, ret);
  876. goto done;
  877. }
  878. ucontrol->value.integer.value[0] = cfg_data.app_type;
  879. ucontrol->value.integer.value[1] = cfg_data.acdb_dev_id;
  880. ucontrol->value.integer.value[2] = cfg_data.sample_rate;
  881. ucontrol->value.integer.value[3] = be_id;
  882. pr_debug("%s: fedai_id %llu, session_type %d, be_id %d, app_type %d, acdb_dev_id %d, sample_rate %d\n",
  883. __func__, fe_id, session_type, be_id,
  884. cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
  885. done:
  886. return ret;
  887. }
  888. static int msm_pcm_capture_app_type_cfg_ctl_put(struct snd_kcontrol *kcontrol,
  889. struct snd_ctl_elem_value *ucontrol)
  890. {
  891. u64 fe_id = kcontrol->private_value;
  892. int session_type = SESSION_TYPE_TX;
  893. int be_id = ucontrol->value.integer.value[3];
  894. struct msm_pcm_stream_app_type_cfg cfg_data = {0, 0, 48000};
  895. int ret = 0;
  896. cfg_data.app_type = ucontrol->value.integer.value[0];
  897. cfg_data.acdb_dev_id = ucontrol->value.integer.value[1];
  898. if (ucontrol->value.integer.value[2] != 0)
  899. cfg_data.sample_rate = ucontrol->value.integer.value[2];
  900. pr_debug("%s: fe_id- %llu session_type- %d be_id- %d app_type- %d acdb_dev_id- %d sample_rate- %d\n",
  901. __func__, fe_id, session_type, be_id,
  902. cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
  903. ret = msm_pcm_routing_reg_stream_app_type_cfg(fe_id, session_type,
  904. be_id, &cfg_data);
  905. if (ret < 0)
  906. pr_err("%s: msm_pcm_routing_reg_stream_app_type_cfg failed returned %d\n",
  907. __func__, ret);
  908. return ret;
  909. }
  910. static int msm_pcm_capture_app_type_cfg_ctl_get(struct snd_kcontrol *kcontrol,
  911. struct snd_ctl_elem_value *ucontrol)
  912. {
  913. u64 fe_id = kcontrol->private_value;
  914. int session_type = SESSION_TYPE_TX;
  915. int be_id = 0;
  916. struct msm_pcm_stream_app_type_cfg cfg_data = {0};
  917. int ret = 0;
  918. ret = msm_pcm_routing_get_stream_app_type_cfg(fe_id, session_type,
  919. &be_id, &cfg_data);
  920. if (ret < 0) {
  921. pr_err("%s: msm_pcm_routing_get_stream_app_type_cfg failed returned %d\n",
  922. __func__, ret);
  923. goto done;
  924. }
  925. ucontrol->value.integer.value[0] = cfg_data.app_type;
  926. ucontrol->value.integer.value[1] = cfg_data.acdb_dev_id;
  927. ucontrol->value.integer.value[2] = cfg_data.sample_rate;
  928. ucontrol->value.integer.value[3] = be_id;
  929. pr_debug("%s: fedai_id %llu, session_type %d, be_id %d, app_type %d, acdb_dev_id %d, sample_rate %d\n",
  930. __func__, fe_id, session_type, be_id,
  931. cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
  932. done:
  933. return ret;
  934. }
  935. static int msm_pcm_add_app_type_controls(struct snd_soc_pcm_runtime *rtd)
  936. {
  937. struct snd_pcm *pcm = rtd->pcm;
  938. struct snd_pcm_usr *app_type_info;
  939. struct snd_kcontrol *kctl;
  940. const char *playback_mixer_ctl_name = "Audio Stream";
  941. const char *capture_mixer_ctl_name = "Audio Stream Capture";
  942. const char *deviceNo = "NN";
  943. const char *suffix = "App Type Cfg";
  944. int ctl_len, ret = 0;
  945. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
  946. ctl_len = strlen(playback_mixer_ctl_name) + 1 +
  947. strlen(deviceNo) + 1 +
  948. strlen(suffix) + 1;
  949. pr_debug("%s: Playback app type cntrl add\n", __func__);
  950. ret = snd_pcm_add_usr_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  951. NULL, 1, ctl_len, rtd->dai_link->id,
  952. &app_type_info);
  953. if (ret < 0) {
  954. pr_err("%s: playback app type cntrl add failed, err: %d\n",
  955. __func__, ret);
  956. return ret;
  957. }
  958. kctl = app_type_info->kctl;
  959. snprintf(kctl->id.name, ctl_len, "%s %d %s",
  960. playback_mixer_ctl_name, rtd->pcm->device, suffix);
  961. kctl->put = msm_pcm_playback_app_type_cfg_ctl_put;
  962. kctl->get = msm_pcm_playback_app_type_cfg_ctl_get;
  963. }
  964. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
  965. ctl_len = strlen(capture_mixer_ctl_name) + 1 +
  966. strlen(deviceNo) + 1 + strlen(suffix) + 1;
  967. pr_debug("%s: Capture app type cntrl add\n", __func__);
  968. ret = snd_pcm_add_usr_ctls(pcm, SNDRV_PCM_STREAM_CAPTURE,
  969. NULL, 1, ctl_len, rtd->dai_link->id,
  970. &app_type_info);
  971. if (ret < 0) {
  972. pr_err("%s: capture app type cntrl add failed, err: %d\n",
  973. __func__, ret);
  974. return ret;
  975. }
  976. kctl = app_type_info->kctl;
  977. snprintf(kctl->id.name, ctl_len, "%s %d %s",
  978. capture_mixer_ctl_name, rtd->pcm->device, suffix);
  979. kctl->put = msm_pcm_capture_app_type_cfg_ctl_put;
  980. kctl->get = msm_pcm_capture_app_type_cfg_ctl_get;
  981. }
  982. return 0;
  983. }
  984. static int msm_pcm_hwdep_ioctl(struct snd_hwdep *hw, struct file *file,
  985. unsigned int cmd, unsigned long arg)
  986. {
  987. int ret = 0;
  988. struct snd_pcm *pcm = hw->private_data;
  989. struct snd_pcm_mmap_fd __user *_mmap_fd = NULL;
  990. struct snd_pcm_mmap_fd mmap_fd;
  991. struct snd_pcm_substream *substream = NULL;
  992. int32_t dir = -1;
  993. switch (cmd) {
  994. case SNDRV_PCM_IOCTL_MMAP_DATA_FD:
  995. _mmap_fd = (struct snd_pcm_mmap_fd __user *)arg;
  996. if (get_user(dir, (int32_t __user *)&(_mmap_fd->dir))) {
  997. pr_err("%s: error copying mmap_fd from user\n",
  998. __func__);
  999. ret = -EFAULT;
  1000. break;
  1001. }
  1002. if (dir != OUT && dir != IN) {
  1003. pr_err("%s invalid stream dir\n", __func__);
  1004. ret = -EINVAL;
  1005. break;
  1006. }
  1007. substream = pcm->streams[dir].substream;
  1008. if (!substream) {
  1009. pr_err("%s substream not found\n", __func__);
  1010. ret = -ENODEV;
  1011. break;
  1012. }
  1013. pr_debug("%s : %s MMAP Data fd\n", __func__,
  1014. dir == 0 ? "P" : "C");
  1015. if (msm_pcm_mmap_fd(substream, &mmap_fd) < 0) {
  1016. pr_err("%s: error getting fd\n",
  1017. __func__);
  1018. ret = -EFAULT;
  1019. break;
  1020. }
  1021. if (put_user(mmap_fd.fd, &_mmap_fd->fd) ||
  1022. put_user(mmap_fd.size, &_mmap_fd->size) ||
  1023. put_user(mmap_fd.actual_size, &_mmap_fd->actual_size)) {
  1024. pr_err("%s: error copying fd\n", __func__);
  1025. return -EFAULT;
  1026. }
  1027. break;
  1028. default:
  1029. ret = -EINVAL;
  1030. break;
  1031. }
  1032. return ret;
  1033. }
  1034. #ifdef CONFIG_COMPAT
  1035. static int msm_pcm_hwdep_compat_ioctl(struct snd_hwdep *hw,
  1036. struct file *file,
  1037. unsigned int cmd,
  1038. unsigned long arg)
  1039. {
  1040. /* we only support mmap fd. Handling is common in both modes */
  1041. return msm_pcm_hwdep_ioctl(hw, file, cmd, arg);
  1042. }
  1043. #else
  1044. static int msm_pcm_hwdep_compat_ioctl(struct snd_hwdep *hw,
  1045. struct file *file,
  1046. unsigned int cmd,
  1047. unsigned long arg)
  1048. {
  1049. return -EINVAL;
  1050. }
  1051. #endif
  1052. static int msm_pcm_add_hwdep_dev(struct snd_soc_pcm_runtime *runtime)
  1053. {
  1054. struct snd_hwdep *hwdep;
  1055. int rc;
  1056. char id[] = "NOIRQ_NN";
  1057. snprintf(id, sizeof(id), "NOIRQ_%d", runtime->pcm->device);
  1058. pr_debug("%s: pcm dev %d\n", __func__, runtime->pcm->device);
  1059. rc = snd_hwdep_new(runtime->card->snd_card,
  1060. &id[0],
  1061. HWDEP_FE_BASE + runtime->pcm->device,
  1062. &hwdep);
  1063. if (!hwdep || rc < 0) {
  1064. pr_err("%s: hwdep intf failed to create %s - hwdep\n", __func__,
  1065. id);
  1066. return rc;
  1067. }
  1068. hwdep->iface = SNDRV_HWDEP_IFACE_AUDIO_BE; /* for lack of a FE iface */
  1069. hwdep->private_data = runtime->pcm; /* of type struct snd_pcm */
  1070. hwdep->ops.ioctl = msm_pcm_hwdep_ioctl;
  1071. hwdep->ops.ioctl_compat = msm_pcm_hwdep_compat_ioctl;
  1072. return 0;
  1073. }
  1074. static int msm_asoc_pcm_new(struct snd_soc_pcm_runtime *rtd)
  1075. {
  1076. struct snd_card *card = rtd->card->snd_card;
  1077. struct snd_pcm *pcm = rtd->pcm;
  1078. int ret;
  1079. pr_debug("%s , register new control\n", __func__);
  1080. if (!card->dev->coherent_dma_mask)
  1081. card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
  1082. ret = msm_pcm_add_channel_map_control(rtd);
  1083. if (ret)
  1084. pr_err("%s: Could not add pcm Channel Map Control\n",
  1085. __func__);
  1086. ret = msm_pcm_add_volume_control(rtd, SNDRV_PCM_STREAM_PLAYBACK);
  1087. if (ret) {
  1088. pr_err("%s: Could not add pcm playback volume Control %d\n",
  1089. __func__, ret);
  1090. }
  1091. ret = msm_pcm_add_volume_control(rtd, SNDRV_PCM_STREAM_CAPTURE);
  1092. if (ret) {
  1093. pr_err("%s: Could not add pcm capture volume Control %d\n",
  1094. __func__, ret);
  1095. }
  1096. ret = msm_pcm_add_fe_topology_control(rtd);
  1097. if (ret) {
  1098. pr_err("%s: Could not add pcm topology control %d\n",
  1099. __func__, ret);
  1100. }
  1101. ret = msm_pcm_add_app_type_controls(rtd);
  1102. if (ret) {
  1103. pr_err("%s: Could not add app type controls failed %d\n",
  1104. __func__, ret);
  1105. }
  1106. ret = msm_pcm_add_hwdep_dev(rtd);
  1107. if (ret)
  1108. pr_err("%s: Could not add hw dep node\n", __func__);
  1109. pcm->nonatomic = true;
  1110. return ret;
  1111. }
  1112. static const struct snd_pcm_ops msm_pcm_ops = {
  1113. .open = msm_pcm_open,
  1114. .prepare = msm_pcm_prepare,
  1115. .copy_user = msm_pcm_copy,
  1116. .hw_params = msm_pcm_hw_params,
  1117. .ioctl = msm_pcm_ioctl,
  1118. #ifdef CONFIG_COMPAT
  1119. .compat_ioctl = msm_pcm_compat_ioctl,
  1120. #endif
  1121. .trigger = msm_pcm_trigger,
  1122. .pointer = msm_pcm_pointer,
  1123. .mmap = msm_pcm_mmap,
  1124. .close = msm_pcm_close,
  1125. };
  1126. static struct snd_soc_component_driver msm_soc_component = {
  1127. .name = DRV_NAME,
  1128. .ops = &msm_pcm_ops,
  1129. .pcm_new = msm_asoc_pcm_new,
  1130. };
  1131. static int msm_pcm_probe(struct platform_device *pdev)
  1132. {
  1133. int rc;
  1134. struct msm_plat_data *pdata;
  1135. const char *latency_level;
  1136. int perf_mode = LOW_LATENCY_PCM_MODE;
  1137. dev_dbg(&pdev->dev, "Pull mode driver probe\n");
  1138. if (of_property_read_bool(pdev->dev.of_node,
  1139. "qcom,msm-pcm-low-latency")) {
  1140. rc = of_property_read_string(pdev->dev.of_node,
  1141. "qcom,latency-level", &latency_level);
  1142. if (!rc) {
  1143. if (!strcmp(latency_level, "ultra"))
  1144. perf_mode = ULTRA_LOW_LATENCY_PCM_MODE;
  1145. else if (!strcmp(latency_level, "ull-pp"))
  1146. perf_mode = ULL_POST_PROCESSING_PCM_MODE;
  1147. }
  1148. }
  1149. pdata = devm_kzalloc(&pdev->dev,
  1150. sizeof(struct msm_plat_data), GFP_KERNEL);
  1151. if (!pdata)
  1152. return -ENOMEM;
  1153. pdata->perf_mode = perf_mode;
  1154. dev_set_drvdata(&pdev->dev, pdata);
  1155. dev_dbg(&pdev->dev, "%s: dev name %s\n",
  1156. __func__, dev_name(&pdev->dev));
  1157. dev_dbg(&pdev->dev, "Pull mode driver register\n");
  1158. rc = snd_soc_register_component(&pdev->dev,
  1159. &msm_soc_component,
  1160. NULL, 0);
  1161. if (rc)
  1162. dev_err(&pdev->dev, "Failed to register pull mode driver\n");
  1163. return rc;
  1164. }
  1165. static int msm_pcm_remove(struct platform_device *pdev)
  1166. {
  1167. struct msm_plat_data *pdata;
  1168. dev_dbg(&pdev->dev, "Pull mode remove\n");
  1169. pdata = dev_get_drvdata(&pdev->dev);
  1170. devm_kfree(&pdev->dev, pdata);
  1171. snd_soc_unregister_component(&pdev->dev);
  1172. return 0;
  1173. }
  1174. static const struct of_device_id msm_pcm_noirq_dt_match[] = {
  1175. {.compatible = "qcom,msm-pcm-dsp-noirq"},
  1176. {}
  1177. };
  1178. MODULE_DEVICE_TABLE(of, msm_pcm_noirq_dt_match);
  1179. static struct platform_driver msm_pcm_driver_noirq = {
  1180. .driver = {
  1181. .name = "msm-pcm-dsp-noirq",
  1182. .owner = THIS_MODULE,
  1183. .of_match_table = msm_pcm_noirq_dt_match,
  1184. .suppress_bind_attrs = true,
  1185. },
  1186. .probe = msm_pcm_probe,
  1187. .remove = msm_pcm_remove,
  1188. };
  1189. int __init msm_pcm_noirq_init(void)
  1190. {
  1191. return platform_driver_register(&msm_pcm_driver_noirq);
  1192. }
  1193. void msm_pcm_noirq_exit(void)
  1194. {
  1195. platform_driver_unregister(&msm_pcm_driver_noirq);
  1196. }
  1197. MODULE_DESCRIPTION("PCM NOIRQ module platform driver");
  1198. MODULE_LICENSE("GPL v2");