idma.c 10 KB

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