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