s3c24xx-pcm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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. unsigned int limit;
  72. int ret;
  73. pr_debug("Entered %s\n", __func__);
  74. if (s3c_dma_has_circular()) {
  75. limit = (prtd->dma_end - prtd->dma_start) / prtd->dma_period;
  76. } else
  77. limit = prtd->dma_limit;
  78. pr_debug("%s: loaded %d, limit %d\n", __func__, prtd->dma_loaded, limit);
  79. while (prtd->dma_loaded < limit) {
  80. unsigned long len = prtd->dma_period;
  81. pr_debug("dma_loaded: %d\n", prtd->dma_loaded);
  82. if ((pos + len) > prtd->dma_end) {
  83. len = prtd->dma_end - pos;
  84. pr_debug(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. pr_debug("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 && !s3c_dma_has_circular()) {
  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. pr_debug("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. pr_debug("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. printk(KERN_ERR "failed to get dma channel\n");
  143. return ret;
  144. }
  145. /* use the circular buffering if we have it available. */
  146. if (s3c_dma_has_circular())
  147. s3c2410_dma_setflags(prtd->params->channel,
  148. S3C2410_DMAF_CIRCULAR);
  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. pr_debug("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. pr_debug("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,
  190. prtd->params->dma_addr);
  191. } else {
  192. s3c2410_dma_devconfig(prtd->params->channel,
  193. S3C2410_DMASRC_HW,
  194. prtd->params->dma_addr);
  195. }
  196. s3c2410_dma_config(prtd->params->channel,
  197. prtd->params->dma_size);
  198. /* flush the DMA channel */
  199. s3c2410_dma_ctrl(prtd->params->channel, S3C2410_DMAOP_FLUSH);
  200. prtd->dma_loaded = 0;
  201. prtd->dma_pos = prtd->dma_start;
  202. /* enqueue dma buffers */
  203. s3c24xx_pcm_enqueue(substream);
  204. return ret;
  205. }
  206. static int s3c24xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  207. {
  208. struct s3c24xx_runtime_data *prtd = substream->runtime->private_data;
  209. int ret = 0;
  210. pr_debug("Entered %s\n", __func__);
  211. spin_lock(&prtd->lock);
  212. switch (cmd) {
  213. case SNDRV_PCM_TRIGGER_START:
  214. case SNDRV_PCM_TRIGGER_RESUME:
  215. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  216. prtd->state |= ST_RUNNING;
  217. s3c2410_dma_ctrl(prtd->params->channel, S3C2410_DMAOP_START);
  218. break;
  219. case SNDRV_PCM_TRIGGER_STOP:
  220. case SNDRV_PCM_TRIGGER_SUSPEND:
  221. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  222. prtd->state &= ~ST_RUNNING;
  223. s3c2410_dma_ctrl(prtd->params->channel, S3C2410_DMAOP_STOP);
  224. break;
  225. default:
  226. ret = -EINVAL;
  227. break;
  228. }
  229. spin_unlock(&prtd->lock);
  230. return ret;
  231. }
  232. static snd_pcm_uframes_t
  233. s3c24xx_pcm_pointer(struct snd_pcm_substream *substream)
  234. {
  235. struct snd_pcm_runtime *runtime = substream->runtime;
  236. struct s3c24xx_runtime_data *prtd = runtime->private_data;
  237. unsigned long res;
  238. dma_addr_t src, dst;
  239. pr_debug("Entered %s\n", __func__);
  240. spin_lock(&prtd->lock);
  241. s3c2410_dma_getposition(prtd->params->channel, &src, &dst);
  242. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  243. res = dst - prtd->dma_start;
  244. else
  245. res = src - prtd->dma_start;
  246. spin_unlock(&prtd->lock);
  247. pr_debug("Pointer %x %x\n", src, dst);
  248. /* we seem to be getting the odd error from the pcm library due
  249. * to out-of-bounds pointers. this is maybe due to the dma engine
  250. * not having loaded the new values for the channel before being
  251. * callled... (todo - fix )
  252. */
  253. if (res >= snd_pcm_lib_buffer_bytes(substream)) {
  254. if (res == snd_pcm_lib_buffer_bytes(substream))
  255. res = 0;
  256. }
  257. return bytes_to_frames(substream->runtime, res);
  258. }
  259. static int s3c24xx_pcm_open(struct snd_pcm_substream *substream)
  260. {
  261. struct snd_pcm_runtime *runtime = substream->runtime;
  262. struct s3c24xx_runtime_data *prtd;
  263. pr_debug("Entered %s\n", __func__);
  264. snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
  265. snd_soc_set_runtime_hwparams(substream, &s3c24xx_pcm_hardware);
  266. prtd = kzalloc(sizeof(struct s3c24xx_runtime_data), GFP_KERNEL);
  267. if (prtd == NULL)
  268. return -ENOMEM;
  269. spin_lock_init(&prtd->lock);
  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. pr_debug("Entered %s\n", __func__);
  278. if (!prtd)
  279. pr_debug("s3c24xx_pcm_close called with prtd == NULL\n");
  280. kfree(prtd);
  281. return 0;
  282. }
  283. static int s3c24xx_pcm_mmap(struct snd_pcm_substream *substream,
  284. struct vm_area_struct *vma)
  285. {
  286. struct snd_pcm_runtime *runtime = substream->runtime;
  287. pr_debug("Entered %s\n", __func__);
  288. return dma_mmap_writecombine(substream->pcm->card->dev, vma,
  289. runtime->dma_area,
  290. runtime->dma_addr,
  291. runtime->dma_bytes);
  292. }
  293. static struct snd_pcm_ops s3c24xx_pcm_ops = {
  294. .open = s3c24xx_pcm_open,
  295. .close = s3c24xx_pcm_close,
  296. .ioctl = snd_pcm_lib_ioctl,
  297. .hw_params = s3c24xx_pcm_hw_params,
  298. .hw_free = s3c24xx_pcm_hw_free,
  299. .prepare = s3c24xx_pcm_prepare,
  300. .trigger = s3c24xx_pcm_trigger,
  301. .pointer = s3c24xx_pcm_pointer,
  302. .mmap = s3c24xx_pcm_mmap,
  303. };
  304. static int s3c24xx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
  305. {
  306. struct snd_pcm_substream *substream = pcm->streams[stream].substream;
  307. struct snd_dma_buffer *buf = &substream->dma_buffer;
  308. size_t size = s3c24xx_pcm_hardware.buffer_bytes_max;
  309. pr_debug("Entered %s\n", __func__);
  310. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  311. buf->dev.dev = pcm->card->dev;
  312. buf->private_data = NULL;
  313. buf->area = dma_alloc_writecombine(pcm->card->dev, size,
  314. &buf->addr, GFP_KERNEL);
  315. if (!buf->area)
  316. return -ENOMEM;
  317. buf->bytes = size;
  318. return 0;
  319. }
  320. static void s3c24xx_pcm_free_dma_buffers(struct snd_pcm *pcm)
  321. {
  322. struct snd_pcm_substream *substream;
  323. struct snd_dma_buffer *buf;
  324. int stream;
  325. pr_debug("Entered %s\n", __func__);
  326. for (stream = 0; stream < 2; stream++) {
  327. substream = pcm->streams[stream].substream;
  328. if (!substream)
  329. continue;
  330. buf = &substream->dma_buffer;
  331. if (!buf->area)
  332. continue;
  333. dma_free_writecombine(pcm->card->dev, buf->bytes,
  334. buf->area, buf->addr);
  335. buf->area = NULL;
  336. }
  337. }
  338. static u64 s3c24xx_pcm_dmamask = DMA_BIT_MASK(32);
  339. static int s3c24xx_pcm_new(struct snd_card *card,
  340. struct snd_soc_dai *dai, struct snd_pcm *pcm)
  341. {
  342. int ret = 0;
  343. pr_debug("Entered %s\n", __func__);
  344. if (!card->dev->dma_mask)
  345. card->dev->dma_mask = &s3c24xx_pcm_dmamask;
  346. if (!card->dev->coherent_dma_mask)
  347. card->dev->coherent_dma_mask = 0xffffffff;
  348. if (dai->playback.channels_min) {
  349. ret = s3c24xx_pcm_preallocate_dma_buffer(pcm,
  350. SNDRV_PCM_STREAM_PLAYBACK);
  351. if (ret)
  352. goto out;
  353. }
  354. if (dai->capture.channels_min) {
  355. ret = s3c24xx_pcm_preallocate_dma_buffer(pcm,
  356. SNDRV_PCM_STREAM_CAPTURE);
  357. if (ret)
  358. goto out;
  359. }
  360. out:
  361. return ret;
  362. }
  363. struct snd_soc_platform s3c24xx_soc_platform = {
  364. .name = "s3c24xx-audio",
  365. .pcm_ops = &s3c24xx_pcm_ops,
  366. .pcm_new = s3c24xx_pcm_new,
  367. .pcm_free = s3c24xx_pcm_free_dma_buffers,
  368. };
  369. EXPORT_SYMBOL_GPL(s3c24xx_soc_platform);
  370. static int __init s3c24xx_soc_platform_init(void)
  371. {
  372. return snd_soc_register_platform(&s3c24xx_soc_platform);
  373. }
  374. module_init(s3c24xx_soc_platform_init);
  375. static void __exit s3c24xx_soc_platform_exit(void)
  376. {
  377. snd_soc_unregister_platform(&s3c24xx_soc_platform);
  378. }
  379. module_exit(s3c24xx_soc_platform_exit);
  380. MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
  381. MODULE_DESCRIPTION("Samsung S3C24XX PCM DMA module");
  382. MODULE_LICENSE("GPL");