dbdma2.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. * NOTE: all of these drivers can only work with a SINGLE instance
  14. * of a PSC. Multiple independent audio devices are impossible
  15. * with ASoC v1.
  16. */
  17. #include <linux/module.h>
  18. #include <linux/init.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/mach-au1x00/au1000.h>
  27. #include <asm/mach-au1x00/au1xxx_dbdma.h>
  28. #include <asm/mach-au1x00/au1xxx_psc.h>
  29. #include "psc.h"
  30. /*#define PCM_DEBUG*/
  31. #define MSG(x...) printk(KERN_INFO "au1xpsc_pcm: " x)
  32. #ifdef PCM_DEBUG
  33. #define DBG MSG
  34. #else
  35. #define DBG(x...) do {} while (0)
  36. #endif
  37. struct au1xpsc_audio_dmadata {
  38. /* DDMA control data */
  39. unsigned int ddma_id; /* DDMA direction ID for this PSC */
  40. u32 ddma_chan; /* DDMA context */
  41. /* PCM context (for irq handlers) */
  42. struct snd_pcm_substream *substream;
  43. unsigned long curr_period; /* current segment DDMA is working on */
  44. unsigned long q_period; /* queue period(s) */
  45. dma_addr_t dma_area; /* address of queued DMA area */
  46. dma_addr_t dma_area_s; /* start address of DMA area */
  47. unsigned long pos; /* current byte position being played */
  48. unsigned long periods; /* number of SG segments in total */
  49. unsigned long period_bytes; /* size in bytes of one SG segment */
  50. /* runtime data */
  51. int msbits;
  52. };
  53. /* instance data. There can be only one, MacLeod!!!! */
  54. static struct au1xpsc_audio_dmadata *au1xpsc_audio_pcmdma[2];
  55. /*
  56. * These settings are somewhat okay, at least on my machine audio plays
  57. * almost skip-free. Especially the 64kB buffer seems to help a LOT.
  58. */
  59. #define AU1XPSC_PERIOD_MIN_BYTES 1024
  60. #define AU1XPSC_BUFFER_MIN_BYTES 65536
  61. #define AU1XPSC_PCM_FMTS \
  62. (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 | \
  63. SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE | \
  64. SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE | \
  65. SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE | \
  66. SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_U32_BE | \
  67. 0)
  68. /* PCM hardware DMA capabilities - platform specific */
  69. static const struct snd_pcm_hardware au1xpsc_pcm_hardware = {
  70. .info = SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
  71. SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BATCH,
  72. .formats = AU1XPSC_PCM_FMTS,
  73. .period_bytes_min = AU1XPSC_PERIOD_MIN_BYTES,
  74. .period_bytes_max = 4096 * 1024 - 1,
  75. .periods_min = 2,
  76. .periods_max = 4096, /* 2 to as-much-as-you-like */
  77. .buffer_bytes_max = 4096 * 1024 - 1,
  78. .fifo_size = 16, /* fifo entries of AC97/I2S PSC */
  79. };
  80. static void au1x_pcm_queue_tx(struct au1xpsc_audio_dmadata *cd)
  81. {
  82. au1xxx_dbdma_put_source(cd->ddma_chan, cd->dma_area,
  83. cd->period_bytes, DDMA_FLAGS_IE);
  84. /* update next-to-queue period */
  85. ++cd->q_period;
  86. cd->dma_area += cd->period_bytes;
  87. if (cd->q_period >= cd->periods) {
  88. cd->q_period = 0;
  89. cd->dma_area = cd->dma_area_s;
  90. }
  91. }
  92. static void au1x_pcm_queue_rx(struct au1xpsc_audio_dmadata *cd)
  93. {
  94. au1xxx_dbdma_put_dest(cd->ddma_chan, cd->dma_area,
  95. cd->period_bytes, DDMA_FLAGS_IE);
  96. /* update next-to-queue period */
  97. ++cd->q_period;
  98. cd->dma_area += cd->period_bytes;
  99. if (cd->q_period >= cd->periods) {
  100. cd->q_period = 0;
  101. cd->dma_area = cd->dma_area_s;
  102. }
  103. }
  104. static void au1x_pcm_dmatx_cb(int irq, void *dev_id)
  105. {
  106. struct au1xpsc_audio_dmadata *cd = dev_id;
  107. cd->pos += cd->period_bytes;
  108. if (++cd->curr_period >= cd->periods) {
  109. cd->pos = 0;
  110. cd->curr_period = 0;
  111. }
  112. snd_pcm_period_elapsed(cd->substream);
  113. au1x_pcm_queue_tx(cd);
  114. }
  115. static void au1x_pcm_dmarx_cb(int irq, void *dev_id)
  116. {
  117. struct au1xpsc_audio_dmadata *cd = dev_id;
  118. cd->pos += cd->period_bytes;
  119. if (++cd->curr_period >= cd->periods) {
  120. cd->pos = 0;
  121. cd->curr_period = 0;
  122. }
  123. snd_pcm_period_elapsed(cd->substream);
  124. au1x_pcm_queue_rx(cd);
  125. }
  126. static void au1x_pcm_dbdma_free(struct au1xpsc_audio_dmadata *pcd)
  127. {
  128. if (pcd->ddma_chan) {
  129. au1xxx_dbdma_stop(pcd->ddma_chan);
  130. au1xxx_dbdma_reset(pcd->ddma_chan);
  131. au1xxx_dbdma_chan_free(pcd->ddma_chan);
  132. pcd->ddma_chan = 0;
  133. pcd->msbits = 0;
  134. }
  135. }
  136. /* in case of missing DMA ring or changed TX-source / RX-dest bit widths,
  137. * allocate (or reallocate) a 2-descriptor DMA ring with bit depth according
  138. * to ALSA-supplied sample depth. This is due to limitations in the dbdma api
  139. * (cannot adjust source/dest widths of already allocated descriptor ring).
  140. */
  141. static int au1x_pcm_dbdma_realloc(struct au1xpsc_audio_dmadata *pcd,
  142. int stype, int msbits)
  143. {
  144. /* DMA only in 8/16/32 bit widths */
  145. if (msbits == 24)
  146. msbits = 32;
  147. /* check current config: correct bits and descriptors allocated? */
  148. if ((pcd->ddma_chan) && (msbits == pcd->msbits))
  149. goto out; /* all ok! */
  150. au1x_pcm_dbdma_free(pcd);
  151. if (stype == PCM_RX)
  152. pcd->ddma_chan = au1xxx_dbdma_chan_alloc(pcd->ddma_id,
  153. DSCR_CMD0_ALWAYS,
  154. au1x_pcm_dmarx_cb, (void *)pcd);
  155. else
  156. pcd->ddma_chan = au1xxx_dbdma_chan_alloc(DSCR_CMD0_ALWAYS,
  157. pcd->ddma_id,
  158. au1x_pcm_dmatx_cb, (void *)pcd);
  159. if (!pcd->ddma_chan)
  160. return -ENOMEM;
  161. au1xxx_dbdma_set_devwidth(pcd->ddma_chan, msbits);
  162. au1xxx_dbdma_ring_alloc(pcd->ddma_chan, 2);
  163. pcd->msbits = msbits;
  164. au1xxx_dbdma_stop(pcd->ddma_chan);
  165. au1xxx_dbdma_reset(pcd->ddma_chan);
  166. out:
  167. return 0;
  168. }
  169. static int au1xpsc_pcm_hw_params(struct snd_pcm_substream *substream,
  170. struct snd_pcm_hw_params *params)
  171. {
  172. struct snd_pcm_runtime *runtime = substream->runtime;
  173. struct au1xpsc_audio_dmadata *pcd;
  174. int stype, ret;
  175. ret = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
  176. if (ret < 0)
  177. goto out;
  178. stype = SUBSTREAM_TYPE(substream);
  179. pcd = au1xpsc_audio_pcmdma[stype];
  180. DBG("runtime->dma_area = 0x%08lx dma_addr_t = 0x%08lx dma_size = %d "
  181. "runtime->min_align %d\n",
  182. (unsigned long)runtime->dma_area,
  183. (unsigned long)runtime->dma_addr, runtime->dma_bytes,
  184. runtime->min_align);
  185. DBG("bits %d frags %d frag_bytes %d is_rx %d\n", params->msbits,
  186. params_periods(params), params_period_bytes(params), stype);
  187. ret = au1x_pcm_dbdma_realloc(pcd, stype, params->msbits);
  188. if (ret) {
  189. MSG("DDMA channel (re)alloc failed!\n");
  190. goto out;
  191. }
  192. pcd->substream = substream;
  193. pcd->period_bytes = params_period_bytes(params);
  194. pcd->periods = params_periods(params);
  195. pcd->dma_area_s = pcd->dma_area = runtime->dma_addr;
  196. pcd->q_period = 0;
  197. pcd->curr_period = 0;
  198. pcd->pos = 0;
  199. ret = 0;
  200. out:
  201. return ret;
  202. }
  203. static int au1xpsc_pcm_hw_free(struct snd_pcm_substream *substream)
  204. {
  205. snd_pcm_lib_free_pages(substream);
  206. return 0;
  207. }
  208. static int au1xpsc_pcm_prepare(struct snd_pcm_substream *substream)
  209. {
  210. struct au1xpsc_audio_dmadata *pcd =
  211. au1xpsc_audio_pcmdma[SUBSTREAM_TYPE(substream)];
  212. au1xxx_dbdma_reset(pcd->ddma_chan);
  213. if (SUBSTREAM_TYPE(substream) == PCM_RX) {
  214. au1x_pcm_queue_rx(pcd);
  215. au1x_pcm_queue_rx(pcd);
  216. } else {
  217. au1x_pcm_queue_tx(pcd);
  218. au1x_pcm_queue_tx(pcd);
  219. }
  220. return 0;
  221. }
  222. static int au1xpsc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  223. {
  224. u32 c = au1xpsc_audio_pcmdma[SUBSTREAM_TYPE(substream)]->ddma_chan;
  225. switch (cmd) {
  226. case SNDRV_PCM_TRIGGER_START:
  227. case SNDRV_PCM_TRIGGER_RESUME:
  228. au1xxx_dbdma_start(c);
  229. break;
  230. case SNDRV_PCM_TRIGGER_STOP:
  231. case SNDRV_PCM_TRIGGER_SUSPEND:
  232. au1xxx_dbdma_stop(c);
  233. break;
  234. default:
  235. return -EINVAL;
  236. }
  237. return 0;
  238. }
  239. static snd_pcm_uframes_t
  240. au1xpsc_pcm_pointer(struct snd_pcm_substream *substream)
  241. {
  242. return bytes_to_frames(substream->runtime,
  243. au1xpsc_audio_pcmdma[SUBSTREAM_TYPE(substream)]->pos);
  244. }
  245. static int au1xpsc_pcm_open(struct snd_pcm_substream *substream)
  246. {
  247. snd_soc_set_runtime_hwparams(substream, &au1xpsc_pcm_hardware);
  248. return 0;
  249. }
  250. static int au1xpsc_pcm_close(struct snd_pcm_substream *substream)
  251. {
  252. au1x_pcm_dbdma_free(au1xpsc_audio_pcmdma[SUBSTREAM_TYPE(substream)]);
  253. return 0;
  254. }
  255. static struct snd_pcm_ops au1xpsc_pcm_ops = {
  256. .open = au1xpsc_pcm_open,
  257. .close = au1xpsc_pcm_close,
  258. .ioctl = snd_pcm_lib_ioctl,
  259. .hw_params = au1xpsc_pcm_hw_params,
  260. .hw_free = au1xpsc_pcm_hw_free,
  261. .prepare = au1xpsc_pcm_prepare,
  262. .trigger = au1xpsc_pcm_trigger,
  263. .pointer = au1xpsc_pcm_pointer,
  264. };
  265. static void au1xpsc_pcm_free_dma_buffers(struct snd_pcm *pcm)
  266. {
  267. snd_pcm_lib_preallocate_free_for_all(pcm);
  268. }
  269. static int au1xpsc_pcm_new(struct snd_card *card,
  270. struct snd_soc_dai *dai,
  271. struct snd_pcm *pcm)
  272. {
  273. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  274. card->dev, AU1XPSC_BUFFER_MIN_BYTES, (4096 * 1024) - 1);
  275. return 0;
  276. }
  277. static int au1xpsc_pcm_probe(struct platform_device *pdev)
  278. {
  279. if (!au1xpsc_audio_pcmdma[PCM_TX] || !au1xpsc_audio_pcmdma[PCM_RX])
  280. return -ENODEV;
  281. return 0;
  282. }
  283. static int au1xpsc_pcm_remove(struct platform_device *pdev)
  284. {
  285. return 0;
  286. }
  287. /* au1xpsc audio platform */
  288. struct snd_soc_platform au1xpsc_soc_platform = {
  289. .name = "au1xpsc-pcm-dbdma",
  290. .probe = au1xpsc_pcm_probe,
  291. .remove = au1xpsc_pcm_remove,
  292. .pcm_ops = &au1xpsc_pcm_ops,
  293. .pcm_new = au1xpsc_pcm_new,
  294. .pcm_free = au1xpsc_pcm_free_dma_buffers,
  295. };
  296. EXPORT_SYMBOL_GPL(au1xpsc_soc_platform);
  297. static int __devinit au1xpsc_pcm_drvprobe(struct platform_device *pdev)
  298. {
  299. struct resource *r;
  300. int ret;
  301. if (au1xpsc_audio_pcmdma[PCM_TX] || au1xpsc_audio_pcmdma[PCM_RX])
  302. return -EBUSY;
  303. /* TX DMA */
  304. au1xpsc_audio_pcmdma[PCM_TX]
  305. = kzalloc(sizeof(struct au1xpsc_audio_dmadata), GFP_KERNEL);
  306. if (!au1xpsc_audio_pcmdma[PCM_TX])
  307. return -ENOMEM;
  308. r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  309. if (!r) {
  310. ret = -ENODEV;
  311. goto out1;
  312. }
  313. (au1xpsc_audio_pcmdma[PCM_TX])->ddma_id = r->start;
  314. /* RX DMA */
  315. au1xpsc_audio_pcmdma[PCM_RX]
  316. = kzalloc(sizeof(struct au1xpsc_audio_dmadata), GFP_KERNEL);
  317. if (!au1xpsc_audio_pcmdma[PCM_RX])
  318. return -ENOMEM;
  319. r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
  320. if (!r) {
  321. ret = -ENODEV;
  322. goto out2;
  323. }
  324. (au1xpsc_audio_pcmdma[PCM_RX])->ddma_id = r->start;
  325. ret = snd_soc_register_platform(&au1xpsc_soc_platform);
  326. if (!ret)
  327. return ret;
  328. out2:
  329. kfree(au1xpsc_audio_pcmdma[PCM_RX]);
  330. au1xpsc_audio_pcmdma[PCM_RX] = NULL;
  331. out1:
  332. kfree(au1xpsc_audio_pcmdma[PCM_TX]);
  333. au1xpsc_audio_pcmdma[PCM_TX] = NULL;
  334. return ret;
  335. }
  336. static int __devexit au1xpsc_pcm_drvremove(struct platform_device *pdev)
  337. {
  338. int i;
  339. snd_soc_unregister_platform(&au1xpsc_soc_platform);
  340. for (i = 0; i < 2; i++) {
  341. if (au1xpsc_audio_pcmdma[i]) {
  342. au1x_pcm_dbdma_free(au1xpsc_audio_pcmdma[i]);
  343. kfree(au1xpsc_audio_pcmdma[i]);
  344. au1xpsc_audio_pcmdma[i] = NULL;
  345. }
  346. }
  347. return 0;
  348. }
  349. static struct platform_driver au1xpsc_pcm_driver = {
  350. .driver = {
  351. .name = "au1xpsc-pcm",
  352. .owner = THIS_MODULE,
  353. },
  354. .probe = au1xpsc_pcm_drvprobe,
  355. .remove = __devexit_p(au1xpsc_pcm_drvremove),
  356. };
  357. static int __init au1xpsc_audio_dbdma_load(void)
  358. {
  359. au1xpsc_audio_pcmdma[PCM_TX] = NULL;
  360. au1xpsc_audio_pcmdma[PCM_RX] = NULL;
  361. return platform_driver_register(&au1xpsc_pcm_driver);
  362. }
  363. static void __exit au1xpsc_audio_dbdma_unload(void)
  364. {
  365. platform_driver_unregister(&au1xpsc_pcm_driver);
  366. }
  367. module_init(au1xpsc_audio_dbdma_load);
  368. module_exit(au1xpsc_audio_dbdma_unload);
  369. struct platform_device *au1xpsc_pcm_add(struct platform_device *pdev)
  370. {
  371. struct resource *res, *r;
  372. struct platform_device *pd;
  373. int id[2];
  374. int ret;
  375. r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  376. if (!r)
  377. return NULL;
  378. id[0] = r->start;
  379. r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
  380. if (!r)
  381. return NULL;
  382. id[1] = r->start;
  383. res = kzalloc(sizeof(struct resource) * 2, GFP_KERNEL);
  384. if (!res)
  385. return NULL;
  386. res[0].start = res[0].end = id[0];
  387. res[1].start = res[1].end = id[1];
  388. res[0].flags = res[1].flags = IORESOURCE_DMA;
  389. pd = platform_device_alloc("au1xpsc-pcm", -1);
  390. if (!pd)
  391. goto out;
  392. pd->resource = res;
  393. pd->num_resources = 2;
  394. ret = platform_device_add(pd);
  395. if (!ret)
  396. return pd;
  397. platform_device_put(pd);
  398. out:
  399. kfree(res);
  400. return NULL;
  401. }
  402. EXPORT_SYMBOL_GPL(au1xpsc_pcm_add);
  403. void au1xpsc_pcm_destroy(struct platform_device *dmapd)
  404. {
  405. if (dmapd)
  406. platform_device_unregister(dmapd);
  407. }
  408. EXPORT_SYMBOL_GPL(au1xpsc_pcm_destroy);
  409. MODULE_LICENSE("GPL");
  410. MODULE_DESCRIPTION("Au12x0/Au1550 PSC Audio DMA driver");
  411. MODULE_AUTHOR("Manuel Lauss");