msm-compr-q6.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413
  1. /* Copyright (c) 2011-2013, 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 <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. runtime->private_data = NULL;
  809. return 0;
  810. }
  811. static int msm_compr_capture_close(struct snd_pcm_substream *substream)
  812. {
  813. struct snd_pcm_runtime *runtime = substream->runtime;
  814. struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
  815. struct compr_audio *compr = runtime->private_data;
  816. struct msm_audio *prtd = &compr->prtd;
  817. int dir = OUT;
  818. pr_debug("%s\n", __func__);
  819. atomic_set(&prtd->pending_buffer, 0);
  820. q6asm_cmd(prtd->audio_client, CMD_CLOSE);
  821. compressed_audio.prtd = NULL;
  822. q6asm_audio_client_buf_free_contiguous(dir,
  823. prtd->audio_client);
  824. if (compr->info.codec_param.codec.id ==
  825. SND_AUDIOCODEC_PASS_THROUGH)
  826. msm_pcm_routing_reg_psthr_stream(
  827. soc_prtd->dai_link->be_id,
  828. prtd->session_id, substream->stream,
  829. 0);
  830. else
  831. msm_pcm_routing_dereg_phy_stream(soc_prtd->dai_link->be_id,
  832. SNDRV_PCM_STREAM_CAPTURE);
  833. q6asm_audio_client_free(prtd->audio_client);
  834. kfree(prtd);
  835. runtime->private_data = NULL;
  836. return 0;
  837. }
  838. static int msm_compr_close(struct snd_pcm_substream *substream)
  839. {
  840. int ret = 0;
  841. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  842. ret = msm_compr_playback_close(substream);
  843. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  844. ret = msm_compr_capture_close(substream);
  845. return ret;
  846. }
  847. static int msm_compr_prepare(struct snd_pcm_substream *substream)
  848. {
  849. int ret = 0;
  850. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  851. ret = msm_compr_playback_prepare(substream);
  852. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  853. ret = msm_compr_capture_prepare(substream);
  854. return ret;
  855. }
  856. static snd_pcm_uframes_t msm_compr_pointer(struct snd_pcm_substream *substream)
  857. {
  858. struct snd_pcm_runtime *runtime = substream->runtime;
  859. struct compr_audio *compr = runtime->private_data;
  860. struct msm_audio *prtd = &compr->prtd;
  861. if (prtd->pcm_irq_pos >= prtd->pcm_size)
  862. prtd->pcm_irq_pos = 0;
  863. pr_debug("%s: pcm_irq_pos = %d, pcm_size = %d, sample_bits = %d,\n"
  864. "frame_bits = %d\n", __func__, prtd->pcm_irq_pos,
  865. prtd->pcm_size, runtime->sample_bits,
  866. runtime->frame_bits);
  867. return bytes_to_frames(runtime, (prtd->pcm_irq_pos));
  868. }
  869. static int msm_compr_mmap(struct snd_pcm_substream *substream,
  870. struct vm_area_struct *vma)
  871. {
  872. int result = 0;
  873. struct snd_pcm_runtime *runtime = substream->runtime;
  874. struct compr_audio *compr = runtime->private_data;
  875. struct msm_audio *prtd = &compr->prtd;
  876. pr_debug("%s\n", __func__);
  877. prtd->mmap_flag = 1;
  878. runtime->render_flag = SNDRV_NON_DMA_MODE;
  879. if (runtime->dma_addr && runtime->dma_bytes) {
  880. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  881. result = remap_pfn_range(vma, vma->vm_start,
  882. runtime->dma_addr >> PAGE_SHIFT,
  883. runtime->dma_bytes,
  884. vma->vm_page_prot);
  885. } else {
  886. pr_err("Physical address or size of buf is NULL");
  887. return -EINVAL;
  888. }
  889. return result;
  890. }
  891. static int msm_compr_hw_params(struct snd_pcm_substream *substream,
  892. struct snd_pcm_hw_params *params)
  893. {
  894. struct snd_pcm_runtime *runtime = substream->runtime;
  895. struct snd_soc_pcm_runtime *soc_prtd = substream->private_data;
  896. struct compr_audio *compr = runtime->private_data;
  897. struct msm_audio *prtd = &compr->prtd;
  898. struct snd_dma_buffer *dma_buf = &substream->dma_buffer;
  899. struct audio_buffer *buf;
  900. int dir, ret;
  901. pr_debug("%s\n", __func__);
  902. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  903. dir = IN;
  904. else
  905. dir = OUT;
  906. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  907. switch (compr->info.codec_param.codec.id) {
  908. case SND_AUDIOCODEC_AC3_PASS_THROUGH:
  909. case SND_AUDIOCODEC_DTS_PASS_THROUGH:
  910. case SND_AUDIOCODEC_DTS_LBR_PASS_THROUGH:
  911. ret = q6asm_open_write_compressed(prtd->audio_client,
  912. compr->codec);
  913. if (ret < 0) {
  914. pr_err("%s: Session out open failed\n",
  915. __func__);
  916. return -ENOMEM;
  917. }
  918. break;
  919. default:
  920. ret = q6asm_open_write(prtd->audio_client,
  921. compr->codec);
  922. if (ret < 0) {
  923. pr_err("%s: Session out open failed\n",
  924. __func__);
  925. return -ENOMEM;
  926. }
  927. msm_pcm_routing_reg_phy_stream(
  928. soc_prtd->dai_link->be_id,
  929. prtd->audio_client->perf_mode,
  930. prtd->session_id,
  931. substream->stream);
  932. if (compr->info.codec_param.codec.transcode_dts) {
  933. prtd->enc_audio_client =
  934. q6asm_audio_client_alloc(
  935. (app_cb)compr_event_handler, compr);
  936. if (!prtd->enc_audio_client) {
  937. pr_err("%s: Could not allocate " \
  938. "memory\n", __func__);
  939. return -ENOMEM;
  940. }
  941. prtd->enc_audio_client->perf_mode = false;
  942. pr_debug("%s Setting up loopback path\n",
  943. __func__);
  944. ret = q6asm_open_transcode_loopback(
  945. prtd->enc_audio_client,
  946. params_channels(params));
  947. if (ret < 0) {
  948. pr_err("%s: Session transcode " \
  949. "loopback open failed\n",
  950. __func__);
  951. return -ENODEV;
  952. }
  953. }
  954. break;
  955. }
  956. } else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  957. switch (compr->info.codec_param.codec.id) {
  958. case SND_AUDIOCODEC_AMRWB:
  959. pr_debug("q6asm_open_read(FORMAT_AMRWB)\n");
  960. ret = q6asm_open_read(prtd->audio_client,
  961. FORMAT_AMRWB);
  962. if (ret < 0) {
  963. pr_err("%s: compressed Session out open failed\n",
  964. __func__);
  965. return -ENOMEM;
  966. }
  967. pr_debug("msm_pcm_routing_reg_phy_stream\n");
  968. msm_pcm_routing_reg_phy_stream(
  969. soc_prtd->dai_link->be_id,
  970. prtd->audio_client->perf_mode,
  971. prtd->session_id, substream->stream);
  972. break;
  973. case SND_AUDIOCODEC_PCM:
  974. pr_debug("q6asm_open_read(FORMAT_PCM)\n");
  975. ret = q6asm_open_read(prtd->audio_client,
  976. FORMAT_MULTI_CHANNEL_LINEAR_PCM);
  977. if (ret < 0) {
  978. pr_err("%s: compressed Session open failed\n",
  979. __func__);
  980. return -ENOMEM;
  981. }
  982. pr_debug("msm_pcm_routing_reg_phy_stream\n");
  983. msm_pcm_routing_reg_phy_stream(
  984. soc_prtd->dai_link->be_id,
  985. prtd->audio_client->perf_mode,
  986. prtd->session_id, substream->stream);
  987. break;
  988. case SND_AUDIOCODEC_PASS_THROUGH:
  989. pr_debug("q6asm_open_read_compressed(COMPRESSED_META_DATA_MODE)\n");
  990. ret = q6asm_open_read_compressed(prtd->audio_client,
  991. MAX_NUM_FRAMES_PER_BUFFER,
  992. COMPRESSED_META_DATA_MODE);
  993. break;
  994. default:
  995. pr_err("Invalid codec for compressed session open\n");
  996. return -EFAULT;
  997. }
  998. if (ret < 0) {
  999. pr_err("%s: compressed Session out open failed\n",
  1000. __func__);
  1001. return -ENOMEM;
  1002. }
  1003. }
  1004. ret = q6asm_set_io_mode(prtd->audio_client, ASYNC_IO_MODE);
  1005. if (ret < 0) {
  1006. pr_err("%s: Set IO mode failed\n", __func__);
  1007. return -ENOMEM;
  1008. }
  1009. /* Modifying kernel hardware params based on userspace config */
  1010. if (params_periods(params) > 0 &&
  1011. (params_periods(params) != runtime->hw.periods_max)) {
  1012. runtime->hw.periods_max = params_periods(params);
  1013. }
  1014. if (params_period_bytes(params) > 0 &&
  1015. (params_period_bytes(params) != runtime->hw.period_bytes_min)) {
  1016. runtime->hw.period_bytes_min = params_period_bytes(params);
  1017. }
  1018. runtime->hw.buffer_bytes_max =
  1019. runtime->hw.period_bytes_min * runtime->hw.periods_max;
  1020. ret = q6asm_audio_client_buf_alloc_contiguous(dir,
  1021. prtd->audio_client,
  1022. runtime->hw.period_bytes_min,
  1023. runtime->hw.periods_max);
  1024. if (ret < 0) {
  1025. pr_err("Audio Start: Buffer Allocation failed "
  1026. "rc = %d\n", ret);
  1027. return -ENOMEM;
  1028. }
  1029. buf = prtd->audio_client->port[dir].buf;
  1030. dma_buf->dev.type = SNDRV_DMA_TYPE_DEV;
  1031. dma_buf->dev.dev = substream->pcm->card->dev;
  1032. dma_buf->private_data = NULL;
  1033. dma_buf->area = buf[0].data;
  1034. dma_buf->addr = buf[0].phys;
  1035. dma_buf->bytes = runtime->hw.buffer_bytes_max;
  1036. pr_debug("%s: buf[%p]dma_buf->area[%p]dma_buf->addr[%p]\n"
  1037. "dma_buf->bytes[%d]\n", __func__,
  1038. (void *)buf, (void *)dma_buf->area,
  1039. (void *)dma_buf->addr, dma_buf->bytes);
  1040. if (!dma_buf->area)
  1041. return -ENOMEM;
  1042. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  1043. return 0;
  1044. }
  1045. static int msm_compr_ioctl(struct snd_pcm_substream *substream,
  1046. unsigned int cmd, void *arg)
  1047. {
  1048. int rc = 0;
  1049. struct snd_pcm_runtime *runtime = substream->runtime;
  1050. struct compr_audio *compr = runtime->private_data;
  1051. struct msm_audio *prtd = &compr->prtd;
  1052. uint64_t timestamp;
  1053. uint64_t temp;
  1054. switch (cmd) {
  1055. case SNDRV_COMPRESS_TSTAMP: {
  1056. struct snd_compr_tstamp tstamp;
  1057. pr_debug("SNDRV_COMPRESS_TSTAMP\n");
  1058. memset(&tstamp, 0x0, sizeof(struct snd_compr_tstamp));
  1059. rc = q6asm_get_session_time(prtd->audio_client, &timestamp);
  1060. if (rc < 0) {
  1061. pr_err("%s: fail to get session tstamp\n", __func__);
  1062. return rc;
  1063. }
  1064. temp = (timestamp * 2 * runtime->channels);
  1065. temp = temp * (runtime->rate/1000);
  1066. temp = div_u64(temp, 1000);
  1067. tstamp.sampling_rate = runtime->rate;
  1068. tstamp.timestamp = timestamp;
  1069. pr_debug("%s: bytes_consumed:,"
  1070. "timestamp = %lld,\n", __func__,
  1071. tstamp.timestamp);
  1072. if (copy_to_user((void *) arg, &tstamp,
  1073. sizeof(struct snd_compr_tstamp)))
  1074. return -EFAULT;
  1075. return 0;
  1076. }
  1077. case SNDRV_COMPRESS_GET_CAPS:
  1078. pr_debug("SNDRV_COMPRESS_GET_CAPS\n");
  1079. if (copy_to_user((void *) arg, &compr->info.compr_cap,
  1080. sizeof(struct snd_compr_caps))) {
  1081. rc = -EFAULT;
  1082. pr_err("%s: ERROR: copy to user\n", __func__);
  1083. return rc;
  1084. }
  1085. return 0;
  1086. case SNDRV_COMPRESS_SET_PARAMS:
  1087. pr_debug("SNDRV_COMPRESS_SET_PARAMS: ");
  1088. if (copy_from_user(&compr->info.codec_param, (void *) arg,
  1089. sizeof(struct snd_compr_params))) {
  1090. rc = -EFAULT;
  1091. pr_err("%s: ERROR: copy from user\n", __func__);
  1092. return rc;
  1093. }
  1094. /*
  1095. * DTS Security needed for the transcode path
  1096. */
  1097. if (compr->info.codec_param.codec.transcode_dts) {
  1098. char modelId[128];
  1099. struct snd_dec_dts opt_dts =
  1100. compr->info.codec_param.codec.dts;
  1101. unsigned int modelIdLength = opt_dts.modelIdLength;
  1102. if (modelIdLength >= sizeof(modelId)) {
  1103. rc = -EINVAL;
  1104. pr_err("%s: ERROR: modelIdLength is"
  1105. "invalid\n", __func__);
  1106. return rc;
  1107. }
  1108. if (copy_from_user(modelId, (void *)opt_dts.modelId,
  1109. modelIdLength))
  1110. pr_err("%s: ERROR: copy modelId\n", __func__);
  1111. modelId[modelIdLength] = '\0';
  1112. pr_debug("%s: Received modelId =%s,length=%d\n",
  1113. __func__, modelId, modelIdLength);
  1114. core_set_dts_model_id(modelIdLength, modelId);
  1115. }
  1116. switch (compr->info.codec_param.codec.id) {
  1117. case SND_AUDIOCODEC_MP3:
  1118. /* For MP3 we dont need any other parameter */
  1119. pr_debug("SND_AUDIOCODEC_MP3\n");
  1120. compr->codec = FORMAT_MP3;
  1121. break;
  1122. case SND_AUDIOCODEC_AAC:
  1123. pr_debug("SND_AUDIOCODEC_AAC\n");
  1124. compr->codec = FORMAT_MPEG4_AAC;
  1125. break;
  1126. case SND_AUDIOCODEC_AC3_PASS_THROUGH:
  1127. pr_debug("SND_AUDIOCODEC_AC3_PASS_THROUGH\n");
  1128. compr->codec = FORMAT_AC3;
  1129. break;
  1130. case SND_AUDIOCODEC_WMA:
  1131. pr_debug("SND_AUDIOCODEC_WMA\n");
  1132. compr->codec = FORMAT_WMA_V9;
  1133. break;
  1134. case SND_AUDIOCODEC_WMA_PRO:
  1135. pr_debug("SND_AUDIOCODEC_WMA_PRO\n");
  1136. compr->codec = FORMAT_WMA_V10PRO;
  1137. break;
  1138. case SND_AUDIOCODEC_DTS_PASS_THROUGH:
  1139. pr_debug("SND_AUDIOCODEC_DTS_PASS_THROUGH\n");
  1140. compr->codec = FORMAT_DTS;
  1141. break;
  1142. case SND_AUDIOCODEC_DTS_LBR_PASS_THROUGH:
  1143. pr_debug("SND_AUDIOCODEC_DTS_LBR_PASS_THROUGH\n");
  1144. compr->codec = FORMAT_DTS_LBR;
  1145. break;
  1146. case SND_AUDIOCODEC_DTS: {
  1147. char modelId[128];
  1148. struct snd_dec_dts opt_dts =
  1149. compr->info.codec_param.codec.dts;
  1150. unsigned int modelIdLength = opt_dts.modelIdLength;
  1151. pr_debug("SND_AUDIOCODEC_DTS\n");
  1152. if (modelIdLength >= sizeof(modelId)) {
  1153. rc = -EINVAL;
  1154. pr_err("%s: ERROR: modelIdLength is"
  1155. "invalid\n", __func__);
  1156. return rc;
  1157. }
  1158. if (copy_from_user(modelId, (void *)opt_dts.modelId,
  1159. modelIdLength))
  1160. pr_err("%s: ERROR: copy modelId\n", __func__);
  1161. modelId[modelIdLength] = '\0';
  1162. pr_debug("%s: Received modelId =%s,length=%d\n",
  1163. __func__, modelId, modelIdLength);
  1164. core_set_dts_model_id(modelIdLength, modelId);
  1165. compr->codec = FORMAT_DTS;
  1166. }
  1167. break;
  1168. case SND_AUDIOCODEC_DTS_LBR:{
  1169. char modelId[128];
  1170. struct snd_dec_dts opt_dts =
  1171. compr->info.codec_param.codec.dts;
  1172. int modelIdLength = opt_dts.modelIdLength;
  1173. pr_debug("SND_AUDIOCODEC_DTS_LBR\n");
  1174. if (copy_from_user(modelId, (void *)opt_dts.modelId,
  1175. modelIdLength))
  1176. pr_err("%s: ERROR: copy modelId\n", __func__);
  1177. modelId[modelIdLength] = '\0';
  1178. pr_debug("%s: Received modelId =%s,length=%d\n",
  1179. __func__, modelId, modelIdLength);
  1180. core_set_dts_model_id(modelIdLength, modelId);
  1181. compr->codec = FORMAT_DTS_LBR;
  1182. }
  1183. break;
  1184. case SND_AUDIOCODEC_AMRWB:
  1185. pr_debug("msm_compr_ioctl SND_AUDIOCODEC_AMRWB\n");
  1186. compr->codec = FORMAT_AMRWB;
  1187. break;
  1188. case SND_AUDIOCODEC_AMRWBPLUS:
  1189. pr_debug("msm_compr_ioctl SND_AUDIOCODEC_AMRWBPLUS\n");
  1190. compr->codec = FORMAT_AMR_WB_PLUS;
  1191. break;
  1192. case SND_AUDIOCODEC_PASS_THROUGH:
  1193. /* format pass through is used for HDMI IN compressed
  1194. where the decoder format is indicated by LPASS */
  1195. pr_debug("msm_compr_ioctl SND_AUDIOCODEC_PASSTHROUGH\n");
  1196. compr->codec = FORMAT_PASS_THROUGH;
  1197. break;
  1198. case SND_AUDIOCODEC_PCM:
  1199. pr_debug("msm_compr_ioctl SND_AUDIOCODEC_PCM\n");
  1200. compr->codec = FORMAT_MULTI_CHANNEL_LINEAR_PCM;
  1201. break;
  1202. case SND_AUDIOCODEC_MP2:
  1203. pr_debug("SND_AUDIOCODEC_MP2\n");
  1204. compr->codec = FORMAT_MP2;
  1205. break;
  1206. default:
  1207. pr_err("msm_compr_ioctl failed..unknown codec\n");
  1208. return -EFAULT;
  1209. }
  1210. return 0;
  1211. case SNDRV_PCM_IOCTL1_RESET:
  1212. pr_debug("SNDRV_PCM_IOCTL1_RESET\n");
  1213. /* Flush only when session is started during CAPTURE,
  1214. while PLAYBACK has no such restriction. */
  1215. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK ||
  1216. (substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
  1217. atomic_read(&prtd->start))) {
  1218. if (atomic_read(&prtd->eos)) {
  1219. prtd->cmd_ack = 1;
  1220. wake_up(&the_locks.eos_wait);
  1221. atomic_set(&prtd->eos, 0);
  1222. atomic_set(&prtd->pending_buffer, 1);
  1223. }
  1224. /* A unlikely race condition possible with FLUSH
  1225. DRAIN if ack is set by flush and reset by drain */
  1226. prtd->cmd_ack = 0;
  1227. rc = q6asm_cmd(prtd->audio_client, CMD_FLUSH);
  1228. if (rc < 0) {
  1229. pr_err("%s: flush cmd failed rc=%d\n",
  1230. __func__, rc);
  1231. return rc;
  1232. }
  1233. rc = wait_event_timeout(the_locks.flush_wait,
  1234. prtd->cmd_ack, 5 * HZ);
  1235. if (!rc)
  1236. pr_err("Flush cmd timeout\n");
  1237. prtd->pcm_irq_pos = 0;
  1238. }
  1239. break;
  1240. case SNDRV_COMPRESS_DRAIN:
  1241. pr_debug("%s: SNDRV_COMPRESS_DRAIN\n", __func__);
  1242. atomic_set(&prtd->eos, 1);
  1243. atomic_set(&prtd->pending_buffer, 0);
  1244. prtd->cmd_ack = 0;
  1245. q6asm_cmd_nowait(prtd->audio_client, CMD_EOS);
  1246. /* Wait indefinitely for DRAIN. Flush can also signal this*/
  1247. rc = wait_event_interruptible(the_locks.eos_wait,
  1248. prtd->cmd_ack);
  1249. if (rc < 0)
  1250. pr_err("EOS cmd interrupted\n");
  1251. pr_debug("%s: SNDRV_COMPRESS_DRAIN out of wait\n", __func__);
  1252. return 0;
  1253. default:
  1254. break;
  1255. }
  1256. return snd_pcm_lib_ioctl(substream, cmd, arg);
  1257. }
  1258. static struct snd_pcm_ops msm_compr_ops = {
  1259. .open = msm_compr_open,
  1260. .hw_params = msm_compr_hw_params,
  1261. .close = msm_compr_close,
  1262. .ioctl = msm_compr_ioctl,
  1263. .prepare = msm_compr_prepare,
  1264. .trigger = msm_compr_trigger,
  1265. .pointer = msm_compr_pointer,
  1266. .mmap = msm_compr_mmap,
  1267. .restart = msm_compr_restart,
  1268. };
  1269. static int msm_asoc_pcm_new(struct snd_soc_pcm_runtime *rtd)
  1270. {
  1271. struct snd_card *card = rtd->card->snd_card;
  1272. int ret = 0;
  1273. if (!card->dev->coherent_dma_mask)
  1274. card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
  1275. return ret;
  1276. }
  1277. static struct snd_soc_platform_driver msm_soc_platform = {
  1278. .ops = &msm_compr_ops,
  1279. .pcm_new = msm_asoc_pcm_new,
  1280. };
  1281. static __devinit int msm_compr_probe(struct platform_device *pdev)
  1282. {
  1283. pr_info("%s: dev name %s\n", __func__, dev_name(&pdev->dev));
  1284. return snd_soc_register_platform(&pdev->dev,
  1285. &msm_soc_platform);
  1286. }
  1287. static int msm_compr_remove(struct platform_device *pdev)
  1288. {
  1289. snd_soc_unregister_platform(&pdev->dev);
  1290. return 0;
  1291. }
  1292. static struct platform_driver msm_compr_driver = {
  1293. .driver = {
  1294. .name = "msm-compr-dsp",
  1295. .owner = THIS_MODULE,
  1296. },
  1297. .probe = msm_compr_probe,
  1298. .remove = __devexit_p(msm_compr_remove),
  1299. };
  1300. static int __init msm_soc_platform_init(void)
  1301. {
  1302. init_waitqueue_head(&the_locks.enable_wait);
  1303. init_waitqueue_head(&the_locks.eos_wait);
  1304. init_waitqueue_head(&the_locks.write_wait);
  1305. init_waitqueue_head(&the_locks.read_wait);
  1306. init_waitqueue_head(&the_locks.flush_wait);
  1307. return platform_driver_register(&msm_compr_driver);
  1308. }
  1309. module_init(msm_soc_platform_init);
  1310. static void __exit msm_soc_platform_exit(void)
  1311. {
  1312. platform_driver_unregister(&msm_compr_driver);
  1313. }
  1314. module_exit(msm_soc_platform_exit);
  1315. MODULE_DESCRIPTION("PCM module platform driver");
  1316. MODULE_LICENSE("GPL v2");