s3c24xx-pcm.c 11 KB

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