dbdma2.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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. unsigned long dma_area; /* address of queued DMA area */
  46. unsigned long 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_flags(cd->ddma_chan,
  83. (void *)phys_to_virt(cd->dma_area),
  84. cd->period_bytes, DDMA_FLAGS_IE);
  85. /* update next-to-queue period */
  86. ++cd->q_period;
  87. cd->dma_area += cd->period_bytes;
  88. if (cd->q_period >= cd->periods) {
  89. cd->q_period = 0;
  90. cd->dma_area = cd->dma_area_s;
  91. }
  92. }
  93. static void au1x_pcm_queue_rx(struct au1xpsc_audio_dmadata *cd)
  94. {
  95. au1xxx_dbdma_put_dest_flags(cd->ddma_chan,
  96. (void *)phys_to_virt(cd->dma_area),
  97. cd->period_bytes, DDMA_FLAGS_IE);
  98. /* update next-to-queue period */
  99. ++cd->q_period;
  100. cd->dma_area += cd->period_bytes;
  101. if (cd->q_period >= cd->periods) {
  102. cd->q_period = 0;
  103. cd->dma_area = cd->dma_area_s;
  104. }
  105. }
  106. static void au1x_pcm_dmatx_cb(int irq, void *dev_id)
  107. {
  108. struct au1xpsc_audio_dmadata *cd = dev_id;
  109. cd->pos += cd->period_bytes;
  110. if (++cd->curr_period >= cd->periods) {
  111. cd->pos = 0;
  112. cd->curr_period = 0;
  113. }
  114. snd_pcm_period_elapsed(cd->substream);
  115. au1x_pcm_queue_tx(cd);
  116. }
  117. static void au1x_pcm_dmarx_cb(int irq, void *dev_id)
  118. {
  119. struct au1xpsc_audio_dmadata *cd = dev_id;
  120. cd->pos += cd->period_bytes;
  121. if (++cd->curr_period >= cd->periods) {
  122. cd->pos = 0;
  123. cd->curr_period = 0;
  124. }
  125. snd_pcm_period_elapsed(cd->substream);
  126. au1x_pcm_queue_rx(cd);
  127. }
  128. static void au1x_pcm_dbdma_free(struct au1xpsc_audio_dmadata *pcd)
  129. {
  130. if (pcd->ddma_chan) {
  131. au1xxx_dbdma_stop(pcd->ddma_chan);
  132. au1xxx_dbdma_reset(pcd->ddma_chan);
  133. au1xxx_dbdma_chan_free(pcd->ddma_chan);
  134. pcd->ddma_chan = 0;
  135. pcd->msbits = 0;
  136. }
  137. }
  138. /* in case of missing DMA ring or changed TX-source / RX-dest bit widths,
  139. * allocate (or reallocate) a 2-descriptor DMA ring with bit depth according
  140. * to ALSA-supplied sample depth. This is due to limitations in the dbdma api
  141. * (cannot adjust source/dest widths of already allocated descriptor ring).
  142. */
  143. static int au1x_pcm_dbdma_realloc(struct au1xpsc_audio_dmadata *pcd,
  144. int stype, int msbits)
  145. {
  146. /* DMA only in 8/16/32 bit widths */
  147. if (msbits == 24)
  148. msbits = 32;
  149. /* check current config: correct bits and descriptors allocated? */
  150. if ((pcd->ddma_chan) && (msbits == pcd->msbits))
  151. goto out; /* all ok! */
  152. au1x_pcm_dbdma_free(pcd);
  153. if (stype == PCM_RX)
  154. pcd->ddma_chan = au1xxx_dbdma_chan_alloc(pcd->ddma_id,
  155. DSCR_CMD0_ALWAYS,
  156. au1x_pcm_dmarx_cb, (void *)pcd);
  157. else
  158. pcd->ddma_chan = au1xxx_dbdma_chan_alloc(DSCR_CMD0_ALWAYS,
  159. pcd->ddma_id,
  160. au1x_pcm_dmatx_cb, (void *)pcd);
  161. if (!pcd->ddma_chan)
  162. return -ENOMEM;
  163. au1xxx_dbdma_set_devwidth(pcd->ddma_chan, msbits);
  164. au1xxx_dbdma_ring_alloc(pcd->ddma_chan, 2);
  165. pcd->msbits = msbits;
  166. au1xxx_dbdma_stop(pcd->ddma_chan);
  167. au1xxx_dbdma_reset(pcd->ddma_chan);
  168. out:
  169. return 0;
  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_TYPE(substream);
  181. pcd = au1xpsc_audio_pcmdma[stype];
  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 = (unsigned long)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 =
  213. au1xpsc_audio_pcmdma[SUBSTREAM_TYPE(substream)];
  214. au1xxx_dbdma_reset(pcd->ddma_chan);
  215. if (SUBSTREAM_TYPE(substream) == PCM_RX) {
  216. au1x_pcm_queue_rx(pcd);
  217. au1x_pcm_queue_rx(pcd);
  218. } else {
  219. au1x_pcm_queue_tx(pcd);
  220. au1x_pcm_queue_tx(pcd);
  221. }
  222. return 0;
  223. }
  224. static int au1xpsc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  225. {
  226. u32 c = au1xpsc_audio_pcmdma[SUBSTREAM_TYPE(substream)]->ddma_chan;
  227. switch (cmd) {
  228. case SNDRV_PCM_TRIGGER_START:
  229. case SNDRV_PCM_TRIGGER_RESUME:
  230. au1xxx_dbdma_start(c);
  231. break;
  232. case SNDRV_PCM_TRIGGER_STOP:
  233. case SNDRV_PCM_TRIGGER_SUSPEND:
  234. au1xxx_dbdma_stop(c);
  235. break;
  236. default:
  237. return -EINVAL;
  238. }
  239. return 0;
  240. }
  241. static snd_pcm_uframes_t
  242. au1xpsc_pcm_pointer(struct snd_pcm_substream *substream)
  243. {
  244. return bytes_to_frames(substream->runtime,
  245. au1xpsc_audio_pcmdma[SUBSTREAM_TYPE(substream)]->pos);
  246. }
  247. static int au1xpsc_pcm_open(struct snd_pcm_substream *substream)
  248. {
  249. snd_soc_set_runtime_hwparams(substream, &au1xpsc_pcm_hardware);
  250. return 0;
  251. }
  252. static int au1xpsc_pcm_close(struct snd_pcm_substream *substream)
  253. {
  254. au1x_pcm_dbdma_free(au1xpsc_audio_pcmdma[SUBSTREAM_TYPE(substream)]);
  255. return 0;
  256. }
  257. static struct snd_pcm_ops au1xpsc_pcm_ops = {
  258. .open = au1xpsc_pcm_open,
  259. .close = au1xpsc_pcm_close,
  260. .ioctl = snd_pcm_lib_ioctl,
  261. .hw_params = au1xpsc_pcm_hw_params,
  262. .hw_free = au1xpsc_pcm_hw_free,
  263. .prepare = au1xpsc_pcm_prepare,
  264. .trigger = au1xpsc_pcm_trigger,
  265. .pointer = au1xpsc_pcm_pointer,
  266. };
  267. static void au1xpsc_pcm_free_dma_buffers(struct snd_pcm *pcm)
  268. {
  269. snd_pcm_lib_preallocate_free_for_all(pcm);
  270. }
  271. static int au1xpsc_pcm_new(struct snd_card *card,
  272. struct snd_soc_dai *dai,
  273. struct snd_pcm *pcm)
  274. {
  275. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  276. card->dev, AU1XPSC_BUFFER_MIN_BYTES, (4096 * 1024) - 1);
  277. return 0;
  278. }
  279. static int au1xpsc_pcm_probe(struct platform_device *pdev)
  280. {
  281. if (!au1xpsc_audio_pcmdma[PCM_TX] || !au1xpsc_audio_pcmdma[PCM_RX])
  282. return -ENODEV;
  283. return 0;
  284. }
  285. static int au1xpsc_pcm_remove(struct platform_device *pdev)
  286. {
  287. return 0;
  288. }
  289. /* au1xpsc audio platform */
  290. struct snd_soc_platform au1xpsc_soc_platform = {
  291. .name = "au1xpsc-pcm-dbdma",
  292. .probe = au1xpsc_pcm_probe,
  293. .remove = au1xpsc_pcm_remove,
  294. .pcm_ops = &au1xpsc_pcm_ops,
  295. .pcm_new = au1xpsc_pcm_new,
  296. .pcm_free = au1xpsc_pcm_free_dma_buffers,
  297. };
  298. EXPORT_SYMBOL_GPL(au1xpsc_soc_platform);
  299. static int __devinit au1xpsc_pcm_drvprobe(struct platform_device *pdev)
  300. {
  301. struct resource *r;
  302. int ret;
  303. if (au1xpsc_audio_pcmdma[PCM_TX] || au1xpsc_audio_pcmdma[PCM_RX])
  304. return -EBUSY;
  305. /* TX DMA */
  306. au1xpsc_audio_pcmdma[PCM_TX]
  307. = kzalloc(sizeof(struct au1xpsc_audio_dmadata), GFP_KERNEL);
  308. if (!au1xpsc_audio_pcmdma[PCM_TX])
  309. return -ENOMEM;
  310. r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  311. if (!r) {
  312. ret = -ENODEV;
  313. goto out1;
  314. }
  315. (au1xpsc_audio_pcmdma[PCM_TX])->ddma_id = r->start;
  316. /* RX DMA */
  317. au1xpsc_audio_pcmdma[PCM_RX]
  318. = kzalloc(sizeof(struct au1xpsc_audio_dmadata), GFP_KERNEL);
  319. if (!au1xpsc_audio_pcmdma[PCM_RX])
  320. return -ENOMEM;
  321. r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
  322. if (!r) {
  323. ret = -ENODEV;
  324. goto out2;
  325. }
  326. (au1xpsc_audio_pcmdma[PCM_RX])->ddma_id = r->start;
  327. ret = snd_soc_register_platform(&au1xpsc_soc_platform);
  328. if (!ret)
  329. return ret;
  330. out2:
  331. kfree(au1xpsc_audio_pcmdma[PCM_RX]);
  332. au1xpsc_audio_pcmdma[PCM_RX] = NULL;
  333. out1:
  334. kfree(au1xpsc_audio_pcmdma[PCM_TX]);
  335. au1xpsc_audio_pcmdma[PCM_TX] = NULL;
  336. return ret;
  337. }
  338. static int __devexit au1xpsc_pcm_drvremove(struct platform_device *pdev)
  339. {
  340. int i;
  341. snd_soc_unregister_platform(&au1xpsc_soc_platform);
  342. for (i = 0; i < 2; i++) {
  343. if (au1xpsc_audio_pcmdma[i]) {
  344. au1x_pcm_dbdma_free(au1xpsc_audio_pcmdma[i]);
  345. kfree(au1xpsc_audio_pcmdma[i]);
  346. au1xpsc_audio_pcmdma[i] = NULL;
  347. }
  348. }
  349. return 0;
  350. }
  351. static struct platform_driver au1xpsc_pcm_driver = {
  352. .driver = {
  353. .name = "au1xpsc-pcm",
  354. .owner = THIS_MODULE,
  355. },
  356. .probe = au1xpsc_pcm_drvprobe,
  357. .remove = __devexit_p(au1xpsc_pcm_drvremove),
  358. };
  359. static int __init au1xpsc_audio_dbdma_load(void)
  360. {
  361. au1xpsc_audio_pcmdma[PCM_TX] = NULL;
  362. au1xpsc_audio_pcmdma[PCM_RX] = NULL;
  363. return platform_driver_register(&au1xpsc_pcm_driver);
  364. }
  365. static void __exit au1xpsc_audio_dbdma_unload(void)
  366. {
  367. platform_driver_unregister(&au1xpsc_pcm_driver);
  368. }
  369. module_init(au1xpsc_audio_dbdma_load);
  370. module_exit(au1xpsc_audio_dbdma_unload);
  371. struct platform_device *au1xpsc_pcm_add(struct platform_device *pdev)
  372. {
  373. struct resource *res, *r;
  374. struct platform_device *pd;
  375. int id[2];
  376. int ret;
  377. r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  378. if (!r)
  379. return NULL;
  380. id[0] = r->start;
  381. r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
  382. if (!r)
  383. return NULL;
  384. id[1] = r->start;
  385. res = kzalloc(sizeof(struct resource) * 2, GFP_KERNEL);
  386. if (!res)
  387. return NULL;
  388. res[0].start = res[0].end = id[0];
  389. res[1].start = res[1].end = id[1];
  390. res[0].flags = res[1].flags = IORESOURCE_DMA;
  391. pd = platform_device_alloc("au1xpsc-pcm", -1);
  392. if (!pd)
  393. goto out;
  394. pd->resource = res;
  395. pd->num_resources = 2;
  396. ret = platform_device_add(pd);
  397. if (!ret)
  398. return pd;
  399. platform_device_put(pd);
  400. out:
  401. kfree(res);
  402. return NULL;
  403. }
  404. EXPORT_SYMBOL_GPL(au1xpsc_pcm_add);
  405. void au1xpsc_pcm_destroy(struct platform_device *dmapd)
  406. {
  407. if (dmapd)
  408. platform_device_unregister(dmapd);
  409. }
  410. EXPORT_SYMBOL_GPL(au1xpsc_pcm_destroy);
  411. MODULE_LICENSE("GPL");
  412. MODULE_DESCRIPTION("Au12x0/Au1550 PSC Audio DMA driver");
  413. MODULE_AUTHOR("Manuel Lauss");