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/io.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/slab.h>
  25. #include <linux/dma-mapping.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/hardware.h>
  32. #include <asm/arch/dma.h>
  33. #include <asm/arch/audio.h>
  34. #include "s3c24xx-pcm.h"
  35. #define S3C24XX_PCM_DEBUG 0
  36. #if S3C24XX_PCM_DEBUG
  37. #define DBG(x...) printk(KERN_DEBUG "s3c24xx-pcm: " x)
  38. #else
  39. #define DBG(x...)
  40. #endif
  41. static const struct snd_pcm_hardware s3c24xx_pcm_hardware = {
  42. .info = SNDRV_PCM_INFO_INTERLEAVED |
  43. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  44. SNDRV_PCM_INFO_MMAP |
  45. SNDRV_PCM_INFO_MMAP_VALID |
  46. SNDRV_PCM_INFO_PAUSE |
  47. SNDRV_PCM_INFO_RESUME,
  48. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  49. SNDRV_PCM_FMTBIT_U16_LE |
  50. SNDRV_PCM_FMTBIT_U8 |
  51. SNDRV_PCM_FMTBIT_S8,
  52. .channels_min = 2,
  53. .channels_max = 2,
  54. .buffer_bytes_max = 128*1024,
  55. .period_bytes_min = PAGE_SIZE,
  56. .period_bytes_max = PAGE_SIZE*2,
  57. .periods_min = 2,
  58. .periods_max = 128,
  59. .fifo_size = 32,
  60. };
  61. struct s3c24xx_runtime_data {
  62. spinlock_t lock;
  63. int state;
  64. unsigned int dma_loaded;
  65. unsigned int dma_limit;
  66. unsigned int dma_period;
  67. dma_addr_t dma_start;
  68. dma_addr_t dma_pos;
  69. dma_addr_t dma_end;
  70. struct s3c24xx_pcm_dma_params *params;
  71. };
  72. /* s3c24xx_pcm_enqueue
  73. *
  74. * place a dma buffer onto the queue for the dma system
  75. * to handle.
  76. */
  77. static void s3c24xx_pcm_enqueue(struct snd_pcm_substream *substream)
  78. {
  79. struct s3c24xx_runtime_data *prtd = substream->runtime->private_data;
  80. dma_addr_t pos = prtd->dma_pos;
  81. int ret;
  82. DBG("Entered %s\n", __func__);
  83. while (prtd->dma_loaded < prtd->dma_limit) {
  84. unsigned long len = prtd->dma_period;
  85. DBG("dma_loaded: %d\n", prtd->dma_loaded);
  86. if ((pos + len) > prtd->dma_end) {
  87. len = prtd->dma_end - pos;
  88. DBG(KERN_DEBUG "%s: corrected dma len %ld\n",
  89. __func__, len);
  90. }
  91. ret = s3c2410_dma_enqueue(prtd->params->channel,
  92. substream, pos, len);
  93. if (ret == 0) {
  94. prtd->dma_loaded++;
  95. pos += prtd->dma_period;
  96. if (pos >= prtd->dma_end)
  97. pos = prtd->dma_start;
  98. } else
  99. break;
  100. }
  101. prtd->dma_pos = pos;
  102. }
  103. static void s3c24xx_audio_buffdone(struct s3c2410_dma_chan *channel,
  104. void *dev_id, int size,
  105. enum s3c2410_dma_buffresult result)
  106. {
  107. struct snd_pcm_substream *substream = dev_id;
  108. struct s3c24xx_runtime_data *prtd;
  109. DBG("Entered %s\n", __func__);
  110. if (result == S3C2410_RES_ABORT || result == S3C2410_RES_ERR)
  111. return;
  112. prtd = substream->runtime->private_data;
  113. if (substream)
  114. snd_pcm_period_elapsed(substream);
  115. spin_lock(&prtd->lock);
  116. if (prtd->state & ST_RUNNING) {
  117. prtd->dma_loaded--;
  118. s3c24xx_pcm_enqueue(substream);
  119. }
  120. spin_unlock(&prtd->lock);
  121. }
  122. static int s3c24xx_pcm_hw_params(struct snd_pcm_substream *substream,
  123. struct snd_pcm_hw_params *params)
  124. {
  125. struct snd_pcm_runtime *runtime = substream->runtime;
  126. struct s3c24xx_runtime_data *prtd = runtime->private_data;
  127. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  128. struct s3c24xx_pcm_dma_params *dma = rtd->dai->cpu_dai->dma_data;
  129. unsigned long totbytes = params_buffer_bytes(params);
  130. int ret = 0;
  131. DBG("Entered %s\n", __func__);
  132. /* return if this is a bufferless transfer e.g.
  133. * codec <--> BT codec or GSM modem -- lg FIXME */
  134. if (!dma)
  135. return 0;
  136. /* this may get called several times by oss emulation
  137. * with different params -HW */
  138. if (prtd->params == NULL) {
  139. /* prepare DMA */
  140. prtd->params = dma;
  141. DBG("params %p, client %p, channel %d\n", prtd->params,
  142. prtd->params->client, prtd->params->channel);
  143. ret = s3c2410_dma_request(prtd->params->channel,
  144. prtd->params->client, NULL);
  145. if (ret) {
  146. DBG(KERN_ERR "failed to get dma channel\n");
  147. return ret;
  148. }
  149. }
  150. s3c2410_dma_set_buffdone_fn(prtd->params->channel,
  151. s3c24xx_audio_buffdone);
  152. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  153. runtime->dma_bytes = totbytes;
  154. spin_lock_irq(&prtd->lock);
  155. prtd->dma_loaded = 0;
  156. prtd->dma_limit = runtime->hw.periods_min;
  157. prtd->dma_period = params_period_bytes(params);
  158. prtd->dma_start = runtime->dma_addr;
  159. prtd->dma_pos = prtd->dma_start;
  160. prtd->dma_end = prtd->dma_start + totbytes;
  161. spin_unlock_irq(&prtd->lock);
  162. return 0;
  163. }
  164. static int s3c24xx_pcm_hw_free(struct snd_pcm_substream *substream)
  165. {
  166. struct s3c24xx_runtime_data *prtd = substream->runtime->private_data;
  167. DBG("Entered %s\n", __func__);
  168. /* TODO - do we need to ensure DMA flushed */
  169. snd_pcm_set_runtime_buffer(substream, NULL);
  170. if (prtd->params) {
  171. s3c2410_dma_free(prtd->params->channel, prtd->params->client);
  172. prtd->params = NULL;
  173. }
  174. return 0;
  175. }
  176. static int s3c24xx_pcm_prepare(struct snd_pcm_substream *substream)
  177. {
  178. struct s3c24xx_runtime_data *prtd = substream->runtime->private_data;
  179. int ret = 0;
  180. DBG("Entered %s\n", __func__);
  181. /* return if this is a bufferless transfer e.g.
  182. * codec <--> BT codec or GSM modem -- lg FIXME */
  183. if (!prtd->params)
  184. return 0;
  185. /* channel needs configuring for mem=>device, increment memory addr,
  186. * sync to pclk, half-word transfers to the IIS-FIFO. */
  187. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  188. s3c2410_dma_devconfig(prtd->params->channel,
  189. S3C2410_DMASRC_MEM, S3C2410_DISRCC_INC |
  190. S3C2410_DISRCC_APB, prtd->params->dma_addr);
  191. s3c2410_dma_config(prtd->params->channel,
  192. prtd->params->dma_size,
  193. S3C2410_DCON_SYNC_PCLK |
  194. S3C2410_DCON_HANDSHAKE);
  195. } else {
  196. s3c2410_dma_config(prtd->params->channel,
  197. prtd->params->dma_size,
  198. S3C2410_DCON_HANDSHAKE |
  199. S3C2410_DCON_SYNC_PCLK);
  200. s3c2410_dma_devconfig(prtd->params->channel,
  201. S3C2410_DMASRC_HW, 0x3,
  202. prtd->params->dma_addr);
  203. }
  204. /* flush the DMA channel */
  205. s3c2410_dma_ctrl(prtd->params->channel, S3C2410_DMAOP_FLUSH);
  206. prtd->dma_loaded = 0;
  207. prtd->dma_pos = prtd->dma_start;
  208. /* enqueue dma buffers */
  209. s3c24xx_pcm_enqueue(substream);
  210. return ret;
  211. }
  212. static int s3c24xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  213. {
  214. struct s3c24xx_runtime_data *prtd = substream->runtime->private_data;
  215. int ret = 0;
  216. DBG("Entered %s\n", __func__);
  217. spin_lock(&prtd->lock);
  218. switch (cmd) {
  219. case SNDRV_PCM_TRIGGER_START:
  220. case SNDRV_PCM_TRIGGER_RESUME:
  221. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  222. prtd->state |= ST_RUNNING;
  223. s3c2410_dma_ctrl(prtd->params->channel, S3C2410_DMAOP_START);
  224. s3c2410_dma_ctrl(prtd->params->channel, S3C2410_DMAOP_STARTED);
  225. break;
  226. case SNDRV_PCM_TRIGGER_STOP:
  227. case SNDRV_PCM_TRIGGER_SUSPEND:
  228. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  229. prtd->state &= ~ST_RUNNING;
  230. s3c2410_dma_ctrl(prtd->params->channel, S3C2410_DMAOP_STOP);
  231. break;
  232. default:
  233. ret = -EINVAL;
  234. break;
  235. }
  236. spin_unlock(&prtd->lock);
  237. return ret;
  238. }
  239. static snd_pcm_uframes_t
  240. s3c24xx_pcm_pointer(struct snd_pcm_substream *substream)
  241. {
  242. struct snd_pcm_runtime *runtime = substream->runtime;
  243. struct s3c24xx_runtime_data *prtd = runtime->private_data;
  244. unsigned long res;
  245. dma_addr_t src, dst;
  246. DBG("Entered %s\n", __func__);
  247. spin_lock(&prtd->lock);
  248. s3c2410_dma_getposition(prtd->params->channel, &src, &dst);
  249. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  250. res = dst - prtd->dma_start;
  251. else
  252. res = src - prtd->dma_start;
  253. spin_unlock(&prtd->lock);
  254. DBG("Pointer %x %x\n", src, dst);
  255. /* we seem to be getting the odd error from the pcm library due
  256. * to out-of-bounds pointers. this is maybe due to the dma engine
  257. * not having loaded the new values for the channel before being
  258. * callled... (todo - fix )
  259. */
  260. if (res >= snd_pcm_lib_buffer_bytes(substream)) {
  261. if (res == snd_pcm_lib_buffer_bytes(substream))
  262. res = 0;
  263. }
  264. return bytes_to_frames(substream->runtime, res);
  265. }
  266. static int s3c24xx_pcm_open(struct snd_pcm_substream *substream)
  267. {
  268. struct snd_pcm_runtime *runtime = substream->runtime;
  269. struct s3c24xx_runtime_data *prtd;
  270. DBG("Entered %s\n", __func__);
  271. snd_soc_set_runtime_hwparams(substream, &s3c24xx_pcm_hardware);
  272. prtd = kzalloc(sizeof(struct s3c24xx_runtime_data), GFP_KERNEL);
  273. if (prtd == NULL)
  274. return -ENOMEM;
  275. spin_lock_init(&prtd->lock);
  276. runtime->private_data = prtd;
  277. return 0;
  278. }
  279. static int s3c24xx_pcm_close(struct snd_pcm_substream *substream)
  280. {
  281. struct snd_pcm_runtime *runtime = substream->runtime;
  282. struct s3c24xx_runtime_data *prtd = runtime->private_data;
  283. DBG("Entered %s\n", __func__);
  284. if (!prtd)
  285. DBG("s3c24xx_pcm_close called with prtd == NULL\n");
  286. kfree(prtd);
  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", __func__);
  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", __func__);
  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", __func__);
  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", __func__);
  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");