msm-pcm-q6-noirq.c 36 KB

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