idma.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*
  2. * sound/soc/samsung/idma.c
  3. *
  4. * Copyright (c) 2011 Samsung Electronics Co., Ltd.
  5. * http://www.samsung.com
  6. *
  7. * I2S0's Internal DMA driver
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. */
  14. #include <linux/interrupt.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/dma-mapping.h>
  17. #include <linux/slab.h>
  18. #include <linux/module.h>
  19. #include <sound/pcm.h>
  20. #include <sound/pcm_params.h>
  21. #include <sound/soc.h>
  22. #include "i2s.h"
  23. #include "idma.h"
  24. #include "dma.h"
  25. #include "i2s-regs.h"
  26. #define ST_RUNNING (1<<0)
  27. #define ST_OPENED (1<<1)
  28. static const struct snd_pcm_hardware idma_hardware = {
  29. .info = SNDRV_PCM_INFO_INTERLEAVED |
  30. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  31. SNDRV_PCM_INFO_MMAP |
  32. SNDRV_PCM_INFO_MMAP_VALID |
  33. SNDRV_PCM_INFO_PAUSE |
  34. SNDRV_PCM_INFO_RESUME,
  35. .buffer_bytes_max = MAX_IDMA_BUFFER,
  36. .period_bytes_min = 128,
  37. .period_bytes_max = MAX_IDMA_PERIOD,
  38. .periods_min = 1,
  39. .periods_max = 2,
  40. };
  41. struct idma_ctrl {
  42. spinlock_t lock;
  43. int state;
  44. dma_addr_t start;
  45. dma_addr_t pos;
  46. dma_addr_t end;
  47. dma_addr_t period;
  48. dma_addr_t periodsz;
  49. void *token;
  50. void (*cb)(void *dt, int bytes_xfer);
  51. };
  52. static struct idma_info {
  53. spinlock_t lock;
  54. void __iomem *regs;
  55. dma_addr_t lp_tx_addr;
  56. } idma;
  57. static int idma_irq;
  58. static void idma_getpos(dma_addr_t *src)
  59. {
  60. *src = idma.lp_tx_addr +
  61. (readl(idma.regs + I2STRNCNT) & 0xffffff) * 4;
  62. }
  63. static int idma_enqueue(struct snd_pcm_substream *substream)
  64. {
  65. struct snd_pcm_runtime *runtime = substream->runtime;
  66. struct idma_ctrl *prtd = substream->runtime->private_data;
  67. u32 val;
  68. spin_lock(&prtd->lock);
  69. prtd->token = (void *) substream;
  70. spin_unlock(&prtd->lock);
  71. /* Internal DMA Level0 Interrupt Address */
  72. val = idma.lp_tx_addr + prtd->periodsz;
  73. writel(val, idma.regs + I2SLVL0ADDR);
  74. /* Start address0 of I2S internal DMA operation. */
  75. val = idma.lp_tx_addr;
  76. writel(val, idma.regs + I2SSTR0);
  77. /*
  78. * Transfer block size for I2S internal DMA.
  79. * Should decide transfer size before start dma operation
  80. */
  81. val = readl(idma.regs + I2SSIZE);
  82. val &= ~(I2SSIZE_TRNMSK << I2SSIZE_SHIFT);
  83. val |= (((runtime->dma_bytes >> 2) &
  84. I2SSIZE_TRNMSK) << I2SSIZE_SHIFT);
  85. writel(val, idma.regs + I2SSIZE);
  86. val = readl(idma.regs + I2SAHB);
  87. val |= AHB_INTENLVL0;
  88. writel(val, idma.regs + I2SAHB);
  89. return 0;
  90. }
  91. static void idma_setcallbk(struct snd_pcm_substream *substream,
  92. void (*cb)(void *, int))
  93. {
  94. struct idma_ctrl *prtd = substream->runtime->private_data;
  95. spin_lock(&prtd->lock);
  96. prtd->cb = cb;
  97. spin_unlock(&prtd->lock);
  98. }
  99. static void idma_control(int op)
  100. {
  101. u32 val = readl(idma.regs + I2SAHB);
  102. spin_lock(&idma.lock);
  103. switch (op) {
  104. case LPAM_DMA_START:
  105. val |= (AHB_INTENLVL0 | AHB_DMAEN);
  106. break;
  107. case LPAM_DMA_STOP:
  108. val &= ~(AHB_INTENLVL0 | AHB_DMAEN);
  109. break;
  110. default:
  111. spin_unlock(&idma.lock);
  112. return;
  113. }
  114. writel(val, idma.regs + I2SAHB);
  115. spin_unlock(&idma.lock);
  116. }
  117. static void idma_done(void *id, int bytes_xfer)
  118. {
  119. struct snd_pcm_substream *substream = id;
  120. struct idma_ctrl *prtd = substream->runtime->private_data;
  121. if (prtd && (prtd->state & ST_RUNNING))
  122. snd_pcm_period_elapsed(substream);
  123. }
  124. static int idma_hw_params(struct snd_pcm_substream *substream,
  125. struct snd_pcm_hw_params *params)
  126. {
  127. struct snd_pcm_runtime *runtime = substream->runtime;
  128. struct idma_ctrl *prtd = substream->runtime->private_data;
  129. u32 mod = readl(idma.regs + I2SMOD);
  130. u32 ahb = readl(idma.regs + I2SAHB);
  131. ahb |= (AHB_DMARLD | AHB_INTMASK);
  132. mod |= MOD_TXS_IDMA;
  133. writel(ahb, idma.regs + I2SAHB);
  134. writel(mod, idma.regs + I2SMOD);
  135. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  136. runtime->dma_bytes = params_buffer_bytes(params);
  137. prtd->start = prtd->pos = runtime->dma_addr;
  138. prtd->period = params_periods(params);
  139. prtd->periodsz = params_period_bytes(params);
  140. prtd->end = runtime->dma_addr + runtime->dma_bytes;
  141. idma_setcallbk(substream, idma_done);
  142. return 0;
  143. }
  144. static int idma_hw_free(struct snd_pcm_substream *substream)
  145. {
  146. snd_pcm_set_runtime_buffer(substream, NULL);
  147. return 0;
  148. }
  149. static int idma_prepare(struct snd_pcm_substream *substream)
  150. {
  151. struct idma_ctrl *prtd = substream->runtime->private_data;
  152. prtd->pos = prtd->start;
  153. /* flush the DMA channel */
  154. idma_control(LPAM_DMA_STOP);
  155. idma_enqueue(substream);
  156. return 0;
  157. }
  158. static int idma_trigger(struct snd_pcm_substream *substream, int cmd)
  159. {
  160. struct idma_ctrl *prtd = substream->runtime->private_data;
  161. int ret = 0;
  162. spin_lock(&prtd->lock);
  163. switch (cmd) {
  164. case SNDRV_PCM_TRIGGER_RESUME:
  165. case SNDRV_PCM_TRIGGER_START:
  166. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  167. prtd->state |= ST_RUNNING;
  168. idma_control(LPAM_DMA_START);
  169. break;
  170. case SNDRV_PCM_TRIGGER_SUSPEND:
  171. case SNDRV_PCM_TRIGGER_STOP:
  172. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  173. prtd->state &= ~ST_RUNNING;
  174. idma_control(LPAM_DMA_STOP);
  175. break;
  176. default:
  177. ret = -EINVAL;
  178. break;
  179. }
  180. spin_unlock(&prtd->lock);
  181. return ret;
  182. }
  183. static snd_pcm_uframes_t
  184. idma_pointer(struct snd_pcm_substream *substream)
  185. {
  186. struct snd_pcm_runtime *runtime = substream->runtime;
  187. struct idma_ctrl *prtd = runtime->private_data;
  188. dma_addr_t src;
  189. unsigned long res;
  190. spin_lock(&prtd->lock);
  191. idma_getpos(&src);
  192. res = src - prtd->start;
  193. spin_unlock(&prtd->lock);
  194. return bytes_to_frames(substream->runtime, res);
  195. }
  196. static int idma_mmap(struct snd_pcm_substream *substream,
  197. struct vm_area_struct *vma)
  198. {
  199. struct snd_pcm_runtime *runtime = substream->runtime;
  200. unsigned long size, offset;
  201. int ret;
  202. /* From snd_pcm_lib_mmap_iomem */
  203. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  204. size = vma->vm_end - vma->vm_start;
  205. offset = vma->vm_pgoff << PAGE_SHIFT;
  206. ret = io_remap_pfn_range(vma, vma->vm_start,
  207. (runtime->dma_addr + offset) >> PAGE_SHIFT,
  208. size, vma->vm_page_prot);
  209. return ret;
  210. }
  211. static irqreturn_t iis_irq(int irqno, void *dev_id)
  212. {
  213. struct idma_ctrl *prtd = (struct idma_ctrl *)dev_id;
  214. u32 iisahb, val, addr;
  215. iisahb = readl(idma.regs + I2SAHB);
  216. val = (iisahb & AHB_LVL0INT) ? AHB_CLRLVL0INT : 0;
  217. if (val) {
  218. iisahb |= val;
  219. writel(iisahb, idma.regs + I2SAHB);
  220. addr = readl(idma.regs + I2SLVL0ADDR) - idma.lp_tx_addr;
  221. addr += prtd->periodsz;
  222. addr %= (u32)(prtd->end - prtd->start);
  223. addr += idma.lp_tx_addr;
  224. writel(addr, idma.regs + I2SLVL0ADDR);
  225. if (prtd->cb)
  226. prtd->cb(prtd->token, prtd->period);
  227. }
  228. return IRQ_HANDLED;
  229. }
  230. static int idma_open(struct snd_pcm_substream *substream)
  231. {
  232. struct snd_pcm_runtime *runtime = substream->runtime;
  233. struct idma_ctrl *prtd;
  234. int ret;
  235. snd_soc_set_runtime_hwparams(substream, &idma_hardware);
  236. prtd = kzalloc(sizeof(struct idma_ctrl), GFP_KERNEL);
  237. if (prtd == NULL)
  238. return -ENOMEM;
  239. ret = request_irq(idma_irq, iis_irq, 0, "i2s", prtd);
  240. if (ret < 0) {
  241. pr_err("fail to claim i2s irq , ret = %d\n", ret);
  242. kfree(prtd);
  243. return ret;
  244. }
  245. spin_lock_init(&prtd->lock);
  246. runtime->private_data = prtd;
  247. return 0;
  248. }
  249. static int idma_close(struct snd_pcm_substream *substream)
  250. {
  251. struct snd_pcm_runtime *runtime = substream->runtime;
  252. struct idma_ctrl *prtd = runtime->private_data;
  253. free_irq(idma_irq, prtd);
  254. if (!prtd)
  255. pr_err("idma_close called with prtd == NULL\n");
  256. kfree(prtd);
  257. return 0;
  258. }
  259. static struct snd_pcm_ops idma_ops = {
  260. .open = idma_open,
  261. .close = idma_close,
  262. .ioctl = snd_pcm_lib_ioctl,
  263. .trigger = idma_trigger,
  264. .pointer = idma_pointer,
  265. .mmap = idma_mmap,
  266. .hw_params = idma_hw_params,
  267. .hw_free = idma_hw_free,
  268. .prepare = idma_prepare,
  269. };
  270. static void idma_free(struct snd_pcm *pcm)
  271. {
  272. struct snd_pcm_substream *substream;
  273. struct snd_dma_buffer *buf;
  274. substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
  275. if (!substream)
  276. return;
  277. buf = &substream->dma_buffer;
  278. if (!buf->area)
  279. return;
  280. iounmap((void __iomem *)buf->area);
  281. buf->area = NULL;
  282. buf->addr = 0;
  283. }
  284. static int preallocate_idma_buffer(struct snd_pcm *pcm, int stream)
  285. {
  286. struct snd_pcm_substream *substream = pcm->streams[stream].substream;
  287. struct snd_dma_buffer *buf = &substream->dma_buffer;
  288. buf->dev.dev = pcm->card->dev;
  289. buf->private_data = NULL;
  290. /* Assign PCM buffer pointers */
  291. buf->dev.type = SNDRV_DMA_TYPE_CONTINUOUS;
  292. buf->addr = idma.lp_tx_addr;
  293. buf->bytes = idma_hardware.buffer_bytes_max;
  294. buf->area = (unsigned char * __force)ioremap(buf->addr, buf->bytes);
  295. return 0;
  296. }
  297. static int idma_new(struct snd_soc_pcm_runtime *rtd)
  298. {
  299. struct snd_card *card = rtd->card->snd_card;
  300. struct snd_pcm *pcm = rtd->pcm;
  301. int ret;
  302. ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(32));
  303. if (ret)
  304. return ret;
  305. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
  306. ret = preallocate_idma_buffer(pcm,
  307. SNDRV_PCM_STREAM_PLAYBACK);
  308. }
  309. return ret;
  310. }
  311. void idma_reg_addr_init(void __iomem *regs, dma_addr_t addr)
  312. {
  313. spin_lock_init(&idma.lock);
  314. idma.regs = regs;
  315. idma.lp_tx_addr = addr;
  316. }
  317. EXPORT_SYMBOL_GPL(idma_reg_addr_init);
  318. static struct snd_soc_platform_driver asoc_idma_platform = {
  319. .ops = &idma_ops,
  320. .pcm_new = idma_new,
  321. .pcm_free = idma_free,
  322. };
  323. static int asoc_idma_platform_probe(struct platform_device *pdev)
  324. {
  325. idma_irq = platform_get_irq(pdev, 0);
  326. if (idma_irq < 0)
  327. return idma_irq;
  328. return devm_snd_soc_register_platform(&pdev->dev, &asoc_idma_platform);
  329. }
  330. static struct platform_driver asoc_idma_driver = {
  331. .driver = {
  332. .name = "samsung-idma",
  333. },
  334. .probe = asoc_idma_platform_probe,
  335. };
  336. module_platform_driver(asoc_idma_driver);
  337. MODULE_AUTHOR("Jaswinder Singh, <jassisinghbrar@gmail.com>");
  338. MODULE_DESCRIPTION("Samsung ASoC IDMA Driver");
  339. MODULE_LICENSE("GPL");