msm8x60-pcm.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  1. /* Copyright (c) 2010-2011, 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 <asm/dma.h>
  27. #include <linux/dma-mapping.h>
  28. #include <linux/delay.h>
  29. #include <mach/qdsp6v2/audio_dev_ctl.h>
  30. #include "msm8x60-pcm.h"
  31. struct snd_msm {
  32. struct snd_card *card;
  33. struct snd_pcm *pcm;
  34. };
  35. static struct snd_pcm_hardware msm_pcm_hardware = {
  36. .info = (SNDRV_PCM_INFO_MMAP |
  37. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  38. SNDRV_PCM_INFO_MMAP_VALID |
  39. SNDRV_PCM_INFO_INTERLEAVED |
  40. SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME),
  41. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  42. .rates = SNDRV_PCM_RATE_8000_48000,
  43. .rate_min = 8000,
  44. .rate_max = 48000,
  45. .channels_min = 1,
  46. .channels_max = 2,
  47. .buffer_bytes_max = 960 * 10,
  48. .period_bytes_min = 960 * 5,
  49. .period_bytes_max = 960 * 5,
  50. .periods_min = 2,
  51. .periods_max = 2,
  52. .fifo_size = 0,
  53. };
  54. /* Conventional and unconventional sample rate supported */
  55. static unsigned int supported_sample_rates[] = {
  56. 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000
  57. };
  58. uint32_t in_frame_info[8][2];
  59. static struct snd_pcm_hw_constraint_list constraints_sample_rates = {
  60. .count = ARRAY_SIZE(supported_sample_rates),
  61. .list = supported_sample_rates,
  62. .mask = 0,
  63. };
  64. static void alsa_out_listener(u32 evt_id, union auddev_evt_data *evt_payload,
  65. void *private_data)
  66. {
  67. int ret = 0;
  68. struct msm_audio *prtd = (struct msm_audio *) private_data;
  69. int dev_rate = 48000;
  70. pr_debug("evt_id = 0x%8x\n", evt_id);
  71. switch (evt_id) {
  72. case AUDDEV_EVT_DEV_RDY:
  73. pr_debug("AUDDEV_EVT_DEV_RDY\n");
  74. prtd->copp_id = evt_payload->routing_id;
  75. pr_debug("prtd->session_id = %d, copp_id= %d",
  76. prtd->session_id, prtd->copp_id);
  77. if (prtd->copp_id == PCM_RX)
  78. dev_rate = 8000;
  79. ret = msm_snddev_set_dec(prtd->session_id, prtd->copp_id, 1,
  80. dev_rate, 1);
  81. break;
  82. case AUDDEV_EVT_DEV_RLS:
  83. pr_debug("AUDDEV_EVT_DEV_RLS\n");
  84. prtd->copp_id = evt_payload->routing_id;
  85. pr_debug("prtd->session_id = %d, copp_id= %d",
  86. prtd->session_id, prtd->copp_id);
  87. if (prtd->copp_id == PCM_RX)
  88. dev_rate = 8000;
  89. ret = msm_snddev_set_dec(prtd->session_id, prtd->copp_id, 0,
  90. dev_rate, 1);
  91. break;
  92. case AUDDEV_EVT_STREAM_VOL_CHG:
  93. pr_debug("AUDDEV_EVT_STREAM_VOL_CHG\n");
  94. break;
  95. default:
  96. pr_debug("Unknown Event\n");
  97. break;
  98. }
  99. }
  100. static void alsa_in_listener(u32 evt_id, union auddev_evt_data *evt_payload,
  101. void *private_data)
  102. {
  103. int ret = 0;
  104. struct msm_audio *prtd = (struct msm_audio *) private_data;
  105. int dev_rate = 48000;
  106. pr_debug("evt_id = 0x%8x\n", evt_id);
  107. switch (evt_id) {
  108. case AUDDEV_EVT_DEV_RDY:
  109. prtd->copp_id = evt_payload->routing_id;
  110. if (prtd->copp_id == PCM_TX)
  111. dev_rate = 8000;
  112. ret = msm_snddev_set_enc(prtd->session_id, prtd->copp_id, 1,
  113. dev_rate, 1);
  114. break;
  115. case AUDDEV_EVT_DEV_RLS:
  116. prtd->copp_id = evt_payload->routing_id;
  117. if (prtd->copp_id == PCM_TX)
  118. dev_rate = 8000;
  119. ret = msm_snddev_set_enc(prtd->session_id, prtd->copp_id, 0,
  120. dev_rate, 1);
  121. break;
  122. default:
  123. pr_debug("Unknown Event\n");
  124. break;
  125. }
  126. }
  127. static void event_handler(uint32_t opcode,
  128. uint32_t token, uint32_t *payload, void *priv)
  129. {
  130. struct msm_audio *prtd = priv;
  131. struct snd_pcm_substream *substream = prtd->substream;
  132. uint32_t *ptrmem = (uint32_t *)payload;
  133. int i = 0;
  134. pr_debug("%s\n", __func__);
  135. switch (opcode) {
  136. case ASM_DATA_EVENT_WRITE_DONE: {
  137. pr_debug("ASM_DATA_EVENT_WRITE_DONE\n");
  138. pr_debug("Buffer Consumed = 0x%08x\n", *ptrmem);
  139. prtd->pcm_irq_pos += prtd->pcm_count;
  140. if (atomic_read(&prtd->start))
  141. snd_pcm_period_elapsed(substream);
  142. atomic_inc(&prtd->out_count);
  143. wake_up(&the_locks.write_wait);
  144. if (!atomic_read(&prtd->start))
  145. break;
  146. if (!prtd->mmap_flag)
  147. break;
  148. pr_debug("%s:writing %d bytes of buffer to dsp 2\n",
  149. __func__, prtd->pcm_count);
  150. q6asm_write_nolock(prtd->audio_client,
  151. prtd->pcm_count, 0, 0, NO_TIMESTAMP);
  152. break;
  153. }
  154. case ASM_DATA_CMDRSP_EOS:
  155. pr_debug("ASM_DATA_CMDRSP_EOS\n");
  156. prtd->cmd_ack = 1;
  157. wake_up(&the_locks.eos_wait);
  158. break;
  159. case ASM_DATA_EVENT_READ_DONE: {
  160. pr_debug("ASM_DATA_EVENT_READ_DONE\n");
  161. pr_debug("token = 0x%08x\n", token);
  162. for (i = 0; i < 8; i++, ++ptrmem)
  163. pr_debug("cmd[%d]=0x%08x\n", i, *ptrmem);
  164. in_frame_info[token][0] = payload[2];
  165. in_frame_info[token][1] = payload[3];
  166. prtd->pcm_irq_pos += in_frame_info[token][0];
  167. pr_debug("pcm_irq_pos=%d\n", prtd->pcm_irq_pos);
  168. if (atomic_read(&prtd->start))
  169. snd_pcm_period_elapsed(substream);
  170. if (atomic_read(&prtd->in_count) <= prtd->periods)
  171. atomic_inc(&prtd->in_count);
  172. wake_up(&the_locks.read_wait);
  173. if (prtd->mmap_flag)
  174. q6asm_read_nolock(prtd->audio_client);
  175. break;
  176. }
  177. case APR_BASIC_RSP_RESULT: {
  178. if (!prtd->mmap_flag
  179. && !atomic_read(&prtd->out_needed))
  180. break;
  181. switch (payload[0]) {
  182. case ASM_SESSION_CMD_RUN:
  183. if (substream->stream
  184. != SNDRV_PCM_STREAM_PLAYBACK)
  185. break;
  186. if (prtd->mmap_flag) {
  187. pr_debug("%s:writing %d bytes"
  188. " of buffer to dsp\n",
  189. __func__,
  190. prtd->pcm_count);
  191. q6asm_write_nolock(prtd->audio_client,
  192. prtd->pcm_count,
  193. 0, 0, NO_TIMESTAMP);
  194. } else {
  195. while (atomic_read(&prtd->out_needed)) {
  196. pr_debug("%s:writing %d bytes"
  197. " of buffer to dsp\n",
  198. __func__,
  199. prtd->pcm_count);
  200. q6asm_write_nolock(prtd->audio_client,
  201. prtd->pcm_count,
  202. 0, 0, NO_TIMESTAMP);
  203. atomic_dec(&prtd->out_needed);
  204. wake_up(&the_locks.write_wait);
  205. };
  206. }
  207. break;
  208. default:
  209. break;
  210. }
  211. }
  212. break;
  213. default:
  214. pr_debug("Not Supported Event opcode[0x%x]\n", opcode);
  215. break;
  216. }
  217. }
  218. static int msm_pcm_playback_prepare(struct snd_pcm_substream *substream)
  219. {
  220. struct snd_pcm_runtime *runtime = substream->runtime;
  221. struct msm_audio *prtd = runtime->private_data;
  222. int ret;
  223. int dev_rate = 48000;
  224. int i = 0;
  225. pr_debug("%s\n", __func__);
  226. prtd->pcm_size = snd_pcm_lib_buffer_bytes(substream);
  227. prtd->pcm_count = snd_pcm_lib_period_bytes(substream);
  228. prtd->pcm_irq_pos = 0;
  229. /* rate and channels are sent to audio driver */
  230. prtd->samp_rate = runtime->rate;
  231. prtd->channel_mode = runtime->channels;
  232. if (prtd->enabled)
  233. return 0;
  234. ret = q6asm_media_format_block_pcm(prtd->audio_client, runtime->rate,
  235. runtime->channels);
  236. if (ret < 0)
  237. pr_debug("%s: CMD Format block failed\n", __func__);
  238. atomic_set(&prtd->out_count, runtime->periods);
  239. atomic_set(&prtd->in_count, 0);
  240. for (i = 0; i < MAX_COPP; i++) {
  241. pr_debug("prtd->session_id = %d, copp_id= %d",
  242. prtd->session_id, i);
  243. if (session_route.playback_session[substream->number][i]
  244. != DEVICE_IGNORE) {
  245. pr_err("Device active\n");
  246. if (i == PCM_RX)
  247. dev_rate = 8000;
  248. msm_snddev_set_dec(prtd->session_id,
  249. i, 1, dev_rate, runtime->channels);
  250. }
  251. }
  252. prtd->enabled = 1;
  253. prtd->cmd_ack = 0;
  254. return 0;
  255. }
  256. static int msm_pcm_capture_prepare(struct snd_pcm_substream *substream)
  257. {
  258. struct snd_pcm_runtime *runtime = substream->runtime;
  259. struct msm_audio *prtd = runtime->private_data;
  260. int ret = 0;
  261. int i = 0;
  262. int dev_rate = 48000;
  263. pr_debug("%s\n", __func__);
  264. prtd->pcm_size = snd_pcm_lib_buffer_bytes(substream);
  265. prtd->pcm_count = snd_pcm_lib_period_bytes(substream);
  266. prtd->pcm_irq_pos = 0;
  267. /* rate and channels are sent to audio driver */
  268. prtd->samp_rate = runtime->rate;
  269. prtd->channel_mode = runtime->channels;
  270. if (prtd->enabled)
  271. return 0;
  272. pr_debug("Samp_rate = %d\n", prtd->samp_rate);
  273. pr_debug("Channel = %d\n", prtd->channel_mode);
  274. ret = q6asm_enc_cfg_blk_pcm(prtd->audio_client, prtd->samp_rate,
  275. prtd->channel_mode);
  276. if (ret < 0)
  277. pr_debug("%s: cmd cfg pcm was block failed", __func__);
  278. for (i = 0; i < runtime->periods; i++)
  279. q6asm_read_nolock(prtd->audio_client);
  280. prtd->periods = runtime->periods;
  281. for (i = 0; i < MAX_COPP; i++) {
  282. pr_debug("prtd->session_id = %d, copp_id= %d",
  283. prtd->session_id,
  284. session_route.capture_session[prtd->session_id][i]);
  285. if (session_route.capture_session[prtd->session_id][i]
  286. != DEVICE_IGNORE) {
  287. if (i == PCM_RX)
  288. dev_rate = 8000;
  289. msm_snddev_set_enc(prtd->session_id, i, 1, dev_rate, 1);
  290. }
  291. }
  292. prtd->enabled = 1;
  293. return ret;
  294. }
  295. static int msm_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  296. {
  297. int ret = 0;
  298. struct snd_pcm_runtime *runtime = substream->runtime;
  299. struct msm_audio *prtd = runtime->private_data;
  300. pr_debug("%s\n", __func__);
  301. switch (cmd) {
  302. case SNDRV_PCM_TRIGGER_START:
  303. case SNDRV_PCM_TRIGGER_RESUME:
  304. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  305. pr_debug("SNDRV_PCM_TRIGGER_START\n");
  306. q6asm_run_nowait(prtd->audio_client, 0, 0, 0);
  307. atomic_set(&prtd->start, 1);
  308. break;
  309. case SNDRV_PCM_TRIGGER_STOP:
  310. pr_debug("SNDRV_PCM_TRIGGER_STOP\n");
  311. atomic_set(&prtd->start, 0);
  312. if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
  313. break;
  314. prtd->cmd_ack = 0;
  315. q6asm_cmd_nowait(prtd->audio_client, CMD_EOS);
  316. break;
  317. case SNDRV_PCM_TRIGGER_SUSPEND:
  318. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  319. pr_debug("SNDRV_PCM_TRIGGER_PAUSE\n");
  320. q6asm_cmd_nowait(prtd->audio_client, CMD_PAUSE);
  321. atomic_set(&prtd->start, 0);
  322. break;
  323. default:
  324. ret = -EINVAL;
  325. break;
  326. }
  327. return ret;
  328. }
  329. static int msm_pcm_open(struct snd_pcm_substream *substream)
  330. {
  331. struct snd_pcm_runtime *runtime = substream->runtime;
  332. struct msm_audio *prtd;
  333. int ret = 0;
  334. pr_debug("%s\n", __func__);
  335. prtd = kzalloc(sizeof(struct msm_audio), GFP_KERNEL);
  336. if (prtd == NULL) {
  337. pr_err("Failed to allocate memory for msm_audio\n");
  338. return -ENOMEM;
  339. }
  340. runtime->hw = msm_pcm_hardware;
  341. prtd->substream = substream;
  342. prtd->audio_client = q6asm_audio_client_alloc(
  343. (app_cb)event_handler, prtd);
  344. if (!prtd->audio_client) {
  345. pr_debug("%s: Could not allocate memory\n", __func__);
  346. kfree(prtd);
  347. return -ENOMEM;
  348. }
  349. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  350. ret = q6asm_open_write(prtd->audio_client, FORMAT_LINEAR_PCM);
  351. if (ret < 0) {
  352. pr_err("%s: pcm out open failed\n", __func__);
  353. q6asm_audio_client_free(prtd->audio_client);
  354. kfree(prtd);
  355. return -ENOMEM;
  356. }
  357. }
  358. /* Capture path */
  359. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  360. ret = q6asm_open_read(prtd->audio_client, FORMAT_LINEAR_PCM);
  361. if (ret < 0) {
  362. pr_err("%s: pcm in open failed\n", __func__);
  363. q6asm_audio_client_free(prtd->audio_client);
  364. kfree(prtd);
  365. return -ENOMEM;
  366. }
  367. }
  368. /* The session id returned by q6asm_open_read above is random and
  369. * hence we cannot use the session id to route from user space.
  370. * This results in need of a hardcoded session id for both playback
  371. * and capture sessions. we can use the subdevice id to identify
  372. * the session and use that for routing. Hence using
  373. * substream->number as the session id for routing purpose. However
  374. * DSP understands the session based on the allocated session id,
  375. * hence using the variable prtd->session_id for all dsp commands.
  376. */
  377. prtd->session_id = prtd->audio_client->session;
  378. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  379. prtd->cmd_ack = 1;
  380. prtd->device_events = AUDDEV_EVT_DEV_RDY |
  381. AUDDEV_EVT_STREAM_VOL_CHG |
  382. AUDDEV_EVT_DEV_RLS;
  383. prtd->source = msm_snddev_route_dec(prtd->session_id);
  384. pr_debug("Register device event listener for"
  385. "SNDRV_PCM_STREAM_PLAYBACK session %d\n",
  386. substream->number);
  387. ret = auddev_register_evt_listner(prtd->device_events,
  388. AUDDEV_CLNT_DEC, substream->number,
  389. alsa_out_listener, (void *) prtd);
  390. if (ret)
  391. pr_debug("failed to register device event listener\n");
  392. } else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  393. prtd->device_events = AUDDEV_EVT_DEV_RDY | AUDDEV_EVT_DEV_RLS |
  394. AUDDEV_EVT_FREQ_CHG;
  395. prtd->source = msm_snddev_route_enc(prtd->session_id);
  396. pr_debug("Register device event listener for"
  397. "SNDRV_PCM_STREAM_CAPTURE session %d\n",
  398. substream->number);
  399. ret = auddev_register_evt_listner(prtd->device_events,
  400. AUDDEV_CLNT_ENC, substream->number,
  401. alsa_in_listener, (void *) prtd);
  402. if (ret)
  403. pr_debug("failed to register device event listener\n");
  404. }
  405. ret = snd_pcm_hw_constraint_list(runtime, 0,
  406. SNDRV_PCM_HW_PARAM_RATE,
  407. &constraints_sample_rates);
  408. if (ret < 0)
  409. pr_debug("snd_pcm_hw_constraint_list failed\n");
  410. /* Ensure that buffer size is a multiple of period size */
  411. ret = snd_pcm_hw_constraint_integer(runtime,
  412. SNDRV_PCM_HW_PARAM_PERIODS);
  413. if (ret < 0)
  414. pr_debug("snd_pcm_hw_constraint_integer failed\n");
  415. prtd->dsp_cnt = 0;
  416. runtime->private_data = prtd;
  417. return 0;
  418. }
  419. static int msm_pcm_playback_copy(struct snd_pcm_substream *substream, int a,
  420. snd_pcm_uframes_t hwoff, void __user *buf, snd_pcm_uframes_t frames)
  421. {
  422. int ret = 0;
  423. int fbytes = 0;
  424. int xfer = 0;
  425. char *bufptr = NULL;
  426. void *data = NULL;
  427. uint32_t idx = 0;
  428. uint32_t size = 0;
  429. struct snd_pcm_runtime *runtime = substream->runtime;
  430. struct msm_audio *prtd = runtime->private_data;
  431. fbytes = frames_to_bytes(runtime, frames);
  432. pr_debug("%s: prtd->out_count = %d\n",
  433. __func__, atomic_read(&prtd->out_count));
  434. ret = wait_event_timeout(the_locks.write_wait,
  435. (atomic_read(&prtd->out_count)), 5 * HZ);
  436. if (ret < 0) {
  437. pr_debug("%s: wait_event_timeout failed\n", __func__);
  438. goto fail;
  439. }
  440. if (!atomic_read(&prtd->out_count)) {
  441. pr_debug("%s: pcm stopped out_count 0\n", __func__);
  442. return 0;
  443. }
  444. data = q6asm_is_cpu_buf_avail(IN, prtd->audio_client, &size, &idx);
  445. bufptr = data;
  446. if (bufptr) {
  447. pr_debug("%s:fbytes =%d: xfer=%d size=%d\n",
  448. __func__, fbytes, xfer, size);
  449. xfer = fbytes;
  450. if (copy_from_user(bufptr, buf, xfer)) {
  451. ret = -EFAULT;
  452. goto fail;
  453. }
  454. buf += xfer;
  455. fbytes -= xfer;
  456. pr_debug("%s:fbytes = %d: xfer=%d\n", __func__, fbytes, xfer);
  457. if (atomic_read(&prtd->start)) {
  458. pr_debug("%s:writing %d bytes of buffer to dsp\n",
  459. __func__, xfer);
  460. ret = q6asm_write_nolock(prtd->audio_client, xfer,
  461. 0, 0, NO_TIMESTAMP);
  462. if (ret < 0) {
  463. ret = -EFAULT;
  464. goto fail;
  465. }
  466. } else
  467. atomic_inc(&prtd->out_needed);
  468. atomic_dec(&prtd->out_count);
  469. }
  470. fail:
  471. return ret;
  472. }
  473. static int msm_pcm_playback_close(struct snd_pcm_substream *substream)
  474. {
  475. struct snd_pcm_runtime *runtime = substream->runtime;
  476. struct msm_audio *prtd = runtime->private_data;
  477. int dir = 0;
  478. int ret = 0;
  479. pr_debug("%s\n", __func__);
  480. dir = IN;
  481. ret = wait_event_timeout(the_locks.eos_wait,
  482. prtd->cmd_ack, 5 * HZ);
  483. if (ret < 0)
  484. pr_err("%s: CMD_EOS failed\n", __func__);
  485. q6asm_cmd(prtd->audio_client, CMD_CLOSE);
  486. q6asm_audio_client_buf_free_contiguous(dir,
  487. prtd->audio_client);
  488. pr_debug("%s\n", __func__);
  489. auddev_unregister_evt_listner(AUDDEV_CLNT_DEC,
  490. substream->number);
  491. pr_debug("%s\n", __func__);
  492. msm_clear_session_id(prtd->session_id);
  493. q6asm_audio_client_free(prtd->audio_client);
  494. kfree(prtd);
  495. return 0;
  496. }
  497. static int msm_pcm_capture_copy(struct snd_pcm_substream *substream,
  498. int channel, snd_pcm_uframes_t hwoff, void __user *buf,
  499. snd_pcm_uframes_t frames)
  500. {
  501. int ret = 0;
  502. int fbytes = 0;
  503. int xfer;
  504. char *bufptr;
  505. void *data = NULL;
  506. static uint32_t idx;
  507. static uint32_t size;
  508. uint32_t offset = 0;
  509. struct snd_pcm_runtime *runtime = substream->runtime;
  510. struct msm_audio *prtd = substream->runtime->private_data;
  511. pr_debug("%s\n", __func__);
  512. fbytes = frames_to_bytes(runtime, frames);
  513. pr_debug("appl_ptr %d\n", (int)runtime->control->appl_ptr);
  514. pr_debug("hw_ptr %d\n", (int)runtime->status->hw_ptr);
  515. pr_debug("avail_min %d\n", (int)runtime->control->avail_min);
  516. ret = wait_event_timeout(the_locks.read_wait,
  517. (atomic_read(&prtd->in_count)), 5 * HZ);
  518. if (ret < 0) {
  519. pr_debug("%s: wait_event_timeout failed\n", __func__);
  520. goto fail;
  521. }
  522. if (!atomic_read(&prtd->in_count)) {
  523. pr_debug("%s: pcm stopped in_count 0\n", __func__);
  524. return 0;
  525. }
  526. pr_debug("Checking if valid buffer is available...%08x\n",
  527. (unsigned int) data);
  528. data = q6asm_is_cpu_buf_avail(OUT, prtd->audio_client, &size, &idx);
  529. bufptr = data;
  530. pr_debug("Size = %d\n", size);
  531. pr_debug("fbytes = %d\n", fbytes);
  532. pr_debug("idx = %d\n", idx);
  533. if (bufptr) {
  534. xfer = fbytes;
  535. if (xfer > size)
  536. xfer = size;
  537. offset = in_frame_info[idx][1];
  538. pr_debug("Offset value = %d\n", offset);
  539. if (copy_to_user(buf, bufptr+offset, xfer)) {
  540. pr_err("Failed to copy buf to user\n");
  541. ret = -EFAULT;
  542. goto fail;
  543. }
  544. fbytes -= xfer;
  545. size -= xfer;
  546. in_frame_info[idx][1] += xfer;
  547. pr_debug("%s:fbytes = %d: size=%d: xfer=%d\n",
  548. __func__, fbytes, size, xfer);
  549. pr_debug(" Sending next buffer to dsp\n");
  550. memset(&in_frame_info[idx], 0,
  551. sizeof(uint32_t) * 2);
  552. atomic_dec(&prtd->in_count);
  553. ret = q6asm_read_nolock(prtd->audio_client);
  554. if (ret < 0) {
  555. pr_err("q6asm read failed\n");
  556. ret = -EFAULT;
  557. goto fail;
  558. }
  559. } else
  560. pr_err("No valid buffer\n");
  561. pr_debug("Returning from capture_copy... %d\n", ret);
  562. fail:
  563. return ret;
  564. }
  565. static int msm_pcm_capture_close(struct snd_pcm_substream *substream)
  566. {
  567. struct snd_pcm_runtime *runtime = substream->runtime;
  568. struct msm_audio *prtd = runtime->private_data;
  569. int dir = OUT;
  570. pr_debug("%s\n", __func__);
  571. q6asm_cmd(prtd->audio_client, CMD_CLOSE);
  572. q6asm_audio_client_buf_free_contiguous(dir,
  573. prtd->audio_client);
  574. auddev_unregister_evt_listner(AUDDEV_CLNT_ENC,
  575. substream->number);
  576. msm_clear_session_id(prtd->session_id);
  577. q6asm_audio_client_free(prtd->audio_client);
  578. kfree(prtd);
  579. return 0;
  580. }
  581. static int msm_pcm_copy(struct snd_pcm_substream *substream, int a,
  582. snd_pcm_uframes_t hwoff, void __user *buf, snd_pcm_uframes_t frames)
  583. {
  584. int ret = 0;
  585. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  586. ret = msm_pcm_playback_copy(substream, a, hwoff, buf, frames);
  587. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  588. ret = msm_pcm_capture_copy(substream, a, hwoff, buf, frames);
  589. return ret;
  590. }
  591. static int msm_pcm_close(struct snd_pcm_substream *substream)
  592. {
  593. int ret = 0;
  594. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  595. ret = msm_pcm_playback_close(substream);
  596. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  597. ret = msm_pcm_capture_close(substream);
  598. return ret;
  599. }
  600. static int msm_pcm_prepare(struct snd_pcm_substream *substream)
  601. {
  602. int ret = 0;
  603. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  604. ret = msm_pcm_playback_prepare(substream);
  605. else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  606. ret = msm_pcm_capture_prepare(substream);
  607. return ret;
  608. }
  609. static snd_pcm_uframes_t msm_pcm_pointer(struct snd_pcm_substream *substream)
  610. {
  611. struct snd_pcm_runtime *runtime = substream->runtime;
  612. struct msm_audio *prtd = runtime->private_data;
  613. if (prtd->pcm_irq_pos >= prtd->pcm_size)
  614. prtd->pcm_irq_pos = 0;
  615. pr_debug("%s: pcm_irq_pos = %d\n", __func__, prtd->pcm_irq_pos);
  616. return bytes_to_frames(runtime, (prtd->pcm_irq_pos));
  617. }
  618. int msm_pcm_mmap(struct snd_pcm_substream *substream,
  619. struct vm_area_struct *vma)
  620. {
  621. struct snd_pcm_runtime *runtime = substream->runtime;
  622. struct msm_audio *prtd = runtime->private_data;
  623. pr_debug("%s\n", __func__);
  624. prtd->mmap_flag = 1;
  625. dma_mmap_coherent(substream->pcm->card->dev, vma,
  626. runtime->dma_area,
  627. runtime->dma_addr,
  628. runtime->dma_bytes);
  629. return 0;
  630. }
  631. int msm_pcm_hw_params(struct snd_pcm_substream *substream,
  632. struct snd_pcm_hw_params *params)
  633. {
  634. struct snd_pcm_runtime *runtime = substream->runtime;
  635. struct msm_audio *prtd = runtime->private_data;
  636. struct snd_dma_buffer *dma_buf = &substream->dma_buffer;
  637. struct audio_buffer *buf;
  638. int dir, ret;
  639. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  640. dir = IN;
  641. else
  642. dir = OUT;
  643. ret = q6asm_audio_client_buf_alloc_contiguous(dir,
  644. prtd->audio_client,
  645. runtime->hw.period_bytes_min,
  646. runtime->hw.periods_max);
  647. if (ret < 0) {
  648. pr_err("Audio Start: Buffer Allocation failed \
  649. rc = %d\n", ret);
  650. return -ENOMEM;
  651. }
  652. buf = prtd->audio_client->port[dir].buf;
  653. pr_debug("%s:buf = %p\n", __func__, buf);
  654. dma_buf->dev.type = SNDRV_DMA_TYPE_DEV;
  655. dma_buf->dev.dev = substream->pcm->card->dev;
  656. dma_buf->private_data = NULL;
  657. dma_buf->area = buf[0].data;
  658. dma_buf->addr = buf[0].phys;
  659. dma_buf->bytes = runtime->hw.buffer_bytes_max;
  660. if (!dma_buf->area)
  661. return -ENOMEM;
  662. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  663. return 0;
  664. }
  665. static struct snd_pcm_ops msm_pcm_ops = {
  666. .open = msm_pcm_open,
  667. .copy = msm_pcm_copy,
  668. .hw_params = msm_pcm_hw_params,
  669. .close = msm_pcm_close,
  670. .ioctl = snd_pcm_lib_ioctl,
  671. .prepare = msm_pcm_prepare,
  672. .trigger = msm_pcm_trigger,
  673. .pointer = msm_pcm_pointer,
  674. .mmap = msm_pcm_mmap,
  675. };
  676. static int msm_pcm_new(struct snd_soc_pcm_runtime *rtd)
  677. {
  678. int ret = 0;
  679. struct snd_card *card = rtd->card->snd_card;
  680. struct snd_pcm *pcm = rtd->pcm;
  681. ret = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_PLAYBACK, 2);
  682. if (ret)
  683. return ret;
  684. ret = snd_pcm_new_stream(pcm, SNDRV_PCM_STREAM_CAPTURE, 1);
  685. if (ret)
  686. return ret;
  687. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &msm_pcm_ops);
  688. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &msm_pcm_ops);
  689. if (!card->dev->coherent_dma_mask)
  690. card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
  691. return ret;
  692. }
  693. struct snd_soc_platform_driver msm_soc_platform = {
  694. .ops = &msm_pcm_ops,
  695. .pcm_new = msm_pcm_new,
  696. };
  697. EXPORT_SYMBOL(msm_soc_platform);
  698. static __devinit int msm_pcm_probe(struct platform_device *pdev)
  699. {
  700. dev_info(&pdev->dev, "%s: dev name %s\n", __func__, dev_name(&pdev->dev));
  701. return snd_soc_register_platform(&pdev->dev,
  702. &msm_soc_platform);
  703. }
  704. static int msm_pcm_remove(struct platform_device *pdev)
  705. {
  706. snd_soc_unregister_platform(&pdev->dev);
  707. return 0;
  708. }
  709. static struct platform_driver msm_pcm_driver = {
  710. .probe = msm_pcm_probe,
  711. .remove = __devexit_p(msm_pcm_remove),
  712. .driver = {
  713. .name = "msm-dsp-audio",
  714. .owner = THIS_MODULE,
  715. },
  716. };
  717. static int __init msm_soc_platform_init(void)
  718. {
  719. return platform_driver_register(&msm_pcm_driver);
  720. }
  721. module_init(msm_soc_platform_init);
  722. static void __exit msm_soc_platform_exit(void)
  723. {
  724. platform_driver_unregister(&msm_pcm_driver);
  725. }
  726. module_exit(msm_soc_platform_exit);
  727. MODULE_DESCRIPTION("PCM module platform driver");
  728. MODULE_LICENSE("GPL v2");