dma-sh7760.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  2. * SH7760 ("camelot") DMABRG audio DMA unit support
  3. *
  4. * Copyright (C) 2007 Manuel Lauss <mano@roarinelk.homelinux.net>
  5. * licensed under the terms outlined in the file COPYING at the root
  6. * of the linux kernel sources.
  7. *
  8. * The SH7760 DMABRG provides 4 dma channels (2x rec, 2x play), which
  9. * trigger an interrupt when one half of the programmed transfer size
  10. * has been xmitted.
  11. *
  12. * FIXME: little-endian only for now
  13. */
  14. #include <linux/module.h>
  15. #include <linux/gfp.h>
  16. #include <linux/init.h>
  17. #include <linux/platform_device.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/dmabrg.h>
  24. /* registers and bits */
  25. #define BRGATXSAR 0x00
  26. #define BRGARXDAR 0x04
  27. #define BRGATXTCR 0x08
  28. #define BRGARXTCR 0x0C
  29. #define BRGACR 0x10
  30. #define BRGATXTCNT 0x14
  31. #define BRGARXTCNT 0x18
  32. #define ACR_RAR (1 << 18)
  33. #define ACR_RDS (1 << 17)
  34. #define ACR_RDE (1 << 16)
  35. #define ACR_TAR (1 << 2)
  36. #define ACR_TDS (1 << 1)
  37. #define ACR_TDE (1 << 0)
  38. /* receiver/transmitter data alignment */
  39. #define ACR_RAM_NONE (0 << 24)
  40. #define ACR_RAM_4BYTE (1 << 24)
  41. #define ACR_RAM_2WORD (2 << 24)
  42. #define ACR_TAM_NONE (0 << 8)
  43. #define ACR_TAM_4BYTE (1 << 8)
  44. #define ACR_TAM_2WORD (2 << 8)
  45. struct camelot_pcm {
  46. unsigned long mmio; /* DMABRG audio channel control reg MMIO */
  47. unsigned int txid; /* ID of first DMABRG IRQ for this unit */
  48. struct snd_pcm_substream *tx_ss;
  49. unsigned long tx_period_size;
  50. unsigned int tx_period;
  51. struct snd_pcm_substream *rx_ss;
  52. unsigned long rx_period_size;
  53. unsigned int rx_period;
  54. } cam_pcm_data[2] = {
  55. {
  56. .mmio = 0xFE3C0040,
  57. .txid = DMABRGIRQ_A0TXF,
  58. },
  59. {
  60. .mmio = 0xFE3C0060,
  61. .txid = DMABRGIRQ_A1TXF,
  62. },
  63. };
  64. #define BRGREG(x) (*(unsigned long *)(cam->mmio + (x)))
  65. /*
  66. * set a minimum of 16kb per period, to avoid interrupt-"storm" and
  67. * resulting skipping. In general, the bigger the minimum size, the
  68. * better for overall system performance. (The SH7760 is a puny CPU
  69. * with a slow SDRAM interface and poor internal bus bandwidth,
  70. * *especially* when the LCDC is active). The minimum for the DMAC
  71. * is 8 bytes; 16kbytes are enough to get skip-free playback of a
  72. * 44kHz/16bit/stereo MP3 on a lightly loaded system, and maintain
  73. * reasonable responsiveness in MPlayer.
  74. */
  75. #define DMABRG_PERIOD_MIN 16 * 1024
  76. #define DMABRG_PERIOD_MAX 0x03fffffc
  77. #define DMABRG_PREALLOC_BUFFER 32 * 1024
  78. #define DMABRG_PREALLOC_BUFFER_MAX 32 * 1024
  79. static struct snd_pcm_hardware camelot_pcm_hardware = {
  80. .info = (SNDRV_PCM_INFO_MMAP |
  81. SNDRV_PCM_INFO_INTERLEAVED |
  82. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  83. SNDRV_PCM_INFO_MMAP_VALID |
  84. SNDRV_PCM_INFO_BATCH),
  85. .buffer_bytes_max = DMABRG_PERIOD_MAX,
  86. .period_bytes_min = DMABRG_PERIOD_MIN,
  87. .period_bytes_max = DMABRG_PERIOD_MAX / 2,
  88. .periods_min = 2,
  89. .periods_max = 2,
  90. .fifo_size = 128,
  91. };
  92. static void camelot_txdma(void *data)
  93. {
  94. struct camelot_pcm *cam = data;
  95. cam->tx_period ^= 1;
  96. snd_pcm_period_elapsed(cam->tx_ss);
  97. }
  98. static void camelot_rxdma(void *data)
  99. {
  100. struct camelot_pcm *cam = data;
  101. cam->rx_period ^= 1;
  102. snd_pcm_period_elapsed(cam->rx_ss);
  103. }
  104. static int camelot_pcm_open(struct snd_pcm_substream *substream)
  105. {
  106. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  107. struct camelot_pcm *cam = &cam_pcm_data[rtd->cpu_dai->id];
  108. int recv = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ? 0:1;
  109. int ret, dmairq;
  110. snd_soc_set_runtime_hwparams(substream, &camelot_pcm_hardware);
  111. /* DMABRG buffer half/full events */
  112. dmairq = (recv) ? cam->txid + 2 : cam->txid;
  113. if (recv) {
  114. cam->rx_ss = substream;
  115. ret = dmabrg_request_irq(dmairq, camelot_rxdma, cam);
  116. if (unlikely(ret)) {
  117. pr_debug("audio unit %d irqs already taken!\n",
  118. rtd->cpu_dai->id);
  119. return -EBUSY;
  120. }
  121. (void)dmabrg_request_irq(dmairq + 1,camelot_rxdma, cam);
  122. } else {
  123. cam->tx_ss = substream;
  124. ret = dmabrg_request_irq(dmairq, camelot_txdma, cam);
  125. if (unlikely(ret)) {
  126. pr_debug("audio unit %d irqs already taken!\n",
  127. rtd->cpu_dai->id);
  128. return -EBUSY;
  129. }
  130. (void)dmabrg_request_irq(dmairq + 1, camelot_txdma, cam);
  131. }
  132. return 0;
  133. }
  134. static int camelot_pcm_close(struct snd_pcm_substream *substream)
  135. {
  136. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  137. struct camelot_pcm *cam = &cam_pcm_data[rtd->cpu_dai->id];
  138. int recv = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ? 0:1;
  139. int dmairq;
  140. dmairq = (recv) ? cam->txid + 2 : cam->txid;
  141. if (recv)
  142. cam->rx_ss = NULL;
  143. else
  144. cam->tx_ss = NULL;
  145. dmabrg_free_irq(dmairq + 1);
  146. dmabrg_free_irq(dmairq);
  147. return 0;
  148. }
  149. static int camelot_hw_params(struct snd_pcm_substream *substream,
  150. struct snd_pcm_hw_params *hw_params)
  151. {
  152. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  153. struct camelot_pcm *cam = &cam_pcm_data[rtd->cpu_dai->id];
  154. int recv = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ? 0:1;
  155. int ret;
  156. ret = snd_pcm_lib_malloc_pages(substream,
  157. params_buffer_bytes(hw_params));
  158. if (ret < 0)
  159. return ret;
  160. if (recv) {
  161. cam->rx_period_size = params_period_bytes(hw_params);
  162. cam->rx_period = 0;
  163. } else {
  164. cam->tx_period_size = params_period_bytes(hw_params);
  165. cam->tx_period = 0;
  166. }
  167. return 0;
  168. }
  169. static int camelot_hw_free(struct snd_pcm_substream *substream)
  170. {
  171. return snd_pcm_lib_free_pages(substream);
  172. }
  173. static int camelot_prepare(struct snd_pcm_substream *substream)
  174. {
  175. struct snd_pcm_runtime *runtime = substream->runtime;
  176. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  177. struct camelot_pcm *cam = &cam_pcm_data[rtd->cpu_dai->id];
  178. pr_debug("PCM data: addr 0x%08ulx len %d\n",
  179. (u32)runtime->dma_addr, runtime->dma_bytes);
  180. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  181. BRGREG(BRGATXSAR) = (unsigned long)runtime->dma_area;
  182. BRGREG(BRGATXTCR) = runtime->dma_bytes;
  183. } else {
  184. BRGREG(BRGARXDAR) = (unsigned long)runtime->dma_area;
  185. BRGREG(BRGARXTCR) = runtime->dma_bytes;
  186. }
  187. return 0;
  188. }
  189. static inline void dmabrg_play_dma_start(struct camelot_pcm *cam)
  190. {
  191. unsigned long acr = BRGREG(BRGACR) & ~(ACR_TDS | ACR_RDS);
  192. /* start DMABRG engine: XFER start, auto-addr-reload */
  193. BRGREG(BRGACR) = acr | ACR_TDE | ACR_TAR | ACR_TAM_2WORD;
  194. }
  195. static inline void dmabrg_play_dma_stop(struct camelot_pcm *cam)
  196. {
  197. unsigned long acr = BRGREG(BRGACR) & ~(ACR_TDS | ACR_RDS);
  198. /* forcibly terminate data transmission */
  199. BRGREG(BRGACR) = acr | ACR_TDS;
  200. }
  201. static inline void dmabrg_rec_dma_start(struct camelot_pcm *cam)
  202. {
  203. unsigned long acr = BRGREG(BRGACR) & ~(ACR_TDS | ACR_RDS);
  204. /* start DMABRG engine: recv start, auto-reload */
  205. BRGREG(BRGACR) = acr | ACR_RDE | ACR_RAR | ACR_RAM_2WORD;
  206. }
  207. static inline void dmabrg_rec_dma_stop(struct camelot_pcm *cam)
  208. {
  209. unsigned long acr = BRGREG(BRGACR) & ~(ACR_TDS | ACR_RDS);
  210. /* forcibly terminate data receiver */
  211. BRGREG(BRGACR) = acr | ACR_RDS;
  212. }
  213. static int camelot_trigger(struct snd_pcm_substream *substream, int cmd)
  214. {
  215. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  216. struct camelot_pcm *cam = &cam_pcm_data[rtd->cpu_dai->id];
  217. int recv = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ? 0:1;
  218. switch (cmd) {
  219. case SNDRV_PCM_TRIGGER_START:
  220. if (recv)
  221. dmabrg_rec_dma_start(cam);
  222. else
  223. dmabrg_play_dma_start(cam);
  224. break;
  225. case SNDRV_PCM_TRIGGER_STOP:
  226. if (recv)
  227. dmabrg_rec_dma_stop(cam);
  228. else
  229. dmabrg_play_dma_stop(cam);
  230. break;
  231. default:
  232. return -EINVAL;
  233. }
  234. return 0;
  235. }
  236. static snd_pcm_uframes_t camelot_pos(struct snd_pcm_substream *substream)
  237. {
  238. struct snd_pcm_runtime *runtime = substream->runtime;
  239. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  240. struct camelot_pcm *cam = &cam_pcm_data[rtd->cpu_dai->id];
  241. int recv = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ? 0:1;
  242. unsigned long pos;
  243. /* cannot use the DMABRG pointer register: under load, by the
  244. * time ALSA comes around to read the register, it is already
  245. * far ahead (or worse, already done with the fragment) of the
  246. * position at the time the IRQ was triggered, which results in
  247. * fast-playback sound in my test application (ScummVM)
  248. */
  249. if (recv)
  250. pos = cam->rx_period ? cam->rx_period_size : 0;
  251. else
  252. pos = cam->tx_period ? cam->tx_period_size : 0;
  253. return bytes_to_frames(runtime, pos);
  254. }
  255. static struct snd_pcm_ops camelot_pcm_ops = {
  256. .open = camelot_pcm_open,
  257. .close = camelot_pcm_close,
  258. .ioctl = snd_pcm_lib_ioctl,
  259. .hw_params = camelot_hw_params,
  260. .hw_free = camelot_hw_free,
  261. .prepare = camelot_prepare,
  262. .trigger = camelot_trigger,
  263. .pointer = camelot_pos,
  264. };
  265. static int camelot_pcm_new(struct snd_soc_pcm_runtime *rtd)
  266. {
  267. struct snd_pcm *pcm = rtd->pcm;
  268. /* dont use SNDRV_DMA_TYPE_DEV, since it will oops the SH kernel
  269. * in MMAP mode (i.e. aplay -M)
  270. */
  271. snd_pcm_lib_preallocate_pages_for_all(pcm,
  272. SNDRV_DMA_TYPE_CONTINUOUS,
  273. snd_dma_continuous_data(GFP_KERNEL),
  274. DMABRG_PREALLOC_BUFFER, DMABRG_PREALLOC_BUFFER_MAX);
  275. return 0;
  276. }
  277. static struct snd_soc_platform_driver sh7760_soc_platform = {
  278. .ops = &camelot_pcm_ops,
  279. .pcm_new = camelot_pcm_new,
  280. };
  281. static int sh7760_soc_platform_probe(struct platform_device *pdev)
  282. {
  283. return devm_snd_soc_register_platform(&pdev->dev, &sh7760_soc_platform);
  284. }
  285. static struct platform_driver sh7760_pcm_driver = {
  286. .driver = {
  287. .name = "sh7760-pcm-audio",
  288. },
  289. .probe = sh7760_soc_platform_probe,
  290. };
  291. module_platform_driver(sh7760_pcm_driver);
  292. MODULE_LICENSE("GPL");
  293. MODULE_DESCRIPTION("SH7760 Audio DMA (DMABRG) driver");
  294. MODULE_AUTHOR("Manuel Lauss <mano@roarinelk.homelinux.net>");