mx1_mx2-pcm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /*
  2. * mx1_mx2-pcm.c -- ALSA SoC interface for Freescale i.MX1x, i.MX2x CPUs
  3. *
  4. * Copyright 2009 Vista Silicon S.L.
  5. * Author: Javier Martin
  6. * javier.martin@vista-silicon.com
  7. *
  8. * Based on mxc-pcm.c by Liam Girdwood.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. *
  14. *
  15. */
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/slab.h>
  20. #include <linux/dma-mapping.h>
  21. #include <sound/core.h>
  22. #include <sound/pcm.h>
  23. #include <sound/pcm_params.h>
  24. #include <sound/soc.h>
  25. #include <asm/dma.h>
  26. #include <mach/hardware.h>
  27. #include <mach/dma-mx1-mx2.h>
  28. #include "mx1_mx2-pcm.h"
  29. static const struct snd_pcm_hardware mx1_mx2_pcm_hardware = {
  30. .info = (SNDRV_PCM_INFO_INTERLEAVED |
  31. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  32. SNDRV_PCM_INFO_MMAP |
  33. SNDRV_PCM_INFO_MMAP_VALID),
  34. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  35. .buffer_bytes_max = 32 * 1024,
  36. .period_bytes_min = 64,
  37. .period_bytes_max = 8 * 1024,
  38. .periods_min = 2,
  39. .periods_max = 255,
  40. .fifo_size = 0,
  41. };
  42. struct mx1_mx2_runtime_data {
  43. int dma_ch;
  44. int active;
  45. unsigned int period;
  46. unsigned int periods;
  47. int tx_spin;
  48. spinlock_t dma_lock;
  49. struct mx1_mx2_pcm_dma_params *dma_params;
  50. };
  51. /**
  52. * This function stops the current dma transfer for playback
  53. * and clears the dma pointers.
  54. *
  55. * @param substream pointer to the structure of the current stream.
  56. *
  57. */
  58. static int audio_stop_dma(struct snd_pcm_substream *substream)
  59. {
  60. struct snd_pcm_runtime *runtime = substream->runtime;
  61. struct mx1_mx2_runtime_data *prtd = runtime->private_data;
  62. unsigned long flags;
  63. spin_lock_irqsave(&prtd->dma_lock, flags);
  64. pr_debug("%s\n", __func__);
  65. prtd->active = 0;
  66. prtd->period = 0;
  67. prtd->periods = 0;
  68. /* this stops the dma channel and clears the buffer ptrs */
  69. imx_dma_disable(prtd->dma_ch);
  70. spin_unlock_irqrestore(&prtd->dma_lock, flags);
  71. return 0;
  72. }
  73. /**
  74. * This function is called whenever a new audio block needs to be
  75. * transferred to the codec. The function receives the address and the size
  76. * of the new block and start a new DMA transfer.
  77. *
  78. * @param substream pointer to the structure of the current stream.
  79. *
  80. */
  81. static int dma_new_period(struct snd_pcm_substream *substream)
  82. {
  83. struct snd_pcm_runtime *runtime = substream->runtime;
  84. struct mx1_mx2_runtime_data *prtd = runtime->private_data;
  85. unsigned int dma_size;
  86. unsigned int offset;
  87. int ret = 0;
  88. dma_addr_t mem_addr;
  89. unsigned int dev_addr;
  90. if (prtd->active) {
  91. dma_size = frames_to_bytes(runtime, runtime->period_size);
  92. offset = dma_size * prtd->period;
  93. pr_debug("%s: period (%d) out of (%d)\n", __func__,
  94. prtd->period,
  95. runtime->periods);
  96. pr_debug("period_size %d frames\n offset %d bytes\n",
  97. (unsigned int)runtime->period_size,
  98. offset);
  99. pr_debug("dma_size %d bytes\n", dma_size);
  100. snd_BUG_ON(dma_size > mx1_mx2_pcm_hardware.period_bytes_max);
  101. mem_addr = (dma_addr_t)(runtime->dma_addr + offset);
  102. dev_addr = prtd->dma_params->per_address;
  103. pr_debug("%s: mem_addr is %x\n dev_addr is %x\n",
  104. __func__, mem_addr, dev_addr);
  105. ret = imx_dma_setup_single(prtd->dma_ch, mem_addr,
  106. dma_size, dev_addr,
  107. prtd->dma_params->transfer_type);
  108. if (ret < 0) {
  109. printk(KERN_ERR "Error %d configuring DMA\n", ret);
  110. return ret;
  111. }
  112. imx_dma_enable(prtd->dma_ch);
  113. pr_debug("%s: transfer enabled\nmem_addr = %x\n",
  114. __func__, (unsigned int) mem_addr);
  115. pr_debug("dev_addr = %x\ndma_size = %d\n",
  116. (unsigned int) dev_addr, dma_size);
  117. prtd->tx_spin = 1; /* FGA little trick to retrieve DMA pos */
  118. prtd->period++;
  119. prtd->period %= runtime->periods;
  120. }
  121. return ret;
  122. }
  123. /**
  124. * This is a callback which will be called
  125. * when a TX transfer finishes. The call occurs
  126. * in interrupt context.
  127. *
  128. * @param dat pointer to the structure of the current stream.
  129. *
  130. */
  131. static void audio_dma_irq(int channel, void *data)
  132. {
  133. struct snd_pcm_substream *substream;
  134. struct snd_pcm_runtime *runtime;
  135. struct mx1_mx2_runtime_data *prtd;
  136. unsigned int dma_size;
  137. unsigned int previous_period;
  138. unsigned int offset;
  139. substream = data;
  140. runtime = substream->runtime;
  141. prtd = runtime->private_data;
  142. previous_period = prtd->periods;
  143. dma_size = frames_to_bytes(runtime, runtime->period_size);
  144. offset = dma_size * previous_period;
  145. prtd->tx_spin = 0;
  146. prtd->periods++;
  147. prtd->periods %= runtime->periods;
  148. pr_debug("%s: irq per %d offset %x\n", __func__, prtd->periods, offset);
  149. /*
  150. * If we are getting a callback for an active stream then we inform
  151. * the PCM middle layer we've finished a period
  152. */
  153. if (prtd->active)
  154. snd_pcm_period_elapsed(substream);
  155. /*
  156. * Trig next DMA transfer
  157. */
  158. dma_new_period(substream);
  159. }
  160. /**
  161. * This function configures the hardware to allow audio
  162. * playback operations. It is called by ALSA framework.
  163. *
  164. * @param substream pointer to the structure of the current stream.
  165. *
  166. * @return 0 on success, -1 otherwise.
  167. */
  168. static int
  169. snd_mx1_mx2_prepare(struct snd_pcm_substream *substream)
  170. {
  171. struct snd_pcm_runtime *runtime = substream->runtime;
  172. struct mx1_mx2_runtime_data *prtd = runtime->private_data;
  173. prtd->period = 0;
  174. prtd->periods = 0;
  175. return 0;
  176. }
  177. static int mx1_mx2_pcm_hw_params(struct snd_pcm_substream *substream,
  178. struct snd_pcm_hw_params *hw_params)
  179. {
  180. struct snd_pcm_runtime *runtime = substream->runtime;
  181. int ret;
  182. ret = snd_pcm_lib_malloc_pages(substream,
  183. params_buffer_bytes(hw_params));
  184. if (ret < 0) {
  185. printk(KERN_ERR "%s: Error %d failed to malloc pcm pages \n",
  186. __func__, ret);
  187. return ret;
  188. }
  189. pr_debug("%s: snd_imx1_mx2_audio_hw_params runtime->dma_addr 0x(%x)\n",
  190. __func__, (unsigned int)runtime->dma_addr);
  191. pr_debug("%s: snd_imx1_mx2_audio_hw_params runtime->dma_area 0x(%x)\n",
  192. __func__, (unsigned int)runtime->dma_area);
  193. pr_debug("%s: snd_imx1_mx2_audio_hw_params runtime->dma_bytes 0x(%x)\n",
  194. __func__, (unsigned int)runtime->dma_bytes);
  195. return ret;
  196. }
  197. static int mx1_mx2_pcm_hw_free(struct snd_pcm_substream *substream)
  198. {
  199. struct snd_pcm_runtime *runtime = substream->runtime;
  200. struct mx1_mx2_runtime_data *prtd = runtime->private_data;
  201. imx_dma_free(prtd->dma_ch);
  202. snd_pcm_lib_free_pages(substream);
  203. return 0;
  204. }
  205. static int mx1_mx2_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  206. {
  207. struct mx1_mx2_runtime_data *prtd = substream->runtime->private_data;
  208. int ret = 0;
  209. switch (cmd) {
  210. case SNDRV_PCM_TRIGGER_START:
  211. prtd->tx_spin = 0;
  212. /* requested stream startup */
  213. prtd->active = 1;
  214. pr_debug("%s: starting dma_new_period\n", __func__);
  215. ret = dma_new_period(substream);
  216. break;
  217. case SNDRV_PCM_TRIGGER_STOP:
  218. /* requested stream shutdown */
  219. pr_debug("%s: stopping dma transfer\n", __func__);
  220. ret = audio_stop_dma(substream);
  221. break;
  222. default:
  223. ret = -EINVAL;
  224. break;
  225. }
  226. return ret;
  227. }
  228. static snd_pcm_uframes_t
  229. mx1_mx2_pcm_pointer(struct snd_pcm_substream *substream)
  230. {
  231. struct snd_pcm_runtime *runtime = substream->runtime;
  232. struct mx1_mx2_runtime_data *prtd = runtime->private_data;
  233. unsigned int offset = 0;
  234. /* tx_spin value is used here to check if a transfer is active */
  235. if (prtd->tx_spin) {
  236. offset = (runtime->period_size * (prtd->periods)) +
  237. (runtime->period_size >> 1);
  238. if (offset >= runtime->buffer_size)
  239. offset = runtime->period_size >> 1;
  240. } else {
  241. offset = (runtime->period_size * (prtd->periods));
  242. if (offset >= runtime->buffer_size)
  243. offset = 0;
  244. }
  245. pr_debug("%s: pointer offset %x\n", __func__, offset);
  246. return offset;
  247. }
  248. static int mx1_mx2_pcm_open(struct snd_pcm_substream *substream)
  249. {
  250. struct snd_pcm_runtime *runtime = substream->runtime;
  251. struct mx1_mx2_runtime_data *prtd;
  252. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  253. struct mx1_mx2_pcm_dma_params *dma_data = rtd->dai->cpu_dai->dma_data;
  254. int ret;
  255. snd_soc_set_runtime_hwparams(substream, &mx1_mx2_pcm_hardware);
  256. ret = snd_pcm_hw_constraint_integer(runtime,
  257. SNDRV_PCM_HW_PARAM_PERIODS);
  258. if (ret < 0)
  259. return ret;
  260. prtd = kzalloc(sizeof(struct mx1_mx2_runtime_data), GFP_KERNEL);
  261. if (prtd == NULL) {
  262. ret = -ENOMEM;
  263. goto out;
  264. }
  265. runtime->private_data = prtd;
  266. if (!dma_data)
  267. return -ENODEV;
  268. prtd->dma_params = dma_data;
  269. pr_debug("%s: Requesting dma channel (%s)\n", __func__,
  270. prtd->dma_params->name);
  271. prtd->dma_ch = imx_dma_request_by_prio(prtd->dma_params->name,
  272. DMA_PRIO_HIGH);
  273. if (prtd->dma_ch < 0) {
  274. printk(KERN_ERR "Error %d requesting dma channel\n", ret);
  275. return ret;
  276. }
  277. imx_dma_config_burstlen(prtd->dma_ch,
  278. prtd->dma_params->watermark_level);
  279. ret = imx_dma_config_channel(prtd->dma_ch,
  280. prtd->dma_params->per_config,
  281. prtd->dma_params->mem_config,
  282. prtd->dma_params->event_id, 0);
  283. if (ret) {
  284. pr_debug(KERN_ERR "Error %d configuring dma channel %d\n",
  285. ret, prtd->dma_ch);
  286. return ret;
  287. }
  288. pr_debug("%s: Setting tx dma callback function\n", __func__);
  289. ret = imx_dma_setup_handlers(prtd->dma_ch,
  290. audio_dma_irq, NULL,
  291. (void *)substream);
  292. if (ret < 0) {
  293. printk(KERN_ERR "Error %d setting dma callback function\n", ret);
  294. return ret;
  295. }
  296. return 0;
  297. out:
  298. return ret;
  299. }
  300. static int mx1_mx2_pcm_close(struct snd_pcm_substream *substream)
  301. {
  302. struct snd_pcm_runtime *runtime = substream->runtime;
  303. struct mx1_mx2_runtime_data *prtd = runtime->private_data;
  304. kfree(prtd);
  305. return 0;
  306. }
  307. static int mx1_mx2_pcm_mmap(struct snd_pcm_substream *substream,
  308. struct vm_area_struct *vma)
  309. {
  310. struct snd_pcm_runtime *runtime = substream->runtime;
  311. return dma_mmap_writecombine(substream->pcm->card->dev, vma,
  312. runtime->dma_area,
  313. runtime->dma_addr,
  314. runtime->dma_bytes);
  315. }
  316. static struct snd_pcm_ops mx1_mx2_pcm_ops = {
  317. .open = mx1_mx2_pcm_open,
  318. .close = mx1_mx2_pcm_close,
  319. .ioctl = snd_pcm_lib_ioctl,
  320. .hw_params = mx1_mx2_pcm_hw_params,
  321. .hw_free = mx1_mx2_pcm_hw_free,
  322. .prepare = snd_mx1_mx2_prepare,
  323. .trigger = mx1_mx2_pcm_trigger,
  324. .pointer = mx1_mx2_pcm_pointer,
  325. .mmap = mx1_mx2_pcm_mmap,
  326. };
  327. static u64 mx1_mx2_pcm_dmamask = 0xffffffff;
  328. static int mx1_mx2_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
  329. {
  330. struct snd_pcm_substream *substream = pcm->streams[stream].substream;
  331. struct snd_dma_buffer *buf = &substream->dma_buffer;
  332. size_t size = mx1_mx2_pcm_hardware.buffer_bytes_max;
  333. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  334. buf->dev.dev = pcm->card->dev;
  335. buf->private_data = NULL;
  336. /* Reserve uncached-buffered memory area for DMA */
  337. buf->area = dma_alloc_writecombine(pcm->card->dev, size,
  338. &buf->addr, GFP_KERNEL);
  339. pr_debug("%s: preallocate_dma_buffer: area=%p, addr=%p, size=%d\n",
  340. __func__, (void *) buf->area, (void *) buf->addr, size);
  341. if (!buf->area)
  342. return -ENOMEM;
  343. buf->bytes = size;
  344. return 0;
  345. }
  346. static void mx1_mx2_pcm_free_dma_buffers(struct snd_pcm *pcm)
  347. {
  348. struct snd_pcm_substream *substream;
  349. struct snd_dma_buffer *buf;
  350. int stream;
  351. for (stream = 0; stream < 2; stream++) {
  352. substream = pcm->streams[stream].substream;
  353. if (!substream)
  354. continue;
  355. buf = &substream->dma_buffer;
  356. if (!buf->area)
  357. continue;
  358. dma_free_writecombine(pcm->card->dev, buf->bytes,
  359. buf->area, buf->addr);
  360. buf->area = NULL;
  361. }
  362. }
  363. static int mx1_mx2_pcm_new(struct snd_card *card, struct snd_soc_dai *dai,
  364. struct snd_pcm *pcm)
  365. {
  366. int ret = 0;
  367. if (!card->dev->dma_mask)
  368. card->dev->dma_mask = &mx1_mx2_pcm_dmamask;
  369. if (!card->dev->coherent_dma_mask)
  370. card->dev->coherent_dma_mask = 0xffffffff;
  371. if (dai->playback.channels_min) {
  372. ret = mx1_mx2_pcm_preallocate_dma_buffer(pcm,
  373. SNDRV_PCM_STREAM_PLAYBACK);
  374. pr_debug("%s: preallocate playback buffer\n", __func__);
  375. if (ret)
  376. goto out;
  377. }
  378. if (dai->capture.channels_min) {
  379. ret = mx1_mx2_pcm_preallocate_dma_buffer(pcm,
  380. SNDRV_PCM_STREAM_CAPTURE);
  381. pr_debug("%s: preallocate capture buffer\n", __func__);
  382. if (ret)
  383. goto out;
  384. }
  385. out:
  386. return ret;
  387. }
  388. struct snd_soc_platform mx1_mx2_soc_platform = {
  389. .name = "mx1_mx2-audio",
  390. .pcm_ops = &mx1_mx2_pcm_ops,
  391. .pcm_new = mx1_mx2_pcm_new,
  392. .pcm_free = mx1_mx2_pcm_free_dma_buffers,
  393. };
  394. EXPORT_SYMBOL_GPL(mx1_mx2_soc_platform);
  395. static int __init mx1_mx2_soc_platform_init(void)
  396. {
  397. return snd_soc_register_platform(&mx1_mx2_soc_platform);
  398. }
  399. module_init(mx1_mx2_soc_platform_init);
  400. static void __exit mx1_mx2_soc_platform_exit(void)
  401. {
  402. snd_soc_unregister_platform(&mx1_mx2_soc_platform);
  403. }
  404. module_exit(mx1_mx2_soc_platform_exit);
  405. MODULE_AUTHOR("Javier Martin, javier.martin@vista-silicon.com");
  406. MODULE_DESCRIPTION("Freescale i.MX2x, i.MX1x PCM DMA module");
  407. MODULE_LICENSE("GPL");