atmel-pcm-pdc.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. * atmel-pcm.c -- ALSA PCM interface for the Atmel atmel SoC.
  3. *
  4. * Copyright (C) 2005 SAN People
  5. * Copyright (C) 2008 Atmel
  6. *
  7. * Authors: Sedji Gaouaou <sedji.gaouaou@atmel.com>
  8. *
  9. * Based on at91-pcm. by:
  10. * Frank Mandarino <fmandarino@endrelia.com>
  11. * Copyright 2006 Endrelia Technologies Inc.
  12. *
  13. * Based on pxa2xx-pcm.c by:
  14. *
  15. * Author: Nicolas Pitre
  16. * Created: Nov 30, 2004
  17. * Copyright: (C) 2004 MontaVista Software, Inc.
  18. *
  19. * This program is free software; you can redistribute it and/or modify
  20. * it under the terms of the GNU General Public License as published by
  21. * the Free Software Foundation; either version 2 of the License, or
  22. * (at your option) any later version.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU General Public License
  30. * along with this program; if not, write to the Free Software
  31. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  32. */
  33. #include <linux/module.h>
  34. #include <linux/init.h>
  35. #include <linux/platform_device.h>
  36. #include <linux/slab.h>
  37. #include <linux/dma-mapping.h>
  38. #include <linux/atmel_pdc.h>
  39. #include <linux/atmel-ssc.h>
  40. #include <sound/core.h>
  41. #include <sound/pcm.h>
  42. #include <sound/pcm_params.h>
  43. #include <sound/soc.h>
  44. #include "atmel-pcm.h"
  45. /*--------------------------------------------------------------------------*\
  46. * Hardware definition
  47. \*--------------------------------------------------------------------------*/
  48. /* TODO: These values were taken from the AT91 platform driver, check
  49. * them against real values for AT32
  50. */
  51. static const struct snd_pcm_hardware atmel_pcm_hardware = {
  52. .info = SNDRV_PCM_INFO_MMAP |
  53. SNDRV_PCM_INFO_MMAP_VALID |
  54. SNDRV_PCM_INFO_INTERLEAVED |
  55. SNDRV_PCM_INFO_PAUSE,
  56. .period_bytes_min = 32,
  57. .period_bytes_max = 8192,
  58. .periods_min = 2,
  59. .periods_max = 1024,
  60. .buffer_bytes_max = ATMEL_SSC_DMABUF_SIZE,
  61. };
  62. /*--------------------------------------------------------------------------*\
  63. * Data types
  64. \*--------------------------------------------------------------------------*/
  65. struct atmel_runtime_data {
  66. struct atmel_pcm_dma_params *params;
  67. dma_addr_t dma_buffer; /* physical address of dma buffer */
  68. dma_addr_t dma_buffer_end; /* first address beyond DMA buffer */
  69. size_t period_size;
  70. dma_addr_t period_ptr; /* physical address of next period */
  71. };
  72. /*--------------------------------------------------------------------------*\
  73. * ISR
  74. \*--------------------------------------------------------------------------*/
  75. static void atmel_pcm_dma_irq(u32 ssc_sr,
  76. struct snd_pcm_substream *substream)
  77. {
  78. struct atmel_runtime_data *prtd = substream->runtime->private_data;
  79. struct atmel_pcm_dma_params *params = prtd->params;
  80. static int count;
  81. count++;
  82. if (ssc_sr & params->mask->ssc_endbuf) {
  83. pr_warn("atmel-pcm: buffer %s on %s (SSC_SR=%#x, count=%d)\n",
  84. substream->stream == SNDRV_PCM_STREAM_PLAYBACK
  85. ? "underrun" : "overrun",
  86. params->name, ssc_sr, count);
  87. /* re-start the PDC */
  88. ssc_writex(params->ssc->regs, ATMEL_PDC_PTCR,
  89. params->mask->pdc_disable);
  90. prtd->period_ptr += prtd->period_size;
  91. if (prtd->period_ptr >= prtd->dma_buffer_end)
  92. prtd->period_ptr = prtd->dma_buffer;
  93. ssc_writex(params->ssc->regs, params->pdc->xpr,
  94. prtd->period_ptr);
  95. ssc_writex(params->ssc->regs, params->pdc->xcr,
  96. prtd->period_size / params->pdc_xfer_size);
  97. ssc_writex(params->ssc->regs, ATMEL_PDC_PTCR,
  98. params->mask->pdc_enable);
  99. }
  100. if (ssc_sr & params->mask->ssc_endx) {
  101. /* Load the PDC next pointer and counter registers */
  102. prtd->period_ptr += prtd->period_size;
  103. if (prtd->period_ptr >= prtd->dma_buffer_end)
  104. prtd->period_ptr = prtd->dma_buffer;
  105. ssc_writex(params->ssc->regs, params->pdc->xnpr,
  106. prtd->period_ptr);
  107. ssc_writex(params->ssc->regs, params->pdc->xncr,
  108. prtd->period_size / params->pdc_xfer_size);
  109. }
  110. snd_pcm_period_elapsed(substream);
  111. }
  112. /*--------------------------------------------------------------------------*\
  113. * PCM operations
  114. \*--------------------------------------------------------------------------*/
  115. static int atmel_pcm_hw_params(struct snd_pcm_substream *substream,
  116. struct snd_pcm_hw_params *params)
  117. {
  118. struct snd_pcm_runtime *runtime = substream->runtime;
  119. struct atmel_runtime_data *prtd = runtime->private_data;
  120. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  121. /* this may get called several times by oss emulation
  122. * with different params */
  123. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  124. runtime->dma_bytes = params_buffer_bytes(params);
  125. prtd->params = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  126. prtd->params->dma_intr_handler = atmel_pcm_dma_irq;
  127. prtd->dma_buffer = runtime->dma_addr;
  128. prtd->dma_buffer_end = runtime->dma_addr + runtime->dma_bytes;
  129. prtd->period_size = params_period_bytes(params);
  130. pr_debug("atmel-pcm: "
  131. "hw_params: DMA for %s initialized "
  132. "(dma_bytes=%zu, period_size=%zu)\n",
  133. prtd->params->name,
  134. runtime->dma_bytes,
  135. prtd->period_size);
  136. return 0;
  137. }
  138. static int atmel_pcm_hw_free(struct snd_pcm_substream *substream)
  139. {
  140. struct atmel_runtime_data *prtd = substream->runtime->private_data;
  141. struct atmel_pcm_dma_params *params = prtd->params;
  142. if (params != NULL) {
  143. ssc_writex(params->ssc->regs, SSC_PDC_PTCR,
  144. params->mask->pdc_disable);
  145. prtd->params->dma_intr_handler = NULL;
  146. }
  147. return 0;
  148. }
  149. static int atmel_pcm_prepare(struct snd_pcm_substream *substream)
  150. {
  151. struct atmel_runtime_data *prtd = substream->runtime->private_data;
  152. struct atmel_pcm_dma_params *params = prtd->params;
  153. ssc_writex(params->ssc->regs, SSC_IDR,
  154. params->mask->ssc_endx | params->mask->ssc_endbuf);
  155. ssc_writex(params->ssc->regs, ATMEL_PDC_PTCR,
  156. params->mask->pdc_disable);
  157. return 0;
  158. }
  159. static int atmel_pcm_trigger(struct snd_pcm_substream *substream,
  160. int cmd)
  161. {
  162. struct snd_pcm_runtime *rtd = substream->runtime;
  163. struct atmel_runtime_data *prtd = rtd->private_data;
  164. struct atmel_pcm_dma_params *params = prtd->params;
  165. int ret = 0;
  166. pr_debug("atmel-pcm:buffer_size = %ld,"
  167. "dma_area = %p, dma_bytes = %zu\n",
  168. rtd->buffer_size, rtd->dma_area, rtd->dma_bytes);
  169. switch (cmd) {
  170. case SNDRV_PCM_TRIGGER_START:
  171. prtd->period_ptr = prtd->dma_buffer;
  172. ssc_writex(params->ssc->regs, params->pdc->xpr,
  173. prtd->period_ptr);
  174. ssc_writex(params->ssc->regs, params->pdc->xcr,
  175. prtd->period_size / params->pdc_xfer_size);
  176. prtd->period_ptr += prtd->period_size;
  177. ssc_writex(params->ssc->regs, params->pdc->xnpr,
  178. prtd->period_ptr);
  179. ssc_writex(params->ssc->regs, params->pdc->xncr,
  180. prtd->period_size / params->pdc_xfer_size);
  181. pr_debug("atmel-pcm: trigger: "
  182. "period_ptr=%lx, xpr=%u, "
  183. "xcr=%u, xnpr=%u, xncr=%u\n",
  184. (unsigned long)prtd->period_ptr,
  185. ssc_readx(params->ssc->regs, params->pdc->xpr),
  186. ssc_readx(params->ssc->regs, params->pdc->xcr),
  187. ssc_readx(params->ssc->regs, params->pdc->xnpr),
  188. ssc_readx(params->ssc->regs, params->pdc->xncr));
  189. ssc_writex(params->ssc->regs, SSC_IER,
  190. params->mask->ssc_endx | params->mask->ssc_endbuf);
  191. ssc_writex(params->ssc->regs, SSC_PDC_PTCR,
  192. params->mask->pdc_enable);
  193. pr_debug("sr=%u imr=%u\n",
  194. ssc_readx(params->ssc->regs, SSC_SR),
  195. ssc_readx(params->ssc->regs, SSC_IER));
  196. break; /* SNDRV_PCM_TRIGGER_START */
  197. case SNDRV_PCM_TRIGGER_STOP:
  198. case SNDRV_PCM_TRIGGER_SUSPEND:
  199. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  200. ssc_writex(params->ssc->regs, ATMEL_PDC_PTCR,
  201. params->mask->pdc_disable);
  202. break;
  203. case SNDRV_PCM_TRIGGER_RESUME:
  204. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  205. ssc_writex(params->ssc->regs, ATMEL_PDC_PTCR,
  206. params->mask->pdc_enable);
  207. break;
  208. default:
  209. ret = -EINVAL;
  210. }
  211. return ret;
  212. }
  213. static snd_pcm_uframes_t atmel_pcm_pointer(
  214. struct snd_pcm_substream *substream)
  215. {
  216. struct snd_pcm_runtime *runtime = substream->runtime;
  217. struct atmel_runtime_data *prtd = runtime->private_data;
  218. struct atmel_pcm_dma_params *params = prtd->params;
  219. dma_addr_t ptr;
  220. snd_pcm_uframes_t x;
  221. ptr = (dma_addr_t) ssc_readx(params->ssc->regs, params->pdc->xpr);
  222. x = bytes_to_frames(runtime, ptr - prtd->dma_buffer);
  223. if (x == runtime->buffer_size)
  224. x = 0;
  225. return x;
  226. }
  227. static int atmel_pcm_open(struct snd_pcm_substream *substream)
  228. {
  229. struct snd_pcm_runtime *runtime = substream->runtime;
  230. struct atmel_runtime_data *prtd;
  231. int ret = 0;
  232. snd_soc_set_runtime_hwparams(substream, &atmel_pcm_hardware);
  233. /* ensure that buffer size is a multiple of period size */
  234. ret = snd_pcm_hw_constraint_integer(runtime,
  235. SNDRV_PCM_HW_PARAM_PERIODS);
  236. if (ret < 0)
  237. goto out;
  238. prtd = kzalloc(sizeof(struct atmel_runtime_data), GFP_KERNEL);
  239. if (prtd == NULL) {
  240. ret = -ENOMEM;
  241. goto out;
  242. }
  243. runtime->private_data = prtd;
  244. out:
  245. return ret;
  246. }
  247. static int atmel_pcm_close(struct snd_pcm_substream *substream)
  248. {
  249. struct atmel_runtime_data *prtd = substream->runtime->private_data;
  250. kfree(prtd);
  251. return 0;
  252. }
  253. static struct snd_pcm_ops atmel_pcm_ops = {
  254. .open = atmel_pcm_open,
  255. .close = atmel_pcm_close,
  256. .ioctl = snd_pcm_lib_ioctl,
  257. .hw_params = atmel_pcm_hw_params,
  258. .hw_free = atmel_pcm_hw_free,
  259. .prepare = atmel_pcm_prepare,
  260. .trigger = atmel_pcm_trigger,
  261. .pointer = atmel_pcm_pointer,
  262. .mmap = atmel_pcm_mmap,
  263. };
  264. static struct snd_soc_platform_driver atmel_soc_platform = {
  265. .ops = &atmel_pcm_ops,
  266. .pcm_new = atmel_pcm_new,
  267. .pcm_free = atmel_pcm_free,
  268. };
  269. int atmel_pcm_pdc_platform_register(struct device *dev)
  270. {
  271. return snd_soc_register_platform(dev, &atmel_soc_platform);
  272. }
  273. EXPORT_SYMBOL(atmel_pcm_pdc_platform_register);
  274. void atmel_pcm_pdc_platform_unregister(struct device *dev)
  275. {
  276. snd_soc_unregister_platform(dev);
  277. }
  278. EXPORT_SYMBOL(atmel_pcm_pdc_platform_unregister);
  279. MODULE_AUTHOR("Sedji Gaouaou <sedji.gaouaou@atmel.com>");
  280. MODULE_DESCRIPTION("Atmel PCM module");
  281. MODULE_LICENSE("GPL");