kirkwood-dma.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * kirkwood-dma.c
  3. *
  4. * (c) 2010 Arnaud Patard <apatard@mandriva.com>
  5. * (c) 2010 Arnaud Patard <arnaud.patard@rtp-net.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or (at your
  10. * option) any later version.
  11. */
  12. #include <linux/init.h>
  13. #include <linux/module.h>
  14. #include <linux/device.h>
  15. #include <linux/io.h>
  16. #include <linux/slab.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/dma-mapping.h>
  19. #include <linux/mbus.h>
  20. #include <sound/soc.h>
  21. #include "kirkwood.h"
  22. static struct kirkwood_dma_data *kirkwood_priv(struct snd_pcm_substream *subs)
  23. {
  24. struct snd_soc_pcm_runtime *soc_runtime = subs->private_data;
  25. return snd_soc_dai_get_drvdata(soc_runtime->cpu_dai);
  26. }
  27. static struct snd_pcm_hardware kirkwood_dma_snd_hw = {
  28. .info = SNDRV_PCM_INFO_INTERLEAVED |
  29. SNDRV_PCM_INFO_MMAP |
  30. SNDRV_PCM_INFO_MMAP_VALID |
  31. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  32. SNDRV_PCM_INFO_PAUSE |
  33. SNDRV_PCM_INFO_NO_PERIOD_WAKEUP,
  34. .buffer_bytes_max = KIRKWOOD_SND_MAX_BUFFER_BYTES,
  35. .period_bytes_min = KIRKWOOD_SND_MIN_PERIOD_BYTES,
  36. .period_bytes_max = KIRKWOOD_SND_MAX_PERIOD_BYTES,
  37. .periods_min = KIRKWOOD_SND_MIN_PERIODS,
  38. .periods_max = KIRKWOOD_SND_MAX_PERIODS,
  39. .fifo_size = 0,
  40. };
  41. static irqreturn_t kirkwood_dma_irq(int irq, void *dev_id)
  42. {
  43. struct kirkwood_dma_data *priv = dev_id;
  44. unsigned long mask, status, cause;
  45. mask = readl(priv->io + KIRKWOOD_INT_MASK);
  46. status = readl(priv->io + KIRKWOOD_INT_CAUSE) & mask;
  47. cause = readl(priv->io + KIRKWOOD_ERR_CAUSE);
  48. if (unlikely(cause)) {
  49. printk(KERN_WARNING "%s: got err interrupt 0x%lx\n",
  50. __func__, cause);
  51. writel(cause, priv->io + KIRKWOOD_ERR_CAUSE);
  52. }
  53. /* we've enabled only bytes interrupts ... */
  54. if (status & ~(KIRKWOOD_INT_CAUSE_PLAY_BYTES | \
  55. KIRKWOOD_INT_CAUSE_REC_BYTES)) {
  56. printk(KERN_WARNING "%s: unexpected interrupt %lx\n",
  57. __func__, status);
  58. return IRQ_NONE;
  59. }
  60. /* ack int */
  61. writel(status, priv->io + KIRKWOOD_INT_CAUSE);
  62. if (status & KIRKWOOD_INT_CAUSE_PLAY_BYTES)
  63. snd_pcm_period_elapsed(priv->substream_play);
  64. if (status & KIRKWOOD_INT_CAUSE_REC_BYTES)
  65. snd_pcm_period_elapsed(priv->substream_rec);
  66. return IRQ_HANDLED;
  67. }
  68. static void
  69. kirkwood_dma_conf_mbus_windows(void __iomem *base, int win,
  70. unsigned long dma,
  71. const struct mbus_dram_target_info *dram)
  72. {
  73. int i;
  74. /* First disable and clear windows */
  75. writel(0, base + KIRKWOOD_AUDIO_WIN_CTRL_REG(win));
  76. writel(0, base + KIRKWOOD_AUDIO_WIN_BASE_REG(win));
  77. /* try to find matching cs for current dma address */
  78. for (i = 0; i < dram->num_cs; i++) {
  79. const struct mbus_dram_window *cs = dram->cs + i;
  80. if ((cs->base & 0xffff0000) < (dma & 0xffff0000)) {
  81. writel(cs->base & 0xffff0000,
  82. base + KIRKWOOD_AUDIO_WIN_BASE_REG(win));
  83. writel(((cs->size - 1) & 0xffff0000) |
  84. (cs->mbus_attr << 8) |
  85. (dram->mbus_dram_target_id << 4) | 1,
  86. base + KIRKWOOD_AUDIO_WIN_CTRL_REG(win));
  87. }
  88. }
  89. }
  90. static int kirkwood_dma_open(struct snd_pcm_substream *substream)
  91. {
  92. int err;
  93. struct snd_pcm_runtime *runtime = substream->runtime;
  94. struct kirkwood_dma_data *priv = kirkwood_priv(substream);
  95. const struct mbus_dram_target_info *dram;
  96. unsigned long addr;
  97. snd_soc_set_runtime_hwparams(substream, &kirkwood_dma_snd_hw);
  98. /* Ensure that all constraints linked to dma burst are fulfilled */
  99. err = snd_pcm_hw_constraint_minmax(runtime,
  100. SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  101. priv->burst * 2,
  102. KIRKWOOD_AUDIO_BUF_MAX-1);
  103. if (err < 0)
  104. return err;
  105. err = snd_pcm_hw_constraint_step(runtime, 0,
  106. SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  107. priv->burst);
  108. if (err < 0)
  109. return err;
  110. err = snd_pcm_hw_constraint_step(substream->runtime, 0,
  111. SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
  112. priv->burst);
  113. if (err < 0)
  114. return err;
  115. if (!priv->substream_play && !priv->substream_rec) {
  116. err = request_irq(priv->irq, kirkwood_dma_irq, IRQF_SHARED,
  117. "kirkwood-i2s", priv);
  118. if (err)
  119. return -EBUSY;
  120. /*
  121. * Enable Error interrupts. We're only ack'ing them but
  122. * it's useful for diagnostics
  123. */
  124. writel((unsigned int)-1, priv->io + KIRKWOOD_ERR_MASK);
  125. }
  126. dram = mv_mbus_dram_info();
  127. addr = substream->dma_buffer.addr;
  128. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  129. if (priv->substream_play)
  130. return -EBUSY;
  131. priv->substream_play = substream;
  132. kirkwood_dma_conf_mbus_windows(priv->io,
  133. KIRKWOOD_PLAYBACK_WIN, addr, dram);
  134. } else {
  135. if (priv->substream_rec)
  136. return -EBUSY;
  137. priv->substream_rec = substream;
  138. kirkwood_dma_conf_mbus_windows(priv->io,
  139. KIRKWOOD_RECORD_WIN, addr, dram);
  140. }
  141. return 0;
  142. }
  143. static int kirkwood_dma_close(struct snd_pcm_substream *substream)
  144. {
  145. struct kirkwood_dma_data *priv = kirkwood_priv(substream);
  146. if (!priv)
  147. return 0;
  148. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  149. priv->substream_play = NULL;
  150. else
  151. priv->substream_rec = NULL;
  152. if (!priv->substream_play && !priv->substream_rec) {
  153. writel(0, priv->io + KIRKWOOD_ERR_MASK);
  154. free_irq(priv->irq, priv);
  155. }
  156. return 0;
  157. }
  158. static int kirkwood_dma_hw_params(struct snd_pcm_substream *substream,
  159. struct snd_pcm_hw_params *params)
  160. {
  161. struct snd_pcm_runtime *runtime = substream->runtime;
  162. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  163. runtime->dma_bytes = params_buffer_bytes(params);
  164. return 0;
  165. }
  166. static int kirkwood_dma_hw_free(struct snd_pcm_substream *substream)
  167. {
  168. snd_pcm_set_runtime_buffer(substream, NULL);
  169. return 0;
  170. }
  171. static int kirkwood_dma_prepare(struct snd_pcm_substream *substream)
  172. {
  173. struct snd_pcm_runtime *runtime = substream->runtime;
  174. struct kirkwood_dma_data *priv = kirkwood_priv(substream);
  175. unsigned long size, count;
  176. /* compute buffer size in term of "words" as requested in specs */
  177. size = frames_to_bytes(runtime, runtime->buffer_size);
  178. size = (size>>2)-1;
  179. count = snd_pcm_lib_period_bytes(substream);
  180. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  181. writel(count, priv->io + KIRKWOOD_PLAY_BYTE_INT_COUNT);
  182. writel(runtime->dma_addr, priv->io + KIRKWOOD_PLAY_BUF_ADDR);
  183. writel(size, priv->io + KIRKWOOD_PLAY_BUF_SIZE);
  184. } else {
  185. writel(count, priv->io + KIRKWOOD_REC_BYTE_INT_COUNT);
  186. writel(runtime->dma_addr, priv->io + KIRKWOOD_REC_BUF_ADDR);
  187. writel(size, priv->io + KIRKWOOD_REC_BUF_SIZE);
  188. }
  189. return 0;
  190. }
  191. static snd_pcm_uframes_t kirkwood_dma_pointer(struct snd_pcm_substream
  192. *substream)
  193. {
  194. struct kirkwood_dma_data *priv = kirkwood_priv(substream);
  195. snd_pcm_uframes_t count;
  196. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  197. count = bytes_to_frames(substream->runtime,
  198. readl(priv->io + KIRKWOOD_PLAY_BYTE_COUNT));
  199. else
  200. count = bytes_to_frames(substream->runtime,
  201. readl(priv->io + KIRKWOOD_REC_BYTE_COUNT));
  202. return count;
  203. }
  204. static struct snd_pcm_ops kirkwood_dma_ops = {
  205. .open = kirkwood_dma_open,
  206. .close = kirkwood_dma_close,
  207. .ioctl = snd_pcm_lib_ioctl,
  208. .hw_params = kirkwood_dma_hw_params,
  209. .hw_free = kirkwood_dma_hw_free,
  210. .prepare = kirkwood_dma_prepare,
  211. .pointer = kirkwood_dma_pointer,
  212. };
  213. static int kirkwood_dma_preallocate_dma_buffer(struct snd_pcm *pcm,
  214. int stream)
  215. {
  216. struct snd_pcm_substream *substream = pcm->streams[stream].substream;
  217. struct snd_dma_buffer *buf = &substream->dma_buffer;
  218. size_t size = kirkwood_dma_snd_hw.buffer_bytes_max;
  219. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  220. buf->dev.dev = pcm->card->dev;
  221. buf->area = dma_alloc_coherent(pcm->card->dev, size,
  222. &buf->addr, GFP_KERNEL);
  223. if (!buf->area)
  224. return -ENOMEM;
  225. buf->bytes = size;
  226. buf->private_data = NULL;
  227. return 0;
  228. }
  229. static int kirkwood_dma_new(struct snd_soc_pcm_runtime *rtd)
  230. {
  231. struct snd_card *card = rtd->card->snd_card;
  232. struct snd_pcm *pcm = rtd->pcm;
  233. int ret;
  234. ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(32));
  235. if (ret)
  236. return ret;
  237. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
  238. ret = kirkwood_dma_preallocate_dma_buffer(pcm,
  239. SNDRV_PCM_STREAM_PLAYBACK);
  240. if (ret)
  241. return ret;
  242. }
  243. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
  244. ret = kirkwood_dma_preallocate_dma_buffer(pcm,
  245. SNDRV_PCM_STREAM_CAPTURE);
  246. if (ret)
  247. return ret;
  248. }
  249. return 0;
  250. }
  251. static void kirkwood_dma_free_dma_buffers(struct snd_pcm *pcm)
  252. {
  253. struct snd_pcm_substream *substream;
  254. struct snd_dma_buffer *buf;
  255. int stream;
  256. for (stream = 0; stream < 2; stream++) {
  257. substream = pcm->streams[stream].substream;
  258. if (!substream)
  259. continue;
  260. buf = &substream->dma_buffer;
  261. if (!buf->area)
  262. continue;
  263. dma_free_coherent(pcm->card->dev, buf->bytes,
  264. buf->area, buf->addr);
  265. buf->area = NULL;
  266. }
  267. }
  268. struct snd_soc_platform_driver kirkwood_soc_platform = {
  269. .ops = &kirkwood_dma_ops,
  270. .pcm_new = kirkwood_dma_new,
  271. .pcm_free = kirkwood_dma_free_dma_buffers,
  272. };