dbdma2.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*
  2. * Au12x0/Au1550 PSC ALSA ASoC audio support.
  3. *
  4. * (c) 2007-2008 MSC Vertriebsges.m.b.H.,
  5. * Manuel Lauss <manuel.lauss@gmail.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * DMA glue for Au1x-PSC audio.
  12. *
  13. */
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/slab.h>
  18. #include <linux/dma-mapping.h>
  19. #include <sound/core.h>
  20. #include <sound/pcm.h>
  21. #include <sound/pcm_params.h>
  22. #include <sound/soc.h>
  23. #include <asm/mach-au1x00/au1000.h>
  24. #include <asm/mach-au1x00/au1xxx_dbdma.h>
  25. #include <asm/mach-au1x00/au1xxx_psc.h>
  26. #include "psc.h"
  27. /*#define PCM_DEBUG*/
  28. #define MSG(x...) printk(KERN_INFO "au1xpsc_pcm: " x)
  29. #ifdef PCM_DEBUG
  30. #define DBG MSG
  31. #else
  32. #define DBG(x...) do {} while (0)
  33. #endif
  34. struct au1xpsc_audio_dmadata {
  35. /* DDMA control data */
  36. unsigned int ddma_id; /* DDMA direction ID for this PSC */
  37. u32 ddma_chan; /* DDMA context */
  38. /* PCM context (for irq handlers) */
  39. struct snd_pcm_substream *substream;
  40. unsigned long curr_period; /* current segment DDMA is working on */
  41. unsigned long q_period; /* queue period(s) */
  42. dma_addr_t dma_area; /* address of queued DMA area */
  43. dma_addr_t dma_area_s; /* start address of DMA area */
  44. unsigned long pos; /* current byte position being played */
  45. unsigned long periods; /* number of SG segments in total */
  46. unsigned long period_bytes; /* size in bytes of one SG segment */
  47. /* runtime data */
  48. int msbits;
  49. };
  50. /*
  51. * These settings are somewhat okay, at least on my machine audio plays
  52. * almost skip-free. Especially the 64kB buffer seems to help a LOT.
  53. */
  54. #define AU1XPSC_PERIOD_MIN_BYTES 1024
  55. #define AU1XPSC_BUFFER_MIN_BYTES 65536
  56. #define AU1XPSC_PCM_FMTS \
  57. (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 | \
  58. SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE | \
  59. SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE | \
  60. SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE | \
  61. SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE | \
  62. 0)
  63. /* PCM hardware DMA capabilities - platform specific */
  64. static const struct snd_pcm_hardware au1xpsc_pcm_hardware = {
  65. .info = SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
  66. SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BATCH,
  67. .formats = AU1XPSC_PCM_FMTS,
  68. .period_bytes_min = AU1XPSC_PERIOD_MIN_BYTES,
  69. .period_bytes_max = 4096 * 1024 - 1,
  70. .periods_min = 2,
  71. .periods_max = 4096, /* 2 to as-much-as-you-like */
  72. .buffer_bytes_max = 4096 * 1024 - 1,
  73. .fifo_size = 16, /* fifo entries of AC97/I2S PSC */
  74. };
  75. static void au1x_pcm_queue_tx(struct au1xpsc_audio_dmadata *cd)
  76. {
  77. au1xxx_dbdma_put_source(cd->ddma_chan, cd->dma_area,
  78. cd->period_bytes, DDMA_FLAGS_IE);
  79. /* update next-to-queue period */
  80. ++cd->q_period;
  81. cd->dma_area += cd->period_bytes;
  82. if (cd->q_period >= cd->periods) {
  83. cd->q_period = 0;
  84. cd->dma_area = cd->dma_area_s;
  85. }
  86. }
  87. static void au1x_pcm_queue_rx(struct au1xpsc_audio_dmadata *cd)
  88. {
  89. au1xxx_dbdma_put_dest(cd->ddma_chan, cd->dma_area,
  90. cd->period_bytes, DDMA_FLAGS_IE);
  91. /* update next-to-queue period */
  92. ++cd->q_period;
  93. cd->dma_area += cd->period_bytes;
  94. if (cd->q_period >= cd->periods) {
  95. cd->q_period = 0;
  96. cd->dma_area = cd->dma_area_s;
  97. }
  98. }
  99. static void au1x_pcm_dmatx_cb(int irq, void *dev_id)
  100. {
  101. struct au1xpsc_audio_dmadata *cd = dev_id;
  102. cd->pos += cd->period_bytes;
  103. if (++cd->curr_period >= cd->periods) {
  104. cd->pos = 0;
  105. cd->curr_period = 0;
  106. }
  107. snd_pcm_period_elapsed(cd->substream);
  108. au1x_pcm_queue_tx(cd);
  109. }
  110. static void au1x_pcm_dmarx_cb(int irq, void *dev_id)
  111. {
  112. struct au1xpsc_audio_dmadata *cd = dev_id;
  113. cd->pos += cd->period_bytes;
  114. if (++cd->curr_period >= cd->periods) {
  115. cd->pos = 0;
  116. cd->curr_period = 0;
  117. }
  118. snd_pcm_period_elapsed(cd->substream);
  119. au1x_pcm_queue_rx(cd);
  120. }
  121. static void au1x_pcm_dbdma_free(struct au1xpsc_audio_dmadata *pcd)
  122. {
  123. if (pcd->ddma_chan) {
  124. au1xxx_dbdma_stop(pcd->ddma_chan);
  125. au1xxx_dbdma_reset(pcd->ddma_chan);
  126. au1xxx_dbdma_chan_free(pcd->ddma_chan);
  127. pcd->ddma_chan = 0;
  128. pcd->msbits = 0;
  129. }
  130. }
  131. /* in case of missing DMA ring or changed TX-source / RX-dest bit widths,
  132. * allocate (or reallocate) a 2-descriptor DMA ring with bit depth according
  133. * to ALSA-supplied sample depth. This is due to limitations in the dbdma api
  134. * (cannot adjust source/dest widths of already allocated descriptor ring).
  135. */
  136. static int au1x_pcm_dbdma_realloc(struct au1xpsc_audio_dmadata *pcd,
  137. int stype, int msbits)
  138. {
  139. /* DMA only in 8/16/32 bit widths */
  140. if (msbits == 24)
  141. msbits = 32;
  142. /* check current config: correct bits and descriptors allocated? */
  143. if ((pcd->ddma_chan) && (msbits == pcd->msbits))
  144. goto out; /* all ok! */
  145. au1x_pcm_dbdma_free(pcd);
  146. if (stype == SNDRV_PCM_STREAM_CAPTURE)
  147. pcd->ddma_chan = au1xxx_dbdma_chan_alloc(pcd->ddma_id,
  148. DSCR_CMD0_ALWAYS,
  149. au1x_pcm_dmarx_cb, (void *)pcd);
  150. else
  151. pcd->ddma_chan = au1xxx_dbdma_chan_alloc(DSCR_CMD0_ALWAYS,
  152. pcd->ddma_id,
  153. au1x_pcm_dmatx_cb, (void *)pcd);
  154. if (!pcd->ddma_chan)
  155. return -ENOMEM;
  156. au1xxx_dbdma_set_devwidth(pcd->ddma_chan, msbits);
  157. au1xxx_dbdma_ring_alloc(pcd->ddma_chan, 2);
  158. pcd->msbits = msbits;
  159. au1xxx_dbdma_stop(pcd->ddma_chan);
  160. au1xxx_dbdma_reset(pcd->ddma_chan);
  161. out:
  162. return 0;
  163. }
  164. static inline struct au1xpsc_audio_dmadata *to_dmadata(struct snd_pcm_substream *ss)
  165. {
  166. struct snd_soc_pcm_runtime *rtd = ss->private_data;
  167. struct au1xpsc_audio_dmadata *pcd =
  168. snd_soc_platform_get_drvdata(rtd->platform);
  169. return &pcd[ss->stream];
  170. }
  171. static int au1xpsc_pcm_hw_params(struct snd_pcm_substream *substream,
  172. struct snd_pcm_hw_params *params)
  173. {
  174. struct snd_pcm_runtime *runtime = substream->runtime;
  175. struct au1xpsc_audio_dmadata *pcd;
  176. int stype, ret;
  177. ret = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
  178. if (ret < 0)
  179. goto out;
  180. stype = substream->stream;
  181. pcd = to_dmadata(substream);
  182. DBG("runtime->dma_area = 0x%08lx dma_addr_t = 0x%08lx dma_size = %d "
  183. "runtime->min_align %d\n",
  184. (unsigned long)runtime->dma_area,
  185. (unsigned long)runtime->dma_addr, runtime->dma_bytes,
  186. runtime->min_align);
  187. DBG("bits %d frags %d frag_bytes %d is_rx %d\n", params->msbits,
  188. params_periods(params), params_period_bytes(params), stype);
  189. ret = au1x_pcm_dbdma_realloc(pcd, stype, params->msbits);
  190. if (ret) {
  191. MSG("DDMA channel (re)alloc failed!\n");
  192. goto out;
  193. }
  194. pcd->substream = substream;
  195. pcd->period_bytes = params_period_bytes(params);
  196. pcd->periods = params_periods(params);
  197. pcd->dma_area_s = pcd->dma_area = runtime->dma_addr;
  198. pcd->q_period = 0;
  199. pcd->curr_period = 0;
  200. pcd->pos = 0;
  201. ret = 0;
  202. out:
  203. return ret;
  204. }
  205. static int au1xpsc_pcm_hw_free(struct snd_pcm_substream *substream)
  206. {
  207. snd_pcm_lib_free_pages(substream);
  208. return 0;
  209. }
  210. static int au1xpsc_pcm_prepare(struct snd_pcm_substream *substream)
  211. {
  212. struct au1xpsc_audio_dmadata *pcd = to_dmadata(substream);
  213. au1xxx_dbdma_reset(pcd->ddma_chan);
  214. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  215. au1x_pcm_queue_rx(pcd);
  216. au1x_pcm_queue_rx(pcd);
  217. } else {
  218. au1x_pcm_queue_tx(pcd);
  219. au1x_pcm_queue_tx(pcd);
  220. }
  221. return 0;
  222. }
  223. static int au1xpsc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  224. {
  225. u32 c = to_dmadata(substream)->ddma_chan;
  226. switch (cmd) {
  227. case SNDRV_PCM_TRIGGER_START:
  228. case SNDRV_PCM_TRIGGER_RESUME:
  229. au1xxx_dbdma_start(c);
  230. break;
  231. case SNDRV_PCM_TRIGGER_STOP:
  232. case SNDRV_PCM_TRIGGER_SUSPEND:
  233. au1xxx_dbdma_stop(c);
  234. break;
  235. default:
  236. return -EINVAL;
  237. }
  238. return 0;
  239. }
  240. static snd_pcm_uframes_t
  241. au1xpsc_pcm_pointer(struct snd_pcm_substream *substream)
  242. {
  243. return bytes_to_frames(substream->runtime, to_dmadata(substream)->pos);
  244. }
  245. static int au1xpsc_pcm_open(struct snd_pcm_substream *substream)
  246. {
  247. struct au1xpsc_audio_dmadata *pcd = to_dmadata(substream);
  248. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  249. int stype = substream->stream, *dmaids;
  250. dmaids = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  251. if (!dmaids)
  252. return -ENODEV; /* whoa, has ordering changed? */
  253. pcd->ddma_id = dmaids[stype];
  254. snd_soc_set_runtime_hwparams(substream, &au1xpsc_pcm_hardware);
  255. return 0;
  256. }
  257. static int au1xpsc_pcm_close(struct snd_pcm_substream *substream)
  258. {
  259. au1x_pcm_dbdma_free(to_dmadata(substream));
  260. return 0;
  261. }
  262. static struct snd_pcm_ops au1xpsc_pcm_ops = {
  263. .open = au1xpsc_pcm_open,
  264. .close = au1xpsc_pcm_close,
  265. .ioctl = snd_pcm_lib_ioctl,
  266. .hw_params = au1xpsc_pcm_hw_params,
  267. .hw_free = au1xpsc_pcm_hw_free,
  268. .prepare = au1xpsc_pcm_prepare,
  269. .trigger = au1xpsc_pcm_trigger,
  270. .pointer = au1xpsc_pcm_pointer,
  271. };
  272. static void au1xpsc_pcm_free_dma_buffers(struct snd_pcm *pcm)
  273. {
  274. snd_pcm_lib_preallocate_free_for_all(pcm);
  275. }
  276. static int au1xpsc_pcm_new(struct snd_soc_pcm_runtime *rtd)
  277. {
  278. struct snd_card *card = rtd->card->snd_card;
  279. struct snd_pcm *pcm = rtd->pcm;
  280. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  281. card->dev, AU1XPSC_BUFFER_MIN_BYTES, (4096 * 1024) - 1);
  282. return 0;
  283. }
  284. /* au1xpsc audio platform */
  285. static struct snd_soc_platform_driver au1xpsc_soc_platform = {
  286. .ops = &au1xpsc_pcm_ops,
  287. .pcm_new = au1xpsc_pcm_new,
  288. .pcm_free = au1xpsc_pcm_free_dma_buffers,
  289. };
  290. static int __devinit au1xpsc_pcm_drvprobe(struct platform_device *pdev)
  291. {
  292. struct au1xpsc_audio_dmadata *dmadata;
  293. dmadata = devm_kzalloc(&pdev->dev,
  294. 2 * sizeof(struct au1xpsc_audio_dmadata),
  295. GFP_KERNEL);
  296. if (!dmadata)
  297. return -ENOMEM;
  298. platform_set_drvdata(pdev, dmadata);
  299. return snd_soc_register_platform(&pdev->dev, &au1xpsc_soc_platform);
  300. }
  301. static int __devexit au1xpsc_pcm_drvremove(struct platform_device *pdev)
  302. {
  303. snd_soc_unregister_platform(&pdev->dev);
  304. return 0;
  305. }
  306. static struct platform_driver au1xpsc_pcm_driver = {
  307. .driver = {
  308. .name = "au1xpsc-pcm",
  309. .owner = THIS_MODULE,
  310. },
  311. .probe = au1xpsc_pcm_drvprobe,
  312. .remove = __devexit_p(au1xpsc_pcm_drvremove),
  313. };
  314. module_platform_driver(au1xpsc_pcm_driver);
  315. MODULE_LICENSE("GPL");
  316. MODULE_DESCRIPTION("Au12x0/Au1550 PSC Audio DMA driver");
  317. MODULE_AUTHOR("Manuel Lauss");