s3c24xx-pcm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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,
  91. substream, pos, len);
  92. if (ret == 0) {
  93. prtd->dma_loaded++;
  94. pos += prtd->dma_period;
  95. if (pos >= prtd->dma_end)
  96. pos = prtd->dma_start;
  97. } else
  98. break;
  99. }
  100. prtd->dma_pos = pos;
  101. }
  102. static void s3c24xx_audio_buffdone(struct s3c2410_dma_chan *channel,
  103. void *dev_id, int size,
  104. enum s3c2410_dma_buffresult result)
  105. {
  106. struct snd_pcm_substream *substream = dev_id;
  107. struct s3c24xx_runtime_data *prtd;
  108. DBG("Entered %s\n", __FUNCTION__);
  109. if (result == S3C2410_RES_ABORT || result == S3C2410_RES_ERR)
  110. return;
  111. prtd = substream->runtime->private_data;
  112. if (substream)
  113. snd_pcm_period_elapsed(substream);
  114. spin_lock(&prtd->lock);
  115. if (prtd->state & ST_RUNNING) {
  116. prtd->dma_loaded--;
  117. s3c24xx_pcm_enqueue(substream);
  118. }
  119. spin_unlock(&prtd->lock);
  120. }
  121. static int s3c24xx_pcm_hw_params(struct snd_pcm_substream *substream,
  122. struct snd_pcm_hw_params *params)
  123. {
  124. struct snd_pcm_runtime *runtime = substream->runtime;
  125. struct s3c24xx_runtime_data *prtd = runtime->private_data;
  126. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  127. struct s3c24xx_pcm_dma_params *dma = rtd->dai->cpu_dai->dma_data;
  128. unsigned long totbytes = params_buffer_bytes(params);
  129. int ret=0;
  130. DBG("Entered %s\n", __FUNCTION__);
  131. /* return if this is a bufferless transfer e.g.
  132. * codec <--> BT codec or GSM modem -- lg FIXME */
  133. if (!dma)
  134. return 0;
  135. /* this may get called several times by oss emulation
  136. * with different params -HW */
  137. if (prtd->params == NULL) {
  138. /* prepare DMA */
  139. prtd->params = dma;
  140. DBG("params %p, client %p, channel %d\n", prtd->params,
  141. prtd->params->client, prtd->params->channel);
  142. ret = s3c2410_dma_request(prtd->params->channel,
  143. prtd->params->client, NULL);
  144. if (ret) {
  145. DBG(KERN_ERR "failed to get dma channel\n");
  146. return ret;
  147. }
  148. }
  149. /* channel needs configuring for mem=>device, increment memory addr,
  150. * sync to pclk, half-word transfers to the IIS-FIFO. */
  151. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  152. s3c2410_dma_devconfig(prtd->params->channel,
  153. S3C2410_DMASRC_MEM, S3C2410_DISRCC_INC |
  154. S3C2410_DISRCC_APB, prtd->params->dma_addr);
  155. s3c2410_dma_config(prtd->params->channel,
  156. prtd->params->dma_size,
  157. S3C2410_DCON_SYNC_PCLK |
  158. S3C2410_DCON_HANDSHAKE);
  159. } else {
  160. s3c2410_dma_config(prtd->params->channel,
  161. prtd->params->dma_size,
  162. S3C2410_DCON_HANDSHAKE |
  163. S3C2410_DCON_SYNC_PCLK);
  164. s3c2410_dma_devconfig(prtd->params->channel,
  165. S3C2410_DMASRC_HW, 0x3,
  166. prtd->params->dma_addr);
  167. }
  168. s3c2410_dma_set_buffdone_fn(prtd->params->channel,
  169. s3c24xx_audio_buffdone);
  170. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  171. runtime->dma_bytes = totbytes;
  172. spin_lock_irq(&prtd->lock);
  173. prtd->dma_loaded = 0;
  174. prtd->dma_limit = runtime->hw.periods_min;
  175. prtd->dma_period = params_period_bytes(params);
  176. prtd->dma_start = runtime->dma_addr;
  177. prtd->dma_pos = prtd->dma_start;
  178. prtd->dma_end = prtd->dma_start + totbytes;
  179. spin_unlock_irq(&prtd->lock);
  180. return 0;
  181. }
  182. static int s3c24xx_pcm_hw_free(struct snd_pcm_substream *substream)
  183. {
  184. struct s3c24xx_runtime_data *prtd = substream->runtime->private_data;
  185. DBG("Entered %s\n", __FUNCTION__);
  186. /* TODO - do we need to ensure DMA flushed */
  187. snd_pcm_set_runtime_buffer(substream, NULL);
  188. if (prtd->params) {
  189. s3c2410_dma_free(prtd->params->channel, prtd->params->client);
  190. prtd->params = NULL;
  191. }
  192. return 0;
  193. }
  194. static int s3c24xx_pcm_prepare(struct snd_pcm_substream *substream)
  195. {
  196. struct s3c24xx_runtime_data *prtd = substream->runtime->private_data;
  197. int ret = 0;
  198. DBG("Entered %s\n", __FUNCTION__);
  199. /* return if this is a bufferless transfer e.g.
  200. * codec <--> BT codec or GSM modem -- lg FIXME */
  201. if (!prtd->params)
  202. return 0;
  203. /* flush the DMA channel */
  204. s3c2410_dma_ctrl(prtd->params->channel, S3C2410_DMAOP_FLUSH);
  205. prtd->dma_loaded = 0;
  206. prtd->dma_pos = prtd->dma_start;
  207. /* enqueue dma buffers */
  208. s3c24xx_pcm_enqueue(substream);
  209. return ret;
  210. }
  211. static int s3c24xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  212. {
  213. struct s3c24xx_runtime_data *prtd = substream->runtime->private_data;
  214. int ret = 0;
  215. DBG("Entered %s\n", __FUNCTION__);
  216. spin_lock(&prtd->lock);
  217. switch (cmd) {
  218. case SNDRV_PCM_TRIGGER_START:
  219. case SNDRV_PCM_TRIGGER_RESUME:
  220. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  221. prtd->state |= ST_RUNNING;
  222. s3c2410_dma_ctrl(prtd->params->channel, S3C2410_DMAOP_START);
  223. s3c2410_dma_ctrl(prtd->params->channel, S3C2410_DMAOP_STARTED);
  224. break;
  225. case SNDRV_PCM_TRIGGER_STOP:
  226. case SNDRV_PCM_TRIGGER_SUSPEND:
  227. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  228. prtd->state &= ~ST_RUNNING;
  229. s3c2410_dma_ctrl(prtd->params->channel, S3C2410_DMAOP_STOP);
  230. break;
  231. default:
  232. ret = -EINVAL;
  233. break;
  234. }
  235. spin_unlock(&prtd->lock);
  236. return ret;
  237. }
  238. static snd_pcm_uframes_t
  239. s3c24xx_pcm_pointer(struct snd_pcm_substream *substream)
  240. {
  241. struct snd_pcm_runtime *runtime = substream->runtime;
  242. struct s3c24xx_runtime_data *prtd = runtime->private_data;
  243. unsigned long res;
  244. dma_addr_t src, dst;
  245. DBG("Entered %s\n", __FUNCTION__);
  246. spin_lock(&prtd->lock);
  247. s3c2410_dma_getposition(prtd->params->channel, &src, &dst);
  248. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  249. res = dst - prtd->dma_start;
  250. else
  251. res = src - prtd->dma_start;
  252. spin_unlock(&prtd->lock);
  253. DBG("Pointer %x %x\n",src,dst);
  254. /* we seem to be getting the odd error from the pcm library due
  255. * to out-of-bounds pointers. this is maybe due to the dma engine
  256. * not having loaded the new values for the channel before being
  257. * callled... (todo - fix )
  258. */
  259. if (res >= snd_pcm_lib_buffer_bytes(substream)) {
  260. if (res == snd_pcm_lib_buffer_bytes(substream))
  261. res = 0;
  262. }
  263. return bytes_to_frames(substream->runtime, res);
  264. }
  265. static int s3c24xx_pcm_open(struct snd_pcm_substream *substream)
  266. {
  267. struct snd_pcm_runtime *runtime = substream->runtime;
  268. struct s3c24xx_runtime_data *prtd;
  269. DBG("Entered %s\n", __FUNCTION__);
  270. snd_soc_set_runtime_hwparams(substream, &s3c24xx_pcm_hardware);
  271. prtd = kzalloc(sizeof(struct s3c24xx_runtime_data), GFP_KERNEL);
  272. if (prtd == NULL)
  273. return -ENOMEM;
  274. spin_lock_init(&prtd->lock);
  275. runtime->private_data = prtd;
  276. return 0;
  277. }
  278. static int s3c24xx_pcm_close(struct snd_pcm_substream *substream)
  279. {
  280. struct snd_pcm_runtime *runtime = substream->runtime;
  281. struct s3c24xx_runtime_data *prtd = runtime->private_data;
  282. DBG("Entered %s\n", __FUNCTION__);
  283. if (prtd)
  284. kfree(prtd);
  285. else
  286. DBG("s3c24xx_pcm_close called with prtd == NULL\n");
  287. return 0;
  288. }
  289. static int s3c24xx_pcm_mmap(struct snd_pcm_substream *substream,
  290. struct vm_area_struct *vma)
  291. {
  292. struct snd_pcm_runtime *runtime = substream->runtime;
  293. DBG("Entered %s\n", __FUNCTION__);
  294. return dma_mmap_writecombine(substream->pcm->card->dev, vma,
  295. runtime->dma_area,
  296. runtime->dma_addr,
  297. runtime->dma_bytes);
  298. }
  299. static struct snd_pcm_ops s3c24xx_pcm_ops = {
  300. .open = s3c24xx_pcm_open,
  301. .close = s3c24xx_pcm_close,
  302. .ioctl = snd_pcm_lib_ioctl,
  303. .hw_params = s3c24xx_pcm_hw_params,
  304. .hw_free = s3c24xx_pcm_hw_free,
  305. .prepare = s3c24xx_pcm_prepare,
  306. .trigger = s3c24xx_pcm_trigger,
  307. .pointer = s3c24xx_pcm_pointer,
  308. .mmap = s3c24xx_pcm_mmap,
  309. };
  310. static int s3c24xx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
  311. {
  312. struct snd_pcm_substream *substream = pcm->streams[stream].substream;
  313. struct snd_dma_buffer *buf = &substream->dma_buffer;
  314. size_t size = s3c24xx_pcm_hardware.buffer_bytes_max;
  315. DBG("Entered %s\n", __FUNCTION__);
  316. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  317. buf->dev.dev = pcm->card->dev;
  318. buf->private_data = NULL;
  319. buf->area = dma_alloc_writecombine(pcm->card->dev, size,
  320. &buf->addr, GFP_KERNEL);
  321. if (!buf->area)
  322. return -ENOMEM;
  323. buf->bytes = size;
  324. return 0;
  325. }
  326. static void s3c24xx_pcm_free_dma_buffers(struct snd_pcm *pcm)
  327. {
  328. struct snd_pcm_substream *substream;
  329. struct snd_dma_buffer *buf;
  330. int stream;
  331. DBG("Entered %s\n", __FUNCTION__);
  332. for (stream = 0; stream < 2; stream++) {
  333. substream = pcm->streams[stream].substream;
  334. if (!substream)
  335. continue;
  336. buf = &substream->dma_buffer;
  337. if (!buf->area)
  338. continue;
  339. dma_free_writecombine(pcm->card->dev, buf->bytes,
  340. buf->area, buf->addr);
  341. buf->area = NULL;
  342. }
  343. }
  344. static u64 s3c24xx_pcm_dmamask = DMA_32BIT_MASK;
  345. static int s3c24xx_pcm_new(struct snd_card *card,
  346. struct snd_soc_codec_dai *dai, struct snd_pcm *pcm)
  347. {
  348. int ret = 0;
  349. DBG("Entered %s\n", __FUNCTION__);
  350. if (!card->dev->dma_mask)
  351. card->dev->dma_mask = &s3c24xx_pcm_dmamask;
  352. if (!card->dev->coherent_dma_mask)
  353. card->dev->coherent_dma_mask = 0xffffffff;
  354. if (dai->playback.channels_min) {
  355. ret = s3c24xx_pcm_preallocate_dma_buffer(pcm,
  356. SNDRV_PCM_STREAM_PLAYBACK);
  357. if (ret)
  358. goto out;
  359. }
  360. if (dai->capture.channels_min) {
  361. ret = s3c24xx_pcm_preallocate_dma_buffer(pcm,
  362. SNDRV_PCM_STREAM_CAPTURE);
  363. if (ret)
  364. goto out;
  365. }
  366. out:
  367. return ret;
  368. }
  369. struct snd_soc_platform s3c24xx_soc_platform = {
  370. .name = "s3c24xx-audio",
  371. .pcm_ops = &s3c24xx_pcm_ops,
  372. .pcm_new = s3c24xx_pcm_new,
  373. .pcm_free = s3c24xx_pcm_free_dma_buffers,
  374. };
  375. EXPORT_SYMBOL_GPL(s3c24xx_soc_platform);
  376. MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
  377. MODULE_DESCRIPTION("Samsung S3C24XX PCM DMA module");
  378. MODULE_LICENSE("GPL");