mx1_mx2-pcm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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 configuring DMA\n");
  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: failed to malloc pcm pages\n", __func__);
  186. return ret;
  187. }
  188. pr_debug("%s: snd_imx1_mx2_audio_hw_params runtime->dma_addr 0x(%x)\n",
  189. __func__, (unsigned int)runtime->dma_addr);
  190. pr_debug("%s: snd_imx1_mx2_audio_hw_params runtime->dma_area 0x(%x)\n",
  191. __func__, (unsigned int)runtime->dma_area);
  192. pr_debug("%s: snd_imx1_mx2_audio_hw_params runtime->dma_bytes 0x(%x)\n",
  193. __func__, (unsigned int)runtime->dma_bytes);
  194. return ret;
  195. }
  196. static int mx1_mx2_pcm_hw_free(struct snd_pcm_substream *substream)
  197. {
  198. struct snd_pcm_runtime *runtime = substream->runtime;
  199. struct mx1_mx2_runtime_data *prtd = runtime->private_data;
  200. imx_dma_free(prtd->dma_ch);
  201. snd_pcm_lib_free_pages(substream);
  202. return 0;
  203. }
  204. static int mx1_mx2_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  205. {
  206. struct mx1_mx2_runtime_data *prtd = substream->runtime->private_data;
  207. int ret = 0;
  208. switch (cmd) {
  209. case SNDRV_PCM_TRIGGER_START:
  210. prtd->tx_spin = 0;
  211. /* requested stream startup */
  212. prtd->active = 1;
  213. pr_debug("%s: starting dma_new_period\n", __func__);
  214. ret = dma_new_period(substream);
  215. break;
  216. case SNDRV_PCM_TRIGGER_STOP:
  217. /* requested stream shutdown */
  218. pr_debug("%s: stopping dma transfer\n", __func__);
  219. ret = audio_stop_dma(substream);
  220. break;
  221. default:
  222. ret = -EINVAL;
  223. break;
  224. }
  225. return ret;
  226. }
  227. static snd_pcm_uframes_t
  228. mx1_mx2_pcm_pointer(struct snd_pcm_substream *substream)
  229. {
  230. struct snd_pcm_runtime *runtime = substream->runtime;
  231. struct mx1_mx2_runtime_data *prtd = runtime->private_data;
  232. unsigned int offset = 0;
  233. /* tx_spin value is used here to check if a transfer is active */
  234. if (prtd->tx_spin) {
  235. offset = (runtime->period_size * (prtd->periods)) +
  236. (runtime->period_size >> 1);
  237. if (offset >= runtime->buffer_size)
  238. offset = runtime->period_size >> 1;
  239. } else {
  240. offset = (runtime->period_size * (prtd->periods));
  241. if (offset >= runtime->buffer_size)
  242. offset = 0;
  243. }
  244. pr_debug("%s: pointer offset %x\n", __func__, offset);
  245. return offset;
  246. }
  247. static int mx1_mx2_pcm_open(struct snd_pcm_substream *substream)
  248. {
  249. struct snd_pcm_runtime *runtime = substream->runtime;
  250. struct mx1_mx2_runtime_data *prtd;
  251. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  252. struct mx1_mx2_pcm_dma_params *dma_data = rtd->dai->cpu_dai->dma_data;
  253. int ret;
  254. snd_soc_set_runtime_hwparams(substream, &mx1_mx2_pcm_hardware);
  255. ret = snd_pcm_hw_constraint_integer(runtime,
  256. SNDRV_PCM_HW_PARAM_PERIODS);
  257. if (ret < 0)
  258. return ret;
  259. prtd = kzalloc(sizeof(struct mx1_mx2_runtime_data), GFP_KERNEL);
  260. if (prtd == NULL) {
  261. ret = -ENOMEM;
  262. goto out;
  263. }
  264. runtime->private_data = prtd;
  265. if (!dma_data)
  266. return -ENODEV;
  267. prtd->dma_params = dma_data;
  268. pr_debug("%s: Requesting dma channel (%s)\n", __func__,
  269. prtd->dma_params->name);
  270. prtd->dma_ch = imx_dma_request_by_prio(prtd->dma_params->name,
  271. DMA_PRIO_HIGH);
  272. if (prtd->dma_ch < 0) {
  273. printk(KERN_ERR "Error requesting dma channel\n");
  274. return ret;
  275. }
  276. imx_dma_config_burstlen(prtd->dma_ch,
  277. prtd->dma_params->watermark_level);
  278. ret = imx_dma_config_channel(prtd->dma_ch,
  279. prtd->dma_params->per_config,
  280. prtd->dma_params->mem_config,
  281. prtd->dma_params->event_id, 0);
  282. if (ret) {
  283. pr_debug(KERN_ERR "Error configuring dma channel %d\n",
  284. prtd->dma_ch);
  285. return ret;
  286. }
  287. pr_debug("%s: Setting tx dma callback function\n", __func__);
  288. ret = imx_dma_setup_handlers(prtd->dma_ch,
  289. audio_dma_irq, NULL,
  290. (void *)substream);
  291. if (ret < 0) {
  292. printk(KERN_ERR "Error setting dma callback function\n");
  293. return ret;
  294. }
  295. return 0;
  296. out:
  297. return ret;
  298. }
  299. static int mx1_mx2_pcm_close(struct snd_pcm_substream *substream)
  300. {
  301. struct snd_pcm_runtime *runtime = substream->runtime;
  302. struct mx1_mx2_runtime_data *prtd = runtime->private_data;
  303. kfree(prtd);
  304. return 0;
  305. }
  306. static int mx1_mx2_pcm_mmap(struct snd_pcm_substream *substream,
  307. struct vm_area_struct *vma)
  308. {
  309. struct snd_pcm_runtime *runtime = substream->runtime;
  310. return dma_mmap_writecombine(substream->pcm->card->dev, vma,
  311. runtime->dma_area,
  312. runtime->dma_addr,
  313. runtime->dma_bytes);
  314. }
  315. struct snd_pcm_ops mx1_mx2_pcm_ops = {
  316. .open = mx1_mx2_pcm_open,
  317. .close = mx1_mx2_pcm_close,
  318. .ioctl = snd_pcm_lib_ioctl,
  319. .hw_params = mx1_mx2_pcm_hw_params,
  320. .hw_free = mx1_mx2_pcm_hw_free,
  321. .prepare = snd_mx1_mx2_prepare,
  322. .trigger = mx1_mx2_pcm_trigger,
  323. .pointer = mx1_mx2_pcm_pointer,
  324. .mmap = mx1_mx2_pcm_mmap,
  325. };
  326. static u64 mx1_mx2_pcm_dmamask = 0xffffffff;
  327. static int mx1_mx2_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
  328. {
  329. struct snd_pcm_substream *substream = pcm->streams[stream].substream;
  330. struct snd_dma_buffer *buf = &substream->dma_buffer;
  331. size_t size = mx1_mx2_pcm_hardware.buffer_bytes_max;
  332. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  333. buf->dev.dev = pcm->card->dev;
  334. buf->private_data = NULL;
  335. /* Reserve uncached-buffered memory area for DMA */
  336. buf->area = dma_alloc_writecombine(pcm->card->dev, size,
  337. &buf->addr, GFP_KERNEL);
  338. pr_debug("%s: preallocate_dma_buffer: area=%p, addr=%p, size=%d\n",
  339. __func__, (void *) buf->area, (void *) buf->addr, size);
  340. if (!buf->area)
  341. return -ENOMEM;
  342. buf->bytes = size;
  343. return 0;
  344. }
  345. static void mx1_mx2_pcm_free_dma_buffers(struct snd_pcm *pcm)
  346. {
  347. struct snd_pcm_substream *substream;
  348. struct snd_dma_buffer *buf;
  349. int stream;
  350. for (stream = 0; stream < 2; stream++) {
  351. substream = pcm->streams[stream].substream;
  352. if (!substream)
  353. continue;
  354. buf = &substream->dma_buffer;
  355. if (!buf->area)
  356. continue;
  357. dma_free_writecombine(pcm->card->dev, buf->bytes,
  358. buf->area, buf->addr);
  359. buf->area = NULL;
  360. }
  361. }
  362. int mx1_mx2_pcm_new(struct snd_card *card, struct snd_soc_dai *dai,
  363. struct snd_pcm *pcm)
  364. {
  365. int ret = 0;
  366. if (!card->dev->dma_mask)
  367. card->dev->dma_mask = &mx1_mx2_pcm_dmamask;
  368. if (!card->dev->coherent_dma_mask)
  369. card->dev->coherent_dma_mask = 0xffffffff;
  370. if (dai->playback.channels_min) {
  371. ret = mx1_mx2_pcm_preallocate_dma_buffer(pcm,
  372. SNDRV_PCM_STREAM_PLAYBACK);
  373. pr_debug("%s: preallocate playback buffer\n", __func__);
  374. if (ret)
  375. goto out;
  376. }
  377. if (dai->capture.channels_min) {
  378. ret = mx1_mx2_pcm_preallocate_dma_buffer(pcm,
  379. SNDRV_PCM_STREAM_CAPTURE);
  380. pr_debug("%s: preallocate capture buffer\n", __func__);
  381. if (ret)
  382. goto out;
  383. }
  384. out:
  385. return ret;
  386. }
  387. struct snd_soc_platform mx1_mx2_soc_platform = {
  388. .name = "mx1_mx2-audio",
  389. .pcm_ops = &mx1_mx2_pcm_ops,
  390. .pcm_new = mx1_mx2_pcm_new,
  391. .pcm_free = mx1_mx2_pcm_free_dma_buffers,
  392. };
  393. EXPORT_SYMBOL_GPL(mx1_mx2_soc_platform);
  394. static int __init mx1_mx2_soc_platform_init(void)
  395. {
  396. return snd_soc_register_platform(&mx1_mx2_soc_platform);
  397. }
  398. module_init(mx1_mx2_soc_platform_init);
  399. static void __exit mx1_mx2_soc_platform_exit(void)
  400. {
  401. snd_soc_unregister_platform(&mx1_mx2_soc_platform);
  402. }
  403. module_exit(mx1_mx2_soc_platform_exit);
  404. MODULE_AUTHOR("Javier Martin, javier.martin@vista-silicon.com");
  405. MODULE_DESCRIPTION("Freescale i.MX2x, i.MX1x PCM DMA module");
  406. MODULE_LICENSE("GPL");