s3c24xx-pcm.c 11 KB

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