s3c24xx-pcm.c 11 KB

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