s3c24xx-pcm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /*
  2. * s3c24xx-pcm.c -- ALSA Soc Audio Layer
  3. *
  4. * (c) 2006 Wolfson Microelectronics PLC.
  5. * Graeme Gregory graeme.gregory@wolfsonmicro.com or linux@wolfsonmicro.com
  6. *
  7. * Copyright 2004-2005 Simtec Electronics
  8. * http://armlinux.simtec.co.uk/
  9. * Ben Dooks <ben@simtec.co.uk>
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License as published by the
  13. * Free Software Foundation; either version 2 of the License, or (at your
  14. * option) any later version.
  15. */
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/io.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/slab.h>
  21. #include <linux/dma-mapping.h>
  22. #include <sound/core.h>
  23. #include <sound/pcm.h>
  24. #include <sound/pcm_params.h>
  25. #include <sound/soc.h>
  26. #include <asm/dma.h>
  27. #include <mach/hardware.h>
  28. #include <mach/dma.h>
  29. #include <plat/audio.h>
  30. #include "s3c24xx-pcm.h"
  31. static const struct snd_pcm_hardware s3c24xx_pcm_hardware = {
  32. .info = SNDRV_PCM_INFO_INTERLEAVED |
  33. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  34. SNDRV_PCM_INFO_MMAP |
  35. SNDRV_PCM_INFO_MMAP_VALID |
  36. SNDRV_PCM_INFO_PAUSE |
  37. SNDRV_PCM_INFO_RESUME,
  38. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  39. SNDRV_PCM_FMTBIT_U16_LE |
  40. SNDRV_PCM_FMTBIT_U8 |
  41. SNDRV_PCM_FMTBIT_S8,
  42. .channels_min = 2,
  43. .channels_max = 2,
  44. .buffer_bytes_max = 128*1024,
  45. .period_bytes_min = PAGE_SIZE,
  46. .period_bytes_max = PAGE_SIZE*2,
  47. .periods_min = 2,
  48. .periods_max = 128,
  49. .fifo_size = 32,
  50. };
  51. struct s3c24xx_runtime_data {
  52. spinlock_t lock;
  53. int state;
  54. unsigned int dma_loaded;
  55. unsigned int dma_limit;
  56. unsigned int dma_period;
  57. dma_addr_t dma_start;
  58. dma_addr_t dma_pos;
  59. dma_addr_t dma_end;
  60. struct s3c24xx_pcm_dma_params *params;
  61. };
  62. /* s3c24xx_pcm_enqueue
  63. *
  64. * place a dma buffer onto the queue for the dma system
  65. * to handle.
  66. */
  67. static void s3c24xx_pcm_enqueue(struct snd_pcm_substream *substream)
  68. {
  69. struct s3c24xx_runtime_data *prtd = substream->runtime->private_data;
  70. dma_addr_t pos = prtd->dma_pos;
  71. int ret;
  72. pr_debug("Entered %s\n", __func__);
  73. while (prtd->dma_loaded < prtd->dma_limit) {
  74. unsigned long len = prtd->dma_period;
  75. pr_debug("dma_loaded: %d\n", prtd->dma_loaded);
  76. if ((pos + len) > prtd->dma_end) {
  77. len = prtd->dma_end - pos;
  78. pr_debug(KERN_DEBUG "%s: corrected dma len %ld\n",
  79. __func__, len);
  80. }
  81. ret = s3c2410_dma_enqueue(prtd->params->channel,
  82. substream, pos, len);
  83. if (ret == 0) {
  84. prtd->dma_loaded++;
  85. pos += prtd->dma_period;
  86. if (pos >= prtd->dma_end)
  87. pos = prtd->dma_start;
  88. } else
  89. break;
  90. }
  91. prtd->dma_pos = pos;
  92. }
  93. static void s3c24xx_audio_buffdone(struct s3c2410_dma_chan *channel,
  94. void *dev_id, int size,
  95. enum s3c2410_dma_buffresult result)
  96. {
  97. struct snd_pcm_substream *substream = dev_id;
  98. struct s3c24xx_runtime_data *prtd;
  99. pr_debug("Entered %s\n", __func__);
  100. if (result == S3C2410_RES_ABORT || result == S3C2410_RES_ERR)
  101. return;
  102. prtd = substream->runtime->private_data;
  103. if (substream)
  104. snd_pcm_period_elapsed(substream);
  105. spin_lock(&prtd->lock);
  106. if (prtd->state & ST_RUNNING) {
  107. prtd->dma_loaded--;
  108. s3c24xx_pcm_enqueue(substream);
  109. }
  110. spin_unlock(&prtd->lock);
  111. }
  112. static int s3c24xx_pcm_hw_params(struct snd_pcm_substream *substream,
  113. struct snd_pcm_hw_params *params)
  114. {
  115. struct snd_pcm_runtime *runtime = substream->runtime;
  116. struct s3c24xx_runtime_data *prtd = runtime->private_data;
  117. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  118. struct s3c24xx_pcm_dma_params *dma = rtd->dai->cpu_dai->dma_data;
  119. unsigned long totbytes = params_buffer_bytes(params);
  120. int ret = 0;
  121. pr_debug("Entered %s\n", __func__);
  122. /* return if this is a bufferless transfer e.g.
  123. * codec <--> BT codec or GSM modem -- lg FIXME */
  124. if (!dma)
  125. return 0;
  126. /* this may get called several times by oss emulation
  127. * with different params -HW */
  128. if (prtd->params == NULL) {
  129. /* prepare DMA */
  130. prtd->params = dma;
  131. pr_debug("params %p, client %p, channel %d\n", prtd->params,
  132. prtd->params->client, prtd->params->channel);
  133. ret = s3c2410_dma_request(prtd->params->channel,
  134. prtd->params->client, NULL);
  135. if (ret < 0) {
  136. printk(KERN_ERR "failed to get dma channel\n");
  137. return ret;
  138. }
  139. }
  140. s3c2410_dma_set_buffdone_fn(prtd->params->channel,
  141. s3c24xx_audio_buffdone);
  142. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  143. runtime->dma_bytes = totbytes;
  144. spin_lock_irq(&prtd->lock);
  145. prtd->dma_loaded = 0;
  146. prtd->dma_limit = runtime->hw.periods_min;
  147. prtd->dma_period = params_period_bytes(params);
  148. prtd->dma_start = runtime->dma_addr;
  149. prtd->dma_pos = prtd->dma_start;
  150. prtd->dma_end = prtd->dma_start + totbytes;
  151. spin_unlock_irq(&prtd->lock);
  152. return 0;
  153. }
  154. static int s3c24xx_pcm_hw_free(struct snd_pcm_substream *substream)
  155. {
  156. struct s3c24xx_runtime_data *prtd = substream->runtime->private_data;
  157. pr_debug("Entered %s\n", __func__);
  158. /* TODO - do we need to ensure DMA flushed */
  159. snd_pcm_set_runtime_buffer(substream, NULL);
  160. if (prtd->params) {
  161. s3c2410_dma_free(prtd->params->channel, prtd->params->client);
  162. prtd->params = NULL;
  163. }
  164. return 0;
  165. }
  166. static int s3c24xx_pcm_prepare(struct snd_pcm_substream *substream)
  167. {
  168. struct s3c24xx_runtime_data *prtd = substream->runtime->private_data;
  169. int ret = 0;
  170. pr_debug("Entered %s\n", __func__);
  171. /* return if this is a bufferless transfer e.g.
  172. * codec <--> BT codec or GSM modem -- lg FIXME */
  173. if (!prtd->params)
  174. return 0;
  175. /* channel needs configuring for mem=>device, increment memory addr,
  176. * sync to pclk, half-word transfers to the IIS-FIFO. */
  177. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  178. s3c2410_dma_devconfig(prtd->params->channel,
  179. S3C2410_DMASRC_MEM, S3C2410_DISRCC_INC |
  180. S3C2410_DISRCC_APB, prtd->params->dma_addr);
  181. s3c2410_dma_config(prtd->params->channel,
  182. prtd->params->dma_size,
  183. S3C2410_DCON_SYNC_PCLK |
  184. S3C2410_DCON_HANDSHAKE);
  185. } else {
  186. s3c2410_dma_config(prtd->params->channel,
  187. prtd->params->dma_size,
  188. S3C2410_DCON_HANDSHAKE |
  189. S3C2410_DCON_SYNC_PCLK);
  190. s3c2410_dma_devconfig(prtd->params->channel,
  191. S3C2410_DMASRC_HW, 0x3,
  192. prtd->params->dma_addr);
  193. }
  194. /* flush the DMA channel */
  195. s3c2410_dma_ctrl(prtd->params->channel, S3C2410_DMAOP_FLUSH);
  196. prtd->dma_loaded = 0;
  197. prtd->dma_pos = prtd->dma_start;
  198. /* enqueue dma buffers */
  199. s3c24xx_pcm_enqueue(substream);
  200. return ret;
  201. }
  202. static int s3c24xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  203. {
  204. struct s3c24xx_runtime_data *prtd = substream->runtime->private_data;
  205. int ret = 0;
  206. pr_debug("Entered %s\n", __func__);
  207. spin_lock(&prtd->lock);
  208. switch (cmd) {
  209. case SNDRV_PCM_TRIGGER_START:
  210. case SNDRV_PCM_TRIGGER_RESUME:
  211. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  212. prtd->state |= ST_RUNNING;
  213. s3c2410_dma_ctrl(prtd->params->channel, S3C2410_DMAOP_START);
  214. s3c2410_dma_ctrl(prtd->params->channel, S3C2410_DMAOP_STARTED);
  215. break;
  216. case SNDRV_PCM_TRIGGER_STOP:
  217. case SNDRV_PCM_TRIGGER_SUSPEND:
  218. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  219. prtd->state &= ~ST_RUNNING;
  220. s3c2410_dma_ctrl(prtd->params->channel, S3C2410_DMAOP_STOP);
  221. break;
  222. default:
  223. ret = -EINVAL;
  224. break;
  225. }
  226. spin_unlock(&prtd->lock);
  227. return ret;
  228. }
  229. static snd_pcm_uframes_t
  230. s3c24xx_pcm_pointer(struct snd_pcm_substream *substream)
  231. {
  232. struct snd_pcm_runtime *runtime = substream->runtime;
  233. struct s3c24xx_runtime_data *prtd = runtime->private_data;
  234. unsigned long res;
  235. dma_addr_t src, dst;
  236. pr_debug("Entered %s\n", __func__);
  237. spin_lock(&prtd->lock);
  238. s3c2410_dma_getposition(prtd->params->channel, &src, &dst);
  239. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  240. res = dst - prtd->dma_start;
  241. else
  242. res = src - prtd->dma_start;
  243. spin_unlock(&prtd->lock);
  244. pr_debug("Pointer %x %x\n", src, dst);
  245. /* we seem to be getting the odd error from the pcm library due
  246. * to out-of-bounds pointers. this is maybe due to the dma engine
  247. * not having loaded the new values for the channel before being
  248. * callled... (todo - fix )
  249. */
  250. if (res >= snd_pcm_lib_buffer_bytes(substream)) {
  251. if (res == snd_pcm_lib_buffer_bytes(substream))
  252. res = 0;
  253. }
  254. return bytes_to_frames(substream->runtime, res);
  255. }
  256. static int s3c24xx_pcm_open(struct snd_pcm_substream *substream)
  257. {
  258. struct snd_pcm_runtime *runtime = substream->runtime;
  259. struct s3c24xx_runtime_data *prtd;
  260. pr_debug("Entered %s\n", __func__);
  261. snd_soc_set_runtime_hwparams(substream, &s3c24xx_pcm_hardware);
  262. prtd = kzalloc(sizeof(struct s3c24xx_runtime_data), GFP_KERNEL);
  263. if (prtd == NULL)
  264. return -ENOMEM;
  265. spin_lock_init(&prtd->lock);
  266. runtime->private_data = prtd;
  267. return 0;
  268. }
  269. static int s3c24xx_pcm_close(struct snd_pcm_substream *substream)
  270. {
  271. struct snd_pcm_runtime *runtime = substream->runtime;
  272. struct s3c24xx_runtime_data *prtd = runtime->private_data;
  273. pr_debug("Entered %s\n", __func__);
  274. if (!prtd)
  275. pr_debug("s3c24xx_pcm_close called with prtd == NULL\n");
  276. kfree(prtd);
  277. return 0;
  278. }
  279. static int s3c24xx_pcm_mmap(struct snd_pcm_substream *substream,
  280. struct vm_area_struct *vma)
  281. {
  282. struct snd_pcm_runtime *runtime = substream->runtime;
  283. pr_debug("Entered %s\n", __func__);
  284. return dma_mmap_writecombine(substream->pcm->card->dev, vma,
  285. runtime->dma_area,
  286. runtime->dma_addr,
  287. runtime->dma_bytes);
  288. }
  289. static struct snd_pcm_ops s3c24xx_pcm_ops = {
  290. .open = s3c24xx_pcm_open,
  291. .close = s3c24xx_pcm_close,
  292. .ioctl = snd_pcm_lib_ioctl,
  293. .hw_params = s3c24xx_pcm_hw_params,
  294. .hw_free = s3c24xx_pcm_hw_free,
  295. .prepare = s3c24xx_pcm_prepare,
  296. .trigger = s3c24xx_pcm_trigger,
  297. .pointer = s3c24xx_pcm_pointer,
  298. .mmap = s3c24xx_pcm_mmap,
  299. };
  300. static int s3c24xx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
  301. {
  302. struct snd_pcm_substream *substream = pcm->streams[stream].substream;
  303. struct snd_dma_buffer *buf = &substream->dma_buffer;
  304. size_t size = s3c24xx_pcm_hardware.buffer_bytes_max;
  305. pr_debug("Entered %s\n", __func__);
  306. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  307. buf->dev.dev = pcm->card->dev;
  308. buf->private_data = NULL;
  309. buf->area = dma_alloc_writecombine(pcm->card->dev, size,
  310. &buf->addr, GFP_KERNEL);
  311. if (!buf->area)
  312. return -ENOMEM;
  313. buf->bytes = size;
  314. return 0;
  315. }
  316. static void s3c24xx_pcm_free_dma_buffers(struct snd_pcm *pcm)
  317. {
  318. struct snd_pcm_substream *substream;
  319. struct snd_dma_buffer *buf;
  320. int stream;
  321. pr_debug("Entered %s\n", __func__);
  322. for (stream = 0; stream < 2; stream++) {
  323. substream = pcm->streams[stream].substream;
  324. if (!substream)
  325. continue;
  326. buf = &substream->dma_buffer;
  327. if (!buf->area)
  328. continue;
  329. dma_free_writecombine(pcm->card->dev, buf->bytes,
  330. buf->area, buf->addr);
  331. buf->area = NULL;
  332. }
  333. }
  334. static u64 s3c24xx_pcm_dmamask = DMA_BIT_MASK(32);
  335. static int s3c24xx_pcm_new(struct snd_card *card,
  336. struct snd_soc_dai *dai, struct snd_pcm *pcm)
  337. {
  338. int ret = 0;
  339. pr_debug("Entered %s\n", __func__);
  340. if (!card->dev->dma_mask)
  341. card->dev->dma_mask = &s3c24xx_pcm_dmamask;
  342. if (!card->dev->coherent_dma_mask)
  343. card->dev->coherent_dma_mask = 0xffffffff;
  344. if (dai->playback.channels_min) {
  345. ret = s3c24xx_pcm_preallocate_dma_buffer(pcm,
  346. SNDRV_PCM_STREAM_PLAYBACK);
  347. if (ret)
  348. goto out;
  349. }
  350. if (dai->capture.channels_min) {
  351. ret = s3c24xx_pcm_preallocate_dma_buffer(pcm,
  352. SNDRV_PCM_STREAM_CAPTURE);
  353. if (ret)
  354. goto out;
  355. }
  356. out:
  357. return ret;
  358. }
  359. struct snd_soc_platform s3c24xx_soc_platform = {
  360. .name = "s3c24xx-audio",
  361. .pcm_ops = &s3c24xx_pcm_ops,
  362. .pcm_new = s3c24xx_pcm_new,
  363. .pcm_free = s3c24xx_pcm_free_dma_buffers,
  364. };
  365. EXPORT_SYMBOL_GPL(s3c24xx_soc_platform);
  366. static int __init s3c24xx_soc_platform_init(void)
  367. {
  368. return snd_soc_register_platform(&s3c24xx_soc_platform);
  369. }
  370. module_init(s3c24xx_soc_platform_init);
  371. static void __exit s3c24xx_soc_platform_exit(void)
  372. {
  373. snd_soc_unregister_platform(&s3c24xx_soc_platform);
  374. }
  375. module_exit(s3c24xx_soc_platform_exit);
  376. MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
  377. MODULE_DESCRIPTION("Samsung S3C24XX PCM DMA module");
  378. MODULE_LICENSE("GPL");