msm-pcm-q6-noirq.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2016-2018, 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. if (!substream->runtime) {
  391. pr_err("%s substream runtime not found\n", __func__);
  392. return -EFAULT;
  393. }
  394. prtd = substream->runtime->private_data;
  395. if (!prtd || !prtd->audio_client || !prtd->mmap_flag) {
  396. pr_err("%s no audio client or not an mmap session\n", __func__);
  397. return -EINVAL;
  398. }
  399. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  400. dir = IN;
  401. else
  402. dir = OUT;
  403. apd = prtd->audio_client->port;
  404. ab = &(apd[dir].buf[0]);
  405. /*
  406. * Passing O_CLOEXEC as flag passed to fd, to be in sync with
  407. * previous implimentation.
  408. * This was the flag used by previous internal wrapper API, which
  409. * used to call dma_buf_fd internally.
  410. */
  411. mmap_fd->fd = dma_buf_fd(ab->dma_buf, O_CLOEXEC);
  412. if (mmap_fd->fd >= 0) {
  413. mmap_fd->dir = dir;
  414. mmap_fd->actual_size = ab->actual_size;
  415. mmap_fd->size = ab->size;
  416. }
  417. return mmap_fd->fd < 0 ? -EFAULT : 0;
  418. }
  419. static int msm_pcm_ioctl(struct snd_pcm_substream *substream,
  420. unsigned int cmd, void *arg)
  421. {
  422. struct snd_pcm_runtime *runtime = substream->runtime;
  423. struct msm_audio *prtd = runtime->private_data;
  424. int dir = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) ? 0 : 1;
  425. struct audio_buffer *buf;
  426. switch (cmd) {
  427. case SNDRV_PCM_IOCTL1_RESET:
  428. pr_debug("%s: %s SNDRV_PCM_IOCTL1_RESET\n", __func__,
  429. dir == 0 ? "P" : "C");
  430. buf = q6asm_shared_io_buf(prtd->audio_client, dir);
  431. if (buf && buf->data)
  432. memset(buf->data, 0, buf->actual_size);
  433. break;
  434. default:
  435. break;
  436. }
  437. return snd_pcm_lib_ioctl(substream, cmd, arg);
  438. }
  439. #ifdef CONFIG_COMPAT
  440. static int msm_pcm_compat_ioctl(struct snd_pcm_substream *substream,
  441. unsigned int cmd, void *arg)
  442. {
  443. /* we only handle RESET which is common for both modes */
  444. return msm_pcm_ioctl(substream, cmd, arg);
  445. }
  446. #endif
  447. static snd_pcm_uframes_t msm_pcm_pointer(struct snd_pcm_substream *substream)
  448. {
  449. struct snd_pcm_runtime *runtime = substream->runtime;
  450. uint32_t read_index, wall_clk_msw, wall_clk_lsw;
  451. /*these are offsets, unlike ASoC's full values*/
  452. snd_pcm_sframes_t hw_ptr;
  453. snd_pcm_sframes_t period_size;
  454. int ret;
  455. int retries = 10;
  456. struct msm_audio *prtd = runtime->private_data;
  457. period_size = runtime->period_size;
  458. do {
  459. ret = q6asm_get_shared_pos(prtd->audio_client,
  460. &read_index, &wall_clk_msw,
  461. &wall_clk_lsw);
  462. } while (ret == -EAGAIN && --retries);
  463. if (ret || !period_size) {
  464. pr_err("get_shared_pos error or zero period size\n");
  465. return 0;
  466. }
  467. hw_ptr = bytes_to_frames(substream->runtime,
  468. read_index);
  469. if (runtime->control->appl_ptr == 0) {
  470. pr_debug("ptr(%s): appl(0), hw = %lu read_index = %u\n",
  471. prtd->substream->stream == SNDRV_PCM_STREAM_PLAYBACK ?
  472. "P" : "C",
  473. hw_ptr, read_index);
  474. }
  475. return (hw_ptr/period_size) * period_size;
  476. }
  477. static int msm_pcm_copy(struct snd_pcm_substream *substream, int a,
  478. unsigned long hwoff, void __user *buf, unsigned long fbytes)
  479. {
  480. return -EINVAL;
  481. }
  482. static int msm_pcm_mmap(struct snd_pcm_substream *substream,
  483. struct vm_area_struct *vma)
  484. {
  485. struct snd_pcm_runtime *runtime = substream->runtime;
  486. struct msm_audio *prtd = runtime->private_data;
  487. struct audio_client *ac = prtd->audio_client;
  488. struct audio_port_data *apd = ac->port;
  489. struct audio_buffer *ab;
  490. int dir = -1;
  491. int ret;
  492. pr_debug("%s: mmap begin\n", __func__);
  493. prtd->mmap_flag = 1;
  494. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  495. dir = IN;
  496. else
  497. dir = OUT;
  498. ab = &(apd[dir].buf[0]);
  499. ret = msm_audio_ion_mmap(ab, vma);
  500. if (ret)
  501. prtd->mmap_flag = 0;
  502. return ret;
  503. }
  504. static int msm_pcm_prepare(struct snd_pcm_substream *substream)
  505. {
  506. struct snd_pcm_runtime *runtime = substream->runtime;
  507. struct msm_audio *prtd = runtime->private_data;
  508. if (!prtd || !prtd->mmap_flag)
  509. return -EIO;
  510. return 0;
  511. }
  512. static int msm_pcm_close(struct snd_pcm_substream *substream)
  513. {
  514. struct snd_pcm_runtime *runtime = substream->runtime;
  515. struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
  516. struct msm_audio *prtd = runtime->private_data;
  517. struct audio_client *ac = prtd->audio_client;
  518. uint32_t timeout;
  519. int dir = 0;
  520. int ret = 0;
  521. if (ac) {
  522. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  523. dir = IN;
  524. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  525. dir = OUT;
  526. /* determine timeout length */
  527. if (runtime->frame_bits == 0 || runtime->rate == 0) {
  528. timeout = CMD_EOS_MIN_TIMEOUT_LENGTH;
  529. } else {
  530. timeout = (runtime->period_size *
  531. CMD_EOS_TIMEOUT_MULTIPLIER) /
  532. ((runtime->frame_bits / 8) *
  533. runtime->rate);
  534. if (timeout < CMD_EOS_MIN_TIMEOUT_LENGTH)
  535. timeout = CMD_EOS_MIN_TIMEOUT_LENGTH;
  536. }
  537. q6asm_cmd(ac, CMD_CLOSE);
  538. ret = q6asm_shared_io_free(ac, dir);
  539. if (ret) {
  540. pr_err("%s: Failed to close pull mode, ret %d\n",
  541. __func__, ret);
  542. }
  543. q6asm_audio_client_free(ac);
  544. }
  545. msm_pcm_routing_dereg_phy_stream(soc_prtd->dai_link->id,
  546. dir == IN ?
  547. SNDRV_PCM_STREAM_PLAYBACK :
  548. SNDRV_PCM_STREAM_CAPTURE);
  549. kfree(prtd);
  550. runtime->private_data = NULL;
  551. return 0;
  552. }
  553. static int msm_pcm_set_volume(struct msm_audio *prtd, uint32_t volume)
  554. {
  555. int rc = 0;
  556. if (prtd && prtd->audio_client) {
  557. pr_debug("%s: channels %d volume 0x%x\n", __func__,
  558. prtd->channel_mode, volume);
  559. rc = q6asm_set_volume(prtd->audio_client, volume);
  560. if (rc < 0) {
  561. pr_err("%s: Send Volume command failed rc=%d\n",
  562. __func__, rc);
  563. }
  564. }
  565. return rc;
  566. }
  567. static int msm_pcm_volume_ctl_get(struct snd_kcontrol *kcontrol,
  568. struct snd_ctl_elem_value *ucontrol)
  569. {
  570. struct snd_pcm_volume *vol = snd_kcontrol_chip(kcontrol);
  571. struct snd_pcm_substream *substream =
  572. vol->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
  573. struct msm_audio *prtd;
  574. pr_debug("%s\n", __func__);
  575. if (!substream) {
  576. pr_err("%s substream not found\n", __func__);
  577. return -ENODEV;
  578. }
  579. if (!substream->runtime) {
  580. pr_debug("%s substream runtime not found\n", __func__);
  581. return 0;
  582. }
  583. prtd = substream->runtime->private_data;
  584. if (prtd)
  585. ucontrol->value.integer.value[0] = prtd->volume;
  586. return 0;
  587. }
  588. static int msm_pcm_volume_ctl_put(struct snd_kcontrol *kcontrol,
  589. struct snd_ctl_elem_value *ucontrol)
  590. {
  591. int rc = 0;
  592. struct snd_pcm_volume *vol = snd_kcontrol_chip(kcontrol);
  593. struct snd_pcm_substream *substream =
  594. vol->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
  595. struct msm_audio *prtd;
  596. int volume = ucontrol->value.integer.value[0];
  597. pr_debug("%s: volume : 0x%x\n", __func__, volume);
  598. if (!substream) {
  599. pr_err("%s substream not found\n", __func__);
  600. return -ENODEV;
  601. }
  602. if (!substream->runtime) {
  603. pr_err("%s substream runtime not found\n", __func__);
  604. return 0;
  605. }
  606. prtd = substream->runtime->private_data;
  607. if (prtd) {
  608. rc = msm_pcm_set_volume(prtd, volume);
  609. prtd->volume = volume;
  610. }
  611. return rc;
  612. }
  613. static int msm_pcm_add_volume_control(struct snd_soc_pcm_runtime *rtd)
  614. {
  615. int ret = 0;
  616. struct snd_pcm *pcm = rtd->pcm;
  617. struct snd_pcm_volume *volume_info;
  618. struct snd_kcontrol *kctl;
  619. dev_dbg(rtd->dev, "%s, Volume control add\n", __func__);
  620. ret = snd_pcm_add_volume_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  621. NULL, 1, rtd->dai_link->id,
  622. &volume_info);
  623. if (ret < 0) {
  624. pr_err("%s volume control failed ret %d\n", __func__, ret);
  625. return ret;
  626. }
  627. kctl = volume_info->kctl;
  628. kctl->put = msm_pcm_volume_ctl_put;
  629. kctl->get = msm_pcm_volume_ctl_get;
  630. kctl->tlv.p = msm_pcm_vol_gain;
  631. return 0;
  632. }
  633. static int msm_pcm_channel_map_put(struct snd_kcontrol *kcontrol,
  634. struct snd_ctl_elem_value *ucontrol)
  635. {
  636. struct snd_soc_component *pcm = snd_kcontrol_chip(kcontrol);
  637. u64 fe_id = kcontrol->private_value;
  638. struct msm_plat_data *pdata = (struct msm_plat_data *)
  639. snd_soc_component_get_drvdata(pcm);
  640. int rc = 0, i = 0;
  641. pr_debug("%s: fe_id- %llu\n", __func__, fe_id);
  642. if (fe_id >= MSM_FRONTEND_DAI_MAX) {
  643. pr_err("%s Received out of bounds fe_id %llu\n",
  644. __func__, fe_id);
  645. rc = -EINVAL;
  646. goto end;
  647. }
  648. if (pdata->ch_map[fe_id]) {
  649. pdata->ch_map[fe_id]->set_ch_map = true;
  650. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL; i++)
  651. pdata->ch_map[fe_id]->channel_map[i] =
  652. (char)(ucontrol->value.integer.value[i]);
  653. } else {
  654. pr_debug("%s: no memory for ch_map, default will be set\n",
  655. __func__);
  656. }
  657. end:
  658. pr_debug("%s: ret %d\n", __func__, rc);
  659. return rc;
  660. }
  661. static int msm_pcm_channel_map_info(struct snd_kcontrol *kcontrol,
  662. struct snd_ctl_elem_info *uinfo)
  663. {
  664. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  665. uinfo->count = 8;
  666. uinfo->value.integer.min = 0;
  667. uinfo->value.integer.max = 0xFFFFFFFF;
  668. return 0;
  669. }
  670. static int msm_pcm_channel_map_get(struct snd_kcontrol *kcontrol,
  671. struct snd_ctl_elem_value *ucontrol)
  672. {
  673. struct snd_soc_component *pcm = snd_kcontrol_chip(kcontrol);
  674. u64 fe_id = kcontrol->private_value;
  675. struct msm_plat_data *pdata = (struct msm_plat_data *)
  676. snd_soc_component_get_drvdata(pcm);
  677. int rc = 0, i = 0;
  678. pr_debug("%s: fe_id- %llu\n", __func__, fe_id);
  679. if (fe_id >= MSM_FRONTEND_DAI_MAX) {
  680. pr_err("%s: Received out of bounds fe_id %llu\n",
  681. __func__, fe_id);
  682. rc = -EINVAL;
  683. goto end;
  684. }
  685. if (pdata->ch_map[fe_id]) {
  686. for (i = 0; i < PCM_FORMAT_MAX_NUM_CHANNEL; i++)
  687. ucontrol->value.integer.value[i] =
  688. pdata->ch_map[fe_id]->channel_map[i];
  689. }
  690. end:
  691. pr_debug("%s: ret %d\n", __func__, rc);
  692. return rc;
  693. }
  694. static int msm_pcm_add_channel_map_control(struct snd_soc_pcm_runtime *rtd)
  695. {
  696. struct snd_soc_component *component = NULL;
  697. const char *mixer_ctl_name = "Playback Channel Map";
  698. const char *deviceNo = "NN";
  699. char *mixer_str = NULL;
  700. struct msm_plat_data *pdata = NULL;
  701. int ctl_len = 0;
  702. struct snd_kcontrol_new fe_channel_map_control[1] = {
  703. {
  704. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  705. .name = "?",
  706. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  707. .info = msm_pcm_channel_map_info,
  708. .get = msm_pcm_channel_map_get,
  709. .put = msm_pcm_channel_map_put,
  710. .private_value = 0,
  711. }
  712. };
  713. if (!rtd) {
  714. pr_err("%s: NULL rtd\n", __func__);
  715. return -EINVAL;
  716. }
  717. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  718. if (!component) {
  719. pr_err("%s: component is NULL\n", __func__);
  720. return -EINVAL;
  721. }
  722. pr_debug("%s: added new pcm FE with name %s, id %d, cpu dai %s, device no %d\n",
  723. __func__, rtd->dai_link->name, rtd->dai_link->id,
  724. rtd->dai_link->cpu_dai_name, rtd->pcm->device);
  725. ctl_len = strlen(mixer_ctl_name) + strlen(deviceNo) + 1;
  726. mixer_str = kzalloc(ctl_len, GFP_KERNEL);
  727. if (!mixer_str)
  728. return -ENOMEM;
  729. snprintf(mixer_str, ctl_len, "%s%d", mixer_ctl_name, rtd->pcm->device);
  730. fe_channel_map_control[0].name = mixer_str;
  731. fe_channel_map_control[0].private_value = rtd->dai_link->id;
  732. pr_debug("%s: Registering new mixer ctl %s\n", __func__, mixer_str);
  733. snd_soc_add_component_controls(component,
  734. fe_channel_map_control,
  735. ARRAY_SIZE(fe_channel_map_control));
  736. pdata = snd_soc_component_get_drvdata(component);
  737. pdata->ch_map[rtd->dai_link->id] =
  738. kzalloc(sizeof(struct msm_pcm_ch_map), GFP_KERNEL);
  739. if (!pdata->ch_map[rtd->dai_link->id]) {
  740. pr_err("%s: Could not allocate memory for channel map\n",
  741. __func__);
  742. kfree(mixer_str);
  743. return -ENOMEM;
  744. }
  745. kfree(mixer_str);
  746. return 0;
  747. }
  748. static int msm_pcm_fe_topology_info(struct snd_kcontrol *kcontrol,
  749. struct snd_ctl_elem_info *uinfo)
  750. {
  751. const struct soc_enum *e = &msm_pcm_fe_topology_enum[0];
  752. return snd_ctl_enum_info(uinfo, 1, e->items, e->texts);
  753. }
  754. static int msm_pcm_fe_topology_get(struct snd_kcontrol *kcontrol,
  755. struct snd_ctl_elem_value *ucontrol)
  756. {
  757. unsigned long fe_id = kcontrol->private_value;
  758. if (fe_id >= MSM_FRONTEND_DAI_MAX) {
  759. pr_err("%s Received out of bound fe_id %lu\n", __func__, fe_id);
  760. return -EINVAL;
  761. }
  762. pr_debug("%s: %lu topology %s\n", __func__, fe_id,
  763. msm_pcm_fe_topology_text[msm_pcm_fe_topology[fe_id]]);
  764. ucontrol->value.enumerated.item[0] = msm_pcm_fe_topology[fe_id];
  765. return 0;
  766. }
  767. static int msm_pcm_fe_topology_put(struct snd_kcontrol *kcontrol,
  768. struct snd_ctl_elem_value *ucontrol)
  769. {
  770. unsigned long fe_id = kcontrol->private_value;
  771. unsigned int item;
  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. item = ucontrol->value.enumerated.item[0];
  777. if (item >= ARRAY_SIZE(msm_pcm_fe_topology_text)) {
  778. pr_err("%s Received out of bound topology %lu\n", __func__,
  779. fe_id);
  780. return -EINVAL;
  781. }
  782. pr_debug("%s: %lu new topology %s\n", __func__, fe_id,
  783. msm_pcm_fe_topology_text[item]);
  784. msm_pcm_fe_topology[fe_id] = item;
  785. return 0;
  786. }
  787. static int msm_pcm_add_fe_topology_control(struct snd_soc_pcm_runtime *rtd)
  788. {
  789. struct snd_soc_component *component = NULL;
  790. const char *mixer_ctl_name = "PCM_Dev";
  791. const char *deviceNo = "NN";
  792. const char *topo_text = "Topology";
  793. char *mixer_str = NULL;
  794. int ctl_len;
  795. int ret;
  796. struct snd_kcontrol_new topology_control[1] = {
  797. {
  798. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  799. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  800. .name = "?",
  801. .info = msm_pcm_fe_topology_info,
  802. .get = msm_pcm_fe_topology_get,
  803. .put = msm_pcm_fe_topology_put,
  804. .private_value = 0,
  805. },
  806. };
  807. component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  808. if (!component) {
  809. pr_err("%s: component is NULL\n", __func__);
  810. return -EINVAL;
  811. }
  812. ctl_len = strlen(mixer_ctl_name) + 1 + strlen(deviceNo) + 1 +
  813. strlen(topo_text) + 1;
  814. mixer_str = kzalloc(ctl_len, GFP_KERNEL);
  815. if (!mixer_str)
  816. return -ENOMEM;
  817. snprintf(mixer_str, ctl_len, "%s %d %s", mixer_ctl_name,
  818. rtd->pcm->device, topo_text);
  819. topology_control[0].name = mixer_str;
  820. topology_control[0].private_value = rtd->dai_link->id;
  821. ret = snd_soc_add_component_controls(component, topology_control,
  822. ARRAY_SIZE(topology_control));
  823. msm_pcm_fe_topology[rtd->dai_link->id] = 0;
  824. kfree(mixer_str);
  825. return ret;
  826. }
  827. static int msm_pcm_playback_app_type_cfg_ctl_put(struct snd_kcontrol *kcontrol,
  828. struct snd_ctl_elem_value *ucontrol)
  829. {
  830. u64 fe_id = kcontrol->private_value;
  831. int session_type = SESSION_TYPE_RX;
  832. int be_id = ucontrol->value.integer.value[3];
  833. struct msm_pcm_stream_app_type_cfg cfg_data = {0, 0, 48000};
  834. int ret = 0;
  835. cfg_data.app_type = ucontrol->value.integer.value[0];
  836. cfg_data.acdb_dev_id = ucontrol->value.integer.value[1];
  837. if (ucontrol->value.integer.value[2] != 0)
  838. cfg_data.sample_rate = ucontrol->value.integer.value[2];
  839. pr_debug("%s: fe_id- %llu session_type- %d be_id- %d app_type- %d acdb_dev_id- %d sample_rate- %d\n",
  840. __func__, fe_id, session_type, be_id,
  841. cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
  842. ret = msm_pcm_routing_reg_stream_app_type_cfg(fe_id, session_type,
  843. be_id, &cfg_data);
  844. if (ret < 0)
  845. pr_err("%s: msm_pcm_routing_reg_stream_app_type_cfg failed returned %d\n",
  846. __func__, ret);
  847. return ret;
  848. }
  849. static int msm_pcm_playback_app_type_cfg_ctl_get(struct snd_kcontrol *kcontrol,
  850. struct snd_ctl_elem_value *ucontrol)
  851. {
  852. u64 fe_id = kcontrol->private_value;
  853. int session_type = SESSION_TYPE_RX;
  854. int be_id = 0;
  855. struct msm_pcm_stream_app_type_cfg cfg_data = {0};
  856. int ret = 0;
  857. ret = msm_pcm_routing_get_stream_app_type_cfg(fe_id, session_type,
  858. &be_id, &cfg_data);
  859. if (ret < 0) {
  860. pr_err("%s: msm_pcm_routing_get_stream_app_type_cfg failed returned %d\n",
  861. __func__, ret);
  862. goto done;
  863. }
  864. ucontrol->value.integer.value[0] = cfg_data.app_type;
  865. ucontrol->value.integer.value[1] = cfg_data.acdb_dev_id;
  866. ucontrol->value.integer.value[2] = cfg_data.sample_rate;
  867. ucontrol->value.integer.value[3] = be_id;
  868. pr_debug("%s: fedai_id %llu, session_type %d, be_id %d, app_type %d, acdb_dev_id %d, sample_rate %d\n",
  869. __func__, fe_id, session_type, be_id,
  870. cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
  871. done:
  872. return ret;
  873. }
  874. static int msm_pcm_capture_app_type_cfg_ctl_put(struct snd_kcontrol *kcontrol,
  875. struct snd_ctl_elem_value *ucontrol)
  876. {
  877. u64 fe_id = kcontrol->private_value;
  878. int session_type = SESSION_TYPE_TX;
  879. int be_id = ucontrol->value.integer.value[3];
  880. struct msm_pcm_stream_app_type_cfg cfg_data = {0, 0, 48000};
  881. int ret = 0;
  882. cfg_data.app_type = ucontrol->value.integer.value[0];
  883. cfg_data.acdb_dev_id = ucontrol->value.integer.value[1];
  884. if (ucontrol->value.integer.value[2] != 0)
  885. cfg_data.sample_rate = ucontrol->value.integer.value[2];
  886. pr_debug("%s: fe_id- %llu session_type- %d be_id- %d app_type- %d acdb_dev_id- %d sample_rate- %d\n",
  887. __func__, fe_id, session_type, be_id,
  888. cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
  889. ret = msm_pcm_routing_reg_stream_app_type_cfg(fe_id, session_type,
  890. be_id, &cfg_data);
  891. if (ret < 0)
  892. pr_err("%s: msm_pcm_routing_reg_stream_app_type_cfg failed returned %d\n",
  893. __func__, ret);
  894. return ret;
  895. }
  896. static int msm_pcm_capture_app_type_cfg_ctl_get(struct snd_kcontrol *kcontrol,
  897. struct snd_ctl_elem_value *ucontrol)
  898. {
  899. u64 fe_id = kcontrol->private_value;
  900. int session_type = SESSION_TYPE_TX;
  901. int be_id = 0;
  902. struct msm_pcm_stream_app_type_cfg cfg_data = {0};
  903. int ret = 0;
  904. ret = msm_pcm_routing_get_stream_app_type_cfg(fe_id, session_type,
  905. &be_id, &cfg_data);
  906. if (ret < 0) {
  907. pr_err("%s: msm_pcm_routing_get_stream_app_type_cfg failed returned %d\n",
  908. __func__, ret);
  909. goto done;
  910. }
  911. ucontrol->value.integer.value[0] = cfg_data.app_type;
  912. ucontrol->value.integer.value[1] = cfg_data.acdb_dev_id;
  913. ucontrol->value.integer.value[2] = cfg_data.sample_rate;
  914. ucontrol->value.integer.value[3] = be_id;
  915. pr_debug("%s: fedai_id %llu, session_type %d, be_id %d, app_type %d, acdb_dev_id %d, sample_rate %d\n",
  916. __func__, fe_id, session_type, be_id,
  917. cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
  918. done:
  919. return ret;
  920. }
  921. static int msm_pcm_add_app_type_controls(struct snd_soc_pcm_runtime *rtd)
  922. {
  923. struct snd_pcm *pcm = rtd->pcm;
  924. struct snd_pcm_usr *app_type_info;
  925. struct snd_kcontrol *kctl;
  926. const char *playback_mixer_ctl_name = "Audio Stream";
  927. const char *capture_mixer_ctl_name = "Audio Stream Capture";
  928. const char *deviceNo = "NN";
  929. const char *suffix = "App Type Cfg";
  930. int ctl_len, ret = 0;
  931. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
  932. ctl_len = strlen(playback_mixer_ctl_name) + 1 +
  933. strlen(deviceNo) + 1 +
  934. strlen(suffix) + 1;
  935. pr_debug("%s: Playback app type cntrl add\n", __func__);
  936. ret = snd_pcm_add_usr_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  937. NULL, 1, ctl_len, rtd->dai_link->id,
  938. &app_type_info);
  939. if (ret < 0) {
  940. pr_err("%s: playback app type cntrl add failed, err: %d\n",
  941. __func__, ret);
  942. return ret;
  943. }
  944. kctl = app_type_info->kctl;
  945. snprintf(kctl->id.name, ctl_len, "%s %d %s",
  946. playback_mixer_ctl_name, rtd->pcm->device, suffix);
  947. kctl->put = msm_pcm_playback_app_type_cfg_ctl_put;
  948. kctl->get = msm_pcm_playback_app_type_cfg_ctl_get;
  949. }
  950. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
  951. ctl_len = strlen(capture_mixer_ctl_name) + 1 +
  952. strlen(deviceNo) + 1 + strlen(suffix) + 1;
  953. pr_debug("%s: Capture app type cntrl add\n", __func__);
  954. ret = snd_pcm_add_usr_ctls(pcm, SNDRV_PCM_STREAM_CAPTURE,
  955. NULL, 1, ctl_len, rtd->dai_link->id,
  956. &app_type_info);
  957. if (ret < 0) {
  958. pr_err("%s: capture app type cntrl add failed, err: %d\n",
  959. __func__, ret);
  960. return ret;
  961. }
  962. kctl = app_type_info->kctl;
  963. snprintf(kctl->id.name, ctl_len, "%s %d %s",
  964. capture_mixer_ctl_name, rtd->pcm->device, suffix);
  965. kctl->put = msm_pcm_capture_app_type_cfg_ctl_put;
  966. kctl->get = msm_pcm_capture_app_type_cfg_ctl_get;
  967. }
  968. return 0;
  969. }
  970. static int msm_pcm_hwdep_ioctl(struct snd_hwdep *hw, struct file *file,
  971. unsigned int cmd, unsigned long arg)
  972. {
  973. int ret = 0;
  974. struct snd_pcm *pcm = hw->private_data;
  975. struct snd_pcm_mmap_fd __user *_mmap_fd = NULL;
  976. struct snd_pcm_mmap_fd mmap_fd;
  977. struct snd_pcm_substream *substream = NULL;
  978. int32_t dir = -1;
  979. switch (cmd) {
  980. case SNDRV_PCM_IOCTL_MMAP_DATA_FD:
  981. _mmap_fd = (struct snd_pcm_mmap_fd __user *)arg;
  982. if (get_user(dir, (int32_t __user *)&(_mmap_fd->dir))) {
  983. pr_err("%s: error copying mmap_fd from user\n",
  984. __func__);
  985. ret = -EFAULT;
  986. break;
  987. }
  988. if (dir != OUT && dir != IN) {
  989. pr_err("%s invalid stream dir\n", __func__);
  990. ret = -EINVAL;
  991. break;
  992. }
  993. substream = pcm->streams[dir].substream;
  994. if (!substream) {
  995. pr_err("%s substream not found\n", __func__);
  996. ret = -ENODEV;
  997. break;
  998. }
  999. pr_debug("%s : %s MMAP Data fd\n", __func__,
  1000. dir == 0 ? "P" : "C");
  1001. if (msm_pcm_mmap_fd(substream, &mmap_fd) < 0) {
  1002. pr_err("%s: error getting fd\n",
  1003. __func__);
  1004. ret = -EFAULT;
  1005. break;
  1006. }
  1007. if (put_user(mmap_fd.fd, &_mmap_fd->fd) ||
  1008. put_user(mmap_fd.size, &_mmap_fd->size) ||
  1009. put_user(mmap_fd.actual_size, &_mmap_fd->actual_size)) {
  1010. pr_err("%s: error copying fd\n", __func__);
  1011. return -EFAULT;
  1012. }
  1013. break;
  1014. default:
  1015. ret = -EINVAL;
  1016. break;
  1017. }
  1018. return ret;
  1019. }
  1020. #ifdef CONFIG_COMPAT
  1021. static int msm_pcm_hwdep_compat_ioctl(struct snd_hwdep *hw,
  1022. struct file *file,
  1023. unsigned int cmd,
  1024. unsigned long arg)
  1025. {
  1026. /* we only support mmap fd. Handling is common in both modes */
  1027. return msm_pcm_hwdep_ioctl(hw, file, cmd, arg);
  1028. }
  1029. #else
  1030. static int msm_pcm_hwdep_compat_ioctl(struct snd_hwdep *hw,
  1031. struct file *file,
  1032. unsigned int cmd,
  1033. unsigned long arg)
  1034. {
  1035. return -EINVAL;
  1036. }
  1037. #endif
  1038. static int msm_pcm_add_hwdep_dev(struct snd_soc_pcm_runtime *runtime)
  1039. {
  1040. struct snd_hwdep *hwdep;
  1041. int rc;
  1042. char id[] = "NOIRQ_NN";
  1043. snprintf(id, sizeof(id), "NOIRQ_%d", runtime->pcm->device);
  1044. pr_debug("%s: pcm dev %d\n", __func__, runtime->pcm->device);
  1045. rc = snd_hwdep_new(runtime->card->snd_card,
  1046. &id[0],
  1047. HWDEP_FE_BASE + runtime->pcm->device,
  1048. &hwdep);
  1049. if (!hwdep || rc < 0) {
  1050. pr_err("%s: hwdep intf failed to create %s - hwdep\n", __func__,
  1051. id);
  1052. return rc;
  1053. }
  1054. hwdep->iface = SNDRV_HWDEP_IFACE_AUDIO_BE; /* for lack of a FE iface */
  1055. hwdep->private_data = runtime->pcm; /* of type struct snd_pcm */
  1056. hwdep->ops.ioctl = msm_pcm_hwdep_ioctl;
  1057. hwdep->ops.ioctl_compat = msm_pcm_hwdep_compat_ioctl;
  1058. return 0;
  1059. }
  1060. static int msm_asoc_pcm_new(struct snd_soc_pcm_runtime *rtd)
  1061. {
  1062. struct snd_card *card = rtd->card->snd_card;
  1063. struct snd_pcm *pcm = rtd->pcm;
  1064. int ret;
  1065. pr_debug("%s , register new control\n", __func__);
  1066. if (!card->dev->coherent_dma_mask)
  1067. card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
  1068. ret = msm_pcm_add_channel_map_control(rtd);
  1069. if (ret)
  1070. pr_err("%s: Could not add pcm Channel Map Control\n",
  1071. __func__);
  1072. ret = msm_pcm_add_volume_control(rtd);
  1073. if (ret) {
  1074. pr_err("%s: Could not add pcm Volume Control %d\n",
  1075. __func__, ret);
  1076. }
  1077. ret = msm_pcm_add_fe_topology_control(rtd);
  1078. if (ret) {
  1079. pr_err("%s: Could not add pcm topology control %d\n",
  1080. __func__, ret);
  1081. }
  1082. ret = msm_pcm_add_app_type_controls(rtd);
  1083. if (ret) {
  1084. pr_err("%s: Could not add app type controls failed %d\n",
  1085. __func__, ret);
  1086. }
  1087. ret = msm_pcm_add_hwdep_dev(rtd);
  1088. if (ret)
  1089. pr_err("%s: Could not add hw dep node\n", __func__);
  1090. pcm->nonatomic = true;
  1091. return ret;
  1092. }
  1093. static const struct snd_pcm_ops msm_pcm_ops = {
  1094. .open = msm_pcm_open,
  1095. .prepare = msm_pcm_prepare,
  1096. .copy_user = msm_pcm_copy,
  1097. .hw_params = msm_pcm_hw_params,
  1098. .ioctl = msm_pcm_ioctl,
  1099. #ifdef CONFIG_COMPAT
  1100. .compat_ioctl = msm_pcm_compat_ioctl,
  1101. #endif
  1102. .trigger = msm_pcm_trigger,
  1103. .pointer = msm_pcm_pointer,
  1104. .mmap = msm_pcm_mmap,
  1105. .close = msm_pcm_close,
  1106. };
  1107. static struct snd_soc_component_driver msm_soc_component = {
  1108. .name = DRV_NAME,
  1109. .ops = &msm_pcm_ops,
  1110. .pcm_new = msm_asoc_pcm_new,
  1111. };
  1112. static int msm_pcm_probe(struct platform_device *pdev)
  1113. {
  1114. int rc;
  1115. struct msm_plat_data *pdata;
  1116. const char *latency_level;
  1117. int perf_mode = LOW_LATENCY_PCM_MODE;
  1118. dev_dbg(&pdev->dev, "Pull mode driver probe\n");
  1119. if (of_property_read_bool(pdev->dev.of_node,
  1120. "qcom,msm-pcm-low-latency")) {
  1121. rc = of_property_read_string(pdev->dev.of_node,
  1122. "qcom,latency-level", &latency_level);
  1123. if (!rc) {
  1124. if (!strcmp(latency_level, "ultra"))
  1125. perf_mode = ULTRA_LOW_LATENCY_PCM_MODE;
  1126. else if (!strcmp(latency_level, "ull-pp"))
  1127. perf_mode = ULL_POST_PROCESSING_PCM_MODE;
  1128. }
  1129. }
  1130. pdata = devm_kzalloc(&pdev->dev,
  1131. sizeof(struct msm_plat_data), GFP_KERNEL);
  1132. if (!pdata)
  1133. return -ENOMEM;
  1134. pdata->perf_mode = perf_mode;
  1135. dev_set_drvdata(&pdev->dev, pdata);
  1136. dev_dbg(&pdev->dev, "%s: dev name %s\n",
  1137. __func__, dev_name(&pdev->dev));
  1138. dev_dbg(&pdev->dev, "Pull mode driver register\n");
  1139. rc = snd_soc_register_component(&pdev->dev,
  1140. &msm_soc_component,
  1141. NULL, 0);
  1142. if (rc)
  1143. dev_err(&pdev->dev, "Failed to register pull mode driver\n");
  1144. return rc;
  1145. }
  1146. static int msm_pcm_remove(struct platform_device *pdev)
  1147. {
  1148. struct msm_plat_data *pdata;
  1149. dev_dbg(&pdev->dev, "Pull mode remove\n");
  1150. pdata = dev_get_drvdata(&pdev->dev);
  1151. devm_kfree(&pdev->dev, pdata);
  1152. snd_soc_unregister_component(&pdev->dev);
  1153. return 0;
  1154. }
  1155. static const struct of_device_id msm_pcm_noirq_dt_match[] = {
  1156. {.compatible = "qcom,msm-pcm-dsp-noirq"},
  1157. {}
  1158. };
  1159. MODULE_DEVICE_TABLE(of, msm_pcm_noirq_dt_match);
  1160. static struct platform_driver msm_pcm_driver_noirq = {
  1161. .driver = {
  1162. .name = "msm-pcm-dsp-noirq",
  1163. .owner = THIS_MODULE,
  1164. .of_match_table = msm_pcm_noirq_dt_match,
  1165. },
  1166. .probe = msm_pcm_probe,
  1167. .remove = msm_pcm_remove,
  1168. };
  1169. int __init msm_pcm_noirq_init(void)
  1170. {
  1171. return platform_driver_register(&msm_pcm_driver_noirq);
  1172. }
  1173. void msm_pcm_noirq_exit(void)
  1174. {
  1175. platform_driver_unregister(&msm_pcm_driver_noirq);
  1176. }
  1177. MODULE_DESCRIPTION("PCM NOIRQ module platform driver");
  1178. MODULE_LICENSE("GPL v2");