s3c24xx-pcm.c 11 KB

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