msm-compr-q6.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410
  1. /* Copyright (c) 2011-2013, 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 <sound/core.h>
  21. #include <sound/soc.h>
  22. #include <sound/soc-dapm.h>
  23. #include <sound/pcm.h>
  24. #include <sound/initval.h>
  25. #include <sound/control.h>
  26. #include <sound/q6asm.h>
  27. #include <sound/pcm_params.h>
  28. #include <asm/dma.h>
  29. #include <linux/dma-mapping.h>
  30. #include <sound/timer.h>
  31. #include <mach/qdsp6v2/q6core.h>
  32. #include <sound/pcm.h>
  33. #include "msm-compr-q6.h"
  34. #include "msm-pcm-routing.h"
  35. #define COMPRE_CAPTURE_NUM_PERIODS 16
  36. /* Allocate the worst case frame size for compressed audio */
  37. #define COMPRE_CAPTURE_HEADER_SIZE (sizeof(struct snd_compr_audio_info))
  38. #define COMPRE_CAPTURE_MAX_FRAME_SIZE (6144)
  39. #define COMPRE_CAPTURE_PERIOD_SIZE ((COMPRE_CAPTURE_MAX_FRAME_SIZE + \
  40. COMPRE_CAPTURE_HEADER_SIZE) * \
  41. MAX_NUM_FRAMES_PER_BUFFER)
  42. #define COMPRE_OUTPUT_METADATA_SIZE (sizeof(struct output_meta_data_st))
  43. struct snd_msm {
  44. struct msm_audio *prtd;
  45. unsigned volume;
  46. };
  47. static struct snd_msm compressed_audio = {NULL, 0x2000} ;
  48. static struct audio_locks the_locks;
  49. static struct snd_pcm_hardware msm_compr_hardware_capture = {
  50. .info = (SNDRV_PCM_INFO_MMAP |
  51. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  52. SNDRV_PCM_INFO_MMAP_VALID |
  53. SNDRV_PCM_INFO_INTERLEAVED |
  54. SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME),
  55. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  56. .rates = SNDRV_PCM_RATE_8000_48000,
  57. .rate_min = 8000,
  58. .rate_max = 48000,
  59. .channels_min = 1,
  60. .channels_max = 8,
  61. .buffer_bytes_max =
  62. COMPRE_CAPTURE_PERIOD_SIZE * COMPRE_CAPTURE_NUM_PERIODS ,
  63. .period_bytes_min = COMPRE_CAPTURE_PERIOD_SIZE,
  64. .period_bytes_max = COMPRE_CAPTURE_PERIOD_SIZE,
  65. .periods_min = COMPRE_CAPTURE_NUM_PERIODS,
  66. .periods_max = COMPRE_CAPTURE_NUM_PERIODS,
  67. .fifo_size = 0,
  68. };
  69. static struct snd_pcm_hardware msm_compr_hardware_playback = {
  70. .info = (SNDRV_PCM_INFO_MMAP |
  71. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  72. SNDRV_PCM_INFO_MMAP_VALID |
  73. SNDRV_PCM_INFO_INTERLEAVED |
  74. SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME),
  75. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  76. .rates = SNDRV_PCM_RATE_8000_48000 | SNDRV_PCM_RATE_KNOT,
  77. .rate_min = 8000,
  78. .rate_max = 48000,
  79. .channels_min = 1,
  80. .channels_max = 8,
  81. .buffer_bytes_max = 1024 * 1024,
  82. .period_bytes_min = 128 * 1024,
  83. .period_bytes_max = 256 * 1024,
  84. .periods_min = 4,
  85. .periods_max = 8,
  86. .fifo_size = 0,
  87. };
  88. /* Conventional and unconventional sample rate supported */
  89. static unsigned int supported_sample_rates[] = {
  90. 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000
  91. };
  92. static struct snd_pcm_hw_constraint_list constraints_sample_rates = {
  93. .count = ARRAY_SIZE(supported_sample_rates),
  94. .list = supported_sample_rates,
  95. .mask = 0,
  96. };
  97. static void compr_event_handler(uint32_t opcode,
  98. uint32_t token, uint32_t *payload, void *priv)
  99. {
  100. struct compr_audio *compr = priv;
  101. struct msm_audio *prtd = &compr->prtd;
  102. struct snd_pcm_substream *substream = prtd->substream;
  103. struct snd_pcm_runtime *runtime = substream->runtime;
  104. struct audio_aio_write_param param;
  105. struct audio_aio_read_param read_param;
  106. struct audio_buffer *buf = NULL;
  107. struct output_meta_data_st output_meta_data;
  108. uint32_t *ptrmem = (uint32_t *)payload;
  109. int i = 0;
  110. int time_stamp_flag = 0;
  111. int buffer_length = 0;
  112. pr_debug("%s opcode =%08x\n", __func__, opcode);
  113. switch (opcode) {
  114. case ASM_DATA_EVENT_WRITE_DONE: {
  115. uint32_t *ptrmem = (uint32_t *)&param;
  116. pr_debug("ASM_DATA_EVENT_WRITE_DONE\n");
  117. pr_debug("Buffer Consumed = 0x%08x\n", *ptrmem);
  118. prtd->pcm_irq_pos += prtd->pcm_count;
  119. if (atomic_read(&prtd->start))
  120. snd_pcm_period_elapsed(substream);
  121. else
  122. if (substream->timer_running)
  123. snd_timer_interrupt(substream->timer, 1);
  124. atomic_inc(&prtd->out_count);
  125. wake_up(&the_locks.write_wait);
  126. if (!atomic_read(&prtd->start)) {
  127. atomic_set(&prtd->pending_buffer, 1);
  128. break;
  129. } else
  130. atomic_set(&prtd->pending_buffer, 0);
  131. if (runtime->status->hw_ptr >= runtime->control->appl_ptr) {
  132. runtime->render_flag |= SNDRV_RENDER_STOPPED;
  133. atomic_set(&prtd->pending_buffer, 1);
  134. pr_debug("%s:compr driver underrun hw_ptr = %ld appl_ptr = %ld\n",
  135. __func__, runtime->status->hw_ptr,
  136. runtime->control->appl_ptr);
  137. break;
  138. }
  139. buf = prtd->audio_client->port[IN].buf;
  140. pr_debug("%s:writing %d bytes of buffer[%d] to dsp 2\n",
  141. __func__, prtd->pcm_count, prtd->out_head);
  142. pr_debug("%s:writing buffer[%d] from 0x%08x\n",
  143. __func__, prtd->out_head,
  144. ((unsigned int)buf[0].phys
  145. + (prtd->out_head * prtd->pcm_count)));
  146. if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
  147. time_stamp_flag = SET_TIMESTAMP;
  148. else
  149. time_stamp_flag = NO_TIMESTAMP;
  150. memcpy(&output_meta_data, (char *)(buf->data +
  151. prtd->out_head * prtd->pcm_count),
  152. COMPRE_OUTPUT_METADATA_SIZE);
  153. buffer_length = output_meta_data.frame_size;
  154. pr_debug("meta_data_length: %d, frame_length: %d\n",
  155. output_meta_data.meta_data_length,
  156. output_meta_data.frame_size);
  157. pr_debug("timestamp_msw: %d, timestamp_lsw: %d\n",
  158. output_meta_data.timestamp_msw,
  159. output_meta_data.timestamp_lsw);
  160. if (buffer_length == 0) {
  161. pr_debug("Recieved a zero length buffer-break out");
  162. break;
  163. }
  164. param.paddr = (unsigned long)buf[0].phys
  165. + (prtd->out_head * prtd->pcm_count)
  166. + output_meta_data.meta_data_length;
  167. param.len = buffer_length;
  168. param.msw_ts = output_meta_data.timestamp_msw;
  169. param.lsw_ts = output_meta_data.timestamp_lsw;
  170. param.flags = time_stamp_flag;
  171. param.uid = (unsigned long)buf[0].phys
  172. + (prtd->out_head * prtd->pcm_count
  173. + output_meta_data.meta_data_length);
  174. for (i = 0; i < sizeof(struct audio_aio_write_param)/4;
  175. i++, ++ptrmem)
  176. pr_debug("cmd[%d]=0x%08x\n", i, *ptrmem);
  177. if (q6asm_async_write(prtd->audio_client,
  178. &param) < 0)
  179. pr_err("%s:q6asm_async_write failed\n",
  180. __func__);
  181. else
  182. prtd->out_head =
  183. (prtd->out_head + 1) & (runtime->periods - 1);
  184. break;
  185. }
  186. case ASM_DATA_CMDRSP_EOS:
  187. pr_debug("ASM_DATA_CMDRSP_EOS\n");
  188. if (atomic_read(&prtd->eos)) {
  189. pr_debug("ASM_DATA_CMDRSP_EOS wake up\n");
  190. prtd->cmd_ack = 1;
  191. wake_up(&the_locks.eos_wait);
  192. atomic_set(&prtd->eos, 0);
  193. }
  194. atomic_set(&prtd->pending_buffer, 1);
  195. break;
  196. case ASM_DATA_EVENT_READ_DONE: {
  197. pr_debug("ASM_DATA_EVENT_READ_DONE\n");
  198. pr_debug("buf = %p, data = 0x%X, *data = %p,\n"
  199. "prtd->pcm_irq_pos = %d\n",
  200. prtd->audio_client->port[OUT].buf,
  201. *(uint32_t *)prtd->audio_client->port[OUT].buf->data,
  202. prtd->audio_client->port[OUT].buf->data,
  203. prtd->pcm_irq_pos);
  204. memcpy(prtd->audio_client->port[OUT].buf->data +
  205. prtd->pcm_irq_pos, (ptrmem + 2),
  206. COMPRE_CAPTURE_HEADER_SIZE);
  207. pr_debug("buf = %p, updated data = 0x%X, *data = %p\n",
  208. prtd->audio_client->port[OUT].buf,
  209. *(uint32_t *)(prtd->audio_client->port[OUT].buf->data +
  210. prtd->pcm_irq_pos),
  211. prtd->audio_client->port[OUT].buf->data);
  212. if (!atomic_read(&prtd->start))
  213. break;
  214. pr_debug("frame size=%d, buffer = 0x%X\n", ptrmem[2],
  215. ptrmem[1]);
  216. if (ptrmem[2] > COMPRE_CAPTURE_MAX_FRAME_SIZE) {
  217. pr_err("Frame length exceeded the max length");
  218. break;
  219. }
  220. buf = prtd->audio_client->port[OUT].buf;
  221. pr_debug("pcm_irq_pos=%d, buf[0].phys = 0x%X\n",
  222. prtd->pcm_irq_pos, (uint32_t)buf[0].phys);
  223. read_param.len = prtd->pcm_count - COMPRE_CAPTURE_HEADER_SIZE;
  224. read_param.paddr = (unsigned long)(buf[0].phys) +
  225. prtd->pcm_irq_pos + COMPRE_CAPTURE_HEADER_SIZE;
  226. prtd->pcm_irq_pos += prtd->pcm_count;
  227. if (atomic_read(&prtd->start))
  228. snd_pcm_period_elapsed(substream);
  229. q6asm_async_read(prtd->audio_client, &read_param);
  230. break;
  231. }
  232. case ASM_DATA_EVENT_READ_COMPRESSED_DONE: {
  233. pr_debug("ASM_DATA_EVENT_READ_COMPRESSED_DONE\n");
  234. pr_debug("buf = %p, data = 0x%X, *data = %p,\n"
  235. "prtd->pcm_irq_pos = %d\n",
  236. prtd->audio_client->port[OUT].buf,
  237. *(uint32_t *)prtd->audio_client->port[OUT].buf->data,
  238. prtd->audio_client->port[OUT].buf->data,
  239. prtd->pcm_irq_pos);
  240. if (!atomic_read(&prtd->start))
  241. break;
  242. buf = prtd->audio_client->port[OUT].buf;
  243. pr_debug("pcm_irq_pos=%d, buf[0].phys = 0x%X\n",
  244. prtd->pcm_irq_pos, (uint32_t)buf[0].phys);
  245. read_param.len = prtd->pcm_count;
  246. read_param.paddr = (unsigned long)(buf[0].phys) +
  247. prtd->pcm_irq_pos;
  248. prtd->pcm_irq_pos += prtd->pcm_count;
  249. if (atomic_read(&prtd->start))
  250. snd_pcm_period_elapsed(substream);
  251. q6asm_async_read_compressed(prtd->audio_client, &read_param);
  252. break;
  253. }
  254. case APR_BASIC_RSP_RESULT: {
  255. switch (payload[0]) {
  256. case ASM_SESSION_CMD_RUN: {
  257. if (substream->stream
  258. != SNDRV_PCM_STREAM_PLAYBACK) {
  259. atomic_set(&prtd->start, 1);
  260. break;
  261. }
  262. if (!atomic_read(&prtd->pending_buffer))
  263. break;
  264. pr_debug("%s:writing %d bytes"
  265. " of buffer[%d] to dsp\n",
  266. __func__, prtd->pcm_count, prtd->out_head);
  267. buf = prtd->audio_client->port[IN].buf;
  268. pr_debug("%s:writing buffer[%d] from 0x%08x\n",
  269. __func__, prtd->out_head,
  270. ((unsigned int)buf[0].phys
  271. + (prtd->out_head * prtd->pcm_count)));
  272. if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
  273. time_stamp_flag = SET_TIMESTAMP;
  274. else
  275. time_stamp_flag = NO_TIMESTAMP;
  276. memcpy(&output_meta_data, (char *)(buf->data +
  277. prtd->out_head * prtd->pcm_count),
  278. COMPRE_OUTPUT_METADATA_SIZE);
  279. buffer_length = output_meta_data.frame_size;
  280. pr_debug("meta_data_length: %d, frame_length: %d\n",
  281. output_meta_data.meta_data_length,
  282. output_meta_data.frame_size);
  283. pr_debug("timestamp_msw: %d, timestamp_lsw: %d\n",
  284. output_meta_data.timestamp_msw,
  285. output_meta_data.timestamp_lsw);
  286. param.paddr = (unsigned long)buf[prtd->out_head].phys
  287. + output_meta_data.meta_data_length;
  288. param.len = buffer_length;
  289. param.msw_ts = output_meta_data.timestamp_msw;
  290. param.lsw_ts = output_meta_data.timestamp_lsw;
  291. param.flags = time_stamp_flag;
  292. param.uid = (unsigned long)buf[prtd->out_head].phys
  293. + output_meta_data.meta_data_length;
  294. if (q6asm_async_write(prtd->audio_client,
  295. &param) < 0)
  296. pr_err("%s:q6asm_async_write failed\n",
  297. __func__);
  298. else
  299. prtd->out_head =
  300. (prtd->out_head + 1)
  301. & (runtime->periods - 1);
  302. atomic_set(&prtd->pending_buffer, 0);
  303. }
  304. break;
  305. case ASM_STREAM_CMD_FLUSH:
  306. pr_debug("ASM_STREAM_CMD_FLUSH\n");
  307. prtd->cmd_ack = 1;
  308. wake_up(&the_locks.flush_wait);
  309. break;
  310. default:
  311. break;
  312. }
  313. break;
  314. }
  315. default:
  316. pr_debug("Not Supported Event opcode[0x%x]\n", opcode);
  317. break;
  318. }
  319. }
  320. static int msm_compr_playback_prepare(struct snd_pcm_substream *substream)
  321. {
  322. struct snd_pcm_runtime *runtime = substream->runtime;
  323. struct compr_audio *compr = runtime->private_data;
  324. struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
  325. struct msm_audio *prtd = &compr->prtd;
  326. struct asm_aac_cfg aac_cfg;
  327. struct asm_wma_cfg wma_cfg;
  328. struct asm_wmapro_cfg wma_pro_cfg;
  329. struct asm_amrwbplus_cfg amrwb_cfg;
  330. int ret;
  331. pr_debug("compressed stream prepare\n");
  332. prtd->pcm_size = snd_pcm_lib_buffer_bytes(substream);
  333. prtd->pcm_count = snd_pcm_lib_period_bytes(substream);
  334. prtd->pcm_irq_pos = 0;
  335. /* rate and channels are sent to audio driver */
  336. prtd->samp_rate = runtime->rate;
  337. prtd->channel_mode = runtime->channels;
  338. prtd->out_head = 0;
  339. atomic_set(&prtd->out_count, runtime->periods);
  340. if (prtd->enabled)
  341. return 0;
  342. switch (compr->info.codec_param.codec.id) {
  343. case SND_AUDIOCODEC_MP3:
  344. pr_debug("%s: SND_AUDIOCODEC_MP3\n", __func__);
  345. ret = q6asm_media_format_block(prtd->audio_client,
  346. compr->codec);
  347. if (ret < 0)
  348. pr_info("%s: CMD Format block failed\n", __func__);
  349. break;
  350. case SND_AUDIOCODEC_AAC:
  351. pr_debug("%s: SND_AUDIOCODEC_AAC\n", __func__);
  352. memset(&aac_cfg, 0x0, sizeof(struct asm_aac_cfg));
  353. aac_cfg.aot = AAC_ENC_MODE_EAAC_P;
  354. aac_cfg.format = 0x03;
  355. aac_cfg.ch_cfg = runtime->channels;
  356. aac_cfg.sample_rate = runtime->rate;
  357. ret = q6asm_media_format_block_aac(prtd->audio_client,
  358. &aac_cfg);
  359. if (ret < 0)
  360. pr_err("%s: CMD Format block failed\n", __func__);
  361. break;
  362. case SND_AUDIOCODEC_AC3_PASS_THROUGH:
  363. case SND_AUDIOCODEC_DTS_PASS_THROUGH:
  364. case SND_AUDIOCODEC_DTS_LBR_PASS_THROUGH:
  365. pr_debug("compressd playback, no need to send decoder params");
  366. pr_debug("decoder id: %d\n",
  367. compr->info.codec_param.codec.id);
  368. msm_pcm_routing_reg_psthr_stream(
  369. soc_prtd->dai_link->be_id,
  370. prtd->session_id, substream->stream,
  371. 1);
  372. break;
  373. case SND_AUDIOCODEC_WMA:
  374. pr_debug("SND_AUDIOCODEC_WMA\n");
  375. memset(&wma_cfg, 0x0, sizeof(struct asm_wma_cfg));
  376. wma_cfg.format_tag = compr->info.codec_param.codec.format;
  377. wma_cfg.ch_cfg = compr->info.codec_param.codec.ch_in;
  378. wma_cfg.sample_rate = compr->info.codec_param.codec.sample_rate;
  379. wma_cfg.avg_bytes_per_sec =
  380. compr->info.codec_param.codec.bit_rate/8;
  381. wma_cfg.block_align = compr->info.codec_param.codec.align;
  382. wma_cfg.valid_bits_per_sample =
  383. compr->info.codec_param.codec.options.wma.bits_per_sample;
  384. wma_cfg.ch_mask =
  385. compr->info.codec_param.codec.options.wma.channelmask;
  386. wma_cfg.encode_opt =
  387. compr->info.codec_param.codec.options.wma.encodeopt;
  388. ret = q6asm_media_format_block_wma(prtd->audio_client,
  389. &wma_cfg);
  390. if (ret < 0)
  391. pr_err("%s: CMD Format block failed\n", __func__);
  392. break;
  393. case SND_AUDIOCODEC_WMA_PRO:
  394. pr_debug("SND_AUDIOCODEC_WMA_PRO\n");
  395. memset(&wma_pro_cfg, 0x0, sizeof(struct asm_wmapro_cfg));
  396. wma_pro_cfg.format_tag = compr->info.codec_param.codec.format;
  397. wma_pro_cfg.ch_cfg = compr->info.codec_param.codec.ch_in;
  398. wma_pro_cfg.sample_rate =
  399. compr->info.codec_param.codec.sample_rate;
  400. wma_pro_cfg.avg_bytes_per_sec =
  401. compr->info.codec_param.codec.bit_rate/8;
  402. wma_pro_cfg.block_align = compr->info.codec_param.codec.align;
  403. wma_pro_cfg.valid_bits_per_sample =
  404. compr->info.codec_param.codec\
  405. .options.wma.bits_per_sample;
  406. wma_pro_cfg.ch_mask =
  407. compr->info.codec_param.codec.options.wma.channelmask;
  408. wma_pro_cfg.encode_opt =
  409. compr->info.codec_param.codec.options.wma.encodeopt;
  410. wma_pro_cfg.adv_encode_opt =
  411. compr->info.codec_param.codec.options.wma.encodeopt1;
  412. wma_pro_cfg.adv_encode_opt2 =
  413. compr->info.codec_param.codec.options.wma.encodeopt2;
  414. ret = q6asm_media_format_block_wmapro(prtd->audio_client,
  415. &wma_pro_cfg);
  416. if (ret < 0)
  417. pr_err("%s: CMD Format block failed\n", __func__);
  418. break;
  419. case SND_AUDIOCODEC_DTS:
  420. case SND_AUDIOCODEC_DTS_LBR:
  421. pr_debug("SND_AUDIOCODEC_DTS\n");
  422. ret = q6asm_media_format_block(prtd->audio_client,
  423. compr->codec);
  424. if (ret < 0) {
  425. pr_err("%s: CMD Format block failed\n", __func__);
  426. return ret;
  427. }
  428. break;
  429. case SND_AUDIOCODEC_AMRWB:
  430. pr_debug("SND_AUDIOCODEC_AMRWB\n");
  431. ret = q6asm_media_format_block(prtd->audio_client,
  432. compr->codec);
  433. if (ret < 0) {
  434. pr_err("%s: CMD Format block failed\n", __func__);
  435. return ret;
  436. }
  437. break;
  438. case SND_AUDIOCODEC_AMRWBPLUS:
  439. pr_debug("SND_AUDIOCODEC_AMRWBPLUS\n");
  440. memset(&amrwb_cfg, 0x0, sizeof(struct asm_amrwbplus_cfg));
  441. amrwb_cfg.size_bytes = sizeof(struct asm_amrwbplus_cfg);
  442. pr_debug("calling q6asm_media_format_block_amrwbplus");
  443. ret = q6asm_media_format_block_amrwbplus(prtd->audio_client,
  444. &amrwb_cfg);
  445. if (ret < 0) {
  446. pr_err("%s: CMD Format block failed\n", __func__);
  447. return ret;
  448. }
  449. break;
  450. case SND_AUDIOCODEC_MP2:
  451. pr_debug("%s: SND_AUDIOCODEC_MP2\n", __func__);
  452. break;
  453. default:
  454. return -EINVAL;
  455. }
  456. if (compr->info.codec_param.codec.transcode_dts) {
  457. msm_pcm_routing_reg_pseudo_stream(
  458. MSM_FRONTEND_DAI_PSEUDO,
  459. prtd->enc_audio_client->perf_mode,
  460. prtd->enc_audio_client->session,
  461. SNDRV_PCM_STREAM_CAPTURE,
  462. 48000, runtime->channels > 6 ?
  463. 6 : runtime->channels);
  464. pr_debug("%s: cmd: DTS ENCDEC CFG BLK\n", __func__);
  465. ret = q6asm_enc_cfg_blk_dts(prtd->enc_audio_client,
  466. DTS_ENC_SAMPLE_RATE48k,
  467. runtime->channels > 6 ?
  468. 6 : runtime->channels);
  469. if (ret < 0)
  470. pr_err("%s: CMD: DTS ENCDEC CFG BLK failed\n",
  471. __func__);
  472. }
  473. prtd->enabled = 1;
  474. prtd->cmd_ack = 0;
  475. return 0;
  476. }
  477. static int msm_compr_capture_prepare(struct snd_pcm_substream *substream)
  478. {
  479. struct snd_pcm_runtime *runtime = substream->runtime;
  480. struct compr_audio *compr = runtime->private_data;
  481. struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
  482. struct msm_audio *prtd = &compr->prtd;
  483. struct audio_buffer *buf = prtd->audio_client->port[OUT].buf;
  484. struct snd_codec *codec = &compr->info.codec_param.codec;
  485. struct audio_aio_read_param read_param;
  486. int ret = 0;
  487. int i;
  488. prtd->pcm_size = snd_pcm_lib_buffer_bytes(substream);
  489. prtd->pcm_count = snd_pcm_lib_period_bytes(substream);
  490. prtd->pcm_irq_pos = 0;
  491. /* rate and channels are sent to audio driver */
  492. prtd->samp_rate = runtime->rate;
  493. prtd->channel_mode = runtime->channels;
  494. if (prtd->enabled)
  495. return ret;
  496. read_param.len = prtd->pcm_count;
  497. switch (codec->id) {
  498. case SND_AUDIOCODEC_AMRWB:
  499. pr_debug("SND_AUDIOCODEC_AMRWB\n");
  500. ret = q6asm_enc_cfg_blk_amrwb(prtd->audio_client,
  501. MAX_NUM_FRAMES_PER_BUFFER,
  502. codec->options.generic.reserved[0] /*bitrate 0-8*/,
  503. codec->options.generic.reserved[1] /*dtx mode 0/1*/);
  504. if (ret < 0)
  505. pr_err("%s: CMD Format block" \
  506. "failed: %d\n", __func__, ret);
  507. break;
  508. case SND_AUDIOCODEC_PCM:
  509. pr_debug("SND_AUDIOCODEC_PCM\n");
  510. ret = q6asm_enc_cfg_blk_multi_ch_pcm(prtd->audio_client,
  511. prtd->samp_rate, prtd->channel_mode);
  512. if (ret < 0)
  513. pr_info("%s: CMD Format block failed\n", __func__);
  514. break;
  515. default:
  516. pr_debug("No config for codec %d\n", codec->id);
  517. }
  518. pr_debug("%s: Samp_rate = %d, Channel = %d, pcm_size = %d,\n"
  519. "pcm_count = %d, periods = %d\n",
  520. __func__, prtd->samp_rate, prtd->channel_mode,
  521. prtd->pcm_size, prtd->pcm_count, runtime->periods);
  522. for (i = 0; i < runtime->periods; i++) {
  523. read_param.uid = i;
  524. switch (codec->id) {
  525. case SND_AUDIOCODEC_AMRWB:
  526. case SND_AUDIOCODEC_PCM:
  527. read_param.len = prtd->pcm_count
  528. - COMPRE_CAPTURE_HEADER_SIZE;
  529. read_param.paddr = (unsigned long)(buf[i].phys)
  530. + COMPRE_CAPTURE_HEADER_SIZE;
  531. pr_debug("Push buffer [%d] to DSP, "\
  532. "paddr: %p, vaddr: %p\n",
  533. i, (void *) read_param.paddr,
  534. buf[i].data);
  535. q6asm_async_read(prtd->audio_client, &read_param);
  536. break;
  537. case SND_AUDIOCODEC_PASS_THROUGH:
  538. read_param.paddr = (unsigned long)(buf[i].phys);
  539. q6asm_async_read_compressed(prtd->audio_client,
  540. &read_param);
  541. break;
  542. default:
  543. pr_err("Invalid format");
  544. ret = -EINVAL;
  545. break;
  546. }
  547. }
  548. prtd->periods = runtime->periods;
  549. prtd->enabled = 1;
  550. if (compr->info.codec_param.codec.id ==
  551. SND_AUDIOCODEC_PASS_THROUGH)
  552. msm_pcm_routing_reg_psthr_stream(
  553. soc_prtd->dai_link->be_id,
  554. prtd->session_id, substream->stream,
  555. 1);
  556. return ret;
  557. }
  558. static int msm_compr_restart(struct snd_pcm_substream *substream)
  559. {
  560. struct snd_pcm_runtime *runtime = substream->runtime;
  561. struct compr_audio *compr = runtime->private_data;
  562. struct msm_audio *prtd = &compr->prtd;
  563. struct audio_aio_write_param param;
  564. struct audio_buffer *buf = NULL;
  565. struct output_meta_data_st output_meta_data;
  566. int time_stamp_flag = 0;
  567. int buffer_length = 0;
  568. pr_err("msm_compr_restart\n");
  569. if (runtime->render_flag & SNDRV_RENDER_STOPPED) {
  570. buf = prtd->audio_client->port[IN].buf;
  571. pr_debug("%s:writing %d bytes of buffer[%d] to dsp 2\n",
  572. __func__, prtd->pcm_count, prtd->out_head);
  573. pr_debug("%s:writing buffer[%d] from 0x%08x\n",
  574. __func__, prtd->out_head,
  575. ((unsigned int)buf[0].phys
  576. + (prtd->out_head * prtd->pcm_count)));
  577. if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
  578. time_stamp_flag = SET_TIMESTAMP;
  579. else
  580. time_stamp_flag = NO_TIMESTAMP;
  581. memcpy(&output_meta_data, (char *)(buf->data +
  582. prtd->out_head * prtd->pcm_count),
  583. COMPRE_OUTPUT_METADATA_SIZE);
  584. buffer_length = output_meta_data.frame_size;
  585. pr_debug("meta_data_length: %d, frame_length: %d\n",
  586. output_meta_data.meta_data_length,
  587. output_meta_data.frame_size);
  588. pr_debug("timestamp_msw: %d, timestamp_lsw: %d\n",
  589. output_meta_data.timestamp_msw,
  590. output_meta_data.timestamp_lsw);
  591. if (buffer_length == 0) {
  592. pr_debug("Recieved a zero length buffer-break out");
  593. return -EINVAL;
  594. }
  595. param.paddr = (unsigned long)buf[0].phys
  596. + (prtd->out_head * prtd->pcm_count)
  597. + output_meta_data.meta_data_length;
  598. param.len = buffer_length;
  599. param.msw_ts = output_meta_data.timestamp_msw;
  600. param.lsw_ts = output_meta_data.timestamp_lsw;
  601. param.flags = time_stamp_flag;
  602. param.uid = (unsigned long)buf[0].phys
  603. + (prtd->out_head * prtd->pcm_count
  604. + output_meta_data.meta_data_length);
  605. if (q6asm_async_write(prtd->audio_client,
  606. &param) < 0)
  607. pr_err("%s:q6asm_async_write failed\n",
  608. __func__);
  609. else
  610. prtd->out_head =
  611. (prtd->out_head + 1) & (runtime->periods - 1);
  612. runtime->render_flag &= ~SNDRV_RENDER_STOPPED;
  613. return 0;
  614. }
  615. return 0;
  616. }
  617. static int msm_compr_trigger(struct snd_pcm_substream *substream, int cmd)
  618. {
  619. int ret = 0;
  620. struct snd_pcm_runtime *runtime = substream->runtime;
  621. struct compr_audio *compr = runtime->private_data;
  622. struct msm_audio *prtd = &compr->prtd;
  623. pr_debug("%s\n", __func__);
  624. switch (cmd) {
  625. case SNDRV_PCM_TRIGGER_START:
  626. prtd->pcm_irq_pos = 0;
  627. /* intentional fall-through */
  628. case SNDRV_PCM_TRIGGER_RESUME:
  629. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  630. pr_debug("%s: Trigger start\n", __func__);
  631. q6asm_run_nowait(prtd->audio_client, 0, 0, 0);
  632. if (prtd->enc_audio_client)
  633. q6asm_run_nowait(prtd->enc_audio_client, 0, 0, 0);
  634. atomic_set(&prtd->start, 1);
  635. break;
  636. case SNDRV_PCM_TRIGGER_STOP:
  637. atomic_set(&prtd->start, 0);
  638. runtime->render_flag &= ~SNDRV_RENDER_STOPPED;
  639. break;
  640. case SNDRV_PCM_TRIGGER_SUSPEND:
  641. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  642. pr_debug("SNDRV_PCM_TRIGGER_PAUSE\n");
  643. q6asm_cmd_nowait(prtd->audio_client, CMD_PAUSE);
  644. if (prtd->enc_audio_client)
  645. q6asm_cmd_nowait(prtd->enc_audio_client, CMD_PAUSE);
  646. atomic_set(&prtd->start, 0);
  647. runtime->render_flag &= ~SNDRV_RENDER_STOPPED;
  648. break;
  649. default:
  650. ret = -EINVAL;
  651. break;
  652. }
  653. return ret;
  654. }
  655. static void populate_codec_list(struct compr_audio *compr,
  656. struct snd_pcm_runtime *runtime)
  657. {
  658. pr_debug("%s\n", __func__);
  659. /* MP3 Block */
  660. compr->info.compr_cap.num_codecs = 14;
  661. compr->info.compr_cap.min_fragment_size = runtime->hw.period_bytes_min;
  662. compr->info.compr_cap.max_fragment_size = runtime->hw.period_bytes_max;
  663. compr->info.compr_cap.min_fragments = runtime->hw.periods_min;
  664. compr->info.compr_cap.max_fragments = runtime->hw.periods_max;
  665. compr->info.compr_cap.codecs[0] = SND_AUDIOCODEC_MP3;
  666. compr->info.compr_cap.codecs[1] = SND_AUDIOCODEC_AAC;
  667. compr->info.compr_cap.codecs[2] = SND_AUDIOCODEC_AC3_PASS_THROUGH;
  668. compr->info.compr_cap.codecs[3] = SND_AUDIOCODEC_WMA;
  669. compr->info.compr_cap.codecs[4] = SND_AUDIOCODEC_WMA_PRO;
  670. compr->info.compr_cap.codecs[5] = SND_AUDIOCODEC_DTS;
  671. compr->info.compr_cap.codecs[6] = SND_AUDIOCODEC_DTS_LBR;
  672. compr->info.compr_cap.codecs[7] = SND_AUDIOCODEC_DTS_PASS_THROUGH;
  673. compr->info.compr_cap.codecs[8] = SND_AUDIOCODEC_AMRWB;
  674. compr->info.compr_cap.codecs[9] = SND_AUDIOCODEC_AMRWBPLUS;
  675. compr->info.compr_cap.codecs[10] = SND_AUDIOCODEC_PASS_THROUGH;
  676. compr->info.compr_cap.codecs[11] = SND_AUDIOCODEC_PCM;
  677. compr->info.compr_cap.codecs[12] = SND_AUDIOCODEC_MP2;
  678. compr->info.compr_cap.codecs[13] = SND_AUDIOCODEC_DTS_LBR_PASS_THROUGH;
  679. /* Add new codecs here and update num_codecs*/
  680. }
  681. static int msm_compr_open(struct snd_pcm_substream *substream)
  682. {
  683. struct snd_pcm_runtime *runtime = substream->runtime;
  684. struct compr_audio *compr;
  685. struct msm_audio *prtd;
  686. int ret = 0;
  687. struct asm_softpause_params softpause = {
  688. .enable = SOFT_PAUSE_ENABLE,
  689. .period = SOFT_PAUSE_PERIOD,
  690. .step = SOFT_PAUSE_STEP,
  691. .rampingcurve = SOFT_PAUSE_CURVE_LINEAR,
  692. };
  693. struct asm_softvolume_params softvol = {
  694. .period = SOFT_VOLUME_PERIOD,
  695. .step = SOFT_VOLUME_STEP,
  696. .rampingcurve = SOFT_VOLUME_CURVE_LINEAR,
  697. };
  698. pr_debug("%s\n", __func__);
  699. compr = kzalloc(sizeof(struct compr_audio), GFP_KERNEL);
  700. if (compr == NULL) {
  701. pr_err("Failed to allocate memory for msm_audio\n");
  702. return -ENOMEM;
  703. }
  704. prtd = &compr->prtd;
  705. prtd->substream = substream;
  706. runtime->render_flag = SNDRV_DMA_MODE;
  707. prtd->audio_client = q6asm_audio_client_alloc(
  708. (app_cb)compr_event_handler, compr);
  709. if (!prtd->audio_client) {
  710. pr_info("%s: Could not allocate memory\n", __func__);
  711. kfree(prtd);
  712. return -ENOMEM;
  713. }
  714. prtd->audio_client->perf_mode = false;
  715. pr_info("%s: session ID %d\n", __func__, prtd->audio_client->session);
  716. prtd->session_id = prtd->audio_client->session;
  717. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  718. runtime->hw = msm_compr_hardware_playback;
  719. prtd->cmd_ack = 1;
  720. } else {
  721. runtime->hw = msm_compr_hardware_capture;
  722. }
  723. ret = snd_pcm_hw_constraint_list(runtime, 0,
  724. SNDRV_PCM_HW_PARAM_RATE,
  725. &constraints_sample_rates);
  726. if (ret < 0)
  727. pr_info("snd_pcm_hw_constraint_list failed\n");
  728. /* Ensure that buffer size is a multiple of period size */
  729. ret = snd_pcm_hw_constraint_integer(runtime,
  730. SNDRV_PCM_HW_PARAM_PERIODS);
  731. if (ret < 0)
  732. pr_info("snd_pcm_hw_constraint_integer failed\n");
  733. prtd->dsp_cnt = 0;
  734. atomic_set(&prtd->pending_buffer, 1);
  735. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  736. compr->codec = FORMAT_MP3;
  737. populate_codec_list(compr, runtime);
  738. runtime->private_data = compr;
  739. atomic_set(&prtd->eos, 0);
  740. compressed_audio.prtd = &compr->prtd;
  741. ret = compressed_set_volume(0);
  742. if (ret < 0)
  743. pr_err("%s : Set Volume failed : %d", __func__, ret);
  744. ret = q6asm_set_softpause(compressed_audio.prtd->audio_client,
  745. &softpause);
  746. if (ret < 0)
  747. pr_err("%s: Send SoftPause Param failed ret=%d\n",
  748. __func__, ret);
  749. ret = q6asm_set_softvolume(compressed_audio.prtd->audio_client,
  750. &softvol);
  751. if (ret < 0)
  752. pr_err("%s: Send SoftVolume Param failed ret=%d\n",
  753. __func__, ret);
  754. return 0;
  755. }
  756. int compressed_set_volume(unsigned volume)
  757. {
  758. int rc = 0;
  759. if (compressed_audio.prtd && compressed_audio.prtd->audio_client) {
  760. rc = q6asm_set_volume(compressed_audio.prtd->audio_client,
  761. volume);
  762. if (rc < 0) {
  763. pr_err("%s: Send Volume command failed"
  764. " rc=%d\n", __func__, rc);
  765. }
  766. }
  767. compressed_audio.volume = volume;
  768. return rc;
  769. }
  770. static int msm_compr_playback_close(struct snd_pcm_substream *substream)
  771. {
  772. struct snd_pcm_runtime *runtime = substream->runtime;
  773. struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
  774. struct compr_audio *compr = runtime->private_data;
  775. struct msm_audio *prtd = &compr->prtd;
  776. int dir = 0;
  777. pr_debug("%s\n", __func__);
  778. dir = IN;
  779. atomic_set(&prtd->pending_buffer, 0);
  780. prtd->pcm_irq_pos = 0;
  781. q6asm_cmd(prtd->audio_client, CMD_CLOSE);
  782. if (prtd->enc_audio_client)
  783. q6asm_cmd(prtd->enc_audio_client, CMD_CLOSE);
  784. compressed_audio.prtd = NULL;
  785. q6asm_audio_client_buf_free_contiguous(dir,
  786. prtd->audio_client);
  787. switch (compr->info.codec_param.codec.id) {
  788. case SND_AUDIOCODEC_AC3_PASS_THROUGH:
  789. case SND_AUDIOCODEC_DTS_PASS_THROUGH:
  790. case SND_AUDIOCODEC_DTS_LBR_PASS_THROUGH:
  791. msm_pcm_routing_reg_psthr_stream(
  792. soc_prtd->dai_link->be_id,
  793. prtd->session_id, substream->stream,
  794. 0);
  795. default:
  796. msm_pcm_routing_dereg_phy_stream(
  797. soc_prtd->dai_link->be_id,
  798. SNDRV_PCM_STREAM_PLAYBACK);
  799. }
  800. if (compr->info.codec_param.codec.transcode_dts) {
  801. msm_pcm_routing_dereg_pseudo_stream(MSM_FRONTEND_DAI_PSEUDO,
  802. prtd->enc_audio_client->session);
  803. }
  804. if (prtd->enc_audio_client)
  805. q6asm_audio_client_free(prtd->enc_audio_client);
  806. q6asm_audio_client_free(prtd->audio_client);
  807. kfree(prtd);
  808. return 0;
  809. }
  810. static int msm_compr_capture_close(struct snd_pcm_substream *substream)
  811. {
  812. struct snd_pcm_runtime *runtime = substream->runtime;
  813. struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
  814. struct compr_audio *compr = runtime->private_data;
  815. struct msm_audio *prtd = &compr->prtd;
  816. int dir = OUT;
  817. pr_debug("%s\n", __func__);
  818. atomic_set(&prtd->pending_buffer, 0);
  819. q6asm_cmd(prtd->audio_client, CMD_CLOSE);
  820. compressed_audio.prtd = NULL;
  821. q6asm_audio_client_buf_free_contiguous(dir,
  822. prtd->audio_client);
  823. if (compr->info.codec_param.codec.id ==
  824. SND_AUDIOCODEC_PASS_THROUGH)
  825. msm_pcm_routing_reg_psthr_stream(
  826. soc_prtd->dai_link->be_id,
  827. prtd->session_id, substream->stream,
  828. 0);
  829. else
  830. msm_pcm_routing_dereg_phy_stream(soc_prtd->dai_link->be_id,
  831. SNDRV_PCM_STREAM_CAPTURE);
  832. q6asm_audio_client_free(prtd->audio_client);
  833. kfree(prtd);
  834. return 0;
  835. }
  836. static int msm_compr_close(struct snd_pcm_substream *substream)
  837. {
  838. int ret = 0;
  839. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  840. ret = msm_compr_playback_close(substream);
  841. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  842. ret = msm_compr_capture_close(substream);
  843. return ret;
  844. }
  845. static int msm_compr_prepare(struct snd_pcm_substream *substream)
  846. {
  847. int ret = 0;
  848. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  849. ret = msm_compr_playback_prepare(substream);
  850. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  851. ret = msm_compr_capture_prepare(substream);
  852. return ret;
  853. }
  854. static snd_pcm_uframes_t msm_compr_pointer(struct snd_pcm_substream *substream)
  855. {
  856. struct snd_pcm_runtime *runtime = substream->runtime;
  857. struct compr_audio *compr = runtime->private_data;
  858. struct msm_audio *prtd = &compr->prtd;
  859. if (prtd->pcm_irq_pos >= prtd->pcm_size)
  860. prtd->pcm_irq_pos = 0;
  861. pr_debug("%s: pcm_irq_pos = %d, pcm_size = %d, sample_bits = %d,\n"
  862. "frame_bits = %d\n", __func__, prtd->pcm_irq_pos,
  863. prtd->pcm_size, runtime->sample_bits,
  864. runtime->frame_bits);
  865. return bytes_to_frames(runtime, (prtd->pcm_irq_pos));
  866. }
  867. static int msm_compr_mmap(struct snd_pcm_substream *substream,
  868. struct vm_area_struct *vma)
  869. {
  870. int result = 0;
  871. struct snd_pcm_runtime *runtime = substream->runtime;
  872. struct compr_audio *compr = runtime->private_data;
  873. struct msm_audio *prtd = &compr->prtd;
  874. pr_debug("%s\n", __func__);
  875. prtd->mmap_flag = 1;
  876. runtime->render_flag = SNDRV_NON_DMA_MODE;
  877. if (runtime->dma_addr && runtime->dma_bytes) {
  878. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  879. result = remap_pfn_range(vma, vma->vm_start,
  880. runtime->dma_addr >> PAGE_SHIFT,
  881. runtime->dma_bytes,
  882. vma->vm_page_prot);
  883. } else {
  884. pr_err("Physical address or size of buf is NULL");
  885. return -EINVAL;
  886. }
  887. return result;
  888. }
  889. static int msm_compr_hw_params(struct snd_pcm_substream *substream,
  890. struct snd_pcm_hw_params *params)
  891. {
  892. struct snd_pcm_runtime *runtime = substream->runtime;
  893. struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
  894. struct compr_audio *compr = runtime->private_data;
  895. struct msm_audio *prtd = &compr->prtd;
  896. struct snd_dma_buffer *dma_buf = &substream->dma_buffer;
  897. struct audio_buffer *buf;
  898. int dir, ret;
  899. pr_debug("%s\n", __func__);
  900. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  901. dir = IN;
  902. else
  903. dir = OUT;
  904. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  905. switch (compr->info.codec_param.codec.id) {
  906. case SND_AUDIOCODEC_AC3_PASS_THROUGH:
  907. case SND_AUDIOCODEC_DTS_PASS_THROUGH:
  908. case SND_AUDIOCODEC_DTS_LBR_PASS_THROUGH:
  909. ret = q6asm_open_write_compressed(prtd->audio_client,
  910. compr->codec);
  911. if (ret < 0) {
  912. pr_err("%s: Session out open failed\n",
  913. __func__);
  914. return -ENOMEM;
  915. }
  916. break;
  917. default:
  918. ret = q6asm_open_write(prtd->audio_client,
  919. compr->codec);
  920. if (ret < 0) {
  921. pr_err("%s: Session out open failed\n",
  922. __func__);
  923. return -ENOMEM;
  924. }
  925. msm_pcm_routing_reg_phy_stream(
  926. soc_prtd->dai_link->be_id,
  927. prtd->audio_client->perf_mode,
  928. prtd->session_id,
  929. substream->stream);
  930. if (compr->info.codec_param.codec.transcode_dts) {
  931. prtd->enc_audio_client =
  932. q6asm_audio_client_alloc(
  933. (app_cb)compr_event_handler, compr);
  934. if (!prtd->enc_audio_client) {
  935. pr_err("%s: Could not allocate " \
  936. "memory\n", __func__);
  937. return -ENOMEM;
  938. }
  939. prtd->enc_audio_client->perf_mode = false;
  940. pr_debug("%s Setting up loopback path\n",
  941. __func__);
  942. ret = q6asm_open_transcode_loopback(
  943. prtd->enc_audio_client,
  944. params_channels(params));
  945. if (ret < 0) {
  946. pr_err("%s: Session transcode " \
  947. "loopback open failed\n",
  948. __func__);
  949. return -ENODEV;
  950. }
  951. }
  952. break;
  953. }
  954. } else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  955. switch (compr->info.codec_param.codec.id) {
  956. case SND_AUDIOCODEC_AMRWB:
  957. pr_debug("q6asm_open_read(FORMAT_AMRWB)\n");
  958. ret = q6asm_open_read(prtd->audio_client,
  959. FORMAT_AMRWB);
  960. if (ret < 0) {
  961. pr_err("%s: compressed Session out open failed\n",
  962. __func__);
  963. return -ENOMEM;
  964. }
  965. pr_debug("msm_pcm_routing_reg_phy_stream\n");
  966. msm_pcm_routing_reg_phy_stream(
  967. soc_prtd->dai_link->be_id,
  968. prtd->audio_client->perf_mode,
  969. prtd->session_id, substream->stream);
  970. break;
  971. case SND_AUDIOCODEC_PCM:
  972. pr_debug("q6asm_open_read(FORMAT_PCM)\n");
  973. ret = q6asm_open_read(prtd->audio_client,
  974. FORMAT_MULTI_CHANNEL_LINEAR_PCM);
  975. if (ret < 0) {
  976. pr_err("%s: compressed Session open failed\n",
  977. __func__);
  978. return -ENOMEM;
  979. }
  980. pr_debug("msm_pcm_routing_reg_phy_stream\n");
  981. msm_pcm_routing_reg_phy_stream(
  982. soc_prtd->dai_link->be_id,
  983. prtd->audio_client->perf_mode,
  984. prtd->session_id, substream->stream);
  985. break;
  986. case SND_AUDIOCODEC_PASS_THROUGH:
  987. pr_debug("q6asm_open_read_compressed(COMPRESSED_META_DATA_MODE)\n");
  988. ret = q6asm_open_read_compressed(prtd->audio_client,
  989. MAX_NUM_FRAMES_PER_BUFFER,
  990. COMPRESSED_META_DATA_MODE);
  991. break;
  992. default:
  993. pr_err("Invalid codec for compressed session open\n");
  994. return -EFAULT;
  995. }
  996. if (ret < 0) {
  997. pr_err("%s: compressed Session out open failed\n",
  998. __func__);
  999. return -ENOMEM;
  1000. }
  1001. }
  1002. ret = q6asm_set_io_mode(prtd->audio_client, ASYNC_IO_MODE);
  1003. if (ret < 0) {
  1004. pr_err("%s: Set IO mode failed\n", __func__);
  1005. return -ENOMEM;
  1006. }
  1007. /* Modifying kernel hardware params based on userspace config */
  1008. if (params_periods(params) > 0 &&
  1009. (params_periods(params) != runtime->hw.periods_max)) {
  1010. runtime->hw.periods_max = params_periods(params);
  1011. }
  1012. if (params_period_bytes(params) > 0 &&
  1013. (params_period_bytes(params) != runtime->hw.period_bytes_min)) {
  1014. runtime->hw.period_bytes_min = params_period_bytes(params);
  1015. }
  1016. runtime->hw.buffer_bytes_max =
  1017. runtime->hw.period_bytes_min * runtime->hw.periods_max;
  1018. ret = q6asm_audio_client_buf_alloc_contiguous(dir,
  1019. prtd->audio_client,
  1020. runtime->hw.period_bytes_min,
  1021. runtime->hw.periods_max);
  1022. if (ret < 0) {
  1023. pr_err("Audio Start: Buffer Allocation failed "
  1024. "rc = %d\n", ret);
  1025. return -ENOMEM;
  1026. }
  1027. buf = prtd->audio_client->port[dir].buf;
  1028. dma_buf->dev.type = SNDRV_DMA_TYPE_DEV;
  1029. dma_buf->dev.dev = substream->pcm->card->dev;
  1030. dma_buf->private_data = NULL;
  1031. dma_buf->area = buf[0].data;
  1032. dma_buf->addr = buf[0].phys;
  1033. dma_buf->bytes = runtime->hw.buffer_bytes_max;
  1034. pr_debug("%s: buf[%p]dma_buf->area[%p]dma_buf->addr[%p]\n"
  1035. "dma_buf->bytes[%d]\n", __func__,
  1036. (void *)buf, (void *)dma_buf->area,
  1037. (void *)dma_buf->addr, dma_buf->bytes);
  1038. if (!dma_buf->area)
  1039. return -ENOMEM;
  1040. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  1041. return 0;
  1042. }
  1043. static int msm_compr_ioctl(struct snd_pcm_substream *substream,
  1044. unsigned int cmd, void *arg)
  1045. {
  1046. int rc = 0;
  1047. struct snd_pcm_runtime *runtime = substream->runtime;
  1048. struct compr_audio *compr = runtime->private_data;
  1049. struct msm_audio *prtd = &compr->prtd;
  1050. uint64_t timestamp;
  1051. uint64_t temp;
  1052. switch (cmd) {
  1053. case SNDRV_COMPRESS_TSTAMP: {
  1054. struct snd_compr_tstamp tstamp;
  1055. pr_debug("SNDRV_COMPRESS_TSTAMP\n");
  1056. memset(&tstamp, 0x0, sizeof(struct snd_compr_tstamp));
  1057. rc = q6asm_get_session_time(prtd->audio_client, &timestamp);
  1058. if (rc < 0) {
  1059. pr_err("%s: fail to get session tstamp\n", __func__);
  1060. return rc;
  1061. }
  1062. temp = (timestamp * 2 * runtime->channels);
  1063. temp = temp * (runtime->rate/1000);
  1064. temp = div_u64(temp, 1000);
  1065. tstamp.sampling_rate = runtime->rate;
  1066. tstamp.timestamp = timestamp;
  1067. pr_debug("%s: bytes_consumed:,"
  1068. "timestamp = %lld,\n", __func__,
  1069. tstamp.timestamp);
  1070. if (copy_to_user((void *) arg, &tstamp,
  1071. sizeof(struct snd_compr_tstamp)))
  1072. return -EFAULT;
  1073. return 0;
  1074. }
  1075. case SNDRV_COMPRESS_GET_CAPS:
  1076. pr_debug("SNDRV_COMPRESS_GET_CAPS\n");
  1077. if (copy_to_user((void *) arg, &compr->info.compr_cap,
  1078. sizeof(struct snd_compr_caps))) {
  1079. rc = -EFAULT;
  1080. pr_err("%s: ERROR: copy to user\n", __func__);
  1081. return rc;
  1082. }
  1083. return 0;
  1084. case SNDRV_COMPRESS_SET_PARAMS:
  1085. pr_debug("SNDRV_COMPRESS_SET_PARAMS: ");
  1086. if (copy_from_user(&compr->info.codec_param, (void *) arg,
  1087. sizeof(struct snd_compr_params))) {
  1088. rc = -EFAULT;
  1089. pr_err("%s: ERROR: copy from user\n", __func__);
  1090. return rc;
  1091. }
  1092. /*
  1093. * DTS Security needed for the transcode path
  1094. */
  1095. if (compr->info.codec_param.codec.transcode_dts) {
  1096. char modelId[128];
  1097. struct snd_dec_dts opt_dts =
  1098. compr->info.codec_param.codec.dts;
  1099. unsigned int modelIdLength = opt_dts.modelIdLength;
  1100. if (modelIdLength >= sizeof(modelId)) {
  1101. rc = -EINVAL;
  1102. pr_err("%s: ERROR: modelIdLength is"
  1103. "invalid\n", __func__);
  1104. return rc;
  1105. }
  1106. if (copy_from_user(modelId, (void *)opt_dts.modelId,
  1107. modelIdLength))
  1108. pr_err("%s: ERROR: copy modelId\n", __func__);
  1109. modelId[modelIdLength] = '\0';
  1110. pr_debug("%s: Received modelId =%s,length=%d\n",
  1111. __func__, modelId, modelIdLength);
  1112. core_set_dts_model_id(modelIdLength, modelId);
  1113. }
  1114. switch (compr->info.codec_param.codec.id) {
  1115. case SND_AUDIOCODEC_MP3:
  1116. /* For MP3 we dont need any other parameter */
  1117. pr_debug("SND_AUDIOCODEC_MP3\n");
  1118. compr->codec = FORMAT_MP3;
  1119. break;
  1120. case SND_AUDIOCODEC_AAC:
  1121. pr_debug("SND_AUDIOCODEC_AAC\n");
  1122. compr->codec = FORMAT_MPEG4_AAC;
  1123. break;
  1124. case SND_AUDIOCODEC_AC3_PASS_THROUGH:
  1125. pr_debug("SND_AUDIOCODEC_AC3_PASS_THROUGH\n");
  1126. compr->codec = FORMAT_AC3;
  1127. break;
  1128. case SND_AUDIOCODEC_WMA:
  1129. pr_debug("SND_AUDIOCODEC_WMA\n");
  1130. compr->codec = FORMAT_WMA_V9;
  1131. break;
  1132. case SND_AUDIOCODEC_WMA_PRO:
  1133. pr_debug("SND_AUDIOCODEC_WMA_PRO\n");
  1134. compr->codec = FORMAT_WMA_V10PRO;
  1135. break;
  1136. case SND_AUDIOCODEC_DTS_PASS_THROUGH:
  1137. pr_debug("SND_AUDIOCODEC_DTS_PASS_THROUGH\n");
  1138. compr->codec = FORMAT_DTS;
  1139. break;
  1140. case SND_AUDIOCODEC_DTS_LBR_PASS_THROUGH:
  1141. pr_debug("SND_AUDIOCODEC_DTS_LBR_PASS_THROUGH\n");
  1142. compr->codec = FORMAT_DTS_LBR;
  1143. break;
  1144. case SND_AUDIOCODEC_DTS: {
  1145. char modelId[128];
  1146. struct snd_dec_dts opt_dts =
  1147. compr->info.codec_param.codec.dts;
  1148. unsigned int modelIdLength = opt_dts.modelIdLength;
  1149. pr_debug("SND_AUDIOCODEC_DTS\n");
  1150. if (modelIdLength >= sizeof(modelId)) {
  1151. rc = -EINVAL;
  1152. pr_err("%s: ERROR: modelIdLength is"
  1153. "invalid\n", __func__);
  1154. return rc;
  1155. }
  1156. if (copy_from_user(modelId, (void *)opt_dts.modelId,
  1157. modelIdLength))
  1158. pr_err("%s: ERROR: copy modelId\n", __func__);
  1159. modelId[modelIdLength] = '\0';
  1160. pr_debug("%s: Received modelId =%s,length=%d\n",
  1161. __func__, modelId, modelIdLength);
  1162. core_set_dts_model_id(modelIdLength, modelId);
  1163. compr->codec = FORMAT_DTS;
  1164. }
  1165. break;
  1166. case SND_AUDIOCODEC_DTS_LBR:{
  1167. char modelId[128];
  1168. struct snd_dec_dts opt_dts =
  1169. compr->info.codec_param.codec.dts;
  1170. int modelIdLength = opt_dts.modelIdLength;
  1171. pr_debug("SND_AUDIOCODEC_DTS_LBR\n");
  1172. if (copy_from_user(modelId, (void *)opt_dts.modelId,
  1173. modelIdLength))
  1174. pr_err("%s: ERROR: copy modelId\n", __func__);
  1175. modelId[modelIdLength] = '\0';
  1176. pr_debug("%s: Received modelId =%s,length=%d\n",
  1177. __func__, modelId, modelIdLength);
  1178. core_set_dts_model_id(modelIdLength, modelId);
  1179. compr->codec = FORMAT_DTS_LBR;
  1180. }
  1181. break;
  1182. case SND_AUDIOCODEC_AMRWB:
  1183. pr_debug("msm_compr_ioctl SND_AUDIOCODEC_AMRWB\n");
  1184. compr->codec = FORMAT_AMRWB;
  1185. break;
  1186. case SND_AUDIOCODEC_AMRWBPLUS:
  1187. pr_debug("msm_compr_ioctl SND_AUDIOCODEC_AMRWBPLUS\n");
  1188. compr->codec = FORMAT_AMR_WB_PLUS;
  1189. break;
  1190. case SND_AUDIOCODEC_PASS_THROUGH:
  1191. /* format pass through is used for HDMI IN compressed
  1192. where the decoder format is indicated by LPASS */
  1193. pr_debug("msm_compr_ioctl SND_AUDIOCODEC_PASSTHROUGH\n");
  1194. compr->codec = FORMAT_PASS_THROUGH;
  1195. break;
  1196. case SND_AUDIOCODEC_PCM:
  1197. pr_debug("msm_compr_ioctl SND_AUDIOCODEC_PCM\n");
  1198. compr->codec = FORMAT_MULTI_CHANNEL_LINEAR_PCM;
  1199. break;
  1200. case SND_AUDIOCODEC_MP2:
  1201. pr_debug("SND_AUDIOCODEC_MP2\n");
  1202. compr->codec = FORMAT_MP2;
  1203. break;
  1204. default:
  1205. pr_err("msm_compr_ioctl failed..unknown codec\n");
  1206. return -EFAULT;
  1207. }
  1208. return 0;
  1209. case SNDRV_PCM_IOCTL1_RESET:
  1210. pr_debug("SNDRV_PCM_IOCTL1_RESET\n");
  1211. /* Flush only when session is started during CAPTURE,
  1212. while PLAYBACK has no such restriction. */
  1213. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK ||
  1214. (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
  1215. atomic_read(&prtd->start))) {
  1216. if (atomic_read(&prtd->eos)) {
  1217. prtd->cmd_ack = 1;
  1218. wake_up(&the_locks.eos_wait);
  1219. atomic_set(&prtd->eos, 0);
  1220. atomic_set(&prtd->pending_buffer, 1);
  1221. }
  1222. /* A unlikely race condition possible with FLUSH
  1223. DRAIN if ack is set by flush and reset by drain */
  1224. prtd->cmd_ack = 0;
  1225. rc = q6asm_cmd(prtd->audio_client, CMD_FLUSH);
  1226. if (rc < 0) {
  1227. pr_err("%s: flush cmd failed rc=%d\n",
  1228. __func__, rc);
  1229. return rc;
  1230. }
  1231. rc = wait_event_timeout(the_locks.flush_wait,
  1232. prtd->cmd_ack, 5 * HZ);
  1233. if (!rc)
  1234. pr_err("Flush cmd timeout\n");
  1235. prtd->pcm_irq_pos = 0;
  1236. }
  1237. break;
  1238. case SNDRV_COMPRESS_DRAIN:
  1239. pr_debug("%s: SNDRV_COMPRESS_DRAIN\n", __func__);
  1240. atomic_set(&prtd->eos, 1);
  1241. atomic_set(&prtd->pending_buffer, 0);
  1242. prtd->cmd_ack = 0;
  1243. q6asm_cmd_nowait(prtd->audio_client, CMD_EOS);
  1244. /* Wait indefinitely for DRAIN. Flush can also signal this*/
  1245. rc = wait_event_interruptible(the_locks.eos_wait,
  1246. prtd->cmd_ack);
  1247. if (rc < 0)
  1248. pr_err("EOS cmd interrupted\n");
  1249. pr_debug("%s: SNDRV_COMPRESS_DRAIN out of wait\n", __func__);
  1250. return 0;
  1251. default:
  1252. break;
  1253. }
  1254. return snd_pcm_lib_ioctl(substream, cmd, arg);
  1255. }
  1256. static struct snd_pcm_ops msm_compr_ops = {
  1257. .open = msm_compr_open,
  1258. .hw_params = msm_compr_hw_params,
  1259. .close = msm_compr_close,
  1260. .ioctl = msm_compr_ioctl,
  1261. .prepare = msm_compr_prepare,
  1262. .trigger = msm_compr_trigger,
  1263. .pointer = msm_compr_pointer,
  1264. .mmap = msm_compr_mmap,
  1265. .restart = msm_compr_restart,
  1266. };
  1267. static int msm_asoc_pcm_new(struct snd_soc_pcm_runtime *rtd)
  1268. {
  1269. struct snd_card *card = rtd->card->snd_card;
  1270. int ret = 0;
  1271. if (!card->dev->coherent_dma_mask)
  1272. card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
  1273. return ret;
  1274. }
  1275. static struct snd_soc_platform_driver msm_soc_platform = {
  1276. .ops = &msm_compr_ops,
  1277. .pcm_new = msm_asoc_pcm_new,
  1278. };
  1279. static __devinit int msm_compr_probe(struct platform_device *pdev)
  1280. {
  1281. pr_info("%s: dev name %s\n", __func__, dev_name(&pdev->dev));
  1282. return snd_soc_register_platform(&pdev->dev,
  1283. &msm_soc_platform);
  1284. }
  1285. static int msm_compr_remove(struct platform_device *pdev)
  1286. {
  1287. snd_soc_unregister_platform(&pdev->dev);
  1288. return 0;
  1289. }
  1290. static struct platform_driver msm_compr_driver = {
  1291. .driver = {
  1292. .name = "msm-compr-dsp",
  1293. .owner = THIS_MODULE,
  1294. },
  1295. .probe = msm_compr_probe,
  1296. .remove = __devexit_p(msm_compr_remove),
  1297. };
  1298. static int __init msm_soc_platform_init(void)
  1299. {
  1300. init_waitqueue_head(&the_locks.enable_wait);
  1301. init_waitqueue_head(&the_locks.eos_wait);
  1302. init_waitqueue_head(&the_locks.write_wait);
  1303. init_waitqueue_head(&the_locks.read_wait);
  1304. init_waitqueue_head(&the_locks.flush_wait);
  1305. return platform_driver_register(&msm_compr_driver);
  1306. }
  1307. module_init(msm_soc_platform_init);
  1308. static void __exit msm_soc_platform_exit(void)
  1309. {
  1310. platform_driver_unregister(&msm_compr_driver);
  1311. }
  1312. module_exit(msm_soc_platform_exit);
  1313. MODULE_DESCRIPTION("PCM module platform driver");
  1314. MODULE_LICENSE("GPL v2");