s3c24xx-pcm.c 11 KB

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