dbdma2.c 9.1 KB

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